2 * Copyright (c) 2003-2004 Sean M. Kelly <smkelly@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Software watchdog daemon.
31 #include <sys/types.h>
32 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/rtprio.h>
39 #include <sys/watchdog.h>
54 static void parseargs(int, char *[]);
55 static void sighandler(int);
56 static void watchdog_loop(void);
57 static int watchdog_init(void);
58 static int watchdog_onoff(int onoff);
59 static int watchdog_patpat(u_int timeout);
60 static void usage(void);
64 const char *pidfile = _PATH_VARRUN "watchdogd.pid";
66 size_t reset_miblen = 3;
67 u_int timeout = WD_TO_16SEC;
72 char *test_cmd = NULL;
75 * Periodically pat the watchdog, preventing it from firing.
78 main(int argc, char *argv[])
85 errx(EX_SOFTWARE, "not super user");
87 parseargs(argc, argv);
89 rtp.type = RTP_PRIO_REALTIME;
91 if (rtprio(RTP_SET, 0, &rtp) == -1)
92 err(EX_OSERR, "rtprio");
94 if (watchdog_init() == -1)
95 errx(EX_SOFTWARE, "unable to initialize watchdog");
98 if (watchdog_onoff(1) == -1)
101 pfh = pidfile_open(pidfile, 0600, &otherpid);
103 if (errno == EEXIST) {
104 errx(EX_SOFTWARE, "%s already running, pid: %d",
105 getprogname(), otherpid);
107 warn("Cannot open or create pidfile");
110 if (debugging == 0 && daemon(0, 0) == -1) {
113 err(EX_OSERR, "daemon");
116 signal(SIGHUP, SIG_IGN);
117 signal(SIGINT, sighandler);
118 signal(SIGTERM, sighandler);
121 if (madvise(0, 0, MADV_PROTECT) != 0)
122 warn("madvise failed");
131 timeout |= WD_PASSIVE;
133 timeout |= WD_ACTIVE;
134 if (watchdog_patpat(timeout) < 0)
135 err(EX_OSERR, "patting the dog");
141 * Catch signals and begin shutdown process.
144 sighandler(int signum)
147 if (signum == SIGINT || signum == SIGTERM)
152 * Open the watchdog device.
158 fd = open("/dev/" _PATH_WATCHDOG, O_RDWR);
161 warn("Could not open watchdog device");
166 * Main program loop which is iterated every second.
174 while (end_program != 2) {
177 if (test_cmd != NULL)
178 failed = system(test_cmd);
180 failed = stat("/etc", &sb);
183 watchdog_patpat(timeout|WD_ACTIVE);
186 if (end_program != 0) {
187 if (watchdog_onoff(0) == 0) {
190 warnx("Could not stop the watchdog, not exitting");
198 * Reset the watchdog timer. This function must be called periodically
199 * to keep the watchdog from firing.
202 watchdog_patpat(u_int t)
205 return ioctl(fd, WDIOCPATPAT, &t);
209 * Toggle the kernel's watchdog. This routine is used to enable and
210 * disable the watchdog.
213 watchdog_onoff(int onoff)
217 return watchdog_patpat((timeout|WD_ACTIVE));
219 return watchdog_patpat(0);
223 * Tell user how to use the program.
229 fprintf(stderr, "usage: watchdogd [-d] [-e cmd] [-I file] [-s sleep] [-t timeout]\n");
231 fprintf(stderr, "usage: watchdog [-d] [-t timeout]\n");
236 * Handle the few command line arguments supported.
239 parseargs(int argc, char *argv[])
246 if (argv[0][c - 1] == 'd')
248 while ((c = getopt(argc, argv,
249 is_daemon ? "I:de:s:t:?" : "dt:?")) != -1) {
258 test_cmd = strdup(optarg);
268 nap = strtol(optarg, &p, 0);
269 if ((p != NULL && *p != '\0') || errno != 0)
270 errx(EX_USAGE, "-s argument is not a number");
275 a = strtod(optarg, &p);
276 if ((p != NULL && *p != '\0') || errno != 0)
277 errx(EX_USAGE, "-t argument is not a number");
279 errx(EX_USAGE, "-t argument must be positive");
281 timeout = WD_TO_NEVER;
283 timeout = 1.0 + log(a * 1e9) / log(2.0);
285 printf("Timeout is 2^%d nanoseconds\n",
295 errx(EX_USAGE, "extra arguments.");
296 if (is_daemon && timeout < WD_TO_1SEC)
297 errx(EX_USAGE, "-t argument is less than one second.");