/* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or http://www.opensolaris.org/os/licensing. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright 2008 Sun Microsystems, Inc. All rights reserved. * Use is subject to license terms. */ #pragma ident "@(#)devfsadm.c 1.98 08/05/07 SMI" /* * Devfsadm replaces drvconfig, audlinks, disks, tapes, ports, devlinks * as a general purpose device administrative utility. It creates * devices special files in /devices and logical links in /dev, and * coordinates updates to /etc/path_to_instance with the kernel. It * operates in both command line mode to handle user or script invoked * reconfiguration updates, and operates in daemon mode to handle dynamic * reconfiguration for hotplugging support. */ #include #include #include #include #include #include #include #include #include "devfsadm_impl.h" /* externs from devalloc.c */ extern void _reset_devalloc(int); extern void _update_devalloc_db(devlist_t *, int, int, char *, char *); extern int _da_check_for_usb(char *, char *); /* create or remove nodes or links. unset with -n */ static int file_mods = TRUE; /* cleanup mode. Set with -C */ static int cleanup = FALSE; /* devlinks -d compatibility */ static int devlinks_debug = FALSE; /* flag to check if system is labeled */ int system_labeled = FALSE; /* flag to enable/disable device allocation with -e/-d */ static int devalloc_flag = 0; /* flag that indicates if device allocation is on or not */ static int devalloc_is_on = 0; /* flag to update device allocation database for this device type */ static int update_devdb = 0; /* * devices to be deallocated with -d : * audio, floppy, cd, floppy, tape, rmdisk. */ static char *devalloc_list[10] = {DDI_NT_AUDIO, DDI_NT_CD, DDI_NT_CD_CHAN, DDI_NT_FD, DDI_NT_TAPE, DDI_NT_BLOCK_CHAN, DDI_NT_UGEN, DDI_NT_USB_ATTACHMENT_POINT, DDI_NT_SCSI_NEXUS, NULL}; /* list of allocatable devices */ static devlist_t devlist; /* load a single driver only. set with -i */ static int single_drv = FALSE; static char *driver = NULL; /* attempt to load drivers or defer attach nodes */ static int load_attach_drv = TRUE; /* set if invoked via /usr/lib/devfsadm/devfsadmd */ static int daemon_mode = FALSE; /* output directed to syslog during daemon mode if set */ static int logflag = FALSE; /* build links in /dev. -x to turn off */ static int build_dev = TRUE; /* build nodes in /devices. -y to turn off */ static int build_devices = TRUE; /* -z to turn off */ static int flush_path_to_inst_enable = TRUE; /* variables used for path_to_inst flushing */ static int inst_count = 0; static mutex_t count_lock; static cond_t cv; /* variables for minor_fini thread */ static mutex_t minor_fini_mutex; static int minor_fini_canceled = TRUE; static int minor_fini_delayed = FALSE; static cond_t minor_fini_cv; static int minor_fini_timeout = MINOR_FINI_TIMEOUT_DEFAULT; /* single-threads /dev modification */ static sema_t dev_sema; /* the program we were invoked as; ie argv[0] */ static char *prog; /* pointers to create/remove link lists */ static create_list_t *create_head = NULL; static remove_list_t *remove_head = NULL; /* supports the class -c option */ static char **classes = NULL; static int num_classes = 0; /* used with verbose option -v or -V */ static int num_verbose = 0; static char **verbose = NULL; static struct mperm *minor_perms = NULL; static driver_alias_t *driver_aliases = NULL; /* set if -r alternate root given */ static char *root_dir = ""; /* /devices or /devices */ static char *devices_dir = DEVICES; /* /dev or /dev */ static char *dev_dir = DEV; /* /etc/dev or /etc/dev */ static char *etc_dev_dir = ETCDEV; /* * writable root (for lock files and doors during install). * This is also root dir for /dev attr dir during install. */ static char *attr_root = NULL; /* /etc/path_to_inst unless -p used */ static char *inst_file = INSTANCE_FILE; /* /usr/lib/devfsadm/linkmods unless -l used */ static char *module_dirs = MODULE_DIRS; /* default uid/gid used if /etc/minor_perm entry not found */ static uid_t root_uid; static gid_t sys_gid; /* /etc/devlink.tab unless devlinks -t used */ static char *devlinktab_file = NULL; /* File and data structure to reserve enumerate IDs */ static char *enumerate_file = ENUMERATE_RESERVED; static enumerate_file_t *enumerate_reserved = NULL; /* set if /dev link is new. speeds up rm_stale_links */ static int linknew = TRUE; /* variables for devlink.tab compat processing */ static devlinktab_list_t *devlinktab_list = NULL; static unsigned int devlinktab_line = 0; /* cache head for devfsadm_enumerate*() functions */ static numeral_set_t *head_numeral_set = NULL; /* list list of devfsadm modules */ static module_t *module_head = NULL; /* name_to_major list used in utility function */ static n2m_t *n2m_list = NULL; /* cache of some links used for performance */ static linkhead_t *headlinkhead = NULL; /* locking variables to prevent multiples writes to /dev */ static int hold_dev_lock = FALSE; static int hold_daemon_lock = FALSE; static int dev_lock_fd; static int daemon_lock_fd; static char dev_lockfile[PATH_MAX + 1]; static char daemon_lockfile[PATH_MAX + 1]; /* last devinfo node/minor processed. used for performance */ static di_node_t lnode; static di_minor_t lminor; static char lphy_path[PATH_MAX + 1] = {""}; /* Globals used by the link database */ static di_devlink_handle_t devlink_cache; static int update_database = FALSE; /* Globals used to set logindev perms */ static struct login_dev *login_dev_cache = NULL; static int login_dev_enable = FALSE; /* Global to use devinfo snapshot cache */ static int use_snapshot_cache = FALSE; /* Global for no-further-processing hash */ static item_t **nfp_hash; static mutex_t nfp_mutex = DEFAULTMUTEX; /* * Packaged directories - not removed even when empty. * The dirs must be listed in canonical form * i.e. without leading "/dev/" */ static char *packaged_dirs[] = {"dsk", "rdsk", "term", NULL}; /* RCM related globals */ static void *librcm_hdl; static rcm_handle_t *rcm_hdl = NULL; static thread_t process_rcm_events_tid; static struct rcm_eventq *volatile rcm_eventq_head = NULL; static struct rcm_eventq *rcm_eventq_tail = NULL; static mutex_t rcm_eventq_lock; static cond_t rcm_eventq_cv; static volatile int need_to_exit_rcm_event_thread = 0; /* Devname globals */ static int devname_debug_msg = 1; static nvlist_t *devname_maps = NULL; static int devname_first_call = 1; static int load_devname_nsmaps = FALSE; static int lookup_door_fd = -1; static char *lookup_door_path; static void load_dev_acl(void); static void update_drvconf(major_t); static void check_reconfig_state(void); static void devname_setup_nsmaps(void); static int s_stat(const char *, struct stat *); static int is_blank(char *); /* sysevent queue related globals */ static mutex_t syseventq_mutex = DEFAULTMUTEX; static syseventq_t *syseventq_front; static syseventq_t *syseventq_back; static void process_syseventq(); int main(int argc, char *argv[]) { struct passwd *pw; struct group *gp; pid_t pid; int cond = 0; (void) setlocale(LC_ALL, ""); (void) textdomain(TEXT_DOMAIN); if ((prog = strrchr(argv[0], '/')) == NULL) { prog = argv[0]; } else { prog++; } if (getuid() != 0) { err_print(MUST_BE_ROOT); devfsadm_exit(1); } /* * Close all files except stdin/stdout/stderr */ closefrom(3); if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) { root_uid = pw->pw_uid; } else { err_print(CANT_FIND_USER, DEFAULT_DEV_USER); root_uid = (uid_t)0; /* assume 0 is root */ } /* the default group is sys */ if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) { sys_gid = gp->gr_gid; } else { err_print(CANT_FIND_GROUP, DEFAULT_DEV_GROUP); sys_gid = (gid_t)3; /* assume 3 is sys */ } (void) umask(0); system_labeled = is_system_labeled(); if (system_labeled == FALSE) { /* * is_system_labeled() will return false in case we are * starting before the first reboot after Trusted Extensions * is enabled. Check the setting in /etc/system to see if * TX is enabled (even if not yet booted). */ if (defopen("/etc/system") == 0) { if (defread("set sys_labeling=1") != NULL) system_labeled = TRUE; /* close defaults file */ (void) defopen(NULL); } } /* * Check if device allocation is enabled. */ if (system_labeled) { /* * In TX, the first line in /etc/security/device_allocate has * DEVICE_ALLOCATION=ON if the feature is enabled. */ devalloc_is_on = da_is_on(); } else if (auditon(A_GETCOND, (caddr_t)&cond, sizeof (cond)) == 0) { /* * Device allocation (and auditing) is enabled if BSM is * enabled. auditon returns -1 and sets errno to EINVAL if BSM * is not enabled. */ devalloc_is_on = 1; } #ifdef DEBUG if (system_labeled == FALSE) { struct stat tx_stat; /* test hook: see also mkdevalloc.c and allocate.c */ system_labeled = is_system_labeled_debug(&tx_stat); } #endif parse_args(argc, argv); (void) sema_init(&dev_sema, 1, USYNC_THREAD, NULL); /* Initialize device allocation list */ devlist.audio = devlist.cd = devlist.floppy = devlist.tape = devlist.rmdisk = NULL; if (daemon_mode == TRUE) { /* * Build /dev and /devices before daemonizing if * reconfig booting and daemon invoked with alternate * root. This is to support install. */ if (getenv(RECONFIG_BOOT) != NULL && root_dir[0] != '\0') { vprint(INFO_MID, CONFIGURING); load_dev_acl(); update_drvconf((major_t)-1); process_devinfo_tree(); (void) modctl(MODSETMINIROOT); } /* * fork before detaching from tty in order to print error * message if unable to acquire file lock. locks not preserved * across forks. Even under debug we want to fork so that * when executed at boot we don't hang. */ if (fork() != 0) { devfsadm_exit(0); } /* set directory to / so it coredumps there */ if (chdir("/") == -1) { err_print(CHROOT_FAILED, strerror(errno)); } /* only one daemon can run at a time */ if ((pid = enter_daemon_lock()) == getpid()) { detachfromtty(); (void) cond_init(&cv, USYNC_THREAD, 0); (void) mutex_init(&count_lock, USYNC_THREAD, 0); if (thr_create(NULL, NULL, (void *(*)(void *))instance_flush_thread, NULL, THR_DETACHED, NULL) != 0) { err_print(CANT_CREATE_THREAD, "daemon", strerror(errno)); devfsadm_exit(1); } /* start the minor_fini_thread */ (void) mutex_init(&minor_fini_mutex, USYNC_THREAD, 0); (void) cond_init(&minor_fini_cv, USYNC_THREAD, 0); if (thr_create(NULL, NULL, (void *(*)(void *))minor_fini_thread, NULL, THR_DETACHED, NULL)) { err_print(CANT_CREATE_THREAD, "minor_fini", strerror(errno)); devfsadm_exit(1); } /* * No need for rcm notifications when running * with an alternate root. So initialize rcm only * when devfsadm is running with root dir "/". * Similarly, logindevperms need only be set * in daemon mode and when root dir is "/". */ if (root_dir[0] == '\0') { (void) rcm_init(); login_dev_enable = TRUE; } daemon_update(); } else { err_print(DAEMON_RUNNING, pid); devfsadm_exit(1); } exit_daemon_lock(); } else { /* not a daemon, so just build /dev and /devices */ process_devinfo_tree(); if (devalloc_flag != 0) /* Enable/disable device allocation */ _reset_devalloc(devalloc_flag); } return (0); } static void update_drvconf(major_t major) { if (modctl(MODLOADDRVCONF, major) != 0) err_print(gettext("update_drvconf failed for major %d\n"), major); } static void load_dev_acl() { if (load_devpolicy() != 0) err_print(gettext("device policy load failed\n")); load_minor_perm_file(); } /* * As devfsadm is run early in boot to provide the kernel with * minor_perm info, we might as well check for reconfig at the * same time to avoid running devfsadm twice. This gets invoked * earlier than the env variable RECONFIG_BOOT is set up. */ static void check_reconfig_state() { struct stat sb; if (s_stat("/reconfigure", &sb) == 0) { (void) modctl(MODDEVNAME, MODDEVNAME_RECONFIG, 0); } } static void modctl_sysavail() { /* * Inform /dev that system is available, that * implicit reconfig can now be performed. */ (void) modctl(MODDEVNAME, MODDEVNAME_SYSAVAIL, 0); } static void set_lock_root(void) { struct stat sb; char *lock_root; size_t len; lock_root = attr_root ? attr_root : root_dir; len = strlen(lock_root) + strlen(ETCDEV) + 1; etc_dev_dir = s_malloc(len); (void) snprintf(etc_dev_dir, len, "%s%s", lock_root, ETCDEV); if (s_stat(etc_dev_dir, &sb) != 0) { s_mkdirp(etc_dev_dir, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); } else if (!S_ISDIR(sb.st_mode)) { err_print(NOT_DIR, etc_dev_dir); devfsadm_exit(1); } } /* * Parse arguments for all 6 programs handled from devfsadm. */ static void parse_args(int argc, char *argv[]) { char opt; char get_linkcompat_opts = FALSE; char *compat_class; int num_aliases = 0; int len; int retval; int add_bind = FALSE; struct aliases *ap = NULL; struct aliases *a_head = NULL; struct aliases *a_tail = NULL; struct modconfig mc; if (strcmp(prog, DISKS) == 0) { compat_class = "disk"; get_linkcompat_opts = TRUE; } else if (strcmp(prog, TAPES) == 0) { compat_class = "tape"; get_linkcompat_opts = TRUE; } else if (strcmp(prog, PORTS) == 0) { compat_class = "port"; get_linkcompat_opts = TRUE; } else if (strcmp(prog, AUDLINKS) == 0) { compat_class = "audio"; get_linkcompat_opts = TRUE; } else if (strcmp(prog, DEVLINKS) == 0) { devlinktab_file = DEVLINKTAB_FILE; build_devices = FALSE; load_attach_drv = FALSE; while ((opt = getopt(argc, argv, "dnr:st:vV:")) != EOF) { switch (opt) { case 'd': file_mods = FALSE; flush_path_to_inst_enable = FALSE; devlinks_debug = TRUE; break; case 'n': /* prevent driver loading and deferred attach */ load_attach_drv = FALSE; break; case 'r': set_root_devices_dev_dir(optarg); if (zone_pathcheck(root_dir) != DEVFSADM_SUCCESS) devfsadm_exit(1); break; case 's': /* * suppress. don't create/remove links/nodes * useful with -v or -V */ file_mods = FALSE; flush_path_to_inst_enable = FALSE; break; case 't': /* supply a non-default table file */ devlinktab_file = optarg; break; case 'v': /* documented verbose flag */ add_verbose_id(VERBOSE_MID); break; case 'V': /* undocumented for extra verbose levels */ add_verbose_id(optarg); break; default: usage(); break; } } if (optind < argc) { usage(); } } else if (strcmp(prog, DRVCONFIG) == 0) { build_dev = FALSE; while ((opt = getopt(argc, argv, "a:bdc:i:m:np:R:r:svV:")) != EOF) { switch (opt) { case 'a': ap = calloc(sizeof (struct aliases), 1); ap->a_name = dequote(optarg); len = strlen(ap->a_name) + 1; if (len > MAXMODCONFNAME) { err_print(ALIAS_TOO_LONG, MAXMODCONFNAME, ap->a_name); devfsadm_exit(1); } ap->a_len = len; if (a_tail == NULL) { a_head = ap; } else { a_tail->a_next = ap; } a_tail = ap; num_aliases++; add_bind = TRUE; break; case 'b': add_bind = TRUE; break; case 'c': (void) strcpy(mc.drvclass, optarg); break; case 'd': /* * need to keep for compatibility, but * do nothing. */ break; case 'i': single_drv = TRUE; (void) strcpy(mc.drvname, optarg); driver = s_strdup(optarg); break; case 'm': mc.major = atoi(optarg); break; case 'n': /* prevent driver loading and deferred attach */ load_attach_drv = FALSE; break; case 'p': /* specify alternate path_to_inst file */ inst_file = s_strdup(optarg); break; case 'R': /* * Private flag for suninstall to populate * device information on the installed root. */ root_dir = s_strdup(optarg); if (zone_pathcheck(root_dir) != DEVFSADM_SUCCESS) devfsadm_exit(devfsadm_copy()); break; case 'r': devices_dir = s_strdup(optarg); if (zone_pathcheck(devices_dir) != DEVFSADM_SUCCESS) devfsadm_exit(1); break; case 's': /* * suppress. don't create nodes * useful with -v or -V */ file_mods = FALSE; flush_path_to_inst_enable = FALSE; break; case 'v': /* documented verbose flag */ add_verbose_id(VERBOSE_MID); break; case 'V': /* undocumented for extra verbose levels */ add_verbose_id(optarg); break; default: usage(); } } if (optind < argc) { usage(); } if ((add_bind == TRUE) && (mc.major == -1 || mc.drvname[0] == NULL)) { err_print(MAJOR_AND_B_FLAG); devfsadm_exit(1); } if (add_bind == TRUE) { mc.num_aliases = num_aliases; mc.ap = a_head; retval = modctl(MODADDMAJBIND, NULL, (caddr_t)&mc); if (retval < 0) { err_print(MODCTL_ADDMAJBIND); } devfsadm_exit(retval); } } else if ((strcmp(prog, DEVFSADM) == 0) || (strcmp(prog, DEVFSADMD) == 0)) { char *zonename = NULL; int init_drvconf = 0; int init_perm = 0; int public_mode = 0; int init_sysavail = 0; if (strcmp(prog, DEVFSADMD) == 0) { daemon_mode = TRUE; } devlinktab_file = DEVLINKTAB_FILE; while ((opt = getopt(argc, argv, "a:Cc:deIi:l:mnp:PR:r:sSt:vV:x:")) != EOF) { if (opt == 'I' || opt == 'P' || opt == 'S') { if (public_mode) usage(); } else { if (init_perm || init_drvconf || init_sysavail) usage(); public_mode = 1; } switch (opt) { case 'a': attr_root = s_strdup(optarg); break; case 'C': cleanup = TRUE; break; case 'c': num_classes++; classes = s_realloc(classes, num_classes * sizeof (char *)); classes[num_classes - 1] = optarg; break; case 'd': if (daemon_mode == FALSE) { /* * Device allocation to be disabled. */ devalloc_flag = DA_OFF; build_dev = FALSE; } break; case 'e': if (daemon_mode == FALSE) { /* * Device allocation to be enabled. */ devalloc_flag = DA_ON; build_dev = FALSE; } break; case 'I': /* update kernel driver.conf cache */ if (daemon_mode == TRUE) usage(); init_drvconf = 1; break; case 'i': single_drv = TRUE; driver = s_strdup(optarg); break; case 'l': /* specify an alternate module load path */ module_dirs = s_strdup(optarg); break; case 'm': load_devname_nsmaps = TRUE; break; case 'n': /* prevent driver loading and deferred attach */ load_attach_drv = FALSE; break; case 'p': /* specify alternate path_to_inst file */ inst_file = s_strdup(optarg); break; case 'P': if (daemon_mode == TRUE) usage(); /* load minor_perm and device_policy */ init_perm = 1; break; case 'R': /* * Private flag for suninstall to populate * device information on the installed root. */ root_dir = s_strdup(optarg); devfsadm_exit(devfsadm_copy()); break; case 'r': set_root_devices_dev_dir(optarg); break; case 's': /* * suppress. don't create/remove links/nodes * useful with -v or -V */ file_mods = FALSE; flush_path_to_inst_enable = FALSE; break; case 'S': if (daemon_mode == TRUE) usage(); init_sysavail = 1; break; case 't': devlinktab_file = optarg; break; case 'v': /* documented verbose flag */ add_verbose_id(VERBOSE_MID); break; case 'V': /* undocumented: specify verbose lvl */ add_verbose_id(optarg); break; case 'x': /* * x is the "private switch" option. The * goal is to not suck up all the other * option letters. */ if (strcmp(optarg, "update_devlinksdb") == 0) { update_database = TRUE; } else if (strcmp(optarg, "no_dev") == 0) { /* don't build /dev */ build_dev = FALSE; } else if (strcmp(optarg, "no_devices") == 0) { /* don't build /devices */ build_devices = FALSE; } else if (strcmp(optarg, "no_p2i") == 0) { /* don't flush path_to_inst */ flush_path_to_inst_enable = FALSE; } else if (strcmp(optarg, "use_dicache") == 0) { use_snapshot_cache = TRUE; } else { usage(); } break; default: usage(); break; } } if (optind < argc) { usage(); } /* * We're not in zone mode; Check to see if the rootpath * collides with any zonepaths. */ if (zonename == NULL) { if (zone_pathcheck(root_dir) != DEVFSADM_SUCCESS) devfsadm_exit(1); } if (init_drvconf || init_perm || init_sysavail) { /* * Load minor perm before force-loading drivers * so the correct permissions are picked up. */ if (init_perm) { check_reconfig_state(); load_dev_acl(); } if (init_drvconf) update_drvconf((major_t)-1); if (init_sysavail) modctl_sysavail(); devfsadm_exit(0); /* NOTREACHED */ } if (load_devname_nsmaps == TRUE) { devname_setup_nsmaps(); devfsadm_exit(0); } } if (get_linkcompat_opts == TRUE) { build_devices = FALSE; load_attach_drv = FALSE; num_classes++; classes = s_realloc(classes, num_classes * sizeof (char *)); classes[num_classes - 1] = compat_class; while ((opt = getopt(argc, argv, "Cnr:svV:")) != EOF) { switch (opt) { case 'C': cleanup = TRUE; break; case 'n': /* prevent driver loading or deferred attach */ load_attach_drv = FALSE; break; case 'r': set_root_devices_dev_dir(optarg); if (zone_pathcheck(root_dir) != DEVFSADM_SUCCESS) devfsadm_exit(1); break; case 's': /* suppress. don't create/remove links/nodes */ /* useful with -v or -V */ file_mods = FALSE; flush_path_to_inst_enable = FALSE; break; case 'v': /* documented verbose flag */ add_verbose_id(VERBOSE_MID); break; case 'V': /* undocumented for extra verbose levels */ add_verbose_id(optarg); break; default: usage(); } } if (optind < argc) { usage(); } } set_lock_root(); } void usage(void) { if (strcmp(prog, DEVLINKS) == 0) { err_print(DEVLINKS_USAGE); } else if (strcmp(prog, DRVCONFIG) == 0) { err_print(DRVCONFIG_USAGE); } else if ((strcmp(prog, DEVFSADM) == 0) || (strcmp(prog, DEVFSADMD) == 0)) { err_print(DEVFSADM_USAGE); } else { err_print(COMPAT_LINK_USAGE); } devfsadm_exit(1); } static void devi_tree_walk(struct dca_impl *dcip, int flags, char *ev_subclass) { char *msg, *name; struct mlist mlist = {0}; di_node_t node; vprint(CHATTY_MID, "devi_tree_walk: root=%s, minor=%s, driver=%s," " error=%d, flags=%u\n", dcip->dci_root, dcip->dci_minor ? dcip->dci_minor : "", dcip->dci_driver ? dcip->dci_driver : "", dcip->dci_error, dcip->dci_flags); assert(dcip->dci_root); if (dcip->dci_flags & DCA_LOAD_DRV) { node = di_init_driver(dcip->dci_driver, flags); msg = DRIVER_FAILURE; name = dcip->dci_driver; } else { node = di_init(dcip->dci_root, flags); msg = DI_INIT_FAILED; name = dcip->dci_root; } if (node == DI_NODE_NIL) { dcip->dci_error = errno; /* * Rapid hotplugging (commonly seen during USB testing), * may remove a device before the create event for it * has been processed. To prevent alarming users with * a superfluous message, we suppress error messages * for ENXIO and hotplug. */ if (!(errno == ENXIO && (dcip->dci_flags & DCA_HOT_PLUG))) err_print(msg, name, strerror(dcip->dci_error)); return; } if (dcip->dci_flags & DCA_FLUSH_PATHINST) flush_path_to_inst(); dcip->dci_arg = &mlist; vprint(CHATTY_MID, "walking device tree\n"); (void) di_walk_minor(node, NULL, DI_CHECK_ALIAS, dcip, check_minor_type); process_deferred_links(dcip, DCA_CREATE_LINK); dcip->dci_arg = NULL; /* * Finished creating devfs files and dev links. * Log sysevent and notify RCM. */ if (ev_subclass) build_and_enq_event(EC_DEV_ADD, ev_subclass, dcip->dci_root, node, dcip->dci_minor); if ((dcip->dci_flags & DCA_NOTIFY_RCM) && rcm_hdl) (void) notify_rcm(node, dcip->dci_minor); /* Add new device to device allocation database */ if (system_labeled && update_devdb) { _update_devalloc_db(&devlist, 0, DA_ADD, NULL, root_dir); update_devdb = 0; } di_fini(node); } static void process_deferred_links(struct dca_impl *dcip, int flags) { struct mlist *dep; struct minor *mp, *smp; vprint(CHATTY_MID, "processing deferred links\n"); dep = dcip->dci_arg; /* * The list head is not used during the deferred create phase */ dcip->dci_arg = NULL; assert(dep); assert((dep->head == NULL) ^ (dep->tail != NULL)); assert(flags == DCA_FREE_LIST || flags == DCA_CREATE_LINK); for (smp = NULL, mp = dep->head; mp; mp = mp->next) { if (flags == DCA_CREATE_LINK) (void) check_minor_type(mp->node, mp->minor, dcip); free(smp); smp = mp; } free(smp); } /* * Called in non-daemon mode to take a snap shot of the devinfo tree. * Then it calls the appropriate functions to build /devices and /dev. * It also flushes path_to_inst. * Except in the devfsadm -i (single driver case), the flags used by devfsadm * needs to match DI_CACHE_SNAPSHOT_FLAGS. That will make DINFOCACHE snapshot * updated. */ void process_devinfo_tree() { uint_t flags; struct dca_impl dci; char name[MAXNAMELEN]; char *fcn = "process_devinfo_tree: "; vprint(CHATTY_MID, "%senter\n", fcn); dca_impl_init("/", NULL, &dci); lock_dev(); /* * Update kernel driver.conf cache when devfsadm/drvconfig * is invoked to build /devices and /dev. */ if (load_attach_drv == TRUE) update_drvconf((major_t)-1); if (single_drv == TRUE) { /* * load a single driver, but walk the entire devinfo tree */ if (load_attach_drv == FALSE) err_print(DRV_LOAD_REQD); vprint(CHATTY_MID, "%sattaching driver (%s)\n", fcn, driver); dci.dci_flags |= DCA_LOAD_DRV; (void) snprintf(name, sizeof (name), "%s", driver); dci.dci_driver = name; flags = DINFOCPYALL | DINFOPATH; } else if (load_attach_drv == TRUE) { /* * Load and attach all drivers, then walk the entire tree. * If the cache flag is set, use DINFOCACHE to get cached * data. */ if (use_snapshot_cache == TRUE) { flags = DINFOCACHE; vprint(CHATTY_MID, "%susing snapshot cache\n", fcn); } else { vprint(CHATTY_MID, "%sattaching all drivers\n", fcn); flags = DI_CACHE_SNAPSHOT_FLAGS; if (cleanup) { /* * remove dangling entries from /etc/devices * files. */ flags |= DINFOCLEANUP; } } } else { /* * For devlinks, disks, ports, tapes and devfsadm -n, * just need to take a snapshot with active devices. */ vprint(CHATTY_MID, "%staking snapshot of active devices\n", fcn); flags = DINFOCPYALL; } if (((load_attach_drv == TRUE) || (single_drv == TRUE)) && (build_devices == TRUE)) { dci.dci_flags |= DCA_FLUSH_PATHINST; } /* handle pre-cleanup operations desired by the modules. */ pre_and_post_cleanup(RM_PRE); devi_tree_walk(&dci, flags, NULL); if (dci.dci_error) { devfsadm_exit(1); } /* handle post-cleanup operations desired by the modules. */ pre_and_post_cleanup(RM_POST); unlock_dev(SYNC_STATE); } /*ARGSUSED*/ static void print_cache_signal(int signo) { if (signal(SIGUSR1, print_cache_signal) == SIG_ERR) { err_print("signal SIGUSR1 failed: %s\n", strerror(errno)); devfsadm_exit(1); } } static void revoke_lookup_door(void) { if (lookup_door_fd != -1) { if (door_revoke(lookup_door_fd) == -1) { err_print("door_revoke of %s failed - %s\n", lookup_door_path, strerror(errno)); } } } /*ARGSUSED*/ static void catch_exit(int signo) { revoke_lookup_door(); } /* * Register with eventd for messages. Create doors for synchronous * link creation. */ static void daemon_update(void) { int fd; char *fcn = "daemon_update: "; char door_file[MAXPATHLEN]; const char *subclass_list; sysevent_handle_t *sysevent_hp; vprint(CHATTY_MID, "%senter\n", fcn); if (signal(SIGUSR1, print_cache_signal) == SIG_ERR) { err_print("signal SIGUSR1 failed: %s\n", strerror(errno)); devfsadm_exit(1); } if (signal(SIGTERM, catch_exit) == SIG_ERR) { err_print("signal SIGTERM failed: %s\n", strerror(errno)); devfsadm_exit(1); } if (snprintf(door_file, sizeof (door_file), "%s%s", attr_root ? attr_root : root_dir, DEVFSADM_SERVICE_DOOR) >= sizeof (door_file)) { err_print("update_daemon failed to open sysevent service " "door\n"); devfsadm_exit(1); } if ((sysevent_hp = sysevent_open_channel_alt( door_file)) == NULL) { err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); devfsadm_exit(1); } if (sysevent_bind_subscriber(sysevent_hp, event_handler) != 0) { err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); (void) sysevent_close_channel(sysevent_hp); devfsadm_exit(1); } subclass_list = EC_SUB_ALL; if (sysevent_register_event(sysevent_hp, EC_ALL, &subclass_list, 1) != 0) { err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); (void) sysevent_unbind_subscriber(sysevent_hp); (void) sysevent_close_channel(sysevent_hp); devfsadm_exit(1); } if (snprintf(door_file, sizeof (door_file), "%s/%s", etc_dev_dir, DEVFSADM_SYNCH_DOOR) >= sizeof (door_file)) { err_print(CANT_CREATE_DOOR, DEVFSADM_SYNCH_DOOR, strerror(ENAMETOOLONG)); devfsadm_exit(1); } (void) s_unlink(door_file); if ((fd = open(door_file, O_RDWR | O_CREAT, SYNCH_DOOR_PERMS)) == -1) { err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); devfsadm_exit(1); } (void) close(fd); if ((fd = door_create(sync_handler, NULL, DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) { err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); (void) s_unlink(door_file); devfsadm_exit(1); } if (fattach(fd, door_file) == -1) { err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); (void) s_unlink(door_file); devfsadm_exit(1); } /* * devname_lookup_door */ if (snprintf(door_file, sizeof (door_file), "%s/%s", etc_dev_dir, DEVNAME_LOOKUP_DOOR) >= sizeof (door_file)) { err_print(CANT_CREATE_DOOR, DEVNAME_LOOKUP_DOOR, strerror(ENAMETOOLONG)); devfsadm_exit(1); } (void) s_unlink(door_file); if ((fd = open(door_file, O_RDWR | O_CREAT, S_IRUSR|S_IWUSR)) == -1) { err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); devfsadm_exit(1); } (void) close(fd); if ((fd = door_create(devname_lookup_handler, NULL, DOOR_REFUSE_DESC)) == -1) { err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); (void) s_unlink(door_file); devfsadm_exit(1); } (void) fdetach(door_file); lookup_door_path = s_strdup(door_file); retry: if (fattach(fd, door_file) == -1) { if (errno == EBUSY) goto retry; err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); (void) s_unlink(door_file); devfsadm_exit(1); } lookup_door_fd = fd; /* pass down the door name to kernel for door_ki_open */ if (devname_kcall(MODDEVNAME_LOOKUPDOOR, (void *)door_file) != 0) err_print(DEVNAME_CONTACT_FAILED, strerror(errno)); else devname_setup_nsmaps(); vprint(CHATTY_MID, "%spausing\n", fcn); for (;;) { (void) pause(); } } /*ARGSUSED*/ static void sync_handler(void *cookie, char *ap, size_t asize, door_desc_t *dp, uint_t ndesc) { door_cred_t dcred; struct dca_off *dcp, rdca; struct dca_impl dci; /* * Must be root to make this call * If caller is not root, don't touch its data. */ if (door_cred(&dcred) != 0 || dcred.dc_euid != 0) { dcp = ⤷ dcp->dca_error = EPERM; goto out; } assert(ap); assert(asize == sizeof (*dcp)); dcp = (void *)ap; /* * Root is always present and is the first component of "name" member */ assert(dcp->dca_root == 0); /* * The structure passed in by the door_client uses offsets * instead of pointers to work across address space boundaries. * Now copy the data into a structure (dca_impl) which uses * pointers. */ dci.dci_root = &dcp->dca_name[dcp->dca_root]; dci.dci_minor = dcp->dca_minor ? &dcp->dca_name[dcp->dca_minor] : NULL; dci.dci_driver = dcp->dca_driver ? &dcp->dca_name[dcp->dca_driver] : NULL; dci.dci_error = 0; dci.dci_flags = dcp->dca_flags | (dci.dci_driver ? DCA_LOAD_DRV : 0); dci.dci_arg = NULL; lock_dev(); devi_tree_walk(&dci, DINFOCPYALL, NULL); dcp->dca_error = dci.dci_error; if (dcp->dca_flags & DCA_DEVLINK_SYNC) unlock_dev(SYNC_STATE); else unlock_dev(CACHE_STATE); out: (void) door_return((char *)dcp, sizeof (*dcp), NULL, 0); } static void lock_dev(void) { vprint(CHATTY_MID, "lock_dev(): entered\n"); if (build_dev == FALSE) return; /* lockout other threads from /dev */ while (sema_wait(&dev_sema) != 0) ; /* * Lock out other devfsadm processes from /dev. * If this wasn't the last process to run, * clear caches */ if (enter_dev_lock() != getpid()) { invalidate_enumerate_cache(); rm_all_links_from_cache(); (void) di_devlink_close(&devlink_cache, DI_LINK_ERROR); /* send any sysevents that were queued up. */ process_syseventq(); } /* * (re)load the reverse links database if not * already cached. */ if (devlink_cache == NULL) devlink_cache = di_devlink_open(root_dir, 0); /* * If modules were unloaded, reload them. Also use module status * as an indication that we should check to see if other binding * files need to be reloaded. */ if (module_head == NULL) { load_modules(); read_minor_perm_file(); read_driver_aliases_file(); read_devlinktab_file(); read_logindevperm_file(); read_enumerate_file(); } if (module_head != NULL) return; if (strcmp(prog, DEVLINKS) == 0) { if (devlinktab_list == NULL) { err_print(NO_LINKTAB, devlinktab_file); err_print(NO_MODULES, module_dirs); err_print(ABORTING); devfsadm_exit(1); } } else { err_print(NO_MODULES, module_dirs); if (strcmp(prog, DEVFSADM) == 0) { err_print(MODIFY_PATH); } } } /* * Unlock the device. If we are processing a CACHE_STATE call, we signal a * minor_fini_thread delayed SYNC_STATE at the end of the call. If we are * processing a SYNC_STATE call, we cancel any minor_fini_thread SYNC_STATE * at both the start and end of the call since we will be doing the SYNC_STATE. */ static void unlock_dev(int flag) { assert(flag == SYNC_STATE || flag == CACHE_STATE); vprint(CHATTY_MID, "unlock_dev(): entered\n"); /* If we are starting a SYNC_STATE, cancel minor_fini_thread SYNC */ if (flag == SYNC_STATE) { (void) mutex_lock(&minor_fini_mutex); minor_fini_canceled = TRUE; minor_fini_delayed = FALSE; (void) mutex_unlock(&minor_fini_mutex); } if (build_dev == FALSE) return; if (devlink_cache == NULL) { err_print(NO_DEVLINK_CACHE); } assert(devlink_cache); if (flag == SYNC_STATE) { unload_modules(); if (update_database) (void) di_devlink_update(devlink_cache); (void) di_devlink_close(&devlink_cache, 0); /* * now that the devlinks db cache has been flushed, it is safe * to send any sysevents that were queued up. */ process_syseventq(); } exit_dev_lock(); (void) mutex_lock(&minor_fini_mutex); if (flag == SYNC_STATE) { /* We did a SYNC_STATE, cancel minor_fini_thread SYNC */ minor_fini_canceled = TRUE; minor_fini_delayed = FALSE; } else { /* We did a CACHE_STATE, start delayed minor_fini_thread SYNC */ minor_fini_canceled = FALSE; minor_fini_delayed = TRUE; (void) cond_signal(&minor_fini_cv); } (void) mutex_unlock(&minor_fini_mutex); (void) sema_post(&dev_sema); } /* * Check that if -r is set, it is not any part of a zone--- that is, that * the zonepath is not a substring of the root path. */ static int zone_pathcheck(char *checkpath) { void *dlhdl = NULL; char *name; char root[MAXPATHLEN]; /* resolved devfsadm root path */ char zroot[MAXPATHLEN]; /* zone root path */ char rzroot[MAXPATHLEN]; /* resolved zone root path */ char tmp[MAXPATHLEN]; FILE *cookie; int err = DEVFSADM_SUCCESS; if (checkpath[0] == '\0') return (DEVFSADM_SUCCESS); /* * Check if zones is available on this system. */ if ((dlhdl = dlopen(LIBZONECFG_PATH, RTLD_LAZY)) == NULL) { return (DEVFSADM_SUCCESS); } bzero(root, sizeof (root)); if (resolvepath(checkpath, root, sizeof (root) - 1) == -1) { /* * In this case the user has done "devfsadm -r" on some path * which does not yet exist, or we got some other misc. error. * We punt and don't resolve the path in this case. */ (void) strlcpy(root, checkpath, sizeof (root)); } if (strlen(root) > 0 && (root[strlen(root) - 1] != '/')) { (void) snprintf(tmp, sizeof (tmp), "%s/", root); (void) strlcpy(root, tmp, sizeof (root)); } cookie = setzoneent(); while ((name = getzoneent(cookie)) != NULL) { /* Skip the global zone */ if (strcmp(name, GLOBAL_ZONENAME) == 0) { free(name); continue; } if (zone_get_zonepath(name, zroot, sizeof (zroot)) != Z_OK) { free(name); continue; } bzero(rzroot, sizeof (rzroot)); if (resolvepath(zroot, rzroot, sizeof (rzroot) - 1) == -1) { /* * Zone path doesn't exist, or other misc error, * so we try using the non-resolved pathname. */ (void) strlcpy(rzroot, zroot, sizeof (rzroot)); } if (strlen(rzroot) > 0 && (rzroot[strlen(rzroot) - 1] != '/')) { (void) snprintf(tmp, sizeof (tmp), "%s/", rzroot); (void) strlcpy(rzroot, tmp, sizeof (rzroot)); } /* * Finally, the comparison. If the zone root path is a * leading substring of the root path, fail. */ if (strncmp(rzroot, root, strlen(rzroot)) == 0) { err_print(ZONE_PATHCHECK, root, name); err = DEVFSADM_FAILURE; free(name); break; } free(name); } endzoneent(cookie); (void) dlclose(dlhdl); return (err); } /* * Called by the daemon when it receives an event from the devfsadm SLM * to syseventd. * * The devfsadm SLM uses a private event channel for communication to * devfsadmd set-up via private libsysevent interfaces. This handler is * used to bind to the devfsadmd channel for event delivery. * The devfsadmd SLM insures single calls to this routine as well as * synchronized event delivery. * */ static void event_handler(sysevent_t *ev) { char *path; char *minor; char *subclass; char *dev_ev_subclass; char *driver_name; nvlist_t *attr_list = NULL; int err = 0; int instance; int branch_event = 0; subclass = sysevent_get_subclass_name(ev); vprint(EVENT_MID, "event_handler: %s id:0X%llx\n", subclass, sysevent_get_seq(ev)); if (strcmp(subclass, ESC_DEVFS_START) == 0) { return; } /* Check if event is an instance modification */ if (strcmp(subclass, ESC_DEVFS_INSTANCE_MOD) == 0) { devfs_instance_mod(); return; } if (sysevent_get_attr_list(ev, &attr_list) != 0) { vprint(EVENT_MID, "event_handler: can not get attr list\n"); return; } if (strcmp(subclass, ESC_DEVFS_DEVI_ADD) == 0 || strcmp(subclass, ESC_DEVFS_DEVI_REMOVE) == 0 || strcmp(subclass, ESC_DEVFS_MINOR_CREATE) == 0 || strcmp(subclass, ESC_DEVFS_MINOR_REMOVE) == 0) { if ((err = nvlist_lookup_string(attr_list, DEVFS_PATHNAME, &path)) != 0) goto out; if (nvlist_lookup_string(attr_list, DEVFS_DEVI_CLASS, &dev_ev_subclass) != 0) dev_ev_subclass = NULL; if (nvlist_lookup_string(attr_list, DEVFS_DRIVER_NAME, &driver_name) != 0) driver_name = NULL; if (nvlist_lookup_int32(attr_list, DEVFS_INSTANCE, &instance) != 0) instance = -1; if (nvlist_lookup_int32(attr_list, DEVFS_BRANCH_EVENT, &branch_event) != 0) branch_event = 0; if (nvlist_lookup_string(attr_list, DEVFS_MINOR_NAME, &minor) != 0) minor = NULL; lock_dev(); if (strcmp(ESC_DEVFS_DEVI_ADD, subclass) == 0) { add_minor_pathname(path, NULL, dev_ev_subclass); if (branch_event) { build_and_enq_event(EC_DEV_BRANCH, ESC_DEV_BRANCH_ADD, path, DI_NODE_NIL, NULL); } } else if (strcmp(ESC_DEVFS_MINOR_CREATE, subclass) == 0) { add_minor_pathname(path, minor, dev_ev_subclass); } else if (strcmp(ESC_DEVFS_MINOR_REMOVE, subclass) == 0) { hot_cleanup(path, minor, dev_ev_subclass, driver_name, instance); } else { /* ESC_DEVFS_DEVI_REMOVE */ hot_cleanup(path, NULL, dev_ev_subclass, driver_name, instance); if (branch_event) { build_and_enq_event(EC_DEV_BRANCH, ESC_DEV_BRANCH_REMOVE, path, DI_NODE_NIL, NULL); } } unlock_dev(CACHE_STATE); } else if (strcmp(subclass, ESC_DEVFS_BRANCH_ADD) == 0 || strcmp(subclass, ESC_DEVFS_BRANCH_REMOVE) == 0) { if ((err = nvlist_lookup_string(attr_list, DEVFS_PATHNAME, &path)) != 0) goto out; /* just log ESC_DEV_BRANCH... event */ if (strcmp(subclass, ESC_DEVFS_BRANCH_ADD) == 0) dev_ev_subclass = ESC_DEV_BRANCH_ADD; else dev_ev_subclass = ESC_DEV_BRANCH_REMOVE; lock_dev(); build_and_enq_event(EC_DEV_BRANCH, dev_ev_subclass, path, DI_NODE_NIL, NULL); unlock_dev(CACHE_STATE); } else err_print(UNKNOWN_EVENT, subclass); out: if (err) err_print(EVENT_ATTR_LOOKUP_FAILED, strerror(err)); nvlist_free(attr_list); } static void dca_impl_init(char *root, char *minor, struct dca_impl *dcip) { assert(root); dcip->dci_root = root; dcip->dci_minor = minor; dcip->dci_driver = NULL; dcip->dci_error = 0; dcip->dci_flags = 0; dcip->dci_arg = NULL; } /* * Kernel logs a message when a devinfo node is attached. Try to create * /dev and /devices for each minor node. minorname can be NULL. */ void add_minor_pathname(char *node, char *minor, char *ev_subclass) { struct dca_impl dci; vprint(CHATTY_MID, "add_minor_pathname: node_path=%s minor=%s\n", node, minor ? minor : "NULL"); dca_impl_init(node, minor, &dci); /* * Restrict hotplug link creation if daemon * started with -i option. */ if (single_drv == TRUE) { dci.dci_driver = driver; } /* * We are being invoked in response to a hotplug * event. Also, notify RCM if nodetype indicates * a network device has been hotplugged. */ dci.dci_flags = DCA_HOT_PLUG | DCA_CHECK_TYPE; devi_tree_walk(&dci, DINFOPROP|DINFOMINOR, ev_subclass); } static di_node_t find_clone_node() { static di_node_t clone_node = DI_NODE_NIL; if (clone_node == DI_NODE_NIL) clone_node = di_init("/pseudo/clone@0", DINFOPROP); return (clone_node); } static int is_descendent_of(di_node_t node, char *driver) { while (node != DI_NODE_NIL) { char *drv = di_driver_name(node); if (strcmp(drv, driver) == 0) return (1); node = di_parent_node(node); } return (0); } /* * Checks the minor type. If it is an alias node, then lookup * the real node/minor first, then call minor_process() to * do the real work. */ static int check_minor_type(di_node_t node, di_minor_t minor, void *arg) { ddi_minor_type minor_type; di_node_t clone_node; char *mn; char *nt; struct mlist *dep; struct dca_impl *dcip = arg; assert(dcip); dep = dcip->dci_arg; mn = di_minor_name(minor); /* * We match driver here instead of in minor_process * as we want the actual driver name. This check is * unnecessary during deferred processing. */ if (dep && ((dcip->dci_driver && !is_descendent_of(node, dcip->dci_driver)) || (dcip->dci_minor && strcmp(mn, dcip->dci_minor)))) { return (DI_WALK_CONTINUE); } if ((dcip->dci_flags & DCA_CHECK_TYPE) && (nt = di_minor_nodetype(minor)) && (strcmp(nt, DDI_NT_NET) == 0)) { dcip->dci_flags |= DCA_NOTIFY_RCM; dcip->dci_flags &= ~DCA_CHECK_TYPE; } minor_type = di_minor_type(minor); if (minor_type == DDM_MINOR) { minor_process(node, minor, dep); } else if (minor_type == DDM_ALIAS) { struct mlist *cdep, clone_del = {0}; clone_node = find_clone_node(); if (clone_node == DI_NODE_NIL) { err_print(DI_INIT_FAILED, "clone", strerror(errno)); return (DI_WALK_CONTINUE); } cdep = dep ? &clone_del : NULL; minor_process(clone_node, minor, cdep); /* * cache "alias" minor node and free "clone" minor */ if (cdep != NULL && cdep->head != NULL) { assert(cdep->tail != NULL); cache_deferred_minor(dep, node, minor); dcip->dci_arg = cdep; process_deferred_links(dcip, DCA_FREE_LIST); dcip->dci_arg = dep; } } return (DI_WALK_CONTINUE); } /* * This is the entry point for each minor node, whether walking * the entire tree via di_walk_minor() or processing a hotplug event * for a single devinfo node (via hotplug ndi_devi_online()). */ /*ARGSUSED*/ static void minor_process(di_node_t node, di_minor_t minor, struct mlist *dep) { create_list_t *create; int defer; vprint(CHATTY_MID, "minor_process: node=%s, minor=%s\n", di_node_name(node), di_minor_name(minor)); if (dep != NULL) { /* * Reset /devices node to minor_perm perm/ownership * if we are here to deactivate device allocation */ if (build_devices == TRUE) { reset_node_permissions(node, minor); } if (build_dev == FALSE) { return; } /* * This function will create any nodes for /etc/devlink.tab. * If devlink.tab handles link creation, we don't call any * devfsadm modules since that could cause duplicate caching * in the enumerate functions if different re strings are * passed that are logically identical. I'm still not * convinced this would cause any harm, but better to be safe. * * Deferred processing is available only for devlinks * created through devfsadm modules. */ if (process_devlink_compat(minor, node) == TRUE) { return; } } else { vprint(CHATTY_MID, "minor_process: deferred processing\n"); } /* * look for relevant link create rules in the modules, and * invoke the link create callback function to build a link * if there is a match. */ defer = 0; for (create = create_head; create != NULL; create = create->next) { if ((minor_matches_rule(node, minor, create) == TRUE) && class_ok(create->create->device_class) == DEVFSADM_SUCCESS) { if (call_minor_init(create->modptr) == DEVFSADM_FAILURE) { continue; } /* * If NOT doing the deferred creates (i.e. 1st pass) and * rule requests deferred processing cache the minor * data. * * If deferred processing (2nd pass), create links * ONLY if rule requests deferred processing. */ if (dep && ((create->create->flags & CREATE_MASK) == CREATE_DEFER)) { defer = 1; continue; } else if (dep == NULL && ((create->create->flags & CREATE_MASK) != CREATE_DEFER)) { continue; } if ((*(create->create->callback_fcn)) (minor, node) == DEVFSADM_TERMINATE) { break; } } } if (defer) cache_deferred_minor(dep, node, minor); } /* * Cache node and minor in defer list. */ static void cache_deferred_minor( struct mlist *dep, di_node_t node, di_minor_t minor) { struct minor *mp; const char *fcn = "cache_deferred_minor"; vprint(CHATTY_MID, "%s node=%s, minor=%s\n", fcn, di_node_name(node), di_minor_name(minor)); if (dep == NULL) { vprint(CHATTY_MID, "%s: cannot cache during " "deferred processing. Ignoring minor\n", fcn); return; } mp = (struct minor *)s_zalloc(sizeof (struct minor)); mp->node = node; mp->minor = minor; mp->next = NULL; assert(dep->head == NULL || dep->tail != NULL); if (dep->head == NULL) { dep->head = mp; } else { dep->tail->next = mp; } dep->tail = mp; } /* * Check to see if "create" link creation rule matches this node/minor. * If it does, return TRUE. */ static int minor_matches_rule(di_node_t node, di_minor_t minor, create_list_t *create) { char *m_nodetype, *m_drvname; if (create->create->node_type != NULL) { m_nodetype = di_minor_nodetype(minor); assert(m_nodetype != NULL); switch (create->create->flags & TYPE_MASK) { case TYPE_EXACT: if (strcmp(create->create->node_type, m_nodetype) != 0) { return (FALSE); } break; case TYPE_PARTIAL: if (strncmp(create->create->node_type, m_nodetype, strlen(create->create->node_type)) != 0) { return (FALSE); } break; case TYPE_RE: if (regexec(&(create->node_type_comp), m_nodetype, 0, NULL, 0) != 0) { return (FALSE); } break; } } if (create->create->drv_name != NULL) { m_drvname = di_driver_name(node); switch (create->create->flags & DRV_MASK) { case DRV_EXACT: if (strcmp(create->create->drv_name, m_drvname) != 0) { return (FALSE); } break; case DRV_RE: if (regexec(&(create->drv_name_comp), m_drvname, 0, NULL, 0) != 0) { return (FALSE); } break; } } return (TRUE); } /* * If no classes were given on the command line, then return DEVFSADM_SUCCESS. * Otherwise, return DEVFSADM_SUCCESS if the device "class" from the module * matches one of the device classes given on the command line, * otherwise, return DEVFSADM_FAILURE. */ static int class_ok(char *class) { int i; if (num_classes == 0) { return (DEVFSADM_SUCCESS); } for (i = 0; i < num_classes; i++) { if (strcmp(class, classes[i]) == 0) { return (DEVFSADM_SUCCESS); } } return (DEVFSADM_FAILURE); } /* * call minor_fini on active modules, then unload ALL modules */ static void unload_modules(void) { module_t *module_free; create_list_t *create_free; remove_list_t *remove_free; while (create_head != NULL) { create_free = create_head; create_head = create_head->next; if ((create_free->create->flags & TYPE_RE) == TYPE_RE) { regfree(&(create_free->node_type_comp)); } if ((create_free->create->flags & DRV_RE) == DRV_RE) { regfree(&(create_free->drv_name_comp)); } free(create_free); } while (remove_head != NULL) { remove_free = remove_head; remove_head = remove_head->next; free(remove_free); } while (module_head != NULL) { if ((module_head->minor_fini != NULL) && ((module_head->flags & MODULE_ACTIVE) == MODULE_ACTIVE)) { (void) (*(module_head->minor_fini))(); } vprint(MODLOAD_MID, "unloading module %s\n", module_head->name); free(module_head->name); (void) dlclose(module_head->dlhandle); module_free = module_head; module_head = module_head->next; free(module_free); } } /* * Load devfsadm logical link processing modules. */ static void load_modules(void) { DIR *mod_dir; struct dirent *entp; char cdir[PATH_MAX + 1]; char *last; char *mdir = module_dirs; char *fcn = "load_modules: "; while (*mdir != '\0') { while (*mdir == ':') { mdir++; } if (*mdir == '\0') { continue; } last = strchr(mdir, ':'); if (last == NULL) { last = mdir + strlen(mdir); } (void) strncpy(cdir, mdir, last - mdir); cdir[last - mdir] = '\0'; mdir += strlen(cdir); if ((mod_dir = opendir(cdir)) == NULL) { vprint(MODLOAD_MID, "%sopendir(%s): %s\n", fcn, cdir, strerror(errno)); continue; } while ((entp = readdir(mod_dir)) != NULL) { if ((strcmp(entp->d_name, ".") == 0) || (strcmp(entp->d_name, "..") == 0)) { continue; } load_module(entp->d_name, cdir); } s_closedir(mod_dir); } } static void load_module(char *mname, char *cdir) { _devfsadm_create_reg_t *create_reg; _devfsadm_remove_reg_V1_t *remove_reg; create_list_t *create_list_element; create_list_t **create_list_next; remove_list_t *remove_list_element; remove_list_t **remove_list_next; char epath[PATH_MAX + 1], *end; char *fcn = "load_module: "; char *dlerrstr; void *dlhandle; module_t *module; int flags; int n; int i; /* ignore any file which does not end in '.so' */ if ((end = strstr(mname, MODULE_SUFFIX)) != NULL) { if (end[strlen(MODULE_SUFFIX)] != '\0') { return; } } else { return; } (void) snprintf(epath, sizeof (epath), "%s/%s", cdir, mname); if ((dlhandle = dlopen(epath, RTLD_LAZY)) == NULL) { dlerrstr = dlerror(); err_print(DLOPEN_FAILED, epath, dlerrstr ? dlerrstr : "unknown error"); return; } /* dlsym the _devfsadm_create_reg structure */ if (NULL == (create_reg = (_devfsadm_create_reg_t *) dlsym(dlhandle, _DEVFSADM_CREATE_REG))) { vprint(MODLOAD_MID, "dlsym(%s, %s): symbol not found\n", epath, _DEVFSADM_CREATE_REG); } else { vprint(MODLOAD_MID, "%sdlsym(%s, %s) succeeded\n", fcn, epath, _DEVFSADM_CREATE_REG); } /* dlsym the _devfsadm_remove_reg structure */ if (NULL == (remove_reg = (_devfsadm_remove_reg_V1_t *) dlsym(dlhandle, _DEVFSADM_REMOVE_REG))) { vprint(MODLOAD_MID, "dlsym(%s,\n\t%s): symbol not found\n", epath, _DEVFSADM_REMOVE_REG); } else { vprint(MODLOAD_MID, "dlsym(%s, %s): succeeded\n", epath, _DEVFSADM_REMOVE_REG); } vprint(MODLOAD_MID, "module %s loaded\n", epath); module = (module_t *)s_malloc(sizeof (module_t)); module->name = s_strdup(epath); module->dlhandle = dlhandle; /* dlsym other module functions, to be called later */ module->minor_fini = (int (*)())dlsym(dlhandle, MINOR_FINI); module->minor_init = (int (*)())dlsym(dlhandle, MINOR_INIT); module->flags = 0; /* * put a ptr to each struct devfsadm_create on "create_head" * list sorted in interpose_lvl. */ if (create_reg != NULL) { for (i = 0; i < create_reg->count; i++) { int flags = create_reg->tblp[i].flags; create_list_element = (create_list_t *) s_malloc(sizeof (create_list_t)); create_list_element->create = &(create_reg->tblp[i]); create_list_element->modptr = module; if (((flags & CREATE_MASK) != 0) && ((flags & CREATE_MASK) != CREATE_DEFER)) { free(create_list_element); err_print("illegal flag combination in " "module create\n"); err_print(IGNORING_ENTRY, i, epath); continue; } if (((flags & TYPE_MASK) == 0) ^ (create_reg->tblp[i].node_type == NULL)) { free(create_list_element); err_print("flags value incompatible with " "node_type value in module create\n"); err_print(IGNORING_ENTRY, i, epath); continue; } if (((flags & TYPE_MASK) != 0) && ((flags & TYPE_MASK) != TYPE_EXACT) && ((flags & TYPE_MASK) != TYPE_RE) && ((flags & TYPE_MASK) != TYPE_PARTIAL)) { free(create_list_element); err_print("illegal TYPE_* flag combination in " "module create\n"); err_print(IGNORING_ENTRY, i, epath); continue; } /* precompile regular expression for efficiency */ if ((flags & TYPE_RE) == TYPE_RE) { if ((n = regcomp(&(create_list_element-> node_type_comp), create_reg->tblp[i].node_type, REG_EXTENDED)) != 0) { free(create_list_element); err_print(REGCOMP_FAILED, create_reg->tblp[i].node_type, n); err_print(IGNORING_ENTRY, i, epath); continue; } } if (((flags & DRV_MASK) == 0) ^ (create_reg->tblp[i].drv_name == NULL)) { if ((flags & TYPE_RE) == TYPE_RE) { regfree(&(create_list_element-> node_type_comp)); } free(create_list_element); err_print("flags value incompatible with " "drv_name value in module create\n"); err_print(IGNORING_ENTRY, i, epath); continue; } if (((flags & DRV_MASK) != 0) && ((flags & DRV_MASK) != DRV_EXACT) && ((flags & DRV_MASK) != DRV_RE)) { if ((flags & TYPE_RE) == TYPE_RE) { regfree(&(create_list_element-> node_type_comp)); } free(create_list_element); err_print("illegal DRV_* flag combination in " "module create\n"); err_print(IGNORING_ENTRY, i, epath); continue; } /* precompile regular expression for efficiency */ if ((create_reg->tblp[i].flags & DRV_RE) == DRV_RE) { if ((n = regcomp(&(create_list_element-> drv_name_comp), create_reg->tblp[i].drv_name, REG_EXTENDED)) != 0) { if ((flags & TYPE_RE) == TYPE_RE) { regfree(&(create_list_element-> node_type_comp)); } free(create_list_element); err_print(REGCOMP_FAILED, create_reg->tblp[i].drv_name, n); err_print(IGNORING_ENTRY, i, epath); continue; } } /* add to list sorted by interpose level */ for (create_list_next = &(create_head); (*create_list_next != NULL) && (*create_list_next)->create->interpose_lvl >= create_list_element->create->interpose_lvl; create_list_next = &((*create_list_next)->next)) ; create_list_element->next = *create_list_next; *create_list_next = create_list_element; } } /* * put a ptr to each struct devfsadm_remove on "remove_head" * list sorted by interpose_lvl. */ flags = 0; if (remove_reg != NULL) { if (remove_reg->version < DEVFSADM_V1) flags |= RM_NOINTERPOSE; for (i = 0; i < remove_reg->count; i++) { remove_list_element = (remove_list_t *) s_malloc(sizeof (remove_list_t)); remove_list_element->remove = &(remove_reg->tblp[i]); remove_list_element->remove->flags |= flags; remove_list_element->modptr = module; for (remove_list_next = &(remove_head); (*remove_list_next != NULL) && (*remove_list_next)->remove->interpose_lvl >= remove_list_element->remove->interpose_lvl; remove_list_next = &((*remove_list_next)->next)) ; remove_list_element->next = *remove_list_next; *remove_list_next = remove_list_element; } } module->next = module_head; module_head = module; } /* * After we have completed a CACHE_STATE, if a SYNC_STATE does not occur * within 'timeout' secs the minor_fini_thread needs to do a SYNC_STATE * so that we still call the minor_fini routines. */ /*ARGSUSED*/ static void minor_fini_thread(void *arg) { timestruc_t abstime; vprint(INITFINI_MID, "minor_fini_thread starting\n"); (void) mutex_lock(&minor_fini_mutex); for (;;) { /* wait the gather period, or until signaled */ abstime.tv_sec = time(NULL) + minor_fini_timeout; abstime.tv_nsec = 0; (void) cond_timedwait(&minor_fini_cv, &minor_fini_mutex, &abstime); /* if minor_fini was canceled, go wait again */ if (minor_fini_canceled == TRUE) continue; /* if minor_fini was delayed, go wait again */ if (minor_fini_delayed == TRUE) { minor_fini_delayed = FALSE; continue; } /* done with cancellations and delays, do the SYNC_STATE */ (void) mutex_unlock(&minor_fini_mutex); lock_dev(); unlock_dev(SYNC_STATE); vprint(INITFINI_MID, "minor_fini sync done\n"); (void) mutex_lock(&minor_fini_mutex); } } /* * Attempt to initialize module, if a minor_init routine exists. Set * the active flag if the routine exists and succeeds. If it doesn't * exist, just set the active flag. */ static int call_minor_init(module_t *module) { char *fcn = "call_minor_init: "; if ((module->flags & MODULE_ACTIVE) == MODULE_ACTIVE) { return (DEVFSADM_SUCCESS); } vprint(INITFINI_MID, "%smodule %s. current state: inactive\n", fcn, module->name); if (module->minor_init == NULL) { module->flags |= MODULE_ACTIVE; vprint(INITFINI_MID, "minor_init not defined\n"); return (DEVFSADM_SUCCESS); } if ((*(module->minor_init))() == DEVFSADM_FAILURE) { err_print(FAILED_FOR_MODULE, MINOR_INIT, module->name); return (DEVFSADM_FAILURE); } vprint(INITFINI_MID, "minor_init() returns DEVFSADM_SUCCESS. " "new state: active\n"); module->flags |= MODULE_ACTIVE; return (DEVFSADM_SUCCESS); } /* * Creates a symlink 'link' to the physical path of node:minor. * Construct link contents, then call create_link_common(). */ /*ARGSUSED*/ int devfsadm_mklink(char *link, di_node_t node, di_minor_t minor, int flags) { char rcontents[PATH_MAX]; char devlink[PATH_MAX]; char phy_path[PATH_MAX]; char *acontents; char *dev_path; int numslashes; int rv; int i, link_exists; int last_was_slash = FALSE; /* * try to use devices path */ if ((node == lnode) && (minor == lminor)) { acontents = lphy_path; } else if (di_minor_type(minor) == DDM_ALIAS) { /* use /pseudo/clone@0: as the phys path */ (void) snprintf(phy_path, sizeof (phy_path), "/pseudo/clone@0:%s", di_driver_name(di_minor_devinfo(minor))); acontents = phy_path; } else { if ((dev_path = di_devfs_path(node)) == NULL) { err_print(DI_DEVFS_PATH_FAILED, strerror(errno)); devfsadm_exit(1); } (void) snprintf(phy_path, sizeof (phy_path), "%s:%s", dev_path, di_minor_name(minor)); di_devfs_path_free(dev_path); acontents = phy_path; } /* prepend link with dev_dir contents */ (void) strlcpy(devlink, dev_dir, sizeof (devlink)); (void) strlcat(devlink, "/", sizeof (devlink)); (void) strlcat(devlink, link, sizeof (devlink)); /* * Calculate # of ../ to add. Account for double '//' in path. * Ignore all leading slashes. */ for (i = 0; link[i] == '/'; i++) ; for (numslashes = 0; link[i] != '\0'; i++) { if (link[i] == '/') { if (last_was_slash == FALSE) { numslashes++; last_was_slash = TRUE; } } else { last_was_slash = FALSE; } } /* Don't count any trailing '/' */ if (link[i-1] == '/') { numslashes--; } rcontents[0] = '\0'; do { (void) strlcat(rcontents, "../", sizeof (rcontents)); } while (numslashes-- != 0); (void) strlcat(rcontents, "devices", sizeof (rcontents)); (void) strlcat(rcontents, acontents, sizeof (rcontents)); if (devlinks_debug == TRUE) { vprint(INFO_MID, "adding link %s ==> %s\n", devlink, rcontents); } if ((rv = create_link_common(devlink, rcontents, &link_exists)) == DEVFSADM_SUCCESS) { linknew = TRUE; add_link_to_cache(link, acontents); } else { linknew = FALSE; } if (link_exists == TRUE) { /* Link exists or was just created */ (void) di_devlink_add_link(devlink_cache, link, rcontents, DI_PRIMARY_LINK); if (system_labeled && (flags & DA_ADD)) { /* * Add this to the list of allocatable devices. If this * is a hotplugged, removable disk, add it as rmdisk. */ int instance = di_instance(node); if ((flags & DA_CD) && (_da_check_for_usb(devlink, root_dir) == 1)) { (void) da_add_list(&devlist, devlink, instance, DA_ADD|DA_RMDISK); update_devdb = DA_RMDISK; } else if (linknew == TRUE) { (void) da_add_list(&devlist, devlink, instance, flags); update_devdb = flags; } } } return (rv); } /* * Creates a symlink link to primary_link. Calculates relative * directory offsets, then calls link_common(). */ /*ARGSUSED*/ int devfsadm_secondary_link(char *link, char *primary_link, int flags) { char contents[PATH_MAX + 1]; char devlink[PATH_MAX + 1]; int rv, link_exists; char *fpath; char *tpath; char *op; /* prepend link with dev_dir contents */ (void) strcpy(devlink, dev_dir); (void) strcat(devlink, "/"); (void) strcat(devlink, link); /* * building extra link, so use first link as link contents, but first * make it relative. */ fpath = link; tpath = primary_link; op = contents; while (*fpath == *tpath && *fpath != '\0') { fpath++, tpath++; } /* Count directories to go up, if any, and add "../" */ while (*fpath != '\0') { if (*fpath == '/') { (void) strcpy(op, "../"); op += 3; } fpath++; } /* * Back up to the start of the current path component, in * case in the middle */ while (tpath != primary_link && *(tpath-1) != '/') { tpath--; } (void) strcpy(op, tpath); if (devlinks_debug == TRUE) { vprint(INFO_MID, "adding extra link %s ==> %s\n", devlink, contents); } if ((rv = create_link_common(devlink, contents, &link_exists)) == DEVFSADM_SUCCESS) { /* * we need to save the ultimate /devices contents, and not the * secondary link, since hotcleanup only looks at /devices path. * Since we don't have devices path here, we can try to get it * by readlink'ing the secondary link. This assumes the primary * link was created first. */ add_link_to_cache(link, lphy_path); linknew = TRUE; if (system_labeled && ((flags & DA_AUDIO) && (flags & DA_ADD))) { /* * Add this device to the list of allocatable devices. */ int instance = 0; op = strrchr(contents, '/'); op++; (void) sscanf(op, "%d", &instance); (void) da_add_list(&devlist, devlink, instance, flags); update_devdb = flags; } } else { linknew = FALSE; } /* * If link exists or was just created, add it to the database */ if (link_exists == TRUE) { (void) di_devlink_add_link(devlink_cache, link, contents, DI_SECONDARY_LINK); } return (rv); } /* returns pointer to the devices directory */ char * devfsadm_get_devices_dir() { return (devices_dir); } /* * Does the actual link creation. VERBOSE_MID only used if there is * a change. CHATTY_MID used otherwise. */ static int create_link_common(char *devlink, char *contents, int *exists) { int try; int linksize; int max_tries = 0; static int prev_link_existed = TRUE; char checkcontents[PATH_MAX + 1]; char *hide; *exists = FALSE; /* Database is not updated when file_mods == FALSE */ if (file_mods == FALSE) { linksize = readlink(devlink, checkcontents, PATH_MAX); if (linksize > 0) { checkcontents[linksize] = '\0'; if (strcmp(checkcontents, contents) != 0) { vprint(CHATTY_MID, REMOVING_LINK, devlink, checkcontents); return (DEVFSADM_SUCCESS); } else { vprint(CHATTY_MID, "link exists and is correct:" " %s -> %s\n", devlink, contents); /* failure only in that the link existed */ return (DEVFSADM_FAILURE); } } else { vprint(VERBOSE_MID, CREATING_LINK, devlink, contents); return (DEVFSADM_SUCCESS); } } /* * systems calls are expensive, so predict whether to readlink * or symlink first, based on previous attempt */ if (prev_link_existed == FALSE) { try = CREATE_LINK; } else { try = READ_LINK; } while (++max_tries <= 3) { switch (try) { case CREATE_LINK: if (symlink(contents, devlink) == 0) { vprint(VERBOSE_MID, CREATING_LINK, devlink, contents); prev_link_existed = FALSE; /* link successfully created */ *exists = TRUE; set_logindev_perms(devlink); return (DEVFSADM_SUCCESS); } else { switch (errno) { case ENOENT: /* dirpath to node doesn't exist */ hide = strrchr(devlink, '/'); *hide = '\0'; s_mkdirp(devlink, S_IRWXU|S_IRGRP| S_IXGRP|S_IROTH|S_IXOTH); *hide = '/'; break; case EEXIST: try = READ_LINK; break; default: err_print(SYMLINK_FAILED, devlink, contents, strerror(errno)); return (DEVFSADM_FAILURE); } } break; case READ_LINK: linksize = readlink(devlink, checkcontents, PATH_MAX); if (linksize >= 0) { checkcontents[linksize] = '\0'; if (strcmp(checkcontents, contents) != 0) { s_unlink(devlink); vprint(VERBOSE_MID, REMOVING_LINK, devlink, checkcontents); try = CREATE_LINK; } else { prev_link_existed = TRUE; vprint(CHATTY_MID, "link exists and is correct:" " %s -> %s\n", devlink, contents); *exists = TRUE; /* failure in that the link existed */ return (DEVFSADM_FAILURE); } } else { switch (errno) { case EINVAL: /* not a symlink, remove and create */ s_unlink(devlink); default: /* maybe it didn't exist at all */ try = CREATE_LINK; break; } } break; } } err_print(MAX_ATTEMPTS, devlink, contents); return (DEVFSADM_FAILURE); } static void set_logindev_perms(char *devlink) { struct login_dev *newdev; struct passwd pwd, *resp; char pwd_buf[PATH_MAX]; int rv; struct stat sb; char *devfs_path = NULL; /* * We only want logindev perms to be set when a device is * hotplugged or an application requests synchronous creates. * So we enable this only in daemon mode. In addition, * login(1) only fixes the std. /dev dir. So we don't * change perms if alternate root is set. * login_dev_enable is TRUE only in these cases. */ if (login_dev_enable != TRUE) return; /* * Normally, /etc/logindevperm has few (8 - 10 entries) which * may be regular expressions (globs were converted to RE). * So just do a linear search through the list. */ for (newdev = login_dev_cache; newdev; newdev = newdev->ldev_next) { vprint(FILES_MID, "matching %s with %s\n", devlink, newdev->ldev_device); if (regexec(&newdev->ldev_device_regex, devlink, 0, NULL, 0) == 0) { vprint(FILES_MID, "matched %s with %s\n", devlink, newdev->ldev_device); break; } } if (newdev == NULL) return; /* * we have a match, now find the driver associated with this * minor node using a snapshot on the physical path */ (void) resolve_link(devlink, NULL, NULL, &devfs_path, 0); if (devfs_path) { di_node_t node; char *drv = NULL; struct driver_list *list; char *p; /* truncate on : so we can take a snapshot */ (void) strcpy(pwd_buf, devfs_path); p = strrchr(pwd_buf, ':'); if (p == NULL) { free(devfs_path); return; } *p = '\0'; vprint(FILES_MID, "link=%s->physpath=%s\n", devlink, pwd_buf); node = di_init(pwd_buf, DINFOMINOR); if (node) { drv = di_driver_name(node); if (drv) { vprint(FILES_MID, "%s: driver is %s\n", devlink, drv); } di_fini(node); } /* search thru the driver list specified in logindevperm */ list = newdev->ldev_driver_list; if ((drv != NULL) && (list != NULL)) { while (list) { if (strcmp(list->driver_name, drv) == 0) { vprint(FILES_MID, "driver %s match!\n", drv); break; } list = list->next; } if (list == NULL) { vprint(FILES_MID, "no driver match!\n"); free(devfs_path); return; } } free(devfs_path); } else { return; } vprint(FILES_MID, "changing permissions of %s\n", devlink); /* * We have a match. We now attempt to determine the * owner and group of the console user. * * stat() the console device newdev->ldev_console * which will always exist - it will have the right owner but * not the right group. Use getpwuid_r() to determine group for this * uid. * Note, it is safe to use name service here since if name services * are not available (during boot or in single-user mode), then * console owner will be root and its gid can be found in * local files. */ if (stat(newdev->ldev_console, &sb) == -1) { vprint(VERBOSE_MID, STAT_FAILED, newdev->ldev_console, strerror(errno)); return; } resp = NULL; rv = getpwuid_r(sb.st_uid, &pwd, pwd_buf, sizeof (pwd_buf), &resp); if (rv || resp == NULL) { rv = rv ? rv : EINVAL; vprint(VERBOSE_MID, GID_FAILED, sb.st_uid, strerror(rv)); return; } assert(&pwd == resp); sb.st_gid = resp->pw_gid; if (chmod(devlink, newdev->ldev_perms) == -1) { vprint(VERBOSE_MID, CHMOD_FAILED, devlink, strerror(errno)); return; } if (chown(devlink, sb.st_uid, sb.st_gid) == -1) { vprint(VERBOSE_MID, CHOWN_FAILED, devlink, strerror(errno)); } } /* * Reset /devices node with appropriate permissions and * ownership as specified in /etc/minor_perm. */ static void reset_node_permissions(di_node_t node, di_minor_t minor) { int spectype; char phy_path[PATH_MAX + 1]; mode_t mode; dev_t dev; uid_t uid; gid_t gid; struct stat sb; char *dev_path, *aminor = NULL; /* lphy_path starts with / */ if ((dev_path = di_devfs_path(node)) == NULL) { err_print(DI_DEVFS_PATH_FAILED, strerror(errno)); devfsadm_exit(1); } (void) strcpy(lphy_path, dev_path); di_devfs_path_free(dev_path); (void) strcat(lphy_path, ":"); if (di_minor_type(minor) == DDM_ALIAS) { char *driver; aminor = di_minor_name(minor); driver = di_driver_name(di_minor_devinfo(minor)); (void) strcat(lphy_path, driver); } else (void) strcat(lphy_path, di_minor_name(minor)); (void) strcpy(phy_path, devices_dir); (void) strcat(phy_path, lphy_path); lnode = node; lminor = minor; vprint(CHATTY_MID, "reset_node_permissions: phy_path=%s lphy_path=%s\n", phy_path, lphy_path); dev = di_minor_devt(minor); spectype = di_minor_spectype(minor); /* block or char */ getattr(phy_path, aminor, spectype, dev, &mode, &uid, &gid); /* * compare and set permissions and ownership * * Under devfs, a quick insertion and removal of USB devices * would cause stat of physical path to fail. In this case, * we emit a verbose message, but don't print errors. */ if ((stat(phy_path, &sb) == -1) || (sb.st_rdev != dev)) { vprint(VERBOSE_MID, NO_DEVFS_NODE, phy_path); return; } /* * If we are here for a new device * If device allocation is on * then * set ownership to root:other and permissions to 0000 * else * set ownership and permissions as specified in minor_perm * If we are here for an existing device * If device allocation is to be turned on * then * reset ownership to root:other and permissions to 0000 * else if device allocation is to be turned off * reset ownership and permissions to those specified in * minor_perm * else * preserve existing/user-modified ownership and * permissions * * devfs indicates a new device by faking access time to be zero. */ if (sb.st_atime != 0) { int i; char *nt; if ((devalloc_flag == 0) && (devalloc_is_on != 1)) /* * Leave existing devices as they are if we are not * turning device allocation on/off. */ return; nt = di_minor_nodetype(minor); if (nt == NULL) return; for (i = 0; devalloc_list[i]; i++) { if (strcmp(nt, devalloc_list[i]) == 0) /* * One of the types recognized by devalloc, * reset attrs. */ break; } if (devalloc_list[i] == NULL) return; } if (file_mods == FALSE) { /* Nothing more to do if simulating */ vprint(VERBOSE_MID, PERM_MSG, phy_path, uid, gid, mode); return; } if ((devalloc_flag == DA_ON) || (devalloc_is_on == 1)) { /* * we are here either to turn device allocation on * or to add a new device while device allocation in on */ mode = DEALLOC_MODE; uid = DA_UID; gid = DA_GID; } if ((devalloc_is_on == 1) || (devalloc_flag == DA_ON) || (sb.st_mode != mode)) { if (chmod(phy_path, mode) == -1) vprint(VERBOSE_MID, CHMOD_FAILED, phy_path, strerror(errno)); } if ((devalloc_is_on == 1) || (devalloc_flag == DA_ON) || (sb.st_uid != uid || sb.st_gid != gid)) { if (chown(phy_path, uid, gid) == -1) vprint(VERBOSE_MID, CHOWN_FAILED, phy_path, strerror(errno)); } /* Report that we actually did something */ vprint(VERBOSE_MID, PERM_MSG, phy_path, uid, gid, mode); } /* * Removes logical link and the minor node it refers to. If file is a * link, we recurse and try to remove the minor node (or link if path is * a double link) that file's link contents refer to. */ static void devfsadm_rm_work(char *file, int recurse, int file_type) { char *fcn = "devfsadm_rm_work: "; int linksize; char contents[PATH_MAX + 1]; char nextfile[PATH_MAX + 1]; char newfile[PATH_MAX + 1]; char *ptr; vprint(REMOVE_MID, "%s%s\n", fcn, file); /* TYPE_LINK split into multiple if's due to excessive indentations */ if (file_type == TYPE_LINK) { (void) strcpy(newfile, dev_dir); (void) strcat(newfile, "/"); (void) strcat(newfile, file); } if ((file_type == TYPE_LINK) && (recurse == TRUE) && ((linksize = readlink(newfile, contents, PATH_MAX)) > 0)) { contents[linksize] = '\0'; if (is_minor_node(contents, &ptr) == DEVFSADM_TRUE) { devfsadm_rm_work(++ptr, FALSE, TYPE_DEVICES); } else { if (strncmp(contents, DEV "/", strlen(DEV) + 1) == 0) { devfsadm_rm_work(&contents[strlen(DEV) + 1], TRUE, TYPE_LINK); } else { if ((ptr = strrchr(file, '/')) != NULL) { *ptr = '\0'; (void) strcpy(nextfile, file); *ptr = '/'; (void) strcat(nextfile, "/"); } else { (void) strcpy(nextfile, ""); } (void) strcat(nextfile, contents); devfsadm_rm_work(nextfile, TRUE, TYPE_LINK); } } } if (file_type == TYPE_LINK) { vprint(VERBOSE_MID, DEVFSADM_UNLINK, newfile); if (file_mods == TRUE) { rm_link_from_cache(file); s_unlink(newfile); rm_parent_dir_if_empty(newfile); invalidate_enumerate_cache(); (void) di_devlink_rm_link(devlink_cache, file); } } /* * Note: we don't remove /devices entries because they are * covered by devfs. */ } void devfsadm_rm_link(char *file) { devfsadm_rm_work(file, FALSE, TYPE_LINK); } void devfsadm_rm_all(char *file) { devfsadm_rm_work(file, TRUE, TYPE_LINK); } static int s_rmdir(char *path) { int i; char *rpath, *dir; const char *fcn = "s_rmdir"; /* * Certain directories are created at install time by packages. * Some of them (listed in packaged_dirs[]) are required by apps * and need to be present even when empty. */ vprint(REMOVE_MID, "%s: checking if %s is packaged\n", fcn, path); rpath = path + strlen(dev_dir) + 1; for (i = 0; (dir = packaged_dirs[i]) != NULL; i++) { if (*rpath == *dir) { if (strcmp(rpath, dir) == 0) { vprint(REMOVE_MID, "%s: skipping packaged dir: " "%s\n", fcn, path); errno = EEXIST; return (-1); } } } return (rmdir(path)); } /* * Try to remove any empty directories up the tree. It is assumed that * pathname is a file that was removed, so start with its parent, and * work up the tree. */ static void rm_parent_dir_if_empty(char *pathname) { char *ptr, path[PATH_MAX + 1]; char *fcn = "rm_parent_dir_if_empty: "; vprint(REMOVE_MID, "%schecking %s if empty\n", fcn, pathname); (void) strcpy(path, pathname); /* * ascend up the dir tree, deleting all empty dirs. * Return immediately if a dir is not empty. */ for (;;) { if ((ptr = strrchr(path, '/')) == NULL) { return; } *ptr = '\0'; if (finddev_emptydir(path)) { /* directory is empty */ if (s_rmdir(path) == 0) { vprint(REMOVE_MID, "%sremoving empty dir %s\n", fcn, path); } else if (errno == EEXIST) { vprint(REMOVE_MID, "%sfailed to remove dir: %s\n", fcn, path); return; } } else { /* some other file is here, so return */ vprint(REMOVE_MID, "%sdir not empty: %s\n", fcn, path); return; } } } /* * This function and all the functions it calls below were added to * handle the unique problem with world wide names (WWN). The problem is * that if a WWN device is moved to another address on the same controller * its logical link will change, while the physical node remains the same. * The result is that two logical links will point to the same physical path * in /devices, the valid link and a stale link. This function will * find all the stale nodes, though at a significant performance cost. * * Caching is used to increase performance. * A cache will be built from disk if the cache tag doesn't already exist. * The cache tag is a regular expression "dir_re", which selects a * subset of disks to search from typically something like * "dev/cXt[0-9]+d[0-9]+s[0-9]+". After the cache is built, consistency must * be maintained, so entries are added as new links are created, and removed * as old links are deleted. The whole cache is flushed if we are a daemon, * and another devfsadm process ran in between. * * Once the cache is built, this function finds the cache which matches * dir_re, and then it searches all links in that cache looking for * any link whose contents match "valid_link_contents" with a corresponding link * which does not match "valid_link". Any such matches are stale and removed. */ void devfsadm_rm_stale_links(char *dir_re, char *valid_link, di_node_t node, di_minor_t minor) { link_t *link; linkhead_t *head; char phy_path[PATH_MAX + 1]; char *valid_link_contents; char *dev_path; char rmlink[PATH_MAX + 1]; /* * try to use devices path */ if ((node == lnode) && (minor == lminor)) { valid_link_contents = lphy_path; } else { if ((dev_path = di_devfs_path(node)) == NULL) { err_print(DI_DEVFS_PATH_FAILED, strerror(errno)); devfsadm_exit(1); } (void) strcpy(phy_path, dev_path); di_devfs_path_free(dev_path); (void) strcat(phy_path, ":"); (void) strcat(phy_path, di_minor_name(minor)); valid_link_contents = phy_path; } /* * As an optimization, check to make sure the corresponding * devlink was just created before continuing. */ if (linknew == FALSE) { return; } head = get_cached_links(dir_re); assert(head->nextlink == NULL); for (link = head->link; link != NULL; link = head->nextlink) { /* * See hot_cleanup() for why we do this */ head->nextlink = link->next; if ((strcmp(link->contents, valid_link_contents) == 0) && (strcmp(link->devlink, valid_link) != 0)) { vprint(CHATTY_MID, "removing %s -> %s\n" "valid link is: %s -> %s\n", link->devlink, link->contents, valid_link, valid_link_contents); /* * Use a copy of the cached link name as the * cache entry will go away during link removal */ (void) snprintf(rmlink, sizeof (rmlink), "%s", link->devlink); devfsadm_rm_link(rmlink); } } } /* * Return previously created cache, or create cache. */ static linkhead_t * get_cached_links(char *dir_re) { recurse_dev_t rd; linkhead_t *linkhead; int n; vprint(BUILDCACHE_MID, "get_cached_links: %s\n", dir_re); for (linkhead = headlinkhead; linkhead != NULL; linkhead = linkhead->nexthead) { if (strcmp(linkhead->dir_re, dir_re) == 0) { return (linkhead); } } /* * This tag is not in cache, so add it, along with all its * matching /dev entries. This is the only time we go to disk. */ linkhead = s_malloc(sizeof (linkhead_t)); linkhead->nexthead = headlinkhead; headlinkhead = linkhead; linkhead->dir_re = s_strdup(dir_re); if ((n = regcomp(&(linkhead->dir_re_compiled), dir_re, REG_EXTENDED)) != 0) { err_print(REGCOMP_FAILED, dir_re, n); } linkhead->nextlink = NULL; linkhead->link = NULL; rd.fcn = build_devlink_list; rd.data = (void *)linkhead; vprint(BUILDCACHE_MID, "get_cached_links: calling recurse_dev_re\n"); /* call build_devlink_list for each directory in the dir_re RE */ if (dir_re[0] == '/') { recurse_dev_re("/", &dir_re[1], &rd); } else { recurse_dev_re(dev_dir, dir_re, &rd); } return (linkhead); } static void build_devlink_list(char *devlink, void *data) { char *fcn = "build_devlink_list: "; char *ptr; char *r_contents; char *r_devlink; char contents[PATH_MAX + 1]; char newlink[PATH_MAX + 1]; char stage_link[PATH_MAX + 1]; int linksize; linkhead_t *linkhead = (linkhead_t *)data; link_t *link; int i = 0; vprint(BUILDCACHE_MID, "%scheck_link: %s\n", fcn, devlink); (void) strcpy(newlink, devlink); do { linksize = readlink(newlink, contents, PATH_MAX); if (linksize <= 0) { /* * The first pass through the do loop we may readlink() * non-symlink files(EINVAL) from false regexec matches. * Suppress error messages in those cases or if the link * content is the empty string. */ if (linksize < 0 && (i || errno != EINVAL)) err_print(READLINK_FAILED, "build_devlink_list", newlink, strerror(errno)); return; } contents[linksize] = '\0'; i = 1; if (is_minor_node(contents, &r_contents) == DEVFSADM_FALSE) { /* * assume that link contents is really a pointer to * another link, so recurse and read its link contents. * * some link contents are absolute: * /dev/audio -> /dev/sound/0 */ if (strncmp(contents, DEV "/", strlen(DEV) + strlen("/")) != 0) { if ((ptr = strrchr(newlink, '/')) == NULL) { vprint(REMOVE_MID, "%s%s -> %s invalid " "link. missing '/'\n", fcn, newlink, contents); return; } *ptr = '\0'; (void) strcpy(stage_link, newlink); *ptr = '/'; (void) strcat(stage_link, "/"); (void) strcat(stage_link, contents); (void) strcpy(newlink, stage_link); } else { (void) strcpy(newlink, dev_dir); (void) strcat(newlink, "/"); (void) strcat(newlink, &contents[strlen(DEV) + strlen("/")]); } } else { newlink[0] = '\0'; } } while (newlink[0] != '\0'); if (strncmp(devlink, dev_dir, strlen(dev_dir)) != 0) { vprint(BUILDCACHE_MID, "%sinvalid link: %s\n", fcn, devlink); return; } r_devlink = devlink + strlen(dev_dir); if (r_devlink[0] != '/') return; link = s_malloc(sizeof (link_t)); /* don't store the '/' after rootdir/dev */ r_devlink += 1; vprint(BUILDCACHE_MID, "%scaching link: %s\n", fcn, r_devlink); link->devlink = s_strdup(r_devlink); link->contents = s_strdup(r_contents); link->next = linkhead->link; linkhead->link = link; } /* * to be consistent, devlink must not begin with / and must be * relative to /dev/, whereas physpath must contain / and be * relative to /devices. */ static void add_link_to_cache(char *devlink, char *physpath) { linkhead_t *linkhead; link_t *link; int added = 0; if (file_mods == FALSE) { return; } vprint(CACHE_MID, "add_link_to_cache: %s -> %s ", devlink, physpath); for (linkhead = headlinkhead; linkhead != NULL; linkhead = linkhead->nexthead) { if (regexec(&(linkhead->dir_re_compiled), devlink, 0, NULL, 0) == 0) { added++; link = s_malloc(sizeof (link_t)); link->devlink = s_strdup(devlink); link->contents = s_strdup(physpath); link->next = linkhead->link; linkhead->link = link; } } vprint(CACHE_MID, " %d %s\n", added, added == 0 ? "NOT ADDED" : "ADDED"); } /* * Remove devlink from cache. Devlink must be relative to /dev/ and not start * with /. */ static void rm_link_from_cache(char *devlink) { linkhead_t *linkhead; link_t **linkp; link_t *save; vprint(CACHE_MID, "rm_link_from_cache enter: %s\n", devlink); for (linkhead = headlinkhead; linkhead != NULL; linkhead = linkhead->nexthead) { if (regexec(&(linkhead->dir_re_compiled), devlink, 0, NULL, 0) == 0) { for (linkp = &(linkhead->link); *linkp != NULL; ) { if ((strcmp((*linkp)->devlink, devlink) == 0)) { save = *linkp; *linkp = (*linkp)->next; /* * We are removing our caller's * "next" link. Update the nextlink * field in the head so that our * callers accesses the next valid * link */ if (linkhead->nextlink == save) linkhead->nextlink = *linkp; free(save->devlink); free(save->contents); free(save); vprint(CACHE_MID, " %s FREED FROM " "CACHE\n", devlink); } else { linkp = &((*linkp)->next); } } } } } static void rm_all_links_from_cache() { linkhead_t *linkhead; linkhead_t *nextlinkhead; link_t *link; link_t *nextlink; vprint(CACHE_MID, "rm_all_links_from_cache\n"); for (linkhead = headlinkhead; linkhead != NULL; linkhead = nextlinkhead) { nextlinkhead = linkhead->nexthead; assert(linkhead->nextlink == NULL); for (link = linkhead->link; link != NULL; link = nextlink) { nextlink = link->next; free(link->devlink); free(link->contents); free(link); } regfree(&(linkhead->dir_re_compiled)); free(linkhead->dir_re); free(linkhead); } headlinkhead = NULL; } /* * Called when the kernel has modified the incore path_to_inst data. This * function will schedule a flush of the data to the filesystem. */ static void devfs_instance_mod(void) { char *fcn = "devfs_instance_mod: "; vprint(PATH2INST_MID, "%senter\n", fcn); /* signal instance thread */ (void) mutex_lock(&count_lock); inst_count++; (void) cond_signal(&cv); (void) mutex_unlock(&count_lock); } static void instance_flush_thread(void) { int i; int idle; for (;;) { (void) mutex_lock(&count_lock); while (inst_count == 0) { (void) cond_wait(&cv, &count_lock); } inst_count = 0; vprint(PATH2INST_MID, "signaled to flush path_to_inst." " Enter delay loop\n"); /* * Wait MAX_IDLE_DELAY seconds after getting the last flush * path_to_inst event before invoking a flush, but never wait * more than MAX_DELAY seconds after getting the first event. */ for (idle = 0, i = 0; i < MAX_DELAY; i++) { (void) mutex_unlock(&count_lock); (void) sleep(1); (void) mutex_lock(&count_lock); /* shorten the delay if we are idle */ if (inst_count == 0) { idle++; if (idle > MAX_IDLE_DELAY) { break; } } else { inst_count = idle = 0; } } (void) mutex_unlock(&count_lock); flush_path_to_inst(); } } /* * Helper function for flush_path_to_inst() below; this routine calls the * inst_sync syscall to flush the path_to_inst database to the given file. */ static int do_inst_sync(char *filename, char *instfilename) { void (*sigsaved)(int); int err = 0, flags = INST_SYNC_IF_REQUIRED; struct stat sb; if (stat(instfilename, &sb) == -1 && errno == ENOENT) flags = INST_SYNC_ALWAYS; vprint(INSTSYNC_MID, "do_inst_sync: about to flush %s\n", filename); sigsaved = sigset(SIGSYS, SIG_IGN); if (inst_sync(filename, flags) == -1) err = errno; (void) sigset(SIGSYS, sigsaved); switch (err) { case 0: return (DEVFSADM_SUCCESS); case EALREADY: /* no-op, path_to_inst already up to date */ return (EALREADY); case ENOSYS: err_print(CANT_LOAD_SYSCALL); break; case EPERM: err_print(SUPER_TO_SYNC); break; default: err_print(INSTSYNC_FAILED, filename, strerror(err)); break; } return (DEVFSADM_FAILURE); } /* * Flush the kernel's path_to_inst database to /etc/path_to_inst. To do so * safely, the database is flushed to a temporary file, then moved into place. * * The following files are used during this process: * /etc/path_to_inst: The path_to_inst file * /etc/path_to_inst.: Contains data flushed from the kernel * /etc/path_to_inst.old: The backup file * /etc/path_to_inst.old.: Temp file for creating backup * */ static void flush_path_to_inst(void) { char *new_inst_file = NULL; char *old_inst_file = NULL; char *old_inst_file_npid = NULL; FILE *inst_file_fp = NULL; FILE *old_inst_file_fp = NULL; struct stat sb; int err = 0; int c; int inst_strlen; vprint(PATH2INST_MID, "flush_path_to_inst: %s\n", (flush_path_to_inst_enable == TRUE) ? "ENABLED" : "DISABLED"); if (flush_path_to_inst_enable == FALSE) { return; } inst_strlen = strlen(inst_file); new_inst_file = s_malloc(inst_strlen + PID_STR_LEN + 2); old_inst_file = s_malloc(inst_strlen + PID_STR_LEN + 6); old_inst_file_npid = s_malloc(inst_strlen + sizeof (INSTANCE_FILE_SUFFIX)); (void) snprintf(new_inst_file, inst_strlen + PID_STR_LEN + 2, "%s.%ld", inst_file, getpid()); if (stat(new_inst_file, &sb) == 0) { s_unlink(new_inst_file); } err = do_inst_sync(new_inst_file, inst_file); if (err != DEVFSADM_SUCCESS) { goto out; /*NOTREACHED*/ } /* * Now we deal with the somewhat tricky updating and renaming * of this critical piece of kernel state. */ /* * Copy the current instance file into a temporary file. * Then rename the temporary file into the backup (.old) * file and rename the newly flushed kernel data into * the instance file. * Of course if 'inst_file' doesn't exist, there's much * less for us to do .. tee hee. */ if ((inst_file_fp = fopen(inst_file, "r")) == NULL) { /* * No such file. Rename the new onto the old */ if ((err = rename(new_inst_file, inst_file)) != 0) err_print(RENAME_FAILED, inst_file, strerror(errno)); goto out; /*NOTREACHED*/ } (void) snprintf(old_inst_file, inst_strlen + PID_STR_LEN + 6, "%s.old.%ld", inst_file, getpid()); if (stat(old_inst_file, &sb) == 0) { s_unlink(old_inst_file); } if ((old_inst_file_fp = fopen(old_inst_file, "w")) == NULL) { /* * Can't open the 'old_inst_file' file for writing. * This is somewhat strange given that the syscall * just succeeded to write a file out.. hmm.. maybe * the fs just filled up or something nasty. * * Anyway, abort what we've done so far. */ err_print(CANT_UPDATE, old_inst_file); err = DEVFSADM_FAILURE; goto out; /*NOTREACHED*/ } /* * Copy current instance file into the temporary file */ err = 0; while ((c = getc(inst_file_fp)) != EOF) { if ((err = putc(c, old_inst_file_fp)) == EOF) { break; } } if (fclose(old_inst_file_fp) == EOF || err == EOF) { vprint(INFO_MID, CANT_UPDATE, old_inst_file); err = DEVFSADM_FAILURE; goto out; /* NOTREACHED */ } /* * Set permissions to be the same on the backup as * /etc/path_to_inst. */ (void) chmod(old_inst_file, 0444); /* * So far, everything we've done is more or less reversible. * But now we're going to commit ourselves. */ (void) snprintf(old_inst_file_npid, inst_strlen + sizeof (INSTANCE_FILE_SUFFIX), "%s%s", inst_file, INSTANCE_FILE_SUFFIX); if ((err = rename(old_inst_file, old_inst_file_npid)) != 0) { err_print(RENAME_FAILED, old_inst_file_npid, strerror(errno)); } else if ((err = rename(new_inst_file, inst_file)) != 0) { err_print(RENAME_FAILED, inst_file, strerror(errno)); } out: if (inst_file_fp != NULL) { if (fclose(inst_file_fp) == EOF) { err_print(FCLOSE_FAILED, inst_file, strerror(errno)); } } if (stat(new_inst_file, &sb) == 0) { s_unlink(new_inst_file); } free(new_inst_file); if (stat(old_inst_file, &sb) == 0) { s_unlink(old_inst_file); } free(old_inst_file); free(old_inst_file_npid); if (err != 0 && err != EALREADY) { err_print(FAILED_TO_UPDATE, inst_file); } } /* * detach from tty. For daemon mode. */ void detachfromtty() { (void) setsid(); if (DEVFSADM_DEBUG_ON == TRUE) { return; } (void) close(0); (void) close(1); (void) close(2); (void) open("/dev/null", O_RDWR, 0); (void) dup(0); (void) dup(0); openlog(DEVFSADMD, LOG_PID, LOG_DAEMON); (void) setlogmask(LOG_UPTO(LOG_INFO)); logflag = TRUE; } /* * Use an advisory lock to synchronize updates to /dev. If the lock is * held by another process, block in the fcntl() system call until that * process drops the lock or exits. The lock file itself is * DEV_LOCK_FILE. The process id of the current and last process owning * the lock is kept in the lock file. After acquiring the lock, read the * process id and return it. It is the process ID which last owned the * lock, and will be used to determine if caches need to be flushed. * * NOTE: if the devlink database is held open by the caller, it may * be closed by this routine. This is to enforce the following lock ordering: * 1) /dev lock 2) database open */ pid_t enter_dev_lock() { struct flock lock; int n; pid_t pid; pid_t last_owner_pid; if (file_mods == FALSE) { return (0); } (void) snprintf(dev_lockfile, sizeof (dev_lockfile), "%s/%s", etc_dev_dir, DEV_LOCK_FILE); vprint(LOCK_MID, "enter_dev_lock: lock file %s\n", dev_lockfile); dev_lock_fd = open(dev_lockfile, O_CREAT|O_RDWR, 0644); if (dev_lock_fd < 0) { err_print(OPEN_FAILED, dev_lockfile, strerror(errno)); devfsadm_exit(1); } lock.l_type = F_WRLCK; lock.l_whence = SEEK_SET; lock.l_start = 0; lock.l_len = 0; /* try for the lock, but don't wait */ if (fcntl(dev_lock_fd, F_SETLK, &lock) == -1) { if ((errno == EACCES) || (errno == EAGAIN)) { pid = 0; n = read(dev_lock_fd, &pid, sizeof (pid_t)); vprint(LOCK_MID, "waiting for PID %d to complete\n", (int)pid); if (lseek(dev_lock_fd, 0, SEEK_SET) == (off_t)-1) { err_print(LSEEK_FAILED, dev_lockfile, strerror(errno)); devfsadm_exit(1); } /* * wait for the dev lock. If we have the database open, * close it first - the order of lock acquisition should * always be: 1) dev_lock 2) database * This is to prevent deadlocks with any locks the * database code may hold. */ (void) di_devlink_close(&devlink_cache, 0); /* send any sysevents that were queued up. */ process_syseventq(); if (fcntl(dev_lock_fd, F_SETLKW, &lock) == -1) { err_print(LOCK_FAILED, dev_lockfile, strerror(errno)); devfsadm_exit(1); } } } hold_dev_lock = TRUE; pid = 0; n = read(dev_lock_fd, &pid, sizeof (pid_t)); if (n == sizeof (pid_t) && pid == getpid()) { return (pid); } last_owner_pid = pid; if (lseek(dev_lock_fd, 0, SEEK_SET) == (off_t)-1) { err_print(LSEEK_FAILED, dev_lockfile, strerror(errno)); devfsadm_exit(1); } pid = getpid(); n = write(dev_lock_fd, &pid, sizeof (pid_t)); if (n != sizeof (pid_t)) { err_print(WRITE_FAILED, dev_lockfile, strerror(errno)); devfsadm_exit(1); } return (last_owner_pid); } /* * Drop the advisory /dev lock, close lock file. Close and re-open the * file every time so to ensure a resync if for some reason the lock file * gets removed. */ void exit_dev_lock() { struct flock unlock; if (hold_dev_lock == FALSE) { return; } vprint(LOCK_MID, "exit_dev_lock: lock file %s\n", dev_lockfile); unlock.l_type = F_UNLCK; unlock.l_whence = SEEK_SET; unlock.l_start = 0; unlock.l_len = 0; if (fcntl(dev_lock_fd, F_SETLK, &unlock) == -1) { err_print(UNLOCK_FAILED, dev_lockfile, strerror(errno)); } hold_dev_lock = FALSE; if (close(dev_lock_fd) == -1) { err_print(CLOSE_FAILED, dev_lockfile, strerror(errno)); devfsadm_exit(1); } } /* * * Use an advisory lock to ensure that only one daemon process is active * in the system at any point in time. If the lock is held by another * process, do not block but return the pid owner of the lock to the * caller immediately. The lock is cleared if the holding daemon process * exits for any reason even if the lock file remains, so the daemon can * be restarted if necessary. The lock file is DAEMON_LOCK_FILE. */ pid_t enter_daemon_lock(void) { struct flock lock; (void) snprintf(daemon_lockfile, sizeof (daemon_lockfile), "%s/%s", etc_dev_dir, DAEMON_LOCK_FILE); vprint(LOCK_MID, "enter_daemon_lock: lock file %s\n", daemon_lockfile); daemon_lock_fd = open(daemon_lockfile, O_CREAT|O_RDWR, 0644); if (daemon_lock_fd < 0) { err_print(OPEN_FAILED, daemon_lockfile, strerror(errno)); devfsadm_exit(1); } lock.l_type = F_WRLCK; lock.l_whence = SEEK_SET; lock.l_start = 0; lock.l_len = 0; if (fcntl(daemon_lock_fd, F_SETLK, &lock) == -1) { if (errno == EAGAIN || errno == EDEADLK) { if (fcntl(daemon_lock_fd, F_GETLK, &lock) == -1) { err_print(LOCK_FAILED, daemon_lockfile, strerror(errno)); devfsadm_exit(1); } return (lock.l_pid); } } hold_daemon_lock = TRUE; return (getpid()); } /* * Drop the advisory daemon lock, close lock file */ void exit_daemon_lock(void) { struct flock lock; if (hold_daemon_lock == FALSE) { return; } vprint(LOCK_MID, "exit_daemon_lock: lock file %s\n", daemon_lockfile); lock.l_type = F_UNLCK; lock.l_whence = SEEK_SET; lock.l_start = 0; lock.l_len = 0; if (fcntl(daemon_lock_fd, F_SETLK, &lock) == -1) { err_print(UNLOCK_FAILED, daemon_lockfile, strerror(errno)); } if (close(daemon_lock_fd) == -1) { err_print(CLOSE_FAILED, daemon_lockfile, strerror(errno)); devfsadm_exit(1); } } /* * Called to removed danging nodes in two different modes: RM_PRE, RM_POST. * RM_PRE mode is called before processing the entire devinfo tree, and RM_POST * is called after processing the entire devinfo tree. */ static void pre_and_post_cleanup(int flags) { remove_list_t *rm; recurse_dev_t rd; cleanup_data_t cleanup_data; char *fcn = "pre_and_post_cleanup: "; if (build_dev == FALSE) return; vprint(CHATTY_MID, "attempting %s-cleanup\n", flags == RM_PRE ? "pre" : "post"); vprint(REMOVE_MID, "%sflags = %d\n", fcn, flags); /* * the generic function recurse_dev_re is shared among different * functions, so set the method and data that it should use for * matches. */ rd.fcn = matching_dev; rd.data = (void *)&cleanup_data; cleanup_data.flags = flags; (void) mutex_lock(&nfp_mutex); nfphash_create(); for (rm = remove_head; rm != NULL; rm = rm->next) { if ((flags & rm->remove->flags) == flags) { cleanup_data.rm = rm; /* * If reached this point, RM_PRE or RM_POST cleanup is * desired. clean_ok() decides whether to clean * under the given circumstances. */ vprint(REMOVE_MID, "%scleanup: PRE or POST\n", fcn); if (clean_ok(rm->remove) == DEVFSADM_SUCCESS) { vprint(REMOVE_MID, "cleanup: cleanup OK\n"); recurse_dev_re(dev_dir, rm->remove->dev_dirs_re, &rd); } } } nfphash_destroy(); (void) mutex_unlock(&nfp_mutex); } /* * clean_ok() determines whether cleanup should be done according * to the following matrix: * * command line arguments RM_PRE RM_POST RM_PRE && RM_POST && * RM_ALWAYS RM_ALWAYS * ---------------------- ------ ----- --------- ---------- * * - - pre-clean post-clean * * -C pre-clean post-clean pre-clean post-clean * * -C -c class pre-clean post-clean pre-clean post-clean * if class if class if class if class * matches matches matches matches * * -c class - - pre-clean post-clean * if class if class * matches matches * */ static int clean_ok(devfsadm_remove_V1_t *remove) { int i; if (single_drv == TRUE) { /* no cleanup at all when using -i option */ return (DEVFSADM_FAILURE); } /* * no cleanup if drivers are not loaded. We make an exception * for the "disks" program however, since disks has a public * cleanup flag (-C) and disk drivers are usually never * unloaded. */ if (load_attach_drv == FALSE && strcmp(prog, DISKS) != 0) { return (DEVFSADM_FAILURE); } /* if the cleanup flag was not specified, return false */ if ((cleanup == FALSE) && ((remove->flags & RM_ALWAYS) == 0)) { return (DEVFSADM_FAILURE); } if (num_classes == 0) { return (DEVFSADM_SUCCESS); } /* * if reached this point, check to see if the class in the given * remove structure matches a class given on the command line */ for (i = 0; i < num_classes; i++) { if (strcmp(remove->device_class, classes[i]) == 0) { return (DEVFSADM_SUCCESS); } } return (DEVFSADM_FAILURE); } /* * Called to remove dangling nodes after receiving a hotplug event * containing the physical node pathname to be removed. */ void hot_cleanup(char *node_path, char *minor_name, char *ev_subclass, char *driver_name, int instance) { link_t *link; linkhead_t *head; remove_list_t *rm; char *fcn = "hot_cleanup: "; char path[PATH_MAX + 1]; int path_len; char rmlink[PATH_MAX + 1]; nvlist_t *nvl = NULL; int skip; int ret; /* * dev links can go away as part of hot cleanup. * So first build event attributes in order capture dev links. */ if (ev_subclass != NULL) nvl = build_event_attributes(EC_DEV_REMOVE, ev_subclass, node_path, DI_NODE_NIL, driver_name, instance, minor_name); (void) strcpy(path, node_path); (void) strcat(path, ":"); (void) strcat(path, minor_name == NULL ? "" : minor_name); path_len = strlen(path); vprint(REMOVE_MID, "%spath=%s\n", fcn, path); (void) mutex_lock(&nfp_mutex); nfphash_create(); for (rm = remove_head; rm != NULL; rm = rm->next) { if ((RM_HOT & rm->remove->flags) == RM_HOT) { head = get_cached_links(rm->remove->dev_dirs_re); assert(head->nextlink == NULL); for (link = head->link; link != NULL; link = head->nextlink) { /* * The remove callback below may remove * the current and/or any or all of the * subsequent links in the list. * Save the next link in the head. If * the callback removes the next link * the saved pointer in the head will be * updated by the callback to point at * the next valid link. */ head->nextlink = link->next; /* * if devlink is in no-further-process hash, * skip its remove */ if (nfphash_lookup(link->devlink) != NULL) continue; if (minor_name) skip = strcmp(link->contents, path); else skip = strncmp(link->contents, path, path_len); if (skip || (call_minor_init(rm->modptr) == DEVFSADM_FAILURE)) continue; vprint(REMOVE_MID, "%sremoving %s -> %s\n", fcn, link->devlink, link->contents); /* * Use a copy of the cached link name * as the cache entry will go away * during link removal */ (void) snprintf(rmlink, sizeof (rmlink), "%s", link->devlink); if (rm->remove->flags & RM_NOINTERPOSE) { ((void (*)(char *)) (rm->remove->callback_fcn))(rmlink); } else { ret = ((int (*)(char *)) (rm->remove->callback_fcn))(rmlink); if (ret == DEVFSADM_TERMINATE) nfphash_insert(rmlink); } } } } nfphash_destroy(); (void) mutex_unlock(&nfp_mutex); /* update device allocation database */ if (system_labeled) { int ret = 0; int devtype = 0; char devname[MAXNAMELEN]; devname[0] = '\0'; if (strstr(node_path, DA_SOUND_NAME)) devtype = DA_AUDIO; else if (strstr(node_path, "disk")) devtype = DA_RMDISK; else goto out; ret = da_remove_list(&devlist, NULL, devtype, devname, sizeof (devname)); if (ret != -1) (void) _update_devalloc_db(&devlist, devtype, DA_REMOVE, devname, root_dir); } out: /* now log an event */ if (nvl) { log_event(EC_DEV_REMOVE, ev_subclass, nvl); free(nvl); } } /* * Open the dir current_dir. For every file which matches the first dir * component of path_re, recurse. If there are no more *dir* path * components left in path_re (ie no more /), then call function rd->fcn. */ static void recurse_dev_re(char *current_dir, char *path_re, recurse_dev_t *rd) { regex_t re1; char *slash; char new_path[PATH_MAX + 1]; char *anchored_path_re; size_t len; finddevhdl_t fhandle; const char *fp; vprint(RECURSEDEV_MID, "recurse_dev_re: curr = %s path=%s\n", current_dir, path_re); if (finddev_readdir(current_dir, &fhandle) != 0) return; len = strlen(path_re); if ((slash = strchr(path_re, '/')) != NULL) { len = (slash - path_re); } anchored_path_re = s_malloc(len + 3); (void) sprintf(anchored_path_re, "^%.*s$", len, path_re); if (regcomp(&re1, anchored_path_re, REG_EXTENDED) != 0) { free(anchored_path_re); goto out; } free(anchored_path_re); while ((fp = finddev_next(fhandle)) != NULL) { if (regexec(&re1, fp, 0, NULL, 0) == 0) { /* match */ (void) strcpy(new_path, current_dir); (void) strcat(new_path, "/"); (void) strcat(new_path, fp); vprint(RECURSEDEV_MID, "recurse_dev_re: match, new " "path = %s\n", new_path); if (slash != NULL) { recurse_dev_re(new_path, slash + 1, rd); } else { /* reached the leaf component of path_re */ vprint(RECURSEDEV_MID, "recurse_dev_re: calling fcn\n"); (*(rd->fcn))(new_path, rd->data); } } } regfree(&re1); out: finddev_close(fhandle); } /* * Found a devpath which matches a RE in the remove structure. * Now check to see if it is dangling. */ static void matching_dev(char *devpath, void *data) { cleanup_data_t *cleanup_data = data; int norm_len = strlen(dev_dir) + strlen("/"); int ret; char *fcn = "matching_dev: "; vprint(RECURSEDEV_MID, "%sexamining devpath = '%s'\n", fcn, devpath); /* * If the link is in the no-further-process hash * don't do any remove operation on it. */ if (nfphash_lookup(devpath + norm_len) != NULL) return; if (resolve_link(devpath, NULL, NULL, NULL, 1) == TRUE) { if (call_minor_init(cleanup_data->rm->modptr) == DEVFSADM_FAILURE) { return; } devpath += norm_len; vprint(RECURSEDEV_MID, "%scalling callback %s\n", fcn, devpath); if (cleanup_data->rm->remove->flags & RM_NOINTERPOSE) ((void (*)(char *)) (cleanup_data->rm->remove->callback_fcn))(devpath); else { ret = ((int (*)(char *)) (cleanup_data->rm->remove->callback_fcn))(devpath); if (ret == DEVFSADM_TERMINATE) { /* * We want no further remove processing for * this link. Add it to the nfp_hash; */ nfphash_insert(devpath); } } } } int devfsadm_read_link(char *link, char **devfs_path) { char devlink[PATH_MAX]; *devfs_path = NULL; /* prepend link with dev_dir contents */ (void) strcpy(devlink, dev_dir); (void) strcat(devlink, "/"); (void) strcat(devlink, link); /* We *don't* want a stat of the /devices node */ (void) resolve_link(devlink, NULL, NULL, devfs_path, 0); return (*devfs_path ? DEVFSADM_SUCCESS : DEVFSADM_FAILURE); } int devfsadm_link_valid(char *link) { struct stat sb; char devlink[PATH_MAX + 1], *contents = NULL; int rv, type; int instance = 0; /* prepend link with dev_dir contents */ (void) strcpy(devlink, dev_dir); (void) strcat(devlink, "/"); (void) strcat(devlink, link); if (!device_exists(devlink) || lstat(devlink, &sb) != 0) { return (DEVFSADM_FALSE); } contents = NULL; type = 0; if (resolve_link(devlink, &contents, &type, NULL, 1) == TRUE) { rv = DEVFSADM_FALSE; } else { rv = DEVFSADM_TRUE; } /* * The link exists. Add it to the database */ (void) di_devlink_add_link(devlink_cache, link, contents, type); if (system_labeled && (rv == DEVFSADM_TRUE) && strstr(devlink, DA_AUDIO_NAME) && contents) { (void) sscanf(contents, "%*[a-z]%d", &instance); (void) da_add_list(&devlist, devlink, instance, DA_ADD|DA_AUDIO); _update_devalloc_db(&devlist, 0, DA_ADD, NULL, root_dir); } free(contents); return (rv); } /* * devpath: Absolute path to /dev link * content_p: Returns malloced string (link content) * type_p: Returns link type: primary or secondary * devfs_path: Returns malloced string: /devices path w/out "/devices" * dangle: if set, check if link is dangling * Returns: * TRUE if dangling * FALSE if not or if caller doesn't care * Caller is assumed to have initialized pointer contents to NULL */ static int resolve_link(char *devpath, char **content_p, int *type_p, char **devfs_path, int dangle) { char contents[PATH_MAX + 1]; char stage_link[PATH_MAX + 1]; char *fcn = "resolve_link: "; char *ptr; int linksize; int rv = TRUE; struct stat sb; linksize = readlink(devpath, contents, PATH_MAX); if (linksize <= 0) { return (FALSE); } else { contents[linksize] = '\0'; } vprint(REMOVE_MID, "%s %s -> %s\n", fcn, devpath, contents); if (content_p) { *content_p = s_strdup(contents); } /* * Check to see if this is a link pointing to another link in /dev. The * cheap way to do this is to look for a lack of ../devices/. */ if (is_minor_node(contents, &ptr) == DEVFSADM_FALSE) { if (type_p) { *type_p = DI_SECONDARY_LINK; } /* * assume that linkcontents is really a pointer to another * link, and if so recurse and read its link contents. */ if (strncmp(contents, DEV "/", strlen(DEV) + 1) == 0) { (void) strcpy(stage_link, dev_dir); (void) strcat(stage_link, "/"); (void) strcpy(stage_link, &contents[strlen(DEV) + strlen("/")]); } else { if ((ptr = strrchr(devpath, '/')) == NULL) { vprint(REMOVE_MID, "%s%s -> %s invalid link. " "missing '/'\n", fcn, devpath, contents); return (TRUE); } *ptr = '\0'; (void) strcpy(stage_link, devpath); *ptr = '/'; (void) strcat(stage_link, "/"); (void) strcat(stage_link, contents); } return (resolve_link(stage_link, NULL, NULL, devfs_path, dangle)); } /* Current link points at a /devices minor node */ if (type_p) { *type_p = DI_PRIMARY_LINK; } if (devfs_path) *devfs_path = s_strdup(ptr); rv = FALSE; if (dangle) rv = (stat(ptr - strlen(DEVICES), &sb) == -1); vprint(REMOVE_MID, "%slink=%s, returning %s\n", fcn, devpath, ((rv == TRUE) ? "TRUE" : "FALSE")); return (rv); } /* * Returns the substring of interest, given a path. */ static char * alloc_cmp_str(const char *path, devfsadm_enumerate_t *dep) { uint_t match; char *np, *ap, *mp; char *cmp_str = NULL; char at[] = "@"; char *fcn = "alloc_cmp_str"; np = ap = mp = NULL; /* * extract match flags from the flags argument. */ match = (dep->flags & MATCH_MASK); vprint(ENUM_MID, "%s: enumeration match type: 0x%x" " path: %s\n", fcn, match, path); /* * MATCH_CALLBACK and MATCH_ALL are the only flags * which may be used if "path" is a /dev path */ if (match == MATCH_CALLBACK) { if (dep->sel_fcn == NULL) { vprint(ENUM_MID, "%s: invalid enumerate" " callback: path: %s\n", fcn, path); return (NULL); } cmp_str = dep->sel_fcn(path, dep->cb_arg); return (cmp_str); } cmp_str = s_strdup(path); if (match == MATCH_ALL) { return (cmp_str); } /* * The remaining flags make sense only for /devices * paths */ if ((mp = strrchr(cmp_str, ':')) == NULL) { vprint(ENUM_MID, "%s: invalid path: %s\n", fcn, path); goto err; } if (match == MATCH_MINOR) { /* A NULL "match_arg" values implies entire minor */ if (get_component(mp + 1, dep->match_arg) == NULL) { vprint(ENUM_MID, "%s: invalid minor component:" " path: %s\n", fcn, path); goto err; } return (cmp_str); } if ((np = strrchr(cmp_str, '/')) == NULL) { vprint(ENUM_MID, "%s: invalid path: %s\n", fcn, path); goto err; } if (match == MATCH_PARENT) { if (strcmp(cmp_str, "/") == 0) { vprint(ENUM_MID, "%s: invalid path: %s\n", fcn, path); goto err; } if (np == cmp_str) { *(np + 1) = '\0'; } else { *np = '\0'; } return (cmp_str); } /* ap can be NULL - Leaf address may not exist or be empty string */ ap = strchr(np+1, '@'); /* minor is no longer of interest */ *mp = '\0'; if (match == MATCH_NODE) { if (ap) *ap = '\0'; return (cmp_str); } else if (match == MATCH_ADDR) { /* * The empty string is a valid address. The only MATCH_ADDR * allowed in this case is against the whole address or * the first component of the address (match_arg=NULL/"0"/"1") * Note that in this case, the path won't have an "@" * As a result ap will be NULL. We fake up an ap = @'\0' * so that get_component() will work correctly. */ if (ap == NULL) { ap = at; } if (get_component(ap + 1, dep->match_arg) == NULL) { vprint(ENUM_MID, "%s: invalid leaf addr. component:" " path: %s\n", fcn, path); goto err; } return (cmp_str); } vprint(ENUM_MID, "%s: invalid enumeration flags: 0x%x" " path: %s\n", fcn, dep->flags, path); /*FALLTHRU*/ err: free(cmp_str); return (NULL); } /* * "str" is expected to be a string with components separated by ',' * The terminating null char is considered a separator. * get_component() will remove the portion of the string beyond * the component indicated. * If comp_str is NULL, the entire "str" is returned. */ static char * get_component(char *str, const char *comp_str) { long comp; char *cp; if (str == NULL) { return (NULL); } if (comp_str == NULL) { return (str); } errno = 0; comp = strtol(comp_str, &cp, 10); if (errno != 0 || *cp != '\0' || comp < 0) { return (NULL); } if (comp == 0) return (str); for (cp = str; ; cp++) { if (*cp == ',' || *cp == '\0') comp--; if (*cp == '\0' || comp <= 0) { break; } } if (comp == 0) { *cp = '\0'; } else { str = NULL; } return (str); } /* * Enumerate serves as a generic counter as well as a means to determine * logical unit/controller numbers for such items as disk and tape * drives. * * rules[] is an array of devfsadm_enumerate_t structures which defines * the enumeration rules to be used for a specified set of links in /dev. * The set of links is specified through regular expressions (of the flavor * described in regex(5)). These regular expressions are used to determine * the set of links in /dev to examine. The last path component in these * regular expressions MUST contain a parenthesized subexpression surrounding * the RE which is to be considered the enumerating component. The subexp * member in a rule is the subexpression number of the enumerating * component. Subexpressions in the last path component are numbered starting * from 1. * * A cache of current id assignments is built up from existing symlinks and * new assignments use the lowest unused id. Assignments are based on a * match of a specified substring of a symlink's contents. If the specified * component for the devfs_path argument matches the corresponding substring * for a existing symlink's contents, the cached id is returned. Else, a new * id is created and returned in *buf. *buf must be freed by the caller. * * An id assignment may be governed by a combination of rules, each rule * applicable to a different subset of links in /dev. For example, controller * numbers may be determined by a combination of disk symlinks in /dev/[r]dsk * and controller symlinks in /dev/cfg, with the two sets requiring different * rules to derive the "substring of interest". In such cases, the rules * array will have more than one element. */ int devfsadm_enumerate_int(char *devfs_path, int index, char **buf, devfsadm_enumerate_t rules[], int nrules) { return (find_enum_id(rules, nrules, devfs_path, index, "0", INTEGER, buf, 0)); } int disk_enumerate_int(char *devfs_path, int index, char **buf, devfsadm_enumerate_t rules[], int nrules) { return (find_enum_id(rules, nrules, devfs_path, index, "0", INTEGER, buf, 1)); } /* * Same as above, but allows a starting value to be specified. * Private to devfsadm.... used by devlinks. */ static int devfsadm_enumerate_int_start(char *devfs_path, int index, char **buf, devfsadm_enumerate_t rules[], int nrules, char *start) { return (find_enum_id(rules, nrules, devfs_path, index, start, INTEGER, buf, 0)); } /* * devfsadm_enumerate_char serves as a generic counter returning * a single letter. */ int devfsadm_enumerate_char(char *devfs_path, int index, char **buf, devfsadm_enumerate_t rules[], int nrules) { return (find_enum_id(rules, nrules, devfs_path, index, "a", LETTER, buf, 0)); } /* * Same as above, but allows a starting char to be specified. * Private to devfsadm - used by ports module (port_link.c) */ int devfsadm_enumerate_char_start(char *devfs_path, int index, char **buf, devfsadm_enumerate_t rules[], int nrules, char *start) { return (find_enum_id(rules, nrules, devfs_path, index, start, LETTER, buf, 0)); } /* * For a given numeral_set (see get_cached_set for desc of numeral_set), * search all cached entries looking for matches on a specified substring * of devfs_path. The substring is derived from devfs_path based on the * rule specified by "index". If a match is found on a cached entry, * return the enumerated id in buf. Otherwise, create a new id by calling * new_id, then cache and return that entry. */ static int find_enum_id(devfsadm_enumerate_t rules[], int nrules, char *devfs_path, int index, char *min, int type, char **buf, int multiple) { numeral_t *matchnp; numeral_t *numeral; int matchcount = 0; char *cmp_str; char *fcn = "find_enum_id"; numeral_set_t *set; if (rules == NULL) { vprint(ENUM_MID, "%s: no rules. path: %s\n", fcn, devfs_path ? devfs_path : ""); return (DEVFSADM_FAILURE); } if (devfs_path == NULL) { vprint(ENUM_MID, "%s: NULL path\n", fcn); return (DEVFSADM_FAILURE); } if (nrules <= 0 || index < 0 || index >= nrules || buf == NULL) { vprint(ENUM_MID, "%s: invalid arguments. path: %s\n", fcn, devfs_path); return (DEVFSADM_FAILURE); } *buf = NULL; cmp_str = alloc_cmp_str(devfs_path, &rules[index]); if (cmp_str == NULL) { return (DEVFSADM_FAILURE); } if ((set = get_enum_cache(rules, nrules)) == NULL) { free(cmp_str); return (DEVFSADM_FAILURE); } assert(nrules == set->re_count); /* * Check and see if a matching entry is already cached. */ matchcount = lookup_enum_cache(set, cmp_str, rules, index, &matchnp); if (matchcount < 0 || matchcount > 1) { free(cmp_str); if (multiple && matchcount > 1) return (DEVFSADM_MULTIPLE); else return (DEVFSADM_FAILURE); } /* if matching entry already cached, return it */ if (matchcount == 1) { /* should never create a link with a reserved ID */ vprint(ENUM_MID, "%s: 1 match w/ ID: %s\n", fcn, matchnp->id); assert(matchnp->flags == 0); *buf = s_strdup(matchnp->id); free(cmp_str); return (DEVFSADM_SUCCESS); } /* * no cached entry, initialize a numeral struct * by calling new_id() and cache onto the numeral_set */ numeral = s_malloc(sizeof (numeral_t)); numeral->id = new_id(set->headnumeral, type, min); numeral->full_path = s_strdup(devfs_path); numeral->rule_index = index; numeral->cmp_str = cmp_str; cmp_str = NULL; numeral->flags = 0; vprint(RSRV_MID, "%s: alloc new_id: %s numeral flags = %d\n", fcn, numeral->id, numeral->flags); /* insert to head of list for fast lookups */ numeral->next = set->headnumeral; set->headnumeral = numeral; *buf = s_strdup(numeral->id); return (DEVFSADM_SUCCESS); } /* * Looks up the specified cache for a match with a specified string * Returns: * -1 : on error. * 0/1/2 : Number of matches. * Returns the matching element only if there is a single match. * If the "uncached" flag is set, derives the "cmp_str" afresh * for the match instead of using cached values. */ static int lookup_enum_cache(numeral_set_t *set, char *cmp_str, devfsadm_enumerate_t rules[], int index, numeral_t **matchnpp) { int matchcount = 0, rv = -1; int uncached; numeral_t *np; char *fcn = "lookup_enum_cache"; char *cp; *matchnpp = NULL; assert(index < set->re_count); if (cmp_str == NULL) { return (-1); } uncached = 0; if ((rules[index].flags & MATCH_UNCACHED) == MATCH_UNCACHED) { uncached = 1; } /* * Check and see if a matching entry is already cached. */ for (np = set->headnumeral; np != NULL; np = np->next) { /* * Skip reserved IDs */ if (np->flags & NUMERAL_RESERVED) { vprint(RSRV_MID, "lookup_enum_cache: " "Cannot Match with reserved ID (%s), " "skipping\n", np->id); assert(np->flags == NUMERAL_RESERVED); continue; } else { vprint(RSRV_MID, "lookup_enum_cache: " "Attempting match with numeral ID: %s" " numeral flags = %d\n", np->id, np->flags); assert(np->flags == 0); } if (np->cmp_str == NULL) { vprint(ENUM_MID, "%s: invalid entry in enumerate" " cache. path: %s\n", fcn, np->full_path); return (-1); } if (uncached) { vprint(CHATTY_MID, "%s: bypassing enumerate cache." " path: %s\n", fcn, cmp_str); cp = alloc_cmp_str(np->full_path, &rules[np->rule_index]); if (cp == NULL) return (-1); rv = strcmp(cmp_str, cp); free(cp); } else { rv = strcmp(cmp_str, np->cmp_str); } if (rv == 0) { if (matchcount++ != 0) { break; /* more than 1 match. */ } *matchnpp = np; } } return (matchcount); } #ifdef DEBUG static void dump_enum_cache(numeral_set_t *setp) { int i; numeral_t *np; char *fcn = "dump_enum_cache"; vprint(ENUM_MID, "%s: re_count = %d\n", fcn, setp->re_count); for (i = 0; i < setp->re_count; i++) { vprint(ENUM_MID, "%s: re[%d] = %s\n", fcn, i, setp->re[i]); } for (np = setp->headnumeral; np != NULL; np = np->next) { vprint(ENUM_MID, "%s: id: %s\n", fcn, np->id); vprint(ENUM_MID, "%s: full_path: %s\n", fcn, np->full_path); vprint(ENUM_MID, "%s: rule_index: %d\n", fcn, np->rule_index); vprint(ENUM_MID, "%s: cmp_str: %s\n", fcn, np->cmp_str); vprint(ENUM_MID, "%s: flags: %d\n", fcn, np->flags); } } #endif /* * For a given set of regular expressions in rules[], this function returns * either a previously cached struct numeral_set or it will create and * cache a new struct numeral_set. There is only one struct numeral_set * for the combination of REs present in rules[]. Each numeral_set contains * the regular expressions in rules[] used for cache selection AND a linked * list of struct numerals, ONE FOR EACH *UNIQUE* numeral or character ID * selected by the grouping parenthesized subexpression found in the last * path component of each rules[].re. For example, the RE: "rmt/([0-9]+)" * selects all the logical nodes of the correct form in dev/rmt/. * Each rmt/X will store a *single* struct numeral... ie 0, 1, 2 each get a * single struct numeral. There is no need to store more than a single logical * node matching X since the information desired in the devfspath would be * identical for the portion of the devfspath of interest. (the part up to, * but not including the minor name in this example.) * * If the given numeral_set is not yet cached, call enumerate_recurse to * create it. */ static numeral_set_t * get_enum_cache(devfsadm_enumerate_t rules[], int nrules) { /* linked list of numeral sets */ numeral_set_t *setp; int i; int ret; char *path_left; enumerate_file_t *entry; char *fcn = "get_enum_cache"; /* * See if we've already cached this numeral set. */ for (setp = head_numeral_set; setp != NULL; setp = setp->next) { /* * check all regexp's passed in function against * those in cached set. */ if (nrules != setp->re_count) { continue; } for (i = 0; i < nrules; i++) { if (strcmp(setp->re[i], rules[i].re) != 0) { break; } } if (i == nrules) { return (setp); } } /* * If the MATCH_UNCACHED flag is set, we should not be here. */ for (i = 0; i < nrules; i++) { if ((rules[i].flags & MATCH_UNCACHED) == MATCH_UNCACHED) { vprint(ENUM_MID, "%s: invalid enumeration flags: " "0x%x\n", fcn, rules[i].flags); return (NULL); } } /* * Since we made it here, we have not yet cached the given set of * logical nodes matching the passed re. Create a cached entry * struct numeral_set and populate it with a minimal set of * logical nodes from /dev. */ setp = s_malloc(sizeof (numeral_set_t)); setp->re = s_malloc(sizeof (char *) * nrules); for (i = 0; i < nrules; i++) { setp->re[i] = s_strdup(rules[i].re); } setp->re_count = nrules; setp->headnumeral = NULL; /* put this new cached set on the cached set list */ setp->next = head_numeral_set; head_numeral_set = setp; /* * For each RE, search the "reserved" list to create numeral IDs that * are reserved. */ for (entry = enumerate_reserved; entry; entry = entry->er_next) { vprint(RSRV_MID, "parsing rstring: %s\n", entry->er_file); for (i = 0; i < nrules; i++) { path_left = s_strdup(setp->re[i]); vprint(RSRV_MID, "parsing rule RE: %s\n", path_left); ret = enumerate_parse(entry->er_file, path_left, setp, rules, i); free(path_left); if (ret == 1) { /* * We found the reserved ID for this entry. * We still keep the entry since it is needed * by the new link bypass code in disks */ vprint(RSRV_MID, "found rsv ID: rstring: %s " "rule RE: %s\n", entry->er_file, path_left); break; } } } /* * For each RE, search disk and cache any matches on the * numeral list. */ for (i = 0; i < nrules; i++) { path_left = s_strdup(setp->re[i]); enumerate_recurse(dev_dir, path_left, setp, rules, i); free(path_left); } #ifdef DEBUG dump_enum_cache(setp); #endif return (setp); } /* * This function stats the pathname namebuf. If this is a directory * entry, we recurse down dname/fname until we find the first symbolic * link, and then stat and return it. This is valid for the same reason * that we only need to read a single pathname for multiple matching * logical ID's... ie, all the logical nodes should contain identical * physical paths for the parts we are interested. */ int get_stat_info(char *namebuf, struct stat *sb) { char *cp; finddevhdl_t fhandle; const char *fp; if (lstat(namebuf, sb) < 0) { (void) err_print(LSTAT_FAILED, namebuf, strerror(errno)); return (DEVFSADM_FAILURE); } if ((sb->st_mode & S_IFMT) == S_IFLNK) { return (DEVFSADM_SUCCESS); } /* * If it is a dir, recurse down until we find a link and * then use the link. */ if ((sb->st_mode & S_IFMT) == S_IFDIR) { if (finddev_readdir(namebuf, &fhandle) != 0) { return (DEVFSADM_FAILURE); } /* * Search each dir entry looking for a symlink. Return * the first symlink found in namebuf. Recurse dirs. */ while ((fp = finddev_next(fhandle)) != NULL) { cp = namebuf + strlen(namebuf); if ((strlcat(namebuf, "/", PATH_MAX) >= PATH_MAX) || (strlcat(namebuf, fp, PATH_MAX) >= PATH_MAX)) { *cp = '\0'; finddev_close(fhandle); return (DEVFSADM_FAILURE); } if (get_stat_info(namebuf, sb) == DEVFSADM_SUCCESS) { finddev_close(fhandle); return (DEVFSADM_SUCCESS); } *cp = '\0'; } finddev_close(fhandle); } /* no symlink found, so return error */ return (DEVFSADM_FAILURE); } /* * An existing matching ID was not found, so this function is called to * create the next lowest ID. In the INTEGER case, return the next * lowest unused integer. In the case of LETTER, return the next lowest * unused letter. Return empty string if all 26 are used. * Only IDs >= min will be returned. */ char * new_id(numeral_t *numeral, int type, char *min) { int imin; temp_t *temp; temp_t *ptr; temp_t **previous; temp_t *head = NULL; char *retval; static char tempbuff[8]; numeral_t *np; if (type == LETTER) { char letter[26], i; if (numeral == NULL) { return (s_strdup(min)); } for (i = 0; i < 26; i++) { letter[i] = 0; } for (np = numeral; np != NULL; np = np->next) { assert(np->flags == 0 || np->flags == NUMERAL_RESERVED); letter[*np->id - 'a']++; } imin = *min - 'a'; for (i = imin; i < 26; i++) { if (letter[i] == 0) { retval = s_malloc(2); retval[0] = 'a' + i; retval[1] = '\0'; return (retval); } } return (s_strdup("")); } if (type == INTEGER) { if (numeral == NULL) { return (s_strdup(min)); } imin = atoi(min); /* sort list */ for (np = numeral; np != NULL; np = np->next