xref: /openbsd-src/sys/kern/kern_watchdog.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*      $OpenBSD: kern_watchdog.c,v 1.1 2003/01/21 16:59:23 markus 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_init(void);
33 void	wdog_tickle(void *arg);
34 void	wdog_shutdown(void *arg);
35 int	(*wdog_ctl_cb)(void *, int) = NULL;
36 void	*wdog_ctl_cb_arg = NULL;
37 int	wdog_period = 0;
38 int	wdog_auto = 1;
39 struct timeout	wdog_timeout;
40 
41 void
42 wdog_init(void)
43 {
44 	timeout_set(&wdog_timeout, wdog_tickle, NULL);
45 	shutdownhook_establish(wdog_shutdown, NULL);
46 }
47 
48 void
49 wdog_register(void *cb_arg, int (*cb)(void *, int))
50 {
51 	if (wdog_ctl_cb != NULL)
52 		return;
53 	wdog_init();
54 	wdog_ctl_cb = cb;
55 	wdog_ctl_cb_arg = cb_arg;
56 }
57 
58 void
59 wdog_tickle(void *arg)
60 {
61 	if (wdog_ctl_cb == NULL)
62 		return;
63 	(void) (*wdog_ctl_cb)(wdog_ctl_cb_arg, wdog_period);
64 	timeout_add(&wdog_timeout, wdog_period * hz / 2);
65 }
66 
67 void
68 wdog_shutdown(void *arg)
69 {
70 	if (wdog_ctl_cb == NULL)
71 		return;
72 	timeout_del(&wdog_timeout);
73 	(void) (*wdog_ctl_cb)(wdog_ctl_cb_arg, 0);
74 }
75 
76 int
77 sysctl_wdog(name, namelen, oldp, oldlenp, newp, newlen)
78 	int *name;
79 	u_int namelen;
80 	void *oldp;
81 	size_t *oldlenp;
82 	void *newp;
83 	size_t newlen;
84 {
85 	int error, period;
86 
87 	if (wdog_ctl_cb == NULL)
88 		return (EOPNOTSUPP);
89 
90 	switch (name[0]) {
91 	case KERN_WATCHDOG_PERIOD:
92 		period = wdog_period;
93 		error = sysctl_int(oldp, oldlenp, newp, newlen, &period);
94 		if (error)
95 			return (error);
96 		if (newp) {
97 			timeout_del(&wdog_timeout);
98 			wdog_period = (*wdog_ctl_cb)(wdog_ctl_cb_arg, period);
99 		}
100 		break;
101 	case KERN_WATCHDOG_AUTO:
102 		error = sysctl_int(oldp, oldlenp, newp, newlen, &wdog_auto);
103 		if (error)
104 			return (error);
105 		break;
106 	default:
107 		return (EINVAL);
108 	}
109 
110 	timeout_del(&wdog_timeout);
111 	if (wdog_auto && wdog_period > 0) {
112 		(void) (*wdog_ctl_cb)(wdog_ctl_cb_arg, wdog_period);
113 		timeout_add(&wdog_timeout, wdog_period * hz / 2);
114 	}
115 	return (error);
116 }
117