kernel - Optimize lwp-specific signaling.
[dragonfly.git] / sys / kern / kern_udev.c
1 /*
2  * Copyright (c) 2010 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Alex Hornung <ahornung@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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.
20  *
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.
33  */
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>
49
50 #include <sys/thread2.h>
51
52 MALLOC_DEFINE(M_UDEV, "udev", "udev allocs");
53 static struct objcache *udev_event_kernel_cache;
54
55 /* XXX: use UUIDs for identification; would need help from devfs */
56
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;
64
65 struct udev_prop_ctx {
66         prop_array_t cdevs;
67         int error;
68 };
69
70 struct udev_event_kernel {
71         struct udev_event ev;
72         TAILQ_ENTRY(udev_event_kernel)  link;
73 };
74
75 struct udev_softc {
76         TAILQ_ENTRY(udev_softc) entry;
77         int opened;
78         int initiated;
79         int unit;
80         cdev_t dev;
81
82         struct udev_event_kernel marker;        /* udev_evq marker */
83 };
84
85 struct cmd_function {
86         const char *cmd;
87         int  (*fn)(struct udev_softc *, struct plistref *,
88                    u_long, prop_dictionary_t);
89 };
90
91
92 static int _udev_dict_set_cstr(prop_dictionary_t, const char *, char *);
93 static int _udev_dict_set_int(prop_dictionary_t, const char *, int64_t);
94 static int _udev_dict_set_uint(prop_dictionary_t, const char *, uint64_t);
95 static int _udev_dict_delete_key(prop_dictionary_t, const char *);
96 static prop_dictionary_t udev_init_dict_event(cdev_t, const char *);
97 static int udev_init_dict(cdev_t);
98 static int udev_destroy_dict(cdev_t);
99 static void udev_event_insert(int, prop_dictionary_t);
100 static void udev_clean_events_locked(void);
101 static char *udev_event_externalize(struct udev_event_kernel *);
102 static void udev_getdevs_scan_callback(char *, cdev_t, bool, void *);
103 static int udev_getdevs_ioctl(struct udev_softc *, struct plistref *,
104                                         u_long, prop_dictionary_t);
105 static void udev_dev_filter_detach(struct knote *);
106 static int udev_dev_filter_read(struct knote *, long);
107
108 static struct dev_ops udev_dev_ops = {
109         { "udev", 0, 0 },
110         .d_open = udev_dev_open,
111         .d_close = udev_dev_close,
112         .d_read = udev_dev_read,
113         .d_kqfilter = udev_dev_kqfilter,
114         .d_ioctl = udev_dev_ioctl
115 };
116
117 static struct cmd_function cmd_fn[] = {
118                 { .cmd = "getdevs", .fn = udev_getdevs_ioctl},
119                 {NULL, NULL}
120 };
121
122 DEVFS_DEFINE_CLONE_BITMAP(udev);
123
124 static TAILQ_HEAD(, udev_softc) udevq;
125 static TAILQ_HEAD(, udev_event_kernel) udev_evq;
126 static struct kqinfo udev_kq;
127 static struct lock udev_lk;
128 static int udev_evqlen;
129 static int udev_initiated_count;
130 static int udev_open_count;
131 static int udev_seqwait;
132 static int udev_seq;
133
134 static int
135 _udev_dict_set_cstr(prop_dictionary_t dict, const char *key, char *str)
136 {
137         prop_string_t   ps;
138
139         KKASSERT(dict != NULL);
140
141         ps = prop_string_create_cstring(str);
142         if (ps == NULL) {
143                 return ENOMEM;
144         }
145
146         if (prop_dictionary_set(dict, key, ps) == false) {
147                 prop_object_release(ps);
148                 return ENOMEM;
149         }
150
151         prop_object_release(ps);
152         return 0;
153 }
154
155 static int
156 _udev_dict_set_int(prop_dictionary_t dict, const char *key, int64_t val)
157 {
158         prop_number_t   pn;
159
160         KKASSERT(dict != NULL);
161
162         pn = prop_number_create_integer(val);
163         if (pn == NULL)
164                 return ENOMEM;
165
166         if (prop_dictionary_set(dict, key, pn) == false) {
167                 prop_object_release(pn);
168                 return ENOMEM;
169         }
170
171         prop_object_release(pn);
172         return 0;
173 }
174
175 static int
176 _udev_dict_set_uint(prop_dictionary_t dict, const char *key, uint64_t val)
177 {
178         prop_number_t   pn;
179
180         KKASSERT(dict != NULL);
181
182         pn = prop_number_create_unsigned_integer(val);
183         if (pn == NULL)
184                 return ENOMEM;
185
186         if (prop_dictionary_set(dict, key, pn) == false) {
187                 prop_object_release(pn);
188                 return ENOMEM;
189         }
190
191         prop_object_release(pn);
192         return 0;
193 }
194
195 static int
196 _udev_dict_delete_key(prop_dictionary_t dict, const char *key)
197 {
198         KKASSERT(dict != NULL);
199
200         prop_dictionary_remove(dict, key);
201
202         return 0;
203 }
204
205 /*
206  * Initialize an event dictionary, which contains three parameters to
207  * identify the device referred to (name, devnum, kptr) and the affected key.
208  */
209 static prop_dictionary_t
210 udev_init_dict_event(cdev_t dev, const char *key)
211 {
212         prop_dictionary_t       dict;
213         uint64_t        kptr;
214         int error;
215
216         kptr = (uint64_t)(uintptr_t)dev;
217         KKASSERT(dev != NULL);
218
219         dict = prop_dictionary_create();
220         if (dict == NULL) {
221                 log(LOG_DEBUG, "udev_init_dict_event: prop_dictionary_create() failed\n");
222                 return NULL;
223         }
224
225         if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
226                 goto error_out;
227         if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
228                 goto error_out;
229         if ((error = _udev_dict_set_uint(dict, "devtype", (dev_dflags(dev) & D_TYPEMASK))))
230                 goto error_out;
231         if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
232                 goto error_out;
233         if ((error = _udev_dict_set_cstr(dict, "key", __DECONST(char *, key))))
234                 goto error_out;
235
236         return dict;
237
238 error_out:
239         prop_object_release(dict);
240         return NULL;
241 }
242
243 int
244 udev_dict_set_cstr(cdev_t dev, const char *key, char *str)
245 {
246         prop_dictionary_t       dict;
247         int error;
248
249         KKASSERT(dev != NULL);
250
251         if (dev->si_dict == NULL) {
252                 error = udev_init_dict(dev);
253                 if (error)
254                         return -1;
255         }
256
257         /* Queue a key update event */
258         dict = udev_init_dict_event(dev, key);
259         if (dict == NULL)
260                 return ENOMEM;
261
262         if ((error = _udev_dict_set_cstr(dict, "value", str))) {
263                 prop_object_release(dict);
264                 return error;
265         }
266         udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
267         prop_object_release(dict);
268
269         error = _udev_dict_set_cstr(dev->si_dict, key, str);
270         return error;
271 }
272
273 int
274 udev_dict_set_int(cdev_t dev, const char *key, int64_t val)
275 {
276         prop_dictionary_t       dict;
277         int error;
278
279         KKASSERT(dev != NULL);
280
281         if (dev->si_dict == NULL) {
282                 error = udev_init_dict(dev);
283                 if (error)
284                         return -1;
285         }
286
287         /* Queue a key update event */
288         dict = udev_init_dict_event(dev, key);
289         if (dict == NULL)
290                 return ENOMEM;
291         if ((error = _udev_dict_set_int(dict, "value", val))) {
292                 prop_object_release(dict);
293                 return error;
294         }
295         udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
296         prop_object_release(dict);
297
298         return _udev_dict_set_int(dev->si_dict, key, val);
299 }
300
301 int
302 udev_dict_set_uint(cdev_t dev, const char *key, uint64_t val)
303 {
304         prop_dictionary_t       dict;
305         int error;
306
307         KKASSERT(dev != NULL);
308
309         if (dev->si_dict == NULL) {
310                 error = udev_init_dict(dev);
311                 if (error)
312                         return -1;
313         }
314
315         /* Queue a key update event */
316         dict = udev_init_dict_event(dev, key);
317         if (dict == NULL)
318                 return ENOMEM;
319         if ((error = _udev_dict_set_uint(dict, "value", val))) {
320                 prop_object_release(dict);
321                 return error;
322         }
323         udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
324         prop_object_release(dict);
325
326         return _udev_dict_set_uint(dev->si_dict, key, val);
327 }
328
329 int
330 udev_dict_delete_key(cdev_t dev, const char *key)
331 {
332         prop_dictionary_t       dict;
333
334         KKASSERT(dev != NULL);
335
336         /* Queue a key removal event */
337         dict = udev_init_dict_event(dev, key);
338         if (dict == NULL)
339                 return ENOMEM;
340         udev_event_insert(UDEV_EV_KEY_REMOVE, dict);
341         prop_object_release(dict);
342
343         return _udev_dict_delete_key(dev->si_dict, key);
344 }
345
346 static int
347 udev_init_dict(cdev_t dev)
348 {
349         prop_dictionary_t dict;
350         uint64_t        kptr;
351         int error;
352
353         kptr = (uint64_t)(uintptr_t)dev;
354
355         KKASSERT(dev != NULL);
356
357         if (dev->si_dict != NULL) {
358 #if 0
359                 log(LOG_DEBUG,
360                     "udev_init_dict: new dict for %s, but has dict already (%p)!\n",
361                     dev->si_name, dev->si_dict);
362 #endif
363                 return 0;
364         }
365
366         dict = prop_dictionary_create();
367         if (dict == NULL) {
368                 log(LOG_DEBUG, "udev_init_dict: prop_dictionary_create() failed\n");
369                 return ENOMEM;
370         }
371
372         if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
373                 goto error_out;
374         if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
375                 goto error_out;
376         if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
377                 goto error_out;
378         if ((error = _udev_dict_set_uint(dict, "devtype", (dev_dflags(dev) & D_TYPEMASK))))
379                 goto error_out;
380
381         /* XXX: The next 3 are marginallly useful, if at all */
382         if ((error = _udev_dict_set_uint(dict, "uid", dev->si_uid)))
383                 goto error_out;
384         if ((error = _udev_dict_set_uint(dict, "gid", dev->si_gid)))
385                 goto error_out;
386         if ((error = _udev_dict_set_int(dict, "mode", dev->si_perms)))
387                 goto error_out;
388
389         if ((error = _udev_dict_set_int(dict, "major", dev->si_umajor)))
390                 goto error_out;
391         if ((error = _udev_dict_set_int(dict, "minor", dev->si_uminor)))
392                 goto error_out;
393         if (dev->si_ops->head.name != NULL) {
394                 if ((error = _udev_dict_set_cstr(dict, "driver",
395                     __DECONST(char *, dev->si_ops->head.name))))
396                         goto error_out;
397         }
398
399         dev->si_dict = dict;
400         return 0;
401
402 error_out:
403         dev->si_dict = NULL;
404         prop_object_release(dict);
405         return error;
406 }
407
408 static int
409 udev_destroy_dict(cdev_t dev)
410 {
411         KKASSERT(dev != NULL);
412
413         if (dev->si_dict != NULL) {
414                 prop_object_release(dev->si_dict);
415                 dev->si_dict = NULL;
416         }
417
418         return 0;
419 }
420
421 static void
422 udev_event_insert(int ev_type, prop_dictionary_t dict)
423 {
424         struct udev_event_kernel *ev;
425         prop_dictionary_t dict_copy;
426
427         /* Only start queing events after client has initiated properly */
428         if (udev_initiated_count) {
429                 dict_copy = prop_dictionary_copy(dict);
430                 if (dict_copy == NULL)
431                         return;
432                 ev = objcache_get(udev_event_kernel_cache, M_WAITOK);
433                 ev->ev.ev_dict = dict_copy;
434                 ev->ev.ev_type = ev_type;
435
436                 lockmgr(&udev_lk, LK_EXCLUSIVE);
437                 TAILQ_INSERT_TAIL(&udev_evq, ev, link);
438                 ++udev_evqlen;
439                 ++udev_seq;
440                 if (udev_seqwait)
441                         wakeup(&udev_seqwait);
442                 lockmgr(&udev_lk, LK_RELEASE);
443                 wakeup(&udev_evq);
444                 KNOTE(&udev_kq.ki_note, 0);
445         } else if (udev_open_count) {
446                 lockmgr(&udev_lk, LK_EXCLUSIVE);
447                 ++udev_seq;
448                 if (udev_seqwait)
449                         wakeup(&udev_seqwait);
450                 lockmgr(&udev_lk, LK_RELEASE);
451                 KNOTE(&udev_kq.ki_note, 0);
452         }
453 }
454
455 static void
456 udev_clean_events_locked(void)
457 {
458         struct udev_event_kernel *ev;
459
460         while ((ev = TAILQ_FIRST(&udev_evq)) &&
461                ev->ev.ev_dict != NULL) {
462                 TAILQ_REMOVE(&udev_evq, ev, link);
463                 objcache_put(udev_event_kernel_cache, ev);
464                 --udev_evqlen;
465         }
466 }
467
468 static char *
469 udev_event_externalize(struct udev_event_kernel *ev)
470 {
471         prop_dictionary_t       dict;
472         char *xml;
473         int error;
474
475         dict = prop_dictionary_create();
476         if (dict == NULL) {
477                 log(LOG_DEBUG,
478                     "udev_event_externalize: prop_dictionary_create() failed\n");
479                 return NULL;
480         }
481
482         if ((error = _udev_dict_set_int(dict, "evtype", ev->ev.ev_type))) {
483                 prop_object_release(dict);
484                 return NULL;
485         }
486
487         if (prop_dictionary_set(dict, "evdict", ev->ev.ev_dict) == false) {
488                 prop_object_release(dict);
489                 return NULL;
490         }
491
492         prop_object_release(ev->ev.ev_dict);
493
494         xml = prop_dictionary_externalize(dict);
495
496         prop_object_release(dict);
497
498         return xml;
499 }
500
501 int
502 udev_event_attach(cdev_t dev, char *name, int alias)
503 {
504         prop_dictionary_t       dict;
505         int error;
506
507         KKASSERT(dev != NULL);
508
509         error = ENOMEM;
510
511         if (alias) {
512                 dict = prop_dictionary_copy(dev->si_dict);
513                 if (dict == NULL)
514                         goto error_out;
515
516                 if ((error = _udev_dict_set_cstr(dict, "name", name))) {
517                         prop_object_release(dict);
518                         goto error_out;
519                 }
520
521                 _udev_dict_set_int(dict, "alias", 1);
522
523                 udev_event_insert(UDEV_EVENT_ATTACH, dict);
524                 prop_object_release(dict);
525         } else {
526                 error = udev_init_dict(dev);
527                 if (error)
528                         goto error_out;
529
530                 _udev_dict_set_int(dev->si_dict, "alias", 0);
531                 udev_event_insert(UDEV_EVENT_ATTACH, dev->si_dict);
532         }
533
534 error_out:
535         return error;
536 }
537
538 int
539 udev_event_detach(cdev_t dev, char *name, int alias)
540 {
541         prop_dictionary_t       dict;
542
543         KKASSERT(dev != NULL);
544
545         if (alias) {
546                 dict = prop_dictionary_copy(dev->si_dict);
547                 if (dict == NULL)
548                         goto error_out;
549
550                 if (_udev_dict_set_cstr(dict, "name", name)) {
551                         prop_object_release(dict);
552                         goto error_out;
553                 }
554
555                 _udev_dict_set_int(dict, "alias", 1);
556
557                 udev_event_insert(UDEV_EVENT_DETACH, dict);
558                 prop_object_release(dict);
559         } else {
560                 udev_event_insert(UDEV_EVENT_DETACH, dev->si_dict);
561         }
562
563 error_out:
564         udev_destroy_dict(dev);
565
566         return 0;
567 }
568
569 /*
570  * Allow multiple opens.  Each opener gets a different device.
571  * Messages are replicated to all devices using a marker system.
572  */
573 static int
574 udev_dev_clone(struct dev_clone_args *ap)
575 {
576         struct udev_softc *softc;
577         int unit;
578
579         unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(udev), 1000);
580         if (unit < 0) {
581                 ap->a_dev = NULL;
582                 return 1;
583         }
584
585         softc = kmalloc(sizeof(*softc), M_UDEV, M_WAITOK | M_ZERO);
586         softc->unit = unit;
587         lockmgr(&udev_lk, LK_EXCLUSIVE);
588         TAILQ_INSERT_TAIL(&udevq, softc, entry);
589         lockmgr(&udev_lk, LK_RELEASE);
590
591         softc->dev = make_only_dev(&udev_dev_ops, unit, ap->a_cred->cr_ruid,
592                                    0, 0600, "udev/%d", unit);
593         softc->dev->si_drv1 = softc;
594         ap->a_dev = softc->dev;
595         return 0;
596 }
597
598 /*
599  * dev stuff
600  */
601 static int
602 udev_dev_open(struct dev_open_args *ap)
603 {
604         struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
605
606         lockmgr(&udev_lk, LK_EXCLUSIVE);
607         if (softc == NULL || softc->opened) {
608                 lockmgr(&udev_lk, LK_RELEASE);
609                 return EBUSY;
610         }
611         softc->opened = 1;
612         ++udev_open_count;
613         lockmgr(&udev_lk, LK_RELEASE);
614
615         return 0;
616 }
617
618 static int
619 udev_dev_close(struct dev_close_args *ap)
620 {
621         struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
622
623         KKASSERT(softc->dev == ap->a_head.a_dev);
624         KKASSERT(softc->opened == 1);
625
626         lockmgr(&udev_lk, LK_EXCLUSIVE);
627         TAILQ_REMOVE(&udevq, softc, entry);
628         devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(udev), softc->unit);
629
630         if (softc->initiated) {
631                 TAILQ_REMOVE(&udev_evq, &softc->marker, link);
632                 softc->initiated = 0;
633                 --udev_initiated_count;
634                 udev_clean_events_locked();
635         }
636         softc->opened = 0;
637         softc->dev = NULL;
638         ap->a_head.a_dev->si_drv1 = NULL;
639         --udev_open_count;
640         lockmgr(&udev_lk, LK_RELEASE);
641
642         destroy_dev(ap->a_head.a_dev);
643         wakeup(&udev_evq);
644
645         kfree(softc, M_UDEV);
646
647         return 0;
648 }
649
650 static struct filterops udev_dev_read_filtops =
651         { FILTEROP_ISFD | FILTEROP_MPSAFE, NULL,
652           udev_dev_filter_detach, udev_dev_filter_read };
653
654 static int
655 udev_dev_kqfilter(struct dev_kqfilter_args *ap)
656 {
657         struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
658         struct knote *kn = ap->a_kn;
659         struct klist *klist;
660
661         ap->a_result = 0;
662         lockmgr(&udev_lk, LK_EXCLUSIVE);
663
664         switch (kn->kn_filter) {
665         case EVFILT_READ:
666                 kn->kn_fop = &udev_dev_read_filtops;
667                 kn->kn_hook = (caddr_t)softc;
668                 break;
669         default:
670                 ap->a_result = EOPNOTSUPP;
671                 lockmgr(&udev_lk, LK_RELEASE);
672                 return (0);
673         }
674
675         klist = &udev_kq.ki_note;
676         knote_insert(klist, kn);
677
678         lockmgr(&udev_lk, LK_RELEASE);
679
680         return (0);
681 }
682
683 static void
684 udev_dev_filter_detach(struct knote *kn)
685 {
686         struct klist *klist;
687
688         lockmgr(&udev_lk, LK_EXCLUSIVE);
689         klist = &udev_kq.ki_note;
690         knote_remove(klist, kn);
691         lockmgr(&udev_lk, LK_RELEASE);
692 }
693
694 static int
695 udev_dev_filter_read(struct knote *kn, long hint)
696 {
697         struct udev_softc *softc = (void *)kn->kn_hook;
698         struct udev_event_kernel *ev;
699         int ready = 0;
700
701         lockmgr(&udev_lk, LK_EXCLUSIVE);
702         if (softc->initiated) {
703                 ev = TAILQ_NEXT(&softc->marker, link);
704                 while (ev && ev->ev.ev_dict == NULL)
705                         ev = TAILQ_NEXT(ev, link);
706                 if (ev)
707                         ready = 1;
708         }
709         lockmgr(&udev_lk, LK_RELEASE);
710
711         return (ready);
712 }
713
714 static int
715 udev_dev_read(struct dev_read_args *ap)
716 {
717         struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
718         struct udev_event_kernel *ev;
719         struct uio *uio = ap->a_uio;
720         char    *xml;
721         size_t  len;
722         int     error;
723
724         lockmgr(&udev_lk, LK_EXCLUSIVE);
725
726         /*
727          * Automatically enable message collection if it has not already
728          * been enabled.
729          */
730         if (softc->initiated == 0) {
731                 softc->initiated = 1;
732                 ++udev_initiated_count;
733                 TAILQ_INSERT_HEAD(&udev_evq, &softc->marker, link);
734         }
735
736         /*
737          * Loop, sleep interruptably until we get an event or signal.
738          */
739         error = 0;
740         for (;;) {
741                 if (softc->initiated) {
742                         ev = TAILQ_NEXT(&softc->marker, link);
743                         while (ev && ev->ev.ev_dict == NULL)
744                                 ev = TAILQ_NEXT(ev, link);
745                         if (ev) {
746                                 if ((xml = udev_event_externalize(ev)) == NULL) {
747                                         error = ENOMEM;
748                                         break;
749                                 }
750                                 len = strlen(xml) + 1; /* include terminator */
751                                 if (uio->uio_resid < len)
752                                         error = ENOMEM;
753                                 else
754                                         error = uiomove((caddr_t)xml, len, uio);
755                                 kfree(xml, M_TEMP);
756
757                                 /*
758                                  * Move the marker
759                                  */
760                                 TAILQ_REMOVE(&udev_evq, &softc->marker, link);
761                                 TAILQ_INSERT_AFTER(&udev_evq,
762                                                    ev, &softc->marker, link);
763                                 udev_clean_events_locked();
764                                 break;
765                         }
766                 }
767                 if (ap->a_ioflag & IO_NDELAY) {
768                         error = EWOULDBLOCK;
769                         break;
770                 }
771                 if ((error = lksleep(&udev_evq, &udev_lk, PCATCH, "udevq", 0)))
772                         break;
773         }
774
775         lockmgr(&udev_lk, LK_RELEASE);
776         return error;
777 }
778
779 static int
780 udev_dev_ioctl(struct dev_ioctl_args *ap)
781 {
782         struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
783         prop_dictionary_t dict;
784         prop_object_t   po;
785         prop_string_t   ps;
786         struct plistref *pref;
787         int i, error;
788         int seq;
789
790         error = 0;
791
792         switch(ap->a_cmd) {
793         case UDEVPROP:
794                 /* Use proplib(3) for userspace/kernel communication */
795                 pref = (struct plistref *)ap->a_data;
796                 error = prop_dictionary_copyin_ioctl(pref, ap->a_cmd, &dict);
797                 if (error)
798                         return error;
799
800                 po = prop_dictionary_get(dict, "command");
801                 if (po == NULL || prop_object_type(po) != PROP_TYPE_STRING) {
802                         log(LOG_DEBUG, "udev: prop_dictionary_get() failed\n");
803                         prop_object_release(dict);
804                         return EINVAL;
805                 }
806
807                 ps = po;
808                 /* Handle cmd */
809                 for(i = 0; cmd_fn[i].cmd != NULL; i++) {
810                         if (prop_string_equals_cstring(ps, cmd_fn[i].cmd))
811                                 break;
812                 }
813
814                 if (cmd_fn[i].cmd != NULL) {
815                         error = cmd_fn[i].fn(softc, pref, ap->a_cmd, dict);
816                 } else {
817                         error = EINVAL;
818                 }
819
820                 //prop_object_release(po);
821                 prop_object_release(dict);
822                 break;
823         case UDEVWAIT:
824                 /*
825                  * Wait for events based on sequence number.  Updates
826                  * sequence number for loop.
827                  */
828                 lockmgr(&udev_lk, LK_EXCLUSIVE);
829                 seq = *(int *)ap->a_data;
830                 ++udev_seqwait;
831                 while (seq == udev_seq) {
832                         error = lksleep(&udev_seqwait, &udev_lk,
833                                         PCATCH, "udevw", 0);
834                         if (error)
835                                 break;
836                 }
837                 --udev_seqwait;
838                 *(int *)ap->a_data = udev_seq;
839                 lockmgr(&udev_lk, LK_RELEASE);
840                 break;
841         default:
842                 error = ENOTTY; /* Inappropriate ioctl for device */
843                 break;
844         }
845
846         return(error);
847 }
848
849 static void
850 udev_getdevs_scan_callback(char *name, cdev_t cdev, bool is_alias, void *arg)
851 {
852         struct udev_prop_ctx *ctx = arg;
853
854         KKASSERT(arg != NULL);
855
856         if (cdev->si_dict == NULL)
857                 return;
858
859         if (prop_array_add(ctx->cdevs, cdev->si_dict) == false) {
860                 ctx->error = EINVAL;
861                 return;
862         }
863 }
864
865 static int
866 udev_getdevs_ioctl(struct udev_softc *softc, struct plistref *pref,
867                    u_long cmd, prop_dictionary_t dict)
868 {
869         prop_dictionary_t odict;
870         struct udev_prop_ctx ctx;
871         int error;
872
873         /*
874          * Ensure event notification is enabled before doing the devfs
875          * scan so nothing gets missed.
876          */
877         lockmgr(&udev_lk, LK_EXCLUSIVE);
878         if (softc->initiated == 0) {
879                 softc->initiated = 1;
880                 ++udev_initiated_count;
881                 TAILQ_INSERT_HEAD(&udev_evq, &softc->marker, link);
882         }
883         lockmgr(&udev_lk, LK_RELEASE);
884
885         /*
886          * Devfs scan to build full dictionary.
887          */
888         ctx.error = 0;
889         ctx.cdevs = prop_array_create();
890         if (ctx.cdevs == NULL) {
891                 log(LOG_DEBUG,
892                     "udev_getdevs_ioctl: prop_array_create() failed\n");
893                 return EINVAL;
894         }
895
896         devfs_scan_callback(udev_getdevs_scan_callback, &ctx);
897
898         if (ctx.error != 0) {
899                 prop_object_release(ctx.cdevs);
900                 return (ctx.error);
901         }
902
903         odict = prop_dictionary_create();
904         if (odict == NULL) {
905                 return ENOMEM;
906         }
907
908         if ((prop_dictionary_set(odict, "array", ctx.cdevs)) == 0) {
909                 log(LOG_DEBUG,
910                     "udev_getdevs_ioctl: prop_dictionary_set failed\n");
911                 prop_object_release(odict);
912                 return ENOMEM;
913         }
914
915         error = prop_dictionary_copyout_ioctl(pref, cmd, odict);
916
917         prop_object_release(odict);
918         return error;
919 }
920
921
922 /*
923  * SYSINIT stuff
924  */
925 static void
926 udev_init(void)
927 {
928         lockinit(&udev_lk, "udevlk", 0, LK_CANRECURSE);
929         TAILQ_INIT(&udevq);
930         TAILQ_INIT(&udev_evq);
931         udev_event_kernel_cache = objcache_create_simple(M_UDEV, sizeof(struct udev_event_kernel));
932 }
933
934 static void
935 udev_uninit(void)
936 {
937         objcache_destroy(udev_event_kernel_cache);
938 }
939
940 static void
941 udev_dev_init(void)
942 {
943         udev_dev = make_autoclone_dev(&udev_dev_ops, &DEVFS_CLONE_BITMAP(udev),
944                                       udev_dev_clone,
945                                       UID_ROOT, GID_WHEEL, 0600, "udev");
946 }
947
948 static void
949 udev_dev_uninit(void)
950 {
951         destroy_dev(udev_dev);
952 }
953
954 SYSINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY,
955         udev_init, NULL);
956 SYSUNINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY,
957         udev_uninit, NULL);
958 SYSINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY,
959         udev_dev_init, NULL);
960 SYSUNINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY,
961         udev_dev_uninit, NULL);