Skip to content

Commit 48a867c

Browse files
authored
systemd-watchdog: improve notifications to watchdog (#595)
system will now be notified on send timeouts and when event handling is running for a long time Signed-off-by: Alexander Mohr <[email protected]>
1 parent d626b15 commit 48a867c

File tree

6 files changed

+50
-26
lines changed

6 files changed

+50
-26
lines changed

src/daemon/dlt-daemon.c

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,6 @@ static int dlt_daemon_check_numeric_setting(char *token,
9595
char *value,
9696
unsigned long *data);
9797

98-
#ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
99-
static uint32_t watchdog_trigger_interval; /* watchdog trigger interval in [s] */
100-
#endif
101-
10298
/* used in main event loop and signal handler */
10399
int g_exit = 0;
104100

@@ -1268,7 +1264,7 @@ int main(int argc, char *argv[])
12681264
if (watchdogUSec)
12691265
watchdogTimeoutSeconds = atoi(watchdogUSec) / 2000000;
12701266

1271-
watchdog_trigger_interval = watchdogTimeoutSeconds;
1267+
daemon.watchdog_trigger_interval = watchdogTimeoutSeconds;
12721268
create_timer_fd(&daemon_local,
12731269
watchdogTimeoutSeconds,
12741270
watchdogTimeoutSeconds,
@@ -3756,7 +3752,7 @@ int dlt_daemon_send_ringbuffer_to_client(DltDaemon *daemon, DltDaemonLocal *daem
37563752
static uint8_t data[DLT_DAEMON_RCVBUFSIZE];
37573753
int length;
37583754
#ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
3759-
uint32_t curr_time;
3755+
uint32_t curr_time = 0U;
37603756
#endif
37613757

37623758
PRINT_FUNCTION_VERBOSE(verbose);
@@ -3771,24 +3767,9 @@ int dlt_daemon_send_ringbuffer_to_client(DltDaemon *daemon, DltDaemonLocal *daem
37713767
return DLT_DAEMON_ERROR_OK;
37723768
}
37733769

3774-
#ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
3775-
3776-
if (sd_notify(0, "WATCHDOG=1") < 0)
3777-
dlt_vlog(LOG_WARNING, "Could not reset systemd watchdog: %s\n", strerror(errno));
3778-
3779-
curr_time = dlt_uptime();
3780-
#endif
3781-
37823770
while ((length = dlt_buffer_copy(&(daemon->client_ringbuffer), data, sizeof(data))) > 0) {
37833771
#ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
3784-
3785-
if ((dlt_uptime() - curr_time) / 10000 >= watchdog_trigger_interval) {
3786-
if (sd_notify(0, "WATCHDOG=1") < 0)
3787-
dlt_log(LOG_WARNING, "Could not reset systemd watchdog\n");
3788-
3789-
curr_time = dlt_uptime();
3790-
}
3791-
3772+
dlt_daemon_trigger_systemd_watchdog_if_necessary(&curr_time, daemon->watchdog_trigger_interval);
37923773
#endif
37933774

37943775
if ((ret =

src/daemon/dlt_daemon_common.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@
8686
#include "dlt_daemon_socket.h"
8787
#include "dlt_daemon_serial.h"
8888

89+
#ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
90+
# include <systemd/sd-daemon.h>
91+
#endif
92+
8993
char *app_recv_buffer = NULL; /* pointer to receiver buffer for application msges */
9094

9195
static int dlt_daemon_cmp_apid(const void *m1, const void *m2)
@@ -1843,3 +1847,14 @@ void dlt_daemon_change_state(DltDaemon *daemon, DltDaemonState newState)
18431847
break;
18441848
}
18451849
}
1850+
1851+
#ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
1852+
void dlt_daemon_trigger_systemd_watchdog_if_necessary(unsigned int* last_trigger_time, unsigned int watchdog_trigger_interval) {
1853+
unsigned int uptime = dlt_uptime();
1854+
if ((uptime - *last_trigger_time) / 10000 >= watchdog_trigger_interval) {
1855+
if (sd_notify(0, "WATCHDOG=1") < 0)
1856+
dlt_vlog(LOG_WARNING, "%s: Could not reset systemd watchdog\n", __func__);
1857+
*last_trigger_time = uptime;
1858+
}
1859+
}
1860+
#endif

src/daemon/dlt_daemon_common.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ typedef struct
200200
#ifdef DLT_SYSTEMD_WATCHDOG_ENFORCE_MSG_RX_ENABLE
201201
int received_message_since_last_watchdog_interval;
202202
#endif
203+
#ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
204+
unsigned int watchdog_trigger_interval; /* watchdog trigger interval in [s] */
205+
#endif
203206
#ifdef DLT_LOG_LEVEL_APP_CONFIG
204207
DltDaemonContextLogSettings *app_id_log_level_settings; /**< Settings for app id specific log levels */
205208
int num_app_id_log_level_settings; /** < count of log level settings */
@@ -563,6 +566,10 @@ void dlt_daemon_control_reset_to_factory_default(DltDaemon *daemon,
563566
*/
564567
void dlt_daemon_change_state(DltDaemon *daemon, DltDaemonState newState);
565568

569+
#ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
570+
void dlt_daemon_trigger_systemd_watchdog_if_necessary(unsigned int* last_trigger_time, unsigned int watchdog_trigger_interval);
571+
#endif
572+
566573
# ifdef __cplusplus
567574
}
568575
# endif

src/daemon/dlt_daemon_event_handler.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "dlt_daemon_connection_types.h"
4343
#include "dlt_daemon_event_handler.h"
4444
#include "dlt_daemon_event_handler_types.h"
45+
#include "dlt_daemon_common.h"
4546

4647
/**
4748
* \def DLT_EV_TIMEOUT_MSEC
@@ -205,6 +206,10 @@ int dlt_daemon_handle_event(DltEventHandler *pEvent,
205206
return ret;
206207
}
207208

209+
#ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
210+
unsigned int start_time = dlt_uptime();
211+
#endif
212+
208213
for (i = 0; i < pEvent->nfds; i++) {
209214
int fd = 0;
210215
DltConnection *con = NULL;
@@ -248,7 +253,8 @@ int dlt_daemon_handle_event(DltEventHandler *pEvent,
248253
if (!callback) {
249254
dlt_vlog(LOG_CRIT, "Unable to find function for %u handle type.\n",
250255
type);
251-
return -1;
256+
/* keep handling remaining events */
257+
continue;
252258
}
253259

254260
/* From now on, callback is correct */
@@ -260,6 +266,9 @@ int dlt_daemon_handle_event(DltEventHandler *pEvent,
260266
type);
261267
return -1;
262268
}
269+
#ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
270+
dlt_daemon_trigger_systemd_watchdog_if_necessary(&start_time, daemon->watchdog_trigger_interval);
271+
#endif
263272
}
264273

265274
return 0;

src/daemon/dlt_daemon_socket.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
#include <linux/stat.h>
4949
#endif
5050

51+
#ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
52+
#include <systemd/sd-daemon.h>
53+
#endif
54+
5155
#include "dlt_types.h"
5256
#include "dlt-daemon.h"
5357
#include "dlt-daemon_cfg.h"
@@ -224,6 +228,14 @@ int dlt_daemon_socket_sendreliable(int sock, void *data_buffer, int message_size
224228
if (ret < 0) {
225229
dlt_vlog(LOG_WARNING,
226230
"%s: socket send failed [errno: %d]!\n", __func__, errno);
231+
#ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
232+
/* notify systemd here that we are still alive
233+
* otherwise we might miss notifying the watchdog when
234+
* the watchdog interval is small and multiple timeouts occur back to back
235+
*/
236+
if (sd_notify(0, "WATCHDOG=1") < 0)
237+
dlt_vlog(LOG_WARNING, "%s: Could not reset systemd watchdog\n", __func__);
238+
#endif
227239
return DLT_DAEMON_ERROR_SEND_FAILED;
228240
} else {
229241
data_sent += ret;

tests/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ else()
1919
set(LIBRARIES socket)
2020
endif()
2121

22-
if(WITH_SYSTEMD)
23-
set(SYSTEMD_LIBS systemd)
24-
endif(WITH_SYSTEMD)
22+
if (WITH_SYSTEMD OR WITH_SYSTEMD_SOCKET_ACTIVATION OR WITH_SYSTEMD_WATCHDOG)
23+
set(LIBRARIES ${LIBRARIES} systemd)
24+
endif()
2525

2626
set(DLT_LIBRARIES dlt ${GTEST_LIBS} ${LIBRARIES})
2727
set(DLT_DAEMON_LIBRARIES dlt_daemon ${GTEST_LIBS} ${LIBRARIES} ${SYSTEMD_LIBS})

0 commit comments

Comments
 (0)