kernel - Throw a global lock around udev dictionary ops
[dragonfly.git] / sys / kern / kern_udev.c
blob468847c7ec40c2696471082b28503c6f2efb6008
1 /*
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
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/buf.h>
39 #include <sys/conf.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>
46 #include <sys/udev.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 {
66 prop_array_t cdevs;
67 int error;
70 struct udev_event_kernel {
71 struct udev_event ev;
72 TAILQ_ENTRY(udev_event_kernel) link;
75 struct udev_softc {
76 TAILQ_ENTRY(udev_softc) entry;
77 int opened;
78 int initiated;
79 int unit;
80 cdev_t dev;
82 struct udev_event_kernel marker; /* udev_evq marker */
85 struct cmd_function {
86 const char *cmd;
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 = {
108 { "udev", 0, 0 },
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},
118 {NULL, NULL}
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;
131 static int udev_seq;
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
140 * returned.
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;
151 if (udict)
152 prop_object_retain(udict);
153 return udict;
157 * Release the dictionary previously returned by udev_get_dict() and unlock
158 * the device's si_dict field. udict may be NULL.
160 static void
161 udev_put_dict(cdev_t dev, prop_dictionary_t udict)
163 if (udict)
164 prop_object_release(udict);
165 lockmgr(&udev_dict_lk, LK_RELEASE);
168 static int
169 _udev_dict_set_cstr(prop_dictionary_t dict, const char *key, char *str)
171 prop_string_t ps;
173 KKASSERT(dict != NULL);
175 ps = prop_string_create_cstring(str);
176 if (ps == NULL) {
177 return ENOMEM;
180 if (prop_dictionary_set(dict, key, ps) == false) {
181 prop_object_release(ps);
182 return ENOMEM;
185 prop_object_release(ps);
186 return 0;
189 static int
190 _udev_dict_set_int(prop_dictionary_t dict, const char *key, int64_t val)
192 prop_number_t pn;
194 KKASSERT(dict != NULL);
196 pn = prop_number_create_integer(val);
197 if (pn == NULL)
198 return ENOMEM;
200 if (prop_dictionary_set(dict, key, pn) == false) {
201 prop_object_release(pn);
202 return ENOMEM;
205 prop_object_release(pn);
206 return 0;
209 static int
210 _udev_dict_set_uint(prop_dictionary_t dict, const char *key, uint64_t val)
212 prop_number_t pn;
214 KKASSERT(dict != NULL);
216 pn = prop_number_create_unsigned_integer(val);
217 if (pn == NULL)
218 return ENOMEM;
220 if (prop_dictionary_set(dict, key, pn) == false) {
221 prop_object_release(pn);
222 return ENOMEM;
225 prop_object_release(pn);
226 return 0;
229 static int
230 _udev_dict_delete_key(prop_dictionary_t dict, const char *key)
232 KKASSERT(dict != NULL);
234 prop_dictionary_remove(dict, key);
236 return 0;
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;
247 uint64_t kptr;
248 int error;
250 kptr = (uint64_t)(uintptr_t)dev;
251 KKASSERT(dev != NULL);
253 dict = prop_dictionary_create();
254 if (dict == NULL) {
255 log(LOG_DEBUG, "udev_init_dict_event: prop_dictionary_create() failed\n");
256 return NULL;
259 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
260 goto error_out;
261 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
262 goto error_out;
263 if ((error = _udev_dict_set_uint(dict, "devtype", (dev_dflags(dev) & D_TYPEMASK))))
264 goto error_out;
265 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
266 goto error_out;
267 if ((error = _udev_dict_set_cstr(dict, "key", __DECONST(char *, key))))
268 goto error_out;
270 return dict;
272 error_out:
273 prop_object_release(dict);
274 return NULL;
278 udev_dict_set_cstr(cdev_t dev, const char *key, char *str)
280 prop_dictionary_t dict;
281 prop_dictionary_t udict;
282 int error;
284 KKASSERT(dev != NULL);
286 while ((udict = udev_get_dict(dev)) == NULL) {
287 error = udev_init_dict(dev);
288 udev_put_dict(dev, udict);
289 if (error)
290 return -1;
293 /* Queue a key update event */
294 dict = udev_init_dict_event(dev, key);
295 if (dict == NULL) {
296 error = ENOMEM;
297 goto errout;
300 if ((error = _udev_dict_set_cstr(dict, "value", str))) {
301 prop_object_release(dict);
302 goto errout;
304 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
305 prop_object_release(dict);
306 error = _udev_dict_set_cstr(udict, key, str);
308 errout:
309 udev_put_dict(dev, udict);
311 return error;
315 udev_dict_set_int(cdev_t dev, const char *key, int64_t val)
317 prop_dictionary_t dict;
318 prop_dictionary_t udict;
319 int error;
321 KKASSERT(dev != NULL);
323 while ((udict = udev_get_dict(dev)) == NULL) {
324 error = udev_init_dict(dev);
325 udev_put_dict(dev, udict);
326 if (error)
327 return -1;
330 /* Queue a key update event */
331 dict = udev_init_dict_event(dev, key);
332 if (dict == NULL) {
333 error = ENOMEM;
334 goto errout;
336 if ((error = _udev_dict_set_int(dict, "value", val))) {
337 prop_object_release(dict);
338 goto errout;
340 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
341 prop_object_release(dict);
342 error = _udev_dict_set_int(udict, key, val);
343 errout:
344 udev_put_dict(dev, udict);
346 return error;
350 udev_dict_set_uint(cdev_t dev, const char *key, uint64_t val)
352 prop_dictionary_t dict;
353 prop_dictionary_t udict;
354 int error;
356 KKASSERT(dev != NULL);
358 while ((udict = udev_get_dict(dev)) == NULL) {
359 error = udev_init_dict(dev);
360 udev_put_dict(dev, udict);
361 if (error)
362 return -1;
365 /* Queue a key update event */
366 dict = udev_init_dict_event(dev, key);
367 if (dict == NULL) {
368 error = ENOMEM;
369 goto errout;
371 if ((error = _udev_dict_set_uint(dict, "value", val))) {
372 prop_object_release(dict);
373 goto errout;
375 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
376 prop_object_release(dict);
377 error = _udev_dict_set_uint(udict, key, val);
378 errout:
379 udev_put_dict(dev, udict);
381 return error;
385 udev_dict_delete_key(cdev_t dev, const char *key)
387 prop_dictionary_t dict;
388 prop_dictionary_t udict;
389 int error;
391 KKASSERT(dev != NULL);
392 udict = udev_get_dict(dev);
393 if (udict == NULL) {
394 error = ENOMEM;
395 goto errout;
398 /* Queue a key removal event */
399 dict = udev_init_dict_event(dev, key);
400 if (dict == NULL) {
401 error = ENOMEM;
402 goto errout;
404 udev_event_insert(UDEV_EV_KEY_REMOVE, dict);
405 prop_object_release(dict);
406 error = _udev_dict_delete_key(udict, key);
407 errout:
408 udev_put_dict(dev, udict);
410 return error;
414 * device dictionary access already locked
416 static int
417 udev_init_dict(cdev_t dev)
419 prop_dictionary_t dict;
420 uint64_t kptr;
421 int error;
423 kptr = (uint64_t)(uintptr_t)dev;
425 KKASSERT(dev != NULL);
427 if (dev->si_dict != NULL) {
428 log(LOG_DEBUG,
429 "udev_init_dict: new dict for %s, but has "
430 "dict already (%p)!\n",
431 dev->si_name, dev->si_dict);
432 return 0;
435 dict = prop_dictionary_create();
436 if (dict == NULL) {
437 log(LOG_DEBUG,
438 "udev_init_dict: prop_dictionary_create() failed\n");
439 return ENOMEM;
442 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
443 goto error_out;
444 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
445 goto error_out;
446 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
447 goto error_out;
448 if ((error = _udev_dict_set_uint(dict, "devtype",
449 (dev_dflags(dev) & D_TYPEMASK)))) {
450 goto error_out;
453 /* XXX: The next 3 are marginallly useful, if at all */
454 if ((error = _udev_dict_set_uint(dict, "uid", dev->si_uid)))
455 goto error_out;
456 if ((error = _udev_dict_set_uint(dict, "gid", dev->si_gid)))
457 goto error_out;
458 if ((error = _udev_dict_set_int(dict, "mode", dev->si_perms)))
459 goto error_out;
461 if ((error = _udev_dict_set_int(dict, "major", dev->si_umajor)))
462 goto error_out;
463 if ((error = _udev_dict_set_int(dict, "minor", dev->si_uminor)))
464 goto error_out;
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))))
468 goto error_out;
471 dev->si_dict = dict;
472 return 0;
474 error_out:
475 dev->si_dict = NULL;
476 prop_object_release(dict);
477 return error;
481 * device dictionary access already locked
483 static int
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 */
492 if (dev->si_dict) {
493 prop_object_release(dev->si_dict);
494 dev->si_dict = NULL;
496 udev_put_dict(dev, udict);
498 return 0;
501 static void
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)
511 return;
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);
518 ++udev_evqlen;
519 ++udev_seq;
520 if (udev_seqwait)
521 wakeup(&udev_seqwait);
522 lockmgr(&udev_lk, LK_RELEASE);
523 wakeup(&udev_evq);
524 KNOTE(&udev_kq.ki_note, 0);
525 } else if (udev_open_count) {
526 lockmgr(&udev_lk, LK_EXCLUSIVE);
527 ++udev_seq;
528 if (udev_seqwait)
529 wakeup(&udev_seqwait);
530 lockmgr(&udev_lk, LK_RELEASE);
531 KNOTE(&udev_kq.ki_note, 0);
535 static void
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);
544 --udev_evqlen;
548 static char *
549 udev_event_externalize(struct udev_event_kernel *ev)
551 prop_dictionary_t dict;
552 char *xml;
553 int error;
555 dict = prop_dictionary_create();
556 if (dict == NULL) {
557 log(LOG_DEBUG,
558 "udev_event_externalize: prop_dictionary_create() failed\n");
559 return NULL;
562 if ((error = _udev_dict_set_int(dict, "evtype", ev->ev.ev_type))) {
563 prop_object_release(dict);
564 return NULL;
567 if (prop_dictionary_set(dict, "evdict", ev->ev.ev_dict) == false) {
568 prop_object_release(dict);
569 return NULL;
572 prop_object_release(ev->ev.ev_dict);
574 xml = prop_dictionary_externalize(dict);
576 prop_object_release(dict);
578 return xml;
582 udev_event_attach(cdev_t dev, char *name, int alias)
584 prop_dictionary_t dict;
585 prop_dictionary_t udict;
586 int error;
588 KKASSERT(dev != NULL);
590 error = ENOMEM;
592 udict = udev_get_dict(dev);
593 if (alias) {
594 if (udict == NULL)
595 goto error_out;
596 dict = prop_dictionary_copy(udict);
597 if (dict == NULL)
598 goto error_out;
600 if ((error = _udev_dict_set_cstr(dict, "name", name))) {
601 prop_object_release(dict);
602 goto error_out;
605 _udev_dict_set_int(dict, "alias", 1);
607 udev_event_insert(UDEV_EVENT_ATTACH, dict);
608 prop_object_release(dict);
609 } else {
610 while (udict == NULL) {
611 error = udev_init_dict(dev);
612 if (error)
613 goto error_out;
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);
620 error_out:
621 udev_put_dict(dev, udict);
623 return error;
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);
635 if (alias) {
636 dict = prop_dictionary_copy(udict);
637 if (dict == NULL)
638 goto error_out;
640 if (_udev_dict_set_cstr(dict, "name", name)) {
641 prop_object_release(dict);
642 goto error_out;
645 _udev_dict_set_int(dict, "alias", 1);
647 udev_event_insert(UDEV_EVENT_DETACH, dict);
648 prop_object_release(dict);
649 } else {
650 if (udict)
651 udev_event_insert(UDEV_EVENT_DETACH, udict);
654 error_out:
655 udev_destroy_dict(dev);
656 udev_put_dict(dev, udict);
658 return 0;
662 * Allow multiple opens. Each opener gets a different device.
663 * Messages are replicated to all devices using a marker system.
665 static int
666 udev_dev_clone(struct dev_clone_args *ap)
668 struct udev_softc *softc;
669 int unit;
671 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(udev), 1000);
672 if (unit < 0) {
673 ap->a_dev = NULL;
674 return 1;
677 softc = kmalloc(sizeof(*softc), M_UDEV, M_WAITOK | M_ZERO);
678 softc->unit = unit;
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;
687 return 0;
691 * dev stuff
693 static int
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);
701 return EBUSY;
703 softc->opened = 1;
704 ++udev_open_count;
705 lockmgr(&udev_lk, LK_RELEASE);
707 return 0;
710 static int
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();
728 softc->opened = 0;
729 softc->dev = NULL;
730 ap->a_head.a_dev->si_drv1 = NULL;
731 --udev_open_count;
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
737 * before calling.
739 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(udev), softc->unit);
740 wakeup(&udev_evq);
742 kfree(softc, M_UDEV);
744 return 0;
747 static struct filterops udev_dev_read_filtops =
748 { FILTEROP_ISFD | FILTEROP_MPSAFE, NULL,
749 udev_dev_filter_detach, udev_dev_filter_read };
751 static int
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;
756 struct klist *klist;
758 ap->a_result = 0;
759 lockmgr(&udev_lk, LK_EXCLUSIVE);
761 switch (kn->kn_filter) {
762 case EVFILT_READ:
763 kn->kn_fop = &udev_dev_read_filtops;
764 kn->kn_hook = (caddr_t)softc;
765 break;
766 default:
767 ap->a_result = EOPNOTSUPP;
768 lockmgr(&udev_lk, LK_RELEASE);
769 return (0);
772 klist = &udev_kq.ki_note;
773 knote_insert(klist, kn);
775 lockmgr(&udev_lk, LK_RELEASE);
777 return (0);
780 static void
781 udev_dev_filter_detach(struct knote *kn)
783 struct klist *klist;
785 lockmgr(&udev_lk, LK_EXCLUSIVE);
786 klist = &udev_kq.ki_note;
787 knote_remove(klist, kn);
788 lockmgr(&udev_lk, LK_RELEASE);
791 static int
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;
796 int ready = 0;
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);
803 if (ev)
804 ready = 1;
806 lockmgr(&udev_lk, LK_RELEASE);
808 return (ready);
811 static int
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;
817 char *xml;
818 size_t len;
819 int error;
821 lockmgr(&udev_lk, LK_EXCLUSIVE);
824 * Automatically enable message collection if it has not already
825 * been enabled.
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.
836 error = 0;
837 for (;;) {
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);
842 if (ev) {
843 if ((xml = udev_event_externalize(ev)) == NULL) {
844 error = ENOMEM;
845 break;
847 len = strlen(xml) + 1; /* include terminator */
848 if (uio->uio_resid < len)
849 error = ENOMEM;
850 else
851 error = uiomove((caddr_t)xml, len, uio);
852 kfree(xml, M_TEMP);
855 * Move the marker
857 TAILQ_REMOVE(&udev_evq, &softc->marker, link);
858 TAILQ_INSERT_AFTER(&udev_evq,
859 ev, &softc->marker, link);
860 udev_clean_events_locked();
861 break;
864 if (ap->a_ioflag & IO_NDELAY) {
865 error = EWOULDBLOCK;
866 break;
868 if ((error = lksleep(&udev_evq, &udev_lk, PCATCH, "udevq", 0)))
869 break;
872 lockmgr(&udev_lk, LK_RELEASE);
873 return error;
876 static int
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;
881 prop_object_t po;
882 prop_string_t ps;
883 struct plistref *pref;
884 int i, error;
885 int seq;
887 error = 0;
889 switch(ap->a_cmd) {
890 case UDEVPROP:
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);
894 if (error)
895 return error;
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);
901 return EINVAL;
904 ps = po;
905 /* Handle cmd */
906 for(i = 0; cmd_fn[i].cmd != NULL; i++) {
907 if (prop_string_equals_cstring(ps, cmd_fn[i].cmd))
908 break;
911 if (cmd_fn[i].cmd != NULL) {
912 error = cmd_fn[i].fn(softc, pref, ap->a_cmd, dict);
913 } else {
914 error = EINVAL;
917 //prop_object_release(po);
918 prop_object_release(dict);
919 break;
920 case UDEVWAIT:
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;
927 ++udev_seqwait;
928 while (seq == udev_seq) {
929 error = lksleep(&udev_seqwait, &udev_lk,
930 PCATCH, "udevw", 0);
931 if (error)
932 break;
934 --udev_seqwait;
935 *(int *)ap->a_data = udev_seq;
936 lockmgr(&udev_lk, LK_RELEASE);
937 break;
938 default:
939 error = ENOTTY; /* Inappropriate ioctl for device */
940 break;
943 return(error);
946 static void
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)
954 return;
956 if (prop_array_add(ctx->cdevs, cdev->si_dict) == false) {
957 ctx->error = EINVAL;
958 return;
962 static int
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;
968 int error;
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.
985 ctx.error = 0;
986 ctx.cdevs = prop_array_create();
987 if (ctx.cdevs == NULL) {
988 log(LOG_DEBUG,
989 "udev_getdevs_ioctl: prop_array_create() failed\n");
990 return EINVAL;
993 devfs_scan_callback(udev_getdevs_scan_callback, &ctx);
995 if (ctx.error != 0) {
996 prop_object_release(ctx.cdevs);
997 return (ctx.error);
1000 odict = prop_dictionary_create();
1001 if (odict == NULL) {
1002 return ENOMEM;
1005 if ((prop_dictionary_set(odict, "array", ctx.cdevs)) == 0) {
1006 log(LOG_DEBUG,
1007 "udev_getdevs_ioctl: prop_dictionary_set failed\n");
1008 prop_object_release(odict);
1009 return ENOMEM;
1012 error = prop_dictionary_copyout_ioctl(pref, cmd, odict);
1014 prop_object_release(odict);
1015 return error;
1020 * SYSINIT stuff
1022 static void
1023 udev_init(void)
1025 lockinit(&udev_lk, "udevlk", 0, LK_CANRECURSE);
1026 TAILQ_INIT(&udevq);
1027 TAILQ_INIT(&udev_evq);
1028 udev_event_kernel_cache = objcache_create_simple(M_UDEV, sizeof(struct udev_event_kernel));
1031 static void
1032 udev_uninit(void)
1034 objcache_destroy(udev_event_kernel_cache);
1037 static void
1038 udev_dev_init(void)
1040 udev_dev = make_autoclone_dev(&udev_dev_ops, &DEVFS_CLONE_BITMAP(udev),
1041 udev_dev_clone,
1042 UID_ROOT, GID_WHEEL, 0600, "udev");
1045 static void
1046 udev_dev_uninit(void)
1048 destroy_dev(udev_dev);
1051 SYSINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY,
1052 udev_init, NULL);
1053 SYSUNINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY,
1054 udev_uninit, NULL);
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);