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