aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/watchdogd
diff options
context:
space:
mode:
authorAlfred Perlstein <alfred@FreeBSD.org>2013-07-27 20:47:01 +0000
committerAlfred Perlstein <alfred@FreeBSD.org>2013-07-27 20:47:01 +0000
commit3d30404f8339e4029b2673e8892134e9b01f1305 (patch)
tree56df7df9ac8f7b431ecb8a19f2ae000fa5ebdd85 /usr.sbin/watchdogd
parent9759c7e7d2d2ec70e20fd17d4e6a18e6b38e3af3 (diff)
downloadsrc-3d30404f8339e4029b2673e8892134e9b01f1305.tar.gz
src-3d30404f8339e4029b2673e8892134e9b01f1305.zip
Fix watchdog pretimeout.
The original API calls for pow2ns, however the new APIs from Linux call for seconds. We need to be able to convert to/from 2^Nns to seconds in both userland and kernel to fix this and properly compare units.
Notes
Notes: svn path=/head/; revision=253719
Diffstat (limited to 'usr.sbin/watchdogd')
-rw-r--r--usr.sbin/watchdogd/watchdogd.c196
1 files changed, 179 insertions, 17 deletions
diff --git a/usr.sbin/watchdogd/watchdogd.c b/usr.sbin/watchdogd/watchdogd.c
index 5416751d4cc3..7b6db94736d1 100644
--- a/usr.sbin/watchdogd/watchdogd.c
+++ b/usr.sbin/watchdogd/watchdogd.c
@@ -39,6 +39,7 @@ __FBSDID("$FreeBSD$");
#include <sys/rtprio.h>
#include <sys/stat.h>
#include <sys/time.h>
+#include <sys/sysctl.h>
#include <sys/watchdog.h>
#include <err.h>
@@ -58,19 +59,24 @@ __FBSDID("$FreeBSD$");
#include <getopt.h>
+static long fetchtimeout(int opt, const char *longopt, const char *myoptarg);
static void parseargs(int, char *[]);
+static int seconds_to_pow2ns(int);
static void sighandler(int);
static void watchdog_loop(void);
static int watchdog_init(void);
static int watchdog_onoff(int onoff);
static int watchdog_patpat(u_int timeout);
static void usage(void);
+static int tstotv(struct timeval *tv, struct timespec *ts);
+static int tvtohz(struct timeval *tv);
static int debugging = 0;
static int end_program = 0;
static const char *pidfile = _PATH_VARRUN "watchdogd.pid";
static u_int timeout = WD_TO_128SEC;
static u_int pretimeout = 0;
+static u_int timeout_sec;
static u_int passive = 0;
static int is_daemon = 0;
static int is_dry_run = 0; /* do not arm the watchdog, only
@@ -183,6 +189,59 @@ main(int argc, char *argv[])
}
}
+static void
+pow2ns_to_ts(int pow2ns, struct timespec *ts)
+{
+ uint64_t ns;
+
+ ns = 1ULL << pow2ns;
+ ts->tv_sec = ns / 1000000000ULL;
+ ts->tv_nsec = ns % 1000000000ULL;
+}
+
+/*
+ * Convert a timeout in seconds to N where 2^N nanoseconds is close to
+ * "seconds".
+ *
+ * The kernel expects the timeouts for watchdogs in "2^N nanosecond format".
+ */
+static u_int
+parse_timeout_to_pow2ns(char opt, const char *longopt, const char *myoptarg)
+{
+ double a;
+ u_int rv;
+ struct timespec ts;
+ struct timeval tv;
+ int ticks;
+ char shortopt[] = "- ";
+
+ if (!longopt)
+ shortopt[1] = opt;
+
+ a = fetchtimeout(opt, longopt, myoptarg);
+
+ if (a == 0)
+ rv = WD_TO_NEVER;
+ else
+ rv = seconds_to_pow2ns(a);
+ pow2ns_to_ts(rv, &ts);
+ tstotv(&tv, &ts);
+ ticks = tvtohz(&tv);
+ if (debugging) {
+ printf("Timeout for %s%s "
+ "is 2^%d nanoseconds "
+ "(in: %s sec -> out: %ld sec %ld ns -> %d ticks)\n",
+ longopt ? "-" : "", longopt ? longopt : shortopt,
+ rv,
+ myoptarg, ts.tv_sec, ts.tv_nsec, ticks);
+ }
+ if (ticks <= 0) {
+ errx(1, "Timeout for %s%s is too small, please choose a higher timeout.", longopt ? "-" : "", longopt ? longopt : shortopt);
+ }
+
+ return (rv);
+}
+
/*
* Catch signals and begin shutdown process.
*/
@@ -513,6 +572,110 @@ timeout_act_str2int(const char *lopt, const char *acts)
return rv;
}
+int
+tstotv(struct timeval *tv, struct timespec *ts)
+{
+
+ tv->tv_sec = ts->tv_sec;
+ tv->tv_usec = ts->tv_nsec / 1000;
+ return 0;
+}
+
+/*
+ * Convert a timeval to a number of ticks.
+ * Mostly copied from the kernel.
+ */
+int
+tvtohz(struct timeval *tv)
+{
+ register unsigned long ticks;
+ register long sec, usec;
+ int hz;
+ size_t hzsize;
+ int error;
+ int tick;
+
+ hzsize = sizeof(hz);
+
+ error = sysctlbyname("kern.hz", &hz, &hzsize, NULL, 0);
+ if (error)
+ err(1, "sysctlbyname kern.hz");
+
+ tick = 1000000 / hz;
+
+ /*
+ * If the number of usecs in the whole seconds part of the time
+ * difference fits in a long, then the total number of usecs will
+ * fit in an unsigned long. Compute the total and convert it to
+ * ticks, rounding up and adding 1 to allow for the current tick
+ * to expire. Rounding also depends on unsigned long arithmetic
+ * to avoid overflow.
+ *
+ * Otherwise, if the number of ticks in the whole seconds part of
+ * the time difference fits in a long, then convert the parts to
+ * ticks separately and add, using similar rounding methods and
+ * overflow avoidance. This method would work in the previous
+ * case but it is slightly slower and assumes that hz is integral.
+ *
+ * Otherwise, round the time difference down to the maximum
+ * representable value.
+ *
+ * If ints have 32 bits, then the maximum value for any timeout in
+ * 10ms ticks is 248 days.
+ */
+ sec = tv->tv_sec;
+ usec = tv->tv_usec;
+ if (usec < 0) {
+ sec--;
+ usec += 1000000;
+ }
+ if (sec < 0) {
+#ifdef DIAGNOSTIC
+ if (usec > 0) {
+ sec++;
+ usec -= 1000000;
+ }
+ printf("tvotohz: negative time difference %ld sec %ld usec\n",
+ sec, usec);
+#endif
+ ticks = 1;
+ } else if (sec <= LONG_MAX / 1000000)
+ ticks = (sec * 1000000 + (unsigned long)usec + (tick - 1))
+ / tick + 1;
+ else if (sec <= LONG_MAX / hz)
+ ticks = sec * hz
+ + ((unsigned long)usec + (tick - 1)) / tick + 1;
+ else
+ ticks = LONG_MAX;
+ if (ticks > INT_MAX)
+ ticks = INT_MAX;
+ return ((int)ticks);
+}
+
+static int
+seconds_to_pow2ns(int seconds)
+{
+ uint64_t power;
+ uint64_t ns;
+ uint64_t shifted;
+
+ if (seconds <= 0)
+ errx(1, "seconds %d < 0", seconds);
+ ns = ((uint64_t)seconds) * 1000000000ULL;
+ power = flsll(ns);
+ shifted = 1ULL << power;
+ if (shifted <= ns) {
+ power++;
+ }
+ if (debugging) {
+ printf("shifted %lld\n", (long long)shifted);
+ printf("seconds_to_pow2ns: seconds: %d, ns %lld, power %d\n",
+ seconds, (long long)ns, (int)power);
+ }
+ return (power);
+}
+
+
/*
* Handle the few command line arguments supported.
*/
@@ -521,9 +684,7 @@ parseargs(int argc, char *argv[])
{
int longindex;
int c;
- char *p;
const char *lopt;
- double a;
/*
* if we end with a 'd' aka 'watchdogd' then we are the daemon program,
@@ -565,21 +726,11 @@ parseargs(int argc, char *argv[])
do_syslog = 0;
break;
case 't':
- p = NULL;
- errno = 0;
- a = strtod(optarg, &p);
- if ((p != NULL && *p != '\0') || errno != 0)
- errx(EX_USAGE, "-t argument is not a number");
- if (a < 0)
- errx(EX_USAGE, "-t argument must be positive");
-
- if (a == 0)
- timeout = WD_TO_NEVER;
- else
- timeout = flsll(a * 1e9);
- if (debugging)
- printf("Timeout is 2^%d nanoseconds\n",
- timeout);
+ timeout_sec = atoi(optarg);
+ timeout = parse_timeout_to_pow2ns(c, NULL, optarg);
+ if (debugging)
+ printf("Timeout is 2^%d nanoseconds\n",
+ timeout);
break;
case 'T':
carp_thresh_seconds = fetchtimeout(c, "NULL", optarg);
@@ -618,4 +769,15 @@ parseargs(int argc, char *argv[])
errx(EX_USAGE, "extra arguments.");
if (is_daemon && timeout < WD_TO_1SEC)
errx(EX_USAGE, "-t argument is less than one second.");
+ if (pretimeout_set) {
+ struct timespec ts;
+
+ pow2ns_to_ts(timeout, &ts);
+ if (pretimeout >= ts.tv_sec) {
+ errx(EX_USAGE,
+ "pretimeout (%d) >= timeout (%d -> %ld)\n"
+ "see manual section TIMEOUT RESOLUTION",
+ pretimeout, timeout_sec, (long)ts.tv_sec);
+ }
+ }
}