2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
15 #include <linux/watchdog.h>
17 #include <sys/ioctl.h>
18 #include <sys/types.h>
24 #include <libubox/uloop.h>
29 #define WDT_PATH "/dev/watchdog"
31 static struct uloop_timeout wdt_timeout
;
32 static int wdt_fd
= -1;
33 static int wdt_drv_timeout
= 30;
34 static int wdt_frequency
= 5;
35 static bool wdt_magicclose
= false;
37 void watchdog_ping(void)
40 if (wdt_fd
>= 0 && write(wdt_fd
, "X", 1) < 0)
41 ERROR("WDT failed to write: %m\n");
44 static void watchdog_timeout_cb(struct uloop_timeout
*t
)
47 uloop_timeout_set(t
, wdt_frequency
* 1000);
50 static int watchdog_open(bool cloexec
)
52 char *env
= getenv("WDTFD");
58 DEBUG(2, "Watchdog handover: fd=%s\n", env
);
62 wdt_fd
= open(WDT_PATH
, O_WRONLY
);
69 fcntl(wdt_fd
, F_SETFD
, fcntl(wdt_fd
, F_GETFD
) | FD_CLOEXEC
);
74 static void watchdog_close(bool with_release
)
80 if (write(wdt_fd
, "V", 1) < 0)
81 ERROR("WDT failed to write release: %m\n");
84 if (close(wdt_fd
) == -1)
85 ERROR("WDT failed to close watchdog: %m\n");
90 static int watchdog_set_drv_timeout(void)
95 return ioctl(wdt_fd
, WDIOC_SETTIMEOUT
, &wdt_drv_timeout
);
98 static void watchdog_print_status(void)
100 struct watchdog_info wdt_info
;
106 if (ioctl(wdt_fd
, WDIOC_GETSUPPORT
, &wdt_info
)) {
107 DEBUG(2, "Watchdog GETSUPPORT failed\n");
111 if (!(wdt_info
.options
& WDIOF_CARDRESET
)) {
112 DEBUG(2, "Watchdog does not have CARDRESET support\n");
116 if (ioctl(wdt_fd
, WDIOC_GETBOOTSTATUS
, &bootstatus
)) {
117 DEBUG(2, "Watchdog GETBOOTSTATUS failed\n");
121 if (bootstatus
& WDIOF_CARDRESET
)
122 LOG("Watchdog has previously reset the system\n");
124 DEBUG(2, "Watchdog did not previously reset the system\n");
127 void watchdog_set_magicclose(bool val
)
129 wdt_magicclose
= val
;
132 bool watchdog_get_magicclose(void)
134 return wdt_magicclose
;
137 void watchdog_set_stopped(bool val
)
140 uloop_timeout_cancel(&wdt_timeout
);
142 watchdog_close(wdt_magicclose
);
146 watchdog_set_drv_timeout();
147 watchdog_timeout_cb(&wdt_timeout
);
151 bool watchdog_get_stopped(void)
153 return !wdt_timeout
.pending
;
156 int watchdog_timeout(int timeout
)
159 DEBUG(4, "Set watchdog timeout: %ds\n", timeout
);
160 wdt_drv_timeout
= timeout
;
163 watchdog_set_drv_timeout();
166 return wdt_drv_timeout
;
169 int watchdog_frequency(int frequency
)
172 DEBUG(4, "Set watchdog frequency: %ds\n", frequency
);
173 wdt_frequency
= frequency
;
176 return wdt_frequency
;
179 char* watchdog_fd(void)
181 static char fd_buf
[12];
186 snprintf(fd_buf
, sizeof(fd_buf
), "%d", wdt_fd
);
191 void watchdog_init(int preinit
)
193 wdt_timeout
.cb
= watchdog_timeout_cb
;
195 if (watchdog_open(!preinit
) < 0)
198 LOG("- watchdog -\n");
199 watchdog_set_drv_timeout();
200 watchdog_timeout_cb(&wdt_timeout
);
202 DEBUG(4, "Opened watchdog with timeout %ds\n", watchdog_timeout(0));
204 watchdog_print_status();
208 void watchdog_set_cloexec(bool val
)
213 int flags
= fcntl(wdt_fd
, F_GETFD
);
217 flags
&= ~FD_CLOEXEC
;
218 fcntl(wdt_fd
, F_SETFD
, flags
);