This source file includes following definitions.
- wdog_register
- wdog_tickle
- wdog_shutdown
- sysctl_wdog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 }