xref: /freebsd/usr.sbin/watchdogd/watchdogd.c (revision 0f2bd1e89db1a2f09268edea21e0ead329e092df)
1 /*-
2  * Copyright (c) 2003-2004  Sean M. Kelly <smkelly@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Software watchdog daemon.
29  */
30 
31 #include <sys/types.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/rtprio.h>
36 #include <sys/stat.h>
37 #include <sys/time.h>
38 #include <sys/watchdog.h>
39 
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <libutil.h>
44 #include <math.h>
45 #include <paths.h>
46 #include <signal.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <sysexits.h>
51 #include <unistd.h>
52 
53 static void	parseargs(int, char *[]);
54 static void	sighandler(int);
55 static void	watchdog_loop(void);
56 static int	watchdog_init(void);
57 static int	watchdog_onoff(int onoff);
58 static int	watchdog_patpat(u_int timeout);
59 static void	usage(void);
60 
61 static int debugging = 0;
62 static int end_program = 0;
63 static const char *pidfile = _PATH_VARRUN "watchdogd.pid";
64 static u_int timeout = WD_TO_16SEC;
65 static u_int passive = 0;
66 static int is_daemon = 0;
67 static int fd = -1;
68 static int nap = 1;
69 static char *test_cmd = NULL;
70 
71 /*
72  * Periodically pat the watchdog, preventing it from firing.
73  */
74 int
75 main(int argc, char *argv[])
76 {
77 	struct rtprio rtp;
78 	struct pidfh *pfh;
79 	pid_t otherpid;
80 
81 	if (getuid() != 0)
82 		errx(EX_SOFTWARE, "not super user");
83 
84 	parseargs(argc, argv);
85 
86 	rtp.type = RTP_PRIO_REALTIME;
87 	rtp.prio = 0;
88 	if (rtprio(RTP_SET, 0, &rtp) == -1)
89 		err(EX_OSERR, "rtprio");
90 
91 	if (watchdog_init() == -1)
92 		errx(EX_SOFTWARE, "unable to initialize watchdog");
93 
94 	if (is_daemon) {
95 		if (watchdog_onoff(1) == -1)
96 			err(EX_OSERR, "patting the dog");
97 
98 		pfh = pidfile_open(pidfile, 0600, &otherpid);
99 		if (pfh == NULL) {
100 			if (errno == EEXIST) {
101 				errx(EX_SOFTWARE, "%s already running, pid: %d",
102 				    getprogname(), otherpid);
103 			}
104 			warn("Cannot open or create pidfile");
105 		}
106 
107 		if (debugging == 0 && daemon(0, 0) == -1) {
108 			watchdog_onoff(0);
109 			pidfile_remove(pfh);
110 			err(EX_OSERR, "daemon");
111 		}
112 
113 		signal(SIGHUP, SIG_IGN);
114 		signal(SIGINT, sighandler);
115 		signal(SIGTERM, sighandler);
116 
117 		pidfile_write(pfh);
118 
119 		watchdog_loop();
120 
121 		/* exiting */
122 		pidfile_remove(pfh);
123 		return (EX_OK);
124 	} else {
125 		if (passive)
126 			timeout |= WD_PASSIVE;
127 		else
128 			timeout |= WD_ACTIVE;
129 		if (watchdog_patpat(timeout) < 0)
130 			err(EX_OSERR, "patting the dog");
131 		return (EX_OK);
132 	}
133 }
134 
135 /*
136  * Catch signals and begin shutdown process.
137  */
138 static void
139 sighandler(int signum)
140 {
141 
142 	if (signum == SIGINT || signum == SIGTERM)
143 		end_program = 1;
144 }
145 
146 /*
147  * Open the watchdog device.
148  */
149 static int
150 watchdog_init(void)
151 {
152 
153 	fd = open("/dev/" _PATH_WATCHDOG, O_RDWR);
154 	if (fd >= 0)
155 		return (0);
156 	warn("Could not open watchdog device");
157 	return (-1);
158 }
159 
160 /*
161  * Main program loop which is iterated every second.
162  */
163 static void
164 watchdog_loop(void)
165 {
166 	struct stat sb;
167 	int failed;
168 
169 	while (end_program != 2) {
170 		failed = 0;
171 
172 		if (test_cmd != NULL)
173 			failed = system(test_cmd);
174 		else
175 			failed = stat("/etc", &sb);
176 
177 		if (failed == 0)
178 			watchdog_patpat(timeout|WD_ACTIVE);
179 		sleep(nap);
180 
181 		if (end_program != 0) {
182 			if (watchdog_onoff(0) == 0) {
183 				end_program = 2;
184 			} else {
185 				warnx("Could not stop the watchdog, not exitting");
186 				end_program = 0;
187 			}
188 		}
189 	}
190 }
191 
192 /*
193  * Reset the watchdog timer. This function must be called periodically
194  * to keep the watchdog from firing.
195  */
196 static int
197 watchdog_patpat(u_int t)
198 {
199 
200 	return ioctl(fd, WDIOCPATPAT, &t);
201 }
202 
203 /*
204  * Toggle the kernel's watchdog. This routine is used to enable and
205  * disable the watchdog.
206  */
207 static int
208 watchdog_onoff(int onoff)
209 {
210 
211 	if (onoff)
212 		return watchdog_patpat((timeout|WD_ACTIVE));
213 	else
214 		return watchdog_patpat(0);
215 }
216 
217 /*
218  * Tell user how to use the program.
219  */
220 static void
221 usage(void)
222 {
223 	if (is_daemon)
224 		fprintf(stderr, "usage: watchdogd [-d] [-e cmd] [-I file] [-s sleep] [-t timeout]\n");
225 	else
226 		fprintf(stderr, "usage: watchdog [-d] [-t timeout]\n");
227 	exit(EX_USAGE);
228 }
229 
230 /*
231  * Handle the few command line arguments supported.
232  */
233 static void
234 parseargs(int argc, char *argv[])
235 {
236 	int c;
237 	char *p;
238 	double a;
239 
240 	c = strlen(argv[0]);
241 	if (argv[0][c - 1] == 'd')
242 		is_daemon = 1;
243 	while ((c = getopt(argc, argv,
244 	    is_daemon ? "I:de:s:t:?" : "dt:?")) != -1) {
245 		switch (c) {
246 		case 'I':
247 			pidfile = optarg;
248 			break;
249 		case 'd':
250 			debugging = 1;
251 			break;
252 		case 'e':
253 			test_cmd = strdup(optarg);
254 			break;
255 #ifdef notyet
256 		case 'p':
257 			passive = 1;
258 			break;
259 #endif
260 		case 's':
261 			p = NULL;
262 			errno = 0;
263 			nap = strtol(optarg, &p, 0);
264 			if ((p != NULL && *p != '\0') || errno != 0)
265 				errx(EX_USAGE, "-s argument is not a number");
266 			break;
267 		case 't':
268 			p = NULL;
269 			errno = 0;
270 			a = strtod(optarg, &p);
271 			if ((p != NULL && *p != '\0') || errno != 0)
272 				errx(EX_USAGE, "-t argument is not a number");
273 			if (a < 0)
274 				errx(EX_USAGE, "-t argument must be positive");
275 			if (a == 0)
276 				timeout = WD_TO_NEVER;
277 			else
278 				timeout = 1.0 + log(a * 1e9) / log(2.0);
279 			if (debugging)
280 				printf("Timeout is 2^%d nanoseconds\n",
281 				    timeout);
282 			break;
283 		case '?':
284 		default:
285 			usage();
286 			/* NOTREACHED */
287 		}
288 	}
289 	if (argc != optind)
290 		errx(EX_USAGE, "extra arguments.");
291 	if (is_daemon && timeout < WD_TO_1SEC)
292 		errx(EX_USAGE, "-t argument is less than one second.");
293 }
294