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