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/types.h" /* for __u32 */
14 #include "linux/watchdog.h"
16 #define OPT_FOREGROUND (1 << 0)
17 #define OPT_STIMER (1 << 1)
18 #define OPT_HTIMER (1 << 2)
20 static void watchdog_shutdown(int sig UNUSED_PARAM
)
22 static const char V
= 'V';
24 write(3, &V
, 1); /* Magic, see watchdog-api.txt in kernel */
25 if (ENABLE_FEATURE_CLEAN_UP
)
30 int watchdog_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
31 int watchdog_main(int argc
, char **argv
)
33 static const struct suffix_mult suffixes
[] = {
40 unsigned stimer_duration
; /* how often to restart */
41 unsigned htimer_duration
= 60000; /* reboots after N ms if not restarted */
45 opt_complementary
= "=1"; /* must have exactly 1 argument */
46 opts
= getopt32(argv
, "Ft:T:", &st_arg
, &ht_arg
);
48 if (opts
& OPT_HTIMER
)
49 htimer_duration
= xatou_sfx(ht_arg
, suffixes
);
50 stimer_duration
= htimer_duration
/ 2;
51 if (opts
& OPT_STIMER
)
52 stimer_duration
= xatou_sfx(st_arg
, suffixes
);
54 bb_signals(BB_FATAL_SIGS
, watchdog_shutdown
);
56 /* Use known fd # - avoid needing global 'int fd' */
57 xmove_fd(xopen(argv
[argc
- 1], O_WRONLY
), 3);
59 /* WDIOC_SETTIMEOUT takes seconds, not milliseconds */
60 htimer_duration
= htimer_duration
/ 1000;
61 #ifndef WDIOC_SETTIMEOUT
62 #error WDIOC_SETTIMEOUT is not defined, cannot compile watchdog applet
64 ioctl_or_warn(3, WDIOC_SETTIMEOUT
, &htimer_duration
);
67 ioctl_or_warn(3, WDIOC_GETTIMEOUT
, &htimer_duration
);
68 printf("watchdog: SW timer is %dms, HW timer is %dms\n",
69 stimer_duration
, htimer_duration
* 1000);
72 if (!(opts
& OPT_FOREGROUND
)) {
73 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT
, argv
);
78 * Make sure we clear the counter before sleeping,
79 * as the counter value is undefined at this point -- PFM
81 write(3, "", 1); /* write zero byte */
82 usleep(stimer_duration
* 1000L);
84 return EXIT_SUCCESS
; /* - not reached, but gcc 4.2.1 is too dumb! */