xref: /openbsd/usr.sbin/watchdogd/watchdogd.c (revision 5b133f3f)
1 /*	$OpenBSD: watchdogd.c,v 1.16 2023/03/08 04:43:15 guenther Exp $ */
2 
3 /*
4  * Copyright (c) 2005 Marc Balmer <mbalmer@openbsd.org>
5  *
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.
9  *
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.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/resource.h>
21 #include <sys/signal.h>
22 #include <sys/sysctl.h>
23 #include <sys/mman.h>
24 
25 #include <err.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 
31 volatile sig_atomic_t	quit = 0;
32 
33 __dead void	usage(void);
34 void		sighdlr(int);
35 int		main(int, char *[]);
36 
37 __dead void
usage(void)38 usage(void)
39 {
40 	extern char *__progname;
41 
42 	fprintf(stderr, "usage: %s [-dnq] [-i interval] [-p period]\n",
43 	    __progname);
44 	exit(1);
45 }
46 
47 void
sighdlr(int signum)48 sighdlr(int signum)
49 {
50 	quit = 1;
51 }
52 
53 int
main(int argc,char * argv[])54 main(int argc, char *argv[])
55 {
56 	struct rlimit	 rlim;
57 	const char	*errstr;
58 	size_t		 len;
59 	u_int		 interval = 0, period = 30, nperiod;
60 	int		 ch, trigauto, sauto, speriod;
61 	int		 quiet = 0, daemonize = 1, retval = 1, do_restore = 1;
62 	int		 mib[3];
63 
64 	while ((ch = getopt(argc, argv, "di:np:q")) != -1) {
65 		switch (ch) {
66 		case 'd':
67 			daemonize = 0;
68 			break;
69 		case 'i':
70 			interval = (u_int)strtonum(optarg, 1LL, 86400LL,
71 			    &errstr);
72 			if (errstr)
73 				errx(1, "interval is %s: %s", errstr, optarg);
74 			break;
75 		case 'n':
76 			do_restore = 0;
77 			break;
78 		case 'p':
79 			period = (u_int)strtonum(optarg, 2LL, 86400LL, &errstr);
80 			if (errstr)
81 				errx(1, "period is %s: %s", errstr, optarg);
82 			break;
83 		case 'q':
84 			quiet = 1;
85 			break;
86 		default:
87 			usage();
88 		}
89 	}
90 
91 	argc -= optind;
92 	argv += optind;
93 	if (argc > 0)
94 		usage();
95 
96 	if (interval == 0 && (interval = period / 3) == 0)
97 		interval = 1;
98 
99 	if (period <= interval)
100 		errx(1, "retrigger interval too long");
101 
102 	/* save kern.watchdog.period and kern.watchdog.auto for restore */
103 	mib[0] = CTL_KERN;
104 	mib[1] = KERN_WATCHDOG;
105 	mib[2] = KERN_WATCHDOG_PERIOD;
106 
107 	len = sizeof(speriod);
108 	if (sysctl(mib, 3, &speriod, &len, &period, sizeof(period)) == -1) {
109 		if (errno == EOPNOTSUPP)
110 			errx(1, "no watchdog timer available");
111 		else
112 			err(1, "can't access kern.watchdog.period");
113 	}
114 
115 	mib[2] = KERN_WATCHDOG_AUTO;
116 	len = sizeof(sauto);
117 	trigauto = 0;
118 
119 	if (sysctl(mib, 3, &sauto, &len, &trigauto, sizeof(trigauto)) == -1)
120 		err(1, "can't access kern.watchdog.auto");
121 
122 	/* Double check the timeout period, some devices change the value */
123 	mib[2] = KERN_WATCHDOG_PERIOD;
124 	len = sizeof(nperiod);
125 	if (sysctl(mib, 3, &nperiod, &len, NULL, 0) == -1) {
126 		warnx("can't read back kern.watchdog.period, "
127 		    "restoring original values");
128 		goto restore;
129 	}
130 
131 	if (nperiod != period && !quiet)
132 		warnx("period adjusted to %d by device", nperiod);
133 
134 	if (nperiod <= interval) {
135 		warnx("retrigger interval %d too long, "
136 		    "restoring original values", interval);
137 		goto restore;
138 	}
139 
140 	if (daemonize && daemon(0, 0)) {
141 		warn("can't daemonize, restoring original values");
142 		goto restore;
143 	}
144 
145 	/*
146 	 * mlockall() below will wire the whole stack up to the limit
147 	 * thus we have to reduce stack size to avoid resource abuse
148 	 */
149 	rlim.rlim_cur = 256 * 1024;
150 	rlim.rlim_max = 256 * 1024;
151 	(void)setrlimit(RLIMIT_STACK, &rlim);
152 
153 	(void)mlockall(MCL_CURRENT | MCL_FUTURE);
154 	setpriority(PRIO_PROCESS, getpid(), -5);
155 
156 	signal(SIGTERM, sighdlr);
157 
158 	retval = 0;
159 	while (!quit) {
160 		if (sysctl(mib, 3, NULL, 0, &period, sizeof(period)) == -1)
161 			quit = retval = 1;
162 		sleep(interval);
163 	}
164 
165 	if (do_restore) {
166 restore:	sysctl(mib, 3, NULL, 0, &speriod, sizeof(speriod));
167 		mib[2] = KERN_WATCHDOG_AUTO;
168 		sysctl(mib, 3, NULL, 0, &sauto, sizeof(sauto));
169 	}
170 
171 	return retval;
172 }
173