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