kernel/vm: Rename *_putpages()'s 'sync' argument to 'flags'.
[dragonfly.git] / sys / kern / kern_udev.c
blob9aa239cedd45574b1cd375c9ba2d909b862fa8ca
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/uio.h>
37 #include <sys/kernel.h>
38 #include <sys/proc.h>
39 #include <sys/buf.h>
40 #include <sys/conf.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>
47 #include <sys/udev.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 {
65 prop_array_t cdevs;
66 int error;
69 struct udev_event_kernel {
70 struct udev_event ev;
71 TAILQ_ENTRY(udev_event_kernel) link;
74 struct udev_softc {
75 TAILQ_ENTRY(udev_softc) entry;
76 int opened;
77 int initiated;
78 int unit;
79 cdev_t dev;
81 struct udev_event_kernel marker; /* udev_evq marker */
84 struct cmd_function {
85 const char *cmd;
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 = {
107 { "udev", 0, 0 },
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},
117 {NULL, NULL}
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;
130 static int udev_seq;
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
139 * returned.
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;
150 if (udict)
151 prop_object_retain(udict);
152 return udict;
156 * Release the dictionary previously returned by udev_get_dict() and unlock
157 * the device's si_dict field. udict may be NULL.
159 static void
160 udev_put_dict(cdev_t dev, prop_dictionary_t udict)
162 if (udict)
163 prop_object_release(udict);
164 lockmgr(&udev_dict_lk, LK_RELEASE);
167 static int
168 _udev_dict_set_cstr(prop_dictionary_t dict, const char *key, char *str)
170 prop_string_t ps;
172 KKASSERT(dict != NULL);
174 ps = prop_string_create_cstring(str);
175 if (ps == NULL) {
176 return ENOMEM;
179 if (prop_dictionary_set(dict, key, ps) == false) {
180 prop_object_release(ps);
181 return ENOMEM;
184 prop_object_release(ps);
185 return 0;
188 static int
189 _udev_dict_set_int(prop_dictionary_t dict, const char *key, int64_t val)
191 prop_number_t pn;
193 KKASSERT(dict != NULL);
195 pn = prop_number_create_integer(val);
196 if (pn == NULL)
197 return ENOMEM;
199 if (prop_dictionary_set(dict, key, pn) == false) {
200 prop_object_release(pn);
201 return ENOMEM;
204 prop_object_release(pn);
205 return 0;
208 static int
209 _udev_dict_set_uint(prop_dictionary_t dict, const char *key, uint64_t val)
211 prop_number_t pn;
213 KKASSERT(dict != NULL);
215 pn = prop_number_create_unsigned_integer(val);
216 if (pn == NULL)
217 return ENOMEM;
219 if (prop_dictionary_set(dict, key, pn) == false) {
220 prop_object_release(pn);
221 return ENOMEM;
224 prop_object_release(pn);
225 return 0;
228 static int
229 _udev_dict_delete_key(prop_dictionary_t dict, const char *key)
231 KKASSERT(dict != NULL);
233 prop_dictionary_remove(dict, key);
235 return 0;
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;
246 uint64_t kptr;
247 int error;
249 kptr = (uint64_t)(uintptr_t)dev;
250 KKASSERT(dev != NULL);
252 dict = prop_dictionary_create();
253 if (dict == NULL) {
254 log(LOG_DEBUG, "udev_init_dict_event: prop_dictionary_create() failed\n");
255 return NULL;
258 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
259 goto error_out;
260 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
261 goto error_out;
262 if ((error = _udev_dict_set_uint(dict, "devtype", (dev_dflags(dev) & D_TYPEMASK))))
263 goto error_out;
264 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
265 goto error_out;
266 if ((error = _udev_dict_set_cstr(dict, "key", __DECONST(char *, key))))
267 goto error_out;
269 return dict;
271 error_out:
272 prop_object_release(dict);
273 return NULL;
277 udev_dict_set_cstr(cdev_t dev, const char *key, char *str)
279 prop_dictionary_t dict;
280 prop_dictionary_t udict;
281 int error;
283 KKASSERT(dev != NULL);
285 while ((udict = udev_get_dict(dev)) == NULL) {
286 error = udev_init_dict(dev);
287 udev_put_dict(dev, udict);
288 if (error)
289 return -1;
292 /* Queue a key update event */
293 dict = udev_init_dict_event(dev, key);
294 if (dict == NULL) {
295 error = ENOMEM;
296 goto errout;
299 if ((error = _udev_dict_set_cstr(dict, "value", str))) {
300 prop_object_release(dict);
301 goto errout;
303 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
304 prop_object_release(dict);
305 error = _udev_dict_set_cstr(udict, key, str);
307 errout:
308 udev_put_dict(dev, udict);
310 return error;
314 udev_dict_set_int(cdev_t dev, const char *key, int64_t val)
316 prop_dictionary_t dict;
317 prop_dictionary_t udict;
318 int error;
320 KKASSERT(dev != NULL);
322 while ((udict = udev_get_dict(dev)) == NULL) {
323 error = udev_init_dict(dev);
324 udev_put_dict(dev, udict);
325 if (error)
326 return -1;
329 /* Queue a key update event */
330 dict = udev_init_dict_event(dev, key);
331 if (dict == NULL) {
332 error = ENOMEM;
333 goto errout;
335 if ((error = _udev_dict_set_int(dict, "value", val))) {
336 prop_object_release(dict);
337 goto errout;
339 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
340 prop_object_release(dict);
341 error = _udev_dict_set_int(udict, key, val);
342 errout:
343 udev_put_dict(dev, udict);
345 return error;
349 udev_dict_set_uint(cdev_t dev, const char *key, uint64_t val)
351 prop_dictionary_t dict;
352 prop_dictionary_t udict;
353 int error;
355 KKASSERT(dev != NULL);
357 while ((udict = udev_get_dict(dev)) == NULL) {
358 error = udev_init_dict(dev);
359 udev_put_dict(dev, udict);
360 if (error)
361 return -1;
364 /* Queue a key update event */
365 dict = udev_init_dict_event(dev, key);
366 if (dict == NULL) {
367 error = ENOMEM;
368 goto errout;
370 if ((error = _udev_dict_set_uint(dict, "value", val))) {
371 prop_object_release(dict);
372 goto errout;
374 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
375 prop_object_release(dict);
376 error = _udev_dict_set_uint(udict, key, val);
377 errout:
378 udev_put_dict(dev, udict);
380 return error;
384 udev_dict_delete_key(cdev_t dev, const char *key)
386 prop_dictionary_t dict;
387 prop_dictionary_t udict;
388 int error;
390 KKASSERT(dev != NULL);
391 udict = udev_get_dict(dev);
392 if (udict == NULL) {
393 error = ENOMEM;
394 goto errout;
397 /* Queue a key removal event */
398 dict = udev_init_dict_event(dev, key);
399 if (dict == NULL) {
400 error = ENOMEM;
401 goto errout;
403 udev_event_insert(UDEV_EV_KEY_REMOVE, dict);
404 prop_object_release(dict);
405 error = _udev_dict_delete_key(udict, key);
406 errout:
407 udev_put_dict(dev, udict);
409 return error;
413 * device dictionary access already locked
415 static int
416 udev_init_dict(cdev_t dev)
418 prop_dictionary_t dict;
419 uint64_t kptr;
420 int error;
422 kptr = (uint64_t)(uintptr_t)dev;
424 KKASSERT(dev != NULL);
426 if (dev->si_dict != NULL) {
427 log(LOG_DEBUG,
428 "udev_init_dict: new dict for %s, but has "
429 "dict already (%p)!\n",
430 dev->si_name, dev->si_dict);
431 return 0;
434 dict = prop_dictionary_create();
435 if (dict == NULL) {
436 log(LOG_DEBUG,
437 "udev_init_dict: prop_dictionary_create() failed\n");
438 return ENOMEM;
441 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
442 goto error_out;
443 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
444 goto error_out;
445 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
446 goto error_out;
447 if ((error = _udev_dict_set_uint(dict, "devtype",
448 (dev_dflags(dev) & D_TYPEMASK)))) {
449 goto error_out;
452 /* XXX: The next 3 are marginallly useful, if at all */
453 if ((error = _udev_dict_set_uint(dict, "uid", dev->si_uid)))
454 goto error_out;
455 if ((error = _udev_dict_set_uint(dict, "gid", dev->si_gid)))
456 goto error_out;
457 if ((error = _udev_dict_set_int(dict, "mode", dev->si_perms)))
458 goto error_out;
460 if ((error = _udev_dict_set_int(dict, "major", dev->si_umajor)))
461 goto error_out;
462 if ((error = _udev_dict_set_int(dict, "minor", dev->si_uminor)))
463 goto error_out;
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))))
467 goto error_out;
470 dev->si_dict = dict;
471 return 0;
473 error_out:
474 dev->si_dict = NULL;
475 prop_object_release(dict);
476 return error;
480 * device dictionary access already locked
482 static int
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 */
491 if (dev->si_dict) {
492 prop_object_release(dev->si_dict);
493 dev->si_dict = NULL;
495 udev_put_dict(dev, udict);
497 return 0;
500 static void
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)
510 return;
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);
517 ++udev_evqlen;
518 ++udev_seq;
519 if (udev_seqwait)
520 wakeup(&udev_seqwait);
521 lockmgr(&udev_lk, LK_RELEASE);
522 wakeup(&udev_evq);
523 KNOTE(&udev_kq.ki_note, 0);
524 } else if (udev_open_count) {
525 lockmgr(&udev_lk, LK_EXCLUSIVE);
526 ++udev_seq;
527 if (udev_seqwait)
528 wakeup(&udev_seqwait);
529 lockmgr(&udev_lk, LK_RELEASE);
530 KNOTE(&udev_kq.ki_note, 0);
534 static void
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);
543 --udev_evqlen;
547 static char *
548 udev_event_externalize(struct udev_event_kernel *ev)
550 prop_dictionary_t dict;
551 char *xml;
552 int error;
554 dict = prop_dictionary_create();
555 if (dict == NULL) {
556 log(LOG_DEBUG,
557 "udev_event_externalize: prop_dictionary_create() failed\n");
558 return NULL;
561 if ((error = _udev_dict_set_int(dict, "evtype", ev->ev.ev_type))) {
562 prop_object_release(dict);
563 return NULL;
566 if (prop_dictionary_set(dict, "evdict", ev->ev.ev_dict) == false) {
567 prop_object_release(dict);
568 return NULL;
571 prop_object_release(ev->ev.ev_dict);
573 xml = prop_dictionary_externalize(dict);
575 prop_object_release(dict);
577 return xml;
581 udev_event_attach(cdev_t dev, char *name, int alias)
583 prop_dictionary_t dict;
584 prop_dictionary_t udict;
585 int error;
587 KKASSERT(dev != NULL);
589 error = ENOMEM;
591 udict = udev_get_dict(dev);
592 if (alias) {
593 if (udict == NULL)
594 goto error_out;
595 dict = prop_dictionary_copy(udict);
596 if (dict == NULL)
597 goto error_out;
599 if ((error = _udev_dict_set_cstr(dict, "name", name))) {
600 prop_object_release(dict);
601 goto error_out;
604 _udev_dict_set_int(dict, "alias", 1);
606 udev_event_insert(UDEV_EVENT_ATTACH, dict);
607 prop_object_release(dict);
608 } else {
609 while (udict == NULL) {
610 error = udev_init_dict(dev);
611 if (error)
612 goto error_out;
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);
619 error_out:
620 udev_put_dict(dev, udict);
622 return error;
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);
634 if (alias) {
635 dict = prop_dictionary_copy(udict);
636 if (dict == NULL)
637 goto error_out;
639 if (_udev_dict_set_cstr(dict, "name", name)) {
640 prop_object_release(dict);
641 goto error_out;
644 _udev_dict_set_int(dict, "alias", 1);
646 udev_event_insert(UDEV_EVENT_DETACH, dict);
647 prop_object_release(dict);
648 } else {
649 if (udict)
650 udev_event_insert(UDEV_EVENT_DETACH, udict);
653 error_out:
654 udev_destroy_dict(dev);
655 udev_put_dict(dev, udict);
657 return 0;
661 * Allow multiple opens. Each opener gets a different device.
662 * Messages are replicated to all devices using a marker system.
664 static int
665 udev_dev_clone(struct dev_clone_args *ap)
667 struct udev_softc *softc;
668 int unit;
670 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(udev), 1000);
671 if (unit < 0) {
672 ap->a_dev = NULL;
673 return 1;
676 softc = kmalloc(sizeof(*softc), M_UDEV, M_WAITOK | M_ZERO);
677 softc->unit = unit;
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;
686 return 0;
690 * dev stuff
692 static int
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);
700 return EBUSY;
702 softc->opened = 1;
703 ++udev_open_count;
704 lockmgr(&udev_lk, LK_RELEASE);
706 return 0;
709 static int
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();
727 softc->opened = 0;
728 softc->dev = NULL;
729 ap->a_head.a_dev->si_drv1 = NULL;
730 --udev_open_count;
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
736 * before calling.
738 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(udev), softc->unit);
739 wakeup(&udev_evq);
741 kfree(softc, M_UDEV);
743 return 0;
746 static struct filterops udev_dev_read_filtops =
747 { FILTEROP_ISFD | FILTEROP_MPSAFE, NULL,
748 udev_dev_filter_detach, udev_dev_filter_read };
750 static int
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;
755 struct klist *klist;
757 ap->a_result = 0;
758 lockmgr(&udev_lk, LK_EXCLUSIVE);
760 switch (kn->kn_filter) {
761 case EVFILT_READ:
762 kn->kn_fop = &udev_dev_read_filtops;
763 kn->kn_hook = (caddr_t)softc;
764 break;
765 default:
766 ap->a_result = EOPNOTSUPP;
767 lockmgr(&udev_lk, LK_RELEASE);
768 return (0);
771 klist = &udev_kq.ki_note;
772 knote_insert(klist, kn);
774 lockmgr(&udev_lk, LK_RELEASE);
776 return (0);
779 static void
780 udev_dev_filter_detach(struct knote *kn)
782 struct klist *klist;
784 lockmgr(&udev_lk, LK_EXCLUSIVE);
785 klist = &udev_kq.ki_note;
786 knote_remove(klist, kn);
787 lockmgr(&udev_lk, LK_RELEASE);
790 static int
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;
795 int ready = 0;
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);
802 if (ev)
803 ready = 1;
805 lockmgr(&udev_lk, LK_RELEASE);
807 return (ready);
810 static int
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;
816 char *xml;
817 size_t len;
818 int error;
820 lockmgr(&udev_lk, LK_EXCLUSIVE);
823 * Automatically enable message collection if it has not already
824 * been enabled.
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.
835 error = 0;
836 for (;;) {
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);
841 if (ev) {
842 if ((xml = udev_event_externalize(ev)) == NULL) {
843 error = ENOMEM;
844 break;
846 len = strlen(xml) + 1; /* include terminator */
847 if (uio->uio_resid < len)
848 error = ENOMEM;
849 else
850 error = uiomove((caddr_t)xml, len, uio);
851 kfree(xml, M_TEMP);
854 * Move the marker
856 TAILQ_REMOVE(&udev_evq, &softc->marker, link);
857 TAILQ_INSERT_AFTER(&udev_evq,
858 ev, &softc->marker, link);
859 udev_clean_events_locked();
860 break;
863 if (ap->a_ioflag & IO_NDELAY) {
864 error = EWOULDBLOCK;
865 break;
867 if ((error = lksleep(&udev_evq, &udev_lk, PCATCH, "udevq", 0)))
868 break;
871 lockmgr(&udev_lk, LK_RELEASE);
872 return error;
875 static int
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;
880 prop_object_t po;
881 prop_string_t ps;
882 struct plistref *pref;
883 int i, error;
884 int seq;
886 error = 0;
888 switch(ap->a_cmd) {
889 case UDEVPROP:
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);
893 if (error)
894 return error;
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);
900 return EINVAL;
903 ps = po;
904 /* Handle cmd */
905 for(i = 0; cmd_fn[i].cmd != NULL; i++) {
906 if (prop_string_equals_cstring(ps, cmd_fn[i].cmd))
907 break;
910 if (cmd_fn[i].cmd != NULL) {
911 error = cmd_fn[i].fn(softc, pref, ap->a_cmd, dict);
912 } else {
913 error = EINVAL;
916 //prop_object_release(po);
917 prop_object_release(dict);
918 break;
919 case UDEVWAIT:
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;
926 ++udev_seqwait;
927 while (seq == udev_seq) {
928 error = lksleep(&udev_seqwait, &udev_lk,
929 PCATCH, "udevw", 0);
930 if (error)
931 break;
933 --udev_seqwait;
934 *(int *)ap->a_data = udev_seq;
935 lockmgr(&udev_lk, LK_RELEASE);
936 break;
937 default:
938 error = ENOTTY; /* Inappropriate ioctl for device */
939 break;
942 return(error);
945 static void
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)
953 return;
955 if (prop_array_add(ctx->cdevs, cdev->si_dict) == false) {
956 ctx->error = EINVAL;
957 return;
961 static int
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;
967 int error;
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.
984 ctx.error = 0;
985 ctx.cdevs = prop_array_create();
986 if (ctx.cdevs == NULL) {
987 log(LOG_DEBUG,
988 "udev_getdevs_ioctl: prop_array_create() failed\n");
989 return EINVAL;
992 devfs_scan_callback(udev_getdevs_scan_callback, &ctx);
994 if (ctx.error != 0) {
995 prop_object_release(ctx.cdevs);
996 return (ctx.error);
999 odict = prop_dictionary_create();
1000 if (odict == NULL) {
1001 return ENOMEM;
1004 if ((prop_dictionary_set(odict, "array", ctx.cdevs)) == 0) {
1005 log(LOG_DEBUG,
1006 "udev_getdevs_ioctl: prop_dictionary_set failed\n");
1007 prop_object_release(odict);
1008 return ENOMEM;
1011 error = prop_dictionary_copyout_ioctl(pref, cmd, odict);
1013 prop_object_release(odict);
1014 return error;
1019 * SYSINIT stuff
1021 static void
1022 udev_init(void)
1024 lockinit(&udev_lk, "udevlk", 0, LK_CANRECURSE);
1025 TAILQ_INIT(&udevq);
1026 TAILQ_INIT(&udev_evq);
1027 udev_event_kernel_cache = objcache_create_simple(M_UDEV, sizeof(struct udev_event_kernel));
1030 static void
1031 udev_uninit(void)
1033 objcache_destroy(udev_event_kernel_cache);
1036 static void
1037 udev_dev_init(void)
1039 udev_dev = make_autoclone_dev(&udev_dev_ops, &DEVFS_CLONE_BITMAP(udev),
1040 udev_dev_clone,
1041 UID_ROOT, GID_WHEEL, 0600, "udev");
1044 static void
1045 udev_dev_uninit(void)
1047 destroy_dev(udev_dev);
1050 SYSINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY,
1051 udev_init, NULL);
1052 SYSUNINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY,
1053 udev_uninit, NULL);
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);