watchdog: stop watchdog first on startup
[oweals/busybox.git] / miscutils / watchdog.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini watchdog implementation for busybox
4  *
5  * Copyright (C) 2003  Paul Mundt <lethal@linux-sh.org>
6  * Copyright (C) 2006  Bernhard Reutner-Fischer <busybox@busybox.net>
7  * Copyright (C) 2008  Darius Augulis <augulis.darius@gmail.com>
8  *
9  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10  */
11 //config:config WATCHDOG
12 //config:       bool "watchdog"
13 //config:       default y
14 //config:       select PLATFORM_LINUX
15 //config:       help
16 //config:         The watchdog utility is used with hardware or software watchdog
17 //config:         device drivers. It opens the specified watchdog device special file
18 //config:         and periodically writes a magic character to the device. If the
19 //config:         watchdog applet ever fails to write the magic character within a
20 //config:         certain amount of time, the watchdog device assumes the system has
21 //config:         hung, and will cause the hardware to reboot.
22
23 //applet:IF_WATCHDOG(APPLET(watchdog, BB_DIR_SBIN, BB_SUID_DROP))
24
25 //kbuild:lib-$(CONFIG_WATCHDOG) += watchdog.o
26
27 //usage:#define watchdog_trivial_usage
28 //usage:       "[-t N[ms]] [-T N[ms]] [-F] DEV"
29 //usage:#define watchdog_full_usage "\n\n"
30 //usage:       "Periodically write to watchdog device DEV\n"
31 //usage:     "\n        -T N    Reboot after N seconds if not reset (default 60)"
32 //usage:     "\n        -t N    Reset every N seconds (default 30)"
33 //usage:     "\n        -F      Run in foreground"
34 //usage:     "\n"
35 //usage:     "\nUse 500ms to specify period in milliseconds"
36
37 #include "libbb.h"
38 #include "linux/types.h" /* for __u32 */
39 #include "linux/watchdog.h"
40
41 #define OPT_FOREGROUND  (1 << 0)
42 #define OPT_STIMER      (1 << 1)
43 #define OPT_HTIMER      (1 << 2)
44
45 static void shutdown_watchdog(void)
46 {
47         static const char V = 'V';
48         write(3, &V, 1);  /* Magic, see watchdog-api.txt in kernel */
49         close(3);
50 }
51
52 static void shutdown_on_signal(int sig UNUSED_PARAM)
53 {
54         remove_pidfile(CONFIG_PID_FILE_PATH "/watchdog.pid");
55         shutdown_watchdog();
56         _exit(EXIT_SUCCESS);
57 }
58
59 static void watchdog_open(const char* device)
60 {
61         /* Use known fd # - avoid needing global 'int fd' */
62         xmove_fd(xopen(device, O_WRONLY), 3);
63
64         /* If the watchdog driver can do something other than cause a reboot
65          * on a timeout, then it's possible this program may be starting from
66          * a state when the watchdog hadn't been previously stopped with
67          * the magic write followed by a close.  In this case the driver may
68          * not start properly, so always do the proper stop first just in case.
69          */
70         shutdown_watchdog();
71
72         xmove_fd(xopen(device, O_WRONLY), 3);
73 }
74
75 int watchdog_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
76 int watchdog_main(int argc, char **argv)
77 {
78         static const struct suffix_mult suffixes[] = {
79                 { "ms", 1 },
80                 { "", 1000 },
81                 { "", 0 }
82         };
83
84         unsigned opts;
85         unsigned stimer_duration; /* how often to restart */
86         unsigned htimer_duration = 60000; /* reboots after N ms if not restarted */
87         char *st_arg;
88         char *ht_arg;
89
90         opt_complementary = "=1"; /* must have exactly 1 argument */
91         opts = getopt32(argv, "Ft:T:", &st_arg, &ht_arg);
92
93         /* We need to daemonize *before* opening the watchdog as many drivers
94          * will only allow one process at a time to do so.  Since daemonizing
95          * is not perfect (child may run before parent finishes exiting), we
96          * can't rely on parent exiting before us (let alone *cleanly* releasing
97          * the watchdog fd -- something else that may not even be allowed).
98          */
99         if (!(opts & OPT_FOREGROUND))
100                 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
101
102         if (opts & OPT_HTIMER)
103                 htimer_duration = xatou_sfx(ht_arg, suffixes);
104         stimer_duration = htimer_duration / 2;
105         if (opts & OPT_STIMER)
106                 stimer_duration = xatou_sfx(st_arg, suffixes);
107
108         bb_signals(BB_FATAL_SIGS, shutdown_on_signal);
109
110         watchdog_open(argv[argc - 1]);
111
112         /* WDIOC_SETTIMEOUT takes seconds, not milliseconds */
113         htimer_duration = htimer_duration / 1000;
114 #ifndef WDIOC_SETTIMEOUT
115 # error WDIOC_SETTIMEOUT is not defined, cannot compile watchdog applet
116 #else
117 # if defined WDIOC_SETOPTIONS && defined WDIOS_ENABLECARD
118         {
119                 static const int enable = WDIOS_ENABLECARD;
120                 ioctl_or_warn(3, WDIOC_SETOPTIONS, (void*) &enable);
121         }
122 # endif
123         ioctl_or_warn(3, WDIOC_SETTIMEOUT, &htimer_duration);
124 #endif
125
126 #if 0
127         ioctl_or_warn(3, WDIOC_GETTIMEOUT, &htimer_duration);
128         printf("watchdog: SW timer is %dms, HW timer is %ds\n",
129                 stimer_duration, htimer_duration * 1000);
130 #endif
131
132         write_pidfile(CONFIG_PID_FILE_PATH "/watchdog.pid");
133
134         while (1) {
135                 /*
136                  * Make sure we clear the counter before sleeping,
137                  * as the counter value is undefined at this point -- PFM
138                  */
139                 write(3, "", 1); /* write zero byte */
140                 usleep(stimer_duration * 1000L);
141         }
142         return EXIT_SUCCESS; /* - not reached, but gcc 4.2.1 is too dumb! */
143 }