2 * Copyright (c) 2010,2018 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>
37 #include <sys/kernel.h>
41 #include <sys/event.h>
42 #include <sys/vnode.h>
43 #include <sys/malloc.h>
44 #include <sys/objcache.h>
45 #include <sys/ctype.h>
46 #include <sys/syslog.h>
48 #include <sys/devfs.h>
49 #include <libprop/proplib.h>
51 MALLOC_DEFINE(M_UDEV
, "udev", "udev allocs");
52 static struct objcache
*udev_event_kernel_cache
;
54 /* XXX: use UUIDs for identification; would need help from devfs */
56 static cdev_t udev_dev
;
57 static d_open_t udev_dev_open
;
58 static d_close_t udev_dev_close
;
59 static d_read_t udev_dev_read
;
60 static d_kqfilter_t udev_dev_kqfilter
;
61 static d_ioctl_t udev_dev_ioctl
;
62 static d_clone_t udev_dev_clone
;
64 struct udev_prop_ctx
{
69 struct udev_event_kernel
{
71 TAILQ_ENTRY(udev_event_kernel
) link
;
75 TAILQ_ENTRY(udev_softc
) entry
;
81 struct udev_event_kernel marker
; /* udev_evq marker */
86 int (*fn
)(struct udev_softc
*, struct plistref
*,
87 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_DEFINE_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
;
131 static struct lock udev_dict_lk
= LOCK_INITIALIZER("dict", 0, 0);
134 * Acquire the device's si_dict and lock the device's si_dict field.
135 * If the device does not have an attached dictionary, NULL is returned.
137 * This function must be matched by a udev_put_dict() call regardless of
138 * the return value. The device field is STILL LOCKED even when NULL is
141 * Currently a single global lock is implemented.
143 static prop_dictionary_t
144 udev_get_dict(cdev_t dev
)
146 prop_dictionary_t udict
;
148 lockmgr(&udev_dict_lk
, LK_EXCLUSIVE
|LK_CANRECURSE
);
149 udict
= dev
->si_dict
;
151 prop_object_retain(udict
);
156 * Release the dictionary previously returned by udev_get_dict() and unlock
157 * the device's si_dict field. udict may be NULL.
160 udev_put_dict(cdev_t dev
, prop_dictionary_t udict
)
163 prop_object_release(udict
);
164 lockmgr(&udev_dict_lk
, LK_RELEASE
);
168 _udev_dict_set_cstr(prop_dictionary_t dict
, const char *key
, char *str
)
172 KKASSERT(dict
!= NULL
);
174 ps
= prop_string_create_cstring(str
);
179 if (prop_dictionary_set(dict
, key
, ps
) == false) {
180 prop_object_release(ps
);
184 prop_object_release(ps
);
189 _udev_dict_set_int(prop_dictionary_t dict
, const char *key
, int64_t val
)
193 KKASSERT(dict
!= NULL
);
195 pn
= prop_number_create_integer(val
);
199 if (prop_dictionary_set(dict
, key
, pn
) == false) {
200 prop_object_release(pn
);
204 prop_object_release(pn
);
209 _udev_dict_set_uint(prop_dictionary_t dict
, const char *key
, uint64_t val
)
213 KKASSERT(dict
!= NULL
);
215 pn
= prop_number_create_unsigned_integer(val
);
219 if (prop_dictionary_set(dict
, key
, pn
) == false) {
220 prop_object_release(pn
);
224 prop_object_release(pn
);
229 _udev_dict_delete_key(prop_dictionary_t dict
, const char *key
)
231 KKASSERT(dict
!= NULL
);
233 prop_dictionary_remove(dict
, key
);
239 * Initialize an event dictionary, which contains three parameters to
240 * identify the device referred to (name, devnum, kptr) and the affected key.
242 static prop_dictionary_t
243 udev_init_dict_event(cdev_t dev
, const char *key
)
245 prop_dictionary_t dict
;
249 kptr
= (uint64_t)(uintptr_t)dev
;
250 KKASSERT(dev
!= NULL
);
252 dict
= prop_dictionary_create();
254 log(LOG_DEBUG
, "udev_init_dict_event: prop_dictionary_create() failed\n");
258 if ((error
= _udev_dict_set_cstr(dict
, "name", dev
->si_name
)))
260 if ((error
= _udev_dict_set_uint(dict
, "devnum", dev
->si_inode
)))
262 if ((error
= _udev_dict_set_uint(dict
, "devtype", (dev_dflags(dev
) & D_TYPEMASK
))))
264 if ((error
= _udev_dict_set_uint(dict
, "kptr", kptr
)))
266 if ((error
= _udev_dict_set_cstr(dict
, "key", __DECONST(char *, key
))))
272 prop_object_release(dict
);
277 udev_dict_set_cstr(cdev_t dev
, const char *key
, char *str
)
279 prop_dictionary_t dict
;
280 prop_dictionary_t udict
;
283 KKASSERT(dev
!= NULL
);
285 while ((udict
= udev_get_dict(dev
)) == NULL
) {
286 error
= udev_init_dict(dev
);
287 udev_put_dict(dev
, udict
);
292 /* Queue a key update event */
293 dict
= udev_init_dict_event(dev
, key
);
299 if ((error
= _udev_dict_set_cstr(dict
, "value", str
))) {
300 prop_object_release(dict
);
303 udev_event_insert(UDEV_EV_KEY_UPDATE
, dict
);
304 prop_object_release(dict
);
305 error
= _udev_dict_set_cstr(udict
, key
, str
);
308 udev_put_dict(dev
, udict
);
314 udev_dict_set_int(cdev_t dev
, const char *key
, int64_t val
)
316 prop_dictionary_t dict
;
317 prop_dictionary_t udict
;
320 KKASSERT(dev
!= NULL
);
322 while ((udict
= udev_get_dict(dev
)) == NULL
) {
323 error
= udev_init_dict(dev
);
324 udev_put_dict(dev
, udict
);
329 /* Queue a key update event */
330 dict
= udev_init_dict_event(dev
, key
);
335 if ((error
= _udev_dict_set_int(dict
, "value", val
))) {
336 prop_object_release(dict
);
339 udev_event_insert(UDEV_EV_KEY_UPDATE
, dict
);
340 prop_object_release(dict
);
341 error
= _udev_dict_set_int(udict
, key
, val
);
343 udev_put_dict(dev
, udict
);
349 udev_dict_set_uint(cdev_t dev
, const char *key
, uint64_t val
)
351 prop_dictionary_t dict
;
352 prop_dictionary_t udict
;
355 KKASSERT(dev
!= NULL
);
357 while ((udict
= udev_get_dict(dev
)) == NULL
) {
358 error
= udev_init_dict(dev
);
359 udev_put_dict(dev
, udict
);
364 /* Queue a key update event */
365 dict
= udev_init_dict_event(dev
, key
);
370 if ((error
= _udev_dict_set_uint(dict
, "value", val
))) {
371 prop_object_release(dict
);
374 udev_event_insert(UDEV_EV_KEY_UPDATE
, dict
);
375 prop_object_release(dict
);
376 error
= _udev_dict_set_uint(udict
, key
, val
);
378 udev_put_dict(dev
, udict
);
384 udev_dict_delete_key(cdev_t dev
, const char *key
)
386 prop_dictionary_t dict
;
387 prop_dictionary_t udict
;
390 KKASSERT(dev
!= NULL
);
391 udict
= udev_get_dict(dev
);
397 /* Queue a key removal event */
398 dict
= udev_init_dict_event(dev
, key
);
403 udev_event_insert(UDEV_EV_KEY_REMOVE
, dict
);
404 prop_object_release(dict
);
405 error
= _udev_dict_delete_key(udict
, key
);
407 udev_put_dict(dev
, udict
);
413 * device dictionary access already locked
416 udev_init_dict(cdev_t dev
)
418 prop_dictionary_t dict
;
422 kptr
= (uint64_t)(uintptr_t)dev
;
424 KKASSERT(dev
!= NULL
);
426 if (dev
->si_dict
!= NULL
) {
428 "udev_init_dict: new dict for %s, but has "
429 "dict already (%p)!\n",
430 dev
->si_name
, dev
->si_dict
);
434 dict
= prop_dictionary_create();
437 "udev_init_dict: prop_dictionary_create() failed\n");
441 if ((error
= _udev_dict_set_cstr(dict
, "name", dev
->si_name
)))
443 if ((error
= _udev_dict_set_uint(dict
, "devnum", dev
->si_inode
)))
445 if ((error
= _udev_dict_set_uint(dict
, "kptr", kptr
)))
447 if ((error
= _udev_dict_set_uint(dict
, "devtype",
448 (dev_dflags(dev
) & D_TYPEMASK
)))) {
452 /* XXX: The next 3 are marginallly useful, if at all */
453 if ((error
= _udev_dict_set_uint(dict
, "uid", dev
->si_uid
)))
455 if ((error
= _udev_dict_set_uint(dict
, "gid", dev
->si_gid
)))
457 if ((error
= _udev_dict_set_int(dict
, "mode", dev
->si_perms
)))
460 if ((error
= _udev_dict_set_int(dict
, "major", dev
->si_umajor
)))
462 if ((error
= _udev_dict_set_int(dict
, "minor", dev
->si_uminor
)))
464 if (dev
->si_ops
->head
.name
!= NULL
) {
465 if ((error
= _udev_dict_set_cstr(dict
, "driver",
466 __DECONST(char *, dev
->si_ops
->head
.name
))))
475 prop_object_release(dict
);
480 * device dictionary access already locked
483 udev_destroy_dict(cdev_t dev
)
485 prop_dictionary_t udict
;
487 KKASSERT(dev
!= NULL
);
488 udict
= udev_get_dict(dev
);
490 /* field is now locked, use directly to handle races */
492 prop_object_release(dev
->si_dict
);
495 udev_put_dict(dev
, udict
);
501 udev_event_insert(int ev_type
, prop_dictionary_t dict
)
503 struct udev_event_kernel
*ev
;
504 prop_dictionary_t dict_copy
;
506 /* Only start queing events after client has initiated properly */
507 if (udev_initiated_count
) {
508 dict_copy
= prop_dictionary_copy(dict
);
509 if (dict_copy
== NULL
)
511 ev
= objcache_get(udev_event_kernel_cache
, M_WAITOK
);
512 ev
->ev
.ev_dict
= dict_copy
;
513 ev
->ev
.ev_type
= ev_type
;
515 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
516 TAILQ_INSERT_TAIL(&udev_evq
, ev
, link
);
520 wakeup(&udev_seqwait
);
521 lockmgr(&udev_lk
, LK_RELEASE
);
523 KNOTE(&udev_kq
.ki_note
, 0);
524 } else if (udev_open_count
) {
525 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
528 wakeup(&udev_seqwait
);
529 lockmgr(&udev_lk
, LK_RELEASE
);
530 KNOTE(&udev_kq
.ki_note
, 0);
535 udev_clean_events_locked(void)
537 struct udev_event_kernel
*ev
;
539 while ((ev
= TAILQ_FIRST(&udev_evq
)) &&
540 ev
->ev
.ev_dict
!= NULL
) {
541 TAILQ_REMOVE(&udev_evq
, ev
, link
);
542 objcache_put(udev_event_kernel_cache
, ev
);
548 udev_event_externalize(struct udev_event_kernel
*ev
)
550 prop_dictionary_t dict
;
554 dict
= prop_dictionary_create();
557 "udev_event_externalize: prop_dictionary_create() failed\n");
561 if ((error
= _udev_dict_set_int(dict
, "evtype", ev
->ev
.ev_type
))) {
562 prop_object_release(dict
);
566 if (prop_dictionary_set(dict
, "evdict", ev
->ev
.ev_dict
) == false) {
567 prop_object_release(dict
);
571 prop_object_release(ev
->ev
.ev_dict
);
573 xml
= prop_dictionary_externalize(dict
);
575 prop_object_release(dict
);
581 udev_event_attach(cdev_t dev
, char *name
, int alias
)
583 prop_dictionary_t dict
;
584 prop_dictionary_t udict
;
587 KKASSERT(dev
!= NULL
);
591 udict
= udev_get_dict(dev
);
595 dict
= prop_dictionary_copy(udict
);
599 if ((error
= _udev_dict_set_cstr(dict
, "name", name
))) {
600 prop_object_release(dict
);
604 _udev_dict_set_int(dict
, "alias", 1);
606 udev_event_insert(UDEV_EVENT_ATTACH
, dict
);
607 prop_object_release(dict
);
609 while (udict
== NULL
) {
610 error
= udev_init_dict(dev
);
613 udev_put_dict(dev
, udict
);
614 udict
= udev_get_dict(dev
);
616 _udev_dict_set_int(udict
, "alias", 0);
617 udev_event_insert(UDEV_EVENT_ATTACH
, udict
);
620 udev_put_dict(dev
, udict
);
626 udev_event_detach(cdev_t dev
, char *name
, int alias
)
628 prop_dictionary_t dict
;
629 prop_dictionary_t udict
;
631 KKASSERT(dev
!= NULL
);
633 udict
= udev_get_dict(dev
);
635 dict
= prop_dictionary_copy(udict
);
639 if (_udev_dict_set_cstr(dict
, "name", name
)) {
640 prop_object_release(dict
);
644 _udev_dict_set_int(dict
, "alias", 1);
646 udev_event_insert(UDEV_EVENT_DETACH
, dict
);
647 prop_object_release(dict
);
650 udev_event_insert(UDEV_EVENT_DETACH
, udict
);
654 udev_destroy_dict(dev
);
655 udev_put_dict(dev
, udict
);
661 * Allow multiple opens. Each opener gets a different device.
662 * Messages are replicated to all devices using a marker system.
665 udev_dev_clone(struct dev_clone_args
*ap
)
667 struct udev_softc
*softc
;
670 unit
= devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(udev
), 1000);
676 softc
= kmalloc(sizeof(*softc
), M_UDEV
, M_WAITOK
| M_ZERO
);
678 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
679 TAILQ_INSERT_TAIL(&udevq
, softc
, entry
);
680 lockmgr(&udev_lk
, LK_RELEASE
);
682 softc
->dev
= make_only_dev(&udev_dev_ops
, unit
, ap
->a_cred
->cr_ruid
,
683 0, 0600, "udevs/%d", unit
);
684 softc
->dev
->si_drv1
= softc
;
685 ap
->a_dev
= softc
->dev
;
693 udev_dev_open(struct dev_open_args
*ap
)
695 struct udev_softc
*softc
= ap
->a_head
.a_dev
->si_drv1
;
697 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
698 if (softc
== NULL
|| softc
->opened
) {
699 lockmgr(&udev_lk
, LK_RELEASE
);
704 lockmgr(&udev_lk
, LK_RELEASE
);
710 udev_dev_close(struct dev_close_args
*ap
)
712 struct udev_softc
*softc
= ap
->a_head
.a_dev
->si_drv1
;
714 KKASSERT(softc
->dev
== ap
->a_head
.a_dev
);
715 KKASSERT(softc
->opened
== 1);
717 destroy_dev(ap
->a_head
.a_dev
);
718 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
719 TAILQ_REMOVE(&udevq
, softc
, entry
);
721 if (softc
->initiated
) {
722 TAILQ_REMOVE(&udev_evq
, &softc
->marker
, link
);
723 softc
->initiated
= 0;
724 --udev_initiated_count
;
725 udev_clean_events_locked();
729 ap
->a_head
.a_dev
->si_drv1
= NULL
;
731 lockmgr(&udev_lk
, LK_RELEASE
);
734 * WARNING! devfs_clone_bitmap_put() interacts with the devfs
735 * thread, avoid deadlocks by ensuring we are unlocked
738 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(udev
), softc
->unit
);
741 kfree(softc
, M_UDEV
);
746 static struct filterops udev_dev_read_filtops
=
747 { FILTEROP_ISFD
| FILTEROP_MPSAFE
, NULL
,
748 udev_dev_filter_detach
, udev_dev_filter_read
};
751 udev_dev_kqfilter(struct dev_kqfilter_args
*ap
)
753 struct udev_softc
*softc
= ap
->a_head
.a_dev
->si_drv1
;
754 struct knote
*kn
= ap
->a_kn
;
758 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
760 switch (kn
->kn_filter
) {
762 kn
->kn_fop
= &udev_dev_read_filtops
;
763 kn
->kn_hook
= (caddr_t
)softc
;
766 ap
->a_result
= EOPNOTSUPP
;
767 lockmgr(&udev_lk
, LK_RELEASE
);
771 klist
= &udev_kq
.ki_note
;
772 knote_insert(klist
, kn
);
774 lockmgr(&udev_lk
, LK_RELEASE
);
780 udev_dev_filter_detach(struct knote
*kn
)
784 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
785 klist
= &udev_kq
.ki_note
;
786 knote_remove(klist
, kn
);
787 lockmgr(&udev_lk
, LK_RELEASE
);
791 udev_dev_filter_read(struct knote
*kn
, long hint
)
793 struct udev_softc
*softc
= (void *)kn
->kn_hook
;
794 struct udev_event_kernel
*ev
;
797 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
798 if (softc
->initiated
) {
799 ev
= TAILQ_NEXT(&softc
->marker
, link
);
800 while (ev
&& ev
->ev
.ev_dict
== NULL
)
801 ev
= TAILQ_NEXT(ev
, link
);
805 lockmgr(&udev_lk
, LK_RELEASE
);
811 udev_dev_read(struct dev_read_args
*ap
)
813 struct udev_softc
*softc
= ap
->a_head
.a_dev
->si_drv1
;
814 struct udev_event_kernel
*ev
;
815 struct uio
*uio
= ap
->a_uio
;
820 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
823 * Automatically enable message collection if it has not already
826 if (softc
->initiated
== 0) {
827 softc
->initiated
= 1;
828 ++udev_initiated_count
;
829 TAILQ_INSERT_HEAD(&udev_evq
, &softc
->marker
, link
);
833 * Loop, sleep interruptably until we get an event or signal.
837 if (softc
->initiated
) {
838 ev
= TAILQ_NEXT(&softc
->marker
, link
);
839 while (ev
&& ev
->ev
.ev_dict
== NULL
)
840 ev
= TAILQ_NEXT(ev
, link
);
842 if ((xml
= udev_event_externalize(ev
)) == NULL
) {
846 len
= strlen(xml
) + 1; /* include terminator */
847 if (uio
->uio_resid
< len
)
850 error
= uiomove((caddr_t
)xml
, len
, uio
);
856 TAILQ_REMOVE(&udev_evq
, &softc
->marker
, link
);
857 TAILQ_INSERT_AFTER(&udev_evq
,
858 ev
, &softc
->marker
, link
);
859 udev_clean_events_locked();
863 if (ap
->a_ioflag
& IO_NDELAY
) {
867 if ((error
= lksleep(&udev_evq
, &udev_lk
, PCATCH
, "udevq", 0)))
871 lockmgr(&udev_lk
, LK_RELEASE
);
876 udev_dev_ioctl(struct dev_ioctl_args
*ap
)
878 struct udev_softc
*softc
= ap
->a_head
.a_dev
->si_drv1
;
879 prop_dictionary_t dict
;
882 struct plistref
*pref
;
890 /* Use proplib(3) for userspace/kernel communication */
891 pref
= (struct plistref
*)ap
->a_data
;
892 error
= prop_dictionary_copyin_ioctl(pref
, ap
->a_cmd
, &dict
);
896 po
= prop_dictionary_get(dict
, "command");
897 if (po
== NULL
|| prop_object_type(po
) != PROP_TYPE_STRING
) {
898 log(LOG_DEBUG
, "udev: prop_dictionary_get() failed\n");
899 prop_object_release(dict
);
905 for(i
= 0; cmd_fn
[i
].cmd
!= NULL
; i
++) {
906 if (prop_string_equals_cstring(ps
, cmd_fn
[i
].cmd
))
910 if (cmd_fn
[i
].cmd
!= NULL
) {
911 error
= cmd_fn
[i
].fn(softc
, pref
, ap
->a_cmd
, dict
);
916 //prop_object_release(po);
917 prop_object_release(dict
);
921 * Wait for events based on sequence number. Updates
922 * sequence number for loop.
924 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
925 seq
= *(int *)ap
->a_data
;
927 while (seq
== udev_seq
) {
928 error
= lksleep(&udev_seqwait
, &udev_lk
,
934 *(int *)ap
->a_data
= udev_seq
;
935 lockmgr(&udev_lk
, LK_RELEASE
);
938 error
= ENOTTY
; /* Inappropriate ioctl for device */
946 udev_getdevs_scan_callback(char *name
, cdev_t cdev
, bool is_alias
, void *arg
)
948 struct udev_prop_ctx
*ctx
= arg
;
950 KKASSERT(arg
!= NULL
);
952 if (cdev
->si_dict
== NULL
)
955 if (prop_array_add(ctx
->cdevs
, cdev
->si_dict
) == false) {
962 udev_getdevs_ioctl(struct udev_softc
*softc
, struct plistref
*pref
,
963 u_long cmd
, prop_dictionary_t dict
)
965 prop_dictionary_t odict
;
966 struct udev_prop_ctx ctx
;
970 * Ensure event notification is enabled before doing the devfs
971 * scan so nothing gets missed.
973 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
974 if (softc
->initiated
== 0) {
975 softc
->initiated
= 1;
976 ++udev_initiated_count
;
977 TAILQ_INSERT_HEAD(&udev_evq
, &softc
->marker
, link
);
979 lockmgr(&udev_lk
, LK_RELEASE
);
982 * Devfs scan to build full dictionary.
985 ctx
.cdevs
= prop_array_create();
986 if (ctx
.cdevs
== NULL
) {
988 "udev_getdevs_ioctl: prop_array_create() failed\n");
992 devfs_scan_callback(udev_getdevs_scan_callback
, &ctx
);
994 if (ctx
.error
!= 0) {
995 prop_object_release(ctx
.cdevs
);
999 odict
= prop_dictionary_create();
1000 if (odict
== NULL
) {
1004 if ((prop_dictionary_set(odict
, "array", ctx
.cdevs
)) == 0) {
1006 "udev_getdevs_ioctl: prop_dictionary_set failed\n");
1007 prop_object_release(odict
);
1011 error
= prop_dictionary_copyout_ioctl(pref
, cmd
, odict
);
1013 prop_object_release(odict
);
1024 lockinit(&udev_lk
, "udevlk", 0, LK_CANRECURSE
);
1026 TAILQ_INIT(&udev_evq
);
1027 udev_event_kernel_cache
= objcache_create_simple(M_UDEV
, sizeof(struct udev_event_kernel
));
1033 objcache_destroy(udev_event_kernel_cache
);
1039 udev_dev
= make_autoclone_dev(&udev_dev_ops
, &DEVFS_CLONE_BITMAP(udev
),
1041 UID_ROOT
, GID_WHEEL
, 0600, "udev");
1045 udev_dev_uninit(void)
1047 destroy_dev(udev_dev
);
1050 SYSINIT(subr_udev_register
, SI_SUB_CREATE_INIT
, SI_ORDER_ANY
,
1052 SYSUNINIT(subr_udev_register
, SI_SUB_CREATE_INIT
, SI_ORDER_ANY
,
1054 SYSINIT(subr_udev_dev_register
, SI_SUB_DRIVERS
, SI_ORDER_ANY
,
1055 udev_dev_init
, NULL
);
1056 SYSUNINIT(subr_udev_dev_register
, SI_SUB_DRIVERS
, SI_ORDER_ANY
,
1057 udev_dev_uninit
, NULL
);