xref: /openbsd/sys/kern/kern_watchdog.c (revision 0d280c5f)
1 /*      $OpenBSD: kern_watchdog.c,v 1.16 2022/08/14 01:58:27 jsg 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/systm.h>
28 #include <sys/timeout.h>
29 #include <sys/sysctl.h>
30 
31 void	wdog_tickle(void *arg);
32 int	(*wdog_ctl_cb)(void *, int) = NULL;
33 void	*wdog_ctl_cb_arg = NULL;
34 int	wdog_period = 0;
35 int	wdog_auto = 1;
36 struct	timeout wdog_timeout;
37 
38 void
wdog_register(int (* cb)(void *,int),void * cb_arg)39 wdog_register(int (*cb)(void *, int), void *cb_arg)
40 {
41 	if (wdog_ctl_cb != NULL)
42 		return;
43 
44 	wdog_ctl_cb = cb;
45 	wdog_ctl_cb_arg = cb_arg;
46 	timeout_set(&wdog_timeout, wdog_tickle, NULL);
47 }
48 
49 void
wdog_tickle(void * arg)50 wdog_tickle(void *arg)
51 {
52 	if (wdog_ctl_cb == NULL)
53 		return;
54 	(void) (*wdog_ctl_cb)(wdog_ctl_cb_arg, wdog_period);
55 	timeout_add_msec(&wdog_timeout, wdog_period * 1000 / 2);
56 }
57 
58 void
wdog_shutdown(void * arg)59 wdog_shutdown(void *arg)
60 {
61 	if (wdog_ctl_cb == NULL || wdog_ctl_cb_arg != arg)
62 		return;
63 	timeout_del(&wdog_timeout);
64 	(void) (*wdog_ctl_cb)(wdog_ctl_cb_arg, 0);
65 	wdog_ctl_cb = NULL;
66 	wdog_period = 0;
67 	wdog_auto = 1;
68 }
69 
70 int
sysctl_wdog(int * name,u_int namelen,void * oldp,size_t * oldlenp,void * newp,size_t newlen)71 sysctl_wdog(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
72     size_t newlen)
73 {
74 	int error, period;
75 
76 	if (wdog_ctl_cb == NULL)
77 		return (EOPNOTSUPP);
78 
79 	switch (name[0]) {
80 	case KERN_WATCHDOG_PERIOD:
81 		period = wdog_period;
82 		error = sysctl_int_bounded(oldp, oldlenp, newp, newlen,
83 		    &period, 0, INT_MAX);
84 		if (error)
85 			return (error);
86 		if (newp) {
87 			timeout_del(&wdog_timeout);
88 			wdog_period = (*wdog_ctl_cb)(wdog_ctl_cb_arg, period);
89 		}
90 		break;
91 	case KERN_WATCHDOG_AUTO:
92 		error = sysctl_int_bounded(oldp, oldlenp, newp, newlen,
93 		    &wdog_auto, 0, 1);
94 		if (error)
95 			return (error);
96 		break;
97 	default:
98 		return (EINVAL);
99 	}
100 
101 	if (wdog_auto && wdog_period > 0) {
102 		(void) (*wdog_ctl_cb)(wdog_ctl_cb_arg, wdog_period);
103 		timeout_add_msec(&wdog_timeout, wdog_period * 1000 / 2);
104 	} else
105 		timeout_del(&wdog_timeout);
106 
107 	return (error);
108 }
109