xref: /freebsd/usr.sbin/watchdogd/watchdogd.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2003-2004  Sean M. Kelly <smkelly@FreeBSD.org>
5  * Copyright (c) 2013 iXsystems.com,
6  *                    author: Alfred Perlstein <alfred@freebsd.org>
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Software watchdog daemon.
34  */
35 
36 #include <sys/types.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/mman.h>
40 #include <sys/param.h>
41 #include <sys/rtprio.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <sys/sysctl.h>
45 #include <sys/watchdog.h>
46 
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <libutil.h>
51 #include <math.h>
52 #include <paths.h>
53 #include <signal.h>
54 #include <stdio.h>
55 #include <stdint.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <strings.h>
59 #include <sysexits.h>
60 #include <syslog.h>
61 #include <unistd.h>
62 
63 #include <getopt.h>
64 
65 static long	fetchtimeout(int opt,
66     const char *longopt, const char *myoptarg, int zero_ok);
67 static void	parseargs(int, char *[]);
68 static int	seconds_to_pow2ns(int);
69 static void	sighandler(int);
70 static void	watchdog_loop(void);
71 static int	watchdog_init(void);
72 static int	watchdog_onoff(int onoff);
73 static int	watchdog_patpat(u_int timeout);
74 static void	usage(void);
75 static int	tstotv(struct timeval *tv, struct timespec *ts);
76 static int	tvtohz(struct timeval *tv);
77 
78 static int debugging = 0;
79 static int end_program = 0;
80 static const char *pidfile = _PATH_VARRUN "watchdogd.pid";
81 static u_int timeout = WD_TO_128SEC;
82 static u_int exit_timeout = WD_TO_NEVER;
83 static u_int pretimeout = 0;
84 static u_int timeout_sec;
85 static u_int nap = 10;
86 static int passive = 0;
87 static int is_daemon = 0;
88 static int is_dry_run = 0;  /* do not arm the watchdog, only
89 			       report on timing of the watch
90 			       program */
91 static int do_timedog = 0;
92 static int do_syslog = 1;
93 static int fd = -1;
94 static int carp_thresh_seconds = -1;
95 static char *test_cmd = NULL;
96 
97 static const char *getopt_shortopts;
98 
99 static int pretimeout_set;
100 static int pretimeout_act;
101 static int pretimeout_act_set;
102 
103 static int softtimeout_set;
104 static int softtimeout_act;
105 static int softtimeout_act_set;
106 
107 static struct option longopts[] = {
108 	{ "debug", no_argument, &debugging, 1 },
109 	{ "pretimeout", required_argument, &pretimeout_set, 1 },
110 	{ "pretimeout-action", required_argument, &pretimeout_act_set, 1 },
111 	{ "softtimeout", no_argument, &softtimeout_set, 1 },
112 	{ "softtimeout-action", required_argument, &softtimeout_act_set, 1 },
113 	{ NULL, 0, NULL, 0}
114 };
115 
116 /*
117  * Periodically pat the watchdog, preventing it from firing.
118  */
119 int
120 main(int argc, char *argv[])
121 {
122 	struct rtprio rtp;
123 	struct pidfh *pfh;
124 	pid_t otherpid;
125 
126 	if (getuid() != 0)
127 		errx(EX_SOFTWARE, "not super user");
128 
129 	parseargs(argc, argv);
130 
131 	if (do_syslog)
132 		openlog("watchdogd", LOG_CONS|LOG_NDELAY|LOG_PERROR,
133 		    LOG_DAEMON);
134 
135 	rtp.type = RTP_PRIO_REALTIME;
136 	rtp.prio = 0;
137 	if (rtprio(RTP_SET, 0, &rtp) == -1)
138 		err(EX_OSERR, "rtprio");
139 
140 	if (!is_dry_run && watchdog_init() == -1)
141 		errx(EX_SOFTWARE, "unable to initialize watchdog");
142 
143 	if (is_daemon) {
144 		if (watchdog_onoff(1) == -1)
145 			err(EX_OSERR, "patting the dog");
146 
147 		pfh = pidfile_open(pidfile, 0600, &otherpid);
148 		if (pfh == NULL) {
149 			if (errno == EEXIST) {
150 				watchdog_onoff(0);
151 				errx(EX_SOFTWARE, "%s already running, pid: %d",
152 				    getprogname(), otherpid);
153 			}
154 			warn("Cannot open or create pidfile");
155 		}
156 
157 		if (debugging == 0 && daemon(0, 0) == -1) {
158 			watchdog_onoff(0);
159 			pidfile_remove(pfh);
160 			err(EX_OSERR, "daemon");
161 		}
162 
163 		signal(SIGHUP, SIG_IGN);
164 		signal(SIGINT, sighandler);
165 		signal(SIGTERM, sighandler);
166 
167 		pidfile_write(pfh);
168 		if (madvise(0, 0, MADV_PROTECT) != 0)
169 			warn("madvise failed");
170 		if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0)
171 			warn("mlockall failed");
172 
173 		watchdog_loop();
174 
175 		/* exiting */
176 		pidfile_remove(pfh);
177 		return (EX_OK);
178 	} else {
179 		if (passive)
180 			timeout |= WD_PASSIVE;
181 		else
182 			timeout |= WD_ACTIVE;
183 		if (watchdog_patpat(timeout) < 0)
184 			err(EX_OSERR, "patting the dog");
185 		return (EX_OK);
186 	}
187 }
188 
189 static void
190 pow2ns_to_ts(int pow2ns, struct timespec *ts)
191 {
192 	uint64_t ns;
193 
194 	ns = 1ULL << pow2ns;
195 	ts->tv_sec = ns / 1000000000ULL;
196 	ts->tv_nsec = ns % 1000000000ULL;
197 }
198 
199 /*
200  * Convert a timeout in seconds to N where 2^N nanoseconds is close to
201  * "seconds".
202  *
203  * The kernel expects the timeouts for watchdogs in "2^N nanosecond format".
204  */
205 static u_int
206 parse_timeout_to_pow2ns(char opt, const char *longopt, const char *myoptarg)
207 {
208 	double a;
209 	u_int rv;
210 	struct timespec ts;
211 	struct timeval tv;
212 	int ticks;
213 	char shortopt[] = "- ";
214 
215 	if (!longopt)
216 		shortopt[1] = opt;
217 
218 	a = fetchtimeout(opt, longopt, myoptarg, 1);
219 
220 	if (a == 0)
221 		rv = WD_TO_NEVER;
222 	else
223 		rv = seconds_to_pow2ns(a);
224 	pow2ns_to_ts(rv, &ts);
225 	tstotv(&tv, &ts);
226 	ticks = tvtohz(&tv);
227 	if (debugging) {
228 		printf("Timeout for %s%s "
229 		    "is 2^%d nanoseconds "
230 		    "(in: %s sec -> out: %jd sec %ld ns -> %d ticks)\n",
231 		    longopt ? "-" : "", longopt ? longopt : shortopt,
232 		    rv,
233 		    myoptarg, (intmax_t)ts.tv_sec, ts.tv_nsec, ticks);
234 	}
235 	if (ticks <= 0) {
236 		errx(1, "Timeout for %s%s is too small, please choose a higher timeout.", longopt ? "-" : "", longopt ? longopt : shortopt);
237 	}
238 
239 	return (rv);
240 }
241 
242 /*
243  * Catch signals and begin shutdown process.
244  */
245 static void
246 sighandler(int signum)
247 {
248 
249 	if (signum == SIGINT || signum == SIGTERM)
250 		end_program = 1;
251 }
252 
253 /*
254  * Open the watchdog device.
255  */
256 static int
257 watchdog_init(void)
258 {
259 
260 	if (is_dry_run)
261 		return 0;
262 
263 	fd = open("/dev/" _PATH_WATCHDOG, O_RDWR);
264 	if (fd >= 0)
265 		return (0);
266 	warn("Could not open watchdog device");
267 	return (-1);
268 }
269 
270 /*
271  * If we are doing timing, then get the time.
272  */
273 static int
274 watchdog_getuptime(struct timespec *tp)
275 {
276 	int error;
277 
278 	if (!do_timedog)
279 		return 0;
280 
281 	error = clock_gettime(CLOCK_UPTIME_FAST, tp);
282 	if (error)
283 		warn("clock_gettime");
284 	return (error);
285 }
286 
287 static long
288 watchdog_check_dogfunction_time(struct timespec *tp_start,
289     struct timespec *tp_end)
290 {
291 	struct timeval tv_start, tv_end, tv_now, tv;
292 	const char *cmd_prefix, *cmd;
293 	struct timespec tp_now;
294 	int sec;
295 
296 	if (!do_timedog)
297 		return (0);
298 
299 	TIMESPEC_TO_TIMEVAL(&tv_start, tp_start);
300 	TIMESPEC_TO_TIMEVAL(&tv_end, tp_end);
301 	timersub(&tv_end, &tv_start, &tv);
302 	sec = tv.tv_sec;
303 	if (sec < carp_thresh_seconds)
304 		return (sec);
305 
306 	if (test_cmd) {
307 		cmd_prefix = "Watchdog program";
308 		cmd = test_cmd;
309 	} else {
310 		cmd_prefix = "Watchdog operation";
311 		cmd = "stat(\"/etc\", &sb)";
312 	}
313 	if (do_syslog)
314 		syslog(LOG_CRIT, "%s: '%s' took too long: "
315 		    "%d.%06ld seconds >= %d seconds threshold",
316 		    cmd_prefix, cmd, sec, (long)tv.tv_usec,
317 		    carp_thresh_seconds);
318 	else
319 		warnx("%s: '%s' took too long: "
320 		    "%d.%06ld seconds >= %d seconds threshold",
321 		    cmd_prefix, cmd, sec, (long)tv.tv_usec,
322 		    carp_thresh_seconds);
323 
324 	/*
325 	 * Adjust the sleep interval again in case syslog(3) took a non-trivial
326 	 * amount of time to run.
327 	 */
328 	if (watchdog_getuptime(&tp_now))
329 		return (sec);
330 	TIMESPEC_TO_TIMEVAL(&tv_now, &tp_now);
331 	timersub(&tv_now, &tv_start, &tv);
332 	sec = tv.tv_sec;
333 
334 	return (sec);
335 }
336 
337 /*
338  * Main program loop which is iterated every second.
339  */
340 static void
341 watchdog_loop(void)
342 {
343 	struct timespec ts_start, ts_end;
344 	struct stat sb;
345 	long waited;
346 	int error, failed;
347 
348 	while (end_program != 2) {
349 		failed = 0;
350 
351 		error = watchdog_getuptime(&ts_start);
352 		if (error) {
353 			end_program = 1;
354 			goto try_end;
355 		}
356 
357 		if (test_cmd != NULL)
358 			failed = system(test_cmd);
359 		else
360 			failed = stat("/etc", &sb);
361 
362 		error = watchdog_getuptime(&ts_end);
363 		if (error) {
364 			end_program = 1;
365 			goto try_end;
366 		}
367 
368 		if (failed == 0)
369 			watchdog_patpat(timeout|WD_ACTIVE);
370 
371 		waited = watchdog_check_dogfunction_time(&ts_start, &ts_end);
372 		if (nap - waited > 0)
373 			sleep(nap - waited);
374 
375 try_end:
376 		if (end_program != 0) {
377 			if (watchdog_onoff(0) == 0) {
378 				end_program = 2;
379 			} else {
380 				warnx("Could not stop the watchdog, not exiting");
381 				end_program = 0;
382 			}
383 		}
384 	}
385 }
386 
387 /*
388  * Reset the watchdog timer. This function must be called periodically
389  * to keep the watchdog from firing.
390  */
391 static int
392 watchdog_patpat(u_int t)
393 {
394 
395 	if (is_dry_run)
396 		return 0;
397 
398 	return ioctl(fd, WDIOCPATPAT, &t);
399 }
400 
401 /*
402  * Toggle the kernel's watchdog. This routine is used to enable and
403  * disable the watchdog.
404  */
405 static int
406 watchdog_onoff(int onoff)
407 {
408 	int error;
409 
410 	/* fake successful watchdog op if a dry run */
411 	if (is_dry_run)
412 		return 0;
413 
414 	if (onoff) {
415 		/*
416 		 * Call the WDIOC_SETSOFT regardless of softtimeout_set
417 		 * because we'll need to turn it off if someone had turned
418 		 * it on.
419 		 */
420 		error = ioctl(fd, WDIOC_SETSOFT, &softtimeout_set);
421 		if (error) {
422 			warn("setting WDIOC_SETSOFT %d", softtimeout_set);
423 			return (error);
424 		}
425 		error = watchdog_patpat((timeout|WD_ACTIVE));
426 		if (error) {
427 			warn("watchdog_patpat failed");
428 			goto failsafe;
429 		}
430 		if (softtimeout_act_set) {
431 			error = ioctl(fd, WDIOC_SETSOFTTIMEOUTACT,
432 			    &softtimeout_act);
433 			if (error) {
434 				warn("setting WDIOC_SETSOFTTIMEOUTACT %d",
435 				    softtimeout_act);
436 				goto failsafe;
437 			}
438 		}
439 		if (pretimeout_set) {
440 			error = ioctl(fd, WDIOC_SETPRETIMEOUT, &pretimeout);
441 			if (error) {
442 				warn("setting WDIOC_SETPRETIMEOUT %d",
443 				    pretimeout);
444 				goto failsafe;
445 			}
446 		}
447 		if (pretimeout_act_set) {
448 			error = ioctl(fd, WDIOC_SETPRETIMEOUTACT,
449 			    &pretimeout_act);
450 			if (error) {
451 				warn("setting WDIOC_SETPRETIMEOUTACT %d",
452 				    pretimeout_act);
453 				goto failsafe;
454 			}
455 		}
456 		/* pat one more time for good measure */
457 		return watchdog_patpat((timeout|WD_ACTIVE));
458 	 } else {
459 		return watchdog_patpat(exit_timeout);
460 	 }
461 failsafe:
462 	watchdog_patpat(exit_timeout);
463 	return (error);
464 }
465 
466 /*
467  * Tell user how to use the program.
468  */
469 static void
470 usage(void)
471 {
472 	if (is_daemon)
473 		fprintf(stderr, "usage:\n"
474 "  watchdogd [-dnSw] [-e cmd] [-I pidfile] [-s sleep] [-t timeout]\n"
475 "            [-T script_timeout] [-x exit_timeout]\n"
476 "            [--debug]\n"
477 "            [--pretimeout seconds] [-pretimeout-action action]\n"
478 "            [--softtimeout] [-softtimeout-action action]\n"
479 );
480 	else
481 		fprintf(stderr, "usage: watchdog [-d] [-t timeout]\n");
482 	exit(EX_USAGE);
483 }
484 
485 static long
486 fetchtimeout(int opt, const char *longopt, const char *myoptarg, int zero_ok)
487 {
488 	const char *errstr;
489 	char *p;
490 	long rv;
491 
492 	errstr = NULL;
493 	p = NULL;
494 	errno = 0;
495 	rv = strtol(myoptarg, &p, 0);
496 	if ((p != NULL && *p != '\0') || errno != 0)
497 		errstr = "is not a number";
498 	if (rv < 0 || (!zero_ok && rv == 0))
499 		errstr = "must be greater than zero";
500 	if (errstr) {
501 		if (longopt)
502 			errx(EX_USAGE, "--%s argument %s", longopt, errstr);
503 		else
504 			errx(EX_USAGE, "-%c argument %s", opt, errstr);
505 	}
506 	return (rv);
507 }
508 
509 struct act_tbl {
510 	const char *at_act;
511 	int at_value;
512 };
513 
514 static const struct act_tbl act_tbl[] = {
515 	{ "panic", WD_SOFT_PANIC },
516 	{ "ddb", WD_SOFT_DDB },
517 	{ "log", WD_SOFT_LOG },
518 	{ "printf", WD_SOFT_PRINTF },
519 	{ NULL, 0 }
520 };
521 
522 static void
523 timeout_act_error(const char *lopt, const char *badact)
524 {
525 	char *opts, *oldopts;
526 	int i;
527 
528 	opts = NULL;
529 	for (i = 0; act_tbl[i].at_act != NULL; i++) {
530 		oldopts = opts;
531 		if (asprintf(&opts, "%s%s%s",
532 		    oldopts == NULL ? "" : oldopts,
533 		    oldopts == NULL ? "" : ", ",
534 		    act_tbl[i].at_act) == -1)
535 			err(EX_OSERR, "malloc");
536 		free(oldopts);
537 	}
538 	warnx("bad --%s argument '%s' must be one of (%s).",
539 	    lopt, badact, opts);
540 	usage();
541 }
542 
543 /*
544  * Take a comma separated list of actions and or the flags
545  * together for the ioctl.
546  */
547 static int
548 timeout_act_str2int(const char *lopt, const char *acts)
549 {
550 	int i;
551 	char *dupacts, *tofree;
552 	char *o;
553 	int rv = 0;
554 
555 	tofree = dupacts = strdup(acts);
556 	if (!tofree)
557 		err(EX_OSERR, "malloc");
558 	while ((o = strsep(&dupacts, ",")) != NULL) {
559 		for (i = 0; act_tbl[i].at_act != NULL; i++) {
560 			if (!strcmp(o, act_tbl[i].at_act)) {
561 				rv |= act_tbl[i].at_value;
562 				break;
563 			}
564 		}
565 		if (act_tbl[i].at_act == NULL)
566 			timeout_act_error(lopt, o);
567 	}
568 	free(tofree);
569 	return rv;
570 }
571 
572 int
573 tstotv(struct timeval *tv, struct timespec *ts)
574 {
575 
576 	tv->tv_sec = ts->tv_sec;
577 	tv->tv_usec = ts->tv_nsec / 1000;
578 	return 0;
579 }
580 
581 /*
582  * Convert a timeval to a number of ticks.
583  * Mostly copied from the kernel.
584  */
585 int
586 tvtohz(struct timeval *tv)
587 {
588 	register unsigned long ticks;
589 	register long sec, usec;
590 	int hz;
591 	size_t hzsize;
592 	int error;
593 	int tick;
594 
595 	hzsize = sizeof(hz);
596 
597 	error = sysctlbyname("kern.hz", &hz, &hzsize, NULL, 0);
598 	if (error)
599 		err(1, "sysctlbyname kern.hz");
600 
601 	tick = 1000000 / hz;
602 
603 	/*
604 	 * If the number of usecs in the whole seconds part of the time
605 	 * difference fits in a long, then the total number of usecs will
606 	 * fit in an unsigned long.  Compute the total and convert it to
607 	 * ticks, rounding up and adding 1 to allow for the current tick
608 	 * to expire.  Rounding also depends on unsigned long arithmetic
609 	 * to avoid overflow.
610 	 *
611 	 * Otherwise, if the number of ticks in the whole seconds part of
612 	 * the time difference fits in a long, then convert the parts to
613 	 * ticks separately and add, using similar rounding methods and
614 	 * overflow avoidance.  This method would work in the previous
615 	 * case but it is slightly slower and assumes that hz is integral.
616 	 *
617 	 * Otherwise, round the time difference down to the maximum
618 	 * representable value.
619 	 *
620 	 * If ints have 32 bits, then the maximum value for any timeout in
621 	 * 10ms ticks is 248 days.
622 	 */
623 	sec = tv->tv_sec;
624 	usec = tv->tv_usec;
625 	if (usec < 0) {
626 		sec--;
627 		usec += 1000000;
628 	}
629 	if (sec < 0) {
630 #ifdef DIAGNOSTIC
631 		if (usec > 0) {
632 			sec++;
633 			usec -= 1000000;
634 		}
635 		printf("tvotohz: negative time difference %ld sec %ld usec\n",
636 		    sec, usec);
637 #endif
638 		ticks = 1;
639 	} else if (sec <= LONG_MAX / 1000000)
640 		ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
641 		    / tick + 1;
642 	else if (sec <= LONG_MAX / hz)
643 		ticks = sec * hz
644 		    + ((unsigned long)usec + (tick - 1)) / tick + 1;
645 	else
646 		ticks = LONG_MAX;
647 	if (ticks > INT_MAX)
648 		ticks = INT_MAX;
649 	return ((int)ticks);
650 }
651 
652 static int
653 seconds_to_pow2ns(int seconds)
654 {
655 	uint64_t power;
656 	uint64_t ns;
657 	uint64_t shifted;
658 
659 	if (seconds <= 0)
660 		errx(1, "seconds %d < 0", seconds);
661 	ns = ((uint64_t)seconds) * 1000000000ULL;
662 	power = flsll(ns);
663 	shifted = 1ULL << power;
664 	if (shifted <= ns) {
665 		power++;
666 	}
667 	if (debugging) {
668 		printf("shifted %lld\n", (long long)shifted);
669 		printf("seconds_to_pow2ns: seconds: %d, ns %lld, power %d\n",
670 		    seconds, (long long)ns, (int)power);
671 	}
672 	return (power);
673 }
674 
675 
676 /*
677  * Handle the few command line arguments supported.
678  */
679 static void
680 parseargs(int argc, char *argv[])
681 {
682 	struct timespec ts;
683 	int longindex;
684 	int c;
685 	const char *lopt;
686 
687 	/* Get the default value of timeout_sec from the default timeout. */
688 	pow2ns_to_ts(timeout, &ts);
689 	timeout_sec = ts.tv_sec;
690 
691 	/*
692 	 * if we end with a 'd' aka 'watchdogd' then we are the daemon program,
693 	 * otherwise run as a command line utility.
694 	 */
695 	c = strlen(argv[0]);
696 	if (argv[0][c - 1] == 'd')
697 		is_daemon = 1;
698 
699 	if (is_daemon)
700 		getopt_shortopts = "I:de:ns:t:ST:wx:?";
701 	else
702 		getopt_shortopts = "dt:?";
703 
704 	while ((c = getopt_long(argc, argv, getopt_shortopts, longopts,
705 		    &longindex)) != -1) {
706 		switch (c) {
707 		case 'I':
708 			pidfile = optarg;
709 			break;
710 		case 'd':
711 			debugging = 1;
712 			break;
713 		case 'e':
714 			test_cmd = strdup(optarg);
715 			break;
716 		case 'n':
717 			is_dry_run = 1;
718 			break;
719 #ifdef notyet
720 		case 'p':
721 			passive = 1;
722 			break;
723 #endif
724 		case 's':
725 			nap = fetchtimeout(c, NULL, optarg, 0);
726 			break;
727 		case 'S':
728 			do_syslog = 0;
729 			break;
730 		case 't':
731 			timeout_sec = atoi(optarg);
732 			timeout = parse_timeout_to_pow2ns(c, NULL, optarg);
733 			if (debugging)
734 				printf("Timeout is 2^%d nanoseconds\n",
735 				    timeout);
736 			break;
737 		case 'T':
738 			carp_thresh_seconds =
739 			    fetchtimeout(c, "NULL", optarg, 0);
740 			break;
741 		case 'w':
742 			do_timedog = 1;
743 			break;
744 		case 'x':
745 			exit_timeout = parse_timeout_to_pow2ns(c, NULL, optarg);
746 			if (exit_timeout != 0)
747 				exit_timeout |= WD_ACTIVE;
748 			break;
749 		case 0:
750 			lopt = longopts[longindex].name;
751 			if (!strcmp(lopt, "pretimeout")) {
752 				pretimeout = fetchtimeout(0, lopt, optarg, 0);
753 			} else if (!strcmp(lopt, "pretimeout-action")) {
754 				pretimeout_act = timeout_act_str2int(lopt,
755 				    optarg);
756 			} else if (!strcmp(lopt, "softtimeout-action")) {
757 				softtimeout_act = timeout_act_str2int(lopt,
758 				    optarg);
759 			} else {
760 		/*		warnx("bad option at index %d: %s", optind,
761 				    argv[optind]);
762 				usage();
763 				*/
764 			}
765 			break;
766 		case '?':
767 		default:
768 			usage();
769 			/* NOTREACHED */
770 		}
771 	}
772 
773 	if (nap > timeout_sec / 2)
774 		nap = timeout_sec / 2;
775 
776 	if (carp_thresh_seconds == -1)
777 		carp_thresh_seconds = nap;
778 
779 	if (argc != optind)
780 		errx(EX_USAGE, "extra arguments.");
781 	if (is_daemon && timeout < WD_TO_1SEC)
782 		errx(EX_USAGE, "-t argument is less than one second.");
783 	if (pretimeout_set) {
784 		if (pretimeout >= timeout_sec) {
785 			errx(EX_USAGE,
786 			    "pretimeout (%d) >= timeout (%d -> %ld)\n"
787 			    "see manual section TIMEOUT RESOLUTION",
788 			    pretimeout, timeout_sec, (long)ts.tv_sec);
789 		}
790 	}
791 }
792