1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * Devfsadm replaces drvconfig, audlinks, disks, tapes, ports, devlinks 31 * as a general purpose device administrative utility. It creates 32 * devices special files in /devices and logical links in /dev, and 33 * coordinates updates to /etc/path_to_instance with the kernel. It 34 * operates in both command line mode to handle user or script invoked 35 * reconfiguration updates, and operates in daemon mode to handle dynamic 36 * reconfiguration for hotplugging support. 37 */ 38 39 #include <string.h> 40 #include <tsol/label.h> 41 #include <bsm/devices.h> 42 #include <bsm/devalloc.h> 43 #include <utime.h> 44 #include "devfsadm_impl.h" 45 46 /* externs from devalloc.c */ 47 extern void _reset_devalloc(int); 48 extern void _update_devalloc_db(devlist_t *, int, int, char *, char *); 49 extern int _da_check_for_usb(char *, char *); 50 51 /* create or remove nodes or links. unset with -n */ 52 static int file_mods = TRUE; 53 54 /* cleanup mode. Set with -C */ 55 static int cleanup = FALSE; 56 57 /* devlinks -d compatibility */ 58 static int devlinks_debug = FALSE; 59 60 /* flag to check if system is labeled */ 61 int system_labeled = FALSE; 62 63 /* flag to enable/disable device allocation with -e/-d */ 64 static int devalloc_flag = 0; 65 66 /* flag to update device allocation database for this device type */ 67 static int update_devdb = 0; 68 69 /* 70 * devices to be deallocated with -d : 71 * audio, floppy, cd, floppy, tape, rmdisk. 72 */ 73 static char *devalloc_list[10] = {DDI_NT_AUDIO, DDI_NT_CD, DDI_NT_CD_CHAN, 74 DDI_NT_FD, DDI_NT_TAPE, DDI_NT_BLOCK_CHAN, 75 DDI_NT_UGEN, DDI_NT_USB_ATTACHMENT_POINT, 76 DDI_NT_SCSI_NEXUS, NULL}; 77 78 /* list of allocatable devices */ 79 static devlist_t devlist; 80 81 /* load a single driver only. set with -i */ 82 static int single_drv = FALSE; 83 static char *driver = NULL; 84 85 /* attempt to load drivers or defer attach nodes */ 86 static int load_attach_drv = TRUE; 87 88 /* set if invoked via /usr/lib/devfsadm/devfsadmd */ 89 static int daemon_mode = FALSE; 90 91 /* output directed to syslog during daemon mode if set */ 92 static int logflag = FALSE; 93 94 /* build links in /dev. -x to turn off */ 95 static int build_dev = TRUE; 96 97 /* build nodes in /devices. -y to turn off */ 98 static int build_devices = TRUE; 99 100 /* -z to turn off */ 101 static int flush_path_to_inst_enable = TRUE; 102 103 /* variables used for path_to_inst flushing */ 104 static int inst_count = 0; 105 static mutex_t count_lock; 106 static cond_t cv; 107 108 /* variables for minor_fini thread */ 109 static mutex_t minor_fini_mutex; 110 static int minor_fini_canceled = TRUE; 111 static int minor_fini_delayed = FALSE; 112 static cond_t minor_fini_cv; 113 static int minor_fini_timeout = MINOR_FINI_TIMEOUT_DEFAULT; 114 115 /* single-threads /dev modification */ 116 static sema_t dev_sema; 117 118 /* the program we were invoked as; ie argv[0] */ 119 static char *prog; 120 121 /* pointers to create/remove link lists */ 122 static create_list_t *create_head = NULL; 123 static remove_list_t *remove_head = NULL; 124 125 /* supports the class -c option */ 126 static char **classes = NULL; 127 static int num_classes = 0; 128 129 /* used with verbose option -v or -V */ 130 static int num_verbose = 0; 131 static char **verbose = NULL; 132 133 static struct mperm *minor_perms = NULL; 134 static driver_alias_t *driver_aliases = NULL; 135 136 /* set if -r alternate root given */ 137 static char *root_dir = ""; 138 139 /* /devices or <rootdir>/devices */ 140 static char *devices_dir = DEVICES; 141 142 /* /dev or <rootdir>/dev */ 143 static char *dev_dir = DEV; 144 145 /* /etc/dev or <rootdir>/etc/dev */ 146 static char *etc_dev_dir = ETCDEV; 147 148 /* 149 * writable root (for lock files and doors during install). 150 * This is also root dir for /dev attr dir during install. 151 */ 152 static char *attr_root = NULL; 153 154 /* /etc/path_to_inst unless -p used */ 155 static char *inst_file = INSTANCE_FILE; 156 157 /* /usr/lib/devfsadm/linkmods unless -l used */ 158 static char *module_dirs = MODULE_DIRS; 159 160 /* default uid/gid used if /etc/minor_perm entry not found */ 161 static uid_t root_uid; 162 static gid_t sys_gid; 163 164 /* /etc/devlink.tab unless devlinks -t used */ 165 static char *devlinktab_file = NULL; 166 167 /* set if /dev link is new. speeds up rm_stale_links */ 168 static int linknew = TRUE; 169 170 /* variables for devlink.tab compat processing */ 171 static devlinktab_list_t *devlinktab_list = NULL; 172 static unsigned int devlinktab_line = 0; 173 174 /* cache head for devfsadm_enumerate*() functions */ 175 static numeral_set_t *head_numeral_set = NULL; 176 177 /* list list of devfsadm modules */ 178 static module_t *module_head = NULL; 179 180 /* name_to_major list used in utility function */ 181 static n2m_t *n2m_list = NULL; 182 183 /* cache of some links used for performance */ 184 static linkhead_t *headlinkhead = NULL; 185 186 /* locking variables to prevent multiples writes to /dev */ 187 static int hold_dev_lock = FALSE; 188 static int hold_daemon_lock = FALSE; 189 static int dev_lock_fd; 190 static int daemon_lock_fd; 191 static char dev_lockfile[PATH_MAX + 1]; 192 static char daemon_lockfile[PATH_MAX + 1]; 193 194 /* last devinfo node/minor processed. used for performance */ 195 static di_node_t lnode; 196 static di_minor_t lminor; 197 static char lphy_path[PATH_MAX + 1] = {""}; 198 199 /* Globals used by the link database */ 200 static di_devlink_handle_t devlink_cache; 201 static int update_database = FALSE; 202 203 /* Globals used to set logindev perms */ 204 static struct login_dev *login_dev_cache = NULL; 205 static int login_dev_enable = FALSE; 206 207 /* Global to use devinfo snapshot cache */ 208 static int use_snapshot_cache = FALSE; 209 210 /* Global for no-further-processing hash */ 211 static item_t **nfp_hash; 212 static mutex_t nfp_mutex = DEFAULTMUTEX; 213 214 /* 215 * Packaged directories - not removed even when empty. 216 * The dirs must be listed in canonical form 217 * i.e. without leading "/dev/" 218 */ 219 static char *packaged_dirs[] = 220 {"dsk", "rdsk", "term", NULL}; 221 222 /* RCM related globals */ 223 static void *librcm_hdl; 224 static rcm_handle_t *rcm_hdl = NULL; 225 static thread_t process_rcm_events_tid; 226 static struct rcm_eventq *volatile rcm_eventq_head = NULL; 227 static struct rcm_eventq *rcm_eventq_tail = NULL; 228 static mutex_t rcm_eventq_lock; 229 static cond_t rcm_eventq_cv; 230 static volatile int need_to_exit_rcm_event_thread = 0; 231 232 /* Devname globals */ 233 static int devname_debug_msg = 1; 234 static nvlist_t *devname_maps = NULL; 235 static int devname_first_call = 1; 236 static int load_devname_nsmaps = FALSE; 237 static int lookup_door_fd = -1; 238 static char *lookup_door_path; 239 240 static void load_dev_acl(void); 241 static void update_drvconf(major_t); 242 static void check_reconfig_state(void); 243 static void devname_setup_nsmaps(void); 244 static int s_stat(const char *, struct stat *); 245 246 static int is_blank(char *); 247 248 /* sysevent queue related globals */ 249 static mutex_t syseventq_mutex = DEFAULTMUTEX; 250 static syseventq_t *syseventq_front; 251 static syseventq_t *syseventq_back; 252 static void process_syseventq(); 253 254 int 255 main(int argc, char *argv[]) 256 { 257 struct stat tx_stat; 258 struct passwd *pw; 259 struct group *gp; 260 pid_t pid; 261 262 (void) setlocale(LC_ALL, ""); 263 (void) textdomain(TEXT_DOMAIN); 264 265 if ((prog = strrchr(argv[0], '/')) == NULL) { 266 prog = argv[0]; 267 } else { 268 prog++; 269 } 270 271 if (getuid() != 0) { 272 err_print(MUST_BE_ROOT); 273 devfsadm_exit(1); 274 } 275 276 /* 277 * Close all files except stdin/stdout/stderr 278 */ 279 closefrom(3); 280 281 if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) { 282 root_uid = pw->pw_uid; 283 } else { 284 err_print(CANT_FIND_USER, DEFAULT_DEV_USER); 285 root_uid = (uid_t)0; /* assume 0 is root */ 286 } 287 288 /* the default group is sys */ 289 290 if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) { 291 sys_gid = gp->gr_gid; 292 } else { 293 err_print(CANT_FIND_GROUP, DEFAULT_DEV_GROUP); 294 sys_gid = (gid_t)3; /* assume 3 is sys */ 295 } 296 297 (void) umask(0); 298 299 system_labeled = is_system_labeled(); 300 if (system_labeled == FALSE) { 301 /* 302 * is_system_labeled() will return false in case we are 303 * starting before the first reboot after Trusted Extensions 304 * is installed. we check for a well known TX binary to 305 * to see if TX is installed. 306 */ 307 if (stat(DA_LABEL_CHECK, &tx_stat) == 0) 308 system_labeled = TRUE; 309 else { 310 /* test hook: see also mkdevalloc.c and allocate.c */ 311 system_labeled = is_system_labeled_debug(&tx_stat); 312 } 313 } 314 315 parse_args(argc, argv); 316 317 (void) sema_init(&dev_sema, 1, USYNC_THREAD, NULL); 318 319 /* Initialize device allocation list */ 320 devlist.audio = devlist.cd = devlist.floppy = devlist.tape = 321 devlist.rmdisk = NULL; 322 323 if (daemon_mode == TRUE) { 324 /* 325 * Build /dev and /devices before daemonizing if 326 * reconfig booting and daemon invoked with alternate 327 * root. This is to support install. 328 */ 329 if (getenv(RECONFIG_BOOT) != NULL && root_dir[0] != '\0') { 330 vprint(INFO_MID, CONFIGURING); 331 load_dev_acl(); 332 update_drvconf((major_t)-1); 333 process_devinfo_tree(); 334 (void) modctl(MODSETMINIROOT); 335 } 336 337 /* 338 * fork before detaching from tty in order to print error 339 * message if unable to acquire file lock. locks not preserved 340 * across forks. Even under debug we want to fork so that 341 * when executed at boot we don't hang. 342 */ 343 if (fork() != 0) { 344 devfsadm_exit(0); 345 } 346 347 /* set directory to / so it coredumps there */ 348 if (chdir("/") == -1) { 349 err_print(CHROOT_FAILED, strerror(errno)); 350 } 351 352 /* only one daemon can run at a time */ 353 if ((pid = enter_daemon_lock()) == getpid()) { 354 detachfromtty(); 355 (void) cond_init(&cv, USYNC_THREAD, 0); 356 (void) mutex_init(&count_lock, USYNC_THREAD, 0); 357 if (thr_create(NULL, NULL, 358 (void *(*)(void *))instance_flush_thread, 359 NULL, THR_DETACHED, NULL) != 0) { 360 err_print(CANT_CREATE_THREAD, "daemon", 361 strerror(errno)); 362 devfsadm_exit(1); 363 } 364 365 /* start the minor_fini_thread */ 366 (void) mutex_init(&minor_fini_mutex, USYNC_THREAD, 0); 367 (void) cond_init(&minor_fini_cv, USYNC_THREAD, 0); 368 if (thr_create(NULL, NULL, 369 (void *(*)(void *))minor_fini_thread, 370 NULL, THR_DETACHED, NULL)) { 371 err_print(CANT_CREATE_THREAD, "minor_fini", 372 strerror(errno)); 373 devfsadm_exit(1); 374 } 375 376 377 /* 378 * No need for rcm notifications when running 379 * with an alternate root. So initialize rcm only 380 * when devfsadm is running with root dir "/". 381 * Similarly, logindevperms need only be set 382 * in daemon mode and when root dir is "/". 383 */ 384 if (root_dir[0] == '\0') { 385 (void) rcm_init(); 386 login_dev_enable = TRUE; 387 } 388 daemon_update(); 389 } else { 390 err_print(DAEMON_RUNNING, pid); 391 devfsadm_exit(1); 392 } 393 exit_daemon_lock(); 394 395 } else { 396 /* not a daemon, so just build /dev and /devices */ 397 process_devinfo_tree(); 398 if (devalloc_flag != 0) 399 /* Enable/disable device allocation */ 400 _reset_devalloc(devalloc_flag); 401 } 402 return (0); 403 } 404 405 static void 406 update_drvconf(major_t major) 407 { 408 if (modctl(MODLOADDRVCONF, major) != 0) 409 err_print(gettext("update_drvconf failed for major %d\n"), 410 major); 411 } 412 413 414 static void 415 load_dev_acl() 416 { 417 if (load_devpolicy() != 0) 418 err_print(gettext("device policy load failed\n")); 419 load_minor_perm_file(); 420 } 421 422 /* 423 * As devfsadm is run early in boot to provide the kernel with 424 * minor_perm info, we might as well check for reconfig at the 425 * same time to avoid running devfsadm twice. This gets invoked 426 * earlier than the env variable RECONFIG_BOOT is set up. 427 */ 428 static void 429 check_reconfig_state() 430 { 431 struct stat sb; 432 433 if (s_stat("/reconfigure", &sb) == 0) { 434 (void) modctl(MODDEVNAME, MODDEVNAME_RECONFIG, 0); 435 } 436 } 437 438 static void 439 modctl_sysavail() 440 { 441 /* 442 * Inform /dev that system is available, that 443 * implicit reconfig can now be performed. 444 */ 445 (void) modctl(MODDEVNAME, MODDEVNAME_SYSAVAIL, 0); 446 } 447 448 static void 449 set_lock_root(void) 450 { 451 struct stat sb; 452 char *lock_root; 453 size_t len; 454 455 lock_root = attr_root ? attr_root : root_dir; 456 457 len = strlen(lock_root) + strlen(ETCDEV) + 1; 458 etc_dev_dir = s_malloc(len); 459 (void) snprintf(etc_dev_dir, len, "%s%s", lock_root, ETCDEV); 460 461 if (s_stat(etc_dev_dir, &sb) != 0) { 462 s_mkdirp(etc_dev_dir, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); 463 } else if (!S_ISDIR(sb.st_mode)) { 464 err_print(NOT_DIR, etc_dev_dir); 465 devfsadm_exit(1); 466 } 467 } 468 469 470 /* 471 * Parse arguments for all 6 programs handled from devfsadm. 472 */ 473 static void 474 parse_args(int argc, char *argv[]) 475 { 476 char opt; 477 char get_linkcompat_opts = FALSE; 478 char *compat_class; 479 int num_aliases = 0; 480 int len; 481 int retval; 482 int add_bind = FALSE; 483 struct aliases *ap = NULL; 484 struct aliases *a_head = NULL; 485 struct aliases *a_tail = NULL; 486 struct modconfig mc; 487 488 if (strcmp(prog, DISKS) == 0) { 489 compat_class = "disk"; 490 get_linkcompat_opts = TRUE; 491 492 } else if (strcmp(prog, TAPES) == 0) { 493 compat_class = "tape"; 494 get_linkcompat_opts = TRUE; 495 496 } else if (strcmp(prog, PORTS) == 0) { 497 compat_class = "port"; 498 get_linkcompat_opts = TRUE; 499 500 } else if (strcmp(prog, AUDLINKS) == 0) { 501 compat_class = "audio"; 502 get_linkcompat_opts = TRUE; 503 504 } else if (strcmp(prog, DEVLINKS) == 0) { 505 devlinktab_file = DEVLINKTAB_FILE; 506 507 build_devices = FALSE; 508 load_attach_drv = FALSE; 509 510 while ((opt = getopt(argc, argv, "dnr:st:vV:")) != EOF) { 511 switch (opt) { 512 case 'd': 513 file_mods = FALSE; 514 flush_path_to_inst_enable = FALSE; 515 devlinks_debug = TRUE; 516 break; 517 case 'n': 518 /* prevent driver loading and deferred attach */ 519 load_attach_drv = FALSE; 520 break; 521 case 'r': 522 set_root_devices_dev_dir(optarg); 523 if (zone_pathcheck(root_dir) != 524 DEVFSADM_SUCCESS) 525 devfsadm_exit(1); 526 break; 527 case 's': 528 /* 529 * suppress. don't create/remove links/nodes 530 * useful with -v or -V 531 */ 532 file_mods = FALSE; 533 flush_path_to_inst_enable = FALSE; 534 break; 535 case 't': 536 /* supply a non-default table file */ 537 devlinktab_file = optarg; 538 break; 539 case 'v': 540 /* documented verbose flag */ 541 add_verbose_id(VERBOSE_MID); 542 break; 543 case 'V': 544 /* undocumented for extra verbose levels */ 545 add_verbose_id(optarg); 546 break; 547 default: 548 usage(); 549 break; 550 } 551 } 552 553 if (optind < argc) { 554 usage(); 555 } 556 557 } else if (strcmp(prog, DRVCONFIG) == 0) { 558 build_dev = FALSE; 559 560 while ((opt = 561 getopt(argc, argv, "a:bdc:i:m:np:R:r:svV:")) != EOF) { 562 switch (opt) { 563 case 'a': 564 ap = calloc(sizeof (struct aliases), 1); 565 ap->a_name = dequote(optarg); 566 len = strlen(ap->a_name) + 1; 567 if (len > MAXMODCONFNAME) { 568 err_print(ALIAS_TOO_LONG, 569 MAXMODCONFNAME, ap->a_name); 570 devfsadm_exit(1); 571 } 572 ap->a_len = len; 573 if (a_tail == NULL) { 574 a_head = ap; 575 } else { 576 a_tail->a_next = ap; 577 } 578 a_tail = ap; 579 num_aliases++; 580 add_bind = TRUE; 581 break; 582 case 'b': 583 add_bind = TRUE; 584 break; 585 case 'c': 586 (void) strcpy(mc.drvclass, optarg); 587 break; 588 case 'd': 589 /* 590 * need to keep for compatibility, but 591 * do nothing. 592 */ 593 break; 594 case 'i': 595 single_drv = TRUE; 596 (void) strcpy(mc.drvname, optarg); 597 driver = s_strdup(optarg); 598 break; 599 case 'm': 600 mc.major = atoi(optarg); 601 break; 602 case 'n': 603 /* prevent driver loading and deferred attach */ 604 load_attach_drv = FALSE; 605 break; 606 case 'p': 607 /* specify alternate path_to_inst file */ 608 inst_file = s_strdup(optarg); 609 break; 610 case 'R': 611 /* 612 * Private flag for suninstall to populate 613 * device information on the installed root. 614 */ 615 root_dir = s_strdup(optarg); 616 if (zone_pathcheck(root_dir) != 617 DEVFSADM_SUCCESS) 618 devfsadm_exit(devfsadm_copy()); 619 break; 620 case 'r': 621 devices_dir = s_strdup(optarg); 622 if (zone_pathcheck(devices_dir) != 623 DEVFSADM_SUCCESS) 624 devfsadm_exit(1); 625 break; 626 case 's': 627 /* 628 * suppress. don't create nodes 629 * useful with -v or -V 630 */ 631 file_mods = FALSE; 632 flush_path_to_inst_enable = FALSE; 633 break; 634 case 'v': 635 /* documented verbose flag */ 636 add_verbose_id(VERBOSE_MID); 637 break; 638 case 'V': 639 /* undocumented for extra verbose levels */ 640 add_verbose_id(optarg); 641 break; 642 default: 643 usage(); 644 } 645 } 646 647 if (optind < argc) { 648 usage(); 649 } 650 651 if ((add_bind == TRUE) && (mc.major == -1 || 652 mc.drvname[0] == NULL)) { 653 err_print(MAJOR_AND_B_FLAG); 654 devfsadm_exit(1); 655 } 656 if (add_bind == TRUE) { 657 mc.num_aliases = num_aliases; 658 mc.ap = a_head; 659 retval = modctl(MODADDMAJBIND, NULL, (caddr_t)&mc); 660 if (retval < 0) { 661 err_print(MODCTL_ADDMAJBIND); 662 } 663 devfsadm_exit(retval); 664 } 665 666 } else if ((strcmp(prog, DEVFSADM) == 0) || 667 (strcmp(prog, DEVFSADMD) == 0)) { 668 char *zonename = NULL; 669 int init_drvconf = 0; 670 int init_perm = 0; 671 int public_mode = 0; 672 int init_sysavail = 0; 673 674 if (strcmp(prog, DEVFSADMD) == 0) { 675 daemon_mode = TRUE; 676 } 677 678 devlinktab_file = DEVLINKTAB_FILE; 679 680 while ((opt = getopt(argc, argv, 681 "a:Cc:deIi:l:mnp:PR:r:sSt:vV:x:")) != EOF) { 682 if (opt == 'I' || opt == 'P' || opt == 'S') { 683 if (public_mode) 684 usage(); 685 } else { 686 if (init_perm || init_drvconf || init_sysavail) 687 usage(); 688 public_mode = 1; 689 } 690 switch (opt) { 691 case 'a': 692 attr_root = s_strdup(optarg); 693 break; 694 case 'C': 695 cleanup = TRUE; 696 break; 697 case 'c': 698 num_classes++; 699 classes = s_realloc(classes, num_classes * 700 sizeof (char *)); 701 classes[num_classes - 1] = optarg; 702 break; 703 case 'd': 704 if (daemon_mode == FALSE) { 705 /* 706 * Device allocation to be disabled. 707 */ 708 devalloc_flag = DA_OFF; 709 build_dev = FALSE; 710 } 711 break; 712 case 'e': 713 if (daemon_mode == FALSE) { 714 /* 715 * Device allocation to be enabled. 716 */ 717 devalloc_flag = DA_ON; 718 build_dev = FALSE; 719 } 720 break; 721 case 'I': /* update kernel driver.conf cache */ 722 if (daemon_mode == TRUE) 723 usage(); 724 init_drvconf = 1; 725 break; 726 case 'i': 727 single_drv = TRUE; 728 driver = s_strdup(optarg); 729 break; 730 case 'l': 731 /* specify an alternate module load path */ 732 module_dirs = s_strdup(optarg); 733 break; 734 case 'm': 735 load_devname_nsmaps = TRUE; 736 break; 737 case 'n': 738 /* prevent driver loading and deferred attach */ 739 load_attach_drv = FALSE; 740 break; 741 case 'p': 742 /* specify alternate path_to_inst file */ 743 inst_file = s_strdup(optarg); 744 break; 745 case 'P': 746 if (daemon_mode == TRUE) 747 usage(); 748 /* load minor_perm and device_policy */ 749 init_perm = 1; 750 break; 751 case 'R': 752 /* 753 * Private flag for suninstall to populate 754 * device information on the installed root. 755 */ 756 root_dir = s_strdup(optarg); 757 devfsadm_exit(devfsadm_copy()); 758 break; 759 case 'r': 760 set_root_devices_dev_dir(optarg); 761 break; 762 case 's': 763 /* 764 * suppress. don't create/remove links/nodes 765 * useful with -v or -V 766 */ 767 file_mods = FALSE; 768 flush_path_to_inst_enable = FALSE; 769 break; 770 case 'S': 771 if (daemon_mode == TRUE) 772 usage(); 773 init_sysavail = 1; 774 break; 775 case 't': 776 devlinktab_file = optarg; 777 break; 778 case 'v': 779 /* documented verbose flag */ 780 add_verbose_id(VERBOSE_MID); 781 break; 782 case 'V': 783 /* undocumented: specify verbose lvl */ 784 add_verbose_id(optarg); 785 break; 786 case 'x': 787 /* 788 * x is the "private switch" option. The 789 * goal is to not suck up all the other 790 * option letters. 791 */ 792 if (strcmp(optarg, "update_devlinksdb") == 0) { 793 update_database = TRUE; 794 } else if (strcmp(optarg, "no_dev") == 0) { 795 /* don't build /dev */ 796 build_dev = FALSE; 797 } else if (strcmp(optarg, "no_devices") == 0) { 798 /* don't build /devices */ 799 build_devices = FALSE; 800 } else if (strcmp(optarg, "no_p2i") == 0) { 801 /* don't flush path_to_inst */ 802 flush_path_to_inst_enable = FALSE; 803 } else if (strcmp(optarg, "use_dicache") == 0) { 804 use_snapshot_cache = TRUE; 805 } else { 806 usage(); 807 } 808 break; 809 default: 810 usage(); 811 break; 812 } 813 } 814 if (optind < argc) { 815 usage(); 816 } 817 818 /* 819 * We're not in zone mode; Check to see if the rootpath 820 * collides with any zonepaths. 821 */ 822 if (zonename == NULL) { 823 if (zone_pathcheck(root_dir) != DEVFSADM_SUCCESS) 824 devfsadm_exit(1); 825 } 826 827 if (init_drvconf || init_perm || init_sysavail) { 828 /* 829 * Load minor perm before force-loading drivers 830 * so the correct permissions are picked up. 831 */ 832 if (init_perm) { 833 check_reconfig_state(); 834 load_dev_acl(); 835 } 836 if (init_drvconf) 837 update_drvconf((major_t)-1); 838 if (init_sysavail) 839 modctl_sysavail(); 840 devfsadm_exit(0); 841 /* NOTREACHED */ 842 } 843 844 if (load_devname_nsmaps == TRUE) { 845 devname_setup_nsmaps(); 846 devfsadm_exit(0); 847 } 848 } 849 850 851 if (get_linkcompat_opts == TRUE) { 852 853 build_devices = FALSE; 854 load_attach_drv = FALSE; 855 num_classes++; 856 classes = s_realloc(classes, num_classes * 857 sizeof (char *)); 858 classes[num_classes - 1] = compat_class; 859 860 while ((opt = getopt(argc, argv, "Cnr:svV:")) != EOF) { 861 switch (opt) { 862 case 'C': 863 cleanup = TRUE; 864 break; 865 case 'n': 866 /* prevent driver loading or deferred attach */ 867 load_attach_drv = FALSE; 868 break; 869 case 'r': 870 set_root_devices_dev_dir(optarg); 871 if (zone_pathcheck(root_dir) != 872 DEVFSADM_SUCCESS) 873 devfsadm_exit(1); 874 break; 875 case 's': 876 /* suppress. don't create/remove links/nodes */ 877 /* useful with -v or -V */ 878 file_mods = FALSE; 879 flush_path_to_inst_enable = FALSE; 880 break; 881 case 'v': 882 /* documented verbose flag */ 883 add_verbose_id(VERBOSE_MID); 884 break; 885 case 'V': 886 /* undocumented for extra verbose levels */ 887 add_verbose_id(optarg); 888 break; 889 default: 890 usage(); 891 } 892 } 893 if (optind < argc) { 894 usage(); 895 } 896 } 897 set_lock_root(); 898 } 899 900 void 901 usage(void) 902 { 903 if (strcmp(prog, DEVLINKS) == 0) { 904 err_print(DEVLINKS_USAGE); 905 } else if (strcmp(prog, DRVCONFIG) == 0) { 906 err_print(DRVCONFIG_USAGE); 907 } else if ((strcmp(prog, DEVFSADM) == 0) || 908 (strcmp(prog, DEVFSADMD) == 0)) { 909 err_print(DEVFSADM_USAGE); 910 } else { 911 err_print(COMPAT_LINK_USAGE); 912 } 913 914 devfsadm_exit(1); 915 } 916 917 static void 918 devi_tree_walk(struct dca_impl *dcip, int flags, char *ev_subclass) 919 { 920 char *msg, *name; 921 struct mlist mlist = {0}; 922 di_node_t node; 923 924 vprint(CHATTY_MID, "devi_tree_walk: root=%s, minor=%s, driver=%s," 925 " error=%d, flags=%u\n", dcip->dci_root, 926 dcip->dci_minor ? dcip->dci_minor : "<NULL>", 927 dcip->dci_driver ? dcip->dci_driver : "<NULL>", dcip->dci_error, 928 dcip->dci_flags); 929 930 assert(dcip->dci_root); 931 932 if (dcip->dci_flags & DCA_LOAD_DRV) { 933 node = di_init_driver(dcip->dci_driver, flags); 934 msg = DRIVER_FAILURE; 935 name = dcip->dci_driver; 936 } else { 937 node = di_init(dcip->dci_root, flags); 938 msg = DI_INIT_FAILED; 939 name = dcip->dci_root; 940 } 941 942 if (node == DI_NODE_NIL) { 943 dcip->dci_error = errno; 944 /* 945 * Rapid hotplugging (commonly seen during USB testing), 946 * may remove a device before the create event for it 947 * has been processed. To prevent alarming users with 948 * a superfluous message, we suppress error messages 949 * for ENXIO and hotplug. 950 */ 951 if (!(errno == ENXIO && (dcip->dci_flags & DCA_HOT_PLUG))) 952 err_print(msg, name, strerror(dcip->dci_error)); 953 return; 954 } 955 956 if (dcip->dci_flags & DCA_FLUSH_PATHINST) 957 flush_path_to_inst(); 958 959 dcip->dci_arg = &mlist; 960 961 vprint(CHATTY_MID, "walking device tree\n"); 962 963 (void) di_walk_minor(node, NULL, DI_CHECK_ALIAS, dcip, 964 check_minor_type); 965 966 process_deferred_links(dcip, DCA_CREATE_LINK); 967 968 dcip->dci_arg = NULL; 969 970 /* 971 * Finished creating devfs files and dev links. 972 * Log sysevent and notify RCM. 973 */ 974 if (ev_subclass) 975 build_and_enq_event(EC_DEV_ADD, ev_subclass, dcip->dci_root, 976 node); 977 978 if ((dcip->dci_flags & DCA_NOTIFY_RCM) && rcm_hdl) 979 (void) notify_rcm(node, dcip->dci_minor); 980 981 /* Add new device to device allocation database */ 982 if (system_labeled && update_devdb) { 983 _update_devalloc_db(&devlist, 0, DA_ADD, NULL, root_dir); 984 update_devdb = 0; 985 } 986 987 di_fini(node); 988 } 989 990 static void 991 process_deferred_links(struct dca_impl *dcip, int flags) 992 { 993 struct mlist *dep; 994 struct minor *mp, *smp; 995 996 vprint(CHATTY_MID, "processing deferred links\n"); 997 998 dep = dcip->dci_arg; 999 1000 /* 1001 * The list head is not used during the deferred create phase 1002 */ 1003 dcip->dci_arg = NULL; 1004 1005 assert(dep); 1006 assert((dep->head == NULL) ^ (dep->tail != NULL)); 1007 assert(flags == DCA_FREE_LIST || flags == DCA_CREATE_LINK); 1008 1009 for (smp = NULL, mp = dep->head; mp; mp = mp->next) { 1010 if (flags == DCA_CREATE_LINK) 1011 (void) check_minor_type(mp->node, mp->minor, dcip); 1012 free(smp); 1013 smp = mp; 1014 } 1015 1016 free(smp); 1017 } 1018 1019 /* 1020 * Called in non-daemon mode to take a snap shot of the devinfo tree. 1021 * Then it calls the appropriate functions to build /devices and /dev. 1022 * It also flushes path_to_inst. 1023 * DINFOCACHE snapshot needs to be updated when devfsadm is run. 1024 * This will only happen if the flags that devfsadm uses matches the flags 1025 * that DINFOCACHE uses and that is why flags is set to 1026 * DI_CACHE_SNAPSHOT_FLAGS. 1027 */ 1028 void 1029 process_devinfo_tree() 1030 { 1031 uint_t flags = DI_CACHE_SNAPSHOT_FLAGS; 1032 struct dca_impl dci; 1033 char name[MAXNAMELEN]; 1034 char *fcn = "process_devinfo_tree: "; 1035 1036 vprint(CHATTY_MID, "%senter\n", fcn); 1037 1038 dca_impl_init("/", NULL, &dci); 1039 1040 lock_dev(); 1041 1042 /* 1043 * Update kernel driver.conf cache when devfsadm/drvconfig 1044 * is invoked to build /devices and /dev. 1045 */ 1046 if (load_attach_drv == TRUE) 1047 update_drvconf((major_t)-1); 1048 1049 if (single_drv == TRUE) { 1050 /* 1051 * load a single driver, but walk the entire devinfo tree 1052 */ 1053 if (load_attach_drv == FALSE) 1054 err_print(DRV_LOAD_REQD); 1055 1056 vprint(CHATTY_MID, "%sattaching driver (%s)\n", fcn, driver); 1057 1058 dci.dci_flags |= DCA_LOAD_DRV; 1059 (void) snprintf(name, sizeof (name), "%s", driver); 1060 dci.dci_driver = name; 1061 1062 } else if (load_attach_drv == TRUE) { 1063 /* 1064 * Load and attach all drivers, then walk the entire tree. 1065 * If the cache flag is set, use DINFOCACHE to get cached 1066 * data. 1067 */ 1068 if (use_snapshot_cache == TRUE) { 1069 flags = DINFOCACHE; 1070 vprint(CHATTY_MID, "%susing snapshot cache\n", fcn); 1071 } else { 1072 vprint(CHATTY_MID, "%sattaching all drivers\n", fcn); 1073 flags |= DINFOFORCE; 1074 if (cleanup) { 1075 /* 1076 * remove dangling entries from /etc/devices 1077 * files. 1078 */ 1079 flags |= DINFOCLEANUP; 1080 } 1081 } 1082 } 1083 1084 if (((load_attach_drv == TRUE) || (single_drv == TRUE)) && 1085 (build_devices == TRUE)) { 1086 dci.dci_flags |= DCA_FLUSH_PATHINST; 1087 } 1088 1089 /* handle pre-cleanup operations desired by the modules. */ 1090 pre_and_post_cleanup(RM_PRE); 1091 1092 devi_tree_walk(&dci, flags, NULL); 1093 1094 if (dci.dci_error) { 1095 devfsadm_exit(1); 1096 } 1097 1098 /* handle post-cleanup operations desired by the modules. */ 1099 pre_and_post_cleanup(RM_POST); 1100 1101 unlock_dev(SYNC_STATE); 1102 } 1103 1104 /*ARGSUSED*/ 1105 static void 1106 print_cache_signal(int signo) 1107 { 1108 if (signal(SIGUSR1, print_cache_signal) == SIG_ERR) { 1109 err_print("signal SIGUSR1 failed: %s\n", strerror(errno)); 1110 devfsadm_exit(1); 1111 } 1112 } 1113 1114 static void 1115 revoke_lookup_door(void) 1116 { 1117 if (lookup_door_fd != -1) { 1118 if (door_revoke(lookup_door_fd) == -1) { 1119 err_print("door_revoke of %s failed - %s\n", 1120 lookup_door_path, strerror(errno)); 1121 } 1122 } 1123 } 1124 1125 /*ARGSUSED*/ 1126 static void 1127 catch_exit(int signo) 1128 { 1129 revoke_lookup_door(); 1130 } 1131 1132 /* 1133 * Register with eventd for messages. Create doors for synchronous 1134 * link creation. 1135 */ 1136 static void 1137 daemon_update(void) 1138 { 1139 int fd; 1140 char *fcn = "daemon_update: "; 1141 char door_file[MAXPATHLEN]; 1142 const char *subclass_list; 1143 sysevent_handle_t *sysevent_hp; 1144 vprint(CHATTY_MID, "%senter\n", fcn); 1145 1146 if (signal(SIGUSR1, print_cache_signal) == SIG_ERR) { 1147 err_print("signal SIGUSR1 failed: %s\n", strerror(errno)); 1148 devfsadm_exit(1); 1149 } 1150 if (signal(SIGTERM, catch_exit) == SIG_ERR) { 1151 err_print("signal SIGTERM failed: %s\n", strerror(errno)); 1152 devfsadm_exit(1); 1153 } 1154 1155 if (snprintf(door_file, sizeof (door_file), 1156 "%s%s", attr_root ? attr_root : root_dir, DEVFSADM_SERVICE_DOOR) 1157 >= sizeof (door_file)) { 1158 err_print("update_daemon failed to open sysevent service " 1159 "door\n"); 1160 devfsadm_exit(1); 1161 } 1162 if ((sysevent_hp = sysevent_open_channel_alt( 1163 door_file)) == NULL) { 1164 err_print(CANT_CREATE_DOOR, 1165 door_file, strerror(errno)); 1166 devfsadm_exit(1); 1167 } 1168 if (sysevent_bind_subscriber(sysevent_hp, event_handler) != 0) { 1169 err_print(CANT_CREATE_DOOR, 1170 door_file, strerror(errno)); 1171 (void) sysevent_close_channel(sysevent_hp); 1172 devfsadm_exit(1); 1173 } 1174 subclass_list = EC_SUB_ALL; 1175 if (sysevent_register_event(sysevent_hp, EC_ALL, &subclass_list, 1) 1176 != 0) { 1177 err_print(CANT_CREATE_DOOR, 1178 door_file, strerror(errno)); 1179 (void) sysevent_unbind_subscriber(sysevent_hp); 1180 (void) sysevent_close_channel(sysevent_hp); 1181 devfsadm_exit(1); 1182 } 1183 if (snprintf(door_file, sizeof (door_file), "%s/%s", 1184 etc_dev_dir, DEVFSADM_SYNCH_DOOR) >= sizeof (door_file)) { 1185 err_print(CANT_CREATE_DOOR, DEVFSADM_SYNCH_DOOR, 1186 strerror(ENAMETOOLONG)); 1187 devfsadm_exit(1); 1188 } 1189 1190 (void) s_unlink(door_file); 1191 if ((fd = open(door_file, O_RDWR | O_CREAT, SYNCH_DOOR_PERMS)) == -1) { 1192 err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); 1193 devfsadm_exit(1); 1194 } 1195 (void) close(fd); 1196 1197 if ((fd = door_create(sync_handler, NULL, 1198 DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) { 1199 err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); 1200 (void) s_unlink(door_file); 1201 devfsadm_exit(1); 1202 } 1203 1204 if (fattach(fd, door_file) == -1) { 1205 err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); 1206 (void) s_unlink(door_file); 1207 devfsadm_exit(1); 1208 } 1209 1210 /* 1211 * devname_lookup_door 1212 */ 1213 if (snprintf(door_file, sizeof (door_file), "%s/%s", 1214 etc_dev_dir, DEVNAME_LOOKUP_DOOR) >= sizeof (door_file)) { 1215 err_print(CANT_CREATE_DOOR, DEVNAME_LOOKUP_DOOR, 1216 strerror(ENAMETOOLONG)); 1217 devfsadm_exit(1); 1218 } 1219 1220 (void) s_unlink(door_file); 1221 if ((fd = open(door_file, O_RDWR | O_CREAT, S_IRUSR|S_IWUSR)) == -1) { 1222 err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); 1223 devfsadm_exit(1); 1224 } 1225 (void) close(fd); 1226 1227 if ((fd = door_create(devname_lookup_handler, NULL, 1228 DOOR_REFUSE_DESC)) == -1) { 1229 err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); 1230 (void) s_unlink(door_file); 1231 devfsadm_exit(1); 1232 } 1233 1234 (void) fdetach(door_file); 1235 lookup_door_path = s_strdup(door_file); 1236 retry: 1237 if (fattach(fd, door_file) == -1) { 1238 if (errno == EBUSY) 1239 goto retry; 1240 err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); 1241 (void) s_unlink(door_file); 1242 devfsadm_exit(1); 1243 } 1244 lookup_door_fd = fd; 1245 1246 /* pass down the door name to kernel for door_ki_open */ 1247 if (devname_kcall(MODDEVNAME_LOOKUPDOOR, (void *)door_file) != 0) 1248 err_print(DEVNAME_CONTACT_FAILED, strerror(errno)); 1249 else 1250 devname_setup_nsmaps(); 1251 1252 vprint(CHATTY_MID, "%spausing\n", fcn); 1253 for (;;) { 1254 (void) pause(); 1255 } 1256 } 1257 1258 /*ARGSUSED*/ 1259 static void 1260 sync_handler(void *cookie, char *ap, size_t asize, 1261 door_desc_t *dp, uint_t ndesc) 1262 { 1263 door_cred_t dcred; 1264 struct dca_off *dcp, rdca; 1265 struct dca_impl dci; 1266 1267 /* 1268 * Must be root to make this call 1269 * If caller is not root, don't touch its data. 1270 */ 1271 if (door_cred(&dcred) != 0 || dcred.dc_euid != 0) { 1272 dcp = ⤷ 1273 dcp->dca_error = EPERM; 1274 goto out; 1275 } 1276 1277 assert(ap); 1278 assert(asize == sizeof (*dcp)); 1279 1280 dcp = (void *)ap; 1281 1282 /* 1283 * Root is always present and is the first component of "name" member 1284 */ 1285 assert(dcp->dca_root == 0); 1286 1287 /* 1288 * The structure passed in by the door_client uses offsets 1289 * instead of pointers to work across address space boundaries. 1290 * Now copy the data into a structure (dca_impl) which uses 1291 * pointers. 1292 */ 1293 dci.dci_root = &dcp->dca_name[dcp->dca_root]; 1294 dci.dci_minor = dcp->dca_minor ? &dcp->dca_name[dcp->dca_minor] : NULL; 1295 dci.dci_driver = 1296 dcp->dca_driver ? &dcp->dca_name[dcp->dca_driver] : NULL; 1297 dci.dci_error = 0; 1298 dci.dci_flags = dcp->dca_flags | (dci.dci_driver ? DCA_LOAD_DRV : 0); 1299 dci.dci_arg = NULL; 1300 1301 lock_dev(); 1302 devi_tree_walk(&dci, DINFOCPYALL, NULL); 1303 dcp->dca_error = dci.dci_error; 1304 1305 if (dcp->dca_flags & DCA_DEVLINK_SYNC) 1306 unlock_dev(SYNC_STATE); 1307 else 1308 unlock_dev(CACHE_STATE); 1309 1310 out: (void) door_return((char *)dcp, sizeof (*dcp), NULL, 0); 1311 } 1312 1313 static void 1314 lock_dev(void) 1315 { 1316 vprint(CHATTY_MID, "lock_dev(): entered\n"); 1317 1318 if (build_dev == FALSE) 1319 return; 1320 1321 /* lockout other threads from /dev */ 1322 while (sema_wait(&dev_sema) != 0); 1323 1324 /* 1325 * Lock out other devfsadm processes from /dev. 1326 * If this wasn't the last process to run, 1327 * clear caches 1328 */ 1329 if (enter_dev_lock() != getpid()) { 1330 invalidate_enumerate_cache(); 1331 rm_all_links_from_cache(); 1332 (void) di_devlink_close(&devlink_cache, DI_LINK_ERROR); 1333 1334 /* send any sysevents that were queued up. */ 1335 process_syseventq(); 1336 } 1337 1338 /* 1339 * (re)load the reverse links database if not 1340 * already cached. 1341 */ 1342 if (devlink_cache == NULL) 1343 devlink_cache = di_devlink_open(root_dir, 0); 1344 1345 /* 1346 * If modules were unloaded, reload them. Also use module status 1347 * as an indication that we should check to see if other binding 1348 * files need to be reloaded. 1349 */ 1350 if (module_head == NULL) { 1351 load_modules(); 1352 read_minor_perm_file(); 1353 read_driver_aliases_file(); 1354 read_devlinktab_file(); 1355 read_logindevperm_file(); 1356 } 1357 1358 if (module_head != NULL) 1359 return; 1360 1361 if (strcmp(prog, DEVLINKS) == 0) { 1362 if (devlinktab_list == NULL) { 1363 err_print(NO_LINKTAB, devlinktab_file); 1364 err_print(NO_MODULES, module_dirs); 1365 err_print(ABORTING); 1366 devfsadm_exit(1); 1367 } 1368 } else { 1369 err_print(NO_MODULES, module_dirs); 1370 if (strcmp(prog, DEVFSADM) == 0) { 1371 err_print(MODIFY_PATH); 1372 } 1373 } 1374 } 1375 1376 /* 1377 * Unlock the device. If we are processing a CACHE_STATE call, we signal a 1378 * minor_fini_thread delayed SYNC_STATE at the end of the call. If we are 1379 * processing a SYNC_STATE call, we cancel any minor_fini_thread SYNC_STATE 1380 * at both the start and end of the call since we will be doing the SYNC_STATE. 1381 */ 1382 static void 1383 unlock_dev(int flag) 1384 { 1385 assert(flag == SYNC_STATE || flag == CACHE_STATE); 1386 1387 vprint(CHATTY_MID, "unlock_dev(): entered\n"); 1388 1389 /* If we are starting a SYNC_STATE, cancel minor_fini_thread SYNC */ 1390 if (flag == SYNC_STATE) { 1391 (void) mutex_lock(&minor_fini_mutex); 1392 minor_fini_canceled = TRUE; 1393 minor_fini_delayed = FALSE; 1394 (void) mutex_unlock(&minor_fini_mutex); 1395 } 1396 1397 if (build_dev == FALSE) 1398 return; 1399 1400 if (devlink_cache == NULL) { 1401 err_print(NO_DEVLINK_CACHE); 1402 } 1403 assert(devlink_cache); 1404 1405 if (flag == SYNC_STATE) { 1406 unload_modules(); 1407 if (update_database) 1408 (void) di_devlink_update(devlink_cache); 1409 (void) di_devlink_close(&devlink_cache, 0); 1410 1411 /* 1412 * now that the devlinks db cache has been flushed, it is safe 1413 * to send any sysevents that were queued up. 1414 */ 1415 process_syseventq(); 1416 } 1417 1418 exit_dev_lock(); 1419 1420 (void) mutex_lock(&minor_fini_mutex); 1421 if (flag == SYNC_STATE) { 1422 /* We did a SYNC_STATE, cancel minor_fini_thread SYNC */ 1423 minor_fini_canceled = TRUE; 1424 minor_fini_delayed = FALSE; 1425 } else { 1426 /* We did a CACHE_STATE, start delayed minor_fini_thread SYNC */ 1427 minor_fini_canceled = FALSE; 1428 minor_fini_delayed = TRUE; 1429 (void) cond_signal(&minor_fini_cv); 1430 } 1431 (void) mutex_unlock(&minor_fini_mutex); 1432 1433 (void) sema_post(&dev_sema); 1434 } 1435 1436 /* 1437 * Check that if -r is set, it is not any part of a zone--- that is, that 1438 * the zonepath is not a substring of the root path. 1439 */ 1440 static int 1441 zone_pathcheck(char *checkpath) 1442 { 1443 void *dlhdl = NULL; 1444 char *name; 1445 char root[MAXPATHLEN]; /* resolved devfsadm root path */ 1446 char zroot[MAXPATHLEN]; /* zone root path */ 1447 char rzroot[MAXPATHLEN]; /* resolved zone root path */ 1448 char tmp[MAXPATHLEN]; 1449 FILE *cookie; 1450 int err = DEVFSADM_SUCCESS; 1451 1452 if (checkpath[0] == '\0') 1453 return (DEVFSADM_SUCCESS); 1454 1455 /* 1456 * Check if zones is available on this system. 1457 */ 1458 if ((dlhdl = dlopen(LIBZONECFG_PATH, RTLD_LAZY)) == NULL) { 1459 return (DEVFSADM_SUCCESS); 1460 } 1461 1462 bzero(root, sizeof (root)); 1463 if (resolvepath(checkpath, root, sizeof (root) - 1) == -1) { 1464 /* 1465 * In this case the user has done "devfsadm -r" on some path 1466 * which does not yet exist, or we got some other misc. error. 1467 * We punt and don't resolve the path in this case. 1468 */ 1469 (void) strlcpy(root, checkpath, sizeof (root)); 1470 } 1471 1472 if (strlen(root) > 0 && (root[strlen(root) - 1] != '/')) { 1473 (void) snprintf(tmp, sizeof (tmp), "%s/", root); 1474 (void) strlcpy(root, tmp, sizeof (root)); 1475 } 1476 1477 cookie = setzoneent(); 1478 while ((name = getzoneent(cookie)) != NULL) { 1479 /* Skip the global zone */ 1480 if (strcmp(name, GLOBAL_ZONENAME) == 0) { 1481 free(name); 1482 continue; 1483 } 1484 1485 if (zone_get_zonepath(name, zroot, sizeof (zroot)) != Z_OK) { 1486 free(name); 1487 continue; 1488 } 1489 1490 bzero(rzroot, sizeof (rzroot)); 1491 if (resolvepath(zroot, rzroot, sizeof (rzroot) - 1) == -1) { 1492 /* 1493 * Zone path doesn't exist, or other misc error, 1494 * so we try using the non-resolved pathname. 1495 */ 1496 (void) strlcpy(rzroot, zroot, sizeof (rzroot)); 1497 } 1498 if (strlen(rzroot) > 0 && (rzroot[strlen(rzroot) - 1] != '/')) { 1499 (void) snprintf(tmp, sizeof (tmp), "%s/", rzroot); 1500 (void) strlcpy(rzroot, tmp, sizeof (rzroot)); 1501 } 1502 1503 /* 1504 * Finally, the comparison. If the zone root path is a 1505 * leading substring of the root path, fail. 1506 */ 1507 if (strncmp(rzroot, root, strlen(rzroot)) == 0) { 1508 err_print(ZONE_PATHCHECK, root, name); 1509 err = DEVFSADM_FAILURE; 1510 free(name); 1511 break; 1512 } 1513 free(name); 1514 } 1515 endzoneent(cookie); 1516 (void) dlclose(dlhdl); 1517 return (err); 1518 } 1519 1520 /* 1521 * Called by the daemon when it receives an event from the devfsadm SLM 1522 * to syseventd. 1523 * 1524 * The devfsadm SLM uses a private event channel for communication to 1525 * devfsadmd set-up via private libsysevent interfaces. This handler is 1526 * used to bind to the devfsadmd channel for event delivery. 1527 * The devfsadmd SLM insures single calls to this routine as well as 1528 * synchronized event delivery. 1529 * 1530 */ 1531 static void 1532 event_handler(sysevent_t *ev) 1533 { 1534 char *path; 1535 char *minor; 1536 char *subclass; 1537 char *dev_ev_subclass; 1538 char *driver_name; 1539 nvlist_t *attr_list = NULL; 1540 int err = 0; 1541 int instance; 1542 int branch_event = 0; 1543 1544 subclass = sysevent_get_subclass_name(ev); 1545 vprint(EVENT_MID, "event_handler: %s id:0X%llx\n", 1546 subclass, sysevent_get_seq(ev)); 1547 1548 if (strcmp(subclass, ESC_DEVFS_START) == 0) { 1549 return; 1550 } 1551 1552 /* Check if event is an instance modification */ 1553 if (strcmp(subclass, ESC_DEVFS_INSTANCE_MOD) == 0) { 1554 devfs_instance_mod(); 1555 return; 1556 } 1557 if (sysevent_get_attr_list(ev, &attr_list) != 0) { 1558 vprint(EVENT_MID, "event_handler: can not get attr list\n"); 1559 return; 1560 } 1561 1562 if (strcmp(subclass, ESC_DEVFS_DEVI_ADD) == 0 || 1563 strcmp(subclass, ESC_DEVFS_DEVI_REMOVE) == 0 || 1564 strcmp(subclass, ESC_DEVFS_MINOR_CREATE) == 0 || 1565 strcmp(subclass, ESC_DEVFS_MINOR_REMOVE) == 0) { 1566 if ((err = nvlist_lookup_string(attr_list, DEVFS_PATHNAME, 1567 &path)) != 0) 1568 goto out; 1569 1570 if (strcmp(subclass, ESC_DEVFS_DEVI_ADD) == 0 || 1571 strcmp(subclass, ESC_DEVFS_DEVI_REMOVE) == 0) { 1572 if (nvlist_lookup_string(attr_list, DEVFS_DEVI_CLASS, 1573 &dev_ev_subclass) != 0) 1574 dev_ev_subclass = NULL; 1575 1576 if (nvlist_lookup_string(attr_list, DEVFS_DRIVER_NAME, 1577 &driver_name) != 0) 1578 driver_name = NULL; 1579 1580 if (nvlist_lookup_int32(attr_list, DEVFS_INSTANCE, 1581 &instance) != 0) 1582 instance = -1; 1583 1584 if (nvlist_lookup_int32(attr_list, DEVFS_BRANCH_EVENT, 1585 &branch_event) != 0) 1586 branch_event = 0; 1587 1588 } else { 1589 if (nvlist_lookup_string(attr_list, DEVFS_MINOR_NAME, 1590 &minor) != 0) 1591 minor = NULL; 1592 } 1593 1594 lock_dev(); 1595 1596 if (strcmp(ESC_DEVFS_DEVI_ADD, subclass) == 0) { 1597 add_minor_pathname(path, NULL, dev_ev_subclass); 1598 if (branch_event) { 1599 build_and_enq_event(EC_DEV_BRANCH, 1600 ESC_DEV_BRANCH_ADD, path, DI_NODE_NIL); 1601 } 1602 1603 } else if (strcmp(ESC_DEVFS_MINOR_CREATE, subclass) == 0) { 1604 add_minor_pathname(path, minor, NULL); 1605 1606 } else if (strcmp(ESC_DEVFS_MINOR_REMOVE, subclass) == 0) { 1607 hot_cleanup(path, minor, NULL, NULL, -1); 1608 1609 } else { /* ESC_DEVFS_DEVI_REMOVE */ 1610 hot_cleanup(path, NULL, dev_ev_subclass, 1611 driver_name, instance); 1612 if (branch_event) { 1613 build_and_enq_event(EC_DEV_BRANCH, 1614 ESC_DEV_BRANCH_REMOVE, path, DI_NODE_NIL); 1615 } 1616 } 1617 1618 unlock_dev(CACHE_STATE); 1619 1620 } else if (strcmp(subclass, ESC_DEVFS_BRANCH_ADD) == 0 || 1621 strcmp(subclass, ESC_DEVFS_BRANCH_REMOVE) == 0) { 1622 if ((err = nvlist_lookup_string(attr_list, 1623 DEVFS_PATHNAME, &path)) != 0) 1624 goto out; 1625 1626 /* just log ESC_DEV_BRANCH... event */ 1627 if (strcmp(subclass, ESC_DEVFS_BRANCH_ADD) == 0) 1628 dev_ev_subclass = ESC_DEV_BRANCH_ADD; 1629 else 1630 dev_ev_subclass = ESC_DEV_BRANCH_REMOVE; 1631 1632 lock_dev(); 1633 build_and_enq_event(EC_DEV_BRANCH, dev_ev_subclass, path, 1634 DI_NODE_NIL); 1635 unlock_dev(CACHE_STATE); 1636 } else 1637 err_print(UNKNOWN_EVENT, subclass); 1638 1639 out: 1640 if (err) 1641 err_print(EVENT_ATTR_LOOKUP_FAILED, strerror(err)); 1642 nvlist_free(attr_list); 1643 } 1644 1645 static void 1646 dca_impl_init(char *root, char *minor, struct dca_impl *dcip) 1647 { 1648 assert(root); 1649 1650 dcip->dci_root = root; 1651 dcip->dci_minor = minor; 1652 dcip->dci_driver = NULL; 1653 dcip->dci_error = 0; 1654 dcip->dci_flags = 0; 1655 dcip->dci_arg = NULL; 1656 } 1657 1658 /* 1659 * Kernel logs a message when a devinfo node is attached. Try to create 1660 * /dev and /devices for each minor node. minorname can be NULL. 1661 */ 1662 void 1663 add_minor_pathname(char *node, char *minor, char *ev_subclass) 1664 { 1665 struct dca_impl dci; 1666 1667 vprint(CHATTY_MID, "add_minor_pathname: node_path=%s minor=%s\n", 1668 node, minor ? minor : "NULL"); 1669 1670 dca_impl_init(node, minor, &dci); 1671 1672 /* 1673 * Restrict hotplug link creation if daemon 1674 * started with -i option. 1675 */ 1676 if (single_drv == TRUE) { 1677 dci.dci_driver = driver; 1678 } 1679 1680 /* 1681 * We are being invoked in response to a hotplug 1682 * event. Also, notify RCM if nodetype indicates 1683 * a network device has been hotplugged. 1684 */ 1685 dci.dci_flags = DCA_HOT_PLUG | DCA_CHECK_TYPE; 1686 1687 devi_tree_walk(&dci, DINFOPROP|DINFOMINOR, ev_subclass); 1688 } 1689 1690 static di_node_t 1691 find_clone_node() 1692 { 1693 static di_node_t clone_node = DI_NODE_NIL; 1694 1695 if (clone_node == DI_NODE_NIL) 1696 clone_node = di_init("/pseudo/clone@0", DINFOPROP); 1697 return (clone_node); 1698 } 1699 1700 static int 1701 is_descendent_of(di_node_t node, char *driver) 1702 { 1703 while (node != DI_NODE_NIL) { 1704 char *drv = di_driver_name(node); 1705 if (strcmp(drv, driver) == 0) 1706 return (1); 1707 node = di_parent_node(node); 1708 } 1709 return (0); 1710 } 1711 1712 /* 1713 * Checks the minor type. If it is an alias node, then lookup 1714 * the real node/minor first, then call minor_process() to 1715 * do the real work. 1716 */ 1717 static int 1718 check_minor_type(di_node_t node, di_minor_t minor, void *arg) 1719 { 1720 ddi_minor_type minor_type; 1721 di_node_t clone_node; 1722 char *mn; 1723 char *nt; 1724 struct mlist *dep; 1725 struct dca_impl *dcip = arg; 1726 1727 assert(dcip); 1728 1729 dep = dcip->dci_arg; 1730 1731 mn = di_minor_name(minor); 1732 1733 /* 1734 * We match driver here instead of in minor_process 1735 * as we want the actual driver name. This check is 1736 * unnecessary during deferred processing. 1737 */ 1738 if (dep && 1739 ((dcip->dci_driver && !is_descendent_of(node, dcip->dci_driver)) || 1740 (dcip->dci_minor && strcmp(mn, dcip->dci_minor)))) { 1741 return (DI_WALK_CONTINUE); 1742 } 1743 1744 if ((dcip->dci_flags & DCA_CHECK_TYPE) && 1745 (nt = di_minor_nodetype(minor)) && 1746 (strcmp(nt, DDI_NT_NET) == 0)) { 1747 dcip->dci_flags |= DCA_NOTIFY_RCM; 1748 dcip->dci_flags &= ~DCA_CHECK_TYPE; 1749 } 1750 1751 minor_type = di_minor_type(minor); 1752 1753 if (minor_type == DDM_MINOR) { 1754 minor_process(node, minor, dep); 1755 1756 } else if (minor_type == DDM_ALIAS) { 1757 struct mlist *cdep, clone_del = {0}; 1758 1759 clone_node = find_clone_node(); 1760 if (clone_node == DI_NODE_NIL) { 1761 err_print(DI_INIT_FAILED, "clone", strerror(errno)); 1762 return (DI_WALK_CONTINUE); 1763 } 1764 1765 cdep = dep ? &clone_del : NULL; 1766 1767 minor_process(clone_node, minor, cdep); 1768 1769 /* 1770 * cache "alias" minor node and free "clone" minor 1771 */ 1772 if (cdep != NULL && cdep->head != NULL) { 1773 assert(cdep->tail != NULL); 1774 cache_deferred_minor(dep, node, minor); 1775 dcip->dci_arg = cdep; 1776 process_deferred_links(dcip, DCA_FREE_LIST); 1777 dcip->dci_arg = dep; 1778 } 1779 } 1780 1781 return (DI_WALK_CONTINUE); 1782 } 1783 1784 1785 /* 1786 * This is the entry point for each minor node, whether walking 1787 * the entire tree via di_walk_minor() or processing a hotplug event 1788 * for a single devinfo node (via hotplug ndi_devi_online()). 1789 */ 1790 /*ARGSUSED*/ 1791 static void 1792 minor_process(di_node_t node, di_minor_t minor, struct mlist *dep) 1793 { 1794 create_list_t *create; 1795 int defer; 1796 1797 vprint(CHATTY_MID, "minor_process: node=%s, minor=%s\n", 1798 di_node_name(node), di_minor_name(minor)); 1799 1800 if (dep != NULL) { 1801 1802 /* 1803 * Reset /devices node to minor_perm perm/ownership 1804 * if we are here to deactivate device allocation 1805 */ 1806 if (build_devices == TRUE) { 1807 reset_node_permissions(node, minor); 1808 } 1809 1810 if (build_dev == FALSE) { 1811 return; 1812 } 1813 1814 /* 1815 * This function will create any nodes for /etc/devlink.tab. 1816 * If devlink.tab handles link creation, we don't call any 1817 * devfsadm modules since that could cause duplicate caching 1818 * in the enumerate functions if different re strings are 1819 * passed that are logically identical. I'm still not 1820 * convinced this would cause any harm, but better to be safe. 1821 * 1822 * Deferred processing is available only for devlinks 1823 * created through devfsadm modules. 1824 */ 1825 if (process_devlink_compat(minor, node) == TRUE) { 1826 return; 1827 } 1828 } else { 1829 vprint(CHATTY_MID, "minor_process: deferred processing\n"); 1830 } 1831 1832 /* 1833 * look for relevant link create rules in the modules, and 1834 * invoke the link create callback function to build a link 1835 * if there is a match. 1836 */ 1837 defer = 0; 1838 for (create = create_head; create != NULL; create = create->next) { 1839 if ((minor_matches_rule(node, minor, create) == TRUE) && 1840 class_ok(create->create->device_class) == 1841 DEVFSADM_SUCCESS) { 1842 if (call_minor_init(create->modptr) == 1843 DEVFSADM_FAILURE) { 1844 continue; 1845 } 1846 1847 /* 1848 * If NOT doing the deferred creates (i.e. 1st pass) and 1849 * rule requests deferred processing cache the minor 1850 * data. 1851 * 1852 * If deferred processing (2nd pass), create links 1853 * ONLY if rule requests deferred processing. 1854 */ 1855 if (dep && ((create->create->flags & CREATE_MASK) == 1856 CREATE_DEFER)) { 1857 defer = 1; 1858 continue; 1859 } else if (dep == NULL && 1860 ((create->create->flags & CREATE_MASK) != 1861 CREATE_DEFER)) { 1862 continue; 1863 } 1864 1865 if ((*(create->create->callback_fcn)) 1866 (minor, node) == DEVFSADM_TERMINATE) { 1867 break; 1868 } 1869 } 1870 } 1871 1872 if (defer) 1873 cache_deferred_minor(dep, node, minor); 1874 } 1875 1876 1877 /* 1878 * Cache node and minor in defer list. 1879 */ 1880 static void 1881 cache_deferred_minor( 1882 struct mlist *dep, 1883 di_node_t node, 1884 di_minor_t minor) 1885 { 1886 struct minor *mp; 1887 const char *fcn = "cache_deferred_minor"; 1888 1889 vprint(CHATTY_MID, "%s node=%s, minor=%s\n", fcn, 1890 di_node_name(node), di_minor_name(minor)); 1891 1892 if (dep == NULL) { 1893 vprint(CHATTY_MID, "%s: cannot cache during " 1894 "deferred processing. Ignoring minor\n", fcn); 1895 return; 1896 } 1897 1898 mp = (struct minor *)s_zalloc(sizeof (struct minor)); 1899 mp->node = node; 1900 mp->minor = minor; 1901 mp->next = NULL; 1902 1903 assert(dep->head == NULL || dep->tail != NULL); 1904 if (dep->head == NULL) { 1905 dep->head = mp; 1906 } else { 1907 dep->tail->next = mp; 1908 } 1909 dep->tail = mp; 1910 } 1911 1912 /* 1913 * Check to see if "create" link creation rule matches this node/minor. 1914 * If it does, return TRUE. 1915 */ 1916 static int 1917 minor_matches_rule(di_node_t node, di_minor_t minor, create_list_t *create) 1918 { 1919 char *m_nodetype, *m_drvname; 1920 1921 if (create->create->node_type != NULL) { 1922 1923 m_nodetype = di_minor_nodetype(minor); 1924 assert(m_nodetype != NULL); 1925 1926 switch (create->create->flags & TYPE_MASK) { 1927 case TYPE_EXACT: 1928 if (strcmp(create->create->node_type, m_nodetype) != 1929 0) { 1930 return (FALSE); 1931 } 1932 break; 1933 case TYPE_PARTIAL: 1934 if (strncmp(create->create->node_type, m_nodetype, 1935 strlen(create->create->node_type)) != 0) { 1936 return (FALSE); 1937 } 1938 break; 1939 case TYPE_RE: 1940 if (regexec(&(create->node_type_comp), m_nodetype, 1941 0, NULL, 0) != 0) { 1942 return (FALSE); 1943 } 1944 break; 1945 } 1946 } 1947 1948 if (create->create->drv_name != NULL) { 1949 m_drvname = di_driver_name(node); 1950 switch (create->create->flags & DRV_MASK) { 1951 case DRV_EXACT: 1952 if (strcmp(create->create->drv_name, m_drvname) != 0) { 1953 return (FALSE); 1954 } 1955 break; 1956 case DRV_RE: 1957 if (regexec(&(create->drv_name_comp), m_drvname, 1958 0, NULL, 0) != 0) { 1959 return (FALSE); 1960 } 1961 break; 1962 } 1963 } 1964 1965 return (TRUE); 1966 } 1967 1968 /* 1969 * If no classes were given on the command line, then return DEVFSADM_SUCCESS. 1970 * Otherwise, return DEVFSADM_SUCCESS if the device "class" from the module 1971 * matches one of the device classes given on the command line, 1972 * otherwise, return DEVFSADM_FAILURE. 1973 */ 1974 static int 1975 class_ok(char *class) 1976 { 1977 int i; 1978 1979 if (num_classes == 0) { 1980 return (DEVFSADM_SUCCESS); 1981 } 1982 1983 for (i = 0; i < num_classes; i++) { 1984 if (strcmp(class, classes[i]) == 0) { 1985 return (DEVFSADM_SUCCESS); 1986 } 1987 } 1988 return (DEVFSADM_FAILURE); 1989 } 1990 1991 /* 1992 * call minor_fini on active modules, then unload ALL modules 1993 */ 1994 static void 1995 unload_modules(void) 1996 { 1997 module_t *module_free; 1998 create_list_t *create_free; 1999 remove_list_t *remove_free; 2000 2001 while (create_head != NULL) { 2002 create_free = create_head; 2003 create_head = create_head->next; 2004 2005 if ((create_free->create->flags & TYPE_RE) == TYPE_RE) { 2006 regfree(&(create_free->node_type_comp)); 2007 } 2008 if ((create_free->create->flags & DRV_RE) == DRV_RE) { 2009 regfree(&(create_free->drv_name_comp)); 2010 } 2011 free(create_free); 2012 } 2013 2014 while (remove_head != NULL) { 2015 remove_free = remove_head; 2016 remove_head = remove_head->next; 2017 free(remove_free); 2018 } 2019 2020 while (module_head != NULL) { 2021 2022 if ((module_head->minor_fini != NULL) && 2023 ((module_head->flags & MODULE_ACTIVE) == MODULE_ACTIVE)) { 2024 (void) (*(module_head->minor_fini))(); 2025 } 2026 2027 vprint(MODLOAD_MID, "unloading module %s\n", module_head->name); 2028 free(module_head->name); 2029 (void) dlclose(module_head->dlhandle); 2030 2031 module_free = module_head; 2032 module_head = module_head->next; 2033 free(module_free); 2034 } 2035 } 2036 2037 /* 2038 * Load devfsadm logical link processing modules. 2039 */ 2040 static void 2041 load_modules(void) 2042 { 2043 DIR *mod_dir; 2044 struct dirent *entp; 2045 char cdir[PATH_MAX + 1]; 2046 char *last; 2047 char *mdir = module_dirs; 2048 char *fcn = "load_modules: "; 2049 2050 while (*mdir != '\0') { 2051 2052 while (*mdir == ':') { 2053 mdir++; 2054 } 2055 2056 if (*mdir == '\0') { 2057 continue; 2058 } 2059 2060 last = strchr(mdir, ':'); 2061 2062 if (last == NULL) { 2063 last = mdir + strlen(mdir); 2064 } 2065 2066 (void) strncpy(cdir, mdir, last - mdir); 2067 cdir[last - mdir] = '\0'; 2068 mdir += strlen(cdir); 2069 2070 if ((mod_dir = opendir(cdir)) == NULL) { 2071 vprint(MODLOAD_MID, "%sopendir(%s): %s\n", 2072 fcn, cdir, strerror(errno)); 2073 continue; 2074 } 2075 2076 while ((entp = readdir(mod_dir)) != NULL) { 2077 2078 if ((strcmp(entp->d_name, ".") == 0) || 2079 (strcmp(entp->d_name, "..") == 0)) { 2080 continue; 2081 } 2082 2083 load_module(entp->d_name, cdir); 2084 } 2085 s_closedir(mod_dir); 2086 } 2087 } 2088 2089 static void 2090 load_module(char *mname, char *cdir) 2091 { 2092 _devfsadm_create_reg_t *create_reg; 2093 _devfsadm_remove_reg_V1_t *remove_reg; 2094 create_list_t *create_list_element; 2095 create_list_t **create_list_next; 2096 remove_list_t *remove_list_element; 2097 remove_list_t **remove_list_next; 2098 char epath[PATH_MAX + 1], *end; 2099 char *fcn = "load_module: "; 2100 char *dlerrstr; 2101 void *dlhandle; 2102 module_t *module; 2103 int flags; 2104 int n; 2105 int i; 2106 2107 /* ignore any file which does not end in '.so' */ 2108 if ((end = strstr(mname, MODULE_SUFFIX)) != NULL) { 2109 if (end[strlen(MODULE_SUFFIX)] != '\0') { 2110 return; 2111 } 2112 } else { 2113 return; 2114 } 2115 2116 (void) snprintf(epath, sizeof (epath), "%s/%s", cdir, mname); 2117 2118 if ((dlhandle = dlopen(epath, RTLD_LAZY)) == NULL) { 2119 dlerrstr = dlerror(); 2120 err_print(DLOPEN_FAILED, epath, 2121 dlerrstr ? dlerrstr : "unknown error"); 2122 return; 2123 } 2124 2125 /* dlsym the _devfsadm_create_reg structure */ 2126 if (NULL == (create_reg = (_devfsadm_create_reg_t *) 2127 dlsym(dlhandle, _DEVFSADM_CREATE_REG))) { 2128 vprint(MODLOAD_MID, "dlsym(%s, %s): symbol not found\n", epath, 2129 _DEVFSADM_CREATE_REG); 2130 } else { 2131 vprint(MODLOAD_MID, "%sdlsym(%s, %s) succeeded\n", 2132 fcn, epath, _DEVFSADM_CREATE_REG); 2133 } 2134 2135 /* dlsym the _devfsadm_remove_reg structure */ 2136 if (NULL == (remove_reg = (_devfsadm_remove_reg_V1_t *) 2137 dlsym(dlhandle, _DEVFSADM_REMOVE_REG))) { 2138 vprint(MODLOAD_MID, "dlsym(%s,\n\t%s): symbol not found\n", 2139 epath, _DEVFSADM_REMOVE_REG); 2140 } else { 2141 vprint(MODLOAD_MID, "dlsym(%s, %s): succeeded\n", 2142 epath, _DEVFSADM_REMOVE_REG); 2143 } 2144 2145 vprint(MODLOAD_MID, "module %s loaded\n", epath); 2146 2147 module = (module_t *)s_malloc(sizeof (module_t)); 2148 module->name = s_strdup(epath); 2149 module->dlhandle = dlhandle; 2150 2151 /* dlsym other module functions, to be called later */ 2152 module->minor_fini = (int (*)())dlsym(dlhandle, MINOR_FINI); 2153 module->minor_init = (int (*)())dlsym(dlhandle, MINOR_INIT); 2154 module->flags = 0; 2155 2156 /* 2157 * put a ptr to each struct devfsadm_create on "create_head" 2158 * list sorted in interpose_lvl. 2159 */ 2160 if (create_reg != NULL) { 2161 for (i = 0; i < create_reg->count; i++) { 2162 int flags = create_reg->tblp[i].flags; 2163 2164 create_list_element = (create_list_t *) 2165 s_malloc(sizeof (create_list_t)); 2166 2167 create_list_element->create = &(create_reg->tblp[i]); 2168 create_list_element->modptr = module; 2169 2170 if (((flags & CREATE_MASK) != 0) && 2171 ((flags & CREATE_MASK) != CREATE_DEFER)) { 2172 free(create_list_element); 2173 err_print("illegal flag combination in " 2174 "module create\n"); 2175 err_print(IGNORING_ENTRY, i, epath); 2176 continue; 2177 } 2178 2179 if (((flags & TYPE_MASK) == 0) ^ 2180 (create_reg->tblp[i].node_type == NULL)) { 2181 free(create_list_element); 2182 err_print("flags value incompatible with " 2183 "node_type value in module create\n"); 2184 err_print(IGNORING_ENTRY, i, epath); 2185 continue; 2186 } 2187 2188 if (((flags & TYPE_MASK) != 0) && 2189 ((flags & TYPE_MASK) != TYPE_EXACT) && 2190 ((flags & TYPE_MASK) != TYPE_RE) && 2191 ((flags & TYPE_MASK) != TYPE_PARTIAL)) { 2192 free(create_list_element); 2193 err_print("illegal TYPE_* flag combination in " 2194 "module create\n"); 2195 err_print(IGNORING_ENTRY, i, epath); 2196 continue; 2197 } 2198 2199 /* precompile regular expression for efficiency */ 2200 if ((flags & TYPE_RE) == TYPE_RE) { 2201 if ((n = regcomp(&(create_list_element-> 2202 node_type_comp), 2203 create_reg->tblp[i].node_type, 2204 REG_EXTENDED)) != 0) { 2205 free(create_list_element); 2206 err_print(REGCOMP_FAILED, 2207 create_reg->tblp[i].node_type, 2208 n); 2209 err_print(IGNORING_ENTRY, i, epath); 2210 continue; 2211 } 2212 } 2213 2214 if (((flags & DRV_MASK) == 0) ^ 2215 (create_reg->tblp[i].drv_name == NULL)) { 2216 if ((flags & TYPE_RE) == TYPE_RE) { 2217 regfree(&(create_list_element-> 2218 node_type_comp)); 2219 } 2220 free(create_list_element); 2221 err_print("flags value incompatible with " 2222 "drv_name value in module create\n"); 2223 err_print(IGNORING_ENTRY, i, epath); 2224 continue; 2225 } 2226 2227 if (((flags & DRV_MASK) != 0) && 2228 ((flags & DRV_MASK) != DRV_EXACT) && 2229 ((flags & DRV_MASK) != DRV_RE)) { 2230 if ((flags & TYPE_RE) == TYPE_RE) { 2231 regfree(&(create_list_element-> 2232 node_type_comp)); 2233 } 2234 free(create_list_element); 2235 err_print("illegal DRV_* flag combination in " 2236 "module create\n"); 2237 err_print(IGNORING_ENTRY, i, epath); 2238 continue; 2239 } 2240 2241 /* precompile regular expression for efficiency */ 2242 if ((create_reg->tblp[i].flags & DRV_RE) == DRV_RE) { 2243 if ((n = regcomp(&(create_list_element-> 2244 drv_name_comp), 2245 create_reg->tblp[i].drv_name, 2246 REG_EXTENDED)) != 0) { 2247 if ((flags & TYPE_RE) == TYPE_RE) { 2248 regfree(&(create_list_element-> 2249 node_type_comp)); 2250 } 2251 free(create_list_element); 2252 err_print(REGCOMP_FAILED, 2253 create_reg->tblp[i].drv_name, 2254 n); 2255 err_print(IGNORING_ENTRY, i, epath); 2256 continue; 2257 } 2258 } 2259 2260 2261 /* add to list sorted by interpose level */ 2262 for (create_list_next = &(create_head); 2263 (*create_list_next != NULL) && 2264 (*create_list_next)->create->interpose_lvl >= 2265 create_list_element->create->interpose_lvl; 2266 create_list_next = 2267 &((*create_list_next)->next)); 2268 create_list_element->next = *create_list_next; 2269 *create_list_next = create_list_element; 2270 } 2271 } 2272 2273 /* 2274 * put a ptr to each struct devfsadm_remove on "remove_head" 2275 * list sorted by interpose_lvl. 2276 */ 2277 flags = 0; 2278 if (remove_reg != NULL) { 2279 if (remove_reg->version < DEVFSADM_V1) 2280 flags |= RM_NOINTERPOSE; 2281 for (i = 0; i < remove_reg->count; i++) { 2282 2283 remove_list_element = (remove_list_t *) 2284 s_malloc(sizeof (remove_list_t)); 2285 2286 remove_list_element->remove = &(remove_reg->tblp[i]); 2287 remove_list_element->remove->flags |= flags; 2288 remove_list_element->modptr = module; 2289 2290 for (remove_list_next = &(remove_head); 2291 (*remove_list_next != NULL) && 2292 (*remove_list_next)->remove->interpose_lvl >= 2293 remove_list_element->remove->interpose_lvl; 2294 remove_list_next = 2295 &((*remove_list_next)->next)); 2296 remove_list_element->next = *remove_list_next; 2297 *remove_list_next = remove_list_element; 2298 } 2299 } 2300 2301 module->next = module_head; 2302 module_head = module; 2303 } 2304 2305 /* 2306 * After we have completed a CACHE_STATE, if a SYNC_STATE does not occur 2307 * within 'timeout' secs the minor_fini_thread needs to do a SYNC_STATE 2308 * so that we still call the minor_fini routines. 2309 */ 2310 /*ARGSUSED*/ 2311 static void 2312 minor_fini_thread(void *arg) 2313 { 2314 timestruc_t abstime; 2315 2316 vprint(INITFINI_MID, "minor_fini_thread starting\n"); 2317 2318 (void) mutex_lock(&minor_fini_mutex); 2319 for (;;) { 2320 /* wait the gather period, or until signaled */ 2321 abstime.tv_sec = time(NULL) + minor_fini_timeout; 2322 abstime.tv_nsec = 0; 2323 (void) cond_timedwait(&minor_fini_cv, 2324 &minor_fini_mutex, &abstime); 2325 2326 /* if minor_fini was canceled, go wait again */ 2327 if (minor_fini_canceled == TRUE) 2328 continue; 2329 2330 /* if minor_fini was delayed, go wait again */ 2331 if (minor_fini_delayed == TRUE) { 2332 minor_fini_delayed = FALSE; 2333 continue; 2334 } 2335 2336 /* done with cancellations and delays, do the SYNC_STATE */ 2337 (void) mutex_unlock(&minor_fini_mutex); 2338 2339 lock_dev(); 2340 unlock_dev(SYNC_STATE); 2341 vprint(INITFINI_MID, "minor_fini sync done\n"); 2342 2343 (void) mutex_lock(&minor_fini_mutex); 2344 } 2345 } 2346 2347 2348 /* 2349 * Attempt to initialize module, if a minor_init routine exists. Set 2350 * the active flag if the routine exists and succeeds. If it doesn't 2351 * exist, just set the active flag. 2352 */ 2353 static int 2354 call_minor_init(module_t *module) 2355 { 2356 char *fcn = "call_minor_init: "; 2357 2358 if ((module->flags & MODULE_ACTIVE) == MODULE_ACTIVE) { 2359 return (DEVFSADM_SUCCESS); 2360 } 2361 2362 vprint(INITFINI_MID, "%smodule %s. current state: inactive\n", 2363 fcn, module->name); 2364 2365 if (module->minor_init == NULL) { 2366 module->flags |= MODULE_ACTIVE; 2367 vprint(INITFINI_MID, "minor_init not defined\n"); 2368 return (DEVFSADM_SUCCESS); 2369 } 2370 2371 if ((*(module->minor_init))() == DEVFSADM_FAILURE) { 2372 err_print(FAILED_FOR_MODULE, MINOR_INIT, module->name); 2373 return (DEVFSADM_FAILURE); 2374 } 2375 2376 vprint(INITFINI_MID, "minor_init() returns DEVFSADM_SUCCESS. " 2377 "new state: active\n"); 2378 2379 module->flags |= MODULE_ACTIVE; 2380 return (DEVFSADM_SUCCESS); 2381 } 2382 2383 /* 2384 * Creates a symlink 'link' to the physical path of node:minor. 2385 * Construct link contents, then call create_link_common(). 2386 */ 2387 /*ARGSUSED*/ 2388 int 2389 devfsadm_mklink(char *link, di_node_t node, di_minor_t minor, int flags) 2390 { 2391 char rcontents[PATH_MAX]; 2392 char devlink[PATH_MAX]; 2393 char phy_path[PATH_MAX]; 2394 char *acontents; 2395 char *dev_path; 2396 int numslashes; 2397 int rv; 2398 int i, link_exists; 2399 int last_was_slash = FALSE; 2400 2401 /* 2402 * try to use devices path 2403 */ 2404 if ((node == lnode) && (minor == lminor)) { 2405 acontents = lphy_path; 2406 } else if (di_minor_type(minor) == DDM_ALIAS) { 2407 /* use /pseudo/clone@0:<driver> as the phys path */ 2408 (void) snprintf(phy_path, sizeof (phy_path), 2409 "/pseudo/clone@0:%s", 2410 di_driver_name(di_minor_devinfo(minor))); 2411 acontents = phy_path; 2412 } else { 2413 if ((dev_path = di_devfs_path(node)) == NULL) { 2414 err_print(DI_DEVFS_PATH_FAILED, strerror(errno)); 2415 devfsadm_exit(1); 2416 } 2417 (void) snprintf(phy_path, sizeof (phy_path), "%s:%s", 2418 dev_path, di_minor_name(minor)); 2419 di_devfs_path_free(dev_path); 2420 acontents = phy_path; 2421 } 2422 2423 /* prepend link with dev_dir contents */ 2424 (void) strlcpy(devlink, dev_dir, sizeof (devlink)); 2425 (void) strlcat(devlink, "/", sizeof (devlink)); 2426 (void) strlcat(devlink, link, sizeof (devlink)); 2427 2428 /* 2429 * Calculate # of ../ to add. Account for double '//' in path. 2430 * Ignore all leading slashes. 2431 */ 2432 for (i = 0; link[i] == '/'; i++) 2433 ; 2434 for (numslashes = 0; link[i] != '\0'; i++) { 2435 if (link[i] == '/') { 2436 if (last_was_slash == FALSE) { 2437 numslashes++; 2438 last_was_slash = TRUE; 2439 } 2440 } else { 2441 last_was_slash = FALSE; 2442 } 2443 } 2444 /* Don't count any trailing '/' */ 2445 if (link[i-1] == '/') { 2446 numslashes--; 2447 } 2448 2449 rcontents[0] = '\0'; 2450 do { 2451 (void) strlcat(rcontents, "../", sizeof (rcontents)); 2452 } while (numslashes-- != 0); 2453 2454 (void) strlcat(rcontents, "devices", sizeof (rcontents)); 2455 (void) strlcat(rcontents, acontents, sizeof (rcontents)); 2456 2457 if (devlinks_debug == TRUE) { 2458 vprint(INFO_MID, "adding link %s ==> %s\n", devlink, rcontents); 2459 } 2460 2461 if ((rv = create_link_common(devlink, rcontents, &link_exists)) 2462 == DEVFSADM_SUCCESS) { 2463 linknew = TRUE; 2464 add_link_to_cache(link, acontents); 2465 if (system_labeled && (flags & DA_ADD)) { 2466 /* 2467 * Add this device to the list of allocatable devices. 2468 */ 2469 int instance = di_instance(node); 2470 2471 (void) da_add_list(&devlist, devlink, instance, flags); 2472 update_devdb = flags; 2473 } 2474 } else { 2475 linknew = FALSE; 2476 } 2477 2478 if (link_exists == TRUE) { 2479 if (system_labeled && (flags & DA_CD)) { 2480 /* 2481 * if this is a removable disk, add it 2482 * as that to device allocation database. 2483 */ 2484 if (_da_check_for_usb(devlink, root_dir) == 1) { 2485 int instance = di_instance(node); 2486 2487 (void) da_add_list(&devlist, devlink, instance, 2488 DA_ADD|DA_RMDISK); 2489 update_devdb = DA_RMDISK; 2490 } 2491 } 2492 /* Link exists or was just created */ 2493 (void) di_devlink_add_link(devlink_cache, link, rcontents, 2494 DI_PRIMARY_LINK); 2495 } 2496 2497 return (rv); 2498 } 2499 2500 /* 2501 * Creates a symlink link to primary_link. Calculates relative 2502 * directory offsets, then calls link_common(). 2503 */ 2504 /*ARGSUSED*/ 2505 int 2506 devfsadm_secondary_link(char *link, char *primary_link, int flags) 2507 { 2508 char contents[PATH_MAX + 1]; 2509 char devlink[PATH_MAX + 1]; 2510 int rv, link_exists; 2511 char *fpath; 2512 char *tpath; 2513 char *op; 2514 2515 /* prepend link with dev_dir contents */ 2516 (void) strcpy(devlink, dev_dir); 2517 (void) strcat(devlink, "/"); 2518 (void) strcat(devlink, link); 2519 /* 2520 * building extra link, so use first link as link contents, but first 2521 * make it relative. 2522 */ 2523 fpath = link; 2524 tpath = primary_link; 2525 op = contents; 2526 2527 while (*fpath == *tpath && *fpath != '\0') { 2528 fpath++, tpath++; 2529 } 2530 2531 /* Count directories to go up, if any, and add "../" */ 2532 while (*fpath != '\0') { 2533 if (*fpath == '/') { 2534 (void) strcpy(op, "../"); 2535 op += 3; 2536 } 2537 fpath++; 2538 } 2539 2540 /* 2541 * Back up to the start of the current path component, in 2542 * case in the middle 2543 */ 2544 while (tpath != primary_link && *(tpath-1) != '/') { 2545 tpath--; 2546 } 2547 (void) strcpy(op, tpath); 2548 2549 if (devlinks_debug == TRUE) { 2550 vprint(INFO_MID, "adding extra link %s ==> %s\n", 2551 devlink, contents); 2552 } 2553 2554 if ((rv = create_link_common(devlink, contents, &link_exists)) 2555 == DEVFSADM_SUCCESS) { 2556 /* 2557 * we need to save the ultimate /devices contents, and not the 2558 * secondary link, since hotcleanup only looks at /devices path. 2559 * Since we don't have devices path here, we can try to get it 2560 * by readlink'ing the secondary link. This assumes the primary 2561 * link was created first. 2562 */ 2563 add_link_to_cache(link, lphy_path); 2564 linknew = TRUE; 2565 if (system_labeled && 2566 ((flags & DA_AUDIO) && (flags & DA_ADD))) { 2567 /* 2568 * Add this device to the list of allocatable devices. 2569 */ 2570 int instance = 0; 2571 2572 op = strrchr(contents, '/'); 2573 op++; 2574 (void) sscanf(op, "%d", &instance); 2575 (void) da_add_list(&devlist, devlink, instance, flags); 2576 update_devdb = flags; 2577 } 2578 } else { 2579 linknew = FALSE; 2580 } 2581 2582 /* 2583 * If link exists or was just created, add it to the database 2584 */ 2585 if (link_exists == TRUE) { 2586 (void) di_devlink_add_link(devlink_cache, link, contents, 2587 DI_SECONDARY_LINK); 2588 } 2589 2590 return (rv); 2591 } 2592 2593 /* returns pointer to the devices directory */ 2594 char * 2595 devfsadm_get_devices_dir() 2596 { 2597 return (devices_dir); 2598 } 2599 2600 /* 2601 * Does the actual link creation. VERBOSE_MID only used if there is 2602 * a change. CHATTY_MID used otherwise. 2603 */ 2604 static int 2605 create_link_common(char *devlink, char *contents, int *exists) 2606 { 2607 int try; 2608 int linksize; 2609 int max_tries = 0; 2610 static int prev_link_existed = TRUE; 2611 char checkcontents[PATH_MAX + 1]; 2612 char *hide; 2613 2614 *exists = FALSE; 2615 2616 /* Database is not updated when file_mods == FALSE */ 2617 if (file_mods == FALSE) { 2618 linksize = readlink(devlink, checkcontents, PATH_MAX); 2619 if (linksize > 0) { 2620 checkcontents[linksize] = '\0'; 2621 if (strcmp(checkcontents, contents) != 0) { 2622 vprint(CHATTY_MID, REMOVING_LINK, 2623 devlink, checkcontents); 2624 return (DEVFSADM_SUCCESS); 2625 } else { 2626 vprint(CHATTY_MID, "link exists and is correct:" 2627 " %s -> %s\n", devlink, contents); 2628 /* failure only in that the link existed */ 2629 return (DEVFSADM_FAILURE); 2630 } 2631 } else { 2632 vprint(VERBOSE_MID, CREATING_LINK, devlink, contents); 2633 return (DEVFSADM_SUCCESS); 2634 } 2635 } 2636 2637 /* 2638 * systems calls are expensive, so predict whether to readlink 2639 * or symlink first, based on previous attempt 2640 */ 2641 if (prev_link_existed == FALSE) { 2642 try = CREATE_LINK; 2643 } else { 2644 try = READ_LINK; 2645 } 2646 2647 while (++max_tries <= 3) { 2648 2649 switch (try) { 2650 case CREATE_LINK: 2651 2652 if (symlink(contents, devlink) == 0) { 2653 vprint(VERBOSE_MID, CREATING_LINK, devlink, 2654 contents); 2655 prev_link_existed = FALSE; 2656 /* link successfully created */ 2657 *exists = TRUE; 2658 set_logindev_perms(devlink); 2659 return (DEVFSADM_SUCCESS); 2660 } else { 2661 switch (errno) { 2662 2663 case ENOENT: 2664 /* dirpath to node doesn't exist */ 2665 hide = strrchr(devlink, '/'); 2666 *hide = '\0'; 2667 s_mkdirp(devlink, S_IRWXU|S_IRGRP| 2668 S_IXGRP|S_IROTH|S_IXOTH); 2669 *hide = '/'; 2670 break; 2671 case EEXIST: 2672 try = READ_LINK; 2673 break; 2674 default: 2675 err_print(SYMLINK_FAILED, devlink, 2676 contents, strerror(errno)); 2677 return (DEVFSADM_FAILURE); 2678 } 2679 } 2680 break; 2681 2682 case READ_LINK: 2683 2684 linksize = readlink(devlink, checkcontents, PATH_MAX); 2685 if (linksize >= 0) { 2686 checkcontents[linksize] = '\0'; 2687 if (strcmp(checkcontents, contents) != 0) { 2688 s_unlink(devlink); 2689 vprint(VERBOSE_MID, REMOVING_LINK, 2690 devlink, checkcontents); 2691 try = CREATE_LINK; 2692 } else { 2693 prev_link_existed = TRUE; 2694 vprint(CHATTY_MID, 2695 "link exists and is correct:" 2696 " %s -> %s\n", devlink, 2697 contents); 2698 *exists = TRUE; 2699 /* failure in that the link existed */ 2700 return (DEVFSADM_FAILURE); 2701 } 2702 } else { 2703 switch (errno) { 2704 case EINVAL: 2705 /* not a symlink, remove and create */ 2706 s_unlink(devlink); 2707 default: 2708 /* maybe it didn't exist at all */ 2709 try = CREATE_LINK; 2710 break; 2711 } 2712 } 2713 break; 2714 } 2715 } 2716 err_print(MAX_ATTEMPTS, devlink, contents); 2717 return (DEVFSADM_FAILURE); 2718 } 2719 2720 static void 2721 set_logindev_perms(char *devlink) 2722 { 2723 struct login_dev *newdev; 2724 struct passwd pwd, *resp; 2725 char pwd_buf[PATH_MAX]; 2726 int rv; 2727 struct stat sb; 2728 char *devfs_path = NULL; 2729 2730 /* 2731 * We only want logindev perms to be set when a device is 2732 * hotplugged or an application requests synchronous creates. 2733 * So we enable this only in daemon mode. In addition, 2734 * login(1) only fixes the std. /dev dir. So we don't 2735 * change perms if alternate root is set. 2736 * login_dev_enable is TRUE only in these cases. 2737 */ 2738 if (login_dev_enable != TRUE) 2739 return; 2740 2741 /* 2742 * Normally, /etc/logindevperm has few (8 - 10 entries) which 2743 * may be regular expressions (globs were converted to RE). 2744 * So just do a linear search through the list. 2745 */ 2746 for (newdev = login_dev_cache; newdev; newdev = newdev->ldev_next) { 2747 vprint(FILES_MID, "matching %s with %s\n", devlink, 2748 newdev->ldev_device); 2749 2750 if (regexec(&newdev->ldev_device_regex, devlink, 0, 2751 NULL, 0) == 0) { 2752 vprint(FILES_MID, "matched %s with %s\n", devlink, 2753 newdev->ldev_device); 2754 break; 2755 } 2756 } 2757 2758 if (newdev == NULL) 2759 return; 2760 2761 /* 2762 * we have a match, now find the driver associated with this 2763 * minor node using a snapshot on the physical path 2764 */ 2765 (void) resolve_link(devlink, NULL, NULL, &devfs_path, 0); 2766 if (devfs_path) { 2767 di_node_t node; 2768 char *drv = NULL; 2769 struct driver_list *list; 2770 char *p; 2771 2772 /* truncate on : so we can take a snapshot */ 2773 (void) strcpy(pwd_buf, devfs_path); 2774 p = strrchr(pwd_buf, ':'); 2775 if (p == NULL) { 2776 free(devfs_path); 2777 return; 2778 } 2779 *p = '\0'; 2780 2781 vprint(FILES_MID, "link=%s->physpath=%s\n", 2782 devlink, pwd_buf); 2783 2784 node = di_init(pwd_buf, DINFOMINOR); 2785 2786 if (node) { 2787 drv = di_driver_name(node); 2788 2789 if (drv) { 2790 vprint(FILES_MID, "%s: driver is %s\n", 2791 devlink, drv); 2792 } 2793 di_fini(node); 2794 } 2795 /* search thru the driver list specified in logindevperm */ 2796 list = newdev->ldev_driver_list; 2797 if ((drv != NULL) && (list != NULL)) { 2798 while (list) { 2799 if (strcmp(list->driver_name, 2800 drv) == 0) { 2801 vprint(FILES_MID, 2802 "driver %s match!\n", drv); 2803 break; 2804 } 2805 list = list->next; 2806 } 2807 if (list == NULL) { 2808 vprint(FILES_MID, "no driver match!\n"); 2809 free(devfs_path); 2810 return; 2811 } 2812 } 2813 free(devfs_path); 2814 } else { 2815 return; 2816 } 2817 2818 vprint(FILES_MID, "changing permissions of %s\n", devlink); 2819 2820 /* 2821 * We have a match. We now attempt to determine the 2822 * owner and group of the console user. 2823 * 2824 * stat() the console device newdev->ldev_console 2825 * which will always exist - it will have the right owner but 2826 * not the right group. Use getpwuid_r() to determine group for this 2827 * uid. 2828 * Note, it is safe to use name service here since if name services 2829 * are not available (during boot or in single-user mode), then 2830 * console owner will be root and its gid can be found in 2831 * local files. 2832 */ 2833 if (stat(newdev->ldev_console, &sb) == -1) { 2834 vprint(VERBOSE_MID, STAT_FAILED, newdev->ldev_console, 2835 strerror(errno)); 2836 return; 2837 } 2838 2839 resp = NULL; 2840 rv = getpwuid_r(sb.st_uid, &pwd, pwd_buf, sizeof (pwd_buf), &resp); 2841 if (rv || resp == NULL) { 2842 rv = rv ? rv : EINVAL; 2843 vprint(VERBOSE_MID, GID_FAILED, sb.st_uid, 2844 strerror(rv)); 2845 return; 2846 } 2847 2848 assert(&pwd == resp); 2849 2850 sb.st_gid = resp->pw_gid; 2851 2852 if (chmod(devlink, newdev->ldev_perms) == -1) { 2853 vprint(VERBOSE_MID, CHMOD_FAILED, devlink, 2854 strerror(errno)); 2855 return; 2856 } 2857 2858 if (chown(devlink, sb.st_uid, sb.st_gid) == -1) { 2859 vprint(VERBOSE_MID, CHOWN_FAILED, devlink, 2860 strerror(errno)); 2861 } 2862 } 2863 2864 /* 2865 * Reset /devices node with appropriate permissions and 2866 * ownership as specified in /etc/minor_perm. 2867 */ 2868 static void 2869 reset_node_permissions(di_node_t node, di_minor_t minor) 2870 { 2871 int devalloc_is_on = 0; 2872 int spectype; 2873 char phy_path[PATH_MAX + 1]; 2874 mode_t mode; 2875 dev_t dev; 2876 uid_t uid; 2877 gid_t gid; 2878 struct stat sb; 2879 char *dev_path, *aminor = NULL; 2880 2881 /* lphy_path starts with / */ 2882 if ((dev_path = di_devfs_path(node)) == NULL) { 2883 err_print(DI_DEVFS_PATH_FAILED, strerror(errno)); 2884 devfsadm_exit(1); 2885 } 2886 (void) strcpy(lphy_path, dev_path); 2887 di_devfs_path_free(dev_path); 2888 2889 (void) strcat(lphy_path, ":"); 2890 if (di_minor_type(minor) == DDM_ALIAS) { 2891 char *driver; 2892 aminor = di_minor_name(minor); 2893 driver = di_driver_name(di_minor_devinfo(minor)); 2894 (void) strcat(lphy_path, driver); 2895 } else 2896 (void) strcat(lphy_path, di_minor_name(minor)); 2897 2898 (void) strcpy(phy_path, devices_dir); 2899 (void) strcat(phy_path, lphy_path); 2900 2901 lnode = node; 2902 lminor = minor; 2903 2904 vprint(CHATTY_MID, "reset_node_permissions: phy_path=%s lphy_path=%s\n", 2905 phy_path, lphy_path); 2906 2907 dev = di_minor_devt(minor); 2908 spectype = di_minor_spectype(minor); /* block or char */ 2909 2910 getattr(phy_path, aminor, spectype, dev, &mode, &uid, &gid); 2911 2912 /* 2913 * compare and set permissions and ownership 2914 * 2915 * Under devfs, a quick insertion and removal of USB devices 2916 * would cause stat of physical path to fail. In this case, 2917 * we emit a verbose message, but don't print errors. 2918 */ 2919 if ((stat(phy_path, &sb) == -1) || (sb.st_rdev != dev)) { 2920 vprint(VERBOSE_MID, NO_DEVFS_NODE, phy_path); 2921 return; 2922 } 2923 2924 /* 2925 * If we are here for a new device 2926 * If device allocation is on 2927 * then 2928 * set ownership to root:other and permissions to 0000 2929 * else 2930 * set ownership and permissions as specified in minor_perm 2931 * If we are here for an existing device 2932 * If device allocation is to be turned on 2933 * then 2934 * reset ownership to root:other and permissions to 0000 2935 * else if device allocation is to be turned off 2936 * reset ownership and permissions to those specified in 2937 * minor_perm 2938 * else 2939 * preserve existing/user-modified ownership and 2940 * permissions 2941 * 2942 * devfs indicates a new device by faking access time to be zero. 2943 */ 2944 devalloc_is_on = da_is_on(); 2945 if (sb.st_atime != 0) { 2946 int i; 2947 char *nt; 2948 2949 if ((devalloc_flag == 0) && (devalloc_is_on != 1)) 2950 /* 2951 * Leave existing devices as they are if we are not 2952 * turning device allocation on/off. 2953 */ 2954 return; 2955 2956 nt = di_minor_nodetype(minor); 2957 2958 if (nt == NULL) 2959 return; 2960 2961 for (i = 0; devalloc_list[i]; i++) { 2962 if (strcmp(nt, devalloc_list[i]) == 0) 2963 /* 2964 * One of the types recognized by devalloc, 2965 * reset attrs. 2966 */ 2967 break; 2968 } 2969 if (devalloc_list[i] == NULL) 2970 return; 2971 } 2972 2973 if (file_mods == FALSE) { 2974 /* Nothing more to do if simulating */ 2975 vprint(VERBOSE_MID, PERM_MSG, phy_path, uid, gid, mode); 2976 return; 2977 } 2978 2979 if ((devalloc_flag == DA_ON) || (devalloc_is_on == 1)) { 2980 /* 2981 * we are here either to turn device allocation on 2982 * or to add a new device while device allocation in on 2983 */ 2984 mode = DEALLOC_MODE; 2985 uid = DA_UID; 2986 gid = DA_GID; 2987 } 2988 2989 if ((devalloc_is_on == 1) || (devalloc_flag == DA_ON) || 2990 (sb.st_mode != mode)) { 2991 if (chmod(phy_path, mode) == -1) 2992 vprint(VERBOSE_MID, CHMOD_FAILED, 2993 phy_path, strerror(errno)); 2994 } 2995 if ((devalloc_is_on == 1) || (devalloc_flag == DA_ON) || 2996 (sb.st_uid != uid || sb.st_gid != gid)) { 2997 if (chown(phy_path, uid, gid) == -1) 2998 vprint(VERBOSE_MID, CHOWN_FAILED, 2999 phy_path, strerror(errno)); 3000 } 3001 3002 /* Report that we actually did something */ 3003 vprint(VERBOSE_MID, PERM_MSG, phy_path, uid, gid, mode); 3004 } 3005 3006 /* 3007 * Removes logical link and the minor node it refers to. If file is a 3008 * link, we recurse and try to remove the minor node (or link if path is 3009 * a double link) that file's link contents refer to. 3010 */ 3011 static void 3012 devfsadm_rm_work(char *file, int recurse, int file_type) 3013 { 3014 char *fcn = "devfsadm_rm_work: "; 3015 int linksize; 3016 char contents[PATH_MAX + 1]; 3017 char nextfile[PATH_MAX + 1]; 3018 char newfile[PATH_MAX + 1]; 3019 char *ptr; 3020 3021 vprint(REMOVE_MID, "%s%s\n", fcn, file); 3022 3023 /* TYPE_LINK split into multiple if's due to excessive indentations */ 3024 if (file_type == TYPE_LINK) { 3025 (void) strcpy(newfile, dev_dir); 3026 (void) strcat(newfile, "/"); 3027 (void) strcat(newfile, file); 3028 } 3029 3030 if ((file_type == TYPE_LINK) && (recurse == TRUE) && 3031 ((linksize = readlink(newfile, contents, PATH_MAX)) > 0)) { 3032 contents[linksize] = '\0'; 3033 3034 if (is_minor_node(contents, &ptr) == DEVFSADM_TRUE) { 3035 devfsadm_rm_work(++ptr, FALSE, TYPE_DEVICES); 3036 } else { 3037 if (strncmp(contents, DEV "/", strlen(DEV) + 1) == 0) { 3038 devfsadm_rm_work(&contents[strlen(DEV) + 1], 3039 TRUE, TYPE_LINK); 3040 } else { 3041 if ((ptr = strrchr(file, '/')) != NULL) { 3042 *ptr = '\0'; 3043 (void) strcpy(nextfile, file); 3044 *ptr = '/'; 3045 (void) strcat(nextfile, "/"); 3046 } else { 3047 (void) strcpy(nextfile, ""); 3048 } 3049 (void) strcat(nextfile, contents); 3050 devfsadm_rm_work(nextfile, TRUE, TYPE_LINK); 3051 } 3052 } 3053 } 3054 3055 if (file_type == TYPE_LINK) { 3056 vprint(VERBOSE_MID, DEVFSADM_UNLINK, newfile); 3057 if (file_mods == TRUE) { 3058 rm_link_from_cache(file); 3059 s_unlink(newfile); 3060 rm_parent_dir_if_empty(newfile); 3061 invalidate_enumerate_cache(); 3062 (void) di_devlink_rm_link(devlink_cache, file); 3063 } 3064 } 3065 3066 /* 3067 * Note: we don't remove /devices entries because they are 3068 * covered by devfs. 3069 */ 3070 } 3071 3072 void 3073 devfsadm_rm_link(char *file) 3074 { 3075 devfsadm_rm_work(file, FALSE, TYPE_LINK); 3076 } 3077 3078 void 3079 devfsadm_rm_all(char *file) 3080 { 3081 devfsadm_rm_work(file, TRUE, TYPE_LINK); 3082 } 3083 3084 static int 3085 s_rmdir(char *path) 3086 { 3087 int i; 3088 char *rpath, *dir; 3089 const char *fcn = "s_rmdir"; 3090 3091 /* 3092 * Certain directories are created at install time by packages. 3093 * Some of them (listed in packaged_dirs[]) are required by apps 3094 * and need to be present even when empty. 3095 */ 3096 vprint(REMOVE_MID, "%s: checking if %s is packaged\n", fcn, path); 3097 3098 rpath = path + strlen(dev_dir) + 1; 3099 3100 for (i = 0; (dir = packaged_dirs[i]) != NULL; i++) { 3101 if (*rpath == *dir) { 3102 if (strcmp(rpath, dir) == 0) { 3103 vprint(REMOVE_MID, "%s: skipping packaged dir: " 3104 "%s\n", fcn, path); 3105 errno = EEXIST; 3106 return (-1); 3107 } 3108 } 3109 } 3110 3111 return (rmdir(path)); 3112 } 3113 3114 /* 3115 * Try to remove any empty directories up the tree. It is assumed that 3116 * pathname is a file that was removed, so start with its parent, and 3117 * work up the tree. 3118 */ 3119 static void 3120 rm_parent_dir_if_empty(char *pathname) 3121 { 3122 char *ptr, path[PATH_MAX + 1]; 3123 char *fcn = "rm_parent_dir_if_empty: "; 3124 finddevhdl_t fhandle; 3125 const char *f; 3126 int rv; 3127 3128 vprint(REMOVE_MID, "%schecking %s if empty\n", fcn, pathname); 3129 3130 (void) strcpy(path, pathname); 3131 3132 /* 3133 * ascend up the dir tree, deleting all empty dirs. 3134 * Return immediately if a dir is not empty. 3135 */ 3136 for (;;) { 3137 3138 if ((ptr = strrchr(path, '/')) == NULL) { 3139 return; 3140 } 3141 3142 *ptr = '\0'; 3143 3144 if ((rv = finddev_readdir(path, &fhandle)) != 0) { 3145 err_print(OPENDIR_FAILED, path, strerror(rv)); 3146 return; 3147 } 3148 3149 /* 3150 * An empty pathlist implies an empty directory 3151 */ 3152 f = finddev_next(fhandle); 3153 finddev_close(fhandle); 3154 if (f == NULL) { 3155 if (s_rmdir(path) == 0) { 3156 vprint(REMOVE_MID, 3157 "%sremoving empty dir %s\n", fcn, path); 3158 } else if (errno == EEXIST) { 3159 vprint(REMOVE_MID, 3160 "%sfailed to remove dir: %s\n", fcn, path); 3161 return; 3162 } 3163 } else { 3164 /* some other file is here, so return */ 3165 vprint(REMOVE_MID, "%sdir not empty: %s\n", fcn, path); 3166 return; 3167 } 3168 } 3169 } 3170 3171 /* 3172 * This function and all the functions it calls below were added to 3173 * handle the unique problem with world wide names (WWN). The problem is 3174 * that if a WWN device is moved to another address on the same controller 3175 * its logical link will change, while the physical node remains the same. 3176 * The result is that two logical links will point to the same physical path 3177 * in /devices, the valid link and a stale link. This function will 3178 * find all the stale nodes, though at a significant performance cost. 3179 * 3180 * Caching is used to increase performance. 3181 * A cache will be built from disk if the cache tag doesn't already exist. 3182 * The cache tag is a regular expression "dir_re", which selects a 3183 * subset of disks to search from typically something like 3184 * "dev/cXt[0-9]+d[0-9]+s[0-9]+". After the cache is built, consistency must 3185 * be maintained, so entries are added as new links are created, and removed 3186 * as old links are deleted. The whole cache is flushed if we are a daemon, 3187 * and another devfsadm process ran in between. 3188 * 3189 * Once the cache is built, this function finds the cache which matches 3190 * dir_re, and then it searches all links in that cache looking for 3191 * any link whose contents match "valid_link_contents" with a corresponding link 3192 * which does not match "valid_link". Any such matches are stale and removed. 3193 */ 3194 void 3195 devfsadm_rm_stale_links(char *dir_re, char *valid_link, di_node_t node, 3196 di_minor_t minor) 3197 { 3198 link_t *link; 3199 linkhead_t *head; 3200 char phy_path[PATH_MAX + 1]; 3201 char *valid_link_contents; 3202 char *dev_path; 3203 char rmlink[PATH_MAX + 1]; 3204 3205 /* 3206 * try to use devices path 3207 */ 3208 if ((node == lnode) && (minor == lminor)) { 3209 valid_link_contents = lphy_path; 3210 } else { 3211 if ((dev_path = di_devfs_path(node)) == NULL) { 3212 err_print(DI_DEVFS_PATH_FAILED, strerror(errno)); 3213 devfsadm_exit(1); 3214 } 3215 (void) strcpy(phy_path, dev_path); 3216 di_devfs_path_free(dev_path); 3217 3218 (void) strcat(phy_path, ":"); 3219 (void) strcat(phy_path, di_minor_name(minor)); 3220 valid_link_contents = phy_path; 3221 } 3222 3223 /* 3224 * As an optimization, check to make sure the corresponding 3225 * devlink was just created before continuing. 3226 */ 3227 3228 if (linknew == FALSE) { 3229 return; 3230 } 3231 3232 head = get_cached_links(dir_re); 3233 3234 assert(head->nextlink == NULL); 3235 3236 for (link = head->link; link != NULL; link = head->nextlink) { 3237 /* 3238 * See hot_cleanup() for why we do this 3239 */ 3240 head->nextlink = link->next; 3241 if ((strcmp(link->contents, valid_link_contents) == 0) && 3242 (strcmp(link->devlink, valid_link) != 0)) { 3243 vprint(CHATTY_MID, "removing %s -> %s\n" 3244 "valid link is: %s -> %s\n", 3245 link->devlink, link->contents, 3246 valid_link, valid_link_contents); 3247 /* 3248 * Use a copy of the cached link name as the 3249 * cache entry will go away during link removal 3250 */ 3251 (void) snprintf(rmlink, sizeof (rmlink), "%s", 3252 link->devlink); 3253 devfsadm_rm_link(rmlink); 3254 } 3255 } 3256 } 3257 3258 /* 3259 * Return previously created cache, or create cache. 3260 */ 3261 static linkhead_t * 3262 get_cached_links(char *dir_re) 3263 { 3264 recurse_dev_t rd; 3265 linkhead_t *linkhead; 3266 int n; 3267 3268 vprint(BUILDCACHE_MID, "get_cached_links: %s\n", dir_re); 3269 3270 for (linkhead = headlinkhead; linkhead != NULL; 3271 linkhead = linkhead->nexthead) { 3272 if (strcmp(linkhead->dir_re, dir_re) == 0) { 3273 return (linkhead); 3274 } 3275 } 3276 3277 /* 3278 * This tag is not in cache, so add it, along with all its 3279 * matching /dev entries. This is the only time we go to disk. 3280 */ 3281 linkhead = s_malloc(sizeof (linkhead_t)); 3282 linkhead->nexthead = headlinkhead; 3283 headlinkhead = linkhead; 3284 linkhead->dir_re = s_strdup(dir_re); 3285 3286 if ((n = regcomp(&(linkhead->dir_re_compiled), dir_re, 3287 REG_EXTENDED)) != 0) { 3288 err_print(REGCOMP_FAILED, dir_re, n); 3289 } 3290 3291 linkhead->nextlink = NULL; 3292 linkhead->link = NULL; 3293 3294 rd.fcn = build_devlink_list; 3295 rd.data = (void *)linkhead; 3296 3297 vprint(BUILDCACHE_MID, "get_cached_links: calling recurse_dev_re\n"); 3298 3299 /* call build_devlink_list for each directory in the dir_re RE */ 3300 if (dir_re[0] == '/') { 3301 recurse_dev_re("/", &dir_re[1], &rd); 3302 } else { 3303 recurse_dev_re(dev_dir, dir_re, &rd); 3304 } 3305 3306 return (linkhead); 3307 } 3308 3309 static void 3310 build_devlink_list(char *devlink, void *data) 3311 { 3312 char *fcn = "build_devlink_list: "; 3313 char *ptr; 3314 char *r_contents; 3315 char *r_devlink; 3316 char contents[PATH_MAX + 1]; 3317 char newlink[PATH_MAX + 1]; 3318 char stage_link[PATH_MAX + 1]; 3319 int linksize; 3320 linkhead_t *linkhead = (linkhead_t *)data; 3321 link_t *link; 3322 int i = 0; 3323 3324 vprint(BUILDCACHE_MID, "%scheck_link: %s\n", fcn, devlink); 3325 3326 (void) strcpy(newlink, devlink); 3327 3328 do { 3329 linksize = readlink(newlink, contents, PATH_MAX); 3330 if (linksize <= 0) { 3331 /* 3332 * The first pass through the do loop we may readlink() 3333 * non-symlink files(EINVAL) from false regexec matches. 3334 * Suppress error messages in those cases or if the link 3335 * content is the empty string. 3336 */ 3337 if (linksize < 0 && (i || errno != EINVAL)) 3338 err_print(READLINK_FAILED, "build_devlink_list", 3339 newlink, strerror(errno)); 3340 return; 3341 } 3342 contents[linksize] = '\0'; 3343 i = 1; 3344 3345 if (is_minor_node(contents, &r_contents) == DEVFSADM_FALSE) { 3346 /* 3347 * assume that link contents is really a pointer to 3348 * another link, so recurse and read its link contents. 3349 * 3350 * some link contents are absolute: 3351 * /dev/audio -> /dev/sound/0 3352 */ 3353 if (strncmp(contents, DEV "/", 3354 strlen(DEV) + strlen("/")) != 0) { 3355 3356 if ((ptr = strrchr(newlink, '/')) == NULL) { 3357 vprint(REMOVE_MID, "%s%s -> %s invalid " 3358 "link. missing '/'\n", fcn, 3359 newlink, contents); 3360 return; 3361 } 3362 *ptr = '\0'; 3363 (void) strcpy(stage_link, newlink); 3364 *ptr = '/'; 3365 (void) strcat(stage_link, "/"); 3366 (void) strcat(stage_link, contents); 3367 (void) strcpy(newlink, stage_link); 3368 } else { 3369 (void) strcpy(newlink, dev_dir); 3370 (void) strcat(newlink, "/"); 3371 (void) strcat(newlink, 3372 &contents[strlen(DEV) + strlen("/")]); 3373 } 3374 3375 } else { 3376 newlink[0] = '\0'; 3377 } 3378 } while (newlink[0] != '\0'); 3379 3380 if (strncmp(devlink, dev_dir, strlen(dev_dir)) != 0) { 3381 vprint(BUILDCACHE_MID, "%sinvalid link: %s\n", fcn, devlink); 3382 return; 3383 } 3384 3385 r_devlink = devlink + strlen(dev_dir); 3386 3387 if (r_devlink[0] != '/') 3388 return; 3389 3390 link = s_malloc(sizeof (link_t)); 3391 3392 /* don't store the '/' after rootdir/dev */ 3393 r_devlink += 1; 3394 3395 vprint(BUILDCACHE_MID, "%scaching link: %s\n", fcn, r_devlink); 3396 link->devlink = s_strdup(r_devlink); 3397 3398 link->contents = s_strdup(r_contents); 3399 3400 link->next = linkhead->link; 3401 linkhead->link = link; 3402 } 3403 3404 /* 3405 * to be consistent, devlink must not begin with / and must be 3406 * relative to /dev/, whereas physpath must contain / and be 3407 * relative to /devices. 3408 */ 3409 static void 3410 add_link_to_cache(char *devlink, char *physpath) 3411 { 3412 linkhead_t *linkhead; 3413 link_t *link; 3414 int added = 0; 3415 3416 if (file_mods == FALSE) { 3417 return; 3418 } 3419 3420 vprint(CACHE_MID, "add_link_to_cache: %s -> %s ", 3421 devlink, physpath); 3422 3423 for (linkhead = headlinkhead; linkhead != NULL; 3424 linkhead = linkhead->nexthead) { 3425 if (regexec(&(linkhead->dir_re_compiled), devlink, 0, NULL, 3426 0) == 0) { 3427 added++; 3428 link = s_malloc(sizeof (link_t)); 3429 link->devlink = s_strdup(devlink); 3430 link->contents = s_strdup(physpath); 3431 link->next = linkhead->link; 3432 linkhead->link = link; 3433 } 3434 } 3435 3436 vprint(CACHE_MID, 3437 " %d %s\n", added, added == 0 ? "NOT ADDED" : "ADDED"); 3438 } 3439 3440 /* 3441 * Remove devlink from cache. Devlink must be relative to /dev/ and not start 3442 * with /. 3443 */ 3444 static void 3445 rm_link_from_cache(char *devlink) 3446 { 3447 linkhead_t *linkhead; 3448 link_t **linkp; 3449 link_t *save; 3450 3451 vprint(CACHE_MID, "rm_link_from_cache enter: %s\n", devlink); 3452 3453 for (linkhead = headlinkhead; linkhead != NULL; 3454 linkhead = linkhead->nexthead) { 3455 if (regexec(&(linkhead->dir_re_compiled), devlink, 0, NULL, 3456 0) == 0) { 3457 3458 for (linkp = &(linkhead->link); *linkp != NULL; ) { 3459 if ((strcmp((*linkp)->devlink, devlink) == 0)) { 3460 save = *linkp; 3461 *linkp = (*linkp)->next; 3462 /* 3463 * We are removing our caller's 3464 * "next" link. Update the nextlink 3465 * field in the head so that our 3466 * callers accesses the next valid 3467 * link 3468 */ 3469 if (linkhead->nextlink == save) 3470 linkhead->nextlink = *linkp; 3471 free(save->devlink); 3472 free(save->contents); 3473 free(save); 3474 vprint(CACHE_MID, " %s FREED FROM " 3475 "CACHE\n", devlink); 3476 } else { 3477 linkp = &((*linkp)->next); 3478 } 3479 } 3480 } 3481 } 3482 } 3483 3484 static void 3485 rm_all_links_from_cache() 3486 { 3487 linkhead_t *linkhead; 3488 linkhead_t *nextlinkhead; 3489 link_t *link; 3490 link_t *nextlink; 3491 3492 vprint(CACHE_MID, "rm_all_links_from_cache\n"); 3493 3494 for (linkhead = headlinkhead; linkhead != NULL; 3495 linkhead = nextlinkhead) { 3496 3497 nextlinkhead = linkhead->nexthead; 3498 assert(linkhead->nextlink == NULL); 3499 for (link = linkhead->link; link != NULL; link = nextlink) { 3500 nextlink = link->next; 3501 free(link->devlink); 3502 free(link->contents); 3503 free(link); 3504 } 3505 regfree(&(linkhead->dir_re_compiled)); 3506 free(linkhead->dir_re); 3507 free(linkhead); 3508 } 3509 headlinkhead = NULL; 3510 } 3511 3512 /* 3513 * Called when the kernel has modified the incore path_to_inst data. This 3514 * function will schedule a flush of the data to the filesystem. 3515 */ 3516 static void 3517 devfs_instance_mod(void) 3518 { 3519 char *fcn = "devfs_instance_mod: "; 3520 vprint(PATH2INST_MID, "%senter\n", fcn); 3521 3522 /* signal instance thread */ 3523 (void) mutex_lock(&count_lock); 3524 inst_count++; 3525 (void) cond_signal(&cv); 3526 (void) mutex_unlock(&count_lock); 3527 } 3528 3529 static void 3530 instance_flush_thread(void) 3531 { 3532 int i; 3533 int idle; 3534 3535 for (;;) { 3536 3537 (void) mutex_lock(&count_lock); 3538 while (inst_count == 0) { 3539 (void) cond_wait(&cv, &count_lock); 3540 } 3541 inst_count = 0; 3542 3543 vprint(PATH2INST_MID, "signaled to flush path_to_inst." 3544 " Enter delay loop\n"); 3545 /* 3546 * Wait MAX_IDLE_DELAY seconds after getting the last flush 3547 * path_to_inst event before invoking a flush, but never wait 3548 * more than MAX_DELAY seconds after getting the first event. 3549 */ 3550 for (idle = 0, i = 0; i < MAX_DELAY; i++) { 3551 3552 (void) mutex_unlock(&count_lock); 3553 (void) sleep(1); 3554 (void) mutex_lock(&count_lock); 3555 3556 /* shorten the delay if we are idle */ 3557 if (inst_count == 0) { 3558 idle++; 3559 if (idle > MAX_IDLE_DELAY) { 3560 break; 3561 } 3562 } else { 3563 inst_count = idle = 0; 3564 } 3565 } 3566 3567 (void) mutex_unlock(&count_lock); 3568 3569 flush_path_to_inst(); 3570 } 3571 } 3572 3573 /* 3574 * Helper function for flush_path_to_inst() below; this routine calls the 3575 * inst_sync syscall to flush the path_to_inst database to the given file. 3576 */ 3577 static int 3578 do_inst_sync(char *filename) 3579 { 3580 void (*sigsaved)(int); 3581 int err = 0; 3582 3583 vprint(INSTSYNC_MID, "do_inst_sync: about to flush %s\n", filename); 3584 sigsaved = sigset(SIGSYS, SIG_IGN); 3585 if (inst_sync(filename, 0) == -1) 3586 err = errno; 3587 (void) sigset(SIGSYS, sigsaved); 3588 3589 switch (err) { 3590 case 0: 3591 return (DEVFSADM_SUCCESS); 3592 case EALREADY: /* no-op, path_to_inst already up to date */ 3593 return (EALREADY); 3594 case ENOSYS: 3595 err_print(CANT_LOAD_SYSCALL); 3596 break; 3597 case EPERM: 3598 err_print(SUPER_TO_SYNC); 3599 break; 3600 default: 3601 err_print(INSTSYNC_FAILED, filename, strerror(err)); 3602 break; 3603 } 3604 return (DEVFSADM_FAILURE); 3605 } 3606 3607 /* 3608 * Flush the kernel's path_to_inst database to /etc/path_to_inst. To do so 3609 * safely, the database is flushed to a temporary file, then moved into place. 3610 * 3611 * The following files are used during this process: 3612 * /etc/path_to_inst: The path_to_inst file 3613 * /etc/path_to_inst.<pid>: Contains data flushed from the kernel 3614 * /etc/path_to_inst.old: The backup file 3615 * /etc/path_to_inst.old.<pid>: Temp file for creating backup 3616 * 3617 */ 3618 static void 3619 flush_path_to_inst(void) 3620 { 3621 char *new_inst_file = NULL; 3622 char *old_inst_file = NULL; 3623 char *old_inst_file_npid = NULL; 3624 FILE *inst_file_fp = NULL; 3625 FILE *old_inst_file_fp = NULL; 3626 struct stat sb; 3627 int err = 0; 3628 int c; 3629 int inst_strlen; 3630 3631 vprint(PATH2INST_MID, "flush_path_to_inst: %s\n", 3632 (flush_path_to_inst_enable == TRUE) ? "ENABLED" : "DISABLED"); 3633 3634 if (flush_path_to_inst_enable == FALSE) { 3635 return; 3636 } 3637 3638 inst_strlen = strlen(inst_file); 3639 new_inst_file = s_malloc(inst_strlen + PID_STR_LEN + 2); 3640 old_inst_file = s_malloc(inst_strlen + PID_STR_LEN + 6); 3641 old_inst_file_npid = s_malloc(inst_strlen + 3642 sizeof (INSTANCE_FILE_SUFFIX)); 3643 3644 (void) snprintf(new_inst_file, inst_strlen + PID_STR_LEN + 2, 3645 "%s.%ld", inst_file, getpid()); 3646 3647 if (stat(new_inst_file, &sb) == 0) { 3648 s_unlink(new_inst_file); 3649 } 3650 3651 if ((err = do_inst_sync(new_inst_file)) != DEVFSADM_SUCCESS) { 3652 goto out; 3653 /*NOTREACHED*/ 3654 } 3655 3656 /* 3657 * Now we deal with the somewhat tricky updating and renaming 3658 * of this critical piece of kernel state. 3659 */ 3660 3661 /* 3662 * Copy the current instance file into a temporary file. 3663 * Then rename the temporary file into the backup (.old) 3664 * file and rename the newly flushed kernel data into 3665 * the instance file. 3666 * Of course if 'inst_file' doesn't exist, there's much 3667 * less for us to do .. tee hee. 3668 */ 3669 if ((inst_file_fp = fopen(inst_file, "r")) == NULL) { 3670 /* 3671 * No such file. Rename the new onto the old 3672 */ 3673 if ((err = rename(new_inst_file, inst_file)) != 0) 3674 err_print(RENAME_FAILED, inst_file, strerror(errno)); 3675 goto out; 3676 /*NOTREACHED*/ 3677 } 3678 3679 (void) snprintf(old_inst_file, inst_strlen + PID_STR_LEN + 6, 3680 "%s.old.%ld", inst_file, getpid()); 3681 3682 if (stat(old_inst_file, &sb) == 0) { 3683 s_unlink(old_inst_file); 3684 } 3685 3686 if ((old_inst_file_fp = fopen(old_inst_file, "w")) == NULL) { 3687 /* 3688 * Can't open the 'old_inst_file' file for writing. 3689 * This is somewhat strange given that the syscall 3690 * just succeeded to write a file out.. hmm.. maybe 3691 * the fs just filled up or something nasty. 3692 * 3693 * Anyway, abort what we've done so far. 3694 */ 3695 err_print(CANT_UPDATE, old_inst_file); 3696 err = DEVFSADM_FAILURE; 3697 goto out; 3698 /*NOTREACHED*/ 3699 } 3700 3701 /* 3702 * Copy current instance file into the temporary file 3703 */ 3704 err = 0; 3705 while ((c = getc(inst_file_fp)) != EOF) { 3706 if ((err = putc(c, old_inst_file_fp)) == EOF) { 3707 break; 3708 } 3709 } 3710 3711 if (fclose(old_inst_file_fp) == EOF || err == EOF) { 3712 vprint(INFO_MID, CANT_UPDATE, old_inst_file); 3713 err = DEVFSADM_FAILURE; 3714 goto out; 3715 /* NOTREACHED */ 3716 } 3717 3718 /* 3719 * Set permissions to be the same on the backup as 3720 * /etc/path_to_inst. 3721 */ 3722 (void) chmod(old_inst_file, 0444); 3723 3724 /* 3725 * So far, everything we've done is more or less reversible. 3726 * But now we're going to commit ourselves. 3727 */ 3728 3729 (void) snprintf(old_inst_file_npid, 3730 inst_strlen + sizeof (INSTANCE_FILE_SUFFIX), 3731 "%s%s", inst_file, INSTANCE_FILE_SUFFIX); 3732 3733 if ((err = rename(old_inst_file, old_inst_file_npid)) != 0) { 3734 err_print(RENAME_FAILED, old_inst_file_npid, 3735 strerror(errno)); 3736 } else if ((err = rename(new_inst_file, inst_file)) != 0) { 3737 err_print(RENAME_FAILED, inst_file, strerror(errno)); 3738 } 3739 3740 out: 3741 if (inst_file_fp != NULL) { 3742 if (fclose(inst_file_fp) == EOF) { 3743 err_print(FCLOSE_FAILED, inst_file, strerror(errno)); 3744 } 3745 } 3746 3747 if (stat(new_inst_file, &sb) == 0) { 3748 s_unlink(new_inst_file); 3749 } 3750 free(new_inst_file); 3751 3752 if (stat(old_inst_file, &sb) == 0) { 3753 s_unlink(old_inst_file); 3754 } 3755 free(old_inst_file); 3756 3757 free(old_inst_file_npid); 3758 3759 if (err != 0 && err != EALREADY) { 3760 err_print(FAILED_TO_UPDATE, inst_file); 3761 } 3762 } 3763 3764 /* 3765 * detach from tty. For daemon mode. 3766 */ 3767 void 3768 detachfromtty() 3769 { 3770 (void) setsid(); 3771 if (DEVFSADM_DEBUG_ON == TRUE) { 3772 return; 3773 } 3774 3775 (void) close(0); 3776 (void) close(1); 3777 (void) close(2); 3778 (void) open("/dev/null", O_RDWR, 0); 3779 (void) dup(0); 3780 (void) dup(0); 3781 openlog(DEVFSADMD, LOG_PID, LOG_DAEMON); 3782 (void) setlogmask(LOG_UPTO(LOG_INFO)); 3783 logflag = TRUE; 3784 } 3785 3786 /* 3787 * Use an advisory lock to synchronize updates to /dev. If the lock is 3788 * held by another process, block in the fcntl() system call until that 3789 * process drops the lock or exits. The lock file itself is 3790 * DEV_LOCK_FILE. The process id of the current and last process owning 3791 * the lock is kept in the lock file. After acquiring the lock, read the 3792 * process id and return it. It is the process ID which last owned the 3793 * lock, and will be used to determine if caches need to be flushed. 3794 * 3795 * NOTE: if the devlink database is held open by the caller, it may 3796 * be closed by this routine. This is to enforce the following lock ordering: 3797 * 1) /dev lock 2) database open 3798 */ 3799 pid_t 3800 enter_dev_lock() 3801 { 3802 struct flock lock; 3803 int n; 3804 pid_t pid; 3805 pid_t last_owner_pid; 3806 3807 if (file_mods == FALSE) { 3808 return (0); 3809 } 3810 3811 (void) snprintf(dev_lockfile, sizeof (dev_lockfile), 3812 "%s/%s", etc_dev_dir, DEV_LOCK_FILE); 3813 3814 vprint(LOCK_MID, "enter_dev_lock: lock file %s\n", dev_lockfile); 3815 3816 dev_lock_fd = open(dev_lockfile, O_CREAT|O_RDWR, 0644); 3817 if (dev_lock_fd < 0) { 3818 err_print(OPEN_FAILED, dev_lockfile, strerror(errno)); 3819 devfsadm_exit(1); 3820 } 3821 3822 lock.l_type = F_WRLCK; 3823 lock.l_whence = SEEK_SET; 3824 lock.l_start = 0; 3825 lock.l_len = 0; 3826 3827 /* try for the lock, but don't wait */ 3828 if (fcntl(dev_lock_fd, F_SETLK, &lock) == -1) { 3829 if ((errno == EACCES) || (errno == EAGAIN)) { 3830 pid = 0; 3831 n = read(dev_lock_fd, &pid, sizeof (pid_t)); 3832 vprint(LOCK_MID, "waiting for PID %d to complete\n", 3833 (int)pid); 3834 if (lseek(dev_lock_fd, 0, SEEK_SET) == (off_t)-1) { 3835 err_print(LSEEK_FAILED, dev_lockfile, 3836 strerror(errno)); 3837 devfsadm_exit(1); 3838 } 3839 /* 3840 * wait for the dev lock. If we have the database open, 3841 * close it first - the order of lock acquisition should 3842 * always be: 1) dev_lock 2) database 3843 * This is to prevent deadlocks with any locks the 3844 * database code may hold. 3845 */ 3846 (void) di_devlink_close(&devlink_cache, 0); 3847 3848 /* send any sysevents that were queued up. */ 3849 process_syseventq(); 3850 3851 if (fcntl(dev_lock_fd, F_SETLKW, &lock) == -1) { 3852 err_print(LOCK_FAILED, dev_lockfile, 3853 strerror(errno)); 3854 devfsadm_exit(1); 3855 } 3856 } 3857 } 3858 3859 hold_dev_lock = TRUE; 3860 pid = 0; 3861 n = read(dev_lock_fd, &pid, sizeof (pid_t)); 3862 if (n == sizeof (pid_t) && pid == getpid()) { 3863 return (pid); 3864 } 3865 3866 last_owner_pid = pid; 3867 3868 if (lseek(dev_lock_fd, 0, SEEK_SET) == (off_t)-1) { 3869 err_print(LSEEK_FAILED, dev_lockfile, strerror(errno)); 3870 devfsadm_exit(1); 3871 } 3872 pid = getpid(); 3873 n = write(dev_lock_fd, &pid, sizeof (pid_t)); 3874 if (n != sizeof (pid_t)) { 3875 err_print(WRITE_FAILED, dev_lockfile, strerror(errno)); 3876 devfsadm_exit(1); 3877 } 3878 3879 return (last_owner_pid); 3880 } 3881 3882 /* 3883 * Drop the advisory /dev lock, close lock file. Close and re-open the 3884 * file every time so to ensure a resync if for some reason the lock file 3885 * gets removed. 3886 */ 3887 void 3888 exit_dev_lock() 3889 { 3890 struct flock unlock; 3891 3892 if (hold_dev_lock == FALSE) { 3893 return; 3894 } 3895 3896 vprint(LOCK_MID, "exit_dev_lock: lock file %s\n", dev_lockfile); 3897 3898 unlock.l_type = F_UNLCK; 3899 unlock.l_whence = SEEK_SET; 3900 unlock.l_start = 0; 3901 unlock.l_len = 0; 3902 3903 if (fcntl(dev_lock_fd, F_SETLK, &unlock) == -1) { 3904 err_print(UNLOCK_FAILED, dev_lockfile, strerror(errno)); 3905 } 3906 3907 hold_dev_lock = FALSE; 3908 3909 if (close(dev_lock_fd) == -1) { 3910 err_print(CLOSE_FAILED, dev_lockfile, strerror(errno)); 3911 devfsadm_exit(1); 3912 } 3913 } 3914 3915 /* 3916 * 3917 * Use an advisory lock to ensure that only one daemon process is active 3918 * in the system at any point in time. If the lock is held by another 3919 * process, do not block but return the pid owner of the lock to the 3920 * caller immediately. The lock is cleared if the holding daemon process 3921 * exits for any reason even if the lock file remains, so the daemon can 3922 * be restarted if necessary. The lock file is DAEMON_LOCK_FILE. 3923 */ 3924 pid_t 3925 enter_daemon_lock(void) 3926 { 3927 struct flock lock; 3928 3929 (void) snprintf(daemon_lockfile, sizeof (daemon_lockfile), 3930 "%s/%s", etc_dev_dir, DAEMON_LOCK_FILE); 3931 3932 vprint(LOCK_MID, "enter_daemon_lock: lock file %s\n", daemon_lockfile); 3933 3934 daemon_lock_fd = open(daemon_lockfile, O_CREAT|O_RDWR, 0644); 3935 if (daemon_lock_fd < 0) { 3936 err_print(OPEN_FAILED, daemon_lockfile, strerror(errno)); 3937 devfsadm_exit(1); 3938 } 3939 3940 lock.l_type = F_WRLCK; 3941 lock.l_whence = SEEK_SET; 3942 lock.l_start = 0; 3943 lock.l_len = 0; 3944 3945 if (fcntl(daemon_lock_fd, F_SETLK, &lock) == -1) { 3946 3947 if (errno == EAGAIN || errno == EDEADLK) { 3948 if (fcntl(daemon_lock_fd, F_GETLK, &lock) == -1) { 3949 err_print(LOCK_FAILED, daemon_lockfile, 3950 strerror(errno)); 3951 devfsadm_exit(1); 3952 } 3953 return (lock.l_pid); 3954 } 3955 } 3956 hold_daemon_lock = TRUE; 3957 return (getpid()); 3958 } 3959 3960 /* 3961 * Drop the advisory daemon lock, close lock file 3962 */ 3963 void 3964 exit_daemon_lock(void) 3965 { 3966 struct flock lock; 3967 3968 if (hold_daemon_lock == FALSE) { 3969 return; 3970 } 3971 3972 vprint(LOCK_MID, "exit_daemon_lock: lock file %s\n", daemon_lockfile); 3973 3974 lock.l_type = F_UNLCK; 3975 lock.l_whence = SEEK_SET; 3976 lock.l_start = 0; 3977 lock.l_len = 0; 3978 3979 if (fcntl(daemon_lock_fd, F_SETLK, &lock) == -1) { 3980 err_print(UNLOCK_FAILED, daemon_lockfile, strerror(errno)); 3981 } 3982 3983 if (close(daemon_lock_fd) == -1) { 3984 err_print(CLOSE_FAILED, daemon_lockfile, strerror(errno)); 3985 devfsadm_exit(1); 3986 } 3987 } 3988 3989 /* 3990 * Called to removed danging nodes in two different modes: RM_PRE, RM_POST. 3991 * RM_PRE mode is called before processing the entire devinfo tree, and RM_POST 3992 * is called after processing the entire devinfo tree. 3993 */ 3994 static void 3995 pre_and_post_cleanup(int flags) 3996 { 3997 remove_list_t *rm; 3998 recurse_dev_t rd; 3999 cleanup_data_t cleanup_data; 4000 char *fcn = "pre_and_post_cleanup: "; 4001 4002 if (build_dev == FALSE) 4003 return; 4004 4005 vprint(CHATTY_MID, "attempting %s-cleanup\n", 4006 flags == RM_PRE ? "pre" : "post"); 4007 vprint(REMOVE_MID, "%sflags = %d\n", fcn, flags); 4008 4009 /* 4010 * the generic function recurse_dev_re is shared among different 4011 * functions, so set the method and data that it should use for 4012 * matches. 4013 */ 4014 rd.fcn = matching_dev; 4015 rd.data = (void *)&cleanup_data; 4016 cleanup_data.flags = flags; 4017 4018 (void) mutex_lock(&nfp_mutex); 4019 nfphash_create(); 4020 4021 for (rm = remove_head; rm != NULL; rm = rm->next) { 4022 if ((flags & rm->remove->flags) == flags) { 4023 cleanup_data.rm = rm; 4024 /* 4025 * If reached this point, RM_PRE or RM_POST cleanup is 4026 * desired. clean_ok() decides whether to clean 4027 * under the given circumstances. 4028 */ 4029 vprint(REMOVE_MID, "%scleanup: PRE or POST\n", fcn); 4030 if (clean_ok(rm->remove) == DEVFSADM_SUCCESS) { 4031 vprint(REMOVE_MID, "cleanup: cleanup OK\n"); 4032 recurse_dev_re(dev_dir, rm->remove-> 4033 dev_dirs_re, &rd); 4034 } 4035 } 4036 } 4037 nfphash_destroy(); 4038 (void) mutex_unlock(&nfp_mutex); 4039 } 4040 4041 /* 4042 * clean_ok() determines whether cleanup should be done according 4043 * to the following matrix: 4044 * 4045 * command line arguments RM_PRE RM_POST RM_PRE && RM_POST && 4046 * RM_ALWAYS RM_ALWAYS 4047 * ---------------------- ------ ----- --------- ---------- 4048 * 4049 * <neither -c nor -C> - - pre-clean post-clean 4050 * 4051 * -C pre-clean post-clean pre-clean post-clean 4052 * 4053 * -C -c class pre-clean post-clean pre-clean post-clean 4054 * if class if class if class if class 4055 * matches matches matches matches 4056 * 4057 * -c class - - pre-clean post-clean 4058 * if class if class 4059 * matches matches 4060 * 4061 */ 4062 static int 4063 clean_ok(devfsadm_remove_V1_t *remove) 4064 { 4065 int i; 4066 4067 if (single_drv == TRUE) { 4068 /* no cleanup at all when using -i option */ 4069 return (DEVFSADM_FAILURE); 4070 } 4071 4072 /* 4073 * no cleanup if drivers are not loaded. We make an exception 4074 * for the "disks" program however, since disks has a public 4075 * cleanup flag (-C) and disk drivers are usually never 4076 * unloaded. 4077 */ 4078 if (load_attach_drv == FALSE && strcmp(prog, DISKS) != 0) { 4079 return (DEVFSADM_FAILURE); 4080 } 4081 4082 /* if the cleanup flag was not specified, return false */ 4083 if ((cleanup == FALSE) && ((remove->flags & RM_ALWAYS) == 0)) { 4084 return (DEVFSADM_FAILURE); 4085 } 4086 4087 if (num_classes == 0) { 4088 return (DEVFSADM_SUCCESS); 4089 } 4090 4091 /* 4092 * if reached this point, check to see if the class in the given 4093 * remove structure matches a class given on the command line 4094 */ 4095 4096 for (i = 0; i < num_classes; i++) { 4097 if (strcmp(remove->device_class, classes[i]) == 0) { 4098 return (DEVFSADM_SUCCESS); 4099 } 4100 } 4101 4102 return (DEVFSADM_FAILURE); 4103 } 4104 4105 /* 4106 * Called to remove dangling nodes after receiving a hotplug event 4107 * containing the physical node pathname to be removed. 4108 */ 4109 void 4110 hot_cleanup(char *node_path, char *minor_name, char *ev_subclass, 4111 char *driver_name, int instance) 4112 { 4113 link_t *link; 4114 linkhead_t *head; 4115 remove_list_t *rm; 4116 char *fcn = "hot_cleanup: "; 4117 char path[PATH_MAX + 1]; 4118 int path_len; 4119 char rmlink[PATH_MAX + 1]; 4120 nvlist_t *nvl = NULL; 4121 int skip; 4122 int ret; 4123 4124 /* 4125 * dev links can go away as part of hot cleanup. 4126 * So first build event attributes in order capture dev links. 4127 */ 4128 if (ev_subclass != NULL) 4129 nvl = build_event_attributes(EC_DEV_REMOVE, ev_subclass, 4130 node_path, DI_NODE_NIL, driver_name, instance); 4131 4132 (void) strcpy(path, node_path); 4133 (void) strcat(path, ":"); 4134 (void) strcat(path, minor_name == NULL ? "" : minor_name); 4135 4136 path_len = strlen(path); 4137 4138 vprint(REMOVE_MID, "%spath=%s\n", fcn, path); 4139 4140 (void) mutex_lock(&nfp_mutex); 4141 nfphash_create(); 4142 4143 for (rm = remove_head; rm != NULL; rm = rm->next) { 4144 if ((RM_HOT & rm->remove->flags) == RM_HOT) { 4145 head = get_cached_links(rm->remove->dev_dirs_re); 4146 assert(head->nextlink == NULL); 4147 for (link = head->link; 4148 link != NULL; link = head->nextlink) { 4149 /* 4150 * The remove callback below may remove 4151 * the current and/or any or all of the 4152 * subsequent links in the list. 4153 * Save the next link in the head. If 4154 * the callback removes the next link 4155 * the saved pointer in the head will be 4156 * updated by the callback to point at 4157 * the next valid link. 4158 */ 4159 head->nextlink = link->next; 4160 4161 /* 4162 * if devlink is in no-further-process hash, 4163 * skip its remove 4164 */ 4165 if (nfphash_lookup(link->devlink) != NULL) 4166 continue; 4167 4168 if (minor_name) 4169 skip = strcmp(link->contents, path); 4170 else 4171 skip = strncmp(link->contents, path, 4172 path_len); 4173 if (skip || 4174 (call_minor_init(rm->modptr) == 4175 DEVFSADM_FAILURE)) 4176 continue; 4177 4178 vprint(REMOVE_MID, 4179 "%sremoving %s -> %s\n", fcn, 4180 link->devlink, link->contents); 4181 /* 4182 * Use a copy of the cached link name 4183 * as the cache entry will go away 4184 * during link removal 4185 */ 4186 (void) snprintf(rmlink, sizeof (rmlink), 4187 "%s", link->devlink); 4188 if (rm->remove->flags & RM_NOINTERPOSE) { 4189 ((void (*)(char *)) 4190 (rm->remove->callback_fcn))(rmlink); 4191 } else { 4192 ret = ((int (*)(char *)) 4193 (rm->remove->callback_fcn))(rmlink); 4194 if (ret == DEVFSADM_TERMINATE) 4195 nfphash_insert(rmlink); 4196 } 4197 } 4198 } 4199 } 4200 4201 nfphash_destroy(); 4202 (void) mutex_unlock(&nfp_mutex); 4203 4204 /* update device allocation database */ 4205 if (system_labeled) { 4206 int ret = 0; 4207 int devtype = 0; 4208 char devname[MAXNAMELEN]; 4209 4210 devname[0] = '\0'; 4211 if (strstr(node_path, DA_SOUND_NAME)) 4212 devtype = DA_AUDIO; 4213 else if (strstr(node_path, "disk")) 4214 devtype = DA_RMDISK; 4215 else 4216 goto out; 4217 ret = da_remove_list(&devlist, NULL, devtype, devname, 4218 sizeof (devname)); 4219 if (ret != -1) 4220 (void) _update_devalloc_db(&devlist, devtype, DA_REMOVE, 4221 devname, root_dir); 4222 } 4223 4224 out: 4225 /* now log an event */ 4226 if (nvl) { 4227 log_event(EC_DEV_REMOVE, ev_subclass, nvl); 4228 free(nvl); 4229 } 4230 } 4231 4232 /* 4233 * Open the dir current_dir. For every file which matches the first dir 4234 * component of path_re, recurse. If there are no more *dir* path 4235 * components left in path_re (ie no more /), then call function rd->fcn. 4236 */ 4237 static void 4238 recurse_dev_re(char *current_dir, char *path_re, recurse_dev_t *rd) 4239 { 4240 regex_t re1; 4241 char *slash; 4242 char new_path[PATH_MAX + 1]; 4243 char *anchored_path_re; 4244 size_t len; 4245 finddevhdl_t fhandle; 4246 const char *fp; 4247 4248 vprint(RECURSEDEV_MID, "recurse_dev_re: curr = %s path=%s\n", 4249 current_dir, path_re); 4250 4251 if (finddev_readdir(current_dir, &fhandle) != 0) 4252 return; 4253 4254 len = strlen(path_re); 4255 if ((slash = strchr(path_re, '/')) != NULL) { 4256 len = (slash - path_re); 4257 } 4258 4259 anchored_path_re = s_malloc(len + 3); 4260 (void) sprintf(anchored_path_re, "^%.*s$", len, path_re); 4261 4262 if (regcomp(&re1, anchored_path_re, REG_EXTENDED) != 0) { 4263 free(anchored_path_re); 4264 goto out; 4265 } 4266 4267 free(anchored_path_re); 4268 4269 while ((fp = finddev_next(fhandle)) != NULL) { 4270 4271 if (regexec(&re1, fp, 0, NULL, 0) == 0) { 4272 /* match */ 4273 (void) strcpy(new_path, current_dir); 4274 (void) strcat(new_path, "/"); 4275 (void) strcat(new_path, fp); 4276 4277 vprint(RECURSEDEV_MID, "recurse_dev_re: match, new " 4278 "path = %s\n", new_path); 4279 4280 if (slash != NULL) { 4281 recurse_dev_re(new_path, slash + 1, rd); 4282 } else { 4283 /* reached the leaf component of path_re */ 4284 vprint(RECURSEDEV_MID, 4285 "recurse_dev_re: calling fcn\n"); 4286 (*(rd->fcn))(new_path, rd->data); 4287 } 4288 } 4289 } 4290 4291 regfree(&re1); 4292 4293 out: 4294 finddev_close(fhandle); 4295 } 4296 4297 /* 4298 * Found a devpath which matches a RE in the remove structure. 4299 * Now check to see if it is dangling. 4300 */ 4301 static void 4302 matching_dev(char *devpath, void *data) 4303 { 4304 cleanup_data_t *cleanup_data = data; 4305 int norm_len = strlen(dev_dir) + strlen("/"); 4306 int ret; 4307 char *fcn = "matching_dev: "; 4308 4309 vprint(RECURSEDEV_MID, "%sexamining devpath = '%s'\n", fcn, 4310 devpath); 4311 4312 /* 4313 * If the link is in the no-further-process hash 4314 * don't do any remove operation on it. 4315 */ 4316 if (nfphash_lookup(devpath + norm_len) != NULL) 4317 return; 4318 4319 if (resolve_link(devpath, NULL, NULL, NULL, 1) == TRUE) { 4320 if (call_minor_init(cleanup_data->rm->modptr) == 4321 DEVFSADM_FAILURE) { 4322 return; 4323 } 4324 4325 devpath += norm_len; 4326 4327 vprint(RECURSEDEV_MID, "%scalling" 4328 " callback %s\n", fcn, devpath); 4329 if (cleanup_data->rm->remove->flags & RM_NOINTERPOSE) 4330 ((void (*)(char *)) 4331 (cleanup_data->rm->remove->callback_fcn))(devpath); 4332 else { 4333 ret = ((int (*)(char *)) 4334 (cleanup_data->rm->remove->callback_fcn))(devpath); 4335 if (ret == DEVFSADM_TERMINATE) { 4336 /* 4337 * We want no further remove processing for 4338 * this link. Add it to the nfp_hash; 4339 */ 4340 nfphash_insert(devpath); 4341 } 4342 } 4343 } 4344 } 4345 4346 int 4347 devfsadm_read_link(char *link, char **devfs_path) 4348 { 4349 char devlink[PATH_MAX]; 4350 4351 *devfs_path = NULL; 4352 4353 /* prepend link with dev_dir contents */ 4354 (void) strcpy(devlink, dev_dir); 4355 (void) strcat(devlink, "/"); 4356 (void) strcat(devlink, link); 4357 4358 /* We *don't* want a stat of the /devices node */ 4359 (void) resolve_link(devlink, NULL, NULL, devfs_path, 0); 4360 4361 return (*devfs_path ? DEVFSADM_SUCCESS : DEVFSADM_FAILURE); 4362 } 4363 4364 int 4365 devfsadm_link_valid(char *link) 4366 { 4367 struct stat sb; 4368 char devlink[PATH_MAX + 1], *contents = NULL; 4369 int rv, type; 4370 int instance = 0; 4371 4372 /* prepend link with dev_dir contents */ 4373 (void) strcpy(devlink, dev_dir); 4374 (void) strcat(devlink, "/"); 4375 (void) strcat(devlink, link); 4376 4377 if (!device_exists(devlink) || lstat(devlink, &sb) != 0) { 4378 return (DEVFSADM_FALSE); 4379 } 4380 4381 contents = NULL; 4382 type = 0; 4383 if (resolve_link(devlink, &contents, &type, NULL, 1) == TRUE) { 4384 rv = DEVFSADM_FALSE; 4385 } else { 4386 rv = DEVFSADM_TRUE; 4387 } 4388 4389 /* 4390 * The link exists. Add it to the database 4391 */ 4392 (void) di_devlink_add_link(devlink_cache, link, contents, type); 4393 if (system_labeled && (rv == DEVFSADM_TRUE) && 4394 strstr(devlink, DA_AUDIO_NAME) && contents) { 4395 (void) sscanf(contents, "%*[a-z]%d", &instance); 4396 (void) da_add_list(&devlist, devlink, instance, 4397 DA_ADD|DA_AUDIO); 4398 _update_devalloc_db(&devlist, 0, DA_ADD, NULL, root_dir); 4399 } 4400 free(contents); 4401 4402 return (rv); 4403 } 4404 4405 /* 4406 * devpath: Absolute path to /dev link 4407 * content_p: Returns malloced string (link content) 4408 * type_p: Returns link type: primary or secondary 4409 * devfs_path: Returns malloced string: /devices path w/out "/devices" 4410 * dangle: if set, check if link is dangling 4411 * Returns: 4412 * TRUE if dangling 4413 * FALSE if not or if caller doesn't care 4414 * Caller is assumed to have initialized pointer contents to NULL 4415 */ 4416 static int 4417 resolve_link(char *devpath, char **content_p, int *type_p, char **devfs_path, 4418 int dangle) 4419 { 4420 char contents[PATH_MAX + 1]; 4421 char stage_link[PATH_MAX + 1]; 4422 char *fcn = "resolve_link: "; 4423 char *ptr; 4424 int linksize; 4425 int rv = TRUE; 4426 struct stat sb; 4427 4428 linksize = readlink(devpath, contents, PATH_MAX); 4429 4430 if (linksize <= 0) { 4431 return (FALSE); 4432 } else { 4433 contents[linksize] = '\0'; 4434 } 4435 vprint(REMOVE_MID, "%s %s -> %s\n", fcn, devpath, contents); 4436 4437 if (content_p) { 4438 *content_p = s_strdup(contents); 4439 } 4440 4441 /* 4442 * Check to see if this is a link pointing to another link in /dev. The 4443 * cheap way to do this is to look for a lack of ../devices/. 4444 */ 4445 4446 if (is_minor_node(contents, &ptr) == DEVFSADM_FALSE) { 4447 4448 if (type_p) { 4449 *type_p = DI_SECONDARY_LINK; 4450 } 4451 4452 /* 4453 * assume that linkcontents is really a pointer to another 4454 * link, and if so recurse and read its link contents. 4455 */ 4456 if (strncmp(contents, DEV "/", strlen(DEV) + 1) == 0) { 4457 (void) strcpy(stage_link, dev_dir); 4458 (void) strcat(stage_link, "/"); 4459 (void) strcpy(stage_link, 4460 &contents[strlen(DEV) + strlen("/")]); 4461 } else { 4462 if ((ptr = strrchr(devpath, '/')) == NULL) { 4463 vprint(REMOVE_MID, "%s%s -> %s invalid link. " 4464 "missing '/'\n", fcn, devpath, 4465 contents); 4466 return (TRUE); 4467 } 4468 *ptr = '\0'; 4469 (void) strcpy(stage_link, devpath); 4470 *ptr = '/'; 4471 (void) strcat(stage_link, "/"); 4472 (void) strcat(stage_link, contents); 4473 } 4474 return (resolve_link(stage_link, NULL, NULL, devfs_path, 4475 dangle)); 4476 } 4477 4478 /* Current link points at a /devices minor node */ 4479 if (type_p) { 4480 *type_p = DI_PRIMARY_LINK; 4481 } 4482 4483 if (devfs_path) 4484 *devfs_path = s_strdup(ptr); 4485 4486 rv = FALSE; 4487 if (dangle) 4488 rv = (stat(ptr - strlen(DEVICES), &sb) == -1); 4489 4490 vprint(REMOVE_MID, "%slink=%s, returning %s\n", fcn, 4491 devpath, ((rv == TRUE) ? "TRUE" : "FALSE")); 4492 4493 return (rv); 4494 } 4495 4496 /* 4497 * Returns the substring of interest, given a path. 4498 */ 4499 static char * 4500 alloc_cmp_str(const char *path, devfsadm_enumerate_t *dep) 4501 { 4502 uint_t match; 4503 char *np, *ap, *mp; 4504 char *cmp_str = NULL; 4505 char at[] = "@"; 4506 char *fcn = "alloc_cmp_str"; 4507 4508 np = ap = mp = NULL; 4509 4510 /* 4511 * extract match flags from the flags argument. 4512 */ 4513 match = (dep->flags & MATCH_MASK); 4514 4515 vprint(ENUM_MID, "%s: enumeration match type: 0x%x" 4516 " path: %s\n", fcn, match, path); 4517 4518 /* 4519 * MATCH_CALLBACK and MATCH_ALL are the only flags 4520 * which may be used if "path" is a /dev path 4521 */ 4522 if (match == MATCH_CALLBACK) { 4523 if (dep->sel_fcn == NULL) { 4524 vprint(ENUM_MID, "%s: invalid enumerate" 4525 " callback: path: %s\n", fcn, path); 4526 return (NULL); 4527 } 4528 cmp_str = dep->sel_fcn(path, dep->cb_arg); 4529 return (cmp_str); 4530 } 4531 4532 cmp_str = s_strdup(path); 4533 4534 if (match == MATCH_ALL) { 4535 return (cmp_str); 4536 } 4537 4538 /* 4539 * The remaining flags make sense only for /devices 4540 * paths 4541 */ 4542 if ((mp = strrchr(cmp_str, ':')) == NULL) { 4543 vprint(ENUM_MID, "%s: invalid path: %s\n", 4544 fcn, path); 4545 goto err; 4546 } 4547 4548 if (match == MATCH_MINOR) { 4549 /* A NULL "match_arg" values implies entire minor */ 4550 if (get_component(mp + 1, dep->match_arg) == NULL) { 4551 vprint(ENUM_MID, "%s: invalid minor component:" 4552 " path: %s\n", fcn, path); 4553 goto err; 4554 } 4555 return (cmp_str); 4556 } 4557 4558 if ((np = strrchr(cmp_str, '/')) == NULL) { 4559 vprint(ENUM_MID, "%s: invalid path: %s\n", fcn, path); 4560 goto err; 4561 } 4562 4563 if (match == MATCH_PARENT) { 4564 if (strcmp(cmp_str, "/") == 0) { 4565 vprint(ENUM_MID, "%s: invalid path: %s\n", 4566 fcn, path); 4567 goto err; 4568 } 4569 4570 if (np == cmp_str) { 4571 *(np + 1) = '\0'; 4572 } else { 4573 *np = '\0'; 4574 } 4575 return (cmp_str); 4576 } 4577 4578 /* ap can be NULL - Leaf address may not exist or be empty string */ 4579 ap = strchr(np+1, '@'); 4580 4581 /* minor is no longer of interest */ 4582 *mp = '\0'; 4583 4584 if (match == MATCH_NODE) { 4585 if (ap) 4586 *ap = '\0'; 4587 return (cmp_str); 4588 } else if (match == MATCH_ADDR) { 4589 /* 4590 * The empty string is a valid address. The only MATCH_ADDR 4591 * allowed in this case is against the whole address or 4592 * the first component of the address (match_arg=NULL/"0"/"1") 4593 * Note that in this case, the path won't have an "@" 4594 * As a result ap will be NULL. We fake up an ap = @'\0' 4595 * so that get_component() will work correctly. 4596 */ 4597 if (ap == NULL) { 4598 ap = at; 4599 } 4600 4601 if (get_component(ap + 1, dep->match_arg) == NULL) { 4602 vprint(ENUM_MID, "%s: invalid leaf addr. component:" 4603 " path: %s\n", fcn, path); 4604 goto err; 4605 } 4606 return (cmp_str); 4607 } 4608 4609 vprint(ENUM_MID, "%s: invalid enumeration flags: 0x%x" 4610 " path: %s\n", fcn, dep->flags, path); 4611 4612 /*FALLTHRU*/ 4613 err: 4614 free(cmp_str); 4615 return (NULL); 4616 } 4617 4618 4619 /* 4620 * "str" is expected to be a string with components separated by ',' 4621 * The terminating null char is considered a separator. 4622 * get_component() will remove the portion of the string beyond 4623 * the component indicated. 4624 * If comp_str is NULL, the entire "str" is returned. 4625 */ 4626 static char * 4627 get_component(char *str, const char *comp_str) 4628 { 4629 long comp; 4630 char *cp; 4631 4632 if (str == NULL) { 4633 return (NULL); 4634 } 4635 4636 if (comp_str == NULL) { 4637 return (str); 4638 } 4639 4640 errno = 0; 4641 comp = strtol(comp_str, &cp, 10); 4642 if (errno != 0 || *cp != '\0' || comp < 0) { 4643 return (NULL); 4644 } 4645 4646 if (comp == 0) 4647 return (str); 4648 4649 for (cp = str; ; cp++) { 4650 if (*cp == ',' || *cp == '\0') 4651 comp--; 4652 if (*cp == '\0' || comp <= 0) { 4653 break; 4654 } 4655 } 4656 4657 if (comp == 0) { 4658 *cp = '\0'; 4659 } else { 4660 str = NULL; 4661 } 4662 4663 return (str); 4664 } 4665 4666 4667 /* 4668 * Enumerate serves as a generic counter as well as a means to determine 4669 * logical unit/controller numbers for such items as disk and tape 4670 * drives. 4671 * 4672 * rules[] is an array of devfsadm_enumerate_t structures which defines 4673 * the enumeration rules to be used for a specified set of links in /dev. 4674 * The set of links is specified through regular expressions (of the flavor 4675 * described in regex(5)). These regular expressions are used to determine 4676 * the set of links in /dev to examine. The last path component in these 4677 * regular expressions MUST contain a parenthesized subexpression surrounding 4678 * the RE which is to be considered the enumerating component. The subexp 4679 * member in a rule is the subexpression number of the enumerating 4680 * component. Subexpressions in the last path component are numbered starting 4681 * from 1. 4682 * 4683 * A cache of current id assignments is built up from existing symlinks and 4684 * new assignments use the lowest unused id. Assignments are based on a 4685 * match of a specified substring of a symlink's contents. If the specified 4686 * component for the devfs_path argument matches the corresponding substring 4687 * for a existing symlink's contents, the cached id is returned. Else, a new 4688 * id is created and returned in *buf. *buf must be freed by the caller. 4689 * 4690 * An id assignment may be governed by a combination of rules, each rule 4691 * applicable to a different subset of links in /dev. For example, controller 4692 * numbers may be determined by a combination of disk symlinks in /dev/[r]dsk 4693 * and controller symlinks in /dev/cfg, with the two sets requiring different 4694 * rules to derive the "substring of interest". In such cases, the rules 4695 * array will have more than one element. 4696 */ 4697 int 4698 devfsadm_enumerate_int(char *devfs_path, int index, char **buf, 4699 devfsadm_enumerate_t rules[], int nrules) 4700 { 4701 return (find_enum_id(rules, nrules, 4702 devfs_path, index, "0", INTEGER, buf, 0)); 4703 } 4704 4705 int 4706 disk_enumerate_int(char *devfs_path, int index, char **buf, 4707 devfsadm_enumerate_t rules[], int nrules) 4708 { 4709 return (find_enum_id(rules, nrules, 4710 devfs_path, index, "0", INTEGER, buf, 1)); 4711 } 4712 4713 /* 4714 * Same as above, but allows a starting value to be specified. 4715 * Private to devfsadm.... used by devlinks. 4716 */ 4717 static int 4718 devfsadm_enumerate_int_start(char *devfs_path, int index, char **buf, 4719 devfsadm_enumerate_t rules[], int nrules, char *start) 4720 { 4721 return (find_enum_id(rules, nrules, 4722 devfs_path, index, start, INTEGER, buf, 0)); 4723 } 4724 4725 /* 4726 * devfsadm_enumerate_char serves as a generic counter returning 4727 * a single letter. 4728 */ 4729 int 4730 devfsadm_enumerate_char(char *devfs_path, int index, char **buf, 4731 devfsadm_enumerate_t rules[], int nrules) 4732 { 4733 return (find_enum_id(rules, nrules, 4734 devfs_path, index, "a", LETTER, buf, 0)); 4735 } 4736 4737 /* 4738 * Same as above, but allows a starting char to be specified. 4739 * Private to devfsadm - used by ports module (port_link.c) 4740 */ 4741 int 4742 devfsadm_enumerate_char_start(char *devfs_path, int index, char **buf, 4743 devfsadm_enumerate_t rules[], int nrules, char *start) 4744 { 4745 return (find_enum_id(rules, nrules, 4746 devfs_path, index, start, LETTER, buf, 0)); 4747 } 4748 4749 4750 /* 4751 * For a given numeral_set (see get_cached_set for desc of numeral_set), 4752 * search all cached entries looking for matches on a specified substring 4753 * of devfs_path. The substring is derived from devfs_path based on the 4754 * rule specified by "index". If a match is found on a cached entry, 4755 * return the enumerated id in buf. Otherwise, create a new id by calling 4756 * new_id, then cache and return that entry. 4757 */ 4758 static int 4759 find_enum_id(devfsadm_enumerate_t rules[], int nrules, 4760 char *devfs_path, int index, char *min, int type, char **buf, 4761 int multiple) 4762 { 4763 numeral_t *matchnp; 4764 numeral_t *numeral; 4765 int matchcount = 0; 4766 char *cmp_str; 4767 char *fcn = "find_enum_id"; 4768 numeral_set_t *set; 4769 4770 if (rules == NULL) { 4771 vprint(ENUM_MID, "%s: no rules. path: %s\n", 4772 fcn, devfs_path ? devfs_path : "<NULL path>"); 4773 return (DEVFSADM_FAILURE); 4774 } 4775 4776 if (devfs_path == NULL) { 4777 vprint(ENUM_MID, "%s: NULL path\n", fcn); 4778 return (DEVFSADM_FAILURE); 4779 } 4780 4781 if (nrules <= 0 || index < 0 || index >= nrules || buf == NULL) { 4782 vprint(ENUM_MID, "%s: invalid arguments. path: %s\n", 4783 fcn, devfs_path); 4784 return (DEVFSADM_FAILURE); 4785 } 4786 4787 *buf = NULL; 4788 4789 4790 cmp_str = alloc_cmp_str(devfs_path, &rules[index]); 4791 if (cmp_str == NULL) { 4792 return (DEVFSADM_FAILURE); 4793 } 4794 4795 if ((set = get_enum_cache(rules, nrules)) == NULL) { 4796 free(cmp_str); 4797 return (DEVFSADM_FAILURE); 4798 } 4799 4800 assert(nrules == set->re_count); 4801 4802 /* 4803 * Check and see if a matching entry is already cached. 4804 */ 4805 matchcount = lookup_enum_cache(set, cmp_str, rules, index, 4806 &matchnp); 4807 4808 if (matchcount < 0 || matchcount > 1) { 4809 free(cmp_str); 4810 if (multiple && matchcount > 1) 4811 return (DEVFSADM_MULTIPLE); 4812 else 4813 return (DEVFSADM_FAILURE); 4814 } 4815 4816 /* if matching entry already cached, return it */ 4817 if (matchcount == 1) { 4818 *buf = s_strdup(matchnp->id); 4819 free(cmp_str); 4820 return (DEVFSADM_SUCCESS); 4821 } 4822 4823 /* 4824 * no cached entry, initialize a numeral struct 4825 * by calling new_id() and cache onto the numeral_set 4826 */ 4827 numeral = s_malloc(sizeof (numeral_t)); 4828 numeral->id = new_id(set->headnumeral, type, min); 4829 numeral->full_path = s_strdup(devfs_path); 4830 numeral->rule_index = index; 4831 numeral->cmp_str = cmp_str; 4832 cmp_str = NULL; 4833 4834 /* insert to head of list for fast lookups */ 4835 numeral->next = set->headnumeral; 4836 set->headnumeral = numeral; 4837 4838 *buf = s_strdup(numeral->id); 4839 return (DEVFSADM_SUCCESS); 4840 } 4841 4842 4843 /* 4844 * Looks up the specified cache for a match with a specified string 4845 * Returns: 4846 * -1 : on error. 4847 * 0/1/2 : Number of matches. 4848 * Returns the matching element only if there is a single match. 4849 * If the "uncached" flag is set, derives the "cmp_str" afresh 4850 * for the match instead of using cached values. 4851 */ 4852 static int 4853 lookup_enum_cache(numeral_set_t *set, char *cmp_str, 4854 devfsadm_enumerate_t rules[], int index, numeral_t **matchnpp) 4855 { 4856 int matchcount = 0, rv = -1; 4857 int uncached; 4858 numeral_t *np; 4859 char *fcn = "lookup_enum_cache"; 4860 char *cp; 4861 4862 *matchnpp = NULL; 4863 4864 assert(index < set->re_count); 4865 4866 if (cmp_str == NULL) { 4867 return (-1); 4868 } 4869 4870 uncached = 0; 4871 if ((rules[index].flags & MATCH_UNCACHED) == MATCH_UNCACHED) { 4872 uncached = 1; 4873 } 4874 4875 /* 4876 * Check and see if a matching entry is already cached. 4877 */ 4878 for (np = set->headnumeral; np != NULL; np = np->next) { 4879 if (np->cmp_str == NULL) { 4880 vprint(ENUM_MID, "%s: invalid entry in enumerate" 4881 " cache. path: %s\n", fcn, np->full_path); 4882 return (-1); 4883 } 4884 4885 if (uncached) { 4886 vprint(CHATTY_MID, "%s: bypassing enumerate cache." 4887 " path: %s\n", fcn, cmp_str); 4888 cp = alloc_cmp_str(np->full_path, 4889 &rules[np->rule_index]); 4890 if (cp == NULL) 4891 return (-1); 4892 rv = strcmp(cmp_str, cp); 4893 free(cp); 4894 } else { 4895 rv = strcmp(cmp_str, np->cmp_str); 4896 } 4897 4898 if (rv == 0) { 4899 if (matchcount++ != 0) { 4900 break; /* more than 1 match. */ 4901 } 4902 *matchnpp = np; 4903 } 4904 } 4905 4906 return (matchcount); 4907 } 4908 4909 #ifdef DEBUG 4910 static void 4911 dump_enum_cache(numeral_set_t *setp) 4912 { 4913 int i; 4914 numeral_t *np; 4915 char *fcn = "dump_enum_cache"; 4916 4917 vprint(ENUM_MID, "%s: re_count = %d\n", fcn, setp->re_count); 4918 for (i = 0; i < setp->re_count; i++) { 4919 vprint(ENUM_MID, "%s: re[%d] = %s\n", fcn, i, setp->re[i]); 4920 } 4921 4922 for (np = setp->headnumeral; np != NULL; np = np->next) { 4923 vprint(ENUM_MID, "%s: id: %s\n", fcn, np->id); 4924 vprint(ENUM_MID, "%s: full_path: %s\n", fcn, np->full_path); 4925 vprint(ENUM_MID, "%s: rule_index: %d\n", fcn, np->rule_index); 4926 vprint(ENUM_MID, "%s: cmp_str: %s\n", fcn, np->cmp_str); 4927 } 4928 } 4929 #endif 4930 4931 /* 4932 * For a given set of regular expressions in rules[], this function returns 4933 * either a previously cached struct numeral_set or it will create and 4934 * cache a new struct numeral_set. There is only one struct numeral_set 4935 * for the combination of REs present in rules[]. Each numeral_set contains 4936 * the regular expressions in rules[] used for cache selection AND a linked 4937 * list of struct numerals, ONE FOR EACH *UNIQUE* numeral or character ID 4938 * selected by the grouping parenthesized subexpression found in the last 4939 * path component of each rules[].re. For example, the RE: "rmt/([0-9]+)" 4940 * selects all the logical nodes of the correct form in dev/rmt/. 4941 * Each rmt/X will store a *single* struct numeral... ie 0, 1, 2 each get a 4942 * single struct numeral. There is no need to store more than a single logical 4943 * node matching X since the information desired in the devfspath would be 4944 * identical for the portion of the devfspath of interest. (the part up to, 4945 * but not including the minor name in this example.) 4946 * 4947 * If the given numeral_set is not yet cached, call enumerate_recurse to 4948 * create it. 4949 */ 4950 static numeral_set_t * 4951 get_enum_cache(devfsadm_enumerate_t rules[], int nrules) 4952 { 4953 /* linked list of numeral sets */ 4954 numeral_set_t *setp; 4955 int i; 4956 char *path_left; 4957 char *fcn = "get_enum_cache"; 4958 4959 /* 4960 * See if we've already cached this numeral set. 4961 */ 4962 for (setp = head_numeral_set; setp != NULL; setp = setp->next) { 4963 /* 4964 * check all regexp's passed in function against 4965 * those in cached set. 4966 */ 4967 if (nrules != setp->re_count) { 4968 continue; 4969 } 4970 4971 for (i = 0; i < nrules; i++) { 4972 if (strcmp(setp->re[i], rules[i].re) != 0) { 4973 break; 4974 } 4975 } 4976 4977 if (i == nrules) { 4978 return (setp); 4979 } 4980 } 4981 4982 /* 4983 * If the MATCH_UNCACHED flag is set, we should not be here. 4984 */ 4985 for (i = 0; i < nrules; i++) { 4986 if ((rules[i].flags & MATCH_UNCACHED) == MATCH_UNCACHED) { 4987 vprint(ENUM_MID, "%s: invalid enumeration flags: " 4988 "0x%x\n", fcn, rules[i].flags); 4989 return (NULL); 4990 } 4991 } 4992 4993 /* 4994 * Since we made it here, we have not yet cached the given set of 4995 * logical nodes matching the passed re. Create a cached entry 4996 * struct numeral_set and populate it with a minimal set of 4997 * logical nodes from /dev. 4998 */ 4999 5000 setp = s_malloc(sizeof (numeral_set_t)); 5001 setp->re = s_malloc(sizeof (char *) * nrules); 5002 for (i = 0; i < nrules; i++) { 5003 setp->re[i] = s_strdup(rules[i].re); 5004 } 5005 setp->re_count = nrules; 5006 setp->headnumeral = NULL; 5007 5008 /* put this new cached set on the cached set list */ 5009 setp->next = head_numeral_set; 5010 head_numeral_set = setp; 5011 5012 /* 5013 * For each RE, search disk and cache any matches on the 5014 * numeral list. 5015 */ 5016 for (i = 0; i < nrules; i++) { 5017 path_left = s_strdup(setp->re[i]); 5018 enumerate_recurse(dev_dir, path_left, setp, rules, i); 5019 free(path_left); 5020 } 5021 5022 #ifdef DEBUG 5023 dump_enum_cache(setp); 5024 #endif 5025 5026 return (setp); 5027 } 5028 5029 5030 /* 5031 * This function stats the pathname namebuf. If this is a directory 5032 * entry, we recurse down dname/fname until we find the first symbolic 5033 * link, and then stat and return it. This is valid for the same reason 5034 * that we only need to read a single pathname for multiple matching 5035 * logical ID's... ie, all the logical nodes should contain identical 5036 * physical paths for the parts we are interested. 5037 */ 5038 int 5039 get_stat_info(char *namebuf, struct stat *sb) 5040 { 5041 char *cp; 5042 finddevhdl_t fhandle; 5043 const char *fp; 5044 5045 if (lstat(namebuf, sb) < 0) { 5046 (void) err_print(LSTAT_FAILED, namebuf, strerror(errno)); 5047 return (DEVFSADM_FAILURE); 5048 } 5049 5050 if ((sb->st_mode & S_IFMT) == S_IFLNK) { 5051 return (DEVFSADM_SUCCESS); 5052 } 5053 5054 /* 5055 * If it is a dir, recurse down until we find a link and 5056 * then use the link. 5057 */ 5058 if ((sb->st_mode & S_IFMT) == S_IFDIR) { 5059 5060 if (finddev_readdir(namebuf, &fhandle) != 0) { 5061 return (DEVFSADM_FAILURE); 5062 } 5063 5064 /* 5065 * Search each dir entry looking for a symlink. Return 5066 * the first symlink found in namebuf. Recurse dirs. 5067 */ 5068 while ((fp = finddev_next(fhandle)) != NULL) { 5069 cp = namebuf + strlen(namebuf); 5070 if ((strlcat(namebuf, "/", PATH_MAX) >= PATH_MAX) || 5071 (strlcat(namebuf, fp, PATH_MAX) >= PATH_MAX)) { 5072 *cp = '\0'; 5073 finddev_close(fhandle); 5074 return (DEVFSADM_FAILURE); 5075 } 5076 if (get_stat_info(namebuf, sb) == DEVFSADM_SUCCESS) { 5077 finddev_close(fhandle); 5078 return (DEVFSADM_SUCCESS); 5079 } 5080 *cp = '\0'; 5081 } 5082 finddev_close(fhandle); 5083 } 5084 5085 /* no symlink found, so return error */ 5086 return (DEVFSADM_FAILURE); 5087 } 5088 5089 /* 5090 * An existing matching ID was not found, so this function is called to 5091 * create the next lowest ID. In the INTEGER case, return the next 5092 * lowest unused integer. In the case of LETTER, return the next lowest 5093 * unused letter. Return empty string if all 26 are used. 5094 * Only IDs >= min will be returned. 5095 */ 5096 char * 5097 new_id(numeral_t *numeral, int type, char *min) 5098 { 5099 int imin; 5100 temp_t *temp; 5101 temp_t *ptr; 5102 temp_t **previous; 5103 temp_t *head = NULL; 5104 char *retval; 5105 static char tempbuff[8]; 5106 numeral_t *np; 5107 5108 if (type == LETTER) { 5109 5110 char letter[26], i; 5111 5112 if (numeral == NULL) { 5113 return (s_strdup(min)); 5114 } 5115 5116 for (i = 0; i < 26; i++) { 5117 letter[i] = 0; 5118 } 5119 5120 for (np = numeral; np != NULL; np = np->next) { 5121 letter[*np->id - 'a']++; 5122 } 5123 5124 imin = *min - 'a'; 5125 5126 for (i = imin; i < 26; i++) { 5127 if (letter[i] == 0) { 5128 retval = s_malloc(2); 5129 retval[0] = 'a' + i; 5130 retval[1] = '\0'; 5131 return (retval); 5132 } 5133 } 5134 5135 return (s_strdup("")); 5136 } 5137 5138 if (type == INTEGER) { 5139 5140 if (numeral == NULL) { 5141 return (s_strdup(min)); 5142 } 5143 5144 imin = atoi(min); 5145 5146 /* sort list */ 5147 for (np = numeral; np != NULL; np = np->next) { 5148 temp = s_malloc(sizeof (temp_t)); 5149 temp->integer = atoi(np->id); 5150 temp->next = NULL; 5151 5152 previous = &head; 5153 for (ptr = head; ptr != NULL; ptr = ptr->next) { 5154 if (temp->integer < ptr->integer) { 5155 temp->next = ptr; 5156 *previous = temp; 5157 break; 5158 } 5159 previous = &(ptr->next); 5160 } 5161 if (ptr == NULL) { 5162 *previous = temp; 5163 } 5164 } 5165 5166 /* now search sorted list for first hole >= imin */ 5167 for (ptr = head; ptr != NULL; ptr = ptr->next) { 5168 if (imin == ptr->integer) { 5169 imin++; 5170 } else { 5171 if (imin < ptr->integer) { 5172 break; 5173 } 5174 } 5175 5176 } 5177 5178 /* free temp list */ 5179 for (ptr = head; ptr != NULL; ) { 5180 temp = ptr; 5181 ptr = ptr->next; 5182 free(temp); 5183 } 5184 5185 (void) sprintf(tempbuff, "%d", imin); 5186 return (s_strdup(tempbuff)); 5187 } 5188 5189 return (s_strdup("")); 5190 } 5191 5192 /* 5193 * Search current_dir for all files which match the first path component 5194 * of path_left, which is an RE. If a match is found, but there are more 5195 * components of path_left, then recurse, otherwise, if we have reached 5196 * the last component of path_left, call create_cached_numerals for each 5197 * file. At some point, recurse_dev_re() should be rewritten so that this 5198 * function can be eliminated. 5199 */ 5200 static void 5201 enumerate_recurse(char *current_dir, char *path_left, numeral_set_t *setp, 5202 devfsadm_enumerate_t rules[], int index) 5203 { 5204 char *slash; 5205 char *new_path; 5206 char *numeral_id; 5207 finddevhdl_t fhandle; 5208 const char *fp; 5209 5210 if (finddev_readdir(current_dir, &fhandle) != 0) { 5211 return; 5212 } 5213 5214 /* get rid of any extra '/' */ 5215 while (*path_left == '/') { 5216 path_left++; 5217 } 5218 5219 if (slash = strchr(path_left, '/')) { 5220 *slash = '\0'; 5221 } 5222 5223 while ((fp = finddev_next(fhandle)) != NULL) { 5224 5225 /* 5226 * Returns true if path_left matches the list entry. 5227 * If it is the last path component, pass subexp 5228 * so that it will return the corresponding ID in 5229 * numeral_id. 5230 */ 5231 numeral_id = NULL; 5232 if (match_path_component(path_left, (char *)fp, &numeral_id, 5233 slash ? 0 : rules[index].subexp)) { 5234 5235 new_path = s_malloc(strlen(current_dir) + 5236 strlen(fp) + 2); 5237 5238 (void) strcpy(new_path, current_dir); 5239 (void) strcat(new_path, "/"); 5240 (void) strcat(new_path, fp); 5241 5242 if (slash != NULL) { 5243 enumerate_recurse(new_path, slash + 1, 5244 setp, rules, index); 5245 } else { 5246 create_cached_numeral(new_path, setp, 5247 numeral_id, rules, index); 5248 if (numeral_id != NULL) { 5249 free(numeral_id); 5250 } 5251 } 5252 free(new_path); 5253 } 5254 } 5255 5256 if (slash != NULL) { 5257 *slash = '/'; 5258 } 5259 finddev_close(fhandle); 5260 } 5261 5262 5263 /* 5264 * Returns true if file matches file_re. If subexp is non-zero, it means 5265 * we are searching the last path component and need to return the 5266 * parenthesized subexpression subexp in id. 5267 * 5268 */ 5269 static int 5270 match_path_component(char *file_re, char *file, char **id, int subexp) 5271 { 5272 regex_t re1; 5273 int match = 0; 5274 int nelements; 5275 regmatch_t *pmatch; 5276 5277 if (subexp != 0) { 5278 nelements = subexp + 1; 5279 pmatch = (regmatch_t *) 5280 s_malloc(sizeof (regmatch_t) * nelements); 5281 } else { 5282 pmatch = NULL; 5283 nelements = 0; 5284 } 5285 5286 if (regcomp(&re1, file_re, REG_EXTENDED) != 0) { 5287 if (pmatch != NULL) { 5288 free(pmatch); 5289 } 5290 return (0); 5291 } 5292 5293 if (regexec(&re1, file, nelements, pmatch, 0) == 0) { 5294 match = 1; 5295 } 5296 5297 if ((match != 0) && (subexp != 0)) { 5298 int size = pmatch[subexp].rm_eo - pmatch[subexp].rm_so; 5299 *id = s_malloc(size + 1); 5300 (void) strncpy(*id, &file[pmatch[subexp].rm_so], size); 5301 (*id)[size] = '\0'; 5302 } 5303 5304 if (pmatch != NULL) { 5305 free(pmatch); 5306 } 5307 regfree(&re1); 5308 return (match); 5309 } 5310 5311 /* 5312 * This function is called for every file which matched the leaf 5313 * component of the RE. If the "numeral_id" is not already on the 5314 * numeral set's numeral list, add it and its physical path. 5315 */ 5316 static void 5317 create_cached_numeral(char *path, numeral_set_t *setp, char *numeral_id, 5318 devfsadm_enumerate_t rules[], int index) 5319 { 5320 char linkbuf[PATH_MAX + 1]; 5321 char lpath[PATH_MAX + 1]; 5322 char *linkptr, *cmp_str; 5323 numeral_t *np; 5324 int linksize; 5325 struct stat sb; 5326 const char *fcn = "create_cached_numeral"; 5327 5328 assert(index >= 0 && index < setp->re_count); 5329 assert(strcmp(rules[index].re, setp->re[index]) == 0); 5330 5331 /* 5332 * We found a numeral_id from an entry in /dev which matched 5333 * the re passed in from devfsadm_enumerate. We only need to make sure 5334 * ONE copy of numeral_id exists on the numeral list. We only need 5335 * to store /dev/dsk/cNtod0s0 and no other entries hanging off 5336 * of controller N. 5337 */ 5338 for (np = setp->headnumeral; np != NULL; np = np->next) { 5339 if (strcmp(numeral_id, np->id) == 0) { 5340 return; 5341 } 5342 } 5343 5344 /* NOT on list, so add it */ 5345 5346 (void) strcpy(lpath, path); 5347 /* 5348 * If path is a dir, it is changed to the first symbolic link it find 5349 * if it finds one. 5350 */ 5351 if (get_stat_info(lpath, &sb) == DEVFSADM_FAILURE) { 5352 return; 5353 } 5354 5355 /* If we get here, we found a symlink */ 5356 linksize = readlink(lpath, linkbuf, PATH_MAX); 5357 5358 if (linksize <= 0) { 5359 err_print(READLINK_FAILED, fcn, lpath, strerror(errno)); 5360 return; 5361 } 5362 5363 linkbuf[linksize] = '\0'; 5364 5365 /* 5366 * the following just points linkptr to the root of the /devices 5367 * node if it is a minor node, otherwise, to the first char of 5368 * linkbuf if it is a link. 5369 */ 5370 (void) is_minor_node(linkbuf, &linkptr); 5371 5372 cmp_str = alloc_cmp_str(linkptr, &rules[index]); 5373 if (cmp_str == NULL) { 5374 return; 5375 } 5376 5377 np = s_malloc(sizeof (numeral_t)); 5378 5379 np->id = s_strdup(numeral_id); 5380 np->full_path = s_strdup(linkptr); 5381 np->rule_index = index; 5382 np->cmp_str = cmp_str; 5383 5384 np->next = setp->headnumeral; 5385 setp->headnumeral = np; 5386 } 5387 5388 5389 /* 5390 * This should be called either before or after granting access to a 5391 * command line version of devfsadm running, since it may have changed 5392 * the state of /dev. It forces future enumerate calls to re-build 5393 * cached information from /dev. 5394 */ 5395 void 5396 invalidate_enumerate_cache(void) 5397 { 5398 numeral_set_t *setp; 5399 numeral_set_t *savedsetp; 5400 numeral_t *savednumset; 5401 numeral_t *numset; 5402 int i; 5403 5404 for (setp = head_numeral_set; setp != NULL; ) { 5405 /* 5406 * check all regexp's passed in function against 5407 * those in cached set. 5408 */ 5409 5410 savedsetp = setp; 5411 setp = setp->next; 5412 5413 for (i = 0; i < savedsetp->re_count; i++) { 5414 free(savedsetp->re[i]); 5415 } 5416 free(savedsetp->re); 5417 5418 for (numset = savedsetp->headnumeral; numset != NULL; ) { 5419 savednumset = numset; 5420 numset = numset->next; 5421 assert(savednumset->rule_index < savedsetp->re_count); 5422 free(savednumset->id); 5423 free(savednumset->full_path); 5424 free(savednumset->cmp_str); 5425 free(savednumset); 5426 } 5427 free(savedsetp); 5428 } 5429 head_numeral_set = NULL; 5430 } 5431 5432 /* 5433 * Copies over links from /dev to <root>/dev and device special files in 5434 * /devices to <root>/devices, preserving the existing file modes. If 5435 * the link or special file already exists on <root>, skip the copy. (it 5436 * would exist only if a package hard coded it there, so assume package 5437 * knows best?). Use /etc/name_to_major and <root>/etc/name_to_major to 5438 * make translations for major numbers on device special files. No need to 5439 * make a translation on minor_perm since if the file was created in the 5440 * miniroot then it would presumably have the same minor_perm entry in 5441 * <root>/etc/minor_perm. To be used only by install. 5442 */ 5443 int 5444 devfsadm_copy(void) 5445 { 5446 char filename[PATH_MAX + 1]; 5447 5448 /* load the installed root's name_to_major for translations */ 5449 (void) snprintf(filename, sizeof (filename), "%s%s", root_dir, 5450 NAME_TO_MAJOR); 5451 if (load_n2m_table(filename) == DEVFSADM_FAILURE) { 5452 return (DEVFSADM_FAILURE); 5453 } 5454 5455 /* Copy /dev to target disk. No need to copy /devices with devfs */ 5456 (void) nftw(DEV, devfsadm_copy_file, 20, FTW_PHYS); 5457 5458 /* Let install handle copying over path_to_inst */ 5459 5460 return (DEVFSADM_SUCCESS); 5461 } 5462 5463 /* 5464 * This function copies links, dirs, and device special files. 5465 * Note that it always returns DEVFSADM_SUCCESS, so that nftw doesn't 5466 * abort. 5467 */ 5468 /*ARGSUSED*/ 5469 static int 5470 devfsadm_copy_file(const char *file, const struct stat *stat, 5471 int flags, struct FTW *ftw) 5472 { 5473 struct stat sp; 5474 dev_t newdev; 5475 char newfile[PATH_MAX + 1]; 5476 char linkcontents[PATH_MAX + 1]; 5477 int bytes; 5478 const char *fcn = "devfsadm_copy_file"; 5479 5480 (void) strcpy(newfile, root_dir); 5481 (void) strcat(newfile, "/"); 5482 (void) strcat(newfile, file); 5483 5484 if (lstat(newfile, &sp) == 0) { 5485 /* newfile already exists, so no need to continue */ 5486 return (DEVFSADM_SUCCESS); 5487 } 5488 5489 if (((stat->st_mode & S_IFMT) == S_IFBLK) || 5490 ((stat->st_mode & S_IFMT) == S_IFCHR)) { 5491 if (translate_major(stat->st_rdev, &newdev) == 5492 DEVFSADM_FAILURE) { 5493 return (DEVFSADM_SUCCESS); 5494 } 5495 if (mknod(newfile, stat->st_mode, newdev) == -1) { 5496 err_print(MKNOD_FAILED, newfile, strerror(errno)); 5497 return (DEVFSADM_SUCCESS); 5498 } 5499 } else if ((stat->st_mode & S_IFMT) == S_IFDIR) { 5500 if (mknod(newfile, stat->st_mode, 0) == -1) { 5501 err_print(MKNOD_FAILED, newfile, strerror(errno)); 5502 return (DEVFSADM_SUCCESS); 5503 } 5504 } else if ((stat->st_mode & S_IFMT) == S_IFLNK) { 5505 if ((bytes = readlink(file, linkcontents, PATH_MAX)) == -1) { 5506 err_print(READLINK_FAILED, fcn, file, strerror(errno)); 5507 return (DEVFSADM_SUCCESS); 5508 } 5509 linkcontents[bytes] = '\0'; 5510 if (symlink(linkcontents, newfile) == -1) { 5511 err_print(SYMLINK_FAILED, newfile, newfile, 5512 strerror(errno)); 5513 return (DEVFSADM_SUCCESS); 5514 } 5515 } 5516 5517 (void) lchown(newfile, stat->st_uid, stat->st_gid); 5518 return (DEVFSADM_SUCCESS); 5519 } 5520 5521 /* 5522 * Given a dev_t from the running kernel, return the new_dev_t 5523 * by translating to the major number found on the installed 5524 * target's root name_to_major file. 5525 */ 5526 static int 5527 translate_major(dev_t old_dev, dev_t *new_dev) 5528 { 5529 major_t oldmajor; 5530 major_t newmajor; 5531 minor_t oldminor; 5532 minor_t newminor; 5533 char cdriver[FILENAME_MAX + 1]; 5534 char driver[FILENAME_MAX + 1]; 5535 char *fcn = "translate_major: "; 5536 5537 oldmajor = major(old_dev); 5538 if (modctl(MODGETNAME, driver, sizeof (driver), 5539 &oldmajor) != 0) { 5540 return (DEVFSADM_FAILURE); 5541 } 5542 5543 if (strcmp(driver, "clone") != 0) { 5544 /* non-clone case */ 5545 5546 /* look up major number is target's name2major */ 5547 if (get_major_no(driver, &newmajor) == DEVFSADM_FAILURE) { 5548 return (DEVFSADM_FAILURE); 5549 } 5550 5551 *new_dev = makedev(newmajor, minor(old_dev)); 5552 if (old_dev != *new_dev) { 5553 vprint(CHATTY_MID, "%sdriver: %s old: %lu,%lu " 5554 "new: %lu,%lu\n", fcn, driver, major(old_dev), 5555 minor(old_dev), major(*new_dev), 5556 minor(*new_dev)); 5557 } 5558 return (DEVFSADM_SUCCESS); 5559 } else { 5560 /* 5561 * The clone is a special case. Look at its minor 5562 * number since it is the major number of the real driver. 5563 */ 5564 if (get_major_no(driver, &newmajor) == DEVFSADM_FAILURE) { 5565 return (DEVFSADM_FAILURE); 5566 } 5567 5568 oldminor = minor(old_dev); 5569 if (modctl(MODGETNAME, cdriver, sizeof (cdriver), 5570 &oldminor) != 0) { 5571 err_print(MODGETNAME_FAILED, oldminor); 5572 return (DEVFSADM_FAILURE); 5573 } 5574 5575 if (get_major_no(cdriver, &newminor) == DEVFSADM_FAILURE) { 5576 return (DEVFSADM_FAILURE); 5577 } 5578 5579 *new_dev = makedev(newmajor, newminor); 5580 if (old_dev != *new_dev) { 5581 vprint(CHATTY_MID, "%sdriver: %s old: " 5582 "%lu,%lu new: %lu,%lu\n", fcn, driver, 5583 major(old_dev), minor(old_dev), 5584 major(*new_dev), minor(*new_dev)); 5585 } 5586 return (DEVFSADM_SUCCESS); 5587 } 5588 } 5589 5590 /* 5591 * 5592 * Find the major number for driver, searching the n2m_list that was 5593 * built in load_n2m_table(). 5594 */ 5595 static int 5596 get_major_no(char *driver, major_t *major) 5597 { 5598 n2m_t *ptr; 5599 5600 for (ptr = n2m_list; ptr != NULL; ptr = ptr->next) { 5601 if (strcmp(ptr->driver, driver) == 0) { 5602 *major = ptr->major; 5603 return (DEVFSADM_SUCCESS); 5604 } 5605 } 5606 err_print(FIND_MAJOR_FAILED, driver); 5607 return (DEVFSADM_FAILURE); 5608 } 5609 5610 /* 5611 * Loads a name_to_major table into memory. Used only for suninstall's 5612 * private -R option to devfsadm, to translate major numbers from the 5613 * running to the installed target disk. 5614 */ 5615 static int 5616 load_n2m_table(char *file) 5617 { 5618 FILE *fp; 5619 char line[1024], *cp; 5620 char driver[PATH_MAX + 1]; 5621 major_t major; 5622 n2m_t *ptr; 5623 int ln = 0; 5624 5625 if ((fp = fopen(file, "r")) == NULL) { 5626 err_print(FOPEN_FAILED, file, strerror(errno)); 5627 return (DEVFSADM_FAILURE); 5628 } 5629 5630 while (fgets(line, sizeof (line), fp) != NULL) { 5631 ln++; 5632 /* cut off comments starting with '#' */ 5633 if ((cp = strchr(line, '#')) != NULL) 5634 *cp = '\0'; 5635 /* ignore comment or blank lines */ 5636 if (is_blank(line)) 5637 continue; 5638 /* sanity-check */ 5639 if (sscanf(line, "%1024s%lu", driver, &major) != 2) { 5640 err_print(IGNORING_LINE_IN, ln, file); 5641 continue; 5642 } 5643 ptr = (n2m_t *)s_malloc(sizeof (n2m_t)); 5644 ptr->major = major; 5645 ptr->driver = s_strdup(driver); 5646 ptr->next = n2m_list; 5647 n2m_list = ptr; 5648 } 5649 if (fclose(fp) == EOF) { 5650 err_print(FCLOSE_FAILED, file, strerror(errno)); 5651 } 5652 return (DEVFSADM_SUCCESS); 5653 } 5654 5655 /* 5656 * Called at devfsadm startup to read in the devlink.tab file. Creates 5657 * a linked list of devlinktab_list structures which will be 5658 * searched for every minor node. 5659 */ 5660 static void 5661 read_devlinktab_file(void) 5662 { 5663 devlinktab_list_t *headp = NULL; 5664 devlinktab_list_t *entryp; 5665 devlinktab_list_t **previous; 5666 devlinktab_list_t *save; 5667 char line[MAX_DEVLINK_LINE], *cp; 5668 char *selector; 5669 char *p_link; 5670 char *s_link; 5671 FILE *fp; 5672 int i; 5673 static struct stat cached_sb; 5674 struct stat current_sb; 5675 static int cached = FALSE; 5676 5677 if (devlinktab_file == NULL) { 5678 return; 5679 } 5680 5681 (void) stat(devlinktab_file, ¤t_sb); 5682 5683 /* if already cached, check to see if it is still valid */ 5684 if (cached == TRUE) { 5685 5686 if (current_sb.st_mtime == cached_sb.st_mtime) { 5687 vprint(FILES_MID, "%s cache valid\n", devlinktab_file); 5688 return; 5689 } 5690 5691 vprint(FILES_MID, "invalidating %s cache\n", devlinktab_file); 5692 5693 while (devlinktab_list != NULL) { 5694 free_link_list(devlinktab_list->p_link); 5695 free_link_list(devlinktab_list->s_link); 5696 free_selector_list(devlinktab_list->selector); 5697 free(devlinktab_list->selector_pattern); 5698 free(devlinktab_list->p_link_pattern); 5699 if (devlinktab_list->s_link_pattern != NULL) { 5700 free(devlinktab_list->s_link_pattern); 5701 } 5702 save = devlinktab_list; 5703 devlinktab_list = devlinktab_list->next; 5704 free(save); 5705 } 5706 } else { 5707 cached = TRUE; 5708 } 5709 5710 (void) stat(devlinktab_file, &cached_sb); 5711 5712 if ((fp = fopen(devlinktab_file, "r")) == NULL) { 5713 err_print(FOPEN_FAILED, devlinktab_file, strerror(errno)); 5714 return; 5715 } 5716 5717 previous = &headp; 5718 5719 while (fgets(line, sizeof (line), fp) != NULL) { 5720 devlinktab_line++; 5721 i = strlen(line); 5722 if (line[i-1] == NEWLINE) { 5723 line[i-1] = '\0'; 5724 } else if (i == sizeof (line-1)) { 5725 err_print(LINE_TOO_LONG, devlinktab_line, 5726 devlinktab_file, sizeof (line)-1); 5727 while (((i = getc(fp)) != '\n') && (i != EOF)); 5728 continue; 5729 } 5730 5731 /* cut off comments starting with '#' */ 5732 if ((cp = strchr(line, '#')) != NULL) 5733 *cp = '\0'; 5734 /* ignore comment or blank lines */ 5735 if (is_blank(line)) 5736 continue; 5737 5738 vprint(DEVLINK_MID, "table: %s line %d: '%s'\n", 5739 devlinktab_file, devlinktab_line, line); 5740 5741 /* break each entry into fields. s_link may be NULL */ 5742 if (split_devlinktab_entry(line, &selector, &p_link, 5743 &s_link) == DEVFSADM_FAILURE) { 5744 vprint(DEVLINK_MID, "split_entry returns failure\n"); 5745 continue; 5746 } else { 5747 vprint(DEVLINK_MID, "split_entry selector='%s' " 5748 "p_link='%s' s_link='%s'\n\n", selector, 5749 p_link, (s_link == NULL) ? "" : s_link); 5750 } 5751 5752 entryp = (devlinktab_list_t *) 5753 s_malloc(sizeof (devlinktab_list_t)); 5754 5755 entryp->line_number = devlinktab_line; 5756 5757 if ((entryp->selector = 5758 create_selector_list(selector)) == NULL) { 5759 free(entryp); 5760 continue; 5761 } 5762 entryp->selector_pattern = s_strdup(selector); 5763 5764 if ((entryp->p_link = create_link_list(p_link)) == NULL) { 5765 free_selector_list(entryp->selector); 5766 free(entryp->selector_pattern); 5767 free(entryp); 5768 continue; 5769 } 5770 5771 entryp->p_link_pattern = s_strdup(p_link); 5772 5773 if (s_link != NULL) { 5774 if ((entryp->s_link = 5775 create_link_list(s_link)) == NULL) { 5776 free_selector_list(entryp->selector); 5777 free_link_list(entryp->p_link); 5778 free(entryp->selector_pattern); 5779 free(entryp->p_link_pattern); 5780 free(entryp); 5781 continue; 5782 } 5783 entryp->s_link_pattern = s_strdup(s_link); 5784 } else { 5785 entryp->s_link = NULL; 5786 entryp->s_link_pattern = NULL; 5787 5788 } 5789 5790 /* append to end of list */ 5791 5792 entryp->next = NULL; 5793 *previous = entryp; 5794 previous = &(entryp->next); 5795 } 5796 if (fclose(fp) == EOF) { 5797 err_print(FCLOSE_FAILED, devlinktab_file, strerror(errno)); 5798 } 5799 devlinktab_list = headp; 5800 } 5801 5802 /* 5803 * 5804 * For a single line entry in devlink.tab, split the line into fields 5805 * selector, p_link, and an optionally s_link. If s_link field is not 5806 * present, then return NULL in s_link (not NULL string). 5807 */ 5808 static int 5809 split_devlinktab_entry(char *entry, char **selector, char **p_link, 5810 char **s_link) 5811 { 5812 char *tab; 5813 5814 *selector = entry; 5815 5816 if ((tab = strchr(entry, TAB)) != NULL) { 5817 *tab = '\0'; 5818 *p_link = ++tab; 5819 } else { 5820 err_print(MISSING_TAB, devlinktab_line, devlinktab_file); 5821 return (DEVFSADM_FAILURE); 5822 } 5823 5824 if (*p_link == '\0') { 5825 err_print(MISSING_DEVNAME, devlinktab_line, devlinktab_file); 5826 return (DEVFSADM_FAILURE); 5827 } 5828 5829 if ((tab = strchr(*p_link, TAB)) != NULL) { 5830 *tab = '\0'; 5831 *s_link = ++tab; 5832 if (strchr(*s_link, TAB) != NULL) { 5833 err_print(TOO_MANY_FIELDS, devlinktab_line, 5834 devlinktab_file); 5835 return (DEVFSADM_FAILURE); 5836 } 5837 } else { 5838 *s_link = NULL; 5839 } 5840 5841 return (DEVFSADM_SUCCESS); 5842 } 5843 5844 /* 5845 * For a given devfs_spec field, for each element in the field, add it to 5846 * a linked list of devfs_spec structures. Return the linked list in 5847 * devfs_spec_list. 5848 */ 5849 static selector_list_t * 5850 create_selector_list(char *selector) 5851 { 5852 char *key; 5853 char *val; 5854 int error = FALSE; 5855 selector_list_t *head_selector_list = NULL; 5856 selector_list_t *selector_list; 5857 5858 /* parse_devfs_spec splits the next field into keyword & value */ 5859 while ((*selector != NULL) && (error == FALSE)) { 5860 if (parse_selector(&selector, &key, 5861 &val) == DEVFSADM_FAILURE) { 5862 error = TRUE; 5863 break; 5864 } else { 5865 selector_list = (selector_list_t *) 5866 s_malloc(sizeof (selector_list_t)); 5867 if (strcmp(NAME_S, key) == 0) { 5868 selector_list->key = NAME; 5869 } else if (strcmp(TYPE_S, key) == 0) { 5870 selector_list->key = TYPE; 5871 } else if (strncmp(ADDR_S, key, ADDR_S_LEN) == 0) { 5872 selector_list->key = ADDR; 5873 if (key[ADDR_S_LEN] == '\0') { 5874 selector_list->arg = 0; 5875 } else if (isdigit(key[ADDR_S_LEN]) != 5876 FALSE) { 5877 selector_list->arg = 5878 atoi(&key[ADDR_S_LEN]); 5879 } else { 5880 error = TRUE; 5881 free(selector_list); 5882 err_print(BADKEYWORD, key, 5883 devlinktab_line, 5884 devlinktab_file); 5885 break; 5886 } 5887 } else if (strncmp(MINOR_S, key, 5888 MINOR_S_LEN) == 0) { 5889 selector_list->key = MINOR; 5890 if (key[MINOR_S_LEN] == '\0') { 5891 selector_list->arg = 0; 5892 } else if (isdigit(key[MINOR_S_LEN]) != 5893 FALSE) { 5894 selector_list->arg = 5895 atoi(&key[MINOR_S_LEN]); 5896 } else { 5897 error = TRUE; 5898 free(selector_list); 5899 err_print(BADKEYWORD, key, 5900 devlinktab_line, 5901 devlinktab_file); 5902 break; 5903 } 5904 vprint(DEVLINK_MID, "MINOR = %s\n", val); 5905 } else { 5906 err_print(UNRECOGNIZED_KEY, key, 5907 devlinktab_line, devlinktab_file); 5908 error = TRUE; 5909 free(selector_list); 5910 break; 5911 } 5912 selector_list->val = s_strdup(val); 5913 selector_list->next = head_selector_list; 5914 head_selector_list = selector_list; 5915 vprint(DEVLINK_MID, "key='%s' val='%s' arg=%d\n", 5916 key, val, selector_list->arg); 5917 } 5918 } 5919 5920 if ((error == FALSE) && (head_selector_list != NULL)) { 5921 return (head_selector_list); 5922 } else { 5923 /* parse failed. Free any allocated structs */ 5924 free_selector_list(head_selector_list); 5925 return (NULL); 5926 } 5927 } 5928 5929 /* 5930 * Takes a semicolon separated list of selector elements and breaks up 5931 * into a keyword-value pair. semicolon and equal characters are 5932 * replaced with NULL's. On success, selector is updated to point to the 5933 * terminating NULL character terminating the keyword-value pair, and the 5934 * function returns DEVFSADM_SUCCESS. If there is a syntax error, 5935 * devfs_spec is not modified and function returns DEVFSADM_FAILURE. 5936 */ 5937 static int 5938 parse_selector(char **selector, char **key, char **val) 5939 { 5940 char *equal; 5941 char *semi_colon; 5942 5943 *key = *selector; 5944 5945 if ((equal = strchr(*key, '=')) != NULL) { 5946 *equal = '\0'; 5947 } else { 5948 err_print(MISSING_EQUAL, devlinktab_line, devlinktab_file); 5949 return (DEVFSADM_FAILURE); 5950 } 5951 5952 *val = ++equal; 5953 if ((semi_colon = strchr(equal, ';')) != NULL) { 5954 *semi_colon = '\0'; 5955 *selector = semi_colon + 1; 5956 } else { 5957 *selector = equal + strlen(equal); 5958 } 5959 return (DEVFSADM_SUCCESS); 5960 } 5961 5962 /* 5963 * link is either the second or third field of devlink.tab. Parse link 5964 * into a linked list of devlink structures and return ptr to list. Each 5965 * list element is either a constant string, or one of the following 5966 * escape sequences: \M, \A, \N, or \D. The first three escape sequences 5967 * take a numerical argument. 5968 */ 5969 static link_list_t * 5970 create_link_list(char *link) 5971 { 5972 int x = 0; 5973 int error = FALSE; 5974 int counter_found = FALSE; 5975 link_list_t *head = NULL; 5976 link_list_t **ptr; 5977 link_list_t *link_list; 5978 char constant[MAX_DEVLINK_LINE]; 5979 char *error_str; 5980 5981 if (link == NULL) { 5982 return (NULL); 5983 } 5984 5985 while ((*link != '\0') && (error == FALSE)) { 5986 link_list = (link_list_t *)s_malloc(sizeof (link_list_t)); 5987 link_list->next = NULL; 5988 5989 while ((*link != '\0') && (*link != '\\')) { 5990 /* a non-escaped string */ 5991 constant[x++] = *(link++); 5992 } 5993 if (x != 0) { 5994 constant[x] = '\0'; 5995 link_list->type = CONSTANT; 5996 link_list->constant = s_strdup(constant); 5997 x = 0; 5998 vprint(DEVLINK_MID, "CONSTANT FOUND %s\n", constant); 5999 } else { 6000 switch (*(++link)) { 6001 case 'M': 6002 link_list->type = MINOR; 6003 break; 6004 case 'A': 6005 link_list->type = ADDR; 6006 break; 6007 case 'N': 6008 if (counter_found == TRUE) { 6009 error = TRUE; 6010 error_str = "multiple counters " 6011 "not permitted"; 6012 free(link_list); 6013 } else { 6014 counter_found = TRUE; 6015 link_list->type = COUNTER; 6016 } 6017 break; 6018 case 'D': 6019 link_list->type = NAME; 6020 break; 6021 default: 6022 error = TRUE; 6023 free(link_list); 6024 error_str = "unrecognized escape sequence"; 6025 break; 6026 } 6027 if (*(link++) != 'D') { 6028 if (isdigit(*link) == FALSE) { 6029 error_str = "escape sequence must be " 6030 "followed by a digit\n"; 6031 error = TRUE; 6032 free(link_list); 6033 } else { 6034 link_list->arg = 6035 (int)strtoul(link, &link, 10); 6036 vprint(DEVLINK_MID, "link_list->arg = " 6037 "%d\n", link_list->arg); 6038 } 6039 } 6040 } 6041 /* append link_list struct to end of list */ 6042 if (error == FALSE) { 6043 for (ptr = &head; *ptr != NULL; ptr = &((*ptr)->next)); 6044 *ptr = link_list; 6045 } 6046 } 6047 6048 if (error == FALSE) { 6049 return (head); 6050 } else { 6051 err_print(CONFIG_INCORRECT, devlinktab_line, devlinktab_file, 6052 error_str); 6053 free_link_list(head); 6054 return (NULL); 6055 } 6056 } 6057 6058 /* 6059 * Called for each minor node devfsadm processes; for each minor node, 6060 * look for matches in the devlinktab_list list which was created on 6061 * startup read_devlinktab_file(). If there is a match, call build_links() 6062 * to build a logical devlink and a possible extra devlink. 6063 */ 6064 static int 6065 process_devlink_compat(di_minor_t minor, di_node_t node) 6066 { 6067 int link_built = FALSE; 6068 devlinktab_list_t *entry; 6069 char *nodetype; 6070 char *dev_path; 6071 6072 if (devlinks_debug == TRUE) { 6073 nodetype = di_minor_nodetype(minor); 6074 assert(nodetype != NULL); 6075 if ((dev_path = di_devfs_path(node)) != NULL) { 6076 vprint(INFO_MID, "'%s' entry: %s:%s\n", nodetype, 6077 dev_path, 6078 di_minor_name(minor) ? di_minor_name(minor) : 6079 ""); 6080 di_devfs_path_free(dev_path); 6081 } 6082 6083 } 6084 6085 6086 /* don't process devlink.tab if devfsadm invoked with -c <class> */ 6087 if (num_classes > 0) { 6088 return (FALSE); 6089 } 6090 6091 for (entry = devlinktab_list; entry != NULL; entry = entry->next) { 6092 if (devlink_matches(entry, minor, node) == DEVFSADM_SUCCESS) { 6093 link_built = TRUE; 6094 (void) build_links(entry, minor, node); 6095 } 6096 } 6097 return (link_built); 6098 } 6099 6100 /* 6101 * For a given devlink.tab devlinktab_list entry, see if the selector 6102 * field matches this minor node. If it does, return DEVFSADM_SUCCESS, 6103 * otherwise DEVFSADM_FAILURE. 6104 */ 6105 static int 6106 devlink_matches(devlinktab_list_t *entry, di_minor_t minor, di_node_t node) 6107 { 6108 selector_list_t *selector = entry->selector; 6109 char *addr; 6110 char *minor_name; 6111 char *node_type; 6112 6113 for (; selector != NULL; selector = selector->next) { 6114 switch (selector->key) { 6115 case NAME: 6116 if (strcmp(di_node_name(node), selector->val) != 0) { 6117 return (DEVFSADM_FAILURE); 6118 } 6119 break; 6120 case TYPE: 6121 node_type = di_minor_nodetype(minor); 6122 assert(node_type != NULL); 6123 if (strcmp(node_type, selector->val) != 0) { 6124 return (DEVFSADM_FAILURE); 6125 } 6126 break; 6127 case ADDR: 6128 if ((addr = di_bus_addr(node)) == NULL) { 6129 return (DEVFSADM_FAILURE); 6130 } 6131 if (selector->arg == 0) { 6132 if (strcmp(addr, selector->val) != 0) { 6133 return (DEVFSADM_FAILURE); 6134 } 6135 } else { 6136 if (compare_field(addr, selector->val, 6137 selector->arg) == DEVFSADM_FAILURE) { 6138 return (DEVFSADM_FAILURE); 6139 } 6140 } 6141 break; 6142 case MINOR: 6143 if ((minor_name = di_minor_name(minor)) == NULL) { 6144 return (DEVFSADM_FAILURE); 6145 } 6146 if (selector->arg == 0) { 6147 if (strcmp(minor_name, selector->val) != 0) { 6148 return (DEVFSADM_FAILURE); 6149 } 6150 } else { 6151 if (compare_field(minor_name, selector->val, 6152 selector->arg) == DEVFSADM_FAILURE) { 6153 return (DEVFSADM_FAILURE); 6154 } 6155 } 6156 break; 6157 default: 6158 return (DEVFSADM_FAILURE); 6159 } 6160 } 6161 6162 return (DEVFSADM_SUCCESS); 6163 } 6164 6165 /* 6166 * For the given minor node and devlinktab_list entry from devlink.tab, 6167 * build a logical dev link and a possible extra devlink. 6168 * Return DEVFSADM_SUCCESS if link is created, otherwise DEVFSADM_FAILURE. 6169 */ 6170 static int 6171 build_links(devlinktab_list_t *entry, di_minor_t minor, di_node_t node) 6172 { 6173 char secondary_link[PATH_MAX + 1]; 6174 char primary_link[PATH_MAX + 1]; 6175 char contents[PATH_MAX + 1]; 6176 char *dev_path; 6177 6178 if ((dev_path = di_devfs_path(node)) == NULL) { 6179 err_print(DI_DEVFS_PATH_FAILED, strerror(errno)); 6180 devfsadm_exit(1); 6181 } 6182 (void) strcpy(contents, dev_path); 6183 di_devfs_path_free(dev_path); 6184 6185 (void) strcat(contents, ":"); 6186 (void) strcat(contents, di_minor_name(minor)); 6187 6188 if (construct_devlink(primary_link, entry->p_link, contents, 6189 minor, node, 6190 entry->p_link_pattern) == DEVFSADM_FAILURE) { 6191 return (DEVFSADM_FAILURE); 6192 } 6193 (void) devfsadm_mklink(primary_link, node, minor, 0); 6194 6195 if (entry->s_link == NULL) { 6196 return (DEVFSADM_SUCCESS); 6197 } 6198 6199 if (construct_devlink(secondary_link, entry->s_link, 6200 primary_link, minor, node, 6201 entry->s_link_pattern) == DEVFSADM_FAILURE) { 6202 return (DEVFSADM_FAILURE); 6203 } 6204 6205 (void) devfsadm_secondary_link(secondary_link, primary_link, 0); 6206 6207 return (DEVFSADM_SUCCESS); 6208 } 6209 6210 /* 6211 * The counter rule for devlink.tab entries is implemented via 6212 * devfsadm_enumerate_int_start(). One of the arguments to this function 6213 * is a path, where each path component is treated as a regular expression. 6214 * For devlink.tab entries, this path regular expression is derived from 6215 * the devlink spec. get_anchored_re() accepts path regular expressions derived 6216 * from devlink.tab entries and inserts the anchors '^' and '$' at the beginning 6217 * and end respectively of each path component. This is done to prevent 6218 * false matches. For example, without anchors, "a/([0-9]+)" will match "ab/c9" 6219 * and incorrect links will be generated. 6220 */ 6221 static int 6222 get_anchored_re(char *link, char *anchored_re, char *pattern) 6223 { 6224 if (*link == '/' || *link == '\0') { 6225 err_print(INVALID_DEVLINK_SPEC, pattern); 6226 return (DEVFSADM_FAILURE); 6227 } 6228 6229 *anchored_re++ = '^'; 6230 for (; *link != '\0'; ) { 6231 if (*link == '/') { 6232 while (*link == '/') 6233 link++; 6234 *anchored_re++ = '$'; 6235 *anchored_re++ = '/'; 6236 if (*link != '\0') { 6237 *anchored_re++ = '^'; 6238 } 6239 } else { 6240 *anchored_re++ = *link++; 6241 if (*link == '\0') { 6242 *anchored_re++ = '$'; 6243 } 6244 } 6245 } 6246 *anchored_re = '\0'; 6247 6248 return (DEVFSADM_SUCCESS); 6249 } 6250 6251 static int 6252 construct_devlink(char *link, link_list_t *link_build, char *contents, 6253 di_minor_t minor, di_node_t node, char *pattern) 6254 { 6255 int counter_offset = -1; 6256 devfsadm_enumerate_t rules[1] = {NULL}; 6257 char templink[PATH_MAX + 1]; 6258 char *buff; 6259 char start[10]; 6260 char *node_path; 6261 char anchored_re[PATH_MAX + 1]; 6262 6263 link[0] = '\0'; 6264 6265 for (; link_build != NULL; link_build = link_build->next) { 6266 switch (link_build->type) { 6267 case NAME: 6268 (void) strcat(link, di_node_name(node)); 6269 break; 6270 case CONSTANT: 6271 (void) strcat(link, link_build->constant); 6272 break; 6273 case ADDR: 6274 if (component_cat(link, di_bus_addr(node), 6275 link_build->arg) == DEVFSADM_FAILURE) { 6276 node_path = di_devfs_path(node); 6277 err_print(CANNOT_BE_USED, pattern, node_path, 6278 di_minor_name(minor)); 6279 di_devfs_path_free(node_path); 6280 return (DEVFSADM_FAILURE); 6281 } 6282 break; 6283 case MINOR: 6284 if (component_cat(link, di_minor_name(minor), 6285 link_build->arg) == DEVFSADM_FAILURE) { 6286 node_path = di_devfs_path(node); 6287 err_print(CANNOT_BE_USED, pattern, node_path, 6288 di_minor_name(minor)); 6289 di_devfs_path_free(node_path); 6290 return (DEVFSADM_FAILURE); 6291 } 6292 break; 6293 case COUNTER: 6294 counter_offset = strlen(link); 6295 (void) strcat(link, "([0-9]+)"); 6296 (void) sprintf(start, "%d", link_build->arg); 6297 break; 6298 default: 6299 return (DEVFSADM_FAILURE); 6300 } 6301 } 6302 6303 if (counter_offset != -1) { 6304 /* 6305 * copy anything appended after "([0-9]+)" into 6306 * templink 6307 */ 6308 6309 (void) strcpy(templink, 6310 &link[counter_offset + strlen("([0-9]+)")]); 6311 if (get_anchored_re(link, anchored_re, pattern) 6312 != DEVFSADM_SUCCESS) { 6313 return (DEVFSADM_FAILURE); 6314 } 6315 rules[0].re = anchored_re; 6316 rules[0].subexp = 1; 6317 rules[0].flags = MATCH_ALL; 6318 if (devfsadm_enumerate_int_start(contents, 0, &buff, 6319 rules, 1, start) == DEVFSADM_FAILURE) { 6320 return (DEVFSADM_FAILURE); 6321 } 6322 (void) strcpy(&link[counter_offset], buff); 6323 free(buff); 6324 (void) strcat(link, templink); 6325 vprint(DEVLINK_MID, "COUNTER is %s\n", link); 6326 } 6327 return (DEVFSADM_SUCCESS); 6328 } 6329 6330 /* 6331 * Compares "field" number of the comma separated list "full_name" with 6332 * field_item. Returns DEVFSADM_SUCCESS for match, 6333 * DEVFSADM_FAILURE for no match. 6334 */ 6335 static int 6336 compare_field(char *full_name, char *field_item, int field) 6337 { 6338 --field; 6339 while ((*full_name != '\0') && (field != 0)) { 6340 if (*(full_name++) == ',') { 6341 field--; 6342 } 6343 } 6344 6345 if (field != 0) { 6346 return (DEVFSADM_FAILURE); 6347 } 6348 6349 while ((*full_name != '\0') && (*field_item != '\0') && 6350 (*full_name != ',')) { 6351 if (*(full_name++) != *(field_item++)) { 6352 return (DEVFSADM_FAILURE); 6353 } 6354 } 6355 6356 if (*field_item != '\0') { 6357 return (DEVFSADM_FAILURE); 6358 } 6359 6360 if ((*full_name == '\0') || (*full_name == ',')) 6361 return (DEVFSADM_SUCCESS); 6362 6363 return (DEVFSADM_FAILURE); 6364 } 6365 6366 /* 6367 * strcat() field # "field" of comma separated list "name" to "link". 6368 * Field 0 is the entire name. 6369 * Return DEVFSADM_SUCCESS or DEVFSADM_FAILURE. 6370 */ 6371 static int 6372 component_cat(char *link, char *name, int field) 6373 { 6374 6375 if (name == NULL) { 6376 return (DEVFSADM_FAILURE); 6377 } 6378 6379 if (field == 0) { 6380 (void) strcat(link, name); 6381 return (DEVFSADM_SUCCESS); 6382 } 6383 6384 while (*link != '\0') { 6385 link++; 6386 } 6387 6388 --field; 6389 while ((*name != '\0') && (field != 0)) { 6390 if (*(name++) == ',') { 6391 --field; 6392 } 6393 } 6394 6395 if (field != 0) { 6396 return (DEVFSADM_FAILURE); 6397 } 6398 6399 while ((*name != '\0') && (*name != ',')) { 6400 *(link++) = *(name++); 6401 } 6402 6403 *link = '\0'; 6404 return (DEVFSADM_SUCCESS); 6405 } 6406 6407 static void 6408 free_selector_list(selector_list_t *head) 6409 { 6410 selector_list_t *temp; 6411 6412 while (head != NULL) { 6413 temp = head; 6414 head = head->next; 6415 free(temp->val); 6416 free(temp); 6417 } 6418 } 6419 6420 static void 6421 free_link_list(link_list_t *head) 6422 { 6423 link_list_t *temp; 6424 6425 while (head != NULL) { 6426 temp = head; 6427 head = head->next; 6428 if (temp->type == CONSTANT) { 6429 free(temp->constant); 6430 } 6431 free(temp); 6432 } 6433 } 6434 6435 /* 6436 * Prints only if level matches one of the debug levels 6437 * given on command line. INFO_MID is always printed. 6438 * 6439 * See devfsadm.h for a listing of globally defined levels and 6440 * meanings. Modules should prefix the level with their 6441 * module name to prevent collisions. 6442 */ 6443 /*PRINTFLIKE2*/ 6444 void 6445 devfsadm_print(char *msgid, char *message, ...) 6446 { 6447 va_list ap; 6448 static int newline = TRUE; 6449 int x; 6450 6451 if (msgid != NULL) { 6452 for (x = 0; x < num_verbose; x++) { 6453 if (strcmp(verbose[x], msgid) == 0) { 6454 break; 6455 } 6456 if (strcmp(verbose[x], ALL_MID) == 0) { 6457 break; 6458 } 6459 } 6460 if (x == num_verbose) { 6461 return; 6462 } 6463 } 6464 6465 va_start(ap, message); 6466 6467 if (msgid == NULL) { 6468 if (logflag == TRUE) { 6469 (void) vsyslog(LOG_NOTICE, message, ap); 6470 } else { 6471 (void) vfprintf(stdout, message, ap); 6472 } 6473 6474 } else { 6475 if (logflag == TRUE) { 6476 (void) syslog(LOG_DEBUG, "%s[%ld]: %s: ", 6477 prog, getpid(), msgid); 6478 (void) vsyslog(LOG_DEBUG, message, ap); 6479 } else { 6480 if (newline == TRUE) { 6481 (void) fprintf(stdout, "%s[%ld]: %s: ", 6482 prog, getpid(), msgid); 6483 } 6484 (void) vfprintf(stdout, message, ap); 6485 } 6486 } 6487 6488 if (message[strlen(message) - 1] == '\n') { 6489 newline = TRUE; 6490 } else { 6491 newline = FALSE; 6492 } 6493 va_end(ap); 6494 } 6495 6496 /* 6497 * print error messages to the terminal or to syslog 6498 */ 6499 /*PRINTFLIKE1*/ 6500 void 6501 devfsadm_errprint(char *message, ...) 6502 { 6503 va_list ap; 6504 6505 va_start(ap, message); 6506 6507 if (logflag == TRUE) { 6508 (void) vsyslog(LOG_ERR, message, ap); 6509 } else { 6510 (void) fprintf(stderr, "%s: ", prog); 6511 (void) vfprintf(stderr, message, ap); 6512 } 6513 va_end(ap); 6514 } 6515 6516 /* 6517 * return noupdate state (-s) 6518 */ 6519 int 6520 devfsadm_noupdate(void) 6521 { 6522 return (file_mods == TRUE ? DEVFSADM_TRUE : DEVFSADM_FALSE); 6523 } 6524 6525 /* 6526 * return current root update path (-r) 6527 */ 6528 const char * 6529 devfsadm_root_path(void) 6530 { 6531 if (root_dir[0] == '\0') { 6532 return ("/"); 6533 } else { 6534 return ((const char *)root_dir); 6535 } 6536 } 6537 6538 void 6539 devfsadm_free_dev_names(char **dev_names, int len) 6540 { 6541 int i; 6542 6543 for (i = 0; i < len; i++) 6544 free(dev_names[i]); 6545 free(dev_names); 6546 } 6547 6548 /* 6549 * Return all devlinks corresponding to phys_path as an array of strings. 6550 * The number of entries in the array is returned through lenp. 6551 * devfsadm_free_dev_names() is used to free the returned array. 6552 * NULL is returned on failure or when there are no matching devlinks. 6553 * 6554 * re is an extended regular expression in regex(5) format used to further 6555 * match devlinks pointing to phys_path; it may be NULL to match all 6556 */ 6557 char ** 6558 devfsadm_lookup_dev_names(char *phys_path, char *re, int *lenp) 6559 { 6560 struct devlink_cb_arg cb_arg; 6561 char **dev_names = NULL; 6562 int i; 6563 6564 *lenp = 0; 6565 cb_arg.count = 0; 6566 cb_arg.rv = 0; 6567 (void) di_devlink_cache_walk(devlink_cache, re, phys_path, 6568 DI_PRIMARY_LINK, &cb_arg, devlink_cb); 6569 6570 if (cb_arg.rv == -1 || cb_arg.count <= 0) 6571 return (NULL); 6572 6573 dev_names = s_malloc(cb_arg.count * sizeof (char *)); 6574 if (dev_names == NULL) 6575 goto out; 6576 6577 for (i = 0; i < cb_arg.count; i++) { 6578 dev_names[i] = s_strdup(cb_arg.dev_names[i]); 6579 if (dev_names[i] == NULL) { 6580 devfsadm_free_dev_names(dev_names, i); 6581 dev_names = NULL; 6582 goto out; 6583 } 6584 } 6585 *lenp = cb_arg.count; 6586 6587 out: 6588 free_dev_names(&cb_arg); 6589 return (dev_names); 6590 } 6591 6592 /* common exit function which ensures releasing locks */ 6593 static void 6594 devfsadm_exit(int status) 6595 { 6596 if (DEVFSADM_DEBUG_ON) { 6597 vprint(INFO_MID, "exit status = %d\n", status); 6598 } 6599 6600 if (rcm_hdl) { 6601 if (thr_self() != process_rcm_events_tid) { 6602 (void) mutex_lock(&rcm_eventq_lock); 6603 need_to_exit_rcm_event_thread = 1; 6604 (void) cond_broadcast(&rcm_eventq_cv); 6605 (void) mutex_unlock(&rcm_eventq_lock); 6606 6607 /* wait until process_rcm_events() thread exits */ 6608 (void) thr_join(process_rcm_events_tid, NULL, NULL); 6609 } 6610 librcm_free_handle(rcm_hdl); 6611 (void) dlclose(librcm_hdl); 6612 } 6613 6614 exit_dev_lock(); 6615 exit_daemon_lock(); 6616 6617 if (logflag == TRUE) { 6618 closelog(); 6619 } 6620 6621 exit(status); 6622 } 6623 6624 /* 6625 * set root_dir, devices_dir, dev_dir using optarg. 6626 */ 6627 static void 6628 set_root_devices_dev_dir(char *dir) 6629 { 6630 size_t len; 6631 6632 root_dir = s_strdup(dir); 6633 len = strlen(dir) + strlen(DEVICES) + 1; 6634 devices_dir = s_malloc(len); 6635 (void) snprintf(devices_dir, len, "%s%s", root_dir, DEVICES); 6636 len = strlen(root_dir) + strlen(DEV) + 1; 6637 dev_dir = s_malloc(len); 6638 (void) snprintf(dev_dir, len, "%s%s", root_dir, DEV); 6639 } 6640 6641 /* 6642 * Removes quotes. 6643 */ 6644 static char * 6645 dequote(char *src) 6646 { 6647 char *dst; 6648 int len; 6649 6650 len = strlen(src); 6651 dst = s_malloc(len + 1); 6652 if (src[0] == '\"' && src[len - 1] == '\"') { 6653 len -= 2; 6654 (void) strncpy(dst, &src[1], len); 6655 dst[len] = '\0'; 6656 } else { 6657 (void) strcpy(dst, src); 6658 } 6659 return (dst); 6660 } 6661 6662 /* 6663 * For a given physical device pathname and spectype, return the 6664 * ownership and permissions attributes by looking in data from 6665 * /etc/minor_perm. If currently in installation mode, check for 6666 * possible major number translations from the miniroot to the installed 6667 * root's name_to_major table. Note that there can be multiple matches, 6668 * but the last match takes effect. pts seems to rely on this 6669 * implementation behavior. 6670 */ 6671 static void 6672 getattr(char *phy_path, char *aminor, int spectype, dev_t dev, mode_t *mode, 6673 uid_t *uid, gid_t *gid) 6674 { 6675 char devname[PATH_MAX + 1]; 6676 char *node_name; 6677 char *minor_name; 6678 int match = FALSE; 6679 int is_clone; 6680 int mp_drvname_matches_node_name; 6681 int mp_drvname_matches_minor_name; 6682 int mp_drvname_is_clone; 6683 int mp_drvname_matches_drvname; 6684 struct mperm *mp; 6685 major_t major_no; 6686 char driver[PATH_MAX + 1]; 6687 6688 /* 6689 * Get the driver name based on the major number since the name 6690 * in /devices may be generic. Could be running with more major 6691 * numbers than are in /etc/name_to_major, so get it from the kernel 6692 */ 6693 major_no = major(dev); 6694 6695 if (modctl(MODGETNAME, driver, sizeof (driver), &major_no) != 0) { 6696 /* return default values */ 6697 goto use_defaults; 6698 } 6699 6700 (void) strcpy(devname, phy_path); 6701 6702 node_name = strrchr(devname, '/'); /* node name is the last */ 6703 /* component */ 6704 if (node_name == NULL) { 6705 err_print(NO_NODE, devname); 6706 goto use_defaults; 6707 } 6708 6709 minor_name = strchr(++node_name, '@'); /* see if it has address part */ 6710 6711 if (minor_name != NULL) { 6712 *minor_name++ = '\0'; 6713 } else { 6714 minor_name = node_name; 6715 } 6716 6717 minor_name = strchr(minor_name, ':'); /* look for minor name */ 6718 6719 if (minor_name == NULL) { 6720 err_print(NO_MINOR, devname); 6721 goto use_defaults; 6722 } 6723 *minor_name++ = '\0'; 6724 6725 /* 6726 * mp->mp_drvname = device name from minor_perm 6727 * mp->mp_minorname = minor part of device name from 6728 * minor_perm 6729 * drvname = name of driver for this device 6730 */ 6731 6732 is_clone = (strcmp(node_name, "clone") == 0 ? TRUE : FALSE); 6733 for (mp = minor_perms; mp != NULL; mp = mp->mp_next) { 6734 mp_drvname_matches_node_name = 6735 (strcmp(mp->mp_drvname, node_name) == 0 ? TRUE : FALSE); 6736 mp_drvname_matches_minor_name = 6737 (strcmp(mp->mp_drvname, minor_name) == 0 ? TRUE:FALSE); 6738 mp_drvname_is_clone = 6739 (strcmp(mp->mp_drvname, "clone") == 0 ? TRUE : FALSE); 6740 mp_drvname_matches_drvname = 6741 (strcmp(mp->mp_drvname, driver) == 0 ? TRUE : FALSE); 6742 6743 /* 6744 * If one of the following cases is true, then we try to change 6745 * the permissions if a "shell global pattern match" of 6746 * mp_>mp_minorname matches minor_name. 6747 * 6748 * 1. mp->mp_drvname matches driver. 6749 * 6750 * OR 6751 * 6752 * 2. mp->mp_drvname matches node_name and this 6753 * name is an alias of the driver name 6754 * 6755 * OR 6756 * 6757 * 3. /devices entry is the clone device and either 6758 * minor_perm entry is the clone device or matches 6759 * the minor part of the clone device. 6760 */ 6761 6762 if ((mp_drvname_matches_drvname == TRUE)|| 6763 ((mp_drvname_matches_node_name == TRUE) && 6764 (alias(driver, node_name) == TRUE)) || 6765 ((is_clone == TRUE) && 6766 ((mp_drvname_is_clone == TRUE) || 6767 (mp_drvname_matches_minor_name == TRUE)))) { 6768 /* 6769 * Check that the minor part of the 6770 * device name from the minor_perm 6771 * entry matches and if so, set the 6772 * permissions. 6773 * 6774 * Under real devfs, clone minor name is changed 6775 * to match the driver name, but minor_perm may 6776 * not match. We reconcile it here. 6777 */ 6778 if (aminor != NULL) 6779 minor_name = aminor; 6780 6781 if (gmatch(minor_name, mp->mp_minorname) != 0) { 6782 *uid = mp->mp_uid; 6783 *gid = mp->mp_gid; 6784 *mode = spectype | mp->mp_mode; 6785 match = TRUE; 6786 } 6787 } 6788 } 6789 6790 if (match == TRUE) { 6791 return; 6792 } 6793 6794 use_defaults: 6795 /* not found in minor_perm, so just use default values */ 6796 *uid = root_uid; 6797 *gid = sys_gid; 6798 *mode = (spectype | 0600); 6799 } 6800 6801 /* 6802 * Called by devfs_read_minor_perm() to report errors 6803 * key is: 6804 * line number: ignoring line number error 6805 * errno: open/close errors 6806 * size: alloc errors 6807 */ 6808 static void 6809 minorperm_err_cb(minorperm_err_t mp_err, int key) 6810 { 6811 switch (mp_err) { 6812 case MP_FOPEN_ERR: 6813 err_print(FOPEN_FAILED, MINOR_PERM_FILE, strerror(key)); 6814 break; 6815 case MP_FCLOSE_ERR: 6816 err_print(FCLOSE_FAILED, MINOR_PERM_FILE, strerror(key)); 6817 break; 6818 case MP_IGNORING_LINE_ERR: 6819 err_print(IGNORING_LINE_IN, key, MINOR_PERM_FILE); 6820 break; 6821 case MP_ALLOC_ERR: 6822 err_print(MALLOC_FAILED, key); 6823 break; 6824 case MP_NVLIST_ERR: 6825 err_print(NVLIST_ERROR, MINOR_PERM_FILE, strerror(key)); 6826 break; 6827 case MP_CANT_FIND_USER_ERR: 6828 err_print(CANT_FIND_USER, DEFAULT_DEV_USER); 6829 break; 6830 case MP_CANT_FIND_GROUP_ERR: 6831 err_print(CANT_FIND_GROUP, DEFAULT_DEV_GROUP); 6832 break; 6833 } 6834 } 6835 6836 static void 6837 read_minor_perm_file(void) 6838 { 6839 static int cached = FALSE; 6840 static struct stat cached_sb; 6841 struct stat current_sb; 6842 6843 (void) stat(MINOR_PERM_FILE, ¤t_sb); 6844 6845 /* If already cached, check to see if it is still valid */ 6846 if (cached == TRUE) { 6847 6848 if (current_sb.st_mtime == cached_sb.st_mtime) { 6849 vprint(FILES_MID, "%s cache valid\n", MINOR_PERM_FILE); 6850 return; 6851 } 6852 devfs_free_minor_perm(minor_perms); 6853 minor_perms = NULL; 6854 } else { 6855 cached = TRUE; 6856 } 6857 6858 (void) stat(MINOR_PERM_FILE, &cached_sb); 6859 6860 vprint(FILES_MID, "loading binding file: %s\n", MINOR_PERM_FILE); 6861 6862 minor_perms = devfs_read_minor_perm(minorperm_err_cb); 6863 } 6864 6865 static void 6866 load_minor_perm_file(void) 6867 { 6868 read_minor_perm_file(); 6869 if (devfs_load_minor_perm(minor_perms, minorperm_err_cb) != 0) 6870 err_print(gettext("minor_perm load failed\n")); 6871 } 6872 6873 static char * 6874 convert_to_re(char *dev) 6875 { 6876 char *p, *l, *out; 6877 int i; 6878 6879 out = s_malloc(PATH_MAX); 6880 6881 for (l = p = dev, i = 0; (*p != '\0') && (i < (PATH_MAX - 1)); 6882 ++p, i++) { 6883 if ((*p == '*') && ((l != p) && (*l == '/'))) { 6884 out[i++] = '.'; 6885 out[i] = '+'; 6886 } else { 6887 out[i] = *p; 6888 } 6889 l = p; 6890 } 6891 out[i] = '\0'; 6892 p = (char *)s_malloc(strlen(out) + 1); 6893 (void) strlcpy(p, out, strlen(out) + 1); 6894 free(out); 6895 6896 vprint(FILES_MID, "converted %s -> %s\n", dev, p); 6897 6898 return (p); 6899 } 6900 6901 static void 6902 read_logindevperm_file(void) 6903 { 6904 static int cached = FALSE; 6905 static struct stat cached_sb; 6906 struct stat current_sb; 6907 struct login_dev *ldev; 6908 FILE *fp; 6909 char line[MAX_LDEV_LINE]; 6910 int ln, perm, rv; 6911 char *cp, *console, *devlist, *dev; 6912 char *lasts, *devlasts, *permstr, *drv; 6913 struct driver_list *list, *next; 6914 6915 /* Read logindevperm only when enabled */ 6916 if (login_dev_enable != TRUE) 6917 return; 6918 6919 if (cached == TRUE) { 6920 if (stat(LDEV_FILE, ¤t_sb) == 0 && 6921 current_sb.st_mtime == cached_sb.st_mtime) { 6922 vprint(FILES_MID, "%s cache valid\n", LDEV_FILE); 6923 return; 6924 } 6925 vprint(FILES_MID, "invalidating %s cache\n", LDEV_FILE); 6926 while (login_dev_cache != NULL) { 6927 6928 ldev = login_dev_cache; 6929 login_dev_cache = ldev->ldev_next; 6930 free(ldev->ldev_console); 6931 free(ldev->ldev_device); 6932 regfree(&ldev->ldev_device_regex); 6933 list = ldev->ldev_driver_list; 6934 while (list) { 6935 next = list->next; 6936 free(list); 6937 list = next; 6938 } 6939 free(ldev); 6940 } 6941 } else { 6942 cached = TRUE; 6943 } 6944 6945 assert(login_dev_cache == NULL); 6946 6947 if (stat(LDEV_FILE, &cached_sb) != 0) { 6948 cached = FALSE; 6949 return; 6950 } 6951 6952 vprint(FILES_MID, "loading file: %s\n", LDEV_FILE); 6953 6954 if ((fp = fopen(LDEV_FILE, "r")) == NULL) { 6955 /* Not fatal to devfsadm */ 6956 cached = FALSE; 6957 err_print(FOPEN_FAILED, LDEV_FILE, strerror(errno)); 6958 return; 6959 } 6960 6961 ln = 0; 6962 while (fgets(line, MAX_LDEV_LINE, fp) != NULL) { 6963 ln++; 6964 6965 /* Remove comments */ 6966 if ((cp = strchr(line, '#')) != NULL) 6967 *cp = '\0'; 6968 6969 if ((console = strtok_r(line, LDEV_DELIMS, &lasts)) == NULL) 6970 continue; /* Blank line */ 6971 6972 if ((permstr = strtok_r(NULL, LDEV_DELIMS, &lasts)) == NULL) { 6973 err_print(IGNORING_LINE_IN, ln, LDEV_FILE); 6974 continue; /* Malformed line */ 6975 } 6976 6977 /* 6978 * permstr is string in octal format. Convert to int 6979 */ 6980 cp = NULL; 6981 errno = 0; 6982 perm = strtol(permstr, &cp, 8); 6983 if (errno || perm < 0 || perm > 0777 || *cp != '\0') { 6984 err_print(IGNORING_LINE_IN, ln, LDEV_FILE); 6985 continue; 6986 } 6987 6988 if ((devlist = strtok_r(NULL, LDEV_DELIMS, &lasts)) == NULL) { 6989 err_print(IGNORING_LINE_IN, ln, LDEV_FILE); 6990 continue; 6991 } 6992 6993 dev = strtok_r(devlist, LDEV_DEV_DELIM, &devlasts); 6994 while (dev) { 6995 6996 ldev = (struct login_dev *)s_zalloc( 6997 sizeof (struct login_dev)); 6998 ldev->ldev_console = s_strdup(console); 6999 ldev->ldev_perms = perm; 7000 7001 /* 7002 * the logical device name may contain '*' which 7003 * we convert to a regular expression 7004 */ 7005 ldev->ldev_device = convert_to_re(dev); 7006 if (ldev->ldev_device && 7007 (rv = regcomp(&ldev->ldev_device_regex, 7008 ldev->ldev_device, REG_EXTENDED))) { 7009 bzero(&ldev->ldev_device_regex, 7010 sizeof (ldev->ldev_device_regex)); 7011 err_print(REGCOMP_FAILED, 7012 ldev->ldev_device, rv); 7013 } 7014 ldev->ldev_next = login_dev_cache; 7015 login_dev_cache = ldev; 7016 dev = strtok_r(NULL, LDEV_DEV_DELIM, &devlasts); 7017 } 7018 7019 drv = strtok_r(NULL, LDEV_DRVLIST_DELIMS, &lasts); 7020 if (drv) { 7021 if (strcmp(drv, LDEV_DRVLIST_NAME) == 0) { 7022 7023 drv = strtok_r(NULL, LDEV_DRV_DELIMS, 7024 &lasts); 7025 7026 while (drv) { 7027 vprint(FILES_MID, 7028 "logindevperm driver=%s\n", 7029 drv); 7030 7031 /* 7032 * create a linked list of driver 7033 * names 7034 */ 7035 list = (struct driver_list *) 7036 s_zalloc( 7037 sizeof (struct driver_list)); 7038 (void) strlcpy(list->driver_name, drv, 7039 sizeof (list->driver_name)); 7040 list->next = ldev->ldev_driver_list; 7041 ldev->ldev_driver_list = list; 7042 drv = strtok_r(NULL, LDEV_DRV_DELIMS, 7043 &lasts); 7044 } 7045 } 7046 } 7047 } 7048 (void) fclose(fp); 7049 } 7050 7051 /* 7052 * Tokens are separated by ' ', '\t', ':', '=', '&', '|', ';', '\n', or '\0' 7053 * 7054 * Returns DEVFSADM_SUCCESS if token found, DEVFSADM_FAILURE otherwise. 7055 */ 7056 static int 7057 getnexttoken(char *next, char **nextp, char **tokenpp, char *tchar) 7058 { 7059 char *cp; 7060 char *cp1; 7061 char *tokenp; 7062 7063 cp = next; 7064 while (*cp == ' ' || *cp == '\t') { 7065 cp++; /* skip leading spaces */ 7066 } 7067 tokenp = cp; /* start of token */ 7068 while (*cp != '\0' && *cp != '\n' && *cp != ' ' && *cp != '\t' && 7069 *cp != ':' && *cp != '=' && *cp != '&' && 7070 *cp != '|' && *cp != ';') { 7071 cp++; /* point to next character */ 7072 } 7073 /* 7074 * If terminating character is a space or tab, look ahead to see if 7075 * there's another terminator that's not a space or a tab. 7076 * (This code handles trailing spaces.) 7077 */ 7078 if (*cp == ' ' || *cp == '\t') { 7079 cp1 = cp; 7080 while (*++cp1 == ' ' || *cp1 == '\t') 7081 ; 7082 if (*cp1 == '=' || *cp1 == ':' || *cp1 == '&' || *cp1 == '|' || 7083 *cp1 == ';' || *cp1 == '\n' || *cp1 == '\0') { 7084 *cp = NULL; /* terminate token */ 7085 cp = cp1; 7086 } 7087 } 7088 if (tchar != NULL) { 7089 *tchar = *cp; /* save terminating character */ 7090 if (*tchar == '\0') { 7091 *tchar = '\n'; 7092 } 7093 } 7094 *cp++ = '\0'; /* terminate token, point to next */ 7095 *nextp = cp; /* set pointer to next character */ 7096 if (cp - tokenp - 1 == 0) { 7097 return (DEVFSADM_FAILURE); 7098 } 7099 *tokenpp = tokenp; 7100 return (DEVFSADM_SUCCESS); 7101 } 7102 7103 /* 7104 * read or reread the driver aliases file 7105 */ 7106 static void 7107 read_driver_aliases_file(void) 7108 { 7109 7110 driver_alias_t *save; 7111 driver_alias_t *lst_tail; 7112 driver_alias_t *ap; 7113 static int cached = FALSE; 7114 FILE *afd; 7115 char line[256]; 7116 char *cp; 7117 char *p; 7118 char t; 7119 int ln = 0; 7120 static struct stat cached_sb; 7121 struct stat current_sb; 7122 7123 (void) stat(ALIASFILE, ¤t_sb); 7124 7125 /* If already cached, check to see if it is still valid */ 7126 if (cached == TRUE) { 7127 7128 if (current_sb.st_mtime == cached_sb.st_mtime) { 7129 vprint(FILES_MID, "%s cache valid\n", ALIASFILE); 7130 return; 7131 } 7132 7133 vprint(FILES_MID, "invalidating %s cache\n", ALIASFILE); 7134 while (driver_aliases != NULL) { 7135 free(driver_aliases->alias_name); 7136 free(driver_aliases->driver_name); 7137 save = driver_aliases; 7138 driver_aliases = driver_aliases->next; 7139 free(save); 7140 } 7141 } else { 7142 cached = TRUE; 7143 } 7144 7145 (void) stat(ALIASFILE, &cached_sb); 7146 7147 vprint(FILES_MID, "loading binding file: %s\n", ALIASFILE); 7148 7149 if ((afd = fopen(ALIASFILE, "r")) == NULL) { 7150 err_print(FOPEN_FAILED, ALIASFILE, strerror(errno)); 7151 devfsadm_exit(1); 7152 } 7153 7154 while (fgets(line, sizeof (line), afd) != NULL) { 7155 ln++; 7156 /* cut off comments starting with '#' */ 7157 if ((cp = strchr(line, '#')) != NULL) 7158 *cp = '\0'; 7159 /* ignore comment or blank lines */ 7160 if (is_blank(line)) 7161 continue; 7162 cp = line; 7163 if (getnexttoken(cp, &cp, &p, &t) == DEVFSADM_FAILURE) { 7164 err_print(IGNORING_LINE_IN, ln, ALIASFILE); 7165 continue; 7166 } 7167 if (t == '\n' || t == '\0') { 7168 err_print(DRV_BUT_NO_ALIAS, ln, ALIASFILE); 7169 continue; 7170 } 7171 ap = (struct driver_alias *) 7172 s_zalloc(sizeof (struct driver_alias)); 7173 ap->driver_name = s_strdup(p); 7174 if (getnexttoken(cp, &cp, &p, &t) == DEVFSADM_FAILURE) { 7175 err_print(DRV_BUT_NO_ALIAS, ln, ALIASFILE); 7176 free(ap->driver_name); 7177 free(ap); 7178 continue; 7179 } 7180 if (*p == '"') { 7181 if (p[strlen(p) - 1] == '"') { 7182 p[strlen(p) - 1] = '\0'; 7183 p++; 7184 } 7185 } 7186 ap->alias_name = s_strdup(p); 7187 if (driver_aliases == NULL) { 7188 driver_aliases = ap; 7189 lst_tail = ap; 7190 } else { 7191 lst_tail->next = ap; 7192 lst_tail = ap; 7193 } 7194 } 7195 if (fclose(afd) == EOF) { 7196 err_print(FCLOSE_FAILED, ALIASFILE, strerror(errno)); 7197 } 7198 } 7199 7200 /* 7201 * return TRUE if alias_name is an alias for driver_name, otherwise 7202 * return FALSE. 7203 */ 7204 static int 7205 alias(char *driver_name, char *alias_name) 7206 { 7207 driver_alias_t *alias; 7208 7209 /* 7210 * check for a match 7211 */ 7212 for (alias = driver_aliases; alias != NULL; alias = alias->next) { 7213 if ((strcmp(alias->driver_name, driver_name) == 0) && 7214 (strcmp(alias->alias_name, alias_name) == 0)) { 7215 return (TRUE); 7216 } 7217 } 7218 return (FALSE); 7219 } 7220 7221 /* 7222 * convenience functions 7223 */ 7224 static int 7225 s_stat(const char *path, struct stat *sbufp) 7226 { 7227 int rv; 7228 retry: 7229 if ((rv = stat(path, sbufp)) == -1) { 7230 if (errno == EINTR) 7231 goto retry; 7232 } 7233 return (rv); 7234 } 7235 7236 static void * 7237 s_malloc(const size_t size) 7238 { 7239 void *rp; 7240 7241 rp = malloc(size); 7242 if (rp == NULL) { 7243 err_print(MALLOC_FAILED, size); 7244 devfsadm_exit(1); 7245 } 7246 return (rp); 7247 } 7248 7249 /* 7250 * convenience functions 7251 */ 7252 static void * 7253 s_realloc(void *ptr, const size_t size) 7254 { 7255 ptr = realloc(ptr, size); 7256 if (ptr == NULL) { 7257 err_print(REALLOC_FAILED, size); 7258 devfsadm_exit(1); 7259 } 7260 return (ptr); 7261 } 7262 7263 static void * 7264 s_zalloc(const size_t size) 7265 { 7266 void *rp; 7267 7268 rp = calloc(1, size); 7269 if (rp == NULL) { 7270 err_print(CALLOC_FAILED, size); 7271 devfsadm_exit(1); 7272 } 7273 return (rp); 7274 } 7275 7276 char * 7277 s_strdup(const char *ptr) 7278 { 7279 void *rp; 7280 7281 rp = strdup(ptr); 7282 if (rp == NULL) { 7283 err_print(STRDUP_FAILED, ptr); 7284 devfsadm_exit(1); 7285 } 7286 return (rp); 7287 } 7288 7289 static void 7290 s_closedir(DIR *dirp) 7291 { 7292 retry: 7293 if (closedir(dirp) != 0) { 7294 if (errno == EINTR) 7295 goto retry; 7296 err_print(CLOSEDIR_FAILED, strerror(errno)); 7297 } 7298 } 7299 7300 static void 7301 s_mkdirp(const char *path, const mode_t mode) 7302 { 7303 vprint(CHATTY_MID, "mkdirp(%s, 0x%lx)\n", path, mode); 7304 if (mkdirp(path, mode) == -1) { 7305 if (errno != EEXIST) { 7306 err_print(MKDIR_FAILED, path, mode, strerror(errno)); 7307 } 7308 } 7309 } 7310 7311 static void 7312 s_unlink(const char *file) 7313 { 7314 retry: 7315 if (unlink(file) == -1) { 7316 if (errno == EINTR || errno == EAGAIN) 7317 goto retry; 7318 if (errno != ENOENT) { 7319 err_print(UNLINK_FAILED, file, strerror(errno)); 7320 } 7321 } 7322 } 7323 7324 static void 7325 add_verbose_id(char *mid) 7326 { 7327 num_verbose++; 7328 verbose = s_realloc(verbose, num_verbose * sizeof (char *)); 7329 verbose[num_verbose - 1] = mid; 7330 } 7331 7332 /* 7333 * returns DEVFSADM_TRUE if contents is a minor node in /devices. 7334 * If mn_root is not NULL, mn_root is set to: 7335 * if contents is a /dev node, mn_root = contents 7336 * OR 7337 * if contents is a /devices node, mn_root set to the '/' 7338 * following /devices. 7339 */ 7340 static int 7341 is_minor_node(char *contents, char **mn_root) 7342 { 7343 char *ptr; 7344 char device_prefix[100]; 7345 7346 (void) snprintf(device_prefix, sizeof (device_prefix), "../devices/"); 7347 7348 if ((ptr = strstr(contents, device_prefix)) != NULL) { 7349 if (mn_root != NULL) { 7350 /* mn_root should point to the / following /devices */ 7351 *mn_root = ptr += strlen(device_prefix) - 1; 7352 } 7353 return (DEVFSADM_TRUE); 7354 } 7355 7356 (void) snprintf(device_prefix, sizeof (device_prefix), "/devices/"); 7357 7358 if (strncmp(contents, device_prefix, strlen(device_prefix)) == 0) { 7359 if (mn_root != NULL) { 7360 /* mn_root should point to the / following /devices */ 7361 *mn_root = contents + strlen(device_prefix) - 1; 7362 } 7363 return (DEVFSADM_TRUE); 7364 } 7365 7366 if (mn_root != NULL) { 7367 *mn_root = contents; 7368 } 7369 return (DEVFSADM_FALSE); 7370 } 7371 7372 /* 7373 * Lookup nvpair corresponding to the given name and type: 7374 * 7375 * The standard nvlist_lookup functions in libnvpair don't work as our 7376 * nvlist is not allocated with NV_UNIQUE_NAME or NV_UNIQUE_NAME_TYPE. 7377 */ 7378 static nvpair_t * 7379 lookup_nvpair(nvlist_t *nvl, char *name, data_type_t type) 7380 { 7381 nvpair_t *nvp; 7382 7383 for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL; 7384 nvp = nvlist_next_nvpair(nvl, nvp)) { 7385 if (strcmp(name, nvpair_name(nvp)) == 0 && 7386 nvpair_type(nvp) == type) 7387 return (nvp); 7388 } 7389 7390 return (NULL); 7391 } 7392 7393 /*ARGSUSED*/ 7394 static void 7395 process_rcm_events(void *arg) 7396 { 7397 struct rcm_eventq *ev, *ev_next; 7398 nvpair_t *nvp; 7399 char *path, *driver; 7400 int instance; 7401 int err; 7402 int need_to_exit; 7403 7404 for (;;) { 7405 (void) mutex_lock(&rcm_eventq_lock); 7406 while (rcm_eventq_head == NULL && 7407 need_to_exit_rcm_event_thread == 0) 7408 (void) cond_wait(&rcm_eventq_cv, &rcm_eventq_lock); 7409 7410 need_to_exit = need_to_exit_rcm_event_thread; 7411 ev = rcm_eventq_head; 7412 rcm_eventq_head = rcm_eventq_tail = NULL; 7413 (void) mutex_unlock(&rcm_eventq_lock); 7414 7415 for (; ev != NULL; ev = ev_next) { 7416 /* 7417 * Private notification interface to RCM: 7418 * Do not retry the RCM notification on an error since 7419 * we do not know whether the failure occurred in 7420 * librcm, rcm_daemon or rcm modules or scripts. 7421 */ 7422 if (librcm_notify_event(rcm_hdl, 7423 RCM_RESOURCE_NETWORK_NEW, 0, ev->nvl, NULL) 7424 != RCM_SUCCESS) { 7425 7426 err = errno; 7427 7428 if (((nvp = lookup_nvpair(ev->nvl, 7429 RCM_NV_DEVFS_PATH, DATA_TYPE_STRING)) 7430 == NULL) || 7431 (nvpair_value_string(nvp, &path) != 0)) 7432 path = "unknown"; 7433 7434 if (((nvp = lookup_nvpair(ev->nvl, 7435 RCM_NV_DRIVER_NAME, DATA_TYPE_STRING)) 7436 == NULL) || 7437 (nvpair_value_string(nvp, &driver) != 0)) 7438 driver = "unknown"; 7439 if (((nvp = lookup_nvpair(ev->nvl, 7440 RCM_NV_INSTANCE, DATA_TYPE_INT32)) 7441 == NULL) || 7442 (nvpair_value_int32(nvp, &instance) != 0)) 7443 instance = -1; 7444 7445 err_print(RCM_NOTIFY_FAILED, path, driver, 7446 instance, strerror(err)); 7447 } 7448 7449 ev_next = ev->next; 7450 nvlist_free(ev->nvl); 7451 free(ev); 7452 } 7453 7454 if (need_to_exit) 7455 return; 7456 } 7457 } 7458 7459 /* 7460 * Initialize rcm related handles and function pointers. 7461 * Since RCM need not present in miniroot, we dlopen librcm. 7462 */ 7463 static int 7464 rcm_init(void) 7465 { 7466 #define LIBRCM_PATH "/usr/lib/librcm.so" 7467 rcm_handle_t *hdl = NULL; 7468 int err; 7469 7470 if ((librcm_hdl = dlopen(LIBRCM_PATH, RTLD_LAZY)) == NULL) { 7471 /* 7472 * don't log an error here, since librcm may not be present 7473 * in miniroot. 7474 */ 7475 return (-1); 7476 } 7477 7478 librcm_alloc_handle = (int (*)())dlsym(librcm_hdl, "rcm_alloc_handle"); 7479 librcm_free_handle = (void (*)())dlsym(librcm_hdl, "rcm_free_handle"); 7480 librcm_notify_event = (int (*)())dlsym(librcm_hdl, "rcm_notify_event"); 7481 7482 if (librcm_alloc_handle == NULL || librcm_notify_event == NULL || 7483 librcm_free_handle == NULL) { 7484 err_print(MISSING_SYMBOLS, LIBRCM_PATH); 7485 goto out; 7486 } 7487 7488 /* Initialize the rcm handle */ 7489 if (librcm_alloc_handle(NULL, 0, NULL, &hdl) != RCM_SUCCESS) { 7490 err_print(RCM_ALLOC_HANDLE_ERROR); 7491 goto out; 7492 } 7493 7494 (void) cond_init(&rcm_eventq_cv, USYNC_THREAD, 0); 7495 (void) mutex_init(&rcm_eventq_lock, USYNC_THREAD, 0); 7496 7497 /* create a thread to notify RCM of events */ 7498 if ((err = thr_create(NULL, 0, (void *(*)(void *))process_rcm_events, 7499 NULL, 0, &process_rcm_events_tid)) != 0) { 7500 err_print(CANT_CREATE_THREAD, "process_rcm_events", 7501 strerror(err)); 7502 goto out; 7503 } 7504 7505 rcm_hdl = hdl; 7506 return (0); 7507 7508 out: 7509 if (hdl) 7510 librcm_free_handle(hdl); 7511 (void) dlclose(librcm_hdl); 7512 return (-1); 7513 } 7514 7515 /* 7516 * Build an nvlist using the minor data. Pack it and add the packed nvlist 7517 * as a byte array to nv_list parameter. 7518 * Return 0 on success, errno on failure. 7519 */ 7520 static int 7521 add_minor_data_to_nvl(nvlist_t *nv_list, di_minor_t minor) 7522 { 7523 nvlist_t *nvl = NULL; 7524 int32_t minor_type; 7525 char *minor_name, *minor_node_type; 7526 int err; 7527 char *buf = NULL; 7528 size_t buflen = 0; 7529 7530 if ((err = nvlist_alloc(&nvl, 0, 0)) != 0) 7531 return (err); 7532 7533 minor_type = (int32_t)di_minor_type(minor); 7534 if ((err = nvlist_add_int32(nvl, RCM_NV_MINOR_TYPE, minor_type)) != 0) 7535 goto error; 7536 7537 minor_name = di_minor_name(minor); 7538 if ((err = nvlist_add_string(nvl, RCM_NV_MINOR_NAME, minor_name)) != 0) 7539 goto error; 7540 7541 if ((minor_node_type = di_minor_nodetype(minor)) == NULL) 7542 minor_node_type = ""; 7543 if ((err = nvlist_add_string(nvl, RCM_NV_MINOR_NODE_TYPE, 7544 minor_node_type)) != 0) 7545 goto error; 7546 7547 if ((err = nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_NATIVE, 0)) != 0) 7548 goto error; 7549 7550 err = nvlist_add_byte_array(nv_list, RCM_NV_MINOR_DATA, 7551 (uchar_t *)(buf), (uint_t)(buflen)); 7552 7553 error: 7554 nvlist_free(nvl); 7555 if (buf) 7556 free(buf); 7557 return (err); 7558 } 7559 7560 static void 7561 enqueue_rcm_event(nvlist_t *nvl) 7562 { 7563 struct rcm_eventq *ev; 7564 7565 ev = (struct rcm_eventq *)s_zalloc(sizeof (struct rcm_eventq)); 7566 ev->nvl = nvl; 7567 7568 (void) mutex_lock(&rcm_eventq_lock); 7569 if (rcm_eventq_head == NULL) 7570 rcm_eventq_head = ev; 7571 else 7572 rcm_eventq_tail->next = ev; 7573 rcm_eventq_tail = ev; 7574 (void) cond_broadcast(&rcm_eventq_cv); 7575 (void) mutex_unlock(&rcm_eventq_lock); 7576 } 7577 7578 /* 7579 * Generate an nvlist using the information given in node and minor_name. 7580 * If minor_name is NULL the nvlist will contain information on 7581 * all minor nodes. Otherwise the nvlist will contain information 7582 * only on the given minor_name. Notify RCM passing the nvlist. 7583 * 7584 * Return 0 upon successfully notifying RCM, errno on failure. 7585 */ 7586 static int 7587 notify_rcm(di_node_t node, char *minor_name) 7588 { 7589 nvlist_t *nvl = NULL; 7590 char *path, *driver_name; 7591 char *node_name; 7592 int err; 7593 int32_t instance; 7594 di_minor_t minor; 7595 7596 if ((driver_name = di_driver_name(node)) == NULL) 7597 driver_name = ""; 7598 7599 instance = (int32_t)di_instance(node); 7600 7601 if ((path = di_devfs_path(node)) == NULL) { 7602 err = errno; 7603 goto error; 7604 } 7605 7606 if ((err = nvlist_alloc(&nvl, 0, 0)) != 0) 7607 goto error; 7608 7609 if ((err = nvlist_add_string(nvl, RCM_NV_DRIVER_NAME, driver_name)) 7610 != 0) 7611 goto error; 7612 7613 if ((err = nvlist_add_int32(nvl, RCM_NV_INSTANCE, instance)) != 0) 7614 goto error; 7615 7616 if ((node_name = di_node_name(node)) == NULL) 7617 node_name = ""; 7618 if ((err = nvlist_add_string(nvl, RCM_NV_NODE_NAME, node_name)) != 0) 7619 goto error; 7620 7621 if ((err = nvlist_add_string(nvl, RCM_NV_DEVFS_PATH, path)) != 0) 7622 goto error; 7623 7624 minor = di_minor_next(node, DI_MINOR_NIL); 7625 while (minor != DI_MINOR_NIL) { 7626 if ((minor_name == NULL) || 7627 (strcmp(minor_name, di_minor_name(minor)) == 0)) { 7628 if ((err = add_minor_data_to_nvl(nvl, minor)) != 0) 7629 goto error; 7630 } 7631 minor = di_minor_next(node, minor); 7632 } 7633 7634 enqueue_rcm_event(nvl); 7635 di_devfs_path_free(path); 7636 return (0); 7637 7638 error: 7639 err_print(RCM_NVLIST_BUILD_ERROR, ((path != NULL) ? path : "unknown"), 7640 driver_name, instance, strerror(err)); 7641 7642 if (path) 7643 di_devfs_path_free(path); 7644 if (nvl) 7645 nvlist_free(nvl); 7646 return (err); 7647 } 7648 7649 /* 7650 * Add the specified property to nvl. 7651 * Returns: 7652 * 0 successfully added 7653 * -1 an error occurred 7654 * 1 could not add the property for reasons not due to errors. 7655 */ 7656 static int 7657 add_property(nvlist_t *nvl, di_prop_t prop) 7658 { 7659 char *name; 7660 char *attr_name; 7661 int n, len; 7662 int32_t *int32p; 7663 int64_t *int64p; 7664 char *str; 7665 char **strarray; 7666 uchar_t *bytep; 7667 int rv = 0; 7668 int i; 7669 7670 if ((name = di_prop_name(prop)) == NULL) 7671 return (-1); 7672 7673 len = sizeof (DEV_PROP_PREFIX) + strlen(name); 7674 if ((attr_name = malloc(len)) == NULL) 7675 return (-1); 7676 7677 (void) strlcpy(attr_name, DEV_PROP_PREFIX, len); 7678 (void) strlcat(attr_name, name, len); 7679 7680 switch (di_prop_type(prop)) { 7681 case DI_PROP_TYPE_BOOLEAN: 7682 if (nvlist_add_boolean(nvl, attr_name) != 0) 7683 goto out; 7684 break; 7685 7686 case DI_PROP_TYPE_INT: 7687 if ((n = di_prop_ints(prop, &int32p)) < 1) 7688 goto out; 7689 7690 if (n <= (PROP_LEN_LIMIT / sizeof (int32_t))) { 7691 if (nvlist_add_int32_array(nvl, attr_name, int32p, 7692 n) != 0) 7693 goto out; 7694 } else 7695 rv = 1; 7696 break; 7697 7698 case DI_PROP_TYPE_INT64: 7699 if ((n = di_prop_int64(prop, &int64p)) < 1) 7700 goto out; 7701 7702 if (n <= (PROP_LEN_LIMIT / sizeof (int64_t))) { 7703 if (nvlist_add_int64_array(nvl, attr_name, int64p, 7704 n) != 0) 7705 goto out; 7706 } else 7707 rv = 1; 7708 break; 7709 7710 case DI_PROP_TYPE_BYTE: 7711 case DI_PROP_TYPE_UNKNOWN: 7712 if ((n = di_prop_bytes(prop, &bytep)) < 1) 7713 goto out; 7714 7715 if (n <= PROP_LEN_LIMIT) { 7716 if (nvlist_add_byte_array(nvl, attr_name, bytep, n) 7717 != 0) 7718 goto out; 7719 } else 7720 rv = 1; 7721 break; 7722 7723 case DI_PROP_TYPE_STRING: 7724 if ((n = di_prop_strings(prop, &str)) < 1) 7725 goto out; 7726 7727 if ((strarray = malloc(n * sizeof (char *))) == NULL) 7728 goto out; 7729 7730 len = 0; 7731 for (i = 0; i < n; i++) { 7732 strarray[i] = str + len; 7733 len += strlen(strarray[i]) + 1; 7734 } 7735 7736 if (len <= PROP_LEN_LIMIT) { 7737 if (nvlist_add_string_array(nvl, attr_name, strarray, 7738 n) != 0) { 7739 free(strarray); 7740 goto out; 7741 } 7742 } else 7743 rv = 1; 7744 free(strarray); 7745 break; 7746 7747 default: 7748 rv = 1; 7749 break; 7750 } 7751 7752 free(attr_name); 7753 return (rv); 7754 7755 out: 7756 free(attr_name); 7757 return (-1); 7758 } 7759 7760 static void 7761 free_dev_names(struct devlink_cb_arg *x) 7762 { 7763 int i; 7764 7765 for (i = 0; i < x->count; i++) { 7766 free(x->dev_names[i]); 7767 free(x->link_contents[i]); 7768 } 7769 } 7770 7771 /* callback function for di_devlink_cache_walk */ 7772 static int 7773 devlink_cb(di_devlink_t dl, void *arg) 7774 { 7775 struct devlink_cb_arg *x = (struct devlink_cb_arg *)arg; 7776 const char *path; 7777 const char *content; 7778 7779 if ((path = di_devlink_path(dl)) == NULL || 7780 (content = di_devlink_content(dl)) == NULL || 7781 (x->dev_names[x->count] = s_strdup(path)) == NULL) 7782 goto out; 7783 7784 if ((x->link_contents[x->count] = s_strdup(content)) == NULL) { 7785 free(x->dev_names[x->count]); 7786 goto out; 7787 } 7788 7789 x->count++; 7790 if (x->count >= MAX_DEV_NAME_COUNT) 7791 return (DI_WALK_TERMINATE); 7792 7793 return (DI_WALK_CONTINUE); 7794 7795 out: 7796 x->rv = -1; 7797 free_dev_names(x); 7798 return (DI_WALK_TERMINATE); 7799 } 7800 7801 /* 7802 * Lookup dev name corresponding to the phys_path. 7803 * phys_path is path to a node or minor node. 7804 * Returns: 7805 * 0 with *dev_name set to the dev name 7806 * Lookup succeeded and dev_name found 7807 * 0 with *dev_name set to NULL 7808 * Lookup encountered no errors but dev name not found 7809 * -1 7810 * Lookup failed 7811 */ 7812 static int 7813 lookup_dev_name(char *phys_path, char **dev_name) 7814 { 7815 struct devlink_cb_arg cb_arg; 7816 7817 *dev_name = NULL; 7818 7819 cb_arg.count = 0; 7820 cb_arg.rv = 0; 7821 (void) di_devlink_cache_walk(devlink_cache, NULL, phys_path, 7822 DI_PRIMARY_LINK, &cb_arg, devlink_cb); 7823 7824 if (cb_arg.rv == -1) 7825 return (-1); 7826 7827 if (cb_arg.count > 0) { 7828 *dev_name = s_strdup(cb_arg.dev_names[0]); 7829 free_dev_names(&cb_arg); 7830 if (*dev_name == NULL) 7831 return (-1); 7832 } 7833 7834 return (0); 7835 } 7836 7837 static char * 7838 lookup_disk_dev_name(char *node_path) 7839 { 7840 struct devlink_cb_arg cb_arg; 7841 char *dev_name = NULL; 7842 int i; 7843 char *p; 7844 int len1, len2; 7845 7846 #define DEV_RDSK "/dev/rdsk/" 7847 #define DISK_RAW_MINOR ",raw" 7848 7849 cb_arg.count = 0; 7850 cb_arg.rv = 0; 7851 (void) di_devlink_cache_walk(devlink_cache, NULL, node_path, 7852 DI_PRIMARY_LINK, &cb_arg, devlink_cb); 7853 7854 if (cb_arg.rv == -1 || cb_arg.count == 0) 7855 return (NULL); 7856 7857 /* first try lookup based on /dev/rdsk name */ 7858 for (i = 0; i < cb_arg.count; i++) { 7859 if (strncmp(cb_arg.dev_names[i], DEV_RDSK, 7860 sizeof (DEV_RDSK) - 1) == 0) { 7861 dev_name = s_strdup(cb_arg.dev_names[i]); 7862 break; 7863 } 7864 } 7865 7866 if (dev_name == NULL) { 7867 /* now try lookup based on a minor name ending with ",raw" */ 7868 len1 = sizeof (DISK_RAW_MINOR) - 1; 7869 for (i = 0; i < cb_arg.count; i++) { 7870 len2 = strlen(cb_arg.link_contents[i]); 7871 if (len2 >= len1 && 7872 strcmp(cb_arg.link_contents[i] + len2 - len1, 7873 DISK_RAW_MINOR) == 0) { 7874 dev_name = s_strdup(cb_arg.dev_names[i]); 7875 break; 7876 } 7877 } 7878 } 7879 7880 free_dev_names(&cb_arg); 7881 7882 if (dev_name == NULL) 7883 return (NULL); 7884 if (strlen(dev_name) == 0) { 7885 free(dev_name); 7886 return (NULL); 7887 } 7888 7889 /* if the name contains slice or partition number strip it */ 7890 p = dev_name + strlen(dev_name) - 1; 7891 if (isdigit(*p)) { 7892 while (p != dev_name && isdigit(*p)) 7893 p--; 7894 if (*p == 's' || *p == 'p') 7895 *p = '\0'; 7896 } 7897 7898 return (dev_name); 7899 } 7900 7901 static char * 7902 lookup_network_dev_name(char *node_path, char *driver_name) 7903 { 7904 char *dev_name = NULL; 7905 char phys_path[MAXPATHLEN]; 7906 7907 if (lookup_dev_name(node_path, &dev_name) == -1) 7908 return (NULL); 7909 7910 if (dev_name == NULL) { 7911 /* dlpi style-2 only interface */ 7912 (void) snprintf(phys_path, sizeof (phys_path), 7913 "/pseudo/clone@0:%s", driver_name); 7914 if (lookup_dev_name(phys_path, &dev_name) == -1 || 7915 dev_name == NULL) 7916 return (NULL); 7917 } 7918 7919 return (dev_name); 7920 } 7921 7922 static char * 7923 lookup_printer_dev_name(char *node_path) 7924 { 7925 struct devlink_cb_arg cb_arg; 7926 char *dev_name = NULL; 7927 int i; 7928 7929 #define DEV_PRINTERS "/dev/printers/" 7930 7931 cb_arg.count = 0; 7932 cb_arg.rv = 0; 7933 (void) di_devlink_cache_walk(devlink_cache, NULL, node_path, 7934 DI_PRIMARY_LINK, &cb_arg, devlink_cb); 7935 7936 if (cb_arg.rv == -1 || cb_arg.count == 0) 7937 return (NULL); 7938 7939 /* first try lookup based on /dev/printers name */ 7940 for (i = 0; i < cb_arg.count; i++) { 7941 if (strncmp(cb_arg.dev_names[i], DEV_PRINTERS, 7942 sizeof (DEV_PRINTERS) - 1) == 0) { 7943 dev_name = s_strdup(cb_arg.dev_names[i]); 7944 break; 7945 } 7946 } 7947 7948 /* fallback to the first name */ 7949 if ((dev_name == NULL) && (cb_arg.count > 0)) 7950 dev_name = s_strdup(cb_arg.dev_names[0]); 7951 7952 free_dev_names(&cb_arg); 7953 7954 return (dev_name); 7955 } 7956 7957 /* 7958 * Build an nvlist containing all attributes for devfs events. 7959 * Returns nvlist pointer on success, NULL on failure. 7960 */ 7961 static nvlist_t * 7962 build_event_attributes(char *class, char *subclass, char *node_path, 7963 di_node_t node, char *driver_name, int instance) 7964 { 7965 nvlist_t *nvl; 7966 int err = 0; 7967 di_prop_t prop; 7968 int count; 7969 char *prop_name; 7970 int x; 7971 char *dev_name = NULL; 7972 int dev_name_lookup_err = 0; 7973 7974 if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, 0)) != 0) { 7975 nvl = NULL; 7976 goto out; 7977 } 7978 7979 if ((err = nvlist_add_int32(nvl, EV_VERSION, EV_V1)) != 0) 7980 goto out; 7981 7982 if ((err = nvlist_add_string(nvl, DEV_PHYS_PATH, node_path)) != 0) 7983 goto out; 7984 7985 if (strcmp(class, EC_DEV_ADD) != 0 && 7986 strcmp(class, EC_DEV_REMOVE) != 0) 7987 return (nvl); 7988 7989 if (driver_name == NULL || instance == -1) 7990 goto out; 7991 7992 if (strcmp(subclass, ESC_DISK) == 0) { 7993 if ((dev_name = lookup_disk_dev_name(node_path)) == NULL) { 7994 dev_name_lookup_err = 1; 7995 goto out; 7996 } 7997 } else if (strcmp(subclass, ESC_NETWORK) == 0) { 7998 if ((dev_name = lookup_network_dev_name(node_path, driver_name)) 7999 == NULL) { 8000 dev_name_lookup_err = 1; 8001 goto out; 8002 } 8003 } else if (strcmp(subclass, ESC_PRINTER) == 0) { 8004 if ((dev_name = lookup_printer_dev_name(node_path)) == NULL) { 8005 dev_name_lookup_err = 1; 8006 goto out; 8007 } 8008 } 8009 8010 if (dev_name) { 8011 if ((err = nvlist_add_string(nvl, DEV_NAME, dev_name)) != 0) 8012 goto out; 8013 free(dev_name); 8014 dev_name = NULL; 8015 } 8016 8017 if ((err = nvlist_add_string(nvl, DEV_DRIVER_NAME, driver_name)) != 0) 8018 goto out; 8019 8020 if ((err = nvlist_add_int32(nvl, DEV_INSTANCE, instance)) != 0) 8021 goto out; 8022 8023 if (strcmp(class, EC_DEV_ADD) == 0) { 8024 /* add properties */ 8025 count = 0; 8026 for (prop = di_prop_next(node, DI_PROP_NIL); 8027 prop != DI_PROP_NIL && count < MAX_PROP_COUNT; 8028 prop = di_prop_next(node, prop)) { 8029 8030 if (di_prop_devt(prop) != DDI_DEV_T_NONE) 8031 continue; 8032 8033 if ((x = add_property(nvl, prop)) == 0) 8034 count++; 8035 else if (x == -1) { 8036 if ((prop_name = di_prop_name(prop)) == NULL) 8037 prop_name = ""; 8038 err_print(PROP_ADD_FAILED, prop_name); 8039 goto out; 8040 } 8041 } 8042 } 8043 8044 return (nvl); 8045 8046 out: 8047 if (nvl) 8048 nvlist_free(nvl); 8049 8050 if (dev_name) 8051 free(dev_name); 8052 8053 if (dev_name_lookup_err) 8054 err_print(DEV_NAME_LOOKUP_FAILED, node_path); 8055 else 8056 err_print(BUILD_EVENT_ATTR_FAILED, (err) ? strerror(err) : ""); 8057 return (NULL); 8058 } 8059 8060 static void 8061 log_event(char *class, char *subclass, nvlist_t *nvl) 8062 { 8063 sysevent_id_t eid; 8064 8065 if (sysevent_post_event(class, subclass, "SUNW", DEVFSADMD, 8066 nvl, &eid) != 0) { 8067 err_print(LOG_EVENT_FAILED, strerror(errno)); 8068 } 8069 } 8070 8071 /* 8072 * When devfsadmd needs to generate sysevents, they are queued for later 8073 * delivery this allows them to be delivered after the devlinks db cache has 8074 * been flushed guaranteeing that applications consuming these events have 8075 * access to an accurate devlinks db. The queue is a FIFO, sysevents to be 8076 * inserted in the front of the queue and consumed off the back. 8077 */ 8078 static void 8079 enqueue_sysevent(char *class, char *subclass, nvlist_t *nvl) 8080 { 8081 syseventq_t *tmp; 8082 8083 if ((tmp = s_zalloc(sizeof (*tmp))) == NULL) 8084 return; 8085 8086 tmp->class = s_strdup(class); 8087 tmp->subclass = s_strdup(subclass); 8088 tmp->nvl = nvl; 8089 8090 (void) mutex_lock(&syseventq_mutex); 8091 if (syseventq_front != NULL) 8092 syseventq_front->next = tmp; 8093 else 8094 syseventq_back = tmp; 8095 syseventq_front = tmp; 8096 (void) mutex_unlock(&syseventq_mutex); 8097 } 8098 8099 static void 8100 process_syseventq() 8101 { 8102 (void) mutex_lock(&syseventq_mutex); 8103 while (syseventq_back != NULL) { 8104 syseventq_t *tmp = syseventq_back; 8105 8106 vprint(CHATTY_MID, "sending queued event: %s, %s\n", 8107 tmp->class, tmp->subclass); 8108 8109 log_event(tmp->class, tmp->subclass, tmp->nvl); 8110 8111 if (tmp->class != NULL) 8112 free(tmp->class); 8113 if (tmp->subclass != NULL) 8114 free(tmp->subclass); 8115 if (tmp->nvl != NULL) 8116 nvlist_free(tmp->nvl); 8117 syseventq_back = syseventq_back->next; 8118 if (syseventq_back == NULL) 8119 syseventq_front = NULL; 8120 free(tmp); 8121 } 8122 (void) mutex_unlock(&syseventq_mutex); 8123 } 8124 8125 static void 8126 build_and_enq_event(char *class, char *subclass, char *node_path, 8127 di_node_t node) 8128 { 8129 nvlist_t *nvl; 8130 8131 vprint(CHATTY_MID, "build_and_enq_event(%s, %s, %s, 0x%8.8x)\n", 8132 class, subclass, node_path, (int)node); 8133 8134 if (node != DI_NODE_NIL) 8135 nvl = build_event_attributes(class, subclass, node_path, node, 8136 di_driver_name(node), di_instance(node)); 8137 else 8138 nvl = build_event_attributes(class, subclass, node_path, node, 8139 NULL, -1); 8140 8141 if (nvl) { 8142 enqueue_sysevent(class, subclass, nvl); 8143 } 8144 } 8145 8146 /* 8147 * is_blank() returns 1 (true) if a line specified is composed of 8148 * whitespace characters only. otherwise, it returns 0 (false). 8149 * 8150 * Note. the argument (line) must be null-terminated. 8151 */ 8152 static int 8153 is_blank(char *line) 8154 { 8155 for (/* nothing */; *line != '\0'; line++) 8156 if (!isspace(*line)) 8157 return (0); 8158 return (1); 8159 } 8160 8161 /* 8162 * Functions to deal with the no-further-processing hash 8163 */ 8164 8165 static void 8166 nfphash_create(void) 8167 { 8168 assert(nfp_hash == NULL); 8169 nfp_hash = s_zalloc(NFP_HASH_SZ * sizeof (item_t *)); 8170 } 8171 8172 static int 8173 nfphash_fcn(char *key) 8174 { 8175 int i; 8176 uint64_t sum = 0; 8177 8178 for (i = 0; key[i] != '\0'; i++) { 8179 sum += (uchar_t)key[i]; 8180 } 8181 8182 return (sum % NFP_HASH_SZ); 8183 } 8184 8185 static item_t * 8186 nfphash_lookup(char *key) 8187 { 8188 int index; 8189 item_t *ip; 8190 8191 index = nfphash_fcn(key); 8192 8193 assert(index >= 0); 8194 8195 for (ip = nfp_hash[index]; ip; ip = ip->i_next) { 8196 if (strcmp(ip->i_key, key) == 0) 8197 return (ip); 8198 } 8199 8200 return (NULL); 8201 } 8202 8203 static void 8204 nfphash_insert(char *key) 8205 { 8206 item_t *ip; 8207 int index; 8208 8209 index = nfphash_fcn(key); 8210 8211 assert(index >= 0); 8212 8213 ip = s_zalloc(sizeof (item_t)); 8214 ip->i_key = s_strdup(key); 8215 8216 ip->i_next = nfp_hash[index]; 8217 nfp_hash[index] = ip; 8218 } 8219 8220 static void 8221 nfphash_destroy(void) 8222 { 8223 int i; 8224 item_t *ip; 8225 8226 for (i = 0; i < NFP_HASH_SZ; i++) { 8227 /*LINTED*/ 8228 while (ip = nfp_hash[i]) { 8229 nfp_hash[i] = ip->i_next; 8230 free(ip->i_key); 8231 free(ip); 8232 } 8233 } 8234 8235 free(nfp_hash); 8236 nfp_hash = NULL; 8237 } 8238 8239 static int 8240 devname_kcall(int subcmd, void *args) 8241 { 8242 int error = 0; 8243 char *nvlbuf = NULL; 8244 size_t nvlsize; 8245 8246 switch (subcmd) { 8247 case MODDEVNAME_NSMAPS: 8248 error = nvlist_pack((nvlist_t *)args, &nvlbuf, &nvlsize, 0, 0); 8249 if (error) { 8250 err_print("packing MODDEVNAME_NSMAPS failed\n"); 8251 break; 8252 } 8253 error = modctl(MODDEVNAME, subcmd, nvlbuf, nvlsize); 8254 if (error) { 8255 vprint(INFO_MID, "modctl(MODDEVNAME, " 8256 "MODDEVNAME_NSMAPS) failed - %s\n", 8257 strerror(errno)); 8258 } 8259 free(nvlbuf); 8260 nvlist_free(args); 8261 break; 8262 case MODDEVNAME_LOOKUPDOOR: 8263 error = modctl(MODDEVNAME, subcmd, (uintptr_t)args); 8264 if (error) { 8265 vprint(INFO_MID, "modctl(MODDEVNAME, " 8266 "MODDEVNAME_LOOKUPDOOR) failed - %s\n", 8267 strerror(errno)); 8268 } 8269 break; 8270 default: 8271 error = EINVAL; 8272 break; 8273 } 8274 return (error); 8275 } 8276 8277 static void 8278 devname_setup_nsmaps(void) 8279 { 8280 int error = 0; 8281 8282 if (devname_first_call) { 8283 devname_first_call = 0; 8284 } 8285 8286 error = di_devname_get_mapinfo(DEVNAME_MASTER_MAP, &devname_maps); 8287 8288 if (error) { 8289 vprint(DEVNAME_MID, "devname_setup_nsmaps: non-existing/empty" 8290 "%s\n", DEVNAME_MASTER_MAP); 8291 } else { 8292 di_devname_print_mapinfo(devname_maps); 8293 8294 /* pass down the existing map names to kernel */ 8295 (void) devname_kcall(MODDEVNAME_NSMAPS, (void *)devname_maps); 8296 } 8297 } 8298 8299 static void 8300 devname_ns_services(uint8_t cmd, char *key, char *map) 8301 { 8302 nvlist_t *nvl = NULL; 8303 int32_t error = 0; 8304 sdev_door_res_t res; 8305 8306 vprint(DEVNAME_MID, "devname_ns_services: cmd %d key %s map %s\n", 8307 cmd, key, map); 8308 8309 switch (cmd) { 8310 case DEVFSADMD_NS_LOOKUP: 8311 vprint(DEVNAME_MID, "calling di_devname_get_mapent\n"); 8312 error = di_devname_get_mapent(key, map, &nvl); 8313 if (nvl == NULL) { 8314 error = DEVFSADM_NS_FAILED; 8315 goto done; 8316 } 8317 8318 if (error) { 8319 nvlist_free(nvl); 8320 goto done; 8321 } 8322 8323 if (devname_debug_msg) 8324 di_devname_print_mapinfo(nvl); 8325 8326 vprint(DEVNAME_MID, "calling di_devname_action_on_key for %d\n", 8327 cmd); 8328 error = di_devname_action_on_key(nvl, cmd, key, (void *)&res); 8329 nvlist_free(nvl); 8330 break; 8331 case DEVFSADMD_NS_READDIR: 8332 vprint(DEVNAME_MID, "calling di_devname_get_mapinfo for cmd %d" 8333 "\n", cmd); 8334 error = di_devname_get_mapinfo(map, &nvl); 8335 if (nvl == NULL) { 8336 error = DEVFSADM_NS_FAILED; 8337 goto done; 8338 } 8339 8340 if (error) { 8341 nvlist_free(nvl); 8342 goto done; 8343 } 8344 8345 if (devname_debug_msg) 8346 di_devname_print_mapinfo(nvl); 8347 8348 vprint(DEVNAME_MID, "calling di_devname_action_on_key\n"); 8349 error = di_devname_action_on_key(nvl, cmd, key, (void *)&res); 8350 nvlist_free(nvl); 8351 break; 8352 default: 8353 error = DEVFSADM_RUN_NOTSUP; 8354 break; 8355 } 8356 8357 done: 8358 vprint(DEVNAME_MID, "error %d\n", error); 8359 res.devfsadm_error = error; 8360 (void) door_return((char *)&res, sizeof (struct sdev_door_res), 8361 NULL, 0); 8362 } 8363 8364 /* ARGSUSED */ 8365 static void 8366 devname_lookup_handler(void *cookie, char *argp, size_t arg_size, 8367 door_desc_t *dp, uint_t n_desc) 8368 { 8369 int32_t error = 0; 8370 door_cred_t dcred; 8371 struct dca_impl dci; 8372 uint8_t cmd; 8373 char *ns_map, *ns_name; 8374 sdev_door_res_t res; 8375 sdev_door_arg_t *args; 8376 8377 if (argp == NULL || arg_size == 0) { 8378 vprint(DEVNAME_MID, "devname_lookup_handler: argp wrong\n"); 8379 error = DEVFSADM_RUN_INVALID; 8380 goto done; 8381 } 8382 vprint(DEVNAME_MID, "devname_lookup_handler\n"); 8383 8384 if (door_cred(&dcred) != 0 || dcred.dc_euid != 0) { 8385 vprint(DEVNAME_MID, "devname_lookup_handler: cred wrong\n"); 8386 error = DEVFSADM_RUN_EPERM; 8387 goto done; 8388 } 8389 8390 args = (sdev_door_arg_t *)argp; 8391 cmd = args->devfsadm_cmd; 8392 8393 vprint(DEVNAME_MID, "devname_lookup_handler: cmd %d\n", cmd); 8394 switch (cmd) { 8395 case DEVFSADMD_NS_LOOKUP: 8396 case DEVFSADMD_NS_READDIR: 8397 ns_name = s_strdup(args->ns_hdl.ns_name); 8398 ns_map = s_strdup(args->ns_hdl.ns_map); 8399 8400 vprint(DEVNAME_MID, " ns_name %s ns_map %s\n", ns_name, ns_map); 8401 if (ns_name == NULL || ns_map == NULL) { 8402 error = DEVFSADM_RUN_INVALID; 8403 goto done; 8404 } 8405 8406 devname_ns_services(cmd, ns_name, ns_map); 8407 return; 8408 case DEVFSADMD_RUN_ALL: 8409 /* 8410 * run "devfsadm" 8411 */ 8412 dci.dci_root = "/"; 8413 dci.dci_minor = NULL; 8414 dci.dci_driver = NULL; 8415 dci.dci_error = 0; 8416 dci.dci_flags = 0; 8417 dci.dci_arg = NULL; 8418 8419 lock_dev(); 8420 update_drvconf((major_t)-1); 8421 dci.dci_flags |= DCA_FLUSH_PATHINST; 8422 8423 pre_and_post_cleanup(RM_PRE); 8424 devi_tree_walk(&dci, DINFOFORCE|DI_CACHE_SNAPSHOT_FLAGS, NULL); 8425 error = (int32_t)dci.dci_error; 8426 if (!error) { 8427 pre_and_post_cleanup(RM_POST); 8428 update_database = TRUE; 8429 unlock_dev(SYNC_STATE); 8430 update_database = FALSE; 8431 } else { 8432 if (DEVFSADM_DEBUG_ON) { 8433 vprint(INFO_MID, "devname_lookup_handler: " 8434 "DEVFSADMD_RUN_ALL failed\n"); 8435 } 8436 8437 unlock_dev(SYNC_STATE); 8438 } 8439 break; 8440 default: 8441 /* log an error here? */ 8442 error = DEVFSADM_RUN_NOTSUP; 8443 break; 8444 } 8445 8446 done: 8447 vprint(DEVNAME_MID, "devname_lookup_handler: error %d\n", error); 8448 res.devfsadm_error = error; 8449 (void) door_return((char *)&res, sizeof (struct sdev_door_res), 8450 NULL, 0); 8451 } 8452