wdogctl.c revision 1.6 1 1.6 christos /* $NetBSD: wdogctl.c,v 1.6 2001/02/04 21:14:32 christos 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.6 christos extern const char *__progname;
63 1.6 christos
64 1.1 thorpej int
65 1.1 thorpej main(int argc, char *argv[])
66 1.1 thorpej {
67 1.1 thorpej int cmds = 0, kflag = 0, uflag = 0, dflag = 0, ch;
68 1.1 thorpej u_int period = WDOG_PERIOD_DEFAULT;
69 1.1 thorpej
70 1.1 thorpej while ((ch = getopt(argc, argv, "Adkp:u")) != -1) {
71 1.1 thorpej switch (ch) {
72 1.1 thorpej case 'A':
73 1.1 thorpej if (cmds == 0 || dflag)
74 1.1 thorpej usage();
75 1.1 thorpej Aflag = 1;
76 1.1 thorpej break;
77 1.1 thorpej
78 1.1 thorpej case 'd':
79 1.1 thorpej dflag = 1;
80 1.1 thorpej cmds++;
81 1.1 thorpej break;
82 1.1 thorpej
83 1.1 thorpej case 'k':
84 1.1 thorpej kflag = 1;
85 1.1 thorpej cmds++;
86 1.1 thorpej break;
87 1.1 thorpej
88 1.1 thorpej case 'p':
89 1.1 thorpej if (cmds == 0 || dflag)
90 1.1 thorpej usage();
91 1.1 thorpej period = atoi(optarg);
92 1.1 thorpej if (period == -1)
93 1.1 thorpej usage();
94 1.1 thorpej break;
95 1.1 thorpej
96 1.1 thorpej case 'u':
97 1.1 thorpej uflag = 1;
98 1.1 thorpej cmds++;
99 1.1 thorpej break;
100 1.1 thorpej
101 1.1 thorpej default:
102 1.1 thorpej usage();
103 1.1 thorpej }
104 1.1 thorpej }
105 1.1 thorpej
106 1.1 thorpej argc -= optind;
107 1.1 thorpej argv += optind;
108 1.1 thorpej
109 1.1 thorpej if (cmds > 1)
110 1.1 thorpej usage();
111 1.1 thorpej
112 1.1 thorpej if (kflag) {
113 1.1 thorpej if (argc != 1)
114 1.1 thorpej usage();
115 1.1 thorpej enable_kernel(argv[0], period);
116 1.1 thorpej } else if (uflag) {
117 1.1 thorpej if (argc != 1)
118 1.1 thorpej usage();
119 1.1 thorpej enable_user(argv[0], period);
120 1.1 thorpej } else if (dflag) {
121 1.1 thorpej if (argc != 0)
122 1.1 thorpej usage();
123 1.1 thorpej disable();
124 1.1 thorpej } else
125 1.1 thorpej list_timers();
126 1.1 thorpej
127 1.1 thorpej exit(0);
128 1.1 thorpej }
129 1.1 thorpej
130 1.1 thorpej void
131 1.1 thorpej enable_kernel(const char *name, u_int period)
132 1.1 thorpej {
133 1.1 thorpej struct wdog_mode wm;
134 1.1 thorpej int fd;
135 1.1 thorpej
136 1.1 thorpej if (strlen(name) >= WDOG_NAMESIZE)
137 1.1 thorpej errx(1, "invalid watchdog timer name: %s", name);
138 1.1 thorpej strcpy(wm.wm_name, name);
139 1.1 thorpej wm.wm_mode = WDOG_MODE_KTICKLE;
140 1.1 thorpej wm.wm_period = period;
141 1.1 thorpej
142 1.1 thorpej if (Aflag)
143 1.1 thorpej wm.wm_mode |= WDOG_FEATURE_ALARM;
144 1.1 thorpej
145 1.1 thorpej fd = open(_PATH_WATCHDOG, O_RDWR, 0644);
146 1.1 thorpej if (fd == -1)
147 1.1 thorpej err(1, "open %s", _PATH_WATCHDOG);
148 1.1 thorpej
149 1.1 thorpej if (ioctl(fd, WDOGIOC_SMODE, &wm) == -1)
150 1.1 thorpej err(1, "WDOGIOC_SMODE");
151 1.1 thorpej }
152 1.1 thorpej
153 1.1 thorpej void
154 1.1 thorpej enable_user(const char *name, u_int period)
155 1.1 thorpej {
156 1.1 thorpej struct wdog_mode wm;
157 1.1 thorpej struct timespec ts;
158 1.1 thorpej pid_t tickler;
159 1.1 thorpej int fd, rv;
160 1.1 thorpej
161 1.1 thorpej if (strlen(name) >= WDOG_NAMESIZE)
162 1.1 thorpej errx(1, "invalid watchdog timer name: %s", name);
163 1.1 thorpej strcpy(wm.wm_name, name);
164 1.1 thorpej wm.wm_mode = WDOG_MODE_UTICKLE;
165 1.1 thorpej wm.wm_period = period;
166 1.1 thorpej
167 1.1 thorpej if (Aflag)
168 1.1 thorpej wm.wm_mode |= WDOG_FEATURE_ALARM;
169 1.1 thorpej
170 1.1 thorpej fd = open(_PATH_WATCHDOG, O_RDWR, 0644);
171 1.1 thorpej if (fd == -1)
172 1.1 thorpej err(1, "open %s", _PATH_WATCHDOG);
173 1.1 thorpej
174 1.1 thorpej /* ...so we can log failures to tickle the timer. */
175 1.4 lukem openlog("wdogctl", LOG_PERROR|LOG_PID, LOG_DAEMON);
176 1.1 thorpej
177 1.1 thorpej /*
178 1.1 thorpej * We fork a child process which detaches from the controlling
179 1.1 thorpej * terminal once the timer is armed, and tickles the timer
180 1.1 thorpej * until we send it a SIGTERM.
181 1.1 thorpej */
182 1.1 thorpej tickler = fork();
183 1.1 thorpej if (tickler == -1)
184 1.1 thorpej err(1, "unable to fork tickler process");
185 1.1 thorpej else if (tickler != 0) {
186 1.1 thorpej if (ioctl(fd, WDOGIOC_SMODE, &wm) == -1) {
187 1.1 thorpej err(1, "WDOGIOC_SMODE");
188 1.1 thorpej (void) kill(tickler, SIGTERM);
189 1.1 thorpej }
190 1.1 thorpej (void) close(fd);
191 1.1 thorpej return;
192 1.1 thorpej }
193 1.1 thorpej
194 1.1 thorpej
195 1.1 thorpej /*
196 1.1 thorpej * Wait for the watchdog to be armed. When it is, loop,
197 1.1 thorpej * tickling the timer, then waiting 1/2 the period before
198 1.1 thorpej * doing it again.
199 1.1 thorpej *
200 1.1 thorpej * If the parent fails to enable the watchdog, it will kill
201 1.1 thorpej * us.
202 1.1 thorpej */
203 1.1 thorpej do {
204 1.1 thorpej rv = ioctl(fd, WDOGIOC_WHICH, &wm);
205 1.1 thorpej } while (rv == -1);
206 1.1 thorpej
207 1.1 thorpej if (ioctl(fd, WDOGIOC_TICKLE) == -1)
208 1.1 thorpej syslog(LOG_EMERG, "unable to tickle watchdog timer %s: %m",
209 1.1 thorpej wm.wm_name);
210 1.1 thorpej
211 1.1 thorpej /*
212 1.1 thorpej * Now detach from the controlling terminal, and just run
213 1.1 thorpej * in the background. The kernel will keep track of who
214 1.1 thorpej * we are, each time we tickle the timer.
215 1.1 thorpej */
216 1.1 thorpej if (daemon(0, 0) == -1) {
217 1.1 thorpej /*
218 1.1 thorpej * We weren't able to go into the background. When
219 1.1 thorpej * we exit, the kernel will disable the watchdog so
220 1.1 thorpej * that the system won't die.
221 1.1 thorpej */
222 1.3 lukem err(1, "unable to detach from terminal");
223 1.1 thorpej }
224 1.1 thorpej
225 1.1 thorpej if (ioctl(fd, WDOGIOC_TICKLE) == -1)
226 1.1 thorpej syslog(LOG_EMERG, "unable to tickle watchdog timer %s: %m",
227 1.1 thorpej wm.wm_name);
228 1.1 thorpej
229 1.1 thorpej for (;;) {
230 1.1 thorpej ts.tv_sec = wm.wm_period / 2;
231 1.1 thorpej ts.tv_nsec = 0;
232 1.1 thorpej (void) nanosleep(&ts, NULL);
233 1.1 thorpej
234 1.1 thorpej if (ioctl(fd, WDOGIOC_TICKLE) == -1)
235 1.1 thorpej syslog(LOG_EMERG,
236 1.1 thorpej "unable to tickle watchdog timer %s: %m",
237 1.1 thorpej wm.wm_name);
238 1.1 thorpej }
239 1.1 thorpej /* NOTREACHED */
240 1.1 thorpej }
241 1.1 thorpej
242 1.1 thorpej void
243 1.1 thorpej disable(void)
244 1.1 thorpej {
245 1.1 thorpej struct wdog_mode wm;
246 1.1 thorpej pid_t tickler;
247 1.1 thorpej int fd;
248 1.1 thorpej
249 1.1 thorpej fd = open(_PATH_WATCHDOG, O_RDWR, 0644);
250 1.1 thorpej if (fd == -1)
251 1.1 thorpej err(1, "open %s", _PATH_WATCHDOG);
252 1.1 thorpej
253 1.1 thorpej if (ioctl(fd, WDOGIOC_WHICH, &wm) == -1) {
254 1.1 thorpej printf("No watchdog timer running.\n");
255 1.1 thorpej return;
256 1.1 thorpej }
257 1.1 thorpej
258 1.1 thorpej /*
259 1.1 thorpej * If the timer is running in UTICKLE mode, we need
260 1.1 thorpej * to kill the wdogctl(8) process that is tickling
261 1.1 thorpej * the timer.
262 1.1 thorpej */
263 1.1 thorpej if (wm.wm_mode == WDOG_MODE_UTICKLE) {
264 1.1 thorpej if (ioctl(fd, WDOGIOC_GTICKLER, &tickler) == -1)
265 1.1 thorpej err(1, "WDOGIOC_GTICKLER");
266 1.1 thorpej (void) close(fd);
267 1.1 thorpej (void) kill(tickler, SIGTERM);
268 1.1 thorpej } else {
269 1.1 thorpej wm.wm_mode = WDOG_MODE_DISARMED;
270 1.1 thorpej if (ioctl(fd, WDOGIOC_SMODE, &wm) == -1)
271 1.1 thorpej err(1, "unable to disarm watchdog %s", wm.wm_name);
272 1.1 thorpej (void) close(fd);
273 1.1 thorpej }
274 1.1 thorpej }
275 1.1 thorpej
276 1.1 thorpej void
277 1.1 thorpej list_timers(void)
278 1.1 thorpej {
279 1.1 thorpej struct wdog_conf wc;
280 1.1 thorpej struct wdog_mode wm;
281 1.1 thorpej char *buf, *cp;
282 1.1 thorpej int fd, count, i;
283 1.1 thorpej pid_t tickler;
284 1.1 thorpej
285 1.1 thorpej fd = open(_PATH_WATCHDOG, O_RDONLY, 0644);
286 1.1 thorpej if (fd == -1)
287 1.1 thorpej err(1, "open %s", _PATH_WATCHDOG);
288 1.1 thorpej
289 1.1 thorpej wc.wc_names = NULL;
290 1.1 thorpej wc.wc_count = 0;
291 1.1 thorpej
292 1.1 thorpej if (ioctl(fd, WDOGIOC_GWDOGS, &wc) == -1)
293 1.1 thorpej err(1, "ioctl WDOGIOC_GWDOGS for count");
294 1.1 thorpej
295 1.1 thorpej count = wc.wc_count;
296 1.1 thorpej if (count == 0) {
297 1.1 thorpej printf("No watchdog timers present.\n");
298 1.1 thorpej goto out;
299 1.1 thorpej }
300 1.1 thorpej
301 1.1 thorpej buf = malloc(count * WDOG_NAMESIZE);
302 1.1 thorpej if (buf == NULL)
303 1.1 thorpej err(1, "malloc %d byte for watchdog names",
304 1.1 thorpej count * WDOG_NAMESIZE);
305 1.1 thorpej
306 1.1 thorpej wc.wc_names = buf;
307 1.1 thorpej if (ioctl(fd, WDOGIOC_GWDOGS, &wc) == -1)
308 1.1 thorpej err(1, "ioctl WDOGIOC_GWDOGS for names");
309 1.1 thorpej
310 1.1 thorpej count = wc.wc_count;
311 1.1 thorpej if (count == 0) {
312 1.1 thorpej printf("No watchdog timers present.\n");
313 1.1 thorpej free(buf);
314 1.1 thorpej goto out;
315 1.1 thorpej }
316 1.1 thorpej
317 1.1 thorpej printf("Available watchdog timers:\n");
318 1.1 thorpej for (i = 0, cp = buf; i < count; i++, cp += WDOG_NAMESIZE) {
319 1.1 thorpej cp[WDOG_NAMESIZE - 1] = '\0';
320 1.1 thorpej strcpy(wm.wm_name, cp);
321 1.1 thorpej
322 1.1 thorpej if (ioctl(fd, WDOGIOC_GMODE, &wm) == -1)
323 1.1 thorpej wm.wm_mode = -1;
324 1.1 thorpej else if (wm.wm_mode == WDOG_MODE_UTICKLE) {
325 1.1 thorpej if (ioctl(fd, WDOGIOC_GTICKLER, &tickler) == -1)
326 1.1 thorpej tickler = (pid_t) -1;
327 1.1 thorpej }
328 1.1 thorpej
329 1.1 thorpej printf("\t%s, %u second period", cp, wm.wm_period);
330 1.1 thorpej if (wm.wm_mode != WDOG_MODE_DISARMED) {
331 1.1 thorpej printf(" [armed, %s tickle",
332 1.1 thorpej wm.wm_mode == WDOG_MODE_KTICKLE ?
333 1.1 thorpej "kernel" : "user");
334 1.1 thorpej if (wm.wm_mode == WDOG_MODE_UTICKLE &&
335 1.1 thorpej tickler != (pid_t) -1)
336 1.1 thorpej printf(", pid %d", tickler);
337 1.1 thorpej printf("]");
338 1.1 thorpej }
339 1.1 thorpej printf("\n");
340 1.1 thorpej }
341 1.1 thorpej out:
342 1.1 thorpej (void) close(fd);
343 1.1 thorpej }
344 1.1 thorpej
345 1.1 thorpej void
346 1.1 thorpej usage(void)
347 1.1 thorpej {
348 1.6 christos fprintf(stderr, "Usage: %s\n", __progname);
349 1.1 thorpej fprintf(stderr, " %s -k [-A] [-p seconds] timer\n", __progname);
350 1.1 thorpej fprintf(stderr, " %s -u [-A] [-p seconds] timer\n", __progname);
351 1.1 thorpej fprintf(stderr, " %s -d\n", __progname);
352 1.1 thorpej
353 1.1 thorpej exit(1);
354 1.1 thorpej }
355