17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5055e805aSedp * Common Development and Distribution License (the "License"). 6055e805aSedp * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21055e805aSedp 227c478bd9Sstevel@tonic-gate /* 23d62bc4baSyz147064 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * Devfsadm replaces drvconfig, audlinks, disks, tapes, ports, devlinks 317c478bd9Sstevel@tonic-gate * as a general purpose device administrative utility. It creates 327c478bd9Sstevel@tonic-gate * devices special files in /devices and logical links in /dev, and 337c478bd9Sstevel@tonic-gate * coordinates updates to /etc/path_to_instance with the kernel. It 347c478bd9Sstevel@tonic-gate * operates in both command line mode to handle user or script invoked 357c478bd9Sstevel@tonic-gate * reconfiguration updates, and operates in daemon mode to handle dynamic 367c478bd9Sstevel@tonic-gate * reconfiguration for hotplugging support. 377c478bd9Sstevel@tonic-gate */ 387c478bd9Sstevel@tonic-gate 3945916cd2Sjpk #include <string.h> 40f875b4ebSrica #include <deflt.h> 4145916cd2Sjpk #include <tsol/label.h> 4245916cd2Sjpk #include <bsm/devices.h> 4345916cd2Sjpk #include <bsm/devalloc.h> 447c478bd9Sstevel@tonic-gate #include <utime.h> 456638bf60Saj #include <sys/param.h> 466638bf60Saj #include <bsm/libbsm.h> 4745916cd2Sjpk #include "devfsadm_impl.h" 487c478bd9Sstevel@tonic-gate 4945916cd2Sjpk /* externs from devalloc.c */ 5045916cd2Sjpk extern void _reset_devalloc(int); 5145916cd2Sjpk extern void _update_devalloc_db(devlist_t *, int, int, char *, char *); 5245916cd2Sjpk extern int _da_check_for_usb(char *, char *); 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate /* create or remove nodes or links. unset with -n */ 557c478bd9Sstevel@tonic-gate static int file_mods = TRUE; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate /* cleanup mode. Set with -C */ 587c478bd9Sstevel@tonic-gate static int cleanup = FALSE; 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate /* devlinks -d compatibility */ 617c478bd9Sstevel@tonic-gate static int devlinks_debug = FALSE; 627c478bd9Sstevel@tonic-gate 6345916cd2Sjpk /* flag to check if system is labeled */ 6445916cd2Sjpk int system_labeled = FALSE; 657c478bd9Sstevel@tonic-gate 6645916cd2Sjpk /* flag to enable/disable device allocation with -e/-d */ 6745916cd2Sjpk static int devalloc_flag = 0; 6845916cd2Sjpk 696638bf60Saj /* flag that indicates if device allocation is on or not */ 706638bf60Saj static int devalloc_is_on = 0; 716638bf60Saj 7245916cd2Sjpk /* flag to update device allocation database for this device type */ 7345916cd2Sjpk static int update_devdb = 0; 7445916cd2Sjpk 7545916cd2Sjpk /* 7645916cd2Sjpk * devices to be deallocated with -d : 7745916cd2Sjpk * audio, floppy, cd, floppy, tape, rmdisk. 7845916cd2Sjpk */ 7945916cd2Sjpk static char *devalloc_list[10] = {DDI_NT_AUDIO, DDI_NT_CD, DDI_NT_CD_CHAN, 8045916cd2Sjpk DDI_NT_FD, DDI_NT_TAPE, DDI_NT_BLOCK_CHAN, 8145916cd2Sjpk DDI_NT_UGEN, DDI_NT_USB_ATTACHMENT_POINT, 8245916cd2Sjpk DDI_NT_SCSI_NEXUS, NULL}; 8345916cd2Sjpk 8445916cd2Sjpk /* list of allocatable devices */ 8545916cd2Sjpk static devlist_t devlist; 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate /* load a single driver only. set with -i */ 887c478bd9Sstevel@tonic-gate static int single_drv = FALSE; 897c478bd9Sstevel@tonic-gate static char *driver = NULL; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate /* attempt to load drivers or defer attach nodes */ 927c478bd9Sstevel@tonic-gate static int load_attach_drv = TRUE; 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate /* set if invoked via /usr/lib/devfsadm/devfsadmd */ 957c478bd9Sstevel@tonic-gate static int daemon_mode = FALSE; 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate /* output directed to syslog during daemon mode if set */ 987c478bd9Sstevel@tonic-gate static int logflag = FALSE; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate /* build links in /dev. -x to turn off */ 1017c478bd9Sstevel@tonic-gate static int build_dev = TRUE; 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate /* build nodes in /devices. -y to turn off */ 1047c478bd9Sstevel@tonic-gate static int build_devices = TRUE; 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate /* -z to turn off */ 1077c478bd9Sstevel@tonic-gate static int flush_path_to_inst_enable = TRUE; 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate /* variables used for path_to_inst flushing */ 1107c478bd9Sstevel@tonic-gate static int inst_count = 0; 1117c478bd9Sstevel@tonic-gate static mutex_t count_lock; 1127c478bd9Sstevel@tonic-gate static cond_t cv; 1137c478bd9Sstevel@tonic-gate 114ff2aee48Scth /* variables for minor_fini thread */ 1157c478bd9Sstevel@tonic-gate static mutex_t minor_fini_mutex; 116ff2aee48Scth static int minor_fini_canceled = TRUE; 117ff2aee48Scth static int minor_fini_delayed = FALSE; 118ff2aee48Scth static cond_t minor_fini_cv; 119ff2aee48Scth static int minor_fini_timeout = MINOR_FINI_TIMEOUT_DEFAULT; 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate /* single-threads /dev modification */ 1227c478bd9Sstevel@tonic-gate static sema_t dev_sema; 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /* the program we were invoked as; ie argv[0] */ 1257c478bd9Sstevel@tonic-gate static char *prog; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /* pointers to create/remove link lists */ 1287c478bd9Sstevel@tonic-gate static create_list_t *create_head = NULL; 1297c478bd9Sstevel@tonic-gate static remove_list_t *remove_head = NULL; 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate /* supports the class -c option */ 1327c478bd9Sstevel@tonic-gate static char **classes = NULL; 1337c478bd9Sstevel@tonic-gate static int num_classes = 0; 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate /* used with verbose option -v or -V */ 1367c478bd9Sstevel@tonic-gate static int num_verbose = 0; 1377c478bd9Sstevel@tonic-gate static char **verbose = NULL; 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate static struct mperm *minor_perms = NULL; 1407c478bd9Sstevel@tonic-gate static driver_alias_t *driver_aliases = NULL; 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate /* set if -r alternate root given */ 1437c478bd9Sstevel@tonic-gate static char *root_dir = ""; 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate /* /devices or <rootdir>/devices */ 1467c478bd9Sstevel@tonic-gate static char *devices_dir = DEVICES; 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate /* /dev or <rootdir>/dev */ 1497c478bd9Sstevel@tonic-gate static char *dev_dir = DEV; 1507c478bd9Sstevel@tonic-gate 151facf4a8dSllai1 /* /etc/dev or <rootdir>/etc/dev */ 152facf4a8dSllai1 static char *etc_dev_dir = ETCDEV; 153facf4a8dSllai1 154facf4a8dSllai1 /* 155facf4a8dSllai1 * writable root (for lock files and doors during install). 156facf4a8dSllai1 * This is also root dir for /dev attr dir during install. 157facf4a8dSllai1 */ 158facf4a8dSllai1 static char *attr_root = NULL; 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate /* /etc/path_to_inst unless -p used */ 1617c478bd9Sstevel@tonic-gate static char *inst_file = INSTANCE_FILE; 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate /* /usr/lib/devfsadm/linkmods unless -l used */ 1647c478bd9Sstevel@tonic-gate static char *module_dirs = MODULE_DIRS; 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate /* default uid/gid used if /etc/minor_perm entry not found */ 1677c478bd9Sstevel@tonic-gate static uid_t root_uid; 1687c478bd9Sstevel@tonic-gate static gid_t sys_gid; 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate /* /etc/devlink.tab unless devlinks -t used */ 1717c478bd9Sstevel@tonic-gate static char *devlinktab_file = NULL; 1727c478bd9Sstevel@tonic-gate 1738d483882Smlf /* File and data structure to reserve enumerate IDs */ 1748d483882Smlf static char *enumerate_file = ENUMERATE_RESERVED; 1758d483882Smlf static enumerate_file_t *enumerate_reserved = NULL; 1768d483882Smlf 1777c478bd9Sstevel@tonic-gate /* set if /dev link is new. speeds up rm_stale_links */ 1787c478bd9Sstevel@tonic-gate static int linknew = TRUE; 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate /* variables for devlink.tab compat processing */ 1817c478bd9Sstevel@tonic-gate static devlinktab_list_t *devlinktab_list = NULL; 1827c478bd9Sstevel@tonic-gate static unsigned int devlinktab_line = 0; 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate /* cache head for devfsadm_enumerate*() functions */ 1857c478bd9Sstevel@tonic-gate static numeral_set_t *head_numeral_set = NULL; 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate /* list list of devfsadm modules */ 1887c478bd9Sstevel@tonic-gate static module_t *module_head = NULL; 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate /* name_to_major list used in utility function */ 1917c478bd9Sstevel@tonic-gate static n2m_t *n2m_list = NULL; 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate /* cache of some links used for performance */ 1947c478bd9Sstevel@tonic-gate static linkhead_t *headlinkhead = NULL; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate /* locking variables to prevent multiples writes to /dev */ 1977c478bd9Sstevel@tonic-gate static int hold_dev_lock = FALSE; 1987c478bd9Sstevel@tonic-gate static int hold_daemon_lock = FALSE; 1997c478bd9Sstevel@tonic-gate static int dev_lock_fd; 2007c478bd9Sstevel@tonic-gate static int daemon_lock_fd; 2017c478bd9Sstevel@tonic-gate static char dev_lockfile[PATH_MAX + 1]; 2027c478bd9Sstevel@tonic-gate static char daemon_lockfile[PATH_MAX + 1]; 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate /* last devinfo node/minor processed. used for performance */ 2057c478bd9Sstevel@tonic-gate static di_node_t lnode; 2067c478bd9Sstevel@tonic-gate static di_minor_t lminor; 2077c478bd9Sstevel@tonic-gate static char lphy_path[PATH_MAX + 1] = {""}; 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate /* Globals used by the link database */ 2107c478bd9Sstevel@tonic-gate static di_devlink_handle_t devlink_cache; 2117c478bd9Sstevel@tonic-gate static int update_database = FALSE; 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate /* Globals used to set logindev perms */ 2147c478bd9Sstevel@tonic-gate static struct login_dev *login_dev_cache = NULL; 2157c478bd9Sstevel@tonic-gate static int login_dev_enable = FALSE; 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate /* Global to use devinfo snapshot cache */ 2187c478bd9Sstevel@tonic-gate static int use_snapshot_cache = FALSE; 2197c478bd9Sstevel@tonic-gate 220aa646b9dSvikram /* Global for no-further-processing hash */ 221aa646b9dSvikram static item_t **nfp_hash; 222aa646b9dSvikram static mutex_t nfp_mutex = DEFAULTMUTEX; 223aa646b9dSvikram 2247c478bd9Sstevel@tonic-gate /* 2257c478bd9Sstevel@tonic-gate * Packaged directories - not removed even when empty. 2267c478bd9Sstevel@tonic-gate * The dirs must be listed in canonical form 2277c478bd9Sstevel@tonic-gate * i.e. without leading "/dev/" 2287c478bd9Sstevel@tonic-gate */ 2297c478bd9Sstevel@tonic-gate static char *packaged_dirs[] = 2307c478bd9Sstevel@tonic-gate {"dsk", "rdsk", "term", NULL}; 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate /* RCM related globals */ 2337c478bd9Sstevel@tonic-gate static void *librcm_hdl; 2347c478bd9Sstevel@tonic-gate static rcm_handle_t *rcm_hdl = NULL; 2357c478bd9Sstevel@tonic-gate static thread_t process_rcm_events_tid; 2367c478bd9Sstevel@tonic-gate static struct rcm_eventq *volatile rcm_eventq_head = NULL; 2377c478bd9Sstevel@tonic-gate static struct rcm_eventq *rcm_eventq_tail = NULL; 2387c478bd9Sstevel@tonic-gate static mutex_t rcm_eventq_lock; 2397c478bd9Sstevel@tonic-gate static cond_t rcm_eventq_cv; 2407c478bd9Sstevel@tonic-gate static volatile int need_to_exit_rcm_event_thread = 0; 2417c478bd9Sstevel@tonic-gate 242facf4a8dSllai1 /* Devname globals */ 243facf4a8dSllai1 static int devname_debug_msg = 1; 244facf4a8dSllai1 static nvlist_t *devname_maps = NULL; 245facf4a8dSllai1 static int devname_first_call = 1; 246facf4a8dSllai1 static int load_devname_nsmaps = FALSE; 247facf4a8dSllai1 static int lookup_door_fd = -1; 248facf4a8dSllai1 static char *lookup_door_path; 249facf4a8dSllai1 2507c478bd9Sstevel@tonic-gate static void load_dev_acl(void); 2517c478bd9Sstevel@tonic-gate static void update_drvconf(major_t); 252facf4a8dSllai1 static void check_reconfig_state(void); 253facf4a8dSllai1 static void devname_setup_nsmaps(void); 254facf4a8dSllai1 static int s_stat(const char *, struct stat *); 2557c478bd9Sstevel@tonic-gate 2561ca93273Seota static int is_blank(char *); 2571ca93273Seota 258f05faa4eSjacobs /* sysevent queue related globals */ 259f05faa4eSjacobs static mutex_t syseventq_mutex = DEFAULTMUTEX; 260f05faa4eSjacobs static syseventq_t *syseventq_front; 261f05faa4eSjacobs static syseventq_t *syseventq_back; 262f05faa4eSjacobs static void process_syseventq(); 263f05faa4eSjacobs 2647c478bd9Sstevel@tonic-gate int 2657c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 2667c478bd9Sstevel@tonic-gate { 2677c478bd9Sstevel@tonic-gate struct passwd *pw; 2687c478bd9Sstevel@tonic-gate struct group *gp; 2697c478bd9Sstevel@tonic-gate pid_t pid; 2706638bf60Saj int cond = 0; 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 2737c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate if ((prog = strrchr(argv[0], '/')) == NULL) { 2767c478bd9Sstevel@tonic-gate prog = argv[0]; 2777c478bd9Sstevel@tonic-gate } else { 2787c478bd9Sstevel@tonic-gate prog++; 2797c478bd9Sstevel@tonic-gate } 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate if (getuid() != 0) { 2827c478bd9Sstevel@tonic-gate err_print(MUST_BE_ROOT); 2837c478bd9Sstevel@tonic-gate devfsadm_exit(1); 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate /* 2877c478bd9Sstevel@tonic-gate * Close all files except stdin/stdout/stderr 2887c478bd9Sstevel@tonic-gate */ 2897c478bd9Sstevel@tonic-gate closefrom(3); 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) { 2927c478bd9Sstevel@tonic-gate root_uid = pw->pw_uid; 2937c478bd9Sstevel@tonic-gate } else { 2947c478bd9Sstevel@tonic-gate err_print(CANT_FIND_USER, DEFAULT_DEV_USER); 2957c478bd9Sstevel@tonic-gate root_uid = (uid_t)0; /* assume 0 is root */ 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate /* the default group is sys */ 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) { 3017c478bd9Sstevel@tonic-gate sys_gid = gp->gr_gid; 3027c478bd9Sstevel@tonic-gate } else { 3037c478bd9Sstevel@tonic-gate err_print(CANT_FIND_GROUP, DEFAULT_DEV_GROUP); 3047c478bd9Sstevel@tonic-gate sys_gid = (gid_t)3; /* assume 3 is sys */ 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate (void) umask(0); 3087c478bd9Sstevel@tonic-gate 30945916cd2Sjpk system_labeled = is_system_labeled(); 31045916cd2Sjpk if (system_labeled == FALSE) { 31145916cd2Sjpk /* 31245916cd2Sjpk * is_system_labeled() will return false in case we are 31345916cd2Sjpk * starting before the first reboot after Trusted Extensions 314f875b4ebSrica * is enabled. Check the setting in /etc/system to see if 315f875b4ebSrica * TX is enabled (even if not yet booted). 31645916cd2Sjpk */ 317f875b4ebSrica if (defopen("/etc/system") == 0) { 318f875b4ebSrica if (defread("set sys_labeling=1") != NULL) 31945916cd2Sjpk system_labeled = TRUE; 320f875b4ebSrica 321f875b4ebSrica /* close defaults file */ 322f875b4ebSrica (void) defopen(NULL); 323f875b4ebSrica } 324f875b4ebSrica } 3256638bf60Saj /* 3266638bf60Saj * Check if device allocation is enabled. 3276638bf60Saj */ 3286638bf60Saj if (system_labeled) { 3296638bf60Saj /* 3306638bf60Saj * In TX, the first line in /etc/security/device_allocate has 3316638bf60Saj * DEVICE_ALLOCATION=ON if the feature is enabled. 3326638bf60Saj */ 3336638bf60Saj devalloc_is_on = da_is_on(); 3346638bf60Saj } else if (auditon(A_GETCOND, (caddr_t)&cond, sizeof (cond)) == 0) { 3356638bf60Saj /* 3366638bf60Saj * Device allocation (and auditing) is enabled if BSM is 3376638bf60Saj * enabled. auditon returns -1 and sets errno to EINVAL if BSM 3386638bf60Saj * is not enabled. 3396638bf60Saj */ 3406638bf60Saj devalloc_is_on = 1; 3416638bf60Saj } 342f875b4ebSrica 343f875b4ebSrica #ifdef DEBUG 344f875b4ebSrica if (system_labeled == FALSE) { 345f875b4ebSrica struct stat tx_stat; 346f875b4ebSrica 347facf4a8dSllai1 /* test hook: see also mkdevalloc.c and allocate.c */ 348facf4a8dSllai1 system_labeled = is_system_labeled_debug(&tx_stat); 349facf4a8dSllai1 } 350f875b4ebSrica #endif 35145916cd2Sjpk 3527c478bd9Sstevel@tonic-gate parse_args(argc, argv); 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate (void) sema_init(&dev_sema, 1, USYNC_THREAD, NULL); 3557c478bd9Sstevel@tonic-gate 35645916cd2Sjpk /* Initialize device allocation list */ 35745916cd2Sjpk devlist.audio = devlist.cd = devlist.floppy = devlist.tape = 35845916cd2Sjpk devlist.rmdisk = NULL; 35945916cd2Sjpk 3607c478bd9Sstevel@tonic-gate if (daemon_mode == TRUE) { 3617c478bd9Sstevel@tonic-gate /* 3627c478bd9Sstevel@tonic-gate * Build /dev and /devices before daemonizing if 3637c478bd9Sstevel@tonic-gate * reconfig booting and daemon invoked with alternate 3647c478bd9Sstevel@tonic-gate * root. This is to support install. 3657c478bd9Sstevel@tonic-gate */ 3667c478bd9Sstevel@tonic-gate if (getenv(RECONFIG_BOOT) != NULL && root_dir[0] != '\0') { 3677c478bd9Sstevel@tonic-gate vprint(INFO_MID, CONFIGURING); 3687c478bd9Sstevel@tonic-gate load_dev_acl(); 3697c478bd9Sstevel@tonic-gate update_drvconf((major_t)-1); 3707c478bd9Sstevel@tonic-gate process_devinfo_tree(); 3717c478bd9Sstevel@tonic-gate (void) modctl(MODSETMINIROOT); 3727c478bd9Sstevel@tonic-gate } 3737c478bd9Sstevel@tonic-gate 3747c478bd9Sstevel@tonic-gate /* 3757c478bd9Sstevel@tonic-gate * fork before detaching from tty in order to print error 3767c478bd9Sstevel@tonic-gate * message if unable to acquire file lock. locks not preserved 3777c478bd9Sstevel@tonic-gate * across forks. Even under debug we want to fork so that 3787c478bd9Sstevel@tonic-gate * when executed at boot we don't hang. 3797c478bd9Sstevel@tonic-gate */ 3807c478bd9Sstevel@tonic-gate if (fork() != 0) { 3817c478bd9Sstevel@tonic-gate devfsadm_exit(0); 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate 3847c478bd9Sstevel@tonic-gate /* set directory to / so it coredumps there */ 3857c478bd9Sstevel@tonic-gate if (chdir("/") == -1) { 3867c478bd9Sstevel@tonic-gate err_print(CHROOT_FAILED, strerror(errno)); 3877c478bd9Sstevel@tonic-gate } 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate /* only one daemon can run at a time */ 3907c478bd9Sstevel@tonic-gate if ((pid = enter_daemon_lock()) == getpid()) { 3917c478bd9Sstevel@tonic-gate detachfromtty(); 3927c478bd9Sstevel@tonic-gate (void) cond_init(&cv, USYNC_THREAD, 0); 3937c478bd9Sstevel@tonic-gate (void) mutex_init(&count_lock, USYNC_THREAD, 0); 3947c478bd9Sstevel@tonic-gate if (thr_create(NULL, NULL, 3957c478bd9Sstevel@tonic-gate (void *(*)(void *))instance_flush_thread, 396ff2aee48Scth NULL, THR_DETACHED, NULL) != 0) { 3977c478bd9Sstevel@tonic-gate err_print(CANT_CREATE_THREAD, "daemon", 3987c478bd9Sstevel@tonic-gate strerror(errno)); 3997c478bd9Sstevel@tonic-gate devfsadm_exit(1); 4007c478bd9Sstevel@tonic-gate } 4017c478bd9Sstevel@tonic-gate 402ff2aee48Scth /* start the minor_fini_thread */ 403ff2aee48Scth (void) mutex_init(&minor_fini_mutex, USYNC_THREAD, 0); 404ff2aee48Scth (void) cond_init(&minor_fini_cv, USYNC_THREAD, 0); 405ff2aee48Scth if (thr_create(NULL, NULL, 406ff2aee48Scth (void *(*)(void *))minor_fini_thread, 407ff2aee48Scth NULL, THR_DETACHED, NULL)) { 408ff2aee48Scth err_print(CANT_CREATE_THREAD, "minor_fini", 409ff2aee48Scth strerror(errno)); 410ff2aee48Scth devfsadm_exit(1); 411ff2aee48Scth } 412ff2aee48Scth 413ff2aee48Scth 4147c478bd9Sstevel@tonic-gate /* 4157c478bd9Sstevel@tonic-gate * No need for rcm notifications when running 4167c478bd9Sstevel@tonic-gate * with an alternate root. So initialize rcm only 4177c478bd9Sstevel@tonic-gate * when devfsadm is running with root dir "/". 4187c478bd9Sstevel@tonic-gate * Similarly, logindevperms need only be set 4197c478bd9Sstevel@tonic-gate * in daemon mode and when root dir is "/". 4207c478bd9Sstevel@tonic-gate */ 4217c478bd9Sstevel@tonic-gate if (root_dir[0] == '\0') { 4227c478bd9Sstevel@tonic-gate (void) rcm_init(); 4237c478bd9Sstevel@tonic-gate login_dev_enable = TRUE; 4247c478bd9Sstevel@tonic-gate } 4257c478bd9Sstevel@tonic-gate daemon_update(); 4267c478bd9Sstevel@tonic-gate } else { 4277c478bd9Sstevel@tonic-gate err_print(DAEMON_RUNNING, pid); 4287c478bd9Sstevel@tonic-gate devfsadm_exit(1); 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate exit_daemon_lock(); 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate } else { 4337c478bd9Sstevel@tonic-gate /* not a daemon, so just build /dev and /devices */ 4347c478bd9Sstevel@tonic-gate process_devinfo_tree(); 43545916cd2Sjpk if (devalloc_flag != 0) 43645916cd2Sjpk /* Enable/disable device allocation */ 43745916cd2Sjpk _reset_devalloc(devalloc_flag); 4387c478bd9Sstevel@tonic-gate } 4397c478bd9Sstevel@tonic-gate return (0); 4407c478bd9Sstevel@tonic-gate } 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate static void 4437c478bd9Sstevel@tonic-gate update_drvconf(major_t major) 4447c478bd9Sstevel@tonic-gate { 4457c478bd9Sstevel@tonic-gate if (modctl(MODLOADDRVCONF, major) != 0) 4467c478bd9Sstevel@tonic-gate err_print(gettext("update_drvconf failed for major %d\n"), 4477c478bd9Sstevel@tonic-gate major); 4487c478bd9Sstevel@tonic-gate } 4497c478bd9Sstevel@tonic-gate 450aa646b9dSvikram 4517c478bd9Sstevel@tonic-gate static void 4527c478bd9Sstevel@tonic-gate load_dev_acl() 4537c478bd9Sstevel@tonic-gate { 4547c478bd9Sstevel@tonic-gate if (load_devpolicy() != 0) 4557c478bd9Sstevel@tonic-gate err_print(gettext("device policy load failed\n")); 4567c478bd9Sstevel@tonic-gate load_minor_perm_file(); 4577c478bd9Sstevel@tonic-gate } 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate /* 460facf4a8dSllai1 * As devfsadm is run early in boot to provide the kernel with 461facf4a8dSllai1 * minor_perm info, we might as well check for reconfig at the 462facf4a8dSllai1 * same time to avoid running devfsadm twice. This gets invoked 463facf4a8dSllai1 * earlier than the env variable RECONFIG_BOOT is set up. 4647c478bd9Sstevel@tonic-gate */ 465facf4a8dSllai1 static void 466facf4a8dSllai1 check_reconfig_state() 4677c478bd9Sstevel@tonic-gate { 468facf4a8dSllai1 struct stat sb; 4697c478bd9Sstevel@tonic-gate 470facf4a8dSllai1 if (s_stat("/reconfigure", &sb) == 0) { 471facf4a8dSllai1 (void) modctl(MODDEVNAME, MODDEVNAME_RECONFIG, 0); 472facf4a8dSllai1 } 4737c478bd9Sstevel@tonic-gate } 4747c478bd9Sstevel@tonic-gate 475facf4a8dSllai1 static void 476facf4a8dSllai1 modctl_sysavail() 477facf4a8dSllai1 { 478facf4a8dSllai1 /* 479facf4a8dSllai1 * Inform /dev that system is available, that 480facf4a8dSllai1 * implicit reconfig can now be performed. 481facf4a8dSllai1 */ 482facf4a8dSllai1 (void) modctl(MODDEVNAME, MODDEVNAME_SYSAVAIL, 0); 4837c478bd9Sstevel@tonic-gate } 4847c478bd9Sstevel@tonic-gate 485facf4a8dSllai1 static void 486facf4a8dSllai1 set_lock_root(void) 487facf4a8dSllai1 { 488facf4a8dSllai1 struct stat sb; 489facf4a8dSllai1 char *lock_root; 490facf4a8dSllai1 size_t len; 4917c478bd9Sstevel@tonic-gate 492facf4a8dSllai1 lock_root = attr_root ? attr_root : root_dir; 493facf4a8dSllai1 494facf4a8dSllai1 len = strlen(lock_root) + strlen(ETCDEV) + 1; 495facf4a8dSllai1 etc_dev_dir = s_malloc(len); 496facf4a8dSllai1 (void) snprintf(etc_dev_dir, len, "%s%s", lock_root, ETCDEV); 497facf4a8dSllai1 498facf4a8dSllai1 if (s_stat(etc_dev_dir, &sb) != 0) { 499facf4a8dSllai1 s_mkdirp(etc_dev_dir, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); 500facf4a8dSllai1 } else if (!S_ISDIR(sb.st_mode)) { 501facf4a8dSllai1 err_print(NOT_DIR, etc_dev_dir); 502facf4a8dSllai1 devfsadm_exit(1); 503facf4a8dSllai1 } 5047c478bd9Sstevel@tonic-gate } 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate 5077c478bd9Sstevel@tonic-gate /* 5087c478bd9Sstevel@tonic-gate * Parse arguments for all 6 programs handled from devfsadm. 5097c478bd9Sstevel@tonic-gate */ 5107c478bd9Sstevel@tonic-gate static void 5117c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[]) 5127c478bd9Sstevel@tonic-gate { 5137c478bd9Sstevel@tonic-gate char opt; 5147c478bd9Sstevel@tonic-gate char get_linkcompat_opts = FALSE; 5157c478bd9Sstevel@tonic-gate char *compat_class; 5167c478bd9Sstevel@tonic-gate int num_aliases = 0; 5177c478bd9Sstevel@tonic-gate int len; 5187c478bd9Sstevel@tonic-gate int retval; 5197c478bd9Sstevel@tonic-gate int add_bind = FALSE; 5207c478bd9Sstevel@tonic-gate struct aliases *ap = NULL; 5217c478bd9Sstevel@tonic-gate struct aliases *a_head = NULL; 5227c478bd9Sstevel@tonic-gate struct aliases *a_tail = NULL; 5237c478bd9Sstevel@tonic-gate struct modconfig mc; 5247c478bd9Sstevel@tonic-gate 5257c478bd9Sstevel@tonic-gate if (strcmp(prog, DISKS) == 0) { 5267c478bd9Sstevel@tonic-gate compat_class = "disk"; 5277c478bd9Sstevel@tonic-gate get_linkcompat_opts = TRUE; 5287c478bd9Sstevel@tonic-gate 5297c478bd9Sstevel@tonic-gate } else if (strcmp(prog, TAPES) == 0) { 5307c478bd9Sstevel@tonic-gate compat_class = "tape"; 5317c478bd9Sstevel@tonic-gate get_linkcompat_opts = TRUE; 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate } else if (strcmp(prog, PORTS) == 0) { 5347c478bd9Sstevel@tonic-gate compat_class = "port"; 5357c478bd9Sstevel@tonic-gate get_linkcompat_opts = TRUE; 5367c478bd9Sstevel@tonic-gate 5377c478bd9Sstevel@tonic-gate } else if (strcmp(prog, AUDLINKS) == 0) { 5387c478bd9Sstevel@tonic-gate compat_class = "audio"; 5397c478bd9Sstevel@tonic-gate get_linkcompat_opts = TRUE; 5407c478bd9Sstevel@tonic-gate 5417c478bd9Sstevel@tonic-gate } else if (strcmp(prog, DEVLINKS) == 0) { 5427c478bd9Sstevel@tonic-gate devlinktab_file = DEVLINKTAB_FILE; 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate build_devices = FALSE; 5457c478bd9Sstevel@tonic-gate load_attach_drv = FALSE; 5467c478bd9Sstevel@tonic-gate 5477c478bd9Sstevel@tonic-gate while ((opt = getopt(argc, argv, "dnr:st:vV:")) != EOF) { 5487c478bd9Sstevel@tonic-gate switch (opt) { 5497c478bd9Sstevel@tonic-gate case 'd': 5507c478bd9Sstevel@tonic-gate file_mods = FALSE; 5517c478bd9Sstevel@tonic-gate flush_path_to_inst_enable = FALSE; 5527c478bd9Sstevel@tonic-gate devlinks_debug = TRUE; 5537c478bd9Sstevel@tonic-gate break; 5547c478bd9Sstevel@tonic-gate case 'n': 5557c478bd9Sstevel@tonic-gate /* prevent driver loading and deferred attach */ 5567c478bd9Sstevel@tonic-gate load_attach_drv = FALSE; 5577c478bd9Sstevel@tonic-gate break; 5587c478bd9Sstevel@tonic-gate case 'r': 559facf4a8dSllai1 set_root_devices_dev_dir(optarg); 5607c478bd9Sstevel@tonic-gate if (zone_pathcheck(root_dir) != 5617c478bd9Sstevel@tonic-gate DEVFSADM_SUCCESS) 5627c478bd9Sstevel@tonic-gate devfsadm_exit(1); 5637c478bd9Sstevel@tonic-gate break; 5647c478bd9Sstevel@tonic-gate case 's': 5657c478bd9Sstevel@tonic-gate /* 5667c478bd9Sstevel@tonic-gate * suppress. don't create/remove links/nodes 5677c478bd9Sstevel@tonic-gate * useful with -v or -V 5687c478bd9Sstevel@tonic-gate */ 5697c478bd9Sstevel@tonic-gate file_mods = FALSE; 5707c478bd9Sstevel@tonic-gate flush_path_to_inst_enable = FALSE; 5717c478bd9Sstevel@tonic-gate break; 5727c478bd9Sstevel@tonic-gate case 't': 5737c478bd9Sstevel@tonic-gate /* supply a non-default table file */ 5747c478bd9Sstevel@tonic-gate devlinktab_file = optarg; 5757c478bd9Sstevel@tonic-gate break; 5767c478bd9Sstevel@tonic-gate case 'v': 5777c478bd9Sstevel@tonic-gate /* documented verbose flag */ 5787c478bd9Sstevel@tonic-gate add_verbose_id(VERBOSE_MID); 5797c478bd9Sstevel@tonic-gate break; 5807c478bd9Sstevel@tonic-gate case 'V': 5817c478bd9Sstevel@tonic-gate /* undocumented for extra verbose levels */ 5827c478bd9Sstevel@tonic-gate add_verbose_id(optarg); 5837c478bd9Sstevel@tonic-gate break; 5847c478bd9Sstevel@tonic-gate default: 5857c478bd9Sstevel@tonic-gate usage(); 5867c478bd9Sstevel@tonic-gate break; 5877c478bd9Sstevel@tonic-gate } 5887c478bd9Sstevel@tonic-gate } 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate if (optind < argc) { 5917c478bd9Sstevel@tonic-gate usage(); 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate 5947c478bd9Sstevel@tonic-gate } else if (strcmp(prog, DRVCONFIG) == 0) { 5957c478bd9Sstevel@tonic-gate build_dev = FALSE; 5967c478bd9Sstevel@tonic-gate 5977c478bd9Sstevel@tonic-gate while ((opt = 5987c478bd9Sstevel@tonic-gate getopt(argc, argv, "a:bdc:i:m:np:R:r:svV:")) != EOF) { 5997c478bd9Sstevel@tonic-gate switch (opt) { 6007c478bd9Sstevel@tonic-gate case 'a': 6017c478bd9Sstevel@tonic-gate ap = calloc(sizeof (struct aliases), 1); 6027c478bd9Sstevel@tonic-gate ap->a_name = dequote(optarg); 6037c478bd9Sstevel@tonic-gate len = strlen(ap->a_name) + 1; 6047c478bd9Sstevel@tonic-gate if (len > MAXMODCONFNAME) { 6057c478bd9Sstevel@tonic-gate err_print(ALIAS_TOO_LONG, 6067c478bd9Sstevel@tonic-gate MAXMODCONFNAME, ap->a_name); 6077c478bd9Sstevel@tonic-gate devfsadm_exit(1); 6087c478bd9Sstevel@tonic-gate } 6097c478bd9Sstevel@tonic-gate ap->a_len = len; 6107c478bd9Sstevel@tonic-gate if (a_tail == NULL) { 6117c478bd9Sstevel@tonic-gate a_head = ap; 6127c478bd9Sstevel@tonic-gate } else { 6137c478bd9Sstevel@tonic-gate a_tail->a_next = ap; 6147c478bd9Sstevel@tonic-gate } 6157c478bd9Sstevel@tonic-gate a_tail = ap; 6167c478bd9Sstevel@tonic-gate num_aliases++; 6177c478bd9Sstevel@tonic-gate add_bind = TRUE; 6187c478bd9Sstevel@tonic-gate break; 6197c478bd9Sstevel@tonic-gate case 'b': 6207c478bd9Sstevel@tonic-gate add_bind = TRUE; 6217c478bd9Sstevel@tonic-gate break; 6227c478bd9Sstevel@tonic-gate case 'c': 6237c478bd9Sstevel@tonic-gate (void) strcpy(mc.drvclass, optarg); 6247c478bd9Sstevel@tonic-gate break; 6257c478bd9Sstevel@tonic-gate case 'd': 6267c478bd9Sstevel@tonic-gate /* 6277c478bd9Sstevel@tonic-gate * need to keep for compatibility, but 6287c478bd9Sstevel@tonic-gate * do nothing. 6297c478bd9Sstevel@tonic-gate */ 6307c478bd9Sstevel@tonic-gate break; 6317c478bd9Sstevel@tonic-gate case 'i': 6327c478bd9Sstevel@tonic-gate single_drv = TRUE; 6337c478bd9Sstevel@tonic-gate (void) strcpy(mc.drvname, optarg); 6347c478bd9Sstevel@tonic-gate driver = s_strdup(optarg); 6357c478bd9Sstevel@tonic-gate break; 6367c478bd9Sstevel@tonic-gate case 'm': 6377c478bd9Sstevel@tonic-gate mc.major = atoi(optarg); 6387c478bd9Sstevel@tonic-gate break; 6397c478bd9Sstevel@tonic-gate case 'n': 6407c478bd9Sstevel@tonic-gate /* prevent driver loading and deferred attach */ 6417c478bd9Sstevel@tonic-gate load_attach_drv = FALSE; 6427c478bd9Sstevel@tonic-gate break; 6437c478bd9Sstevel@tonic-gate case 'p': 6447c478bd9Sstevel@tonic-gate /* specify alternate path_to_inst file */ 6457c478bd9Sstevel@tonic-gate inst_file = s_strdup(optarg); 6467c478bd9Sstevel@tonic-gate break; 6477c478bd9Sstevel@tonic-gate case 'R': 6487c478bd9Sstevel@tonic-gate /* 6497c478bd9Sstevel@tonic-gate * Private flag for suninstall to populate 6507c478bd9Sstevel@tonic-gate * device information on the installed root. 6517c478bd9Sstevel@tonic-gate */ 6527c478bd9Sstevel@tonic-gate root_dir = s_strdup(optarg); 6537c478bd9Sstevel@tonic-gate if (zone_pathcheck(root_dir) != 6547c478bd9Sstevel@tonic-gate DEVFSADM_SUCCESS) 6557c478bd9Sstevel@tonic-gate devfsadm_exit(devfsadm_copy()); 6567c478bd9Sstevel@tonic-gate break; 6577c478bd9Sstevel@tonic-gate case 'r': 6587c478bd9Sstevel@tonic-gate devices_dir = s_strdup(optarg); 6597c478bd9Sstevel@tonic-gate if (zone_pathcheck(devices_dir) != 6607c478bd9Sstevel@tonic-gate DEVFSADM_SUCCESS) 6617c478bd9Sstevel@tonic-gate devfsadm_exit(1); 6627c478bd9Sstevel@tonic-gate break; 6637c478bd9Sstevel@tonic-gate case 's': 6647c478bd9Sstevel@tonic-gate /* 6657c478bd9Sstevel@tonic-gate * suppress. don't create nodes 6667c478bd9Sstevel@tonic-gate * useful with -v or -V 6677c478bd9Sstevel@tonic-gate */ 6687c478bd9Sstevel@tonic-gate file_mods = FALSE; 6697c478bd9Sstevel@tonic-gate flush_path_to_inst_enable = FALSE; 6707c478bd9Sstevel@tonic-gate break; 6717c478bd9Sstevel@tonic-gate case 'v': 6727c478bd9Sstevel@tonic-gate /* documented verbose flag */ 6737c478bd9Sstevel@tonic-gate add_verbose_id(VERBOSE_MID); 6747c478bd9Sstevel@tonic-gate break; 6757c478bd9Sstevel@tonic-gate case 'V': 6767c478bd9Sstevel@tonic-gate /* undocumented for extra verbose levels */ 6777c478bd9Sstevel@tonic-gate add_verbose_id(optarg); 6787c478bd9Sstevel@tonic-gate break; 6797c478bd9Sstevel@tonic-gate default: 6807c478bd9Sstevel@tonic-gate usage(); 6817c478bd9Sstevel@tonic-gate } 6827c478bd9Sstevel@tonic-gate } 6837c478bd9Sstevel@tonic-gate 6847c478bd9Sstevel@tonic-gate if (optind < argc) { 6857c478bd9Sstevel@tonic-gate usage(); 6867c478bd9Sstevel@tonic-gate } 6877c478bd9Sstevel@tonic-gate 6887c478bd9Sstevel@tonic-gate if ((add_bind == TRUE) && (mc.major == -1 || 6897c478bd9Sstevel@tonic-gate mc.drvname[0] == NULL)) { 6907c478bd9Sstevel@tonic-gate err_print(MAJOR_AND_B_FLAG); 6917c478bd9Sstevel@tonic-gate devfsadm_exit(1); 6927c478bd9Sstevel@tonic-gate } 6937c478bd9Sstevel@tonic-gate if (add_bind == TRUE) { 6947c478bd9Sstevel@tonic-gate mc.num_aliases = num_aliases; 6957c478bd9Sstevel@tonic-gate mc.ap = a_head; 6967c478bd9Sstevel@tonic-gate retval = modctl(MODADDMAJBIND, NULL, (caddr_t)&mc); 6977c478bd9Sstevel@tonic-gate if (retval < 0) { 6987c478bd9Sstevel@tonic-gate err_print(MODCTL_ADDMAJBIND); 6997c478bd9Sstevel@tonic-gate } 7007c478bd9Sstevel@tonic-gate devfsadm_exit(retval); 7017c478bd9Sstevel@tonic-gate } 7027c478bd9Sstevel@tonic-gate 7037c478bd9Sstevel@tonic-gate } else if ((strcmp(prog, DEVFSADM) == 0) || 7047c478bd9Sstevel@tonic-gate (strcmp(prog, DEVFSADMD) == 0)) { 7057c478bd9Sstevel@tonic-gate char *zonename = NULL; 7067c478bd9Sstevel@tonic-gate int init_drvconf = 0; 7077c478bd9Sstevel@tonic-gate int init_perm = 0; 7087c478bd9Sstevel@tonic-gate int public_mode = 0; 709facf4a8dSllai1 int init_sysavail = 0; 7107c478bd9Sstevel@tonic-gate 7117c478bd9Sstevel@tonic-gate if (strcmp(prog, DEVFSADMD) == 0) { 7127c478bd9Sstevel@tonic-gate daemon_mode = TRUE; 7137c478bd9Sstevel@tonic-gate } 7147c478bd9Sstevel@tonic-gate 7157c478bd9Sstevel@tonic-gate devlinktab_file = DEVLINKTAB_FILE; 7167c478bd9Sstevel@tonic-gate 7177c478bd9Sstevel@tonic-gate while ((opt = getopt(argc, argv, 718facf4a8dSllai1 "a:Cc:deIi:l:mnp:PR:r:sSt:vV:x:")) != EOF) { 719facf4a8dSllai1 if (opt == 'I' || opt == 'P' || opt == 'S') { 7207c478bd9Sstevel@tonic-gate if (public_mode) 7217c478bd9Sstevel@tonic-gate usage(); 7227c478bd9Sstevel@tonic-gate } else { 723facf4a8dSllai1 if (init_perm || init_drvconf || init_sysavail) 7247c478bd9Sstevel@tonic-gate usage(); 7257c478bd9Sstevel@tonic-gate public_mode = 1; 7267c478bd9Sstevel@tonic-gate } 7277c478bd9Sstevel@tonic-gate switch (opt) { 728facf4a8dSllai1 case 'a': 729facf4a8dSllai1 attr_root = s_strdup(optarg); 730facf4a8dSllai1 break; 7317c478bd9Sstevel@tonic-gate case 'C': 7327c478bd9Sstevel@tonic-gate cleanup = TRUE; 7337c478bd9Sstevel@tonic-gate break; 7347c478bd9Sstevel@tonic-gate case 'c': 7357c478bd9Sstevel@tonic-gate num_classes++; 7367c478bd9Sstevel@tonic-gate classes = s_realloc(classes, num_classes * 7377c478bd9Sstevel@tonic-gate sizeof (char *)); 7387c478bd9Sstevel@tonic-gate classes[num_classes - 1] = optarg; 7397c478bd9Sstevel@tonic-gate break; 7407c478bd9Sstevel@tonic-gate case 'd': 7417c478bd9Sstevel@tonic-gate if (daemon_mode == FALSE) { 74245916cd2Sjpk /* 74345916cd2Sjpk * Device allocation to be disabled. 74445916cd2Sjpk */ 74545916cd2Sjpk devalloc_flag = DA_OFF; 74645916cd2Sjpk build_dev = FALSE; 74745916cd2Sjpk } 74845916cd2Sjpk break; 74945916cd2Sjpk case 'e': 75045916cd2Sjpk if (daemon_mode == FALSE) { 75145916cd2Sjpk /* 75245916cd2Sjpk * Device allocation to be enabled. 75345916cd2Sjpk */ 75445916cd2Sjpk devalloc_flag = DA_ON; 7557c478bd9Sstevel@tonic-gate build_dev = FALSE; 7567c478bd9Sstevel@tonic-gate } 7577c478bd9Sstevel@tonic-gate break; 7587c478bd9Sstevel@tonic-gate case 'I': /* update kernel driver.conf cache */ 7597c478bd9Sstevel@tonic-gate if (daemon_mode == TRUE) 7607c478bd9Sstevel@tonic-gate usage(); 7617c478bd9Sstevel@tonic-gate init_drvconf = 1; 7627c478bd9Sstevel@tonic-gate break; 7637c478bd9Sstevel@tonic-gate case 'i': 7647c478bd9Sstevel@tonic-gate single_drv = TRUE; 7657c478bd9Sstevel@tonic-gate driver = s_strdup(optarg); 7667c478bd9Sstevel@tonic-gate break; 7677c478bd9Sstevel@tonic-gate case 'l': 7687c478bd9Sstevel@tonic-gate /* specify an alternate module load path */ 7697c478bd9Sstevel@tonic-gate module_dirs = s_strdup(optarg); 7707c478bd9Sstevel@tonic-gate break; 771facf4a8dSllai1 case 'm': 772facf4a8dSllai1 load_devname_nsmaps = TRUE; 773facf4a8dSllai1 break; 7747c478bd9Sstevel@tonic-gate case 'n': 7757c478bd9Sstevel@tonic-gate /* prevent driver loading and deferred attach */ 7767c478bd9Sstevel@tonic-gate load_attach_drv = FALSE; 7777c478bd9Sstevel@tonic-gate break; 7787c478bd9Sstevel@tonic-gate case 'p': 7797c478bd9Sstevel@tonic-gate /* specify alternate path_to_inst file */ 7807c478bd9Sstevel@tonic-gate inst_file = s_strdup(optarg); 7817c478bd9Sstevel@tonic-gate break; 7827c478bd9Sstevel@tonic-gate case 'P': 7837c478bd9Sstevel@tonic-gate if (daemon_mode == TRUE) 7847c478bd9Sstevel@tonic-gate usage(); 7857c478bd9Sstevel@tonic-gate /* load minor_perm and device_policy */ 7867c478bd9Sstevel@tonic-gate init_perm = 1; 7877c478bd9Sstevel@tonic-gate break; 7887c478bd9Sstevel@tonic-gate case 'R': 7897c478bd9Sstevel@tonic-gate /* 7907c478bd9Sstevel@tonic-gate * Private flag for suninstall to populate 7917c478bd9Sstevel@tonic-gate * device information on the installed root. 7927c478bd9Sstevel@tonic-gate */ 7937c478bd9Sstevel@tonic-gate root_dir = s_strdup(optarg); 7947c478bd9Sstevel@tonic-gate devfsadm_exit(devfsadm_copy()); 7957c478bd9Sstevel@tonic-gate break; 7967c478bd9Sstevel@tonic-gate case 'r': 797facf4a8dSllai1 set_root_devices_dev_dir(optarg); 7987c478bd9Sstevel@tonic-gate break; 7997c478bd9Sstevel@tonic-gate case 's': 8007c478bd9Sstevel@tonic-gate /* 8017c478bd9Sstevel@tonic-gate * suppress. don't create/remove links/nodes 8027c478bd9Sstevel@tonic-gate * useful with -v or -V 8037c478bd9Sstevel@tonic-gate */ 8047c478bd9Sstevel@tonic-gate file_mods = FALSE; 8057c478bd9Sstevel@tonic-gate flush_path_to_inst_enable = FALSE; 8067c478bd9Sstevel@tonic-gate break; 807facf4a8dSllai1 case 'S': 808facf4a8dSllai1 if (daemon_mode == TRUE) 809facf4a8dSllai1 usage(); 810facf4a8dSllai1 init_sysavail = 1; 811facf4a8dSllai1 break; 8127c478bd9Sstevel@tonic-gate case 't': 8137c478bd9Sstevel@tonic-gate devlinktab_file = optarg; 8147c478bd9Sstevel@tonic-gate break; 8157c478bd9Sstevel@tonic-gate case 'v': 8167c478bd9Sstevel@tonic-gate /* documented verbose flag */ 8177c478bd9Sstevel@tonic-gate add_verbose_id(VERBOSE_MID); 8187c478bd9Sstevel@tonic-gate break; 8197c478bd9Sstevel@tonic-gate case 'V': 8207c478bd9Sstevel@tonic-gate /* undocumented: specify verbose lvl */ 8217c478bd9Sstevel@tonic-gate add_verbose_id(optarg); 8227c478bd9Sstevel@tonic-gate break; 8237c478bd9Sstevel@tonic-gate case 'x': 8247c478bd9Sstevel@tonic-gate /* 8257c478bd9Sstevel@tonic-gate * x is the "private switch" option. The 8267c478bd9Sstevel@tonic-gate * goal is to not suck up all the other 8277c478bd9Sstevel@tonic-gate * option letters. 8287c478bd9Sstevel@tonic-gate */ 8297c478bd9Sstevel@tonic-gate if (strcmp(optarg, "update_devlinksdb") == 0) { 8307c478bd9Sstevel@tonic-gate update_database = TRUE; 8317c478bd9Sstevel@tonic-gate } else if (strcmp(optarg, "no_dev") == 0) { 8327c478bd9Sstevel@tonic-gate /* don't build /dev */ 8337c478bd9Sstevel@tonic-gate build_dev = FALSE; 8347c478bd9Sstevel@tonic-gate } else if (strcmp(optarg, "no_devices") == 0) { 8357c478bd9Sstevel@tonic-gate /* don't build /devices */ 8367c478bd9Sstevel@tonic-gate build_devices = FALSE; 8377c478bd9Sstevel@tonic-gate } else if (strcmp(optarg, "no_p2i") == 0) { 8387c478bd9Sstevel@tonic-gate /* don't flush path_to_inst */ 8397c478bd9Sstevel@tonic-gate flush_path_to_inst_enable = FALSE; 8407c478bd9Sstevel@tonic-gate } else if (strcmp(optarg, "use_dicache") == 0) { 8417c478bd9Sstevel@tonic-gate use_snapshot_cache = TRUE; 8427c478bd9Sstevel@tonic-gate } else { 8437c478bd9Sstevel@tonic-gate usage(); 8447c478bd9Sstevel@tonic-gate } 8457c478bd9Sstevel@tonic-gate break; 8467c478bd9Sstevel@tonic-gate default: 8477c478bd9Sstevel@tonic-gate usage(); 8487c478bd9Sstevel@tonic-gate break; 8497c478bd9Sstevel@tonic-gate } 8507c478bd9Sstevel@tonic-gate } 8517c478bd9Sstevel@tonic-gate if (optind < argc) { 8527c478bd9Sstevel@tonic-gate usage(); 8537c478bd9Sstevel@tonic-gate } 8547c478bd9Sstevel@tonic-gate 8557c478bd9Sstevel@tonic-gate /* 8567c478bd9Sstevel@tonic-gate * We're not in zone mode; Check to see if the rootpath 8577c478bd9Sstevel@tonic-gate * collides with any zonepaths. 8587c478bd9Sstevel@tonic-gate */ 8597c478bd9Sstevel@tonic-gate if (zonename == NULL) { 8607c478bd9Sstevel@tonic-gate if (zone_pathcheck(root_dir) != DEVFSADM_SUCCESS) 8617c478bd9Sstevel@tonic-gate devfsadm_exit(1); 8627c478bd9Sstevel@tonic-gate } 8637c478bd9Sstevel@tonic-gate 864facf4a8dSllai1 if (init_drvconf || init_perm || init_sysavail) { 8657c478bd9Sstevel@tonic-gate /* 8667c478bd9Sstevel@tonic-gate * Load minor perm before force-loading drivers 8677c478bd9Sstevel@tonic-gate * so the correct permissions are picked up. 8687c478bd9Sstevel@tonic-gate */ 869facf4a8dSllai1 if (init_perm) { 870facf4a8dSllai1 check_reconfig_state(); 8717c478bd9Sstevel@tonic-gate load_dev_acl(); 872facf4a8dSllai1 } 8737c478bd9Sstevel@tonic-gate if (init_drvconf) 8747c478bd9Sstevel@tonic-gate update_drvconf((major_t)-1); 875facf4a8dSllai1 if (init_sysavail) 876facf4a8dSllai1 modctl_sysavail(); 8777c478bd9Sstevel@tonic-gate devfsadm_exit(0); 8787c478bd9Sstevel@tonic-gate /* NOTREACHED */ 8797c478bd9Sstevel@tonic-gate } 880facf4a8dSllai1 881facf4a8dSllai1 if (load_devname_nsmaps == TRUE) { 882facf4a8dSllai1 devname_setup_nsmaps(); 883facf4a8dSllai1 devfsadm_exit(0); 884facf4a8dSllai1 } 8857c478bd9Sstevel@tonic-gate } 8867c478bd9Sstevel@tonic-gate 8877c478bd9Sstevel@tonic-gate 8887c478bd9Sstevel@tonic-gate if (get_linkcompat_opts == TRUE) { 8897c478bd9Sstevel@tonic-gate 8907c478bd9Sstevel@tonic-gate build_devices = FALSE; 8917c478bd9Sstevel@tonic-gate load_attach_drv = FALSE; 8927c478bd9Sstevel@tonic-gate num_classes++; 8937c478bd9Sstevel@tonic-gate classes = s_realloc(classes, num_classes * 8947c478bd9Sstevel@tonic-gate sizeof (char *)); 8957c478bd9Sstevel@tonic-gate classes[num_classes - 1] = compat_class; 8967c478bd9Sstevel@tonic-gate 8977c478bd9Sstevel@tonic-gate while ((opt = getopt(argc, argv, "Cnr:svV:")) != EOF) { 8987c478bd9Sstevel@tonic-gate switch (opt) { 8997c478bd9Sstevel@tonic-gate case 'C': 9007c478bd9Sstevel@tonic-gate cleanup = TRUE; 9017c478bd9Sstevel@tonic-gate break; 9027c478bd9Sstevel@tonic-gate case 'n': 9037c478bd9Sstevel@tonic-gate /* prevent driver loading or deferred attach */ 9047c478bd9Sstevel@tonic-gate load_attach_drv = FALSE; 9057c478bd9Sstevel@tonic-gate break; 9067c478bd9Sstevel@tonic-gate case 'r': 907facf4a8dSllai1 set_root_devices_dev_dir(optarg); 9087c478bd9Sstevel@tonic-gate if (zone_pathcheck(root_dir) != 9097c478bd9Sstevel@tonic-gate DEVFSADM_SUCCESS) 9107c478bd9Sstevel@tonic-gate devfsadm_exit(1); 9117c478bd9Sstevel@tonic-gate break; 9127c478bd9Sstevel@tonic-gate case 's': 9137c478bd9Sstevel@tonic-gate /* suppress. don't create/remove links/nodes */ 9147c478bd9Sstevel@tonic-gate /* useful with -v or -V */ 9157c478bd9Sstevel@tonic-gate file_mods = FALSE; 9167c478bd9Sstevel@tonic-gate flush_path_to_inst_enable = FALSE; 9177c478bd9Sstevel@tonic-gate break; 9187c478bd9Sstevel@tonic-gate case 'v': 9197c478bd9Sstevel@tonic-gate /* documented verbose flag */ 9207c478bd9Sstevel@tonic-gate add_verbose_id(VERBOSE_MID); 9217c478bd9Sstevel@tonic-gate break; 9227c478bd9Sstevel@tonic-gate case 'V': 9237c478bd9Sstevel@tonic-gate /* undocumented for extra verbose levels */ 9247c478bd9Sstevel@tonic-gate add_verbose_id(optarg); 9257c478bd9Sstevel@tonic-gate break; 9267c478bd9Sstevel@tonic-gate default: 9277c478bd9Sstevel@tonic-gate usage(); 9287c478bd9Sstevel@tonic-gate } 9297c478bd9Sstevel@tonic-gate } 9307c478bd9Sstevel@tonic-gate if (optind < argc) { 9317c478bd9Sstevel@tonic-gate usage(); 9327c478bd9Sstevel@tonic-gate } 9337c478bd9Sstevel@tonic-gate } 934facf4a8dSllai1 set_lock_root(); 9357c478bd9Sstevel@tonic-gate } 9367c478bd9Sstevel@tonic-gate 9377c478bd9Sstevel@tonic-gate void 9387c478bd9Sstevel@tonic-gate usage(void) 9397c478bd9Sstevel@tonic-gate { 9407c478bd9Sstevel@tonic-gate if (strcmp(prog, DEVLINKS) == 0) { 9417c478bd9Sstevel@tonic-gate err_print(DEVLINKS_USAGE); 9427c478bd9Sstevel@tonic-gate } else if (strcmp(prog, DRVCONFIG) == 0) { 9437c478bd9Sstevel@tonic-gate err_print(DRVCONFIG_USAGE); 9447c478bd9Sstevel@tonic-gate } else if ((strcmp(prog, DEVFSADM) == 0) || 9457c478bd9Sstevel@tonic-gate (strcmp(prog, DEVFSADMD) == 0)) { 9467c478bd9Sstevel@tonic-gate err_print(DEVFSADM_USAGE); 9477c478bd9Sstevel@tonic-gate } else { 9487c478bd9Sstevel@tonic-gate err_print(COMPAT_LINK_USAGE); 9497c478bd9Sstevel@tonic-gate } 9507c478bd9Sstevel@tonic-gate 9517c478bd9Sstevel@tonic-gate devfsadm_exit(1); 9527c478bd9Sstevel@tonic-gate } 9537c478bd9Sstevel@tonic-gate 9547c478bd9Sstevel@tonic-gate static void 9557c478bd9Sstevel@tonic-gate devi_tree_walk(struct dca_impl *dcip, int flags, char *ev_subclass) 9567c478bd9Sstevel@tonic-gate { 9577c478bd9Sstevel@tonic-gate char *msg, *name; 9587c478bd9Sstevel@tonic-gate struct mlist mlist = {0}; 9597c478bd9Sstevel@tonic-gate di_node_t node; 9607c478bd9Sstevel@tonic-gate 9617c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "devi_tree_walk: root=%s, minor=%s, driver=%s," 9627c478bd9Sstevel@tonic-gate " error=%d, flags=%u\n", dcip->dci_root, 9637c478bd9Sstevel@tonic-gate dcip->dci_minor ? dcip->dci_minor : "<NULL>", 9647c478bd9Sstevel@tonic-gate dcip->dci_driver ? dcip->dci_driver : "<NULL>", dcip->dci_error, 9657c478bd9Sstevel@tonic-gate dcip->dci_flags); 9667c478bd9Sstevel@tonic-gate 9677c478bd9Sstevel@tonic-gate assert(dcip->dci_root); 9687c478bd9Sstevel@tonic-gate 9697c478bd9Sstevel@tonic-gate if (dcip->dci_flags & DCA_LOAD_DRV) { 9707c478bd9Sstevel@tonic-gate node = di_init_driver(dcip->dci_driver, flags); 9717c478bd9Sstevel@tonic-gate msg = DRIVER_FAILURE; 9727c478bd9Sstevel@tonic-gate name = dcip->dci_driver; 9737c478bd9Sstevel@tonic-gate } else { 9747c478bd9Sstevel@tonic-gate node = di_init(dcip->dci_root, flags); 9757c478bd9Sstevel@tonic-gate msg = DI_INIT_FAILED; 9767c478bd9Sstevel@tonic-gate name = dcip->dci_root; 9777c478bd9Sstevel@tonic-gate } 9787c478bd9Sstevel@tonic-gate 9797c478bd9Sstevel@tonic-gate if (node == DI_NODE_NIL) { 9807c478bd9Sstevel@tonic-gate dcip->dci_error = errno; 9817c478bd9Sstevel@tonic-gate /* 9827c478bd9Sstevel@tonic-gate * Rapid hotplugging (commonly seen during USB testing), 9837c478bd9Sstevel@tonic-gate * may remove a device before the create event for it 9847c478bd9Sstevel@tonic-gate * has been processed. To prevent alarming users with 9857c478bd9Sstevel@tonic-gate * a superfluous message, we suppress error messages 9867c478bd9Sstevel@tonic-gate * for ENXIO and hotplug. 9877c478bd9Sstevel@tonic-gate */ 9887c478bd9Sstevel@tonic-gate if (!(errno == ENXIO && (dcip->dci_flags & DCA_HOT_PLUG))) 9897c478bd9Sstevel@tonic-gate err_print(msg, name, strerror(dcip->dci_error)); 9907c478bd9Sstevel@tonic-gate return; 9917c478bd9Sstevel@tonic-gate } 9927c478bd9Sstevel@tonic-gate 9937c478bd9Sstevel@tonic-gate if (dcip->dci_flags & DCA_FLUSH_PATHINST) 9947c478bd9Sstevel@tonic-gate flush_path_to_inst(); 9957c478bd9Sstevel@tonic-gate 9967c478bd9Sstevel@tonic-gate dcip->dci_arg = &mlist; 9977c478bd9Sstevel@tonic-gate 9987c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "walking device tree\n"); 9997c478bd9Sstevel@tonic-gate 10007c478bd9Sstevel@tonic-gate (void) di_walk_minor(node, NULL, DI_CHECK_ALIAS, dcip, 10017c478bd9Sstevel@tonic-gate check_minor_type); 10027c478bd9Sstevel@tonic-gate 10037c478bd9Sstevel@tonic-gate process_deferred_links(dcip, DCA_CREATE_LINK); 10047c478bd9Sstevel@tonic-gate 10057c478bd9Sstevel@tonic-gate dcip->dci_arg = NULL; 10067c478bd9Sstevel@tonic-gate 10077c478bd9Sstevel@tonic-gate /* 10087c478bd9Sstevel@tonic-gate * Finished creating devfs files and dev links. 10097c478bd9Sstevel@tonic-gate * Log sysevent and notify RCM. 10107c478bd9Sstevel@tonic-gate */ 10117c478bd9Sstevel@tonic-gate if (ev_subclass) 1012f05faa4eSjacobs build_and_enq_event(EC_DEV_ADD, ev_subclass, dcip->dci_root, 101330294554Sphitran node, dcip->dci_minor); 10147c478bd9Sstevel@tonic-gate 10157c478bd9Sstevel@tonic-gate if ((dcip->dci_flags & DCA_NOTIFY_RCM) && rcm_hdl) 10167c478bd9Sstevel@tonic-gate (void) notify_rcm(node, dcip->dci_minor); 10177c478bd9Sstevel@tonic-gate 101845916cd2Sjpk /* Add new device to device allocation database */ 101945916cd2Sjpk if (system_labeled && update_devdb) { 102045916cd2Sjpk _update_devalloc_db(&devlist, 0, DA_ADD, NULL, root_dir); 102145916cd2Sjpk update_devdb = 0; 102245916cd2Sjpk } 102345916cd2Sjpk 10247c478bd9Sstevel@tonic-gate di_fini(node); 10257c478bd9Sstevel@tonic-gate } 10267c478bd9Sstevel@tonic-gate 10277c478bd9Sstevel@tonic-gate static void 10287c478bd9Sstevel@tonic-gate process_deferred_links(struct dca_impl *dcip, int flags) 10297c478bd9Sstevel@tonic-gate { 10307c478bd9Sstevel@tonic-gate struct mlist *dep; 10317c478bd9Sstevel@tonic-gate struct minor *mp, *smp; 10327c478bd9Sstevel@tonic-gate 10337c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "processing deferred links\n"); 10347c478bd9Sstevel@tonic-gate 10357c478bd9Sstevel@tonic-gate dep = dcip->dci_arg; 10367c478bd9Sstevel@tonic-gate 10377c478bd9Sstevel@tonic-gate /* 10387c478bd9Sstevel@tonic-gate * The list head is not used during the deferred create phase 10397c478bd9Sstevel@tonic-gate */ 10407c478bd9Sstevel@tonic-gate dcip->dci_arg = NULL; 10417c478bd9Sstevel@tonic-gate 10427c478bd9Sstevel@tonic-gate assert(dep); 10437c478bd9Sstevel@tonic-gate assert((dep->head == NULL) ^ (dep->tail != NULL)); 10447c478bd9Sstevel@tonic-gate assert(flags == DCA_FREE_LIST || flags == DCA_CREATE_LINK); 10457c478bd9Sstevel@tonic-gate 10467c478bd9Sstevel@tonic-gate for (smp = NULL, mp = dep->head; mp; mp = mp->next) { 10477c478bd9Sstevel@tonic-gate if (flags == DCA_CREATE_LINK) 10487c478bd9Sstevel@tonic-gate (void) check_minor_type(mp->node, mp->minor, dcip); 10497c478bd9Sstevel@tonic-gate free(smp); 10507c478bd9Sstevel@tonic-gate smp = mp; 10517c478bd9Sstevel@tonic-gate } 10527c478bd9Sstevel@tonic-gate 10537c478bd9Sstevel@tonic-gate free(smp); 10547c478bd9Sstevel@tonic-gate } 10557c478bd9Sstevel@tonic-gate 10567c478bd9Sstevel@tonic-gate /* 10577c478bd9Sstevel@tonic-gate * Called in non-daemon mode to take a snap shot of the devinfo tree. 10587c478bd9Sstevel@tonic-gate * Then it calls the appropriate functions to build /devices and /dev. 10597c478bd9Sstevel@tonic-gate * It also flushes path_to_inst. 106098a9beefShtk * Except in the devfsadm -i (single driver case), the flags used by devfsadm 106198a9beefShtk * needs to match DI_CACHE_SNAPSHOT_FLAGS. That will make DINFOCACHE snapshot 106298a9beefShtk * updated. 10637c478bd9Sstevel@tonic-gate */ 10647c478bd9Sstevel@tonic-gate void 10657c478bd9Sstevel@tonic-gate process_devinfo_tree() 10667c478bd9Sstevel@tonic-gate { 106798a9beefShtk uint_t flags; 10687c478bd9Sstevel@tonic-gate struct dca_impl dci; 10697c478bd9Sstevel@tonic-gate char name[MAXNAMELEN]; 10707c478bd9Sstevel@tonic-gate char *fcn = "process_devinfo_tree: "; 10717c478bd9Sstevel@tonic-gate 10727c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "%senter\n", fcn); 10737c478bd9Sstevel@tonic-gate 10747c478bd9Sstevel@tonic-gate dca_impl_init("/", NULL, &dci); 10757c478bd9Sstevel@tonic-gate 10767c478bd9Sstevel@tonic-gate lock_dev(); 10777c478bd9Sstevel@tonic-gate 10787c478bd9Sstevel@tonic-gate /* 10797c478bd9Sstevel@tonic-gate * Update kernel driver.conf cache when devfsadm/drvconfig 10807c478bd9Sstevel@tonic-gate * is invoked to build /devices and /dev. 10817c478bd9Sstevel@tonic-gate */ 10827c478bd9Sstevel@tonic-gate if (load_attach_drv == TRUE) 10837c478bd9Sstevel@tonic-gate update_drvconf((major_t)-1); 10847c478bd9Sstevel@tonic-gate 10857c478bd9Sstevel@tonic-gate if (single_drv == TRUE) { 10867c478bd9Sstevel@tonic-gate /* 10877c478bd9Sstevel@tonic-gate * load a single driver, but walk the entire devinfo tree 10887c478bd9Sstevel@tonic-gate */ 10897c478bd9Sstevel@tonic-gate if (load_attach_drv == FALSE) 10907c478bd9Sstevel@tonic-gate err_print(DRV_LOAD_REQD); 10917c478bd9Sstevel@tonic-gate 10927c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "%sattaching driver (%s)\n", fcn, driver); 10937c478bd9Sstevel@tonic-gate 10947c478bd9Sstevel@tonic-gate dci.dci_flags |= DCA_LOAD_DRV; 10957c478bd9Sstevel@tonic-gate (void) snprintf(name, sizeof (name), "%s", driver); 10967c478bd9Sstevel@tonic-gate dci.dci_driver = name; 109798a9beefShtk flags = DINFOCPYALL | DINFOPATH; 10987c478bd9Sstevel@tonic-gate 10997c478bd9Sstevel@tonic-gate } else if (load_attach_drv == TRUE) { 11007c478bd9Sstevel@tonic-gate /* 110145916cd2Sjpk * Load and attach all drivers, then walk the entire tree. 11027c478bd9Sstevel@tonic-gate * If the cache flag is set, use DINFOCACHE to get cached 11037c478bd9Sstevel@tonic-gate * data. 11047c478bd9Sstevel@tonic-gate */ 11057c478bd9Sstevel@tonic-gate if (use_snapshot_cache == TRUE) { 11067c478bd9Sstevel@tonic-gate flags = DINFOCACHE; 11077c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "%susing snapshot cache\n", fcn); 11087c478bd9Sstevel@tonic-gate } else { 11097c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "%sattaching all drivers\n", fcn); 111098a9beefShtk flags = DI_CACHE_SNAPSHOT_FLAGS; 11113c34adc5Sramat if (cleanup) { 11123c34adc5Sramat /* 11133c34adc5Sramat * remove dangling entries from /etc/devices 11143c34adc5Sramat * files. 11153c34adc5Sramat */ 11163c34adc5Sramat flags |= DINFOCLEANUP; 11173c34adc5Sramat } 11187c478bd9Sstevel@tonic-gate } 1119*34087db8Shtk } else { 1120*34087db8Shtk /* 1121*34087db8Shtk * For devlinks, disks, ports, tapes and devfsadm -n, 1122*34087db8Shtk * just need to take a snapshot with active devices. 1123*34087db8Shtk */ 1124*34087db8Shtk vprint(CHATTY_MID, "%staking snapshot of active devices\n", 1125*34087db8Shtk fcn); 1126*34087db8Shtk flags = DINFOCPYALL; 11277c478bd9Sstevel@tonic-gate } 11287c478bd9Sstevel@tonic-gate 11297c478bd9Sstevel@tonic-gate if (((load_attach_drv == TRUE) || (single_drv == TRUE)) && 11307c478bd9Sstevel@tonic-gate (build_devices == TRUE)) { 11317c478bd9Sstevel@tonic-gate dci.dci_flags |= DCA_FLUSH_PATHINST; 11327c478bd9Sstevel@tonic-gate } 11337c478bd9Sstevel@tonic-gate 11347c478bd9Sstevel@tonic-gate /* handle pre-cleanup operations desired by the modules. */ 11357c478bd9Sstevel@tonic-gate pre_and_post_cleanup(RM_PRE); 11367c478bd9Sstevel@tonic-gate 11377c478bd9Sstevel@tonic-gate devi_tree_walk(&dci, flags, NULL); 11387c478bd9Sstevel@tonic-gate 11397c478bd9Sstevel@tonic-gate if (dci.dci_error) { 11407c478bd9Sstevel@tonic-gate devfsadm_exit(1); 11417c478bd9Sstevel@tonic-gate } 11427c478bd9Sstevel@tonic-gate 11437c478bd9Sstevel@tonic-gate /* handle post-cleanup operations desired by the modules. */ 11447c478bd9Sstevel@tonic-gate pre_and_post_cleanup(RM_POST); 11457c478bd9Sstevel@tonic-gate 11467c478bd9Sstevel@tonic-gate unlock_dev(SYNC_STATE); 11477c478bd9Sstevel@tonic-gate } 11487c478bd9Sstevel@tonic-gate 11497c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 11507c478bd9Sstevel@tonic-gate static void 11517c478bd9Sstevel@tonic-gate print_cache_signal(int signo) 11527c478bd9Sstevel@tonic-gate { 11537c478bd9Sstevel@tonic-gate if (signal(SIGUSR1, print_cache_signal) == SIG_ERR) { 11547c478bd9Sstevel@tonic-gate err_print("signal SIGUSR1 failed: %s\n", strerror(errno)); 11557c478bd9Sstevel@tonic-gate devfsadm_exit(1); 11567c478bd9Sstevel@tonic-gate } 11577c478bd9Sstevel@tonic-gate } 11587c478bd9Sstevel@tonic-gate 1159facf4a8dSllai1 static void 1160facf4a8dSllai1 revoke_lookup_door(void) 1161facf4a8dSllai1 { 1162facf4a8dSllai1 if (lookup_door_fd != -1) { 1163facf4a8dSllai1 if (door_revoke(lookup_door_fd) == -1) { 1164facf4a8dSllai1 err_print("door_revoke of %s failed - %s\n", 1165facf4a8dSllai1 lookup_door_path, strerror(errno)); 1166facf4a8dSllai1 } 1167facf4a8dSllai1 } 1168facf4a8dSllai1 } 1169facf4a8dSllai1 1170facf4a8dSllai1 /*ARGSUSED*/ 1171facf4a8dSllai1 static void 1172facf4a8dSllai1 catch_exit(int signo) 1173facf4a8dSllai1 { 1174facf4a8dSllai1 revoke_lookup_door(); 1175facf4a8dSllai1 } 1176facf4a8dSllai1 11777c478bd9Sstevel@tonic-gate /* 11787c478bd9Sstevel@tonic-gate * Register with eventd for messages. Create doors for synchronous 11797c478bd9Sstevel@tonic-gate * link creation. 11807c478bd9Sstevel@tonic-gate */ 11817c478bd9Sstevel@tonic-gate static void 11827c478bd9Sstevel@tonic-gate daemon_update(void) 11837c478bd9Sstevel@tonic-gate { 11847c478bd9Sstevel@tonic-gate int fd; 11857c478bd9Sstevel@tonic-gate char *fcn = "daemon_update: "; 11867c478bd9Sstevel@tonic-gate char door_file[MAXPATHLEN]; 11877c478bd9Sstevel@tonic-gate const char *subclass_list; 11887c478bd9Sstevel@tonic-gate sysevent_handle_t *sysevent_hp; 11897c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "%senter\n", fcn); 11907c478bd9Sstevel@tonic-gate 11917c478bd9Sstevel@tonic-gate if (signal(SIGUSR1, print_cache_signal) == SIG_ERR) { 11927c478bd9Sstevel@tonic-gate err_print("signal SIGUSR1 failed: %s\n", strerror(errno)); 11937c478bd9Sstevel@tonic-gate devfsadm_exit(1); 11947c478bd9Sstevel@tonic-gate } 1195facf4a8dSllai1 if (signal(SIGTERM, catch_exit) == SIG_ERR) { 1196facf4a8dSllai1 err_print("signal SIGTERM failed: %s\n", strerror(errno)); 1197facf4a8dSllai1 devfsadm_exit(1); 1198facf4a8dSllai1 } 11997c478bd9Sstevel@tonic-gate 12007c478bd9Sstevel@tonic-gate if (snprintf(door_file, sizeof (door_file), 1201facf4a8dSllai1 "%s%s", attr_root ? attr_root : root_dir, DEVFSADM_SERVICE_DOOR) 1202facf4a8dSllai1 >= sizeof (door_file)) { 12037c478bd9Sstevel@tonic-gate err_print("update_daemon failed to open sysevent service " 12047c478bd9Sstevel@tonic-gate "door\n"); 12057c478bd9Sstevel@tonic-gate devfsadm_exit(1); 12067c478bd9Sstevel@tonic-gate } 12077c478bd9Sstevel@tonic-gate if ((sysevent_hp = sysevent_open_channel_alt( 12087c478bd9Sstevel@tonic-gate door_file)) == NULL) { 12097c478bd9Sstevel@tonic-gate err_print(CANT_CREATE_DOOR, 12107c478bd9Sstevel@tonic-gate door_file, strerror(errno)); 12117c478bd9Sstevel@tonic-gate devfsadm_exit(1); 12127c478bd9Sstevel@tonic-gate } 12137c478bd9Sstevel@tonic-gate if (sysevent_bind_subscriber(sysevent_hp, event_handler) != 0) { 12147c478bd9Sstevel@tonic-gate err_print(CANT_CREATE_DOOR, 12157c478bd9Sstevel@tonic-gate door_file, strerror(errno)); 12167c478bd9Sstevel@tonic-gate (void) sysevent_close_channel(sysevent_hp); 12177c478bd9Sstevel@tonic-gate devfsadm_exit(1); 12187c478bd9Sstevel@tonic-gate } 12197c478bd9Sstevel@tonic-gate subclass_list = EC_SUB_ALL; 12207c478bd9Sstevel@tonic-gate if (sysevent_register_event(sysevent_hp, EC_ALL, &subclass_list, 1) 12217c478bd9Sstevel@tonic-gate != 0) { 12227c478bd9Sstevel@tonic-gate err_print(CANT_CREATE_DOOR, 12237c478bd9Sstevel@tonic-gate door_file, strerror(errno)); 12247c478bd9Sstevel@tonic-gate (void) sysevent_unbind_subscriber(sysevent_hp); 12257c478bd9Sstevel@tonic-gate (void) sysevent_close_channel(sysevent_hp); 12267c478bd9Sstevel@tonic-gate devfsadm_exit(1); 12277c478bd9Sstevel@tonic-gate } 1228facf4a8dSllai1 if (snprintf(door_file, sizeof (door_file), "%s/%s", 1229facf4a8dSllai1 etc_dev_dir, DEVFSADM_SYNCH_DOOR) >= sizeof (door_file)) { 1230facf4a8dSllai1 err_print(CANT_CREATE_DOOR, DEVFSADM_SYNCH_DOOR, 12317c478bd9Sstevel@tonic-gate strerror(ENAMETOOLONG)); 12327c478bd9Sstevel@tonic-gate devfsadm_exit(1); 12337c478bd9Sstevel@tonic-gate } 12347c478bd9Sstevel@tonic-gate 12357c478bd9Sstevel@tonic-gate (void) s_unlink(door_file); 12367c478bd9Sstevel@tonic-gate if ((fd = open(door_file, O_RDWR | O_CREAT, SYNCH_DOOR_PERMS)) == -1) { 12377c478bd9Sstevel@tonic-gate err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); 12387c478bd9Sstevel@tonic-gate devfsadm_exit(1); 12397c478bd9Sstevel@tonic-gate } 12407c478bd9Sstevel@tonic-gate (void) close(fd); 12417c478bd9Sstevel@tonic-gate 12427c478bd9Sstevel@tonic-gate if ((fd = door_create(sync_handler, NULL, 12437c478bd9Sstevel@tonic-gate DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) { 12447c478bd9Sstevel@tonic-gate err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); 12457c478bd9Sstevel@tonic-gate (void) s_unlink(door_file); 12467c478bd9Sstevel@tonic-gate devfsadm_exit(1); 12477c478bd9Sstevel@tonic-gate } 12487c478bd9Sstevel@tonic-gate 12497c478bd9Sstevel@tonic-gate if (fattach(fd, door_file) == -1) { 12507c478bd9Sstevel@tonic-gate err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); 12517c478bd9Sstevel@tonic-gate (void) s_unlink(door_file); 12527c478bd9Sstevel@tonic-gate devfsadm_exit(1); 12537c478bd9Sstevel@tonic-gate } 12547c478bd9Sstevel@tonic-gate 12557c478bd9Sstevel@tonic-gate /* 1256facf4a8dSllai1 * devname_lookup_door 12577c478bd9Sstevel@tonic-gate */ 1258facf4a8dSllai1 if (snprintf(door_file, sizeof (door_file), "%s/%s", 1259facf4a8dSllai1 etc_dev_dir, DEVNAME_LOOKUP_DOOR) >= sizeof (door_file)) { 1260facf4a8dSllai1 err_print(CANT_CREATE_DOOR, DEVNAME_LOOKUP_DOOR, 1261facf4a8dSllai1 strerror(ENAMETOOLONG)); 1262facf4a8dSllai1 devfsadm_exit(1); 12637c478bd9Sstevel@tonic-gate } 12647c478bd9Sstevel@tonic-gate 1265facf4a8dSllai1 (void) s_unlink(door_file); 1266facf4a8dSllai1 if ((fd = open(door_file, O_RDWR | O_CREAT, S_IRUSR|S_IWUSR)) == -1) { 1267facf4a8dSllai1 err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); 1268facf4a8dSllai1 devfsadm_exit(1); 1269facf4a8dSllai1 } 1270facf4a8dSllai1 (void) close(fd); 1271facf4a8dSllai1 1272facf4a8dSllai1 if ((fd = door_create(devname_lookup_handler, NULL, 1273facf4a8dSllai1 DOOR_REFUSE_DESC)) == -1) { 1274facf4a8dSllai1 err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); 1275facf4a8dSllai1 (void) s_unlink(door_file); 1276facf4a8dSllai1 devfsadm_exit(1); 1277facf4a8dSllai1 } 1278facf4a8dSllai1 1279facf4a8dSllai1 (void) fdetach(door_file); 1280facf4a8dSllai1 lookup_door_path = s_strdup(door_file); 1281facf4a8dSllai1 retry: 1282facf4a8dSllai1 if (fattach(fd, door_file) == -1) { 1283facf4a8dSllai1 if (errno == EBUSY) 1284facf4a8dSllai1 goto retry; 1285facf4a8dSllai1 err_print(CANT_CREATE_DOOR, door_file, strerror(errno)); 1286facf4a8dSllai1 (void) s_unlink(door_file); 1287facf4a8dSllai1 devfsadm_exit(1); 1288facf4a8dSllai1 } 1289facf4a8dSllai1 lookup_door_fd = fd; 1290facf4a8dSllai1 1291facf4a8dSllai1 /* pass down the door name to kernel for door_ki_open */ 1292facf4a8dSllai1 if (devname_kcall(MODDEVNAME_LOOKUPDOOR, (void *)door_file) != 0) 1293facf4a8dSllai1 err_print(DEVNAME_CONTACT_FAILED, strerror(errno)); 1294facf4a8dSllai1 else 1295facf4a8dSllai1 devname_setup_nsmaps(); 1296facf4a8dSllai1 12977c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "%spausing\n", fcn); 12987c478bd9Sstevel@tonic-gate for (;;) { 12997c478bd9Sstevel@tonic-gate (void) pause(); 13007c478bd9Sstevel@tonic-gate } 13017c478bd9Sstevel@tonic-gate } 13027c478bd9Sstevel@tonic-gate 13037c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 13047c478bd9Sstevel@tonic-gate static void 13057c478bd9Sstevel@tonic-gate sync_handler(void *cookie, char *ap, size_t asize, 13067c478bd9Sstevel@tonic-gate door_desc_t *dp, uint_t ndesc) 13077c478bd9Sstevel@tonic-gate { 13087c478bd9Sstevel@tonic-gate door_cred_t dcred; 13097c478bd9Sstevel@tonic-gate struct dca_off *dcp, rdca; 13107c478bd9Sstevel@tonic-gate struct dca_impl dci; 13117c478bd9Sstevel@tonic-gate 13127c478bd9Sstevel@tonic-gate /* 13137c478bd9Sstevel@tonic-gate * Must be root to make this call 13147c478bd9Sstevel@tonic-gate * If caller is not root, don't touch its data. 13157c478bd9Sstevel@tonic-gate */ 13167c478bd9Sstevel@tonic-gate if (door_cred(&dcred) != 0 || dcred.dc_euid != 0) { 13177c478bd9Sstevel@tonic-gate dcp = ⤷ 13187c478bd9Sstevel@tonic-gate dcp->dca_error = EPERM; 13197c478bd9Sstevel@tonic-gate goto out; 13207c478bd9Sstevel@tonic-gate } 13217c478bd9Sstevel@tonic-gate 13227c478bd9Sstevel@tonic-gate assert(ap); 13237c478bd9Sstevel@tonic-gate assert(asize == sizeof (*dcp)); 13247c478bd9Sstevel@tonic-gate 13257c478bd9Sstevel@tonic-gate dcp = (void *)ap; 13267c478bd9Sstevel@tonic-gate 13277c478bd9Sstevel@tonic-gate /* 13287c478bd9Sstevel@tonic-gate * Root is always present and is the first component of "name" member 13297c478bd9Sstevel@tonic-gate */ 13307c478bd9Sstevel@tonic-gate assert(dcp->dca_root == 0); 13317c478bd9Sstevel@tonic-gate 13327c478bd9Sstevel@tonic-gate /* 13337c478bd9Sstevel@tonic-gate * The structure passed in by the door_client uses offsets 13347c478bd9Sstevel@tonic-gate * instead of pointers to work across address space boundaries. 13357c478bd9Sstevel@tonic-gate * Now copy the data into a structure (dca_impl) which uses 13367c478bd9Sstevel@tonic-gate * pointers. 13377c478bd9Sstevel@tonic-gate */ 13387c478bd9Sstevel@tonic-gate dci.dci_root = &dcp->dca_name[dcp->dca_root]; 13397c478bd9Sstevel@tonic-gate dci.dci_minor = dcp->dca_minor ? &dcp->dca_name[dcp->dca_minor] : NULL; 13407c478bd9Sstevel@tonic-gate dci.dci_driver = 13417c478bd9Sstevel@tonic-gate dcp->dca_driver ? &dcp->dca_name[dcp->dca_driver] : NULL; 13427c478bd9Sstevel@tonic-gate dci.dci_error = 0; 13437c478bd9Sstevel@tonic-gate dci.dci_flags = dcp->dca_flags | (dci.dci_driver ? DCA_LOAD_DRV : 0); 13447c478bd9Sstevel@tonic-gate dci.dci_arg = NULL; 13457c478bd9Sstevel@tonic-gate 13467c478bd9Sstevel@tonic-gate lock_dev(); 13477c478bd9Sstevel@tonic-gate devi_tree_walk(&dci, DINFOCPYALL, NULL); 13487c478bd9Sstevel@tonic-gate dcp->dca_error = dci.dci_error; 13497c478bd9Sstevel@tonic-gate 1350ff2aee48Scth if (dcp->dca_flags & DCA_DEVLINK_SYNC) 1351ff2aee48Scth unlock_dev(SYNC_STATE); 1352ff2aee48Scth else 1353ff2aee48Scth unlock_dev(CACHE_STATE); 13547c478bd9Sstevel@tonic-gate 1355ff2aee48Scth out: (void) door_return((char *)dcp, sizeof (*dcp), NULL, 0); 13567c478bd9Sstevel@tonic-gate } 13577c478bd9Sstevel@tonic-gate 13587c478bd9Sstevel@tonic-gate static void 13597c478bd9Sstevel@tonic-gate lock_dev(void) 13607c478bd9Sstevel@tonic-gate { 13617c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "lock_dev(): entered\n"); 13627c478bd9Sstevel@tonic-gate 13637c478bd9Sstevel@tonic-gate if (build_dev == FALSE) 13647c478bd9Sstevel@tonic-gate return; 13657c478bd9Sstevel@tonic-gate 13667c478bd9Sstevel@tonic-gate /* lockout other threads from /dev */ 13677c478bd9Sstevel@tonic-gate while (sema_wait(&dev_sema) != 0); 13687c478bd9Sstevel@tonic-gate 13697c478bd9Sstevel@tonic-gate /* 13707c478bd9Sstevel@tonic-gate * Lock out other devfsadm processes from /dev. 13717c478bd9Sstevel@tonic-gate * If this wasn't the last process to run, 13727c478bd9Sstevel@tonic-gate * clear caches 13737c478bd9Sstevel@tonic-gate */ 13747c478bd9Sstevel@tonic-gate if (enter_dev_lock() != getpid()) { 13757c478bd9Sstevel@tonic-gate invalidate_enumerate_cache(); 13767c478bd9Sstevel@tonic-gate rm_all_links_from_cache(); 13777c478bd9Sstevel@tonic-gate (void) di_devlink_close(&devlink_cache, DI_LINK_ERROR); 1378f05faa4eSjacobs 1379f05faa4eSjacobs /* send any sysevents that were queued up. */ 1380f05faa4eSjacobs process_syseventq(); 13817c478bd9Sstevel@tonic-gate } 13827c478bd9Sstevel@tonic-gate 13837c478bd9Sstevel@tonic-gate /* 13847c478bd9Sstevel@tonic-gate * (re)load the reverse links database if not 13857c478bd9Sstevel@tonic-gate * already cached. 13867c478bd9Sstevel@tonic-gate */ 13877c478bd9Sstevel@tonic-gate if (devlink_cache == NULL) 13887c478bd9Sstevel@tonic-gate devlink_cache = di_devlink_open(root_dir, 0); 13897c478bd9Sstevel@tonic-gate 13907c478bd9Sstevel@tonic-gate /* 13917c478bd9Sstevel@tonic-gate * If modules were unloaded, reload them. Also use module status 13927c478bd9Sstevel@tonic-gate * as an indication that we should check to see if other binding 13937c478bd9Sstevel@tonic-gate * files need to be reloaded. 13947c478bd9Sstevel@tonic-gate */ 13957c478bd9Sstevel@tonic-gate if (module_head == NULL) { 13967c478bd9Sstevel@tonic-gate load_modules(); 13977c478bd9Sstevel@tonic-gate read_minor_perm_file(); 13987c478bd9Sstevel@tonic-gate read_driver_aliases_file(); 13997c478bd9Sstevel@tonic-gate read_devlinktab_file(); 14007c478bd9Sstevel@tonic-gate read_logindevperm_file(); 14018d483882Smlf read_enumerate_file(); 14027c478bd9Sstevel@tonic-gate } 14037c478bd9Sstevel@tonic-gate 14047c478bd9Sstevel@tonic-gate if (module_head != NULL) 14057c478bd9Sstevel@tonic-gate return; 14067c478bd9Sstevel@tonic-gate 14077c478bd9Sstevel@tonic-gate if (strcmp(prog, DEVLINKS) == 0) { 14087c478bd9Sstevel@tonic-gate if (devlinktab_list == NULL) { 14097c478bd9Sstevel@tonic-gate err_print(NO_LINKTAB, devlinktab_file); 14107c478bd9Sstevel@tonic-gate err_print(NO_MODULES, module_dirs); 14117c478bd9Sstevel@tonic-gate err_print(ABORTING); 14127c478bd9Sstevel@tonic-gate devfsadm_exit(1); 14137c478bd9Sstevel@tonic-gate } 14147c478bd9Sstevel@tonic-gate } else { 14157c478bd9Sstevel@tonic-gate err_print(NO_MODULES, module_dirs); 14167c478bd9Sstevel@tonic-gate if (strcmp(prog, DEVFSADM) == 0) { 14177c478bd9Sstevel@tonic-gate err_print(MODIFY_PATH); 14187c478bd9Sstevel@tonic-gate } 14197c478bd9Sstevel@tonic-gate } 14207c478bd9Sstevel@tonic-gate } 14217c478bd9Sstevel@tonic-gate 1422ff2aee48Scth /* 1423ff2aee48Scth * Unlock the device. If we are processing a CACHE_STATE call, we signal a 1424ff2aee48Scth * minor_fini_thread delayed SYNC_STATE at the end of the call. If we are 1425ff2aee48Scth * processing a SYNC_STATE call, we cancel any minor_fini_thread SYNC_STATE 1426ff2aee48Scth * at both the start and end of the call since we will be doing the SYNC_STATE. 1427ff2aee48Scth */ 14287c478bd9Sstevel@tonic-gate static void 14297c478bd9Sstevel@tonic-gate unlock_dev(int flag) 14307c478bd9Sstevel@tonic-gate { 1431ff2aee48Scth assert(flag == SYNC_STATE || flag == CACHE_STATE); 1432ff2aee48Scth 14337c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "unlock_dev(): entered\n"); 14347c478bd9Sstevel@tonic-gate 1435ff2aee48Scth /* If we are starting a SYNC_STATE, cancel minor_fini_thread SYNC */ 1436ff2aee48Scth if (flag == SYNC_STATE) { 1437ff2aee48Scth (void) mutex_lock(&minor_fini_mutex); 1438ff2aee48Scth minor_fini_canceled = TRUE; 1439ff2aee48Scth minor_fini_delayed = FALSE; 1440ff2aee48Scth (void) mutex_unlock(&minor_fini_mutex); 1441ff2aee48Scth } 1442ff2aee48Scth 14437c478bd9Sstevel@tonic-gate if (build_dev == FALSE) 14447c478bd9Sstevel@tonic-gate return; 14457c478bd9Sstevel@tonic-gate 1446568e756aSvikram if (devlink_cache == NULL) { 1447568e756aSvikram err_print(NO_DEVLINK_CACHE); 1448568e756aSvikram } 14497c478bd9Sstevel@tonic-gate assert(devlink_cache); 14507c478bd9Sstevel@tonic-gate 14517c478bd9Sstevel@tonic-gate if (flag == SYNC_STATE) { 14527c478bd9Sstevel@tonic-gate unload_modules(); 14537c478bd9Sstevel@tonic-gate if (update_database) 14547c478bd9Sstevel@tonic-gate (void) di_devlink_update(devlink_cache); 14557c478bd9Sstevel@tonic-gate (void) di_devlink_close(&devlink_cache, 0); 1456f05faa4eSjacobs 1457f05faa4eSjacobs /* 1458f05faa4eSjacobs * now that the devlinks db cache has been flushed, it is safe 1459f05faa4eSjacobs * to send any sysevents that were queued up. 1460f05faa4eSjacobs */ 1461f05faa4eSjacobs process_syseventq(); 14627c478bd9Sstevel@tonic-gate } 14637c478bd9Sstevel@tonic-gate 14647c478bd9Sstevel@tonic-gate exit_dev_lock(); 14657c478bd9Sstevel@tonic-gate 1466ff2aee48Scth (void) mutex_lock(&minor_fini_mutex); 1467ff2aee48Scth if (flag == SYNC_STATE) { 1468ff2aee48Scth /* We did a SYNC_STATE, cancel minor_fini_thread SYNC */ 1469ff2aee48Scth minor_fini_canceled = TRUE; 1470ff2aee48Scth minor_fini_delayed = FALSE; 1471ff2aee48Scth } else { 1472ff2aee48Scth /* We did a CACHE_STATE, start delayed minor_fini_thread SYNC */ 1473ff2aee48Scth minor_fini_canceled = FALSE; 1474ff2aee48Scth minor_fini_delayed = TRUE; 1475ff2aee48Scth (void) cond_signal(&minor_fini_cv); 1476ff2aee48Scth } 1477ff2aee48Scth (void) mutex_unlock(&minor_fini_mutex); 1478ff2aee48Scth 14797c478bd9Sstevel@tonic-gate (void) sema_post(&dev_sema); 14807c478bd9Sstevel@tonic-gate } 14817c478bd9Sstevel@tonic-gate 14827c478bd9Sstevel@tonic-gate /* 14837c478bd9Sstevel@tonic-gate * Check that if -r is set, it is not any part of a zone--- that is, that 14847c478bd9Sstevel@tonic-gate * the zonepath is not a substring of the root path. 14857c478bd9Sstevel@tonic-gate */ 14867c478bd9Sstevel@tonic-gate static int 14877c478bd9Sstevel@tonic-gate zone_pathcheck(char *checkpath) 14887c478bd9Sstevel@tonic-gate { 14897c478bd9Sstevel@tonic-gate void *dlhdl = NULL; 14907c478bd9Sstevel@tonic-gate char *name; 14917c478bd9Sstevel@tonic-gate char root[MAXPATHLEN]; /* resolved devfsadm root path */ 14927c478bd9Sstevel@tonic-gate char zroot[MAXPATHLEN]; /* zone root path */ 14937c478bd9Sstevel@tonic-gate char rzroot[MAXPATHLEN]; /* resolved zone root path */ 14947c478bd9Sstevel@tonic-gate char tmp[MAXPATHLEN]; 14957c478bd9Sstevel@tonic-gate FILE *cookie; 14967c478bd9Sstevel@tonic-gate int err = DEVFSADM_SUCCESS; 14977c478bd9Sstevel@tonic-gate 14987c478bd9Sstevel@tonic-gate if (checkpath[0] == '\0') 14997c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 15007c478bd9Sstevel@tonic-gate 15017c478bd9Sstevel@tonic-gate /* 15027c478bd9Sstevel@tonic-gate * Check if zones is available on this system. 15037c478bd9Sstevel@tonic-gate */ 15047c478bd9Sstevel@tonic-gate if ((dlhdl = dlopen(LIBZONECFG_PATH, RTLD_LAZY)) == NULL) { 15057c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 15067c478bd9Sstevel@tonic-gate } 15077c478bd9Sstevel@tonic-gate 15087c478bd9Sstevel@tonic-gate bzero(root, sizeof (root)); 15097c478bd9Sstevel@tonic-gate if (resolvepath(checkpath, root, sizeof (root) - 1) == -1) { 15107c478bd9Sstevel@tonic-gate /* 1511facf4a8dSllai1 * In this case the user has done "devfsadm -r" on some path 15127c478bd9Sstevel@tonic-gate * which does not yet exist, or we got some other misc. error. 15137c478bd9Sstevel@tonic-gate * We punt and don't resolve the path in this case. 15147c478bd9Sstevel@tonic-gate */ 15157c478bd9Sstevel@tonic-gate (void) strlcpy(root, checkpath, sizeof (root)); 15167c478bd9Sstevel@tonic-gate } 15177c478bd9Sstevel@tonic-gate 15187c478bd9Sstevel@tonic-gate if (strlen(root) > 0 && (root[strlen(root) - 1] != '/')) { 15197c478bd9Sstevel@tonic-gate (void) snprintf(tmp, sizeof (tmp), "%s/", root); 15207c478bd9Sstevel@tonic-gate (void) strlcpy(root, tmp, sizeof (root)); 15217c478bd9Sstevel@tonic-gate } 15227c478bd9Sstevel@tonic-gate 15237c478bd9Sstevel@tonic-gate cookie = setzoneent(); 15247c478bd9Sstevel@tonic-gate while ((name = getzoneent(cookie)) != NULL) { 15257c478bd9Sstevel@tonic-gate /* Skip the global zone */ 15267c478bd9Sstevel@tonic-gate if (strcmp(name, GLOBAL_ZONENAME) == 0) { 15277c478bd9Sstevel@tonic-gate free(name); 15287c478bd9Sstevel@tonic-gate continue; 15297c478bd9Sstevel@tonic-gate } 15307c478bd9Sstevel@tonic-gate 15317c478bd9Sstevel@tonic-gate if (zone_get_zonepath(name, zroot, sizeof (zroot)) != Z_OK) { 15327c478bd9Sstevel@tonic-gate free(name); 15337c478bd9Sstevel@tonic-gate continue; 15347c478bd9Sstevel@tonic-gate } 15357c478bd9Sstevel@tonic-gate 15367c478bd9Sstevel@tonic-gate bzero(rzroot, sizeof (rzroot)); 15377c478bd9Sstevel@tonic-gate if (resolvepath(zroot, rzroot, sizeof (rzroot) - 1) == -1) { 15387c478bd9Sstevel@tonic-gate /* 15397c478bd9Sstevel@tonic-gate * Zone path doesn't exist, or other misc error, 15407c478bd9Sstevel@tonic-gate * so we try using the non-resolved pathname. 15417c478bd9Sstevel@tonic-gate */ 15427c478bd9Sstevel@tonic-gate (void) strlcpy(rzroot, zroot, sizeof (rzroot)); 15437c478bd9Sstevel@tonic-gate } 15447c478bd9Sstevel@tonic-gate if (strlen(rzroot) > 0 && (rzroot[strlen(rzroot) - 1] != '/')) { 15457c478bd9Sstevel@tonic-gate (void) snprintf(tmp, sizeof (tmp), "%s/", rzroot); 15467c478bd9Sstevel@tonic-gate (void) strlcpy(rzroot, tmp, sizeof (rzroot)); 15477c478bd9Sstevel@tonic-gate } 15487c478bd9Sstevel@tonic-gate 15497c478bd9Sstevel@tonic-gate /* 15507c478bd9Sstevel@tonic-gate * Finally, the comparison. If the zone root path is a 15517c478bd9Sstevel@tonic-gate * leading substring of the root path, fail. 15527c478bd9Sstevel@tonic-gate */ 15537c478bd9Sstevel@tonic-gate if (strncmp(rzroot, root, strlen(rzroot)) == 0) { 15547c478bd9Sstevel@tonic-gate err_print(ZONE_PATHCHECK, root, name); 15557c478bd9Sstevel@tonic-gate err = DEVFSADM_FAILURE; 15567c478bd9Sstevel@tonic-gate free(name); 15577c478bd9Sstevel@tonic-gate break; 15587c478bd9Sstevel@tonic-gate } 15597c478bd9Sstevel@tonic-gate free(name); 15607c478bd9Sstevel@tonic-gate } 15617c478bd9Sstevel@tonic-gate endzoneent(cookie); 15627c478bd9Sstevel@tonic-gate (void) dlclose(dlhdl); 15637c478bd9Sstevel@tonic-gate return (err); 15647c478bd9Sstevel@tonic-gate } 15657c478bd9Sstevel@tonic-gate 15667c478bd9Sstevel@tonic-gate /* 15677c478bd9Sstevel@tonic-gate * Called by the daemon when it receives an event from the devfsadm SLM 15687c478bd9Sstevel@tonic-gate * to syseventd. 15697c478bd9Sstevel@tonic-gate * 15707c478bd9Sstevel@tonic-gate * The devfsadm SLM uses a private event channel for communication to 15717c478bd9Sstevel@tonic-gate * devfsadmd set-up via private libsysevent interfaces. This handler is 15727c478bd9Sstevel@tonic-gate * used to bind to the devfsadmd channel for event delivery. 15737c478bd9Sstevel@tonic-gate * The devfsadmd SLM insures single calls to this routine as well as 15747c478bd9Sstevel@tonic-gate * synchronized event delivery. 15757c478bd9Sstevel@tonic-gate * 15767c478bd9Sstevel@tonic-gate */ 15777c478bd9Sstevel@tonic-gate static void 15787c478bd9Sstevel@tonic-gate event_handler(sysevent_t *ev) 15797c478bd9Sstevel@tonic-gate { 15807c478bd9Sstevel@tonic-gate char *path; 15817c478bd9Sstevel@tonic-gate char *minor; 15827c478bd9Sstevel@tonic-gate char *subclass; 15837c478bd9Sstevel@tonic-gate char *dev_ev_subclass; 15847c478bd9Sstevel@tonic-gate char *driver_name; 15857c478bd9Sstevel@tonic-gate nvlist_t *attr_list = NULL; 15867c478bd9Sstevel@tonic-gate int err = 0; 15877c478bd9Sstevel@tonic-gate int instance; 15887c478bd9Sstevel@tonic-gate int branch_event = 0; 15897c478bd9Sstevel@tonic-gate 15907c478bd9Sstevel@tonic-gate subclass = sysevent_get_subclass_name(ev); 15917c478bd9Sstevel@tonic-gate vprint(EVENT_MID, "event_handler: %s id:0X%llx\n", 15927c478bd9Sstevel@tonic-gate subclass, sysevent_get_seq(ev)); 15937c478bd9Sstevel@tonic-gate 1594facf4a8dSllai1 if (strcmp(subclass, ESC_DEVFS_START) == 0) { 1595facf4a8dSllai1 return; 1596facf4a8dSllai1 } 1597facf4a8dSllai1 15987c478bd9Sstevel@tonic-gate /* Check if event is an instance modification */ 15997c478bd9Sstevel@tonic-gate if (strcmp(subclass, ESC_DEVFS_INSTANCE_MOD) == 0) { 16007c478bd9Sstevel@tonic-gate devfs_instance_mod(); 16017c478bd9Sstevel@tonic-gate return; 16027c478bd9Sstevel@tonic-gate } 16037c478bd9Sstevel@tonic-gate if (sysevent_get_attr_list(ev, &attr_list) != 0) { 16047c478bd9Sstevel@tonic-gate vprint(EVENT_MID, "event_handler: can not get attr list\n"); 16057c478bd9Sstevel@tonic-gate return; 16067c478bd9Sstevel@tonic-gate } 16077c478bd9Sstevel@tonic-gate 16087c478bd9Sstevel@tonic-gate if (strcmp(subclass, ESC_DEVFS_DEVI_ADD) == 0 || 16097c478bd9Sstevel@tonic-gate strcmp(subclass, ESC_DEVFS_DEVI_REMOVE) == 0 || 16107c478bd9Sstevel@tonic-gate strcmp(subclass, ESC_DEVFS_MINOR_CREATE) == 0 || 16117c478bd9Sstevel@tonic-gate strcmp(subclass, ESC_DEVFS_MINOR_REMOVE) == 0) { 16127c478bd9Sstevel@tonic-gate if ((err = nvlist_lookup_string(attr_list, DEVFS_PATHNAME, 16137c478bd9Sstevel@tonic-gate &path)) != 0) 16147c478bd9Sstevel@tonic-gate goto out; 16157c478bd9Sstevel@tonic-gate 16167c478bd9Sstevel@tonic-gate if (nvlist_lookup_string(attr_list, DEVFS_DEVI_CLASS, 16177c478bd9Sstevel@tonic-gate &dev_ev_subclass) != 0) 16187c478bd9Sstevel@tonic-gate dev_ev_subclass = NULL; 16197c478bd9Sstevel@tonic-gate 16207c478bd9Sstevel@tonic-gate if (nvlist_lookup_string(attr_list, DEVFS_DRIVER_NAME, 16217c478bd9Sstevel@tonic-gate &driver_name) != 0) 16227c478bd9Sstevel@tonic-gate driver_name = NULL; 16237c478bd9Sstevel@tonic-gate 16247c478bd9Sstevel@tonic-gate if (nvlist_lookup_int32(attr_list, DEVFS_INSTANCE, 16257c478bd9Sstevel@tonic-gate &instance) != 0) 16267c478bd9Sstevel@tonic-gate instance = -1; 16277c478bd9Sstevel@tonic-gate 16287c478bd9Sstevel@tonic-gate if (nvlist_lookup_int32(attr_list, DEVFS_BRANCH_EVENT, 16297c478bd9Sstevel@tonic-gate &branch_event) != 0) 16307c478bd9Sstevel@tonic-gate branch_event = 0; 16317c478bd9Sstevel@tonic-gate 16327c478bd9Sstevel@tonic-gate if (nvlist_lookup_string(attr_list, DEVFS_MINOR_NAME, 16337c478bd9Sstevel@tonic-gate &minor) != 0) 16347c478bd9Sstevel@tonic-gate minor = NULL; 16357c478bd9Sstevel@tonic-gate 16367c478bd9Sstevel@tonic-gate lock_dev(); 16377c478bd9Sstevel@tonic-gate 16387c478bd9Sstevel@tonic-gate if (strcmp(ESC_DEVFS_DEVI_ADD, subclass) == 0) { 16397c478bd9Sstevel@tonic-gate add_minor_pathname(path, NULL, dev_ev_subclass); 16407c478bd9Sstevel@tonic-gate if (branch_event) { 1641f05faa4eSjacobs build_and_enq_event(EC_DEV_BRANCH, 164230294554Sphitran ESC_DEV_BRANCH_ADD, path, DI_NODE_NIL, 164330294554Sphitran NULL); 16447c478bd9Sstevel@tonic-gate } 16457c478bd9Sstevel@tonic-gate 16467c478bd9Sstevel@tonic-gate } else if (strcmp(ESC_DEVFS_MINOR_CREATE, subclass) == 0) { 164730294554Sphitran add_minor_pathname(path, minor, dev_ev_subclass); 16487c478bd9Sstevel@tonic-gate 16497c478bd9Sstevel@tonic-gate } else if (strcmp(ESC_DEVFS_MINOR_REMOVE, subclass) == 0) { 165030294554Sphitran hot_cleanup(path, minor, dev_ev_subclass, driver_name, 165130294554Sphitran instance); 16527c478bd9Sstevel@tonic-gate 16537c478bd9Sstevel@tonic-gate } else { /* ESC_DEVFS_DEVI_REMOVE */ 16547c478bd9Sstevel@tonic-gate hot_cleanup(path, NULL, dev_ev_subclass, 16557c478bd9Sstevel@tonic-gate driver_name, instance); 16567c478bd9Sstevel@tonic-gate if (branch_event) { 1657f05faa4eSjacobs build_and_enq_event(EC_DEV_BRANCH, 165830294554Sphitran ESC_DEV_BRANCH_REMOVE, path, DI_NODE_NIL, 165930294554Sphitran NULL); 16607c478bd9Sstevel@tonic-gate } 16617c478bd9Sstevel@tonic-gate } 16627c478bd9Sstevel@tonic-gate 16637c478bd9Sstevel@tonic-gate unlock_dev(CACHE_STATE); 16647c478bd9Sstevel@tonic-gate 16657c478bd9Sstevel@tonic-gate } else if (strcmp(subclass, ESC_DEVFS_BRANCH_ADD) == 0 || 16667c478bd9Sstevel@tonic-gate strcmp(subclass, ESC_DEVFS_BRANCH_REMOVE) == 0) { 16677c478bd9Sstevel@tonic-gate if ((err = nvlist_lookup_string(attr_list, 16687c478bd9Sstevel@tonic-gate DEVFS_PATHNAME, &path)) != 0) 16697c478bd9Sstevel@tonic-gate goto out; 16707c478bd9Sstevel@tonic-gate 16717c478bd9Sstevel@tonic-gate /* just log ESC_DEV_BRANCH... event */ 16727c478bd9Sstevel@tonic-gate if (strcmp(subclass, ESC_DEVFS_BRANCH_ADD) == 0) 16737c478bd9Sstevel@tonic-gate dev_ev_subclass = ESC_DEV_BRANCH_ADD; 16747c478bd9Sstevel@tonic-gate else 16757c478bd9Sstevel@tonic-gate dev_ev_subclass = ESC_DEV_BRANCH_REMOVE; 16767c478bd9Sstevel@tonic-gate 1677d2596142Scth lock_dev(); 1678f05faa4eSjacobs build_and_enq_event(EC_DEV_BRANCH, dev_ev_subclass, path, 167930294554Sphitran DI_NODE_NIL, NULL); 1680d2596142Scth unlock_dev(CACHE_STATE); 16817c478bd9Sstevel@tonic-gate } else 16827c478bd9Sstevel@tonic-gate err_print(UNKNOWN_EVENT, subclass); 16837c478bd9Sstevel@tonic-gate 16847c478bd9Sstevel@tonic-gate out: 16857c478bd9Sstevel@tonic-gate if (err) 16867c478bd9Sstevel@tonic-gate err_print(EVENT_ATTR_LOOKUP_FAILED, strerror(err)); 16877c478bd9Sstevel@tonic-gate nvlist_free(attr_list); 16887c478bd9Sstevel@tonic-gate } 16897c478bd9Sstevel@tonic-gate 16907c478bd9Sstevel@tonic-gate static void 16917c478bd9Sstevel@tonic-gate dca_impl_init(char *root, char *minor, struct dca_impl *dcip) 16927c478bd9Sstevel@tonic-gate { 16937c478bd9Sstevel@tonic-gate assert(root); 16947c478bd9Sstevel@tonic-gate 16957c478bd9Sstevel@tonic-gate dcip->dci_root = root; 16967c478bd9Sstevel@tonic-gate dcip->dci_minor = minor; 16977c478bd9Sstevel@tonic-gate dcip->dci_driver = NULL; 16987c478bd9Sstevel@tonic-gate dcip->dci_error = 0; 16997c478bd9Sstevel@tonic-gate dcip->dci_flags = 0; 17007c478bd9Sstevel@tonic-gate dcip->dci_arg = NULL; 17017c478bd9Sstevel@tonic-gate } 17027c478bd9Sstevel@tonic-gate 17037c478bd9Sstevel@tonic-gate /* 17047c478bd9Sstevel@tonic-gate * Kernel logs a message when a devinfo node is attached. Try to create 17057c478bd9Sstevel@tonic-gate * /dev and /devices for each minor node. minorname can be NULL. 17067c478bd9Sstevel@tonic-gate */ 17077c478bd9Sstevel@tonic-gate void 17087c478bd9Sstevel@tonic-gate add_minor_pathname(char *node, char *minor, char *ev_subclass) 17097c478bd9Sstevel@tonic-gate { 17107c478bd9Sstevel@tonic-gate struct dca_impl dci; 17117c478bd9Sstevel@tonic-gate 17127c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "add_minor_pathname: node_path=%s minor=%s\n", 17137c478bd9Sstevel@tonic-gate node, minor ? minor : "NULL"); 17147c478bd9Sstevel@tonic-gate 17157c478bd9Sstevel@tonic-gate dca_impl_init(node, minor, &dci); 17167c478bd9Sstevel@tonic-gate 17177c478bd9Sstevel@tonic-gate /* 17187c478bd9Sstevel@tonic-gate * Restrict hotplug link creation if daemon 17197c478bd9Sstevel@tonic-gate * started with -i option. 17207c478bd9Sstevel@tonic-gate */ 17217c478bd9Sstevel@tonic-gate if (single_drv == TRUE) { 17227c478bd9Sstevel@tonic-gate dci.dci_driver = driver; 17237c478bd9Sstevel@tonic-gate } 17247c478bd9Sstevel@tonic-gate 17257c478bd9Sstevel@tonic-gate /* 17267c478bd9Sstevel@tonic-gate * We are being invoked in response to a hotplug 17277c478bd9Sstevel@tonic-gate * event. Also, notify RCM if nodetype indicates 17287c478bd9Sstevel@tonic-gate * a network device has been hotplugged. 17297c478bd9Sstevel@tonic-gate */ 17307c478bd9Sstevel@tonic-gate dci.dci_flags = DCA_HOT_PLUG | DCA_CHECK_TYPE; 17317c478bd9Sstevel@tonic-gate 17327c478bd9Sstevel@tonic-gate devi_tree_walk(&dci, DINFOPROP|DINFOMINOR, ev_subclass); 17337c478bd9Sstevel@tonic-gate } 17347c478bd9Sstevel@tonic-gate 17357c478bd9Sstevel@tonic-gate static di_node_t 17367c478bd9Sstevel@tonic-gate find_clone_node() 17377c478bd9Sstevel@tonic-gate { 17387c478bd9Sstevel@tonic-gate static di_node_t clone_node = DI_NODE_NIL; 17397c478bd9Sstevel@tonic-gate 17407c478bd9Sstevel@tonic-gate if (clone_node == DI_NODE_NIL) 17417c478bd9Sstevel@tonic-gate clone_node = di_init("/pseudo/clone@0", DINFOPROP); 17427c478bd9Sstevel@tonic-gate return (clone_node); 17437c478bd9Sstevel@tonic-gate } 17447c478bd9Sstevel@tonic-gate 17457c478bd9Sstevel@tonic-gate static int 17467c478bd9Sstevel@tonic-gate is_descendent_of(di_node_t node, char *driver) 17477c478bd9Sstevel@tonic-gate { 17487c478bd9Sstevel@tonic-gate while (node != DI_NODE_NIL) { 17497c478bd9Sstevel@tonic-gate char *drv = di_driver_name(node); 17507c478bd9Sstevel@tonic-gate if (strcmp(drv, driver) == 0) 17517c478bd9Sstevel@tonic-gate return (1); 17527c478bd9Sstevel@tonic-gate node = di_parent_node(node); 17537c478bd9Sstevel@tonic-gate } 17547c478bd9Sstevel@tonic-gate return (0); 17557c478bd9Sstevel@tonic-gate } 17567c478bd9Sstevel@tonic-gate 17577c478bd9Sstevel@tonic-gate /* 17587c478bd9Sstevel@tonic-gate * Checks the minor type. If it is an alias node, then lookup 17597c478bd9Sstevel@tonic-gate * the real node/minor first, then call minor_process() to 17607c478bd9Sstevel@tonic-gate * do the real work. 17617c478bd9Sstevel@tonic-gate */ 17627c478bd9Sstevel@tonic-gate static int 17637c478bd9Sstevel@tonic-gate check_minor_type(di_node_t node, di_minor_t minor, void *arg) 17647c478bd9Sstevel@tonic-gate { 17657c478bd9Sstevel@tonic-gate ddi_minor_type minor_type; 17667c478bd9Sstevel@tonic-gate di_node_t clone_node; 17677c478bd9Sstevel@tonic-gate char *mn; 17687c478bd9Sstevel@tonic-gate char *nt; 17697c478bd9Sstevel@tonic-gate struct mlist *dep; 17707c478bd9Sstevel@tonic-gate struct dca_impl *dcip = arg; 17717c478bd9Sstevel@tonic-gate 17727c478bd9Sstevel@tonic-gate assert(dcip); 17737c478bd9Sstevel@tonic-gate 17747c478bd9Sstevel@tonic-gate dep = dcip->dci_arg; 17757c478bd9Sstevel@tonic-gate 17767c478bd9Sstevel@tonic-gate mn = di_minor_name(minor); 17777c478bd9Sstevel@tonic-gate 17787c478bd9Sstevel@tonic-gate /* 17797c478bd9Sstevel@tonic-gate * We match driver here instead of in minor_process 17807c478bd9Sstevel@tonic-gate * as we want the actual driver name. This check is 17817c478bd9Sstevel@tonic-gate * unnecessary during deferred processing. 17827c478bd9Sstevel@tonic-gate */ 17837c478bd9Sstevel@tonic-gate if (dep && 17847c478bd9Sstevel@tonic-gate ((dcip->dci_driver && !is_descendent_of(node, dcip->dci_driver)) || 17857c478bd9Sstevel@tonic-gate (dcip->dci_minor && strcmp(mn, dcip->dci_minor)))) { 17867c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 17877c478bd9Sstevel@tonic-gate } 17887c478bd9Sstevel@tonic-gate 17897c478bd9Sstevel@tonic-gate if ((dcip->dci_flags & DCA_CHECK_TYPE) && 17907c478bd9Sstevel@tonic-gate (nt = di_minor_nodetype(minor)) && 1791210db224Sericheng (strcmp(nt, DDI_NT_NET) == 0)) { 17927c478bd9Sstevel@tonic-gate dcip->dci_flags |= DCA_NOTIFY_RCM; 17937c478bd9Sstevel@tonic-gate dcip->dci_flags &= ~DCA_CHECK_TYPE; 17947c478bd9Sstevel@tonic-gate } 17957c478bd9Sstevel@tonic-gate 17967c478bd9Sstevel@tonic-gate minor_type = di_minor_type(minor); 17977c478bd9Sstevel@tonic-gate 17987c478bd9Sstevel@tonic-gate if (minor_type == DDM_MINOR) { 17997c478bd9Sstevel@tonic-gate minor_process(node, minor, dep); 18007c478bd9Sstevel@tonic-gate 18017c478bd9Sstevel@tonic-gate } else if (minor_type == DDM_ALIAS) { 18027c478bd9Sstevel@tonic-gate struct mlist *cdep, clone_del = {0}; 18037c478bd9Sstevel@tonic-gate 18047c478bd9Sstevel@tonic-gate clone_node = find_clone_node(); 18057c478bd9Sstevel@tonic-gate if (clone_node == DI_NODE_NIL) { 18067c478bd9Sstevel@tonic-gate err_print(DI_INIT_FAILED, "clone", strerror(errno)); 18077c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 18087c478bd9Sstevel@tonic-gate } 18097c478bd9Sstevel@tonic-gate 18107c478bd9Sstevel@tonic-gate cdep = dep ? &clone_del : NULL; 18117c478bd9Sstevel@tonic-gate 18127c478bd9Sstevel@tonic-gate minor_process(clone_node, minor, cdep); 18137c478bd9Sstevel@tonic-gate 18147c478bd9Sstevel@tonic-gate /* 18157c478bd9Sstevel@tonic-gate * cache "alias" minor node and free "clone" minor 18167c478bd9Sstevel@tonic-gate */ 18177c478bd9Sstevel@tonic-gate if (cdep != NULL && cdep->head != NULL) { 18187c478bd9Sstevel@tonic-gate assert(cdep->tail != NULL); 18197c478bd9Sstevel@tonic-gate cache_deferred_minor(dep, node, minor); 18207c478bd9Sstevel@tonic-gate dcip->dci_arg = cdep; 18217c478bd9Sstevel@tonic-gate process_deferred_links(dcip, DCA_FREE_LIST); 18227c478bd9Sstevel@tonic-gate dcip->dci_arg = dep; 18237c478bd9Sstevel@tonic-gate } 18247c478bd9Sstevel@tonic-gate } 18257c478bd9Sstevel@tonic-gate 18267c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 18277c478bd9Sstevel@tonic-gate } 18287c478bd9Sstevel@tonic-gate 18297c478bd9Sstevel@tonic-gate 18307c478bd9Sstevel@tonic-gate /* 18317c478bd9Sstevel@tonic-gate * This is the entry point for each minor node, whether walking 18327c478bd9Sstevel@tonic-gate * the entire tree via di_walk_minor() or processing a hotplug event 18337c478bd9Sstevel@tonic-gate * for a single devinfo node (via hotplug ndi_devi_online()). 18347c478bd9Sstevel@tonic-gate */ 18357c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 18367c478bd9Sstevel@tonic-gate static void 18377c478bd9Sstevel@tonic-gate minor_process(di_node_t node, di_minor_t minor, struct mlist *dep) 18387c478bd9Sstevel@tonic-gate { 18397c478bd9Sstevel@tonic-gate create_list_t *create; 18407c478bd9Sstevel@tonic-gate int defer; 18417c478bd9Sstevel@tonic-gate 18427c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "minor_process: node=%s, minor=%s\n", 18437c478bd9Sstevel@tonic-gate di_node_name(node), di_minor_name(minor)); 18447c478bd9Sstevel@tonic-gate 18457c478bd9Sstevel@tonic-gate if (dep != NULL) { 18467c478bd9Sstevel@tonic-gate 18477c478bd9Sstevel@tonic-gate /* 18487c478bd9Sstevel@tonic-gate * Reset /devices node to minor_perm perm/ownership 18497c478bd9Sstevel@tonic-gate * if we are here to deactivate device allocation 18507c478bd9Sstevel@tonic-gate */ 18517c478bd9Sstevel@tonic-gate if (build_devices == TRUE) { 18527c478bd9Sstevel@tonic-gate reset_node_permissions(node, minor); 18537c478bd9Sstevel@tonic-gate } 18547c478bd9Sstevel@tonic-gate 18557c478bd9Sstevel@tonic-gate if (build_dev == FALSE) { 18567c478bd9Sstevel@tonic-gate return; 18577c478bd9Sstevel@tonic-gate } 18587c478bd9Sstevel@tonic-gate 18597c478bd9Sstevel@tonic-gate /* 18607c478bd9Sstevel@tonic-gate * This function will create any nodes for /etc/devlink.tab. 18617c478bd9Sstevel@tonic-gate * If devlink.tab handles link creation, we don't call any 18627c478bd9Sstevel@tonic-gate * devfsadm modules since that could cause duplicate caching 18637c478bd9Sstevel@tonic-gate * in the enumerate functions if different re strings are 18647c478bd9Sstevel@tonic-gate * passed that are logically identical. I'm still not 18657c478bd9Sstevel@tonic-gate * convinced this would cause any harm, but better to be safe. 18667c478bd9Sstevel@tonic-gate * 18677c478bd9Sstevel@tonic-gate * Deferred processing is available only for devlinks 18687c478bd9Sstevel@tonic-gate * created through devfsadm modules. 18697c478bd9Sstevel@tonic-gate */ 18707c478bd9Sstevel@tonic-gate if (process_devlink_compat(minor, node) == TRUE) { 18717c478bd9Sstevel@tonic-gate return; 18727c478bd9Sstevel@tonic-gate } 18737c478bd9Sstevel@tonic-gate } else { 18747c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "minor_process: deferred processing\n"); 18757c478bd9Sstevel@tonic-gate } 18767c478bd9Sstevel@tonic-gate 18777c478bd9Sstevel@tonic-gate /* 18787c478bd9Sstevel@tonic-gate * look for relevant link create rules in the modules, and 18797c478bd9Sstevel@tonic-gate * invoke the link create callback function to build a link 18807c478bd9Sstevel@tonic-gate * if there is a match. 18817c478bd9Sstevel@tonic-gate */ 18827c478bd9Sstevel@tonic-gate defer = 0; 18837c478bd9Sstevel@tonic-gate for (create = create_head; create != NULL; create = create->next) { 18847c478bd9Sstevel@tonic-gate if ((minor_matches_rule(node, minor, create) == TRUE) && 18857c478bd9Sstevel@tonic-gate class_ok(create->create->device_class) == 18867c478bd9Sstevel@tonic-gate DEVFSADM_SUCCESS) { 18877c478bd9Sstevel@tonic-gate if (call_minor_init(create->modptr) == 18887c478bd9Sstevel@tonic-gate DEVFSADM_FAILURE) { 18897c478bd9Sstevel@tonic-gate continue; 18907c478bd9Sstevel@tonic-gate } 18917c478bd9Sstevel@tonic-gate 18927c478bd9Sstevel@tonic-gate /* 18937c478bd9Sstevel@tonic-gate * If NOT doing the deferred creates (i.e. 1st pass) and 18947c478bd9Sstevel@tonic-gate * rule requests deferred processing cache the minor 18957c478bd9Sstevel@tonic-gate * data. 18967c478bd9Sstevel@tonic-gate * 18977c478bd9Sstevel@tonic-gate * If deferred processing (2nd pass), create links 18987c478bd9Sstevel@tonic-gate * ONLY if rule requests deferred processing. 18997c478bd9Sstevel@tonic-gate */ 19007c478bd9Sstevel@tonic-gate if (dep && ((create->create->flags & CREATE_MASK) == 19017c478bd9Sstevel@tonic-gate CREATE_DEFER)) { 19027c478bd9Sstevel@tonic-gate defer = 1; 19037c478bd9Sstevel@tonic-gate continue; 19047c478bd9Sstevel@tonic-gate } else if (dep == NULL && 19057c478bd9Sstevel@tonic-gate ((create->create->flags & CREATE_MASK) != 19067c478bd9Sstevel@tonic-gate CREATE_DEFER)) { 19077c478bd9Sstevel@tonic-gate continue; 19087c478bd9Sstevel@tonic-gate } 19097c478bd9Sstevel@tonic-gate 19107c478bd9Sstevel@tonic-gate if ((*(create->create->callback_fcn)) 19117c478bd9Sstevel@tonic-gate (minor, node) == DEVFSADM_TERMINATE) { 19127c478bd9Sstevel@tonic-gate break; 19137c478bd9Sstevel@tonic-gate } 19147c478bd9Sstevel@tonic-gate } 19157c478bd9Sstevel@tonic-gate } 19167c478bd9Sstevel@tonic-gate 19177c478bd9Sstevel@tonic-gate if (defer) 19187c478bd9Sstevel@tonic-gate cache_deferred_minor(dep, node, minor); 19197c478bd9Sstevel@tonic-gate } 19207c478bd9Sstevel@tonic-gate 19217c478bd9Sstevel@tonic-gate 19227c478bd9Sstevel@tonic-gate /* 19237c478bd9Sstevel@tonic-gate * Cache node and minor in defer list. 19247c478bd9Sstevel@tonic-gate */ 19257c478bd9Sstevel@tonic-gate static void 19267c478bd9Sstevel@tonic-gate cache_deferred_minor( 19277c478bd9Sstevel@tonic-gate struct mlist *dep, 19287c478bd9Sstevel@tonic-gate di_node_t node, 19297c478bd9Sstevel@tonic-gate di_minor_t minor) 19307c478bd9Sstevel@tonic-gate { 19317c478bd9Sstevel@tonic-gate struct minor *mp; 19327c478bd9Sstevel@tonic-gate const char *fcn = "cache_deferred_minor"; 19337c478bd9Sstevel@tonic-gate 19347c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "%s node=%s, minor=%s\n", fcn, 19357c478bd9Sstevel@tonic-gate di_node_name(node), di_minor_name(minor)); 19367c478bd9Sstevel@tonic-gate 19377c478bd9Sstevel@tonic-gate if (dep == NULL) { 19387c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "%s: cannot cache during " 19397c478bd9Sstevel@tonic-gate "deferred processing. Ignoring minor\n", fcn); 19407c478bd9Sstevel@tonic-gate return; 19417c478bd9Sstevel@tonic-gate } 19427c478bd9Sstevel@tonic-gate 19437c478bd9Sstevel@tonic-gate mp = (struct minor *)s_zalloc(sizeof (struct minor)); 19447c478bd9Sstevel@tonic-gate mp->node = node; 19457c478bd9Sstevel@tonic-gate mp->minor = minor; 19467c478bd9Sstevel@tonic-gate mp->next = NULL; 19477c478bd9Sstevel@tonic-gate 19487c478bd9Sstevel@tonic-gate assert(dep->head == NULL || dep->tail != NULL); 19497c478bd9Sstevel@tonic-gate if (dep->head == NULL) { 19507c478bd9Sstevel@tonic-gate dep->head = mp; 19517c478bd9Sstevel@tonic-gate } else { 19527c478bd9Sstevel@tonic-gate dep->tail->next = mp; 19537c478bd9Sstevel@tonic-gate } 19547c478bd9Sstevel@tonic-gate dep->tail = mp; 19557c478bd9Sstevel@tonic-gate } 19567c478bd9Sstevel@tonic-gate 19577c478bd9Sstevel@tonic-gate /* 19587c478bd9Sstevel@tonic-gate * Check to see if "create" link creation rule matches this node/minor. 19597c478bd9Sstevel@tonic-gate * If it does, return TRUE. 19607c478bd9Sstevel@tonic-gate */ 19617c478bd9Sstevel@tonic-gate static int 19627c478bd9Sstevel@tonic-gate minor_matches_rule(di_node_t node, di_minor_t minor, create_list_t *create) 19637c478bd9Sstevel@tonic-gate { 19647c478bd9Sstevel@tonic-gate char *m_nodetype, *m_drvname; 19657c478bd9Sstevel@tonic-gate 19667c478bd9Sstevel@tonic-gate if (create->create->node_type != NULL) { 19677c478bd9Sstevel@tonic-gate 19687c478bd9Sstevel@tonic-gate m_nodetype = di_minor_nodetype(minor); 19697c478bd9Sstevel@tonic-gate assert(m_nodetype != NULL); 19707c478bd9Sstevel@tonic-gate 19717c478bd9Sstevel@tonic-gate switch (create->create->flags & TYPE_MASK) { 19727c478bd9Sstevel@tonic-gate case TYPE_EXACT: 19737c478bd9Sstevel@tonic-gate if (strcmp(create->create->node_type, m_nodetype) != 19747c478bd9Sstevel@tonic-gate 0) { 19757c478bd9Sstevel@tonic-gate return (FALSE); 19767c478bd9Sstevel@tonic-gate } 19777c478bd9Sstevel@tonic-gate break; 19787c478bd9Sstevel@tonic-gate case TYPE_PARTIAL: 19797c478bd9Sstevel@tonic-gate if (strncmp(create->create->node_type, m_nodetype, 19807c478bd9Sstevel@tonic-gate strlen(create->create->node_type)) != 0) { 19817c478bd9Sstevel@tonic-gate return (FALSE); 19827c478bd9Sstevel@tonic-gate } 19837c478bd9Sstevel@tonic-gate break; 19847c478bd9Sstevel@tonic-gate case TYPE_RE: 19857c478bd9Sstevel@tonic-gate if (regexec(&(create->node_type_comp), m_nodetype, 19867c478bd9Sstevel@tonic-gate 0, NULL, 0) != 0) { 19877c478bd9Sstevel@tonic-gate return (FALSE); 19887c478bd9Sstevel@tonic-gate } 19897c478bd9Sstevel@tonic-gate break; 19907c478bd9Sstevel@tonic-gate } 19917c478bd9Sstevel@tonic-gate } 19927c478bd9Sstevel@tonic-gate 19937c478bd9Sstevel@tonic-gate if (create->create->drv_name != NULL) { 19947c478bd9Sstevel@tonic-gate m_drvname = di_driver_name(node); 19957c478bd9Sstevel@tonic-gate switch (create->create->flags & DRV_MASK) { 19967c478bd9Sstevel@tonic-gate case DRV_EXACT: 19977c478bd9Sstevel@tonic-gate if (strcmp(create->create->drv_name, m_drvname) != 0) { 19987c478bd9Sstevel@tonic-gate return (FALSE); 19997c478bd9Sstevel@tonic-gate } 20007c478bd9Sstevel@tonic-gate break; 20017c478bd9Sstevel@tonic-gate case DRV_RE: 20027c478bd9Sstevel@tonic-gate if (regexec(&(create->drv_name_comp), m_drvname, 20037c478bd9Sstevel@tonic-gate 0, NULL, 0) != 0) { 20047c478bd9Sstevel@tonic-gate return (FALSE); 20057c478bd9Sstevel@tonic-gate } 20067c478bd9Sstevel@tonic-gate break; 20077c478bd9Sstevel@tonic-gate } 20087c478bd9Sstevel@tonic-gate } 20097c478bd9Sstevel@tonic-gate 20107c478bd9Sstevel@tonic-gate return (TRUE); 20117c478bd9Sstevel@tonic-gate } 20127c478bd9Sstevel@tonic-gate 20137c478bd9Sstevel@tonic-gate /* 20147c478bd9Sstevel@tonic-gate * If no classes were given on the command line, then return DEVFSADM_SUCCESS. 20157c478bd9Sstevel@tonic-gate * Otherwise, return DEVFSADM_SUCCESS if the device "class" from the module 20167c478bd9Sstevel@tonic-gate * matches one of the device classes given on the command line, 20177c478bd9Sstevel@tonic-gate * otherwise, return DEVFSADM_FAILURE. 20187c478bd9Sstevel@tonic-gate */ 20197c478bd9Sstevel@tonic-gate static int 20207c478bd9Sstevel@tonic-gate class_ok(char *class) 20217c478bd9Sstevel@tonic-gate { 20227c478bd9Sstevel@tonic-gate int i; 20237c478bd9Sstevel@tonic-gate 20247c478bd9Sstevel@tonic-gate if (num_classes == 0) { 20257c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 20267c478bd9Sstevel@tonic-gate } 20277c478bd9Sstevel@tonic-gate 20287c478bd9Sstevel@tonic-gate for (i = 0; i < num_classes; i++) { 20297c478bd9Sstevel@tonic-gate if (strcmp(class, classes[i]) == 0) { 20307c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 20317c478bd9Sstevel@tonic-gate } 20327c478bd9Sstevel@tonic-gate } 20337c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 20347c478bd9Sstevel@tonic-gate } 20357c478bd9Sstevel@tonic-gate 20367c478bd9Sstevel@tonic-gate /* 20377c478bd9Sstevel@tonic-gate * call minor_fini on active modules, then unload ALL modules 20387c478bd9Sstevel@tonic-gate */ 20397c478bd9Sstevel@tonic-gate static void 20407c478bd9Sstevel@tonic-gate unload_modules(void) 20417c478bd9Sstevel@tonic-gate { 20427c478bd9Sstevel@tonic-gate module_t *module_free; 20437c478bd9Sstevel@tonic-gate create_list_t *create_free; 20447c478bd9Sstevel@tonic-gate remove_list_t *remove_free; 20457c478bd9Sstevel@tonic-gate 20467c478bd9Sstevel@tonic-gate while (create_head != NULL) { 20477c478bd9Sstevel@tonic-gate create_free = create_head; 20487c478bd9Sstevel@tonic-gate create_head = create_head->next; 20497c478bd9Sstevel@tonic-gate 20507c478bd9Sstevel@tonic-gate if ((create_free->create->flags & TYPE_RE) == TYPE_RE) { 20517c478bd9Sstevel@tonic-gate regfree(&(create_free->node_type_comp)); 20527c478bd9Sstevel@tonic-gate } 20537c478bd9Sstevel@tonic-gate if ((create_free->create->flags & DRV_RE) == DRV_RE) { 20547c478bd9Sstevel@tonic-gate regfree(&(create_free->drv_name_comp)); 20557c478bd9Sstevel@tonic-gate } 20567c478bd9Sstevel@tonic-gate free(create_free); 20577c478bd9Sstevel@tonic-gate } 20587c478bd9Sstevel@tonic-gate 20597c478bd9Sstevel@tonic-gate while (remove_head != NULL) { 20607c478bd9Sstevel@tonic-gate remove_free = remove_head; 20617c478bd9Sstevel@tonic-gate remove_head = remove_head->next; 20627c478bd9Sstevel@tonic-gate free(remove_free); 20637c478bd9Sstevel@tonic-gate } 20647c478bd9Sstevel@tonic-gate 20657c478bd9Sstevel@tonic-gate while (module_head != NULL) { 20667c478bd9Sstevel@tonic-gate 20677c478bd9Sstevel@tonic-gate if ((module_head->minor_fini != NULL) && 20687c478bd9Sstevel@tonic-gate ((module_head->flags & MODULE_ACTIVE) == MODULE_ACTIVE)) { 20697c478bd9Sstevel@tonic-gate (void) (*(module_head->minor_fini))(); 20707c478bd9Sstevel@tonic-gate } 20717c478bd9Sstevel@tonic-gate 20727c478bd9Sstevel@tonic-gate vprint(MODLOAD_MID, "unloading module %s\n", module_head->name); 20737c478bd9Sstevel@tonic-gate free(module_head->name); 20747c478bd9Sstevel@tonic-gate (void) dlclose(module_head->dlhandle); 20757c478bd9Sstevel@tonic-gate 20767c478bd9Sstevel@tonic-gate module_free = module_head; 20777c478bd9Sstevel@tonic-gate module_head = module_head->next; 20787c478bd9Sstevel@tonic-gate free(module_free); 20797c478bd9Sstevel@tonic-gate } 20807c478bd9Sstevel@tonic-gate } 20817c478bd9Sstevel@tonic-gate 20827c478bd9Sstevel@tonic-gate /* 20837c478bd9Sstevel@tonic-gate * Load devfsadm logical link processing modules. 20847c478bd9Sstevel@tonic-gate */ 20857c478bd9Sstevel@tonic-gate static void 20867c478bd9Sstevel@tonic-gate load_modules(void) 20877c478bd9Sstevel@tonic-gate { 20887c478bd9Sstevel@tonic-gate DIR *mod_dir; 20894bc0a2efScasper struct dirent *entp; 20907c478bd9Sstevel@tonic-gate char cdir[PATH_MAX + 1]; 20917c478bd9Sstevel@tonic-gate char *last; 20927c478bd9Sstevel@tonic-gate char *mdir = module_dirs; 20937c478bd9Sstevel@tonic-gate char *fcn = "load_modules: "; 20947c478bd9Sstevel@tonic-gate 20957c478bd9Sstevel@tonic-gate while (*mdir != '\0') { 20967c478bd9Sstevel@tonic-gate 20977c478bd9Sstevel@tonic-gate while (*mdir == ':') { 20987c478bd9Sstevel@tonic-gate mdir++; 20997c478bd9Sstevel@tonic-gate } 21007c478bd9Sstevel@tonic-gate 21017c478bd9Sstevel@tonic-gate if (*mdir == '\0') { 21027c478bd9Sstevel@tonic-gate continue; 21037c478bd9Sstevel@tonic-gate } 21047c478bd9Sstevel@tonic-gate 21057c478bd9Sstevel@tonic-gate last = strchr(mdir, ':'); 21067c478bd9Sstevel@tonic-gate 21077c478bd9Sstevel@tonic-gate if (last == NULL) { 21087c478bd9Sstevel@tonic-gate last = mdir + strlen(mdir); 21097c478bd9Sstevel@tonic-gate } 21107c478bd9Sstevel@tonic-gate 21117c478bd9Sstevel@tonic-gate (void) strncpy(cdir, mdir, last - mdir); 21127c478bd9Sstevel@tonic-gate cdir[last - mdir] = '\0'; 21137c478bd9Sstevel@tonic-gate mdir += strlen(cdir); 21147c478bd9Sstevel@tonic-gate 21157c478bd9Sstevel@tonic-gate if ((mod_dir = opendir(cdir)) == NULL) { 21167c478bd9Sstevel@tonic-gate vprint(MODLOAD_MID, "%sopendir(%s): %s\n", 21177c478bd9Sstevel@tonic-gate fcn, cdir, strerror(errno)); 21187c478bd9Sstevel@tonic-gate continue; 21197c478bd9Sstevel@tonic-gate } 21207c478bd9Sstevel@tonic-gate 21214bc0a2efScasper while ((entp = readdir(mod_dir)) != NULL) { 21227c478bd9Sstevel@tonic-gate 21237c478bd9Sstevel@tonic-gate if ((strcmp(entp->d_name, ".") == 0) || 21247c478bd9Sstevel@tonic-gate (strcmp(entp->d_name, "..") == 0)) { 21257c478bd9Sstevel@tonic-gate continue; 21267c478bd9Sstevel@tonic-gate } 21277c478bd9Sstevel@tonic-gate 21287c478bd9Sstevel@tonic-gate load_module(entp->d_name, cdir); 21297c478bd9Sstevel@tonic-gate } 21307c478bd9Sstevel@tonic-gate s_closedir(mod_dir); 21317c478bd9Sstevel@tonic-gate } 21327c478bd9Sstevel@tonic-gate } 21337c478bd9Sstevel@tonic-gate 21347c478bd9Sstevel@tonic-gate static void 21357c478bd9Sstevel@tonic-gate load_module(char *mname, char *cdir) 21367c478bd9Sstevel@tonic-gate { 21377c478bd9Sstevel@tonic-gate _devfsadm_create_reg_t *create_reg; 2138aa646b9dSvikram _devfsadm_remove_reg_V1_t *remove_reg; 21397c478bd9Sstevel@tonic-gate create_list_t *create_list_element; 21407c478bd9Sstevel@tonic-gate create_list_t **create_list_next; 21417c478bd9Sstevel@tonic-gate remove_list_t *remove_list_element; 21427c478bd9Sstevel@tonic-gate remove_list_t **remove_list_next; 21437c478bd9Sstevel@tonic-gate char epath[PATH_MAX + 1], *end; 21447c478bd9Sstevel@tonic-gate char *fcn = "load_module: "; 21457c478bd9Sstevel@tonic-gate char *dlerrstr; 21467c478bd9Sstevel@tonic-gate void *dlhandle; 21477c478bd9Sstevel@tonic-gate module_t *module; 2148aa646b9dSvikram int flags; 21497c478bd9Sstevel@tonic-gate int n; 21507c478bd9Sstevel@tonic-gate int i; 21517c478bd9Sstevel@tonic-gate 21527c478bd9Sstevel@tonic-gate /* ignore any file which does not end in '.so' */ 21537c478bd9Sstevel@tonic-gate if ((end = strstr(mname, MODULE_SUFFIX)) != NULL) { 21547c478bd9Sstevel@tonic-gate if (end[strlen(MODULE_SUFFIX)] != '\0') { 21557c478bd9Sstevel@tonic-gate return; 21567c478bd9Sstevel@tonic-gate } 21577c478bd9Sstevel@tonic-gate } else { 21587c478bd9Sstevel@tonic-gate return; 21597c478bd9Sstevel@tonic-gate } 21607c478bd9Sstevel@tonic-gate 21617c478bd9Sstevel@tonic-gate (void) snprintf(epath, sizeof (epath), "%s/%s", cdir, mname); 21627c478bd9Sstevel@tonic-gate 21637c478bd9Sstevel@tonic-gate if ((dlhandle = dlopen(epath, RTLD_LAZY)) == NULL) { 21647c478bd9Sstevel@tonic-gate dlerrstr = dlerror(); 21657c478bd9Sstevel@tonic-gate err_print(DLOPEN_FAILED, epath, 21667c478bd9Sstevel@tonic-gate dlerrstr ? dlerrstr : "unknown error"); 21677c478bd9Sstevel@tonic-gate return; 21687c478bd9Sstevel@tonic-gate } 21697c478bd9Sstevel@tonic-gate 21707c478bd9Sstevel@tonic-gate /* dlsym the _devfsadm_create_reg structure */ 21717c478bd9Sstevel@tonic-gate if (NULL == (create_reg = (_devfsadm_create_reg_t *) 21727c478bd9Sstevel@tonic-gate dlsym(dlhandle, _DEVFSADM_CREATE_REG))) { 21737c478bd9Sstevel@tonic-gate vprint(MODLOAD_MID, "dlsym(%s, %s): symbol not found\n", epath, 21747c478bd9Sstevel@tonic-gate _DEVFSADM_CREATE_REG); 21757c478bd9Sstevel@tonic-gate } else { 21767c478bd9Sstevel@tonic-gate vprint(MODLOAD_MID, "%sdlsym(%s, %s) succeeded\n", 21777c478bd9Sstevel@tonic-gate fcn, epath, _DEVFSADM_CREATE_REG); 21787c478bd9Sstevel@tonic-gate } 21797c478bd9Sstevel@tonic-gate 21807c478bd9Sstevel@tonic-gate /* dlsym the _devfsadm_remove_reg structure */ 2181aa646b9dSvikram if (NULL == (remove_reg = (_devfsadm_remove_reg_V1_t *) 21827c478bd9Sstevel@tonic-gate dlsym(dlhandle, _DEVFSADM_REMOVE_REG))) { 21837c478bd9Sstevel@tonic-gate vprint(MODLOAD_MID, "dlsym(%s,\n\t%s): symbol not found\n", 21847c478bd9Sstevel@tonic-gate epath, _DEVFSADM_REMOVE_REG); 21857c478bd9Sstevel@tonic-gate } else { 21867c478bd9Sstevel@tonic-gate vprint(MODLOAD_MID, "dlsym(%s, %s): succeeded\n", 21877c478bd9Sstevel@tonic-gate epath, _DEVFSADM_REMOVE_REG); 21887c478bd9Sstevel@tonic-gate } 21897c478bd9Sstevel@tonic-gate 21907c478bd9Sstevel@tonic-gate vprint(MODLOAD_MID, "module %s loaded\n", epath); 21917c478bd9Sstevel@tonic-gate 21927c478bd9Sstevel@tonic-gate module = (module_t *)s_malloc(sizeof (module_t)); 21937c478bd9Sstevel@tonic-gate module->name = s_strdup(epath); 21947c478bd9Sstevel@tonic-gate module->dlhandle = dlhandle; 21957c478bd9Sstevel@tonic-gate 21967c478bd9Sstevel@tonic-gate /* dlsym other module functions, to be called later */ 21977c478bd9Sstevel@tonic-gate module->minor_fini = (int (*)())dlsym(dlhandle, MINOR_FINI); 21987c478bd9Sstevel@tonic-gate module->minor_init = (int (*)())dlsym(dlhandle, MINOR_INIT); 21997c478bd9Sstevel@tonic-gate module->flags = 0; 22007c478bd9Sstevel@tonic-gate 22017c478bd9Sstevel@tonic-gate /* 22027c478bd9Sstevel@tonic-gate * put a ptr to each struct devfsadm_create on "create_head" 22037c478bd9Sstevel@tonic-gate * list sorted in interpose_lvl. 22047c478bd9Sstevel@tonic-gate */ 22057c478bd9Sstevel@tonic-gate if (create_reg != NULL) { 22067c478bd9Sstevel@tonic-gate for (i = 0; i < create_reg->count; i++) { 22077c478bd9Sstevel@tonic-gate int flags = create_reg->tblp[i].flags; 22087c478bd9Sstevel@tonic-gate 22097c478bd9Sstevel@tonic-gate create_list_element = (create_list_t *) 22107c478bd9Sstevel@tonic-gate s_malloc(sizeof (create_list_t)); 22117c478bd9Sstevel@tonic-gate 22127c478bd9Sstevel@tonic-gate create_list_element->create = &(create_reg->tblp[i]); 22137c478bd9Sstevel@tonic-gate create_list_element->modptr = module; 22147c478bd9Sstevel@tonic-gate 22157c478bd9Sstevel@tonic-gate if (((flags & CREATE_MASK) != 0) && 22167c478bd9Sstevel@tonic-gate ((flags & CREATE_MASK) != CREATE_DEFER)) { 22177c478bd9Sstevel@tonic-gate free(create_list_element); 22187c478bd9Sstevel@tonic-gate err_print("illegal flag combination in " 22197c478bd9Sstevel@tonic-gate "module create\n"); 22207c478bd9Sstevel@tonic-gate err_print(IGNORING_ENTRY, i, epath); 22217c478bd9Sstevel@tonic-gate continue; 22227c478bd9Sstevel@tonic-gate } 22237c478bd9Sstevel@tonic-gate 22247c478bd9Sstevel@tonic-gate if (((flags & TYPE_MASK) == 0) ^ 22257c478bd9Sstevel@tonic-gate (create_reg->tblp[i].node_type == NULL)) { 22267c478bd9Sstevel@tonic-gate free(create_list_element); 22277c478bd9Sstevel@tonic-gate err_print("flags value incompatible with " 22287c478bd9Sstevel@tonic-gate "node_type value in module create\n"); 22297c478bd9Sstevel@tonic-gate err_print(IGNORING_ENTRY, i, epath); 22307c478bd9Sstevel@tonic-gate continue; 22317c478bd9Sstevel@tonic-gate } 22327c478bd9Sstevel@tonic-gate 22337c478bd9Sstevel@tonic-gate if (((flags & TYPE_MASK) != 0) && 22347c478bd9Sstevel@tonic-gate ((flags & TYPE_MASK) != TYPE_EXACT) && 22357c478bd9Sstevel@tonic-gate ((flags & TYPE_MASK) != TYPE_RE) && 22367c478bd9Sstevel@tonic-gate ((flags & TYPE_MASK) != TYPE_PARTIAL)) { 22377c478bd9Sstevel@tonic-gate free(create_list_element); 22387c478bd9Sstevel@tonic-gate err_print("illegal TYPE_* flag combination in " 22397c478bd9Sstevel@tonic-gate "module create\n"); 22407c478bd9Sstevel@tonic-gate err_print(IGNORING_ENTRY, i, epath); 22417c478bd9Sstevel@tonic-gate continue; 22427c478bd9Sstevel@tonic-gate } 22437c478bd9Sstevel@tonic-gate 22447c478bd9Sstevel@tonic-gate /* precompile regular expression for efficiency */ 22457c478bd9Sstevel@tonic-gate if ((flags & TYPE_RE) == TYPE_RE) { 22467c478bd9Sstevel@tonic-gate if ((n = regcomp(&(create_list_element-> 22477c478bd9Sstevel@tonic-gate node_type_comp), 22487c478bd9Sstevel@tonic-gate create_reg->tblp[i].node_type, 22497c478bd9Sstevel@tonic-gate REG_EXTENDED)) != 0) { 22507c478bd9Sstevel@tonic-gate free(create_list_element); 22517c478bd9Sstevel@tonic-gate err_print(REGCOMP_FAILED, 22527c478bd9Sstevel@tonic-gate create_reg->tblp[i].node_type, 22537c478bd9Sstevel@tonic-gate n); 22547c478bd9Sstevel@tonic-gate err_print(IGNORING_ENTRY, i, epath); 22557c478bd9Sstevel@tonic-gate continue; 22567c478bd9Sstevel@tonic-gate } 22577c478bd9Sstevel@tonic-gate } 22587c478bd9Sstevel@tonic-gate 22597c478bd9Sstevel@tonic-gate if (((flags & DRV_MASK) == 0) ^ 22607c478bd9Sstevel@tonic-gate (create_reg->tblp[i].drv_name == NULL)) { 22617c478bd9Sstevel@tonic-gate if ((flags & TYPE_RE) == TYPE_RE) { 22627c478bd9Sstevel@tonic-gate regfree(&(create_list_element-> 22637c478bd9Sstevel@tonic-gate node_type_comp)); 22647c478bd9Sstevel@tonic-gate } 22657c478bd9Sstevel@tonic-gate free(create_list_element); 22667c478bd9Sstevel@tonic-gate err_print("flags value incompatible with " 22677c478bd9Sstevel@tonic-gate "drv_name value in module create\n"); 22687c478bd9Sstevel@tonic-gate err_print(IGNORING_ENTRY, i, epath); 22697c478bd9Sstevel@tonic-gate continue; 22707c478bd9Sstevel@tonic-gate } 22717c478bd9Sstevel@tonic-gate 22727c478bd9Sstevel@tonic-gate if (((flags & DRV_MASK) != 0) && 22737c478bd9Sstevel@tonic-gate ((flags & DRV_MASK) != DRV_EXACT) && 22747c478bd9Sstevel@tonic-gate ((flags & DRV_MASK) != DRV_RE)) { 22757c478bd9Sstevel@tonic-gate if ((flags & TYPE_RE) == TYPE_RE) { 22767c478bd9Sstevel@tonic-gate regfree(&(create_list_element-> 22777c478bd9Sstevel@tonic-gate node_type_comp)); 22787c478bd9Sstevel@tonic-gate } 22797c478bd9Sstevel@tonic-gate free(create_list_element); 22807c478bd9Sstevel@tonic-gate err_print("illegal DRV_* flag combination in " 22817c478bd9Sstevel@tonic-gate "module create\n"); 22827c478bd9Sstevel@tonic-gate err_print(IGNORING_ENTRY, i, epath); 22837c478bd9Sstevel@tonic-gate continue; 22847c478bd9Sstevel@tonic-gate } 22857c478bd9Sstevel@tonic-gate 22867c478bd9Sstevel@tonic-gate /* precompile regular expression for efficiency */ 22877c478bd9Sstevel@tonic-gate if ((create_reg->tblp[i].flags & DRV_RE) == DRV_RE) { 22887c478bd9Sstevel@tonic-gate if ((n = regcomp(&(create_list_element-> 22897c478bd9Sstevel@tonic-gate drv_name_comp), 22907c478bd9Sstevel@tonic-gate create_reg->tblp[i].drv_name, 22917c478bd9Sstevel@tonic-gate REG_EXTENDED)) != 0) { 22927c478bd9Sstevel@tonic-gate if ((flags & TYPE_RE) == TYPE_RE) { 22937c478bd9Sstevel@tonic-gate regfree(&(create_list_element-> 22947c478bd9Sstevel@tonic-gate node_type_comp)); 22957c478bd9Sstevel@tonic-gate } 22967c478bd9Sstevel@tonic-gate free(create_list_element); 22977c478bd9Sstevel@tonic-gate err_print(REGCOMP_FAILED, 22987c478bd9Sstevel@tonic-gate create_reg->tblp[i].drv_name, 22997c478bd9Sstevel@tonic-gate n); 23007c478bd9Sstevel@tonic-gate err_print(IGNORING_ENTRY, i, epath); 23017c478bd9Sstevel@tonic-gate continue; 23027c478bd9Sstevel@tonic-gate } 23037c478bd9Sstevel@tonic-gate } 23047c478bd9Sstevel@tonic-gate 23057c478bd9Sstevel@tonic-gate 23067c478bd9Sstevel@tonic-gate /* add to list sorted by interpose level */ 23077c478bd9Sstevel@tonic-gate for (create_list_next = &(create_head); 23087c478bd9Sstevel@tonic-gate (*create_list_next != NULL) && 23097c478bd9Sstevel@tonic-gate (*create_list_next)->create->interpose_lvl >= 23107c478bd9Sstevel@tonic-gate create_list_element->create->interpose_lvl; 23117c478bd9Sstevel@tonic-gate create_list_next = 23127c478bd9Sstevel@tonic-gate &((*create_list_next)->next)); 23137c478bd9Sstevel@tonic-gate create_list_element->next = *create_list_next; 23147c478bd9Sstevel@tonic-gate *create_list_next = create_list_element; 23157c478bd9Sstevel@tonic-gate } 23167c478bd9Sstevel@tonic-gate } 23177c478bd9Sstevel@tonic-gate 23187c478bd9Sstevel@tonic-gate /* 23197c478bd9Sstevel@tonic-gate * put a ptr to each struct devfsadm_remove on "remove_head" 23207c478bd9Sstevel@tonic-gate * list sorted by interpose_lvl. 23217c478bd9Sstevel@tonic-gate */ 2322aa646b9dSvikram flags = 0; 23237c478bd9Sstevel@tonic-gate if (remove_reg != NULL) { 2324aa646b9dSvikram if (remove_reg->version < DEVFSADM_V1) 2325aa646b9dSvikram flags |= RM_NOINTERPOSE; 23267c478bd9Sstevel@tonic-gate for (i = 0; i < remove_reg->count; i++) { 23277c478bd9Sstevel@tonic-gate 23287c478bd9Sstevel@tonic-gate remove_list_element = (remove_list_t *) 23297c478bd9Sstevel@tonic-gate s_malloc(sizeof (remove_list_t)); 23307c478bd9Sstevel@tonic-gate 23317c478bd9Sstevel@tonic-gate remove_list_element->remove = &(remove_reg->tblp[i]); 2332aa646b9dSvikram remove_list_element->remove->flags |= flags; 23337c478bd9Sstevel@tonic-gate remove_list_element->modptr = module; 23347c478bd9Sstevel@tonic-gate 23357c478bd9Sstevel@tonic-gate for (remove_list_next = &(remove_head); 23367c478bd9Sstevel@tonic-gate (*remove_list_next != NULL) && 23377c478bd9Sstevel@tonic-gate (*remove_list_next)->remove->interpose_lvl >= 23387c478bd9Sstevel@tonic-gate remove_list_element->remove->interpose_lvl; 23397c478bd9Sstevel@tonic-gate remove_list_next = 23407c478bd9Sstevel@tonic-gate &((*remove_list_next)->next)); 23417c478bd9Sstevel@tonic-gate remove_list_element->next = *remove_list_next; 23427c478bd9Sstevel@tonic-gate *remove_list_next = remove_list_element; 23437c478bd9Sstevel@tonic-gate } 23447c478bd9Sstevel@tonic-gate } 23457c478bd9Sstevel@tonic-gate 23467c478bd9Sstevel@tonic-gate module->next = module_head; 23477c478bd9Sstevel@tonic-gate module_head = module; 23487c478bd9Sstevel@tonic-gate } 23497c478bd9Sstevel@tonic-gate 23507c478bd9Sstevel@tonic-gate /* 2351ff2aee48Scth * After we have completed a CACHE_STATE, if a SYNC_STATE does not occur 2352ff2aee48Scth * within 'timeout' secs the minor_fini_thread needs to do a SYNC_STATE 2353ff2aee48Scth * so that we still call the minor_fini routines. 23547c478bd9Sstevel@tonic-gate */ 23557c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 23567c478bd9Sstevel@tonic-gate static void 2357ff2aee48Scth minor_fini_thread(void *arg) 23587c478bd9Sstevel@tonic-gate { 2359ff2aee48Scth timestruc_t abstime; 2360ff2aee48Scth 2361ff2aee48Scth vprint(INITFINI_MID, "minor_fini_thread starting\n"); 23627c478bd9Sstevel@tonic-gate 23637c478bd9Sstevel@tonic-gate (void) mutex_lock(&minor_fini_mutex); 2364ff2aee48Scth for (;;) { 2365ff2aee48Scth /* wait the gather period, or until signaled */ 2366ff2aee48Scth abstime.tv_sec = time(NULL) + minor_fini_timeout; 2367ff2aee48Scth abstime.tv_nsec = 0; 2368ff2aee48Scth (void) cond_timedwait(&minor_fini_cv, 2369ff2aee48Scth &minor_fini_mutex, &abstime); 23707c478bd9Sstevel@tonic-gate 2371ff2aee48Scth /* if minor_fini was canceled, go wait again */ 2372ff2aee48Scth if (minor_fini_canceled == TRUE) 2373ff2aee48Scth continue; 23747c478bd9Sstevel@tonic-gate 2375ff2aee48Scth /* if minor_fini was delayed, go wait again */ 2376ff2aee48Scth if (minor_fini_delayed == TRUE) { 2377ff2aee48Scth minor_fini_delayed = FALSE; 2378ff2aee48Scth continue; 2379ff2aee48Scth } 23807c478bd9Sstevel@tonic-gate 2381ff2aee48Scth /* done with cancellations and delays, do the SYNC_STATE */ 23827c478bd9Sstevel@tonic-gate (void) mutex_unlock(&minor_fini_mutex); 23837c478bd9Sstevel@tonic-gate 23847c478bd9Sstevel@tonic-gate lock_dev(); 23857c478bd9Sstevel@tonic-gate unlock_dev(SYNC_STATE); 2386ff2aee48Scth vprint(INITFINI_MID, "minor_fini sync done\n"); 23877c478bd9Sstevel@tonic-gate 23887c478bd9Sstevel@tonic-gate (void) mutex_lock(&minor_fini_mutex); 23897c478bd9Sstevel@tonic-gate } 2390ff2aee48Scth } 23917c478bd9Sstevel@tonic-gate 23927c478bd9Sstevel@tonic-gate 23937c478bd9Sstevel@tonic-gate /* 23947c478bd9Sstevel@tonic-gate * Attempt to initialize module, if a minor_init routine exists. Set 23957c478bd9Sstevel@tonic-gate * the active flag if the routine exists and succeeds. If it doesn't 23967c478bd9Sstevel@tonic-gate * exist, just set the active flag. 23977c478bd9Sstevel@tonic-gate */ 23987c478bd9Sstevel@tonic-gate static int 23997c478bd9Sstevel@tonic-gate call_minor_init(module_t *module) 24007c478bd9Sstevel@tonic-gate { 24017c478bd9Sstevel@tonic-gate char *fcn = "call_minor_init: "; 24027c478bd9Sstevel@tonic-gate 24037c478bd9Sstevel@tonic-gate if ((module->flags & MODULE_ACTIVE) == MODULE_ACTIVE) { 24047c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 24057c478bd9Sstevel@tonic-gate } 24067c478bd9Sstevel@tonic-gate 24077c478bd9Sstevel@tonic-gate vprint(INITFINI_MID, "%smodule %s. current state: inactive\n", 24087c478bd9Sstevel@tonic-gate fcn, module->name); 24097c478bd9Sstevel@tonic-gate 24107c478bd9Sstevel@tonic-gate if (module->minor_init == NULL) { 24117c478bd9Sstevel@tonic-gate module->flags |= MODULE_ACTIVE; 24127c478bd9Sstevel@tonic-gate vprint(INITFINI_MID, "minor_init not defined\n"); 24137c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 24147c478bd9Sstevel@tonic-gate } 24157c478bd9Sstevel@tonic-gate 24167c478bd9Sstevel@tonic-gate if ((*(module->minor_init))() == DEVFSADM_FAILURE) { 24177c478bd9Sstevel@tonic-gate err_print(FAILED_FOR_MODULE, MINOR_INIT, module->name); 24187c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 24197c478bd9Sstevel@tonic-gate } 24207c478bd9Sstevel@tonic-gate 24217c478bd9Sstevel@tonic-gate vprint(INITFINI_MID, "minor_init() returns DEVFSADM_SUCCESS. " 24227c478bd9Sstevel@tonic-gate "new state: active\n"); 24237c478bd9Sstevel@tonic-gate 24247c478bd9Sstevel@tonic-gate module->flags |= MODULE_ACTIVE; 24257c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 24267c478bd9Sstevel@tonic-gate } 24277c478bd9Sstevel@tonic-gate 24287c478bd9Sstevel@tonic-gate /* 24297c478bd9Sstevel@tonic-gate * Creates a symlink 'link' to the physical path of node:minor. 24307c478bd9Sstevel@tonic-gate * Construct link contents, then call create_link_common(). 24317c478bd9Sstevel@tonic-gate */ 24327c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 24337c478bd9Sstevel@tonic-gate int 2434facf4a8dSllai1 devfsadm_mklink(char *link, di_node_t node, di_minor_t minor, int flags) 24357c478bd9Sstevel@tonic-gate { 24367c478bd9Sstevel@tonic-gate char rcontents[PATH_MAX]; 24377c478bd9Sstevel@tonic-gate char devlink[PATH_MAX]; 24387c478bd9Sstevel@tonic-gate char phy_path[PATH_MAX]; 24397c478bd9Sstevel@tonic-gate char *acontents; 24407c478bd9Sstevel@tonic-gate char *dev_path; 24417c478bd9Sstevel@tonic-gate int numslashes; 24427c478bd9Sstevel@tonic-gate int rv; 24437c478bd9Sstevel@tonic-gate int i, link_exists; 24447c478bd9Sstevel@tonic-gate int last_was_slash = FALSE; 24457c478bd9Sstevel@tonic-gate 24467c478bd9Sstevel@tonic-gate /* 24477c478bd9Sstevel@tonic-gate * try to use devices path 24487c478bd9Sstevel@tonic-gate */ 24497c478bd9Sstevel@tonic-gate if ((node == lnode) && (minor == lminor)) { 24507c478bd9Sstevel@tonic-gate acontents = lphy_path; 24517c478bd9Sstevel@tonic-gate } else if (di_minor_type(minor) == DDM_ALIAS) { 24527c478bd9Sstevel@tonic-gate /* use /pseudo/clone@0:<driver> as the phys path */ 24537c478bd9Sstevel@tonic-gate (void) snprintf(phy_path, sizeof (phy_path), 24547c478bd9Sstevel@tonic-gate "/pseudo/clone@0:%s", 24557c478bd9Sstevel@tonic-gate di_driver_name(di_minor_devinfo(minor))); 24567c478bd9Sstevel@tonic-gate acontents = phy_path; 24577c478bd9Sstevel@tonic-gate } else { 24587c478bd9Sstevel@tonic-gate if ((dev_path = di_devfs_path(node)) == NULL) { 24597c478bd9Sstevel@tonic-gate err_print(DI_DEVFS_PATH_FAILED, strerror(errno)); 24607c478bd9Sstevel@tonic-gate devfsadm_exit(1); 24617c478bd9Sstevel@tonic-gate } 24627c478bd9Sstevel@tonic-gate (void) snprintf(phy_path, sizeof (phy_path), "%s:%s", 24637c478bd9Sstevel@tonic-gate dev_path, di_minor_name(minor)); 24647c478bd9Sstevel@tonic-gate di_devfs_path_free(dev_path); 24657c478bd9Sstevel@tonic-gate acontents = phy_path; 24667c478bd9Sstevel@tonic-gate } 24677c478bd9Sstevel@tonic-gate 24687c478bd9Sstevel@tonic-gate /* prepend link with dev_dir contents */ 24697c478bd9Sstevel@tonic-gate (void) strlcpy(devlink, dev_dir, sizeof (devlink)); 24707c478bd9Sstevel@tonic-gate (void) strlcat(devlink, "/", sizeof (devlink)); 24717c478bd9Sstevel@tonic-gate (void) strlcat(devlink, link, sizeof (devlink)); 24727c478bd9Sstevel@tonic-gate 24737c478bd9Sstevel@tonic-gate /* 24747c478bd9Sstevel@tonic-gate * Calculate # of ../ to add. Account for double '//' in path. 24757c478bd9Sstevel@tonic-gate * Ignore all leading slashes. 24767c478bd9Sstevel@tonic-gate */ 24777c478bd9Sstevel@tonic-gate for (i = 0; link[i] == '/'; i++) 24787c478bd9Sstevel@tonic-gate ; 24797c478bd9Sstevel@tonic-gate for (numslashes = 0; link[i] != '\0'; i++) { 24807c478bd9Sstevel@tonic-gate if (link[i] == '/') { 24817c478bd9Sstevel@tonic-gate if (last_was_slash == FALSE) { 24827c478bd9Sstevel@tonic-gate numslashes++; 24837c478bd9Sstevel@tonic-gate last_was_slash = TRUE; 24847c478bd9Sstevel@tonic-gate } 24857c478bd9Sstevel@tonic-gate } else { 24867c478bd9Sstevel@tonic-gate last_was_slash = FALSE; 24877c478bd9Sstevel@tonic-gate } 24887c478bd9Sstevel@tonic-gate } 24897c478bd9Sstevel@tonic-gate /* Don't count any trailing '/' */ 24907c478bd9Sstevel@tonic-gate if (link[i-1] == '/') { 24917c478bd9Sstevel@tonic-gate numslashes--; 24927c478bd9Sstevel@tonic-gate } 24937c478bd9Sstevel@tonic-gate 24947c478bd9Sstevel@tonic-gate rcontents[0] = '\0'; 24957c478bd9Sstevel@tonic-gate do { 24967c478bd9Sstevel@tonic-gate (void) strlcat(rcontents, "../", sizeof (rcontents)); 24977c478bd9Sstevel@tonic-gate } while (numslashes-- != 0); 24987c478bd9Sstevel@tonic-gate 24997c478bd9Sstevel@tonic-gate (void) strlcat(rcontents, "devices", sizeof (rcontents)); 25007c478bd9Sstevel@tonic-gate (void) strlcat(rcontents, acontents, sizeof (rcontents)); 25017c478bd9Sstevel@tonic-gate 25027c478bd9Sstevel@tonic-gate if (devlinks_debug == TRUE) { 25037c478bd9Sstevel@tonic-gate vprint(INFO_MID, "adding link %s ==> %s\n", devlink, rcontents); 25047c478bd9Sstevel@tonic-gate } 25057c478bd9Sstevel@tonic-gate 25067c478bd9Sstevel@tonic-gate if ((rv = create_link_common(devlink, rcontents, &link_exists)) 25077c478bd9Sstevel@tonic-gate == DEVFSADM_SUCCESS) { 25087c478bd9Sstevel@tonic-gate linknew = TRUE; 25097c478bd9Sstevel@tonic-gate add_link_to_cache(link, acontents); 25107c478bd9Sstevel@tonic-gate } else { 25117c478bd9Sstevel@tonic-gate linknew = FALSE; 25127c478bd9Sstevel@tonic-gate } 25137c478bd9Sstevel@tonic-gate 25147c478bd9Sstevel@tonic-gate if (link_exists == TRUE) { 25157c478bd9Sstevel@tonic-gate /* Link exists or was just created */ 25167c478bd9Sstevel@tonic-gate (void) di_devlink_add_link(devlink_cache, link, rcontents, 25177c478bd9Sstevel@tonic-gate DI_PRIMARY_LINK); 25186e670f77Saj 25196e670f77Saj if (system_labeled && (flags & DA_ADD)) { 25206e670f77Saj /* 25216e670f77Saj * Add this to the list of allocatable devices. If this 25226e670f77Saj * is a hotplugged, removable disk, add it as rmdisk. 25236e670f77Saj */ 25246e670f77Saj int instance = di_instance(node); 25256e670f77Saj 25266e670f77Saj if ((flags & DA_CD) && 25276e670f77Saj (_da_check_for_usb(devlink, root_dir) == 1)) { 25286e670f77Saj (void) da_add_list(&devlist, devlink, instance, 25296e670f77Saj DA_ADD|DA_RMDISK); 25306e670f77Saj update_devdb = DA_RMDISK; 25316e670f77Saj } else if (linknew == TRUE) { 25326e670f77Saj (void) da_add_list(&devlist, devlink, instance, 25336e670f77Saj flags); 25346e670f77Saj update_devdb = flags; 25356e670f77Saj } 25366e670f77Saj } 25377c478bd9Sstevel@tonic-gate } 25387c478bd9Sstevel@tonic-gate 25397c478bd9Sstevel@tonic-gate return (rv); 25407c478bd9Sstevel@tonic-gate } 25417c478bd9Sstevel@tonic-gate 25427c478bd9Sstevel@tonic-gate /* 25437c478bd9Sstevel@tonic-gate * Creates a symlink link to primary_link. Calculates relative 25447c478bd9Sstevel@tonic-gate * directory offsets, then calls link_common(). 25457c478bd9Sstevel@tonic-gate */ 25467c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 25477c478bd9Sstevel@tonic-gate int 25487c478bd9Sstevel@tonic-gate devfsadm_secondary_link(char *link, char *primary_link, int flags) 25497c478bd9Sstevel@tonic-gate { 25507c478bd9Sstevel@tonic-gate char contents[PATH_MAX + 1]; 25517c478bd9Sstevel@tonic-gate char devlink[PATH_MAX + 1]; 25527c478bd9Sstevel@tonic-gate int rv, link_exists; 25537c478bd9Sstevel@tonic-gate char *fpath; 25547c478bd9Sstevel@tonic-gate char *tpath; 25557c478bd9Sstevel@tonic-gate char *op; 25567c478bd9Sstevel@tonic-gate 25577c478bd9Sstevel@tonic-gate /* prepend link with dev_dir contents */ 25587c478bd9Sstevel@tonic-gate (void) strcpy(devlink, dev_dir); 25597c478bd9Sstevel@tonic-gate (void) strcat(devlink, "/"); 25607c478bd9Sstevel@tonic-gate (void) strcat(devlink, link); 25617c478bd9Sstevel@tonic-gate /* 25627c478bd9Sstevel@tonic-gate * building extra link, so use first link as link contents, but first 25637c478bd9Sstevel@tonic-gate * make it relative. 25647c478bd9Sstevel@tonic-gate */ 25657c478bd9Sstevel@tonic-gate fpath = link; 25667c478bd9Sstevel@tonic-gate tpath = primary_link; 25677c478bd9Sstevel@tonic-gate op = contents; 25687c478bd9Sstevel@tonic-gate 25697c478bd9Sstevel@tonic-gate while (*fpath == *tpath && *fpath != '\0') { 25707c478bd9Sstevel@tonic-gate fpath++, tpath++; 25717c478bd9Sstevel@tonic-gate } 25727c478bd9Sstevel@tonic-gate 25737c478bd9Sstevel@tonic-gate /* Count directories to go up, if any, and add "../" */ 25747c478bd9Sstevel@tonic-gate while (*fpath != '\0') { 25757c478bd9Sstevel@tonic-gate if (*fpath == '/') { 25767c478bd9Sstevel@tonic-gate (void) strcpy(op, "../"); 25777c478bd9Sstevel@tonic-gate op += 3; 25787c478bd9Sstevel@tonic-gate } 25797c478bd9Sstevel@tonic-gate fpath++; 25807c478bd9Sstevel@tonic-gate } 25817c478bd9Sstevel@tonic-gate 25827c478bd9Sstevel@tonic-gate /* 25837c478bd9Sstevel@tonic-gate * Back up to the start of the current path component, in 25847c478bd9Sstevel@tonic-gate * case in the middle 25857c478bd9Sstevel@tonic-gate */ 25867c478bd9Sstevel@tonic-gate while (tpath != primary_link && *(tpath-1) != '/') { 25877c478bd9Sstevel@tonic-gate tpath--; 25887c478bd9Sstevel@tonic-gate } 25897c478bd9Sstevel@tonic-gate (void) strcpy(op, tpath); 25907c478bd9Sstevel@tonic-gate 25917c478bd9Sstevel@tonic-gate if (devlinks_debug == TRUE) { 25927c478bd9Sstevel@tonic-gate vprint(INFO_MID, "adding extra link %s ==> %s\n", 25937c478bd9Sstevel@tonic-gate devlink, contents); 25947c478bd9Sstevel@tonic-gate } 25957c478bd9Sstevel@tonic-gate 25967c478bd9Sstevel@tonic-gate if ((rv = create_link_common(devlink, contents, &link_exists)) 25977c478bd9Sstevel@tonic-gate == DEVFSADM_SUCCESS) { 25987c478bd9Sstevel@tonic-gate /* 25997c478bd9Sstevel@tonic-gate * we need to save the ultimate /devices contents, and not the 26007c478bd9Sstevel@tonic-gate * secondary link, since hotcleanup only looks at /devices path. 26017c478bd9Sstevel@tonic-gate * Since we don't have devices path here, we can try to get it 26027c478bd9Sstevel@tonic-gate * by readlink'ing the secondary link. This assumes the primary 26037c478bd9Sstevel@tonic-gate * link was created first. 26047c478bd9Sstevel@tonic-gate */ 26057c478bd9Sstevel@tonic-gate add_link_to_cache(link, lphy_path); 26067c478bd9Sstevel@tonic-gate linknew = TRUE; 260745916cd2Sjpk if (system_labeled && 260845916cd2Sjpk ((flags & DA_AUDIO) && (flags & DA_ADD))) { 260945916cd2Sjpk /* 261045916cd2Sjpk * Add this device to the list of allocatable devices. 261145916cd2Sjpk */ 261245916cd2Sjpk int instance = 0; 261345916cd2Sjpk 261445916cd2Sjpk op = strrchr(contents, '/'); 261545916cd2Sjpk op++; 261645916cd2Sjpk (void) sscanf(op, "%d", &instance); 261745916cd2Sjpk (void) da_add_list(&devlist, devlink, instance, flags); 261845916cd2Sjpk update_devdb = flags; 261945916cd2Sjpk } 26207c478bd9Sstevel@tonic-gate } else { 26217c478bd9Sstevel@tonic-gate linknew = FALSE; 26227c478bd9Sstevel@tonic-gate } 26237c478bd9Sstevel@tonic-gate 26247c478bd9Sstevel@tonic-gate /* 26257c478bd9Sstevel@tonic-gate * If link exists or was just created, add it to the database 26267c478bd9Sstevel@tonic-gate */ 26277c478bd9Sstevel@tonic-gate if (link_exists == TRUE) { 26287c478bd9Sstevel@tonic-gate (void) di_devlink_add_link(devlink_cache, link, contents, 26297c478bd9Sstevel@tonic-gate DI_SECONDARY_LINK); 26307c478bd9Sstevel@tonic-gate } 26317c478bd9Sstevel@tonic-gate 26327c478bd9Sstevel@tonic-gate return (rv); 26337c478bd9Sstevel@tonic-gate } 26347c478bd9Sstevel@tonic-gate 26357c478bd9Sstevel@tonic-gate /* returns pointer to the devices directory */ 26367c478bd9Sstevel@tonic-gate char * 26377c478bd9Sstevel@tonic-gate devfsadm_get_devices_dir() 26387c478bd9Sstevel@tonic-gate { 26397c478bd9Sstevel@tonic-gate return (devices_dir); 26407c478bd9Sstevel@tonic-gate } 26417c478bd9Sstevel@tonic-gate 26427c478bd9Sstevel@tonic-gate /* 26437c478bd9Sstevel@tonic-gate * Does the actual link creation. VERBOSE_MID only used if there is 26447c478bd9Sstevel@tonic-gate * a change. CHATTY_MID used otherwise. 26457c478bd9Sstevel@tonic-gate */ 26467c478bd9Sstevel@tonic-gate static int 26477c478bd9Sstevel@tonic-gate create_link_common(char *devlink, char *contents, int *exists) 26487c478bd9Sstevel@tonic-gate { 26497c478bd9Sstevel@tonic-gate int try; 26507c478bd9Sstevel@tonic-gate int linksize; 26517c478bd9Sstevel@tonic-gate int max_tries = 0; 26527c478bd9Sstevel@tonic-gate static int prev_link_existed = TRUE; 26537c478bd9Sstevel@tonic-gate char checkcontents[PATH_MAX + 1]; 26547c478bd9Sstevel@tonic-gate char *hide; 26557c478bd9Sstevel@tonic-gate 26567c478bd9Sstevel@tonic-gate *exists = FALSE; 26577c478bd9Sstevel@tonic-gate 26587c478bd9Sstevel@tonic-gate /* Database is not updated when file_mods == FALSE */ 26597c478bd9Sstevel@tonic-gate if (file_mods == FALSE) { 26607c478bd9Sstevel@tonic-gate linksize = readlink(devlink, checkcontents, PATH_MAX); 26617c478bd9Sstevel@tonic-gate if (linksize > 0) { 26627c478bd9Sstevel@tonic-gate checkcontents[linksize] = '\0'; 26637c478bd9Sstevel@tonic-gate if (strcmp(checkcontents, contents) != 0) { 26647c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, REMOVING_LINK, 26657c478bd9Sstevel@tonic-gate devlink, checkcontents); 26667c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 26677c478bd9Sstevel@tonic-gate } else { 26687c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "link exists and is correct:" 26697c478bd9Sstevel@tonic-gate " %s -> %s\n", devlink, contents); 26707c478bd9Sstevel@tonic-gate /* failure only in that the link existed */ 26717c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 26727c478bd9Sstevel@tonic-gate } 26737c478bd9Sstevel@tonic-gate } else { 26747c478bd9Sstevel@tonic-gate vprint(VERBOSE_MID, CREATING_LINK, devlink, contents); 26757c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 26767c478bd9Sstevel@tonic-gate } 26777c478bd9Sstevel@tonic-gate } 26787c478bd9Sstevel@tonic-gate 26797c478bd9Sstevel@tonic-gate /* 26807c478bd9Sstevel@tonic-gate * systems calls are expensive, so predict whether to readlink 26817c478bd9Sstevel@tonic-gate * or symlink first, based on previous attempt 26827c478bd9Sstevel@tonic-gate */ 26837c478bd9Sstevel@tonic-gate if (prev_link_existed == FALSE) { 26847c478bd9Sstevel@tonic-gate try = CREATE_LINK; 26857c478bd9Sstevel@tonic-gate } else { 26867c478bd9Sstevel@tonic-gate try = READ_LINK; 26877c478bd9Sstevel@tonic-gate } 26887c478bd9Sstevel@tonic-gate 26897c478bd9Sstevel@tonic-gate while (++max_tries <= 3) { 26907c478bd9Sstevel@tonic-gate 26917c478bd9Sstevel@tonic-gate switch (try) { 26927c478bd9Sstevel@tonic-gate case CREATE_LINK: 26937c478bd9Sstevel@tonic-gate 26947c478bd9Sstevel@tonic-gate if (symlink(contents, devlink) == 0) { 26957c478bd9Sstevel@tonic-gate vprint(VERBOSE_MID, CREATING_LINK, devlink, 26967c478bd9Sstevel@tonic-gate contents); 26977c478bd9Sstevel@tonic-gate prev_link_existed = FALSE; 26987c478bd9Sstevel@tonic-gate /* link successfully created */ 26997c478bd9Sstevel@tonic-gate *exists = TRUE; 27007c478bd9Sstevel@tonic-gate set_logindev_perms(devlink); 27017c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 27027c478bd9Sstevel@tonic-gate } else { 27037c478bd9Sstevel@tonic-gate switch (errno) { 27047c478bd9Sstevel@tonic-gate 27057c478bd9Sstevel@tonic-gate case ENOENT: 27067c478bd9Sstevel@tonic-gate /* dirpath to node doesn't exist */ 27077c478bd9Sstevel@tonic-gate hide = strrchr(devlink, '/'); 27087c478bd9Sstevel@tonic-gate *hide = '\0'; 27097c478bd9Sstevel@tonic-gate s_mkdirp(devlink, S_IRWXU|S_IRGRP| 27107c478bd9Sstevel@tonic-gate S_IXGRP|S_IROTH|S_IXOTH); 27117c478bd9Sstevel@tonic-gate *hide = '/'; 27127c478bd9Sstevel@tonic-gate break; 27137c478bd9Sstevel@tonic-gate case EEXIST: 27147c478bd9Sstevel@tonic-gate try = READ_LINK; 27157c478bd9Sstevel@tonic-gate break; 27167c478bd9Sstevel@tonic-gate default: 27177c478bd9Sstevel@tonic-gate err_print(SYMLINK_FAILED, devlink, 27187c478bd9Sstevel@tonic-gate contents, strerror(errno)); 27197c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 27207c478bd9Sstevel@tonic-gate } 27217c478bd9Sstevel@tonic-gate } 27227c478bd9Sstevel@tonic-gate break; 27237c478bd9Sstevel@tonic-gate 27247c478bd9Sstevel@tonic-gate case READ_LINK: 27257c478bd9Sstevel@tonic-gate 27267c478bd9Sstevel@tonic-gate linksize = readlink(devlink, checkcontents, PATH_MAX); 27277c478bd9Sstevel@tonic-gate if (linksize >= 0) { 27287c478bd9Sstevel@tonic-gate checkcontents[linksize] = '\0'; 27297c478bd9Sstevel@tonic-gate if (strcmp(checkcontents, contents) != 0) { 27307c478bd9Sstevel@tonic-gate s_unlink(devlink); 27317c478bd9Sstevel@tonic-gate vprint(VERBOSE_MID, REMOVING_LINK, 27327c478bd9Sstevel@tonic-gate devlink, checkcontents); 27337c478bd9Sstevel@tonic-gate try = CREATE_LINK; 27347c478bd9Sstevel@tonic-gate } else { 27357c478bd9Sstevel@tonic-gate prev_link_existed = TRUE; 27367c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, 27377c478bd9Sstevel@tonic-gate "link exists and is correct:" 27387c478bd9Sstevel@tonic-gate " %s -> %s\n", devlink, 27397c478bd9Sstevel@tonic-gate contents); 27407c478bd9Sstevel@tonic-gate *exists = TRUE; 27417c478bd9Sstevel@tonic-gate /* failure in that the link existed */ 27427c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 27437c478bd9Sstevel@tonic-gate } 27447c478bd9Sstevel@tonic-gate } else { 27457c478bd9Sstevel@tonic-gate switch (errno) { 27467c478bd9Sstevel@tonic-gate case EINVAL: 27477c478bd9Sstevel@tonic-gate /* not a symlink, remove and create */ 27487c478bd9Sstevel@tonic-gate s_unlink(devlink); 27497c478bd9Sstevel@tonic-gate default: 27507c478bd9Sstevel@tonic-gate /* maybe it didn't exist at all */ 27517c478bd9Sstevel@tonic-gate try = CREATE_LINK; 27527c478bd9Sstevel@tonic-gate break; 27537c478bd9Sstevel@tonic-gate } 27547c478bd9Sstevel@tonic-gate } 27557c478bd9Sstevel@tonic-gate break; 27567c478bd9Sstevel@tonic-gate } 27577c478bd9Sstevel@tonic-gate } 27587c478bd9Sstevel@tonic-gate err_print(MAX_ATTEMPTS, devlink, contents); 27597c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 27607c478bd9Sstevel@tonic-gate } 27617c478bd9Sstevel@tonic-gate 27627c478bd9Sstevel@tonic-gate static void 27637c478bd9Sstevel@tonic-gate set_logindev_perms(char *devlink) 27647c478bd9Sstevel@tonic-gate { 27657c478bd9Sstevel@tonic-gate struct login_dev *newdev; 27667c478bd9Sstevel@tonic-gate struct passwd pwd, *resp; 27677c478bd9Sstevel@tonic-gate char pwd_buf[PATH_MAX]; 27687c478bd9Sstevel@tonic-gate int rv; 27697c478bd9Sstevel@tonic-gate struct stat sb; 27707c478bd9Sstevel@tonic-gate char *devfs_path = NULL; 27717c478bd9Sstevel@tonic-gate 27727c478bd9Sstevel@tonic-gate /* 27737c478bd9Sstevel@tonic-gate * We only want logindev perms to be set when a device is 27747c478bd9Sstevel@tonic-gate * hotplugged or an application requests synchronous creates. 27757c478bd9Sstevel@tonic-gate * So we enable this only in daemon mode. In addition, 27767c478bd9Sstevel@tonic-gate * login(1) only fixes the std. /dev dir. So we don't 27777c478bd9Sstevel@tonic-gate * change perms if alternate root is set. 27787c478bd9Sstevel@tonic-gate * login_dev_enable is TRUE only in these cases. 27797c478bd9Sstevel@tonic-gate */ 27807c478bd9Sstevel@tonic-gate if (login_dev_enable != TRUE) 27817c478bd9Sstevel@tonic-gate return; 27827c478bd9Sstevel@tonic-gate 27837c478bd9Sstevel@tonic-gate /* 27847c478bd9Sstevel@tonic-gate * Normally, /etc/logindevperm has few (8 - 10 entries) which 27857c478bd9Sstevel@tonic-gate * may be regular expressions (globs were converted to RE). 27867c478bd9Sstevel@tonic-gate * So just do a linear search through the list. 27877c478bd9Sstevel@tonic-gate */ 27887c478bd9Sstevel@tonic-gate for (newdev = login_dev_cache; newdev; newdev = newdev->ldev_next) { 27897c478bd9Sstevel@tonic-gate vprint(FILES_MID, "matching %s with %s\n", devlink, 27907c478bd9Sstevel@tonic-gate newdev->ldev_device); 27917c478bd9Sstevel@tonic-gate 27927c478bd9Sstevel@tonic-gate if (regexec(&newdev->ldev_device_regex, devlink, 0, 27937c478bd9Sstevel@tonic-gate NULL, 0) == 0) { 27947c478bd9Sstevel@tonic-gate vprint(FILES_MID, "matched %s with %s\n", devlink, 27957c478bd9Sstevel@tonic-gate newdev->ldev_device); 27967c478bd9Sstevel@tonic-gate break; 27977c478bd9Sstevel@tonic-gate } 27987c478bd9Sstevel@tonic-gate } 27997c478bd9Sstevel@tonic-gate 28007c478bd9Sstevel@tonic-gate if (newdev == NULL) 28017c478bd9Sstevel@tonic-gate return; 28027c478bd9Sstevel@tonic-gate 28037c478bd9Sstevel@tonic-gate /* 28047c478bd9Sstevel@tonic-gate * we have a match, now find the driver associated with this 28057c478bd9Sstevel@tonic-gate * minor node using a snapshot on the physical path 28067c478bd9Sstevel@tonic-gate */ 28077c478bd9Sstevel@tonic-gate (void) resolve_link(devlink, NULL, NULL, &devfs_path, 0); 28087c478bd9Sstevel@tonic-gate if (devfs_path) { 28097c478bd9Sstevel@tonic-gate di_node_t node; 28107c478bd9Sstevel@tonic-gate char *drv = NULL; 28117c478bd9Sstevel@tonic-gate struct driver_list *list; 28127c478bd9Sstevel@tonic-gate char *p; 28137c478bd9Sstevel@tonic-gate 28147c478bd9Sstevel@tonic-gate /* truncate on : so we can take a snapshot */ 28157c478bd9Sstevel@tonic-gate (void) strcpy(pwd_buf, devfs_path); 28167c478bd9Sstevel@tonic-gate p = strrchr(pwd_buf, ':'); 28177c478bd9Sstevel@tonic-gate if (p == NULL) { 28187c478bd9Sstevel@tonic-gate free(devfs_path); 28197c478bd9Sstevel@tonic-gate return; 28207c478bd9Sstevel@tonic-gate } 28217c478bd9Sstevel@tonic-gate *p = '\0'; 28227c478bd9Sstevel@tonic-gate 28237c478bd9Sstevel@tonic-gate vprint(FILES_MID, "link=%s->physpath=%s\n", 28247c478bd9Sstevel@tonic-gate devlink, pwd_buf); 28257c478bd9Sstevel@tonic-gate 28267c478bd9Sstevel@tonic-gate node = di_init(pwd_buf, DINFOMINOR); 28277c478bd9Sstevel@tonic-gate 28287c478bd9Sstevel@tonic-gate if (node) { 28297c478bd9Sstevel@tonic-gate drv = di_driver_name(node); 28307c478bd9Sstevel@tonic-gate 28317c478bd9Sstevel@tonic-gate if (drv) { 28327c478bd9Sstevel@tonic-gate vprint(FILES_MID, "%s: driver is %s\n", 28337c478bd9Sstevel@tonic-gate devlink, drv); 28347c478bd9Sstevel@tonic-gate } 28357c478bd9Sstevel@tonic-gate di_fini(node); 28367c478bd9Sstevel@tonic-gate } 28377c478bd9Sstevel@tonic-gate /* search thru the driver list specified in logindevperm */ 28387c478bd9Sstevel@tonic-gate list = newdev->ldev_driver_list; 28397c478bd9Sstevel@tonic-gate if ((drv != NULL) && (list != NULL)) { 28407c478bd9Sstevel@tonic-gate while (list) { 28417c478bd9Sstevel@tonic-gate if (strcmp(list->driver_name, 28427c478bd9Sstevel@tonic-gate drv) == 0) { 28437c478bd9Sstevel@tonic-gate vprint(FILES_MID, 28447c478bd9Sstevel@tonic-gate "driver %s match!\n", drv); 28457c478bd9Sstevel@tonic-gate break; 28467c478bd9Sstevel@tonic-gate } 28477c478bd9Sstevel@tonic-gate list = list->next; 28487c478bd9Sstevel@tonic-gate } 28497c478bd9Sstevel@tonic-gate if (list == NULL) { 28507c478bd9Sstevel@tonic-gate vprint(FILES_MID, "no driver match!\n"); 28517c478bd9Sstevel@tonic-gate free(devfs_path); 28527c478bd9Sstevel@tonic-gate return; 28537c478bd9Sstevel@tonic-gate } 28547c478bd9Sstevel@tonic-gate } 28557c478bd9Sstevel@tonic-gate free(devfs_path); 28567c478bd9Sstevel@tonic-gate } else { 28577c478bd9Sstevel@tonic-gate return; 28587c478bd9Sstevel@tonic-gate } 28597c478bd9Sstevel@tonic-gate 28607c478bd9Sstevel@tonic-gate vprint(FILES_MID, "changing permissions of %s\n", devlink); 28617c478bd9Sstevel@tonic-gate 28627c478bd9Sstevel@tonic-gate /* 28637c478bd9Sstevel@tonic-gate * We have a match. We now attempt to determine the 28647c478bd9Sstevel@tonic-gate * owner and group of the console user. 28657c478bd9Sstevel@tonic-gate * 28667c478bd9Sstevel@tonic-gate * stat() the console device newdev->ldev_console 28677c478bd9Sstevel@tonic-gate * which will always exist - it will have the right owner but 28687c478bd9Sstevel@tonic-gate * not the right group. Use getpwuid_r() to determine group for this 28697c478bd9Sstevel@tonic-gate * uid. 28707c478bd9Sstevel@tonic-gate * Note, it is safe to use name service here since if name services 28717c478bd9Sstevel@tonic-gate * are not available (during boot or in single-user mode), then 28727c478bd9Sstevel@tonic-gate * console owner will be root and its gid can be found in 28737c478bd9Sstevel@tonic-gate * local files. 28747c478bd9Sstevel@tonic-gate */ 28757c478bd9Sstevel@tonic-gate if (stat(newdev->ldev_console, &sb) == -1) { 28767c478bd9Sstevel@tonic-gate vprint(VERBOSE_MID, STAT_FAILED, newdev->ldev_console, 28777c478bd9Sstevel@tonic-gate strerror(errno)); 28787c478bd9Sstevel@tonic-gate return; 28797c478bd9Sstevel@tonic-gate } 28807c478bd9Sstevel@tonic-gate 28817c478bd9Sstevel@tonic-gate resp = NULL; 28827c478bd9Sstevel@tonic-gate rv = getpwuid_r(sb.st_uid, &pwd, pwd_buf, sizeof (pwd_buf), &resp); 28837c478bd9Sstevel@tonic-gate if (rv || resp == NULL) { 28847c478bd9Sstevel@tonic-gate rv = rv ? rv : EINVAL; 28857c478bd9Sstevel@tonic-gate vprint(VERBOSE_MID, GID_FAILED, sb.st_uid, 28867c478bd9Sstevel@tonic-gate strerror(rv)); 28877c478bd9Sstevel@tonic-gate return; 28887c478bd9Sstevel@tonic-gate } 28897c478bd9Sstevel@tonic-gate 28907c478bd9Sstevel@tonic-gate assert(&pwd == resp); 28917c478bd9Sstevel@tonic-gate 28927c478bd9Sstevel@tonic-gate sb.st_gid = resp->pw_gid; 28937c478bd9Sstevel@tonic-gate 28947c478bd9Sstevel@tonic-gate if (chmod(devlink, newdev->ldev_perms) == -1) { 28957c478bd9Sstevel@tonic-gate vprint(VERBOSE_MID, CHMOD_FAILED, devlink, 28967c478bd9Sstevel@tonic-gate strerror(errno)); 28977c478bd9Sstevel@tonic-gate return; 28987c478bd9Sstevel@tonic-gate } 28997c478bd9Sstevel@tonic-gate 29007c478bd9Sstevel@tonic-gate if (chown(devlink, sb.st_uid, sb.st_gid) == -1) { 29017c478bd9Sstevel@tonic-gate vprint(VERBOSE_MID, CHOWN_FAILED, devlink, 29027c478bd9Sstevel@tonic-gate strerror(errno)); 29037c478bd9Sstevel@tonic-gate } 29047c478bd9Sstevel@tonic-gate } 29057c478bd9Sstevel@tonic-gate 29067c478bd9Sstevel@tonic-gate /* 29077c478bd9Sstevel@tonic-gate * Reset /devices node with appropriate permissions and 29087c478bd9Sstevel@tonic-gate * ownership as specified in /etc/minor_perm. 29097c478bd9Sstevel@tonic-gate */ 29107c478bd9Sstevel@tonic-gate static void 29117c478bd9Sstevel@tonic-gate reset_node_permissions(di_node_t node, di_minor_t minor) 29127c478bd9Sstevel@tonic-gate { 29137c478bd9Sstevel@tonic-gate int spectype; 29147c478bd9Sstevel@tonic-gate char phy_path[PATH_MAX + 1]; 29157c478bd9Sstevel@tonic-gate mode_t mode; 29167c478bd9Sstevel@tonic-gate dev_t dev; 29177c478bd9Sstevel@tonic-gate uid_t uid; 29187c478bd9Sstevel@tonic-gate gid_t gid; 29197c478bd9Sstevel@tonic-gate struct stat sb; 29207c478bd9Sstevel@tonic-gate char *dev_path, *aminor = NULL; 29217c478bd9Sstevel@tonic-gate 29227c478bd9Sstevel@tonic-gate /* lphy_path starts with / */ 29237c478bd9Sstevel@tonic-gate if ((dev_path = di_devfs_path(node)) == NULL) { 29247c478bd9Sstevel@tonic-gate err_print(DI_DEVFS_PATH_FAILED, strerror(errno)); 29257c478bd9Sstevel@tonic-gate devfsadm_exit(1); 29267c478bd9Sstevel@tonic-gate } 29277c478bd9Sstevel@tonic-gate (void) strcpy(lphy_path, dev_path); 29287c478bd9Sstevel@tonic-gate di_devfs_path_free(dev_path); 29297c478bd9Sstevel@tonic-gate 29307c478bd9Sstevel@tonic-gate (void) strcat(lphy_path, ":"); 29317c478bd9Sstevel@tonic-gate if (di_minor_type(minor) == DDM_ALIAS) { 29327c478bd9Sstevel@tonic-gate char *driver; 29337c478bd9Sstevel@tonic-gate aminor = di_minor_name(minor); 29347c478bd9Sstevel@tonic-gate driver = di_driver_name(di_minor_devinfo(minor)); 29357c478bd9Sstevel@tonic-gate (void) strcat(lphy_path, driver); 29367c478bd9Sstevel@tonic-gate } else 29377c478bd9Sstevel@tonic-gate (void) strcat(lphy_path, di_minor_name(minor)); 29387c478bd9Sstevel@tonic-gate 29397c478bd9Sstevel@tonic-gate (void) strcpy(phy_path, devices_dir); 29407c478bd9Sstevel@tonic-gate (void) strcat(phy_path, lphy_path); 29417c478bd9Sstevel@tonic-gate 29427c478bd9Sstevel@tonic-gate lnode = node; 29437c478bd9Sstevel@tonic-gate lminor = minor; 29447c478bd9Sstevel@tonic-gate 29457c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "reset_node_permissions: phy_path=%s lphy_path=%s\n", 29467c478bd9Sstevel@tonic-gate phy_path, lphy_path); 29477c478bd9Sstevel@tonic-gate 29487c478bd9Sstevel@tonic-gate dev = di_minor_devt(minor); 29497c478bd9Sstevel@tonic-gate spectype = di_minor_spectype(minor); /* block or char */ 29507c478bd9Sstevel@tonic-gate 29517c478bd9Sstevel@tonic-gate getattr(phy_path, aminor, spectype, dev, &mode, &uid, &gid); 29527c478bd9Sstevel@tonic-gate 29537c478bd9Sstevel@tonic-gate /* 29547c478bd9Sstevel@tonic-gate * compare and set permissions and ownership 29557c478bd9Sstevel@tonic-gate * 29567c478bd9Sstevel@tonic-gate * Under devfs, a quick insertion and removal of USB devices 29577c478bd9Sstevel@tonic-gate * would cause stat of physical path to fail. In this case, 29587c478bd9Sstevel@tonic-gate * we emit a verbose message, but don't print errors. 29597c478bd9Sstevel@tonic-gate */ 29607c478bd9Sstevel@tonic-gate if ((stat(phy_path, &sb) == -1) || (sb.st_rdev != dev)) { 29617c478bd9Sstevel@tonic-gate vprint(VERBOSE_MID, NO_DEVFS_NODE, phy_path); 29627c478bd9Sstevel@tonic-gate return; 29637c478bd9Sstevel@tonic-gate } 29647c478bd9Sstevel@tonic-gate 29657c478bd9Sstevel@tonic-gate /* 296645916cd2Sjpk * If we are here for a new device 296745916cd2Sjpk * If device allocation is on 296845916cd2Sjpk * then 296945916cd2Sjpk * set ownership to root:other and permissions to 0000 297045916cd2Sjpk * else 297145916cd2Sjpk * set ownership and permissions as specified in minor_perm 297245916cd2Sjpk * If we are here for an existing device 297345916cd2Sjpk * If device allocation is to be turned on 297445916cd2Sjpk * then 297545916cd2Sjpk * reset ownership to root:other and permissions to 0000 297645916cd2Sjpk * else if device allocation is to be turned off 297745916cd2Sjpk * reset ownership and permissions to those specified in 297845916cd2Sjpk * minor_perm 297945916cd2Sjpk * else 2980ff2aee48Scth * preserve existing/user-modified ownership and 298145916cd2Sjpk * permissions 298245916cd2Sjpk * 298345916cd2Sjpk * devfs indicates a new device by faking access time to be zero. 29847c478bd9Sstevel@tonic-gate */ 29857c478bd9Sstevel@tonic-gate if (sb.st_atime != 0) { 29867c478bd9Sstevel@tonic-gate int i; 29877c478bd9Sstevel@tonic-gate char *nt; 29887c478bd9Sstevel@tonic-gate 298945916cd2Sjpk if ((devalloc_flag == 0) && (devalloc_is_on != 1)) 299045916cd2Sjpk /* 299145916cd2Sjpk * Leave existing devices as they are if we are not 299245916cd2Sjpk * turning device allocation on/off. 299345916cd2Sjpk */ 29947c478bd9Sstevel@tonic-gate return; 29957c478bd9Sstevel@tonic-gate 29967c478bd9Sstevel@tonic-gate nt = di_minor_nodetype(minor); 299745916cd2Sjpk 29987c478bd9Sstevel@tonic-gate if (nt == NULL) 29997c478bd9Sstevel@tonic-gate return; 300045916cd2Sjpk 300145916cd2Sjpk for (i = 0; devalloc_list[i]; i++) { 300245916cd2Sjpk if (strcmp(nt, devalloc_list[i]) == 0) 300345916cd2Sjpk /* 300445916cd2Sjpk * One of the types recognized by devalloc, 300545916cd2Sjpk * reset attrs. 300645916cd2Sjpk */ 30077c478bd9Sstevel@tonic-gate break; 30087c478bd9Sstevel@tonic-gate } 300945916cd2Sjpk if (devalloc_list[i] == NULL) 30107c478bd9Sstevel@tonic-gate return; 30117c478bd9Sstevel@tonic-gate } 30127c478bd9Sstevel@tonic-gate 30137c478bd9Sstevel@tonic-gate if (file_mods == FALSE) { 30147c478bd9Sstevel@tonic-gate /* Nothing more to do if simulating */ 30157c478bd9Sstevel@tonic-gate vprint(VERBOSE_MID, PERM_MSG, phy_path, uid, gid, mode); 30167c478bd9Sstevel@tonic-gate return; 30177c478bd9Sstevel@tonic-gate } 30187c478bd9Sstevel@tonic-gate 301945916cd2Sjpk if ((devalloc_flag == DA_ON) || (devalloc_is_on == 1)) { 302045916cd2Sjpk /* 302145916cd2Sjpk * we are here either to turn device allocation on 302245916cd2Sjpk * or to add a new device while device allocation in on 302345916cd2Sjpk */ 302445916cd2Sjpk mode = DEALLOC_MODE; 302545916cd2Sjpk uid = DA_UID; 302645916cd2Sjpk gid = DA_GID; 302745916cd2Sjpk } 302845916cd2Sjpk 302945916cd2Sjpk if ((devalloc_is_on == 1) || (devalloc_flag == DA_ON) || 303045916cd2Sjpk (sb.st_mode != mode)) { 30317c478bd9Sstevel@tonic-gate if (chmod(phy_path, mode) == -1) 30327c478bd9Sstevel@tonic-gate vprint(VERBOSE_MID, CHMOD_FAILED, 30337c478bd9Sstevel@tonic-gate phy_path, strerror(errno)); 30347c478bd9Sstevel@tonic-gate } 303545916cd2Sjpk if ((devalloc_is_on == 1) || (devalloc_flag == DA_ON) || 303645916cd2Sjpk (sb.st_uid != uid || sb.st_gid != gid)) { 30377c478bd9Sstevel@tonic-gate if (chown(phy_path, uid, gid) == -1) 30387c478bd9Sstevel@tonic-gate vprint(VERBOSE_MID, CHOWN_FAILED, 30397c478bd9Sstevel@tonic-gate phy_path, strerror(errno)); 30407c478bd9Sstevel@tonic-gate } 30417c478bd9Sstevel@tonic-gate 30427c478bd9Sstevel@tonic-gate /* Report that we actually did something */ 30437c478bd9Sstevel@tonic-gate vprint(VERBOSE_MID, PERM_MSG, phy_path, uid, gid, mode); 30447c478bd9Sstevel@tonic-gate } 30457c478bd9Sstevel@tonic-gate 30467c478bd9Sstevel@tonic-gate /* 30477c478bd9Sstevel@tonic-gate * Removes logical link and the minor node it refers to. If file is a 30487c478bd9Sstevel@tonic-gate * link, we recurse and try to remove the minor node (or link if path is 30497c478bd9Sstevel@tonic-gate * a double link) that file's link contents refer to. 30507c478bd9Sstevel@tonic-gate */ 30517c478bd9Sstevel@tonic-gate static void 30527c478bd9Sstevel@tonic-gate devfsadm_rm_work(char *file, int recurse, int file_type) 30537c478bd9Sstevel@tonic-gate { 30547c478bd9Sstevel@tonic-gate char *fcn = "devfsadm_rm_work: "; 30557c478bd9Sstevel@tonic-gate int linksize; 30567c478bd9Sstevel@tonic-gate char contents[PATH_MAX + 1]; 30577c478bd9Sstevel@tonic-gate char nextfile[PATH_MAX + 1]; 30587c478bd9Sstevel@tonic-gate char newfile[PATH_MAX + 1]; 30597c478bd9Sstevel@tonic-gate char *ptr; 30607c478bd9Sstevel@tonic-gate 30617c478bd9Sstevel@tonic-gate vprint(REMOVE_MID, "%s%s\n", fcn, file); 30627c478bd9Sstevel@tonic-gate 30637c478bd9Sstevel@tonic-gate /* TYPE_LINK split into multiple if's due to excessive indentations */ 30647c478bd9Sstevel@tonic-gate if (file_type == TYPE_LINK) { 30657c478bd9Sstevel@tonic-gate (void) strcpy(newfile, dev_dir); 30667c478bd9Sstevel@tonic-gate (void) strcat(newfile, "/"); 30677c478bd9Sstevel@tonic-gate (void) strcat(newfile, file); 30687c478bd9Sstevel@tonic-gate } 30697c478bd9Sstevel@tonic-gate 30707c478bd9Sstevel@tonic-gate if ((file_type == TYPE_LINK) && (recurse == TRUE) && 30717c478bd9Sstevel@tonic-gate ((linksize = readlink(newfile, contents, PATH_MAX)) > 0)) { 30727c478bd9Sstevel@tonic-gate contents[linksize] = '\0'; 30737c478bd9Sstevel@tonic-gate 30747c478bd9Sstevel@tonic-gate if (is_minor_node(contents, &ptr) == DEVFSADM_TRUE) { 30757c478bd9Sstevel@tonic-gate devfsadm_rm_work(++ptr, FALSE, TYPE_DEVICES); 30767c478bd9Sstevel@tonic-gate } else { 30777c478bd9Sstevel@tonic-gate if (strncmp(contents, DEV "/", strlen(DEV) + 1) == 0) { 30787c478bd9Sstevel@tonic-gate devfsadm_rm_work(&contents[strlen(DEV) + 1], 30797c478bd9Sstevel@tonic-gate TRUE, TYPE_LINK); 30807c478bd9Sstevel@tonic-gate } else { 30817c478bd9Sstevel@tonic-gate if ((ptr = strrchr(file, '/')) != NULL) { 30827c478bd9Sstevel@tonic-gate *ptr = '\0'; 30837c478bd9Sstevel@tonic-gate (void) strcpy(nextfile, file); 30847c478bd9Sstevel@tonic-gate *ptr = '/'; 30857c478bd9Sstevel@tonic-gate (void) strcat(nextfile, "/"); 30867c478bd9Sstevel@tonic-gate } else { 30877c478bd9Sstevel@tonic-gate (void) strcpy(nextfile, ""); 30887c478bd9Sstevel@tonic-gate } 30897c478bd9Sstevel@tonic-gate (void) strcat(nextfile, contents); 30907c478bd9Sstevel@tonic-gate devfsadm_rm_work(nextfile, TRUE, TYPE_LINK); 30917c478bd9Sstevel@tonic-gate } 30927c478bd9Sstevel@tonic-gate } 30937c478bd9Sstevel@tonic-gate } 30947c478bd9Sstevel@tonic-gate 30957c478bd9Sstevel@tonic-gate if (file_type == TYPE_LINK) { 30967c478bd9Sstevel@tonic-gate vprint(VERBOSE_MID, DEVFSADM_UNLINK, newfile); 30977c478bd9Sstevel@tonic-gate if (file_mods == TRUE) { 30987c478bd9Sstevel@tonic-gate rm_link_from_cache(file); 30997c478bd9Sstevel@tonic-gate s_unlink(newfile); 31007c478bd9Sstevel@tonic-gate rm_parent_dir_if_empty(newfile); 31017c478bd9Sstevel@tonic-gate invalidate_enumerate_cache(); 31027c478bd9Sstevel@tonic-gate (void) di_devlink_rm_link(devlink_cache, file); 31037c478bd9Sstevel@tonic-gate } 31047c478bd9Sstevel@tonic-gate } 31057c478bd9Sstevel@tonic-gate 31067c478bd9Sstevel@tonic-gate /* 31077c478bd9Sstevel@tonic-gate * Note: we don't remove /devices entries because they are 31087c478bd9Sstevel@tonic-gate * covered by devfs. 31097c478bd9Sstevel@tonic-gate */ 31107c478bd9Sstevel@tonic-gate } 31117c478bd9Sstevel@tonic-gate 31127c478bd9Sstevel@tonic-gate void 31137c478bd9Sstevel@tonic-gate devfsadm_rm_link(char *file) 31147c478bd9Sstevel@tonic-gate { 31157c478bd9Sstevel@tonic-gate devfsadm_rm_work(file, FALSE, TYPE_LINK); 31167c478bd9Sstevel@tonic-gate } 31177c478bd9Sstevel@tonic-gate 31187c478bd9Sstevel@tonic-gate void 31197c478bd9Sstevel@tonic-gate devfsadm_rm_all(char *file) 31207c478bd9Sstevel@tonic-gate { 31217c478bd9Sstevel@tonic-gate devfsadm_rm_work(file, TRUE, TYPE_LINK); 31227c478bd9Sstevel@tonic-gate } 31237c478bd9Sstevel@tonic-gate 31244bc0a2efScasper static int 31257c478bd9Sstevel@tonic-gate s_rmdir(char *path) 31267c478bd9Sstevel@tonic-gate { 31277c478bd9Sstevel@tonic-gate int i; 31287c478bd9Sstevel@tonic-gate char *rpath, *dir; 31297c478bd9Sstevel@tonic-gate const char *fcn = "s_rmdir"; 31307c478bd9Sstevel@tonic-gate 31317c478bd9Sstevel@tonic-gate /* 31327c478bd9Sstevel@tonic-gate * Certain directories are created at install time by packages. 31337c478bd9Sstevel@tonic-gate * Some of them (listed in packaged_dirs[]) are required by apps 31347c478bd9Sstevel@tonic-gate * and need to be present even when empty. 31357c478bd9Sstevel@tonic-gate */ 31367c478bd9Sstevel@tonic-gate vprint(REMOVE_MID, "%s: checking if %s is packaged\n", fcn, path); 31377c478bd9Sstevel@tonic-gate 31387c478bd9Sstevel@tonic-gate rpath = path + strlen(dev_dir) + 1; 31397c478bd9Sstevel@tonic-gate 31407c478bd9Sstevel@tonic-gate for (i = 0; (dir = packaged_dirs[i]) != NULL; i++) { 31417c478bd9Sstevel@tonic-gate if (*rpath == *dir) { 31427c478bd9Sstevel@tonic-gate if (strcmp(rpath, dir) == 0) { 31437c478bd9Sstevel@tonic-gate vprint(REMOVE_MID, "%s: skipping packaged dir: " 31447c478bd9Sstevel@tonic-gate "%s\n", fcn, path); 31454bc0a2efScasper errno = EEXIST; 31464bc0a2efScasper return (-1); 31477c478bd9Sstevel@tonic-gate } 31487c478bd9Sstevel@tonic-gate } 31497c478bd9Sstevel@tonic-gate } 31507c478bd9Sstevel@tonic-gate 31514bc0a2efScasper return (rmdir(path)); 31527c478bd9Sstevel@tonic-gate } 31537c478bd9Sstevel@tonic-gate 31547c478bd9Sstevel@tonic-gate /* 31557c478bd9Sstevel@tonic-gate * Try to remove any empty directories up the tree. It is assumed that 31567c478bd9Sstevel@tonic-gate * pathname is a file that was removed, so start with its parent, and 31577c478bd9Sstevel@tonic-gate * work up the tree. 31587c478bd9Sstevel@tonic-gate */ 31597c478bd9Sstevel@tonic-gate static void 31607c478bd9Sstevel@tonic-gate rm_parent_dir_if_empty(char *pathname) 31617c478bd9Sstevel@tonic-gate { 31627c478bd9Sstevel@tonic-gate char *ptr, path[PATH_MAX + 1]; 31637c478bd9Sstevel@tonic-gate char *fcn = "rm_parent_dir_if_empty: "; 31647c478bd9Sstevel@tonic-gate 31657c478bd9Sstevel@tonic-gate vprint(REMOVE_MID, "%schecking %s if empty\n", fcn, pathname); 31667c478bd9Sstevel@tonic-gate 31677c478bd9Sstevel@tonic-gate (void) strcpy(path, pathname); 31687c478bd9Sstevel@tonic-gate 31697c478bd9Sstevel@tonic-gate /* 31707c478bd9Sstevel@tonic-gate * ascend up the dir tree, deleting all empty dirs. 31717c478bd9Sstevel@tonic-gate * Return immediately if a dir is not empty. 31727c478bd9Sstevel@tonic-gate */ 31737c478bd9Sstevel@tonic-gate for (;;) { 31747c478bd9Sstevel@tonic-gate 31757c478bd9Sstevel@tonic-gate if ((ptr = strrchr(path, '/')) == NULL) { 31767c478bd9Sstevel@tonic-gate return; 31777c478bd9Sstevel@tonic-gate } 31787c478bd9Sstevel@tonic-gate 31797c478bd9Sstevel@tonic-gate *ptr = '\0'; 31807c478bd9Sstevel@tonic-gate 3181e37c6c37Scth if (finddev_emptydir(path)) { 3182e37c6c37Scth /* directory is empty */ 3183facf4a8dSllai1 if (s_rmdir(path) == 0) { 3184facf4a8dSllai1 vprint(REMOVE_MID, 3185facf4a8dSllai1 "%sremoving empty dir %s\n", fcn, path); 3186facf4a8dSllai1 } else if (errno == EEXIST) { 3187facf4a8dSllai1 vprint(REMOVE_MID, 3188facf4a8dSllai1 "%sfailed to remove dir: %s\n", fcn, path); 3189facf4a8dSllai1 return; 3190facf4a8dSllai1 } 3191facf4a8dSllai1 } else { 3192facf4a8dSllai1 /* some other file is here, so return */ 31937c478bd9Sstevel@tonic-gate vprint(REMOVE_MID, "%sdir not empty: %s\n", fcn, path); 31947c478bd9Sstevel@tonic-gate return; 31957c478bd9Sstevel@tonic-gate } 31967c478bd9Sstevel@tonic-gate } 31977c478bd9Sstevel@tonic-gate } 31987c478bd9Sstevel@tonic-gate 31997c478bd9Sstevel@tonic-gate /* 32007c478bd9Sstevel@tonic-gate * This function and all the functions it calls below were added to 32017c478bd9Sstevel@tonic-gate * handle the unique problem with world wide names (WWN). The problem is 32027c478bd9Sstevel@tonic-gate * that if a WWN device is moved to another address on the same controller 32037c478bd9Sstevel@tonic-gate * its logical link will change, while the physical node remains the same. 32047c478bd9Sstevel@tonic-gate * The result is that two logical links will point to the same physical path 32057c478bd9Sstevel@tonic-gate * in /devices, the valid link and a stale link. This function will 32067c478bd9Sstevel@tonic-gate * find all the stale nodes, though at a significant performance cost. 32077c478bd9Sstevel@tonic-gate * 32087c478bd9Sstevel@tonic-gate * Caching is used to increase performance. 32097c478bd9Sstevel@tonic-gate * A cache will be built from disk if the cache tag doesn't already exist. 32107c478bd9Sstevel@tonic-gate * The cache tag is a regular expression "dir_re", which selects a 32117c478bd9Sstevel@tonic-gate * subset of disks to search from typically something like 32127c478bd9Sstevel@tonic-gate * "dev/cXt[0-9]+d[0-9]+s[0-9]+". After the cache is built, consistency must 32137c478bd9Sstevel@tonic-gate * be maintained, so entries are added as new links are created, and removed 32147c478bd9Sstevel@tonic-gate * as old links are deleted. The whole cache is flushed if we are a daemon, 32157c478bd9Sstevel@tonic-gate * and another devfsadm process ran in between. 32167c478bd9Sstevel@tonic-gate * 32177c478bd9Sstevel@tonic-gate * Once the cache is built, this function finds the cache which matches 32187c478bd9Sstevel@tonic-gate * dir_re, and then it searches all links in that cache looking for 32197c478bd9Sstevel@tonic-gate * any link whose contents match "valid_link_contents" with a corresponding link 32207c478bd9Sstevel@tonic-gate * which does not match "valid_link". Any such matches are stale and removed. 32217c478bd9Sstevel@tonic-gate */ 32227c478bd9Sstevel@tonic-gate void 32237c478bd9Sstevel@tonic-gate devfsadm_rm_stale_links(char *dir_re, char *valid_link, di_node_t node, 32247c478bd9Sstevel@tonic-gate di_minor_t minor) 32257c478bd9Sstevel@tonic-gate { 32267c478bd9Sstevel@tonic-gate link_t *link; 32277c478bd9Sstevel@tonic-gate linkhead_t *head; 32287c478bd9Sstevel@tonic-gate char phy_path[PATH_MAX + 1]; 32297c478bd9Sstevel@tonic-gate char *valid_link_contents; 32307c478bd9Sstevel@tonic-gate char *dev_path; 32317c478bd9Sstevel@tonic-gate char rmlink[PATH_MAX + 1]; 32327c478bd9Sstevel@tonic-gate 32337c478bd9Sstevel@tonic-gate /* 32347c478bd9Sstevel@tonic-gate * try to use devices path 32357c478bd9Sstevel@tonic-gate */ 32367c478bd9Sstevel@tonic-gate if ((node == lnode) && (minor == lminor)) { 32377c478bd9Sstevel@tonic-gate valid_link_contents = lphy_path; 32387c478bd9Sstevel@tonic-gate } else { 32397c478bd9Sstevel@tonic-gate if ((dev_path = di_devfs_path(node)) == NULL) { 32407c478bd9Sstevel@tonic-gate err_print(DI_DEVFS_PATH_FAILED, strerror(errno)); 32417c478bd9Sstevel@tonic-gate devfsadm_exit(1); 32427c478bd9Sstevel@tonic-gate } 32437c478bd9Sstevel@tonic-gate (void) strcpy(phy_path, dev_path); 32447c478bd9Sstevel@tonic-gate di_devfs_path_free(dev_path); 32457c478bd9Sstevel@tonic-gate 32467c478bd9Sstevel@tonic-gate (void) strcat(phy_path, ":"); 32477c478bd9Sstevel@tonic-gate (void) strcat(phy_path, di_minor_name(minor)); 32487c478bd9Sstevel@tonic-gate valid_link_contents = phy_path; 32497c478bd9Sstevel@tonic-gate } 32507c478bd9Sstevel@tonic-gate 32517c478bd9Sstevel@tonic-gate /* 32527c478bd9Sstevel@tonic-gate * As an optimization, check to make sure the corresponding 32537c478bd9Sstevel@tonic-gate * devlink was just created before continuing. 32547c478bd9Sstevel@tonic-gate */ 32557c478bd9Sstevel@tonic-gate 32567c478bd9Sstevel@tonic-gate if (linknew == FALSE) { 32577c478bd9Sstevel@tonic-gate return; 32587c478bd9Sstevel@tonic-gate } 32597c478bd9Sstevel@tonic-gate 32607c478bd9Sstevel@tonic-gate head = get_cached_links(dir_re); 32617c478bd9Sstevel@tonic-gate 32627c478bd9Sstevel@tonic-gate assert(head->nextlink == NULL); 32637c478bd9Sstevel@tonic-gate 32647c478bd9Sstevel@tonic-gate for (link = head->link; link != NULL; link = head->nextlink) { 32657c478bd9Sstevel@tonic-gate /* 32667c478bd9Sstevel@tonic-gate * See hot_cleanup() for why we do this 32677c478bd9Sstevel@tonic-gate */ 32687c478bd9Sstevel@tonic-gate head->nextlink = link->next; 32697c478bd9Sstevel@tonic-gate if ((strcmp(link->contents, valid_link_contents) == 0) && 32707c478bd9Sstevel@tonic-gate (strcmp(link->devlink, valid_link) != 0)) { 32717c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "removing %s -> %s\n" 32727c478bd9Sstevel@tonic-gate "valid link is: %s -> %s\n", 32737c478bd9Sstevel@tonic-gate link->devlink, link->contents, 32747c478bd9Sstevel@tonic-gate valid_link, valid_link_contents); 32757c478bd9Sstevel@tonic-gate /* 32767c478bd9Sstevel@tonic-gate * Use a copy of the cached link name as the 32777c478bd9Sstevel@tonic-gate * cache entry will go away during link removal 32787c478bd9Sstevel@tonic-gate */ 32797c478bd9Sstevel@tonic-gate (void) snprintf(rmlink, sizeof (rmlink), "%s", 32807c478bd9Sstevel@tonic-gate link->devlink); 32817c478bd9Sstevel@tonic-gate devfsadm_rm_link(rmlink); 32827c478bd9Sstevel@tonic-gate } 32837c478bd9Sstevel@tonic-gate } 32847c478bd9Sstevel@tonic-gate } 32857c478bd9Sstevel@tonic-gate 32867c478bd9Sstevel@tonic-gate /* 32877c478bd9Sstevel@tonic-gate * Return previously created cache, or create cache. 32887c478bd9Sstevel@tonic-gate */ 32897c478bd9Sstevel@tonic-gate static linkhead_t * 32907c478bd9Sstevel@tonic-gate get_cached_links(char *dir_re) 32917c478bd9Sstevel@tonic-gate { 32927c478bd9Sstevel@tonic-gate recurse_dev_t rd; 32937c478bd9Sstevel@tonic-gate linkhead_t *linkhead; 32947c478bd9Sstevel@tonic-gate int n; 32957c478bd9Sstevel@tonic-gate 32967c478bd9Sstevel@tonic-gate vprint(BUILDCACHE_MID, "get_cached_links: %s\n", dir_re); 32977c478bd9Sstevel@tonic-gate 32987c478bd9Sstevel@tonic-gate for (linkhead = headlinkhead; linkhead != NULL; 32997c478bd9Sstevel@tonic-gate linkhead = linkhead->nexthead) { 33007c478bd9Sstevel@tonic-gate if (strcmp(linkhead->dir_re, dir_re) == 0) { 33017c478bd9Sstevel@tonic-gate return (linkhead); 33027c478bd9Sstevel@tonic-gate } 33037c478bd9Sstevel@tonic-gate } 33047c478bd9Sstevel@tonic-gate 33057c478bd9Sstevel@tonic-gate /* 33067c478bd9Sstevel@tonic-gate * This tag is not in cache, so add it, along with all its 33077c478bd9Sstevel@tonic-gate * matching /dev entries. This is the only time we go to disk. 33087c478bd9Sstevel@tonic-gate */ 33097c478bd9Sstevel@tonic-gate linkhead = s_malloc(sizeof (linkhead_t)); 33107c478bd9Sstevel@tonic-gate linkhead->nexthead = headlinkhead; 33117c478bd9Sstevel@tonic-gate headlinkhead = linkhead; 33127c478bd9Sstevel@tonic-gate linkhead->dir_re = s_strdup(dir_re); 33137c478bd9Sstevel@tonic-gate 33147c478bd9Sstevel@tonic-gate if ((n = regcomp(&(linkhead->dir_re_compiled), dir_re, 33157c478bd9Sstevel@tonic-gate REG_EXTENDED)) != 0) { 33167c478bd9Sstevel@tonic-gate err_print(REGCOMP_FAILED, dir_re, n); 33177c478bd9Sstevel@tonic-gate } 33187c478bd9Sstevel@tonic-gate 33197c478bd9Sstevel@tonic-gate linkhead->nextlink = NULL; 33207c478bd9Sstevel@tonic-gate linkhead->link = NULL; 33217c478bd9Sstevel@tonic-gate 33227c478bd9Sstevel@tonic-gate rd.fcn = build_devlink_list; 33237c478bd9Sstevel@tonic-gate rd.data = (void *)linkhead; 33247c478bd9Sstevel@tonic-gate 33257c478bd9Sstevel@tonic-gate vprint(BUILDCACHE_MID, "get_cached_links: calling recurse_dev_re\n"); 33267c478bd9Sstevel@tonic-gate 33277c478bd9Sstevel@tonic-gate /* call build_devlink_list for each directory in the dir_re RE */ 33287c478bd9Sstevel@tonic-gate if (dir_re[0] == '/') { 33297c478bd9Sstevel@tonic-gate recurse_dev_re("/", &dir_re[1], &rd); 33307c478bd9Sstevel@tonic-gate } else { 33317c478bd9Sstevel@tonic-gate recurse_dev_re(dev_dir, dir_re, &rd); 33327c478bd9Sstevel@tonic-gate } 33337c478bd9Sstevel@tonic-gate 33347c478bd9Sstevel@tonic-gate return (linkhead); 33357c478bd9Sstevel@tonic-gate } 33367c478bd9Sstevel@tonic-gate 33377c478bd9Sstevel@tonic-gate static void 33387c478bd9Sstevel@tonic-gate build_devlink_list(char *devlink, void *data) 33397c478bd9Sstevel@tonic-gate { 33407c478bd9Sstevel@tonic-gate char *fcn = "build_devlink_list: "; 33417c478bd9Sstevel@tonic-gate char *ptr; 33427c478bd9Sstevel@tonic-gate char *r_contents; 33437c478bd9Sstevel@tonic-gate char *r_devlink; 33447c478bd9Sstevel@tonic-gate char contents[PATH_MAX + 1]; 33457c478bd9Sstevel@tonic-gate char newlink[PATH_MAX + 1]; 33467c478bd9Sstevel@tonic-gate char stage_link[PATH_MAX + 1]; 33477c478bd9Sstevel@tonic-gate int linksize; 33487c478bd9Sstevel@tonic-gate linkhead_t *linkhead = (linkhead_t *)data; 33497c478bd9Sstevel@tonic-gate link_t *link; 33507c478bd9Sstevel@tonic-gate int i = 0; 33517c478bd9Sstevel@tonic-gate 33527c478bd9Sstevel@tonic-gate vprint(BUILDCACHE_MID, "%scheck_link: %s\n", fcn, devlink); 33537c478bd9Sstevel@tonic-gate 33547c478bd9Sstevel@tonic-gate (void) strcpy(newlink, devlink); 33557c478bd9Sstevel@tonic-gate 33567c478bd9Sstevel@tonic-gate do { 33577c478bd9Sstevel@tonic-gate linksize = readlink(newlink, contents, PATH_MAX); 33587c478bd9Sstevel@tonic-gate if (linksize <= 0) { 33597c478bd9Sstevel@tonic-gate /* 33607c478bd9Sstevel@tonic-gate * The first pass through the do loop we may readlink() 33617c478bd9Sstevel@tonic-gate * non-symlink files(EINVAL) from false regexec matches. 33627c478bd9Sstevel@tonic-gate * Suppress error messages in those cases or if the link 33637c478bd9Sstevel@tonic-gate * content is the empty string. 33647c478bd9Sstevel@tonic-gate */ 33657c478bd9Sstevel@tonic-gate if (linksize < 0 && (i || errno != EINVAL)) 33667c478bd9Sstevel@tonic-gate err_print(READLINK_FAILED, "build_devlink_list", 33677c478bd9Sstevel@tonic-gate newlink, strerror(errno)); 33687c478bd9Sstevel@tonic-gate return; 33697c478bd9Sstevel@tonic-gate } 33707c478bd9Sstevel@tonic-gate contents[linksize] = '\0'; 33717c478bd9Sstevel@tonic-gate i = 1; 33727c478bd9Sstevel@tonic-gate 33737c478bd9Sstevel@tonic-gate if (is_minor_node(contents, &r_contents) == DEVFSADM_FALSE) { 33747c478bd9Sstevel@tonic-gate /* 33757c478bd9Sstevel@tonic-gate * assume that link contents is really a pointer to 33767c478bd9Sstevel@tonic-gate * another link, so recurse and read its link contents. 33777c478bd9Sstevel@tonic-gate * 33787c478bd9Sstevel@tonic-gate * some link contents are absolute: 33797c478bd9Sstevel@tonic-gate * /dev/audio -> /dev/sound/0 33807c478bd9Sstevel@tonic-gate */ 33817c478bd9Sstevel@tonic-gate if (strncmp(contents, DEV "/", 33827c478bd9Sstevel@tonic-gate strlen(DEV) + strlen("/")) != 0) { 33837c478bd9Sstevel@tonic-gate 33847c478bd9Sstevel@tonic-gate if ((ptr = strrchr(newlink, '/')) == NULL) { 33857c478bd9Sstevel@tonic-gate vprint(REMOVE_MID, "%s%s -> %s invalid " 33867c478bd9Sstevel@tonic-gate "link. missing '/'\n", fcn, 33877c478bd9Sstevel@tonic-gate newlink, contents); 33887c478bd9Sstevel@tonic-gate return; 33897c478bd9Sstevel@tonic-gate } 33907c478bd9Sstevel@tonic-gate *ptr = '\0'; 33917c478bd9Sstevel@tonic-gate (void) strcpy(stage_link, newlink); 33927c478bd9Sstevel@tonic-gate *ptr = '/'; 33937c478bd9Sstevel@tonic-gate (void) strcat(stage_link, "/"); 33947c478bd9Sstevel@tonic-gate (void) strcat(stage_link, contents); 33957c478bd9Sstevel@tonic-gate (void) strcpy(newlink, stage_link); 33967c478bd9Sstevel@tonic-gate } else { 33977c478bd9Sstevel@tonic-gate (void) strcpy(newlink, dev_dir); 33987c478bd9Sstevel@tonic-gate (void) strcat(newlink, "/"); 33997c478bd9Sstevel@tonic-gate (void) strcat(newlink, 34007c478bd9Sstevel@tonic-gate &contents[strlen(DEV) + strlen("/")]); 34017c478bd9Sstevel@tonic-gate } 34027c478bd9Sstevel@tonic-gate 34037c478bd9Sstevel@tonic-gate } else { 34047c478bd9Sstevel@tonic-gate newlink[0] = '\0'; 34057c478bd9Sstevel@tonic-gate } 34067c478bd9Sstevel@tonic-gate } while (newlink[0] != '\0'); 34077c478bd9Sstevel@tonic-gate 34087c478bd9Sstevel@tonic-gate if (strncmp(devlink, dev_dir, strlen(dev_dir)) != 0) { 34097c478bd9Sstevel@tonic-gate vprint(BUILDCACHE_MID, "%sinvalid link: %s\n", fcn, devlink); 34107c478bd9Sstevel@tonic-gate return; 34117c478bd9Sstevel@tonic-gate } 34127c478bd9Sstevel@tonic-gate 34137c478bd9Sstevel@tonic-gate r_devlink = devlink + strlen(dev_dir); 34147c478bd9Sstevel@tonic-gate 34157c478bd9Sstevel@tonic-gate if (r_devlink[0] != '/') 34167c478bd9Sstevel@tonic-gate return; 34177c478bd9Sstevel@tonic-gate 34187c478bd9Sstevel@tonic-gate link = s_malloc(sizeof (link_t)); 34197c478bd9Sstevel@tonic-gate 34207c478bd9Sstevel@tonic-gate /* don't store the '/' after rootdir/dev */ 34217c478bd9Sstevel@tonic-gate r_devlink += 1; 34227c478bd9Sstevel@tonic-gate 34237c478bd9Sstevel@tonic-gate vprint(BUILDCACHE_MID, "%scaching link: %s\n", fcn, r_devlink); 34247c478bd9Sstevel@tonic-gate link->devlink = s_strdup(r_devlink); 34257c478bd9Sstevel@tonic-gate 34267c478bd9Sstevel@tonic-gate link->contents = s_strdup(r_contents); 34277c478bd9Sstevel@tonic-gate 34287c478bd9Sstevel@tonic-gate link->next = linkhead->link; 34297c478bd9Sstevel@tonic-gate linkhead->link = link; 34307c478bd9Sstevel@tonic-gate } 34317c478bd9Sstevel@tonic-gate 34327c478bd9Sstevel@tonic-gate /* 34337c478bd9Sstevel@tonic-gate * to be consistent, devlink must not begin with / and must be 34347c478bd9Sstevel@tonic-gate * relative to /dev/, whereas physpath must contain / and be 34357c478bd9Sstevel@tonic-gate * relative to /devices. 34367c478bd9Sstevel@tonic-gate */ 34377c478bd9Sstevel@tonic-gate static void 34387c478bd9Sstevel@tonic-gate add_link_to_cache(char *devlink, char *physpath) 34397c478bd9Sstevel@tonic-gate { 34407c478bd9Sstevel@tonic-gate linkhead_t *linkhead; 34417c478bd9Sstevel@tonic-gate link_t *link; 34427c478bd9Sstevel@tonic-gate int added = 0; 34437c478bd9Sstevel@tonic-gate 34447c478bd9Sstevel@tonic-gate if (file_mods == FALSE) { 34457c478bd9Sstevel@tonic-gate return; 34467c478bd9Sstevel@tonic-gate } 34477c478bd9Sstevel@tonic-gate 34487c478bd9Sstevel@tonic-gate vprint(CACHE_MID, "add_link_to_cache: %s -> %s ", 34497c478bd9Sstevel@tonic-gate devlink, physpath); 34507c478bd9Sstevel@tonic-gate 34517c478bd9Sstevel@tonic-gate for (linkhead = headlinkhead; linkhead != NULL; 34527c478bd9Sstevel@tonic-gate linkhead = linkhead->nexthead) { 34537c478bd9Sstevel@tonic-gate if (regexec(&(linkhead->dir_re_compiled), devlink, 0, NULL, 34547c478bd9Sstevel@tonic-gate 0) == 0) { 34557c478bd9Sstevel@tonic-gate added++; 34567c478bd9Sstevel@tonic-gate link = s_malloc(sizeof (link_t)); 34577c478bd9Sstevel@tonic-gate link->devlink = s_strdup(devlink); 34587c478bd9Sstevel@tonic-gate link->contents = s_strdup(physpath); 34597c478bd9Sstevel@tonic-gate link->next = linkhead->link; 34607c478bd9Sstevel@tonic-gate linkhead->link = link; 34617c478bd9Sstevel@tonic-gate } 34627c478bd9Sstevel@tonic-gate } 34637c478bd9Sstevel@tonic-gate 34647c478bd9Sstevel@tonic-gate vprint(CACHE_MID, 34657c478bd9Sstevel@tonic-gate " %d %s\n", added, added == 0 ? "NOT ADDED" : "ADDED"); 34667c478bd9Sstevel@tonic-gate } 34677c478bd9Sstevel@tonic-gate 34687c478bd9Sstevel@tonic-gate /* 34697c478bd9Sstevel@tonic-gate * Remove devlink from cache. Devlink must be relative to /dev/ and not start 34707c478bd9Sstevel@tonic-gate * with /. 34717c478bd9Sstevel@tonic-gate */ 34727c478bd9Sstevel@tonic-gate static void 34737c478bd9Sstevel@tonic-gate rm_link_from_cache(char *devlink) 34747c478bd9Sstevel@tonic-gate { 34757c478bd9Sstevel@tonic-gate linkhead_t *linkhead; 34767c478bd9Sstevel@tonic-gate link_t **linkp; 34777c478bd9Sstevel@tonic-gate link_t *save; 34787c478bd9Sstevel@tonic-gate 34797c478bd9Sstevel@tonic-gate vprint(CACHE_MID, "rm_link_from_cache enter: %s\n", devlink); 34807c478bd9Sstevel@tonic-gate 34817c478bd9Sstevel@tonic-gate for (linkhead = headlinkhead; linkhead != NULL; 34827c478bd9Sstevel@tonic-gate linkhead = linkhead->nexthead) { 34837c478bd9Sstevel@tonic-gate if (regexec(&(linkhead->dir_re_compiled), devlink, 0, NULL, 34847c478bd9Sstevel@tonic-gate 0) == 0) { 34857c478bd9Sstevel@tonic-gate 34867c478bd9Sstevel@tonic-gate for (linkp = &(linkhead->link); *linkp != NULL; ) { 34877c478bd9Sstevel@tonic-gate if ((strcmp((*linkp)->devlink, devlink) == 0)) { 34887c478bd9Sstevel@tonic-gate save = *linkp; 34897c478bd9Sstevel@tonic-gate *linkp = (*linkp)->next; 34907c478bd9Sstevel@tonic-gate /* 34917c478bd9Sstevel@tonic-gate * We are removing our caller's 34927c478bd9Sstevel@tonic-gate * "next" link. Update the nextlink 34937c478bd9Sstevel@tonic-gate * field in the head so that our 34947c478bd9Sstevel@tonic-gate * callers accesses the next valid 34957c478bd9Sstevel@tonic-gate * link 34967c478bd9Sstevel@tonic-gate */ 34977c478bd9Sstevel@tonic-gate if (linkhead->nextlink == save) 34987c478bd9Sstevel@tonic-gate linkhead->nextlink = *linkp; 34997c478bd9Sstevel@tonic-gate free(save->devlink); 35007c478bd9Sstevel@tonic-gate free(save->contents); 35017c478bd9Sstevel@tonic-gate free(save); 35027c478bd9Sstevel@tonic-gate vprint(CACHE_MID, " %s FREED FROM " 35037c478bd9Sstevel@tonic-gate "CACHE\n", devlink); 35047c478bd9Sstevel@tonic-gate } else { 35057c478bd9Sstevel@tonic-gate linkp = &((*linkp)->next); 35067c478bd9Sstevel@tonic-gate } 35077c478bd9Sstevel@tonic-gate } 35087c478bd9Sstevel@tonic-gate } 35097c478bd9Sstevel@tonic-gate } 35107c478bd9Sstevel@tonic-gate } 35117c478bd9Sstevel@tonic-gate 35127c478bd9Sstevel@tonic-gate static void 35137c478bd9Sstevel@tonic-gate rm_all_links_from_cache() 35147c478bd9Sstevel@tonic-gate { 35157c478bd9Sstevel@tonic-gate linkhead_t *linkhead; 35167c478bd9Sstevel@tonic-gate linkhead_t *nextlinkhead; 35177c478bd9Sstevel@tonic-gate link_t *link; 35187c478bd9Sstevel@tonic-gate link_t *nextlink; 35197c478bd9Sstevel@tonic-gate 35207c478bd9Sstevel@tonic-gate vprint(CACHE_MID, "rm_all_links_from_cache\n"); 35217c478bd9Sstevel@tonic-gate 35227c478bd9Sstevel@tonic-gate for (linkhead = headlinkhead; linkhead != NULL; 35237c478bd9Sstevel@tonic-gate linkhead = nextlinkhead) { 35247c478bd9Sstevel@tonic-gate 35257c478bd9Sstevel@tonic-gate nextlinkhead = linkhead->nexthead; 35267c478bd9Sstevel@tonic-gate assert(linkhead->nextlink == NULL); 35277c478bd9Sstevel@tonic-gate for (link = linkhead->link; link != NULL; link = nextlink) { 35287c478bd9Sstevel@tonic-gate nextlink = link->next; 35297c478bd9Sstevel@tonic-gate free(link->devlink); 35307c478bd9Sstevel@tonic-gate free(link->contents); 35317c478bd9Sstevel@tonic-gate free(link); 35327c478bd9Sstevel@tonic-gate } 35337c478bd9Sstevel@tonic-gate regfree(&(linkhead->dir_re_compiled)); 35347c478bd9Sstevel@tonic-gate free(linkhead->dir_re); 35357c478bd9Sstevel@tonic-gate free(linkhead); 35367c478bd9Sstevel@tonic-gate } 35377c478bd9Sstevel@tonic-gate headlinkhead = NULL; 35387c478bd9Sstevel@tonic-gate } 35397c478bd9Sstevel@tonic-gate 35407c478bd9Sstevel@tonic-gate /* 35417c478bd9Sstevel@tonic-gate * Called when the kernel has modified the incore path_to_inst data. This 35427c478bd9Sstevel@tonic-gate * function will schedule a flush of the data to the filesystem. 35437c478bd9Sstevel@tonic-gate */ 35447c478bd9Sstevel@tonic-gate static void 35457c478bd9Sstevel@tonic-gate devfs_instance_mod(void) 35467c478bd9Sstevel@tonic-gate { 35477c478bd9Sstevel@tonic-gate char *fcn = "devfs_instance_mod: "; 35487c478bd9Sstevel@tonic-gate vprint(PATH2INST_MID, "%senter\n", fcn); 35497c478bd9Sstevel@tonic-gate 35507c478bd9Sstevel@tonic-gate /* signal instance thread */ 35517c478bd9Sstevel@tonic-gate (void) mutex_lock(&count_lock); 35527c478bd9Sstevel@tonic-gate inst_count++; 35537c478bd9Sstevel@tonic-gate (void) cond_signal(&cv); 35547c478bd9Sstevel@tonic-gate (void) mutex_unlock(&count_lock); 35557c478bd9Sstevel@tonic-gate } 35567c478bd9Sstevel@tonic-gate 35577c478bd9Sstevel@tonic-gate static void 35587c478bd9Sstevel@tonic-gate instance_flush_thread(void) 35597c478bd9Sstevel@tonic-gate { 35607c478bd9Sstevel@tonic-gate int i; 35617c478bd9Sstevel@tonic-gate int idle; 35627c478bd9Sstevel@tonic-gate 35637c478bd9Sstevel@tonic-gate for (;;) { 35647c478bd9Sstevel@tonic-gate 35657c478bd9Sstevel@tonic-gate (void) mutex_lock(&count_lock); 35667c478bd9Sstevel@tonic-gate while (inst_count == 0) { 35677c478bd9Sstevel@tonic-gate (void) cond_wait(&cv, &count_lock); 35687c478bd9Sstevel@tonic-gate } 35697c478bd9Sstevel@tonic-gate inst_count = 0; 35707c478bd9Sstevel@tonic-gate 35717c478bd9Sstevel@tonic-gate vprint(PATH2INST_MID, "signaled to flush path_to_inst." 35727c478bd9Sstevel@tonic-gate " Enter delay loop\n"); 35737c478bd9Sstevel@tonic-gate /* 35747c478bd9Sstevel@tonic-gate * Wait MAX_IDLE_DELAY seconds after getting the last flush 35757c478bd9Sstevel@tonic-gate * path_to_inst event before invoking a flush, but never wait 35767c478bd9Sstevel@tonic-gate * more than MAX_DELAY seconds after getting the first event. 35777c478bd9Sstevel@tonic-gate */ 35787c478bd9Sstevel@tonic-gate for (idle = 0, i = 0; i < MAX_DELAY; i++) { 35797c478bd9Sstevel@tonic-gate 35807c478bd9Sstevel@tonic-gate (void) mutex_unlock(&count_lock); 35817c478bd9Sstevel@tonic-gate (void) sleep(1); 35827c478bd9Sstevel@tonic-gate (void) mutex_lock(&count_lock); 35837c478bd9Sstevel@tonic-gate 35847c478bd9Sstevel@tonic-gate /* shorten the delay if we are idle */ 35857c478bd9Sstevel@tonic-gate if (inst_count == 0) { 35867c478bd9Sstevel@tonic-gate idle++; 35877c478bd9Sstevel@tonic-gate if (idle > MAX_IDLE_DELAY) { 35887c478bd9Sstevel@tonic-gate break; 35897c478bd9Sstevel@tonic-gate } 35907c478bd9Sstevel@tonic-gate } else { 35917c478bd9Sstevel@tonic-gate inst_count = idle = 0; 35927c478bd9Sstevel@tonic-gate } 35937c478bd9Sstevel@tonic-gate } 35947c478bd9Sstevel@tonic-gate 35957c478bd9Sstevel@tonic-gate (void) mutex_unlock(&count_lock); 35967c478bd9Sstevel@tonic-gate 35977c478bd9Sstevel@tonic-gate flush_path_to_inst(); 35987c478bd9Sstevel@tonic-gate } 35997c478bd9Sstevel@tonic-gate } 36007c478bd9Sstevel@tonic-gate 36017c478bd9Sstevel@tonic-gate /* 36027c478bd9Sstevel@tonic-gate * Helper function for flush_path_to_inst() below; this routine calls the 36037c478bd9Sstevel@tonic-gate * inst_sync syscall to flush the path_to_inst database to the given file. 36047c478bd9Sstevel@tonic-gate */ 36057c478bd9Sstevel@tonic-gate static int 36067c478bd9Sstevel@tonic-gate do_inst_sync(char *filename) 36077c478bd9Sstevel@tonic-gate { 36087c478bd9Sstevel@tonic-gate void (*sigsaved)(int); 36097c478bd9Sstevel@tonic-gate int err = 0; 36107c478bd9Sstevel@tonic-gate 36117c478bd9Sstevel@tonic-gate vprint(INSTSYNC_MID, "do_inst_sync: about to flush %s\n", filename); 36127c478bd9Sstevel@tonic-gate sigsaved = sigset(SIGSYS, SIG_IGN); 36137c478bd9Sstevel@tonic-gate if (inst_sync(filename, 0) == -1) 36147c478bd9Sstevel@tonic-gate err = errno; 36157c478bd9Sstevel@tonic-gate (void) sigset(SIGSYS, sigsaved); 36167c478bd9Sstevel@tonic-gate 36177c478bd9Sstevel@tonic-gate switch (err) { 36187c478bd9Sstevel@tonic-gate case 0: 36197c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 36207c478bd9Sstevel@tonic-gate case EALREADY: /* no-op, path_to_inst already up to date */ 36217c478bd9Sstevel@tonic-gate return (EALREADY); 36227c478bd9Sstevel@tonic-gate case ENOSYS: 36237c478bd9Sstevel@tonic-gate err_print(CANT_LOAD_SYSCALL); 36247c478bd9Sstevel@tonic-gate break; 36257c478bd9Sstevel@tonic-gate case EPERM: 36267c478bd9Sstevel@tonic-gate err_print(SUPER_TO_SYNC); 36277c478bd9Sstevel@tonic-gate break; 36287c478bd9Sstevel@tonic-gate default: 36297c478bd9Sstevel@tonic-gate err_print(INSTSYNC_FAILED, filename, strerror(err)); 36307c478bd9Sstevel@tonic-gate break; 36317c478bd9Sstevel@tonic-gate } 36327c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 36337c478bd9Sstevel@tonic-gate } 36347c478bd9Sstevel@tonic-gate 36357c478bd9Sstevel@tonic-gate /* 36367c478bd9Sstevel@tonic-gate * Flush the kernel's path_to_inst database to /etc/path_to_inst. To do so 36377c478bd9Sstevel@tonic-gate * safely, the database is flushed to a temporary file, then moved into place. 36387c478bd9Sstevel@tonic-gate * 36397c478bd9Sstevel@tonic-gate * The following files are used during this process: 36407c478bd9Sstevel@tonic-gate * /etc/path_to_inst: The path_to_inst file 36417c478bd9Sstevel@tonic-gate * /etc/path_to_inst.<pid>: Contains data flushed from the kernel 36427c478bd9Sstevel@tonic-gate * /etc/path_to_inst.old: The backup file 36437c478bd9Sstevel@tonic-gate * /etc/path_to_inst.old.<pid>: Temp file for creating backup 36447c478bd9Sstevel@tonic-gate * 36457c478bd9Sstevel@tonic-gate */ 36467c478bd9Sstevel@tonic-gate static void 36477c478bd9Sstevel@tonic-gate flush_path_to_inst(void) 36487c478bd9Sstevel@tonic-gate { 36497c478bd9Sstevel@tonic-gate char *new_inst_file = NULL; 36507c478bd9Sstevel@tonic-gate char *old_inst_file = NULL; 36517c478bd9Sstevel@tonic-gate char *old_inst_file_npid = NULL; 36527c478bd9Sstevel@tonic-gate FILE *inst_file_fp = NULL; 36537c478bd9Sstevel@tonic-gate FILE *old_inst_file_fp = NULL; 36547c478bd9Sstevel@tonic-gate struct stat sb; 36557c478bd9Sstevel@tonic-gate int err = 0; 36567c478bd9Sstevel@tonic-gate int c; 36577c478bd9Sstevel@tonic-gate int inst_strlen; 36587c478bd9Sstevel@tonic-gate 36597c478bd9Sstevel@tonic-gate vprint(PATH2INST_MID, "flush_path_to_inst: %s\n", 36607c478bd9Sstevel@tonic-gate (flush_path_to_inst_enable == TRUE) ? "ENABLED" : "DISABLED"); 36617c478bd9Sstevel@tonic-gate 36627c478bd9Sstevel@tonic-gate if (flush_path_to_inst_enable == FALSE) { 36637c478bd9Sstevel@tonic-gate return; 36647c478bd9Sstevel@tonic-gate } 36657c478bd9Sstevel@tonic-gate 36667c478bd9Sstevel@tonic-gate inst_strlen = strlen(inst_file); 36677c478bd9Sstevel@tonic-gate new_inst_file = s_malloc(inst_strlen + PID_STR_LEN + 2); 36687c478bd9Sstevel@tonic-gate old_inst_file = s_malloc(inst_strlen + PID_STR_LEN + 6); 36697c478bd9Sstevel@tonic-gate old_inst_file_npid = s_malloc(inst_strlen + 36707c478bd9Sstevel@tonic-gate sizeof (INSTANCE_FILE_SUFFIX)); 36717c478bd9Sstevel@tonic-gate 36727c478bd9Sstevel@tonic-gate (void) snprintf(new_inst_file, inst_strlen + PID_STR_LEN + 2, 36737c478bd9Sstevel@tonic-gate "%s.%ld", inst_file, getpid()); 36747c478bd9Sstevel@tonic-gate 36757c478bd9Sstevel@tonic-gate if (stat(new_inst_file, &sb) == 0) { 36767c478bd9Sstevel@tonic-gate s_unlink(new_inst_file); 36777c478bd9Sstevel@tonic-gate } 36787c478bd9Sstevel@tonic-gate 36797c478bd9Sstevel@tonic-gate if ((err = do_inst_sync(new_inst_file)) != DEVFSADM_SUCCESS) { 36807c478bd9Sstevel@tonic-gate goto out; 36817c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 36827c478bd9Sstevel@tonic-gate } 36837c478bd9Sstevel@tonic-gate 36847c478bd9Sstevel@tonic-gate /* 36857c478bd9Sstevel@tonic-gate * Now we deal with the somewhat tricky updating and renaming 36867c478bd9Sstevel@tonic-gate * of this critical piece of kernel state. 36877c478bd9Sstevel@tonic-gate */ 36887c478bd9Sstevel@tonic-gate 36897c478bd9Sstevel@tonic-gate /* 36907c478bd9Sstevel@tonic-gate * Copy the current instance file into a temporary file. 36917c478bd9Sstevel@tonic-gate * Then rename the temporary file into the backup (.old) 36927c478bd9Sstevel@tonic-gate * file and rename the newly flushed kernel data into 36937c478bd9Sstevel@tonic-gate * the instance file. 36947c478bd9Sstevel@tonic-gate * Of course if 'inst_file' doesn't exist, there's much 36957c478bd9Sstevel@tonic-gate * less for us to do .. tee hee. 36967c478bd9Sstevel@tonic-gate */ 36977c478bd9Sstevel@tonic-gate if ((inst_file_fp = fopen(inst_file, "r")) == NULL) { 36987c478bd9Sstevel@tonic-gate /* 36997c478bd9Sstevel@tonic-gate * No such file. Rename the new onto the old 37007c478bd9Sstevel@tonic-gate */ 37017c478bd9Sstevel@tonic-gate if ((err = rename(new_inst_file, inst_file)) != 0) 37027c478bd9Sstevel@tonic-gate err_print(RENAME_FAILED, inst_file, strerror(errno)); 37037c478bd9Sstevel@tonic-gate goto out; 37047c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 37057c478bd9Sstevel@tonic-gate } 37067c478bd9Sstevel@tonic-gate 37077c478bd9Sstevel@tonic-gate (void) snprintf(old_inst_file, inst_strlen + PID_STR_LEN + 6, 37087c478bd9Sstevel@tonic-gate "%s.old.%ld", inst_file, getpid()); 37097c478bd9Sstevel@tonic-gate 37107c478bd9Sstevel@tonic-gate if (stat(old_inst_file, &sb) == 0) { 37117c478bd9Sstevel@tonic-gate s_unlink(old_inst_file); 37127c478bd9Sstevel@tonic-gate } 37137c478bd9Sstevel@tonic-gate 37147c478bd9Sstevel@tonic-gate if ((old_inst_file_fp = fopen(old_inst_file, "w")) == NULL) { 37157c478bd9Sstevel@tonic-gate /* 37167c478bd9Sstevel@tonic-gate * Can't open the 'old_inst_file' file for writing. 37177c478bd9Sstevel@tonic-gate * This is somewhat strange given that the syscall 37187c478bd9Sstevel@tonic-gate * just succeeded to write a file out.. hmm.. maybe 37197c478bd9Sstevel@tonic-gate * the fs just filled up or something nasty. 37207c478bd9Sstevel@tonic-gate * 37217c478bd9Sstevel@tonic-gate * Anyway, abort what we've done so far. 37227c478bd9Sstevel@tonic-gate */ 37237c478bd9Sstevel@tonic-gate err_print(CANT_UPDATE, old_inst_file); 37247c478bd9Sstevel@tonic-gate err = DEVFSADM_FAILURE; 37257c478bd9Sstevel@tonic-gate goto out; 37267c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 37277c478bd9Sstevel@tonic-gate } 37287c478bd9Sstevel@tonic-gate 37297c478bd9Sstevel@tonic-gate /* 37307c478bd9Sstevel@tonic-gate * Copy current instance file into the temporary file 37317c478bd9Sstevel@tonic-gate */ 37327c478bd9Sstevel@tonic-gate err = 0; 37337c478bd9Sstevel@tonic-gate while ((c = getc(inst_file_fp)) != EOF) { 37347c478bd9Sstevel@tonic-gate if ((err = putc(c, old_inst_file_fp)) == EOF) { 37357c478bd9Sstevel@tonic-gate break; 37367c478bd9Sstevel@tonic-gate } 37377c478bd9Sstevel@tonic-gate } 37387c478bd9Sstevel@tonic-gate 37397c478bd9Sstevel@tonic-gate if (fclose(old_inst_file_fp) == EOF || err == EOF) { 37407c478bd9Sstevel@tonic-gate vprint(INFO_MID, CANT_UPDATE, old_inst_file); 37417c478bd9Sstevel@tonic-gate err = DEVFSADM_FAILURE; 37427c478bd9Sstevel@tonic-gate goto out; 37437c478bd9Sstevel@tonic-gate /* NOTREACHED */ 37447c478bd9Sstevel@tonic-gate } 37457c478bd9Sstevel@tonic-gate 37467c478bd9Sstevel@tonic-gate /* 37477c478bd9Sstevel@tonic-gate * Set permissions to be the same on the backup as 37487c478bd9Sstevel@tonic-gate * /etc/path_to_inst. 37497c478bd9Sstevel@tonic-gate */ 37507c478bd9Sstevel@tonic-gate (void) chmod(old_inst_file, 0444); 37517c478bd9Sstevel@tonic-gate 37527c478bd9Sstevel@tonic-gate /* 37537c478bd9Sstevel@tonic-gate * So far, everything we've done is more or less reversible. 37547c478bd9Sstevel@tonic-gate * But now we're going to commit ourselves. 37557c478bd9Sstevel@tonic-gate */ 37567c478bd9Sstevel@tonic-gate 37577c478bd9Sstevel@tonic-gate (void) snprintf(old_inst_file_npid, 37587c478bd9Sstevel@tonic-gate inst_strlen + sizeof (INSTANCE_FILE_SUFFIX), 37597c478bd9Sstevel@tonic-gate "%s%s", inst_file, INSTANCE_FILE_SUFFIX); 37607c478bd9Sstevel@tonic-gate 37617c478bd9Sstevel@tonic-gate if ((err = rename(old_inst_file, old_inst_file_npid)) != 0) { 37627c478bd9Sstevel@tonic-gate err_print(RENAME_FAILED, old_inst_file_npid, 37637c478bd9Sstevel@tonic-gate strerror(errno)); 37647c478bd9Sstevel@tonic-gate } else if ((err = rename(new_inst_file, inst_file)) != 0) { 37657c478bd9Sstevel@tonic-gate err_print(RENAME_FAILED, inst_file, strerror(errno)); 37667c478bd9Sstevel@tonic-gate } 37677c478bd9Sstevel@tonic-gate 37687c478bd9Sstevel@tonic-gate out: 37697c478bd9Sstevel@tonic-gate if (inst_file_fp != NULL) { 37707c478bd9Sstevel@tonic-gate if (fclose(inst_file_fp) == EOF) { 37717c478bd9Sstevel@tonic-gate err_print(FCLOSE_FAILED, inst_file, strerror(errno)); 37727c478bd9Sstevel@tonic-gate } 37737c478bd9Sstevel@tonic-gate } 37747c478bd9Sstevel@tonic-gate 37757c478bd9Sstevel@tonic-gate if (stat(new_inst_file, &sb) == 0) { 37767c478bd9Sstevel@tonic-gate s_unlink(new_inst_file); 37777c478bd9Sstevel@tonic-gate } 37787c478bd9Sstevel@tonic-gate free(new_inst_file); 37797c478bd9Sstevel@tonic-gate 37807c478bd9Sstevel@tonic-gate if (stat(old_inst_file, &sb) == 0) { 37817c478bd9Sstevel@tonic-gate s_unlink(old_inst_file); 37827c478bd9Sstevel@tonic-gate } 37837c478bd9Sstevel@tonic-gate free(old_inst_file); 37847c478bd9Sstevel@tonic-gate 37857c478bd9Sstevel@tonic-gate free(old_inst_file_npid); 37867c478bd9Sstevel@tonic-gate 37877c478bd9Sstevel@tonic-gate if (err != 0 && err != EALREADY) { 37887c478bd9Sstevel@tonic-gate err_print(FAILED_TO_UPDATE, inst_file); 37897c478bd9Sstevel@tonic-gate } 37907c478bd9Sstevel@tonic-gate } 37917c478bd9Sstevel@tonic-gate 37927c478bd9Sstevel@tonic-gate /* 37937c478bd9Sstevel@tonic-gate * detach from tty. For daemon mode. 37947c478bd9Sstevel@tonic-gate */ 37957c478bd9Sstevel@tonic-gate void 37967c478bd9Sstevel@tonic-gate detachfromtty() 37977c478bd9Sstevel@tonic-gate { 37987c478bd9Sstevel@tonic-gate (void) setsid(); 37997c478bd9Sstevel@tonic-gate if (DEVFSADM_DEBUG_ON == TRUE) { 38007c478bd9Sstevel@tonic-gate return; 38017c478bd9Sstevel@tonic-gate } 38027c478bd9Sstevel@tonic-gate 38037c478bd9Sstevel@tonic-gate (void) close(0); 38047c478bd9Sstevel@tonic-gate (void) close(1); 38057c478bd9Sstevel@tonic-gate (void) close(2); 38067c478bd9Sstevel@tonic-gate (void) open("/dev/null", O_RDWR, 0); 38077c478bd9Sstevel@tonic-gate (void) dup(0); 38087c478bd9Sstevel@tonic-gate (void) dup(0); 38097c478bd9Sstevel@tonic-gate openlog(DEVFSADMD, LOG_PID, LOG_DAEMON); 38107c478bd9Sstevel@tonic-gate (void) setlogmask(LOG_UPTO(LOG_INFO)); 38117c478bd9Sstevel@tonic-gate logflag = TRUE; 38127c478bd9Sstevel@tonic-gate } 38137c478bd9Sstevel@tonic-gate 38147c478bd9Sstevel@tonic-gate /* 38157c478bd9Sstevel@tonic-gate * Use an advisory lock to synchronize updates to /dev. If the lock is 38167c478bd9Sstevel@tonic-gate * held by another process, block in the fcntl() system call until that 38177c478bd9Sstevel@tonic-gate * process drops the lock or exits. The lock file itself is 38187c478bd9Sstevel@tonic-gate * DEV_LOCK_FILE. The process id of the current and last process owning 38197c478bd9Sstevel@tonic-gate * the lock is kept in the lock file. After acquiring the lock, read the 38207c478bd9Sstevel@tonic-gate * process id and return it. It is the process ID which last owned the 38217c478bd9Sstevel@tonic-gate * lock, and will be used to determine if caches need to be flushed. 38227c478bd9Sstevel@tonic-gate * 38237c478bd9Sstevel@tonic-gate * NOTE: if the devlink database is held open by the caller, it may 38247c478bd9Sstevel@tonic-gate * be closed by this routine. This is to enforce the following lock ordering: 38257c478bd9Sstevel@tonic-gate * 1) /dev lock 2) database open 38267c478bd9Sstevel@tonic-gate */ 38277c478bd9Sstevel@tonic-gate pid_t 38287c478bd9Sstevel@tonic-gate enter_dev_lock() 38297c478bd9Sstevel@tonic-gate { 38307c478bd9Sstevel@tonic-gate struct flock lock; 38317c478bd9Sstevel@tonic-gate int n; 38327c478bd9Sstevel@tonic-gate pid_t pid; 38337c478bd9Sstevel@tonic-gate pid_t last_owner_pid; 38347c478bd9Sstevel@tonic-gate 38357c478bd9Sstevel@tonic-gate if (file_mods == FALSE) { 38367c478bd9Sstevel@tonic-gate return (0); 38377c478bd9Sstevel@tonic-gate } 38387c478bd9Sstevel@tonic-gate 38397c478bd9Sstevel@tonic-gate (void) snprintf(dev_lockfile, sizeof (dev_lockfile), 3840facf4a8dSllai1 "%s/%s", etc_dev_dir, DEV_LOCK_FILE); 38417c478bd9Sstevel@tonic-gate 38427c478bd9Sstevel@tonic-gate vprint(LOCK_MID, "enter_dev_lock: lock file %s\n", dev_lockfile); 38437c478bd9Sstevel@tonic-gate 38447c478bd9Sstevel@tonic-gate dev_lock_fd = open(dev_lockfile, O_CREAT|O_RDWR, 0644); 38457c478bd9Sstevel@tonic-gate if (dev_lock_fd < 0) { 38467c478bd9Sstevel@tonic-gate err_print(OPEN_FAILED, dev_lockfile, strerror(errno)); 38477c478bd9Sstevel@tonic-gate devfsadm_exit(1); 38487c478bd9Sstevel@tonic-gate } 38497c478bd9Sstevel@tonic-gate 38507c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK; 38517c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 38527c478bd9Sstevel@tonic-gate lock.l_start = 0; 38537c478bd9Sstevel@tonic-gate lock.l_len = 0; 38547c478bd9Sstevel@tonic-gate 38557c478bd9Sstevel@tonic-gate /* try for the lock, but don't wait */ 38567c478bd9Sstevel@tonic-gate if (fcntl(dev_lock_fd, F_SETLK, &lock) == -1) { 38577c478bd9Sstevel@tonic-gate if ((errno == EACCES) || (errno == EAGAIN)) { 38587c478bd9Sstevel@tonic-gate pid = 0; 38597c478bd9Sstevel@tonic-gate n = read(dev_lock_fd, &pid, sizeof (pid_t)); 38607c478bd9Sstevel@tonic-gate vprint(LOCK_MID, "waiting for PID %d to complete\n", 38617c478bd9Sstevel@tonic-gate (int)pid); 38627c478bd9Sstevel@tonic-gate if (lseek(dev_lock_fd, 0, SEEK_SET) == (off_t)-1) { 38637c478bd9Sstevel@tonic-gate err_print(LSEEK_FAILED, dev_lockfile, 38647c478bd9Sstevel@tonic-gate strerror(errno)); 38657c478bd9Sstevel@tonic-gate devfsadm_exit(1); 38667c478bd9Sstevel@tonic-gate } 38677c478bd9Sstevel@tonic-gate /* 38687c478bd9Sstevel@tonic-gate * wait for the dev lock. If we have the database open, 38697c478bd9Sstevel@tonic-gate * close it first - the order of lock acquisition should 38707c478bd9Sstevel@tonic-gate * always be: 1) dev_lock 2) database 38717c478bd9Sstevel@tonic-gate * This is to prevent deadlocks with any locks the 38727c478bd9Sstevel@tonic-gate * database code may hold. 38737c478bd9Sstevel@tonic-gate */ 38747c478bd9Sstevel@tonic-gate (void) di_devlink_close(&devlink_cache, 0); 3875f05faa4eSjacobs 3876f05faa4eSjacobs /* send any sysevents that were queued up. */ 3877f05faa4eSjacobs process_syseventq(); 3878f05faa4eSjacobs 38797c478bd9Sstevel@tonic-gate if (fcntl(dev_lock_fd, F_SETLKW, &lock) == -1) { 38807c478bd9Sstevel@tonic-gate err_print(LOCK_FAILED, dev_lockfile, 38817c478bd9Sstevel@tonic-gate strerror(errno)); 38827c478bd9Sstevel@tonic-gate devfsadm_exit(1); 38837c478bd9Sstevel@tonic-gate } 38847c478bd9Sstevel@tonic-gate } 38857c478bd9Sstevel@tonic-gate } 38867c478bd9Sstevel@tonic-gate 38877c478bd9Sstevel@tonic-gate hold_dev_lock = TRUE; 38887c478bd9Sstevel@tonic-gate pid = 0; 38897c478bd9Sstevel@tonic-gate n = read(dev_lock_fd, &pid, sizeof (pid_t)); 38907c478bd9Sstevel@tonic-gate if (n == sizeof (pid_t) && pid == getpid()) { 38917c478bd9Sstevel@tonic-gate return (pid); 38927c478bd9Sstevel@tonic-gate } 38937c478bd9Sstevel@tonic-gate 38947c478bd9Sstevel@tonic-gate last_owner_pid = pid; 38957c478bd9Sstevel@tonic-gate 38967c478bd9Sstevel@tonic-gate if (lseek(dev_lock_fd, 0, SEEK_SET) == (off_t)-1) { 38977c478bd9Sstevel@tonic-gate err_print(LSEEK_FAILED, dev_lockfile, strerror(errno)); 38987c478bd9Sstevel@tonic-gate devfsadm_exit(1); 38997c478bd9Sstevel@tonic-gate } 39007c478bd9Sstevel@tonic-gate pid = getpid(); 39017c478bd9Sstevel@tonic-gate n = write(dev_lock_fd, &pid, sizeof (pid_t)); 39027c478bd9Sstevel@tonic-gate if (n != sizeof (pid_t)) { 39037c478bd9Sstevel@tonic-gate err_print(WRITE_FAILED, dev_lockfile, strerror(errno)); 39047c478bd9Sstevel@tonic-gate devfsadm_exit(1); 39057c478bd9Sstevel@tonic-gate } 39067c478bd9Sstevel@tonic-gate 39077c478bd9Sstevel@tonic-gate return (last_owner_pid); 39087c478bd9Sstevel@tonic-gate } 39097c478bd9Sstevel@tonic-gate 39107c478bd9Sstevel@tonic-gate /* 39117c478bd9Sstevel@tonic-gate * Drop the advisory /dev lock, close lock file. Close and re-open the 39127c478bd9Sstevel@tonic-gate * file every time so to ensure a resync if for some reason the lock file 39137c478bd9Sstevel@tonic-gate * gets removed. 39147c478bd9Sstevel@tonic-gate */ 39157c478bd9Sstevel@tonic-gate void 39167c478bd9Sstevel@tonic-gate exit_dev_lock() 39177c478bd9Sstevel@tonic-gate { 39187c478bd9Sstevel@tonic-gate struct flock unlock; 39197c478bd9Sstevel@tonic-gate 39207c478bd9Sstevel@tonic-gate if (hold_dev_lock == FALSE) { 39217c478bd9Sstevel@tonic-gate return; 39227c478bd9Sstevel@tonic-gate } 39237c478bd9Sstevel@tonic-gate 39247c478bd9Sstevel@tonic-gate vprint(LOCK_MID, "exit_dev_lock: lock file %s\n", dev_lockfile); 39257c478bd9Sstevel@tonic-gate 39267c478bd9Sstevel@tonic-gate unlock.l_type = F_UNLCK; 39277c478bd9Sstevel@tonic-gate unlock.l_whence = SEEK_SET; 39287c478bd9Sstevel@tonic-gate unlock.l_start = 0; 39297c478bd9Sstevel@tonic-gate unlock.l_len = 0; 39307c478bd9Sstevel@tonic-gate 39317c478bd9Sstevel@tonic-gate if (fcntl(dev_lock_fd, F_SETLK, &unlock) == -1) { 39327c478bd9Sstevel@tonic-gate err_print(UNLOCK_FAILED, dev_lockfile, strerror(errno)); 39337c478bd9Sstevel@tonic-gate } 39347c478bd9Sstevel@tonic-gate 39357c478bd9Sstevel@tonic-gate hold_dev_lock = FALSE; 39367c478bd9Sstevel@tonic-gate 39377c478bd9Sstevel@tonic-gate if (close(dev_lock_fd) == -1) { 39387c478bd9Sstevel@tonic-gate err_print(CLOSE_FAILED, dev_lockfile, strerror(errno)); 39397c478bd9Sstevel@tonic-gate devfsadm_exit(1); 39407c478bd9Sstevel@tonic-gate } 39417c478bd9Sstevel@tonic-gate } 39427c478bd9Sstevel@tonic-gate 39437c478bd9Sstevel@tonic-gate /* 39447c478bd9Sstevel@tonic-gate * 39457c478bd9Sstevel@tonic-gate * Use an advisory lock to ensure that only one daemon process is active 39467c478bd9Sstevel@tonic-gate * in the system at any point in time. If the lock is held by another 39477c478bd9Sstevel@tonic-gate * process, do not block but return the pid owner of the lock to the 39487c478bd9Sstevel@tonic-gate * caller immediately. The lock is cleared if the holding daemon process 39497c478bd9Sstevel@tonic-gate * exits for any reason even if the lock file remains, so the daemon can 39507c478bd9Sstevel@tonic-gate * be restarted if necessary. The lock file is DAEMON_LOCK_FILE. 39517c478bd9Sstevel@tonic-gate */ 39527c478bd9Sstevel@tonic-gate pid_t 39537c478bd9Sstevel@tonic-gate enter_daemon_lock(void) 39547c478bd9Sstevel@tonic-gate { 39557c478bd9Sstevel@tonic-gate struct flock lock; 39567c478bd9Sstevel@tonic-gate 39577c478bd9Sstevel@tonic-gate (void) snprintf(daemon_lockfile, sizeof (daemon_lockfile), 3958facf4a8dSllai1 "%s/%s", etc_dev_dir, DAEMON_LOCK_FILE); 39597c478bd9Sstevel@tonic-gate 39607c478bd9Sstevel@tonic-gate vprint(LOCK_MID, "enter_daemon_lock: lock file %s\n", daemon_lockfile); 39617c478bd9Sstevel@tonic-gate 39627c478bd9Sstevel@tonic-gate daemon_lock_fd = open(daemon_lockfile, O_CREAT|O_RDWR, 0644); 39637c478bd9Sstevel@tonic-gate if (daemon_lock_fd < 0) { 39647c478bd9Sstevel@tonic-gate err_print(OPEN_FAILED, daemon_lockfile, strerror(errno)); 39657c478bd9Sstevel@tonic-gate devfsadm_exit(1); 39667c478bd9Sstevel@tonic-gate } 39677c478bd9Sstevel@tonic-gate 39687c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK; 39697c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 39707c478bd9Sstevel@tonic-gate lock.l_start = 0; 39717c478bd9Sstevel@tonic-gate lock.l_len = 0; 39727c478bd9Sstevel@tonic-gate 39737c478bd9Sstevel@tonic-gate if (fcntl(daemon_lock_fd, F_SETLK, &lock) == -1) { 39747c478bd9Sstevel@tonic-gate 39757c478bd9Sstevel@tonic-gate if (errno == EAGAIN || errno == EDEADLK) { 39767c478bd9Sstevel@tonic-gate if (fcntl(daemon_lock_fd, F_GETLK, &lock) == -1) { 39777c478bd9Sstevel@tonic-gate err_print(LOCK_FAILED, daemon_lockfile, 39787c478bd9Sstevel@tonic-gate strerror(errno)); 39797c478bd9Sstevel@tonic-gate devfsadm_exit(1); 39807c478bd9Sstevel@tonic-gate } 39817c478bd9Sstevel@tonic-gate return (lock.l_pid); 39827c478bd9Sstevel@tonic-gate } 39837c478bd9Sstevel@tonic-gate } 39847c478bd9Sstevel@tonic-gate hold_daemon_lock = TRUE; 39857c478bd9Sstevel@tonic-gate return (getpid()); 39867c478bd9Sstevel@tonic-gate } 39877c478bd9Sstevel@tonic-gate 39887c478bd9Sstevel@tonic-gate /* 39897c478bd9Sstevel@tonic-gate * Drop the advisory daemon lock, close lock file 39907c478bd9Sstevel@tonic-gate */ 39917c478bd9Sstevel@tonic-gate void 39927c478bd9Sstevel@tonic-gate exit_daemon_lock(void) 39937c478bd9Sstevel@tonic-gate { 39947c478bd9Sstevel@tonic-gate struct flock lock; 39957c478bd9Sstevel@tonic-gate 39967c478bd9Sstevel@tonic-gate if (hold_daemon_lock == FALSE) { 39977c478bd9Sstevel@tonic-gate return; 39987c478bd9Sstevel@tonic-gate } 39997c478bd9Sstevel@tonic-gate 40007c478bd9Sstevel@tonic-gate vprint(LOCK_MID, "exit_daemon_lock: lock file %s\n", daemon_lockfile); 40017c478bd9Sstevel@tonic-gate 40027c478bd9Sstevel@tonic-gate lock.l_type = F_UNLCK; 40037c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 40047c478bd9Sstevel@tonic-gate lock.l_start = 0; 40057c478bd9Sstevel@tonic-gate lock.l_len = 0; 40067c478bd9Sstevel@tonic-gate 40077c478bd9Sstevel@tonic-gate if (fcntl(daemon_lock_fd, F_SETLK, &lock) == -1) { 40087c478bd9Sstevel@tonic-gate err_print(UNLOCK_FAILED, daemon_lockfile, strerror(errno)); 40097c478bd9Sstevel@tonic-gate } 40107c478bd9Sstevel@tonic-gate 40117c478bd9Sstevel@tonic-gate if (close(daemon_lock_fd) == -1) { 40127c478bd9Sstevel@tonic-gate err_print(CLOSE_FAILED, daemon_lockfile, strerror(errno)); 40137c478bd9Sstevel@tonic-gate devfsadm_exit(1); 40147c478bd9Sstevel@tonic-gate } 40157c478bd9Sstevel@tonic-gate } 40167c478bd9Sstevel@tonic-gate 40177c478bd9Sstevel@tonic-gate /* 40187c478bd9Sstevel@tonic-gate * Called to removed danging nodes in two different modes: RM_PRE, RM_POST. 40197c478bd9Sstevel@tonic-gate * RM_PRE mode is called before processing the entire devinfo tree, and RM_POST 40207c478bd9Sstevel@tonic-gate * is called after processing the entire devinfo tree. 40217c478bd9Sstevel@tonic-gate */ 40227c478bd9Sstevel@tonic-gate static void 40237c478bd9Sstevel@tonic-gate pre_and_post_cleanup(int flags) 40247c478bd9Sstevel@tonic-gate { 40257c478bd9Sstevel@tonic-gate remove_list_t *rm; 40267c478bd9Sstevel@tonic-gate recurse_dev_t rd; 40277c478bd9Sstevel@tonic-gate cleanup_data_t cleanup_data; 40287c478bd9Sstevel@tonic-gate char *fcn = "pre_and_post_cleanup: "; 40297c478bd9Sstevel@tonic-gate 40307c478bd9Sstevel@tonic-gate if (build_dev == FALSE) 40317c478bd9Sstevel@tonic-gate return; 40327c478bd9Sstevel@tonic-gate 40337c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "attempting %s-cleanup\n", 40347c478bd9Sstevel@tonic-gate flags == RM_PRE ? "pre" : "post"); 40357c478bd9Sstevel@tonic-gate vprint(REMOVE_MID, "%sflags = %d\n", fcn, flags); 40367c478bd9Sstevel@tonic-gate 40377c478bd9Sstevel@tonic-gate /* 40387c478bd9Sstevel@tonic-gate * the generic function recurse_dev_re is shared among different 40397c478bd9Sstevel@tonic-gate * functions, so set the method and data that it should use for 40407c478bd9Sstevel@tonic-gate * matches. 40417c478bd9Sstevel@tonic-gate */ 40427c478bd9Sstevel@tonic-gate rd.fcn = matching_dev; 40437c478bd9Sstevel@tonic-gate rd.data = (void *)&cleanup_data; 40447c478bd9Sstevel@tonic-gate cleanup_data.flags = flags; 40457c478bd9Sstevel@tonic-gate 4046aa646b9dSvikram (void) mutex_lock(&nfp_mutex); 4047aa646b9dSvikram nfphash_create(); 4048aa646b9dSvikram 40497c478bd9Sstevel@tonic-gate for (rm = remove_head; rm != NULL; rm = rm->next) { 40507c478bd9Sstevel@tonic-gate if ((flags & rm->remove->flags) == flags) { 40517c478bd9Sstevel@tonic-gate cleanup_data.rm = rm; 40527c478bd9Sstevel@tonic-gate /* 40537c478bd9Sstevel@tonic-gate * If reached this point, RM_PRE or RM_POST cleanup is 40547c478bd9Sstevel@tonic-gate * desired. clean_ok() decides whether to clean 40557c478bd9Sstevel@tonic-gate * under the given circumstances. 40567c478bd9Sstevel@tonic-gate */ 40577c478bd9Sstevel@tonic-gate vprint(REMOVE_MID, "%scleanup: PRE or POST\n", fcn); 40587c478bd9Sstevel@tonic-gate if (clean_ok(rm->remove) == DEVFSADM_SUCCESS) { 40597c478bd9Sstevel@tonic-gate vprint(REMOVE_MID, "cleanup: cleanup OK\n"); 40607c478bd9Sstevel@tonic-gate recurse_dev_re(dev_dir, rm->remove-> 40617c478bd9Sstevel@tonic-gate dev_dirs_re, &rd); 40627c478bd9Sstevel@tonic-gate } 40637c478bd9Sstevel@tonic-gate } 40647c478bd9Sstevel@tonic-gate } 4065aa646b9dSvikram nfphash_destroy(); 4066aa646b9dSvikram (void) mutex_unlock(&nfp_mutex); 40677c478bd9Sstevel@tonic-gate } 40687c478bd9Sstevel@tonic-gate 40697c478bd9Sstevel@tonic-gate /* 40707c478bd9Sstevel@tonic-gate * clean_ok() determines whether cleanup should be done according 40717c478bd9Sstevel@tonic-gate * to the following matrix: 40727c478bd9Sstevel@tonic-gate * 40737c478bd9Sstevel@tonic-gate * command line arguments RM_PRE RM_POST RM_PRE && RM_POST && 40747c478bd9Sstevel@tonic-gate * RM_ALWAYS RM_ALWAYS 40757c478bd9Sstevel@tonic-gate * ---------------------- ------ ----- --------- ---------- 40767c478bd9Sstevel@tonic-gate * 40777c478bd9Sstevel@tonic-gate * <neither -c nor -C> - - pre-clean post-clean 40787c478bd9Sstevel@tonic-gate * 40797c478bd9Sstevel@tonic-gate * -C pre-clean post-clean pre-clean post-clean 40807c478bd9Sstevel@tonic-gate * 40817c478bd9Sstevel@tonic-gate * -C -c class pre-clean post-clean pre-clean post-clean 40827c478bd9Sstevel@tonic-gate * if class if class if class if class 40837c478bd9Sstevel@tonic-gate * matches matches matches matches 40847c478bd9Sstevel@tonic-gate * 40857c478bd9Sstevel@tonic-gate * -c class - - pre-clean post-clean 40867c478bd9Sstevel@tonic-gate * if class if class 40877c478bd9Sstevel@tonic-gate * matches matches 40887c478bd9Sstevel@tonic-gate * 40897c478bd9Sstevel@tonic-gate */ 40907c478bd9Sstevel@tonic-gate static int 4091aa646b9dSvikram clean_ok(devfsadm_remove_V1_t *remove) 40927c478bd9Sstevel@tonic-gate { 40937c478bd9Sstevel@tonic-gate int i; 40947c478bd9Sstevel@tonic-gate 40957c478bd9Sstevel@tonic-gate if (single_drv == TRUE) { 40967c478bd9Sstevel@tonic-gate /* no cleanup at all when using -i option */ 40977c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 40987c478bd9Sstevel@tonic-gate } 40997c478bd9Sstevel@tonic-gate 41007c478bd9Sstevel@tonic-gate /* 41017c478bd9Sstevel@tonic-gate * no cleanup if drivers are not loaded. We make an exception 41027c478bd9Sstevel@tonic-gate * for the "disks" program however, since disks has a public 41037c478bd9Sstevel@tonic-gate * cleanup flag (-C) and disk drivers are usually never 41047c478bd9Sstevel@tonic-gate * unloaded. 41057c478bd9Sstevel@tonic-gate */ 41067c478bd9Sstevel@tonic-gate if (load_attach_drv == FALSE && strcmp(prog, DISKS) != 0) { 41077c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 41087c478bd9Sstevel@tonic-gate } 41097c478bd9Sstevel@tonic-gate 41107c478bd9Sstevel@tonic-gate /* if the cleanup flag was not specified, return false */ 41117c478bd9Sstevel@tonic-gate if ((cleanup == FALSE) && ((remove->flags & RM_ALWAYS) == 0)) { 41127c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 41137c478bd9Sstevel@tonic-gate } 41147c478bd9Sstevel@tonic-gate 41157c478bd9Sstevel@tonic-gate if (num_classes == 0) { 41167c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 41177c478bd9Sstevel@tonic-gate } 41187c478bd9Sstevel@tonic-gate 41197c478bd9Sstevel@tonic-gate /* 41207c478bd9Sstevel@tonic-gate * if reached this point, check to see if the class in the given 41217c478bd9Sstevel@tonic-gate * remove structure matches a class given on the command line 41227c478bd9Sstevel@tonic-gate */ 41237c478bd9Sstevel@tonic-gate 41247c478bd9Sstevel@tonic-gate for (i = 0; i < num_classes; i++) { 41257c478bd9Sstevel@tonic-gate if (strcmp(remove->device_class, classes[i]) == 0) { 41267c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 41277c478bd9Sstevel@tonic-gate } 41287c478bd9Sstevel@tonic-gate } 41297c478bd9Sstevel@tonic-gate 41307c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 41317c478bd9Sstevel@tonic-gate } 41327c478bd9Sstevel@tonic-gate 41337c478bd9Sstevel@tonic-gate /* 41347c478bd9Sstevel@tonic-gate * Called to remove dangling nodes after receiving a hotplug event 41357c478bd9Sstevel@tonic-gate * containing the physical node pathname to be removed. 41367c478bd9Sstevel@tonic-gate */ 41377c478bd9Sstevel@tonic-gate void 41387c478bd9Sstevel@tonic-gate hot_cleanup(char *node_path, char *minor_name, char *ev_subclass, 41397c478bd9Sstevel@tonic-gate char *driver_name, int instance) 41407c478bd9Sstevel@tonic-gate { 41417c478bd9Sstevel@tonic-gate link_t *link; 41427c478bd9Sstevel@tonic-gate linkhead_t *head; 41437c478bd9Sstevel@tonic-gate remove_list_t *rm; 41447c478bd9Sstevel@tonic-gate char *fcn = "hot_cleanup: "; 41457c478bd9Sstevel@tonic-gate char path[PATH_MAX + 1]; 41467c478bd9Sstevel@tonic-gate int path_len; 41477c478bd9Sstevel@tonic-gate char rmlink[PATH_MAX + 1]; 41487c478bd9Sstevel@tonic-gate nvlist_t *nvl = NULL; 4149fb9251a6Scth int skip; 4150aa646b9dSvikram int ret; 41517c478bd9Sstevel@tonic-gate 41527c478bd9Sstevel@tonic-gate /* 41537c478bd9Sstevel@tonic-gate * dev links can go away as part of hot cleanup. 41547c478bd9Sstevel@tonic-gate * So first build event attributes in order capture dev links. 41557c478bd9Sstevel@tonic-gate */ 41567c478bd9Sstevel@tonic-gate if (ev_subclass != NULL) 41577c478bd9Sstevel@tonic-gate nvl = build_event_attributes(EC_DEV_REMOVE, ev_subclass, 415830294554Sphitran node_path, DI_NODE_NIL, driver_name, instance, minor_name); 41597c478bd9Sstevel@tonic-gate 41607c478bd9Sstevel@tonic-gate (void) strcpy(path, node_path); 41617c478bd9Sstevel@tonic-gate (void) strcat(path, ":"); 41627c478bd9Sstevel@tonic-gate (void) strcat(path, minor_name == NULL ? "" : minor_name); 41637c478bd9Sstevel@tonic-gate 41647c478bd9Sstevel@tonic-gate path_len = strlen(path); 41657c478bd9Sstevel@tonic-gate 41667c478bd9Sstevel@tonic-gate vprint(REMOVE_MID, "%spath=%s\n", fcn, path); 41677c478bd9Sstevel@tonic-gate 4168aa646b9dSvikram (void) mutex_lock(&nfp_mutex); 4169aa646b9dSvikram nfphash_create(); 4170aa646b9dSvikram 41717c478bd9Sstevel@tonic-gate for (rm = remove_head; rm != NULL; rm = rm->next) { 41727c478bd9Sstevel@tonic-gate if ((RM_HOT & rm->remove->flags) == RM_HOT) { 41737c478bd9Sstevel@tonic-gate head = get_cached_links(rm->remove->dev_dirs_re); 41747c478bd9Sstevel@tonic-gate assert(head->nextlink == NULL); 41757c478bd9Sstevel@tonic-gate for (link = head->link; 41767c478bd9Sstevel@tonic-gate link != NULL; link = head->nextlink) { 41777c478bd9Sstevel@tonic-gate /* 41787c478bd9Sstevel@tonic-gate * The remove callback below may remove 41797c478bd9Sstevel@tonic-gate * the current and/or any or all of the 41807c478bd9Sstevel@tonic-gate * subsequent links in the list. 41817c478bd9Sstevel@tonic-gate * Save the next link in the head. If 41827c478bd9Sstevel@tonic-gate * the callback removes the next link 41837c478bd9Sstevel@tonic-gate * the saved pointer in the head will be 41847c478bd9Sstevel@tonic-gate * updated by the callback to point at 41857c478bd9Sstevel@tonic-gate * the next valid link. 41867c478bd9Sstevel@tonic-gate */ 41877c478bd9Sstevel@tonic-gate head->nextlink = link->next; 4188aa646b9dSvikram 4189aa646b9dSvikram /* 4190aa646b9dSvikram * if devlink is in no-further-process hash, 4191aa646b9dSvikram * skip its remove 4192aa646b9dSvikram */ 4193aa646b9dSvikram if (nfphash_lookup(link->devlink) != NULL) 4194aa646b9dSvikram continue; 4195aa646b9dSvikram 4196fb9251a6Scth if (minor_name) 4197fb9251a6Scth skip = strcmp(link->contents, path); 4198fb9251a6Scth else 4199fb9251a6Scth skip = strncmp(link->contents, path, 4200fb9251a6Scth path_len); 4201fb9251a6Scth if (skip || 4202fb9251a6Scth (call_minor_init(rm->modptr) == 4203fb9251a6Scth DEVFSADM_FAILURE)) 42047c478bd9Sstevel@tonic-gate continue; 42057c478bd9Sstevel@tonic-gate 42067c478bd9Sstevel@tonic-gate vprint(REMOVE_MID, 42077c478bd9Sstevel@tonic-gate "%sremoving %s -> %s\n", fcn, 42087c478bd9Sstevel@tonic-gate link->devlink, link->contents); 42097c478bd9Sstevel@tonic-gate /* 42107c478bd9Sstevel@tonic-gate * Use a copy of the cached link name 42117c478bd9Sstevel@tonic-gate * as the cache entry will go away 42127c478bd9Sstevel@tonic-gate * during link removal 42137c478bd9Sstevel@tonic-gate */ 42147c478bd9Sstevel@tonic-gate (void) snprintf(rmlink, sizeof (rmlink), 42157c478bd9Sstevel@tonic-gate "%s", link->devlink); 4216aa646b9dSvikram if (rm->remove->flags & RM_NOINTERPOSE) { 4217aa646b9dSvikram ((void (*)(char *)) 4218aa646b9dSvikram (rm->remove->callback_fcn))(rmlink); 4219aa646b9dSvikram } else { 4220aa646b9dSvikram ret = ((int (*)(char *)) 4221aa646b9dSvikram (rm->remove->callback_fcn))(rmlink); 4222aa646b9dSvikram if (ret == DEVFSADM_TERMINATE) 4223aa646b9dSvikram nfphash_insert(rmlink); 42247c478bd9Sstevel@tonic-gate } 42257c478bd9Sstevel@tonic-gate } 42267c478bd9Sstevel@tonic-gate } 4227aa646b9dSvikram } 4228aa646b9dSvikram 4229aa646b9dSvikram nfphash_destroy(); 4230aa646b9dSvikram (void) mutex_unlock(&nfp_mutex); 42317c478bd9Sstevel@tonic-gate 423245916cd2Sjpk /* update device allocation database */ 423345916cd2Sjpk if (system_labeled) { 423445916cd2Sjpk int ret = 0; 423545916cd2Sjpk int devtype = 0; 423645916cd2Sjpk char devname[MAXNAMELEN]; 423745916cd2Sjpk 423845916cd2Sjpk devname[0] = '\0'; 423945916cd2Sjpk if (strstr(node_path, DA_SOUND_NAME)) 424045916cd2Sjpk devtype = DA_AUDIO; 424145916cd2Sjpk else if (strstr(node_path, "disk")) 424245916cd2Sjpk devtype = DA_RMDISK; 424345916cd2Sjpk else 424445916cd2Sjpk goto out; 424545916cd2Sjpk ret = da_remove_list(&devlist, NULL, devtype, devname, 424645916cd2Sjpk sizeof (devname)); 424745916cd2Sjpk if (ret != -1) 424845916cd2Sjpk (void) _update_devalloc_db(&devlist, devtype, DA_REMOVE, 424945916cd2Sjpk devname, root_dir); 425045916cd2Sjpk } 425145916cd2Sjpk 425245916cd2Sjpk out: 42537c478bd9Sstevel@tonic-gate /* now log an event */ 42547c478bd9Sstevel@tonic-gate if (nvl) { 42557c478bd9Sstevel@tonic-gate log_event(EC_DEV_REMOVE, ev_subclass, nvl); 42567c478bd9Sstevel@tonic-gate free(nvl); 42577c478bd9Sstevel@tonic-gate } 42587c478bd9Sstevel@tonic-gate } 42597c478bd9Sstevel@tonic-gate 42607c478bd9Sstevel@tonic-gate /* 42617c478bd9Sstevel@tonic-gate * Open the dir current_dir. For every file which matches the first dir 42627c478bd9Sstevel@tonic-gate * component of path_re, recurse. If there are no more *dir* path 42637c478bd9Sstevel@tonic-gate * components left in path_re (ie no more /), then call function rd->fcn. 42647c478bd9Sstevel@tonic-gate */ 42657c478bd9Sstevel@tonic-gate static void 42667c478bd9Sstevel@tonic-gate recurse_dev_re(char *current_dir, char *path_re, recurse_dev_t *rd) 42677c478bd9Sstevel@tonic-gate { 42687c478bd9Sstevel@tonic-gate regex_t re1; 42697c478bd9Sstevel@tonic-gate char *slash; 42707c478bd9Sstevel@tonic-gate char new_path[PATH_MAX + 1]; 42717c478bd9Sstevel@tonic-gate char *anchored_path_re; 42727c478bd9Sstevel@tonic-gate size_t len; 427373de625bSjg finddevhdl_t fhandle; 427473de625bSjg const char *fp; 42757c478bd9Sstevel@tonic-gate 42767c478bd9Sstevel@tonic-gate vprint(RECURSEDEV_MID, "recurse_dev_re: curr = %s path=%s\n", 42777c478bd9Sstevel@tonic-gate current_dir, path_re); 42787c478bd9Sstevel@tonic-gate 427973de625bSjg if (finddev_readdir(current_dir, &fhandle) != 0) 42807c478bd9Sstevel@tonic-gate return; 42817c478bd9Sstevel@tonic-gate 42827c478bd9Sstevel@tonic-gate len = strlen(path_re); 42837c478bd9Sstevel@tonic-gate if ((slash = strchr(path_re, '/')) != NULL) { 42847c478bd9Sstevel@tonic-gate len = (slash - path_re); 42857c478bd9Sstevel@tonic-gate } 42867c478bd9Sstevel@tonic-gate 42877c478bd9Sstevel@tonic-gate anchored_path_re = s_malloc(len + 3); 42887c478bd9Sstevel@tonic-gate (void) sprintf(anchored_path_re, "^%.*s$", len, path_re); 42897c478bd9Sstevel@tonic-gate 42907c478bd9Sstevel@tonic-gate if (regcomp(&re1, anchored_path_re, REG_EXTENDED) != 0) { 42917c478bd9Sstevel@tonic-gate free(anchored_path_re); 42927c478bd9Sstevel@tonic-gate goto out; 42937c478bd9Sstevel@tonic-gate } 42947c478bd9Sstevel@tonic-gate 42957c478bd9Sstevel@tonic-gate free(anchored_path_re); 42967c478bd9Sstevel@tonic-gate 429773de625bSjg while ((fp = finddev_next(fhandle)) != NULL) { 42987c478bd9Sstevel@tonic-gate 429973de625bSjg if (regexec(&re1, fp, 0, NULL, 0) == 0) { 43007c478bd9Sstevel@tonic-gate /* match */ 43017c478bd9Sstevel@tonic-gate (void) strcpy(new_path, current_dir); 43027c478bd9Sstevel@tonic-gate (void) strcat(new_path, "/"); 430373de625bSjg (void) strcat(new_path, fp); 43047c478bd9Sstevel@tonic-gate 43057c478bd9Sstevel@tonic-gate vprint(RECURSEDEV_MID, "recurse_dev_re: match, new " 43067c478bd9Sstevel@tonic-gate "path = %s\n", new_path); 43077c478bd9Sstevel@tonic-gate 43087c478bd9Sstevel@tonic-gate if (slash != NULL) { 43097c478bd9Sstevel@tonic-gate recurse_dev_re(new_path, slash + 1, rd); 43107c478bd9Sstevel@tonic-gate } else { 43117c478bd9Sstevel@tonic-gate /* reached the leaf component of path_re */ 43127c478bd9Sstevel@tonic-gate vprint(RECURSEDEV_MID, 43137c478bd9Sstevel@tonic-gate "recurse_dev_re: calling fcn\n"); 43147c478bd9Sstevel@tonic-gate (*(rd->fcn))(new_path, rd->data); 43157c478bd9Sstevel@tonic-gate } 43167c478bd9Sstevel@tonic-gate } 43177c478bd9Sstevel@tonic-gate } 43187c478bd9Sstevel@tonic-gate 43197c478bd9Sstevel@tonic-gate regfree(&re1); 43207c478bd9Sstevel@tonic-gate 43217c478bd9Sstevel@tonic-gate out: 432273de625bSjg finddev_close(fhandle); 43237c478bd9Sstevel@tonic-gate } 43247c478bd9Sstevel@tonic-gate 43257c478bd9Sstevel@tonic-gate /* 43267c478bd9Sstevel@tonic-gate * Found a devpath which matches a RE in the remove structure. 43277c478bd9Sstevel@tonic-gate * Now check to see if it is dangling. 43287c478bd9Sstevel@tonic-gate */ 43297c478bd9Sstevel@tonic-gate static void 43307c478bd9Sstevel@tonic-gate matching_dev(char *devpath, void *data) 43317c478bd9Sstevel@tonic-gate { 43327c478bd9Sstevel@tonic-gate cleanup_data_t *cleanup_data = data; 4333aa646b9dSvikram int norm_len = strlen(dev_dir) + strlen("/"); 4334aa646b9dSvikram int ret; 43357c478bd9Sstevel@tonic-gate char *fcn = "matching_dev: "; 43367c478bd9Sstevel@tonic-gate 43377c478bd9Sstevel@tonic-gate vprint(RECURSEDEV_MID, "%sexamining devpath = '%s'\n", fcn, 43387c478bd9Sstevel@tonic-gate devpath); 43397c478bd9Sstevel@tonic-gate 4340aa646b9dSvikram /* 4341aa646b9dSvikram * If the link is in the no-further-process hash 4342aa646b9dSvikram * don't do any remove operation on it. 4343aa646b9dSvikram */ 4344aa646b9dSvikram if (nfphash_lookup(devpath + norm_len) != NULL) 4345aa646b9dSvikram return; 4346aa646b9dSvikram 43477c478bd9Sstevel@tonic-gate if (resolve_link(devpath, NULL, NULL, NULL, 1) == TRUE) { 43487c478bd9Sstevel@tonic-gate if (call_minor_init(cleanup_data->rm->modptr) == 43497c478bd9Sstevel@tonic-gate DEVFSADM_FAILURE) { 43507c478bd9Sstevel@tonic-gate return; 43517c478bd9Sstevel@tonic-gate } 43527c478bd9Sstevel@tonic-gate 4353aa646b9dSvikram devpath += norm_len; 43547c478bd9Sstevel@tonic-gate 43557c478bd9Sstevel@tonic-gate vprint(RECURSEDEV_MID, "%scalling" 43567c478bd9Sstevel@tonic-gate " callback %s\n", fcn, devpath); 4357aa646b9dSvikram if (cleanup_data->rm->remove->flags & RM_NOINTERPOSE) 4358aa646b9dSvikram ((void (*)(char *)) 4359aa646b9dSvikram (cleanup_data->rm->remove->callback_fcn))(devpath); 4360aa646b9dSvikram else { 4361aa646b9dSvikram ret = ((int (*)(char *)) 4362aa646b9dSvikram (cleanup_data->rm->remove->callback_fcn))(devpath); 4363aa646b9dSvikram if (ret == DEVFSADM_TERMINATE) { 4364aa646b9dSvikram /* 4365aa646b9dSvikram * We want no further remove processing for 4366aa646b9dSvikram * this link. Add it to the nfp_hash; 4367aa646b9dSvikram */ 4368aa646b9dSvikram nfphash_insert(devpath); 4369aa646b9dSvikram } 4370aa646b9dSvikram } 43717c478bd9Sstevel@tonic-gate } 43727c478bd9Sstevel@tonic-gate } 43737c478bd9Sstevel@tonic-gate 43747c478bd9Sstevel@tonic-gate int 43757c478bd9Sstevel@tonic-gate devfsadm_read_link(char *link, char **devfs_path) 43767c478bd9Sstevel@tonic-gate { 43777c478bd9Sstevel@tonic-gate char devlink[PATH_MAX]; 43787c478bd9Sstevel@tonic-gate 43797c478bd9Sstevel@tonic-gate *devfs_path = NULL; 43807c478bd9Sstevel@tonic-gate 43817c478bd9Sstevel@tonic-gate /* prepend link with dev_dir contents */ 43827c478bd9Sstevel@tonic-gate (void) strcpy(devlink, dev_dir); 43837c478bd9Sstevel@tonic-gate (void) strcat(devlink, "/"); 43847c478bd9Sstevel@tonic-gate (void) strcat(devlink, link); 43857c478bd9Sstevel@tonic-gate 43867c478bd9Sstevel@tonic-gate /* We *don't* want a stat of the /devices node */ 43877c478bd9Sstevel@tonic-gate (void) resolve_link(devlink, NULL, NULL, devfs_path, 0); 43887c478bd9Sstevel@tonic-gate 43897c478bd9Sstevel@tonic-gate return (*devfs_path ? DEVFSADM_SUCCESS : DEVFSADM_FAILURE); 43907c478bd9Sstevel@tonic-gate } 43917c478bd9Sstevel@tonic-gate 43927c478bd9Sstevel@tonic-gate int 43937c478bd9Sstevel@tonic-gate devfsadm_link_valid(char *link) 43947c478bd9Sstevel@tonic-gate { 43957c478bd9Sstevel@tonic-gate struct stat sb; 439645916cd2Sjpk char devlink[PATH_MAX + 1], *contents = NULL; 43977c478bd9Sstevel@tonic-gate int rv, type; 439845916cd2Sjpk int instance = 0; 43997c478bd9Sstevel@tonic-gate 44007c478bd9Sstevel@tonic-gate /* prepend link with dev_dir contents */ 44017c478bd9Sstevel@tonic-gate (void) strcpy(devlink, dev_dir); 44027c478bd9Sstevel@tonic-gate (void) strcat(devlink, "/"); 44037c478bd9Sstevel@tonic-gate (void) strcat(devlink, link); 44047c478bd9Sstevel@tonic-gate 4405facf4a8dSllai1 if (!device_exists(devlink) || lstat(devlink, &sb) != 0) { 44067c478bd9Sstevel@tonic-gate return (DEVFSADM_FALSE); 44077c478bd9Sstevel@tonic-gate } 44087c478bd9Sstevel@tonic-gate 44097c478bd9Sstevel@tonic-gate contents = NULL; 44107c478bd9Sstevel@tonic-gate type = 0; 44117c478bd9Sstevel@tonic-gate if (resolve_link(devlink, &contents, &type, NULL, 1) == TRUE) { 44127c478bd9Sstevel@tonic-gate rv = DEVFSADM_FALSE; 44137c478bd9Sstevel@tonic-gate } else { 44147c478bd9Sstevel@tonic-gate rv = DEVFSADM_TRUE; 44157c478bd9Sstevel@tonic-gate } 44167c478bd9Sstevel@tonic-gate 44177c478bd9Sstevel@tonic-gate /* 44187c478bd9Sstevel@tonic-gate * The link exists. Add it to the database 44197c478bd9Sstevel@tonic-gate */ 44207c478bd9Sstevel@tonic-gate (void) di_devlink_add_link(devlink_cache, link, contents, type); 442145916cd2Sjpk if (system_labeled && (rv == DEVFSADM_TRUE) && 442245916cd2Sjpk strstr(devlink, DA_AUDIO_NAME) && contents) { 442345916cd2Sjpk (void) sscanf(contents, "%*[a-z]%d", &instance); 442445916cd2Sjpk (void) da_add_list(&devlist, devlink, instance, 442545916cd2Sjpk DA_ADD|DA_AUDIO); 442645916cd2Sjpk _update_devalloc_db(&devlist, 0, DA_ADD, NULL, root_dir); 442745916cd2Sjpk } 44287c478bd9Sstevel@tonic-gate free(contents); 44297c478bd9Sstevel@tonic-gate 44307c478bd9Sstevel@tonic-gate return (rv); 44317c478bd9Sstevel@tonic-gate } 44327c478bd9Sstevel@tonic-gate 44337c478bd9Sstevel@tonic-gate /* 44347c478bd9Sstevel@tonic-gate * devpath: Absolute path to /dev link 44357c478bd9Sstevel@tonic-gate * content_p: Returns malloced string (link content) 44367c478bd9Sstevel@tonic-gate * type_p: Returns link type: primary or secondary 44377c478bd9Sstevel@tonic-gate * devfs_path: Returns malloced string: /devices path w/out "/devices" 44387c478bd9Sstevel@tonic-gate * dangle: if set, check if link is dangling 44397c478bd9Sstevel@tonic-gate * Returns: 44407c478bd9Sstevel@tonic-gate * TRUE if dangling 44417c478bd9Sstevel@tonic-gate * FALSE if not or if caller doesn't care 44427c478bd9Sstevel@tonic-gate * Caller is assumed to have initialized pointer contents to NULL 44437c478bd9Sstevel@tonic-gate */ 44447c478bd9Sstevel@tonic-gate static int 44457c478bd9Sstevel@tonic-gate resolve_link(char *devpath, char **content_p, int *type_p, char **devfs_path, 44467c478bd9Sstevel@tonic-gate int dangle) 44477c478bd9Sstevel@tonic-gate { 44487c478bd9Sstevel@tonic-gate char contents[PATH_MAX + 1]; 44497c478bd9Sstevel@tonic-gate char stage_link[PATH_MAX + 1]; 44507c478bd9Sstevel@tonic-gate char *fcn = "resolve_link: "; 44517c478bd9Sstevel@tonic-gate char *ptr; 44527c478bd9Sstevel@tonic-gate int linksize; 44537c478bd9Sstevel@tonic-gate int rv = TRUE; 44547c478bd9Sstevel@tonic-gate struct stat sb; 44557c478bd9Sstevel@tonic-gate 44567c478bd9Sstevel@tonic-gate linksize = readlink(devpath, contents, PATH_MAX); 44577c478bd9Sstevel@tonic-gate 44587c478bd9Sstevel@tonic-gate if (linksize <= 0) { 44597c478bd9Sstevel@tonic-gate return (FALSE); 44607c478bd9Sstevel@tonic-gate } else { 44617c478bd9Sstevel@tonic-gate contents[linksize] = '\0'; 44627c478bd9Sstevel@tonic-gate } 44637c478bd9Sstevel@tonic-gate vprint(REMOVE_MID, "%s %s -> %s\n", fcn, devpath, contents); 44647c478bd9Sstevel@tonic-gate 44657c478bd9Sstevel@tonic-gate if (content_p) { 44667c478bd9Sstevel@tonic-gate *content_p = s_strdup(contents); 44677c478bd9Sstevel@tonic-gate } 44687c478bd9Sstevel@tonic-gate 44697c478bd9Sstevel@tonic-gate /* 44707c478bd9Sstevel@tonic-gate * Check to see if this is a link pointing to another link in /dev. The 44717c478bd9Sstevel@tonic-gate * cheap way to do this is to look for a lack of ../devices/. 44727c478bd9Sstevel@tonic-gate */ 44737c478bd9Sstevel@tonic-gate 44747c478bd9Sstevel@tonic-gate if (is_minor_node(contents, &ptr) == DEVFSADM_FALSE) { 44757c478bd9Sstevel@tonic-gate 44767c478bd9Sstevel@tonic-gate if (type_p) { 44777c478bd9Sstevel@tonic-gate *type_p = DI_SECONDARY_LINK; 44787c478bd9Sstevel@tonic-gate } 44797c478bd9Sstevel@tonic-gate 44807c478bd9Sstevel@tonic-gate /* 44817c478bd9Sstevel@tonic-gate * assume that linkcontents is really a pointer to another 44827c478bd9Sstevel@tonic-gate * link, and if so recurse and read its link contents. 44837c478bd9Sstevel@tonic-gate */ 44847c478bd9Sstevel@tonic-gate if (strncmp(contents, DEV "/", strlen(DEV) + 1) == 0) { 44857c478bd9Sstevel@tonic-gate (void) strcpy(stage_link, dev_dir); 44867c478bd9Sstevel@tonic-gate (void) strcat(stage_link, "/"); 44877c478bd9Sstevel@tonic-gate (void) strcpy(stage_link, 44887c478bd9Sstevel@tonic-gate &contents[strlen(DEV) + strlen("/")]); 44897c478bd9Sstevel@tonic-gate } else { 44907c478bd9Sstevel@tonic-gate if ((ptr = strrchr(devpath, '/')) == NULL) { 44917c478bd9Sstevel@tonic-gate vprint(REMOVE_MID, "%s%s -> %s invalid link. " 44927c478bd9Sstevel@tonic-gate "missing '/'\n", fcn, devpath, 44937c478bd9Sstevel@tonic-gate contents); 44947c478bd9Sstevel@tonic-gate return (TRUE); 44957c478bd9Sstevel@tonic-gate } 44967c478bd9Sstevel@tonic-gate *ptr = '\0'; 44977c478bd9Sstevel@tonic-gate (void) strcpy(stage_link, devpath); 44987c478bd9Sstevel@tonic-gate *ptr = '/'; 44997c478bd9Sstevel@tonic-gate (void) strcat(stage_link, "/"); 45007c478bd9Sstevel@tonic-gate (void) strcat(stage_link, contents); 45017c478bd9Sstevel@tonic-gate } 45027c478bd9Sstevel@tonic-gate return (resolve_link(stage_link, NULL, NULL, devfs_path, 45037c478bd9Sstevel@tonic-gate dangle)); 45047c478bd9Sstevel@tonic-gate } 45057c478bd9Sstevel@tonic-gate 45067c478bd9Sstevel@tonic-gate /* Current link points at a /devices minor node */ 45077c478bd9Sstevel@tonic-gate if (type_p) { 45087c478bd9Sstevel@tonic-gate *type_p = DI_PRIMARY_LINK; 45097c478bd9Sstevel@tonic-gate } 45107c478bd9Sstevel@tonic-gate 45117c478bd9Sstevel@tonic-gate if (devfs_path) 45127c478bd9Sstevel@tonic-gate *devfs_path = s_strdup(ptr); 45137c478bd9Sstevel@tonic-gate 45147c478bd9Sstevel@tonic-gate rv = FALSE; 45157c478bd9Sstevel@tonic-gate if (dangle) 45167c478bd9Sstevel@tonic-gate rv = (stat(ptr - strlen(DEVICES), &sb) == -1); 45177c478bd9Sstevel@tonic-gate 45187c478bd9Sstevel@tonic-gate vprint(REMOVE_MID, "%slink=%s, returning %s\n", fcn, 45197c478bd9Sstevel@tonic-gate devpath, ((rv == TRUE) ? "TRUE" : "FALSE")); 45207c478bd9Sstevel@tonic-gate 45217c478bd9Sstevel@tonic-gate return (rv); 45227c478bd9Sstevel@tonic-gate } 45237c478bd9Sstevel@tonic-gate 45247c478bd9Sstevel@tonic-gate /* 45257c478bd9Sstevel@tonic-gate * Returns the substring of interest, given a path. 45267c478bd9Sstevel@tonic-gate */ 45277c478bd9Sstevel@tonic-gate static char * 45287c478bd9Sstevel@tonic-gate alloc_cmp_str(const char *path, devfsadm_enumerate_t *dep) 45297c478bd9Sstevel@tonic-gate { 45307c478bd9Sstevel@tonic-gate uint_t match; 45317c478bd9Sstevel@tonic-gate char *np, *ap, *mp; 45327c478bd9Sstevel@tonic-gate char *cmp_str = NULL; 45337c478bd9Sstevel@tonic-gate char at[] = "@"; 45347c478bd9Sstevel@tonic-gate char *fcn = "alloc_cmp_str"; 45357c478bd9Sstevel@tonic-gate 45367c478bd9Sstevel@tonic-gate np = ap = mp = NULL; 45377c478bd9Sstevel@tonic-gate 45387c478bd9Sstevel@tonic-gate /* 45397c478bd9Sstevel@tonic-gate * extract match flags from the flags argument. 45407c478bd9Sstevel@tonic-gate */ 45417c478bd9Sstevel@tonic-gate match = (dep->flags & MATCH_MASK); 45427c478bd9Sstevel@tonic-gate 45437c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: enumeration match type: 0x%x" 45447c478bd9Sstevel@tonic-gate " path: %s\n", fcn, match, path); 45457c478bd9Sstevel@tonic-gate 45467c478bd9Sstevel@tonic-gate /* 45477c478bd9Sstevel@tonic-gate * MATCH_CALLBACK and MATCH_ALL are the only flags 45487c478bd9Sstevel@tonic-gate * which may be used if "path" is a /dev path 45497c478bd9Sstevel@tonic-gate */ 45507c478bd9Sstevel@tonic-gate if (match == MATCH_CALLBACK) { 45517c478bd9Sstevel@tonic-gate if (dep->sel_fcn == NULL) { 45527c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: invalid enumerate" 45537c478bd9Sstevel@tonic-gate " callback: path: %s\n", fcn, path); 45547c478bd9Sstevel@tonic-gate return (NULL); 45557c478bd9Sstevel@tonic-gate } 45567c478bd9Sstevel@tonic-gate cmp_str = dep->sel_fcn(path, dep->cb_arg); 45577c478bd9Sstevel@tonic-gate return (cmp_str); 45587c478bd9Sstevel@tonic-gate } 45597c478bd9Sstevel@tonic-gate 45607c478bd9Sstevel@tonic-gate cmp_str = s_strdup(path); 45617c478bd9Sstevel@tonic-gate 45627c478bd9Sstevel@tonic-gate if (match == MATCH_ALL) { 45637c478bd9Sstevel@tonic-gate return (cmp_str); 45647c478bd9Sstevel@tonic-gate } 45657c478bd9Sstevel@tonic-gate 45667c478bd9Sstevel@tonic-gate /* 45677c478bd9Sstevel@tonic-gate * The remaining flags make sense only for /devices 45687c478bd9Sstevel@tonic-gate * paths 45697c478bd9Sstevel@tonic-gate */ 45707c478bd9Sstevel@tonic-gate if ((mp = strrchr(cmp_str, ':')) == NULL) { 45717c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: invalid path: %s\n", 45727c478bd9Sstevel@tonic-gate fcn, path); 45737c478bd9Sstevel@tonic-gate goto err; 45747c478bd9Sstevel@tonic-gate } 45757c478bd9Sstevel@tonic-gate 45767c478bd9Sstevel@tonic-gate if (match == MATCH_MINOR) { 45777c478bd9Sstevel@tonic-gate /* A NULL "match_arg" values implies entire minor */ 45787c478bd9Sstevel@tonic-gate if (get_component(mp + 1, dep->match_arg) == NULL) { 45797c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: invalid minor component:" 45807c478bd9Sstevel@tonic-gate " path: %s\n", fcn, path); 45817c478bd9Sstevel@tonic-gate goto err; 45827c478bd9Sstevel@tonic-gate } 45837c478bd9Sstevel@tonic-gate return (cmp_str); 45847c478bd9Sstevel@tonic-gate } 45857c478bd9Sstevel@tonic-gate 45867c478bd9Sstevel@tonic-gate if ((np = strrchr(cmp_str, '/')) == NULL) { 45877c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: invalid path: %s\n", fcn, path); 45887c478bd9Sstevel@tonic-gate goto err; 45897c478bd9Sstevel@tonic-gate } 45907c478bd9Sstevel@tonic-gate 45917c478bd9Sstevel@tonic-gate if (match == MATCH_PARENT) { 45927c478bd9Sstevel@tonic-gate if (strcmp(cmp_str, "/") == 0) { 45937c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: invalid path: %s\n", 45947c478bd9Sstevel@tonic-gate fcn, path); 45957c478bd9Sstevel@tonic-gate goto err; 45967c478bd9Sstevel@tonic-gate } 45977c478bd9Sstevel@tonic-gate 45987c478bd9Sstevel@tonic-gate if (np == cmp_str) { 45997c478bd9Sstevel@tonic-gate *(np + 1) = '\0'; 46007c478bd9Sstevel@tonic-gate } else { 46017c478bd9Sstevel@tonic-gate *np = '\0'; 46027c478bd9Sstevel@tonic-gate } 46037c478bd9Sstevel@tonic-gate return (cmp_str); 46047c478bd9Sstevel@tonic-gate } 46057c478bd9Sstevel@tonic-gate 46067c478bd9Sstevel@tonic-gate /* ap can be NULL - Leaf address may not exist or be empty string */ 46077c478bd9Sstevel@tonic-gate ap = strchr(np+1, '@'); 46087c478bd9Sstevel@tonic-gate 46097c478bd9Sstevel@tonic-gate /* minor is no longer of interest */ 46107c478bd9Sstevel@tonic-gate *mp = '\0'; 46117c478bd9Sstevel@tonic-gate 46127c478bd9Sstevel@tonic-gate if (match == MATCH_NODE) { 46137c478bd9Sstevel@tonic-gate if (ap) 46147c478bd9Sstevel@tonic-gate *ap = '\0'; 46157c478bd9Sstevel@tonic-gate return (cmp_str); 46167c478bd9Sstevel@tonic-gate } else if (match == MATCH_ADDR) { 46177c478bd9Sstevel@tonic-gate /* 46187c478bd9Sstevel@tonic-gate * The empty string is a valid address. The only MATCH_ADDR 46197c478bd9Sstevel@tonic-gate * allowed in this case is against the whole address or 46207c478bd9Sstevel@tonic-gate * the first component of the address (match_arg=NULL/"0"/"1") 46217c478bd9Sstevel@tonic-gate * Note that in this case, the path won't have an "@" 46227c478bd9Sstevel@tonic-gate * As a result ap will be NULL. We fake up an ap = @'\0' 46237c478bd9Sstevel@tonic-gate * so that get_component() will work correctly. 46247c478bd9Sstevel@tonic-gate */ 46257c478bd9Sstevel@tonic-gate if (ap == NULL) { 46267c478bd9Sstevel@tonic-gate ap = at; 46277c478bd9Sstevel@tonic-gate } 46287c478bd9Sstevel@tonic-gate 46297c478bd9Sstevel@tonic-gate if (get_component(ap + 1, dep->match_arg) == NULL) { 46307c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: invalid leaf addr. component:" 46317c478bd9Sstevel@tonic-gate " path: %s\n", fcn, path); 46327c478bd9Sstevel@tonic-gate goto err; 46337c478bd9Sstevel@tonic-gate } 46347c478bd9Sstevel@tonic-gate return (cmp_str); 46357c478bd9Sstevel@tonic-gate } 46367c478bd9Sstevel@tonic-gate 46377c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: invalid enumeration flags: 0x%x" 46387c478bd9Sstevel@tonic-gate " path: %s\n", fcn, dep->flags, path); 46397c478bd9Sstevel@tonic-gate 46407c478bd9Sstevel@tonic-gate /*FALLTHRU*/ 46417c478bd9Sstevel@tonic-gate err: 46427c478bd9Sstevel@tonic-gate free(cmp_str); 46437c478bd9Sstevel@tonic-gate return (NULL); 46447c478bd9Sstevel@tonic-gate } 46457c478bd9Sstevel@tonic-gate 46467c478bd9Sstevel@tonic-gate 46477c478bd9Sstevel@tonic-gate /* 46487c478bd9Sstevel@tonic-gate * "str" is expected to be a string with components separated by ',' 46497c478bd9Sstevel@tonic-gate * The terminating null char is considered a separator. 46507c478bd9Sstevel@tonic-gate * get_component() will remove the portion of the string beyond 46517c478bd9Sstevel@tonic-gate * the component indicated. 46527c478bd9Sstevel@tonic-gate * If comp_str is NULL, the entire "str" is returned. 46537c478bd9Sstevel@tonic-gate */ 46547c478bd9Sstevel@tonic-gate static char * 46557c478bd9Sstevel@tonic-gate get_component(char *str, const char *comp_str) 46567c478bd9Sstevel@tonic-gate { 46577c478bd9Sstevel@tonic-gate long comp; 46587c478bd9Sstevel@tonic-gate char *cp; 46597c478bd9Sstevel@tonic-gate 46607c478bd9Sstevel@tonic-gate if (str == NULL) { 46617c478bd9Sstevel@tonic-gate return (NULL); 46627c478bd9Sstevel@tonic-gate } 46637c478bd9Sstevel@tonic-gate 46647c478bd9Sstevel@tonic-gate if (comp_str == NULL) { 46657c478bd9Sstevel@tonic-gate return (str); 46667c478bd9Sstevel@tonic-gate } 46677c478bd9Sstevel@tonic-gate 46687c478bd9Sstevel@tonic-gate errno = 0; 46697c478bd9Sstevel@tonic-gate comp = strtol(comp_str, &cp, 10); 46707c478bd9Sstevel@tonic-gate if (errno != 0 || *cp != '\0' || comp < 0) { 46717c478bd9Sstevel@tonic-gate return (NULL); 46727c478bd9Sstevel@tonic-gate } 46737c478bd9Sstevel@tonic-gate 46747c478bd9Sstevel@tonic-gate if (comp == 0) 46757c478bd9Sstevel@tonic-gate return (str); 46767c478bd9Sstevel@tonic-gate 46777c478bd9Sstevel@tonic-gate for (cp = str; ; cp++) { 46787c478bd9Sstevel@tonic-gate if (*cp == ',' || *cp == '\0') 46797c478bd9Sstevel@tonic-gate comp--; 46807c478bd9Sstevel@tonic-gate if (*cp == '\0' || comp <= 0) { 46817c478bd9Sstevel@tonic-gate break; 46827c478bd9Sstevel@tonic-gate } 46837c478bd9Sstevel@tonic-gate } 46847c478bd9Sstevel@tonic-gate 46857c478bd9Sstevel@tonic-gate if (comp == 0) { 46867c478bd9Sstevel@tonic-gate *cp = '\0'; 46877c478bd9Sstevel@tonic-gate } else { 46887c478bd9Sstevel@tonic-gate str = NULL; 46897c478bd9Sstevel@tonic-gate } 46907c478bd9Sstevel@tonic-gate 46917c478bd9Sstevel@tonic-gate return (str); 46927c478bd9Sstevel@tonic-gate } 46937c478bd9Sstevel@tonic-gate 46947c478bd9Sstevel@tonic-gate 46957c478bd9Sstevel@tonic-gate /* 46967c478bd9Sstevel@tonic-gate * Enumerate serves as a generic counter as well as a means to determine 46977c478bd9Sstevel@tonic-gate * logical unit/controller numbers for such items as disk and tape 46987c478bd9Sstevel@tonic-gate * drives. 46997c478bd9Sstevel@tonic-gate * 47007c478bd9Sstevel@tonic-gate * rules[] is an array of devfsadm_enumerate_t structures which defines 47017c478bd9Sstevel@tonic-gate * the enumeration rules to be used for a specified set of links in /dev. 47027c478bd9Sstevel@tonic-gate * The set of links is specified through regular expressions (of the flavor 47037c478bd9Sstevel@tonic-gate * described in regex(5)). These regular expressions are used to determine 47047c478bd9Sstevel@tonic-gate * the set of links in /dev to examine. The last path component in these 47057c478bd9Sstevel@tonic-gate * regular expressions MUST contain a parenthesized subexpression surrounding 47067c478bd9Sstevel@tonic-gate * the RE which is to be considered the enumerating component. The subexp 47077c478bd9Sstevel@tonic-gate * member in a rule is the subexpression number of the enumerating 47087c478bd9Sstevel@tonic-gate * component. Subexpressions in the last path component are numbered starting 47097c478bd9Sstevel@tonic-gate * from 1. 47107c478bd9Sstevel@tonic-gate * 47117c478bd9Sstevel@tonic-gate * A cache of current id assignments is built up from existing symlinks and 47127c478bd9Sstevel@tonic-gate * new assignments use the lowest unused id. Assignments are based on a 47137c478bd9Sstevel@tonic-gate * match of a specified substring of a symlink's contents. If the specified 47147c478bd9Sstevel@tonic-gate * component for the devfs_path argument matches the corresponding substring 47157c478bd9Sstevel@tonic-gate * for a existing symlink's contents, the cached id is returned. Else, a new 47167c478bd9Sstevel@tonic-gate * id is created and returned in *buf. *buf must be freed by the caller. 47177c478bd9Sstevel@tonic-gate * 47187c478bd9Sstevel@tonic-gate * An id assignment may be governed by a combination of rules, each rule 47197c478bd9Sstevel@tonic-gate * applicable to a different subset of links in /dev. For example, controller 47207c478bd9Sstevel@tonic-gate * numbers may be determined by a combination of disk symlinks in /dev/[r]dsk 47217c478bd9Sstevel@tonic-gate * and controller symlinks in /dev/cfg, with the two sets requiring different 47227c478bd9Sstevel@tonic-gate * rules to derive the "substring of interest". In such cases, the rules 47237c478bd9Sstevel@tonic-gate * array will have more than one element. 47247c478bd9Sstevel@tonic-gate */ 47257c478bd9Sstevel@tonic-gate int 47267c478bd9Sstevel@tonic-gate devfsadm_enumerate_int(char *devfs_path, int index, char **buf, 47277c478bd9Sstevel@tonic-gate devfsadm_enumerate_t rules[], int nrules) 47287c478bd9Sstevel@tonic-gate { 47297c478bd9Sstevel@tonic-gate return (find_enum_id(rules, nrules, 47307c478bd9Sstevel@tonic-gate devfs_path, index, "0", INTEGER, buf, 0)); 47317c478bd9Sstevel@tonic-gate } 47327c478bd9Sstevel@tonic-gate 47337c478bd9Sstevel@tonic-gate int 47347c478bd9Sstevel@tonic-gate disk_enumerate_int(char *devfs_path, int index, char **buf, 47357c478bd9Sstevel@tonic-gate devfsadm_enumerate_t rules[], int nrules) 47367c478bd9Sstevel@tonic-gate { 47377c478bd9Sstevel@tonic-gate return (find_enum_id(rules, nrules, 47387c478bd9Sstevel@tonic-gate devfs_path, index, "0", INTEGER, buf, 1)); 47397c478bd9Sstevel@tonic-gate } 47407c478bd9Sstevel@tonic-gate 47417c478bd9Sstevel@tonic-gate /* 47427c478bd9Sstevel@tonic-gate * Same as above, but allows a starting value to be specified. 47437c478bd9Sstevel@tonic-gate * Private to devfsadm.... used by devlinks. 47447c478bd9Sstevel@tonic-gate */ 47457c478bd9Sstevel@tonic-gate static int 47467c478bd9Sstevel@tonic-gate devfsadm_enumerate_int_start(char *devfs_path, int index, char **buf, 47477c478bd9Sstevel@tonic-gate devfsadm_enumerate_t rules[], int nrules, char *start) 47487c478bd9Sstevel@tonic-gate { 47497c478bd9Sstevel@tonic-gate return (find_enum_id(rules, nrules, 47507c478bd9Sstevel@tonic-gate devfs_path, index, start, INTEGER, buf, 0)); 47517c478bd9Sstevel@tonic-gate } 47527c478bd9Sstevel@tonic-gate 47537c478bd9Sstevel@tonic-gate /* 47547c478bd9Sstevel@tonic-gate * devfsadm_enumerate_char serves as a generic counter returning 47557c478bd9Sstevel@tonic-gate * a single letter. 47567c478bd9Sstevel@tonic-gate */ 47577c478bd9Sstevel@tonic-gate int 47587c478bd9Sstevel@tonic-gate devfsadm_enumerate_char(char *devfs_path, int index, char **buf, 47597c478bd9Sstevel@tonic-gate devfsadm_enumerate_t rules[], int nrules) 47607c478bd9Sstevel@tonic-gate { 47617c478bd9Sstevel@tonic-gate return (find_enum_id(rules, nrules, 47627c478bd9Sstevel@tonic-gate devfs_path, index, "a", LETTER, buf, 0)); 47637c478bd9Sstevel@tonic-gate } 47647c478bd9Sstevel@tonic-gate 47657c478bd9Sstevel@tonic-gate /* 47667c478bd9Sstevel@tonic-gate * Same as above, but allows a starting char to be specified. 47677c478bd9Sstevel@tonic-gate * Private to devfsadm - used by ports module (port_link.c) 47687c478bd9Sstevel@tonic-gate */ 47697c478bd9Sstevel@tonic-gate int 47707c478bd9Sstevel@tonic-gate devfsadm_enumerate_char_start(char *devfs_path, int index, char **buf, 47717c478bd9Sstevel@tonic-gate devfsadm_enumerate_t rules[], int nrules, char *start) 47727c478bd9Sstevel@tonic-gate { 47737c478bd9Sstevel@tonic-gate return (find_enum_id(rules, nrules, 47747c478bd9Sstevel@tonic-gate devfs_path, index, start, LETTER, buf, 0)); 47757c478bd9Sstevel@tonic-gate } 47767c478bd9Sstevel@tonic-gate 47777c478bd9Sstevel@tonic-gate 47787c478bd9Sstevel@tonic-gate /* 47797c478bd9Sstevel@tonic-gate * For a given numeral_set (see get_cached_set for desc of numeral_set), 47807c478bd9Sstevel@tonic-gate * search all cached entries looking for matches on a specified substring 47817c478bd9Sstevel@tonic-gate * of devfs_path. The substring is derived from devfs_path based on the 47827c478bd9Sstevel@tonic-gate * rule specified by "index". If a match is found on a cached entry, 47837c478bd9Sstevel@tonic-gate * return the enumerated id in buf. Otherwise, create a new id by calling 47847c478bd9Sstevel@tonic-gate * new_id, then cache and return that entry. 47857c478bd9Sstevel@tonic-gate */ 47867c478bd9Sstevel@tonic-gate static int 47877c478bd9Sstevel@tonic-gate find_enum_id(devfsadm_enumerate_t rules[], int nrules, 47887c478bd9Sstevel@tonic-gate char *devfs_path, int index, char *min, int type, char **buf, 47897c478bd9Sstevel@tonic-gate int multiple) 47907c478bd9Sstevel@tonic-gate { 47917c478bd9Sstevel@tonic-gate numeral_t *matchnp; 47927c478bd9Sstevel@tonic-gate numeral_t *numeral; 47937c478bd9Sstevel@tonic-gate int matchcount = 0; 47947c478bd9Sstevel@tonic-gate char *cmp_str; 47957c478bd9Sstevel@tonic-gate char *fcn = "find_enum_id"; 47967c478bd9Sstevel@tonic-gate numeral_set_t *set; 47977c478bd9Sstevel@tonic-gate 47987c478bd9Sstevel@tonic-gate if (rules == NULL) { 47997c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: no rules. path: %s\n", 48007c478bd9Sstevel@tonic-gate fcn, devfs_path ? devfs_path : "<NULL path>"); 48017c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 48027c478bd9Sstevel@tonic-gate } 48037c478bd9Sstevel@tonic-gate 48047c478bd9Sstevel@tonic-gate if (devfs_path == NULL) { 48057c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: NULL path\n", fcn); 48067c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 48077c478bd9Sstevel@tonic-gate } 48087c478bd9Sstevel@tonic-gate 48097c478bd9Sstevel@tonic-gate if (nrules <= 0 || index < 0 || index >= nrules || buf == NULL) { 48107c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: invalid arguments. path: %s\n", 48117c478bd9Sstevel@tonic-gate fcn, devfs_path); 48127c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 48137c478bd9Sstevel@tonic-gate } 48147c478bd9Sstevel@tonic-gate 48157c478bd9Sstevel@tonic-gate *buf = NULL; 48167c478bd9Sstevel@tonic-gate 48177c478bd9Sstevel@tonic-gate 48187c478bd9Sstevel@tonic-gate cmp_str = alloc_cmp_str(devfs_path, &rules[index]); 48197c478bd9Sstevel@tonic-gate if (cmp_str == NULL) { 48207c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 48217c478bd9Sstevel@tonic-gate } 48227c478bd9Sstevel@tonic-gate 48237c478bd9Sstevel@tonic-gate if ((set = get_enum_cache(rules, nrules)) == NULL) { 48247c478bd9Sstevel@tonic-gate free(cmp_str); 48257c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 48267c478bd9Sstevel@tonic-gate } 48277c478bd9Sstevel@tonic-gate 48287c478bd9Sstevel@tonic-gate assert(nrules == set->re_count); 48297c478bd9Sstevel@tonic-gate 48307c478bd9Sstevel@tonic-gate /* 48317c478bd9Sstevel@tonic-gate * Check and see if a matching entry is already cached. 48327c478bd9Sstevel@tonic-gate */ 48337c478bd9Sstevel@tonic-gate matchcount = lookup_enum_cache(set, cmp_str, rules, index, 48347c478bd9Sstevel@tonic-gate &matchnp); 48357c478bd9Sstevel@tonic-gate 48367c478bd9Sstevel@tonic-gate if (matchcount < 0 || matchcount > 1) { 48377c478bd9Sstevel@tonic-gate free(cmp_str); 48387c478bd9Sstevel@tonic-gate if (multiple && matchcount > 1) 48397c478bd9Sstevel@tonic-gate return (DEVFSADM_MULTIPLE); 48407c478bd9Sstevel@tonic-gate else 48417c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 48427c478bd9Sstevel@tonic-gate } 48437c478bd9Sstevel@tonic-gate 48447c478bd9Sstevel@tonic-gate /* if matching entry already cached, return it */ 48457c478bd9Sstevel@tonic-gate if (matchcount == 1) { 48468d483882Smlf /* should never create a link with a reserved ID */ 48478d483882Smlf vprint(ENUM_MID, "%s: 1 match w/ ID: %s\n", fcn, matchnp->id); 48488d483882Smlf assert(matchnp->flags == 0); 48497c478bd9Sstevel@tonic-gate *buf = s_strdup(matchnp->id); 48507c478bd9Sstevel@tonic-gate free(cmp_str); 48517c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 48527c478bd9Sstevel@tonic-gate } 48537c478bd9Sstevel@tonic-gate 48547c478bd9Sstevel@tonic-gate /* 48557c478bd9Sstevel@tonic-gate * no cached entry, initialize a numeral struct 48567c478bd9Sstevel@tonic-gate * by calling new_id() and cache onto the numeral_set 48577c478bd9Sstevel@tonic-gate */ 48587c478bd9Sstevel@tonic-gate numeral = s_malloc(sizeof (numeral_t)); 48597c478bd9Sstevel@tonic-gate numeral->id = new_id(set->headnumeral, type, min); 48607c478bd9Sstevel@tonic-gate numeral->full_path = s_strdup(devfs_path); 48617c478bd9Sstevel@tonic-gate numeral->rule_index = index; 48627c478bd9Sstevel@tonic-gate numeral->cmp_str = cmp_str; 48637c478bd9Sstevel@tonic-gate cmp_str = NULL; 48648d483882Smlf numeral->flags = 0; 48658d483882Smlf vprint(RSRV_MID, "%s: alloc new_id: %s numeral flags = %d\n", 48668d483882Smlf fcn, numeral->id, numeral->flags); 48678d483882Smlf 48687c478bd9Sstevel@tonic-gate 48697c478bd9Sstevel@tonic-gate /* insert to head of list for fast lookups */ 48707c478bd9Sstevel@tonic-gate numeral->next = set->headnumeral; 48717c478bd9Sstevel@tonic-gate set->headnumeral = numeral; 48727c478bd9Sstevel@tonic-gate 48737c478bd9Sstevel@tonic-gate *buf = s_strdup(numeral->id); 48747c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 48757c478bd9Sstevel@tonic-gate } 48767c478bd9Sstevel@tonic-gate 48777c478bd9Sstevel@tonic-gate 48787c478bd9Sstevel@tonic-gate /* 48797c478bd9Sstevel@tonic-gate * Looks up the specified cache for a match with a specified string 48807c478bd9Sstevel@tonic-gate * Returns: 48817c478bd9Sstevel@tonic-gate * -1 : on error. 48827c478bd9Sstevel@tonic-gate * 0/1/2 : Number of matches. 48837c478bd9Sstevel@tonic-gate * Returns the matching element only if there is a single match. 48847c478bd9Sstevel@tonic-gate * If the "uncached" flag is set, derives the "cmp_str" afresh 48857c478bd9Sstevel@tonic-gate * for the match instead of using cached values. 48867c478bd9Sstevel@tonic-gate */ 48877c478bd9Sstevel@tonic-gate static int 48887c478bd9Sstevel@tonic-gate lookup_enum_cache(numeral_set_t *set, char *cmp_str, 48897c478bd9Sstevel@tonic-gate devfsadm_enumerate_t rules[], int index, numeral_t **matchnpp) 48907c478bd9Sstevel@tonic-gate { 48917c478bd9Sstevel@tonic-gate int matchcount = 0, rv = -1; 48927c478bd9Sstevel@tonic-gate int uncached; 48937c478bd9Sstevel@tonic-gate numeral_t *np; 48947c478bd9Sstevel@tonic-gate char *fcn = "lookup_enum_cache"; 48957c478bd9Sstevel@tonic-gate char *cp; 48967c478bd9Sstevel@tonic-gate 48977c478bd9Sstevel@tonic-gate *matchnpp = NULL; 48987c478bd9Sstevel@tonic-gate 48997c478bd9Sstevel@tonic-gate assert(index < set->re_count); 49007c478bd9Sstevel@tonic-gate 49017c478bd9Sstevel@tonic-gate if (cmp_str == NULL) { 49027c478bd9Sstevel@tonic-gate return (-1); 49037c478bd9Sstevel@tonic-gate } 49047c478bd9Sstevel@tonic-gate 49057c478bd9Sstevel@tonic-gate uncached = 0; 49067c478bd9Sstevel@tonic-gate if ((rules[index].flags & MATCH_UNCACHED) == MATCH_UNCACHED) { 49077c478bd9Sstevel@tonic-gate uncached = 1; 49087c478bd9Sstevel@tonic-gate } 49097c478bd9Sstevel@tonic-gate 49107c478bd9Sstevel@tonic-gate /* 49117c478bd9Sstevel@tonic-gate * Check and see if a matching entry is already cached. 49127c478bd9Sstevel@tonic-gate */ 49137c478bd9Sstevel@tonic-gate for (np = set->headnumeral; np != NULL; np = np->next) { 49148d483882Smlf 49158d483882Smlf /* 49168d483882Smlf * Skip reserved IDs 49178d483882Smlf */ 49188d483882Smlf if (np->flags & NUMERAL_RESERVED) { 49198d483882Smlf vprint(RSRV_MID, "lookup_enum_cache: " 49208d483882Smlf "Cannot Match with reserved ID (%s), " 49218d483882Smlf "skipping\n", np->id); 49228d483882Smlf assert(np->flags == NUMERAL_RESERVED); 49238d483882Smlf continue; 49248d483882Smlf } else { 49258d483882Smlf vprint(RSRV_MID, "lookup_enum_cache: " 49268d483882Smlf "Attempting match with numeral ID: %s" 49278d483882Smlf " numeral flags = %d\n", np->id, np->flags); 49288d483882Smlf assert(np->flags == 0); 49298d483882Smlf } 49308d483882Smlf 49317c478bd9Sstevel@tonic-gate if (np->cmp_str == NULL) { 49327c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: invalid entry in enumerate" 49337c478bd9Sstevel@tonic-gate " cache. path: %s\n", fcn, np->full_path); 49347c478bd9Sstevel@tonic-gate return (-1); 49357c478bd9Sstevel@tonic-gate } 49367c478bd9Sstevel@tonic-gate 49377c478bd9Sstevel@tonic-gate if (uncached) { 49387c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "%s: bypassing enumerate cache." 49397c478bd9Sstevel@tonic-gate " path: %s\n", fcn, cmp_str); 49407c478bd9Sstevel@tonic-gate cp = alloc_cmp_str(np->full_path, 49417c478bd9Sstevel@tonic-gate &rules[np->rule_index]); 49427c478bd9Sstevel@tonic-gate if (cp == NULL) 49437c478bd9Sstevel@tonic-gate return (-1); 49447c478bd9Sstevel@tonic-gate rv = strcmp(cmp_str, cp); 49457c478bd9Sstevel@tonic-gate free(cp); 49467c478bd9Sstevel@tonic-gate } else { 49477c478bd9Sstevel@tonic-gate rv = strcmp(cmp_str, np->cmp_str); 49487c478bd9Sstevel@tonic-gate } 49497c478bd9Sstevel@tonic-gate 49507c478bd9Sstevel@tonic-gate if (rv == 0) { 49517c478bd9Sstevel@tonic-gate if (matchcount++ != 0) { 49527c478bd9Sstevel@tonic-gate break; /* more than 1 match. */ 49537c478bd9Sstevel@tonic-gate } 49547c478bd9Sstevel@tonic-gate *matchnpp = np; 49557c478bd9Sstevel@tonic-gate } 49567c478bd9Sstevel@tonic-gate } 49577c478bd9Sstevel@tonic-gate 49587c478bd9Sstevel@tonic-gate return (matchcount); 49597c478bd9Sstevel@tonic-gate } 49607c478bd9Sstevel@tonic-gate 49617c478bd9Sstevel@tonic-gate #ifdef DEBUG 49627c478bd9Sstevel@tonic-gate static void 49637c478bd9Sstevel@tonic-gate dump_enum_cache(numeral_set_t *setp) 49647c478bd9Sstevel@tonic-gate { 49657c478bd9Sstevel@tonic-gate int i; 49667c478bd9Sstevel@tonic-gate numeral_t *np; 49677c478bd9Sstevel@tonic-gate char *fcn = "dump_enum_cache"; 49687c478bd9Sstevel@tonic-gate 49697c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: re_count = %d\n", fcn, setp->re_count); 49707c478bd9Sstevel@tonic-gate for (i = 0; i < setp->re_count; i++) { 49717c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: re[%d] = %s\n", fcn, i, setp->re[i]); 49727c478bd9Sstevel@tonic-gate } 49737c478bd9Sstevel@tonic-gate 49747c478bd9Sstevel@tonic-gate for (np = setp->headnumeral; np != NULL; np = np->next) { 49757c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: id: %s\n", fcn, np->id); 49767c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: full_path: %s\n", fcn, np->full_path); 49777c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: rule_index: %d\n", fcn, np->rule_index); 49787c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: cmp_str: %s\n", fcn, np->cmp_str); 49798d483882Smlf vprint(ENUM_MID, "%s: flags: %d\n", fcn, np->flags); 49807c478bd9Sstevel@tonic-gate } 49817c478bd9Sstevel@tonic-gate } 49827c478bd9Sstevel@tonic-gate #endif 49837c478bd9Sstevel@tonic-gate 49847c478bd9Sstevel@tonic-gate /* 49857c478bd9Sstevel@tonic-gate * For a given set of regular expressions in rules[], this function returns 49867c478bd9Sstevel@tonic-gate * either a previously cached struct numeral_set or it will create and 49877c478bd9Sstevel@tonic-gate * cache a new struct numeral_set. There is only one struct numeral_set 49887c478bd9Sstevel@tonic-gate * for the combination of REs present in rules[]. Each numeral_set contains 49897c478bd9Sstevel@tonic-gate * the regular expressions in rules[] used for cache selection AND a linked 49907c478bd9Sstevel@tonic-gate * list of struct numerals, ONE FOR EACH *UNIQUE* numeral or character ID 49917c478bd9Sstevel@tonic-gate * selected by the grouping parenthesized subexpression found in the last 49927c478bd9Sstevel@tonic-gate * path component of each rules[].re. For example, the RE: "rmt/([0-9]+)" 49937c478bd9Sstevel@tonic-gate * selects all the logical nodes of the correct form in dev/rmt/. 49947c478bd9Sstevel@tonic-gate * Each rmt/X will store a *single* struct numeral... ie 0, 1, 2 each get a 49957c478bd9Sstevel@tonic-gate * single struct numeral. There is no need to store more than a single logical 49967c478bd9Sstevel@tonic-gate * node matching X since the information desired in the devfspath would be 49977c478bd9Sstevel@tonic-gate * identical for the portion of the devfspath of interest. (the part up to, 49987c478bd9Sstevel@tonic-gate * but not including the minor name in this example.) 49997c478bd9Sstevel@tonic-gate * 50007c478bd9Sstevel@tonic-gate * If the given numeral_set is not yet cached, call enumerate_recurse to 50017c478bd9Sstevel@tonic-gate * create it. 50027c478bd9Sstevel@tonic-gate */ 50037c478bd9Sstevel@tonic-gate static numeral_set_t * 50047c478bd9Sstevel@tonic-gate get_enum_cache(devfsadm_enumerate_t rules[], int nrules) 50057c478bd9Sstevel@tonic-gate { 50067c478bd9Sstevel@tonic-gate /* linked list of numeral sets */ 50077c478bd9Sstevel@tonic-gate numeral_set_t *setp; 50087c478bd9Sstevel@tonic-gate int i; 50098d483882Smlf int ret; 50107c478bd9Sstevel@tonic-gate char *path_left; 50118d483882Smlf enumerate_file_t *entry; 50127c478bd9Sstevel@tonic-gate char *fcn = "get_enum_cache"; 50137c478bd9Sstevel@tonic-gate 50147c478bd9Sstevel@tonic-gate /* 50157c478bd9Sstevel@tonic-gate * See if we've already cached this numeral set. 50167c478bd9Sstevel@tonic-gate */ 50177c478bd9Sstevel@tonic-gate for (setp = head_numeral_set; setp != NULL; setp = setp->next) { 50187c478bd9Sstevel@tonic-gate /* 50197c478bd9Sstevel@tonic-gate * check all regexp's passed in function against 50207c478bd9Sstevel@tonic-gate * those in cached set. 50217c478bd9Sstevel@tonic-gate */ 50227c478bd9Sstevel@tonic-gate if (nrules != setp->re_count) { 50237c478bd9Sstevel@tonic-gate continue; 50247c478bd9Sstevel@tonic-gate } 50257c478bd9Sstevel@tonic-gate 50267c478bd9Sstevel@tonic-gate for (i = 0; i < nrules; i++) { 50277c478bd9Sstevel@tonic-gate if (strcmp(setp->re[i], rules[i].re) != 0) { 50287c478bd9Sstevel@tonic-gate break; 50297c478bd9Sstevel@tonic-gate } 50307c478bd9Sstevel@tonic-gate } 50317c478bd9Sstevel@tonic-gate 50327c478bd9Sstevel@tonic-gate if (i == nrules) { 50337c478bd9Sstevel@tonic-gate return (setp); 50347c478bd9Sstevel@tonic-gate } 50357c478bd9Sstevel@tonic-gate } 50367c478bd9Sstevel@tonic-gate 50377c478bd9Sstevel@tonic-gate /* 50387c478bd9Sstevel@tonic-gate * If the MATCH_UNCACHED flag is set, we should not be here. 50397c478bd9Sstevel@tonic-gate */ 50407c478bd9Sstevel@tonic-gate for (i = 0; i < nrules; i++) { 50417c478bd9Sstevel@tonic-gate if ((rules[i].flags & MATCH_UNCACHED) == MATCH_UNCACHED) { 50427c478bd9Sstevel@tonic-gate vprint(ENUM_MID, "%s: invalid enumeration flags: " 50437c478bd9Sstevel@tonic-gate "0x%x\n", fcn, rules[i].flags); 50447c478bd9Sstevel@tonic-gate return (NULL); 50457c478bd9Sstevel@tonic-gate } 50467c478bd9Sstevel@tonic-gate } 50477c478bd9Sstevel@tonic-gate 50487c478bd9Sstevel@tonic-gate /* 50497c478bd9Sstevel@tonic-gate * Since we made it here, we have not yet cached the given set of 50507c478bd9Sstevel@tonic-gate * logical nodes matching the passed re. Create a cached entry 50517c478bd9Sstevel@tonic-gate * struct numeral_set and populate it with a minimal set of 50527c478bd9Sstevel@tonic-gate * logical nodes from /dev. 50537c478bd9Sstevel@tonic-gate */ 50547c478bd9Sstevel@tonic-gate 50557c478bd9Sstevel@tonic-gate setp = s_malloc(sizeof (numeral_set_t)); 50567c478bd9Sstevel@tonic-gate setp->re = s_malloc(sizeof (char *) * nrules); 50577c478bd9Sstevel@tonic-gate for (i = 0; i < nrules; i++) { 50587c478bd9Sstevel@tonic-gate setp->re[i] = s_strdup(rules[i].re); 50597c478bd9Sstevel@tonic-gate } 50607c478bd9Sstevel@tonic-gate setp->re_count = nrules; 50617c478bd9Sstevel@tonic-gate setp->headnumeral = NULL; 50627c478bd9Sstevel@tonic-gate 50637c478bd9Sstevel@tonic-gate /* put this new cached set on the cached set list */ 50647c478bd9Sstevel@tonic-gate setp->next = head_numeral_set; 50657c478bd9Sstevel@tonic-gate head_numeral_set = setp; 50667c478bd9Sstevel@tonic-gate 50677c478bd9Sstevel@tonic-gate /* 50688d483882Smlf * For each RE, search the "reserved" list to create numeral IDs that 50698d483882Smlf * are reserved. 50708d483882Smlf */ 50718d483882Smlf for (entry = enumerate_reserved; entry; entry = entry->er_next) { 50728d483882Smlf 50738d483882Smlf vprint(RSRV_MID, "parsing rstring: %s\n", entry->er_file); 50748d483882Smlf 50758d483882Smlf for (i = 0; i < nrules; i++) { 50768d483882Smlf path_left = s_strdup(setp->re[i]); 50778d483882Smlf vprint(RSRV_MID, "parsing rule RE: %s\n", path_left); 50788d483882Smlf ret = enumerate_parse(entry->er_file, path_left, 50798d483882Smlf setp, rules, i); 50808d483882Smlf free(path_left); 50818d483882Smlf if (ret == 1) { 50828d483882Smlf /* 50838d483882Smlf * We found the reserved ID for this entry. 50848d483882Smlf * We still keep the entry since it is needed 50858d483882Smlf * by the new link bypass code in disks 50868d483882Smlf */ 50878d483882Smlf vprint(RSRV_MID, "found rsv ID: rstring: %s " 50888d483882Smlf "rule RE: %s\n", entry->er_file, path_left); 50898d483882Smlf break; 50908d483882Smlf } 50918d483882Smlf } 50928d483882Smlf } 50938d483882Smlf 50948d483882Smlf /* 50957c478bd9Sstevel@tonic-gate * For each RE, search disk and cache any matches on the 5096facf4a8dSllai1 * numeral list. 50977c478bd9Sstevel@tonic-gate */ 50987c478bd9Sstevel@tonic-gate for (i = 0; i < nrules; i++) { 50997c478bd9Sstevel@tonic-gate path_left = s_strdup(setp->re[i]); 5100facf4a8dSllai1 enumerate_recurse(dev_dir, path_left, setp, rules, i); 51017c478bd9Sstevel@tonic-gate free(path_left); 51027c478bd9Sstevel@tonic-gate } 51037c478bd9Sstevel@tonic-gate 51047c478bd9Sstevel@tonic-gate #ifdef DEBUG 51057c478bd9Sstevel@tonic-gate dump_enum_cache(setp); 51067c478bd9Sstevel@tonic-gate #endif 51077c478bd9Sstevel@tonic-gate 51087c478bd9Sstevel@tonic-gate return (setp); 51097c478bd9Sstevel@tonic-gate } 51107c478bd9Sstevel@tonic-gate 51117c478bd9Sstevel@tonic-gate 51127c478bd9Sstevel@tonic-gate /* 51137c478bd9Sstevel@tonic-gate * This function stats the pathname namebuf. If this is a directory 51147c478bd9Sstevel@tonic-gate * entry, we recurse down dname/fname until we find the first symbolic 51157c478bd9Sstevel@tonic-gate * link, and then stat and return it. This is valid for the same reason 51167c478bd9Sstevel@tonic-gate * that we only need to read a single pathname for multiple matching 51177c478bd9Sstevel@tonic-gate * logical ID's... ie, all the logical nodes should contain identical 51187c478bd9Sstevel@tonic-gate * physical paths for the parts we are interested. 51197c478bd9Sstevel@tonic-gate */ 51207c478bd9Sstevel@tonic-gate int 51217c478bd9Sstevel@tonic-gate get_stat_info(char *namebuf, struct stat *sb) 51227c478bd9Sstevel@tonic-gate { 51237c478bd9Sstevel@tonic-gate char *cp; 512473de625bSjg finddevhdl_t fhandle; 512573de625bSjg const char *fp; 51267c478bd9Sstevel@tonic-gate 51277c478bd9Sstevel@tonic-gate if (lstat(namebuf, sb) < 0) { 51287c478bd9Sstevel@tonic-gate (void) err_print(LSTAT_FAILED, namebuf, strerror(errno)); 51297c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 51307c478bd9Sstevel@tonic-gate } 51317c478bd9Sstevel@tonic-gate 51327c478bd9Sstevel@tonic-gate if ((sb->st_mode & S_IFMT) == S_IFLNK) { 51337c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 51347c478bd9Sstevel@tonic-gate } 51357c478bd9Sstevel@tonic-gate 51367c478bd9Sstevel@tonic-gate /* 51377c478bd9Sstevel@tonic-gate * If it is a dir, recurse down until we find a link and 51387c478bd9Sstevel@tonic-gate * then use the link. 51397c478bd9Sstevel@tonic-gate */ 51407c478bd9Sstevel@tonic-gate if ((sb->st_mode & S_IFMT) == S_IFDIR) { 51417c478bd9Sstevel@tonic-gate 514273de625bSjg if (finddev_readdir(namebuf, &fhandle) != 0) { 51437c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 51447c478bd9Sstevel@tonic-gate } 51457c478bd9Sstevel@tonic-gate 51467c478bd9Sstevel@tonic-gate /* 51477c478bd9Sstevel@tonic-gate * Search each dir entry looking for a symlink. Return 51487c478bd9Sstevel@tonic-gate * the first symlink found in namebuf. Recurse dirs. 51497c478bd9Sstevel@tonic-gate */ 515073de625bSjg while ((fp = finddev_next(fhandle)) != NULL) { 51517c478bd9Sstevel@tonic-gate cp = namebuf + strlen(namebuf); 5152facf4a8dSllai1 if ((strlcat(namebuf, "/", PATH_MAX) >= PATH_MAX) || 515373de625bSjg (strlcat(namebuf, fp, PATH_MAX) >= PATH_MAX)) { 5154facf4a8dSllai1 *cp = '\0'; 515573de625bSjg finddev_close(fhandle); 5156facf4a8dSllai1 return (DEVFSADM_FAILURE); 5157facf4a8dSllai1 } 51587c478bd9Sstevel@tonic-gate if (get_stat_info(namebuf, sb) == DEVFSADM_SUCCESS) { 515973de625bSjg finddev_close(fhandle); 51607c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 51617c478bd9Sstevel@tonic-gate } 51627c478bd9Sstevel@tonic-gate *cp = '\0'; 51637c478bd9Sstevel@tonic-gate } 516473de625bSjg finddev_close(fhandle); 51657c478bd9Sstevel@tonic-gate } 51667c478bd9Sstevel@tonic-gate 51677c478bd9Sstevel@tonic-gate /* no symlink found, so return error */ 51687c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 51697c478bd9Sstevel@tonic-gate } 51707c478bd9Sstevel@tonic-gate 51717c478bd9Sstevel@tonic-gate /* 51727c478bd9Sstevel@tonic-gate * An existing matching ID was not found, so this function is called to 51737c478bd9Sstevel@tonic-gate * create the next lowest ID. In the INTEGER case, return the next 51747c478bd9Sstevel@tonic-gate * lowest unused integer. In the case of LETTER, return the next lowest 51757c478bd9Sstevel@tonic-gate * unused letter. Return empty string if all 26 are used. 51767c478bd9Sstevel@tonic-gate * Only IDs >= min will be returned. 51777c478bd9Sstevel@tonic-gate */ 51787c478bd9Sstevel@tonic-gate char * 51797c478bd9Sstevel@tonic-gate new_id(numeral_t *numeral, int type, char *min) 51807c478bd9Sstevel@tonic-gate { 51817c478bd9Sstevel@tonic-gate int imin; 51827c478bd9Sstevel@tonic-gate temp_t *temp; 51837c478bd9Sstevel@tonic-gate temp_t *ptr; 51847c478bd9Sstevel@tonic-gate temp_t **previous; 51857c478bd9Sstevel@tonic-gate temp_t *head = NULL; 51867c478bd9Sstevel@tonic-gate char *retval; 51877c478bd9Sstevel@tonic-gate static char tempbuff[8]; 51887c478bd9Sstevel@tonic-gate numeral_t *np; 51897c478bd9Sstevel@tonic-gate 51907c478bd9Sstevel@tonic-gate if (type == LETTER) { 51917c478bd9Sstevel@tonic-gate 51927c478bd9Sstevel@tonic-gate char letter[26], i; 51937c478bd9Sstevel@tonic-gate 51947c478bd9Sstevel@tonic-gate if (numeral == NULL) { 51957c478bd9Sstevel@tonic-gate return (s_strdup(min)); 51967c478bd9Sstevel@tonic-gate } 51977c478bd9Sstevel@tonic-gate 51987c478bd9Sstevel@tonic-gate for (i = 0; i < 26; i++) { 51997c478bd9Sstevel@tonic-gate letter[i] = 0; 52007c478bd9Sstevel@tonic-gate } 52017c478bd9Sstevel@tonic-gate 52027c478bd9Sstevel@tonic-gate for (np = numeral; np != NULL; np = np->next) { 52038d483882Smlf assert(np->flags == 0 || 52048d483882Smlf np->flags == NUMERAL_RESERVED); 52057c478bd9Sstevel@tonic-gate letter[*np->id - 'a']++; 52067c478bd9Sstevel@tonic-gate } 52077c478bd9Sstevel@tonic-gate 52087c478bd9Sstevel@tonic-gate imin = *min - 'a'; 52097c478bd9Sstevel@tonic-gate 52107c478bd9Sstevel@tonic-gate for (i = imin; i < 26; i++) { 52117c478bd9Sstevel@tonic-gate if (letter[i] == 0) { 52127c478bd9Sstevel@tonic-gate retval = s_malloc(2); 52137c478bd9Sstevel@tonic-gate retval[0] = 'a' + i; 52147c478bd9Sstevel@tonic-gate retval[1] = '\0'; 52157c478bd9Sstevel@tonic-gate return (retval); 52167c478bd9Sstevel@tonic-gate } 52177c478bd9Sstevel@tonic-gate } 52187c478bd9Sstevel@tonic-gate 52197c478bd9Sstevel@tonic-gate return (s_strdup("")); 52207c478bd9Sstevel@tonic-gate } 52217c478bd9Sstevel@tonic-gate 52227c478bd9Sstevel@tonic-gate if (type == INTEGER) { 52237c478bd9Sstevel@tonic-gate 52247c478bd9Sstevel@tonic-gate if (numeral == NULL) { 52257c478bd9Sstevel@tonic-gate return (s_strdup(min)); 52267c478bd9Sstevel@tonic-gate } 52277c478bd9Sstevel@tonic-gate 52287c478bd9Sstevel@tonic-gate imin = atoi(min); 52297c478bd9Sstevel@tonic-gate 52307c478bd9Sstevel@tonic-gate /* sort list */ 52317c478bd9Sstevel@tonic-gate for (np = numeral; np != NULL; np = np->next) { 52328d483882Smlf assert(np->flags == 0 || 52338d483882Smlf np->flags == NUMERAL_RESERVED); 52347c478bd9Sstevel@tonic-gate temp = s_malloc(sizeof (temp_t)); 52357c478bd9Sstevel@tonic-gate temp->integer = atoi(np->id); 52367c478bd9Sstevel@tonic-gate temp->next = NULL; 52377c478bd9Sstevel@tonic-gate 52387c478bd9Sstevel@tonic-gate previous = &head; 52397c478bd9Sstevel@tonic-gate for (ptr = head; ptr != NULL; ptr = ptr->next) { 52407c478bd9Sstevel@tonic-gate if (temp->integer < ptr->integer) { 52417c478bd9Sstevel@tonic-gate temp->next = ptr; 52427c478bd9Sstevel@tonic-gate *previous = temp; 52437c478bd9Sstevel@tonic-gate break; 52447c478bd9Sstevel@tonic-gate } 52457c478bd9Sstevel@tonic-gate previous = &(ptr->next); 52467c478bd9Sstevel@tonic-gate } 52477c478bd9Sstevel@tonic-gate if (ptr == NULL) { 52487c478bd9Sstevel@tonic-gate *previous = temp; 52497c478bd9Sstevel@tonic-gate } 52507c478bd9Sstevel@tonic-gate } 52517c478bd9Sstevel@tonic-gate 52527c478bd9Sstevel@tonic-gate /* now search sorted list for first hole >= imin */ 52537c478bd9Sstevel@tonic-gate for (ptr = head; ptr != NULL; ptr = ptr->next) { 52547c478bd9Sstevel@tonic-gate if (imin == ptr->integer) { 52557c478bd9Sstevel@tonic-gate imin++; 52567c478bd9Sstevel@tonic-gate } else { 52577c478bd9Sstevel@tonic-gate if (imin < ptr->integer) { 52587c478bd9Sstevel@tonic-gate break; 52597c478bd9Sstevel@tonic-gate } 52607c478bd9Sstevel@tonic-gate } 52617c478bd9Sstevel@tonic-gate 52627c478bd9Sstevel@tonic-gate } 52637c478bd9Sstevel@tonic-gate 52647c478bd9Sstevel@tonic-gate /* free temp list */ 52657c478bd9Sstevel@tonic-gate for (ptr = head; ptr != NULL; ) { 52667c478bd9Sstevel@tonic-gate temp = ptr; 52677c478bd9Sstevel@tonic-gate ptr = ptr->next; 52687c478bd9Sstevel@tonic-gate free(temp); 52697c478bd9Sstevel@tonic-gate } 52707c478bd9Sstevel@tonic-gate 52717c478bd9Sstevel@tonic-gate (void) sprintf(tempbuff, "%d", imin); 52727c478bd9Sstevel@tonic-gate return (s_strdup(tempbuff)); 52737c478bd9Sstevel@tonic-gate } 52747c478bd9Sstevel@tonic-gate 52757c478bd9Sstevel@tonic-gate return (s_strdup("")); 52767c478bd9Sstevel@tonic-gate } 52777c478bd9Sstevel@tonic-gate 52788d483882Smlf static int 52798d483882Smlf enumerate_parse(char *rsvstr, char *path_left, numeral_set_t *setp, 52808d483882Smlf devfsadm_enumerate_t rules[], int index) 52818d483882Smlf { 52828d483882Smlf char *slash1 = NULL; 52838d483882Smlf char *slash2 = NULL; 52848d483882Smlf char *numeral_id; 52858d483882Smlf char *path_left_save; 52868d483882Smlf char *rsvstr_save; 52878d483882Smlf int ret = 0; 52888d483882Smlf static int warned = 0; 52898d483882Smlf 52908d483882Smlf rsvstr_save = rsvstr; 52918d483882Smlf path_left_save = path_left; 52928d483882Smlf 52938d483882Smlf if (rsvstr == NULL || rsvstr[0] == '\0' || rsvstr[0] == '/') { 52948d483882Smlf if (!warned) { 52958d483882Smlf err_print("invalid reserved filepath: %s\n", 52968d483882Smlf rsvstr ? rsvstr : "<NULL>"); 52978d483882Smlf warned = 1; 52988d483882Smlf } 52998d483882Smlf return (0); 53008d483882Smlf } 53018d483882Smlf 53028d483882Smlf vprint(RSRV_MID, "processing rule: %s, rstring: %s\n", 53038d483882Smlf path_left, rsvstr); 53048d483882Smlf 53058d483882Smlf 53068d483882Smlf for (;;) { 53078d483882Smlf /* get rid of any extra '/' in the reserve string */ 53088d483882Smlf while (*rsvstr == '/') { 53098d483882Smlf rsvstr++; 53108d483882Smlf } 53118d483882Smlf 53128d483882Smlf /* get rid of any extra '/' in the RE */ 53138d483882Smlf while (*path_left == '/') { 53148d483882Smlf path_left++; 53158d483882Smlf } 53168d483882Smlf 53178d483882Smlf if (slash1 = strchr(path_left, '/')) { 53188d483882Smlf *slash1 = '\0'; 53198d483882Smlf } 53208d483882Smlf if (slash2 = strchr(rsvstr, '/')) { 53218d483882Smlf *slash2 = '\0'; 53228d483882Smlf } 53238d483882Smlf 53248d483882Smlf if ((slash1 != NULL) ^ (slash2 != NULL)) { 53258d483882Smlf ret = 0; 53268d483882Smlf vprint(RSRV_MID, "mismatch in # of path components\n"); 53278d483882Smlf goto out; 53288d483882Smlf } 53298d483882Smlf 53308d483882Smlf /* 53318d483882Smlf * Returns true if path_left matches the list entry. 53328d483882Smlf * If it is the last path component, pass subexp 53338d483882Smlf * so that it will return the corresponding ID in 53348d483882Smlf * numeral_id. 53358d483882Smlf */ 53368d483882Smlf numeral_id = NULL; 53378d483882Smlf if (match_path_component(path_left, rsvstr, &numeral_id, 53388d483882Smlf slash1 ? 0 : rules[index].subexp)) { 53398d483882Smlf 53408d483882Smlf /* We have a match. */ 53418d483882Smlf if (slash1 == NULL) { 53428d483882Smlf /* Is last path component */ 53438d483882Smlf vprint(RSRV_MID, "match and last component\n"); 53448d483882Smlf create_reserved_numeral(setp, numeral_id); 53458d483882Smlf if (numeral_id != NULL) { 53468d483882Smlf free(numeral_id); 53478d483882Smlf } 53488d483882Smlf ret = 1; 53498d483882Smlf goto out; 53508d483882Smlf } else { 53518d483882Smlf /* Not last path component. Continue parsing */ 53528d483882Smlf *slash1 = '/'; 53538d483882Smlf *slash2 = '/'; 53548d483882Smlf path_left = slash1 + 1; 53558d483882Smlf rsvstr = slash2 + 1; 53568d483882Smlf vprint(RSRV_MID, 53578d483882Smlf "match and NOT last component\n"); 53588d483882Smlf continue; 53598d483882Smlf } 53608d483882Smlf } else { 53618d483882Smlf /* No match */ 53628d483882Smlf ret = 0; 53638d483882Smlf vprint(RSRV_MID, "No match: rule RE = %s, " 53648d483882Smlf "rstring = %s\n", path_left, rsvstr); 53658d483882Smlf goto out; 53668d483882Smlf } 53678d483882Smlf } 53688d483882Smlf 53698d483882Smlf out: 53708d483882Smlf if (slash1) 53718d483882Smlf *slash1 = '/'; 53728d483882Smlf if (slash2) 53738d483882Smlf *slash2 = '/'; 53748d483882Smlf 53758d483882Smlf if (ret == 1) { 53768d483882Smlf vprint(RSRV_MID, "match: rule RE: %s, rstring: %s\n", 53778d483882Smlf path_left_save, rsvstr_save); 53788d483882Smlf } else { 53798d483882Smlf vprint(RSRV_MID, "NO match: rule RE: %s, rstring: %s\n", 53808d483882Smlf path_left_save, rsvstr_save); 53818d483882Smlf } 53828d483882Smlf 53838d483882Smlf return (ret); 53848d483882Smlf } 53858d483882Smlf 53867c478bd9Sstevel@tonic-gate /* 53877c478bd9Sstevel@tonic-gate * Search current_dir for all files which match the first path component 53887c478bd9Sstevel@tonic-gate * of path_left, which is an RE. If a match is found, but there are more 53897c478bd9Sstevel@tonic-gate * components of path_left, then recurse, otherwise, if we have reached 53907c478bd9Sstevel@tonic-gate * the last component of path_left, call create_cached_numerals for each 53917c478bd9Sstevel@tonic-gate * file. At some point, recurse_dev_re() should be rewritten so that this 53927c478bd9Sstevel@tonic-gate * function can be eliminated. 53937c478bd9Sstevel@tonic-gate */ 53947c478bd9Sstevel@tonic-gate static void 53957c478bd9Sstevel@tonic-gate enumerate_recurse(char *current_dir, char *path_left, numeral_set_t *setp, 53967c478bd9Sstevel@tonic-gate devfsadm_enumerate_t rules[], int index) 53977c478bd9Sstevel@tonic-gate { 53987c478bd9Sstevel@tonic-gate char *slash; 53997c478bd9Sstevel@tonic-gate char *new_path; 54007c478bd9Sstevel@tonic-gate char *numeral_id; 540173de625bSjg finddevhdl_t fhandle; 540273de625bSjg const char *fp; 54037c478bd9Sstevel@tonic-gate 540473de625bSjg if (finddev_readdir(current_dir, &fhandle) != 0) { 54057c478bd9Sstevel@tonic-gate return; 54067c478bd9Sstevel@tonic-gate } 54077c478bd9Sstevel@tonic-gate 54087c478bd9Sstevel@tonic-gate /* get rid of any extra '/' */ 54097c478bd9Sstevel@tonic-gate while (*path_left == '/') { 54107c478bd9Sstevel@tonic-gate path_left++; 54117c478bd9Sstevel@tonic-gate } 54127c478bd9Sstevel@tonic-gate 54137c478bd9Sstevel@tonic-gate if (slash = strchr(path_left, '/')) { 54147c478bd9Sstevel@tonic-gate *slash = '\0'; 54157c478bd9Sstevel@tonic-gate } 54167c478bd9Sstevel@tonic-gate 541773de625bSjg while ((fp = finddev_next(fhandle)) != NULL) { 54187c478bd9Sstevel@tonic-gate 54197c478bd9Sstevel@tonic-gate /* 5420facf4a8dSllai1 * Returns true if path_left matches the list entry. 54217c478bd9Sstevel@tonic-gate * If it is the last path component, pass subexp 54227c478bd9Sstevel@tonic-gate * so that it will return the corresponding ID in 54237c478bd9Sstevel@tonic-gate * numeral_id. 54247c478bd9Sstevel@tonic-gate */ 54257c478bd9Sstevel@tonic-gate numeral_id = NULL; 542673de625bSjg if (match_path_component(path_left, (char *)fp, &numeral_id, 54277c478bd9Sstevel@tonic-gate slash ? 0 : rules[index].subexp)) { 54287c478bd9Sstevel@tonic-gate 54297c478bd9Sstevel@tonic-gate new_path = s_malloc(strlen(current_dir) + 543073de625bSjg strlen(fp) + 2); 54317c478bd9Sstevel@tonic-gate 54327c478bd9Sstevel@tonic-gate (void) strcpy(new_path, current_dir); 54337c478bd9Sstevel@tonic-gate (void) strcat(new_path, "/"); 543473de625bSjg (void) strcat(new_path, fp); 54357c478bd9Sstevel@tonic-gate 54367c478bd9Sstevel@tonic-gate if (slash != NULL) { 54377c478bd9Sstevel@tonic-gate enumerate_recurse(new_path, slash + 1, 54387c478bd9Sstevel@tonic-gate setp, rules, index); 54397c478bd9Sstevel@tonic-gate } else { 54407c478bd9Sstevel@tonic-gate create_cached_numeral(new_path, setp, 54417c478bd9Sstevel@tonic-gate numeral_id, rules, index); 54427c478bd9Sstevel@tonic-gate if (numeral_id != NULL) { 54437c478bd9Sstevel@tonic-gate free(numeral_id); 54447c478bd9Sstevel@tonic-gate } 54457c478bd9Sstevel@tonic-gate } 54467c478bd9Sstevel@tonic-gate free(new_path); 54477c478bd9Sstevel@tonic-gate } 54487c478bd9Sstevel@tonic-gate } 54497c478bd9Sstevel@tonic-gate 54507c478bd9Sstevel@tonic-gate if (slash != NULL) { 54517c478bd9Sstevel@tonic-gate *slash = '/'; 54527c478bd9Sstevel@tonic-gate } 545373de625bSjg finddev_close(fhandle); 54547c478bd9Sstevel@tonic-gate } 54557c478bd9Sstevel@tonic-gate 54567c478bd9Sstevel@tonic-gate 54577c478bd9Sstevel@tonic-gate /* 54587c478bd9Sstevel@tonic-gate * Returns true if file matches file_re. If subexp is non-zero, it means 54597c478bd9Sstevel@tonic-gate * we are searching the last path component and need to return the 54607c478bd9Sstevel@tonic-gate * parenthesized subexpression subexp in id. 54617c478bd9Sstevel@tonic-gate * 54627c478bd9Sstevel@tonic-gate */ 54637c478bd9Sstevel@tonic-gate static int 54647c478bd9Sstevel@tonic-gate match_path_component(char *file_re, char *file, char **id, int subexp) 54657c478bd9Sstevel@tonic-gate { 54667c478bd9Sstevel@tonic-gate regex_t re1; 54677c478bd9Sstevel@tonic-gate int match = 0; 54687c478bd9Sstevel@tonic-gate int nelements; 54697c478bd9Sstevel@tonic-gate regmatch_t *pmatch; 54707c478bd9Sstevel@tonic-gate 54717c478bd9Sstevel@tonic-gate if (subexp != 0) { 54727c478bd9Sstevel@tonic-gate nelements = subexp + 1; 54737c478bd9Sstevel@tonic-gate pmatch = (regmatch_t *) 54747c478bd9Sstevel@tonic-gate s_malloc(sizeof (regmatch_t) * nelements); 54757c478bd9Sstevel@tonic-gate } else { 54767c478bd9Sstevel@tonic-gate pmatch = NULL; 54777c478bd9Sstevel@tonic-gate nelements = 0; 54787c478bd9Sstevel@tonic-gate } 54797c478bd9Sstevel@tonic-gate 54807c478bd9Sstevel@tonic-gate if (regcomp(&re1, file_re, REG_EXTENDED) != 0) { 54817c478bd9Sstevel@tonic-gate if (pmatch != NULL) { 54827c478bd9Sstevel@tonic-gate free(pmatch); 54837c478bd9Sstevel@tonic-gate } 54847c478bd9Sstevel@tonic-gate return (0); 54857c478bd9Sstevel@tonic-gate } 54867c478bd9Sstevel@tonic-gate 54877c478bd9Sstevel@tonic-gate if (regexec(&re1, file, nelements, pmatch, 0) == 0) { 54887c478bd9Sstevel@tonic-gate match = 1; 54897c478bd9Sstevel@tonic-gate } 54907c478bd9Sstevel@tonic-gate 54917c478bd9Sstevel@tonic-gate if ((match != 0) && (subexp != 0)) { 54927c478bd9Sstevel@tonic-gate int size = pmatch[subexp].rm_eo - pmatch[subexp].rm_so; 54937c478bd9Sstevel@tonic-gate *id = s_malloc(size + 1); 54947c478bd9Sstevel@tonic-gate (void) strncpy(*id, &file[pmatch[subexp].rm_so], size); 54957c478bd9Sstevel@tonic-gate (*id)[size] = '\0'; 54967c478bd9Sstevel@tonic-gate } 54977c478bd9Sstevel@tonic-gate 54987c478bd9Sstevel@tonic-gate if (pmatch != NULL) { 54997c478bd9Sstevel@tonic-gate free(pmatch); 55007c478bd9Sstevel@tonic-gate } 55017c478bd9Sstevel@tonic-gate regfree(&re1); 55027c478bd9Sstevel@tonic-gate return (match); 55037c478bd9Sstevel@tonic-gate } 55047c478bd9Sstevel@tonic-gate 55058d483882Smlf static void 55068d483882Smlf create_reserved_numeral(numeral_set_t *setp, char *numeral_id) 55078d483882Smlf { 55088d483882Smlf numeral_t *np; 55098d483882Smlf 55108d483882Smlf vprint(RSRV_MID, "Attempting to create reserved numeral: %s\n", 55118d483882Smlf numeral_id); 55128d483882Smlf 55138d483882Smlf /* 55148d483882Smlf * We found a numeral_id from an entry in the enumerate_reserved file 55158d483882Smlf * which matched the re passed in from devfsadm_enumerate. We only 55168d483882Smlf * need to make sure ONE copy of numeral_id exists on the numeral list. 55178d483882Smlf * We only need to store /dev/dsk/cNtod0s0 and no other entries 55188d483882Smlf * hanging off of controller N. 55198d483882Smlf */ 55208d483882Smlf for (np = setp->headnumeral; np != NULL; np = np->next) { 55218d483882Smlf if (strcmp(numeral_id, np->id) == 0) { 55228d483882Smlf vprint(RSRV_MID, "ID: %s, already reserved\n", np->id); 55238d483882Smlf assert(np->flags == NUMERAL_RESERVED); 55248d483882Smlf return; 55258d483882Smlf } else { 55268d483882Smlf assert(np->flags == 0 || 55278d483882Smlf np->flags == NUMERAL_RESERVED); 55288d483882Smlf } 55298d483882Smlf } 55308d483882Smlf 55318d483882Smlf /* NOT on list, so add it */ 55328d483882Smlf np = s_malloc(sizeof (numeral_t)); 55338d483882Smlf np->id = s_strdup(numeral_id); 55348d483882Smlf np->full_path = NULL; 55358d483882Smlf np->rule_index = 0; 55368d483882Smlf np->cmp_str = NULL; 55378d483882Smlf np->flags = NUMERAL_RESERVED; 55388d483882Smlf np->next = setp->headnumeral; 55398d483882Smlf setp->headnumeral = np; 55408d483882Smlf 55418d483882Smlf vprint(RSRV_MID, "Reserved numeral ID: %s\n", np->id); 55428d483882Smlf } 55438d483882Smlf 55447c478bd9Sstevel@tonic-gate /* 55457c478bd9Sstevel@tonic-gate * This function is called for every file which matched the leaf 55467c478bd9Sstevel@tonic-gate * component of the RE. If the "numeral_id" is not already on the 55477c478bd9Sstevel@tonic-gate * numeral set's numeral list, add it and its physical path. 55487c478bd9Sstevel@tonic-gate */ 55497c478bd9Sstevel@tonic-gate static void 55507c478bd9Sstevel@tonic-gate create_cached_numeral(char *path, numeral_set_t *setp, char *numeral_id, 55517c478bd9Sstevel@tonic-gate devfsadm_enumerate_t rules[], int index) 55527c478bd9Sstevel@tonic-gate { 55537c478bd9Sstevel@tonic-gate char linkbuf[PATH_MAX + 1]; 55547c478bd9Sstevel@tonic-gate char lpath[PATH_MAX + 1]; 55557c478bd9Sstevel@tonic-gate char *linkptr, *cmp_str; 55567c478bd9Sstevel@tonic-gate numeral_t *np; 55577c478bd9Sstevel@tonic-gate int linksize; 55587c478bd9Sstevel@tonic-gate struct stat sb; 55597c478bd9Sstevel@tonic-gate const char *fcn = "create_cached_numeral"; 55607c478bd9Sstevel@tonic-gate 55617c478bd9Sstevel@tonic-gate assert(index >= 0 && index < setp->re_count); 55627c478bd9Sstevel@tonic-gate assert(strcmp(rules[index].re, setp->re[index]) == 0); 55637c478bd9Sstevel@tonic-gate 55647c478bd9Sstevel@tonic-gate /* 55657c478bd9Sstevel@tonic-gate * We found a numeral_id from an entry in /dev which matched 55667c478bd9Sstevel@tonic-gate * the re passed in from devfsadm_enumerate. We only need to make sure 55677c478bd9Sstevel@tonic-gate * ONE copy of numeral_id exists on the numeral list. We only need 55687c478bd9Sstevel@tonic-gate * to store /dev/dsk/cNtod0s0 and no other entries hanging off 55697c478bd9Sstevel@tonic-gate * of controller N. 55707c478bd9Sstevel@tonic-gate */ 55717c478bd9Sstevel@tonic-gate for (np = setp->headnumeral; np != NULL; np = np->next) { 55728d483882Smlf assert(np->flags == 0 || np->flags == NUMERAL_RESERVED); 55737c478bd9Sstevel@tonic-gate if (strcmp(numeral_id, np->id) == 0) { 55748d483882Smlf /* 55758d483882Smlf * Note that we can't assert that the flags field 55768d483882Smlf * of the numeral is 0, since both reserved and 55778d483882Smlf * unreserved links in /dev come here 55788d483882Smlf */ 55798d483882Smlf if (np->flags == NUMERAL_RESERVED) { 55808d483882Smlf vprint(RSRV_MID, "ID derived from /dev link is" 55818d483882Smlf " reserved: %s\n", np->id); 55828d483882Smlf } else { 55838d483882Smlf vprint(RSRV_MID, "ID derived from /dev link is" 55848d483882Smlf " NOT reserved: %s\n", np->id); 55858d483882Smlf } 55867c478bd9Sstevel@tonic-gate return; 55877c478bd9Sstevel@tonic-gate } 55887c478bd9Sstevel@tonic-gate } 55897c478bd9Sstevel@tonic-gate 55907c478bd9Sstevel@tonic-gate /* NOT on list, so add it */ 55917c478bd9Sstevel@tonic-gate 55927c478bd9Sstevel@tonic-gate (void) strcpy(lpath, path); 55937c478bd9Sstevel@tonic-gate /* 55947c478bd9Sstevel@tonic-gate * If path is a dir, it is changed to the first symbolic link it find 55957c478bd9Sstevel@tonic-gate * if it finds one. 55967c478bd9Sstevel@tonic-gate */ 55977c478bd9Sstevel@tonic-gate if (get_stat_info(lpath, &sb) == DEVFSADM_FAILURE) { 55987c478bd9Sstevel@tonic-gate return; 55997c478bd9Sstevel@tonic-gate } 56007c478bd9Sstevel@tonic-gate 56017c478bd9Sstevel@tonic-gate /* If we get here, we found a symlink */ 56027c478bd9Sstevel@tonic-gate linksize = readlink(lpath, linkbuf, PATH_MAX); 56037c478bd9Sstevel@tonic-gate 56047c478bd9Sstevel@tonic-gate if (linksize <= 0) { 56057c478bd9Sstevel@tonic-gate err_print(READLINK_FAILED, fcn, lpath, strerror(errno)); 56067c478bd9Sstevel@tonic-gate return; 56077c478bd9Sstevel@tonic-gate } 56087c478bd9Sstevel@tonic-gate 56097c478bd9Sstevel@tonic-gate linkbuf[linksize] = '\0'; 56107c478bd9Sstevel@tonic-gate 56117c478bd9Sstevel@tonic-gate /* 56127c478bd9Sstevel@tonic-gate * the following just points linkptr to the root of the /devices 56137c478bd9Sstevel@tonic-gate * node if it is a minor node, otherwise, to the first char of 56147c478bd9Sstevel@tonic-gate * linkbuf if it is a link. 56157c478bd9Sstevel@tonic-gate */ 56167c478bd9Sstevel@tonic-gate (void) is_minor_node(linkbuf, &linkptr); 56177c478bd9Sstevel@tonic-gate 56187c478bd9Sstevel@tonic-gate cmp_str = alloc_cmp_str(linkptr, &rules[index]); 56197c478bd9Sstevel@tonic-gate if (cmp_str == NULL) { 56207c478bd9Sstevel@tonic-gate return; 56217c478bd9Sstevel@tonic-gate } 56227c478bd9Sstevel@tonic-gate 56237c478bd9Sstevel@tonic-gate np = s_malloc(sizeof (numeral_t)); 56247c478bd9Sstevel@tonic-gate 56257c478bd9Sstevel@tonic-gate np->id = s_strdup(numeral_id); 56267c478bd9Sstevel@tonic-gate np->full_path = s_strdup(linkptr); 56277c478bd9Sstevel@tonic-gate np->rule_index = index; 56287c478bd9Sstevel@tonic-gate np->cmp_str = cmp_str; 56298d483882Smlf np->flags = 0; 56307c478bd9Sstevel@tonic-gate 56317c478bd9Sstevel@tonic-gate np->next = setp->headnumeral; 56327c478bd9Sstevel@tonic-gate setp->headnumeral = np; 56337c478bd9Sstevel@tonic-gate } 56347c478bd9Sstevel@tonic-gate 56357c478bd9Sstevel@tonic-gate 56367c478bd9Sstevel@tonic-gate /* 56377c478bd9Sstevel@tonic-gate * This should be called either before or after granting access to a 56387c478bd9Sstevel@tonic-gate * command line version of devfsadm running, since it may have changed 56397c478bd9Sstevel@tonic-gate * the state of /dev. It forces future enumerate calls to re-build 56407c478bd9Sstevel@tonic-gate * cached information from /dev. 56417c478bd9Sstevel@tonic-gate */ 56427c478bd9Sstevel@tonic-gate void 56437c478bd9Sstevel@tonic-gate invalidate_enumerate_cache(void) 56447c478bd9Sstevel@tonic-gate { 56457c478bd9Sstevel@tonic-gate numeral_set_t *setp; 56467c478bd9Sstevel@tonic-gate numeral_set_t *savedsetp; 56477c478bd9Sstevel@tonic-gate numeral_t *savednumset; 56487c478bd9Sstevel@tonic-gate numeral_t *numset; 56497c478bd9Sstevel@tonic-gate int i; 56507c478bd9Sstevel@tonic-gate 56517c478bd9Sstevel@tonic-gate for (setp = head_numeral_set; setp != NULL; ) { 56527c478bd9Sstevel@tonic-gate /* 56537c478bd9Sstevel@tonic-gate * check all regexp's passed in function against 56547c478bd9Sstevel@tonic-gate * those in cached set. 56557c478bd9Sstevel@tonic-gate */ 56567c478bd9Sstevel@tonic-gate 56577c478bd9Sstevel@tonic-gate savedsetp = setp; 56587c478bd9Sstevel@tonic-gate setp = setp->next; 56597c478bd9Sstevel@tonic-gate 56607c478bd9Sstevel@tonic-gate for (i = 0; i < savedsetp->re_count; i++) { 56617c478bd9Sstevel@tonic-gate free(savedsetp->re[i]); 56627c478bd9Sstevel@tonic-gate } 56637c478bd9Sstevel@tonic-gate free(savedsetp->re); 56647c478bd9Sstevel@tonic-gate 56657c478bd9Sstevel@tonic-gate for (numset = savedsetp->headnumeral; numset != NULL; ) { 56667c478bd9Sstevel@tonic-gate savednumset = numset; 56677c478bd9Sstevel@tonic-gate numset = numset->next; 56687c478bd9Sstevel@tonic-gate assert(savednumset->rule_index < savedsetp->re_count); 56697c478bd9Sstevel@tonic-gate free(savednumset->id); 56707c478bd9Sstevel@tonic-gate free(savednumset->full_path); 56717c478bd9Sstevel@tonic-gate free(savednumset->cmp_str); 56727c478bd9Sstevel@tonic-gate free(savednumset); 56737c478bd9Sstevel@tonic-gate } 56747c478bd9Sstevel@tonic-gate free(savedsetp); 56757c478bd9Sstevel@tonic-gate } 56767c478bd9Sstevel@tonic-gate head_numeral_set = NULL; 56777c478bd9Sstevel@tonic-gate } 56787c478bd9Sstevel@tonic-gate 56797c478bd9Sstevel@tonic-gate /* 56807c478bd9Sstevel@tonic-gate * Copies over links from /dev to <root>/dev and device special files in 56817c478bd9Sstevel@tonic-gate * /devices to <root>/devices, preserving the existing file modes. If 56827c478bd9Sstevel@tonic-gate * the link or special file already exists on <root>, skip the copy. (it 56837c478bd9Sstevel@tonic-gate * would exist only if a package hard coded it there, so assume package 56847c478bd9Sstevel@tonic-gate * knows best?). Use /etc/name_to_major and <root>/etc/name_to_major to 56857c478bd9Sstevel@tonic-gate * make translations for major numbers on device special files. No need to 56867c478bd9Sstevel@tonic-gate * make a translation on minor_perm since if the file was created in the 56877c478bd9Sstevel@tonic-gate * miniroot then it would presumably have the same minor_perm entry in 56887c478bd9Sstevel@tonic-gate * <root>/etc/minor_perm. To be used only by install. 56897c478bd9Sstevel@tonic-gate */ 56907c478bd9Sstevel@tonic-gate int 56917c478bd9Sstevel@tonic-gate devfsadm_copy(void) 56927c478bd9Sstevel@tonic-gate { 56937c478bd9Sstevel@tonic-gate char filename[PATH_MAX + 1]; 56947c478bd9Sstevel@tonic-gate 56957c478bd9Sstevel@tonic-gate /* load the installed root's name_to_major for translations */ 56967c478bd9Sstevel@tonic-gate (void) snprintf(filename, sizeof (filename), "%s%s", root_dir, 56977c478bd9Sstevel@tonic-gate NAME_TO_MAJOR); 56987c478bd9Sstevel@tonic-gate if (load_n2m_table(filename) == DEVFSADM_FAILURE) { 56997c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 57007c478bd9Sstevel@tonic-gate } 57017c478bd9Sstevel@tonic-gate 57027c478bd9Sstevel@tonic-gate /* Copy /dev to target disk. No need to copy /devices with devfs */ 57037c478bd9Sstevel@tonic-gate (void) nftw(DEV, devfsadm_copy_file, 20, FTW_PHYS); 57047c478bd9Sstevel@tonic-gate 57057c478bd9Sstevel@tonic-gate /* Let install handle copying over path_to_inst */ 57067c478bd9Sstevel@tonic-gate 57077c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 57087c478bd9Sstevel@tonic-gate } 57097c478bd9Sstevel@tonic-gate 57107c478bd9Sstevel@tonic-gate /* 57117c478bd9Sstevel@tonic-gate * This function copies links, dirs, and device special files. 57127c478bd9Sstevel@tonic-gate * Note that it always returns DEVFSADM_SUCCESS, so that nftw doesn't 57137c478bd9Sstevel@tonic-gate * abort. 57147c478bd9Sstevel@tonic-gate */ 57157c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 57167c478bd9Sstevel@tonic-gate static int 57177c478bd9Sstevel@tonic-gate devfsadm_copy_file(const char *file, const struct stat *stat, 57187c478bd9Sstevel@tonic-gate int flags, struct FTW *ftw) 57197c478bd9Sstevel@tonic-gate { 57207c478bd9Sstevel@tonic-gate struct stat sp; 57217c478bd9Sstevel@tonic-gate dev_t newdev; 57227c478bd9Sstevel@tonic-gate char newfile[PATH_MAX + 1]; 57237c478bd9Sstevel@tonic-gate char linkcontents[PATH_MAX + 1]; 57247c478bd9Sstevel@tonic-gate int bytes; 57257c478bd9Sstevel@tonic-gate const char *fcn = "devfsadm_copy_file"; 57267c478bd9Sstevel@tonic-gate 57277c478bd9Sstevel@tonic-gate (void) strcpy(newfile, root_dir); 57287c478bd9Sstevel@tonic-gate (void) strcat(newfile, "/"); 57297c478bd9Sstevel@tonic-gate (void) strcat(newfile, file); 57307c478bd9Sstevel@tonic-gate 57317c478bd9Sstevel@tonic-gate if (lstat(newfile, &sp) == 0) { 57327c478bd9Sstevel@tonic-gate /* newfile already exists, so no need to continue */ 57337c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 57347c478bd9Sstevel@tonic-gate } 57357c478bd9Sstevel@tonic-gate 57367c478bd9Sstevel@tonic-gate if (((stat->st_mode & S_IFMT) == S_IFBLK) || 57377c478bd9Sstevel@tonic-gate ((stat->st_mode & S_IFMT) == S_IFCHR)) { 57387c478bd9Sstevel@tonic-gate if (translate_major(stat->st_rdev, &newdev) == 57397c478bd9Sstevel@tonic-gate DEVFSADM_FAILURE) { 57407c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 57417c478bd9Sstevel@tonic-gate } 57427c478bd9Sstevel@tonic-gate if (mknod(newfile, stat->st_mode, newdev) == -1) { 57437c478bd9Sstevel@tonic-gate err_print(MKNOD_FAILED, newfile, strerror(errno)); 57447c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 57457c478bd9Sstevel@tonic-gate } 57467c478bd9Sstevel@tonic-gate } else if ((stat->st_mode & S_IFMT) == S_IFDIR) { 57477c478bd9Sstevel@tonic-gate if (mknod(newfile, stat->st_mode, 0) == -1) { 57487c478bd9Sstevel@tonic-gate err_print(MKNOD_FAILED, newfile, strerror(errno)); 57497c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 57507c478bd9Sstevel@tonic-gate } 57517c478bd9Sstevel@tonic-gate } else if ((stat->st_mode & S_IFMT) == S_IFLNK) { 57527c478bd9Sstevel@tonic-gate if ((bytes = readlink(file, linkcontents, PATH_MAX)) == -1) { 57537c478bd9Sstevel@tonic-gate err_print(READLINK_FAILED, fcn, file, strerror(errno)); 57547c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 57557c478bd9Sstevel@tonic-gate } 57567c478bd9Sstevel@tonic-gate linkcontents[bytes] = '\0'; 57577c478bd9Sstevel@tonic-gate if (symlink(linkcontents, newfile) == -1) { 57587c478bd9Sstevel@tonic-gate err_print(SYMLINK_FAILED, newfile, newfile, 57597c478bd9Sstevel@tonic-gate strerror(errno)); 57607c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 57617c478bd9Sstevel@tonic-gate } 57627c478bd9Sstevel@tonic-gate } 57637c478bd9Sstevel@tonic-gate 57647c478bd9Sstevel@tonic-gate (void) lchown(newfile, stat->st_uid, stat->st_gid); 57657c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 57667c478bd9Sstevel@tonic-gate } 57677c478bd9Sstevel@tonic-gate 57687c478bd9Sstevel@tonic-gate /* 57697c478bd9Sstevel@tonic-gate * Given a dev_t from the running kernel, return the new_dev_t 57707c478bd9Sstevel@tonic-gate * by translating to the major number found on the installed 57717c478bd9Sstevel@tonic-gate * target's root name_to_major file. 57727c478bd9Sstevel@tonic-gate */ 57737c478bd9Sstevel@tonic-gate static int 57747c478bd9Sstevel@tonic-gate translate_major(dev_t old_dev, dev_t *new_dev) 57757c478bd9Sstevel@tonic-gate { 57767c478bd9Sstevel@tonic-gate major_t oldmajor; 57777c478bd9Sstevel@tonic-gate major_t newmajor; 57787c478bd9Sstevel@tonic-gate minor_t oldminor; 57797c478bd9Sstevel@tonic-gate minor_t newminor; 57807c478bd9Sstevel@tonic-gate char cdriver[FILENAME_MAX + 1]; 57817c478bd9Sstevel@tonic-gate char driver[FILENAME_MAX + 1]; 57827c478bd9Sstevel@tonic-gate char *fcn = "translate_major: "; 57837c478bd9Sstevel@tonic-gate 57847c478bd9Sstevel@tonic-gate oldmajor = major(old_dev); 57857c478bd9Sstevel@tonic-gate if (modctl(MODGETNAME, driver, sizeof (driver), 57867c478bd9Sstevel@tonic-gate &oldmajor) != 0) { 57877c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 57887c478bd9Sstevel@tonic-gate } 57897c478bd9Sstevel@tonic-gate 57907c478bd9Sstevel@tonic-gate if (strcmp(driver, "clone") != 0) { 57917c478bd9Sstevel@tonic-gate /* non-clone case */ 57927c478bd9Sstevel@tonic-gate 57937c478bd9Sstevel@tonic-gate /* look up major number is target's name2major */ 57947c478bd9Sstevel@tonic-gate if (get_major_no(driver, &newmajor) == DEVFSADM_FAILURE) { 57957c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 57967c478bd9Sstevel@tonic-gate } 57977c478bd9Sstevel@tonic-gate 57987c478bd9Sstevel@tonic-gate *new_dev = makedev(newmajor, minor(old_dev)); 57997c478bd9Sstevel@tonic-gate if (old_dev != *new_dev) { 58007c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "%sdriver: %s old: %lu,%lu " 58017c478bd9Sstevel@tonic-gate "new: %lu,%lu\n", fcn, driver, major(old_dev), 58027c478bd9Sstevel@tonic-gate minor(old_dev), major(*new_dev), 58037c478bd9Sstevel@tonic-gate minor(*new_dev)); 58047c478bd9Sstevel@tonic-gate } 58057c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 58067c478bd9Sstevel@tonic-gate } else { 58077c478bd9Sstevel@tonic-gate /* 58087c478bd9Sstevel@tonic-gate * The clone is a special case. Look at its minor 58097c478bd9Sstevel@tonic-gate * number since it is the major number of the real driver. 58107c478bd9Sstevel@tonic-gate */ 58117c478bd9Sstevel@tonic-gate if (get_major_no(driver, &newmajor) == DEVFSADM_FAILURE) { 58127c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 58137c478bd9Sstevel@tonic-gate } 58147c478bd9Sstevel@tonic-gate 58157c478bd9Sstevel@tonic-gate oldminor = minor(old_dev); 58167c478bd9Sstevel@tonic-gate if (modctl(MODGETNAME, cdriver, sizeof (cdriver), 58177c478bd9Sstevel@tonic-gate &oldminor) != 0) { 58187c478bd9Sstevel@tonic-gate err_print(MODGETNAME_FAILED, oldminor); 58197c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 58207c478bd9Sstevel@tonic-gate } 58217c478bd9Sstevel@tonic-gate 58227c478bd9Sstevel@tonic-gate if (get_major_no(cdriver, &newminor) == DEVFSADM_FAILURE) { 58237c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 58247c478bd9Sstevel@tonic-gate } 58257c478bd9Sstevel@tonic-gate 58267c478bd9Sstevel@tonic-gate *new_dev = makedev(newmajor, newminor); 58277c478bd9Sstevel@tonic-gate if (old_dev != *new_dev) { 58287c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "%sdriver: %s old: " 58297c478bd9Sstevel@tonic-gate "%lu,%lu new: %lu,%lu\n", fcn, driver, 58307c478bd9Sstevel@tonic-gate major(old_dev), minor(old_dev), 58317c478bd9Sstevel@tonic-gate major(*new_dev), minor(*new_dev)); 58327c478bd9Sstevel@tonic-gate } 58337c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 58347c478bd9Sstevel@tonic-gate } 58357c478bd9Sstevel@tonic-gate } 58367c478bd9Sstevel@tonic-gate 58377c478bd9Sstevel@tonic-gate /* 58387c478bd9Sstevel@tonic-gate * 58397c478bd9Sstevel@tonic-gate * Find the major number for driver, searching the n2m_list that was 58407c478bd9Sstevel@tonic-gate * built in load_n2m_table(). 58417c478bd9Sstevel@tonic-gate */ 58427c478bd9Sstevel@tonic-gate static int 58437c478bd9Sstevel@tonic-gate get_major_no(char *driver, major_t *major) 58447c478bd9Sstevel@tonic-gate { 58457c478bd9Sstevel@tonic-gate n2m_t *ptr; 58467c478bd9Sstevel@tonic-gate 58477c478bd9Sstevel@tonic-gate for (ptr = n2m_list; ptr != NULL; ptr = ptr->next) { 58487c478bd9Sstevel@tonic-gate if (strcmp(ptr->driver, driver) == 0) { 58497c478bd9Sstevel@tonic-gate *major = ptr->major; 58507c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 58517c478bd9Sstevel@tonic-gate } 58527c478bd9Sstevel@tonic-gate } 58537c478bd9Sstevel@tonic-gate err_print(FIND_MAJOR_FAILED, driver); 58547c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 58557c478bd9Sstevel@tonic-gate } 58567c478bd9Sstevel@tonic-gate 58577c478bd9Sstevel@tonic-gate /* 58587c478bd9Sstevel@tonic-gate * Loads a name_to_major table into memory. Used only for suninstall's 58597c478bd9Sstevel@tonic-gate * private -R option to devfsadm, to translate major numbers from the 58607c478bd9Sstevel@tonic-gate * running to the installed target disk. 58617c478bd9Sstevel@tonic-gate */ 58627c478bd9Sstevel@tonic-gate static int 58637c478bd9Sstevel@tonic-gate load_n2m_table(char *file) 58647c478bd9Sstevel@tonic-gate { 58657c478bd9Sstevel@tonic-gate FILE *fp; 58661ca93273Seota char line[1024], *cp; 58677c478bd9Sstevel@tonic-gate char driver[PATH_MAX + 1]; 58687c478bd9Sstevel@tonic-gate major_t major; 58697c478bd9Sstevel@tonic-gate n2m_t *ptr; 58707c478bd9Sstevel@tonic-gate int ln = 0; 58717c478bd9Sstevel@tonic-gate 58727c478bd9Sstevel@tonic-gate if ((fp = fopen(file, "r")) == NULL) { 58737c478bd9Sstevel@tonic-gate err_print(FOPEN_FAILED, file, strerror(errno)); 58747c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 58757c478bd9Sstevel@tonic-gate } 58767c478bd9Sstevel@tonic-gate 58777c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line), fp) != NULL) { 58787c478bd9Sstevel@tonic-gate ln++; 58791ca93273Seota /* cut off comments starting with '#' */ 58801ca93273Seota if ((cp = strchr(line, '#')) != NULL) 58811ca93273Seota *cp = '\0'; 58821ca93273Seota /* ignore comment or blank lines */ 58831ca93273Seota if (is_blank(line)) 58847c478bd9Sstevel@tonic-gate continue; 58851ca93273Seota /* sanity-check */ 58867c478bd9Sstevel@tonic-gate if (sscanf(line, "%1024s%lu", driver, &major) != 2) { 58877c478bd9Sstevel@tonic-gate err_print(IGNORING_LINE_IN, ln, file); 58887c478bd9Sstevel@tonic-gate continue; 58897c478bd9Sstevel@tonic-gate } 58907c478bd9Sstevel@tonic-gate ptr = (n2m_t *)s_malloc(sizeof (n2m_t)); 58917c478bd9Sstevel@tonic-gate ptr->major = major; 58927c478bd9Sstevel@tonic-gate ptr->driver = s_strdup(driver); 58937c478bd9Sstevel@tonic-gate ptr->next = n2m_list; 58947c478bd9Sstevel@tonic-gate n2m_list = ptr; 58957c478bd9Sstevel@tonic-gate } 58967c478bd9Sstevel@tonic-gate if (fclose(fp) == EOF) { 58977c478bd9Sstevel@tonic-gate err_print(FCLOSE_FAILED, file, strerror(errno)); 58987c478bd9Sstevel@tonic-gate } 58997c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 59007c478bd9Sstevel@tonic-gate } 59017c478bd9Sstevel@tonic-gate 59027c478bd9Sstevel@tonic-gate /* 59038d483882Smlf * Called at devfsadm startup to read the file /etc/dev/enumerate_reserved 59048d483882Smlf * Creates a linked list of devlinks from which reserved IDs can be derived 59058d483882Smlf */ 59068d483882Smlf static void 59078d483882Smlf read_enumerate_file(void) 59088d483882Smlf { 59098d483882Smlf FILE *fp; 59108d483882Smlf int linenum; 59118d483882Smlf char line[PATH_MAX+1]; 59128d483882Smlf enumerate_file_t *entry; 59138d483882Smlf struct stat current_sb; 59148d483882Smlf static struct stat cached_sb; 59158d483882Smlf static int cached = FALSE; 59168d483882Smlf 59178d483882Smlf assert(enumerate_file); 59188d483882Smlf 59198d483882Smlf if (stat(enumerate_file, ¤t_sb) == -1) { 59208d483882Smlf vprint(RSRV_MID, "No reserved file: %s\n", enumerate_file); 59218d483882Smlf cached = FALSE; 59228d483882Smlf if (enumerate_reserved != NULL) { 59238d483882Smlf vprint(RSRV_MID, "invalidating %s cache\n", 59248d483882Smlf enumerate_file); 59258d483882Smlf } 59268d483882Smlf while (enumerate_reserved != NULL) { 59278d483882Smlf entry = enumerate_reserved; 59288d483882Smlf enumerate_reserved = entry->er_next; 59298d483882Smlf free(entry->er_file); 59308d483882Smlf free(entry->er_id); 59318d483882Smlf free(entry); 59328d483882Smlf } 59338d483882Smlf return; 59348d483882Smlf } 59358d483882Smlf 59368d483882Smlf /* if already cached, check to see if it is still valid */ 59378d483882Smlf if (cached == TRUE) { 59388d483882Smlf 59398d483882Smlf if (current_sb.st_mtime == cached_sb.st_mtime) { 59408d483882Smlf vprint(RSRV_MID, "%s cache valid\n", enumerate_file); 59418d483882Smlf vprint(FILES_MID, "%s cache valid\n", enumerate_file); 59428d483882Smlf return; 59438d483882Smlf } 59448d483882Smlf 59458d483882Smlf vprint(RSRV_MID, "invalidating %s cache\n", enumerate_file); 59468d483882Smlf vprint(FILES_MID, "invalidating %s cache\n", enumerate_file); 59478d483882Smlf 59488d483882Smlf while (enumerate_reserved != NULL) { 59498d483882Smlf entry = enumerate_reserved; 59508d483882Smlf enumerate_reserved = entry->er_next; 59518d483882Smlf free(entry->er_file); 59528d483882Smlf free(entry->er_id); 59538d483882Smlf free(entry); 59548d483882Smlf } 59558d483882Smlf vprint(RSRV_MID, "Recaching file: %s\n", enumerate_file); 59568d483882Smlf } else { 59578d483882Smlf vprint(RSRV_MID, "Caching file (first time): %s\n", 59588d483882Smlf enumerate_file); 59598d483882Smlf cached = TRUE; 59608d483882Smlf } 59618d483882Smlf 59628d483882Smlf (void) stat(enumerate_file, &cached_sb); 59638d483882Smlf 59648d483882Smlf if ((fp = fopen(enumerate_file, "r")) == NULL) { 59658d483882Smlf err_print(FOPEN_FAILED, enumerate_file, strerror(errno)); 59668d483882Smlf return; 59678d483882Smlf } 59688d483882Smlf 59698d483882Smlf vprint(RSRV_MID, "Reading reserve file: %s\n", enumerate_file); 59708d483882Smlf linenum = 0; 59718d483882Smlf while (fgets(line, sizeof (line), fp) != NULL) { 59728d483882Smlf char *cp, *ncp; 59738d483882Smlf 59748d483882Smlf linenum++; 59758d483882Smlf 59768d483882Smlf /* remove newline */ 59778d483882Smlf cp = strchr(line, '\n'); 59788d483882Smlf if (cp) 59798d483882Smlf *cp = '\0'; 59808d483882Smlf 59818d483882Smlf vprint(RSRV_MID, "Reserve file: line %d: %s\n", 59828d483882Smlf linenum, line); 59838d483882Smlf 59848d483882Smlf /* skip over space and tab */ 59858d483882Smlf for (cp = line; *cp == ' ' || *cp == '\t'; cp++); 59868d483882Smlf 59878d483882Smlf if (*cp == '\0' || *cp == '#') { 59888d483882Smlf vprint(RSRV_MID, "Skipping line: '%s'\n", line); 59898d483882Smlf continue; /* blank line or comment line */ 59908d483882Smlf } 59918d483882Smlf 59928d483882Smlf ncp = cp; 59938d483882Smlf 59948d483882Smlf /* delete trailing blanks */ 59958d483882Smlf for (; *cp != ' ' && *cp != '\t' && *cp != '\0'; cp++); 59968d483882Smlf *cp = '\0'; 59978d483882Smlf 59988d483882Smlf entry = s_zalloc(sizeof (enumerate_file_t)); 59998d483882Smlf entry->er_file = s_strdup(ncp); 60008d483882Smlf entry->er_id = NULL; 60018d483882Smlf entry->er_next = enumerate_reserved; 60028d483882Smlf enumerate_reserved = entry; 60038d483882Smlf } 60048d483882Smlf 60058d483882Smlf if (fclose(fp) == EOF) { 60068d483882Smlf err_print(FCLOSE_FAILED, enumerate_file, strerror(errno)); 60078d483882Smlf } 60088d483882Smlf } 60098d483882Smlf 60108d483882Smlf /* 60117c478bd9Sstevel@tonic-gate * Called at devfsadm startup to read in the devlink.tab file. Creates 60127c478bd9Sstevel@tonic-gate * a linked list of devlinktab_list structures which will be 60137c478bd9Sstevel@tonic-gate * searched for every minor node. 60147c478bd9Sstevel@tonic-gate */ 60157c478bd9Sstevel@tonic-gate static void 60167c478bd9Sstevel@tonic-gate read_devlinktab_file(void) 60177c478bd9Sstevel@tonic-gate { 60187c478bd9Sstevel@tonic-gate devlinktab_list_t *headp = NULL; 60197c478bd9Sstevel@tonic-gate devlinktab_list_t *entryp; 60207c478bd9Sstevel@tonic-gate devlinktab_list_t **previous; 60217c478bd9Sstevel@tonic-gate devlinktab_list_t *save; 60221ca93273Seota char line[MAX_DEVLINK_LINE], *cp; 60237c478bd9Sstevel@tonic-gate char *selector; 60247c478bd9Sstevel@tonic-gate char *p_link; 60257c478bd9Sstevel@tonic-gate char *s_link; 60267c478bd9Sstevel@tonic-gate FILE *fp; 60277c478bd9Sstevel@tonic-gate int i; 60287c478bd9Sstevel@tonic-gate static struct stat cached_sb; 60297c478bd9Sstevel@tonic-gate struct stat current_sb; 60307c478bd9Sstevel@tonic-gate static int cached = FALSE; 60317c478bd9Sstevel@tonic-gate 60327c478bd9Sstevel@tonic-gate if (devlinktab_file == NULL) { 60337c478bd9Sstevel@tonic-gate return; 60347c478bd9Sstevel@tonic-gate } 60357c478bd9Sstevel@tonic-gate 60367c478bd9Sstevel@tonic-gate (void) stat(devlinktab_file, ¤t_sb); 60377c478bd9Sstevel@tonic-gate 60387c478bd9Sstevel@tonic-gate /* if already cached, check to see if it is still valid */ 60397c478bd9Sstevel@tonic-gate if (cached == TRUE) { 60407c478bd9Sstevel@tonic-gate 60417c478bd9Sstevel@tonic-gate if (current_sb.st_mtime == cached_sb.st_mtime) { 60427c478bd9Sstevel@tonic-gate vprint(FILES_MID, "%s cache valid\n", devlinktab_file); 60437c478bd9Sstevel@tonic-gate return; 60447c478bd9Sstevel@tonic-gate } 60457c478bd9Sstevel@tonic-gate 60467c478bd9Sstevel@tonic-gate vprint(FILES_MID, "invalidating %s cache\n", devlinktab_file); 60477c478bd9Sstevel@tonic-gate 60487c478bd9Sstevel@tonic-gate while (devlinktab_list != NULL) { 60497c478bd9Sstevel@tonic-gate free_link_list(devlinktab_list->p_link); 60507c478bd9Sstevel@tonic-gate free_link_list(devlinktab_list->s_link); 60517c478bd9Sstevel@tonic-gate free_selector_list(devlinktab_list->selector); 60527c478bd9Sstevel@tonic-gate free(devlinktab_list->selector_pattern); 60537c478bd9Sstevel@tonic-gate free(devlinktab_list->p_link_pattern); 60547c478bd9Sstevel@tonic-gate if (devlinktab_list->s_link_pattern != NULL) { 60557c478bd9Sstevel@tonic-gate free(devlinktab_list->s_link_pattern); 60567c478bd9Sstevel@tonic-gate } 60577c478bd9Sstevel@tonic-gate save = devlinktab_list; 60587c478bd9Sstevel@tonic-gate devlinktab_list = devlinktab_list->next; 60597c478bd9Sstevel@tonic-gate free(save); 60607c478bd9Sstevel@tonic-gate } 60617c478bd9Sstevel@tonic-gate } else { 60627c478bd9Sstevel@tonic-gate cached = TRUE; 60637c478bd9Sstevel@tonic-gate } 60647c478bd9Sstevel@tonic-gate 60657c478bd9Sstevel@tonic-gate (void) stat(devlinktab_file, &cached_sb); 60667c478bd9Sstevel@tonic-gate 60677c478bd9Sstevel@tonic-gate if ((fp = fopen(devlinktab_file, "r")) == NULL) { 60687c478bd9Sstevel@tonic-gate err_print(FOPEN_FAILED, devlinktab_file, strerror(errno)); 60697c478bd9Sstevel@tonic-gate return; 60707c478bd9Sstevel@tonic-gate } 60717c478bd9Sstevel@tonic-gate 60727c478bd9Sstevel@tonic-gate previous = &headp; 60737c478bd9Sstevel@tonic-gate 60747c478bd9Sstevel@tonic-gate while (fgets(line, sizeof (line), fp) != NULL) { 60757c478bd9Sstevel@tonic-gate devlinktab_line++; 60767c478bd9Sstevel@tonic-gate i = strlen(line); 60777c478bd9Sstevel@tonic-gate if (line[i-1] == NEWLINE) { 60787c478bd9Sstevel@tonic-gate line[i-1] = '\0'; 60797c478bd9Sstevel@tonic-gate } else if (i == sizeof (line-1)) { 60807c478bd9Sstevel@tonic-gate err_print(LINE_TOO_LONG, devlinktab_line, 60817c478bd9Sstevel@tonic-gate devlinktab_file, sizeof (line)-1); 60827c478bd9Sstevel@tonic-gate while (((i = getc(fp)) != '\n') && (i != EOF)); 60837c478bd9Sstevel@tonic-gate continue; 60847c478bd9Sstevel@tonic-gate } 60857c478bd9Sstevel@tonic-gate 60861ca93273Seota /* cut off comments starting with '#' */ 60871ca93273Seota if ((cp = strchr(line, '#')) != NULL) 60881ca93273Seota *cp = '\0'; 60891ca93273Seota /* ignore comment or blank lines */ 60901ca93273Seota if (is_blank(line)) 60917c478bd9Sstevel@tonic-gate continue; 60927c478bd9Sstevel@tonic-gate 60937c478bd9Sstevel@tonic-gate vprint(DEVLINK_MID, "table: %s line %d: '%s'\n", 60947c478bd9Sstevel@tonic-gate devlinktab_file, devlinktab_line, line); 60957c478bd9Sstevel@tonic-gate 60967c478bd9Sstevel@tonic-gate /* break each entry into fields. s_link may be NULL */ 60977c478bd9Sstevel@tonic-gate if (split_devlinktab_entry(line, &selector, &p_link, 60987c478bd9Sstevel@tonic-gate &s_link) == DEVFSADM_FAILURE) { 60997c478bd9Sstevel@tonic-gate vprint(DEVLINK_MID, "split_entry returns failure\n"); 61007c478bd9Sstevel@tonic-gate continue; 61017c478bd9Sstevel@tonic-gate } else { 61027c478bd9Sstevel@tonic-gate vprint(DEVLINK_MID, "split_entry selector='%s' " 61037c478bd9Sstevel@tonic-gate "p_link='%s' s_link='%s'\n\n", selector, 61047c478bd9Sstevel@tonic-gate p_link, (s_link == NULL) ? "" : s_link); 61057c478bd9Sstevel@tonic-gate } 61067c478bd9Sstevel@tonic-gate 61077c478bd9Sstevel@tonic-gate entryp = (devlinktab_list_t *) 61087c478bd9Sstevel@tonic-gate s_malloc(sizeof (devlinktab_list_t)); 61097c478bd9Sstevel@tonic-gate 61107c478bd9Sstevel@tonic-gate entryp->line_number = devlinktab_line; 61117c478bd9Sstevel@tonic-gate 61127c478bd9Sstevel@tonic-gate if ((entryp->selector = 61137c478bd9Sstevel@tonic-gate create_selector_list(selector)) == NULL) { 61147c478bd9Sstevel@tonic-gate free(entryp); 61157c478bd9Sstevel@tonic-gate continue; 61167c478bd9Sstevel@tonic-gate } 61177c478bd9Sstevel@tonic-gate entryp->selector_pattern = s_strdup(selector); 61187c478bd9Sstevel@tonic-gate 61197c478bd9Sstevel@tonic-gate if ((entryp->p_link = create_link_list(p_link)) == NULL) { 61207c478bd9Sstevel@tonic-gate free_selector_list(entryp->selector); 61217c478bd9Sstevel@tonic-gate free(entryp->selector_pattern); 61227c478bd9Sstevel@tonic-gate free(entryp); 61237c478bd9Sstevel@tonic-gate continue; 61247c478bd9Sstevel@tonic-gate } 61257c478bd9Sstevel@tonic-gate 61267c478bd9Sstevel@tonic-gate entryp->p_link_pattern = s_strdup(p_link); 61277c478bd9Sstevel@tonic-gate 61287c478bd9Sstevel@tonic-gate if (s_link != NULL) { 61297c478bd9Sstevel@tonic-gate if ((entryp->s_link = 61307c478bd9Sstevel@tonic-gate create_link_list(s_link)) == NULL) { 61317c478bd9Sstevel@tonic-gate free_selector_list(entryp->selector); 61327c478bd9Sstevel@tonic-gate free_link_list(entryp->p_link); 61337c478bd9Sstevel@tonic-gate free(entryp->selector_pattern); 61347c478bd9Sstevel@tonic-gate free(entryp->p_link_pattern); 61357c478bd9Sstevel@tonic-gate free(entryp); 61367c478bd9Sstevel@tonic-gate continue; 61377c478bd9Sstevel@tonic-gate } 61387c478bd9Sstevel@tonic-gate entryp->s_link_pattern = s_strdup(s_link); 61397c478bd9Sstevel@tonic-gate } else { 61407c478bd9Sstevel@tonic-gate entryp->s_link = NULL; 61417c478bd9Sstevel@tonic-gate entryp->s_link_pattern = NULL; 61427c478bd9Sstevel@tonic-gate 61437c478bd9Sstevel@tonic-gate } 61447c478bd9Sstevel@tonic-gate 61457c478bd9Sstevel@tonic-gate /* append to end of list */ 61467c478bd9Sstevel@tonic-gate 61477c478bd9Sstevel@tonic-gate entryp->next = NULL; 61487c478bd9Sstevel@tonic-gate *previous = entryp; 61497c478bd9Sstevel@tonic-gate previous = &(entryp->next); 61507c478bd9Sstevel@tonic-gate } 61517c478bd9Sstevel@tonic-gate if (fclose(fp) == EOF) { 61527c478bd9Sstevel@tonic-gate err_print(FCLOSE_FAILED, devlinktab_file, strerror(errno)); 61537c478bd9Sstevel@tonic-gate } 61547c478bd9Sstevel@tonic-gate devlinktab_list = headp; 61557c478bd9Sstevel@tonic-gate } 61567c478bd9Sstevel@tonic-gate 61577c478bd9Sstevel@tonic-gate /* 61587c478bd9Sstevel@tonic-gate * 61597c478bd9Sstevel@tonic-gate * For a single line entry in devlink.tab, split the line into fields 61607c478bd9Sstevel@tonic-gate * selector, p_link, and an optionally s_link. If s_link field is not 61617c478bd9Sstevel@tonic-gate * present, then return NULL in s_link (not NULL string). 61627c478bd9Sstevel@tonic-gate */ 61637c478bd9Sstevel@tonic-gate static int 61647c478bd9Sstevel@tonic-gate split_devlinktab_entry(char *entry, char **selector, char **p_link, 61657c478bd9Sstevel@tonic-gate char **s_link) 61667c478bd9Sstevel@tonic-gate { 61677c478bd9Sstevel@tonic-gate char *tab; 61687c478bd9Sstevel@tonic-gate 61697c478bd9Sstevel@tonic-gate *selector = entry; 61707c478bd9Sstevel@tonic-gate 61717c478bd9Sstevel@tonic-gate if ((tab = strchr(entry, TAB)) != NULL) { 61727c478bd9Sstevel@tonic-gate *tab = '\0'; 61737c478bd9Sstevel@tonic-gate *p_link = ++tab; 61747c478bd9Sstevel@tonic-gate } else { 61757c478bd9Sstevel@tonic-gate err_print(MISSING_TAB, devlinktab_line, devlinktab_file); 61767c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 61777c478bd9Sstevel@tonic-gate } 61787c478bd9Sstevel@tonic-gate 61797c478bd9Sstevel@tonic-gate if (*p_link == '\0') { 61807c478bd9Sstevel@tonic-gate err_print(MISSING_DEVNAME, devlinktab_line, devlinktab_file); 61817c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 61827c478bd9Sstevel@tonic-gate } 61837c478bd9Sstevel@tonic-gate 61847c478bd9Sstevel@tonic-gate if ((tab = strchr(*p_link, TAB)) != NULL) { 61857c478bd9Sstevel@tonic-gate *tab = '\0'; 61867c478bd9Sstevel@tonic-gate *s_link = ++tab; 61877c478bd9Sstevel@tonic-gate if (strchr(*s_link, TAB) != NULL) { 61887c478bd9Sstevel@tonic-gate err_print(TOO_MANY_FIELDS, devlinktab_line, 61897c478bd9Sstevel@tonic-gate devlinktab_file); 61907c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 61917c478bd9Sstevel@tonic-gate } 61927c478bd9Sstevel@tonic-gate } else { 61937c478bd9Sstevel@tonic-gate *s_link = NULL; 61947c478bd9Sstevel@tonic-gate } 61957c478bd9Sstevel@tonic-gate 61967c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 61977c478bd9Sstevel@tonic-gate } 61987c478bd9Sstevel@tonic-gate 61997c478bd9Sstevel@tonic-gate /* 62007c478bd9Sstevel@tonic-gate * For a given devfs_spec field, for each element in the field, add it to 62017c478bd9Sstevel@tonic-gate * a linked list of devfs_spec structures. Return the linked list in 62027c478bd9Sstevel@tonic-gate * devfs_spec_list. 62037c478bd9Sstevel@tonic-gate */ 62047c478bd9Sstevel@tonic-gate static selector_list_t * 62057c478bd9Sstevel@tonic-gate create_selector_list(char *selector) 62067c478bd9Sstevel@tonic-gate { 62077c478bd9Sstevel@tonic-gate char *key; 62087c478bd9Sstevel@tonic-gate char *val; 62097c478bd9Sstevel@tonic-gate int error = FALSE; 62107c478bd9Sstevel@tonic-gate selector_list_t *head_selector_list = NULL; 62117c478bd9Sstevel@tonic-gate selector_list_t *selector_list; 62127c478bd9Sstevel@tonic-gate 62137c478bd9Sstevel@tonic-gate /* parse_devfs_spec splits the next field into keyword & value */ 62147c478bd9Sstevel@tonic-gate while ((*selector != NULL) && (error == FALSE)) { 62157c478bd9Sstevel@tonic-gate if (parse_selector(&selector, &key, 62167c478bd9Sstevel@tonic-gate &val) == DEVFSADM_FAILURE) { 62177c478bd9Sstevel@tonic-gate error = TRUE; 62187c478bd9Sstevel@tonic-gate break; 62197c478bd9Sstevel@tonic-gate } else { 62207c478bd9Sstevel@tonic-gate selector_list = (selector_list_t *) 62217c478bd9Sstevel@tonic-gate s_malloc(sizeof (selector_list_t)); 62227c478bd9Sstevel@tonic-gate if (strcmp(NAME_S, key) == 0) { 62237c478bd9Sstevel@tonic-gate selector_list->key = NAME; 62247c478bd9Sstevel@tonic-gate } else if (strcmp(TYPE_S, key) == 0) { 62257c478bd9Sstevel@tonic-gate selector_list->key = TYPE; 62267c478bd9Sstevel@tonic-gate } else if (strncmp(ADDR_S, key, ADDR_S_LEN) == 0) { 62277c478bd9Sstevel@tonic-gate selector_list->key = ADDR; 62287c478bd9Sstevel@tonic-gate if (key[ADDR_S_LEN] == '\0') { 62297c478bd9Sstevel@tonic-gate selector_list->arg = 0; 62307c478bd9Sstevel@tonic-gate } else if (isdigit(key[ADDR_S_LEN]) != 62317c478bd9Sstevel@tonic-gate FALSE) { 62327c478bd9Sstevel@tonic-gate selector_list->arg = 62337c478bd9Sstevel@tonic-gate atoi(&key[ADDR_S_LEN]); 62347c478bd9Sstevel@tonic-gate } else { 62357c478bd9Sstevel@tonic-gate error = TRUE; 62367c478bd9Sstevel@tonic-gate free(selector_list); 62377c478bd9Sstevel@tonic-gate err_print(BADKEYWORD, key, 62387c478bd9Sstevel@tonic-gate devlinktab_line, 62397c478bd9Sstevel@tonic-gate devlinktab_file); 62407c478bd9Sstevel@tonic-gate break; 62417c478bd9Sstevel@tonic-gate } 62427c478bd9Sstevel@tonic-gate } else if (strncmp(MINOR_S, key, 62437c478bd9Sstevel@tonic-gate MINOR_S_LEN) == 0) { 62447c478bd9Sstevel@tonic-gate selector_list->key = MINOR; 62457c478bd9Sstevel@tonic-gate if (key[MINOR_S_LEN] == '\0') { 62467c478bd9Sstevel@tonic-gate selector_list->arg = 0; 62477c478bd9Sstevel@tonic-gate } else if (isdigit(key[MINOR_S_LEN]) != 62487c478bd9Sstevel@tonic-gate FALSE) { 62497c478bd9Sstevel@tonic-gate selector_list->arg = 62507c478bd9Sstevel@tonic-gate atoi(&key[MINOR_S_LEN]); 62517c478bd9Sstevel@tonic-gate } else { 62527c478bd9Sstevel@tonic-gate error = TRUE; 62537c478bd9Sstevel@tonic-gate free(selector_list); 62547c478bd9Sstevel@tonic-gate err_print(BADKEYWORD, key, 62557c478bd9Sstevel@tonic-gate devlinktab_line, 62567c478bd9Sstevel@tonic-gate devlinktab_file); 62577c478bd9Sstevel@tonic-gate break; 62587c478bd9Sstevel@tonic-gate } 62597c478bd9Sstevel@tonic-gate vprint(DEVLINK_MID, "MINOR = %s\n", val); 62607c478bd9Sstevel@tonic-gate } else { 62617c478bd9Sstevel@tonic-gate err_print(UNRECOGNIZED_KEY, key, 62627c478bd9Sstevel@tonic-gate devlinktab_line, devlinktab_file); 62637c478bd9Sstevel@tonic-gate error = TRUE; 62647c478bd9Sstevel@tonic-gate free(selector_list); 62657c478bd9Sstevel@tonic-gate break; 62667c478bd9Sstevel@tonic-gate } 62677c478bd9Sstevel@tonic-gate selector_list->val = s_strdup(val); 62687c478bd9Sstevel@tonic-gate selector_list->next = head_selector_list; 62697c478bd9Sstevel@tonic-gate head_selector_list = selector_list; 62707c478bd9Sstevel@tonic-gate vprint(DEVLINK_MID, "key='%s' val='%s' arg=%d\n", 62717c478bd9Sstevel@tonic-gate key, val, selector_list->arg); 62727c478bd9Sstevel@tonic-gate } 62737c478bd9Sstevel@tonic-gate } 62747c478bd9Sstevel@tonic-gate 62757c478bd9Sstevel@tonic-gate if ((error == FALSE) && (head_selector_list != NULL)) { 62767c478bd9Sstevel@tonic-gate return (head_selector_list); 62777c478bd9Sstevel@tonic-gate } else { 62787c478bd9Sstevel@tonic-gate /* parse failed. Free any allocated structs */ 62797c478bd9Sstevel@tonic-gate free_selector_list(head_selector_list); 62807c478bd9Sstevel@tonic-gate return (NULL); 62817c478bd9Sstevel@tonic-gate } 62827c478bd9Sstevel@tonic-gate } 62837c478bd9Sstevel@tonic-gate 62847c478bd9Sstevel@tonic-gate /* 62857c478bd9Sstevel@tonic-gate * Takes a semicolon separated list of selector elements and breaks up 62867c478bd9Sstevel@tonic-gate * into a keyword-value pair. semicolon and equal characters are 62877c478bd9Sstevel@tonic-gate * replaced with NULL's. On success, selector is updated to point to the 62887c478bd9Sstevel@tonic-gate * terminating NULL character terminating the keyword-value pair, and the 62897c478bd9Sstevel@tonic-gate * function returns DEVFSADM_SUCCESS. If there is a syntax error, 62907c478bd9Sstevel@tonic-gate * devfs_spec is not modified and function returns DEVFSADM_FAILURE. 62917c478bd9Sstevel@tonic-gate */ 62927c478bd9Sstevel@tonic-gate static int 62937c478bd9Sstevel@tonic-gate parse_selector(char **selector, char **key, char **val) 62947c478bd9Sstevel@tonic-gate { 62957c478bd9Sstevel@tonic-gate char *equal; 62967c478bd9Sstevel@tonic-gate char *semi_colon; 62977c478bd9Sstevel@tonic-gate 62987c478bd9Sstevel@tonic-gate *key = *selector; 62997c478bd9Sstevel@tonic-gate 63007c478bd9Sstevel@tonic-gate if ((equal = strchr(*key, '=')) != NULL) { 63017c478bd9Sstevel@tonic-gate *equal = '\0'; 63027c478bd9Sstevel@tonic-gate } else { 63037c478bd9Sstevel@tonic-gate err_print(MISSING_EQUAL, devlinktab_line, devlinktab_file); 63047c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 63057c478bd9Sstevel@tonic-gate } 63067c478bd9Sstevel@tonic-gate 63077c478bd9Sstevel@tonic-gate *val = ++equal; 63087c478bd9Sstevel@tonic-gate if ((semi_colon = strchr(equal, ';')) != NULL) { 63097c478bd9Sstevel@tonic-gate *semi_colon = '\0'; 63107c478bd9Sstevel@tonic-gate *selector = semi_colon + 1; 63117c478bd9Sstevel@tonic-gate } else { 63127c478bd9Sstevel@tonic-gate *selector = equal + strlen(equal); 63137c478bd9Sstevel@tonic-gate } 63147c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 63157c478bd9Sstevel@tonic-gate } 63167c478bd9Sstevel@tonic-gate 63177c478bd9Sstevel@tonic-gate /* 63187c478bd9Sstevel@tonic-gate * link is either the second or third field of devlink.tab. Parse link 63197c478bd9Sstevel@tonic-gate * into a linked list of devlink structures and return ptr to list. Each 63207c478bd9Sstevel@tonic-gate * list element is either a constant string, or one of the following 63217c478bd9Sstevel@tonic-gate * escape sequences: \M, \A, \N, or \D. The first three escape sequences 63227c478bd9Sstevel@tonic-gate * take a numerical argument. 63237c478bd9Sstevel@tonic-gate */ 63247c478bd9Sstevel@tonic-gate static link_list_t * 63257c478bd9Sstevel@tonic-gate create_link_list(char *link) 63267c478bd9Sstevel@tonic-gate { 63277c478bd9Sstevel@tonic-gate int x = 0; 63287c478bd9Sstevel@tonic-gate int error = FALSE; 63297c478bd9Sstevel@tonic-gate int counter_found = FALSE; 63307c478bd9Sstevel@tonic-gate link_list_t *head = NULL; 63317c478bd9Sstevel@tonic-gate link_list_t **ptr; 63327c478bd9Sstevel@tonic-gate link_list_t *link_list; 63337c478bd9Sstevel@tonic-gate char constant[MAX_DEVLINK_LINE]; 63347c478bd9Sstevel@tonic-gate char *error_str; 63357c478bd9Sstevel@tonic-gate 63367c478bd9Sstevel@tonic-gate if (link == NULL) { 63377c478bd9Sstevel@tonic-gate return (NULL); 63387c478bd9Sstevel@tonic-gate } 63397c478bd9Sstevel@tonic-gate 63407c478bd9Sstevel@tonic-gate while ((*link != '\0') && (error == FALSE)) { 63417c478bd9Sstevel@tonic-gate link_list = (link_list_t *)s_malloc(sizeof (link_list_t)); 63427c478bd9Sstevel@tonic-gate link_list->next = NULL; 63437c478bd9Sstevel@tonic-gate 63447c478bd9Sstevel@tonic-gate while ((*link != '\0') && (*link != '\\')) { 63457c478bd9Sstevel@tonic-gate /* a non-escaped string */ 63467c478bd9Sstevel@tonic-gate constant[x++] = *(link++); 63477c478bd9Sstevel@tonic-gate } 63487c478bd9Sstevel@tonic-gate if (x != 0) { 63497c478bd9Sstevel@tonic-gate constant[x] = '\0'; 63507c478bd9Sstevel@tonic-gate link_list->type = CONSTANT; 63517c478bd9Sstevel@tonic-gate link_list->constant = s_strdup(constant); 63527c478bd9Sstevel@tonic-gate x = 0; 63537c478bd9Sstevel@tonic-gate vprint(DEVLINK_MID, "CONSTANT FOUND %s\n", constant); 63547c478bd9Sstevel@tonic-gate } else { 63557c478bd9Sstevel@tonic-gate switch (*(++link)) { 63567c478bd9Sstevel@tonic-gate case 'M': 63577c478bd9Sstevel@tonic-gate link_list->type = MINOR; 63587c478bd9Sstevel@tonic-gate break; 63597c478bd9Sstevel@tonic-gate case 'A': 63607c478bd9Sstevel@tonic-gate link_list->type = ADDR; 63617c478bd9Sstevel@tonic-gate break; 63627c478bd9Sstevel@tonic-gate case 'N': 63637c478bd9Sstevel@tonic-gate if (counter_found == TRUE) { 63647c478bd9Sstevel@tonic-gate error = TRUE; 63657c478bd9Sstevel@tonic-gate error_str = "multiple counters " 63667c478bd9Sstevel@tonic-gate "not permitted"; 63677c478bd9Sstevel@tonic-gate free(link_list); 63687c478bd9Sstevel@tonic-gate } else { 63697c478bd9Sstevel@tonic-gate counter_found = TRUE; 63707c478bd9Sstevel@tonic-gate link_list->type = COUNTER; 63717c478bd9Sstevel@tonic-gate } 63727c478bd9Sstevel@tonic-gate break; 63737c478bd9Sstevel@tonic-gate case 'D': 63747c478bd9Sstevel@tonic-gate link_list->type = NAME; 63757c478bd9Sstevel@tonic-gate break; 63767c478bd9Sstevel@tonic-gate default: 63777c478bd9Sstevel@tonic-gate error = TRUE; 63787c478bd9Sstevel@tonic-gate free(link_list); 63797c478bd9Sstevel@tonic-gate error_str = "unrecognized escape sequence"; 63807c478bd9Sstevel@tonic-gate break; 63817c478bd9Sstevel@tonic-gate } 63827c478bd9Sstevel@tonic-gate if (*(link++) != 'D') { 63837c478bd9Sstevel@tonic-gate if (isdigit(*link) == FALSE) { 63847c478bd9Sstevel@tonic-gate error_str = "escape sequence must be " 63857c478bd9Sstevel@tonic-gate "followed by a digit\n"; 63867c478bd9Sstevel@tonic-gate error = TRUE; 63877c478bd9Sstevel@tonic-gate free(link_list); 63887c478bd9Sstevel@tonic-gate } else { 63897c478bd9Sstevel@tonic-gate link_list->arg = 63907c478bd9Sstevel@tonic-gate (int)strtoul(link, &link, 10); 63917c478bd9Sstevel@tonic-gate vprint(DEVLINK_MID, "link_list->arg = " 63927c478bd9Sstevel@tonic-gate "%d\n", link_list->arg); 63937c478bd9Sstevel@tonic-gate } 63947c478bd9Sstevel@tonic-gate } 63957c478bd9Sstevel@tonic-gate } 63967c478bd9Sstevel@tonic-gate /* append link_list struct to end of list */ 63977c478bd9Sstevel@tonic-gate if (error == FALSE) { 63987c478bd9Sstevel@tonic-gate for (ptr = &head; *ptr != NULL; ptr = &((*ptr)->next)); 63997c478bd9Sstevel@tonic-gate *ptr = link_list; 64007c478bd9Sstevel@tonic-gate } 64017c478bd9Sstevel@tonic-gate } 64027c478bd9Sstevel@tonic-gate 64037c478bd9Sstevel@tonic-gate if (error == FALSE) { 64047c478bd9Sstevel@tonic-gate return (head); 64057c478bd9Sstevel@tonic-gate } else { 64067c478bd9Sstevel@tonic-gate err_print(CONFIG_INCORRECT, devlinktab_line, devlinktab_file, 64077c478bd9Sstevel@tonic-gate error_str); 64087c478bd9Sstevel@tonic-gate free_link_list(head); 64097c478bd9Sstevel@tonic-gate return (NULL); 64107c478bd9Sstevel@tonic-gate } 64117c478bd9Sstevel@tonic-gate } 64127c478bd9Sstevel@tonic-gate 64137c478bd9Sstevel@tonic-gate /* 64147c478bd9Sstevel@tonic-gate * Called for each minor node devfsadm processes; for each minor node, 64157c478bd9Sstevel@tonic-gate * look for matches in the devlinktab_list list which was created on 64167c478bd9Sstevel@tonic-gate * startup read_devlinktab_file(). If there is a match, call build_links() 64177c478bd9Sstevel@tonic-gate * to build a logical devlink and a possible extra devlink. 64187c478bd9Sstevel@tonic-gate */ 64197c478bd9Sstevel@tonic-gate static int 64207c478bd9Sstevel@tonic-gate process_devlink_compat(di_minor_t minor, di_node_t node) 64217c478bd9Sstevel@tonic-gate { 64227c478bd9Sstevel@tonic-gate int link_built = FALSE; 64237c478bd9Sstevel@tonic-gate devlinktab_list_t *entry; 64247c478bd9Sstevel@tonic-gate char *nodetype; 64257c478bd9Sstevel@tonic-gate char *dev_path; 64267c478bd9Sstevel@tonic-gate 64277c478bd9Sstevel@tonic-gate if (devlinks_debug == TRUE) { 64287c478bd9Sstevel@tonic-gate nodetype = di_minor_nodetype(minor); 64297c478bd9Sstevel@tonic-gate assert(nodetype != NULL); 64307c478bd9Sstevel@tonic-gate if ((dev_path = di_devfs_path(node)) != NULL) { 64317c478bd9Sstevel@tonic-gate vprint(INFO_MID, "'%s' entry: %s:%s\n", nodetype, 64327c478bd9Sstevel@tonic-gate dev_path, 64337c478bd9Sstevel@tonic-gate di_minor_name(minor) ? di_minor_name(minor) : 64347c478bd9Sstevel@tonic-gate ""); 64357c478bd9Sstevel@tonic-gate di_devfs_path_free(dev_path); 64367c478bd9Sstevel@tonic-gate } 64377c478bd9Sstevel@tonic-gate 64387c478bd9Sstevel@tonic-gate } 64397c478bd9Sstevel@tonic-gate 64407c478bd9Sstevel@tonic-gate 64417c478bd9Sstevel@tonic-gate /* don't process devlink.tab if devfsadm invoked with -c <class> */ 64427c478bd9Sstevel@tonic-gate if (num_classes > 0) { 64437c478bd9Sstevel@tonic-gate return (FALSE); 64447c478bd9Sstevel@tonic-gate } 64457c478bd9Sstevel@tonic-gate 64467c478bd9Sstevel@tonic-gate for (entry = devlinktab_list; entry != NULL; entry = entry->next) { 64477c478bd9Sstevel@tonic-gate if (devlink_matches(entry, minor, node) == DEVFSADM_SUCCESS) { 64487c478bd9Sstevel@tonic-gate link_built = TRUE; 64497c478bd9Sstevel@tonic-gate (void) build_links(entry, minor, node); 64507c478bd9Sstevel@tonic-gate } 64517c478bd9Sstevel@tonic-gate } 64527c478bd9Sstevel@tonic-gate return (link_built); 64537c478bd9Sstevel@tonic-gate } 64547c478bd9Sstevel@tonic-gate 64557c478bd9Sstevel@tonic-gate /* 64567c478bd9Sstevel@tonic-gate * For a given devlink.tab devlinktab_list entry, see if the selector 64577c478bd9Sstevel@tonic-gate * field matches this minor node. If it does, return DEVFSADM_SUCCESS, 64587c478bd9Sstevel@tonic-gate * otherwise DEVFSADM_FAILURE. 64597c478bd9Sstevel@tonic-gate */ 64607c478bd9Sstevel@tonic-gate static int 64617c478bd9Sstevel@tonic-gate devlink_matches(devlinktab_list_t *entry, di_minor_t minor, di_node_t node) 64627c478bd9Sstevel@tonic-gate { 64637c478bd9Sstevel@tonic-gate selector_list_t *selector = entry->selector; 64647c478bd9Sstevel@tonic-gate char *addr; 64657c478bd9Sstevel@tonic-gate char *minor_name; 64667c478bd9Sstevel@tonic-gate char *node_type; 64677c478bd9Sstevel@tonic-gate 64687c478bd9Sstevel@tonic-gate for (; selector != NULL; selector = selector->next) { 64697c478bd9Sstevel@tonic-gate switch (selector->key) { 64707c478bd9Sstevel@tonic-gate case NAME: 64717c478bd9Sstevel@tonic-gate if (strcmp(di_node_name(node), selector->val) != 0) { 64727c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 64737c478bd9Sstevel@tonic-gate } 64747c478bd9Sstevel@tonic-gate break; 64757c478bd9Sstevel@tonic-gate case TYPE: 64767c478bd9Sstevel@tonic-gate node_type = di_minor_nodetype(minor); 64777c478bd9Sstevel@tonic-gate assert(node_type != NULL); 64787c478bd9Sstevel@tonic-gate if (strcmp(node_type, selector->val) != 0) { 64797c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 64807c478bd9Sstevel@tonic-gate } 64817c478bd9Sstevel@tonic-gate break; 64827c478bd9Sstevel@tonic-gate case ADDR: 64837c478bd9Sstevel@tonic-gate if ((addr = di_bus_addr(node)) == NULL) { 64847c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 64857c478bd9Sstevel@tonic-gate } 64867c478bd9Sstevel@tonic-gate if (selector->arg == 0) { 64877c478bd9Sstevel@tonic-gate if (strcmp(addr, selector->val) != 0) { 64887c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 64897c478bd9Sstevel@tonic-gate } 64907c478bd9Sstevel@tonic-gate } else { 64917c478bd9Sstevel@tonic-gate if (compare_field(addr, selector->val, 64927c478bd9Sstevel@tonic-gate selector->arg) == DEVFSADM_FAILURE) { 64937c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 64947c478bd9Sstevel@tonic-gate } 64957c478bd9Sstevel@tonic-gate } 64967c478bd9Sstevel@tonic-gate break; 64977c478bd9Sstevel@tonic-gate case MINOR: 64987c478bd9Sstevel@tonic-gate if ((minor_name = di_minor_name(minor)) == NULL) { 64997c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 65007c478bd9Sstevel@tonic-gate } 65017c478bd9Sstevel@tonic-gate if (selector->arg == 0) { 65027c478bd9Sstevel@tonic-gate if (strcmp(minor_name, selector->val) != 0) { 65037c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 65047c478bd9Sstevel@tonic-gate } 65057c478bd9Sstevel@tonic-gate } else { 65067c478bd9Sstevel@tonic-gate if (compare_field(minor_name, selector->val, 65077c478bd9Sstevel@tonic-gate selector->arg) == DEVFSADM_FAILURE) { 65087c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 65097c478bd9Sstevel@tonic-gate } 65107c478bd9Sstevel@tonic-gate } 65117c478bd9Sstevel@tonic-gate break; 65127c478bd9Sstevel@tonic-gate default: 65137c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 65147c478bd9Sstevel@tonic-gate } 65157c478bd9Sstevel@tonic-gate } 65167c478bd9Sstevel@tonic-gate 65177c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 65187c478bd9Sstevel@tonic-gate } 65197c478bd9Sstevel@tonic-gate 65207c478bd9Sstevel@tonic-gate /* 65217c478bd9Sstevel@tonic-gate * For the given minor node and devlinktab_list entry from devlink.tab, 65227c478bd9Sstevel@tonic-gate * build a logical dev link and a possible extra devlink. 65237c478bd9Sstevel@tonic-gate * Return DEVFSADM_SUCCESS if link is created, otherwise DEVFSADM_FAILURE. 65247c478bd9Sstevel@tonic-gate */ 65257c478bd9Sstevel@tonic-gate static int 65267c478bd9Sstevel@tonic-gate build_links(devlinktab_list_t *entry, di_minor_t minor, di_node_t node) 65277c478bd9Sstevel@tonic-gate { 65287c478bd9Sstevel@tonic-gate char secondary_link[PATH_MAX + 1]; 65297c478bd9Sstevel@tonic-gate char primary_link[PATH_MAX + 1]; 65307c478bd9Sstevel@tonic-gate char contents[PATH_MAX + 1]; 65317c478bd9Sstevel@tonic-gate char *dev_path; 65327c478bd9Sstevel@tonic-gate 65337c478bd9Sstevel@tonic-gate if ((dev_path = di_devfs_path(node)) == NULL) { 65347c478bd9Sstevel@tonic-gate err_print(DI_DEVFS_PATH_FAILED, strerror(errno)); 65357c478bd9Sstevel@tonic-gate devfsadm_exit(1); 65367c478bd9Sstevel@tonic-gate } 65377c478bd9Sstevel@tonic-gate (void) strcpy(contents, dev_path); 65387c478bd9Sstevel@tonic-gate di_devfs_path_free(dev_path); 65397c478bd9Sstevel@tonic-gate 65407c478bd9Sstevel@tonic-gate (void) strcat(contents, ":"); 65417c478bd9Sstevel@tonic-gate (void) strcat(contents, di_minor_name(minor)); 65427c478bd9Sstevel@tonic-gate 65437c478bd9Sstevel@tonic-gate if (construct_devlink(primary_link, entry->p_link, contents, 65447c478bd9Sstevel@tonic-gate minor, node, 65457c478bd9Sstevel@tonic-gate entry->p_link_pattern) == DEVFSADM_FAILURE) { 65467c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 65477c478bd9Sstevel@tonic-gate } 65487c478bd9Sstevel@tonic-gate (void) devfsadm_mklink(primary_link, node, minor, 0); 65497c478bd9Sstevel@tonic-gate 65507c478bd9Sstevel@tonic-gate if (entry->s_link == NULL) { 65517c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 65527c478bd9Sstevel@tonic-gate } 65537c478bd9Sstevel@tonic-gate 65547c478bd9Sstevel@tonic-gate if (construct_devlink(secondary_link, entry->s_link, 65557c478bd9Sstevel@tonic-gate primary_link, minor, node, 65567c478bd9Sstevel@tonic-gate entry->s_link_pattern) == DEVFSADM_FAILURE) { 65577c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 65587c478bd9Sstevel@tonic-gate } 65597c478bd9Sstevel@tonic-gate 65607c478bd9Sstevel@tonic-gate (void) devfsadm_secondary_link(secondary_link, primary_link, 0); 65617c478bd9Sstevel@tonic-gate 65627c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 65637c478bd9Sstevel@tonic-gate } 65647c478bd9Sstevel@tonic-gate 65657c478bd9Sstevel@tonic-gate /* 65667c478bd9Sstevel@tonic-gate * The counter rule for devlink.tab entries is implemented via 65677c478bd9Sstevel@tonic-gate * devfsadm_enumerate_int_start(). One of the arguments to this function 65687c478bd9Sstevel@tonic-gate * is a path, where each path component is treated as a regular expression. 65697c478bd9Sstevel@tonic-gate * For devlink.tab entries, this path regular expression is derived from 65707c478bd9Sstevel@tonic-gate * the devlink spec. get_anchored_re() accepts path regular expressions derived 65717c478bd9Sstevel@tonic-gate * from devlink.tab entries and inserts the anchors '^' and '$' at the beginning 65727c478bd9Sstevel@tonic-gate * and end respectively of each path component. This is done to prevent 65737c478bd9Sstevel@tonic-gate * false matches. For example, without anchors, "a/([0-9]+)" will match "ab/c9" 65747c478bd9Sstevel@tonic-gate * and incorrect links will be generated. 65757c478bd9Sstevel@tonic-gate */ 65767c478bd9Sstevel@tonic-gate static int 65777c478bd9Sstevel@tonic-gate get_anchored_re(char *link, char *anchored_re, char *pattern) 65787c478bd9Sstevel@tonic-gate { 65797c478bd9Sstevel@tonic-gate if (*link == '/' || *link == '\0') { 65807c478bd9Sstevel@tonic-gate err_print(INVALID_DEVLINK_SPEC, pattern); 65817c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 65827c478bd9Sstevel@tonic-gate } 65837c478bd9Sstevel@tonic-gate 65847c478bd9Sstevel@tonic-gate *anchored_re++ = '^'; 65857c478bd9Sstevel@tonic-gate for (; *link != '\0'; ) { 65867c478bd9Sstevel@tonic-gate if (*link == '/') { 65877c478bd9Sstevel@tonic-gate while (*link == '/') 65887c478bd9Sstevel@tonic-gate link++; 65897c478bd9Sstevel@tonic-gate *anchored_re++ = '$'; 65907c478bd9Sstevel@tonic-gate *anchored_re++ = '/'; 65917c478bd9Sstevel@tonic-gate if (*link != '\0') { 65927c478bd9Sstevel@tonic-gate *anchored_re++ = '^'; 65937c478bd9Sstevel@tonic-gate } 65947c478bd9Sstevel@tonic-gate } else { 65957c478bd9Sstevel@tonic-gate *anchored_re++ = *link++; 65967c478bd9Sstevel@tonic-gate if (*link == '\0') { 65977c478bd9Sstevel@tonic-gate *anchored_re++ = '$'; 65987c478bd9Sstevel@tonic-gate } 65997c478bd9Sstevel@tonic-gate } 66007c478bd9Sstevel@tonic-gate } 66017c478bd9Sstevel@tonic-gate *anchored_re = '\0'; 66027c478bd9Sstevel@tonic-gate 66037c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 66047c478bd9Sstevel@tonic-gate } 66057c478bd9Sstevel@tonic-gate 66067c478bd9Sstevel@tonic-gate static int 66077c478bd9Sstevel@tonic-gate construct_devlink(char *link, link_list_t *link_build, char *contents, 66087c478bd9Sstevel@tonic-gate di_minor_t minor, di_node_t node, char *pattern) 66097c478bd9Sstevel@tonic-gate { 66107c478bd9Sstevel@tonic-gate int counter_offset = -1; 66117c478bd9Sstevel@tonic-gate devfsadm_enumerate_t rules[1] = {NULL}; 66127c478bd9Sstevel@tonic-gate char templink[PATH_MAX + 1]; 66137c478bd9Sstevel@tonic-gate char *buff; 66147c478bd9Sstevel@tonic-gate char start[10]; 66157c478bd9Sstevel@tonic-gate char *node_path; 66167c478bd9Sstevel@tonic-gate char anchored_re[PATH_MAX + 1]; 66177c478bd9Sstevel@tonic-gate 66187c478bd9Sstevel@tonic-gate link[0] = '\0'; 66197c478bd9Sstevel@tonic-gate 66207c478bd9Sstevel@tonic-gate for (; link_build != NULL; link_build = link_build->next) { 66217c478bd9Sstevel@tonic-gate switch (link_build->type) { 66227c478bd9Sstevel@tonic-gate case NAME: 66237c478bd9Sstevel@tonic-gate (void) strcat(link, di_node_name(node)); 66247c478bd9Sstevel@tonic-gate break; 66257c478bd9Sstevel@tonic-gate case CONSTANT: 66267c478bd9Sstevel@tonic-gate (void) strcat(link, link_build->constant); 66277c478bd9Sstevel@tonic-gate break; 66287c478bd9Sstevel@tonic-gate case ADDR: 66297c478bd9Sstevel@tonic-gate if (component_cat(link, di_bus_addr(node), 66307c478bd9Sstevel@tonic-gate link_build->arg) == DEVFSADM_FAILURE) { 66317c478bd9Sstevel@tonic-gate node_path = di_devfs_path(node); 66327c478bd9Sstevel@tonic-gate err_print(CANNOT_BE_USED, pattern, node_path, 66337c478bd9Sstevel@tonic-gate di_minor_name(minor)); 66347c478bd9Sstevel@tonic-gate di_devfs_path_free(node_path); 66357c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 66367c478bd9Sstevel@tonic-gate } 66377c478bd9Sstevel@tonic-gate break; 66387c478bd9Sstevel@tonic-gate case MINOR: 66397c478bd9Sstevel@tonic-gate if (component_cat(link, di_minor_name(minor), 66407c478bd9Sstevel@tonic-gate link_build->arg) == DEVFSADM_FAILURE) { 66417c478bd9Sstevel@tonic-gate node_path = di_devfs_path(node); 66427c478bd9Sstevel@tonic-gate err_print(CANNOT_BE_USED, pattern, node_path, 66437c478bd9Sstevel@tonic-gate di_minor_name(minor)); 66447c478bd9Sstevel@tonic-gate di_devfs_path_free(node_path); 66457c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 66467c478bd9Sstevel@tonic-gate } 66477c478bd9Sstevel@tonic-gate break; 66487c478bd9Sstevel@tonic-gate case COUNTER: 66497c478bd9Sstevel@tonic-gate counter_offset = strlen(link); 66507c478bd9Sstevel@tonic-gate (void) strcat(link, "([0-9]+)"); 66517c478bd9Sstevel@tonic-gate (void) sprintf(start, "%d", link_build->arg); 66527c478bd9Sstevel@tonic-gate break; 66537c478bd9Sstevel@tonic-gate default: 66547c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 66557c478bd9Sstevel@tonic-gate } 66567c478bd9Sstevel@tonic-gate } 66577c478bd9Sstevel@tonic-gate 66587c478bd9Sstevel@tonic-gate if (counter_offset != -1) { 66597c478bd9Sstevel@tonic-gate /* 66607c478bd9Sstevel@tonic-gate * copy anything appended after "([0-9]+)" into 66617c478bd9Sstevel@tonic-gate * templink 66627c478bd9Sstevel@tonic-gate */ 66637c478bd9Sstevel@tonic-gate 66647c478bd9Sstevel@tonic-gate (void) strcpy(templink, 66657c478bd9Sstevel@tonic-gate &link[counter_offset + strlen("([0-9]+)")]); 66667c478bd9Sstevel@tonic-gate if (get_anchored_re(link, anchored_re, pattern) 66677c478bd9Sstevel@tonic-gate != DEVFSADM_SUCCESS) { 66687c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 66697c478bd9Sstevel@tonic-gate } 66707c478bd9Sstevel@tonic-gate rules[0].re = anchored_re; 66717c478bd9Sstevel@tonic-gate rules[0].subexp = 1; 66727c478bd9Sstevel@tonic-gate rules[0].flags = MATCH_ALL; 66737c478bd9Sstevel@tonic-gate if (devfsadm_enumerate_int_start(contents, 0, &buff, 66747c478bd9Sstevel@tonic-gate rules, 1, start) == DEVFSADM_FAILURE) { 66757c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 66767c478bd9Sstevel@tonic-gate } 66777c478bd9Sstevel@tonic-gate (void) strcpy(&link[counter_offset], buff); 66787c478bd9Sstevel@tonic-gate free(buff); 66797c478bd9Sstevel@tonic-gate (void) strcat(link, templink); 66807c478bd9Sstevel@tonic-gate vprint(DEVLINK_MID, "COUNTER is %s\n", link); 66817c478bd9Sstevel@tonic-gate } 66827c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 66837c478bd9Sstevel@tonic-gate } 66847c478bd9Sstevel@tonic-gate 66857c478bd9Sstevel@tonic-gate /* 66867c478bd9Sstevel@tonic-gate * Compares "field" number of the comma separated list "full_name" with 66877c478bd9Sstevel@tonic-gate * field_item. Returns DEVFSADM_SUCCESS for match, 66887c478bd9Sstevel@tonic-gate * DEVFSADM_FAILURE for no match. 66897c478bd9Sstevel@tonic-gate */ 66907c478bd9Sstevel@tonic-gate static int 66917c478bd9Sstevel@tonic-gate compare_field(char *full_name, char *field_item, int field) 66927c478bd9Sstevel@tonic-gate { 66937c478bd9Sstevel@tonic-gate --field; 66947c478bd9Sstevel@tonic-gate while ((*full_name != '\0') && (field != 0)) { 66957c478bd9Sstevel@tonic-gate if (*(full_name++) == ',') { 66967c478bd9Sstevel@tonic-gate field--; 66977c478bd9Sstevel@tonic-gate } 66987c478bd9Sstevel@tonic-gate } 66997c478bd9Sstevel@tonic-gate 67007c478bd9Sstevel@tonic-gate if (field != 0) { 67017c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 67027c478bd9Sstevel@tonic-gate } 67037c478bd9Sstevel@tonic-gate 67047c478bd9Sstevel@tonic-gate while ((*full_name != '\0') && (*field_item != '\0') && 67057c478bd9Sstevel@tonic-gate (*full_name != ',')) { 67067c478bd9Sstevel@tonic-gate if (*(full_name++) != *(field_item++)) { 67077c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 67087c478bd9Sstevel@tonic-gate } 67097c478bd9Sstevel@tonic-gate } 67107c478bd9Sstevel@tonic-gate 67117c478bd9Sstevel@tonic-gate if (*field_item != '\0') { 67127c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 67137c478bd9Sstevel@tonic-gate } 67147c478bd9Sstevel@tonic-gate 67157c478bd9Sstevel@tonic-gate if ((*full_name == '\0') || (*full_name == ',')) 67167c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 67177c478bd9Sstevel@tonic-gate 67187c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 67197c478bd9Sstevel@tonic-gate } 67207c478bd9Sstevel@tonic-gate 67217c478bd9Sstevel@tonic-gate /* 67227c478bd9Sstevel@tonic-gate * strcat() field # "field" of comma separated list "name" to "link". 67237c478bd9Sstevel@tonic-gate * Field 0 is the entire name. 67247c478bd9Sstevel@tonic-gate * Return DEVFSADM_SUCCESS or DEVFSADM_FAILURE. 67257c478bd9Sstevel@tonic-gate */ 67267c478bd9Sstevel@tonic-gate static int 67277c478bd9Sstevel@tonic-gate component_cat(char *link, char *name, int field) 67287c478bd9Sstevel@tonic-gate { 67297c478bd9Sstevel@tonic-gate 67307c478bd9Sstevel@tonic-gate if (name == NULL) { 67317c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 67327c478bd9Sstevel@tonic-gate } 67337c478bd9Sstevel@tonic-gate 67347c478bd9Sstevel@tonic-gate if (field == 0) { 67357c478bd9Sstevel@tonic-gate (void) strcat(link, name); 67367c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 67377c478bd9Sstevel@tonic-gate } 67387c478bd9Sstevel@tonic-gate 67397c478bd9Sstevel@tonic-gate while (*link != '\0') { 67407c478bd9Sstevel@tonic-gate link++; 67417c478bd9Sstevel@tonic-gate } 67427c478bd9Sstevel@tonic-gate 67437c478bd9Sstevel@tonic-gate --field; 67447c478bd9Sstevel@tonic-gate while ((*name != '\0') && (field != 0)) { 67457c478bd9Sstevel@tonic-gate if (*(name++) == ',') { 67467c478bd9Sstevel@tonic-gate --field; 67477c478bd9Sstevel@tonic-gate } 67487c478bd9Sstevel@tonic-gate } 67497c478bd9Sstevel@tonic-gate 67507c478bd9Sstevel@tonic-gate if (field != 0) { 67517c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 67527c478bd9Sstevel@tonic-gate } 67537c478bd9Sstevel@tonic-gate 67547c478bd9Sstevel@tonic-gate while ((*name != '\0') && (*name != ',')) { 67557c478bd9Sstevel@tonic-gate *(link++) = *(name++); 67567c478bd9Sstevel@tonic-gate } 67577c478bd9Sstevel@tonic-gate 67587c478bd9Sstevel@tonic-gate *link = '\0'; 67597c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 67607c478bd9Sstevel@tonic-gate } 67617c478bd9Sstevel@tonic-gate 67627c478bd9Sstevel@tonic-gate static void 67637c478bd9Sstevel@tonic-gate free_selector_list(selector_list_t *head) 67647c478bd9Sstevel@tonic-gate { 67657c478bd9Sstevel@tonic-gate selector_list_t *temp; 67667c478bd9Sstevel@tonic-gate 67677c478bd9Sstevel@tonic-gate while (head != NULL) { 67687c478bd9Sstevel@tonic-gate temp = head; 67697c478bd9Sstevel@tonic-gate head = head->next; 67707c478bd9Sstevel@tonic-gate free(temp->val); 67717c478bd9Sstevel@tonic-gate free(temp); 67727c478bd9Sstevel@tonic-gate } 67737c478bd9Sstevel@tonic-gate } 67747c478bd9Sstevel@tonic-gate 67757c478bd9Sstevel@tonic-gate static void 67767c478bd9Sstevel@tonic-gate free_link_list(link_list_t *head) 67777c478bd9Sstevel@tonic-gate { 67787c478bd9Sstevel@tonic-gate link_list_t *temp; 67797c478bd9Sstevel@tonic-gate 67807c478bd9Sstevel@tonic-gate while (head != NULL) { 67817c478bd9Sstevel@tonic-gate temp = head; 67827c478bd9Sstevel@tonic-gate head = head->next; 67837c478bd9Sstevel@tonic-gate if (temp->type == CONSTANT) { 67847c478bd9Sstevel@tonic-gate free(temp->constant); 67857c478bd9Sstevel@tonic-gate } 67867c478bd9Sstevel@tonic-gate free(temp); 67877c478bd9Sstevel@tonic-gate } 67887c478bd9Sstevel@tonic-gate } 67897c478bd9Sstevel@tonic-gate 67907c478bd9Sstevel@tonic-gate /* 67917c478bd9Sstevel@tonic-gate * Prints only if level matches one of the debug levels 67927c478bd9Sstevel@tonic-gate * given on command line. INFO_MID is always printed. 67937c478bd9Sstevel@tonic-gate * 67947c478bd9Sstevel@tonic-gate * See devfsadm.h for a listing of globally defined levels and 67957c478bd9Sstevel@tonic-gate * meanings. Modules should prefix the level with their 67967c478bd9Sstevel@tonic-gate * module name to prevent collisions. 67977c478bd9Sstevel@tonic-gate */ 67987c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 67997c478bd9Sstevel@tonic-gate void 68007c478bd9Sstevel@tonic-gate devfsadm_print(char *msgid, char *message, ...) 68017c478bd9Sstevel@tonic-gate { 68027c478bd9Sstevel@tonic-gate va_list ap; 68037c478bd9Sstevel@tonic-gate static int newline = TRUE; 68047c478bd9Sstevel@tonic-gate int x; 68057c478bd9Sstevel@tonic-gate 68067c478bd9Sstevel@tonic-gate if (msgid != NULL) { 68077c478bd9Sstevel@tonic-gate for (x = 0; x < num_verbose; x++) { 68087c478bd9Sstevel@tonic-gate if (strcmp(verbose[x], msgid) == 0) { 68097c478bd9Sstevel@tonic-gate break; 68107c478bd9Sstevel@tonic-gate } 68117c478bd9Sstevel@tonic-gate if (strcmp(verbose[x], ALL_MID) == 0) { 68127c478bd9Sstevel@tonic-gate break; 68137c478bd9Sstevel@tonic-gate } 68147c478bd9Sstevel@tonic-gate } 68157c478bd9Sstevel@tonic-gate if (x == num_verbose) { 68167c478bd9Sstevel@tonic-gate return; 68177c478bd9Sstevel@tonic-gate } 68187c478bd9Sstevel@tonic-gate } 68197c478bd9Sstevel@tonic-gate 68207c478bd9Sstevel@tonic-gate va_start(ap, message); 68217c478bd9Sstevel@tonic-gate 68227c478bd9Sstevel@tonic-gate if (msgid == NULL) { 68237c478bd9Sstevel@tonic-gate if (logflag == TRUE) { 68247c478bd9Sstevel@tonic-gate (void) vsyslog(LOG_NOTICE, message, ap); 68257c478bd9Sstevel@tonic-gate } else { 68267c478bd9Sstevel@tonic-gate (void) vfprintf(stdout, message, ap); 68277c478bd9Sstevel@tonic-gate } 68287c478bd9Sstevel@tonic-gate 68297c478bd9Sstevel@tonic-gate } else { 68307c478bd9Sstevel@tonic-gate if (logflag == TRUE) { 68317c478bd9Sstevel@tonic-gate (void) syslog(LOG_DEBUG, "%s[%ld]: %s: ", 68327c478bd9Sstevel@tonic-gate prog, getpid(), msgid); 68337c478bd9Sstevel@tonic-gate (void) vsyslog(LOG_DEBUG, message, ap); 68347c478bd9Sstevel@tonic-gate } else { 68357c478bd9Sstevel@tonic-gate if (newline == TRUE) { 68367c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "%s[%ld]: %s: ", 68377c478bd9Sstevel@tonic-gate prog, getpid(), msgid); 68387c478bd9Sstevel@tonic-gate } 68397c478bd9Sstevel@tonic-gate (void) vfprintf(stdout, message, ap); 68407c478bd9Sstevel@tonic-gate } 68417c478bd9Sstevel@tonic-gate } 68427c478bd9Sstevel@tonic-gate 68437c478bd9Sstevel@tonic-gate if (message[strlen(message) - 1] == '\n') { 68447c478bd9Sstevel@tonic-gate newline = TRUE; 68457c478bd9Sstevel@tonic-gate } else { 68467c478bd9Sstevel@tonic-gate newline = FALSE; 68477c478bd9Sstevel@tonic-gate } 68487c478bd9Sstevel@tonic-gate va_end(ap); 68497c478bd9Sstevel@tonic-gate } 68507c478bd9Sstevel@tonic-gate 68517c478bd9Sstevel@tonic-gate /* 68527c478bd9Sstevel@tonic-gate * print error messages to the terminal or to syslog 68537c478bd9Sstevel@tonic-gate */ 68547c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 68557c478bd9Sstevel@tonic-gate void 68567c478bd9Sstevel@tonic-gate devfsadm_errprint(char *message, ...) 68577c478bd9Sstevel@tonic-gate { 68587c478bd9Sstevel@tonic-gate va_list ap; 68597c478bd9Sstevel@tonic-gate 68607c478bd9Sstevel@tonic-gate va_start(ap, message); 68617c478bd9Sstevel@tonic-gate 68627c478bd9Sstevel@tonic-gate if (logflag == TRUE) { 68637c478bd9Sstevel@tonic-gate (void) vsyslog(LOG_ERR, message, ap); 68647c478bd9Sstevel@tonic-gate } else { 68657c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", prog); 68667c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, message, ap); 68677c478bd9Sstevel@tonic-gate } 68687c478bd9Sstevel@tonic-gate va_end(ap); 68697c478bd9Sstevel@tonic-gate } 68707c478bd9Sstevel@tonic-gate 68717c478bd9Sstevel@tonic-gate /* 68727c478bd9Sstevel@tonic-gate * return noupdate state (-s) 68737c478bd9Sstevel@tonic-gate */ 68747c478bd9Sstevel@tonic-gate int 68757c478bd9Sstevel@tonic-gate devfsadm_noupdate(void) 68767c478bd9Sstevel@tonic-gate { 68777c478bd9Sstevel@tonic-gate return (file_mods == TRUE ? DEVFSADM_TRUE : DEVFSADM_FALSE); 68787c478bd9Sstevel@tonic-gate } 68797c478bd9Sstevel@tonic-gate 68807c478bd9Sstevel@tonic-gate /* 68817c478bd9Sstevel@tonic-gate * return current root update path (-r) 68827c478bd9Sstevel@tonic-gate */ 68837c478bd9Sstevel@tonic-gate const char * 68847c478bd9Sstevel@tonic-gate devfsadm_root_path(void) 68857c478bd9Sstevel@tonic-gate { 68867c478bd9Sstevel@tonic-gate if (root_dir[0] == '\0') { 68877c478bd9Sstevel@tonic-gate return ("/"); 68887c478bd9Sstevel@tonic-gate } else { 68897c478bd9Sstevel@tonic-gate return ((const char *)root_dir); 68907c478bd9Sstevel@tonic-gate } 68917c478bd9Sstevel@tonic-gate } 68927c478bd9Sstevel@tonic-gate 68933c4226f9Spjha void 68943c4226f9Spjha devfsadm_free_dev_names(char **dev_names, int len) 68953c4226f9Spjha { 68963c4226f9Spjha int i; 68973c4226f9Spjha 68983c4226f9Spjha for (i = 0; i < len; i++) 68993c4226f9Spjha free(dev_names[i]); 69003c4226f9Spjha free(dev_names); 69013c4226f9Spjha } 69023c4226f9Spjha 69033c4226f9Spjha /* 69043c4226f9Spjha * Return all devlinks corresponding to phys_path as an array of strings. 69053c4226f9Spjha * The number of entries in the array is returned through lenp. 69063c4226f9Spjha * devfsadm_free_dev_names() is used to free the returned array. 69073c4226f9Spjha * NULL is returned on failure or when there are no matching devlinks. 69083c4226f9Spjha * 69093c4226f9Spjha * re is an extended regular expression in regex(5) format used to further 69103c4226f9Spjha * match devlinks pointing to phys_path; it may be NULL to match all 69113c4226f9Spjha */ 69123c4226f9Spjha char ** 69133c4226f9Spjha devfsadm_lookup_dev_names(char *phys_path, char *re, int *lenp) 69143c4226f9Spjha { 69153c4226f9Spjha struct devlink_cb_arg cb_arg; 69163c4226f9Spjha char **dev_names = NULL; 69173c4226f9Spjha int i; 69183c4226f9Spjha 69193c4226f9Spjha *lenp = 0; 69203c4226f9Spjha cb_arg.count = 0; 69213c4226f9Spjha cb_arg.rv = 0; 69223c4226f9Spjha (void) di_devlink_cache_walk(devlink_cache, re, phys_path, 69233c4226f9Spjha DI_PRIMARY_LINK, &cb_arg, devlink_cb); 69243c4226f9Spjha 69253c4226f9Spjha if (cb_arg.rv == -1 || cb_arg.count <= 0) 69263c4226f9Spjha return (NULL); 69273c4226f9Spjha 69283c4226f9Spjha dev_names = s_malloc(cb_arg.count * sizeof (char *)); 69293c4226f9Spjha if (dev_names == NULL) 69303c4226f9Spjha goto out; 69313c4226f9Spjha 69323c4226f9Spjha for (i = 0; i < cb_arg.count; i++) { 69333c4226f9Spjha dev_names[i] = s_strdup(cb_arg.dev_names[i]); 69343c4226f9Spjha if (dev_names[i] == NULL) { 69353c4226f9Spjha devfsadm_free_dev_names(dev_names, i); 69363c4226f9Spjha dev_names = NULL; 69373c4226f9Spjha goto out; 69383c4226f9Spjha } 69393c4226f9Spjha } 69403c4226f9Spjha *lenp = cb_arg.count; 69413c4226f9Spjha 69423c4226f9Spjha out: 69433c4226f9Spjha free_dev_names(&cb_arg); 69443c4226f9Spjha return (dev_names); 69453c4226f9Spjha } 69463c4226f9Spjha 69477c478bd9Sstevel@tonic-gate /* common exit function which ensures releasing locks */ 69487c478bd9Sstevel@tonic-gate static void 69497c478bd9Sstevel@tonic-gate devfsadm_exit(int status) 69507c478bd9Sstevel@tonic-gate { 69517c478bd9Sstevel@tonic-gate if (DEVFSADM_DEBUG_ON) { 69527c478bd9Sstevel@tonic-gate vprint(INFO_MID, "exit status = %d\n", status); 69537c478bd9Sstevel@tonic-gate } 69547c478bd9Sstevel@tonic-gate 69557c478bd9Sstevel@tonic-gate if (rcm_hdl) { 69567c478bd9Sstevel@tonic-gate if (thr_self() != process_rcm_events_tid) { 69577c478bd9Sstevel@tonic-gate (void) mutex_lock(&rcm_eventq_lock); 69587c478bd9Sstevel@tonic-gate need_to_exit_rcm_event_thread = 1; 69597c478bd9Sstevel@tonic-gate (void) cond_broadcast(&rcm_eventq_cv); 69607c478bd9Sstevel@tonic-gate (void) mutex_unlock(&rcm_eventq_lock); 69617c478bd9Sstevel@tonic-gate 69627c478bd9Sstevel@tonic-gate /* wait until process_rcm_events() thread exits */ 69637c478bd9Sstevel@tonic-gate (void) thr_join(process_rcm_events_tid, NULL, NULL); 69647c478bd9Sstevel@tonic-gate } 69657c478bd9Sstevel@tonic-gate librcm_free_handle(rcm_hdl); 69667c478bd9Sstevel@tonic-gate (void) dlclose(librcm_hdl); 69677c478bd9Sstevel@tonic-gate } 69687c478bd9Sstevel@tonic-gate 69697c478bd9Sstevel@tonic-gate exit_dev_lock(); 69707c478bd9Sstevel@tonic-gate exit_daemon_lock(); 69717c478bd9Sstevel@tonic-gate 69727c478bd9Sstevel@tonic-gate if (logflag == TRUE) { 69737c478bd9Sstevel@tonic-gate closelog(); 69747c478bd9Sstevel@tonic-gate } 69757c478bd9Sstevel@tonic-gate 69767c478bd9Sstevel@tonic-gate exit(status); 69777c478bd9Sstevel@tonic-gate } 69787c478bd9Sstevel@tonic-gate 69797c478bd9Sstevel@tonic-gate /* 6980facf4a8dSllai1 * set root_dir, devices_dir, dev_dir using optarg. 69817c478bd9Sstevel@tonic-gate */ 69827c478bd9Sstevel@tonic-gate static void 6983facf4a8dSllai1 set_root_devices_dev_dir(char *dir) 69847c478bd9Sstevel@tonic-gate { 69857c478bd9Sstevel@tonic-gate size_t len; 69867c478bd9Sstevel@tonic-gate 69877c478bd9Sstevel@tonic-gate root_dir = s_strdup(dir); 69887c478bd9Sstevel@tonic-gate len = strlen(dir) + strlen(DEVICES) + 1; 69897c478bd9Sstevel@tonic-gate devices_dir = s_malloc(len); 69907c478bd9Sstevel@tonic-gate (void) snprintf(devices_dir, len, "%s%s", root_dir, DEVICES); 69917c478bd9Sstevel@tonic-gate len = strlen(root_dir) + strlen(DEV) + 1; 69927c478bd9Sstevel@tonic-gate dev_dir = s_malloc(len); 69937c478bd9Sstevel@tonic-gate (void) snprintf(dev_dir, len, "%s%s", root_dir, DEV); 69947c478bd9Sstevel@tonic-gate } 69957c478bd9Sstevel@tonic-gate 69967c478bd9Sstevel@tonic-gate /* 69977c478bd9Sstevel@tonic-gate * Removes quotes. 69987c478bd9Sstevel@tonic-gate */ 69997c478bd9Sstevel@tonic-gate static char * 70007c478bd9Sstevel@tonic-gate dequote(char *src) 70017c478bd9Sstevel@tonic-gate { 70027c478bd9Sstevel@tonic-gate char *dst; 70037c478bd9Sstevel@tonic-gate int len; 70047c478bd9Sstevel@tonic-gate 70057c478bd9Sstevel@tonic-gate len = strlen(src); 70067c478bd9Sstevel@tonic-gate dst = s_malloc(len + 1); 70077c478bd9Sstevel@tonic-gate if (src[0] == '\"' && src[len - 1] == '\"') { 70087c478bd9Sstevel@tonic-gate len -= 2; 70097c478bd9Sstevel@tonic-gate (void) strncpy(dst, &src[1], len); 70107c478bd9Sstevel@tonic-gate dst[len] = '\0'; 70117c478bd9Sstevel@tonic-gate } else { 70127c478bd9Sstevel@tonic-gate (void) strcpy(dst, src); 70137c478bd9Sstevel@tonic-gate } 70147c478bd9Sstevel@tonic-gate return (dst); 70157c478bd9Sstevel@tonic-gate } 70167c478bd9Sstevel@tonic-gate 70177c478bd9Sstevel@tonic-gate /* 70187c478bd9Sstevel@tonic-gate * For a given physical device pathname and spectype, return the 70197c478bd9Sstevel@tonic-gate * ownership and permissions attributes by looking in data from 70207c478bd9Sstevel@tonic-gate * /etc/minor_perm. If currently in installation mode, check for 70217c478bd9Sstevel@tonic-gate * possible major number translations from the miniroot to the installed 70227c478bd9Sstevel@tonic-gate * root's name_to_major table. Note that there can be multiple matches, 70237c478bd9Sstevel@tonic-gate * but the last match takes effect. pts seems to rely on this 70247c478bd9Sstevel@tonic-gate * implementation behavior. 70257c478bd9Sstevel@tonic-gate */ 70267c478bd9Sstevel@tonic-gate static void 70277c478bd9Sstevel@tonic-gate getattr(char *phy_path, char *aminor, int spectype, dev_t dev, mode_t *mode, 70287c478bd9Sstevel@tonic-gate uid_t *uid, gid_t *gid) 70297c478bd9Sstevel@tonic-gate { 70307c478bd9Sstevel@tonic-gate char devname[PATH_MAX + 1]; 70317c478bd9Sstevel@tonic-gate char *node_name; 70327c478bd9Sstevel@tonic-gate char *minor_name; 70337c478bd9Sstevel@tonic-gate int match = FALSE; 70347c478bd9Sstevel@tonic-gate int is_clone; 70357c478bd9Sstevel@tonic-gate int mp_drvname_matches_node_name; 70367c478bd9Sstevel@tonic-gate int mp_drvname_matches_minor_name; 70377c478bd9Sstevel@tonic-gate int mp_drvname_is_clone; 70387c478bd9Sstevel@tonic-gate int mp_drvname_matches_drvname; 70397c478bd9Sstevel@tonic-gate struct mperm *mp; 70407c478bd9Sstevel@tonic-gate major_t major_no; 70417c478bd9Sstevel@tonic-gate char driver[PATH_MAX + 1]; 70427c478bd9Sstevel@tonic-gate 70437c478bd9Sstevel@tonic-gate /* 70447c478bd9Sstevel@tonic-gate * Get the driver name based on the major number since the name 70457c478bd9Sstevel@tonic-gate * in /devices may be generic. Could be running with more major 70467c478bd9Sstevel@tonic-gate * numbers than are in /etc/name_to_major, so get it from the kernel 70477c478bd9Sstevel@tonic-gate */ 70487c478bd9Sstevel@tonic-gate major_no = major(dev); 70497c478bd9Sstevel@tonic-gate 70507c478bd9Sstevel@tonic-gate if (modctl(MODGETNAME, driver, sizeof (driver), &major_no) != 0) { 70517c478bd9Sstevel@tonic-gate /* return default values */ 70527c478bd9Sstevel@tonic-gate goto use_defaults; 70537c478bd9Sstevel@tonic-gate } 70547c478bd9Sstevel@tonic-gate 70557c478bd9Sstevel@tonic-gate (void) strcpy(devname, phy_path); 70567c478bd9Sstevel@tonic-gate 70577c478bd9Sstevel@tonic-gate node_name = strrchr(devname, '/'); /* node name is the last */ 70587c478bd9Sstevel@tonic-gate /* component */ 70597c478bd9Sstevel@tonic-gate if (node_name == NULL) { 70607c478bd9Sstevel@tonic-gate err_print(NO_NODE, devname); 70617c478bd9Sstevel@tonic-gate goto use_defaults; 70627c478bd9Sstevel@tonic-gate } 70637c478bd9Sstevel@tonic-gate 70647c478bd9Sstevel@tonic-gate minor_name = strchr(++node_name, '@'); /* see if it has address part */ 70657c478bd9Sstevel@tonic-gate 70667c478bd9Sstevel@tonic-gate if (minor_name != NULL) { 70677c478bd9Sstevel@tonic-gate *minor_name++ = '\0'; 70687c478bd9Sstevel@tonic-gate } else { 70697c478bd9Sstevel@tonic-gate minor_name = node_name; 70707c478bd9Sstevel@tonic-gate } 70717c478bd9Sstevel@tonic-gate 70727c478bd9Sstevel@tonic-gate minor_name = strchr(minor_name, ':'); /* look for minor name */ 70737c478bd9Sstevel@tonic-gate 70747c478bd9Sstevel@tonic-gate if (minor_name == NULL) { 70757c478bd9Sstevel@tonic-gate err_print(NO_MINOR, devname); 70767c478bd9Sstevel@tonic-gate goto use_defaults; 70777c478bd9Sstevel@tonic-gate } 70787c478bd9Sstevel@tonic-gate *minor_name++ = '\0'; 70797c478bd9Sstevel@tonic-gate 70807c478bd9Sstevel@tonic-gate /* 70817c478bd9Sstevel@tonic-gate * mp->mp_drvname = device name from minor_perm 70827c478bd9Sstevel@tonic-gate * mp->mp_minorname = minor part of device name from 70837c478bd9Sstevel@tonic-gate * minor_perm 70847c478bd9Sstevel@tonic-gate * drvname = name of driver for this device 70857c478bd9Sstevel@tonic-gate */ 70867c478bd9Sstevel@tonic-gate 70877c478bd9Sstevel@tonic-gate is_clone = (strcmp(node_name, "clone") == 0 ? TRUE : FALSE); 70887c478bd9Sstevel@tonic-gate for (mp = minor_perms; mp != NULL; mp = mp->mp_next) { 70897c478bd9Sstevel@tonic-gate mp_drvname_matches_node_name = 70907c478bd9Sstevel@tonic-gate (strcmp(mp->mp_drvname, node_name) == 0 ? TRUE : FALSE); 70917c478bd9Sstevel@tonic-gate mp_drvname_matches_minor_name = 70927c478bd9Sstevel@tonic-gate (strcmp(mp->mp_drvname, minor_name) == 0 ? TRUE:FALSE); 70937c478bd9Sstevel@tonic-gate mp_drvname_is_clone = 70947c478bd9Sstevel@tonic-gate (strcmp(mp->mp_drvname, "clone") == 0 ? TRUE : FALSE); 70957c478bd9Sstevel@tonic-gate mp_drvname_matches_drvname = 70967c478bd9Sstevel@tonic-gate (strcmp(mp->mp_drvname, driver) == 0 ? TRUE : FALSE); 70977c478bd9Sstevel@tonic-gate 70987c478bd9Sstevel@tonic-gate /* 70997c478bd9Sstevel@tonic-gate * If one of the following cases is true, then we try to change 71007c478bd9Sstevel@tonic-gate * the permissions if a "shell global pattern match" of 71017c478bd9Sstevel@tonic-gate * mp_>mp_minorname matches minor_name. 71027c478bd9Sstevel@tonic-gate * 71037c478bd9Sstevel@tonic-gate * 1. mp->mp_drvname matches driver. 71047c478bd9Sstevel@tonic-gate * 71057c478bd9Sstevel@tonic-gate * OR 71067c478bd9Sstevel@tonic-gate * 71077c478bd9Sstevel@tonic-gate * 2. mp->mp_drvname matches node_name and this 71087c478bd9Sstevel@tonic-gate * name is an alias of the driver name 71097c478bd9Sstevel@tonic-gate * 71107c478bd9Sstevel@tonic-gate * OR 71117c478bd9Sstevel@tonic-gate * 71127c478bd9Sstevel@tonic-gate * 3. /devices entry is the clone device and either 71137c478bd9Sstevel@tonic-gate * minor_perm entry is the clone device or matches 71147c478bd9Sstevel@tonic-gate * the minor part of the clone device. 71157c478bd9Sstevel@tonic-gate */ 71167c478bd9Sstevel@tonic-gate 71177c478bd9Sstevel@tonic-gate if ((mp_drvname_matches_drvname == TRUE)|| 71187c478bd9Sstevel@tonic-gate ((mp_drvname_matches_node_name == TRUE) && 71197c478bd9Sstevel@tonic-gate (alias(driver, node_name) == TRUE)) || 71207c478bd9Sstevel@tonic-gate ((is_clone == TRUE) && 71217c478bd9Sstevel@tonic-gate ((mp_drvname_is_clone == TRUE) || 71227c478bd9Sstevel@tonic-gate (mp_drvname_matches_minor_name == TRUE)))) { 71237c478bd9Sstevel@tonic-gate /* 71247c478bd9Sstevel@tonic-gate * Check that the minor part of the 71257c478bd9Sstevel@tonic-gate * device name from the minor_perm 71267c478bd9Sstevel@tonic-gate * entry matches and if so, set the 71277c478bd9Sstevel@tonic-gate * permissions. 71287c478bd9Sstevel@tonic-gate * 71297c478bd9Sstevel@tonic-gate * Under real devfs, clone minor name is changed 71307c478bd9Sstevel@tonic-gate * to match the driver name, but minor_perm may 71317c478bd9Sstevel@tonic-gate * not match. We reconcile it here. 71327c478bd9Sstevel@tonic-gate */ 71337c478bd9Sstevel@tonic-gate if (aminor != NULL) 71347c478bd9Sstevel@tonic-gate minor_name = aminor; 71357c478bd9Sstevel@tonic-gate 71367c478bd9Sstevel@tonic-gate if (gmatch(minor_name, mp->mp_minorname) != 0) { 71377c478bd9Sstevel@tonic-gate *uid = mp->mp_uid; 71387c478bd9Sstevel@tonic-gate *gid = mp->mp_gid; 71397c478bd9Sstevel@tonic-gate *mode = spectype | mp->mp_mode; 71407c478bd9Sstevel@tonic-gate match = TRUE; 71417c478bd9Sstevel@tonic-gate } 71427c478bd9Sstevel@tonic-gate } 71437c478bd9Sstevel@tonic-gate } 71447c478bd9Sstevel@tonic-gate 71457c478bd9Sstevel@tonic-gate if (match == TRUE) { 71467c478bd9Sstevel@tonic-gate return; 71477c478bd9Sstevel@tonic-gate } 71487c478bd9Sstevel@tonic-gate 71497c478bd9Sstevel@tonic-gate use_defaults: 71507c478bd9Sstevel@tonic-gate /* not found in minor_perm, so just use default values */ 71517c478bd9Sstevel@tonic-gate *uid = root_uid; 71527c478bd9Sstevel@tonic-gate *gid = sys_gid; 71537c478bd9Sstevel@tonic-gate *mode = (spectype | 0600); 71547c478bd9Sstevel@tonic-gate } 71557c478bd9Sstevel@tonic-gate 71567c478bd9Sstevel@tonic-gate /* 71577c478bd9Sstevel@tonic-gate * Called by devfs_read_minor_perm() to report errors 71587c478bd9Sstevel@tonic-gate * key is: 71597c478bd9Sstevel@tonic-gate * line number: ignoring line number error 71607c478bd9Sstevel@tonic-gate * errno: open/close errors 71617c478bd9Sstevel@tonic-gate * size: alloc errors 71627c478bd9Sstevel@tonic-gate */ 71637c478bd9Sstevel@tonic-gate static void 71647c478bd9Sstevel@tonic-gate minorperm_err_cb(minorperm_err_t mp_err, int key) 71657c478bd9Sstevel@tonic-gate { 71667c478bd9Sstevel@tonic-gate switch (mp_err) { 71677c478bd9Sstevel@tonic-gate case MP_FOPEN_ERR: 71687c478bd9Sstevel@tonic-gate err_print(FOPEN_FAILED, MINOR_PERM_FILE, strerror(key)); 71697c478bd9Sstevel@tonic-gate break; 71707c478bd9Sstevel@tonic-gate case MP_FCLOSE_ERR: 71717c478bd9Sstevel@tonic-gate err_print(FCLOSE_FAILED, MINOR_PERM_FILE, strerror(key)); 71727c478bd9Sstevel@tonic-gate break; 71737c478bd9Sstevel@tonic-gate case MP_IGNORING_LINE_ERR: 71747c478bd9Sstevel@tonic-gate err_print(IGNORING_LINE_IN, key, MINOR_PERM_FILE); 71757c478bd9Sstevel@tonic-gate break; 71767c478bd9Sstevel@tonic-gate case MP_ALLOC_ERR: 71777c478bd9Sstevel@tonic-gate err_print(MALLOC_FAILED, key); 71787c478bd9Sstevel@tonic-gate break; 71797c478bd9Sstevel@tonic-gate case MP_NVLIST_ERR: 71807c478bd9Sstevel@tonic-gate err_print(NVLIST_ERROR, MINOR_PERM_FILE, strerror(key)); 71817c478bd9Sstevel@tonic-gate break; 71827c478bd9Sstevel@tonic-gate case MP_CANT_FIND_USER_ERR: 71837c478bd9Sstevel@tonic-gate err_print(CANT_FIND_USER, DEFAULT_DEV_USER); 71847c478bd9Sstevel@tonic-gate break; 71857c478bd9Sstevel@tonic-gate case MP_CANT_FIND_GROUP_ERR: 71867c478bd9Sstevel@tonic-gate err_print(CANT_FIND_GROUP, DEFAULT_DEV_GROUP); 71877c478bd9Sstevel@tonic-gate break; 71887c478bd9Sstevel@tonic-gate } 71897c478bd9Sstevel@tonic-gate } 71907c478bd9Sstevel@tonic-gate 71917c478bd9Sstevel@tonic-gate static void 71927c478bd9Sstevel@tonic-gate read_minor_perm_file(void) 71937c478bd9Sstevel@tonic-gate { 71947c478bd9Sstevel@tonic-gate static int cached = FALSE; 71957c478bd9Sstevel@tonic-gate static struct stat cached_sb; 71967c478bd9Sstevel@tonic-gate struct stat current_sb; 71977c478bd9Sstevel@tonic-gate 71987c478bd9Sstevel@tonic-gate (void) stat(MINOR_PERM_FILE, ¤t_sb); 71997c478bd9Sstevel@tonic-gate 72007c478bd9Sstevel@tonic-gate /* If already cached, check to see if it is still valid */ 72017c478bd9Sstevel@tonic-gate if (cached == TRUE) { 72027c478bd9Sstevel@tonic-gate 72037c478bd9Sstevel@tonic-gate if (current_sb.st_mtime == cached_sb.st_mtime) { 72047c478bd9Sstevel@tonic-gate vprint(FILES_MID, "%s cache valid\n", MINOR_PERM_FILE); 72057c478bd9Sstevel@tonic-gate return; 72067c478bd9Sstevel@tonic-gate } 72077c478bd9Sstevel@tonic-gate devfs_free_minor_perm(minor_perms); 72087c478bd9Sstevel@tonic-gate minor_perms = NULL; 72097c478bd9Sstevel@tonic-gate } else { 72107c478bd9Sstevel@tonic-gate cached = TRUE; 72117c478bd9Sstevel@tonic-gate } 72127c478bd9Sstevel@tonic-gate 72137c478bd9Sstevel@tonic-gate (void) stat(MINOR_PERM_FILE, &cached_sb); 72147c478bd9Sstevel@tonic-gate 72157c478bd9Sstevel@tonic-gate vprint(FILES_MID, "loading binding file: %s\n", MINOR_PERM_FILE); 72167c478bd9Sstevel@tonic-gate 72177c478bd9Sstevel@tonic-gate minor_perms = devfs_read_minor_perm(minorperm_err_cb); 72187c478bd9Sstevel@tonic-gate } 72197c478bd9Sstevel@tonic-gate 72207c478bd9Sstevel@tonic-gate static void 72217c478bd9Sstevel@tonic-gate load_minor_perm_file(void) 72227c478bd9Sstevel@tonic-gate { 72237c478bd9Sstevel@tonic-gate read_minor_perm_file(); 72247c478bd9Sstevel@tonic-gate if (devfs_load_minor_perm(minor_perms, minorperm_err_cb) != 0) 72257c478bd9Sstevel@tonic-gate err_print(gettext("minor_perm load failed\n")); 72267c478bd9Sstevel@tonic-gate } 72277c478bd9Sstevel@tonic-gate 72287c478bd9Sstevel@tonic-gate static char * 72297c478bd9Sstevel@tonic-gate convert_to_re(char *dev) 72307c478bd9Sstevel@tonic-gate { 72317c478bd9Sstevel@tonic-gate char *p, *l, *out; 72327c478bd9Sstevel@tonic-gate int i; 72337c478bd9Sstevel@tonic-gate 72347c478bd9Sstevel@tonic-gate out = s_malloc(PATH_MAX); 72357c478bd9Sstevel@tonic-gate 72367c478bd9Sstevel@tonic-gate for (l = p = dev, i = 0; (*p != '\0') && (i < (PATH_MAX - 1)); 72377c478bd9Sstevel@tonic-gate ++p, i++) { 72387c478bd9Sstevel@tonic-gate if ((*p == '*') && ((l != p) && (*l == '/'))) { 72397c478bd9Sstevel@tonic-gate out[i++] = '.'; 72407c478bd9Sstevel@tonic-gate out[i] = '+'; 72417c478bd9Sstevel@tonic-gate } else { 72427c478bd9Sstevel@tonic-gate out[i] = *p; 72437c478bd9Sstevel@tonic-gate } 72447c478bd9Sstevel@tonic-gate l = p; 72457c478bd9Sstevel@tonic-gate } 72467c478bd9Sstevel@tonic-gate out[i] = '\0'; 72477c478bd9Sstevel@tonic-gate p = (char *)s_malloc(strlen(out) + 1); 72487c478bd9Sstevel@tonic-gate (void) strlcpy(p, out, strlen(out) + 1); 72497c478bd9Sstevel@tonic-gate free(out); 72507c478bd9Sstevel@tonic-gate 72517c478bd9Sstevel@tonic-gate vprint(FILES_MID, "converted %s -> %s\n", dev, p); 72527c478bd9Sstevel@tonic-gate 72537c478bd9Sstevel@tonic-gate return (p); 72547c478bd9Sstevel@tonic-gate } 72557c478bd9Sstevel@tonic-gate 72567c478bd9Sstevel@tonic-gate static void 72577c478bd9Sstevel@tonic-gate read_logindevperm_file(void) 72587c478bd9Sstevel@tonic-gate { 72597c478bd9Sstevel@tonic-gate static int cached = FALSE; 72607c478bd9Sstevel@tonic-gate static struct stat cached_sb; 72617c478bd9Sstevel@tonic-gate struct stat current_sb; 72627c478bd9Sstevel@tonic-gate struct login_dev *ldev; 72637c478bd9Sstevel@tonic-gate FILE *fp; 72647c478bd9Sstevel@tonic-gate char line[MAX_LDEV_LINE]; 72657c478bd9Sstevel@tonic-gate int ln, perm, rv; 72666e670f77Saj char *cp, *console, *dlist, *dev; 72677c478bd9Sstevel@tonic-gate char *lasts, *devlasts, *permstr, *drv; 72687c478bd9Sstevel@tonic-gate struct driver_list *list, *next; 72697c478bd9Sstevel@tonic-gate 72707c478bd9Sstevel@tonic-gate /* Read logindevperm only when enabled */ 72717c478bd9Sstevel@tonic-gate if (login_dev_enable != TRUE) 72727c478bd9Sstevel@tonic-gate return; 72737c478bd9Sstevel@tonic-gate 72747c478bd9Sstevel@tonic-gate if (cached == TRUE) { 72757c478bd9Sstevel@tonic-gate if (stat(LDEV_FILE, ¤t_sb) == 0 && 72767c478bd9Sstevel@tonic-gate current_sb.st_mtime == cached_sb.st_mtime) { 72777c478bd9Sstevel@tonic-gate vprint(FILES_MID, "%s cache valid\n", LDEV_FILE); 72787c478bd9Sstevel@tonic-gate return; 72797c478bd9Sstevel@tonic-gate } 72807c478bd9Sstevel@tonic-gate vprint(FILES_MID, "invalidating %s cache\n", LDEV_FILE); 72817c478bd9Sstevel@tonic-gate while (login_dev_cache != NULL) { 72827c478bd9Sstevel@tonic-gate 72837c478bd9Sstevel@tonic-gate ldev = login_dev_cache; 72847c478bd9Sstevel@tonic-gate login_dev_cache = ldev->ldev_next; 72857c478bd9Sstevel@tonic-gate free(ldev->ldev_console); 72867c478bd9Sstevel@tonic-gate free(ldev->ldev_device); 72877c478bd9Sstevel@tonic-gate regfree(&ldev->ldev_device_regex); 72887c478bd9Sstevel@tonic-gate list = ldev->ldev_driver_list; 72897c478bd9Sstevel@tonic-gate while (list) { 72907c478bd9Sstevel@tonic-gate next = list->next; 72917c478bd9Sstevel@tonic-gate free(list); 72927c478bd9Sstevel@tonic-gate list = next; 72937c478bd9Sstevel@tonic-gate } 72947c478bd9Sstevel@tonic-gate free(ldev); 72957c478bd9Sstevel@tonic-gate } 72967c478bd9Sstevel@tonic-gate } else { 72977c478bd9Sstevel@tonic-gate cached = TRUE; 72987c478bd9Sstevel@tonic-gate } 72997c478bd9Sstevel@tonic-gate 73007c478bd9Sstevel@tonic-gate assert(login_dev_cache == NULL); 73017c478bd9Sstevel@tonic-gate 73027c478bd9Sstevel@tonic-gate if (stat(LDEV_FILE, &cached_sb) != 0) { 73037c478bd9Sstevel@tonic-gate cached = FALSE; 73047c478bd9Sstevel@tonic-gate return; 73057c478bd9Sstevel@tonic-gate } 73067c478bd9Sstevel@tonic-gate 73077c478bd9Sstevel@tonic-gate vprint(FILES_MID, "loading file: %s\n", LDEV_FILE); 73087c478bd9Sstevel@tonic-gate 73097c478bd9Sstevel@tonic-gate if ((fp = fopen(LDEV_FILE, "r")) == NULL) { 73107c478bd9Sstevel@tonic-gate /* Not fatal to devfsadm */ 73117c478bd9Sstevel@tonic-gate cached = FALSE; 73127c478bd9Sstevel@tonic-gate err_print(FOPEN_FAILED, LDEV_FILE, strerror(errno)); 73137c478bd9Sstevel@tonic-gate return; 73147c478bd9Sstevel@tonic-gate } 73157c478bd9Sstevel@tonic-gate 73167c478bd9Sstevel@tonic-gate ln = 0; 73177c478bd9Sstevel@tonic-gate while (fgets(line, MAX_LDEV_LINE, fp) != NULL) { 73187c478bd9Sstevel@tonic-gate ln++; 73197c478bd9Sstevel@tonic-gate 73207c478bd9Sstevel@tonic-gate /* Remove comments */ 73217c478bd9Sstevel@tonic-gate if ((cp = strchr(line, '#')) != NULL) 73227c478bd9Sstevel@tonic-gate *cp = '\0'; 73237c478bd9Sstevel@tonic-gate 73247c478bd9Sstevel@tonic-gate if ((console = strtok_r(line, LDEV_DELIMS, &lasts)) == NULL) 73257c478bd9Sstevel@tonic-gate continue; /* Blank line */ 73267c478bd9Sstevel@tonic-gate 73277c478bd9Sstevel@tonic-gate if ((permstr = strtok_r(NULL, LDEV_DELIMS, &lasts)) == NULL) { 73287c478bd9Sstevel@tonic-gate err_print(IGNORING_LINE_IN, ln, LDEV_FILE); 73297c478bd9Sstevel@tonic-gate continue; /* Malformed line */ 73307c478bd9Sstevel@tonic-gate } 73317c478bd9Sstevel@tonic-gate 73327c478bd9Sstevel@tonic-gate /* 73337c478bd9Sstevel@tonic-gate * permstr is string in octal format. Convert to int 73347c478bd9Sstevel@tonic-gate */ 73357c478bd9Sstevel@tonic-gate cp = NULL; 73367c478bd9Sstevel@tonic-gate errno = 0; 73377c478bd9Sstevel@tonic-gate perm = strtol(permstr, &cp, 8); 73387c478bd9Sstevel@tonic-gate if (errno || perm < 0 || perm > 0777 || *cp != '\0') { 73397c478bd9Sstevel@tonic-gate err_print(IGNORING_LINE_IN, ln, LDEV_FILE); 73407c478bd9Sstevel@tonic-gate continue; 73417c478bd9Sstevel@tonic-gate } 73427c478bd9Sstevel@tonic-gate 73436e670f77Saj if ((dlist = strtok_r(NULL, LDEV_DELIMS, &lasts)) == NULL) { 73447c478bd9Sstevel@tonic-gate err_print(IGNORING_LINE_IN, ln, LDEV_FILE); 73457c478bd9Sstevel@tonic-gate continue; 73467c478bd9Sstevel@tonic-gate } 73477c478bd9Sstevel@tonic-gate 73486e670f77Saj dev = strtok_r(dlist, LDEV_DEV_DELIM, &devlasts); 73497c478bd9Sstevel@tonic-gate while (dev) { 73507c478bd9Sstevel@tonic-gate 73517c478bd9Sstevel@tonic-gate ldev = (struct login_dev *)s_zalloc( 73527c478bd9Sstevel@tonic-gate sizeof (struct login_dev)); 73537c478bd9Sstevel@tonic-gate ldev->ldev_console = s_strdup(console); 73547c478bd9Sstevel@tonic-gate ldev->ldev_perms = perm; 73557c478bd9Sstevel@tonic-gate 73567c478bd9Sstevel@tonic-gate /* 73577c478bd9Sstevel@tonic-gate * the logical device name may contain '*' which 73587c478bd9Sstevel@tonic-gate * we convert to a regular expression 73597c478bd9Sstevel@tonic-gate */ 73607c478bd9Sstevel@tonic-gate ldev->ldev_device = convert_to_re(dev); 73617c478bd9Sstevel@tonic-gate if (ldev->ldev_device && 73627c478bd9Sstevel@tonic-gate (rv = regcomp(&ldev->ldev_device_regex, 73637c478bd9Sstevel@tonic-gate ldev->ldev_device, REG_EXTENDED))) { 73647c478bd9Sstevel@tonic-gate bzero(&ldev->ldev_device_regex, 73657c478bd9Sstevel@tonic-gate sizeof (ldev->ldev_device_regex)); 73667c478bd9Sstevel@tonic-gate err_print(REGCOMP_FAILED, 73677c478bd9Sstevel@tonic-gate ldev->ldev_device, rv); 73687c478bd9Sstevel@tonic-gate } 73697c478bd9Sstevel@tonic-gate ldev->ldev_next = login_dev_cache; 73707c478bd9Sstevel@tonic-gate login_dev_cache = ldev; 73717c478bd9Sstevel@tonic-gate dev = strtok_r(NULL, LDEV_DEV_DELIM, &devlasts); 73727c478bd9Sstevel@tonic-gate } 73737c478bd9Sstevel@tonic-gate 73747c478bd9Sstevel@tonic-gate drv = strtok_r(NULL, LDEV_DRVLIST_DELIMS, &lasts); 73757c478bd9Sstevel@tonic-gate if (drv) { 73767c478bd9Sstevel@tonic-gate if (strcmp(drv, LDEV_DRVLIST_NAME) == 0) { 73777c478bd9Sstevel@tonic-gate 73787c478bd9Sstevel@tonic-gate drv = strtok_r(NULL, LDEV_DRV_DELIMS, 73797c478bd9Sstevel@tonic-gate &lasts); 73807c478bd9Sstevel@tonic-gate 73817c478bd9Sstevel@tonic-gate while (drv) { 73827c478bd9Sstevel@tonic-gate vprint(FILES_MID, 73837c478bd9Sstevel@tonic-gate "logindevperm driver=%s\n", 73847c478bd9Sstevel@tonic-gate drv); 73857c478bd9Sstevel@tonic-gate 73867c478bd9Sstevel@tonic-gate /* 73877c478bd9Sstevel@tonic-gate * create a linked list of driver 73887c478bd9Sstevel@tonic-gate * names 73897c478bd9Sstevel@tonic-gate */ 73907c478bd9Sstevel@tonic-gate list = (struct driver_list *) 73917c478bd9Sstevel@tonic-gate s_zalloc( 73927c478bd9Sstevel@tonic-gate sizeof (struct driver_list)); 73937c478bd9Sstevel@tonic-gate (void) strlcpy(list->driver_name, drv, 73947c478bd9Sstevel@tonic-gate sizeof (list->driver_name)); 73957c478bd9Sstevel@tonic-gate list->next = ldev->ldev_driver_list; 73967c478bd9Sstevel@tonic-gate ldev->ldev_driver_list = list; 73977c478bd9Sstevel@tonic-gate drv = strtok_r(NULL, LDEV_DRV_DELIMS, 73987c478bd9Sstevel@tonic-gate &lasts); 73997c478bd9Sstevel@tonic-gate } 74007c478bd9Sstevel@tonic-gate } 74017c478bd9Sstevel@tonic-gate } 74027c478bd9Sstevel@tonic-gate } 74037c478bd9Sstevel@tonic-gate (void) fclose(fp); 74047c478bd9Sstevel@tonic-gate } 74057c478bd9Sstevel@tonic-gate 74067c478bd9Sstevel@tonic-gate /* 74077c478bd9Sstevel@tonic-gate * Tokens are separated by ' ', '\t', ':', '=', '&', '|', ';', '\n', or '\0' 74087c478bd9Sstevel@tonic-gate * 74097c478bd9Sstevel@tonic-gate * Returns DEVFSADM_SUCCESS if token found, DEVFSADM_FAILURE otherwise. 74107c478bd9Sstevel@tonic-gate */ 74117c478bd9Sstevel@tonic-gate static int 74127c478bd9Sstevel@tonic-gate getnexttoken(char *next, char **nextp, char **tokenpp, char *tchar) 74137c478bd9Sstevel@tonic-gate { 74147c478bd9Sstevel@tonic-gate char *cp; 74157c478bd9Sstevel@tonic-gate char *cp1; 74167c478bd9Sstevel@tonic-gate char *tokenp; 74177c478bd9Sstevel@tonic-gate 74187c478bd9Sstevel@tonic-gate cp = next; 74197c478bd9Sstevel@tonic-gate while (*cp == ' ' || *cp == '\t') { 74207c478bd9Sstevel@tonic-gate cp++; /* skip leading spaces */ 74217c478bd9Sstevel@tonic-gate } 74227c478bd9Sstevel@tonic-gate tokenp = cp; /* start of token */ 74237c478bd9Sstevel@tonic-gate while (*cp != '\0' && *cp != '\n' && *cp != ' ' && *cp != '\t' && 74247c478bd9Sstevel@tonic-gate *cp != ':' && *cp != '=' && *cp != '&' && 74257c478bd9Sstevel@tonic-gate *cp != '|' && *cp != ';') { 74267c478bd9Sstevel@tonic-gate cp++; /* point to next character */ 74277c478bd9Sstevel@tonic-gate } 74287c478bd9Sstevel@tonic-gate /* 74297c478bd9Sstevel@tonic-gate * If terminating character is a space or tab, look ahead to see if 74307c478bd9Sstevel@tonic-gate * there's another terminator that's not a space or a tab. 74317c478bd9Sstevel@tonic-gate * (This code handles trailing spaces.) 74327c478bd9Sstevel@tonic-gate */ 74337c478bd9Sstevel@tonic-gate if (*cp == ' ' || *cp == '\t') { 74347c478bd9Sstevel@tonic-gate cp1 = cp; 74357c478bd9Sstevel@tonic-gate while (*++cp1 == ' ' || *cp1 == '\t') 74367c478bd9Sstevel@tonic-gate ; 74377c478bd9Sstevel@tonic-gate if (*cp1 == '=' || *cp1 == ':' || *cp1 == '&' || *cp1 == '|' || 74387c478bd9Sstevel@tonic-gate *cp1 == ';' || *cp1 == '\n' || *cp1 == '\0') { 74397c478bd9Sstevel@tonic-gate *cp = NULL; /* terminate token */ 74407c478bd9Sstevel@tonic-gate cp = cp1; 74417c478bd9Sstevel@tonic-gate } 74427c478bd9Sstevel@tonic-gate } 74437c478bd9Sstevel@tonic-gate if (tchar != NULL) { 74447c478bd9Sstevel@tonic-gate *tchar = *cp; /* save terminating character */ 74457c478bd9Sstevel@tonic-gate if (*tchar == '\0') { 74467c478bd9Sstevel@tonic-gate *tchar = '\n'; 74477c478bd9Sstevel@tonic-gate } 74487c478bd9Sstevel@tonic-gate } 74497c478bd9Sstevel@tonic-gate *cp++ = '\0'; /* terminate token, point to next */ 74507c478bd9Sstevel@tonic-gate *nextp = cp; /* set pointer to next character */ 74517c478bd9Sstevel@tonic-gate if (cp - tokenp - 1 == 0) { 74527c478bd9Sstevel@tonic-gate return (DEVFSADM_FAILURE); 74537c478bd9Sstevel@tonic-gate } 74547c478bd9Sstevel@tonic-gate *tokenpp = tokenp; 74557c478bd9Sstevel@tonic-gate return (DEVFSADM_SUCCESS); 74567c478bd9Sstevel@tonic-gate } 74577c478bd9Sstevel@tonic-gate 74587c478bd9Sstevel@tonic-gate /* 74597c478bd9Sstevel@tonic-gate * read or reread the driver aliases file 74607c478bd9Sstevel@tonic-gate */ 74617c478bd9Sstevel@tonic-gate static void 74627c478bd9Sstevel@tonic-gate read_driver_aliases_file(void) 74637c478bd9Sstevel@tonic-gate { 74647c478bd9Sstevel@tonic-gate 74657c478bd9Sstevel@tonic-gate driver_alias_t *save; 74667c478bd9Sstevel@tonic-gate driver_alias_t *lst_tail; 74677c478bd9Sstevel@tonic-gate driver_alias_t *ap; 74687c478bd9Sstevel@tonic-gate static int cached = FALSE; 74697c478bd9Sstevel@tonic-gate FILE *afd; 74707c478bd9Sstevel@tonic-gate char line[256]; 74717c478bd9Sstevel@tonic-gate char *cp; 74727c478bd9Sstevel@tonic-gate char *p; 74737c478bd9Sstevel@tonic-gate char t; 74747c478bd9Sstevel@tonic-gate int ln = 0; 74757c478bd9Sstevel@tonic-gate static struct stat cached_sb; 74767c478bd9Sstevel@tonic-gate struct stat current_sb; 74777c478bd9Sstevel@tonic-gate 74787c478bd9Sstevel@tonic-gate (void) stat(ALIASFILE, ¤t_sb); 74797c478bd9Sstevel@tonic-gate 74807c478bd9Sstevel@tonic-gate /* If already cached, check to see if it is still valid */ 74817c478bd9Sstevel@tonic-gate if (cached == TRUE) { 74827c478bd9Sstevel@tonic-gate 74837c478bd9Sstevel@tonic-gate if (current_sb.st_mtime == cached_sb.st_mtime) { 74847c478bd9Sstevel@tonic-gate vprint(FILES_MID, "%s cache valid\n", ALIASFILE); 74857c478bd9Sstevel@tonic-gate return; 74867c478bd9Sstevel@tonic-gate } 74877c478bd9Sstevel@tonic-gate 74887c478bd9Sstevel@tonic-gate vprint(FILES_MID, "invalidating %s cache\n", ALIASFILE); 74897c478bd9Sstevel@tonic-gate while (driver_aliases != NULL) { 74907c478bd9Sstevel@tonic-gate free(driver_aliases->alias_name); 74917c478bd9Sstevel@tonic-gate free(driver_aliases->driver_name); 74927c478bd9Sstevel@tonic-gate save = driver_aliases; 74937c478bd9Sstevel@tonic-gate driver_aliases = driver_aliases->next; 74947c478bd9Sstevel@tonic-gate free(save); 74957c478bd9Sstevel@tonic-gate } 74967c478bd9Sstevel@tonic-gate } else { 74977c478bd9Sstevel@tonic-gate cached = TRUE; 74987c478bd9Sstevel@tonic-gate } 74997c478bd9Sstevel@tonic-gate 75007c478bd9Sstevel@tonic-gate (void) stat(ALIASFILE, &cached_sb); 75017c478bd9Sstevel@tonic-gate 75027c478bd9Sstevel@tonic-gate vprint(FILES_MID, "loading binding file: %s\n", ALIASFILE); 75037c478bd9Sstevel@tonic-gate 75047c478bd9Sstevel@tonic-gate if ((afd = fopen(ALIASFILE, "r")) == NULL) { 75057c478bd9Sstevel@tonic-gate err_print(FOPEN_FAILED, ALIASFILE, strerror(errno)); 75067c478bd9Sstevel@tonic-gate devfsadm_exit(1); 75077c478bd9Sstevel@tonic-gate } 75087c478bd9Sstevel@tonic-gate 75091ca93273Seota while (fgets(line, sizeof (line), afd) != NULL) { 75107c478bd9Sstevel@tonic-gate ln++; 75111ca93273Seota /* cut off comments starting with '#' */ 75121ca93273Seota if ((cp = strchr(line, '#')) != NULL) 75131ca93273Seota *cp = '\0'; 75141ca93273Seota /* ignore comment or blank lines */ 75151ca93273Seota if (is_blank(line)) 75161ca93273Seota continue; 75177c478bd9Sstevel@tonic-gate cp = line; 75187c478bd9Sstevel@tonic-gate if (getnexttoken(cp, &cp, &p, &t) == DEVFSADM_FAILURE) { 75197c478bd9Sstevel@tonic-gate err_print(IGNORING_LINE_IN, ln, ALIASFILE); 75207c478bd9Sstevel@tonic-gate continue; 75217c478bd9Sstevel@tonic-gate } 75227c478bd9Sstevel@tonic-gate if (t == '\n' || t == '\0') { 75237c478bd9Sstevel@tonic-gate err_print(DRV_BUT_NO_ALIAS, ln, ALIASFILE); 75247c478bd9Sstevel@tonic-gate continue; 75257c478bd9Sstevel@tonic-gate } 75267c478bd9Sstevel@tonic-gate ap = (struct driver_alias *) 75277c478bd9Sstevel@tonic-gate s_zalloc(sizeof (struct driver_alias)); 75287c478bd9Sstevel@tonic-gate ap->driver_name = s_strdup(p); 75297c478bd9Sstevel@tonic-gate if (getnexttoken(cp, &cp, &p, &t) == DEVFSADM_FAILURE) { 75307c478bd9Sstevel@tonic-gate err_print(DRV_BUT_NO_ALIAS, ln, ALIASFILE); 75317c478bd9Sstevel@tonic-gate free(ap->driver_name); 75327c478bd9Sstevel@tonic-gate free(ap); 75337c478bd9Sstevel@tonic-gate continue; 75347c478bd9Sstevel@tonic-gate } 75357c478bd9Sstevel@tonic-gate if (*p == '"') { 75367c478bd9Sstevel@tonic-gate if (p[strlen(p) - 1] == '"') { 75377c478bd9Sstevel@tonic-gate p[strlen(p) - 1] = '\0'; 75387c478bd9Sstevel@tonic-gate p++; 75397c478bd9Sstevel@tonic-gate } 75407c478bd9Sstevel@tonic-gate } 75417c478bd9Sstevel@tonic-gate ap->alias_name = s_strdup(p); 75427c478bd9Sstevel@tonic-gate if (driver_aliases == NULL) { 75437c478bd9Sstevel@tonic-gate driver_aliases = ap; 75447c478bd9Sstevel@tonic-gate lst_tail = ap; 75457c478bd9Sstevel@tonic-gate } else { 75467c478bd9Sstevel@tonic-gate lst_tail->next = ap; 75477c478bd9Sstevel@tonic-gate lst_tail = ap; 75487c478bd9Sstevel@tonic-gate } 75497c478bd9Sstevel@tonic-gate } 75507c478bd9Sstevel@tonic-gate if (fclose(afd) == EOF) { 75517c478bd9Sstevel@tonic-gate err_print(FCLOSE_FAILED, ALIASFILE, strerror(errno)); 75527c478bd9Sstevel@tonic-gate } 75537c478bd9Sstevel@tonic-gate } 75547c478bd9Sstevel@tonic-gate 75557c478bd9Sstevel@tonic-gate /* 75567c478bd9Sstevel@tonic-gate * return TRUE if alias_name is an alias for driver_name, otherwise 75577c478bd9Sstevel@tonic-gate * return FALSE. 75587c478bd9Sstevel@tonic-gate */ 75597c478bd9Sstevel@tonic-gate static int 75607c478bd9Sstevel@tonic-gate alias(char *driver_name, char *alias_name) 75617c478bd9Sstevel@tonic-gate { 75627c478bd9Sstevel@tonic-gate driver_alias_t *alias; 75637c478bd9Sstevel@tonic-gate 75647c478bd9Sstevel@tonic-gate /* 75657c478bd9Sstevel@tonic-gate * check for a match 75667c478bd9Sstevel@tonic-gate */ 75677c478bd9Sstevel@tonic-gate for (alias = driver_aliases; alias != NULL; alias = alias->next) { 75687c478bd9Sstevel@tonic-gate if ((strcmp(alias->driver_name, driver_name) == 0) && 75697c478bd9Sstevel@tonic-gate (strcmp(alias->alias_name, alias_name) == 0)) { 75707c478bd9Sstevel@tonic-gate return (TRUE); 75717c478bd9Sstevel@tonic-gate } 75727c478bd9Sstevel@tonic-gate } 75737c478bd9Sstevel@tonic-gate return (FALSE); 75747c478bd9Sstevel@tonic-gate } 75757c478bd9Sstevel@tonic-gate 75767c478bd9Sstevel@tonic-gate /* 75777c478bd9Sstevel@tonic-gate * convenience functions 75787c478bd9Sstevel@tonic-gate */ 7579facf4a8dSllai1 static int 7580facf4a8dSllai1 s_stat(const char *path, struct stat *sbufp) 7581facf4a8dSllai1 { 7582facf4a8dSllai1 int rv; 7583facf4a8dSllai1 retry: 7584facf4a8dSllai1 if ((rv = stat(path, sbufp)) == -1) { 7585facf4a8dSllai1 if (errno == EINTR) 7586facf4a8dSllai1 goto retry; 7587facf4a8dSllai1 } 7588facf4a8dSllai1 return (rv); 7589facf4a8dSllai1 } 7590facf4a8dSllai1 75917c478bd9Sstevel@tonic-gate static void * 75927c478bd9Sstevel@tonic-gate s_malloc(const size_t size) 75937c478bd9Sstevel@tonic-gate { 75947c478bd9Sstevel@tonic-gate void *rp; 75957c478bd9Sstevel@tonic-gate 75967c478bd9Sstevel@tonic-gate rp = malloc(size); 75977c478bd9Sstevel@tonic-gate if (rp == NULL) { 75987c478bd9Sstevel@tonic-gate err_print(MALLOC_FAILED, size); 75997c478bd9Sstevel@tonic-gate devfsadm_exit(1); 76007c478bd9Sstevel@tonic-gate } 76017c478bd9Sstevel@tonic-gate return (rp); 76027c478bd9Sstevel@tonic-gate } 76037c478bd9Sstevel@tonic-gate 76047c478bd9Sstevel@tonic-gate /* 76057c478bd9Sstevel@tonic-gate * convenience functions 76067c478bd9Sstevel@tonic-gate */ 76077c478bd9Sstevel@tonic-gate static void * 76087c478bd9Sstevel@tonic-gate s_realloc(void *ptr, const size_t size) 76097c478bd9Sstevel@tonic-gate { 76107c478bd9Sstevel@tonic-gate ptr = realloc(ptr, size); 76117c478bd9Sstevel@tonic-gate if (ptr == NULL) { 76127c478bd9Sstevel@tonic-gate err_print(REALLOC_FAILED, size); 76137c478bd9Sstevel@tonic-gate devfsadm_exit(1); 76147c478bd9Sstevel@tonic-gate } 76157c478bd9Sstevel@tonic-gate return (ptr); 76167c478bd9Sstevel@tonic-gate } 76177c478bd9Sstevel@tonic-gate 76187c478bd9Sstevel@tonic-gate static void * 76197c478bd9Sstevel@tonic-gate s_zalloc(const size_t size) 76207c478bd9Sstevel@tonic-gate { 76217c478bd9Sstevel@tonic-gate void *rp; 76227c478bd9Sstevel@tonic-gate 76237c478bd9Sstevel@tonic-gate rp = calloc(1, size); 76247c478bd9Sstevel@tonic-gate if (rp == NULL) { 76257c478bd9Sstevel@tonic-gate err_print(CALLOC_FAILED, size); 76267c478bd9Sstevel@tonic-gate devfsadm_exit(1); 76277c478bd9Sstevel@tonic-gate } 76287c478bd9Sstevel@tonic-gate return (rp); 76297c478bd9Sstevel@tonic-gate } 76307c478bd9Sstevel@tonic-gate 76317c478bd9Sstevel@tonic-gate char * 76327c478bd9Sstevel@tonic-gate s_strdup(const char *ptr) 76337c478bd9Sstevel@tonic-gate { 76347c478bd9Sstevel@tonic-gate void *rp; 76357c478bd9Sstevel@tonic-gate 76367c478bd9Sstevel@tonic-gate rp = strdup(ptr); 76377c478bd9Sstevel@tonic-gate if (rp == NULL) { 76387c478bd9Sstevel@tonic-gate err_print(STRDUP_FAILED, ptr); 76397c478bd9Sstevel@tonic-gate devfsadm_exit(1); 76407c478bd9Sstevel@tonic-gate } 76417c478bd9Sstevel@tonic-gate return (rp); 76427c478bd9Sstevel@tonic-gate } 76437c478bd9Sstevel@tonic-gate 76447c478bd9Sstevel@tonic-gate static void 76457c478bd9Sstevel@tonic-gate s_closedir(DIR *dirp) 76467c478bd9Sstevel@tonic-gate { 76477c478bd9Sstevel@tonic-gate retry: 76487c478bd9Sstevel@tonic-gate if (closedir(dirp) != 0) { 76497c478bd9Sstevel@tonic-gate if (errno == EINTR) 76507c478bd9Sstevel@tonic-gate goto retry; 76517c478bd9Sstevel@tonic-gate err_print(CLOSEDIR_FAILED, strerror(errno)); 76527c478bd9Sstevel@tonic-gate } 76537c478bd9Sstevel@tonic-gate } 76547c478bd9Sstevel@tonic-gate 76557c478bd9Sstevel@tonic-gate static void 76567c478bd9Sstevel@tonic-gate s_mkdirp(const char *path, const mode_t mode) 76577c478bd9Sstevel@tonic-gate { 76587c478bd9Sstevel@tonic-gate vprint(CHATTY_MID, "mkdirp(%s, 0x%lx)\n", path, mode); 76597c478bd9Sstevel@tonic-gate if (mkdirp(path, mode) == -1) { 76607c478bd9Sstevel@tonic-gate if (errno != EEXIST) { 76617c478bd9Sstevel@tonic-gate err_print(MKDIR_FAILED, path, mode, strerror(errno)); 76627c478bd9Sstevel@tonic-gate } 76637c478bd9Sstevel@tonic-gate } 76647c478bd9Sstevel@tonic-gate } 76657c478bd9Sstevel@tonic-gate 76667c478bd9Sstevel@tonic-gate static void 76677c478bd9Sstevel@tonic-gate s_unlink(const char *file) 76687c478bd9Sstevel@tonic-gate { 76697c478bd9Sstevel@tonic-gate retry: 76707c478bd9Sstevel@tonic-gate if (unlink(file) == -1) { 76717c478bd9Sstevel@tonic-gate if (errno == EINTR || errno == EAGAIN) 76727c478bd9Sstevel@tonic-gate goto retry; 76737c478bd9Sstevel@tonic-gate if (errno != ENOENT) { 76747c478bd9Sstevel@tonic-gate err_print(UNLINK_FAILED, file, strerror(errno)); 76757c478bd9Sstevel@tonic-gate } 76767c478bd9Sstevel@tonic-gate } 76777c478bd9Sstevel@tonic-gate } 76787c478bd9Sstevel@tonic-gate 76797c478bd9Sstevel@tonic-gate static void 76807c478bd9Sstevel@tonic-gate add_verbose_id(char *mid) 76817c478bd9Sstevel@tonic-gate { 76827c478bd9Sstevel@tonic-gate num_verbose++; 76837c478bd9Sstevel@tonic-gate verbose = s_realloc(verbose, num_verbose * sizeof (char *)); 76847c478bd9Sstevel@tonic-gate verbose[num_verbose - 1] = mid; 76857c478bd9Sstevel@tonic-gate } 76867c478bd9Sstevel@tonic-gate 76877c478bd9Sstevel@tonic-gate /* 76887c478bd9Sstevel@tonic-gate * returns DEVFSADM_TRUE if contents is a minor node in /devices. 76897c478bd9Sstevel@tonic-gate * If mn_root is not NULL, mn_root is set to: 76907c478bd9Sstevel@tonic-gate * if contents is a /dev node, mn_root = contents 76917c478bd9Sstevel@tonic-gate * OR 76927c478bd9Sstevel@tonic-gate * if contents is a /devices node, mn_root set to the '/' 76937c478bd9Sstevel@tonic-gate * following /devices. 76947c478bd9Sstevel@tonic-gate */ 76957c478bd9Sstevel@tonic-gate static int 76967c478bd9Sstevel@tonic-gate is_minor_node(char *contents, char **mn_root) 76977c478bd9Sstevel@tonic-gate { 76987c478bd9Sstevel@tonic-gate char *ptr; 76997c478bd9Sstevel@tonic-gate char device_prefix[100]; 77007c478bd9Sstevel@tonic-gate 77017c478bd9Sstevel@tonic-gate (void) snprintf(device_prefix, sizeof (device_prefix), "../devices/"); 77027c478bd9Sstevel@tonic-gate 77037c478bd9Sstevel@tonic-gate if ((ptr = strstr(contents, device_prefix)) != NULL) { 77047c478bd9Sstevel@tonic-gate if (mn_root != NULL) { 77057c478bd9Sstevel@tonic-gate /* mn_root should point to the / following /devices */ 77067c478bd9Sstevel@tonic-gate *mn_root = ptr += strlen(device_prefix) - 1; 77077c478bd9Sstevel@tonic-gate } 77087c478bd9Sstevel@tonic-gate return (DEVFSADM_TRUE); 77097c478bd9Sstevel@tonic-gate } 77107c478bd9Sstevel@tonic-gate 77117c478bd9Sstevel@tonic-gate (void) snprintf(device_prefix, sizeof (device_prefix), "/devices/"); 77127c478bd9Sstevel@tonic-gate 77137c478bd9Sstevel@tonic-gate if (strncmp(contents, device_prefix, strlen(device_prefix)) == 0) { 77147c478bd9Sstevel@tonic-gate if (mn_root != NULL) { 77157c478bd9Sstevel@tonic-gate /* mn_root should point to the / following /devices */ 77167c478bd9Sstevel@tonic-gate *mn_root = contents + strlen(device_prefix) - 1; 77177c478bd9Sstevel@tonic-gate } 77187c478bd9Sstevel@tonic-gate return (DEVFSADM_TRUE); 77197c478bd9Sstevel@tonic-gate } 77207c478bd9Sstevel@tonic-gate 77217c478bd9Sstevel@tonic-gate if (mn_root != NULL) { 77227c478bd9Sstevel@tonic-gate *mn_root = contents; 77237c478bd9Sstevel@tonic-gate } 77247c478bd9Sstevel@tonic-gate return (DEVFSADM_FALSE); 77257c478bd9Sstevel@tonic-gate } 77267c478bd9Sstevel@tonic-gate 77277c478bd9Sstevel@tonic-gate /* 77287c478bd9Sstevel@tonic-gate * Lookup nvpair corresponding to the given name and type: 77297c478bd9Sstevel@tonic-gate * 77307c478bd9Sstevel@tonic-gate * The standard nvlist_lookup functions in libnvpair don't work as our 77317c478bd9Sstevel@tonic-gate * nvlist is not allocated with NV_UNIQUE_NAME or NV_UNIQUE_NAME_TYPE. 77327c478bd9Sstevel@tonic-gate */ 77337c478bd9Sstevel@tonic-gate static nvpair_t * 77347c478bd9Sstevel@tonic-gate lookup_nvpair(nvlist_t *nvl, char *name, data_type_t type) 77357c478bd9Sstevel@tonic-gate { 77367c478bd9Sstevel@tonic-gate nvpair_t *nvp; 77377c478bd9Sstevel@tonic-gate 77387c478bd9Sstevel@tonic-gate for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL; 77397c478bd9Sstevel@tonic-gate nvp = nvlist_next_nvpair(nvl, nvp)) { 77407c478bd9Sstevel@tonic-gate if (strcmp(name, nvpair_name(nvp)) == 0 && 77417c478bd9Sstevel@tonic-gate nvpair_type(nvp) == type) 77427c478bd9Sstevel@tonic-gate return (nvp); 77437c478bd9Sstevel@tonic-gate } 77447c478bd9Sstevel@tonic-gate 77457c478bd9Sstevel@tonic-gate return (NULL); 77467c478bd9Sstevel@tonic-gate } 77477c478bd9Sstevel@tonic-gate 77487c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 77497c478bd9Sstevel@tonic-gate static void 77507c478bd9Sstevel@tonic-gate process_rcm_events(void *arg) 77517c478bd9Sstevel@tonic-gate { 77527c478bd9Sstevel@tonic-gate struct rcm_eventq *ev, *ev_next; 7753210db224Sericheng nvpair_t *nvp; 7754210db224Sericheng char *path, *driver; 7755210db224Sericheng int instance; 7756210db224Sericheng int err; 77577c478bd9Sstevel@tonic-gate int need_to_exit; 77587c478bd9Sstevel@tonic-gate 77597c478bd9Sstevel@tonic-gate for (;;) { 77607c478bd9Sstevel@tonic-gate (void) mutex_lock(&rcm_eventq_lock); 77617c478bd9Sstevel@tonic-gate while (rcm_eventq_head == NULL && 77627c478bd9Sstevel@tonic-gate need_to_exit_rcm_event_thread == 0) 77637c478bd9Sstevel@tonic-gate (void) cond_wait(&rcm_eventq_cv, &rcm_eventq_lock); 77647c478bd9Sstevel@tonic-gate 77657c478bd9Sstevel@tonic-gate need_to_exit = need_to_exit_rcm_event_thread; 77667c478bd9Sstevel@tonic-gate ev = rcm_eventq_head; 77677c478bd9Sstevel@tonic-gate rcm_eventq_head = rcm_eventq_tail = NULL; 77687c478bd9Sstevel@tonic-gate (void) mutex_unlock(&rcm_eventq_lock); 77697c478bd9Sstevel@tonic-gate 77707c478bd9Sstevel@tonic-gate for (; ev != NULL; ev = ev_next) { 77717c478bd9Sstevel@tonic-gate /* 77727c478bd9Sstevel@tonic-gate * Private notification interface to RCM: 77737c478bd9Sstevel@tonic-gate * Do not retry the RCM notification on an error since 77747c478bd9Sstevel@tonic-gate * we do not know whether the failure occurred in 77757c478bd9Sstevel@tonic-gate * librcm, rcm_daemon or rcm modules or scripts. 77767c478bd9Sstevel@tonic-gate */ 7777210db224Sericheng if (librcm_notify_event(rcm_hdl, 7778210db224Sericheng RCM_RESOURCE_NETWORK_NEW, 0, ev->nvl, NULL) 7779210db224Sericheng != RCM_SUCCESS) { 77807c478bd9Sstevel@tonic-gate 7781210db224Sericheng err = errno; 77827c478bd9Sstevel@tonic-gate 7783210db224Sericheng if (((nvp = lookup_nvpair(ev->nvl, 7784210db224Sericheng RCM_NV_DEVFS_PATH, DATA_TYPE_STRING)) 7785210db224Sericheng == NULL) || 7786210db224Sericheng (nvpair_value_string(nvp, &path) != 0)) 7787210db224Sericheng path = "unknown"; 7788210db224Sericheng 7789210db224Sericheng if (((nvp = lookup_nvpair(ev->nvl, 7790210db224Sericheng RCM_NV_DRIVER_NAME, DATA_TYPE_STRING)) 7791210db224Sericheng == NULL) || 7792210db224Sericheng (nvpair_value_string(nvp, &driver) != 0)) 7793210db224Sericheng driver = "unknown"; 7794210db224Sericheng if (((nvp = lookup_nvpair(ev->nvl, 7795210db224Sericheng RCM_NV_INSTANCE, DATA_TYPE_INT32)) 7796210db224Sericheng == NULL) || 7797210db224Sericheng (nvpair_value_int32(nvp, &instance) != 0)) 7798210db224Sericheng instance = -1; 7799210db224Sericheng 7800210db224Sericheng err_print(RCM_NOTIFY_FAILED, path, driver, 7801210db224Sericheng instance, strerror(err)); 7802210db224Sericheng } 78037c478bd9Sstevel@tonic-gate 78047c478bd9Sstevel@tonic-gate ev_next = ev->next; 78057c478bd9Sstevel@tonic-gate nvlist_free(ev->nvl); 78067c478bd9Sstevel@tonic-gate free(ev); 78077c478bd9Sstevel@tonic-gate } 78087c478bd9Sstevel@tonic-gate 78097c478bd9Sstevel@tonic-gate if (need_to_exit) 78107c478bd9Sstevel@tonic-gate return; 78117c478bd9Sstevel@tonic-gate } 78127c478bd9Sstevel@tonic-gate } 78137c478bd9Sstevel@tonic-gate 78147c478bd9Sstevel@tonic-gate /* 78157c478bd9Sstevel@tonic-gate * Initialize rcm related handles and function pointers. 78167c478bd9Sstevel@tonic-gate * Since RCM need not present in miniroot, we dlopen librcm. 78177c478bd9Sstevel@tonic-gate */ 78187c478bd9Sstevel@tonic-gate static int 78197c478bd9Sstevel@tonic-gate rcm_init(void) 78207c478bd9Sstevel@tonic-gate { 7821d62bc4baSyz147064 #define LIBRCM_PATH "/lib/librcm.so" 78227c478bd9Sstevel@tonic-gate rcm_handle_t *hdl = NULL; 78237c478bd9Sstevel@tonic-gate int err; 78247c478bd9Sstevel@tonic-gate 78257c478bd9Sstevel@tonic-gate if ((librcm_hdl = dlopen(LIBRCM_PATH, RTLD_LAZY)) == NULL) { 78267c478bd9Sstevel@tonic-gate /* 78277c478bd9Sstevel@tonic-gate * don't log an error here, since librcm may not be present 78287c478bd9Sstevel@tonic-gate * in miniroot. 78297c478bd9Sstevel@tonic-gate */ 78307c478bd9Sstevel@tonic-gate return (-1); 78317c478bd9Sstevel@tonic-gate } 78327c478bd9Sstevel@tonic-gate 78337c478bd9Sstevel@tonic-gate librcm_alloc_handle = (int (*)())dlsym(librcm_hdl, "rcm_alloc_handle"); 78347c478bd9Sstevel@tonic-gate librcm_free_handle = (void (*)())dlsym(librcm_hdl, "rcm_free_handle"); 78357c478bd9Sstevel@tonic-gate librcm_notify_event = (int (*)())dlsym(librcm_hdl, "rcm_notify_event"); 78367c478bd9Sstevel@tonic-gate 78377c478bd9Sstevel@tonic-gate if (librcm_alloc_handle == NULL || librcm_notify_event == NULL || 78387c478bd9Sstevel@tonic-gate librcm_free_handle == NULL) { 78397c478bd9Sstevel@tonic-gate err_print(MISSING_SYMBOLS, LIBRCM_PATH); 78407c478bd9Sstevel@tonic-gate goto out; 78417c478bd9Sstevel@tonic-gate } 78427c478bd9Sstevel@tonic-gate 78437c478bd9Sstevel@tonic-gate /* Initialize the rcm handle */ 78447c478bd9Sstevel@tonic-gate if (librcm_alloc_handle(NULL, 0, NULL, &hdl) != RCM_SUCCESS) { 78457c478bd9Sstevel@tonic-gate err_print(RCM_ALLOC_HANDLE_ERROR); 78467c478bd9Sstevel@tonic-gate goto out; 78477c478bd9Sstevel@tonic-gate } 78487c478bd9Sstevel@tonic-gate 78497c478bd9Sstevel@tonic-gate (void) cond_init(&rcm_eventq_cv, USYNC_THREAD, 0); 78507c478bd9Sstevel@tonic-gate (void) mutex_init(&rcm_eventq_lock, USYNC_THREAD, 0); 78517c478bd9Sstevel@tonic-gate 78527c478bd9Sstevel@tonic-gate /* create a thread to notify RCM of events */ 78537c478bd9Sstevel@tonic-gate if ((err = thr_create(NULL, 0, (void *(*)(void *))process_rcm_events, 78547c478bd9Sstevel@tonic-gate NULL, 0, &process_rcm_events_tid)) != 0) { 78557c478bd9Sstevel@tonic-gate err_print(CANT_CREATE_THREAD, "process_rcm_events", 78567c478bd9Sstevel@tonic-gate strerror(err)); 78577c478bd9Sstevel@tonic-gate goto out; 78587c478bd9Sstevel@tonic-gate } 78597c478bd9Sstevel@tonic-gate 78607c478bd9Sstevel@tonic-gate rcm_hdl = hdl; 78617c478bd9Sstevel@tonic-gate return (0); 78627c478bd9Sstevel@tonic-gate 78637c478bd9Sstevel@tonic-gate out: 78647c478bd9Sstevel@tonic-gate if (hdl) 78657c478bd9Sstevel@tonic-gate librcm_free_handle(hdl); 78667c478bd9Sstevel@tonic-gate (void) dlclose(librcm_hdl); 78677c478bd9Sstevel@tonic-gate return (-1); 78687c478bd9Sstevel@tonic-gate } 78697c478bd9Sstevel@tonic-gate 78707c478bd9Sstevel@tonic-gate /* 78717c478bd9Sstevel@tonic-gate * Build an nvlist using the minor data. Pack it and add the packed nvlist 78727c478bd9Sstevel@tonic-gate * as a byte array to nv_list parameter. 78737c478bd9Sstevel@tonic-gate * Return 0 on success, errno on failure. 78747c478bd9Sstevel@tonic-gate */ 78757c478bd9Sstevel@tonic-gate static int 78767c478bd9Sstevel@tonic-gate add_minor_data_to_nvl(nvlist_t *nv_list, di_minor_t minor) 78777c478bd9Sstevel@tonic-gate { 78787c478bd9Sstevel@tonic-gate nvlist_t *nvl = NULL; 78797c478bd9Sstevel@tonic-gate int32_t minor_type; 78807c478bd9Sstevel@tonic-gate char *minor_name, *minor_node_type; 78817c478bd9Sstevel@tonic-gate int err; 78827c478bd9Sstevel@tonic-gate char *buf = NULL; 78837c478bd9Sstevel@tonic-gate size_t buflen = 0; 78847c478bd9Sstevel@tonic-gate 78857c478bd9Sstevel@tonic-gate if ((err = nvlist_alloc(&nvl, 0, 0)) != 0) 78867c478bd9Sstevel@tonic-gate return (err); 78877c478bd9Sstevel@tonic-gate 78887c478bd9Sstevel@tonic-gate minor_type = (int32_t)di_minor_type(minor); 78897c478bd9Sstevel@tonic-gate if ((err = nvlist_add_int32(nvl, RCM_NV_MINOR_TYPE, minor_type)) != 0) 78907c478bd9Sstevel@tonic-gate goto error; 78917c478bd9Sstevel@tonic-gate 78927c478bd9Sstevel@tonic-gate minor_name = di_minor_name(minor); 78937c478bd9Sstevel@tonic-gate if ((err = nvlist_add_string(nvl, RCM_NV_MINOR_NAME, minor_name)) != 0) 78947c478bd9Sstevel@tonic-gate goto error; 78957c478bd9Sstevel@tonic-gate 78967c478bd9Sstevel@tonic-gate if ((minor_node_type = di_minor_nodetype(minor)) == NULL) 78977c478bd9Sstevel@tonic-gate minor_node_type = ""; 78987c478bd9Sstevel@tonic-gate if ((err = nvlist_add_string(nvl, RCM_NV_MINOR_NODE_TYPE, 78997c478bd9Sstevel@tonic-gate minor_node_type)) != 0) 79007c478bd9Sstevel@tonic-gate goto error; 79017c478bd9Sstevel@tonic-gate 79027c478bd9Sstevel@tonic-gate if ((err = nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_NATIVE, 0)) != 0) 79037c478bd9Sstevel@tonic-gate goto error; 79047c478bd9Sstevel@tonic-gate 79057c478bd9Sstevel@tonic-gate err = nvlist_add_byte_array(nv_list, RCM_NV_MINOR_DATA, 79067c478bd9Sstevel@tonic-gate (uchar_t *)(buf), (uint_t)(buflen)); 79077c478bd9Sstevel@tonic-gate 79087c478bd9Sstevel@tonic-gate error: 79097c478bd9Sstevel@tonic-gate nvlist_free(nvl); 79107c478bd9Sstevel@tonic-gate if (buf) 79117c478bd9Sstevel@tonic-gate free(buf); 79127c478bd9Sstevel@tonic-gate return (err); 79137c478bd9Sstevel@tonic-gate } 79147c478bd9Sstevel@tonic-gate 79157c478bd9Sstevel@tonic-gate static void 79167c478bd9Sstevel@tonic-gate enqueue_rcm_event(nvlist_t *nvl) 79177c478bd9Sstevel@tonic-gate { 79187c478bd9Sstevel@tonic-gate struct rcm_eventq *ev; 79197c478bd9Sstevel@tonic-gate 79207c478bd9Sstevel@tonic-gate ev = (struct rcm_eventq *)s_zalloc(sizeof (struct rcm_eventq)); 79217c478bd9Sstevel@tonic-gate ev->nvl = nvl; 79227c478bd9Sstevel@tonic-gate 79237c478bd9Sstevel@tonic-gate (void) mutex_lock(&rcm_eventq_lock); 79247c478bd9Sstevel@tonic-gate if (rcm_eventq_head == NULL) 79257c478bd9Sstevel@tonic-gate rcm_eventq_head = ev; 79267c478bd9Sstevel@tonic-gate else 79277c478bd9Sstevel@tonic-gate rcm_eventq_tail->next = ev; 79287c478bd9Sstevel@tonic-gate rcm_eventq_tail = ev; 79297c478bd9Sstevel@tonic-gate (void) cond_broadcast(&rcm_eventq_cv); 79307c478bd9Sstevel@tonic-gate (void) mutex_unlock(&rcm_eventq_lock); 79317c478bd9Sstevel@tonic-gate } 79327c478bd9Sstevel@tonic-gate 79337c478bd9Sstevel@tonic-gate /* 79347c478bd9Sstevel@tonic-gate * Generate an nvlist using the information given in node and minor_name. 79357c478bd9Sstevel@tonic-gate * If minor_name is NULL the nvlist will contain information on 79367c478bd9Sstevel@tonic-gate * all minor nodes. Otherwise the nvlist will contain information 79377c478bd9Sstevel@tonic-gate * only on the given minor_name. Notify RCM passing the nvlist. 79387c478bd9Sstevel@tonic-gate * 79397c478bd9Sstevel@tonic-gate * Return 0 upon successfully notifying RCM, errno on failure. 79407c478bd9Sstevel@tonic-gate */ 79417c478bd9Sstevel@tonic-gate static int 79427c478bd9Sstevel@tonic-gate notify_rcm(di_node_t node, char *minor_name) 79437c478bd9Sstevel@tonic-gate { 79447c478bd9Sstevel@tonic-gate nvlist_t *nvl = NULL; 79457c478bd9Sstevel@tonic-gate char *path, *driver_name; 79467c478bd9Sstevel@tonic-gate char *node_name; 79477c478bd9Sstevel@tonic-gate int err; 79487c478bd9Sstevel@tonic-gate int32_t instance; 79497c478bd9Sstevel@tonic-gate di_minor_t minor; 79507c478bd9Sstevel@tonic-gate 79517c478bd9Sstevel@tonic-gate if ((driver_name = di_driver_name(node)) == NULL) 79527c478bd9Sstevel@tonic-gate driver_name = ""; 79537c478bd9Sstevel@tonic-gate 79547c478bd9Sstevel@tonic-gate instance = (int32_t)di_instance(node); 79557c478bd9Sstevel@tonic-gate 79567c478bd9Sstevel@tonic-gate if ((path = di_devfs_path(node)) == NULL) { 79577c478bd9Sstevel@tonic-gate err = errno; 79587c478bd9Sstevel@tonic-gate goto error; 79597c478bd9Sstevel@tonic-gate } 79607c478bd9Sstevel@tonic-gate 79617c478bd9Sstevel@tonic-gate if ((err = nvlist_alloc(&nvl, 0, 0)) != 0) 79627c478bd9Sstevel@tonic-gate goto error; 79637c478bd9Sstevel@tonic-gate 79647c478bd9Sstevel@tonic-gate if ((err = nvlist_add_string(nvl, RCM_NV_DRIVER_NAME, driver_name)) 79657c478bd9Sstevel@tonic-gate != 0) 79667c478bd9Sstevel@tonic-gate goto error; 79677c478bd9Sstevel@tonic-gate 79687c478bd9Sstevel@tonic-gate if ((err = nvlist_add_int32(nvl, RCM_NV_INSTANCE, instance)) != 0) 79697c478bd9Sstevel@tonic-gate goto error; 79707c478bd9Sstevel@tonic-gate 79717c478bd9Sstevel@tonic-gate if ((node_name = di_node_name(node)) == NULL) 79727c478bd9Sstevel@tonic-gate node_name = ""; 79737c478bd9Sstevel@tonic-gate if ((err = nvlist_add_string(nvl, RCM_NV_NODE_NAME, node_name)) != 0) 79747c478bd9Sstevel@tonic-gate goto error; 79757c478bd9Sstevel@tonic-gate 79767c478bd9Sstevel@tonic-gate if ((err = nvlist_add_string(nvl, RCM_NV_DEVFS_PATH, path)) != 0) 79777c478bd9Sstevel@tonic-gate goto error; 79787c478bd9Sstevel@tonic-gate 79797c478bd9Sstevel@tonic-gate minor = di_minor_next(node, DI_MINOR_NIL); 79807c478bd9Sstevel@tonic-gate while (minor != DI_MINOR_NIL) { 79817c478bd9Sstevel@tonic-gate if ((minor_name == NULL) || 79827c478bd9Sstevel@tonic-gate (strcmp(minor_name, di_minor_name(minor)) == 0)) { 79837c478bd9Sstevel@tonic-gate if ((err = add_minor_data_to_nvl(nvl, minor)) != 0) 79847c478bd9Sstevel@tonic-gate goto error; 79857c478bd9Sstevel@tonic-gate } 79867c478bd9Sstevel@tonic-gate minor = di_minor_next(node, minor); 79877c478bd9Sstevel@tonic-gate } 79887c478bd9Sstevel@tonic-gate 79897c478bd9Sstevel@tonic-gate enqueue_rcm_event(nvl); 79907c478bd9Sstevel@tonic-gate di_devfs_path_free(path); 79917c478bd9Sstevel@tonic-gate return (0); 79927c478bd9Sstevel@tonic-gate 79937c478bd9Sstevel@tonic-gate error: 79947c478bd9Sstevel@tonic-gate err_print(RCM_NVLIST_BUILD_ERROR, ((path != NULL) ? path : "unknown"), 79957c478bd9Sstevel@tonic-gate driver_name, instance, strerror(err)); 79967c478bd9Sstevel@tonic-gate 79977c478bd9Sstevel@tonic-gate if (path) 79987c478bd9Sstevel@tonic-gate di_devfs_path_free(path); 79997c478bd9Sstevel@tonic-gate if (nvl) 80007c478bd9Sstevel@tonic-gate nvlist_free(nvl); 80017c478bd9Sstevel@tonic-gate return (err); 80027c478bd9Sstevel@tonic-gate } 80037c478bd9Sstevel@tonic-gate 80047c478bd9Sstevel@tonic-gate /* 80057c478bd9Sstevel@tonic-gate * Add the specified property to nvl. 80067c478bd9Sstevel@tonic-gate * Returns: 80077c478bd9Sstevel@tonic-gate * 0 successfully added 80087c478bd9Sstevel@tonic-gate * -1 an error occurred 80097c478bd9Sstevel@tonic-gate * 1 could not add the property for reasons not due to errors. 80107c478bd9Sstevel@tonic-gate */ 80117c478bd9Sstevel@tonic-gate static int 80127c478bd9Sstevel@tonic-gate add_property(nvlist_t *nvl, di_prop_t prop) 80137c478bd9Sstevel@tonic-gate { 80147c478bd9Sstevel@tonic-gate char *name; 80157c478bd9Sstevel@tonic-gate char *attr_name; 80167c478bd9Sstevel@tonic-gate int n, len; 80177c478bd9Sstevel@tonic-gate int32_t *int32p; 80187c478bd9Sstevel@tonic-gate int64_t *int64p; 80197c478bd9Sstevel@tonic-gate char *str; 80207c478bd9Sstevel@tonic-gate char **strarray; 80217c478bd9Sstevel@tonic-gate uchar_t *bytep; 80227c478bd9Sstevel@tonic-gate int rv = 0; 80237c478bd9Sstevel@tonic-gate int i; 80247c478bd9Sstevel@tonic-gate 80257c478bd9Sstevel@tonic-gate if ((name = di_prop_name(prop)) == NULL) 80267c478bd9Sstevel@tonic-gate return (-1); 80277c478bd9Sstevel@tonic-gate 80287c478bd9Sstevel@tonic-gate len = sizeof (DEV_PROP_PREFIX) + strlen(name); 80297c478bd9Sstevel@tonic-gate if ((attr_name = malloc(len)) == NULL) 80307c478bd9Sstevel@tonic-gate return (-1); 80317c478bd9Sstevel@tonic-gate 80327c478bd9Sstevel@tonic-gate (void) strlcpy(attr_name, DEV_PROP_PREFIX, len); 80337c478bd9Sstevel@tonic-gate (void) strlcat(attr_name, name, len); 80347c478bd9Sstevel@tonic-gate 80357c478bd9Sstevel@tonic-gate switch (di_prop_type(prop)) { 80367c478bd9Sstevel@tonic-gate case DI_PROP_TYPE_BOOLEAN: 80377c478bd9Sstevel@tonic-gate if (nvlist_add_boolean(nvl, attr_name) != 0) 80387c478bd9Sstevel@tonic-gate goto out; 80397c478bd9Sstevel@tonic-gate break; 80407c478bd9Sstevel@tonic-gate 80417c478bd9Sstevel@tonic-gate case DI_PROP_TYPE_INT: 80427c478bd9Sstevel@tonic-gate if ((n = di_prop_ints(prop, &int32p)) < 1) 80437c478bd9Sstevel@tonic-gate goto out; 80447c478bd9Sstevel@tonic-gate 80457c478bd9Sstevel@tonic-gate if (n <= (PROP_LEN_LIMIT / sizeof (int32_t))) { 80467c478bd9Sstevel@tonic-gate if (nvlist_add_int32_array(nvl, attr_name, int32p, 80477c478bd9Sstevel@tonic-gate n) != 0) 80487c478bd9Sstevel@tonic-gate goto out; 80497c478bd9Sstevel@tonic-gate } else 80507c478bd9Sstevel@tonic-gate rv = 1; 80517c478bd9Sstevel@tonic-gate break; 80527c478bd9Sstevel@tonic-gate 80537c478bd9Sstevel@tonic-gate case DI_PROP_TYPE_INT64: 80547c478bd9Sstevel@tonic-gate if ((n = di_prop_int64(prop, &int64p)) < 1) 80557c478bd9Sstevel@tonic-gate goto out; 80567c478bd9Sstevel@tonic-gate 80577c478bd9Sstevel@tonic-gate if (n <= (PROP_LEN_LIMIT / sizeof (int64_t))) { 80587c478bd9Sstevel@tonic-gate if (nvlist_add_int64_array(nvl, attr_name, int64p, 80597c478bd9Sstevel@tonic-gate n) != 0) 80607c478bd9Sstevel@tonic-gate goto out; 80617c478bd9Sstevel@tonic-gate } else 80627c478bd9Sstevel@tonic-gate rv = 1; 80637c478bd9Sstevel@tonic-gate break; 80647c478bd9Sstevel@tonic-gate 80657c478bd9Sstevel@tonic-gate case DI_PROP_TYPE_BYTE: 80667c478bd9Sstevel@tonic-gate case DI_PROP_TYPE_UNKNOWN: 80677c478bd9Sstevel@tonic-gate if ((n = di_prop_bytes(prop, &bytep)) < 1) 80687c478bd9Sstevel@tonic-gate goto out; 80697c478bd9Sstevel@tonic-gate 80707c478bd9Sstevel@tonic-gate if (n <= PROP_LEN_LIMIT) { 80717c478bd9Sstevel@tonic-gate if (nvlist_add_byte_array(nvl, attr_name, bytep, n) 80727c478bd9Sstevel@tonic-gate != 0) 80737c478bd9Sstevel@tonic-gate goto out; 80747c478bd9Sstevel@tonic-gate } else 80757c478bd9Sstevel@tonic-gate rv = 1; 80767c478bd9Sstevel@tonic-gate break; 80777c478bd9Sstevel@tonic-gate 80787c478bd9Sstevel@tonic-gate case DI_PROP_TYPE_STRING: 80797c478bd9Sstevel@tonic-gate if ((n = di_prop_strings(prop, &str)) < 1) 80807c478bd9Sstevel@tonic-gate goto out; 80817c478bd9Sstevel@tonic-gate 80827c478bd9Sstevel@tonic-gate if ((strarray = malloc(n * sizeof (char *))) == NULL) 80837c478bd9Sstevel@tonic-gate goto out; 80847c478bd9Sstevel@tonic-gate 80857c478bd9Sstevel@tonic-gate len = 0; 80867c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++) { 80877c478bd9Sstevel@tonic-gate strarray[i] = str + len; 80887c478bd9Sstevel@tonic-gate len += strlen(strarray[i]) + 1; 80897c478bd9Sstevel@tonic-gate } 80907c478bd9Sstevel@tonic-gate 80917c478bd9Sstevel@tonic-gate if (len <= PROP_LEN_LIMIT) { 80927c478bd9Sstevel@tonic-gate if (nvlist_add_string_array(nvl, attr_name, strarray, 80937c478bd9Sstevel@tonic-gate n) != 0) { 80947c478bd9Sstevel@tonic-gate free(strarray); 80957c478bd9Sstevel@tonic-gate goto out; 80967c478bd9Sstevel@tonic-gate } 80977c478bd9Sstevel@tonic-gate } else 80987c478bd9Sstevel@tonic-gate rv = 1; 80997c478bd9Sstevel@tonic-gate free(strarray); 81007c478bd9Sstevel@tonic-gate break; 81017c478bd9Sstevel@tonic-gate 81027c478bd9Sstevel@tonic-gate default: 81037c478bd9Sstevel@tonic-gate rv = 1; 81047c478bd9Sstevel@tonic-gate break; 81057c478bd9Sstevel@tonic-gate } 81067c478bd9Sstevel@tonic-gate 81077c478bd9Sstevel@tonic-gate free(attr_name); 81087c478bd9Sstevel@tonic-gate return (rv); 81097c478bd9Sstevel@tonic-gate 81107c478bd9Sstevel@tonic-gate out: 81117c478bd9Sstevel@tonic-gate free(attr_name); 81127c478bd9Sstevel@tonic-gate return (-1); 81137c478bd9Sstevel@tonic-gate } 81147c478bd9Sstevel@tonic-gate 81157c478bd9Sstevel@tonic-gate static void 81167c478bd9Sstevel@tonic-gate free_dev_names(struct devlink_cb_arg *x) 81177c478bd9Sstevel@tonic-gate { 81187c478bd9Sstevel@tonic-gate int i; 81197c478bd9Sstevel@tonic-gate 81207c478bd9Sstevel@tonic-gate for (i = 0; i < x->count; i++) { 81217c478bd9Sstevel@tonic-gate free(x->dev_names[i]); 81227c478bd9Sstevel@tonic-gate free(x->link_contents[i]); 81237c478bd9Sstevel@tonic-gate } 81247c478bd9Sstevel@tonic-gate } 81257c478bd9Sstevel@tonic-gate 81267c478bd9Sstevel@tonic-gate /* callback function for di_devlink_cache_walk */ 81277c478bd9Sstevel@tonic-gate static int 81287c478bd9Sstevel@tonic-gate devlink_cb(di_devlink_t dl, void *arg) 81297c478bd9Sstevel@tonic-gate { 81307c478bd9Sstevel@tonic-gate struct devlink_cb_arg *x = (struct devlink_cb_arg *)arg; 81317c478bd9Sstevel@tonic-gate const char *path; 81327c478bd9Sstevel@tonic-gate const char *content; 81337c478bd9Sstevel@tonic-gate 81347c478bd9Sstevel@tonic-gate if ((path = di_devlink_path(dl)) == NULL || 81357c478bd9Sstevel@tonic-gate (content = di_devlink_content(dl)) == NULL || 813612d8cf2aSjg (x->dev_names[x->count] = s_strdup(path)) == NULL) 81377c478bd9Sstevel@tonic-gate goto out; 81387c478bd9Sstevel@tonic-gate 813912d8cf2aSjg if ((x->link_contents[x->count] = s_strdup(content)) == NULL) { 81407c478bd9Sstevel@tonic-gate free(x->dev_names[x->count]); 81417c478bd9Sstevel@tonic-gate goto out; 81427c478bd9Sstevel@tonic-gate } 81437c478bd9Sstevel@tonic-gate 81447c478bd9Sstevel@tonic-gate x->count++; 81457c478bd9Sstevel@tonic-gate if (x->count >= MAX_DEV_NAME_COUNT) 81467c478bd9Sstevel@tonic-gate return (DI_WALK_TERMINATE); 81477c478bd9Sstevel@tonic-gate 81487c478bd9Sstevel@tonic-gate return (DI_WALK_CONTINUE); 81497c478bd9Sstevel@tonic-gate 81507c478bd9Sstevel@tonic-gate out: 81517c478bd9Sstevel@tonic-gate x->rv = -1; 81527c478bd9Sstevel@tonic-gate free_dev_names(x); 81537c478bd9Sstevel@tonic-gate return (DI_WALK_TERMINATE); 81547c478bd9Sstevel@tonic-gate } 81557c478bd9Sstevel@tonic-gate 81567c478bd9Sstevel@tonic-gate /* 81577c478bd9Sstevel@tonic-gate * Lookup dev name corresponding to the phys_path. 81587c478bd9Sstevel@tonic-gate * phys_path is path to a node or minor node. 81597c478bd9Sstevel@tonic-gate * Returns: 81607c478bd9Sstevel@tonic-gate * 0 with *dev_name set to the dev name 81617c478bd9Sstevel@tonic-gate * Lookup succeeded and dev_name found 81627c478bd9Sstevel@tonic-gate * 0 with *dev_name set to NULL 81637c478bd9Sstevel@tonic-gate * Lookup encountered no errors but dev name not found 81647c478bd9Sstevel@tonic-gate * -1 81657c478bd9Sstevel@tonic-gate * Lookup failed 81667c478bd9Sstevel@tonic-gate */ 81677c478bd9Sstevel@tonic-gate static int 81687c478bd9Sstevel@tonic-gate lookup_dev_name(char *phys_path, char **dev_name) 81697c478bd9Sstevel@tonic-gate { 81707c478bd9Sstevel@tonic-gate struct devlink_cb_arg cb_arg; 81717c478bd9Sstevel@tonic-gate 81727c478bd9Sstevel@tonic-gate *dev_name = NULL; 81737c478bd9Sstevel@tonic-gate 81747c478bd9Sstevel@tonic-gate cb_arg.count = 0; 81757c478bd9Sstevel@tonic-gate cb_arg.rv = 0; 81767c478bd9Sstevel@tonic-gate (void) di_devlink_cache_walk(devlink_cache, NULL, phys_path, 81777c478bd9Sstevel@tonic-gate DI_PRIMARY_LINK, &cb_arg, devlink_cb); 81787c478bd9Sstevel@tonic-gate 81797c478bd9Sstevel@tonic-gate if (cb_arg.rv == -1) 81807c478bd9Sstevel@tonic-gate return (-1); 81817c478bd9Sstevel@tonic-gate 81827c478bd9Sstevel@tonic-gate if (cb_arg.count > 0) { 818312d8cf2aSjg *dev_name = s_strdup(cb_arg.dev_names[0]); 81847c478bd9Sstevel@tonic-gate free_dev_names(&cb_arg); 81857c478bd9Sstevel@tonic-gate if (*dev_name == NULL) 81867c478bd9Sstevel@tonic-gate return (-1); 81877c478bd9Sstevel@tonic-gate } 81887c478bd9Sstevel@tonic-gate 81897c478bd9Sstevel@tonic-gate return (0); 81907c478bd9Sstevel@tonic-gate } 81917c478bd9Sstevel@tonic-gate 81927c478bd9Sstevel@tonic-gate static char * 81937c478bd9Sstevel@tonic-gate lookup_disk_dev_name(char *node_path) 81947c478bd9Sstevel@tonic-gate { 81957c478bd9Sstevel@tonic-gate struct devlink_cb_arg cb_arg; 81967c478bd9Sstevel@tonic-gate char *dev_name = NULL; 81977c478bd9Sstevel@tonic-gate int i; 81987c478bd9Sstevel@tonic-gate char *p; 81997c478bd9Sstevel@tonic-gate int len1, len2; 82007c478bd9Sstevel@tonic-gate 82017c478bd9Sstevel@tonic-gate #define DEV_RDSK "/dev/rdsk/" 82027c478bd9Sstevel@tonic-gate #define DISK_RAW_MINOR ",raw" 82037c478bd9Sstevel@tonic-gate 82047c478bd9Sstevel@tonic-gate cb_arg.count = 0; 82057c478bd9Sstevel@tonic-gate cb_arg.rv = 0; 82067c478bd9Sstevel@tonic-gate (void) di_devlink_cache_walk(devlink_cache, NULL, node_path, 82077c478bd9Sstevel@tonic-gate DI_PRIMARY_LINK, &cb_arg, devlink_cb); 82087c478bd9Sstevel@tonic-gate 82097c478bd9Sstevel@tonic-gate if (cb_arg.rv == -1 || cb_arg.count == 0) 82107c478bd9Sstevel@tonic-gate return (NULL); 82117c478bd9Sstevel@tonic-gate 82127c478bd9Sstevel@tonic-gate /* first try lookup based on /dev/rdsk name */ 82137c478bd9Sstevel@tonic-gate for (i = 0; i < cb_arg.count; i++) { 82147c478bd9Sstevel@tonic-gate if (strncmp(cb_arg.dev_names[i], DEV_RDSK, 82157c478bd9Sstevel@tonic-gate sizeof (DEV_RDSK) - 1) == 0) { 821612d8cf2aSjg dev_name = s_strdup(cb_arg.dev_names[i]); 82177c478bd9Sstevel@tonic-gate break; 82187c478bd9Sstevel@tonic-gate } 82197c478bd9Sstevel@tonic-gate } 82207c478bd9Sstevel@tonic-gate 82217c478bd9Sstevel@tonic-gate if (dev_name == NULL) { 82227c478bd9Sstevel@tonic-gate /* now try lookup based on a minor name ending with ",raw" */ 82237c478bd9Sstevel@tonic-gate len1 = sizeof (DISK_RAW_MINOR) - 1; 82247c478bd9Sstevel@tonic-gate for (i = 0; i < cb_arg.count; i++) { 82257c478bd9Sstevel@tonic-gate len2 = strlen(cb_arg.link_contents[i]); 82267c478bd9Sstevel@tonic-gate if (len2 >= len1 && 82277c478bd9Sstevel@tonic-gate strcmp(cb_arg.link_contents[i] + len2 - len1, 82287c478bd9Sstevel@tonic-gate DISK_RAW_MINOR) == 0) { 822912d8cf2aSjg dev_name = s_strdup(cb_arg.dev_names[i]); 82307c478bd9Sstevel@tonic-gate break; 82317c478bd9Sstevel@tonic-gate } 82327c478bd9Sstevel@tonic-gate } 82337c478bd9Sstevel@tonic-gate } 82347c478bd9Sstevel@tonic-gate 82357c478bd9Sstevel@tonic-gate free_dev_names(&cb_arg); 82367c478bd9Sstevel@tonic-gate 823712d8cf2aSjg if (dev_name == NULL) 823812d8cf2aSjg return (NULL); 82397c478bd9Sstevel@tonic-gate if (strlen(dev_name) == 0) { 82407c478bd9Sstevel@tonic-gate free(dev_name); 82417c478bd9Sstevel@tonic-gate return (NULL); 82427c478bd9Sstevel@tonic-gate } 82437c478bd9Sstevel@tonic-gate 82447c478bd9Sstevel@tonic-gate /* if the name contains slice or partition number strip it */ 82457c478bd9Sstevel@tonic-gate p = dev_name + strlen(dev_name) - 1; 82467c478bd9Sstevel@tonic-gate if (isdigit(*p)) { 82477c478bd9Sstevel@tonic-gate while (p != dev_name && isdigit(*p)) 82487c478bd9Sstevel@tonic-gate p--; 82497c478bd9Sstevel@tonic-gate if (*p == 's' || *p == 'p') 82507c478bd9Sstevel@tonic-gate *p = '\0'; 82517c478bd9Sstevel@tonic-gate } 82527c478bd9Sstevel@tonic-gate 82537c478bd9Sstevel@tonic-gate return (dev_name); 82547c478bd9Sstevel@tonic-gate } 82557c478bd9Sstevel@tonic-gate 82567c478bd9Sstevel@tonic-gate static char * 825730294554Sphitran lookup_lofi_dev_name(char *node_path, char *minor) 825830294554Sphitran { 825930294554Sphitran struct devlink_cb_arg cb_arg; 826030294554Sphitran char *dev_name = NULL; 826130294554Sphitran int i; 826230294554Sphitran int len1, len2; 826330294554Sphitran 826430294554Sphitran cb_arg.count = 0; 826530294554Sphitran cb_arg.rv = 0; 826630294554Sphitran (void) di_devlink_cache_walk(devlink_cache, NULL, node_path, 826730294554Sphitran DI_PRIMARY_LINK, &cb_arg, devlink_cb); 826830294554Sphitran 826930294554Sphitran if (cb_arg.rv == -1 || cb_arg.count == 0) 827030294554Sphitran return (NULL); 827130294554Sphitran 827230294554Sphitran /* lookup based on a minor name ending with ",raw" */ 827330294554Sphitran len1 = strlen(minor); 827430294554Sphitran for (i = 0; i < cb_arg.count; i++) { 827530294554Sphitran len2 = strlen(cb_arg.link_contents[i]); 827630294554Sphitran if (len2 >= len1 && 827730294554Sphitran strcmp(cb_arg.link_contents[i] + len2 - len1, 827830294554Sphitran minor) == 0) { 827930294554Sphitran dev_name = s_strdup(cb_arg.dev_names[i]); 828030294554Sphitran break; 828130294554Sphitran } 828230294554Sphitran } 828330294554Sphitran 828430294554Sphitran free_dev_names(&cb_arg); 828530294554Sphitran 828630294554Sphitran if (dev_name == NULL) 828730294554Sphitran return (NULL); 828830294554Sphitran if (strlen(dev_name) == 0) { 828930294554Sphitran free(dev_name); 829030294554Sphitran return (NULL); 829130294554Sphitran } 829230294554Sphitran 829330294554Sphitran return (dev_name); 829430294554Sphitran } 829530294554Sphitran 829630294554Sphitran static char * 82977c478bd9Sstevel@tonic-gate lookup_network_dev_name(char *node_path, char *driver_name) 82987c478bd9Sstevel@tonic-gate { 82997c478bd9Sstevel@tonic-gate char *dev_name = NULL; 83007c478bd9Sstevel@tonic-gate char phys_path[MAXPATHLEN]; 83017c478bd9Sstevel@tonic-gate 83027c478bd9Sstevel@tonic-gate if (lookup_dev_name(node_path, &dev_name) == -1) 83037c478bd9Sstevel@tonic-gate return (NULL); 83047c478bd9Sstevel@tonic-gate 83057c478bd9Sstevel@tonic-gate if (dev_name == NULL) { 83067c478bd9Sstevel@tonic-gate /* dlpi style-2 only interface */ 83077c478bd9Sstevel@tonic-gate (void) snprintf(phys_path, sizeof (phys_path), 83087c478bd9Sstevel@tonic-gate "/pseudo/clone@0:%s", driver_name); 83097c478bd9Sstevel@tonic-gate if (lookup_dev_name(phys_path, &dev_name) == -1 || 83107c478bd9Sstevel@tonic-gate dev_name == NULL) 83117c478bd9Sstevel@tonic-gate return (NULL); 83127c478bd9Sstevel@tonic-gate } 83137c478bd9Sstevel@tonic-gate 83147c478bd9Sstevel@tonic-gate return (dev_name); 83157c478bd9Sstevel@tonic-gate } 83167c478bd9Sstevel@tonic-gate 8317db11e6feSjacobs static char * 8318db11e6feSjacobs lookup_printer_dev_name(char *node_path) 8319db11e6feSjacobs { 8320db11e6feSjacobs struct devlink_cb_arg cb_arg; 8321db11e6feSjacobs char *dev_name = NULL; 8322db11e6feSjacobs int i; 8323db11e6feSjacobs 8324db11e6feSjacobs #define DEV_PRINTERS "/dev/printers/" 8325db11e6feSjacobs 8326db11e6feSjacobs cb_arg.count = 0; 8327db11e6feSjacobs cb_arg.rv = 0; 8328db11e6feSjacobs (void) di_devlink_cache_walk(devlink_cache, NULL, node_path, 8329db11e6feSjacobs DI_PRIMARY_LINK, &cb_arg, devlink_cb); 8330db11e6feSjacobs 8331db11e6feSjacobs if (cb_arg.rv == -1 || cb_arg.count == 0) 8332db11e6feSjacobs return (NULL); 8333db11e6feSjacobs 8334db11e6feSjacobs /* first try lookup based on /dev/printers name */ 8335db11e6feSjacobs for (i = 0; i < cb_arg.count; i++) { 8336db11e6feSjacobs if (strncmp(cb_arg.dev_names[i], DEV_PRINTERS, 8337db11e6feSjacobs sizeof (DEV_PRINTERS) - 1) == 0) { 8338db11e6feSjacobs dev_name = s_strdup(cb_arg.dev_names[i]); 8339db11e6feSjacobs break; 8340db11e6feSjacobs } 8341db11e6feSjacobs } 8342db11e6feSjacobs 8343db11e6feSjacobs /* fallback to the first name */ 8344db11e6feSjacobs if ((dev_name == NULL) && (cb_arg.count > 0)) 8345db11e6feSjacobs dev_name = s_strdup(cb_arg.dev_names[0]); 8346db11e6feSjacobs 8347db11e6feSjacobs free_dev_names(&cb_arg); 8348db11e6feSjacobs 8349db11e6feSjacobs return (dev_name); 8350db11e6feSjacobs } 8351db11e6feSjacobs 83527c478bd9Sstevel@tonic-gate /* 83537c478bd9Sstevel@tonic-gate * Build an nvlist containing all attributes for devfs events. 83547c478bd9Sstevel@tonic-gate * Returns nvlist pointer on success, NULL on failure. 83557c478bd9Sstevel@tonic-gate */ 83567c478bd9Sstevel@tonic-gate static nvlist_t * 83577c478bd9Sstevel@tonic-gate build_event_attributes(char *class, char *subclass, char *node_path, 835830294554Sphitran di_node_t node, char *driver_name, int instance, char *minor) 83597c478bd9Sstevel@tonic-gate { 83607c478bd9Sstevel@tonic-gate nvlist_t *nvl; 83617c478bd9Sstevel@tonic-gate int err = 0; 83627c478bd9Sstevel@tonic-gate di_prop_t prop; 83637c478bd9Sstevel@tonic-gate int count; 83647c478bd9Sstevel@tonic-gate char *prop_name; 83657c478bd9Sstevel@tonic-gate int x; 83667c478bd9Sstevel@tonic-gate char *dev_name = NULL; 83677c478bd9Sstevel@tonic-gate int dev_name_lookup_err = 0; 83687c478bd9Sstevel@tonic-gate 83697c478bd9Sstevel@tonic-gate if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, 0)) != 0) { 83707c478bd9Sstevel@tonic-gate nvl = NULL; 83717c478bd9Sstevel@tonic-gate goto out; 83727c478bd9Sstevel@tonic-gate } 83737c478bd9Sstevel@tonic-gate 83747c478bd9Sstevel@tonic-gate if ((err = nvlist_add_int32(nvl, EV_VERSION, EV_V1)) != 0) 83757c478bd9Sstevel@tonic-gate goto out; 83767c478bd9Sstevel@tonic-gate 83777c478bd9Sstevel@tonic-gate if ((err = nvlist_add_string(nvl, DEV_PHYS_PATH, node_path)) != 0) 83787c478bd9Sstevel@tonic-gate goto out; 83797c478bd9Sstevel@tonic-gate 83807c478bd9Sstevel@tonic-gate if (strcmp(class, EC_DEV_ADD) != 0 && 83817c478bd9Sstevel@tonic-gate strcmp(class, EC_DEV_REMOVE) != 0) 83827c478bd9Sstevel@tonic-gate return (nvl); 83837c478bd9Sstevel@tonic-gate 83847c478bd9Sstevel@tonic-gate if (driver_name == NULL || instance == -1) 83857c478bd9Sstevel@tonic-gate goto out; 83867c478bd9Sstevel@tonic-gate 83877c478bd9Sstevel@tonic-gate if (strcmp(subclass, ESC_DISK) == 0) { 83887c478bd9Sstevel@tonic-gate if ((dev_name = lookup_disk_dev_name(node_path)) == NULL) { 83897c478bd9Sstevel@tonic-gate dev_name_lookup_err = 1; 83907c478bd9Sstevel@tonic-gate goto out; 83917c478bd9Sstevel@tonic-gate } 83927c478bd9Sstevel@tonic-gate } else if (strcmp(subclass, ESC_NETWORK) == 0) { 83937c478bd9Sstevel@tonic-gate if ((dev_name = lookup_network_dev_name(node_path, driver_name)) 83947c478bd9Sstevel@tonic-gate == NULL) { 83957c478bd9Sstevel@tonic-gate dev_name_lookup_err = 1; 83967c478bd9Sstevel@tonic-gate goto out; 83977c478bd9Sstevel@tonic-gate } 8398db11e6feSjacobs } else if (strcmp(subclass, ESC_PRINTER) == 0) { 8399db11e6feSjacobs if ((dev_name = lookup_printer_dev_name(node_path)) == NULL) { 8400db11e6feSjacobs dev_name_lookup_err = 1; 8401db11e6feSjacobs goto out; 8402db11e6feSjacobs } 840330294554Sphitran } else if (strcmp(subclass, ESC_LOFI) == 0) { 840430294554Sphitran /* 840530294554Sphitran * The raw minor node is created or removed after the block 840630294554Sphitran * node. Lofi devfs events are dependent on this behavior. 840730294554Sphitran * Generate the sysevent only for the raw minor node. 840830294554Sphitran */ 840930294554Sphitran if (strstr(minor, "raw") == NULL) { 841030294554Sphitran if (nvl) { 841130294554Sphitran nvlist_free(nvl); 841230294554Sphitran } 841330294554Sphitran return (NULL); 841430294554Sphitran } 841530294554Sphitran if ((dev_name = lookup_lofi_dev_name(node_path, minor)) == 841630294554Sphitran NULL) { 841730294554Sphitran dev_name_lookup_err = 1; 841830294554Sphitran goto out; 841930294554Sphitran } 84207c478bd9Sstevel@tonic-gate } 84217c478bd9Sstevel@tonic-gate 84227c478bd9Sstevel@tonic-gate if (dev_name) { 84237c478bd9Sstevel@tonic-gate if ((err = nvlist_add_string(nvl, DEV_NAME, dev_name)) != 0) 84247c478bd9Sstevel@tonic-gate goto out; 84257c478bd9Sstevel@tonic-gate free(dev_name); 84267c478bd9Sstevel@tonic-gate dev_name = NULL; 84277c478bd9Sstevel@tonic-gate } 84287c478bd9Sstevel@tonic-gate 84297c478bd9Sstevel@tonic-gate if ((err = nvlist_add_string(nvl, DEV_DRIVER_NAME, driver_name)) != 0) 84307c478bd9Sstevel@tonic-gate goto out; 84317c478bd9Sstevel@tonic-gate 84327c478bd9Sstevel@tonic-gate if ((err = nvlist_add_int32(nvl, DEV_INSTANCE, instance)) != 0) 84337c478bd9Sstevel@tonic-gate goto out; 84347c478bd9Sstevel@tonic-gate 84357c478bd9Sstevel@tonic-gate if (strcmp(class, EC_DEV_ADD) == 0) { 84367c478bd9Sstevel@tonic-gate /* add properties */ 84377c478bd9Sstevel@tonic-gate count = 0; 84387c478bd9Sstevel@tonic-gate for (prop = di_prop_next(node, DI_PROP_NIL); 84397c478bd9Sstevel@tonic-gate prop != DI_PROP_NIL && count < MAX_PROP_COUNT; 84407c478bd9Sstevel@tonic-gate prop = di_prop_next(node, prop)) { 84417c478bd9Sstevel@tonic-gate 84427c478bd9Sstevel@tonic-gate if (di_prop_devt(prop) != DDI_DEV_T_NONE) 84437c478bd9Sstevel@tonic-gate continue; 84447c478bd9Sstevel@tonic-gate 84457c478bd9Sstevel@tonic-gate if ((x = add_property(nvl, prop)) == 0) 84467c478bd9Sstevel@tonic-gate count++; 84477c478bd9Sstevel@tonic-gate else if (x == -1) { 84487c478bd9Sstevel@tonic-gate if ((prop_name = di_prop_name(prop)) == NULL) 84497c478bd9Sstevel@tonic-gate prop_name = ""; 84507c478bd9Sstevel@tonic-gate err_print(PROP_ADD_FAILED, prop_name); 84517c478bd9Sstevel@tonic-gate goto out; 84527c478bd9Sstevel@tonic-gate } 84537c478bd9Sstevel@tonic-gate } 84547c478bd9Sstevel@tonic-gate } 84557c478bd9Sstevel@tonic-gate 84567c478bd9Sstevel@tonic-gate return (nvl); 84577c478bd9Sstevel@tonic-gate 84587c478bd9Sstevel@tonic-gate out: 84597c478bd9Sstevel@tonic-gate if (nvl) 84607c478bd9Sstevel@tonic-gate nvlist_free(nvl); 84617c478bd9Sstevel@tonic-gate 84627c478bd9Sstevel@tonic-gate if (dev_name) 84637c478bd9Sstevel@tonic-gate free(dev_name); 84647c478bd9Sstevel@tonic-gate 84657c478bd9Sstevel@tonic-gate if (dev_name_lookup_err) 84667c478bd9Sstevel@tonic-gate err_print(DEV_NAME_LOOKUP_FAILED, node_path); 84677c478bd9Sstevel@tonic-gate else 84687c478bd9Sstevel@tonic-gate err_print(BUILD_EVENT_ATTR_FAILED, (err) ? strerror(err) : ""); 84697c478bd9Sstevel@tonic-gate return (NULL); 84707c478bd9Sstevel@tonic-gate } 84717c478bd9Sstevel@tonic-gate 84727c478bd9Sstevel@tonic-gate static void 84737c478bd9Sstevel@tonic-gate log_event(char *class, char *subclass, nvlist_t *nvl) 84747c478bd9Sstevel@tonic-gate { 84757c478bd9Sstevel@tonic-gate sysevent_id_t eid; 84767c478bd9Sstevel@tonic-gate 84777c478bd9Sstevel@tonic-gate if (sysevent_post_event(class, subclass, "SUNW", DEVFSADMD, 84787c478bd9Sstevel@tonic-gate nvl, &eid) != 0) { 84797c478bd9Sstevel@tonic-gate err_print(LOG_EVENT_FAILED, strerror(errno)); 84807c478bd9Sstevel@tonic-gate } 84817c478bd9Sstevel@tonic-gate } 84827c478bd9Sstevel@tonic-gate 8483f05faa4eSjacobs /* 8484f05faa4eSjacobs * When devfsadmd needs to generate sysevents, they are queued for later 8485f05faa4eSjacobs * delivery this allows them to be delivered after the devlinks db cache has 8486f05faa4eSjacobs * been flushed guaranteeing that applications consuming these events have 8487f05faa4eSjacobs * access to an accurate devlinks db. The queue is a FIFO, sysevents to be 8488f05faa4eSjacobs * inserted in the front of the queue and consumed off the back. 8489f05faa4eSjacobs */ 84907c478bd9Sstevel@tonic-gate static void 8491f05faa4eSjacobs enqueue_sysevent(char *class, char *subclass, nvlist_t *nvl) 8492f05faa4eSjacobs { 8493f05faa4eSjacobs syseventq_t *tmp; 8494f05faa4eSjacobs 8495f05faa4eSjacobs if ((tmp = s_zalloc(sizeof (*tmp))) == NULL) 8496f05faa4eSjacobs return; 8497f05faa4eSjacobs 8498f05faa4eSjacobs tmp->class = s_strdup(class); 8499f05faa4eSjacobs tmp->subclass = s_strdup(subclass); 8500f05faa4eSjacobs tmp->nvl = nvl; 8501f05faa4eSjacobs 8502f05faa4eSjacobs (void) mutex_lock(&syseventq_mutex); 8503f05faa4eSjacobs if (syseventq_front != NULL) 8504f05faa4eSjacobs syseventq_front->next = tmp; 8505f05faa4eSjacobs else 8506f05faa4eSjacobs syseventq_back = tmp; 8507f05faa4eSjacobs syseventq_front = tmp; 8508f05faa4eSjacobs (void) mutex_unlock(&syseventq_mutex); 8509f05faa4eSjacobs } 8510f05faa4eSjacobs 8511f05faa4eSjacobs static void 8512f05faa4eSjacobs process_syseventq() 8513f05faa4eSjacobs { 8514f05faa4eSjacobs (void) mutex_lock(&syseventq_mutex); 8515f05faa4eSjacobs while (syseventq_back != NULL) { 8516f05faa4eSjacobs syseventq_t *tmp = syseventq_back; 8517f05faa4eSjacobs 8518f05faa4eSjacobs vprint(CHATTY_MID, "sending queued event: %s, %s\n", 8519f05faa4eSjacobs tmp->class, tmp->subclass); 8520f05faa4eSjacobs 8521f05faa4eSjacobs log_event(tmp->class, tmp->subclass, tmp->nvl); 8522f05faa4eSjacobs 8523f05faa4eSjacobs if (tmp->class != NULL) 8524f05faa4eSjacobs free(tmp->class); 8525f05faa4eSjacobs if (tmp->subclass != NULL) 8526f05faa4eSjacobs free(tmp->subclass); 8527f05faa4eSjacobs if (tmp->nvl != NULL) 8528f05faa4eSjacobs nvlist_free(tmp->nvl); 8529f05faa4eSjacobs syseventq_back = syseventq_back->next; 8530f05faa4eSjacobs if (syseventq_back == NULL) 8531f05faa4eSjacobs syseventq_front = NULL; 8532f05faa4eSjacobs free(tmp); 8533f05faa4eSjacobs } 8534f05faa4eSjacobs (void) mutex_unlock(&syseventq_mutex); 8535f05faa4eSjacobs } 8536f05faa4eSjacobs 8537f05faa4eSjacobs static void 8538f05faa4eSjacobs build_and_enq_event(char *class, char *subclass, char *node_path, 853930294554Sphitran di_node_t node, char *minor) 85407c478bd9Sstevel@tonic-gate { 85417c478bd9Sstevel@tonic-gate nvlist_t *nvl; 85427c478bd9Sstevel@tonic-gate 8543f05faa4eSjacobs vprint(CHATTY_MID, "build_and_enq_event(%s, %s, %s, 0x%8.8x)\n", 8544f05faa4eSjacobs class, subclass, node_path, (int)node); 8545f05faa4eSjacobs 85467c478bd9Sstevel@tonic-gate if (node != DI_NODE_NIL) 85477c478bd9Sstevel@tonic-gate nvl = build_event_attributes(class, subclass, node_path, node, 854830294554Sphitran di_driver_name(node), di_instance(node), minor); 85497c478bd9Sstevel@tonic-gate else 85507c478bd9Sstevel@tonic-gate nvl = build_event_attributes(class, subclass, node_path, node, 855130294554Sphitran NULL, -1, minor); 85527c478bd9Sstevel@tonic-gate 85537c478bd9Sstevel@tonic-gate if (nvl) { 8554f05faa4eSjacobs enqueue_sysevent(class, subclass, nvl); 85557c478bd9Sstevel@tonic-gate } 85567c478bd9Sstevel@tonic-gate } 8557facf4a8dSllai1 85581ca93273Seota /* 85591ca93273Seota * is_blank() returns 1 (true) if a line specified is composed of 85601ca93273Seota * whitespace characters only. otherwise, it returns 0 (false). 85611ca93273Seota * 85621ca93273Seota * Note. the argument (line) must be null-terminated. 85631ca93273Seota */ 85641ca93273Seota static int 85651ca93273Seota is_blank(char *line) 85661ca93273Seota { 85671ca93273Seota for (/* nothing */; *line != '\0'; line++) 85681ca93273Seota if (!isspace(*line)) 85691ca93273Seota return (0); 85701ca93273Seota return (1); 85711ca93273Seota } 85721ca93273Seota 8573aa646b9dSvikram /* 8574aa646b9dSvikram * Functions to deal with the no-further-processing hash 8575aa646b9dSvikram */ 8576aa646b9dSvikram 8577aa646b9dSvikram static void 8578aa646b9dSvikram nfphash_create(void) 8579aa646b9dSvikram { 8580aa646b9dSvikram assert(nfp_hash == NULL); 8581aa646b9dSvikram nfp_hash = s_zalloc(NFP_HASH_SZ * sizeof (item_t *)); 8582aa646b9dSvikram } 8583aa646b9dSvikram 8584aa646b9dSvikram static int 8585aa646b9dSvikram nfphash_fcn(char *key) 8586aa646b9dSvikram { 8587aa646b9dSvikram int i; 8588aa646b9dSvikram uint64_t sum = 0; 8589aa646b9dSvikram 8590aa646b9dSvikram for (i = 0; key[i] != '\0'; i++) { 8591aa646b9dSvikram sum += (uchar_t)key[i]; 8592aa646b9dSvikram } 8593aa646b9dSvikram 8594aa646b9dSvikram return (sum % NFP_HASH_SZ); 8595aa646b9dSvikram } 8596aa646b9dSvikram 8597aa646b9dSvikram static item_t * 8598aa646b9dSvikram nfphash_lookup(char *key) 8599aa646b9dSvikram { 8600aa646b9dSvikram int index; 8601aa646b9dSvikram item_t *ip; 8602aa646b9dSvikram 8603aa646b9dSvikram index = nfphash_fcn(key); 8604aa646b9dSvikram 8605aa646b9dSvikram assert(index >= 0); 8606aa646b9dSvikram 8607aa646b9dSvikram for (ip = nfp_hash[index]; ip; ip = ip->i_next) { 8608aa646b9dSvikram if (strcmp(ip->i_key, key) == 0) 8609aa646b9dSvikram return (ip); 8610aa646b9dSvikram } 8611aa646b9dSvikram 8612aa646b9dSvikram return (NULL); 8613aa646b9dSvikram } 8614aa646b9dSvikram 8615aa646b9dSvikram static void 8616aa646b9dSvikram nfphash_insert(char *key) 8617aa646b9dSvikram { 8618aa646b9dSvikram item_t *ip; 8619aa646b9dSvikram int index; 8620aa646b9dSvikram 8621aa646b9dSvikram index = nfphash_fcn(key); 8622aa646b9dSvikram 8623aa646b9dSvikram assert(index >= 0); 8624aa646b9dSvikram 8625aa646b9dSvikram ip = s_zalloc(sizeof (item_t)); 8626aa646b9dSvikram ip->i_key = s_strdup(key); 8627aa646b9dSvikram 8628aa646b9dSvikram ip->i_next = nfp_hash[index]; 8629aa646b9dSvikram nfp_hash[index] = ip; 8630aa646b9dSvikram } 8631aa646b9dSvikram 8632aa646b9dSvikram static void 8633aa646b9dSvikram nfphash_destroy(void) 8634aa646b9dSvikram { 8635aa646b9dSvikram int i; 8636aa646b9dSvikram item_t *ip; 8637aa646b9dSvikram 8638aa646b9dSvikram for (i = 0; i < NFP_HASH_SZ; i++) { 8639aa646b9dSvikram /*LINTED*/ 8640aa646b9dSvikram while (ip = nfp_hash[i]) { 8641aa646b9dSvikram nfp_hash[i] = ip->i_next; 8642aa646b9dSvikram free(ip->i_key); 8643aa646b9dSvikram free(ip); 8644aa646b9dSvikram } 8645aa646b9dSvikram } 8646aa646b9dSvikram 8647aa646b9dSvikram free(nfp_hash); 8648aa646b9dSvikram nfp_hash = NULL; 8649aa646b9dSvikram } 8650aa646b9dSvikram 8651facf4a8dSllai1 static int 8652facf4a8dSllai1 devname_kcall(int subcmd, void *args) 8653facf4a8dSllai1 { 8654facf4a8dSllai1 int error = 0; 8655facf4a8dSllai1 char *nvlbuf = NULL; 8656facf4a8dSllai1 size_t nvlsize; 8657facf4a8dSllai1 8658facf4a8dSllai1 switch (subcmd) { 8659facf4a8dSllai1 case MODDEVNAME_NSMAPS: 8660facf4a8dSllai1 error = nvlist_pack((nvlist_t *)args, &nvlbuf, &nvlsize, 0, 0); 8661facf4a8dSllai1 if (error) { 8662facf4a8dSllai1 err_print("packing MODDEVNAME_NSMAPS failed\n"); 8663facf4a8dSllai1 break; 8664facf4a8dSllai1 } 8665facf4a8dSllai1 error = modctl(MODDEVNAME, subcmd, nvlbuf, nvlsize); 8666facf4a8dSllai1 if (error) { 8667facf4a8dSllai1 vprint(INFO_MID, "modctl(MODDEVNAME, " 8668facf4a8dSllai1 "MODDEVNAME_NSMAPS) failed - %s\n", 8669facf4a8dSllai1 strerror(errno)); 8670facf4a8dSllai1 } 8671facf4a8dSllai1 free(nvlbuf); 8672facf4a8dSllai1 nvlist_free(args); 8673facf4a8dSllai1 break; 8674facf4a8dSllai1 case MODDEVNAME_LOOKUPDOOR: 8675facf4a8dSllai1 error = modctl(MODDEVNAME, subcmd, (uintptr_t)args); 8676facf4a8dSllai1 if (error) { 8677facf4a8dSllai1 vprint(INFO_MID, "modctl(MODDEVNAME, " 8678facf4a8dSllai1 "MODDEVNAME_LOOKUPDOOR) failed - %s\n", 8679facf4a8dSllai1 strerror(errno)); 8680facf4a8dSllai1 } 8681facf4a8dSllai1 break; 8682facf4a8dSllai1 default: 8683facf4a8dSllai1 error = EINVAL; 8684facf4a8dSllai1 break; 8685facf4a8dSllai1 } 8686facf4a8dSllai1 return (error); 8687facf4a8dSllai1 } 8688facf4a8dSllai1 8689facf4a8dSllai1 static void 8690facf4a8dSllai1 devname_setup_nsmaps(void) 8691facf4a8dSllai1 { 8692facf4a8dSllai1 int error = 0; 8693facf4a8dSllai1 8694facf4a8dSllai1 if (devname_first_call) { 8695facf4a8dSllai1 devname_first_call = 0; 8696facf4a8dSllai1 } 8697facf4a8dSllai1 8698facf4a8dSllai1 error = di_devname_get_mapinfo(DEVNAME_MASTER_MAP, &devname_maps); 8699facf4a8dSllai1 8700facf4a8dSllai1 if (error) { 87010bfaec69Sllai1 vprint(DEVNAME_MID, "devname_setup_nsmaps: non-existing/empty" 87020bfaec69Sllai1 "%s\n", DEVNAME_MASTER_MAP); 8703facf4a8dSllai1 } else { 8704facf4a8dSllai1 di_devname_print_mapinfo(devname_maps); 87050bfaec69Sllai1 8706facf4a8dSllai1 /* pass down the existing map names to kernel */ 8707facf4a8dSllai1 (void) devname_kcall(MODDEVNAME_NSMAPS, (void *)devname_maps); 8708facf4a8dSllai1 } 8709facf4a8dSllai1 } 8710facf4a8dSllai1 8711facf4a8dSllai1 static void 8712facf4a8dSllai1 devname_ns_services(uint8_t cmd, char *key, char *map) 8713facf4a8dSllai1 { 8714facf4a8dSllai1 nvlist_t *nvl = NULL; 8715facf4a8dSllai1 int32_t error = 0; 8716facf4a8dSllai1 sdev_door_res_t res; 8717facf4a8dSllai1 8718facf4a8dSllai1 vprint(DEVNAME_MID, "devname_ns_services: cmd %d key %s map %s\n", 8719facf4a8dSllai1 cmd, key, map); 8720facf4a8dSllai1 8721facf4a8dSllai1 switch (cmd) { 8722facf4a8dSllai1 case DEVFSADMD_NS_LOOKUP: 8723facf4a8dSllai1 vprint(DEVNAME_MID, "calling di_devname_get_mapent\n"); 8724facf4a8dSllai1 error = di_devname_get_mapent(key, map, &nvl); 8725facf4a8dSllai1 if (nvl == NULL) { 8726facf4a8dSllai1 error = DEVFSADM_NS_FAILED; 8727facf4a8dSllai1 goto done; 8728facf4a8dSllai1 } 8729facf4a8dSllai1 8730facf4a8dSllai1 if (error) { 8731facf4a8dSllai1 nvlist_free(nvl); 8732facf4a8dSllai1 goto done; 8733facf4a8dSllai1 } 8734facf4a8dSllai1 8735facf4a8dSllai1 if (devname_debug_msg) 8736facf4a8dSllai1 di_devname_print_mapinfo(nvl); 8737facf4a8dSllai1 8738facf4a8dSllai1 vprint(DEVNAME_MID, "calling di_devname_action_on_key for %d\n", 8739facf4a8dSllai1 cmd); 8740facf4a8dSllai1 error = di_devname_action_on_key(nvl, cmd, key, (void *)&res); 8741facf4a8dSllai1 nvlist_free(nvl); 8742facf4a8dSllai1 break; 8743facf4a8dSllai1 case DEVFSADMD_NS_READDIR: 8744facf4a8dSllai1 vprint(DEVNAME_MID, "calling di_devname_get_mapinfo for cmd %d" 8745facf4a8dSllai1 "\n", cmd); 8746facf4a8dSllai1 error = di_devname_get_mapinfo(map, &nvl); 8747facf4a8dSllai1 if (nvl == NULL) { 8748facf4a8dSllai1 error = DEVFSADM_NS_FAILED; 8749facf4a8dSllai1 goto done; 8750facf4a8dSllai1 } 8751facf4a8dSllai1 8752facf4a8dSllai1 if (error) { 8753facf4a8dSllai1 nvlist_free(nvl); 8754facf4a8dSllai1 goto done; 8755facf4a8dSllai1 } 8756facf4a8dSllai1 8757facf4a8dSllai1 if (devname_debug_msg) 8758facf4a8dSllai1 di_devname_print_mapinfo(nvl); 8759facf4a8dSllai1 8760facf4a8dSllai1 vprint(DEVNAME_MID, "calling di_devname_action_on_key\n"); 8761facf4a8dSllai1 error = di_devname_action_on_key(nvl, cmd, key, (void *)&res); 8762facf4a8dSllai1 nvlist_free(nvl); 8763facf4a8dSllai1 break; 8764facf4a8dSllai1 default: 8765facf4a8dSllai1 error = DEVFSADM_RUN_NOTSUP; 8766facf4a8dSllai1 break; 8767facf4a8dSllai1 } 8768facf4a8dSllai1 8769facf4a8dSllai1 done: 8770facf4a8dSllai1 vprint(DEVNAME_MID, "error %d\n", error); 8771facf4a8dSllai1 res.devfsadm_error = error; 8772facf4a8dSllai1 (void) door_return((char *)&res, sizeof (struct sdev_door_res), 8773facf4a8dSllai1 NULL, 0); 8774facf4a8dSllai1 } 8775facf4a8dSllai1 8776facf4a8dSllai1 /* ARGSUSED */ 8777facf4a8dSllai1 static void 8778facf4a8dSllai1 devname_lookup_handler(void *cookie, char *argp, size_t arg_size, 8779facf4a8dSllai1 door_desc_t *dp, uint_t n_desc) 8780facf4a8dSllai1 { 8781facf4a8dSllai1 int32_t error = 0; 8782facf4a8dSllai1 door_cred_t dcred; 8783facf4a8dSllai1 struct dca_impl dci; 8784facf4a8dSllai1 uint8_t cmd; 8785facf4a8dSllai1 char *ns_map, *ns_name; 8786facf4a8dSllai1 sdev_door_res_t res; 8787facf4a8dSllai1 sdev_door_arg_t *args; 8788facf4a8dSllai1 8789facf4a8dSllai1 if (argp == NULL || arg_size == 0) { 8790facf4a8dSllai1 vprint(DEVNAME_MID, "devname_lookup_handler: argp wrong\n"); 8791facf4a8dSllai1 error = DEVFSADM_RUN_INVALID; 8792facf4a8dSllai1 goto done; 8793facf4a8dSllai1 } 8794facf4a8dSllai1 vprint(DEVNAME_MID, "devname_lookup_handler\n"); 8795facf4a8dSllai1 8796facf4a8dSllai1 if (door_cred(&dcred) != 0 || dcred.dc_euid != 0) { 8797facf4a8dSllai1 vprint(DEVNAME_MID, "devname_lookup_handler: cred wrong\n"); 8798facf4a8dSllai1 error = DEVFSADM_RUN_EPERM; 8799facf4a8dSllai1 goto done; 8800facf4a8dSllai1 } 8801facf4a8dSllai1 8802facf4a8dSllai1 args = (sdev_door_arg_t *)argp; 8803facf4a8dSllai1 cmd = args->devfsadm_cmd; 8804facf4a8dSllai1 8805facf4a8dSllai1 vprint(DEVNAME_MID, "devname_lookup_handler: cmd %d\n", cmd); 8806facf4a8dSllai1 switch (cmd) { 8807facf4a8dSllai1 case DEVFSADMD_NS_LOOKUP: 8808facf4a8dSllai1 case DEVFSADMD_NS_READDIR: 8809facf4a8dSllai1 ns_name = s_strdup(args->ns_hdl.ns_name); 8810facf4a8dSllai1 ns_map = s_strdup(args->ns_hdl.ns_map); 8811facf4a8dSllai1 8812facf4a8dSllai1 vprint(DEVNAME_MID, " ns_name %s ns_map %s\n", ns_name, ns_map); 8813facf4a8dSllai1 if (ns_name == NULL || ns_map == NULL) { 8814facf4a8dSllai1 error = DEVFSADM_RUN_INVALID; 8815facf4a8dSllai1 goto done; 8816facf4a8dSllai1 } 8817facf4a8dSllai1 8818facf4a8dSllai1 devname_ns_services(cmd, ns_name, ns_map); 8819facf4a8dSllai1 return; 8820facf4a8dSllai1 case DEVFSADMD_RUN_ALL: 8821facf4a8dSllai1 /* 8822facf4a8dSllai1 * run "devfsadm" 8823facf4a8dSllai1 */ 8824facf4a8dSllai1 dci.dci_root = "/"; 8825facf4a8dSllai1 dci.dci_minor = NULL; 8826facf4a8dSllai1 dci.dci_driver = NULL; 8827facf4a8dSllai1 dci.dci_error = 0; 8828facf4a8dSllai1 dci.dci_flags = 0; 8829facf4a8dSllai1 dci.dci_arg = NULL; 8830facf4a8dSllai1 8831facf4a8dSllai1 lock_dev(); 8832facf4a8dSllai1 update_drvconf((major_t)-1); 8833facf4a8dSllai1 dci.dci_flags |= DCA_FLUSH_PATHINST; 8834facf4a8dSllai1 8835facf4a8dSllai1 pre_and_post_cleanup(RM_PRE); 8836facf4a8dSllai1 devi_tree_walk(&dci, DINFOFORCE|DI_CACHE_SNAPSHOT_FLAGS, NULL); 8837facf4a8dSllai1 error = (int32_t)dci.dci_error; 8838facf4a8dSllai1 if (!error) { 8839facf4a8dSllai1 pre_and_post_cleanup(RM_POST); 8840facf4a8dSllai1 update_database = TRUE; 8841facf4a8dSllai1 unlock_dev(SYNC_STATE); 8842facf4a8dSllai1 update_database = FALSE; 8843facf4a8dSllai1 } else { 8844facf4a8dSllai1 if (DEVFSADM_DEBUG_ON) { 8845facf4a8dSllai1 vprint(INFO_MID, "devname_lookup_handler: " 8846facf4a8dSllai1 "DEVFSADMD_RUN_ALL failed\n"); 8847facf4a8dSllai1 } 8848facf4a8dSllai1 8849facf4a8dSllai1 unlock_dev(SYNC_STATE); 8850facf4a8dSllai1 } 8851facf4a8dSllai1 break; 8852facf4a8dSllai1 default: 8853facf4a8dSllai1 /* log an error here? */ 8854facf4a8dSllai1 error = DEVFSADM_RUN_NOTSUP; 8855facf4a8dSllai1 break; 8856facf4a8dSllai1 } 8857facf4a8dSllai1 8858facf4a8dSllai1 done: 8859facf4a8dSllai1 vprint(DEVNAME_MID, "devname_lookup_handler: error %d\n", error); 8860facf4a8dSllai1 res.devfsadm_error = error; 8861facf4a8dSllai1 (void) door_return((char *)&res, sizeof (struct sdev_door_res), 8862facf4a8dSllai1 NULL, 0); 8863facf4a8dSllai1 } 88648d483882Smlf 88658d483882Smlf 88668d483882Smlf di_devlink_handle_t 88678d483882Smlf devfsadm_devlink_cache(void) 88688d483882Smlf { 88698d483882Smlf return (devlink_cache); 88708d483882Smlf } 88718d483882Smlf 88728d483882Smlf int 88738d483882Smlf devfsadm_reserve_id_cache(devlink_re_t re_array[], enumerate_file_t *head) 88748d483882Smlf { 88758d483882Smlf enumerate_file_t *entry; 88768d483882Smlf int nelem; 88778d483882Smlf int i; 88788d483882Smlf int subex; 88798d483882Smlf char *re; 88808d483882Smlf size_t size; 88818d483882Smlf regmatch_t *pmch; 88828d483882Smlf 88838d483882Smlf /* 88848d483882Smlf * Check the <RE, subexp> array passed in and compile it. 88858d483882Smlf */ 88868d483882Smlf for (i = 0; re_array[i].d_re; i++) { 88878d483882Smlf if (re_array[i].d_subexp == 0) { 88888d483882Smlf err_print("bad subexp value in RE: %s\n", 88898d483882Smlf re_array[i].d_re); 88908d483882Smlf goto bad_re; 88918d483882Smlf } 88928d483882Smlf 88938d483882Smlf re = re_array[i].d_re; 88948d483882Smlf if (regcomp(&re_array[i].d_rcomp, re, REG_EXTENDED) != 0) { 88958d483882Smlf err_print("reg. exp. failed to compile: %s\n", re); 88968d483882Smlf goto bad_re; 88978d483882Smlf } 88988d483882Smlf subex = re_array[i].d_subexp; 88998d483882Smlf nelem = subex + 1; 89008d483882Smlf re_array[i].d_pmatch = s_malloc(sizeof (regmatch_t) * nelem); 89018d483882Smlf } 89028d483882Smlf 89038d483882Smlf entry = head ? head : enumerate_reserved; 89048d483882Smlf for (; entry; entry = entry->er_next) { 89058d483882Smlf if (entry->er_id) { 89068d483882Smlf vprint(RSBY_MID, "entry %s already has ID %s\n", 89078d483882Smlf entry->er_file, entry->er_id); 89088d483882Smlf continue; 89098d483882Smlf } 89108d483882Smlf for (i = 0; re_array[i].d_re; i++) { 89118d483882Smlf subex = re_array[i].d_subexp; 89128d483882Smlf pmch = re_array[i].d_pmatch; 89138d483882Smlf if (regexec(&re_array[i].d_rcomp, entry->er_file, 89148d483882Smlf subex + 1, pmch, 0) != 0) { 89158d483882Smlf /* No match */ 89168d483882Smlf continue; 89178d483882Smlf } 89188d483882Smlf size = pmch[subex].rm_eo - pmch[subex].rm_so; 89198d483882Smlf entry->er_id = s_malloc(size + 1); 89208d483882Smlf (void) strncpy(entry->er_id, 89218d483882Smlf &entry->er_file[pmch[subex].rm_so], size); 89228d483882Smlf entry->er_id[size] = '\0'; 89238d483882Smlf if (head) { 89248d483882Smlf vprint(RSBY_MID, "devlink(%s) matches RE(%s). " 89258d483882Smlf "ID is %s\n", entry->er_file, 89268d483882Smlf re_array[i].d_re, entry->er_id); 89278d483882Smlf } else { 89288d483882Smlf vprint(RSBY_MID, "rsrv entry(%s) matches " 89298d483882Smlf "RE(%s) ID is %s\n", entry->er_file, 89308d483882Smlf re_array[i].d_re, entry->er_id); 89318d483882Smlf } 89328d483882Smlf break; 89338d483882Smlf } 89348d483882Smlf } 89358d483882Smlf 89368d483882Smlf for (i = 0; re_array[i].d_re; i++) { 89378d483882Smlf regfree(&re_array[i].d_rcomp); 89388d483882Smlf assert(re_array[i].d_pmatch); 89398d483882Smlf free(re_array[i].d_pmatch); 89408d483882Smlf } 89418d483882Smlf 89428d483882Smlf entry = head ? head : enumerate_reserved; 89438d483882Smlf for (; entry; entry = entry->er_next) { 89448d483882Smlf if (entry->er_id == NULL) 89458d483882Smlf continue; 89468d483882Smlf if (head) { 89478d483882Smlf vprint(RSBY_MID, "devlink: %s\n", entry->er_file); 89488d483882Smlf vprint(RSBY_MID, "ID: %s\n", entry->er_id); 89498d483882Smlf } else { 89508d483882Smlf vprint(RSBY_MID, "reserve file entry: %s\n", 89518d483882Smlf entry->er_file); 89528d483882Smlf vprint(RSBY_MID, "reserve file id: %s\n", 89538d483882Smlf entry->er_id); 89548d483882Smlf } 89558d483882Smlf } 89568d483882Smlf 89578d483882Smlf return (DEVFSADM_SUCCESS); 89588d483882Smlf 89598d483882Smlf bad_re: 89608d483882Smlf for (i = i-1; i >= 0; i--) { 89618d483882Smlf regfree(&re_array[i].d_rcomp); 89628d483882Smlf assert(re_array[i].d_pmatch); 89638d483882Smlf free(re_array[i].d_pmatch); 89648d483882Smlf } 89658d483882Smlf return (DEVFSADM_FAILURE); 89668d483882Smlf } 89678d483882Smlf 89688d483882Smlf /* 8969e37c6c37Scth * Return 1 if we have reserved links. 8970e37c6c37Scth */ 8971e37c6c37Scth int 8972e37c6c37Scth devfsadm_have_reserved() 8973e37c6c37Scth { 8974e37c6c37Scth return (enumerate_reserved ? 1 : 0); 8975e37c6c37Scth } 8976e37c6c37Scth 8977e37c6c37Scth /* 89788d483882Smlf * This functions errs on the side of caution. If there is any error 89798d483882Smlf * we assume that the devlink is *not* reserved 89808d483882Smlf */ 89818d483882Smlf int 89828d483882Smlf devfsadm_is_reserved(devlink_re_t re_array[], char *devlink) 89838d483882Smlf { 89848d483882Smlf int match; 89858d483882Smlf enumerate_file_t estruct = {NULL}; 89868d483882Smlf enumerate_file_t *entry; 89878d483882Smlf 89888d483882Smlf match = 0; 89898d483882Smlf estruct.er_file = devlink; 89908d483882Smlf estruct.er_id = NULL; 89918d483882Smlf estruct.er_next = NULL; 89928d483882Smlf 89938d483882Smlf if (devfsadm_reserve_id_cache(re_array, &estruct) != DEVFSADM_SUCCESS) { 89948d483882Smlf err_print("devfsadm_is_reserved: devlink (%s) does not " 89958d483882Smlf "match RE\n", devlink); 89968d483882Smlf return (0); 89978d483882Smlf } 89988d483882Smlf if (estruct.er_id == NULL) { 89998d483882Smlf err_print("devfsadm_is_reserved: ID derived from devlink %s " 90008d483882Smlf "is NULL\n", devlink); 90018d483882Smlf return (0); 90028d483882Smlf } 90038d483882Smlf 90048d483882Smlf entry = enumerate_reserved; 90058d483882Smlf for (; entry; entry = entry->er_next) { 90068d483882Smlf if (entry->er_id == NULL) 90078d483882Smlf continue; 90088d483882Smlf if (strcmp(entry->er_id, estruct.er_id) != 0) 90098d483882Smlf continue; 90108d483882Smlf match = 1; 90118d483882Smlf vprint(RSBY_MID, "reserve file entry (%s) and devlink (%s) " 90128d483882Smlf "match\n", entry->er_file, devlink); 90138d483882Smlf break; 90148d483882Smlf } 90158d483882Smlf 90168d483882Smlf free(estruct.er_id); 90178d483882Smlf return (match); 90188d483882Smlf } 9019