Magellan Linux

Contents of /tags/mkinitrd-6_1_12/busybox/miscutils/watchdog.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 939 - (show annotations) (download)
Tue Nov 17 21:24:51 2009 UTC (14 years, 6 months ago) by niro
File MIME type: text/plain
File size: 2238 byte(s)
tagged 'mkinitrd-6_1_12'
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 the GPL v2 or later, see the file LICENSE in this tarball.
10 */
11
12 #include "libbb.h"
13 #include "linux/watchdog.h"
14
15 #define OPT_FOREGROUND (1 << 0)
16 #define OPT_STIMER (1 << 1)
17 #define OPT_HTIMER (1 << 2)
18
19 static void watchdog_shutdown(int sig UNUSED_PARAM)
20 {
21 static const char V = 'V';
22
23 write(3, &V, 1); /* Magic, see watchdog-api.txt in kernel */
24 if (ENABLE_FEATURE_CLEAN_UP)
25 close(3);
26 exit(EXIT_SUCCESS);
27 }
28
29 int watchdog_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
30 int watchdog_main(int argc, char **argv)
31 {
32 static const struct suffix_mult suffixes[] = {
33 { "ms", 1 },
34 { "", 1000 },
35 { }
36 };
37
38 unsigned opts;
39 unsigned stimer_duration; /* how often to restart */
40 unsigned htimer_duration = 60000; /* reboots after N ms if not restarted */
41 char *st_arg;
42 char *ht_arg;
43
44 opt_complementary = "=1"; /* must have exactly 1 argument */
45 opts = getopt32(argv, "Ft:T:", &st_arg, &ht_arg);
46
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);
52
53 bb_signals(BB_FATAL_SIGS, watchdog_shutdown);
54
55 /* Use known fd # - avoid needing global 'int fd' */
56 xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3);
57
58 /* WDIOC_SETTIMEOUT takes seconds, not milliseconds */
59 htimer_duration = htimer_duration / 1000;
60 ioctl_or_warn(3, WDIOC_SETTIMEOUT, &htimer_duration);
61 #if 0
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);
65 #endif
66
67 if (!(opts & OPT_FOREGROUND)) {
68 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
69 }
70
71 while (1) {
72 /*
73 * Make sure we clear the counter before sleeping, as the counter value
74 * is undefined at this point -- PFM
75 */
76 write(3, "", 1); /* write zero byte */
77 usleep(stimer_duration * 1000L);
78 }
79 return EXIT_SUCCESS; /* - not reached, but gcc 4.2.1 is too dumb! */
80 }