blob: 55fee8a40cce6d121ed69f02cf6195539f8cd7df [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
Eric Andersencde8f532003-07-22 07:39:18 +000018/* Watchdog file descriptor */
19static int fd;
20
Bernhard Reutner-Fischer20f40002006-01-30 17:17:14 +000021static void watchdog_shutdown(int ATTRIBUTE_UNUSED unused)
Eric Andersencde8f532003-07-22 07:39:18 +000022{
23 write(fd, "V", 1); /* Magic */
24 close(fd);
25 exit(0);
26}
27
Rob Landley1b751c82005-10-28 09:24:33 +000028int watchdog_main(int argc, char **argv)
Eric Andersenffde8672001-01-25 23:40:32 +000029{
Bernhard Reutner-Fischer5f6d67b2006-06-03 20:53:18 +000030 unsigned long 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
33 if (bb_getopt_ulflags(argc, argv, "t:", &t_arg))
Rob Landley1b751c82005-10-28 09:24:33 +000034 timer_duration = bb_xgetlarg(t_arg, 10, 0, INT_MAX);
Eric Andersencde8f532003-07-22 07:39:18 +000035
36 /* We're only interested in the watchdog device .. */
37 if (optind < argc - 1 || argc == 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000038 bb_show_usage();
Eric Andersenffde8672001-01-25 23:40:32 +000039
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +000040 bb_xdaemon(0, 1);
Eric Andersencde8f532003-07-22 07:39:18 +000041
42 signal(SIGHUP, watchdog_shutdown);
43 signal(SIGINT, watchdog_shutdown);
44
45 fd = bb_xopen(argv[argc - 1], O_WRONLY);
Eric Andersenffde8672001-01-25 23:40:32 +000046
47 while (1) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +000048 /*
Eric Andersencde8f532003-07-22 07:39:18 +000049 * Make sure we clear the counter before sleeping, as the counter value
50 * is undefined at this point -- PFM
51 */
Eric Andersenffde8672001-01-25 23:40:32 +000052 write(fd, "\0", 1);
Eric Andersencde8f532003-07-22 07:39:18 +000053 sleep(timer_duration);
Eric Andersenffde8672001-01-25 23:40:32 +000054 }
55
Eric Andersencde8f532003-07-22 07:39:18 +000056 watchdog_shutdown(0);
57
58 return EXIT_SUCCESS;
Eric Andersenffde8672001-01-25 23:40:32 +000059}