blob: 4ad21cbc9f2aae01a398a223a596c44e220b25da [file] [log] [blame]
Eric Andersenffde8672001-01-25 23:40:32 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini watchdog implementation for busybox
4 *
Eric Andersencde8f532003-07-22 07:39:18 +00005 * Copyright (C) 2003 Paul Mundt <lethal@linux-sh.org>
Bernhard Reutner-Fischer5f6d67b2006-06-03 20:53:18 +00006 * Copyright (C) 2006 Bernhard Fischer <busybox@busybox.net>
Eric Andersenffde8672001-01-25 23:40:32 +00007 *
Rob Landley1b751c82005-10-28 09:24:33 +00008 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersenffde8672001-01-25 23:40:32 +00009 */
10
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000011#include "libbb.h"
Eric Andersenffde8672001-01-25 23:40:32 +000012
Mike Frysingercd68a2e2006-06-26 21:31:17 +000013#define OPT_FOREGROUND 0x01
14#define OPT_TIMER 0x02
15
Denis Vlasenkob44c7902008-03-17 09:29:43 +000016static void watchdog_shutdown(int sig ATTRIBUTE_UNUSED)
Eric Andersencde8f532003-07-22 07:39:18 +000017{
Denis Vlasenko3638cc42007-09-05 12:13:51 +000018 static const char V = 'V';
19
20 write(3, &V, 1); /* Magic, see watchdog-api.txt in kernel */
Denis Vlasenkocfa2b3a2007-03-14 21:55:41 +000021 if (ENABLE_FEATURE_CLEAN_UP)
22 close(3);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +000023 exit(EXIT_SUCCESS);
Eric Andersencde8f532003-07-22 07:39:18 +000024}
25
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000026int watchdog_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landley1b751c82005-10-28 09:24:33 +000027int watchdog_main(int argc, char **argv)
Eric Andersenffde8672001-01-25 23:40:32 +000028{
Denis Vlasenko67b23e62006-10-03 21:00:06 +000029 unsigned opts;
Denis Vlasenko3638cc42007-09-05 12:13:51 +000030 unsigned timer_duration = 30000; /* Userspace timer duration, in milliseconds */
Rob Landley1b751c82005-10-28 09:24:33 +000031 char *t_arg;
Bernhard Reutner-Fischer5f6d67b2006-06-03 20:53:18 +000032
Denis Vlasenko5a142022007-03-26 13:20:54 +000033 opt_complementary = "=1"; /* must have 1 argument */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000034 opts = getopt32(argv, "Ft:", &t_arg);
Mike Frysingercd68a2e2006-06-26 21:31:17 +000035
Denis Vlasenko3638cc42007-09-05 12:13:51 +000036 if (opts & OPT_TIMER) {
37 static const struct suffix_mult suffixes[] = {
38 { "ms", 1 },
39 { "", 1000 },
40 { }
41 };
42 timer_duration = xatou_sfx(t_arg, suffixes);
43 }
Eric Andersencde8f532003-07-22 07:39:18 +000044
Denis Vlasenko9067f132007-03-24 12:11:17 +000045 if (!(opts & OPT_FOREGROUND)) {
Denis Vlasenko5a142022007-03-26 13:20:54 +000046 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
Denis Vlasenko9067f132007-03-24 12:11:17 +000047 }
Eric Andersencde8f532003-07-22 07:39:18 +000048
Denis Vlasenkocf7cf622008-03-19 19:38:46 +000049 bb_signals(BB_FATAL_SIGS, watchdog_shutdown);
Eric Andersencde8f532003-07-22 07:39:18 +000050
Denis Vlasenkocfa2b3a2007-03-14 21:55:41 +000051 /* Use known fd # - avoid needing global 'int fd' */
Denis Vlasenko5a142022007-03-26 13:20:54 +000052 xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3);
Eric Andersenffde8672001-01-25 23:40:32 +000053
Denis Vlasenkob44c7902008-03-17 09:29:43 +000054// TODO?
55// if (!(opts & OPT_TIMER)) {
56// if (ioctl(fd, WDIOC_GETTIMEOUT, &timer_duration) == 0)
57// timer_duration *= 500;
58// else
59// timer_duration = 30000;
60// }
61
Eric Andersenffde8672001-01-25 23:40:32 +000062 while (1) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +000063 /*
Eric Andersencde8f532003-07-22 07:39:18 +000064 * Make sure we clear the counter before sleeping, as the counter value
65 * is undefined at this point -- PFM
66 */
Denis Vlasenko3638cc42007-09-05 12:13:51 +000067 write(3, "", 1); /* write zero byte */
68 usleep(timer_duration * 1000L);
Eric Andersenffde8672001-01-25 23:40:32 +000069 }
Denis Vlasenko3638cc42007-09-05 12:13:51 +000070 return EXIT_SUCCESS; /* - not reached, but gcc 4.2.1 is too dumb! */
Eric Andersenffde8672001-01-25 23:40:32 +000071}