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>
36 #include <sys/kernel.h>
40 #include <sys/event.h>
41 #include <sys/vnode.h>
42 #include <sys/malloc.h>
43 #include <sys/objcache.h>
44 #include <sys/ctype.h>
45 #include <sys/syslog.h>
47 #include <sys/devfs.h>
48 #include <libprop/proplib.h>
50 #include <sys/thread2.h>
52 MALLOC_DEFINE(M_UDEV
, "udev", "udev allocs");
53 static struct objcache
*udev_event_kernel_cache
;
55 /* XXX: use UUIDs for identification; would need help from devfs */
57 static cdev_t udev_dev
;
58 static d_open_t udev_dev_open
;
59 static d_close_t udev_dev_close
;
60 static d_read_t udev_dev_read
;
61 static d_kqfilter_t udev_dev_kqfilter
;
62 static d_ioctl_t udev_dev_ioctl
;
63 static d_clone_t udev_dev_clone
;
65 struct udev_prop_ctx
{
70 struct udev_event_kernel
{
72 TAILQ_ENTRY(udev_event_kernel
) link
;
76 TAILQ_ENTRY(udev_softc
) entry
;
82 struct udev_event_kernel marker
; /* udev_evq marker */
87 int (*fn
)(struct udev_softc
*, struct plistref
*,
88 u_long
, prop_dictionary_t
);
91 static int _udev_dict_set_cstr(prop_dictionary_t
, const char *, char *);
92 static int _udev_dict_set_int(prop_dictionary_t
, const char *, int64_t);
93 static int _udev_dict_set_uint(prop_dictionary_t
, const char *, uint64_t);
94 static int _udev_dict_delete_key(prop_dictionary_t
, const char *);
95 static prop_dictionary_t
udev_init_dict_event(cdev_t
, const char *);
96 static int udev_init_dict(cdev_t
);
97 static int udev_destroy_dict(cdev_t
);
98 static void udev_event_insert(int, prop_dictionary_t
);
99 static void udev_clean_events_locked(void);
100 static char *udev_event_externalize(struct udev_event_kernel
*);
101 static void udev_getdevs_scan_callback(char *, cdev_t
, bool, void *);
102 static int udev_getdevs_ioctl(struct udev_softc
*, struct plistref
*,
103 u_long
, prop_dictionary_t
);
104 static void udev_dev_filter_detach(struct knote
*);
105 static int udev_dev_filter_read(struct knote
*, long);
107 static struct dev_ops udev_dev_ops
= {
109 .d_open
= udev_dev_open
,
110 .d_close
= udev_dev_close
,
111 .d_read
= udev_dev_read
,
112 .d_kqfilter
= udev_dev_kqfilter
,
113 .d_ioctl
= udev_dev_ioctl
116 static struct cmd_function cmd_fn
[] = {
117 { .cmd
= "getdevs", .fn
= udev_getdevs_ioctl
},
121 DEVFS_DEFINE_CLONE_BITMAP(udev
);
123 static TAILQ_HEAD(, udev_softc
) udevq
;
124 static TAILQ_HEAD(, udev_event_kernel
) udev_evq
;
125 static struct kqinfo udev_kq
;
126 static struct lock udev_lk
;
127 static int udev_evqlen
;
128 static int udev_initiated_count
;
129 static int udev_open_count
;
130 static int udev_seqwait
;
132 static struct lock udev_dict_lk
= LOCK_INITIALIZER("dict", 0, 0);
135 * Acquire the device's si_dict and lock the device's si_dict field.
136 * If the device does not have an attached dictionary, NULL is returned.
138 * This function must be matched by a udev_put_dict() call regardless of
139 * the return value. The device field is STILL LOCKED even when NULL is
142 * Currently a single global lock is implemented.
144 static prop_dictionary_t
145 udev_get_dict(cdev_t dev
)
147 prop_dictionary_t udict
;
149 lockmgr(&udev_dict_lk
, LK_EXCLUSIVE
|LK_CANRECURSE
);
150 udict
= dev
->si_dict
;
152 prop_object_retain(udict
);
157 * Release the dictionary previously returned by udev_get_dict() and unlock
158 * the device's si_dict field. udict may be NULL.
161 udev_put_dict(cdev_t dev
, prop_dictionary_t udict
)
164 prop_object_release(udict
);
165 lockmgr(&udev_dict_lk
, LK_RELEASE
);
169 _udev_dict_set_cstr(prop_dictionary_t dict
, const char *key
, char *str
)
173 KKASSERT(dict
!= NULL
);
175 ps
= prop_string_create_cstring(str
);
180 if (prop_dictionary_set(dict
, key
, ps
) == false) {
181 prop_object_release(ps
);
185 prop_object_release(ps
);
190 _udev_dict_set_int(prop_dictionary_t dict
, const char *key
, int64_t val
)
194 KKASSERT(dict
!= NULL
);
196 pn
= prop_number_create_integer(val
);
200 if (prop_dictionary_set(dict
, key
, pn
) == false) {
201 prop_object_release(pn
);
205 prop_object_release(pn
);
210 _udev_dict_set_uint(prop_dictionary_t dict
, const char *key
, uint64_t val
)
214 KKASSERT(dict
!= NULL
);
216 pn
= prop_number_create_unsigned_integer(val
);
220 if (prop_dictionary_set(dict
, key
, pn
) == false) {
221 prop_object_release(pn
);
225 prop_object_release(pn
);
230 _udev_dict_delete_key(prop_dictionary_t dict
, const char *key
)
232 KKASSERT(dict
!= NULL
);
234 prop_dictionary_remove(dict
, key
);
240 * Initialize an event dictionary, which contains three parameters to
241 * identify the device referred to (name, devnum, kptr) and the affected key.
243 static prop_dictionary_t
244 udev_init_dict_event(cdev_t dev
, const char *key
)
246 prop_dictionary_t dict
;
250 kptr
= (uint64_t)(uintptr_t)dev
;
251 KKASSERT(dev
!= NULL
);
253 dict
= prop_dictionary_create();
255 log(LOG_DEBUG
, "udev_init_dict_event: prop_dictionary_create() failed\n");
259 if ((error
= _udev_dict_set_cstr(dict
, "name", dev
->si_name
)))
261 if ((error
= _udev_dict_set_uint(dict
, "devnum", dev
->si_inode
)))
263 if ((error
= _udev_dict_set_uint(dict
, "devtype", (dev_dflags(dev
) & D_TYPEMASK
))))
265 if ((error
= _udev_dict_set_uint(dict
, "kptr", kptr
)))
267 if ((error
= _udev_dict_set_cstr(dict
, "key", __DECONST(char *, key
))))
273 prop_object_release(dict
);
278 udev_dict_set_cstr(cdev_t dev
, const char *key
, char *str
)
280 prop_dictionary_t dict
;
281 prop_dictionary_t udict
;
284 KKASSERT(dev
!= NULL
);
286 while ((udict
= udev_get_dict(dev
)) == NULL
) {
287 error
= udev_init_dict(dev
);
288 udev_put_dict(dev
, udict
);
293 /* Queue a key update event */
294 dict
= udev_init_dict_event(dev
, key
);
300 if ((error
= _udev_dict_set_cstr(dict
, "value", str
))) {
301 prop_object_release(dict
);
304 udev_event_insert(UDEV_EV_KEY_UPDATE
, dict
);
305 prop_object_release(dict
);
306 error
= _udev_dict_set_cstr(udict
, key
, str
);
309 udev_put_dict(dev
, udict
);
315 udev_dict_set_int(cdev_t dev
, const char *key
, int64_t val
)
317 prop_dictionary_t dict
;
318 prop_dictionary_t udict
;
321 KKASSERT(dev
!= NULL
);
323 while ((udict
= udev_get_dict(dev
)) == NULL
) {
324 error
= udev_init_dict(dev
);
325 udev_put_dict(dev
, udict
);
330 /* Queue a key update event */
331 dict
= udev_init_dict_event(dev
, key
);
336 if ((error
= _udev_dict_set_int(dict
, "value", val
))) {
337 prop_object_release(dict
);
340 udev_event_insert(UDEV_EV_KEY_UPDATE
, dict
);
341 prop_object_release(dict
);
342 error
= _udev_dict_set_int(udict
, key
, val
);
344 udev_put_dict(dev
, udict
);
350 udev_dict_set_uint(cdev_t dev
, const char *key
, uint64_t val
)
352 prop_dictionary_t dict
;
353 prop_dictionary_t udict
;
356 KKASSERT(dev
!= NULL
);
358 while ((udict
= udev_get_dict(dev
)) == NULL
) {
359 error
= udev_init_dict(dev
);
360 udev_put_dict(dev
, udict
);
365 /* Queue a key update event */
366 dict
= udev_init_dict_event(dev
, key
);
371 if ((error
= _udev_dict_set_uint(dict
, "value", val
))) {
372 prop_object_release(dict
);
375 udev_event_insert(UDEV_EV_KEY_UPDATE
, dict
);
376 prop_object_release(dict
);
377 error
= _udev_dict_set_uint(udict
, key
, val
);
379 udev_put_dict(dev
, udict
);
385 udev_dict_delete_key(cdev_t dev
, const char *key
)
387 prop_dictionary_t dict
;
388 prop_dictionary_t udict
;
391 KKASSERT(dev
!= NULL
);
392 udict
= udev_get_dict(dev
);
398 /* Queue a key removal event */
399 dict
= udev_init_dict_event(dev
, key
);
404 udev_event_insert(UDEV_EV_KEY_REMOVE
, dict
);
405 prop_object_release(dict
);
406 error
= _udev_dict_delete_key(udict
, key
);
408 udev_put_dict(dev
, udict
);
414 * device dictionary access already locked
417 udev_init_dict(cdev_t dev
)
419 prop_dictionary_t dict
;
423 kptr
= (uint64_t)(uintptr_t)dev
;
425 KKASSERT(dev
!= NULL
);
427 if (dev
->si_dict
!= NULL
) {
429 "udev_init_dict: new dict for %s, but has "
430 "dict already (%p)!\n",
431 dev
->si_name
, dev
->si_dict
);
435 dict
= prop_dictionary_create();
438 "udev_init_dict: prop_dictionary_create() failed\n");
442 if ((error
= _udev_dict_set_cstr(dict
, "name", dev
->si_name
)))
444 if ((error
= _udev_dict_set_uint(dict
, "devnum", dev
->si_inode
)))
446 if ((error
= _udev_dict_set_uint(dict
, "kptr", kptr
)))
448 if ((error
= _udev_dict_set_uint(dict
, "devtype",
449 (dev_dflags(dev
) & D_TYPEMASK
)))) {
453 /* XXX: The next 3 are marginallly useful, if at all */
454 if ((error
= _udev_dict_set_uint(dict
, "uid", dev
->si_uid
)))
456 if ((error
= _udev_dict_set_uint(dict
, "gid", dev
->si_gid
)))
458 if ((error
= _udev_dict_set_int(dict
, "mode", dev
->si_perms
)))
461 if ((error
= _udev_dict_set_int(dict
, "major", dev
->si_umajor
)))
463 if ((error
= _udev_dict_set_int(dict
, "minor", dev
->si_uminor
)))
465 if (dev
->si_ops
->head
.name
!= NULL
) {
466 if ((error
= _udev_dict_set_cstr(dict
, "driver",
467 __DECONST(char *, dev
->si_ops
->head
.name
))))
476 prop_object_release(dict
);
481 * device dictionary access already locked
484 udev_destroy_dict(cdev_t dev
)
486 prop_dictionary_t udict
;
488 KKASSERT(dev
!= NULL
);
489 udict
= udev_get_dict(dev
);
491 /* field is now locked, use directly to handle races */
493 prop_object_release(dev
->si_dict
);
496 udev_put_dict(dev
, udict
);
502 udev_event_insert(int ev_type
, prop_dictionary_t dict
)
504 struct udev_event_kernel
*ev
;
505 prop_dictionary_t dict_copy
;
507 /* Only start queing events after client has initiated properly */
508 if (udev_initiated_count
) {
509 dict_copy
= prop_dictionary_copy(dict
);
510 if (dict_copy
== NULL
)
512 ev
= objcache_get(udev_event_kernel_cache
, M_WAITOK
);
513 ev
->ev
.ev_dict
= dict_copy
;
514 ev
->ev
.ev_type
= ev_type
;
516 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
517 TAILQ_INSERT_TAIL(&udev_evq
, ev
, link
);
521 wakeup(&udev_seqwait
);
522 lockmgr(&udev_lk
, LK_RELEASE
);
524 KNOTE(&udev_kq
.ki_note
, 0);
525 } else if (udev_open_count
) {
526 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
529 wakeup(&udev_seqwait
);
530 lockmgr(&udev_lk
, LK_RELEASE
);
531 KNOTE(&udev_kq
.ki_note
, 0);
536 udev_clean_events_locked(void)
538 struct udev_event_kernel
*ev
;
540 while ((ev
= TAILQ_FIRST(&udev_evq
)) &&
541 ev
->ev
.ev_dict
!= NULL
) {
542 TAILQ_REMOVE(&udev_evq
, ev
, link
);
543 objcache_put(udev_event_kernel_cache
, ev
);
549 udev_event_externalize(struct udev_event_kernel
*ev
)
551 prop_dictionary_t dict
;
555 dict
= prop_dictionary_create();
558 "udev_event_externalize: prop_dictionary_create() failed\n");
562 if ((error
= _udev_dict_set_int(dict
, "evtype", ev
->ev
.ev_type
))) {
563 prop_object_release(dict
);
567 if (prop_dictionary_set(dict
, "evdict", ev
->ev
.ev_dict
) == false) {
568 prop_object_release(dict
);
572 prop_object_release(ev
->ev
.ev_dict
);
574 xml
= prop_dictionary_externalize(dict
);
576 prop_object_release(dict
);
582 udev_event_attach(cdev_t dev
, char *name
, int alias
)
584 prop_dictionary_t dict
;
585 prop_dictionary_t udict
;
588 KKASSERT(dev
!= NULL
);
592 udict
= udev_get_dict(dev
);
596 dict
= prop_dictionary_copy(udict
);
600 if ((error
= _udev_dict_set_cstr(dict
, "name", name
))) {
601 prop_object_release(dict
);
605 _udev_dict_set_int(dict
, "alias", 1);
607 udev_event_insert(UDEV_EVENT_ATTACH
, dict
);
608 prop_object_release(dict
);
610 while (udict
== NULL
) {
611 error
= udev_init_dict(dev
);
614 udev_put_dict(dev
, udict
);
615 udict
= udev_get_dict(dev
);
617 _udev_dict_set_int(udict
, "alias", 0);
618 udev_event_insert(UDEV_EVENT_ATTACH
, udict
);
621 udev_put_dict(dev
, udict
);
627 udev_event_detach(cdev_t dev
, char *name
, int alias
)
629 prop_dictionary_t dict
;
630 prop_dictionary_t udict
;
632 KKASSERT(dev
!= NULL
);
634 udict
= udev_get_dict(dev
);
636 dict
= prop_dictionary_copy(udict
);
640 if (_udev_dict_set_cstr(dict
, "name", name
)) {
641 prop_object_release(dict
);
645 _udev_dict_set_int(dict
, "alias", 1);
647 udev_event_insert(UDEV_EVENT_DETACH
, dict
);
648 prop_object_release(dict
);
651 udev_event_insert(UDEV_EVENT_DETACH
, udict
);
655 udev_destroy_dict(dev
);
656 udev_put_dict(dev
, udict
);
662 * Allow multiple opens. Each opener gets a different device.
663 * Messages are replicated to all devices using a marker system.
666 udev_dev_clone(struct dev_clone_args
*ap
)
668 struct udev_softc
*softc
;
671 unit
= devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(udev
), 1000);
677 softc
= kmalloc(sizeof(*softc
), M_UDEV
, M_WAITOK
| M_ZERO
);
679 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
680 TAILQ_INSERT_TAIL(&udevq
, softc
, entry
);
681 lockmgr(&udev_lk
, LK_RELEASE
);
683 softc
->dev
= make_only_dev(&udev_dev_ops
, unit
, ap
->a_cred
->cr_ruid
,
684 0, 0600, "udevs/%d", unit
);
685 softc
->dev
->si_drv1
= softc
;
686 ap
->a_dev
= softc
->dev
;
694 udev_dev_open(struct dev_open_args
*ap
)
696 struct udev_softc
*softc
= ap
->a_head
.a_dev
->si_drv1
;
698 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
699 if (softc
== NULL
|| softc
->opened
) {
700 lockmgr(&udev_lk
, LK_RELEASE
);
705 lockmgr(&udev_lk
, LK_RELEASE
);
711 udev_dev_close(struct dev_close_args
*ap
)
713 struct udev_softc
*softc
= ap
->a_head
.a_dev
->si_drv1
;
715 KKASSERT(softc
->dev
== ap
->a_head
.a_dev
);
716 KKASSERT(softc
->opened
== 1);
718 destroy_dev(ap
->a_head
.a_dev
);
719 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
720 TAILQ_REMOVE(&udevq
, softc
, entry
);
722 if (softc
->initiated
) {
723 TAILQ_REMOVE(&udev_evq
, &softc
->marker
, link
);
724 softc
->initiated
= 0;
725 --udev_initiated_count
;
726 udev_clean_events_locked();
730 ap
->a_head
.a_dev
->si_drv1
= NULL
;
732 lockmgr(&udev_lk
, LK_RELEASE
);
735 * WARNING! devfs_clone_bitmap_put() interacts with the devfs
736 * thread, avoid deadlocks by ensuring we are unlocked
739 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(udev
), softc
->unit
);
742 kfree(softc
, M_UDEV
);
747 static struct filterops udev_dev_read_filtops
=
748 { FILTEROP_ISFD
| FILTEROP_MPSAFE
, NULL
,
749 udev_dev_filter_detach
, udev_dev_filter_read
};
752 udev_dev_kqfilter(struct dev_kqfilter_args
*ap
)
754 struct udev_softc
*softc
= ap
->a_head
.a_dev
->si_drv1
;
755 struct knote
*kn
= ap
->a_kn
;
759 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
761 switch (kn
->kn_filter
) {
763 kn
->kn_fop
= &udev_dev_read_filtops
;
764 kn
->kn_hook
= (caddr_t
)softc
;
767 ap
->a_result
= EOPNOTSUPP
;
768 lockmgr(&udev_lk
, LK_RELEASE
);
772 klist
= &udev_kq
.ki_note
;
773 knote_insert(klist
, kn
);
775 lockmgr(&udev_lk
, LK_RELEASE
);
781 udev_dev_filter_detach(struct knote
*kn
)
785 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
786 klist
= &udev_kq
.ki_note
;
787 knote_remove(klist
, kn
);
788 lockmgr(&udev_lk
, LK_RELEASE
);
792 udev_dev_filter_read(struct knote
*kn
, long hint
)
794 struct udev_softc
*softc
= (void *)kn
->kn_hook
;
795 struct udev_event_kernel
*ev
;
798 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
799 if (softc
->initiated
) {
800 ev
= TAILQ_NEXT(&softc
->marker
, link
);
801 while (ev
&& ev
->ev
.ev_dict
== NULL
)
802 ev
= TAILQ_NEXT(ev
, link
);
806 lockmgr(&udev_lk
, LK_RELEASE
);
812 udev_dev_read(struct dev_read_args
*ap
)
814 struct udev_softc
*softc
= ap
->a_head
.a_dev
->si_drv1
;
815 struct udev_event_kernel
*ev
;
816 struct uio
*uio
= ap
->a_uio
;
821 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
824 * Automatically enable message collection if it has not already
827 if (softc
->initiated
== 0) {
828 softc
->initiated
= 1;
829 ++udev_initiated_count
;
830 TAILQ_INSERT_HEAD(&udev_evq
, &softc
->marker
, link
);
834 * Loop, sleep interruptably until we get an event or signal.
838 if (softc
->initiated
) {
839 ev
= TAILQ_NEXT(&softc
->marker
, link
);
840 while (ev
&& ev
->ev
.ev_dict
== NULL
)
841 ev
= TAILQ_NEXT(ev
, link
);
843 if ((xml
= udev_event_externalize(ev
)) == NULL
) {
847 len
= strlen(xml
) + 1; /* include terminator */
848 if (uio
->uio_resid
< len
)
851 error
= uiomove((caddr_t
)xml
, len
, uio
);
857 TAILQ_REMOVE(&udev_evq
, &softc
->marker
, link
);
858 TAILQ_INSERT_AFTER(&udev_evq
,
859 ev
, &softc
->marker
, link
);
860 udev_clean_events_locked();
864 if (ap
->a_ioflag
& IO_NDELAY
) {
868 if ((error
= lksleep(&udev_evq
, &udev_lk
, PCATCH
, "udevq", 0)))
872 lockmgr(&udev_lk
, LK_RELEASE
);
877 udev_dev_ioctl(struct dev_ioctl_args
*ap
)
879 struct udev_softc
*softc
= ap
->a_head
.a_dev
->si_drv1
;
880 prop_dictionary_t dict
;
883 struct plistref
*pref
;
891 /* Use proplib(3) for userspace/kernel communication */
892 pref
= (struct plistref
*)ap
->a_data
;
893 error
= prop_dictionary_copyin_ioctl(pref
, ap
->a_cmd
, &dict
);
897 po
= prop_dictionary_get(dict
, "command");
898 if (po
== NULL
|| prop_object_type(po
) != PROP_TYPE_STRING
) {
899 log(LOG_DEBUG
, "udev: prop_dictionary_get() failed\n");
900 prop_object_release(dict
);
906 for(i
= 0; cmd_fn
[i
].cmd
!= NULL
; i
++) {
907 if (prop_string_equals_cstring(ps
, cmd_fn
[i
].cmd
))
911 if (cmd_fn
[i
].cmd
!= NULL
) {
912 error
= cmd_fn
[i
].fn(softc
, pref
, ap
->a_cmd
, dict
);
917 //prop_object_release(po);
918 prop_object_release(dict
);
922 * Wait for events based on sequence number. Updates
923 * sequence number for loop.
925 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
926 seq
= *(int *)ap
->a_data
;
928 while (seq
== udev_seq
) {
929 error
= lksleep(&udev_seqwait
, &udev_lk
,
935 *(int *)ap
->a_data
= udev_seq
;
936 lockmgr(&udev_lk
, LK_RELEASE
);
939 error
= ENOTTY
; /* Inappropriate ioctl for device */
947 udev_getdevs_scan_callback(char *name
, cdev_t cdev
, bool is_alias
, void *arg
)
949 struct udev_prop_ctx
*ctx
= arg
;
951 KKASSERT(arg
!= NULL
);
953 if (cdev
->si_dict
== NULL
)
956 if (prop_array_add(ctx
->cdevs
, cdev
->si_dict
) == false) {
963 udev_getdevs_ioctl(struct udev_softc
*softc
, struct plistref
*pref
,
964 u_long cmd
, prop_dictionary_t dict
)
966 prop_dictionary_t odict
;
967 struct udev_prop_ctx ctx
;
971 * Ensure event notification is enabled before doing the devfs
972 * scan so nothing gets missed.
974 lockmgr(&udev_lk
, LK_EXCLUSIVE
);
975 if (softc
->initiated
== 0) {
976 softc
->initiated
= 1;
977 ++udev_initiated_count
;
978 TAILQ_INSERT_HEAD(&udev_evq
, &softc
->marker
, link
);
980 lockmgr(&udev_lk
, LK_RELEASE
);
983 * Devfs scan to build full dictionary.
986 ctx
.cdevs
= prop_array_create();
987 if (ctx
.cdevs
== NULL
) {
989 "udev_getdevs_ioctl: prop_array_create() failed\n");
993 devfs_scan_callback(udev_getdevs_scan_callback
, &ctx
);
995 if (ctx
.error
!= 0) {
996 prop_object_release(ctx
.cdevs
);
1000 odict
= prop_dictionary_create();
1001 if (odict
== NULL
) {
1005 if ((prop_dictionary_set(odict
, "array", ctx
.cdevs
)) == 0) {
1007 "udev_getdevs_ioctl: prop_dictionary_set failed\n");
1008 prop_object_release(odict
);
1012 error
= prop_dictionary_copyout_ioctl(pref
, cmd
, odict
);
1014 prop_object_release(odict
);
1025 lockinit(&udev_lk
, "udevlk", 0, LK_CANRECURSE
);
1027 TAILQ_INIT(&udev_evq
);
1028 udev_event_kernel_cache
= objcache_create_simple(M_UDEV
, sizeof(struct udev_event_kernel
));
1034 objcache_destroy(udev_event_kernel_cache
);
1040 udev_dev
= make_autoclone_dev(&udev_dev_ops
, &DEVFS_CLONE_BITMAP(udev
),
1042 UID_ROOT
, GID_WHEEL
, 0600, "udev");
1046 udev_dev_uninit(void)
1048 destroy_dev(udev_dev
);
1051 SYSINIT(subr_udev_register
, SI_SUB_CREATE_INIT
, SI_ORDER_ANY
,
1053 SYSUNINIT(subr_udev_register
, SI_SUB_CREATE_INIT
, SI_ORDER_ANY
,
1055 SYSINIT(subr_udev_dev_register
, SI_SUB_DRIVERS
, SI_ORDER_ANY
,
1056 udev_dev_init
, NULL
);
1057 SYSUNINIT(subr_udev_dev_register
, SI_SUB_DRIVERS
, SI_ORDER_ANY
,
1058 udev_dev_uninit
, NULL
);