procd: Remove redundant errno variable in several printf functions.
[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 static bool wdt_magicclose = false;
35
36 void watchdog_ping(void)
37 {
38 DEBUG(4, "Ping\n");
39 if (wdt_fd >= 0 && write(wdt_fd, "X", 1) < 0)
40 ERROR("WDT failed to write: %m\n");
41 }
42
43 static void watchdog_timeout_cb(struct uloop_timeout *t)
44 {
45 watchdog_ping();
46 uloop_timeout_set(t, wdt_frequency * 1000);
47 }
48
49 static int watchdog_open(bool cloexec)
50 {
51 char *env = getenv("WDTFD");
52
53 if (wdt_fd >= 0)
54 return wdt_fd;
55
56 if (env) {
57 DEBUG(2, "Watchdog handover: fd=%s\n", env);
58 wdt_fd = atoi(env);
59 unsetenv("WDTFD");
60 } else {
61 wdt_fd = open(WDT_PATH, O_WRONLY);
62 }
63
64 if (wdt_fd < 0)
65 return wdt_fd;
66
67 if (cloexec)
68 fcntl(wdt_fd, F_SETFD, fcntl(wdt_fd, F_GETFD) | FD_CLOEXEC);
69
70 return wdt_fd;
71 }
72
73 static void watchdog_close(void)
74 {
75 if (wdt_fd < 0)
76 return;
77
78 if (write(wdt_fd, "V", 1) < 0)
79 ERROR("WDT failed to write release: %m\n");
80
81 if (close(wdt_fd) == -1)
82 ERROR("WDT failed to close watchdog: %m\n");
83
84 wdt_fd = -1;
85 }
86
87 void watchdog_set_magicclose(bool val)
88 {
89 wdt_magicclose = val;
90 }
91
92 bool watchdog_get_magicclose(void)
93 {
94 return wdt_magicclose;
95 }
96
97 void watchdog_set_stopped(bool val)
98 {
99 if (val) {
100 uloop_timeout_cancel(&wdt_timeout);
101
102 if (wdt_magicclose)
103 watchdog_close();
104 }
105 else {
106 watchdog_open(true);
107 watchdog_timeout_cb(&wdt_timeout);
108 }
109 }
110
111 bool watchdog_get_stopped(void)
112 {
113 return !wdt_timeout.pending;
114 }
115
116 int watchdog_timeout(int timeout)
117 {
118 if (wdt_fd < 0)
119 return 0;
120
121 if (timeout) {
122 DEBUG(4, "Set watchdog timeout: %ds\n", timeout);
123 ioctl(wdt_fd, WDIOC_SETTIMEOUT, &timeout);
124 }
125 ioctl(wdt_fd, WDIOC_GETTIMEOUT, &timeout);
126
127 return timeout;
128 }
129
130 int watchdog_frequency(int frequency)
131 {
132 if (wdt_fd < 0)
133 return 0;
134
135 if (frequency) {
136 DEBUG(4, "Set watchdog frequency: %ds\n", frequency);
137 wdt_frequency = frequency;
138 }
139
140 return wdt_frequency;
141 }
142
143 char* watchdog_fd(void)
144 {
145 static char fd_buf[3];
146
147 if (wdt_fd < 0)
148 return NULL;
149 snprintf(fd_buf, sizeof(fd_buf), "%d", wdt_fd);
150
151 return fd_buf;
152 }
153
154 void watchdog_init(int preinit)
155 {
156 wdt_timeout.cb = watchdog_timeout_cb;
157
158 if (watchdog_open(!preinit) < 0)
159 return;
160
161 LOG("- watchdog -\n");
162 watchdog_timeout(30);
163 watchdog_timeout_cb(&wdt_timeout);
164
165 DEBUG(4, "Opened watchdog with timeout %ds\n", watchdog_timeout(0));
166 }
167
168
169 void watchdog_set_cloexec(bool val)
170 {
171 if (wdt_fd < 0)
172 return;
173
174 int flags = fcntl(wdt_fd, F_GETFD);
175 if (val)
176 flags |= FD_CLOEXEC;
177 else
178 flags &= ~FD_CLOEXEC;
179 fcntl(wdt_fd, F_SETFD, flags);
180 }