blob: 9dbefb985ee343daa694e60e03cdd05931eaaca9 [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
Eric Andersencde8f532003-07-22 07:39:18 +000016/* Watchdog file descriptor */
17static int fd;
18
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +000019static void watchdog_shutdown(int ATTRIBUTE_UNUSED unused)
Eric Andersencde8f532003-07-22 07:39:18 +000020{
Mike Frysinger2fc534f2006-06-07 21:37:49 +000021 write(fd, "V", 1); /* Magic, see watchdog-api.txt in kernel */
Eric Andersencde8f532003-07-22 07:39:18 +000022 close(fd);
23 exit(0);
24}
25
Denis Vlasenko06af2162007-02-03 17:28:39 +000026int watchdog_main(int argc, char **argv);
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 Vlasenko13858992006-10-08 12:49:22 +000030 unsigned timer_duration = 30; /* Userspace timer duration, in seconds */
Rob Landley1b751c82005-10-28 09:24:33 +000031 char *t_arg;
Bernhard Reutner-Fischer5f6d67b2006-06-03 20:53:18 +000032
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
38 /* We're only interested in the watchdog device .. */
39 if (optind < argc - 1 || argc == 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000040 bb_show_usage();
Eric Andersenffde8672001-01-25 23:40:32 +000041
Mike Frysinger9be74352006-06-07 21:48:43 +000042#ifdef BB_NOMMU
Mike Frysingercd68a2e2006-06-26 21:31:17 +000043 if (!(opts & OPT_FOREGROUND))
44 vfork_daemon_rexec(0, 1, argc, argv, "-F");
Mike Frysinger9be74352006-06-07 21:48:43 +000045#else
Rob Landleyd921b2e2006-08-03 15:41:12 +000046 xdaemon(0, 1);
Mike Frysinger9be74352006-06-07 21:48:43 +000047#endif
Eric Andersencde8f532003-07-22 07:39:18 +000048
49 signal(SIGHUP, watchdog_shutdown);
50 signal(SIGINT, watchdog_shutdown);
51
Rob Landleyd921b2e2006-08-03 15:41:12 +000052 fd = xopen(argv[argc - 1], O_WRONLY);
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 */
Eric Andersenffde8672001-01-25 23:40:32 +000059 write(fd, "\0", 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
65 return EXIT_SUCCESS;
Eric Andersenffde8672001-01-25 23:40:32 +000066}