1 /* $OpenBSD: sthen $ */
4 * Copyright (c) 2005 Marc Balmer <mbalmer@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/param.h>
20 #include <sys/sysctl.h>
29 volatile sig_atomic_t quit = 0;
31 __dead void usage(void);
33 int main(int, char *[]);
38 extern char *__progname;
40 fprintf(stderr, "usage: %s [-dnq] [-i interval] [-p period]\n",
53 main(int argc, char *argv[])
58 u_int interval = 0, period = 30, nperiod;
59 int ch, trigauto, sauto, speriod;
60 int quiet = 0, daemonize = 1, retval = 1, do_restore = 1;
63 while ((ch = getopt(argc, argv, "di:np:q")) != -1) {
69 interval = (u_int)strtonum(optarg, 1LL, 86400LL,
72 errx(1, "interval is %s: %s", errstr, optarg);
78 period = (u_int)strtonum(optarg, 2LL, 86400LL, &errstr);
80 errx(1, "period is %s: %s", errstr, optarg);
95 if (interval == 0 && (interval = period / 3) == 0)
98 if (period <= interval)
99 errx(1, "retrigger interval too long");
101 /* save kern.watchdog.period and kern.watchdog.auto for restore */
103 mib[1] = KERN_WATCHDOG;
104 mib[2] = KERN_WATCHDOG_PERIOD;
106 len = sizeof(speriod);
107 if (sysctl(mib, 3, &speriod, &len, &period, sizeof(period)) == -1) {
108 if (errno == EOPNOTSUPP)
109 errx(1, "no watchdog timer available");
111 err(1, "can't access kern.watchdog.period");
114 mib[2] = KERN_WATCHDOG_AUTO;
118 if (sysctl(mib, 3, &sauto, &len, &trigauto, sizeof(trigauto)) == -1)
119 err(1, "can't access kern.watchdog.auto");
121 /* Double check the timeout period, some devices change the value */
122 mib[2] = KERN_WATCHDOG_PERIOD;
123 len = sizeof(nperiod);
124 if (sysctl(mib, 3, &nperiod, &len, NULL, 0) == -1) {
125 warnx("can't read back kern.watchdog.period, "
126 "restoring original values");
130 if (nperiod != period && !quiet)
131 warnx("period adjusted to %d by device", nperiod);
133 if (nperiod <= interval) {
134 warnx("retrigger interval %d too long, "
135 "restoring original values", interval);
139 if (daemonize && daemon(0, 0)) {
140 warn("can't daemonize, restoring original values");
145 * mlockall() below will wire the whole stack up to the limit
146 * thus we have to reduce stack size to avoid resource abuse
148 rlim.rlim_cur = 256 * 1024;
149 rlim.rlim_max = 256 * 1024;
150 (void)setrlimit(RLIMIT_STACK, &rlim);
152 (void)mlockall(MCL_CURRENT | MCL_FUTURE);
153 setpriority(PRIO_PROCESS, getpid(), -5);
155 signal(SIGTERM, sighdlr);
159 if (sysctl(mib, 3, NULL, 0, &period, sizeof(period)) == -1)
165 restore: sysctl(mib, 3, NULL, 0, &speriod, sizeof(speriod));
166 mib[2] = KERN_WATCHDOG_AUTO;
167 sysctl(mib, 3, NULL, 0, &sauto, sizeof(sauto));