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