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