nlookup - introduce nlookup_init_root
[dragonfly.git] / sys / kern / kern_wdog.c
blobc9ca5730fe6e640543cf1e3aed39ceabb6647ecd
1 /*
2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 #include "opt_cpu.h"
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/sysproto.h>
40 #include <sys/ioccom.h>
41 #include <sys/spinlock2.h>
42 #include <sys/conf.h>
43 #include <sys/device.h>
44 #include <sys/filedesc.h>
45 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/event.h>
48 #include <sys/sysctl.h>
49 #include <sys/queue.h>
50 #include <sys/wdog.h>
51 #include <machine/limits.h>
53 #ifdef WATCHDOG_ENABLE
54 static LIST_HEAD(, watchdog) wdoglist = LIST_HEAD_INITIALIZER(&wdoglist);
55 static struct spinlock wdogmtx;
56 static struct callout wdog_callout;
58 static int wdog_auto_enable = 1;
59 static int wdog_auto_period = WDOG_DEFAULT_PERIOD;
61 static void wdog_reset_all(void *unused);
63 void
64 wdog_register(struct watchdog *wd)
66 spin_lock_wr(&wdogmtx);
67 wd->period = WDOG_DEFAULT_PERIOD;
68 LIST_INSERT_HEAD(&wdoglist, wd, link);
69 spin_unlock_wr(&wdogmtx);
71 wdog_reset_all(NULL);
73 kprintf("wdog: Watchdog %s registered, max period = %ds , period = %ds\n",
74 wd->name, wd->period_max, wd->period);
77 void
78 wdog_unregister(struct watchdog *wd)
80 spin_lock_wr(&wdogmtx);
81 LIST_REMOVE(wd, link);
82 spin_unlock_wr(&wdogmtx);
84 kprintf("wdog: Watchdog %s unregistered\n", wd->name);
87 static int
88 wdog_reset(struct watchdog *wd)
90 return (wd->period = wd->wdog_fn(wd->arg, wd->period));
93 static void
94 wdog_reset_all(void *unused)
96 struct watchdog *wd;
97 int period, min_period = INT_MAX;
99 spin_lock_wr(&wdogmtx);
100 LIST_FOREACH(wd, &wdoglist, link) {
101 period = wdog_reset(wd);
102 if (period < min_period)
103 min_period = period;
105 if (wdog_auto_enable)
106 callout_reset(&wdog_callout, min_period * hz / 2, wdog_reset_all, NULL);
108 wdog_auto_period = min_period;
110 spin_unlock_wr(&wdogmtx);
113 static void
114 wdog_set_period(int period)
116 struct watchdog *wd;
118 spin_lock_wr(&wdogmtx);
119 LIST_FOREACH(wd, &wdoglist, link) {
120 /* XXX: check for period_max */
121 wd->period = period;
123 spin_unlock_wr(&wdogmtx);
127 static int
128 wdog_sysctl_auto(SYSCTL_HANDLER_ARGS)
130 int error;
132 error = sysctl_handle_int(oidp, &wdog_auto_enable, 1, req);
133 if (error || req->newptr == NULL)
134 return error;
136 /* has changed, do something */
137 callout_stop(&wdog_callout);
138 if (wdog_auto_enable) {
139 wdog_reset_all(NULL);
142 kprintf("wdog: In-kernel automatic watchdog reset %s\n",
143 (wdog_auto_enable)?"enabled":"disabled");
145 return 0;
148 static int
149 wdog_sysctl_period(SYSCTL_HANDLER_ARGS)
151 int error;
153 error = sysctl_handle_int(oidp, &wdog_auto_period, WDOG_DEFAULT_PERIOD, req);
154 if (error || req->newptr == NULL)
155 return error;
157 /* has changed, do something */
158 callout_stop(&wdog_callout);
159 wdog_set_period(wdog_auto_period);
160 wdog_reset_all(NULL);
162 if (wdog_auto_period != 0)
163 kprintf("wdog: Watchdog period set to %ds\n", wdog_auto_period);
164 else
165 kprintf("wdog: Disabled watchdog(s)\n");
167 return 0;
170 void
171 wdog_disable(void)
173 callout_stop(&wdog_callout);
174 wdog_set_period(0);
175 wdog_reset_all(NULL);
178 static SYSCTL_NODE(_kern, OID_AUTO, watchdog, CTLFLAG_RW, 0, "watchdog");
179 SYSCTL_PROC(_kern_watchdog, OID_AUTO, auto, CTLTYPE_INT | CTLFLAG_RW,
180 NULL, 0, wdog_sysctl_auto, "I", "auto in-kernel watchdog reset "
181 "(0 = disabled, 1 = enabled)");
182 SYSCTL_PROC(_kern_watchdog, OID_AUTO, period, CTLTYPE_INT | CTLFLAG_RW,
183 NULL, 0, wdog_sysctl_period, "I", "watchdog period "
184 "(value in seconds)");
187 static int
188 wdog_ioctl(struct dev_ioctl_args *ap)
190 if (wdog_auto_enable)
191 return EINVAL;
193 if (ap->a_cmd == WDIOCRESET) {
194 wdog_reset_all(NULL);
195 } else {
196 return EINVAL;
199 return 0;
202 static struct dev_ops wdog_ops = {
203 { "wdog", 0, 0 },
204 .d_ioctl = wdog_ioctl,
207 static void
208 wdog_init(void)
210 spin_init(&wdogmtx);
211 make_dev(&wdog_ops, 0,
212 UID_ROOT, GID_WHEEL, 0600, "wdog");
213 callout_init_mp(&wdog_callout);
215 kprintf("wdog: In-kernel automatic watchdog reset %s\n",
216 (wdog_auto_enable)?"enabled":"disabled");
219 static void
220 wdog_uninit(void)
222 callout_stop(&wdog_callout);
223 callout_deactivate(&wdog_callout);
224 dev_ops_remove_all(&wdog_ops);
225 spin_uninit(&wdogmtx);
228 SYSINIT(wdog_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, wdog_init, NULL);
229 SYSUNINIT(wdog_register, SI_SUB_PRE_DRIVERS, SI_ORDER_ANY, wdog_uninit, NULL);
231 #endif /* WATCHDOG_ENABLE */