blob: e8275e68e0baa3750247f338f9b8ef558c944f33 [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{
Mike Frysinger2fc534f2006-06-07 21:37:49 +000023 write(fd, "V", 1); /* Magic, see watchdog-api.txt in kernel */
Eric Andersencde8f532003-07-22 07:39:18 +000024 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
Mike Frysinger9be74352006-06-07 21:48:43 +000040#ifdef BB_NOMMU
41 vfork_daemon(0, 1);
42#else
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +000043 bb_xdaemon(0, 1);
Mike Frysinger9be74352006-06-07 21:48:43 +000044#endif
Eric Andersencde8f532003-07-22 07:39:18 +000045
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}