2001-01-26 05:10:32 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini watchdog implementation for busybox
|
|
|
|
*
|
2003-07-22 13:09:18 +05:30
|
|
|
* Copyright (C) 2003 Paul Mundt <lethal@linux-sh.org>
|
2008-09-25 17:43:34 +05:30
|
|
|
* Copyright (C) 2006 Bernhard Reutner-Fischer <busybox@busybox.net>
|
2008-09-08 04:52:08 +05:30
|
|
|
* Copyright (C) 2008 Darius Augulis <augulis.darius@gmail.com>
|
2001-01-26 05:10:32 +05:30
|
|
|
*
|
2005-10-28 14:54:33 +05:30
|
|
|
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
2001-01-26 05:10:32 +05:30
|
|
|
*/
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2008-12-14 20:19:06 +05:30
|
|
|
#include "linux/types.h" /* for __u32 */
|
2008-09-08 04:52:08 +05:30
|
|
|
#include "linux/watchdog.h"
|
2001-01-26 05:10:32 +05:30
|
|
|
|
2008-09-08 04:52:08 +05:30
|
|
|
#define OPT_FOREGROUND (1 << 0)
|
|
|
|
#define OPT_STIMER (1 << 1)
|
|
|
|
#define OPT_HTIMER (1 << 2)
|
2006-06-27 03:01:17 +05:30
|
|
|
|
2008-07-05 14:48:54 +05:30
|
|
|
static void watchdog_shutdown(int sig UNUSED_PARAM)
|
2003-07-22 13:09:18 +05:30
|
|
|
{
|
2007-09-05 17:43:51 +05:30
|
|
|
static const char V = 'V';
|
|
|
|
|
|
|
|
write(3, &V, 1); /* Magic, see watchdog-api.txt in kernel */
|
2007-03-15 03:25:41 +05:30
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
close(3);
|
2008-05-19 14:59:47 +05:30
|
|
|
exit(EXIT_SUCCESS);
|
2003-07-22 13:09:18 +05:30
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int watchdog_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2005-10-28 14:54:33 +05:30
|
|
|
int watchdog_main(int argc, char **argv)
|
2001-01-26 05:10:32 +05:30
|
|
|
{
|
2008-09-08 04:52:08 +05:30
|
|
|
static const struct suffix_mult suffixes[] = {
|
|
|
|
{ "ms", 1 },
|
|
|
|
{ "", 1000 },
|
2009-09-06 16:17:55 +05:30
|
|
|
{ "", 0 }
|
2008-09-08 04:52:08 +05:30
|
|
|
};
|
2006-06-04 02:23:18 +05:30
|
|
|
|
2008-09-08 04:52:08 +05:30
|
|
|
unsigned opts;
|
|
|
|
unsigned stimer_duration; /* how often to restart */
|
|
|
|
unsigned htimer_duration = 60000; /* reboots after N ms if not restarted */
|
|
|
|
char *st_arg;
|
|
|
|
char *ht_arg;
|
2006-06-27 03:01:17 +05:30
|
|
|
|
2008-09-08 04:52:08 +05:30
|
|
|
opt_complementary = "=1"; /* must have exactly 1 argument */
|
|
|
|
opts = getopt32(argv, "Ft:T:", &st_arg, &ht_arg);
|
2001-01-26 05:10:32 +05:30
|
|
|
|
2009-04-25 11:46:37 +05:30
|
|
|
/* We need to daemonize *before* opening the watchdog as many drivers
|
|
|
|
* will only allow one process at a time to do so. Since daemonizing
|
|
|
|
* is not perfect (child may run before parent finishes exiting), we
|
|
|
|
* can't rely on parent exiting before us (let alone *cleanly* releasing
|
|
|
|
* the watchdog fd -- something else that may not even be allowed).
|
|
|
|
*/
|
|
|
|
if (!(opts & OPT_FOREGROUND))
|
|
|
|
bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
|
|
|
|
|
2008-09-08 04:52:08 +05:30
|
|
|
if (opts & OPT_HTIMER)
|
|
|
|
htimer_duration = xatou_sfx(ht_arg, suffixes);
|
|
|
|
stimer_duration = htimer_duration / 2;
|
|
|
|
if (opts & OPT_STIMER)
|
|
|
|
stimer_duration = xatou_sfx(st_arg, suffixes);
|
2003-07-22 13:09:18 +05:30
|
|
|
|
2008-03-20 01:08:46 +05:30
|
|
|
bb_signals(BB_FATAL_SIGS, watchdog_shutdown);
|
2003-07-22 13:09:18 +05:30
|
|
|
|
2007-03-15 03:25:41 +05:30
|
|
|
/* Use known fd # - avoid needing global 'int fd' */
|
2007-03-26 18:50:54 +05:30
|
|
|
xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3);
|
2001-01-26 05:10:32 +05:30
|
|
|
|
2008-10-04 22:10:17 +05:30
|
|
|
/* WDIOC_SETTIMEOUT takes seconds, not milliseconds */
|
|
|
|
htimer_duration = htimer_duration / 1000;
|
2008-12-14 20:19:06 +05:30
|
|
|
#ifndef WDIOC_SETTIMEOUT
|
2009-04-20 14:56:17 +05:30
|
|
|
# error WDIOC_SETTIMEOUT is not defined, cannot compile watchdog applet
|
2008-12-14 20:19:06 +05:30
|
|
|
#else
|
2009-04-20 14:56:17 +05:30
|
|
|
# if defined WDIOC_SETOPTIONS && defined WDIOS_ENABLECARD
|
|
|
|
{
|
|
|
|
static const int enable = WDIOS_ENABLECARD;
|
|
|
|
ioctl_or_warn(3, WDIOC_SETOPTIONS, (void*) &enable);
|
|
|
|
}
|
|
|
|
# endif
|
2008-09-08 04:52:08 +05:30
|
|
|
ioctl_or_warn(3, WDIOC_SETTIMEOUT, &htimer_duration);
|
2008-12-14 20:19:06 +05:30
|
|
|
#endif
|
2009-04-20 14:56:17 +05:30
|
|
|
|
2008-09-08 04:52:08 +05:30
|
|
|
#if 0
|
|
|
|
ioctl_or_warn(3, WDIOC_GETTIMEOUT, &htimer_duration);
|
2009-04-20 14:56:17 +05:30
|
|
|
printf("watchdog: SW timer is %dms, HW timer is %ds\n",
|
2008-09-08 04:52:08 +05:30
|
|
|
stimer_duration, htimer_duration * 1000);
|
|
|
|
#endif
|
|
|
|
|
2001-01-26 05:10:32 +05:30
|
|
|
while (1) {
|
2004-03-15 13:59:22 +05:30
|
|
|
/*
|
2008-12-14 20:19:06 +05:30
|
|
|
* Make sure we clear the counter before sleeping,
|
|
|
|
* as the counter value is undefined at this point -- PFM
|
2003-07-22 13:09:18 +05:30
|
|
|
*/
|
2007-09-05 17:43:51 +05:30
|
|
|
write(3, "", 1); /* write zero byte */
|
2008-09-08 04:52:08 +05:30
|
|
|
usleep(stimer_duration * 1000L);
|
2001-01-26 05:10:32 +05:30
|
|
|
}
|
2007-09-05 17:43:51 +05:30
|
|
|
return EXIT_SUCCESS; /* - not reached, but gcc 4.2.1 is too dumb! */
|
2001-01-26 05:10:32 +05:30
|
|
|
}
|