blob: ed9026d9e449f31516fbb0c77a7170909e4c7227 [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
Mike Frysinger9be74352006-06-07 21:48:43 +000041#ifdef BB_NOMMU
Mike Frysingercd68a2e2006-06-26 21:31:17 +000042 if (!(opts & OPT_FOREGROUND))
43 vfork_daemon_rexec(0, 1, argc, argv, "-F");
Mike Frysinger9be74352006-06-07 21:48:43 +000044#else
Rob Landleyd921b2e2006-08-03 15:41:12 +000045 xdaemon(0, 1);
Mike Frysinger9be74352006-06-07 21:48:43 +000046#endif
Eric Andersencde8f532003-07-22 07:39:18 +000047
48 signal(SIGHUP, watchdog_shutdown);
49 signal(SIGINT, watchdog_shutdown);
50
Denis Vlasenkocfa2b3a2007-03-14 21:55:41 +000051 /* Use known fd # - avoid needing global 'int fd' */
52 dup2(xopen(argv[argc - 1], O_WRONLY), 3);
Eric Andersenffde8672001-01-25 23:40:32 +000053
54 while (1) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +000055 /*
Eric Andersencde8f532003-07-22 07:39:18 +000056 * Make sure we clear the counter before sleeping, as the counter value
57 * is undefined at this point -- PFM
58 */
Denis Vlasenkocfa2b3a2007-03-14 21:55:41 +000059 write(3, "", 1);
Eric Andersencde8f532003-07-22 07:39:18 +000060 sleep(timer_duration);
Eric Andersenffde8672001-01-25 23:40:32 +000061 }
62
Eric Andersencde8f532003-07-22 07:39:18 +000063 watchdog_shutdown(0);
64
Denis Vlasenkocfa2b3a2007-03-14 21:55:41 +000065 /* return EXIT_SUCCESS; */
Eric Andersenffde8672001-01-25 23:40:32 +000066}