1 /* vi: set sw=4 ts=4: */
3 * Mini watchdog implementation for busybox
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>
9 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
13 #include "linux/watchdog.h"
15 #define OPT_FOREGROUND (1 << 0)
16 #define OPT_STIMER (1 << 1)
17 #define OPT_HTIMER (1 << 2)
19 static void watchdog_shutdown(int sig UNUSED_PARAM)
21 static const char V = 'V';
23 write(3, &V, 1); /* Magic, see watchdog-api.txt in kernel */
24 if (ENABLE_FEATURE_CLEAN_UP)
29 int watchdog_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
30 int watchdog_main(int argc, char **argv)
32 static const struct suffix_mult suffixes[] = {
39 unsigned stimer_duration; /* how often to restart */
40 unsigned htimer_duration = 60000; /* reboots after N ms if not restarted */
44 opt_complementary = "=1"; /* must have exactly 1 argument */
45 opts = getopt32(argv, "Ft:T:", &st_arg, &ht_arg);
47 if (opts & OPT_HTIMER)
48 htimer_duration = xatou_sfx(ht_arg, suffixes);
49 stimer_duration = htimer_duration / 2;
50 if (opts & OPT_STIMER)
51 stimer_duration = xatou_sfx(st_arg, suffixes);
53 bb_signals(BB_FATAL_SIGS, watchdog_shutdown);
55 /* Use known fd # - avoid needing global 'int fd' */
56 xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3);
58 /* WDIOC_SETTIMEOUT takes seconds, not milliseconds */
59 htimer_duration = htimer_duration / 1000;
60 ioctl_or_warn(3, WDIOC_SETTIMEOUT, &htimer_duration);
62 ioctl_or_warn(3, WDIOC_GETTIMEOUT, &htimer_duration);
63 printf("watchdog: SW timer is %dms, HW timer is %dms\n",
64 stimer_duration, htimer_duration * 1000);
67 if (!(opts & OPT_FOREGROUND)) {
68 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
73 * Make sure we clear the counter before sleeping, as the counter value
74 * is undefined at this point -- PFM
76 write(3, "", 1); /* write zero byte */
77 usleep(stimer_duration * 1000L);
79 return EXIT_SUCCESS; /* - not reached, but gcc 4.2.1 is too dumb! */