blob: ddd349d9bc22196f0a42c0351e277c31bb09d394 [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#include <stdio.h>
13#include <fcntl.h>
Eric Andersened3ef502001-01-27 08:24:39 +000014#include <unistd.h>
15#include <stdlib.h>
Eric Andersencde8f532003-07-22 07:39:18 +000016#include <signal.h>
Eric Andersenffde8672001-01-25 23:40:32 +000017
Mike Frysingercd68a2e2006-06-26 21:31:17 +000018#define OPT_FOREGROUND 0x01
19#define OPT_TIMER 0x02
20
Eric Andersencde8f532003-07-22 07:39:18 +000021/* Watchdog file descriptor */
22static int fd;
23
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +000024static void watchdog_shutdown(int ATTRIBUTE_UNUSED unused)
Eric Andersencde8f532003-07-22 07:39:18 +000025{
Mike Frysinger2fc534f2006-06-07 21:37:49 +000026 write(fd, "V", 1); /* Magic, see watchdog-api.txt in kernel */
Eric Andersencde8f532003-07-22 07:39:18 +000027 close(fd);
28 exit(0);
29}
30
Rob Landley1b751c82005-10-28 09:24:33 +000031int watchdog_main(int argc, char **argv)
Eric Andersenffde8672001-01-25 23:40:32 +000032{
Mike Frysingercd68a2e2006-06-26 21:31:17 +000033 unsigned long opts;
Bernhard Reutner-Fischer5f6d67b2006-06-03 20:53:18 +000034 unsigned long timer_duration = 30; /* Userspace timer duration, in seconds */
Rob Landley1b751c82005-10-28 09:24:33 +000035 char *t_arg;
Bernhard Reutner-Fischer5f6d67b2006-06-03 20:53:18 +000036
Mike Frysingercd68a2e2006-06-26 21:31:17 +000037 opts = bb_getopt_ulflags(argc, argv, "Ft:", &t_arg);
38
39 if (opts & OPT_TIMER)
Rob Landley1b751c82005-10-28 09:24:33 +000040 timer_duration = bb_xgetlarg(t_arg, 10, 0, INT_MAX);
Eric Andersencde8f532003-07-22 07:39:18 +000041
42 /* We're only interested in the watchdog device .. */
43 if (optind < argc - 1 || argc == 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 bb_show_usage();
Eric Andersenffde8672001-01-25 23:40:32 +000045
Mike Frysinger9be74352006-06-07 21:48:43 +000046#ifdef BB_NOMMU
Mike Frysingercd68a2e2006-06-26 21:31:17 +000047 if (!(opts & OPT_FOREGROUND))
48 vfork_daemon_rexec(0, 1, argc, argv, "-F");
Mike Frysinger9be74352006-06-07 21:48:43 +000049#else
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +000050 bb_xdaemon(0, 1);
Mike Frysinger9be74352006-06-07 21:48:43 +000051#endif
Eric Andersencde8f532003-07-22 07:39:18 +000052
53 signal(SIGHUP, watchdog_shutdown);
54 signal(SIGINT, watchdog_shutdown);
55
56 fd = bb_xopen(argv[argc - 1], O_WRONLY);
Eric Andersenffde8672001-01-25 23:40:32 +000057
58 while (1) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +000059 /*
Eric Andersencde8f532003-07-22 07:39:18 +000060 * Make sure we clear the counter before sleeping, as the counter value
61 * is undefined at this point -- PFM
62 */
Eric Andersenffde8672001-01-25 23:40:32 +000063 write(fd, "\0", 1);
Eric Andersencde8f532003-07-22 07:39:18 +000064 sleep(timer_duration);
Eric Andersenffde8672001-01-25 23:40:32 +000065 }
66
Eric Andersencde8f532003-07-22 07:39:18 +000067 watchdog_shutdown(0);
68
69 return EXIT_SUCCESS;
Eric Andersenffde8672001-01-25 23:40:32 +000070}