blob: e3d77d17eed165a4daeb069cf80f554d059a8fb3 [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 Vlasenko67b23e62006-10-03 21:00:06 +000032 opts = getopt32(argc, argv, "Ft:", &t_arg);
Mike Frysingercd68a2e2006-06-26 21:31:17 +000033
34 if (opts & OPT_TIMER)
Denis Vlasenko13858992006-10-08 12:49:22 +000035 timer_duration = xatou(t_arg);
Eric Andersencde8f532003-07-22 07:39:18 +000036
37 /* We're only interested in the watchdog device .. */
38 if (optind < argc - 1 || argc == 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 bb_show_usage();
Eric Andersenffde8672001-01-25 23:40:32 +000040
Denis Vlasenko9067f132007-03-24 12:11:17 +000041 if (!(opts & OPT_FOREGROUND)) {
Mike Frysinger9be74352006-06-07 21:48:43 +000042#ifdef BB_NOMMU
Denis Vlasenko9067f132007-03-24 12:11:17 +000043 if (!re_execed)
44 vfork_daemon_rexec(0, 1, argv);
Mike Frysinger9be74352006-06-07 21:48:43 +000045#else
Denis Vlasenko9067f132007-03-24 12:11:17 +000046 xdaemon(0, 1);
Mike Frysinger9be74352006-06-07 21:48:43 +000047#endif
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' */
54 dup2(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 Vlasenkocfa2b3a2007-03-14 21:55:41 +000061 write(3, "", 1);
Eric Andersencde8f532003-07-22 07:39:18 +000062 sleep(timer_duration);
Eric Andersenffde8672001-01-25 23:40:32 +000063 }
64
Eric Andersencde8f532003-07-22 07:39:18 +000065 watchdog_shutdown(0);
66
Denis Vlasenkocfa2b3a2007-03-14 21:55:41 +000067 /* return EXIT_SUCCESS; */
Eric Andersenffde8672001-01-25 23:40:32 +000068}