1 /*- 2 * Copyright (c) 1999-2002 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/kernel.h> 32 #include <sys/systm.h> 33 #include <sys/bus.h> 34 #include <sys/bio.h> 35 #include <sys/lock.h> 36 #include <sys/mutex.h> 37 #include <sys/module.h> 38 #include <sys/malloc.h> 39 #include <sys/conf.h> 40 #include <sys/vnode.h> 41 #include <sys/queue.h> 42 #include <sys/poll.h> 43 #include <sys/sx.h> 44 #include <sys/ctype.h> 45 #include <sys/ucred.h> 46 #include <sys/taskqueue.h> 47 #include <machine/stdarg.h> 48 49 #include <fs/devfs/devfs_int.h> 50 #include <vm/vm.h> 51 52 static MALLOC_DEFINE(M_DEVT, "cdev", "cdev storage"); 53 54 struct mtx devmtx; 55 static void destroy_devl(struct cdev *dev); 56 static int destroy_dev_sched_cbl(struct cdev *dev, 57 void (*cb)(void *), void *arg); 58 static int make_dev_credv(int flags, struct cdev **dres, struct cdevsw *devsw, 59 int unit, struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, 60 va_list ap); 61 62 static struct cdev_priv_list cdevp_free_list = 63 TAILQ_HEAD_INITIALIZER(cdevp_free_list); 64 static SLIST_HEAD(free_cdevsw, cdevsw) cdevsw_gt_post_list = 65 SLIST_HEAD_INITIALIZER(cdevsw_gt_post_list); 66 67 void 68 dev_lock(void) 69 { 70 71 mtx_lock(&devmtx); 72 } 73 74 /* 75 * Free all the memory collected while the cdev mutex was 76 * locked. Since devmtx is after the system map mutex, free() cannot 77 * be called immediately and is postponed until cdev mutex can be 78 * dropped. 79 */ 80 static void 81 dev_unlock_and_free(void) 82 { 83 struct cdev_priv_list cdp_free; 84 struct free_cdevsw csw_free; 85 struct cdev_priv *cdp; 86 struct cdevsw *csw; 87 88 mtx_assert(&devmtx, MA_OWNED); 89 90 /* 91 * Make the local copy of the list heads while the dev_mtx is 92 * held. Free it later. 93 */ 94 TAILQ_INIT(&cdp_free); 95 TAILQ_CONCAT(&cdp_free, &cdevp_free_list, cdp_list); 96 csw_free = cdevsw_gt_post_list; 97 SLIST_INIT(&cdevsw_gt_post_list); 98 99 mtx_unlock(&devmtx); 100 101 while ((cdp = TAILQ_FIRST(&cdp_free)) != NULL) { 102 TAILQ_REMOVE(&cdp_free, cdp, cdp_list); 103 devfs_free(&cdp->cdp_c); 104 } 105 while ((csw = SLIST_FIRST(&csw_free)) != NULL) { 106 SLIST_REMOVE_HEAD(&csw_free, d_postfree_list); 107 free(csw, M_DEVT); 108 } 109 } 110 111 static void 112 dev_free_devlocked(struct cdev *cdev) 113 { 114 struct cdev_priv *cdp; 115 116 mtx_assert(&devmtx, MA_OWNED); 117 cdp = cdev2priv(cdev); 118 TAILQ_INSERT_HEAD(&cdevp_free_list, cdp, cdp_list); 119 } 120 121 static void 122 cdevsw_free_devlocked(struct cdevsw *csw) 123 { 124 125 mtx_assert(&devmtx, MA_OWNED); 126 SLIST_INSERT_HEAD(&cdevsw_gt_post_list, csw, d_postfree_list); 127 } 128 129 void 130 dev_unlock(void) 131 { 132 133 mtx_unlock(&devmtx); 134 } 135 136 void 137 dev_ref(struct cdev *dev) 138 { 139 140 mtx_assert(&devmtx, MA_NOTOWNED); 141 mtx_lock(&devmtx); 142 dev->si_refcount++; 143 mtx_unlock(&devmtx); 144 } 145 146 void 147 dev_refl(struct cdev *dev) 148 { 149 150 mtx_assert(&devmtx, MA_OWNED); 151 dev->si_refcount++; 152 } 153 154 void 155 dev_rel(struct cdev *dev) 156 { 157 int flag = 0; 158 159 mtx_assert(&devmtx, MA_NOTOWNED); 160 dev_lock(); 161 dev->si_refcount--; 162 KASSERT(dev->si_refcount >= 0, 163 ("dev_rel(%s) gave negative count", devtoname(dev))); 164 #if 0 165 if (dev->si_usecount == 0 && 166 (dev->si_flags & SI_CHEAPCLONE) && (dev->si_flags & SI_NAMED)) 167 ; 168 else 169 #endif 170 if (dev->si_devsw == NULL && dev->si_refcount == 0) { 171 LIST_REMOVE(dev, si_list); 172 flag = 1; 173 } 174 dev_unlock(); 175 if (flag) 176 devfs_free(dev); 177 } 178 179 struct cdevsw * 180 dev_refthread(struct cdev *dev, int *ref) 181 { 182 struct cdevsw *csw; 183 struct cdev_priv *cdp; 184 185 mtx_assert(&devmtx, MA_NOTOWNED); 186 if ((dev->si_flags & SI_ETERNAL) != 0) { 187 *ref = 0; 188 return (dev->si_devsw); 189 } 190 dev_lock(); 191 csw = dev->si_devsw; 192 if (csw != NULL) { 193 cdp = cdev2priv(dev); 194 if ((cdp->cdp_flags & CDP_SCHED_DTR) == 0) 195 dev->si_threadcount++; 196 else 197 csw = NULL; 198 } 199 dev_unlock(); 200 *ref = 1; 201 return (csw); 202 } 203 204 struct cdevsw * 205 devvn_refthread(struct vnode *vp, struct cdev **devp, int *ref) 206 { 207 struct cdevsw *csw; 208 struct cdev_priv *cdp; 209 struct cdev *dev; 210 211 mtx_assert(&devmtx, MA_NOTOWNED); 212 if ((vp->v_vflag & VV_ETERNALDEV) != 0) { 213 dev = vp->v_rdev; 214 if (dev == NULL) 215 return (NULL); 216 KASSERT((dev->si_flags & SI_ETERNAL) != 0, 217 ("Not eternal cdev")); 218 *ref = 0; 219 csw = dev->si_devsw; 220 KASSERT(csw != NULL, ("Eternal cdev is destroyed")); 221 *devp = dev; 222 return (csw); 223 } 224 225 csw = NULL; 226 dev_lock(); 227 dev = vp->v_rdev; 228 if (dev == NULL) { 229 dev_unlock(); 230 return (NULL); 231 } 232 cdp = cdev2priv(dev); 233 if ((cdp->cdp_flags & CDP_SCHED_DTR) == 0) { 234 csw = dev->si_devsw; 235 if (csw != NULL) 236 dev->si_threadcount++; 237 } 238 dev_unlock(); 239 if (csw != NULL) { 240 *devp = dev; 241 *ref = 1; 242 } 243 return (csw); 244 } 245 246 void 247 dev_relthread(struct cdev *dev, int ref) 248 { 249 250 mtx_assert(&devmtx, MA_NOTOWNED); 251 if (!ref) 252 return; 253 dev_lock(); 254 KASSERT(dev->si_threadcount > 0, 255 ("%s threadcount is wrong", dev->si_name)); 256 dev->si_threadcount--; 257 dev_unlock(); 258 } 259 260 int 261 nullop(void) 262 { 263 264 return (0); 265 } 266 267 int 268 eopnotsupp(void) 269 { 270 271 return (EOPNOTSUPP); 272 } 273 274 static int 275 enxio(void) 276 { 277 return (ENXIO); 278 } 279 280 static int 281 enodev(void) 282 { 283 return (ENODEV); 284 } 285 286 /* Define a dead_cdevsw for use when devices leave unexpectedly. */ 287 288 #define dead_open (d_open_t *)enxio 289 #define dead_close (d_close_t *)enxio 290 #define dead_read (d_read_t *)enxio 291 #define dead_write (d_write_t *)enxio 292 #define dead_ioctl (d_ioctl_t *)enxio 293 #define dead_poll (d_poll_t *)enodev 294 #define dead_mmap (d_mmap_t *)enodev 295 296 static void 297 dead_strategy(struct bio *bp) 298 { 299 300 biofinish(bp, NULL, ENXIO); 301 } 302 303 #define dead_dump (dumper_t *)enxio 304 #define dead_kqfilter (d_kqfilter_t *)enxio 305 #define dead_mmap_single (d_mmap_single_t *)enodev 306 307 static struct cdevsw dead_cdevsw = { 308 .d_version = D_VERSION, 309 .d_flags = D_NEEDGIANT, /* XXX: does dead_strategy need this ? */ 310 .d_open = dead_open, 311 .d_close = dead_close, 312 .d_read = dead_read, 313 .d_write = dead_write, 314 .d_ioctl = dead_ioctl, 315 .d_poll = dead_poll, 316 .d_mmap = dead_mmap, 317 .d_strategy = dead_strategy, 318 .d_name = "dead", 319 .d_dump = dead_dump, 320 .d_kqfilter = dead_kqfilter, 321 .d_mmap_single = dead_mmap_single 322 }; 323 324 /* Default methods if driver does not specify method */ 325 326 #define null_open (d_open_t *)nullop 327 #define null_close (d_close_t *)nullop 328 #define no_read (d_read_t *)enodev 329 #define no_write (d_write_t *)enodev 330 #define no_ioctl (d_ioctl_t *)enodev 331 #define no_mmap (d_mmap_t *)enodev 332 #define no_kqfilter (d_kqfilter_t *)enodev 333 #define no_mmap_single (d_mmap_single_t *)enodev 334 335 static void 336 no_strategy(struct bio *bp) 337 { 338 339 biofinish(bp, NULL, ENODEV); 340 } 341 342 static int 343 no_poll(struct cdev *dev __unused, int events, struct thread *td __unused) 344 { 345 346 return (poll_no_poll(events)); 347 } 348 349 #define no_dump (dumper_t *)enodev 350 351 static int 352 giant_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 353 { 354 struct cdevsw *dsw; 355 int ref, retval; 356 357 dsw = dev_refthread(dev, &ref); 358 if (dsw == NULL) 359 return (ENXIO); 360 mtx_lock(&Giant); 361 retval = dsw->d_gianttrick->d_open(dev, oflags, devtype, td); 362 mtx_unlock(&Giant); 363 dev_relthread(dev, ref); 364 return (retval); 365 } 366 367 static int 368 giant_fdopen(struct cdev *dev, int oflags, struct thread *td, struct file *fp) 369 { 370 struct cdevsw *dsw; 371 int ref, retval; 372 373 dsw = dev_refthread(dev, &ref); 374 if (dsw == NULL) 375 return (ENXIO); 376 mtx_lock(&Giant); 377 retval = dsw->d_gianttrick->d_fdopen(dev, oflags, td, fp); 378 mtx_unlock(&Giant); 379 dev_relthread(dev, ref); 380 return (retval); 381 } 382 383 static int 384 giant_close(struct cdev *dev, int fflag, int devtype, struct thread *td) 385 { 386 struct cdevsw *dsw; 387 int ref, retval; 388 389 dsw = dev_refthread(dev, &ref); 390 if (dsw == NULL) 391 return (ENXIO); 392 mtx_lock(&Giant); 393 retval = dsw->d_gianttrick->d_close(dev, fflag, devtype, td); 394 mtx_unlock(&Giant); 395 dev_relthread(dev, ref); 396 return (retval); 397 } 398 399 static void 400 giant_strategy(struct bio *bp) 401 { 402 struct cdevsw *dsw; 403 struct cdev *dev; 404 int ref; 405 406 dev = bp->bio_dev; 407 dsw = dev_refthread(dev, &ref); 408 if (dsw == NULL) { 409 biofinish(bp, NULL, ENXIO); 410 return; 411 } 412 mtx_lock(&Giant); 413 dsw->d_gianttrick->d_strategy(bp); 414 mtx_unlock(&Giant); 415 dev_relthread(dev, ref); 416 } 417 418 static int 419 giant_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td) 420 { 421 struct cdevsw *dsw; 422 int ref, retval; 423 424 dsw = dev_refthread(dev, &ref); 425 if (dsw == NULL) 426 return (ENXIO); 427 mtx_lock(&Giant); 428 retval = dsw->d_gianttrick->d_ioctl(dev, cmd, data, fflag, td); 429 mtx_unlock(&Giant); 430 dev_relthread(dev, ref); 431 return (retval); 432 } 433 434 static int 435 giant_read(struct cdev *dev, struct uio *uio, int ioflag) 436 { 437 struct cdevsw *dsw; 438 int ref, retval; 439 440 dsw = dev_refthread(dev, &ref); 441 if (dsw == NULL) 442 return (ENXIO); 443 mtx_lock(&Giant); 444 retval = dsw->d_gianttrick->d_read(dev, uio, ioflag); 445 mtx_unlock(&Giant); 446 dev_relthread(dev, ref); 447 return (retval); 448 } 449 450 static int 451 giant_write(struct cdev *dev, struct uio *uio, int ioflag) 452 { 453 struct cdevsw *dsw; 454 int ref, retval; 455 456 dsw = dev_refthread(dev, &ref); 457 if (dsw == NULL) 458 return (ENXIO); 459 mtx_lock(&Giant); 460 retval = dsw->d_gianttrick->d_write(dev, uio, ioflag); 461 mtx_unlock(&Giant); 462 dev_relthread(dev, ref); 463 return (retval); 464 } 465 466 static int 467 giant_poll(struct cdev *dev, int events, struct thread *td) 468 { 469 struct cdevsw *dsw; 470 int ref, retval; 471 472 dsw = dev_refthread(dev, &ref); 473 if (dsw == NULL) 474 return (ENXIO); 475 mtx_lock(&Giant); 476 retval = dsw->d_gianttrick->d_poll(dev, events, td); 477 mtx_unlock(&Giant); 478 dev_relthread(dev, ref); 479 return (retval); 480 } 481 482 static int 483 giant_kqfilter(struct cdev *dev, struct knote *kn) 484 { 485 struct cdevsw *dsw; 486 int ref, retval; 487 488 dsw = dev_refthread(dev, &ref); 489 if (dsw == NULL) 490 return (ENXIO); 491 mtx_lock(&Giant); 492 retval = dsw->d_gianttrick->d_kqfilter(dev, kn); 493 mtx_unlock(&Giant); 494 dev_relthread(dev, ref); 495 return (retval); 496 } 497 498 static int 499 giant_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, int nprot, 500 vm_memattr_t *memattr) 501 { 502 struct cdevsw *dsw; 503 int ref, retval; 504 505 dsw = dev_refthread(dev, &ref); 506 if (dsw == NULL) 507 return (ENXIO); 508 mtx_lock(&Giant); 509 retval = dsw->d_gianttrick->d_mmap(dev, offset, paddr, nprot, 510 memattr); 511 mtx_unlock(&Giant); 512 dev_relthread(dev, ref); 513 return (retval); 514 } 515 516 static int 517 giant_mmap_single(struct cdev *dev, vm_ooffset_t *offset, vm_size_t size, 518 vm_object_t *object, int nprot) 519 { 520 struct cdevsw *dsw; 521 int ref, retval; 522 523 dsw = dev_refthread(dev, &ref); 524 if (dsw == NULL) 525 return (ENXIO); 526 mtx_lock(&Giant); 527 retval = dsw->d_gianttrick->d_mmap_single(dev, offset, size, object, 528 nprot); 529 mtx_unlock(&Giant); 530 dev_relthread(dev, ref); 531 return (retval); 532 } 533 534 static void 535 notify(struct cdev *dev, const char *ev, int flags) 536 { 537 static const char prefix[] = "cdev="; 538 char *data; 539 int namelen, mflags; 540 541 if (cold) 542 return; 543 mflags = (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK; 544 namelen = strlen(dev->si_name); 545 data = malloc(namelen + sizeof(prefix), M_TEMP, mflags); 546 if (data == NULL) 547 return; 548 memcpy(data, prefix, sizeof(prefix) - 1); 549 memcpy(data + sizeof(prefix) - 1, dev->si_name, namelen + 1); 550 devctl_notify_f("DEVFS", "CDEV", ev, data, mflags); 551 free(data, M_TEMP); 552 } 553 554 static void 555 notify_create(struct cdev *dev, int flags) 556 { 557 558 notify(dev, "CREATE", flags); 559 } 560 561 static void 562 notify_destroy(struct cdev *dev) 563 { 564 565 notify(dev, "DESTROY", MAKEDEV_WAITOK); 566 } 567 568 static struct cdev * 569 newdev(struct cdevsw *csw, int unit, struct cdev *si) 570 { 571 struct cdev *si2; 572 573 mtx_assert(&devmtx, MA_OWNED); 574 if (csw->d_flags & D_NEEDMINOR) { 575 /* We may want to return an existing device */ 576 LIST_FOREACH(si2, &csw->d_devs, si_list) { 577 if (dev2unit(si2) == unit) { 578 dev_free_devlocked(si); 579 return (si2); 580 } 581 } 582 } 583 si->si_drv0 = unit; 584 si->si_devsw = csw; 585 LIST_INSERT_HEAD(&csw->d_devs, si, si_list); 586 return (si); 587 } 588 589 static void 590 fini_cdevsw(struct cdevsw *devsw) 591 { 592 struct cdevsw *gt; 593 594 if (devsw->d_gianttrick != NULL) { 595 gt = devsw->d_gianttrick; 596 memcpy(devsw, gt, sizeof *devsw); 597 cdevsw_free_devlocked(gt); 598 devsw->d_gianttrick = NULL; 599 } 600 devsw->d_flags &= ~D_INIT; 601 } 602 603 static int 604 prep_cdevsw(struct cdevsw *devsw, int flags) 605 { 606 struct cdevsw *dsw2; 607 608 mtx_assert(&devmtx, MA_OWNED); 609 if (devsw->d_flags & D_INIT) 610 return (0); 611 if (devsw->d_flags & D_NEEDGIANT) { 612 dev_unlock(); 613 dsw2 = malloc(sizeof *dsw2, M_DEVT, 614 (flags & MAKEDEV_NOWAIT) ? M_NOWAIT : M_WAITOK); 615 dev_lock(); 616 if (dsw2 == NULL && !(devsw->d_flags & D_INIT)) 617 return (ENOMEM); 618 } else 619 dsw2 = NULL; 620 if (devsw->d_flags & D_INIT) { 621 if (dsw2 != NULL) 622 cdevsw_free_devlocked(dsw2); 623 return (0); 624 } 625 626 if (devsw->d_version != D_VERSION_03) { 627 printf( 628 "WARNING: Device driver \"%s\" has wrong version %s\n", 629 devsw->d_name == NULL ? "???" : devsw->d_name, 630 "and is disabled. Recompile KLD module."); 631 devsw->d_open = dead_open; 632 devsw->d_close = dead_close; 633 devsw->d_read = dead_read; 634 devsw->d_write = dead_write; 635 devsw->d_ioctl = dead_ioctl; 636 devsw->d_poll = dead_poll; 637 devsw->d_mmap = dead_mmap; 638 devsw->d_mmap_single = dead_mmap_single; 639 devsw->d_strategy = dead_strategy; 640 devsw->d_dump = dead_dump; 641 devsw->d_kqfilter = dead_kqfilter; 642 } 643 644 if (devsw->d_flags & D_NEEDGIANT) { 645 if (devsw->d_gianttrick == NULL) { 646 memcpy(dsw2, devsw, sizeof *dsw2); 647 devsw->d_gianttrick = dsw2; 648 dsw2 = NULL; 649 } 650 } 651 652 #define FIXUP(member, noop, giant) \ 653 do { \ 654 if (devsw->member == NULL) { \ 655 devsw->member = noop; \ 656 } else if (devsw->d_flags & D_NEEDGIANT) \ 657 devsw->member = giant; \ 658 } \ 659 while (0) 660 661 FIXUP(d_open, null_open, giant_open); 662 FIXUP(d_fdopen, NULL, giant_fdopen); 663 FIXUP(d_close, null_close, giant_close); 664 FIXUP(d_read, no_read, giant_read); 665 FIXUP(d_write, no_write, giant_write); 666 FIXUP(d_ioctl, no_ioctl, giant_ioctl); 667 FIXUP(d_poll, no_poll, giant_poll); 668 FIXUP(d_mmap, no_mmap, giant_mmap); 669 FIXUP(d_strategy, no_strategy, giant_strategy); 670 FIXUP(d_kqfilter, no_kqfilter, giant_kqfilter); 671 FIXUP(d_mmap_single, no_mmap_single, giant_mmap_single); 672 673 if (devsw->d_dump == NULL) devsw->d_dump = no_dump; 674 675 LIST_INIT(&devsw->d_devs); 676 677 devsw->d_flags |= D_INIT; 678 679 if (dsw2 != NULL) 680 cdevsw_free_devlocked(dsw2); 681 return (0); 682 } 683 684 static int 685 prep_devname(struct cdev *dev, const char *fmt, va_list ap) 686 { 687 int len; 688 char *from, *q, *s, *to; 689 690 mtx_assert(&devmtx, MA_OWNED); 691 692 len = vsnrprintf(dev->__si_namebuf, sizeof(dev->__si_namebuf), 32, 693 fmt, ap); 694 if (len > sizeof(dev->__si_namebuf) - 1) 695 return (ENAMETOOLONG); 696 697 /* Strip leading slashes. */ 698 for (from = dev->__si_namebuf; *from == '/'; from++) 699 ; 700 701 for (to = dev->__si_namebuf; *from != '\0'; from++, to++) { 702 /* Treat multiple sequential slashes as single. */ 703 while (from[0] == '/' && from[1] == '/') 704 from++; 705 /* Trailing slash is considered invalid. */ 706 if (from[0] == '/' && from[1] == '\0') 707 return (EINVAL); 708 *to = *from; 709 } 710 *to = '\0'; 711 712 if (dev->__si_namebuf[0] == '\0') 713 return (EINVAL); 714 715 /* Disallow "." and ".." components. */ 716 for (s = dev->__si_namebuf;;) { 717 for (q = s; *q != '/' && *q != '\0'; q++) 718 ; 719 if (q - s == 1 && s[0] == '.') 720 return (EINVAL); 721 if (q - s == 2 && s[0] == '.' && s[1] == '.') 722 return (EINVAL); 723 if (*q != '/') 724 break; 725 s = q + 1; 726 } 727 728 if (devfs_dev_exists(dev->__si_namebuf) != 0) 729 return (EEXIST); 730 731 return (0); 732 } 733 734 static int 735 make_dev_credv(int flags, struct cdev **dres, struct cdevsw *devsw, int unit, 736 struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, 737 va_list ap) 738 { 739 struct cdev *dev, *dev_new; 740 int res; 741 742 KASSERT((flags & MAKEDEV_WAITOK) == 0 || (flags & MAKEDEV_NOWAIT) == 0, 743 ("make_dev_credv: both WAITOK and NOWAIT specified")); 744 dev_new = devfs_alloc(flags); 745 if (dev_new == NULL) 746 return (ENOMEM); 747 dev_lock(); 748 res = prep_cdevsw(devsw, flags); 749 if (res != 0) { 750 dev_unlock(); 751 devfs_free(dev_new); 752 return (res); 753 } 754 dev = newdev(devsw, unit, dev_new); 755 if ((dev->si_flags & SI_NAMED) == 0) { 756 res = prep_devname(dev, fmt, ap); 757 if (res != 0) { 758 if ((flags & MAKEDEV_CHECKNAME) == 0) { 759 panic( 760 "make_dev_credv: bad si_name (error=%d, si_name=%s)", 761 res, dev->si_name); 762 } 763 if (dev == dev_new) { 764 LIST_REMOVE(dev, si_list); 765 dev_unlock(); 766 devfs_free(dev); 767 } 768 return (res); 769 } 770 } 771 if (flags & MAKEDEV_REF) 772 dev_refl(dev); 773 if (flags & MAKEDEV_ETERNAL) 774 dev->si_flags |= SI_ETERNAL; 775 if (dev->si_flags & SI_CHEAPCLONE && 776 dev->si_flags & SI_NAMED) { 777 /* 778 * This is allowed as it removes races and generally 779 * simplifies cloning devices. 780 * XXX: still ?? 781 */ 782 dev_unlock_and_free(); 783 *dres = dev; 784 return (0); 785 } 786 KASSERT(!(dev->si_flags & SI_NAMED), 787 ("make_dev() by driver %s on pre-existing device (min=%x, name=%s)", 788 devsw->d_name, dev2unit(dev), devtoname(dev))); 789 dev->si_flags |= SI_NAMED; 790 if (cr != NULL) 791 dev->si_cred = crhold(cr); 792 dev->si_uid = uid; 793 dev->si_gid = gid; 794 dev->si_mode = mode; 795 796 devfs_create(dev); 797 clean_unrhdrl(devfs_inos); 798 dev_unlock_and_free(); 799 800 notify_create(dev, flags); 801 802 *dres = dev; 803 return (0); 804 } 805 806 struct cdev * 807 make_dev(struct cdevsw *devsw, int unit, uid_t uid, gid_t gid, int mode, 808 const char *fmt, ...) 809 { 810 struct cdev *dev; 811 va_list ap; 812 int res; 813 814 va_start(ap, fmt); 815 res = make_dev_credv(0, &dev, devsw, unit, NULL, uid, gid, mode, fmt, 816 ap); 817 va_end(ap); 818 KASSERT(res == 0 && dev != NULL, 819 ("make_dev: failed make_dev_credv (error=%d)", res)); 820 return (dev); 821 } 822 823 struct cdev * 824 make_dev_cred(struct cdevsw *devsw, int unit, struct ucred *cr, uid_t uid, 825 gid_t gid, int mode, const char *fmt, ...) 826 { 827 struct cdev *dev; 828 va_list ap; 829 int res; 830 831 va_start(ap, fmt); 832 res = make_dev_credv(0, &dev, devsw, unit, cr, uid, gid, mode, fmt, ap); 833 va_end(ap); 834 835 KASSERT(res == 0 && dev != NULL, 836 ("make_dev_cred: failed make_dev_credv (error=%d)", res)); 837 return (dev); 838 } 839 840 struct cdev * 841 make_dev_credf(int flags, struct cdevsw *devsw, int unit, struct ucred *cr, 842 uid_t uid, gid_t gid, int mode, const char *fmt, ...) 843 { 844 struct cdev *dev; 845 va_list ap; 846 int res; 847 848 va_start(ap, fmt); 849 res = make_dev_credv(flags, &dev, devsw, unit, cr, uid, gid, mode, 850 fmt, ap); 851 va_end(ap); 852 853 KASSERT(((flags & MAKEDEV_NOWAIT) != 0 && res == ENOMEM) || 854 ((flags & MAKEDEV_CHECKNAME) != 0 && res != ENOMEM) || res == 0, 855 ("make_dev_credf: failed make_dev_credv (error=%d)", res)); 856 return (res == 0 ? dev : NULL); 857 } 858 859 int 860 make_dev_p(int flags, struct cdev **cdev, struct cdevsw *devsw, 861 struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, ...) 862 { 863 va_list ap; 864 int res; 865 866 va_start(ap, fmt); 867 res = make_dev_credv(flags, cdev, devsw, 0, cr, uid, gid, mode, 868 fmt, ap); 869 va_end(ap); 870 871 KASSERT(((flags & MAKEDEV_NOWAIT) != 0 && res == ENOMEM) || 872 ((flags & MAKEDEV_CHECKNAME) != 0 && res != ENOMEM) || res == 0, 873 ("make_dev_p: failed make_dev_credv (error=%d)", res)); 874 return (res); 875 } 876 877 static void 878 dev_dependsl(struct cdev *pdev, struct cdev *cdev) 879 { 880 881 cdev->si_parent = pdev; 882 cdev->si_flags |= SI_CHILD; 883 LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings); 884 } 885 886 887 void 888 dev_depends(struct cdev *pdev, struct cdev *cdev) 889 { 890 891 dev_lock(); 892 dev_dependsl(pdev, cdev); 893 dev_unlock(); 894 } 895 896 static int 897 make_dev_alias_v(int flags, struct cdev **cdev, struct cdev *pdev, 898 const char *fmt, va_list ap) 899 { 900 struct cdev *dev; 901 int error; 902 903 KASSERT(pdev != NULL, ("make_dev_alias_v: pdev is NULL")); 904 KASSERT((flags & MAKEDEV_WAITOK) == 0 || (flags & MAKEDEV_NOWAIT) == 0, 905 ("make_dev_alias_v: both WAITOK and NOWAIT specified")); 906 KASSERT((flags & ~(MAKEDEV_WAITOK | MAKEDEV_NOWAIT | 907 MAKEDEV_CHECKNAME)) == 0, 908 ("make_dev_alias_v: invalid flags specified (flags=%02x)", flags)); 909 910 dev = devfs_alloc(flags); 911 if (dev == NULL) 912 return (ENOMEM); 913 dev_lock(); 914 dev->si_flags |= SI_ALIAS; 915 error = prep_devname(dev, fmt, ap); 916 if (error != 0) { 917 if ((flags & MAKEDEV_CHECKNAME) == 0) { 918 panic("make_dev_alias_v: bad si_name " 919 "(error=%d, si_name=%s)", error, dev->si_name); 920 } 921 dev_unlock(); 922 devfs_free(dev); 923 return (error); 924 } 925 dev->si_flags |= SI_NAMED; 926 devfs_create(dev); 927 dev_dependsl(pdev, dev); 928 clean_unrhdrl(devfs_inos); 929 dev_unlock(); 930 931 notify_create(dev, flags); 932 *cdev = dev; 933 934 return (0); 935 } 936 937 struct cdev * 938 make_dev_alias(struct cdev *pdev, const char *fmt, ...) 939 { 940 struct cdev *dev; 941 va_list ap; 942 int res; 943 944 va_start(ap, fmt); 945 res = make_dev_alias_v(MAKEDEV_WAITOK, &dev, pdev, fmt, ap); 946 va_end(ap); 947 948 KASSERT(res == 0 && dev != NULL, 949 ("make_dev_alias: failed make_dev_alias_v (error=%d)", res)); 950 return (dev); 951 } 952 953 int 954 make_dev_alias_p(int flags, struct cdev **cdev, struct cdev *pdev, 955 const char *fmt, ...) 956 { 957 va_list ap; 958 int res; 959 960 va_start(ap, fmt); 961 res = make_dev_alias_v(flags, cdev, pdev, fmt, ap); 962 va_end(ap); 963 return (res); 964 } 965 966 static void 967 destroy_devl(struct cdev *dev) 968 { 969 struct cdevsw *csw; 970 struct cdev_privdata *p; 971 972 mtx_assert(&devmtx, MA_OWNED); 973 KASSERT(dev->si_flags & SI_NAMED, 974 ("WARNING: Driver mistake: destroy_dev on %d\n", dev2unit(dev))); 975 KASSERT((dev->si_flags & SI_ETERNAL) == 0, 976 ("WARNING: Driver mistake: destroy_dev on eternal %d\n", 977 dev2unit(dev))); 978 979 devfs_destroy(dev); 980 981 /* Remove name marking */ 982 dev->si_flags &= ~SI_NAMED; 983 984 dev->si_refcount++; /* Avoid race with dev_rel() */ 985 986 /* If we are a child, remove us from the parents list */ 987 if (dev->si_flags & SI_CHILD) { 988 LIST_REMOVE(dev, si_siblings); 989 dev->si_flags &= ~SI_CHILD; 990 } 991 992 /* Kill our children */ 993 while (!LIST_EMPTY(&dev->si_children)) 994 destroy_devl(LIST_FIRST(&dev->si_children)); 995 996 /* Remove from clone list */ 997 if (dev->si_flags & SI_CLONELIST) { 998 LIST_REMOVE(dev, si_clone); 999 dev->si_flags &= ~SI_CLONELIST; 1000 } 1001 1002 csw = dev->si_devsw; 1003 dev->si_devsw = NULL; /* already NULL for SI_ALIAS */ 1004 while (csw != NULL && csw->d_purge != NULL && dev->si_threadcount) { 1005 csw->d_purge(dev); 1006 msleep(csw, &devmtx, PRIBIO, "devprg", hz/10); 1007 if (dev->si_threadcount) 1008 printf("Still %lu threads in %s\n", 1009 dev->si_threadcount, devtoname(dev)); 1010 } 1011 while (dev->si_threadcount != 0) { 1012 /* Use unique dummy wait ident */ 1013 msleep(&csw, &devmtx, PRIBIO, "devdrn", hz / 10); 1014 } 1015 1016 dev_unlock(); 1017 notify_destroy(dev); 1018 mtx_lock(&cdevpriv_mtx); 1019 while ((p = LIST_FIRST(&cdev2priv(dev)->cdp_fdpriv)) != NULL) { 1020 devfs_destroy_cdevpriv(p); 1021 mtx_lock(&cdevpriv_mtx); 1022 } 1023 mtx_unlock(&cdevpriv_mtx); 1024 dev_lock(); 1025 1026 dev->si_drv1 = 0; 1027 dev->si_drv2 = 0; 1028 bzero(&dev->__si_u, sizeof(dev->__si_u)); 1029 1030 if (!(dev->si_flags & SI_ALIAS)) { 1031 /* Remove from cdevsw list */ 1032 LIST_REMOVE(dev, si_list); 1033 1034 /* If cdevsw has no more struct cdev *'s, clean it */ 1035 if (LIST_EMPTY(&csw->d_devs)) { 1036 fini_cdevsw(csw); 1037 wakeup(&csw->d_devs); 1038 } 1039 } 1040 dev->si_flags &= ~SI_ALIAS; 1041 dev->si_refcount--; /* Avoid race with dev_rel() */ 1042 1043 if (dev->si_refcount > 0) { 1044 LIST_INSERT_HEAD(&dead_cdevsw.d_devs, dev, si_list); 1045 } else { 1046 dev_free_devlocked(dev); 1047 } 1048 } 1049 1050 void 1051 destroy_dev(struct cdev *dev) 1052 { 1053 1054 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "destroy_dev"); 1055 dev_lock(); 1056 destroy_devl(dev); 1057 dev_unlock_and_free(); 1058 } 1059 1060 const char * 1061 devtoname(struct cdev *dev) 1062 { 1063 1064 return (dev->si_name); 1065 } 1066 1067 int 1068 dev_stdclone(char *name, char **namep, const char *stem, int *unit) 1069 { 1070 int u, i; 1071 1072 i = strlen(stem); 1073 if (bcmp(stem, name, i) != 0) 1074 return (0); 1075 if (!isdigit(name[i])) 1076 return (0); 1077 u = 0; 1078 if (name[i] == '0' && isdigit(name[i+1])) 1079 return (0); 1080 while (isdigit(name[i])) { 1081 u *= 10; 1082 u += name[i++] - '0'; 1083 } 1084 if (u > 0xffffff) 1085 return (0); 1086 *unit = u; 1087 if (namep) 1088 *namep = &name[i]; 1089 if (name[i]) 1090 return (2); 1091 return (1); 1092 } 1093 1094 /* 1095 * Helper functions for cloning device drivers. 1096 * 1097 * The objective here is to make it unnecessary for the device drivers to 1098 * use rman or similar to manage their unit number space. Due to the way 1099 * we do "on-demand" devices, using rman or other "private" methods 1100 * will be very tricky to lock down properly once we lock down this file. 1101 * 1102 * Instead we give the drivers these routines which puts the struct cdev *'s 1103 * that are to be managed on their own list, and gives the driver the ability 1104 * to ask for the first free unit number or a given specified unit number. 1105 * 1106 * In addition these routines support paired devices (pty, nmdm and similar) 1107 * by respecting a number of "flag" bits in the minor number. 1108 * 1109 */ 1110 1111 struct clonedevs { 1112 LIST_HEAD(,cdev) head; 1113 }; 1114 1115 void 1116 clone_setup(struct clonedevs **cdp) 1117 { 1118 1119 *cdp = malloc(sizeof **cdp, M_DEVBUF, M_WAITOK | M_ZERO); 1120 LIST_INIT(&(*cdp)->head); 1121 } 1122 1123 int 1124 clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up, 1125 struct cdev **dp, int extra) 1126 { 1127 struct clonedevs *cd; 1128 struct cdev *dev, *ndev, *dl, *de; 1129 int unit, low, u; 1130 1131 KASSERT(*cdp != NULL, 1132 ("clone_setup() not called in driver \"%s\"", csw->d_name)); 1133 KASSERT(!(extra & CLONE_UNITMASK), 1134 ("Illegal extra bits (0x%x) in clone_create", extra)); 1135 KASSERT(*up <= CLONE_UNITMASK, 1136 ("Too high unit (0x%x) in clone_create", *up)); 1137 KASSERT(csw->d_flags & D_NEEDMINOR, 1138 ("clone_create() on cdevsw without minor numbers")); 1139 1140 1141 /* 1142 * Search the list for a lot of things in one go: 1143 * A preexisting match is returned immediately. 1144 * The lowest free unit number if we are passed -1, and the place 1145 * in the list where we should insert that new element. 1146 * The place to insert a specified unit number, if applicable 1147 * the end of the list. 1148 */ 1149 unit = *up; 1150 ndev = devfs_alloc(MAKEDEV_WAITOK); 1151 dev_lock(); 1152 prep_cdevsw(csw, MAKEDEV_WAITOK); 1153 low = extra; 1154 de = dl = NULL; 1155 cd = *cdp; 1156 LIST_FOREACH(dev, &cd->head, si_clone) { 1157 KASSERT(dev->si_flags & SI_CLONELIST, 1158 ("Dev %p(%s) should be on clonelist", dev, dev->si_name)); 1159 u = dev2unit(dev); 1160 if (u == (unit | extra)) { 1161 *dp = dev; 1162 dev_unlock(); 1163 devfs_free(ndev); 1164 return (0); 1165 } 1166 if (unit == -1 && u == low) { 1167 low++; 1168 de = dev; 1169 continue; 1170 } else if (u < (unit | extra)) { 1171 de = dev; 1172 continue; 1173 } else if (u > (unit | extra)) { 1174 dl = dev; 1175 break; 1176 } 1177 } 1178 if (unit == -1) 1179 unit = low & CLONE_UNITMASK; 1180 dev = newdev(csw, unit | extra, ndev); 1181 if (dev->si_flags & SI_CLONELIST) { 1182 printf("dev %p (%s) is on clonelist\n", dev, dev->si_name); 1183 printf("unit=%d, low=%d, extra=0x%x\n", unit, low, extra); 1184 LIST_FOREACH(dev, &cd->head, si_clone) { 1185 printf("\t%p %s\n", dev, dev->si_name); 1186 } 1187 panic("foo"); 1188 } 1189 KASSERT(!(dev->si_flags & SI_CLONELIST), 1190 ("Dev %p(%s) should not be on clonelist", dev, dev->si_name)); 1191 if (dl != NULL) 1192 LIST_INSERT_BEFORE(dl, dev, si_clone); 1193 else if (de != NULL) 1194 LIST_INSERT_AFTER(de, dev, si_clone); 1195 else 1196 LIST_INSERT_HEAD(&cd->head, dev, si_clone); 1197 dev->si_flags |= SI_CLONELIST; 1198 *up = unit; 1199 dev_unlock_and_free(); 1200 return (1); 1201 } 1202 1203 /* 1204 * Kill everything still on the list. The driver should already have 1205 * disposed of any softc hung of the struct cdev *'s at this time. 1206 */ 1207 void 1208 clone_cleanup(struct clonedevs **cdp) 1209 { 1210 struct cdev *dev; 1211 struct cdev_priv *cp; 1212 struct clonedevs *cd; 1213 1214 cd = *cdp; 1215 if (cd == NULL) 1216 return; 1217 dev_lock(); 1218 while (!LIST_EMPTY(&cd->head)) { 1219 dev = LIST_FIRST(&cd->head); 1220 LIST_REMOVE(dev, si_clone); 1221 KASSERT(dev->si_flags & SI_CLONELIST, 1222 ("Dev %p(%s) should be on clonelist", dev, dev->si_name)); 1223 dev->si_flags &= ~SI_CLONELIST; 1224 cp = cdev2priv(dev); 1225 if (!(cp->cdp_flags & CDP_SCHED_DTR)) { 1226 cp->cdp_flags |= CDP_SCHED_DTR; 1227 KASSERT(dev->si_flags & SI_NAMED, 1228 ("Driver has goofed in cloning underways udev %x unit %x", dev2udev(dev), dev2unit(dev))); 1229 destroy_devl(dev); 1230 } 1231 } 1232 dev_unlock_and_free(); 1233 free(cd, M_DEVBUF); 1234 *cdp = NULL; 1235 } 1236 1237 static TAILQ_HEAD(, cdev_priv) dev_ddtr = 1238 TAILQ_HEAD_INITIALIZER(dev_ddtr); 1239 static struct task dev_dtr_task; 1240 1241 static void 1242 destroy_dev_tq(void *ctx, int pending) 1243 { 1244 struct cdev_priv *cp; 1245 struct cdev *dev; 1246 void (*cb)(void *); 1247 void *cb_arg; 1248 1249 dev_lock(); 1250 while (!TAILQ_EMPTY(&dev_ddtr)) { 1251 cp = TAILQ_FIRST(&dev_ddtr); 1252 dev = &cp->cdp_c; 1253 KASSERT(cp->cdp_flags & CDP_SCHED_DTR, 1254 ("cdev %p in dev_destroy_tq without CDP_SCHED_DTR", cp)); 1255 TAILQ_REMOVE(&dev_ddtr, cp, cdp_dtr_list); 1256 cb = cp->cdp_dtr_cb; 1257 cb_arg = cp->cdp_dtr_cb_arg; 1258 destroy_devl(dev); 1259 dev_unlock_and_free(); 1260 dev_rel(dev); 1261 if (cb != NULL) 1262 cb(cb_arg); 1263 dev_lock(); 1264 } 1265 dev_unlock(); 1266 } 1267 1268 /* 1269 * devmtx shall be locked on entry. devmtx will be unlocked after 1270 * function return. 1271 */ 1272 static int 1273 destroy_dev_sched_cbl(struct cdev *dev, void (*cb)(void *), void *arg) 1274 { 1275 struct cdev_priv *cp; 1276 1277 mtx_assert(&devmtx, MA_OWNED); 1278 cp = cdev2priv(dev); 1279 if (cp->cdp_flags & CDP_SCHED_DTR) { 1280 dev_unlock(); 1281 return (0); 1282 } 1283 dev_refl(dev); 1284 cp->cdp_flags |= CDP_SCHED_DTR; 1285 cp->cdp_dtr_cb = cb; 1286 cp->cdp_dtr_cb_arg = arg; 1287 TAILQ_INSERT_TAIL(&dev_ddtr, cp, cdp_dtr_list); 1288 dev_unlock(); 1289 taskqueue_enqueue(taskqueue_swi_giant, &dev_dtr_task); 1290 return (1); 1291 } 1292 1293 int 1294 destroy_dev_sched_cb(struct cdev *dev, void (*cb)(void *), void *arg) 1295 { 1296 1297 dev_lock(); 1298 return (destroy_dev_sched_cbl(dev, cb, arg)); 1299 } 1300 1301 int 1302 destroy_dev_sched(struct cdev *dev) 1303 { 1304 1305 return (destroy_dev_sched_cb(dev, NULL, NULL)); 1306 } 1307 1308 void 1309 destroy_dev_drain(struct cdevsw *csw) 1310 { 1311 1312 dev_lock(); 1313 while (!LIST_EMPTY(&csw->d_devs)) { 1314 msleep(&csw->d_devs, &devmtx, PRIBIO, "devscd", hz/10); 1315 } 1316 dev_unlock(); 1317 } 1318 1319 void 1320 drain_dev_clone_events(void) 1321 { 1322 1323 sx_xlock(&clone_drain_lock); 1324 sx_xunlock(&clone_drain_lock); 1325 } 1326 1327 static void 1328 devdtr_init(void *dummy __unused) 1329 { 1330 1331 TASK_INIT(&dev_dtr_task, 0, destroy_dev_tq, NULL); 1332 } 1333 1334 SYSINIT(devdtr, SI_SUB_DEVFS, SI_ORDER_SECOND, devdtr_init, NULL); 1335 1336 #include "opt_ddb.h" 1337 #ifdef DDB 1338 #include <sys/kernel.h> 1339 1340 #include <ddb/ddb.h> 1341 1342 DB_SHOW_COMMAND(cdev, db_show_cdev) 1343 { 1344 struct cdev_priv *cdp; 1345 struct cdev *dev; 1346 u_int flags; 1347 char buf[512]; 1348 1349 if (!have_addr) { 1350 TAILQ_FOREACH(cdp, &cdevp_list, cdp_list) { 1351 dev = &cdp->cdp_c; 1352 db_printf("%s %p\n", dev->si_name, dev); 1353 if (db_pager_quit) 1354 break; 1355 } 1356 return; 1357 } 1358 1359 dev = (struct cdev *)addr; 1360 cdp = cdev2priv(dev); 1361 db_printf("dev %s ref %d use %ld thr %ld inuse %u fdpriv %p\n", 1362 dev->si_name, dev->si_refcount, dev->si_usecount, 1363 dev->si_threadcount, cdp->cdp_inuse, cdp->cdp_fdpriv.lh_first); 1364 db_printf("devsw %p si_drv0 %d si_drv1 %p si_drv2 %p\n", 1365 dev->si_devsw, dev->si_drv0, dev->si_drv1, dev->si_drv2); 1366 flags = dev->si_flags; 1367 #define SI_FLAG(flag) do { \ 1368 if (flags & (flag)) { \ 1369 if (buf[0] != '\0') \ 1370 strlcat(buf, ", ", sizeof(buf)); \ 1371 strlcat(buf, (#flag) + 3, sizeof(buf)); \ 1372 flags &= ~(flag); \ 1373 } \ 1374 } while (0) 1375 buf[0] = '\0'; 1376 SI_FLAG(SI_ETERNAL); 1377 SI_FLAG(SI_ALIAS); 1378 SI_FLAG(SI_NAMED); 1379 SI_FLAG(SI_CHEAPCLONE); 1380 SI_FLAG(SI_CHILD); 1381 SI_FLAG(SI_DEVOPEN); 1382 SI_FLAG(SI_CONSOPEN); 1383 SI_FLAG(SI_DUMPDEV); 1384 SI_FLAG(SI_CANDELETE); 1385 SI_FLAG(SI_CLONELIST); 1386 db_printf("si_flags %s\n", buf); 1387 1388 flags = cdp->cdp_flags; 1389 #define CDP_FLAG(flag) do { \ 1390 if (flags & (flag)) { \ 1391 if (buf[0] != '\0') \ 1392 strlcat(buf, ", ", sizeof(buf)); \ 1393 strlcat(buf, (#flag) + 4, sizeof(buf)); \ 1394 flags &= ~(flag); \ 1395 } \ 1396 } while (0) 1397 buf[0] = '\0'; 1398 CDP_FLAG(CDP_ACTIVE); 1399 CDP_FLAG(CDP_SCHED_DTR); 1400 db_printf("cdp_flags %s\n", buf); 1401 } 1402 #endif 1403