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>
40 #include <sys/event.h>
41 #include <sys/malloc.h>
42 #include <sys/ctype.h>
43 #include <sys/syslog.h>
45 #include <sys/devfs.h>
46 #include <libprop/proplib.h>
48 #include <sys/thread2.h>
50 MALLOC_DEFINE(M_UDEV, "udev", "udev allocs");
52 /* XXX: use UUIDs for identification; would need help from devfs */
54 static cdev_t udev_dev;
55 static d_open_t udev_dev_open;
56 static d_close_t udev_dev_close;
57 static d_read_t udev_dev_read;
58 static d_kqfilter_t udev_dev_kqfilter;
59 static d_ioctl_t udev_dev_ioctl;
60 static d_clone_t udev_dev_clone;
62 struct udev_prop_ctx {
67 struct udev_event_kernel {
69 TAILQ_ENTRY(udev_event_kernel) link;
73 TAILQ_ENTRY(udev_softc) entry;
79 struct udev_event_kernel marker; /* udev_evq marker */
84 int (*fn)(struct udev_softc *, struct plistref *,
85 u_long, prop_dictionary_t);
89 static int _udev_dict_set_cstr(prop_dictionary_t, const char *, char *);
90 static int _udev_dict_set_int(prop_dictionary_t, const char *, int64_t);
91 static int _udev_dict_set_uint(prop_dictionary_t, const char *, uint64_t);
92 static int _udev_dict_delete_key(prop_dictionary_t, const char *);
93 static prop_dictionary_t udev_init_dict_event(cdev_t, const char *);
94 static int udev_init_dict(cdev_t);
95 static int udev_destroy_dict(cdev_t);
96 static void udev_event_insert(int, prop_dictionary_t);
97 static void udev_clean_events_locked(void);
98 static char *udev_event_externalize(struct udev_event_kernel *);
99 static void udev_getdevs_scan_callback(char *, cdev_t, bool, void *);
100 static int udev_getdevs_ioctl(struct udev_softc *, struct plistref *,
101 u_long, prop_dictionary_t);
102 static void udev_dev_filter_detach(struct knote *);
103 static int udev_dev_filter_read(struct knote *, long);
105 static struct dev_ops udev_dev_ops = {
107 .d_open = udev_dev_open,
108 .d_close = udev_dev_close,
109 .d_read = udev_dev_read,
110 .d_kqfilter = udev_dev_kqfilter,
111 .d_ioctl = udev_dev_ioctl
114 static struct cmd_function cmd_fn[] = {
115 { .cmd = "getdevs", .fn = udev_getdevs_ioctl},
119 DEVFS_DECLARE_CLONE_BITMAP(udev);
121 static TAILQ_HEAD(, udev_softc) udevq;
122 static TAILQ_HEAD(, udev_event_kernel) udev_evq;
123 static struct kqinfo udev_kq;
124 static struct lock udev_lk;
125 static int udev_evqlen;
126 static int udev_initiated_count;
129 _udev_dict_set_cstr(prop_dictionary_t dict, const char *key, char *str)
133 KKASSERT(dict != NULL);
135 ps = prop_string_create_cstring(str);
140 if (prop_dictionary_set(dict, key, ps) == false) {
141 prop_object_release(ps);
145 prop_object_release(ps);
150 _udev_dict_set_int(prop_dictionary_t dict, const char *key, int64_t val)
154 KKASSERT(dict != NULL);
156 pn = prop_number_create_integer(val);
160 if (prop_dictionary_set(dict, key, pn) == false) {
161 prop_object_release(pn);
165 prop_object_release(pn);
170 _udev_dict_set_uint(prop_dictionary_t dict, const char *key, uint64_t val)
174 KKASSERT(dict != NULL);
176 pn = prop_number_create_unsigned_integer(val);
180 if (prop_dictionary_set(dict, key, pn) == false) {
181 prop_object_release(pn);
185 prop_object_release(pn);
190 _udev_dict_delete_key(prop_dictionary_t dict, const char *key)
192 KKASSERT(dict != NULL);
194 prop_dictionary_remove(dict, key);
200 * Initialize an event dictionary, which contains three parameters to
201 * identify the device referred to (name, devnum, kptr) and the affected key.
203 static prop_dictionary_t
204 udev_init_dict_event(cdev_t dev, const char *key)
206 prop_dictionary_t dict;
210 kptr = (uint64_t)(uintptr_t)dev;
211 KKASSERT(dev != NULL);
213 dict = prop_dictionary_create();
215 log(LOG_DEBUG, "udev_init_dict_event: prop_dictionary_create() failed\n");
219 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
221 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
223 if ((error = _udev_dict_set_uint(dict, "devtype", (dev_dflags(dev) & D_TYPEMASK))))
225 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
227 if ((error = _udev_dict_set_cstr(dict, "key", __DECONST(char *, key))))
233 prop_object_release(dict);
238 udev_dict_set_cstr(cdev_t dev, const char *key, char *str)
240 prop_dictionary_t dict;
243 KKASSERT(dev != NULL);
245 if (dev->si_dict == NULL) {
246 error = udev_init_dict(dev);
251 /* Queue a key update event */
252 dict = udev_init_dict_event(dev, key);
256 if ((error = _udev_dict_set_cstr(dict, "value", str))) {
257 prop_object_release(dict);
260 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
261 prop_object_release(dict);
263 error = _udev_dict_set_cstr(dev->si_dict, key, str);
268 udev_dict_set_int(cdev_t dev, const char *key, int64_t val)
270 prop_dictionary_t dict;
273 KKASSERT(dev != NULL);
275 if (dev->si_dict == NULL) {
276 error = udev_init_dict(dev);
281 /* Queue a key update event */
282 dict = udev_init_dict_event(dev, key);
285 if ((error = _udev_dict_set_int(dict, "value", val))) {
286 prop_object_release(dict);
289 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
290 prop_object_release(dict);
292 return _udev_dict_set_int(dev->si_dict, key, val);
296 udev_dict_set_uint(cdev_t dev, const char *key, uint64_t val)
298 prop_dictionary_t dict;
301 KKASSERT(dev != NULL);
303 if (dev->si_dict == NULL) {
304 error = udev_init_dict(dev);
309 /* Queue a key update event */
310 dict = udev_init_dict_event(dev, key);
313 if ((error = _udev_dict_set_uint(dict, "value", val))) {
314 prop_object_release(dict);
317 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
318 prop_object_release(dict);
320 return _udev_dict_set_uint(dev->si_dict, key, val);
324 udev_dict_delete_key(cdev_t dev, const char *key)
326 prop_dictionary_t dict;
328 KKASSERT(dev != NULL);
330 /* Queue a key removal event */
331 dict = udev_init_dict_event(dev, key);
334 udev_event_insert(UDEV_EV_KEY_REMOVE, dict);
335 prop_object_release(dict);
337 return _udev_dict_delete_key(dev->si_dict, key);
341 udev_init_dict(cdev_t dev)
343 prop_dictionary_t dict;
347 kptr = (uint64_t)(uintptr_t)dev;
349 KKASSERT(dev != NULL);
351 if (dev->si_dict != NULL) {
354 "udev_init_dict: new dict for %s, but has dict already (%p)!\n",
355 dev->si_name, dev->si_dict);
360 dict = prop_dictionary_create();
362 log(LOG_DEBUG, "udev_init_dict: prop_dictionary_create() failed\n");
366 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
368 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
370 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
372 if ((error = _udev_dict_set_uint(dict, "devtype", (dev_dflags(dev) & D_TYPEMASK))))
375 /* XXX: The next 3 are marginallly useful, if at all */
376 if ((error = _udev_dict_set_uint(dict, "uid", dev->si_uid)))
378 if ((error = _udev_dict_set_uint(dict, "gid", dev->si_gid)))
380 if ((error = _udev_dict_set_int(dict, "mode", dev->si_perms)))
383 if ((error = _udev_dict_set_int(dict, "major", dev->si_umajor)))
385 if ((error = _udev_dict_set_int(dict, "minor", dev->si_uminor)))
387 if (dev->si_ops->head.name != NULL) {
388 if ((error = _udev_dict_set_cstr(dict, "driver",
389 __DECONST(char *, dev->si_ops->head.name))))
398 prop_object_release(dict);
403 udev_destroy_dict(cdev_t dev)
405 KKASSERT(dev != NULL);
407 if (dev->si_dict != NULL) {
408 prop_object_release(dev->si_dict);
416 udev_event_insert(int ev_type, prop_dictionary_t dict)
418 struct udev_event_kernel *ev;
420 /* Only start queing events after client has initiated properly */
421 if (udev_initiated_count == 0)
424 /* XXX: use objcache eventually */
425 ev = kmalloc(sizeof(*ev), M_UDEV, M_WAITOK);
426 ev->ev.ev_dict = prop_dictionary_copy(dict);
427 if (ev->ev.ev_dict == NULL) {
431 ev->ev.ev_type = ev_type;
433 lockmgr(&udev_lk, LK_EXCLUSIVE);
434 TAILQ_INSERT_TAIL(&udev_evq, ev, link);
436 lockmgr(&udev_lk, LK_RELEASE);
439 KNOTE(&udev_kq.ki_note, 0);
443 udev_clean_events_locked(void)
445 struct udev_event_kernel *ev;
447 while ((ev = TAILQ_FIRST(&udev_evq)) &&
448 ev->ev.ev_dict != NULL) {
449 TAILQ_REMOVE(&udev_evq, ev, link);
456 udev_event_externalize(struct udev_event_kernel *ev)
458 prop_dictionary_t dict;
463 dict = prop_dictionary_create();
466 "udev_event_externalize: prop_dictionary_create() failed\n");
470 if ((error = _udev_dict_set_int(dict, "evtype", ev->ev.ev_type))) {
471 prop_object_release(dict);
475 if (prop_dictionary_set(dict, "evdict", ev->ev.ev_dict) == false) {
476 prop_object_release(dict);
480 prop_object_release(ev->ev.ev_dict);
482 xml = prop_dictionary_externalize(dict);
484 prop_object_release(dict);
490 udev_event_attach(cdev_t dev, char *name, int alias)
492 prop_dictionary_t dict;
495 KKASSERT(dev != NULL);
500 dict = prop_dictionary_copy(dev->si_dict);
504 if ((error = _udev_dict_set_cstr(dict, "name", name))) {
505 prop_object_release(dict);
509 _udev_dict_set_int(dict, "alias", 1);
511 udev_event_insert(UDEV_EVENT_ATTACH, dict);
512 prop_object_release(dict);
514 error = udev_init_dict(dev);
518 _udev_dict_set_int(dev->si_dict, "alias", 0);
519 udev_event_insert(UDEV_EVENT_ATTACH, dev->si_dict);
527 udev_event_detach(cdev_t dev, char *name, int alias)
529 prop_dictionary_t dict;
531 KKASSERT(dev != NULL);
534 dict = prop_dictionary_copy(dev->si_dict);
538 if (_udev_dict_set_cstr(dict, "name", name)) {
539 prop_object_release(dict);
543 _udev_dict_set_int(dict, "alias", 1);
545 udev_event_insert(UDEV_EVENT_DETACH, dict);
546 prop_object_release(dict);
548 udev_event_insert(UDEV_EVENT_DETACH, dev->si_dict);
552 udev_destroy_dict(dev);
558 * Allow multiple opens. Each opener gets a different device.
559 * Messages are replicated to all devices using a marker system.
562 udev_dev_clone(struct dev_clone_args *ap)
564 struct udev_softc *softc;
567 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(udev), 1000);
573 softc = kmalloc(sizeof(*softc), M_UDEV, M_WAITOK | M_ZERO);
575 lockmgr(&udev_lk, LK_EXCLUSIVE);
576 TAILQ_INSERT_TAIL(&udevq, softc, entry);
577 lockmgr(&udev_lk, LK_RELEASE);
579 softc->dev = make_only_dev(&udev_dev_ops, unit, ap->a_cred->cr_ruid,
580 0, 0600, "udev/%d", unit);
581 softc->dev->si_drv1 = softc;
582 ap->a_dev = softc->dev;
590 udev_dev_open(struct dev_open_args *ap)
592 struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
594 lockmgr(&udev_lk, LK_EXCLUSIVE);
595 if (softc == NULL || softc->opened) {
596 lockmgr(&udev_lk, LK_RELEASE);
600 lockmgr(&udev_lk, LK_RELEASE);
606 udev_dev_close(struct dev_close_args *ap)
608 struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
610 KKASSERT(softc->dev == ap->a_head.a_dev);
611 KKASSERT(softc->opened == 1);
613 lockmgr(&udev_lk, LK_EXCLUSIVE);
614 TAILQ_REMOVE(&udevq, softc, entry);
615 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(udev), softc->unit);
617 if (softc->initiated) {
618 TAILQ_REMOVE(&udev_evq, &softc->marker, link);
619 softc->initiated = 0;
620 --udev_initiated_count;
621 udev_clean_events_locked();
625 ap->a_head.a_dev->si_drv1 = NULL;
626 lockmgr(&udev_lk, LK_RELEASE);
628 destroy_dev(ap->a_head.a_dev);
631 kfree(softc, M_UDEV);
636 static struct filterops udev_dev_read_filtops =
637 { FILTEROP_ISFD, NULL, udev_dev_filter_detach, udev_dev_filter_read };
640 udev_dev_kqfilter(struct dev_kqfilter_args *ap)
642 struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
643 struct knote *kn = ap->a_kn;
647 lockmgr(&udev_lk, LK_EXCLUSIVE);
649 switch (kn->kn_filter) {
651 kn->kn_fop = &udev_dev_read_filtops;
652 kn->kn_hook = (caddr_t)softc;
655 ap->a_result = EOPNOTSUPP;
656 lockmgr(&udev_lk, LK_RELEASE);
660 klist = &udev_kq.ki_note;
661 knote_insert(klist, kn);
663 lockmgr(&udev_lk, LK_RELEASE);
669 udev_dev_filter_detach(struct knote *kn)
673 lockmgr(&udev_lk, LK_EXCLUSIVE);
674 klist = &udev_kq.ki_note;
675 knote_remove(klist, kn);
676 lockmgr(&udev_lk, LK_RELEASE);
680 udev_dev_filter_read(struct knote *kn, long hint)
682 struct udev_softc *softc = (void *)kn->kn_hook;
683 struct udev_event_kernel *ev;
686 lockmgr(&udev_lk, LK_EXCLUSIVE);
687 if (softc->initiated) {
688 ev = TAILQ_NEXT(&softc->marker, link);
689 while (ev && ev->ev.ev_dict == NULL)
690 ev = TAILQ_NEXT(ev, link);
694 lockmgr(&udev_lk, LK_RELEASE);
700 udev_dev_read(struct dev_read_args *ap)
702 struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
703 struct udev_event_kernel *ev;
704 struct uio *uio = ap->a_uio;
709 lockmgr(&udev_lk, LK_EXCLUSIVE);
713 if (softc->initiated) {
714 ev = TAILQ_NEXT(&softc->marker, link);
715 while (ev && ev->ev.ev_dict == NULL)
716 ev = TAILQ_NEXT(ev, link);
718 if ((xml = udev_event_externalize(ev)) == NULL) {
722 len = strlen(xml) + 1; /* include terminator */
723 if (uio->uio_resid < len)
726 error = uiomove((caddr_t)xml, len, uio);
732 TAILQ_REMOVE(&udev_evq, &softc->marker, link);
733 TAILQ_INSERT_AFTER(&udev_evq,
734 ev, &softc->marker, link);
735 udev_clean_events_locked();
739 if ((error = lksleep(&udev_evq, &udev_lk, PCATCH, "udevq", 0)))
743 lockmgr(&udev_lk, LK_RELEASE);
748 udev_dev_ioctl(struct dev_ioctl_args *ap)
750 struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
751 prop_dictionary_t dict;
754 struct plistref *pref;
761 /* Use proplib(3) for userspace/kernel communication */
762 pref = (struct plistref *)ap->a_data;
763 error = prop_dictionary_copyin_ioctl(pref, ap->a_cmd, &dict);
767 po = prop_dictionary_get(dict, "command");
768 if (po == NULL || prop_object_type(po) != PROP_TYPE_STRING) {
769 log(LOG_DEBUG, "udev: prop_dictionary_get() failed\n");
770 prop_object_release(dict);
776 for(i = 0; cmd_fn[i].cmd != NULL; i++) {
777 if (prop_string_equals_cstring(ps, cmd_fn[i].cmd))
781 if (cmd_fn[i].cmd != NULL) {
782 error = cmd_fn[i].fn(softc, pref, ap->a_cmd, dict);
787 //prop_object_release(po);
788 prop_object_release(dict);
791 error = ENOTTY; /* Inappropriate ioctl for device */
799 udev_getdevs_scan_callback(char *name, cdev_t cdev, bool is_alias, void *arg)
801 struct udev_prop_ctx *ctx = arg;
803 KKASSERT(arg != NULL);
805 if (cdev->si_dict == NULL)
808 if (prop_array_add(ctx->cdevs, cdev->si_dict) == false) {
815 udev_getdevs_ioctl(struct udev_softc *softc, struct plistref *pref,
816 u_long cmd, prop_dictionary_t dict)
818 prop_dictionary_t odict;
819 struct udev_prop_ctx ctx;
823 ctx.cdevs = prop_array_create();
824 if (ctx.cdevs == NULL) {
826 "udev_getdevs_ioctl: prop_array_create() failed\n");
830 devfs_scan_callback(udev_getdevs_scan_callback, &ctx);
832 if (ctx.error != 0) {
833 prop_object_release(ctx.cdevs);
836 lockmgr(&udev_lk, LK_EXCLUSIVE);
837 if (softc->initiated == 0) {
838 softc->initiated = 1;
839 ++udev_initiated_count;
840 TAILQ_INSERT_HEAD(&udev_evq, &softc->marker, link);
842 lockmgr(&udev_lk, LK_RELEASE);
844 odict = prop_dictionary_create();
849 if ((prop_dictionary_set(odict, "array", ctx.cdevs)) == 0) {
851 "udev_getdevs_ioctl: prop_dictionary_set failed\n");
852 prop_object_release(odict);
856 error = prop_dictionary_copyout_ioctl(pref, cmd, odict);
858 prop_object_release(odict);
869 lockinit(&udev_lk, "udevlk", 0, LK_CANRECURSE);
871 TAILQ_INIT(&udev_evq);
882 udev_dev = make_autoclone_dev(&udev_dev_ops, &DEVFS_CLONE_BITMAP(udev),
884 UID_ROOT, GID_WHEEL, 0600, "udev");
888 udev_dev_uninit(void)
890 destroy_dev(udev_dev);
893 SYSINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY,
895 SYSUNINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY,
897 SYSINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY,
898 udev_dev_init, NULL);
899 SYSUNINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY,
900 udev_dev_uninit, NULL);