add ifdefs to make service/instance.c compile on non-linux systems
[project/procd.git] / watchdog.c
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #include <linux/watchdog.h>
16
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21
22 #include <unistd.h>
23
24 #include <libubox/uloop.h>
25
26 #include "procd.h"
27 #include "watchdog.h"
28
29 #define WDT_PATH "/dev/watchdog"
30
31 static struct uloop_timeout wdt_timeout;
32 static int wdt_fd = -1;
33 static int wdt_frequency = 5;
34
35 void watchdog_ping(void)
36 {
37 DEBUG(4, "Ping\n");
38 if (wdt_fd >= 0 && write(wdt_fd, "X", 1) < 0)
39 ERROR("WDT failed to write: %s\n", strerror(errno));
40 }
41
42 static void watchdog_timeout_cb(struct uloop_timeout *t)
43 {
44 watchdog_ping();
45 uloop_timeout_set(t, wdt_frequency * 1000);
46 }
47
48 void watchdog_set_stopped(bool val)
49 {
50 if (val)
51 uloop_timeout_cancel(&wdt_timeout);
52 else
53 watchdog_timeout_cb(&wdt_timeout);
54 }
55
56 bool watchdog_get_stopped(void)
57 {
58 return !wdt_timeout.pending;
59 }
60
61 int watchdog_timeout(int timeout)
62 {
63 if (wdt_fd < 0)
64 return 0;
65
66 if (timeout) {
67 DEBUG(4, "Set watchdog timeout: %ds\n", timeout);
68 ioctl(wdt_fd, WDIOC_SETTIMEOUT, &timeout);
69 }
70 ioctl(wdt_fd, WDIOC_GETTIMEOUT, &timeout);
71
72 return timeout;
73 }
74
75 int watchdog_frequency(int frequency)
76 {
77 if (wdt_fd < 0)
78 return 0;
79
80 if (frequency) {
81 DEBUG(4, "Set watchdog frequency: %ds\n", frequency);
82 wdt_frequency = frequency;
83 }
84
85 return wdt_frequency;
86 }
87
88 char* watchdog_fd(void)
89 {
90 static char fd_buf[3];
91
92 if (wdt_fd < 0)
93 return NULL;
94 snprintf(fd_buf, sizeof(fd_buf), "%d", wdt_fd);
95
96 return fd_buf;
97 }
98
99 void watchdog_init(int preinit)
100 {
101 char *env = getenv("WDTFD");
102
103 if (wdt_fd >= 0)
104 return;
105
106 wdt_timeout.cb = watchdog_timeout_cb;
107 if (env) {
108 DEBUG(2, "Watchdog handover: fd=%s\n", env);
109 wdt_fd = atoi(env);
110 unsetenv("WDTFD");
111 } else {
112 wdt_fd = open("/dev/watchdog", O_WRONLY);
113 }
114
115 if (wdt_fd < 0)
116 return;
117
118 if (!preinit)
119 fcntl(wdt_fd, F_SETFD, fcntl(wdt_fd, F_GETFD) | FD_CLOEXEC);
120
121 LOG("- watchdog -\n");
122 watchdog_timeout(30);
123 watchdog_timeout_cb(&wdt_timeout);
124
125 DEBUG(4, "Opened watchdog with timeout %ds\n", watchdog_timeout(0));
126 }
127
128
129 void watchdog_no_cloexec(void)
130 {
131 if (wdt_fd < 0)
132 return;
133
134 fcntl(wdt_fd, F_SETFD, fcntl(wdt_fd, F_GETFD) & ~FD_CLOEXEC);
135 }