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/ioccom.h>
42 #include <sys/malloc.h>
43 #include <sys/ctype.h>
44 #include <sys/syslog.h>
46 #include <sys/devfs.h>
47 #include <libprop/proplib.h>
49 #include <sys/thread2.h>
51 MALLOC_DEFINE(M_UDEV, "udev", "udev allocs");
53 /* XXX: use UUIDs for identification; would need help from devfs */
55 static cdev_t udev_dev;
56 static d_open_t udev_dev_open;
57 static d_close_t udev_dev_close;
58 static d_read_t udev_dev_read;
59 static d_kqfilter_t udev_dev_kqfilter;
60 static d_ioctl_t udev_dev_ioctl;
62 static int _udev_dict_set_cstr(prop_dictionary_t, const char *, char *);
63 static int _udev_dict_set_int(prop_dictionary_t, const char *, int64_t);
64 static int _udev_dict_set_uint(prop_dictionary_t, const char *, uint64_t);
65 static int _udev_dict_delete_key(prop_dictionary_t, const char *);
66 static prop_dictionary_t udev_init_dict_event(cdev_t, const char *);
67 static int udev_init_dict(cdev_t);
68 static int udev_destroy_dict(cdev_t);
69 static void udev_event_insert(int, prop_dictionary_t);
70 static struct udev_event_kernel *udev_event_remove(void);
71 static void udev_event_free(struct udev_event_kernel *);
72 static char *udev_event_externalize(struct udev_event_kernel *);
73 static void udev_getdevs_scan_callback(cdev_t, void *);
74 static int udev_getdevs_ioctl(struct plistref *, u_long, prop_dictionary_t);
75 static void udev_dev_filter_detach(struct knote *);
76 static int udev_dev_filter_read(struct knote *, long);
80 int (*fn)(struct plistref *, u_long, prop_dictionary_t);
83 struct udev_prop_ctx {
88 struct udev_event_kernel {
90 TAILQ_ENTRY(udev_event_kernel) link;
101 TAILQ_HEAD(, udev_event_kernel) ev_queue; /* list of thread_io */
104 static struct dev_ops udev_dev_ops = {
106 .d_open = udev_dev_open,
107 .d_close = udev_dev_close,
108 .d_read = udev_dev_read,
109 .d_kqfilter = udev_dev_kqfilter,
110 .d_ioctl = udev_dev_ioctl
113 static struct cmd_function cmd_fn[] = {
114 { .cmd = "getdevs", .fn = udev_getdevs_ioctl},
119 _udev_dict_set_cstr(prop_dictionary_t dict, const char *key, char *str)
123 KKASSERT(dict != NULL);
125 ps = prop_string_create_cstring(str);
130 if (prop_dictionary_set(dict, key, ps) == false) {
131 prop_object_release(ps);
135 prop_object_release(ps);
140 _udev_dict_set_int(prop_dictionary_t dict, const char *key, int64_t val)
144 KKASSERT(dict != NULL);
146 pn = prop_number_create_integer(val);
150 if (prop_dictionary_set(dict, key, pn) == false) {
151 prop_object_release(pn);
155 prop_object_release(pn);
160 _udev_dict_set_uint(prop_dictionary_t dict, const char *key, uint64_t val)
164 KKASSERT(dict != NULL);
166 pn = prop_number_create_unsigned_integer(val);
170 if (prop_dictionary_set(dict, key, pn) == false) {
171 prop_object_release(pn);
175 prop_object_release(pn);
180 _udev_dict_delete_key(prop_dictionary_t dict, const char *key)
182 KKASSERT(dict != NULL);
184 prop_dictionary_remove(dict, key);
190 * Initialize an event dictionary, which contains three parameters to
191 * identify the device referred to (name, devnum, kptr) and the affected key.
193 static prop_dictionary_t
194 udev_init_dict_event(cdev_t dev, const char *key)
196 prop_dictionary_t dict;
200 kptr = (uint64_t)(uintptr_t)dev;
201 KKASSERT(dev != NULL);
203 dict = prop_dictionary_create();
205 log(LOG_DEBUG, "udev_init_dict_event: prop_dictionary_create() failed\n");
209 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
211 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
213 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
215 if ((error = _udev_dict_set_cstr(dict, "key", __DECONST(char *, key))))
221 prop_object_release(dict);
226 udev_dict_set_cstr(cdev_t dev, const char *key, char *str)
228 prop_dictionary_t dict;
231 KKASSERT(dev != NULL);
233 if (dev->si_dict == NULL) {
234 error = udev_init_dict(dev);
239 /* Queue a key update event */
240 dict = udev_init_dict_event(dev, key);
244 if ((error = _udev_dict_set_cstr(dict, "value", str))) {
245 prop_object_release(dict);
248 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
249 prop_object_release(dict);
251 error = _udev_dict_set_cstr(dev->si_dict, key, str);
256 udev_dict_set_int(cdev_t dev, const char *key, int64_t val)
258 prop_dictionary_t dict;
261 KKASSERT(dev != NULL);
263 if (dev->si_dict == NULL) {
264 error = udev_init_dict(dev);
269 /* Queue a key update event */
270 dict = udev_init_dict_event(dev, key);
273 if ((error = _udev_dict_set_int(dict, "value", val))) {
274 prop_object_release(dict);
277 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
278 prop_object_release(dict);
280 return _udev_dict_set_int(dev->si_dict, key, val);
284 udev_dict_set_uint(cdev_t dev, const char *key, uint64_t val)
286 prop_dictionary_t dict;
289 KKASSERT(dev != NULL);
291 if (dev->si_dict == NULL) {
292 error = udev_init_dict(dev);
297 /* Queue a key update event */
298 dict = udev_init_dict_event(dev, key);
301 if ((error = _udev_dict_set_uint(dict, "value", val))) {
302 prop_object_release(dict);
305 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
306 prop_object_release(dict);
308 return _udev_dict_set_uint(dev->si_dict, key, val);
312 udev_dict_delete_key(cdev_t dev, const char *key)
314 prop_dictionary_t dict;
316 KKASSERT(dev != NULL);
318 /* Queue a key removal event */
319 dict = udev_init_dict_event(dev, key);
322 udev_event_insert(UDEV_EV_KEY_REMOVE, dict);
323 prop_object_release(dict);
325 return _udev_dict_delete_key(dev->si_dict, key);
329 udev_init_dict(cdev_t dev)
331 prop_dictionary_t dict;
335 kptr = (uint64_t)(uintptr_t)dev;
337 KKASSERT(dev != NULL);
339 if (dev->si_dict != NULL) {
342 "udev_init_dict: new dict for %s, but has dict already (%p)!\n",
343 dev->si_name, dev->si_dict);
348 dict = prop_dictionary_create();
350 log(LOG_DEBUG, "udev_init_dict: prop_dictionary_create() failed\n");
354 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
356 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
358 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
361 /* XXX: The next 3 are marginallly useful, if at all */
362 if ((error = _udev_dict_set_uint(dict, "uid", dev->si_uid)))
364 if ((error = _udev_dict_set_uint(dict, "gid", dev->si_gid)))
366 if ((error = _udev_dict_set_int(dict, "mode", dev->si_perms)))
369 if ((error = _udev_dict_set_int(dict, "major", umajor(dev->si_inode))))
371 if ((error = _udev_dict_set_int(dict, "minor", dev->si_uminor)))
373 if (dev->si_ops->head.name != NULL) {
374 if ((error = _udev_dict_set_cstr(dict, "driver",
375 __DECONST(char *, dev->si_ops->head.name))))
384 prop_object_release(dict);
389 udev_destroy_dict(cdev_t dev)
391 KKASSERT(dev != NULL);
393 if (dev->si_dict != NULL) {
394 prop_object_release(dev->si_dict);
402 udev_event_insert(int ev_type, prop_dictionary_t dict)
404 struct udev_event_kernel *ev;
406 /* Only start queing events after client has initiated properly */
407 if (!udevctx.initiated)
410 /* XXX: use objcache eventually */
411 ev = kmalloc(sizeof(*ev), M_UDEV, M_WAITOK);
412 ev->ev.ev_dict = prop_dictionary_copy(dict);
413 if (ev->ev.ev_dict == NULL) {
417 ev->ev.ev_type = ev_type;
419 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
420 TAILQ_INSERT_TAIL(&udevctx.ev_queue, ev, link);
422 lockmgr(&udevctx.lock, LK_RELEASE);
425 KNOTE(&udevctx.kq.ki_note, 0);
428 static struct udev_event_kernel *
429 udev_event_remove(void)
431 struct udev_event_kernel *ev;
433 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
434 if (TAILQ_EMPTY(&udevctx.ev_queue)) {
435 lockmgr(&udevctx.lock, LK_RELEASE);
439 ev = TAILQ_FIRST(&udevctx.ev_queue);
440 TAILQ_REMOVE(&udevctx.ev_queue, ev, link);
442 lockmgr(&udevctx.lock, LK_RELEASE);
448 udev_event_free(struct udev_event_kernel *ev)
450 /* XXX: use objcache eventually */
455 udev_event_externalize(struct udev_event_kernel *ev)
457 prop_dictionary_t dict;
462 dict = prop_dictionary_create();
464 log(LOG_DEBUG, "udev_event_externalize: prop_dictionary_create() failed\n");
468 if ((error = _udev_dict_set_int(dict, "evtype", ev->ev.ev_type))) {
469 prop_object_release(dict);
473 if (prop_dictionary_set(dict, "evdict", ev->ev.ev_dict) == false) {
474 prop_object_release(dict);
478 prop_object_release(ev->ev.ev_dict);
480 xml = prop_dictionary_externalize(dict);
482 prop_object_release(dict);
488 udev_event_attach(cdev_t dev, char *name, int alias)
490 prop_dictionary_t dict;
493 KKASSERT(dev != NULL);
498 dict = prop_dictionary_copy(dev->si_dict);
502 if ((error = _udev_dict_set_cstr(dict, "name", name))) {
503 prop_object_release(dict);
507 _udev_dict_set_int(dict, "alias", 1);
509 udev_event_insert(UDEV_EVENT_ATTACH, dict);
510 prop_object_release(dict);
512 error = udev_init_dict(dev);
516 _udev_dict_set_int(dev->si_dict, "alias", 0);
517 udev_event_insert(UDEV_EVENT_ATTACH, dev->si_dict);
525 udev_event_detach(cdev_t dev, char *name, int alias)
527 prop_dictionary_t dict;
529 KKASSERT(dev != NULL);
532 dict = prop_dictionary_copy(dev->si_dict);
536 if (_udev_dict_set_cstr(dict, "name", name)) {
537 prop_object_release(dict);
541 _udev_dict_set_int(dict, "alias", 1);
543 udev_event_insert(UDEV_EVENT_DETACH, dict);
544 prop_object_release(dict);
546 udev_event_insert(UDEV_EVENT_DETACH, dev->si_dict);
550 udev_destroy_dict(dev);
559 udev_dev_open(struct dev_open_args *ap)
570 udev_dev_close(struct dev_close_args *ap)
573 udevctx.initiated = 0;
579 static struct filterops udev_dev_read_filtops =
580 { FILTEROP_ISFD, NULL, udev_dev_filter_detach, udev_dev_filter_read };
583 udev_dev_kqfilter(struct dev_kqfilter_args *ap)
585 struct knote *kn = ap->a_kn;
589 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
591 switch (kn->kn_filter) {
593 kn->kn_fop = &udev_dev_read_filtops;
596 ap->a_result = EOPNOTSUPP;
597 lockmgr(&udevctx.lock, LK_RELEASE);
601 klist = &udevctx.kq.ki_note;
602 knote_insert(klist, kn);
604 lockmgr(&udevctx.lock, LK_RELEASE);
610 udev_dev_filter_detach(struct knote *kn)
614 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
615 klist = &udevctx.kq.ki_note;
616 knote_remove(klist, kn);
617 lockmgr(&udevctx.lock, LK_RELEASE);
621 udev_dev_filter_read(struct knote *kn, long hint)
625 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
626 if (!TAILQ_EMPTY(&udevctx.ev_queue))
628 lockmgr(&udevctx.lock, LK_RELEASE);
634 udev_dev_read(struct dev_read_args *ap)
636 struct udev_event_kernel *ev;
637 struct uio *uio = ap->a_uio;
643 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
646 if ((ev = udev_event_remove()) != NULL) {
647 if ((xml = udev_event_externalize(ev)) == NULL) {
648 lockmgr(&udevctx.lock, LK_RELEASE);
652 len = strlen(xml) + 1; /* account for NULL-termination */
653 if (uio->uio_resid < len) {
656 error = uiomove((caddr_t)xml, len, uio);
661 lockmgr(&udevctx.lock, LK_RELEASE);
665 if ((error = lksleep(&udevctx, &udevctx.lock, 0, "udevq", 0))) {
666 lockmgr(&udevctx.lock, LK_RELEASE);
671 lockmgr(&udevctx.lock, LK_RELEASE);
676 udev_dev_ioctl(struct dev_ioctl_args *ap)
678 prop_dictionary_t dict;
681 struct plistref *pref;
688 /* Use proplib(3) for userspace/kernel communication */
689 pref = (struct plistref *)ap->a_data;
690 error = prop_dictionary_copyin_ioctl(pref, ap->a_cmd, &dict);
694 po = prop_dictionary_get(dict, "command");
695 if (po == NULL || prop_object_type(po) != PROP_TYPE_STRING) {
696 log(LOG_DEBUG, "udev: prop_dictionary_get() failed\n");
697 prop_object_release(dict);
703 for(i = 0; cmd_fn[i].cmd != NULL; i++) {
704 if (prop_string_equals_cstring(ps, cmd_fn[i].cmd))
708 if (cmd_fn[i].cmd != NULL) {
709 error = cmd_fn[i].fn(pref, ap->a_cmd, dict);
714 //prop_object_release(po);
715 prop_object_release(dict);
718 error = ENOTTY; /* Inappropriate ioctl for device */
726 udev_getdevs_scan_callback(cdev_t cdev, void *arg)
728 struct udev_prop_ctx *ctx = arg;
730 KKASSERT(arg != NULL);
732 if (cdev->si_dict == NULL)
735 if (prop_array_add(ctx->cdevs, cdev->si_dict) == false) {
742 udev_getdevs_ioctl(struct plistref *pref, u_long cmd, prop_dictionary_t dict)
744 prop_dictionary_t odict;
745 struct udev_prop_ctx ctx;
749 ctx.cdevs = prop_array_create();
750 if (ctx.cdevs == NULL) {
751 log(LOG_DEBUG, "udev_getdevs_ioctl: prop_array_create() failed\n");
755 /* XXX: need devfs_scan_alias_callback() */
756 devfs_scan_callback(udev_getdevs_scan_callback, &ctx);
758 if (ctx.error != 0) {
759 prop_object_release(ctx.cdevs);
762 udevctx.initiated = 1;
764 odict = prop_dictionary_create();
769 if ((prop_dictionary_set(odict, "array", ctx.cdevs)) == 0) {
770 log(LOG_DEBUG, "udev_getdevs_ioctl: prop_dictionary_set failed\n");
771 prop_object_release(odict);
775 error = prop_dictionary_copyout_ioctl(pref, cmd, odict);
777 prop_object_release(odict);
788 lockinit(&udevctx.lock, "udevevq", 0, LK_CANRECURSE);
789 TAILQ_INIT(&udevctx.ev_queue);
800 udev_dev = make_dev(&udev_dev_ops,
809 udev_dev_uninit(void)
811 destroy_dev(udev_dev);
814 SYSINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY, udev_init, NULL);
815 SYSUNINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY, udev_uninit, NULL);
816 SYSINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY, udev_dev_init, NULL);
817 SYSUNINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY, udev_dev_uninit, NULL);