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
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2001-01-26 05:10:32 +05:30
|
|
|
*/
|
2016-11-23 03:44:24 +05:30
|
|
|
//config:config WATCHDOG
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "watchdog (5.3 kb)"
|
2016-11-23 03:44:24 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: The watchdog utility is used with hardware or software watchdog
|
|
|
|
//config: device drivers. It opens the specified watchdog device special file
|
|
|
|
//config: and periodically writes a magic character to the device. If the
|
|
|
|
//config: watchdog applet ever fails to write the magic character within a
|
|
|
|
//config: certain amount of time, the watchdog device assumes the system has
|
|
|
|
//config: hung, and will cause the hardware to reboot.
|
2021-04-06 13:44:53 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_WATCHDOG_OPEN_TWICE
|
|
|
|
//config: bool "Open watchdog device twice, closing it gracefully in between"
|
|
|
|
//config: depends on WATCHDOG
|
|
|
|
//config: default n # this behavior was essentially a hack for a broken driver
|
|
|
|
//config: help
|
|
|
|
//config: When enabled, the watchdog device is opened and then immediately
|
|
|
|
//config: magic-closed, before being opened a second time. This may be necessary
|
|
|
|
//config: for some watchdog devices, but can cause spurious warnings in the
|
|
|
|
//config: kernel log if the nowayout feature is enabled. Also, if this workaround
|
|
|
|
//config: is really needed for you machine to work properly, consider whether
|
|
|
|
//config: it should be fixed in the kernel driver instead. Even when disabled,
|
|
|
|
//config: the behaviour is easily emulated with a "printf 'V' > /dev/watchdog"
|
|
|
|
//config: immediately before starting the busybox watchdog daemon. Say n unless
|
|
|
|
//config: you know that you absolutely need this.
|
2001-01-26 05:10:32 +05:30
|
|
|
|
2016-11-23 04:24:17 +05:30
|
|
|
//applet:IF_WATCHDOG(APPLET(watchdog, BB_DIR_SBIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_WATCHDOG) += watchdog.o
|
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//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-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2017-07-08 22:23:49 +05:30
|
|
|
#include <linux/types.h> /* for __u32 */
|
|
|
|
#include <linux/watchdog.h>
|
|
|
|
|
|
|
|
#ifndef WDIOC_SETOPTIONS
|
|
|
|
# define WDIOC_SETOPTIONS 0x5704
|
|
|
|
#endif
|
|
|
|
#ifndef WDIOC_SETTIMEOUT
|
|
|
|
# define WDIOC_SETTIMEOUT 0x5706
|
|
|
|
#endif
|
|
|
|
#ifndef WDIOC_GETTIMEOUT
|
|
|
|
# define WDIOC_GETTIMEOUT 0x5707
|
|
|
|
#endif
|
|
|
|
#ifndef WDIOS_ENABLECARD
|
|
|
|
# define WDIOS_ENABLECARD 2
|
|
|
|
#endif
|
2001-01-26 05:10:32 +05:30
|
|
|
|
2017-07-08 22:05:25 +05:30
|
|
|
static void shutdown_watchdog(void)
|
2003-07-22 13:09:18 +05:30
|
|
|
{
|
2007-09-05 17:43:51 +05:30
|
|
|
static const char V = 'V';
|
2017-07-08 22:05:25 +05:30
|
|
|
write(3, &V, 1); /* Magic, see watchdog-api.txt in kernel */
|
|
|
|
close(3);
|
|
|
|
}
|
2007-09-05 17:43:51 +05:30
|
|
|
|
2017-07-08 22:05:25 +05:30
|
|
|
static void shutdown_on_signal(int sig UNUSED_PARAM)
|
|
|
|
{
|
2019-03-18 00:17:52 +05:30
|
|
|
remove_pidfile_std_path_and_ext("watchdog");
|
2017-07-08 22:05:25 +05:30
|
|
|
shutdown_watchdog();
|
2010-10-29 01:04:56 +05:30
|
|
|
_exit(EXIT_SUCCESS);
|
2003-07-22 13:09:18 +05:30
|
|
|
}
|
|
|
|
|
2017-07-08 22:05:25 +05:30
|
|
|
static void watchdog_open(const char* device)
|
|
|
|
{
|
|
|
|
/* Use known fd # - avoid needing global 'int fd' */
|
|
|
|
xmove_fd(xopen(device, O_WRONLY), 3);
|
|
|
|
|
2021-04-06 13:44:53 +05:30
|
|
|
#if ENABLE_FEATURE_WATCHDOG_OPEN_TWICE
|
2017-07-08 22:05:25 +05:30
|
|
|
/* If the watchdog driver can do something other than cause a reboot
|
|
|
|
* on a timeout, then it's possible this program may be starting from
|
|
|
|
* a state when the watchdog hadn't been previously stopped with
|
|
|
|
* the magic write followed by a close. In this case the driver may
|
|
|
|
* not start properly, so always do the proper stop first just in case.
|
|
|
|
*/
|
|
|
|
shutdown_watchdog();
|
|
|
|
|
|
|
|
xmove_fd(xopen(device, O_WRONLY), 3);
|
2021-04-06 13:44:53 +05:30
|
|
|
#endif
|
2017-07-08 22:05:25 +05:30
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int watchdog_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2017-07-08 22:23:49 +05:30
|
|
|
int watchdog_main(int argc UNUSED_PARAM, char **argv)
|
2001-01-26 05:10:32 +05:30
|
|
|
{
|
2017-07-08 22:23:49 +05:30
|
|
|
static const int enable = WDIOS_ENABLECARD;
|
2020-11-30 17:33:03 +05:30
|
|
|
static const struct suffix_mult suffixes[] ALIGN_SUFFIX = {
|
2008-09-08 04:52:08 +05:30
|
|
|
{ "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
|
|
|
|
2021-04-06 13:44:53 +05:30
|
|
|
#define OPT_FOREGROUND (1 << 0)
|
|
|
|
#define OPT_STIMER (1 << 1)
|
|
|
|
#define OPT_HTIMER (1 << 2)
|
2017-08-09 01:25:02 +05:30
|
|
|
opts = getopt32(argv, "^" "Ft:T:" "\0" "=1"/*must have exactly 1 arg*/,
|
|
|
|
&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);
|
|
|
|
|
2017-07-08 22:23:49 +05:30
|
|
|
/* maybe bb_logenv_override(); here for LOGGING=syslog to work? */
|
|
|
|
|
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
|
|
|
|
2017-07-08 22:05:25 +05:30
|
|
|
bb_signals(BB_FATAL_SIGS, shutdown_on_signal);
|
2003-07-22 13:09:18 +05:30
|
|
|
|
2017-07-08 22:23:49 +05:30
|
|
|
watchdog_open(argv[optind]);
|
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;
|
2017-07-08 22:23:49 +05:30
|
|
|
ioctl_or_warn(3, WDIOC_SETOPTIONS, (void*) &enable);
|
2008-09-08 04:52:08 +05:30
|
|
|
ioctl_or_warn(3, WDIOC_SETTIMEOUT, &htimer_duration);
|
|
|
|
#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",
|
2021-04-06 13:44:53 +05:30
|
|
|
stimer_duration, htimer_duration);
|
2008-09-08 04:52:08 +05:30
|
|
|
#endif
|
|
|
|
|
2019-03-18 00:17:52 +05:30
|
|
|
write_pidfile_std_path_and_ext("watchdog");
|
2012-12-11 01:19:39 +05:30
|
|
|
|
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 */
|
2020-11-29 17:10:25 +05:30
|
|
|
msleep(stimer_duration);
|
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
|
|
|
}
|