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