kernel - Allow multiple opens on /dev/udev
[dragonfly.git] / sys / kern / kern_udev.c
CommitLineData
3a3826b3
AH
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>
38714400 40#include <sys/event.h>
3a3826b3
AH
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
38714400
SG
48#include <sys/thread2.h>
49
3a3826b3
AH
50MALLOC_DEFINE(M_UDEV, "udev", "udev allocs");
51
52/* XXX: use UUIDs for identification; would need help from devfs */
53
54static cdev_t udev_dev;
55static d_open_t udev_dev_open;
56static d_close_t udev_dev_close;
57static d_read_t udev_dev_read;
38714400 58static d_kqfilter_t udev_dev_kqfilter;
3a3826b3 59static d_ioctl_t udev_dev_ioctl;
9b881e96 60static d_clone_t udev_dev_clone;
3a3826b3
AH
61
62struct udev_prop_ctx {
63 prop_array_t cdevs;
64 int error;
65};
66
67struct udev_event_kernel {
68 struct udev_event ev;
69 TAILQ_ENTRY(udev_event_kernel) link;
70};
71
72struct udev_softc {
9b881e96 73 TAILQ_ENTRY(udev_softc) entry;
3a3826b3
AH
74 int opened;
75 int initiated;
9b881e96
MD
76 int unit;
77 cdev_t dev;
3a3826b3 78
9b881e96
MD
79 struct udev_event_kernel marker; /* udev_evq marker */
80};
81
82struct cmd_function {
83 const char *cmd;
84 int (*fn)(struct udev_softc *, struct plistref *,
85 u_long, prop_dictionary_t);
86};
3a3826b3 87
9b881e96
MD
88
89static int _udev_dict_set_cstr(prop_dictionary_t, const char *, char *);
90static int _udev_dict_set_int(prop_dictionary_t, const char *, int64_t);
91static int _udev_dict_set_uint(prop_dictionary_t, const char *, uint64_t);
92static int _udev_dict_delete_key(prop_dictionary_t, const char *);
93static prop_dictionary_t udev_init_dict_event(cdev_t, const char *);
94static int udev_init_dict(cdev_t);
95static int udev_destroy_dict(cdev_t);
96static void udev_event_insert(int, prop_dictionary_t);
97static void udev_clean_events_locked(void);
98static char *udev_event_externalize(struct udev_event_kernel *);
99static void udev_getdevs_scan_callback(char *, cdev_t, bool, void *);
100static int udev_getdevs_ioctl(struct udev_softc *, struct plistref *,
101 u_long, prop_dictionary_t);
102static void udev_dev_filter_detach(struct knote *);
103static int udev_dev_filter_read(struct knote *, long);
3a3826b3
AH
104
105static struct dev_ops udev_dev_ops = {
d4b8aec4 106 { "udev", 0, 0 },
3a3826b3
AH
107 .d_open = udev_dev_open,
108 .d_close = udev_dev_close,
109 .d_read = udev_dev_read,
38714400 110 .d_kqfilter = udev_dev_kqfilter,
3a3826b3
AH
111 .d_ioctl = udev_dev_ioctl
112};
113
f5d8307c 114static struct cmd_function cmd_fn[] = {
3a3826b3
AH
115 { .cmd = "getdevs", .fn = udev_getdevs_ioctl},
116 {NULL, NULL}
117};
118
9b881e96
MD
119DEVFS_DECLARE_CLONE_BITMAP(udev);
120
121static TAILQ_HEAD(, udev_softc) udevq;
122static TAILQ_HEAD(, udev_event_kernel) udev_evq;
123static struct kqinfo udev_kq;
124static struct lock udev_lk;
125static int udev_evqlen;
126static int udev_initiated_count;
127
3a3826b3
AH
128static 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);
f5d8307c 136 if (ps == NULL) {
3a3826b3 137 return ENOMEM;
f5d8307c 138 }
3a3826b3
AH
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
149static 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
169static 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
189static 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 */
203static prop_dictionary_t
204udev_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;
539b1063 223 if ((error = _udev_dict_set_uint(dict, "devtype", (dev_dflags(dev) & D_TYPEMASK))))
751a320c 224 goto error_out;
3a3826b3
AH
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
f5d8307c
AH
230 return dict;
231
3a3826b3
AH
232error_out:
233 prop_object_release(dict);
234 return NULL;
235}
236
237int
238udev_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
f5d8307c
AH
245 if (dev->si_dict == NULL) {
246 error = udev_init_dict(dev);
247 if (error)
248 return -1;
249 }
250
3a3826b3
AH
251 /* Queue a key update event */
252 dict = udev_init_dict_event(dev, key);
253 if (dict == NULL)
254 return ENOMEM;
f5d8307c 255
3a3826b3
AH
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
f5d8307c
AH
263 error = _udev_dict_set_cstr(dev->si_dict, key, str);
264 return error;
3a3826b3
AH
265}
266
267int
268udev_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
f5d8307c
AH
275 if (dev->si_dict == NULL) {
276 error = udev_init_dict(dev);
277 if (error)
278 return -1;
279 }
280
3a3826b3
AH
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
295int
296udev_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
f5d8307c
AH
303 if (dev->si_dict == NULL) {
304 error = udev_init_dict(dev);
305 if (error)
306 return -1;
307 }
308
3a3826b3
AH
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
323int
324udev_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
340static int
341udev_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);
f5d8307c
AH
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
3a3826b3
AH
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;
539b1063 372 if ((error = _udev_dict_set_uint(dict, "devtype", (dev_dflags(dev) & D_TYPEMASK))))
751a320c 373 goto error_out;
3a3826b3
AH
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
619e8f47 383 if ((error = _udev_dict_set_int(dict, "major", dev->si_umajor)))
3a3826b3
AH
384 goto error_out;
385 if ((error = _udev_dict_set_int(dict, "minor", dev->si_uminor)))
386 goto error_out;
f5d8307c
AH
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 }
3a3826b3
AH
392
393 dev->si_dict = dict;
394 return 0;
395
396error_out:
397 dev->si_dict = NULL;
398 prop_object_release(dict);
399 return error;
400}
401
402static int
403udev_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
415static void
416udev_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 */
9b881e96 421 if (udev_initiated_count == 0)
3a3826b3
AH
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
9b881e96
MD
433 lockmgr(&udev_lk, LK_EXCLUSIVE);
434 TAILQ_INSERT_TAIL(&udev_evq, ev, link);
435 ++udev_evqlen;
436 lockmgr(&udev_lk, LK_RELEASE);
3a3826b3 437
9b881e96
MD
438 wakeup(&udev_evq);
439 KNOTE(&udev_kq.ki_note, 0);
3a3826b3
AH
440}
441
9b881e96
MD
442static void
443udev_clean_events_locked(void)
3a3826b3
AH
444{
445 struct udev_event_kernel *ev;
446
9b881e96
MD
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;
3a3826b3 452 }
3a3826b3
AH
453}
454
455static char *
456udev_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) {
9b881e96
MD
465 log(LOG_DEBUG,
466 "udev_event_externalize: prop_dictionary_create() failed\n");
3a3826b3
AH
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
489int
490udev_event_attach(cdev_t dev, char *name, int alias)
491{
492 prop_dictionary_t dict;
493 int error;
494
495 KKASSERT(dev != NULL);
496
f5d8307c 497 error = ENOMEM;
3a3826b3
AH
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 {
f5d8307c
AH
514 error = udev_init_dict(dev);
515 if (error)
516 goto error_out;
517
3a3826b3
AH
518 _udev_dict_set_int(dev->si_dict, "alias", 0);
519 udev_event_insert(UDEV_EVENT_ATTACH, dev->si_dict);
520 }
521
522error_out:
3a3826b3
AH
523 return error;
524}
525
526int
527udev_event_detach(cdev_t dev, char *name, int alias)
528{
529 prop_dictionary_t dict;
530
531 KKASSERT(dev != NULL);
532
3a3826b3
AH
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
551error_out:
552 udev_destroy_dict(dev);
553
3a3826b3
AH
554 return 0;
555}
556
557/*
9b881e96
MD
558 * Allow multiple opens. Each opener gets a different device.
559 * Messages are replicated to all devices using a marker system.
560 */
561static int
562udev_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/*
3a3826b3
AH
587 * dev stuff
588 */
589static int
590udev_dev_open(struct dev_open_args *ap)
591{
9b881e96 592 struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
3a3826b3 593
9b881e96
MD
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);
3a3826b3
AH
601
602 return 0;
603}
604
605static int
606udev_dev_close(struct dev_close_args *ap)
607{
9b881e96
MD
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);
3a3826b3
AH
632
633 return 0;
634}
635
38714400 636static struct filterops udev_dev_read_filtops =
4c91dbc9 637 { FILTEROP_ISFD, NULL, udev_dev_filter_detach, udev_dev_filter_read };
38714400
SG
638
639static int
640udev_dev_kqfilter(struct dev_kqfilter_args *ap)
641{
9b881e96 642 struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
38714400
SG
643 struct knote *kn = ap->a_kn;
644 struct klist *klist;
645
646 ap->a_result = 0;
9b881e96 647 lockmgr(&udev_lk, LK_EXCLUSIVE);
38714400
SG
648
649 switch (kn->kn_filter) {
650 case EVFILT_READ:
651 kn->kn_fop = &udev_dev_read_filtops;
9b881e96 652 kn->kn_hook = (caddr_t)softc;
38714400
SG
653 break;
654 default:
b287d649 655 ap->a_result = EOPNOTSUPP;
9b881e96 656 lockmgr(&udev_lk, LK_RELEASE);
38714400
SG
657 return (0);
658 }
659
9b881e96 660 klist = &udev_kq.ki_note;
5b22f1a7 661 knote_insert(klist, kn);
38714400 662
9b881e96 663 lockmgr(&udev_lk, LK_RELEASE);
38714400
SG
664
665 return (0);
666}
667
668static void
669udev_dev_filter_detach(struct knote *kn)
670{
671 struct klist *klist;
672
9b881e96
MD
673 lockmgr(&udev_lk, LK_EXCLUSIVE);
674 klist = &udev_kq.ki_note;
5b22f1a7 675 knote_remove(klist, kn);
9b881e96 676 lockmgr(&udev_lk, LK_RELEASE);
38714400
SG
677}
678
679static int
680udev_dev_filter_read(struct knote *kn, long hint)
681{
9b881e96
MD
682 struct udev_softc *softc = (void *)kn->kn_hook;
683 struct udev_event_kernel *ev;
38714400
SG
684 int ready = 0;
685
9b881e96
MD
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);
38714400
SG
695
696 return (ready);
697}
698
3a3826b3
AH
699static int
700udev_dev_read(struct dev_read_args *ap)
701{
9b881e96 702 struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
3a3826b3
AH
703 struct udev_event_kernel *ev;
704 struct uio *uio = ap->a_uio;
705 char *xml;
706 size_t len;
707 int error;
708
9b881e96 709 lockmgr(&udev_lk, LK_EXCLUSIVE);
3a3826b3 710
9b881e96 711 error = 0;
3a3826b3 712 for (;;) {
9b881e96
MD
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;
3a3826b3 737 }
3a3826b3 738 }
9b881e96
MD
739 if ((error = lksleep(&udev_evq, &udev_lk, PCATCH, "udevq", 0)))
740 break;
3a3826b3
AH
741 }
742
9b881e96
MD
743 lockmgr(&udev_lk, LK_RELEASE);
744 return error;
3a3826b3
AH
745}
746
747static int
748udev_dev_ioctl(struct dev_ioctl_args *ap)
749{
9b881e96 750 struct udev_softc *softc = ap->a_head.a_dev->si_drv1;
3a3826b3
AH
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) {
9b881e96 782 error = cmd_fn[i].fn(softc, pref, ap->a_cmd, dict);
3a3826b3
AH
783 } else {
784 error = EINVAL;
785 }
786
787 //prop_object_release(po);
3a3826b3
AH
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
798static void
b28e21ef 799udev_getdevs_scan_callback(char *name, cdev_t cdev, bool is_alias, void *arg)
3a3826b3
AH
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
814static int
9b881e96
MD
815udev_getdevs_ioctl(struct udev_softc *softc, struct plistref *pref,
816 u_long cmd, prop_dictionary_t dict)
3a3826b3
AH
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) {
9b881e96
MD
825 log(LOG_DEBUG,
826 "udev_getdevs_ioctl: prop_array_create() failed\n");
3a3826b3
AH
827 return EINVAL;
828 }
829
3a3826b3
AH
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 }
9b881e96
MD
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);
3a3826b3
AH
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) {
9b881e96
MD
850 log(LOG_DEBUG,
851 "udev_getdevs_ioctl: prop_dictionary_set failed\n");
3a3826b3
AH
852 prop_object_release(odict);
853 return ENOMEM;
854 }
855
856 error = prop_dictionary_copyout_ioctl(pref, cmd, odict);
857
3a3826b3
AH
858 prop_object_release(odict);
859 return error;
860}
861
862
863/*
864 * SYSINIT stuff
865 */
866static void
867udev_init(void)
868{
9b881e96
MD
869 lockinit(&udev_lk, "udevlk", 0, LK_CANRECURSE);
870 TAILQ_INIT(&udevq);
871 TAILQ_INIT(&udev_evq);
3a3826b3
AH
872}
873
874static void
875udev_uninit(void)
876{
877}
878
879static void
880udev_dev_init(void)
881{
9b881e96
MD
882 udev_dev = make_autoclone_dev(&udev_dev_ops, &DEVFS_CLONE_BITMAP(udev),
883 udev_dev_clone,
884 UID_ROOT, GID_WHEEL, 0600, "udev");
3a3826b3
AH
885}
886
887static void
888udev_dev_uninit(void)
889{
890 destroy_dev(udev_dev);
891}
892
9b881e96
MD
893SYSINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY,
894 udev_init, NULL);
895SYSUNINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY,
896 udev_uninit, NULL);
897SYSINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY,
898 udev_dev_init, NULL);
899SYSUNINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY,
900 udev_dev_uninit, NULL);