service: use blob_memdup()
[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 static void watchdog_timeout_cb(struct uloop_timeout *t)
36 {
37         DEBUG(4, "Ping\n");
38         if (write(wdt_fd, "X", 1) < 0)
39                 ERROR("WDT failed to write: %s\n", strerror(errno));
40         uloop_timeout_set(t, wdt_frequency * 1000);
41 }
42
43 void watchdog_set_stopped(bool val)
44 {
45         if (val)
46                 uloop_timeout_cancel(&wdt_timeout);
47         else
48                 watchdog_timeout_cb(&wdt_timeout);
49 }
50
51 bool watchdog_get_stopped(void)
52 {
53         return !wdt_timeout.pending;
54 }
55
56 int watchdog_timeout(int timeout)
57 {
58         if (wdt_fd < 0)
59                 return 0;
60
61         if (timeout) {
62                 DEBUG(4, "Set watchdog timeout: %ds\n", timeout);
63                 ioctl(wdt_fd, WDIOC_SETTIMEOUT, &timeout);
64         }
65         ioctl(wdt_fd, WDIOC_GETTIMEOUT, &timeout);
66
67         return timeout;
68 }
69
70 int watchdog_frequency(int frequency)
71 {
72         if (wdt_fd < 0)
73                 return 0;
74
75         if (frequency) {
76                 DEBUG(4, "Set watchdog frequency: %ds\n", frequency);
77                 wdt_frequency = frequency;
78         }
79
80         return wdt_frequency;
81 }
82
83 char* watchdog_fd(void)
84 {
85         static char fd_buf[3];
86
87         if (wdt_fd < 0)
88                 return NULL;
89         snprintf(fd_buf, sizeof(fd_buf), "%d", wdt_fd);
90
91         return fd_buf;
92 }
93
94 void watchdog_init(int preinit)
95 {
96         char *env = getenv("WDTFD");
97
98         if (wdt_fd >= 0)
99                 return;
100
101         wdt_timeout.cb = watchdog_timeout_cb;
102         if (env) {
103                 DEBUG(2, "Watchdog handover: fd=%s\n", env);
104                 wdt_fd = atoi(env);
105                 unsetenv("WDTFD");
106         } else {
107                 wdt_fd = open("/dev/watchdog", O_WRONLY);
108         }
109
110         if (wdt_fd < 0)
111                 return;
112
113         if (!preinit)
114                 fcntl(wdt_fd, F_SETFD, fcntl(wdt_fd, F_GETFD) | FD_CLOEXEC);
115
116         LOG("- watchdog -\n");
117         watchdog_timeout(30);
118         watchdog_timeout_cb(&wdt_timeout);
119
120         DEBUG(4, "Opened watchdog with timeout %ds\n", watchdog_timeout(0));
121 }