2001-01-25 23:40:32 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini watchdog implementation for busybox
|
|
|
|
*
|
2003-07-22 07:39:18 +00:00
|
|
|
* Copyright (C) 2003 Paul Mundt <lethal@linux-sh.org>
|
2008-09-25 12:13:34 +00:00
|
|
|
* Copyright (C) 2006 Bernhard Reutner-Fischer <busybox@busybox.net>
|
2008-09-07 23:22:08 +00:00
|
|
|
* Copyright (C) 2008 Darius Augulis <augulis.darius@gmail.com>
|
2001-01-25 23:40:32 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2001-01-25 23:40:32 +00:00
|
|
|
*/
|
|
|
|
|
2011-04-11 01:29:49 +00:00
|
|
|
//usage:#define watchdog_trivial_usage
|
|
|
|
//usage: "[-t N[ms]] [-T N[ms]] [-F] DEV"
|
|
|
|
//usage:#define watchdog_full_usage "\n\n"
|
|
|
|
//usage: "Periodically write to watchdog device DEV\n"
|
|
|
|
//usage: "\n -T N Reboot after N seconds if not reset (default 60)"
|
|
|
|
//usage: "\n -t N Reset every N seconds (default 30)"
|
|
|
|
//usage: "\n -F Run in foreground"
|
|
|
|
//usage: "\n"
|
|
|
|
//usage: "\nUse 500ms to specify period in milliseconds"
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2008-12-14 14:49:06 +00:00
|
|
|
#include "linux/types.h" /* for __u32 */
|
2008-09-07 23:22:08 +00:00
|
|
|
#include "linux/watchdog.h"
|
2001-01-25 23:40:32 +00:00
|
|
|
|
2008-09-07 23:22:08 +00:00
|
|
|
#define OPT_FOREGROUND (1 << 0)
|
|
|
|
#define OPT_STIMER (1 << 1)
|
|
|
|
#define OPT_HTIMER (1 << 2)
|
2006-06-26 21:31:17 +00:00
|
|
|
|
2008-07-05 09:18:54 +00:00
|
|
|
static void watchdog_shutdown(int sig UNUSED_PARAM)
|
2003-07-22 07:39:18 +00:00
|
|
|
{
|
2007-09-05 12:13:51 +00:00
|
|
|
static const char V = 'V';
|
|
|
|
|
2010-10-28 16:57:19 +00:00
|
|
|
write(3, &V, 1); /* Magic, see watchdog-api.txt in kernel */
|
2007-03-14 21:55:41 +00:00
|
|
|
if (ENABLE_FEATURE_CLEAN_UP)
|
|
|
|
close(3);
|
2010-10-28 19:34:56 +00:00
|
|
|
_exit(EXIT_SUCCESS);
|
2003-07-22 07:39:18 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int watchdog_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2005-10-28 09:24:33 +00:00
|
|
|
int watchdog_main(int argc, char **argv)
|
2001-01-25 23:40:32 +00:00
|
|
|
{
|
2008-09-07 23:22:08 +00:00
|
|
|
static const struct suffix_mult suffixes[] = {
|
|
|
|
{ "ms", 1 },
|
|
|
|
{ "", 1000 },
|
2009-09-06 10:47:55 +00:00
|
|
|
{ "", 0 }
|
2008-09-07 23:22:08 +00:00
|
|
|
};
|
2006-06-03 20:53:18 +00:00
|
|
|
|
2008-09-07 23:22:08 +00:00
|
|
|
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-26 21:31:17 +00:00
|
|
|
|
2008-09-07 23:22:08 +00:00
|
|
|
opt_complementary = "=1"; /* must have exactly 1 argument */
|
|
|
|
opts = getopt32(argv, "Ft:T:", &st_arg, &ht_arg);
|
2001-01-25 23:40:32 +00:00
|
|
|
|
2009-04-25 06:16:37 +00:00
|
|
|
/* 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-07 23:22:08 +00:00
|
|
|
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 07:39:18 +00:00
|
|
|
|
2008-03-19 19:38:46 +00:00
|
|
|
bb_signals(BB_FATAL_SIGS, watchdog_shutdown);
|
2003-07-22 07:39:18 +00:00
|
|
|
|
2007-03-14 21:55:41 +00:00
|
|
|
/* Use known fd # - avoid needing global 'int fd' */
|
2007-03-26 13:20:54 +00:00
|
|
|
xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3);
|
2001-01-25 23:40:32 +00:00
|
|
|
|
2008-10-04 16:40:17 +00:00
|
|
|
/* WDIOC_SETTIMEOUT takes seconds, not milliseconds */
|
|
|
|
htimer_duration = htimer_duration / 1000;
|
2008-12-14 14:49:06 +00:00
|
|
|
#ifndef WDIOC_SETTIMEOUT
|
2009-04-20 09:26:17 +00:00
|
|
|
# error WDIOC_SETTIMEOUT is not defined, cannot compile watchdog applet
|
2008-12-14 14:49:06 +00:00
|
|
|
#else
|
2009-04-20 09:26:17 +00:00
|
|
|
# if defined WDIOC_SETOPTIONS && defined WDIOS_ENABLECARD
|
|
|
|
{
|
|
|
|
static const int enable = WDIOS_ENABLECARD;
|
|
|
|
ioctl_or_warn(3, WDIOC_SETOPTIONS, (void*) &enable);
|
|
|
|
}
|
|
|
|
# endif
|
2008-09-07 23:22:08 +00:00
|
|
|
ioctl_or_warn(3, WDIOC_SETTIMEOUT, &htimer_duration);
|
2008-12-14 14:49:06 +00:00
|
|
|
#endif
|
2009-04-20 09:26:17 +00:00
|
|
|
|
2008-09-07 23:22:08 +00:00
|
|
|
#if 0
|
|
|
|
ioctl_or_warn(3, WDIOC_GETTIMEOUT, &htimer_duration);
|
2009-04-20 09:26:17 +00:00
|
|
|
printf("watchdog: SW timer is %dms, HW timer is %ds\n",
|
2008-09-07 23:22:08 +00:00
|
|
|
stimer_duration, htimer_duration * 1000);
|
|
|
|
#endif
|
|
|
|
|
2001-01-25 23:40:32 +00:00
|
|
|
while (1) {
|
2004-03-15 08:29:22 +00:00
|
|
|
/*
|
2008-12-14 14:49:06 +00:00
|
|
|
* Make sure we clear the counter before sleeping,
|
|
|
|
* as the counter value is undefined at this point -- PFM
|
2003-07-22 07:39:18 +00:00
|
|
|
*/
|
2007-09-05 12:13:51 +00:00
|
|
|
write(3, "", 1); /* write zero byte */
|
2008-09-07 23:22:08 +00:00
|
|
|
usleep(stimer_duration * 1000L);
|
2001-01-25 23:40:32 +00:00
|
|
|
}
|
2007-09-05 12:13:51 +00:00
|
|
|
return EXIT_SUCCESS; /* - not reached, but gcc 4.2.1 is too dumb! */
|
2001-01-25 23:40:32 +00:00
|
|
|
}
|