wdogctl.c revision 1.7 1 1.7 cgd /* $NetBSD: wdogctl.c,v 1.7 2001/02/20 23:25:29 cgd Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2000 Zembu Labs, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Author: Jason R. Thorpe <thorpej (at) zembu.com>
8 1.1 thorpej *
9 1.1 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1 thorpej * modification, are permitted provided that the following conditions
11 1.1 thorpej * are met:
12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1 thorpej * documentation and/or other materials provided with the distribution.
17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1 thorpej * must display the following acknowledgement:
19 1.1 thorpej * This product includes software developed by Zembu Labs, Inc.
20 1.1 thorpej * 4. Neither the name of Zembu Labs nor the names of its employees may
21 1.1 thorpej * be used to endorse or promote products derived from this software
22 1.1 thorpej * without specific prior written permission.
23 1.1 thorpej *
24 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
25 1.1 thorpej * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
26 1.1 thorpej * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
27 1.1 thorpej * CLAIMED. IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 1.1 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 1.1 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 1.1 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 1.1 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 1.1 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 1.1 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1 thorpej */
35 1.1 thorpej
36 1.1 thorpej #include <sys/param.h>
37 1.1 thorpej #include <sys/ioctl.h>
38 1.1 thorpej #include <sys/wdog.h>
39 1.1 thorpej
40 1.1 thorpej #include <err.h>
41 1.1 thorpej #include <errno.h>
42 1.1 thorpej #include <fcntl.h>
43 1.1 thorpej #include <stdio.h>
44 1.1 thorpej #include <stdlib.h>
45 1.1 thorpej #include <time.h>
46 1.1 thorpej #include <signal.h>
47 1.1 thorpej #include <syslog.h>
48 1.1 thorpej #include <unistd.h>
49 1.2 minoura #include <string.h>
50 1.1 thorpej
51 1.1 thorpej #define _PATH_WATCHDOG "/dev/watchdog"
52 1.1 thorpej
53 1.1 thorpej int main(int, char *[]);
54 1.1 thorpej void enable_kernel(const char *, u_int);
55 1.1 thorpej void enable_user(const char *, u_int);
56 1.1 thorpej void disable(void);
57 1.1 thorpej void list_timers(void);
58 1.1 thorpej void usage(void);
59 1.1 thorpej
60 1.1 thorpej int Aflag;
61 1.1 thorpej
62 1.1 thorpej int
63 1.1 thorpej main(int argc, char *argv[])
64 1.1 thorpej {
65 1.1 thorpej int cmds = 0, kflag = 0, uflag = 0, dflag = 0, ch;
66 1.1 thorpej u_int period = WDOG_PERIOD_DEFAULT;
67 1.1 thorpej
68 1.1 thorpej while ((ch = getopt(argc, argv, "Adkp:u")) != -1) {
69 1.1 thorpej switch (ch) {
70 1.1 thorpej case 'A':
71 1.1 thorpej if (cmds == 0 || dflag)
72 1.1 thorpej usage();
73 1.1 thorpej Aflag = 1;
74 1.1 thorpej break;
75 1.1 thorpej
76 1.1 thorpej case 'd':
77 1.1 thorpej dflag = 1;
78 1.1 thorpej cmds++;
79 1.1 thorpej break;
80 1.1 thorpej
81 1.1 thorpej case 'k':
82 1.1 thorpej kflag = 1;
83 1.1 thorpej cmds++;
84 1.1 thorpej break;
85 1.1 thorpej
86 1.1 thorpej case 'p':
87 1.1 thorpej if (cmds == 0 || dflag)
88 1.1 thorpej usage();
89 1.1 thorpej period = atoi(optarg);
90 1.1 thorpej if (period == -1)
91 1.1 thorpej usage();
92 1.1 thorpej break;
93 1.1 thorpej
94 1.1 thorpej case 'u':
95 1.1 thorpej uflag = 1;
96 1.1 thorpej cmds++;
97 1.1 thorpej break;
98 1.1 thorpej
99 1.1 thorpej default:
100 1.1 thorpej usage();
101 1.1 thorpej }
102 1.1 thorpej }
103 1.1 thorpej
104 1.1 thorpej argc -= optind;
105 1.1 thorpej argv += optind;
106 1.1 thorpej
107 1.1 thorpej if (cmds > 1)
108 1.1 thorpej usage();
109 1.1 thorpej
110 1.1 thorpej if (kflag) {
111 1.1 thorpej if (argc != 1)
112 1.1 thorpej usage();
113 1.1 thorpej enable_kernel(argv[0], period);
114 1.1 thorpej } else if (uflag) {
115 1.1 thorpej if (argc != 1)
116 1.1 thorpej usage();
117 1.1 thorpej enable_user(argv[0], period);
118 1.1 thorpej } else if (dflag) {
119 1.1 thorpej if (argc != 0)
120 1.1 thorpej usage();
121 1.1 thorpej disable();
122 1.1 thorpej } else
123 1.1 thorpej list_timers();
124 1.1 thorpej
125 1.1 thorpej exit(0);
126 1.1 thorpej }
127 1.1 thorpej
128 1.1 thorpej void
129 1.1 thorpej enable_kernel(const char *name, u_int period)
130 1.1 thorpej {
131 1.1 thorpej struct wdog_mode wm;
132 1.1 thorpej int fd;
133 1.1 thorpej
134 1.1 thorpej if (strlen(name) >= WDOG_NAMESIZE)
135 1.1 thorpej errx(1, "invalid watchdog timer name: %s", name);
136 1.1 thorpej strcpy(wm.wm_name, name);
137 1.1 thorpej wm.wm_mode = WDOG_MODE_KTICKLE;
138 1.1 thorpej wm.wm_period = period;
139 1.1 thorpej
140 1.1 thorpej if (Aflag)
141 1.1 thorpej wm.wm_mode |= WDOG_FEATURE_ALARM;
142 1.1 thorpej
143 1.1 thorpej fd = open(_PATH_WATCHDOG, O_RDWR, 0644);
144 1.1 thorpej if (fd == -1)
145 1.1 thorpej err(1, "open %s", _PATH_WATCHDOG);
146 1.1 thorpej
147 1.1 thorpej if (ioctl(fd, WDOGIOC_SMODE, &wm) == -1)
148 1.1 thorpej err(1, "WDOGIOC_SMODE");
149 1.1 thorpej }
150 1.1 thorpej
151 1.1 thorpej void
152 1.1 thorpej enable_user(const char *name, u_int period)
153 1.1 thorpej {
154 1.1 thorpej struct wdog_mode wm;
155 1.1 thorpej struct timespec ts;
156 1.1 thorpej pid_t tickler;
157 1.1 thorpej int fd, rv;
158 1.1 thorpej
159 1.1 thorpej if (strlen(name) >= WDOG_NAMESIZE)
160 1.1 thorpej errx(1, "invalid watchdog timer name: %s", name);
161 1.1 thorpej strcpy(wm.wm_name, name);
162 1.1 thorpej wm.wm_mode = WDOG_MODE_UTICKLE;
163 1.1 thorpej wm.wm_period = period;
164 1.1 thorpej
165 1.1 thorpej if (Aflag)
166 1.1 thorpej wm.wm_mode |= WDOG_FEATURE_ALARM;
167 1.1 thorpej
168 1.1 thorpej fd = open(_PATH_WATCHDOG, O_RDWR, 0644);
169 1.1 thorpej if (fd == -1)
170 1.1 thorpej err(1, "open %s", _PATH_WATCHDOG);
171 1.1 thorpej
172 1.1 thorpej /* ...so we can log failures to tickle the timer. */
173 1.4 lukem openlog("wdogctl", LOG_PERROR|LOG_PID, LOG_DAEMON);
174 1.1 thorpej
175 1.1 thorpej /*
176 1.1 thorpej * We fork a child process which detaches from the controlling
177 1.1 thorpej * terminal once the timer is armed, and tickles the timer
178 1.1 thorpej * until we send it a SIGTERM.
179 1.1 thorpej */
180 1.1 thorpej tickler = fork();
181 1.1 thorpej if (tickler == -1)
182 1.1 thorpej err(1, "unable to fork tickler process");
183 1.1 thorpej else if (tickler != 0) {
184 1.1 thorpej if (ioctl(fd, WDOGIOC_SMODE, &wm) == -1) {
185 1.1 thorpej err(1, "WDOGIOC_SMODE");
186 1.1 thorpej (void) kill(tickler, SIGTERM);
187 1.1 thorpej }
188 1.1 thorpej (void) close(fd);
189 1.1 thorpej return;
190 1.1 thorpej }
191 1.1 thorpej
192 1.1 thorpej
193 1.1 thorpej /*
194 1.1 thorpej * Wait for the watchdog to be armed. When it is, loop,
195 1.1 thorpej * tickling the timer, then waiting 1/2 the period before
196 1.1 thorpej * doing it again.
197 1.1 thorpej *
198 1.1 thorpej * If the parent fails to enable the watchdog, it will kill
199 1.1 thorpej * us.
200 1.1 thorpej */
201 1.1 thorpej do {
202 1.1 thorpej rv = ioctl(fd, WDOGIOC_WHICH, &wm);
203 1.1 thorpej } while (rv == -1);
204 1.1 thorpej
205 1.1 thorpej if (ioctl(fd, WDOGIOC_TICKLE) == -1)
206 1.1 thorpej syslog(LOG_EMERG, "unable to tickle watchdog timer %s: %m",
207 1.1 thorpej wm.wm_name);
208 1.1 thorpej
209 1.1 thorpej /*
210 1.1 thorpej * Now detach from the controlling terminal, and just run
211 1.1 thorpej * in the background. The kernel will keep track of who
212 1.1 thorpej * we are, each time we tickle the timer.
213 1.1 thorpej */
214 1.1 thorpej if (daemon(0, 0) == -1) {
215 1.1 thorpej /*
216 1.1 thorpej * We weren't able to go into the background. When
217 1.1 thorpej * we exit, the kernel will disable the watchdog so
218 1.1 thorpej * that the system won't die.
219 1.1 thorpej */
220 1.3 lukem err(1, "unable to detach from terminal");
221 1.1 thorpej }
222 1.1 thorpej
223 1.1 thorpej if (ioctl(fd, WDOGIOC_TICKLE) == -1)
224 1.1 thorpej syslog(LOG_EMERG, "unable to tickle watchdog timer %s: %m",
225 1.1 thorpej wm.wm_name);
226 1.1 thorpej
227 1.1 thorpej for (;;) {
228 1.1 thorpej ts.tv_sec = wm.wm_period / 2;
229 1.1 thorpej ts.tv_nsec = 0;
230 1.1 thorpej (void) nanosleep(&ts, NULL);
231 1.1 thorpej
232 1.1 thorpej if (ioctl(fd, WDOGIOC_TICKLE) == -1)
233 1.1 thorpej syslog(LOG_EMERG,
234 1.1 thorpej "unable to tickle watchdog timer %s: %m",
235 1.1 thorpej wm.wm_name);
236 1.1 thorpej }
237 1.1 thorpej /* NOTREACHED */
238 1.1 thorpej }
239 1.1 thorpej
240 1.1 thorpej void
241 1.1 thorpej disable(void)
242 1.1 thorpej {
243 1.1 thorpej struct wdog_mode wm;
244 1.1 thorpej pid_t tickler;
245 1.1 thorpej int fd;
246 1.1 thorpej
247 1.1 thorpej fd = open(_PATH_WATCHDOG, O_RDWR, 0644);
248 1.1 thorpej if (fd == -1)
249 1.1 thorpej err(1, "open %s", _PATH_WATCHDOG);
250 1.1 thorpej
251 1.1 thorpej if (ioctl(fd, WDOGIOC_WHICH, &wm) == -1) {
252 1.1 thorpej printf("No watchdog timer running.\n");
253 1.1 thorpej return;
254 1.1 thorpej }
255 1.1 thorpej
256 1.1 thorpej /*
257 1.1 thorpej * If the timer is running in UTICKLE mode, we need
258 1.1 thorpej * to kill the wdogctl(8) process that is tickling
259 1.1 thorpej * the timer.
260 1.1 thorpej */
261 1.1 thorpej if (wm.wm_mode == WDOG_MODE_UTICKLE) {
262 1.1 thorpej if (ioctl(fd, WDOGIOC_GTICKLER, &tickler) == -1)
263 1.1 thorpej err(1, "WDOGIOC_GTICKLER");
264 1.1 thorpej (void) close(fd);
265 1.1 thorpej (void) kill(tickler, SIGTERM);
266 1.1 thorpej } else {
267 1.1 thorpej wm.wm_mode = WDOG_MODE_DISARMED;
268 1.1 thorpej if (ioctl(fd, WDOGIOC_SMODE, &wm) == -1)
269 1.1 thorpej err(1, "unable to disarm watchdog %s", wm.wm_name);
270 1.1 thorpej (void) close(fd);
271 1.1 thorpej }
272 1.1 thorpej }
273 1.1 thorpej
274 1.1 thorpej void
275 1.1 thorpej list_timers(void)
276 1.1 thorpej {
277 1.1 thorpej struct wdog_conf wc;
278 1.1 thorpej struct wdog_mode wm;
279 1.1 thorpej char *buf, *cp;
280 1.1 thorpej int fd, count, i;
281 1.1 thorpej pid_t tickler;
282 1.1 thorpej
283 1.1 thorpej fd = open(_PATH_WATCHDOG, O_RDONLY, 0644);
284 1.1 thorpej if (fd == -1)
285 1.1 thorpej err(1, "open %s", _PATH_WATCHDOG);
286 1.1 thorpej
287 1.1 thorpej wc.wc_names = NULL;
288 1.1 thorpej wc.wc_count = 0;
289 1.1 thorpej
290 1.1 thorpej if (ioctl(fd, WDOGIOC_GWDOGS, &wc) == -1)
291 1.1 thorpej err(1, "ioctl WDOGIOC_GWDOGS for count");
292 1.1 thorpej
293 1.1 thorpej count = wc.wc_count;
294 1.1 thorpej if (count == 0) {
295 1.1 thorpej printf("No watchdog timers present.\n");
296 1.1 thorpej goto out;
297 1.1 thorpej }
298 1.1 thorpej
299 1.1 thorpej buf = malloc(count * WDOG_NAMESIZE);
300 1.1 thorpej if (buf == NULL)
301 1.1 thorpej err(1, "malloc %d byte for watchdog names",
302 1.1 thorpej count * WDOG_NAMESIZE);
303 1.1 thorpej
304 1.1 thorpej wc.wc_names = buf;
305 1.1 thorpej if (ioctl(fd, WDOGIOC_GWDOGS, &wc) == -1)
306 1.1 thorpej err(1, "ioctl WDOGIOC_GWDOGS for names");
307 1.1 thorpej
308 1.1 thorpej count = wc.wc_count;
309 1.1 thorpej if (count == 0) {
310 1.1 thorpej printf("No watchdog timers present.\n");
311 1.1 thorpej free(buf);
312 1.1 thorpej goto out;
313 1.1 thorpej }
314 1.1 thorpej
315 1.1 thorpej printf("Available watchdog timers:\n");
316 1.1 thorpej for (i = 0, cp = buf; i < count; i++, cp += WDOG_NAMESIZE) {
317 1.1 thorpej cp[WDOG_NAMESIZE - 1] = '\0';
318 1.1 thorpej strcpy(wm.wm_name, cp);
319 1.1 thorpej
320 1.1 thorpej if (ioctl(fd, WDOGIOC_GMODE, &wm) == -1)
321 1.1 thorpej wm.wm_mode = -1;
322 1.1 thorpej else if (wm.wm_mode == WDOG_MODE_UTICKLE) {
323 1.1 thorpej if (ioctl(fd, WDOGIOC_GTICKLER, &tickler) == -1)
324 1.1 thorpej tickler = (pid_t) -1;
325 1.1 thorpej }
326 1.1 thorpej
327 1.1 thorpej printf("\t%s, %u second period", cp, wm.wm_period);
328 1.1 thorpej if (wm.wm_mode != WDOG_MODE_DISARMED) {
329 1.1 thorpej printf(" [armed, %s tickle",
330 1.1 thorpej wm.wm_mode == WDOG_MODE_KTICKLE ?
331 1.1 thorpej "kernel" : "user");
332 1.1 thorpej if (wm.wm_mode == WDOG_MODE_UTICKLE &&
333 1.1 thorpej tickler != (pid_t) -1)
334 1.1 thorpej printf(", pid %d", tickler);
335 1.1 thorpej printf("]");
336 1.1 thorpej }
337 1.1 thorpej printf("\n");
338 1.1 thorpej }
339 1.1 thorpej out:
340 1.1 thorpej (void) close(fd);
341 1.1 thorpej }
342 1.1 thorpej
343 1.1 thorpej void
344 1.1 thorpej usage(void)
345 1.1 thorpej {
346 1.7 cgd fprintf(stderr, "Usage: %s\n", getprogname());
347 1.7 cgd fprintf(stderr, " %s -k [-A] [-p seconds] timer\n",
348 1.7 cgd getprogname());
349 1.7 cgd fprintf(stderr, " %s -u [-A] [-p seconds] timer\n",
350 1.7 cgd getprogname());
351 1.7 cgd fprintf(stderr, " %s -d\n", getprogname());
352 1.1 thorpej
353 1.1 thorpej exit(1);
354 1.1 thorpej }
355