xref: /openbsd-src/sys/kern/kern_watchdog.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*      $OpenBSD: kern_watchdog.c,v 1.7 2006/12/21 12:47:57 mbalmer Exp $        */
2 
3 /*
4  * Copyright (c) 2003 Markus Friedl.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #include <sys/param.h>
27 #include <sys/kernel.h>
28 #include <sys/systm.h>
29 #include <sys/sysctl.h>
30 #include <sys/time.h>
31 
32 void	wdog_tickle(void *arg);
33 void	wdog_shutdown(void *arg);
34 int	(*wdog_ctl_cb)(void *, int) = NULL;
35 void	*wdog_ctl_cb_arg = NULL;
36 int	wdog_period = 0;
37 int	wdog_auto = 1;
38 void	*wdog_cookie;
39 struct	timeout wdog_timeout;
40 
41 void
42 wdog_register(void *cb_arg, int (*cb)(void *, int))
43 {
44 	if (wdog_ctl_cb != NULL)
45 		return;
46 
47 	wdog_ctl_cb = cb;
48 	wdog_ctl_cb_arg = cb_arg;
49 	timeout_set(&wdog_timeout, wdog_tickle, NULL);
50 	wdog_cookie = shutdownhook_establish(wdog_shutdown, NULL);
51 }
52 
53 void
54 wdog_tickle(void *arg)
55 {
56 	if (wdog_ctl_cb == NULL)
57 		return;
58 	(void) (*wdog_ctl_cb)(wdog_ctl_cb_arg, wdog_period);
59 	timeout_add(&wdog_timeout, wdog_period * hz / 2);
60 }
61 
62 void
63 wdog_shutdown(void *arg)
64 {
65 	if (wdog_ctl_cb == NULL)
66 		return;
67 	timeout_del(&wdog_timeout);
68 	(void) (*wdog_ctl_cb)(wdog_ctl_cb_arg, 0);
69 	wdog_ctl_cb = NULL;
70 	wdog_period = 0;
71 	wdog_auto = 1;
72 }
73 
74 int
75 sysctl_wdog(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
76     size_t newlen)
77 {
78 	int error, period;
79 
80 	if (wdog_ctl_cb == NULL)
81 		return (EOPNOTSUPP);
82 
83 	switch (name[0]) {
84 	case KERN_WATCHDOG_PERIOD:
85 		period = wdog_period;
86 		error = sysctl_int(oldp, oldlenp, newp, newlen, &period);
87 		if (error)
88 			return (error);
89 		if (newp) {
90 			timeout_del(&wdog_timeout);
91 			wdog_period = (*wdog_ctl_cb)(wdog_ctl_cb_arg, period);
92 		}
93 		break;
94 	case KERN_WATCHDOG_AUTO:
95 		error = sysctl_int(oldp, oldlenp, newp, newlen, &wdog_auto);
96 		if (error)
97 			return (error);
98 		if (wdog_auto && wdog_cookie == NULL)
99 			wdog_cookie = shutdownhook_establish(wdog_shutdown,
100 			    NULL);
101 		else if (!wdog_auto && wdog_cookie) {
102 			shutdownhook_disestablish(wdog_cookie);
103 			wdog_cookie = NULL;
104 		}
105 		break;
106 	default:
107 		return (EINVAL);
108 	}
109 
110 	if (wdog_auto && wdog_period > 0) {
111 		(void) (*wdog_ctl_cb)(wdog_ctl_cb_arg, wdog_period);
112 		timeout_add(&wdog_timeout, wdog_period * hz / 2);
113 	} else
114 		timeout_del(&wdog_timeout);
115 
116 	return (error);
117 }
118