/src/mosquitto/src/watchdog.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | Copyright (c) 2009-2020 Roger Light <roger@atchoo.org> |
3 | | |
4 | | All rights reserved. This program and the accompanying materials |
5 | | are made available under the terms of the Eclipse Public License 2.0 |
6 | | and Eclipse Distribution License v1.0 which accompany this distribution. |
7 | | |
8 | | The Eclipse Public License is available at |
9 | | https://www.eclipse.org/legal/epl-2.0/ |
10 | | and the Eclipse Distribution License is available at |
11 | | http://www.eclipse.org/org/documents/edl-v10.php. |
12 | | |
13 | | SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
14 | | |
15 | | Contributors: |
16 | | Roger Light - initial implementation and documentation. |
17 | | Tatsuzo Osawa - Add epoll. |
18 | | */ |
19 | | |
20 | | #include "config.h" |
21 | | |
22 | | #include <stdlib.h> |
23 | | #include <time.h> |
24 | | #include "mosquitto.h" |
25 | | |
26 | | #ifdef WITH_SYSTEMD |
27 | | # include <systemd/sd-daemon.h> |
28 | | #endif |
29 | | |
30 | | #ifdef WITH_SYSTEMD |
31 | | static time_t next_ping = 0; |
32 | | static time_t ping_sec = 0; |
33 | | #endif |
34 | | |
35 | | void watchdog__init(void) |
36 | 0 | { |
37 | | #ifdef WITH_SYSTEMD |
38 | | char *watchdog_usec = getenv("WATCHDOG_USEC"); |
39 | | next_ping = mosquitto_time(); |
40 | | ping_sec = 0; |
41 | | |
42 | | if(watchdog_usec){ |
43 | | char *endptr = NULL; |
44 | | long usec = strtol(watchdog_usec, &endptr, 10); |
45 | | if(watchdog_usec[0] != '\0' && endptr[0] == '\0' && usec > 0){ |
46 | | ping_sec = (usec / 1000000) / 2; |
47 | | } |
48 | | next_ping = mosquitto_time(); |
49 | | } |
50 | | #endif |
51 | 0 | } |
52 | | |
53 | | void watchdog__check(void) |
54 | 0 | { |
55 | | #ifdef WITH_SYSTEMD |
56 | | if(ping_sec){ |
57 | | time_t now = mosquitto_time(); |
58 | | if(now > next_ping){ |
59 | | sd_notify(0, "WATCHDOG=1"); |
60 | | next_ping = now + ping_sec; |
61 | | } |
62 | | } |
63 | | #endif |
64 | 0 | } |