sysmon.c revision 1.28.8.1 1 1.28.8.1 pgoyette /* $NetBSD: sysmon.c,v 1.28.8.1 2017/04/27 05:36:36 pgoyette Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 2000 Zembu Labs, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Author: Jason R. Thorpe <thorpej (at) zembu.com>
8 1.1 thorpej *
9 1.1 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1 thorpej * modification, are permitted provided that the following conditions
11 1.1 thorpej * are met:
12 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer.
14 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
16 1.1 thorpej * documentation and/or other materials provided with the distribution.
17 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
18 1.1 thorpej * must display the following acknowledgement:
19 1.1 thorpej * This product includes software developed by Zembu Labs, Inc.
20 1.1 thorpej * 4. Neither the name of Zembu Labs nor the names of its employees may
21 1.1 thorpej * be used to endorse or promote products derived from this software
22 1.1 thorpej * without specific prior written permission.
23 1.1 thorpej *
24 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
25 1.1 thorpej * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
26 1.1 thorpej * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
27 1.1 thorpej * CLAIMED. IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 1.1 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 1.1 thorpej * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 1.1 thorpej * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 1.1 thorpej * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 1.1 thorpej * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 1.1 thorpej * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1 thorpej */
35 1.1 thorpej
36 1.1 thorpej /*
37 1.4 thorpej * Clearing house for system monitoring hardware. We currently
38 1.8 thorpej * handle environmental sensors, watchdog timers, and power management.
39 1.1 thorpej */
40 1.5 lukem
41 1.5 lukem #include <sys/cdefs.h>
42 1.28.8.1 pgoyette __KERNEL_RCSID(0, "$NetBSD: sysmon.c,v 1.28.8.1 2017/04/27 05:36:36 pgoyette Exp $");
43 1.1 thorpej
44 1.1 thorpej #include <sys/param.h>
45 1.1 thorpej #include <sys/conf.h>
46 1.1 thorpej #include <sys/errno.h>
47 1.1 thorpej #include <sys/fcntl.h>
48 1.3 thorpej #include <sys/callout.h>
49 1.3 thorpej #include <sys/kernel.h>
50 1.3 thorpej #include <sys/systm.h>
51 1.3 thorpej #include <sys/proc.h>
52 1.20 pgoyette #include <sys/module.h>
53 1.20 pgoyette #include <sys/mutex.h>
54 1.20 pgoyette #include <sys/device.h>
55 1.25 pgoyette #include <sys/once.h>
56 1.28.8.1 pgoyette #include <sys/localcount.h>
57 1.1 thorpej
58 1.1 thorpej #include <dev/sysmon/sysmonvar.h>
59 1.1 thorpej
60 1.6 gehenna dev_type_open(sysmonopen);
61 1.6 gehenna dev_type_close(sysmonclose);
62 1.6 gehenna dev_type_ioctl(sysmonioctl);
63 1.8 thorpej dev_type_read(sysmonread);
64 1.8 thorpej dev_type_poll(sysmonpoll);
65 1.8 thorpej dev_type_kqfilter(sysmonkqfilter);
66 1.6 gehenna
67 1.6 gehenna const struct cdevsw sysmon_cdevsw = {
68 1.28.8.1 pgoyette DEVSW_MODULE_INIT
69 1.18 dholland .d_open = sysmonopen,
70 1.18 dholland .d_close = sysmonclose,
71 1.18 dholland .d_read = sysmonread,
72 1.18 dholland .d_write = nowrite,
73 1.18 dholland .d_ioctl = sysmonioctl,
74 1.18 dholland .d_stop = nostop,
75 1.18 dholland .d_tty = notty,
76 1.18 dholland .d_poll = sysmonpoll,
77 1.18 dholland .d_mmap = nommap,
78 1.18 dholland .d_kqfilter = sysmonkqfilter,
79 1.19 dholland .d_discard = nodiscard,
80 1.18 dholland .d_flag = D_OTHER | D_MPSAFE
81 1.6 gehenna };
82 1.1 thorpej
83 1.20 pgoyette static int sysmon_modcmd(modcmd_t, void *);
84 1.25 pgoyette static int sm_init_once(void);
85 1.20 pgoyette
86 1.20 pgoyette /*
87 1.20 pgoyette * Info about our minor "devices"
88 1.20 pgoyette */
89 1.20 pgoyette static struct sysmon_opvec *sysmon_opvec_table[] = { NULL, NULL, NULL };
90 1.20 pgoyette static int sysmon_refcnt[] = { 0, 0, 0 };
91 1.20 pgoyette static const char *sysmon_mod[] = { "sysmon_envsys",
92 1.20 pgoyette "sysmon_wdog",
93 1.20 pgoyette "sysmon_power" };
94 1.25 pgoyette static kmutex_t sysmon_minor_mtx;
95 1.20 pgoyette
96 1.25 pgoyette #ifdef _MODULE
97 1.25 pgoyette static bool sm_is_attached;
98 1.25 pgoyette #endif
99 1.20 pgoyette
100 1.25 pgoyette ONCE_DECL(once_sm);
101 1.20 pgoyette
102 1.20 pgoyette /*
103 1.20 pgoyette * sysmon_attach_minor
104 1.20 pgoyette *
105 1.20 pgoyette * Attach a minor device for wdog, power, or envsys. Manage a
106 1.20 pgoyette * reference count so we can prevent the device from being
107 1.20 pgoyette * detached if there are still users with the minor device opened.
108 1.20 pgoyette *
109 1.20 pgoyette * If the opvec argument is NULL, this is a request to detach the
110 1.20 pgoyette * minor device - make sure the refcnt is zero!
111 1.20 pgoyette */
112 1.20 pgoyette int
113 1.20 pgoyette sysmon_attach_minor(int minor, struct sysmon_opvec *opvec)
114 1.20 pgoyette {
115 1.20 pgoyette int ret;
116 1.20 pgoyette
117 1.25 pgoyette mutex_enter(&sysmon_minor_mtx);
118 1.20 pgoyette if (opvec) {
119 1.20 pgoyette if (sysmon_opvec_table[minor] == NULL) {
120 1.20 pgoyette sysmon_refcnt[minor] = 0;
121 1.20 pgoyette sysmon_opvec_table[minor] = opvec;
122 1.20 pgoyette ret = 0;
123 1.20 pgoyette } else
124 1.20 pgoyette ret = EEXIST;
125 1.20 pgoyette } else {
126 1.20 pgoyette if (sysmon_refcnt[minor] == 0) {
127 1.20 pgoyette sysmon_opvec_table[minor] = NULL;
128 1.20 pgoyette ret = 0;
129 1.20 pgoyette } else
130 1.20 pgoyette ret = EBUSY;
131 1.20 pgoyette }
132 1.20 pgoyette
133 1.25 pgoyette mutex_exit(&sysmon_minor_mtx);
134 1.20 pgoyette return ret;
135 1.20 pgoyette }
136 1.20 pgoyette
137 1.1 thorpej /*
138 1.1 thorpej * sysmonopen:
139 1.1 thorpej *
140 1.1 thorpej * Open the system monitor device.
141 1.1 thorpej */
142 1.1 thorpej int
143 1.11 christos sysmonopen(dev_t dev, int flag, int mode, struct lwp *l)
144 1.1 thorpej {
145 1.4 thorpej int error;
146 1.3 thorpej
147 1.25 pgoyette mutex_enter(&sysmon_minor_mtx);
148 1.20 pgoyette
149 1.3 thorpej switch (minor(dev)) {
150 1.3 thorpej case SYSMON_MINOR_ENVSYS:
151 1.3 thorpej case SYSMON_MINOR_WDOG:
152 1.8 thorpej case SYSMON_MINOR_POWER:
153 1.20 pgoyette if (sysmon_opvec_table[minor(dev)] == NULL) {
154 1.25 pgoyette mutex_exit(&sysmon_minor_mtx);
155 1.20 pgoyette error = module_autoload(sysmon_mod[minor(dev)],
156 1.20 pgoyette MODULE_CLASS_MISC);
157 1.28 pgoyette if (error)
158 1.28 pgoyette return error;
159 1.25 pgoyette mutex_enter(&sysmon_minor_mtx);
160 1.26 pgoyette if (sysmon_opvec_table[minor(dev)] == NULL) {
161 1.20 pgoyette error = ENODEV;
162 1.26 pgoyette break;
163 1.26 pgoyette }
164 1.20 pgoyette }
165 1.20 pgoyette error = (sysmon_opvec_table[minor(dev)]->so_open)(dev, flag,
166 1.20 pgoyette mode, l);
167 1.20 pgoyette if (error == 0)
168 1.20 pgoyette sysmon_refcnt[minor(dev)]++;
169 1.8 thorpej break;
170 1.3 thorpej default:
171 1.3 thorpej error = ENODEV;
172 1.3 thorpej }
173 1.1 thorpej
174 1.25 pgoyette mutex_exit(&sysmon_minor_mtx);
175 1.28 pgoyette return error;
176 1.1 thorpej }
177 1.1 thorpej
178 1.1 thorpej /*
179 1.1 thorpej * sysmonclose:
180 1.1 thorpej *
181 1.1 thorpej * Close the system monitor device.
182 1.1 thorpej */
183 1.1 thorpej int
184 1.11 christos sysmonclose(dev_t dev, int flag, int mode, struct lwp *l)
185 1.1 thorpej {
186 1.4 thorpej int error;
187 1.3 thorpej
188 1.3 thorpej switch (minor(dev)) {
189 1.3 thorpej case SYSMON_MINOR_ENVSYS:
190 1.3 thorpej case SYSMON_MINOR_WDOG:
191 1.8 thorpej case SYSMON_MINOR_POWER:
192 1.20 pgoyette if (sysmon_opvec_table[minor(dev)] == NULL)
193 1.20 pgoyette error = ENODEV;
194 1.20 pgoyette else {
195 1.20 pgoyette error = (sysmon_opvec_table[minor(dev)]->so_close)(dev,
196 1.20 pgoyette flag, mode, l);
197 1.20 pgoyette if (error == 0) {
198 1.20 pgoyette sysmon_refcnt[minor(dev)]--;
199 1.20 pgoyette KASSERT(sysmon_refcnt[minor(dev)] >= 0);
200 1.20 pgoyette }
201 1.20 pgoyette }
202 1.8 thorpej break;
203 1.3 thorpej default:
204 1.3 thorpej error = ENODEV;
205 1.3 thorpej }
206 1.1 thorpej
207 1.3 thorpej return (error);
208 1.1 thorpej }
209 1.1 thorpej
210 1.1 thorpej /*
211 1.1 thorpej * sysmonioctl:
212 1.1 thorpej *
213 1.1 thorpej * Perform a control request.
214 1.1 thorpej */
215 1.1 thorpej int
216 1.15 christos sysmonioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
217 1.1 thorpej {
218 1.3 thorpej int error;
219 1.3 thorpej
220 1.3 thorpej switch (minor(dev)) {
221 1.3 thorpej case SYSMON_MINOR_ENVSYS:
222 1.3 thorpej case SYSMON_MINOR_WDOG:
223 1.8 thorpej case SYSMON_MINOR_POWER:
224 1.20 pgoyette if (sysmon_opvec_table[minor(dev)] == NULL)
225 1.20 pgoyette error = ENODEV;
226 1.20 pgoyette else
227 1.20 pgoyette error = (sysmon_opvec_table[minor(dev)]->so_ioctl)(dev,
228 1.20 pgoyette cmd, data, flag, l);
229 1.8 thorpej break;
230 1.3 thorpej default:
231 1.3 thorpej error = ENODEV;
232 1.8 thorpej }
233 1.8 thorpej
234 1.8 thorpej return (error);
235 1.8 thorpej }
236 1.8 thorpej
237 1.8 thorpej /*
238 1.8 thorpej * sysmonread:
239 1.8 thorpej *
240 1.8 thorpej * Perform a read request.
241 1.8 thorpej */
242 1.8 thorpej int
243 1.14 christos sysmonread(dev_t dev, struct uio *uio, int flags)
244 1.8 thorpej {
245 1.8 thorpej int error;
246 1.8 thorpej
247 1.8 thorpej switch (minor(dev)) {
248 1.8 thorpej case SYSMON_MINOR_POWER:
249 1.20 pgoyette if (sysmon_opvec_table[minor(dev)] == NULL)
250 1.20 pgoyette error = ENODEV;
251 1.20 pgoyette else
252 1.20 pgoyette error = (sysmon_opvec_table[minor(dev)]->so_read)(dev,
253 1.20 pgoyette uio, flags);
254 1.8 thorpej break;
255 1.8 thorpej default:
256 1.8 thorpej error = ENODEV;
257 1.8 thorpej }
258 1.8 thorpej
259 1.8 thorpej return (error);
260 1.8 thorpej }
261 1.8 thorpej
262 1.8 thorpej /*
263 1.8 thorpej * sysmonpoll:
264 1.8 thorpej *
265 1.8 thorpej * Poll the system monitor device.
266 1.8 thorpej */
267 1.8 thorpej int
268 1.14 christos sysmonpoll(dev_t dev, int events, struct lwp *l)
269 1.8 thorpej {
270 1.8 thorpej int rv;
271 1.8 thorpej
272 1.8 thorpej switch (minor(dev)) {
273 1.8 thorpej case SYSMON_MINOR_POWER:
274 1.20 pgoyette if (sysmon_opvec_table[minor(dev)] == NULL)
275 1.20 pgoyette rv = events;
276 1.20 pgoyette else
277 1.20 pgoyette rv = (sysmon_opvec_table[minor(dev)]->so_poll)(dev,
278 1.20 pgoyette events, l);
279 1.8 thorpej break;
280 1.8 thorpej default:
281 1.8 thorpej rv = events;
282 1.8 thorpej }
283 1.8 thorpej
284 1.8 thorpej return (rv);
285 1.8 thorpej }
286 1.8 thorpej
287 1.8 thorpej /*
288 1.8 thorpej * sysmonkqfilter:
289 1.8 thorpej *
290 1.8 thorpej * Kqueue filter for the system monitor device.
291 1.8 thorpej */
292 1.8 thorpej int
293 1.14 christos sysmonkqfilter(dev_t dev, struct knote *kn)
294 1.8 thorpej {
295 1.8 thorpej int error;
296 1.8 thorpej
297 1.8 thorpej switch (minor(dev)) {
298 1.8 thorpej case SYSMON_MINOR_POWER:
299 1.20 pgoyette if (sysmon_opvec_table[minor(dev)] == NULL)
300 1.20 pgoyette error = ENODEV;
301 1.20 pgoyette else
302 1.20 pgoyette error = (sysmon_opvec_table[minor(dev)]->so_filter)(dev,
303 1.20 pgoyette kn);
304 1.8 thorpej break;
305 1.8 thorpej default:
306 1.8 thorpej error = 1;
307 1.3 thorpej }
308 1.3 thorpej
309 1.3 thorpej return (error);
310 1.1 thorpej }
311 1.20 pgoyette
312 1.20 pgoyette MODULE(MODULE_CLASS_DRIVER, sysmon, "");
313 1.20 pgoyette
314 1.25 pgoyette static int
315 1.25 pgoyette sm_init_once(void)
316 1.25 pgoyette {
317 1.25 pgoyette
318 1.25 pgoyette mutex_init(&sysmon_minor_mtx, MUTEX_DEFAULT, IPL_NONE);
319 1.25 pgoyette
320 1.25 pgoyette return 0;
321 1.25 pgoyette }
322 1.25 pgoyette
323 1.20 pgoyette int
324 1.20 pgoyette sysmon_init(void)
325 1.20 pgoyette {
326 1.25 pgoyette int error;
327 1.21 pgoyette #ifdef _MODULE
328 1.20 pgoyette devmajor_t bmajor, cmajor;
329 1.21 pgoyette #endif
330 1.20 pgoyette
331 1.25 pgoyette error = RUN_ONCE(&once_sm, sm_init_once);
332 1.20 pgoyette
333 1.21 pgoyette #ifdef _MODULE
334 1.25 pgoyette mutex_enter(&sysmon_minor_mtx);
335 1.25 pgoyette if (!sm_is_attached) {
336 1.25 pgoyette bmajor = cmajor = -1;
337 1.25 pgoyette error = devsw_attach("sysmon", NULL, &bmajor,
338 1.25 pgoyette &sysmon_cdevsw, &cmajor);
339 1.25 pgoyette sm_is_attached = (error != 0);
340 1.20 pgoyette }
341 1.25 pgoyette mutex_exit(&sysmon_minor_mtx);
342 1.21 pgoyette #endif
343 1.20 pgoyette
344 1.20 pgoyette return error;
345 1.20 pgoyette }
346 1.20 pgoyette
347 1.20 pgoyette int
348 1.20 pgoyette sysmon_fini(void)
349 1.20 pgoyette {
350 1.20 pgoyette int error = 0;
351 1.20 pgoyette
352 1.25 pgoyette if ((sysmon_opvec_table[SYSMON_MINOR_ENVSYS] != NULL) ||
353 1.25 pgoyette (sysmon_opvec_table[SYSMON_MINOR_WDOG] != NULL) ||
354 1.25 pgoyette (sysmon_opvec_table[SYSMON_MINOR_POWER] != NULL))
355 1.20 pgoyette error = EBUSY;
356 1.20 pgoyette
357 1.25 pgoyette #ifdef _MODULE
358 1.25 pgoyette if (error == 0) {
359 1.25 pgoyette mutex_enter(&sysmon_minor_mtx);
360 1.25 pgoyette sm_is_attached = false;
361 1.25 pgoyette error = devsw_detach(NULL, &sysmon_cdevsw);
362 1.25 pgoyette mutex_exit(&sysmon_minor_mtx);
363 1.20 pgoyette }
364 1.25 pgoyette #endif
365 1.20 pgoyette
366 1.20 pgoyette return error;
367 1.20 pgoyette }
368 1.20 pgoyette
369 1.20 pgoyette static
370 1.20 pgoyette int
371 1.20 pgoyette sysmon_modcmd(modcmd_t cmd, void *arg)
372 1.20 pgoyette {
373 1.20 pgoyette int ret;
374 1.20 pgoyette
375 1.20 pgoyette switch (cmd) {
376 1.20 pgoyette case MODULE_CMD_INIT:
377 1.20 pgoyette ret = sysmon_init();
378 1.20 pgoyette break;
379 1.20 pgoyette
380 1.20 pgoyette case MODULE_CMD_FINI:
381 1.20 pgoyette ret = sysmon_fini();
382 1.20 pgoyette break;
383 1.20 pgoyette
384 1.20 pgoyette case MODULE_CMD_STAT:
385 1.20 pgoyette default:
386 1.20 pgoyette ret = ENOTTY;
387 1.20 pgoyette }
388 1.20 pgoyette
389 1.20 pgoyette return ret;
390 1.20 pgoyette }
391