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>
55 static void parseargs(int, char *[]);
56 static void sighandler(int);
57 static void watchdog_loop(void);
58 static int watchdog_init(void);
59 static int watchdog_onoff(int onoff);
60 static int watchdog_patpat(u_int timeout);
61 static void usage(void);
65 const char *pidfile = _PATH_VARRUN "watchdogd.pid";
67 size_t reset_miblen = 3;
68 u_int timeout = WD_TO_16SEC;
73 char *test_cmd = NULL;
76 * Periodically pat the watchdog, preventing it from firing.
79 main(int argc, char *argv[])
86 errx(EX_SOFTWARE, "not super user");
88 parseargs(argc, argv);
90 rtp.type = RTP_PRIO_REALTIME;
92 if (rtprio(RTP_SET, 0, &rtp) == -1)
93 err(EX_OSERR, "rtprio");
95 if (watchdog_init() == -1)
96 errx(EX_SOFTWARE, "unable to initialize watchdog");
99 if (watchdog_onoff(1) == -1)
102 pfh = pidfile_open(pidfile, 0600, &otherpid);
104 if (errno == EEXIST) {
105 errx(EX_SOFTWARE, "%s already running, pid: %d",
106 getprogname(), otherpid);
108 warn("Cannot open or create pidfile");
111 if (debugging == 0 && daemon(0, 0) == -1) {
114 err(EX_OSERR, "daemon");
117 signal(SIGHUP, SIG_IGN);
118 signal(SIGINT, sighandler);
119 signal(SIGTERM, sighandler);
122 if (madvise(0, 0, MADV_PROTECT) != 0)
123 warn("madvise failed");
124 if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0)
125 warn("mlockall failed");
134 timeout |= WD_PASSIVE;
136 timeout |= WD_ACTIVE;
137 if (watchdog_patpat(timeout) < 0)
138 err(EX_OSERR, "patting the dog");
144 * Catch signals and begin shutdown process.
147 sighandler(int signum)
150 if (signum == SIGINT || signum == SIGTERM)
155 * Open the watchdog device.
161 fd = open("/dev/" _PATH_WATCHDOG, O_RDWR);
164 warn("Could not open watchdog device");
169 * Main program loop which is iterated every second.
177 while (end_program != 2) {
180 if (test_cmd != NULL)
181 failed = system(test_cmd);
183 failed = stat("/etc", &sb);
186 watchdog_patpat(timeout|WD_ACTIVE);
189 if (end_program != 0) {
190 if (watchdog_onoff(0) == 0) {
193 warnx("Could not stop the watchdog, not exitting");
201 * Reset the watchdog timer. This function must be called periodically
202 * to keep the watchdog from firing.
205 watchdog_patpat(u_int t)
208 return ioctl(fd, WDIOCPATPAT, &t);
212 * Toggle the kernel's watchdog. This routine is used to enable and
213 * disable the watchdog.
216 watchdog_onoff(int onoff)
220 return watchdog_patpat((timeout|WD_ACTIVE));
222 return watchdog_patpat(0);
226 * Tell user how to use the program.
232 fprintf(stderr, "usage: watchdogd [-d] [-e cmd] [-I file] [-s sleep] [-t timeout]\n");
234 fprintf(stderr, "usage: watchdog [-d] [-t timeout]\n");
239 * Handle the few command line arguments supported.
242 parseargs(int argc, char *argv[])
249 if (argv[0][c - 1] == 'd')
251 while ((c = getopt(argc, argv,
252 is_daemon ? "I:de:s:t:?" : "dt:?")) != -1) {
261 test_cmd = strdup(optarg);
271 nap = strtol(optarg, &p, 0);
272 if ((p != NULL && *p != '\0') || errno != 0)
273 errx(EX_USAGE, "-s argument is not a number");
278 a = strtod(optarg, &p);
279 if ((p != NULL && *p != '\0') || errno != 0)
280 errx(EX_USAGE, "-t argument is not a number");
282 errx(EX_USAGE, "-t argument must be positive");
284 timeout = WD_TO_NEVER;
286 timeout = flsll(a * 1e9);
288 printf("Timeout is 2^%d nanoseconds\n",
298 errx(EX_USAGE, "extra arguments.");
299 if (is_daemon && timeout < WD_TO_1SEC)
300 errx(EX_USAGE, "-t argument is less than one second.");