sysmon_wdog.c revision 1.29 1 1.29 pgoyette /* $NetBSD: sysmon_wdog.c,v 1.29 2015/12/14 01:08:47 pgoyette 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 /*
37 1.1 thorpej * Watchdog timer framework for sysmon. Hardware (and software)
38 1.1 thorpej * watchdog timers can register themselves here to provide a
39 1.1 thorpej * watchdog function, which provides an abstract interface to the
40 1.1 thorpej * user.
41 1.1 thorpej */
42 1.3 lukem
43 1.3 lukem #include <sys/cdefs.h>
44 1.29 pgoyette __KERNEL_RCSID(0, "$NetBSD: sysmon_wdog.c,v 1.29 2015/12/14 01:08:47 pgoyette Exp $");
45 1.1 thorpej
46 1.1 thorpej #include <sys/param.h>
47 1.1 thorpej #include <sys/conf.h>
48 1.1 thorpej #include <sys/errno.h>
49 1.1 thorpej #include <sys/fcntl.h>
50 1.24 dyoung #include <sys/condvar.h>
51 1.21 xtraeme #include <sys/mutex.h>
52 1.1 thorpej #include <sys/callout.h>
53 1.1 thorpej #include <sys/kernel.h>
54 1.1 thorpej #include <sys/systm.h>
55 1.1 thorpej #include <sys/proc.h>
56 1.26 pgoyette #include <sys/module.h>
57 1.27 pgoyette #include <sys/once.h>
58 1.1 thorpej
59 1.1 thorpej #include <dev/sysmon/sysmonvar.h>
60 1.1 thorpej
61 1.20 xtraeme static LIST_HEAD(, sysmon_wdog) sysmon_wdog_list =
62 1.1 thorpej LIST_HEAD_INITIALIZER(&sysmon_wdog_list);
63 1.20 xtraeme static int sysmon_wdog_count;
64 1.20 xtraeme static kmutex_t sysmon_wdog_list_mtx, sysmon_wdog_mtx;
65 1.24 dyoung static kcondvar_t sysmon_wdog_cv;
66 1.20 xtraeme static struct sysmon_wdog *sysmon_armed_wdog;
67 1.20 xtraeme static callout_t sysmon_wdog_callout;
68 1.20 xtraeme static void *sysmon_wdog_sdhook;
69 1.25 matt static void *sysmon_wdog_cphook;
70 1.17 ad
71 1.1 thorpej struct sysmon_wdog *sysmon_wdog_find(const char *);
72 1.1 thorpej void sysmon_wdog_release(struct sysmon_wdog *);
73 1.1 thorpej int sysmon_wdog_setmode(struct sysmon_wdog *, int, u_int);
74 1.1 thorpej void sysmon_wdog_ktickle(void *);
75 1.25 matt void sysmon_wdog_critpoll(void *);
76 1.1 thorpej void sysmon_wdog_shutdown(void *);
77 1.24 dyoung void sysmon_wdog_ref(struct sysmon_wdog *);
78 1.1 thorpej
79 1.26 pgoyette static struct sysmon_opvec sysmon_wdog_opvec = {
80 1.26 pgoyette sysmonopen_wdog, sysmonclose_wdog, sysmonioctl_wdog,
81 1.26 pgoyette NULL, NULL, NULL
82 1.26 pgoyette };
83 1.26 pgoyette
84 1.29 pgoyette MODULE(MODULE_CLASS_DRIVER, sysmon_wdog, "sysmon");
85 1.26 pgoyette
86 1.27 pgoyette ONCE_DECL(once_wdog);
87 1.27 pgoyette
88 1.27 pgoyette static int
89 1.27 pgoyette wdog_preinit(void)
90 1.27 pgoyette {
91 1.27 pgoyette
92 1.27 pgoyette mutex_init(&sysmon_wdog_list_mtx, MUTEX_DEFAULT, IPL_NONE);
93 1.27 pgoyette mutex_init(&sysmon_wdog_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK);
94 1.27 pgoyette cv_init(&sysmon_wdog_cv, "wdogref");
95 1.28 matt callout_init(&sysmon_wdog_callout, 0);
96 1.27 pgoyette
97 1.27 pgoyette return 0;
98 1.27 pgoyette }
99 1.27 pgoyette
100 1.26 pgoyette int
101 1.20 xtraeme sysmon_wdog_init(void)
102 1.20 xtraeme {
103 1.26 pgoyette int error;
104 1.26 pgoyette
105 1.27 pgoyette (void)RUN_ONCE(&once_wdog, wdog_preinit);
106 1.27 pgoyette
107 1.25 matt sysmon_wdog_sdhook = shutdownhook_establish(sysmon_wdog_shutdown, NULL);
108 1.25 matt if (sysmon_wdog_sdhook == NULL)
109 1.25 matt printf("WARNING: unable to register watchdog shutdown hook\n");
110 1.25 matt sysmon_wdog_cphook = critpollhook_establish(sysmon_wdog_critpoll, NULL);
111 1.25 matt if (sysmon_wdog_cphook == NULL)
112 1.25 matt printf("WARNING: unable to register watchdog critpoll hook\n");
113 1.26 pgoyette
114 1.26 pgoyette error = sysmon_attach_minor(SYSMON_MINOR_WDOG, &sysmon_wdog_opvec);
115 1.26 pgoyette
116 1.26 pgoyette return error;
117 1.26 pgoyette }
118 1.26 pgoyette
119 1.26 pgoyette int
120 1.26 pgoyette sysmon_wdog_fini(void)
121 1.26 pgoyette {
122 1.26 pgoyette int error;
123 1.26 pgoyette
124 1.26 pgoyette if ( ! LIST_EMPTY(&sysmon_wdog_list))
125 1.26 pgoyette return EBUSY;
126 1.26 pgoyette
127 1.26 pgoyette error = sysmon_attach_minor(SYSMON_MINOR_WDOG, NULL);
128 1.26 pgoyette
129 1.26 pgoyette if (error == 0) {
130 1.26 pgoyette callout_destroy(&sysmon_wdog_callout);
131 1.26 pgoyette critpollhook_disestablish(sysmon_wdog_cphook);
132 1.26 pgoyette shutdownhook_disestablish(sysmon_wdog_sdhook);
133 1.26 pgoyette cv_destroy(&sysmon_wdog_cv);
134 1.26 pgoyette mutex_destroy(&sysmon_wdog_mtx);
135 1.26 pgoyette mutex_destroy(&sysmon_wdog_list_mtx);
136 1.26 pgoyette }
137 1.26 pgoyette
138 1.26 pgoyette return error;
139 1.20 xtraeme }
140 1.20 xtraeme
141 1.1 thorpej /*
142 1.1 thorpej * sysmonopen_wdog:
143 1.1 thorpej *
144 1.1 thorpej * Open the system monitor device.
145 1.1 thorpej */
146 1.1 thorpej int
147 1.20 xtraeme sysmonopen_wdog(dev_t dev, int flag, int mode, struct lwp *l)
148 1.1 thorpej {
149 1.1 thorpej
150 1.20 xtraeme return 0;
151 1.1 thorpej }
152 1.1 thorpej
153 1.1 thorpej /*
154 1.1 thorpej * sysmonclose_wdog:
155 1.1 thorpej *
156 1.1 thorpej * Close the system monitor device.
157 1.1 thorpej */
158 1.1 thorpej int
159 1.20 xtraeme sysmonclose_wdog(dev_t dev, int flag, int mode, struct lwp *l)
160 1.1 thorpej {
161 1.1 thorpej struct sysmon_wdog *smw;
162 1.20 xtraeme int error = 0;
163 1.1 thorpej
164 1.1 thorpej /*
165 1.1 thorpej * If this is the last close, and there is a watchdog
166 1.1 thorpej * running in UTICKLE mode, we need to disable it,
167 1.1 thorpej * otherwise the system will reset in short order.
168 1.1 thorpej *
169 1.1 thorpej * XXX Maybe we should just go into KTICKLE mode?
170 1.1 thorpej */
171 1.20 xtraeme mutex_enter(&sysmon_wdog_mtx);
172 1.1 thorpej if ((smw = sysmon_armed_wdog) != NULL) {
173 1.11 drochner if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_UTICKLE) {
174 1.1 thorpej error = sysmon_wdog_setmode(smw,
175 1.1 thorpej WDOG_MODE_DISARMED, smw->smw_period);
176 1.1 thorpej if (error) {
177 1.1 thorpej printf("WARNING: UNABLE TO DISARM "
178 1.1 thorpej "WATCHDOG %s ON CLOSE!\n",
179 1.1 thorpej smw->smw_name);
180 1.1 thorpej /*
181 1.1 thorpej * ...we will probably reboot soon.
182 1.1 thorpej */
183 1.1 thorpej }
184 1.1 thorpej }
185 1.1 thorpej }
186 1.20 xtraeme mutex_exit(&sysmon_wdog_mtx);
187 1.1 thorpej
188 1.20 xtraeme return error;
189 1.1 thorpej }
190 1.1 thorpej
191 1.1 thorpej /*
192 1.1 thorpej * sysmonioctl_wdog:
193 1.1 thorpej *
194 1.1 thorpej * Perform a watchdog control request.
195 1.1 thorpej */
196 1.1 thorpej int
197 1.20 xtraeme sysmonioctl_wdog(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
198 1.1 thorpej {
199 1.1 thorpej struct sysmon_wdog *smw;
200 1.20 xtraeme int error = 0;
201 1.1 thorpej
202 1.1 thorpej switch (cmd) {
203 1.1 thorpej case WDOGIOC_GMODE:
204 1.1 thorpej {
205 1.1 thorpej struct wdog_mode *wm = (void *) data;
206 1.1 thorpej
207 1.1 thorpej wm->wm_name[sizeof(wm->wm_name) - 1] = '\0';
208 1.1 thorpej smw = sysmon_wdog_find(wm->wm_name);
209 1.1 thorpej if (smw == NULL) {
210 1.1 thorpej error = ESRCH;
211 1.1 thorpej break;
212 1.1 thorpej }
213 1.1 thorpej
214 1.1 thorpej wm->wm_mode = smw->smw_mode;
215 1.1 thorpej wm->wm_period = smw->smw_period;
216 1.1 thorpej sysmon_wdog_release(smw);
217 1.1 thorpej break;
218 1.1 thorpej }
219 1.1 thorpej
220 1.1 thorpej case WDOGIOC_SMODE:
221 1.1 thorpej {
222 1.1 thorpej struct wdog_mode *wm = (void *) data;
223 1.1 thorpej
224 1.1 thorpej if ((flag & FWRITE) == 0) {
225 1.1 thorpej error = EPERM;
226 1.1 thorpej break;
227 1.1 thorpej }
228 1.1 thorpej
229 1.1 thorpej wm->wm_name[sizeof(wm->wm_name) - 1] = '\0';
230 1.1 thorpej smw = sysmon_wdog_find(wm->wm_name);
231 1.1 thorpej if (smw == NULL) {
232 1.1 thorpej error = ESRCH;
233 1.1 thorpej break;
234 1.1 thorpej }
235 1.1 thorpej
236 1.1 thorpej if (wm->wm_mode & ~(WDOG_MODE_MASK|WDOG_FEATURE_MASK))
237 1.1 thorpej error = EINVAL;
238 1.1 thorpej else {
239 1.20 xtraeme mutex_enter(&sysmon_wdog_mtx);
240 1.1 thorpej error = sysmon_wdog_setmode(smw, wm->wm_mode,
241 1.1 thorpej wm->wm_period);
242 1.20 xtraeme mutex_exit(&sysmon_wdog_mtx);
243 1.1 thorpej }
244 1.1 thorpej
245 1.1 thorpej sysmon_wdog_release(smw);
246 1.1 thorpej break;
247 1.1 thorpej }
248 1.1 thorpej
249 1.1 thorpej case WDOGIOC_WHICH:
250 1.1 thorpej {
251 1.1 thorpej struct wdog_mode *wm = (void *) data;
252 1.1 thorpej
253 1.20 xtraeme mutex_enter(&sysmon_wdog_mtx);
254 1.1 thorpej if ((smw = sysmon_armed_wdog) != NULL) {
255 1.1 thorpej strcpy(wm->wm_name, smw->smw_name);
256 1.1 thorpej wm->wm_mode = smw->smw_mode;
257 1.1 thorpej wm->wm_period = smw->smw_period;
258 1.1 thorpej } else
259 1.1 thorpej error = ESRCH;
260 1.20 xtraeme mutex_exit(&sysmon_wdog_mtx);
261 1.1 thorpej break;
262 1.1 thorpej }
263 1.1 thorpej
264 1.1 thorpej case WDOGIOC_TICKLE:
265 1.1 thorpej if ((flag & FWRITE) == 0) {
266 1.1 thorpej error = EPERM;
267 1.1 thorpej break;
268 1.1 thorpej }
269 1.1 thorpej
270 1.20 xtraeme mutex_enter(&sysmon_wdog_mtx);
271 1.1 thorpej if ((smw = sysmon_armed_wdog) != NULL) {
272 1.1 thorpej error = (*smw->smw_tickle)(smw);
273 1.1 thorpej if (error == 0)
274 1.12 christos smw->smw_tickler = l->l_proc->p_pid;
275 1.1 thorpej } else
276 1.1 thorpej error = ESRCH;
277 1.20 xtraeme mutex_exit(&sysmon_wdog_mtx);
278 1.1 thorpej break;
279 1.1 thorpej
280 1.1 thorpej case WDOGIOC_GTICKLER:
281 1.4 simonb if ((smw = sysmon_armed_wdog) != NULL)
282 1.4 simonb *(pid_t *)data = smw->smw_tickler;
283 1.4 simonb else
284 1.4 simonb error = ESRCH;
285 1.1 thorpej break;
286 1.1 thorpej
287 1.1 thorpej case WDOGIOC_GWDOGS:
288 1.1 thorpej {
289 1.1 thorpej struct wdog_conf *wc = (void *) data;
290 1.1 thorpej char *cp;
291 1.1 thorpej int i;
292 1.1 thorpej
293 1.20 xtraeme mutex_enter(&sysmon_wdog_list_mtx);
294 1.1 thorpej if (wc->wc_names == NULL)
295 1.1 thorpej wc->wc_count = sysmon_wdog_count;
296 1.1 thorpej else {
297 1.1 thorpej for (i = 0, cp = wc->wc_names,
298 1.1 thorpej smw = LIST_FIRST(&sysmon_wdog_list);
299 1.1 thorpej i < sysmon_wdog_count && smw != NULL && error == 0;
300 1.1 thorpej i++, cp += WDOG_NAMESIZE,
301 1.1 thorpej smw = LIST_NEXT(smw, smw_list))
302 1.1 thorpej error = copyout(smw->smw_name, cp,
303 1.1 thorpej strlen(smw->smw_name) + 1);
304 1.1 thorpej wc->wc_count = i;
305 1.1 thorpej }
306 1.20 xtraeme mutex_exit(&sysmon_wdog_list_mtx);
307 1.1 thorpej break;
308 1.1 thorpej }
309 1.1 thorpej
310 1.1 thorpej default:
311 1.1 thorpej error = ENOTTY;
312 1.1 thorpej }
313 1.1 thorpej
314 1.20 xtraeme return error;
315 1.1 thorpej }
316 1.1 thorpej
317 1.1 thorpej /*
318 1.1 thorpej * sysmon_wdog_register:
319 1.1 thorpej *
320 1.1 thorpej * Register a watchdog device.
321 1.1 thorpej */
322 1.1 thorpej int
323 1.1 thorpej sysmon_wdog_register(struct sysmon_wdog *smw)
324 1.1 thorpej {
325 1.1 thorpej struct sysmon_wdog *lsmw;
326 1.1 thorpej int error = 0;
327 1.1 thorpej
328 1.27 pgoyette (void)RUN_ONCE(&once_wdog, wdog_preinit);
329 1.27 pgoyette
330 1.20 xtraeme mutex_enter(&sysmon_wdog_list_mtx);
331 1.1 thorpej
332 1.23 dyoung LIST_FOREACH(lsmw, &sysmon_wdog_list, smw_list) {
333 1.1 thorpej if (strcmp(lsmw->smw_name, smw->smw_name) == 0) {
334 1.1 thorpej error = EEXIST;
335 1.1 thorpej goto out;
336 1.1 thorpej }
337 1.1 thorpej }
338 1.1 thorpej
339 1.1 thorpej smw->smw_mode = WDOG_MODE_DISARMED;
340 1.1 thorpej smw->smw_tickler = (pid_t) -1;
341 1.1 thorpej smw->smw_refcnt = 0;
342 1.1 thorpej sysmon_wdog_count++;
343 1.1 thorpej LIST_INSERT_HEAD(&sysmon_wdog_list, smw, smw_list);
344 1.1 thorpej
345 1.1 thorpej out:
346 1.20 xtraeme mutex_exit(&sysmon_wdog_list_mtx);
347 1.20 xtraeme return error;
348 1.1 thorpej }
349 1.1 thorpej
350 1.1 thorpej /*
351 1.1 thorpej * sysmon_wdog_unregister:
352 1.1 thorpej *
353 1.1 thorpej * Unregister a watchdog device.
354 1.1 thorpej */
355 1.24 dyoung int
356 1.1 thorpej sysmon_wdog_unregister(struct sysmon_wdog *smw)
357 1.1 thorpej {
358 1.24 dyoung int rc = 0;
359 1.1 thorpej
360 1.20 xtraeme mutex_enter(&sysmon_wdog_list_mtx);
361 1.24 dyoung while (smw->smw_refcnt > 0 && rc == 0) {
362 1.24 dyoung aprint_debug("%s: %d users remain\n", smw->smw_name,
363 1.24 dyoung smw->smw_refcnt);
364 1.24 dyoung rc = cv_wait_sig(&sysmon_wdog_cv, &sysmon_wdog_list_mtx);
365 1.24 dyoung }
366 1.24 dyoung if (rc == 0) {
367 1.24 dyoung sysmon_wdog_count--;
368 1.24 dyoung LIST_REMOVE(smw, smw_list);
369 1.24 dyoung }
370 1.20 xtraeme mutex_exit(&sysmon_wdog_list_mtx);
371 1.24 dyoung return rc;
372 1.1 thorpej }
373 1.1 thorpej
374 1.1 thorpej /*
375 1.25 matt * sysmon_wdog_critpoll:
376 1.25 matt *
377 1.25 matt * Perform critical operations during long polling periods
378 1.25 matt */
379 1.25 matt void
380 1.25 matt sysmon_wdog_critpoll(void *arg)
381 1.25 matt {
382 1.25 matt struct sysmon_wdog *smw = sysmon_armed_wdog;
383 1.25 matt
384 1.25 matt if (smw == NULL)
385 1.25 matt return;
386 1.25 matt
387 1.25 matt if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_KTICKLE) {
388 1.25 matt if ((*smw->smw_tickle)(smw) != 0) {
389 1.25 matt printf("WARNING: KERNEL TICKLE OF WATCHDOG %s "
390 1.25 matt "FAILED!\n", smw->smw_name);
391 1.25 matt }
392 1.25 matt }
393 1.25 matt }
394 1.25 matt
395 1.25 matt /*
396 1.1 thorpej * sysmon_wdog_find:
397 1.1 thorpej *
398 1.1 thorpej * Find a watchdog device. We increase the reference
399 1.1 thorpej * count on a match.
400 1.1 thorpej */
401 1.1 thorpej struct sysmon_wdog *
402 1.1 thorpej sysmon_wdog_find(const char *name)
403 1.1 thorpej {
404 1.1 thorpej struct sysmon_wdog *smw;
405 1.1 thorpej
406 1.20 xtraeme mutex_enter(&sysmon_wdog_list_mtx);
407 1.1 thorpej
408 1.23 dyoung LIST_FOREACH(smw, &sysmon_wdog_list, smw_list) {
409 1.1 thorpej if (strcmp(smw->smw_name, name) == 0)
410 1.1 thorpej break;
411 1.1 thorpej }
412 1.1 thorpej
413 1.1 thorpej if (smw != NULL)
414 1.1 thorpej smw->smw_refcnt++;
415 1.1 thorpej
416 1.20 xtraeme mutex_exit(&sysmon_wdog_list_mtx);
417 1.20 xtraeme return smw;
418 1.1 thorpej }
419 1.1 thorpej
420 1.1 thorpej /*
421 1.1 thorpej * sysmon_wdog_release:
422 1.1 thorpej *
423 1.1 thorpej * Release a watchdog device.
424 1.1 thorpej */
425 1.1 thorpej void
426 1.1 thorpej sysmon_wdog_release(struct sysmon_wdog *smw)
427 1.1 thorpej {
428 1.1 thorpej
429 1.20 xtraeme mutex_enter(&sysmon_wdog_list_mtx);
430 1.1 thorpej KASSERT(smw->smw_refcnt != 0);
431 1.1 thorpej smw->smw_refcnt--;
432 1.24 dyoung cv_signal(&sysmon_wdog_cv);
433 1.24 dyoung mutex_exit(&sysmon_wdog_list_mtx);
434 1.24 dyoung }
435 1.24 dyoung
436 1.24 dyoung void
437 1.24 dyoung sysmon_wdog_ref(struct sysmon_wdog *smw)
438 1.24 dyoung {
439 1.24 dyoung mutex_enter(&sysmon_wdog_list_mtx);
440 1.24 dyoung smw->smw_refcnt++;
441 1.20 xtraeme mutex_exit(&sysmon_wdog_list_mtx);
442 1.1 thorpej }
443 1.1 thorpej
444 1.1 thorpej /*
445 1.1 thorpej * sysmon_wdog_setmode:
446 1.1 thorpej *
447 1.1 thorpej * Set the mode of a watchdog device.
448 1.1 thorpej */
449 1.1 thorpej int
450 1.1 thorpej sysmon_wdog_setmode(struct sysmon_wdog *smw, int mode, u_int period)
451 1.1 thorpej {
452 1.1 thorpej u_int operiod = smw->smw_period;
453 1.1 thorpej int omode = smw->smw_mode;
454 1.1 thorpej int error = 0;
455 1.1 thorpej
456 1.1 thorpej smw->smw_period = period;
457 1.1 thorpej smw->smw_mode = mode;
458 1.1 thorpej
459 1.1 thorpej switch (mode & WDOG_MODE_MASK) {
460 1.1 thorpej case WDOG_MODE_DISARMED:
461 1.1 thorpej if (smw != sysmon_armed_wdog) {
462 1.1 thorpej error = EINVAL;
463 1.1 thorpej goto out;
464 1.1 thorpej }
465 1.1 thorpej break;
466 1.1 thorpej
467 1.1 thorpej case WDOG_MODE_KTICKLE:
468 1.1 thorpej case WDOG_MODE_UTICKLE:
469 1.10 smb case WDOG_MODE_ETICKLE:
470 1.1 thorpej if (sysmon_armed_wdog != NULL) {
471 1.1 thorpej error = EBUSY;
472 1.1 thorpej goto out;
473 1.1 thorpej }
474 1.1 thorpej break;
475 1.1 thorpej
476 1.1 thorpej default:
477 1.1 thorpej error = EINVAL;
478 1.1 thorpej goto out;
479 1.1 thorpej }
480 1.1 thorpej
481 1.1 thorpej error = (*smw->smw_setmode)(smw);
482 1.1 thorpej
483 1.1 thorpej out:
484 1.1 thorpej if (error) {
485 1.1 thorpej smw->smw_period = operiod;
486 1.1 thorpej smw->smw_mode = omode;
487 1.1 thorpej } else {
488 1.1 thorpej if ((mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
489 1.2 thorpej sysmon_armed_wdog = NULL;
490 1.1 thorpej smw->smw_tickler = (pid_t) -1;
491 1.24 dyoung sysmon_wdog_release(smw);
492 1.1 thorpej if ((omode & WDOG_MODE_MASK) == WDOG_MODE_KTICKLE)
493 1.1 thorpej callout_stop(&sysmon_wdog_callout);
494 1.1 thorpej } else {
495 1.1 thorpej sysmon_armed_wdog = smw;
496 1.24 dyoung sysmon_wdog_ref(smw);
497 1.1 thorpej if ((mode & WDOG_MODE_MASK) == WDOG_MODE_KTICKLE) {
498 1.1 thorpej callout_reset(&sysmon_wdog_callout,
499 1.1 thorpej WDOG_PERIOD_TO_TICKS(smw->smw_period) / 2,
500 1.1 thorpej sysmon_wdog_ktickle, NULL);
501 1.1 thorpej }
502 1.1 thorpej }
503 1.1 thorpej }
504 1.20 xtraeme return error;
505 1.1 thorpej }
506 1.1 thorpej
507 1.1 thorpej /*
508 1.1 thorpej * sysmon_wdog_ktickle:
509 1.1 thorpej *
510 1.1 thorpej * Kernel watchdog tickle routine.
511 1.1 thorpej */
512 1.1 thorpej void
513 1.14 christos sysmon_wdog_ktickle(void *arg)
514 1.1 thorpej {
515 1.1 thorpej struct sysmon_wdog *smw;
516 1.1 thorpej
517 1.20 xtraeme mutex_enter(&sysmon_wdog_mtx);
518 1.1 thorpej if ((smw = sysmon_armed_wdog) != NULL) {
519 1.1 thorpej if ((*smw->smw_tickle)(smw) != 0) {
520 1.1 thorpej printf("WARNING: KERNEL TICKLE OF WATCHDOG %s "
521 1.1 thorpej "FAILED!\n", smw->smw_name);
522 1.1 thorpej /*
523 1.1 thorpej * ...we will probably reboot soon.
524 1.1 thorpej */
525 1.1 thorpej }
526 1.1 thorpej callout_reset(&sysmon_wdog_callout,
527 1.1 thorpej WDOG_PERIOD_TO_TICKS(smw->smw_period) / 2,
528 1.1 thorpej sysmon_wdog_ktickle, NULL);
529 1.1 thorpej }
530 1.20 xtraeme mutex_exit(&sysmon_wdog_mtx);
531 1.1 thorpej }
532 1.1 thorpej
533 1.1 thorpej /*
534 1.1 thorpej * sysmon_wdog_shutdown:
535 1.1 thorpej *
536 1.1 thorpej * Perform shutdown-time operations.
537 1.1 thorpej */
538 1.1 thorpej void
539 1.14 christos sysmon_wdog_shutdown(void *arg)
540 1.1 thorpej {
541 1.1 thorpej struct sysmon_wdog *smw;
542 1.1 thorpej
543 1.1 thorpej /*
544 1.1 thorpej * XXX Locking here? I don't think it's necessary.
545 1.1 thorpej */
546 1.1 thorpej
547 1.1 thorpej if ((smw = sysmon_armed_wdog) != NULL) {
548 1.1 thorpej if (sysmon_wdog_setmode(smw, WDOG_MODE_DISARMED,
549 1.1 thorpej smw->smw_period))
550 1.1 thorpej printf("WARNING: FAILED TO SHUTDOWN WATCHDOG %s!\n",
551 1.1 thorpej smw->smw_name);
552 1.1 thorpej }
553 1.1 thorpej }
554 1.26 pgoyette static
555 1.26 pgoyette int
556 1.26 pgoyette sysmon_wdog_modcmd(modcmd_t cmd, void *arg)
557 1.26 pgoyette {
558 1.26 pgoyette int ret;
559 1.26 pgoyette
560 1.26 pgoyette switch (cmd) {
561 1.26 pgoyette case MODULE_CMD_INIT:
562 1.26 pgoyette ret = sysmon_wdog_init();
563 1.26 pgoyette break;
564 1.26 pgoyette
565 1.26 pgoyette case MODULE_CMD_FINI:
566 1.26 pgoyette ret = sysmon_wdog_fini();
567 1.26 pgoyette break;
568 1.26 pgoyette
569 1.26 pgoyette case MODULE_CMD_STAT:
570 1.26 pgoyette default:
571 1.26 pgoyette ret = ENOTTY;
572 1.26 pgoyette }
573 1.26 pgoyette
574 1.26 pgoyette return ret;
575 1.26 pgoyette }
576