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