2 * Copyright (c) 2010 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
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
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
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
41 #include <sys/ioccom.h>
42 #include <sys/malloc.h>
43 #include <sys/ctype.h>
44 #include <sys/syslog.h>
46 #include <sys/devfs.h>
47 #include <libprop/proplib.h>
49 MALLOC_DEFINE(M_UDEV, "udev", "udev allocs");
51 /* XXX: use UUIDs for identification; would need help from devfs */
53 static cdev_t udev_dev;
54 static d_open_t udev_dev_open;
55 static d_close_t udev_dev_close;
56 static d_read_t udev_dev_read;
57 static d_poll_t udev_dev_poll;
58 static d_ioctl_t udev_dev_ioctl;
60 static int _udev_dict_set_cstr(prop_dictionary_t, const char *, char *);
61 static int _udev_dict_set_int(prop_dictionary_t, const char *, int64_t);
62 static int _udev_dict_set_uint(prop_dictionary_t, const char *, uint64_t);
63 static int _udev_dict_delete_key(prop_dictionary_t, const char *);
64 static prop_dictionary_t udev_init_dict_event(cdev_t, const char *);
65 static int udev_init_dict(cdev_t);
66 static int udev_destroy_dict(cdev_t);
67 static void udev_event_insert(int, prop_dictionary_t);
68 static struct udev_event_kernel *udev_event_remove(void);
69 static void udev_event_free(struct udev_event_kernel *);
70 static char *udev_event_externalize(struct udev_event_kernel *);
71 static void udev_getdevs_scan_callback(cdev_t, void *);
72 static int udev_getdevs_ioctl(struct plistref *, u_long, prop_dictionary_t);
76 int (*fn)(struct plistref *, u_long, prop_dictionary_t);
79 struct udev_prop_ctx {
84 struct udev_event_kernel {
86 TAILQ_ENTRY(udev_event_kernel) link;
97 TAILQ_HEAD(, udev_event_kernel) ev_queue; /* list of thread_io */
100 static struct dev_ops udev_dev_ops = {
102 .d_open = udev_dev_open,
103 .d_close = udev_dev_close,
104 .d_read = udev_dev_read,
105 .d_poll = udev_dev_poll,
106 .d_ioctl = udev_dev_ioctl
109 struct cmd_function cmd_fn[] = {
110 { .cmd = "getdevs", .fn = udev_getdevs_ioctl},
115 _udev_dict_set_cstr(prop_dictionary_t dict, const char *key, char *str)
119 KKASSERT(dict != NULL);
121 ps = prop_string_create_cstring(str);
125 if (prop_dictionary_set(dict, key, ps) == false) {
126 prop_object_release(ps);
130 prop_object_release(ps);
135 _udev_dict_set_int(prop_dictionary_t dict, const char *key, int64_t val)
139 KKASSERT(dict != NULL);
141 pn = prop_number_create_integer(val);
145 if (prop_dictionary_set(dict, key, pn) == false) {
146 prop_object_release(pn);
150 prop_object_release(pn);
155 _udev_dict_set_uint(prop_dictionary_t dict, const char *key, uint64_t val)
159 KKASSERT(dict != NULL);
161 pn = prop_number_create_unsigned_integer(val);
165 if (prop_dictionary_set(dict, key, pn) == false) {
166 prop_object_release(pn);
170 prop_object_release(pn);
175 _udev_dict_delete_key(prop_dictionary_t dict, const char *key)
177 KKASSERT(dict != NULL);
179 prop_dictionary_remove(dict, key);
185 * Initialize an event dictionary, which contains three parameters to
186 * identify the device referred to (name, devnum, kptr) and the affected key.
188 static prop_dictionary_t
189 udev_init_dict_event(cdev_t dev, const char *key)
191 prop_dictionary_t dict;
195 kptr = (uint64_t)(uintptr_t)dev;
196 KKASSERT(dev != NULL);
198 dict = prop_dictionary_create();
200 log(LOG_DEBUG, "udev_init_dict_event: prop_dictionary_create() failed\n");
204 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
206 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
208 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
210 if ((error = _udev_dict_set_cstr(dict, "key", __DECONST(char *, key))))
214 prop_object_release(dict);
219 udev_dict_set_cstr(cdev_t dev, const char *key, char *str)
221 prop_dictionary_t dict;
224 KKASSERT(dev != NULL);
226 /* Queue a key update event */
227 dict = udev_init_dict_event(dev, key);
230 if ((error = _udev_dict_set_cstr(dict, "value", str))) {
231 prop_object_release(dict);
234 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
235 prop_object_release(dict);
237 return _udev_dict_set_cstr(dev->si_dict, key, str);
241 udev_dict_set_int(cdev_t dev, const char *key, int64_t val)
243 prop_dictionary_t dict;
246 KKASSERT(dev != NULL);
248 /* Queue a key update event */
249 dict = udev_init_dict_event(dev, key);
252 if ((error = _udev_dict_set_int(dict, "value", val))) {
253 prop_object_release(dict);
256 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
257 prop_object_release(dict);
259 return _udev_dict_set_int(dev->si_dict, key, val);
263 udev_dict_set_uint(cdev_t dev, const char *key, uint64_t val)
265 prop_dictionary_t dict;
268 KKASSERT(dev != NULL);
270 /* Queue a key update event */
271 dict = udev_init_dict_event(dev, key);
274 if ((error = _udev_dict_set_uint(dict, "value", val))) {
275 prop_object_release(dict);
278 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
279 prop_object_release(dict);
281 return _udev_dict_set_uint(dev->si_dict, key, val);
285 udev_dict_delete_key(cdev_t dev, const char *key)
287 prop_dictionary_t dict;
289 KKASSERT(dev != NULL);
291 /* Queue a key removal event */
292 dict = udev_init_dict_event(dev, key);
295 udev_event_insert(UDEV_EV_KEY_REMOVE, dict);
296 prop_object_release(dict);
298 return _udev_dict_delete_key(dev->si_dict, key);
302 udev_init_dict(cdev_t dev)
304 prop_dictionary_t dict;
308 kptr = (uint64_t)(uintptr_t)dev;
310 KKASSERT(dev != NULL);
311 dict = prop_dictionary_create();
313 log(LOG_DEBUG, "udev_init_dict: prop_dictionary_create() failed\n");
317 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
319 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
321 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
324 /* XXX: The next 3 are marginallly useful, if at all */
325 if ((error = _udev_dict_set_uint(dict, "uid", dev->si_uid)))
327 if ((error = _udev_dict_set_uint(dict, "gid", dev->si_gid)))
329 if ((error = _udev_dict_set_int(dict, "mode", dev->si_perms)))
332 if ((error = _udev_dict_set_int(dict, "major", umajor(dev->si_inode))))
334 if ((error = _udev_dict_set_int(dict, "minor", dev->si_uminor)))
342 prop_object_release(dict);
347 udev_destroy_dict(cdev_t dev)
349 KKASSERT(dev != NULL);
351 if (dev->si_dict != NULL) {
352 prop_object_release(dev->si_dict);
360 udev_event_insert(int ev_type, prop_dictionary_t dict)
362 struct udev_event_kernel *ev;
364 /* Only start queing events after client has initiated properly */
365 if (!udevctx.initiated)
368 /* XXX: use objcache eventually */
369 ev = kmalloc(sizeof(*ev), M_UDEV, M_WAITOK);
370 ev->ev.ev_dict = prop_dictionary_copy(dict);
371 if (ev->ev.ev_dict == NULL) {
375 ev->ev.ev_type = ev_type;
377 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
378 TAILQ_INSERT_TAIL(&udevctx.ev_queue, ev, link);
380 lockmgr(&udevctx.lock, LK_RELEASE);
383 selwakeup(&udevctx.sel);
386 static struct udev_event_kernel *
387 udev_event_remove(void)
389 struct udev_event_kernel *ev;
391 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
392 if (TAILQ_EMPTY(&udevctx.ev_queue)) {
393 lockmgr(&udevctx.lock, LK_RELEASE);
397 ev = TAILQ_FIRST(&udevctx.ev_queue);
398 TAILQ_REMOVE(&udevctx.ev_queue, ev, link);
400 lockmgr(&udevctx.lock, LK_RELEASE);
406 udev_event_free(struct udev_event_kernel *ev)
408 /* XXX: use objcache eventually */
413 udev_event_externalize(struct udev_event_kernel *ev)
415 prop_dictionary_t dict;
420 dict = prop_dictionary_create();
422 log(LOG_DEBUG, "udev_event_externalize: prop_dictionary_create() failed\n");
426 if ((error = _udev_dict_set_int(dict, "evtype", ev->ev.ev_type))) {
427 prop_object_release(dict);
431 if (prop_dictionary_set(dict, "evdict", ev->ev.ev_dict) == false) {
432 prop_object_release(dict);
436 prop_object_release(ev->ev.ev_dict);
438 xml = prop_dictionary_externalize(dict);
440 prop_object_release(dict);
446 udev_event_attach(cdev_t dev, char *name, int alias)
448 prop_dictionary_t dict;
451 KKASSERT(dev != NULL);
453 error = udev_init_dict(dev);
458 dict = prop_dictionary_copy(dev->si_dict);
462 if ((error = _udev_dict_set_cstr(dict, "name", name))) {
463 prop_object_release(dict);
467 _udev_dict_set_int(dict, "alias", 1);
469 udev_event_insert(UDEV_EVENT_ATTACH, dict);
470 prop_object_release(dict);
472 _udev_dict_set_int(dev->si_dict, "alias", 0);
473 udev_event_insert(UDEV_EVENT_ATTACH, dev->si_dict);
481 udev_event_detach(cdev_t dev, char *name, int alias)
483 prop_dictionary_t dict;
485 KKASSERT(dev != NULL);
488 dict = prop_dictionary_copy(dev->si_dict);
492 if (_udev_dict_set_cstr(dict, "name", name)) {
493 prop_object_release(dict);
497 _udev_dict_set_int(dict, "alias", 1);
499 udev_event_insert(UDEV_EVENT_DETACH, dict);
500 prop_object_release(dict);
502 udev_event_insert(UDEV_EVENT_DETACH, dev->si_dict);
506 udev_destroy_dict(dev);
515 udev_dev_open(struct dev_open_args *ap)
526 udev_dev_close(struct dev_close_args *ap)
529 udevctx.initiated = 0;
536 udev_dev_poll(struct dev_poll_args *ap)
540 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
541 if (ap->a_events & (POLLIN | POLLRDNORM)) {
542 if (!TAILQ_EMPTY(&udevctx.ev_queue))
543 revents = ap->a_events & (POLLIN | POLLRDNORM);
545 selrecord(curthread, &udevctx.sel);
547 lockmgr(&udevctx.lock, LK_RELEASE);
549 ap->a_events = revents;
554 udev_dev_read(struct dev_read_args *ap)
556 struct udev_event_kernel *ev;
557 struct uio *uio = ap->a_uio;
563 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
566 if ((ev = udev_event_remove()) != NULL) {
567 if ((xml = udev_event_externalize(ev)) == NULL) {
568 lockmgr(&udevctx.lock, LK_RELEASE);
572 len = strlen(xml) + 1; /* account for NULL-termination */
573 if (uio->uio_resid < len) {
576 error = uiomove((caddr_t)xml, len, uio);
581 lockmgr(&udevctx.lock, LK_RELEASE);
585 if ((error = lksleep(&udevctx, &udevctx.lock, 0, "udevq", 0))) {
586 lockmgr(&udevctx.lock, LK_RELEASE);
591 lockmgr(&udevctx.lock, LK_RELEASE);
596 udev_dev_ioctl(struct dev_ioctl_args *ap)
598 prop_dictionary_t dict;
601 struct plistref *pref;
608 /* Use proplib(3) for userspace/kernel communication */
609 pref = (struct plistref *)ap->a_data;
610 error = prop_dictionary_copyin_ioctl(pref, ap->a_cmd, &dict);
614 po = prop_dictionary_get(dict, "command");
615 if (po == NULL || prop_object_type(po) != PROP_TYPE_STRING) {
616 log(LOG_DEBUG, "udev: prop_dictionary_get() failed\n");
617 prop_object_release(dict);
623 for(i = 0; cmd_fn[i].cmd != NULL; i++) {
624 if (prop_string_equals_cstring(ps, cmd_fn[i].cmd))
628 if (cmd_fn[i].cmd != NULL) {
629 log(LOG_DEBUG, "udev: ioctl %s called\n", cmd_fn[i].cmd);
630 error = cmd_fn[i].fn(pref, ap->a_cmd, dict);
635 //prop_object_release(po);
637 prop_object_release(dict);
640 error = ENOTTY; /* Inappropriate ioctl for device */
648 udev_getdevs_scan_callback(cdev_t cdev, void *arg)
650 struct udev_prop_ctx *ctx = arg;
652 KKASSERT(arg != NULL);
654 if (cdev->si_dict == NULL)
657 if (prop_array_add(ctx->cdevs, cdev->si_dict) == false) {
664 udev_getdevs_ioctl(struct plistref *pref, u_long cmd, prop_dictionary_t dict)
666 prop_dictionary_t odict;
667 struct udev_prop_ctx ctx;
671 ctx.cdevs = prop_array_create();
672 if (ctx.cdevs == NULL) {
673 log(LOG_DEBUG, "udev_getdevs_ioctl: prop_array_create() failed\n");
677 /* XXX: need devfs_scan_alias_callback() */
678 devfs_scan_callback(udev_getdevs_scan_callback, &ctx);
680 if (ctx.error != 0) {
681 prop_object_release(ctx.cdevs);
684 udevctx.initiated = 1;
686 odict = prop_dictionary_create();
691 if ((prop_dictionary_set(odict, "array", ctx.cdevs)) == 0) {
692 log(LOG_DEBUG, "udev_getdevs_ioctl: prop_dictionary_set failed\n");
693 prop_object_release(odict);
697 error = prop_dictionary_copyout_ioctl(pref, cmd, odict);
699 /* XXX: need to release ctx.cdevs? */
700 prop_object_release(odict);
711 lockinit(&udevctx.lock, "udevevq", 0, LK_CANRECURSE);
712 TAILQ_INIT(&udevctx.ev_queue);
723 udev_dev = make_dev(&udev_dev_ops,
732 udev_dev_uninit(void)
734 destroy_dev(udev_dev);
737 SYSINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY, udev_init, NULL);
738 SYSUNINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY, udev_uninit, NULL);
739 SYSINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY, udev_dev_init, NULL);
740 SYSUNINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY, udev_dev_uninit, NULL);