From cf37e80e6bc2068cf5d77d8373c5bb0ba34f5fc6 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Tue, 25 Feb 2025 11:50:02 +0000 Subject: [PATCH] Add support for systemd watchdog. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a reworked version of https://github.com/eclipse-mosquitto/mosquitto/pull/3158 by Gaƫl PORTAY. --- ChangeLog.txt | 3 +- src/CMakeLists.txt | 1 + src/Makefile | 1 + src/loop.c | 2 ++ src/mosquitto_broker_internal.h | 6 ++++ src/watchdog.c | 63 +++++++++++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/watchdog.c diff --git a/ChangeLog.txt b/ChangeLog.txt index f766a9cf..6a531236 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -77,7 +77,8 @@ Broker: - Add suport for PROXY protocol v1 and v2. - Log message if a client attempts to connect with TLS to a non-TLS listener. - Add `listener_allow_anonymous` option. -- Add `listener_auto_id_prefix` ` option. +- Add `listener_auto_id_prefix` option. +- Add support for systemd watchdog. Plugins / plugin interface: - Add persist-sqlite plugin. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c6ba0692..f4a09d6e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -73,6 +73,7 @@ add_executable(mosquitto ../lib/tls_mosq.c topic_tok.c ../lib/util_mosq.c ../lib/util_mosq.h + watchdog.c websockets.c will_delay.c ../lib/will_mosq.c ../lib/will_mosq.h diff --git a/src/Makefile b/src/Makefile index 0c65d5c9..65cefa70 100644 --- a/src/Makefile +++ b/src/Makefile @@ -109,6 +109,7 @@ OBJS= mosquitto.o \ subs.o \ sys_tree.o \ topic_tok.o \ + watchdog.o \ websockets.o \ will_delay.o \ xtreport.o diff --git a/src/loop.c b/src/loop.c index 45a8fe09..4c8a0374 100644 --- a/src/loop.c +++ b/src/loop.c @@ -170,6 +170,7 @@ int mosquitto_main_loop(struct mosquitto__listener_sock *listensock, int listens int rc; + watchdog__init(); #if defined(WITH_WEBSOCKETS) && WITH_WEBSOCKETS == WS_IS_LWS && LWS_LIBRARY_VERSION_NUMBER == 3002000 memset(&sul, 0, sizeof(struct lws_sorted_usec_list)); #endif @@ -194,6 +195,7 @@ int mosquitto_main_loop(struct mosquitto__listener_sock *listensock, int listens #endif keepalive__check(); + watchdog__check(); #ifdef WITH_BRIDGE bridge_check(); diff --git a/src/mosquitto_broker_internal.h b/src/mosquitto_broker_internal.h index bdcbe72b..a543b066 100644 --- a/src/mosquitto_broker_internal.h +++ b/src/mosquitto_broker_internal.h @@ -949,6 +949,12 @@ void service_run(char *name); DWORD WINAPI SigThreadProc(void* data); #endif +/* ============================================================ + * Watchdog + * ============================================================ */ +void watchdog__init(void); +void watchdog__check(void); + /* ============================================================ * Websockets related functions * ============================================================ */ diff --git a/src/watchdog.c b/src/watchdog.c new file mode 100644 index 00000000..c6fcdc2d --- /dev/null +++ b/src/watchdog.c @@ -0,0 +1,63 @@ +/* +Copyright (c) 2009-2020 Roger Light + +All rights reserved. This program and the accompanying materials +are made available under the terms of the Eclipse Public License 2.0 +and Eclipse Distribution License v1.0 which accompany this distribution. + +The Eclipse Public License is available at + https://www.eclipse.org/legal/epl-2.0/ +and the Eclipse Distribution License is available at + http://www.eclipse.org/org/documents/edl-v10.php. + +SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause + +Contributors: + Roger Light - initial implementation and documentation. + Tatsuzo Osawa - Add epoll. +*/ + +#include "config.h" + +#include +#include "mosquitto.h" + +#ifdef WITH_SYSTEMD +# include +#endif + +#ifdef WITH_SYSTEMD +static time_t next_ping = 0; +static time_t ping_sec = 0; +#endif + +void watchdog__init(void) +{ +#ifdef WITH_SYSTEMD + char *watchdog_usec = getenv("WATCHDOG_USEC"); + time_t next_ping = mosquitto_time(); + time_t ping_sec = 0; + + 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; + } + next_ping = mosquito_time(); + } +#endif +} + +void watchdog__check(void) +{ +#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 +}