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/vnode.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 #include <sys/thread2.h>
51 MALLOC_DEFINE(M_UDEV, "udev", "udev allocs");
53 /* XXX: use UUIDs for identification; would need help from devfs */
55 static cdev_t udev_dev;
56 static d_open_t udev_dev_open;
57 static d_close_t udev_dev_close;
58 static d_read_t udev_dev_read;
59 static d_kqfilter_t udev_dev_kqfilter;
60 static d_ioctl_t udev_dev_ioctl;
61 static d_clone_t udev_dev_clone;
63 struct udev_prop_ctx {
68 struct udev_event_kernel {
70 TAILQ_ENTRY(udev_event_kernel) link;
74 TAILQ_ENTRY(udev_softc) entry;
80 struct udev_event_kernel marker; /* udev_evq marker */
85 int (*fn)(struct udev_softc *, struct plistref *,
86 u_long, prop_dictionary_t);
90 static int _udev_dict_set_cstr(prop_dictionary_t, const char *, char *);
91 static int _udev_dict_set_int(prop_dictionary_t, const char *, int64_t);
92 static int _udev_dict_set_uint(prop_dictionary_t, const char *, uint64_t);
93 static int _udev_dict_delete_key(prop_dictionary_t, const char *);
94 static prop_dictionary_t udev_init_dict_event(cdev_t, const char *);
95 static int udev_init_dict(cdev_t);
96 static int udev_destroy_dict(cdev_t);
97 static void udev_event_insert(int, prop_dictionary_t);
98 static void udev_clean_events_locked(void);
99 static char *udev_event_externalize(struct udev_event_kernel *);
100 static void udev_getdevs_scan_callback(char *, cdev_t, bool, void *);
101 static int udev_getdevs_ioctl(struct udev_softc *, struct plistref *,
102 u_long, prop_dictionary_t);
103 static void udev_dev_filter_detach(struct knote *);
104 static int udev_dev_filter_read(struct knote *, long);
106 static struct dev_ops udev_dev_ops = {
108 .d_open = udev_dev_open,
109 .d_close = udev_dev_close,
110 .d_read = udev_dev_read,
111 .d_kqfilter = udev_dev_kqfilter,
112 .d_ioctl = udev_dev_ioctl
115 static struct cmd_function cmd_fn[] = {
116 { .cmd = "getdevs", .fn = udev_getdevs_ioctl},
120 DEVFS_DECLARE_CLONE_BITMAP(udev);
122 static TAILQ_HEAD(, udev_softc) udevq;
123 static TAILQ_HEAD(, udev_event_kernel) udev_evq;
124 static struct kqinfo udev_kq;
125 static struct lock udev_lk;
126 static int udev_evqlen;
127 static int udev_initiated_count;
128 static int udev_open_count;
129 static int udev_seqwait;
133 _udev_dict_set_cstr(prop_dictionary_t dict, const char *key, char *str)
137 KKASSERT(dict != NULL);
139 ps = prop_string_create_cstring(str);
144 if (prop_dictionary_set(dict, key, ps) == false) {
145 prop_object_release(ps);
149 prop_object_release(ps);
154 _udev_dict_set_int(prop_dictionary_t dict, const char *key, int64_t val)
158 KKASSERT(dict != NULL);
160 pn = prop_number_create_integer(val);
164 if (prop_dictionary_set(dict, key, pn) == false) {
165 prop_object_release(pn);
169 prop_object_release(pn);
174 _udev_dict_set_uint(prop_dictionary_t dict, const char *key, uint64_t val)
178 KKASSERT(dict != NULL);
180 pn = prop_number_create_unsigned_integer(val);
184 if (prop_dictionary_set(dict, key, pn) == false) {
185 prop_object_release(pn);
189 prop_object_release(pn);
194 _udev_dict_delete_key(prop_dictionary_t dict, const char *key)
196 KKASSERT(dict != NULL);
198 prop_dictionary_remove(dict, key);
204 * Initialize an event dictionary, which contains three parameters to
205 * identify the device referred to (name, devnum, kptr) and the affected key.
207 static prop_dictionary_t
208 udev_init_dict_event(cdev_t dev, const char *key)
210 prop_dictionary_t dict;
214 kptr = (uint64_t)(uintptr_t)dev;
215 KKASSERT(dev != NULL);
217 dict = prop_dictionary_create();
219 log(LOG_DEBUG, "udev_init_dict_event: prop_dictionary_create() failed\n");
223 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
225 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
227 if ((error = _udev_dict_set_uint(dict, "devtype", (dev_dflags(dev) & D_TYPEMASK))))
229 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
231 if ((error = _udev_dict_set_cstr(dict, "key", __DECONST(char *, key))))
237 prop_object_release(dict);
242 udev_dict_set_cstr(cdev_t dev, const char *key, char *str)
244 prop_dictionary_t dict;
247 KKASSERT(dev != NULL);
249 if (dev->si_dict == NULL) {
250 error = udev_init_dict(dev);
255 /* Queue a key update event */
256 dict = udev_init_dict_event(dev, key);
260 if ((error = _udev_dict_set_cstr(dict, "value", str))) {
261 prop_object_release(dict);
264 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
265 prop_object_release(dict);
267 error = _udev_dict_set_cstr(dev->si_dict, key, str);
272 udev_dict_set_int(cdev_t dev, const char *key, int64_t val)
274 prop_dictionary_t dict;
277 KKASSERT(dev != NULL);
279 if (dev->si_dict == NULL) {
280 error = udev_init_dict(dev);
285 /* Queue a key update event */
286 dict = udev_init_dict_event(dev, key);
289 if ((error = _udev_dict_set_int(dict, "value", val))) {
290 prop_object_release(dict);
293 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
294 prop_object_release(dict);
296 return _udev_dict_set_int(dev->si_dict, key, val);
300 udev_dict_set_uint(cdev_t dev, const char *key, uint64_t val)
302 prop_dictionary_t dict;
305 KKASSERT(dev != NULL);
307 if (dev->si_dict == NULL) {
308 error = udev_init_dict(dev);
313 /* Queue a key update event */
314 dict = udev_init_dict_event(dev, key);
317 if ((error = _udev_dict_set_uint(dict, "value", val))) {
318 prop_object_release(dict);
321 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
322 prop_object_release(dict);
324 return _udev_dict_set_uint(dev->si_dict, key, val);
328 udev_dict_delete_key(cdev_t dev, const char *key)
330 prop_dictionary_t dict;
332 KKASSERT(dev != NULL);
334 /* Queue a key removal event */
335 dict = udev_init_dict_event(dev, key);
338 udev_event_insert(UDEV_EV_KEY_REMOVE, dict);
339 prop_object_release(dict);
341 return _udev_dict_delete_key(dev->si_dict, key);
345 udev_init_dict(cdev_t dev)
347 prop_dictionary_t dict;
351 kptr = (uint64_t)(uintptr_t)dev;
353 KKASSERT(dev != NULL);
355 if (dev->si_dict != NULL) {
358 "udev_init_dict: new dict for %s, but has dict already (%p)!\n",
359 dev->si_name, dev->si_dict);
364 dict = prop_dictionary_create();
366 log(LOG_DEBUG, "udev_init_dict: prop_dictionary_create() failed\n");
370 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
372 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
374 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
376 if ((error = _udev_dict_set_uint(dict, "devtype", (dev_dflags(dev) & D_TYPEMASK))))
379 /* XXX: The next 3 are marginallly useful, if at all */
380 if ((error = _udev_dict_set_uint(dict, "uid", dev->si_uid)))
382 if ((error = _udev_dict_set_uint(dict, "gid", dev->si_gid)))
384 if ((error = _udev_dict_set_int(dict, "mode", dev->si_perms)))
387 if ((error = _udev_dict_set_int(dict, "major", dev->si_umajor)))
389 if ((error = _udev_dict_set_int(dict, "minor", dev->si_uminor)))
391 if (dev->si_ops->head.name != NULL) {
392 if ((error = _udev_dict_set_cstr(dict, "driver",
393 __DECONST(char *, dev->si_ops->head.name))))
402 prop_object_release(dict);
407 udev_destroy_dict(cdev_t dev)
409 KKASSERT(dev != NULL);
411 if (dev->si_dict != NULL) {
412 prop_object_release(dev->si_dict);
420 udev_event_insert(int ev_type, prop_dictionary_t dict)
422 struct udev_event_kernel *ev;
424 /* Only start queing events after client has initiated properly */
425 if (udev_initiated_count) {
426 /* XXX: use objcache eventually */
427 ev = kmalloc(sizeof(*ev), M_UDEV, M_WAITOK);
428 ev->ev.ev_dict = prop_dictionary_copy(dict);
429 if (ev->ev.ev_dict == NULL) {
433 ev->ev.ev_type = ev_type;
435 lockmgr(&udev_lk, LK_EXCLUSIVE);
436 TAILQ_INSERT_TAIL(&udev_evq, ev, link);
440 wakeup(&udev_seqwait);
441 lockmgr(&udev_lk, LK_RELEASE);
443 KNOTE(&udev_kq.ki_note, 0);
444 } else if (udev_open_count) {
445 lockmgr(&udev_lk, LK_EXCLUSIVE);
448 wakeup(&udev_seqwait);
449 lockmgr(&udev_lk, LK_RELEASE);
450 KNOTE(&udev_kq.ki_note, 0);
455 udev_clean_events_locked(void)
457 struct udev_event_kernel *ev;
459 while ((ev = TAILQ_FIRST(&udev_evq)) &&
460 ev->ev.ev_dict != NULL) {
461 TAILQ_REMOVE(&udev_evq, ev, link);
468 udev_event_externalize(struct udev_event_kernel *ev)
470 prop_dictionary_t dict;
475 dict = prop_dictionary_create();
478 "udev_event_externalize: prop_dictionary_create() failed\n");
482 if ((error = _udev_dict_set_int(dict, "evtype", ev->ev.ev_type))) {
483 prop_object_release(dict);
487 if (prop_dictionary_set(dict, "evdict", ev->ev.ev_dict) == false) {
488 prop_object_release(dict);
492 prop_object_release(ev->ev.ev_dict);
494 xml = prop_dictionary_externalize(dict);
496 prop_object_release(dict);
502 udev_event_attach(cdev_t dev, char *name, int alias)
504 prop_dictionary_t dict;
507 KKASSERT(dev != NULL);
512 dict = prop_dictionary_copy(dev->si_dict);
516 if ((error = _udev_dict_set_cstr(dict, "name", name))) {
517 prop_object_release(dict);
521 _udev_dict_set_int(dict, "alias", 1);
523 udev_event_insert(UDEV_EVENT_ATTACH, dict);
524 prop_object_release(dict);
526 error = udev_init_dict(dev);
530 _udev_dict_set_int(dev->si_dict, "alias", 0);
531 udev_event_insert(UDEV_EVENT_ATTACH, dev->si_dict);
539 udev_event_detach(cdev_t dev, char *name, int alias)
541 prop_dictionary_t dict;
543 KKASSERT(dev != NULL);
546 dict = prop_dictionary_copy(dev->si_dict);
550 if (_udev_dict_set_cstr(dict, "name", name)) {
551 prop_object_release(dict);
555 _udev_dict_set_int(dict, "alias", 1);
557 udev_event_insert(UDEV_EVENT_DETACH, dict);
558 prop_object_release(dict);
560 udev_event_insert(UDEV_EVENT_DETACH, dev->si_dict);
564 udev_destroy_dict(dev);
570 * Allow multiple opens. Each opener gets a different device.
571 * Messages are replicated to all devices using a marker system.
574 udev_dev_clone(struct dev_clone_args *ap)
576 struct udev_softc *softc;
579 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(udev), 1000);
585 softc = kmalloc(sizeof(*softc), M_UDEV, M_WAITOK | M_ZERO);
587 lockmgr(&udev_lk, LK_EXCLUSIVE);
588 TAILQ_INSERT_TAIL(&udevq, softc, entry);
589 lockmgr(&udev_lk, LK_RELEASE);
591 softc->dev = make_only_dev(&udev_dev_ops, unit, ap->a_cred->cr_ruid,
592 0, 0600, "udev/%d", unit);
593 softc->dev->si_drv1 = softc;
594 ap->a_dev = softc->dev;
602 udev_dev_open(struct dev_open_args *ap)
604 struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
606 lockmgr(&udev_lk, LK_EXCLUSIVE);
607 if (softc == NULL || softc->opened) {
608 lockmgr(&udev_lk, LK_RELEASE);
613 lockmgr(&udev_lk, LK_RELEASE);
619 udev_dev_close(struct dev_close_args *ap)
621 struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
623 KKASSERT(softc->dev == ap->a_head.a_dev);
624 KKASSERT(softc->opened == 1);
626 lockmgr(&udev_lk, LK_EXCLUSIVE);
627 TAILQ_REMOVE(&udevq, softc, entry);
628 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(udev), softc->unit);
630 if (softc->initiated) {
631 TAILQ_REMOVE(&udev_evq, &softc->marker, link);
632 softc->initiated = 0;
633 --udev_initiated_count;
634 udev_clean_events_locked();
638 ap->a_head.a_dev->si_drv1 = NULL;
640 lockmgr(&udev_lk, LK_RELEASE);
642 destroy_dev(ap->a_head.a_dev);
645 kfree(softc, M_UDEV);
650 static struct filterops udev_dev_read_filtops =
651 { FILTEROP_ISFD, NULL, udev_dev_filter_detach, udev_dev_filter_read };
654 udev_dev_kqfilter(struct dev_kqfilter_args *ap)
656 struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
657 struct knote *kn = ap->a_kn;
661 lockmgr(&udev_lk, LK_EXCLUSIVE);
663 switch (kn->kn_filter) {
665 kn->kn_fop = &udev_dev_read_filtops;
666 kn->kn_hook = (caddr_t)softc;
669 ap->a_result = EOPNOTSUPP;
670 lockmgr(&udev_lk, LK_RELEASE);
674 klist = &udev_kq.ki_note;
675 knote_insert(klist, kn);
677 lockmgr(&udev_lk, LK_RELEASE);
683 udev_dev_filter_detach(struct knote *kn)
687 lockmgr(&udev_lk, LK_EXCLUSIVE);
688 klist = &udev_kq.ki_note;
689 knote_remove(klist, kn);
690 lockmgr(&udev_lk, LK_RELEASE);
694 udev_dev_filter_read(struct knote *kn, long hint)
696 struct udev_softc *softc = (void *)kn->kn_hook;
697 struct udev_event_kernel *ev;
700 lockmgr(&udev_lk, LK_EXCLUSIVE);
701 if (softc->initiated) {
702 ev = TAILQ_NEXT(&softc->marker, link);
703 while (ev && ev->ev.ev_dict == NULL)
704 ev = TAILQ_NEXT(ev, link);
708 lockmgr(&udev_lk, LK_RELEASE);
714 udev_dev_read(struct dev_read_args *ap)
716 struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
717 struct udev_event_kernel *ev;
718 struct uio *uio = ap->a_uio;
723 lockmgr(&udev_lk, LK_EXCLUSIVE);
726 * Automatically enable message collection if it has not already
729 if (softc->initiated == 0) {
730 softc->initiated = 1;
731 ++udev_initiated_count;
732 TAILQ_INSERT_HEAD(&udev_evq, &softc->marker, link);
736 * Loop, sleep interruptably until we get an event or signal.
740 if (softc->initiated) {
741 ev = TAILQ_NEXT(&softc->marker, link);
742 while (ev && ev->ev.ev_dict == NULL)
743 ev = TAILQ_NEXT(ev, link);
745 if ((xml = udev_event_externalize(ev)) == NULL) {
749 len = strlen(xml) + 1; /* include terminator */
750 if (uio->uio_resid < len)
753 error = uiomove((caddr_t)xml, len, uio);
759 TAILQ_REMOVE(&udev_evq, &softc->marker, link);
760 TAILQ_INSERT_AFTER(&udev_evq,
761 ev, &softc->marker, link);
762 udev_clean_events_locked();
766 if (ap->a_ioflag & IO_NDELAY) {
770 if ((error = lksleep(&udev_evq, &udev_lk, PCATCH, "udevq", 0)))
774 lockmgr(&udev_lk, LK_RELEASE);
779 udev_dev_ioctl(struct dev_ioctl_args *ap)
781 struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
782 prop_dictionary_t dict;
785 struct plistref *pref;
793 /* Use proplib(3) for userspace/kernel communication */
794 pref = (struct plistref *)ap->a_data;
795 error = prop_dictionary_copyin_ioctl(pref, ap->a_cmd, &dict);
799 po = prop_dictionary_get(dict, "command");
800 if (po == NULL || prop_object_type(po) != PROP_TYPE_STRING) {
801 log(LOG_DEBUG, "udev: prop_dictionary_get() failed\n");
802 prop_object_release(dict);
808 for(i = 0; cmd_fn[i].cmd != NULL; i++) {
809 if (prop_string_equals_cstring(ps, cmd_fn[i].cmd))
813 if (cmd_fn[i].cmd != NULL) {
814 error = cmd_fn[i].fn(softc, pref, ap->a_cmd, dict);
819 //prop_object_release(po);
820 prop_object_release(dict);
824 * Wait for events based on sequence number. Updates
825 * sequence number for loop.
827 lockmgr(&udev_lk, LK_EXCLUSIVE);
828 seq = *(int *)ap->a_data;
830 while (seq == udev_seq) {
831 error = lksleep(&udev_seqwait, &udev_lk,
837 *(int *)ap->a_data = udev_seq;
838 lockmgr(&udev_lk, LK_RELEASE);
841 error = ENOTTY; /* Inappropriate ioctl for device */
849 udev_getdevs_scan_callback(char *name, cdev_t cdev, bool is_alias, void *arg)
851 struct udev_prop_ctx *ctx = arg;
853 KKASSERT(arg != NULL);
855 if (cdev->si_dict == NULL)
858 if (prop_array_add(ctx->cdevs, cdev->si_dict) == false) {
865 udev_getdevs_ioctl(struct udev_softc *softc, struct plistref *pref,
866 u_long cmd, prop_dictionary_t dict)
868 prop_dictionary_t odict;
869 struct udev_prop_ctx ctx;
873 * Ensure event notification is enabled before doing the devfs
874 * scan so nothing gets missed.
876 lockmgr(&udev_lk, LK_EXCLUSIVE);
877 if (softc->initiated == 0) {
878 softc->initiated = 1;
879 ++udev_initiated_count;
880 TAILQ_INSERT_HEAD(&udev_evq, &softc->marker, link);
882 lockmgr(&udev_lk, LK_RELEASE);
885 * Devfs scan to build full dictionary.
888 ctx.cdevs = prop_array_create();
889 if (ctx.cdevs == NULL) {
891 "udev_getdevs_ioctl: prop_array_create() failed\n");
895 devfs_scan_callback(udev_getdevs_scan_callback, &ctx);
897 if (ctx.error != 0) {
898 prop_object_release(ctx.cdevs);
902 odict = prop_dictionary_create();
907 if ((prop_dictionary_set(odict, "array", ctx.cdevs)) == 0) {
909 "udev_getdevs_ioctl: prop_dictionary_set failed\n");
910 prop_object_release(odict);
914 error = prop_dictionary_copyout_ioctl(pref, cmd, odict);
916 prop_object_release(odict);
927 lockinit(&udev_lk, "udevlk", 0, LK_CANRECURSE);
929 TAILQ_INIT(&udev_evq);
940 udev_dev = make_autoclone_dev(&udev_dev_ops, &DEVFS_CLONE_BITMAP(udev),
942 UID_ROOT, GID_WHEEL, 0600, "udev");
946 udev_dev_uninit(void)
948 destroy_dev(udev_dev);
951 SYSINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY,
953 SYSUNINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY,
955 SYSINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY,
956 udev_dev_init, NULL);
957 SYSUNINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY,
958 udev_dev_uninit, NULL);