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(void)
79 if (write(wdt_fd
, "V", 1) < 0)
80 ERROR("WDT failed to write release: %m\n");
82 if (close(wdt_fd
) == -1)
83 ERROR("WDT failed to close watchdog: %m\n");
88 static int watchdog_set_drv_timeout(void)
93 return ioctl(wdt_fd
, WDIOC_SETTIMEOUT
, &wdt_drv_timeout
);
96 static void watchdog_print_status(void)
98 struct watchdog_info wdt_info
;
104 if (ioctl(wdt_fd
, WDIOC_GETSUPPORT
, &wdt_info
)) {
105 DEBUG(2, "Watchdog GETSUPPORT failed\n");
109 if (!(wdt_info
.options
& WDIOF_CARDRESET
)) {
110 DEBUG(2, "Watchdog does not have CARDRESET support\n");
114 if (ioctl(wdt_fd
, WDIOC_GETBOOTSTATUS
, &bootstatus
)) {
115 DEBUG(2, "Watchdog GETBOOTSTATUS failed\n");
119 if (bootstatus
& WDIOF_CARDRESET
)
120 LOG("Watchdog has previously reset the system\n");
122 DEBUG(2, "Watchdog did not previously reset the system\n");
125 void watchdog_set_magicclose(bool val
)
127 wdt_magicclose
= val
;
130 bool watchdog_get_magicclose(void)
132 return wdt_magicclose
;
135 void watchdog_set_stopped(bool val
)
138 uloop_timeout_cancel(&wdt_timeout
);
145 watchdog_set_drv_timeout();
146 watchdog_timeout_cb(&wdt_timeout
);
150 bool watchdog_get_stopped(void)
152 return !wdt_timeout
.pending
;
155 int watchdog_timeout(int timeout
)
158 DEBUG(4, "Set watchdog timeout: %ds\n", timeout
);
159 wdt_drv_timeout
= timeout
;
162 watchdog_set_drv_timeout();
165 return wdt_drv_timeout
;
168 int watchdog_frequency(int frequency
)
171 DEBUG(4, "Set watchdog frequency: %ds\n", frequency
);
172 wdt_frequency
= frequency
;
175 return wdt_frequency
;
178 char* watchdog_fd(void)
180 static char fd_buf
[12];
185 snprintf(fd_buf
, sizeof(fd_buf
), "%d", wdt_fd
);
190 void watchdog_init(int preinit
)
192 wdt_timeout
.cb
= watchdog_timeout_cb
;
194 if (watchdog_open(!preinit
) < 0)
197 LOG("- watchdog -\n");
198 watchdog_set_drv_timeout();
199 watchdog_timeout_cb(&wdt_timeout
);
201 DEBUG(4, "Opened watchdog with timeout %ds\n", watchdog_timeout(0));
203 watchdog_print_status();
207 void watchdog_set_cloexec(bool val
)
212 int flags
= fcntl(wdt_fd
, F_GETFD
);
216 flags
&= ~FD_CLOEXEC
;
217 fcntl(wdt_fd
, F_SETFD
, flags
);