blob: 58a6065536f1d5c5b4da7ae28e38269eaaa69719 [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
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +000043 bb_xdaemon(0, 1);
Eric Andersencde8f532003-07-22 07:39:18 +000044
45 signal(SIGHUP, watchdog_shutdown);
46 signal(SIGINT, watchdog_shutdown);
47
48 fd = bb_xopen(argv[argc - 1], O_WRONLY);
Eric Andersenffde8672001-01-25 23:40:32 +000049
50 while (1) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +000051 /*
Eric Andersencde8f532003-07-22 07:39:18 +000052 * Make sure we clear the counter before sleeping, as the counter value
53 * is undefined at this point -- PFM
54 */
Eric Andersenffde8672001-01-25 23:40:32 +000055 write(fd, "\0", 1);
Eric Andersencde8f532003-07-22 07:39:18 +000056 sleep(timer_duration);
Eric Andersenffde8672001-01-25 23:40:32 +000057 }
58
Eric Andersencde8f532003-07-22 07:39:18 +000059 watchdog_shutdown(0);
60
61 return EXIT_SUCCESS;
Eric Andersenffde8672001-01-25 23:40:32 +000062}