blob: 55dd69857e1ec1fb9406f2ad686f67c860a18f82 [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>
Eric Andersenffde8672001-01-25 23:40:32 +00006 *
Rob Landley1b751c82005-10-28 09:24:33 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersenffde8672001-01-25 23:40:32 +00008 */
9
Eric Andersenffde8672001-01-25 23:40:32 +000010#include <stdio.h>
11#include <fcntl.h>
Eric Andersened3ef502001-01-27 08:24:39 +000012#include <unistd.h>
13#include <stdlib.h>
Eric Andersencde8f532003-07-22 07:39:18 +000014#include <signal.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000015#include "busybox.h"
Eric Andersenffde8672001-01-25 23:40:32 +000016
Eric Andersencde8f532003-07-22 07:39:18 +000017/* Userspace timer duration, in seconds */
18static unsigned int timer_duration = 30;
19
20/* Watchdog file descriptor */
21static int fd;
22
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +000023static void watchdog_shutdown(int ATTRIBUTE_UNUSED unused)
Eric Andersencde8f532003-07-22 07:39:18 +000024{
25 write(fd, "V", 1); /* Magic */
26 close(fd);
27 exit(0);
28}
29
Rob Landley1b751c82005-10-28 09:24:33 +000030int watchdog_main(int argc, char **argv)
Eric Andersenffde8672001-01-25 23:40:32 +000031{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000032
Rob Landley1b751c82005-10-28 09:24:33 +000033 char *t_arg;
34 unsigned long flags;
35 flags = bb_getopt_ulflags(argc, argv, "t:", &t_arg);
36 if (flags & 1)
37 timer_duration = bb_xgetlarg(t_arg, 10, 0, INT_MAX);
Eric Andersencde8f532003-07-22 07:39:18 +000038
39 /* We're only interested in the watchdog device .. */
40 if (optind < argc - 1 || argc == 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000041 bb_show_usage();
Eric Andersenffde8672001-01-25 23:40:32 +000042
Eric Andersencde8f532003-07-22 07:39:18 +000043 if (daemon(0, 1) < 0)
44 bb_perror_msg_and_die("Failed forking watchdog daemon");
45
46 signal(SIGHUP, watchdog_shutdown);
47 signal(SIGINT, watchdog_shutdown);
48
49 fd = bb_xopen(argv[argc - 1], O_WRONLY);
Eric Andersenffde8672001-01-25 23:40:32 +000050
51 while (1) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +000052 /*
Eric Andersencde8f532003-07-22 07:39:18 +000053 * Make sure we clear the counter before sleeping, as the counter value
54 * is undefined at this point -- PFM
55 */
Eric Andersenffde8672001-01-25 23:40:32 +000056 write(fd, "\0", 1);
Eric Andersencde8f532003-07-22 07:39:18 +000057 sleep(timer_duration);
Eric Andersenffde8672001-01-25 23:40:32 +000058 }
59
Eric Andersencde8f532003-07-22 07:39:18 +000060 watchdog_shutdown(0);
61
62 return EXIT_SUCCESS;
Eric Andersenffde8672001-01-25 23:40:32 +000063}