2 * Copyright (c) 2010 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
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
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
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
40 #include <sys/event.h>
41 #include <sys/malloc.h>
42 #include <sys/ctype.h>
43 #include <sys/syslog.h>
45 #include <sys/devfs.h>
46 #include <libprop/proplib.h>
48 #include <sys/thread2.h>
50 MALLOC_DEFINE(M_UDEV, "udev", "udev allocs");
52 /* XXX: use UUIDs for identification; would need help from devfs */
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;
61 static int _udev_dict_set_cstr(prop_dictionary_t, const char *, char *);
62 static int _udev_dict_set_int(prop_dictionary_t, const char *, int64_t);
63 static int _udev_dict_set_uint(prop_dictionary_t, const char *, uint64_t);
64 static int _udev_dict_delete_key(prop_dictionary_t, const char *);
65 static prop_dictionary_t udev_init_dict_event(cdev_t, const char *);
66 static int udev_init_dict(cdev_t);
67 static int udev_destroy_dict(cdev_t);
68 static void udev_event_insert(int, prop_dictionary_t);
69 static struct udev_event_kernel *udev_event_remove(void);
70 static void udev_event_free(struct udev_event_kernel *);
71 static char *udev_event_externalize(struct udev_event_kernel *);
72 static void udev_getdevs_scan_callback(cdev_t, void *);
73 static int udev_getdevs_ioctl(struct plistref *, u_long, prop_dictionary_t);
74 static void udev_dev_filter_detach(struct knote *);
75 static int udev_dev_filter_read(struct knote *, long);
79 int (*fn)(struct plistref *, u_long, prop_dictionary_t);
82 struct udev_prop_ctx {
87 struct udev_event_kernel {
89 TAILQ_ENTRY(udev_event_kernel) link;
100 TAILQ_HEAD(, udev_event_kernel) ev_queue; /* list of thread_io */
103 static struct dev_ops udev_dev_ops = {
105 .d_open = udev_dev_open,
106 .d_close = udev_dev_close,
107 .d_read = udev_dev_read,
108 .d_kqfilter = udev_dev_kqfilter,
109 .d_ioctl = udev_dev_ioctl
112 static struct cmd_function cmd_fn[] = {
113 { .cmd = "getdevs", .fn = udev_getdevs_ioctl},
118 _udev_dict_set_cstr(prop_dictionary_t dict, const char *key, char *str)
122 KKASSERT(dict != NULL);
124 ps = prop_string_create_cstring(str);
129 if (prop_dictionary_set(dict, key, ps) == false) {
130 prop_object_release(ps);
134 prop_object_release(ps);
139 _udev_dict_set_int(prop_dictionary_t dict, const char *key, int64_t val)
143 KKASSERT(dict != NULL);
145 pn = prop_number_create_integer(val);
149 if (prop_dictionary_set(dict, key, pn) == false) {
150 prop_object_release(pn);
154 prop_object_release(pn);
159 _udev_dict_set_uint(prop_dictionary_t dict, const char *key, uint64_t val)
163 KKASSERT(dict != NULL);
165 pn = prop_number_create_unsigned_integer(val);
169 if (prop_dictionary_set(dict, key, pn) == false) {
170 prop_object_release(pn);
174 prop_object_release(pn);
179 _udev_dict_delete_key(prop_dictionary_t dict, const char *key)
181 KKASSERT(dict != NULL);
183 prop_dictionary_remove(dict, key);
189 * Initialize an event dictionary, which contains three parameters to
190 * identify the device referred to (name, devnum, kptr) and the affected key.
192 static prop_dictionary_t
193 udev_init_dict_event(cdev_t dev, const char *key)
195 prop_dictionary_t dict;
199 kptr = (uint64_t)(uintptr_t)dev;
200 KKASSERT(dev != NULL);
202 dict = prop_dictionary_create();
204 log(LOG_DEBUG, "udev_init_dict_event: prop_dictionary_create() failed\n");
208 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
210 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
212 if ((error = _udev_dict_set_uint(dict, "devtype", (dev_dflags(dev) & D_TYPEMASK))))
214 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
216 if ((error = _udev_dict_set_cstr(dict, "key", __DECONST(char *, key))))
222 prop_object_release(dict);
227 udev_dict_set_cstr(cdev_t dev, const char *key, char *str)
229 prop_dictionary_t dict;
232 KKASSERT(dev != NULL);
234 if (dev->si_dict == NULL) {
235 error = udev_init_dict(dev);
240 /* Queue a key update event */
241 dict = udev_init_dict_event(dev, key);
245 if ((error = _udev_dict_set_cstr(dict, "value", str))) {
246 prop_object_release(dict);
249 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
250 prop_object_release(dict);
252 error = _udev_dict_set_cstr(dev->si_dict, key, str);
257 udev_dict_set_int(cdev_t dev, const char *key, int64_t val)
259 prop_dictionary_t dict;
262 KKASSERT(dev != NULL);
264 if (dev->si_dict == NULL) {
265 error = udev_init_dict(dev);
270 /* Queue a key update event */
271 dict = udev_init_dict_event(dev, key);
274 if ((error = _udev_dict_set_int(dict, "value", val))) {
275 prop_object_release(dict);
278 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
279 prop_object_release(dict);
281 return _udev_dict_set_int(dev->si_dict, key, val);
285 udev_dict_set_uint(cdev_t dev, const char *key, uint64_t val)
287 prop_dictionary_t dict;
290 KKASSERT(dev != NULL);
292 if (dev->si_dict == NULL) {
293 error = udev_init_dict(dev);
298 /* Queue a key update event */
299 dict = udev_init_dict_event(dev, key);
302 if ((error = _udev_dict_set_uint(dict, "value", val))) {
303 prop_object_release(dict);
306 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
307 prop_object_release(dict);
309 return _udev_dict_set_uint(dev->si_dict, key, val);
313 udev_dict_delete_key(cdev_t dev, const char *key)
315 prop_dictionary_t dict;
317 KKASSERT(dev != NULL);
319 /* Queue a key removal event */
320 dict = udev_init_dict_event(dev, key);
323 udev_event_insert(UDEV_EV_KEY_REMOVE, dict);
324 prop_object_release(dict);
326 return _udev_dict_delete_key(dev->si_dict, key);
330 udev_init_dict(cdev_t dev)
332 prop_dictionary_t dict;
336 kptr = (uint64_t)(uintptr_t)dev;
338 KKASSERT(dev != NULL);
340 if (dev->si_dict != NULL) {
343 "udev_init_dict: new dict for %s, but has dict already (%p)!\n",
344 dev->si_name, dev->si_dict);
349 dict = prop_dictionary_create();
351 log(LOG_DEBUG, "udev_init_dict: prop_dictionary_create() failed\n");
355 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
357 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
359 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
361 if ((error = _udev_dict_set_uint(dict, "devtype", (dev_dflags(dev) & D_TYPEMASK))))
364 /* XXX: The next 3 are marginallly useful, if at all */
365 if ((error = _udev_dict_set_uint(dict, "uid", dev->si_uid)))
367 if ((error = _udev_dict_set_uint(dict, "gid", dev->si_gid)))
369 if ((error = _udev_dict_set_int(dict, "mode", dev->si_perms)))
372 if ((error = _udev_dict_set_int(dict, "major", umajor(dev->si_inode))))
374 if ((error = _udev_dict_set_int(dict, "minor", dev->si_uminor)))
376 if (dev->si_ops->head.name != NULL) {
377 if ((error = _udev_dict_set_cstr(dict, "driver",
378 __DECONST(char *, dev->si_ops->head.name))))
387 prop_object_release(dict);
392 udev_destroy_dict(cdev_t dev)
394 KKASSERT(dev != NULL);
396 if (dev->si_dict != NULL) {
397 prop_object_release(dev->si_dict);
405 udev_event_insert(int ev_type, prop_dictionary_t dict)
407 struct udev_event_kernel *ev;
409 /* Only start queing events after client has initiated properly */
410 if (!udevctx.initiated)
413 /* XXX: use objcache eventually */
414 ev = kmalloc(sizeof(*ev), M_UDEV, M_WAITOK);
415 ev->ev.ev_dict = prop_dictionary_copy(dict);
416 if (ev->ev.ev_dict == NULL) {
420 ev->ev.ev_type = ev_type;
422 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
423 TAILQ_INSERT_TAIL(&udevctx.ev_queue, ev, link);
425 lockmgr(&udevctx.lock, LK_RELEASE);
428 KNOTE(&udevctx.kq.ki_note, 0);
431 static struct udev_event_kernel *
432 udev_event_remove(void)
434 struct udev_event_kernel *ev;
436 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
437 if (TAILQ_EMPTY(&udevctx.ev_queue)) {
438 lockmgr(&udevctx.lock, LK_RELEASE);
442 ev = TAILQ_FIRST(&udevctx.ev_queue);
443 TAILQ_REMOVE(&udevctx.ev_queue, ev, link);
445 lockmgr(&udevctx.lock, LK_RELEASE);
451 udev_event_free(struct udev_event_kernel *ev)
453 /* XXX: use objcache eventually */
458 udev_event_externalize(struct udev_event_kernel *ev)
460 prop_dictionary_t dict;
465 dict = prop_dictionary_create();
467 log(LOG_DEBUG, "udev_event_externalize: prop_dictionary_create() failed\n");
471 if ((error = _udev_dict_set_int(dict, "evtype", ev->ev.ev_type))) {
472 prop_object_release(dict);
476 if (prop_dictionary_set(dict, "evdict", ev->ev.ev_dict) == false) {
477 prop_object_release(dict);
481 prop_object_release(ev->ev.ev_dict);
483 xml = prop_dictionary_externalize(dict);
485 prop_object_release(dict);
491 udev_event_attach(cdev_t dev, char *name, int alias)
493 prop_dictionary_t dict;
496 KKASSERT(dev != NULL);
501 dict = prop_dictionary_copy(dev->si_dict);
505 if ((error = _udev_dict_set_cstr(dict, "name", name))) {
506 prop_object_release(dict);
510 _udev_dict_set_int(dict, "alias", 1);
512 udev_event_insert(UDEV_EVENT_ATTACH, dict);
513 prop_object_release(dict);
515 error = udev_init_dict(dev);
519 _udev_dict_set_int(dev->si_dict, "alias", 0);
520 udev_event_insert(UDEV_EVENT_ATTACH, dev->si_dict);
528 udev_event_detach(cdev_t dev, char *name, int alias)
530 prop_dictionary_t dict;
532 KKASSERT(dev != NULL);
535 dict = prop_dictionary_copy(dev->si_dict);
539 if (_udev_dict_set_cstr(dict, "name", name)) {
540 prop_object_release(dict);
544 _udev_dict_set_int(dict, "alias", 1);
546 udev_event_insert(UDEV_EVENT_DETACH, dict);
547 prop_object_release(dict);
549 udev_event_insert(UDEV_EVENT_DETACH, dev->si_dict);
553 udev_destroy_dict(dev);
562 udev_dev_open(struct dev_open_args *ap)
573 udev_dev_close(struct dev_close_args *ap)
576 udevctx.initiated = 0;
582 static struct filterops udev_dev_read_filtops =
583 { FILTEROP_ISFD, NULL, udev_dev_filter_detach, udev_dev_filter_read };
586 udev_dev_kqfilter(struct dev_kqfilter_args *ap)
588 struct knote *kn = ap->a_kn;
592 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
594 switch (kn->kn_filter) {
596 kn->kn_fop = &udev_dev_read_filtops;
599 ap->a_result = EOPNOTSUPP;
600 lockmgr(&udevctx.lock, LK_RELEASE);
604 klist = &udevctx.kq.ki_note;
605 knote_insert(klist, kn);
607 lockmgr(&udevctx.lock, LK_RELEASE);
613 udev_dev_filter_detach(struct knote *kn)
617 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
618 klist = &udevctx.kq.ki_note;
619 knote_remove(klist, kn);
620 lockmgr(&udevctx.lock, LK_RELEASE);
624 udev_dev_filter_read(struct knote *kn, long hint)
628 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
629 if (!TAILQ_EMPTY(&udevctx.ev_queue))
631 lockmgr(&udevctx.lock, LK_RELEASE);
637 udev_dev_read(struct dev_read_args *ap)
639 struct udev_event_kernel *ev;
640 struct uio *uio = ap->a_uio;
646 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
649 if ((ev = udev_event_remove()) != NULL) {
650 if ((xml = udev_event_externalize(ev)) == NULL) {
651 lockmgr(&udevctx.lock, LK_RELEASE);
655 len = strlen(xml) + 1; /* account for NULL-termination */
656 if (uio->uio_resid < len) {
659 error = uiomove((caddr_t)xml, len, uio);
664 lockmgr(&udevctx.lock, LK_RELEASE);
668 if ((error = lksleep(&udevctx, &udevctx.lock, 0, "udevq", 0))) {
669 lockmgr(&udevctx.lock, LK_RELEASE);
674 lockmgr(&udevctx.lock, LK_RELEASE);
679 udev_dev_ioctl(struct dev_ioctl_args *ap)
681 prop_dictionary_t dict;
684 struct plistref *pref;
691 /* Use proplib(3) for userspace/kernel communication */
692 pref = (struct plistref *)ap->a_data;
693 error = prop_dictionary_copyin_ioctl(pref, ap->a_cmd, &dict);
697 po = prop_dictionary_get(dict, "command");
698 if (po == NULL || prop_object_type(po) != PROP_TYPE_STRING) {
699 log(LOG_DEBUG, "udev: prop_dictionary_get() failed\n");
700 prop_object_release(dict);
706 for(i = 0; cmd_fn[i].cmd != NULL; i++) {
707 if (prop_string_equals_cstring(ps, cmd_fn[i].cmd))
711 if (cmd_fn[i].cmd != NULL) {
712 error = cmd_fn[i].fn(pref, ap->a_cmd, dict);
717 //prop_object_release(po);
718 prop_object_release(dict);
721 error = ENOTTY; /* Inappropriate ioctl for device */
729 udev_getdevs_scan_callback(cdev_t cdev, void *arg)
731 struct udev_prop_ctx *ctx = arg;
733 KKASSERT(arg != NULL);
735 if (cdev->si_dict == NULL)
738 if (prop_array_add(ctx->cdevs, cdev->si_dict) == false) {
745 udev_getdevs_ioctl(struct plistref *pref, u_long cmd, prop_dictionary_t dict)
747 prop_dictionary_t odict;
748 struct udev_prop_ctx ctx;
752 ctx.cdevs = prop_array_create();
753 if (ctx.cdevs == NULL) {
754 log(LOG_DEBUG, "udev_getdevs_ioctl: prop_array_create() failed\n");
758 /* XXX: need devfs_scan_alias_callback() */
759 devfs_scan_callback(udev_getdevs_scan_callback, &ctx);
761 if (ctx.error != 0) {
762 prop_object_release(ctx.cdevs);
765 udevctx.initiated = 1;
767 odict = prop_dictionary_create();
772 if ((prop_dictionary_set(odict, "array", ctx.cdevs)) == 0) {
773 log(LOG_DEBUG, "udev_getdevs_ioctl: prop_dictionary_set failed\n");
774 prop_object_release(odict);
778 error = prop_dictionary_copyout_ioctl(pref, cmd, odict);
780 prop_object_release(odict);
791 lockinit(&udevctx.lock, "udevevq", 0, LK_CANRECURSE);
792 TAILQ_INIT(&udevctx.ev_queue);
803 udev_dev = make_dev(&udev_dev_ops,
812 udev_dev_uninit(void)
814 destroy_dev(udev_dev);
817 SYSINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY, udev_init, NULL);
818 SYSUNINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY, udev_uninit, NULL);
819 SYSINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY, udev_dev_init, NULL);
820 SYSUNINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY, udev_dev_uninit, NULL);