blob: 28ae9bdb04a485097fb819a7a01d51431985ef51 [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
Bernhard Reutner-Fischerc89982d2006-06-03 19:49:21 +000011#include "busybox.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 Vlasenkocfa2b3a2007-03-14 21:55:41 +000016static void watchdog_shutdown(int ATTRIBUTE_UNUSED sig) ATTRIBUTE_NORETURN;
17static void watchdog_shutdown(int ATTRIBUTE_UNUSED sig)
Eric Andersencde8f532003-07-22 07:39:18 +000018{
Denis Vlasenkocfa2b3a2007-03-14 21:55:41 +000019 write(3, "V", 1); /* Magic, see watchdog-api.txt in kernel */
20 if (ENABLE_FEATURE_CLEAN_UP)
21 close(3);
Eric Andersencde8f532003-07-22 07:39:18 +000022 exit(0);
23}
24
Denis Vlasenko06af2162007-02-03 17:28:39 +000025int watchdog_main(int argc, char **argv);
Rob Landley1b751c82005-10-28 09:24:33 +000026int watchdog_main(int argc, char **argv)
Eric Andersenffde8672001-01-25 23:40:32 +000027{
Denis Vlasenko67b23e62006-10-03 21:00:06 +000028 unsigned opts;
Denis Vlasenko13858992006-10-08 12:49:22 +000029 unsigned timer_duration = 30; /* Userspace timer duration, in seconds */
Rob Landley1b751c82005-10-28 09:24:33 +000030 char *t_arg;
Bernhard Reutner-Fischer5f6d67b2006-06-03 20:53:18 +000031
Denis Vlasenko5a142022007-03-26 13:20:54 +000032 opt_complementary = "=1"; /* must have 1 argument */
Denis Vlasenko67b23e62006-10-03 21:00:06 +000033 opts = getopt32(argc, argv, "Ft:", &t_arg);
Mike Frysingercd68a2e2006-06-26 21:31:17 +000034
35 if (opts & OPT_TIMER)
Denis Vlasenko13858992006-10-08 12:49:22 +000036 timer_duration = xatou(t_arg);
Eric Andersencde8f532003-07-22 07:39:18 +000037
Denis Vlasenko9067f132007-03-24 12:11:17 +000038 if (!(opts & OPT_FOREGROUND)) {
Denis Vlasenko5a142022007-03-26 13:20:54 +000039 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
Denis Vlasenko9067f132007-03-24 12:11:17 +000040 }
Eric Andersencde8f532003-07-22 07:39:18 +000041
42 signal(SIGHUP, watchdog_shutdown);
43 signal(SIGINT, watchdog_shutdown);
44
Denis Vlasenkocfa2b3a2007-03-14 21:55:41 +000045 /* Use known fd # - avoid needing global 'int fd' */
Denis Vlasenko5a142022007-03-26 13:20:54 +000046 xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3);
Eric Andersenffde8672001-01-25 23:40:32 +000047
48 while (1) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +000049 /*
Eric Andersencde8f532003-07-22 07:39:18 +000050 * Make sure we clear the counter before sleeping, as the counter value
51 * is undefined at this point -- PFM
52 */
Denis Vlasenkocfa2b3a2007-03-14 21:55:41 +000053 write(3, "", 1);
Eric Andersencde8f532003-07-22 07:39:18 +000054 sleep(timer_duration);
Eric Andersenffde8672001-01-25 23:40:32 +000055 }
56
Eric Andersencde8f532003-07-22 07:39:18 +000057 watchdog_shutdown(0);
Denis Vlasenkocfa2b3a2007-03-14 21:55:41 +000058 /* return EXIT_SUCCESS; */
Eric Andersenffde8672001-01-25 23:40:32 +000059}