blob: 14bd44f48236a2935216a658414d44aa63690d4c [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 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 Vlasenko3638cc42007-09-05 12:13:51 +000019 static const char V = 'V';
20
21 write(3, &V, 1); /* Magic, see watchdog-api.txt in kernel */
Denis Vlasenkocfa2b3a2007-03-14 21:55:41 +000022 if (ENABLE_FEATURE_CLEAN_UP)
23 close(3);
Eric Andersencde8f532003-07-22 07:39:18 +000024 exit(0);
25}
26
Denis Vlasenko06af2162007-02-03 17:28:39 +000027int watchdog_main(int argc, char **argv);
Rob Landley1b751c82005-10-28 09:24:33 +000028int watchdog_main(int argc, char **argv)
Eric Andersenffde8672001-01-25 23:40:32 +000029{
Denis Vlasenko67b23e62006-10-03 21:00:06 +000030 unsigned opts;
Denis Vlasenko3638cc42007-09-05 12:13:51 +000031 unsigned timer_duration = 30000; /* Userspace timer duration, in milliseconds */
Rob Landley1b751c82005-10-28 09:24:33 +000032 char *t_arg;
Bernhard Reutner-Fischer5f6d67b2006-06-03 20:53:18 +000033
Denis Vlasenko5a142022007-03-26 13:20:54 +000034 opt_complementary = "=1"; /* must have 1 argument */
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000035 opts = getopt32(argv, "Ft:", &t_arg);
Mike Frysingercd68a2e2006-06-26 21:31:17 +000036
Denis Vlasenko3638cc42007-09-05 12:13:51 +000037 if (opts & OPT_TIMER) {
38 static const struct suffix_mult suffixes[] = {
39 { "ms", 1 },
40 { "", 1000 },
41 { }
42 };
43 timer_duration = xatou_sfx(t_arg, suffixes);
44 }
Eric Andersencde8f532003-07-22 07:39:18 +000045
Denis Vlasenko9067f132007-03-24 12:11:17 +000046 if (!(opts & OPT_FOREGROUND)) {
Denis Vlasenko5a142022007-03-26 13:20:54 +000047 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
Denis Vlasenko9067f132007-03-24 12:11:17 +000048 }
Eric Andersencde8f532003-07-22 07:39:18 +000049
50 signal(SIGHUP, watchdog_shutdown);
51 signal(SIGINT, watchdog_shutdown);
52
Denis Vlasenkocfa2b3a2007-03-14 21:55:41 +000053 /* Use known fd # - avoid needing global 'int fd' */
Denis Vlasenko5a142022007-03-26 13:20:54 +000054 xmove_fd(xopen(argv[argc - 1], O_WRONLY), 3);
Eric Andersenffde8672001-01-25 23:40:32 +000055
56 while (1) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +000057 /*
Eric Andersencde8f532003-07-22 07:39:18 +000058 * Make sure we clear the counter before sleeping, as the counter value
59 * is undefined at this point -- PFM
60 */
Denis Vlasenko3638cc42007-09-05 12:13:51 +000061 write(3, "", 1); /* write zero byte */
62 usleep(timer_duration * 1000L);
Eric Andersenffde8672001-01-25 23:40:32 +000063 }
Denis Vlasenko3638cc42007-09-05 12:13:51 +000064 return EXIT_SUCCESS; /* - not reached, but gcc 4.2.1 is too dumb! */
Eric Andersenffde8672001-01-25 23:40:32 +000065}