diff --git a/service/systemd/mosquitto.service.notify b/service/systemd/mosquitto.service.notify index 06772ddadd..e85daa62a3 100644 --- a/service/systemd/mosquitto.service.notify +++ b/service/systemd/mosquitto.service.notify @@ -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 diff --git a/src/loop.c b/src/loop.c index ec9533984e..277b59f4c1 100644 --- a/src/loop.c +++ b/src/loop.c @@ -45,6 +45,10 @@ SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause # include #endif +#ifdef WITH_SYSTEMD +# include +#endif + #include "mosquitto_broker_internal.h" #include "memory_mosq.h" #include "mqtt_protocol.h" @@ -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)); @@ -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