xref: /openbsd-src/sys/kern/kern_watchdog.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*      $OpenBSD: kern_watchdog.c,v 1.8 2010/04/20 22:05:43 tedu 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/proc.h>
30 #include <sys/sysctl.h>
31 #include <sys/time.h>
32 
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 void	*wdog_cookie;
40 struct	timeout wdog_timeout;
41 
42 void
43 wdog_register(void *cb_arg, int (*cb)(void *, int))
44 {
45 	if (wdog_ctl_cb != NULL)
46 		return;
47 
48 	wdog_ctl_cb = cb;
49 	wdog_ctl_cb_arg = cb_arg;
50 	timeout_set(&wdog_timeout, wdog_tickle, NULL);
51 	wdog_cookie = shutdownhook_establish(wdog_shutdown, NULL);
52 }
53 
54 void
55 wdog_tickle(void *arg)
56 {
57 	if (wdog_ctl_cb == NULL)
58 		return;
59 	(void) (*wdog_ctl_cb)(wdog_ctl_cb_arg, wdog_period);
60 	timeout_add(&wdog_timeout, wdog_period * hz / 2);
61 }
62 
63 void
64 wdog_shutdown(void *arg)
65 {
66 	if (wdog_ctl_cb == NULL)
67 		return;
68 	timeout_del(&wdog_timeout);
69 	(void) (*wdog_ctl_cb)(wdog_ctl_cb_arg, 0);
70 	wdog_ctl_cb = NULL;
71 	wdog_period = 0;
72 	wdog_auto = 1;
73 }
74 
75 int
76 sysctl_wdog(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
77     size_t newlen)
78 {
79 	int error, period;
80 
81 	if (wdog_ctl_cb == NULL)
82 		return (EOPNOTSUPP);
83 
84 	switch (name[0]) {
85 	case KERN_WATCHDOG_PERIOD:
86 		period = wdog_period;
87 		error = sysctl_int(oldp, oldlenp, newp, newlen, &period);
88 		if (error)
89 			return (error);
90 		if (newp) {
91 			timeout_del(&wdog_timeout);
92 			wdog_period = (*wdog_ctl_cb)(wdog_ctl_cb_arg, period);
93 		}
94 		break;
95 	case KERN_WATCHDOG_AUTO:
96 		error = sysctl_int(oldp, oldlenp, newp, newlen, &wdog_auto);
97 		if (error)
98 			return (error);
99 		if (wdog_auto && wdog_cookie == NULL)
100 			wdog_cookie = shutdownhook_establish(wdog_shutdown,
101 			    NULL);
102 		else if (!wdog_auto && wdog_cookie) {
103 			shutdownhook_disestablish(wdog_cookie);
104 			wdog_cookie = NULL;
105 		}
106 		break;
107 	default:
108 		return (EINVAL);
109 	}
110 
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 	} else
115 		timeout_del(&wdog_timeout);
116 
117 	return (error);
118 }
119