blob: ebbab2df3cf2cc15087f506254fc7bd4ae963d2d [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
Rob Landley1b751c82005-10-28 09:24:33 +000026int watchdog_main(int argc, char **argv)
Eric Andersenffde8672001-01-25 23:40:32 +000027{
Mike Frysingercd68a2e2006-06-26 21:31:17 +000028 unsigned long opts;
Bernhard Reutner-Fischer5f6d67b2006-06-03 20:53:18 +000029 unsigned long 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
Mike Frysingercd68a2e2006-06-26 21:31:17 +000032 opts = bb_getopt_ulflags(argc, argv, "Ft:", &t_arg);
33
34 if (opts & OPT_TIMER)
Rob Landley1b751c82005-10-28 09:24:33 +000035 timer_duration = bb_xgetlarg(t_arg, 10, 0, INT_MAX);
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
Rob Landleyd921b2e2006-08-03 15:41:12 +000051 fd = xopen(argv[argc - 1], O_WRONLY);
Eric Andersenffde8672001-01-25 23:40:32 +000052
53 while (1) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +000054 /*
Eric Andersencde8f532003-07-22 07:39:18 +000055 * Make sure we clear the counter before sleeping, as the counter value
56 * is undefined at this point -- PFM
57 */
Eric Andersenffde8672001-01-25 23:40:32 +000058 write(fd, "\0", 1);
Eric Andersencde8f532003-07-22 07:39:18 +000059 sleep(timer_duration);
Eric Andersenffde8672001-01-25 23:40:32 +000060 }
61
Eric Andersencde8f532003-07-22 07:39:18 +000062 watchdog_shutdown(0);
63
64 return EXIT_SUCCESS;
Eric Andersenffde8672001-01-25 23:40:32 +000065}