diff --git a/usr.sbin/watchdogd/watchdogd.c b/usr.sbin/watchdogd/watchdogd.c --- a/usr.sbin/watchdogd/watchdogd.c +++ b/usr.sbin/watchdogd/watchdogd.c @@ -49,6 +49,7 @@ #include #include #include +#include #include #include #include @@ -68,6 +69,7 @@ static int watchdog_init(void); static int watchdog_onoff(int onoff); static int watchdog_patpat(sbintime_t); +static int watchdog_control(u_int); static void usage(void); static int tvtohz(struct timeval *tv); @@ -86,6 +88,8 @@ static int is_dry_run = 0; /* do not arm the watchdog, only report on timing of the watch program */ +static bool no_fork = false; +static bool need_timeout = false; static int do_timedog = 0; static int do_syslog = 1; static int fd = -1; @@ -130,6 +134,11 @@ openlog("watchdogd", LOG_CONS|LOG_NDELAY|LOG_PERROR, LOG_DAEMON); + /* Retrieve old timeout if necessary */ + if (need_timeout) + ioctl(fd, WDIOC_GETTIMEOUT, &timeout); + else + ioctl(fd, WDIOC_SETTIMEOUT, &timeout); rtp.type = RTP_PRIO_REALTIME; rtp.prio = 0; if (rtprio(RTP_SET, 0, &rtp) == -1) @@ -152,7 +161,7 @@ warn("Cannot open or create pidfile"); } - if (debugging == 0 && daemon(0, 0) == -1) { + if (debugging == 0 && !no_fork && daemon(0, 0) == -1) { watchdog_onoff(0); pidfile_remove(pfh); err(EX_OSERR, "daemon"); @@ -347,7 +356,7 @@ } if (failed == 0) - watchdog_patpat(timeout); + watchdog_control(WD_CTRL_RESET); waited = watchdog_check_dogfunction_time(&ts_start, &ts_end); if (nap - waited > 0) @@ -653,7 +662,7 @@ is_daemon = 1; if (is_daemon) - getopt_shortopts = "I:de:ns:t:ST:wx:?"; + getopt_shortopts = "I:Dde:ns:t:ST:wx:?"; else getopt_shortopts = "dt:?"; @@ -663,6 +672,9 @@ case 'I': pidfile = optarg; break; + case 'D': + no_fork = true; + break; case 'd': debugging = 1; break; @@ -685,8 +697,11 @@ break; case 't': timeout_sec = atoi(optarg); - timeout = parse_timeout_to_sbt(c, NULL, optarg); - if (debugging) + if (timeout_sec == -1) + need_timeout = true; + else + timeout = parse_timeout_to_sbt(c, NULL, optarg); + if (debugging && !need_timeout) printf("Timeout is %d\n", (int)(timeout / SBT_1S)); break; @@ -734,7 +749,7 @@ if (argc != optind) errx(EX_USAGE, "extra arguments."); - if (is_daemon && timeout < WD_TO_1SEC) + if (is_daemon && timeout < SBT_1S) errx(EX_USAGE, "-t argument is less than one second."); if (pretimeout_set) { if (pretimeout >= timeout_sec) {