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