[821] | 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 Fischer <busybox@busybox.net>
|
---|
| 7 | *
|
---|
| 8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
---|
| 9 | */
|
---|
| 10 |
|
---|
[1765] | 11 | #include "libbb.h"
|
---|
[821] | 12 |
|
---|
| 13 | #define OPT_FOREGROUND 0x01
|
---|
| 14 | #define OPT_TIMER 0x02
|
---|
| 15 |
|
---|
[1765] | 16 | static void watchdog_shutdown(int ATTRIBUTE_UNUSED sig) ATTRIBUTE_NORETURN;
|
---|
| 17 | static void watchdog_shutdown(int ATTRIBUTE_UNUSED sig)
|
---|
[821] | 18 | {
|
---|
[1765] | 19 | write(3, "V", 1); /* Magic, see watchdog-api.txt in kernel */
|
---|
| 20 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
| 21 | close(3);
|
---|
[821] | 22 | exit(0);
|
---|
| 23 | }
|
---|
| 24 |
|
---|
[1765] | 25 | int watchdog_main(int argc, char **argv);
|
---|
[821] | 26 | int watchdog_main(int argc, char **argv)
|
---|
| 27 | {
|
---|
[1765] | 28 | unsigned opts;
|
---|
| 29 | unsigned timer_duration = 30; /* Userspace timer duration, in seconds */
|
---|
[821] | 30 | char *t_arg;
|
---|
| 31 |
|
---|
[1765] | 32 | opt_complementary = "=1"; /* must have 1 argument */
|
---|
| 33 | opts = getopt32(argv, "Ft:", &t_arg);
|
---|
[821] | 34 |
|
---|
| 35 | if (opts & OPT_TIMER)
|
---|
[1765] | 36 | timer_duration = xatou(t_arg);
|
---|
[821] | 37 |
|
---|
[1765] | 38 | if (!(opts & OPT_FOREGROUND)) {
|
---|
| 39 | bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
|
---|
| 40 | }
|
---|
[821] | 41 |
|
---|
| 42 | signal(SIGHUP, watchdog_shutdown);
|
---|
| 43 | signal(SIGINT, watchdog_shutdown);
|
---|
| 44 |
|
---|
[1765] | 45 | /* Use known fd # - avoid needing global 'int fd' */
|
---|
| 46 | xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3);
|
---|
[821] | 47 |
|
---|
| 48 | while (1) {
|
---|
| 49 | /*
|
---|
| 50 | * Make sure we clear the counter before sleeping, as the counter value
|
---|
| 51 | * is undefined at this point -- PFM
|
---|
| 52 | */
|
---|
[1765] | 53 | write(3, "", 1);
|
---|
[821] | 54 | sleep(timer_duration);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | watchdog_shutdown(0);
|
---|
[1765] | 58 | /* return EXIT_SUCCESS; */
|
---|
[821] | 59 | }
|
---|