Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add systemd watchdog support #3158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions service/systemd/mosquitto.service.notify
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Wants=network.target

[Service]
Type=notify
WatchdogSec=3min
NotifyAccess=main
ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
ExecReload=/bin/kill -HUP $MAINPID
Expand Down
28 changes: 28 additions & 0 deletions src/loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
# include <libwebsockets.h>
#endif

#ifdef WITH_SYSTEMD
# include <systemd/sd-daemon.h>
#endif

#include "mosquitto_broker_internal.h"
#include "memory_mosq.h"
#include "mqtt_protocol.h"
Expand Down Expand Up @@ -170,9 +174,23 @@ int mosquitto_main_loop(struct mosquitto__listener_sock *listensock, int listens
#endif
#ifdef WITH_WEBSOCKETS
int i;
#endif
#ifdef WITH_SYSTEMD
char *watchdog_usec = getenv("WATCHDOG_USEC");
time_t next_ping = mosquitto_time();
time_t ping_sec = 0;
#endif
int rc;

#ifdef WITH_SYSTEMD
if(watchdog_usec){
char *endptr = NULL;
long usec = strtol(watchdog_usec, &endptr, 10);
if(watchdog_usec[0] != '\0' && endptr[0] == '\0' && usec > 0){
ping_sec = (usec / 1000000) / 2;
}
}
#endif

#if defined(WITH_WEBSOCKETS) && LWS_LIBRARY_VERSION_NUMBER == 3002000
memset(&sul, 0, sizeof(struct lws_sorted_usec_list));
Expand All @@ -187,6 +205,16 @@ int mosquitto_main_loop(struct mosquitto__listener_sock *listensock, int listens
#endif

while(run){
#ifdef WITH_SYSTEMD
if(ping_sec){
time_t now = mosquitto_time();
if(now > next_ping){
sd_notify(0, "WATCHDOG=1");
next_ping = now + ping_sec;
}
}
#endif

queue_plugin_msgs();
context__free_disused();
#ifdef WITH_SYS_TREE
Expand Down