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