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>
41 #include <sys/event.h>
42 #include <sys/ioccom.h>
43 #include <sys/malloc.h>
44 #include <sys/ctype.h>
45 #include <sys/syslog.h>
47 #include <sys/devfs.h>
48 #include <libprop/proplib.h>
50 #include <sys/thread2.h>
52 MALLOC_DEFINE(M_UDEV, "udev", "udev allocs");
54 /* XXX: use UUIDs for identification; would need help from devfs */
56 static cdev_t udev_dev;
57 static d_open_t udev_dev_open;
58 static d_close_t udev_dev_close;
59 static d_read_t udev_dev_read;
60 static d_poll_t udev_dev_poll;
61 static d_kqfilter_t udev_dev_kqfilter;
62 static d_ioctl_t udev_dev_ioctl;
64 static int _udev_dict_set_cstr(prop_dictionary_t, const char *, char *);
65 static int _udev_dict_set_int(prop_dictionary_t, const char *, int64_t);
66 static int _udev_dict_set_uint(prop_dictionary_t, const char *, uint64_t);
67 static int _udev_dict_delete_key(prop_dictionary_t, const char *);
68 static prop_dictionary_t udev_init_dict_event(cdev_t, const char *);
69 static int udev_init_dict(cdev_t);
70 static int udev_destroy_dict(cdev_t);
71 static void udev_event_insert(int, prop_dictionary_t);
72 static struct udev_event_kernel *udev_event_remove(void);
73 static void udev_event_free(struct udev_event_kernel *);
74 static char *udev_event_externalize(struct udev_event_kernel *);
75 static void udev_getdevs_scan_callback(cdev_t, void *);
76 static int udev_getdevs_ioctl(struct plistref *, u_long, prop_dictionary_t);
77 static void udev_dev_filter_detach(struct knote *);
78 static int udev_dev_filter_read(struct knote *, long);
82 int (*fn)(struct plistref *, u_long, prop_dictionary_t);
85 struct udev_prop_ctx {
90 struct udev_event_kernel {
92 TAILQ_ENTRY(udev_event_kernel) link;
103 TAILQ_HEAD(, udev_event_kernel) ev_queue; /* list of thread_io */
106 static struct dev_ops udev_dev_ops = {
107 { "udev", 0, D_KQFILTER },
108 .d_open = udev_dev_open,
109 .d_close = udev_dev_close,
110 .d_read = udev_dev_read,
111 .d_poll = udev_dev_poll,
112 .d_kqfilter = udev_dev_kqfilter,
113 .d_ioctl = udev_dev_ioctl
116 struct cmd_function cmd_fn[] = {
117 { .cmd = "getdevs", .fn = udev_getdevs_ioctl},
122 _udev_dict_set_cstr(prop_dictionary_t dict, const char *key, char *str)
126 KKASSERT(dict != NULL);
128 ps = prop_string_create_cstring(str);
132 if (prop_dictionary_set(dict, key, ps) == false) {
133 prop_object_release(ps);
137 prop_object_release(ps);
142 _udev_dict_set_int(prop_dictionary_t dict, const char *key, int64_t val)
146 KKASSERT(dict != NULL);
148 pn = prop_number_create_integer(val);
152 if (prop_dictionary_set(dict, key, pn) == false) {
153 prop_object_release(pn);
157 prop_object_release(pn);
162 _udev_dict_set_uint(prop_dictionary_t dict, const char *key, uint64_t val)
166 KKASSERT(dict != NULL);
168 pn = prop_number_create_unsigned_integer(val);
172 if (prop_dictionary_set(dict, key, pn) == false) {
173 prop_object_release(pn);
177 prop_object_release(pn);
182 _udev_dict_delete_key(prop_dictionary_t dict, const char *key)
184 KKASSERT(dict != NULL);
186 prop_dictionary_remove(dict, key);
192 * Initialize an event dictionary, which contains three parameters to
193 * identify the device referred to (name, devnum, kptr) and the affected key.
195 static prop_dictionary_t
196 udev_init_dict_event(cdev_t dev, const char *key)
198 prop_dictionary_t dict;
202 kptr = (uint64_t)(uintptr_t)dev;
203 KKASSERT(dev != NULL);
205 dict = prop_dictionary_create();
207 log(LOG_DEBUG, "udev_init_dict_event: prop_dictionary_create() failed\n");
211 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
213 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
215 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
217 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 /* Queue a key update event */
234 dict = udev_init_dict_event(dev, key);
237 if ((error = _udev_dict_set_cstr(dict, "value", str))) {
238 prop_object_release(dict);
241 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
242 prop_object_release(dict);
244 return _udev_dict_set_cstr(dev->si_dict, key, str);
248 udev_dict_set_int(cdev_t dev, const char *key, int64_t val)
250 prop_dictionary_t dict;
253 KKASSERT(dev != NULL);
255 /* Queue a key update event */
256 dict = udev_init_dict_event(dev, key);
259 if ((error = _udev_dict_set_int(dict, "value", val))) {
260 prop_object_release(dict);
263 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
264 prop_object_release(dict);
266 return _udev_dict_set_int(dev->si_dict, key, val);
270 udev_dict_set_uint(cdev_t dev, const char *key, uint64_t val)
272 prop_dictionary_t dict;
275 KKASSERT(dev != NULL);
277 /* Queue a key update event */
278 dict = udev_init_dict_event(dev, key);
281 if ((error = _udev_dict_set_uint(dict, "value", val))) {
282 prop_object_release(dict);
285 udev_event_insert(UDEV_EV_KEY_UPDATE, dict);
286 prop_object_release(dict);
288 return _udev_dict_set_uint(dev->si_dict, key, val);
292 udev_dict_delete_key(cdev_t dev, const char *key)
294 prop_dictionary_t dict;
296 KKASSERT(dev != NULL);
298 /* Queue a key removal event */
299 dict = udev_init_dict_event(dev, key);
302 udev_event_insert(UDEV_EV_KEY_REMOVE, dict);
303 prop_object_release(dict);
305 return _udev_dict_delete_key(dev->si_dict, key);
309 udev_init_dict(cdev_t dev)
311 prop_dictionary_t dict;
315 kptr = (uint64_t)(uintptr_t)dev;
317 KKASSERT(dev != NULL);
318 dict = prop_dictionary_create();
320 log(LOG_DEBUG, "udev_init_dict: prop_dictionary_create() failed\n");
324 if ((error = _udev_dict_set_cstr(dict, "name", dev->si_name)))
326 if ((error = _udev_dict_set_uint(dict, "devnum", dev->si_inode)))
328 if ((error = _udev_dict_set_uint(dict, "kptr", kptr)))
331 /* XXX: The next 3 are marginallly useful, if at all */
332 if ((error = _udev_dict_set_uint(dict, "uid", dev->si_uid)))
334 if ((error = _udev_dict_set_uint(dict, "gid", dev->si_gid)))
336 if ((error = _udev_dict_set_int(dict, "mode", dev->si_perms)))
339 if ((error = _udev_dict_set_int(dict, "major", umajor(dev->si_inode))))
341 if ((error = _udev_dict_set_int(dict, "minor", dev->si_uminor)))
349 prop_object_release(dict);
354 udev_destroy_dict(cdev_t dev)
356 KKASSERT(dev != NULL);
358 if (dev->si_dict != NULL) {
359 prop_object_release(dev->si_dict);
367 udev_event_insert(int ev_type, prop_dictionary_t dict)
369 struct udev_event_kernel *ev;
371 /* Only start queing events after client has initiated properly */
372 if (!udevctx.initiated)
375 /* XXX: use objcache eventually */
376 ev = kmalloc(sizeof(*ev), M_UDEV, M_WAITOK);
377 ev->ev.ev_dict = prop_dictionary_copy(dict);
378 if (ev->ev.ev_dict == NULL) {
382 ev->ev.ev_type = ev_type;
384 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
385 TAILQ_INSERT_TAIL(&udevctx.ev_queue, ev, link);
387 lockmgr(&udevctx.lock, LK_RELEASE);
390 selwakeup(&udevctx.sel);
393 static struct udev_event_kernel *
394 udev_event_remove(void)
396 struct udev_event_kernel *ev;
398 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
399 if (TAILQ_EMPTY(&udevctx.ev_queue)) {
400 lockmgr(&udevctx.lock, LK_RELEASE);
404 ev = TAILQ_FIRST(&udevctx.ev_queue);
405 TAILQ_REMOVE(&udevctx.ev_queue, ev, link);
407 lockmgr(&udevctx.lock, LK_RELEASE);
413 udev_event_free(struct udev_event_kernel *ev)
415 /* XXX: use objcache eventually */
420 udev_event_externalize(struct udev_event_kernel *ev)
422 prop_dictionary_t dict;
427 dict = prop_dictionary_create();
429 log(LOG_DEBUG, "udev_event_externalize: prop_dictionary_create() failed\n");
433 if ((error = _udev_dict_set_int(dict, "evtype", ev->ev.ev_type))) {
434 prop_object_release(dict);
438 if (prop_dictionary_set(dict, "evdict", ev->ev.ev_dict) == false) {
439 prop_object_release(dict);
443 prop_object_release(ev->ev.ev_dict);
445 xml = prop_dictionary_externalize(dict);
447 prop_object_release(dict);
453 udev_event_attach(cdev_t dev, char *name, int alias)
455 prop_dictionary_t dict;
458 KKASSERT(dev != NULL);
460 error = udev_init_dict(dev);
465 dict = prop_dictionary_copy(dev->si_dict);
469 if ((error = _udev_dict_set_cstr(dict, "name", name))) {
470 prop_object_release(dict);
474 _udev_dict_set_int(dict, "alias", 1);
476 udev_event_insert(UDEV_EVENT_ATTACH, dict);
477 prop_object_release(dict);
479 _udev_dict_set_int(dev->si_dict, "alias", 0);
480 udev_event_insert(UDEV_EVENT_ATTACH, dev->si_dict);
488 udev_event_detach(cdev_t dev, char *name, int alias)
490 prop_dictionary_t dict;
492 KKASSERT(dev != NULL);
495 dict = prop_dictionary_copy(dev->si_dict);
499 if (_udev_dict_set_cstr(dict, "name", name)) {
500 prop_object_release(dict);
504 _udev_dict_set_int(dict, "alias", 1);
506 udev_event_insert(UDEV_EVENT_DETACH, dict);
507 prop_object_release(dict);
509 udev_event_insert(UDEV_EVENT_DETACH, dev->si_dict);
513 udev_destroy_dict(dev);
522 udev_dev_open(struct dev_open_args *ap)
533 udev_dev_close(struct dev_close_args *ap)
536 udevctx.initiated = 0;
543 udev_dev_poll(struct dev_poll_args *ap)
547 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
548 if (ap->a_events & (POLLIN | POLLRDNORM)) {
549 if (!TAILQ_EMPTY(&udevctx.ev_queue))
550 revents = ap->a_events & (POLLIN | POLLRDNORM);
552 selrecord(curthread, &udevctx.sel);
554 lockmgr(&udevctx.lock, LK_RELEASE);
556 ap->a_events = revents;
560 static struct filterops udev_dev_read_filtops =
561 { 1, NULL, udev_dev_filter_detach, udev_dev_filter_read };
564 udev_dev_kqfilter(struct dev_kqfilter_args *ap)
566 struct knote *kn = ap->a_kn;
570 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
572 switch (kn->kn_filter) {
574 kn->kn_fop = &udev_dev_read_filtops;
577 ap->a_result = EOPNOTSUPP;
578 lockmgr(&udevctx.lock, LK_RELEASE);
583 klist = &udevctx.sel.si_note;
584 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
587 lockmgr(&udevctx.lock, LK_RELEASE);
593 udev_dev_filter_detach(struct knote *kn)
597 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
599 klist = &udevctx.sel.si_note;
600 SLIST_REMOVE(klist, kn, knote, kn_selnext);
602 lockmgr(&udevctx.lock, LK_RELEASE);
606 udev_dev_filter_read(struct knote *kn, long hint)
610 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
611 if (!TAILQ_EMPTY(&udevctx.ev_queue))
613 lockmgr(&udevctx.lock, LK_RELEASE);
619 udev_dev_read(struct dev_read_args *ap)
621 struct udev_event_kernel *ev;
622 struct uio *uio = ap->a_uio;
628 lockmgr(&udevctx.lock, LK_EXCLUSIVE);
631 if ((ev = udev_event_remove()) != NULL) {
632 if ((xml = udev_event_externalize(ev)) == NULL) {
633 lockmgr(&udevctx.lock, LK_RELEASE);
637 len = strlen(xml) + 1; /* account for NULL-termination */
638 if (uio->uio_resid < len) {
641 error = uiomove((caddr_t)xml, len, uio);
646 lockmgr(&udevctx.lock, LK_RELEASE);
650 if ((error = lksleep(&udevctx, &udevctx.lock, 0, "udevq", 0))) {
651 lockmgr(&udevctx.lock, LK_RELEASE);
656 lockmgr(&udevctx.lock, LK_RELEASE);
661 udev_dev_ioctl(struct dev_ioctl_args *ap)
663 prop_dictionary_t dict;
666 struct plistref *pref;
673 /* Use proplib(3) for userspace/kernel communication */
674 pref = (struct plistref *)ap->a_data;
675 error = prop_dictionary_copyin_ioctl(pref, ap->a_cmd, &dict);
679 po = prop_dictionary_get(dict, "command");
680 if (po == NULL || prop_object_type(po) != PROP_TYPE_STRING) {
681 log(LOG_DEBUG, "udev: prop_dictionary_get() failed\n");
682 prop_object_release(dict);
688 for(i = 0; cmd_fn[i].cmd != NULL; i++) {
689 if (prop_string_equals_cstring(ps, cmd_fn[i].cmd))
693 if (cmd_fn[i].cmd != NULL) {
694 log(LOG_DEBUG, "udev: ioctl %s called\n", cmd_fn[i].cmd);
695 error = cmd_fn[i].fn(pref, ap->a_cmd, dict);
700 //prop_object_release(po);
702 prop_object_release(dict);
705 error = ENOTTY; /* Inappropriate ioctl for device */
713 udev_getdevs_scan_callback(cdev_t cdev, void *arg)
715 struct udev_prop_ctx *ctx = arg;
717 KKASSERT(arg != NULL);
719 if (cdev->si_dict == NULL)
722 if (prop_array_add(ctx->cdevs, cdev->si_dict) == false) {
729 udev_getdevs_ioctl(struct plistref *pref, u_long cmd, prop_dictionary_t dict)
731 prop_dictionary_t odict;
732 struct udev_prop_ctx ctx;
736 ctx.cdevs = prop_array_create();
737 if (ctx.cdevs == NULL) {
738 log(LOG_DEBUG, "udev_getdevs_ioctl: prop_array_create() failed\n");
742 /* XXX: need devfs_scan_alias_callback() */
743 devfs_scan_callback(udev_getdevs_scan_callback, &ctx);
745 if (ctx.error != 0) {
746 prop_object_release(ctx.cdevs);
749 udevctx.initiated = 1;
751 odict = prop_dictionary_create();
756 if ((prop_dictionary_set(odict, "array", ctx.cdevs)) == 0) {
757 log(LOG_DEBUG, "udev_getdevs_ioctl: prop_dictionary_set failed\n");
758 prop_object_release(odict);
762 error = prop_dictionary_copyout_ioctl(pref, cmd, odict);
764 /* XXX: need to release ctx.cdevs? */
765 prop_object_release(odict);
776 lockinit(&udevctx.lock, "udevevq", 0, LK_CANRECURSE);
777 TAILQ_INIT(&udevctx.ev_queue);
788 udev_dev = make_dev(&udev_dev_ops,
797 udev_dev_uninit(void)
799 destroy_dev(udev_dev);
802 SYSINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY, udev_init, NULL);
803 SYSUNINIT(subr_udev_register, SI_SUB_CREATE_INIT, SI_ORDER_ANY, udev_uninit, NULL);
804 SYSINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY, udev_dev_init, NULL);
805 SYSUNINIT(subr_udev_dev_register, SI_SUB_DRIVERS, SI_ORDER_ANY, udev_dev_uninit, NULL);