xref: /illumos-gate/usr/src/cmd/devfsadm/devfsadm.c (revision 17e9b2b7)
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 /*
2366ea8494SVikram Hegde  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Devfsadm replaces drvconfig, audlinks, disks, tapes, ports, devlinks
297c478bd9Sstevel@tonic-gate  * as a general purpose device administrative utility.	It creates
307c478bd9Sstevel@tonic-gate  * devices special files in /devices and logical links in /dev, and
317c478bd9Sstevel@tonic-gate  * coordinates updates to /etc/path_to_instance with the kernel.  It
327c478bd9Sstevel@tonic-gate  * operates in both command line mode to handle user or script invoked
337c478bd9Sstevel@tonic-gate  * reconfiguration updates, and operates in daemon mode to handle dynamic
347c478bd9Sstevel@tonic-gate  * reconfiguration for hotplugging support.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
3745916cd2Sjpk #include <string.h>
38f875b4ebSrica #include <deflt.h>
3945916cd2Sjpk #include <tsol/label.h>
4045916cd2Sjpk #include <bsm/devices.h>
4145916cd2Sjpk #include <bsm/devalloc.h>
427c478bd9Sstevel@tonic-gate #include <utime.h>
436638bf60Saj #include <sys/param.h>
446638bf60Saj #include <bsm/libbsm.h>
45*17e9b2b7SDhanaraj M #include <zone.h>
4645916cd2Sjpk #include "devfsadm_impl.h"
477c478bd9Sstevel@tonic-gate 
4845916cd2Sjpk /* externs from devalloc.c */
4945916cd2Sjpk extern void  _reset_devalloc(int);
5045916cd2Sjpk extern void _update_devalloc_db(devlist_t *, int, int, char *, char *);
5145916cd2Sjpk extern int _da_check_for_usb(char *, char *);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /* create or remove nodes or links. unset with -n */
547c478bd9Sstevel@tonic-gate static int file_mods = TRUE;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* cleanup mode.  Set with -C */
577c478bd9Sstevel@tonic-gate static int cleanup = FALSE;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /* devlinks -d compatibility */
607c478bd9Sstevel@tonic-gate static int devlinks_debug = FALSE;
617c478bd9Sstevel@tonic-gate 
6245916cd2Sjpk /* flag to check if system is labeled */
6345916cd2Sjpk int system_labeled = FALSE;
6445916cd2Sjpk 
6545916cd2Sjpk /* flag to enable/disable device allocation with -e/-d */
6645916cd2Sjpk static int devalloc_flag = 0;
6745916cd2Sjpk 
686638bf60Saj /* flag that indicates if device allocation is on or not */
696638bf60Saj static int devalloc_is_on = 0;
706638bf60Saj 
7145916cd2Sjpk /* flag to update device allocation database for this device type */
7245916cd2Sjpk static int update_devdb = 0;
7345916cd2Sjpk 
7445916cd2Sjpk /*
7545916cd2Sjpk  * devices to be deallocated with -d :
7645916cd2Sjpk  *	audio, floppy, cd, floppy, tape, rmdisk.
7745916cd2Sjpk  */
7845916cd2Sjpk static char *devalloc_list[10] = {DDI_NT_AUDIO, DDI_NT_CD, DDI_NT_CD_CHAN,
7945916cd2Sjpk 				    DDI_NT_FD, DDI_NT_TAPE, DDI_NT_BLOCK_CHAN,
8045916cd2Sjpk 				    DDI_NT_UGEN, DDI_NT_USB_ATTACHMENT_POINT,
8145916cd2Sjpk 				    DDI_NT_SCSI_NEXUS, NULL};
827c478bd9Sstevel@tonic-gate 
8345916cd2Sjpk /* list of allocatable devices */
8445916cd2Sjpk static devlist_t devlist;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /* load a single driver only.  set with -i */
877c478bd9Sstevel@tonic-gate static int single_drv = FALSE;
887c478bd9Sstevel@tonic-gate static char *driver = NULL;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /* attempt to load drivers or defer attach nodes */
917c478bd9Sstevel@tonic-gate static int load_attach_drv = TRUE;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /* set if invoked via /usr/lib/devfsadm/devfsadmd */
947c478bd9Sstevel@tonic-gate static int daemon_mode = FALSE;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /* output directed to syslog during daemon mode if set */
977c478bd9Sstevel@tonic-gate static int logflag = FALSE;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /* build links in /dev.  -x to turn off */
1007c478bd9Sstevel@tonic-gate static int build_dev = TRUE;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /* build nodes in /devices.  -y to turn off */
1037c478bd9Sstevel@tonic-gate static int build_devices = TRUE;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /* -z to turn off */
1067c478bd9Sstevel@tonic-gate static int flush_path_to_inst_enable = TRUE;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate /* variables used for path_to_inst flushing */
1097c478bd9Sstevel@tonic-gate static int inst_count = 0;
1107c478bd9Sstevel@tonic-gate static mutex_t count_lock;
1117c478bd9Sstevel@tonic-gate static cond_t cv;
1127c478bd9Sstevel@tonic-gate 
113ff2aee48Scth /* variables for minor_fini thread */
1147c478bd9Sstevel@tonic-gate static mutex_t minor_fini_mutex;
115ff2aee48Scth static int minor_fini_canceled = TRUE;
116ff2aee48Scth static int minor_fini_delayed = FALSE;
117ff2aee48Scth static cond_t minor_fini_cv;
118ff2aee48Scth static int minor_fini_timeout = MINOR_FINI_TIMEOUT_DEFAULT;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /* single-threads /dev modification */
1217c478bd9Sstevel@tonic-gate static sema_t dev_sema;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /* the program we were invoked as; ie argv[0] */
1247c478bd9Sstevel@tonic-gate static char *prog;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /* pointers to create/remove link lists */
1277c478bd9Sstevel@tonic-gate static create_list_t *create_head = NULL;
1287c478bd9Sstevel@tonic-gate static remove_list_t *remove_head = NULL;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate /*  supports the class -c option */
1317c478bd9Sstevel@tonic-gate static char **classes = NULL;
1327c478bd9Sstevel@tonic-gate static int num_classes = 0;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /* used with verbose option -v or -V */
1357c478bd9Sstevel@tonic-gate static int num_verbose = 0;
1367c478bd9Sstevel@tonic-gate static char **verbose = NULL;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate static struct mperm *minor_perms = NULL;
1397c478bd9Sstevel@tonic-gate static driver_alias_t *driver_aliases = NULL;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /* set if -r alternate root given */
1427c478bd9Sstevel@tonic-gate static char *root_dir = "";
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate /* /devices or <rootdir>/devices */
1457c478bd9Sstevel@tonic-gate static char *devices_dir  = DEVICES;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /* /dev or <rootdir>/dev */
1487c478bd9Sstevel@tonic-gate static char *dev_dir = DEV;
1497c478bd9Sstevel@tonic-gate 
150facf4a8dSllai /* /etc/dev or <rootdir>/etc/dev */
151facf4a8dSllai static char *etc_dev_dir = ETCDEV;
152facf4a8dSllai 
153facf4a8dSllai /*
154facf4a8dSllai  * writable root (for lock files and doors during install).
155facf4a8dSllai  * This is also root dir for /dev attr dir during install.
156facf4a8dSllai  */
157facf4a8dSllai static char *attr_root = NULL;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /* /etc/path_to_inst unless -p used */
1607c478bd9Sstevel@tonic-gate static char *inst_file = INSTANCE_FILE;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /* /usr/lib/devfsadm/linkmods unless -l used */
1637c478bd9Sstevel@tonic-gate static char *module_dirs = MODULE_DIRS;
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /* default uid/gid used if /etc/minor_perm entry not found */
1667c478bd9Sstevel@tonic-gate static uid_t root_uid;
1677c478bd9Sstevel@tonic-gate static gid_t sys_gid;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /* /etc/devlink.tab unless devlinks -t used */
1707c478bd9Sstevel@tonic-gate static char *devlinktab_file = NULL;
1717c478bd9Sstevel@tonic-gate 
1728d483882Smlf /* File and data structure to reserve enumerate IDs */
1738d483882Smlf static char *enumerate_file = ENUMERATE_RESERVED;
1748d483882Smlf static enumerate_file_t *enumerate_reserved = NULL;
1758d483882Smlf 
1767c478bd9Sstevel@tonic-gate /* set if /dev link is new. speeds up rm_stale_links */
1777c478bd9Sstevel@tonic-gate static int linknew = TRUE;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate /* variables for devlink.tab compat processing */
1807c478bd9Sstevel@tonic-gate static devlinktab_list_t *devlinktab_list = NULL;
1817c478bd9Sstevel@tonic-gate static unsigned int devlinktab_line = 0;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /* cache head for devfsadm_enumerate*() functions */
1847c478bd9Sstevel@tonic-gate static numeral_set_t *head_numeral_set = NULL;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate /* list list of devfsadm modules */
1877c478bd9Sstevel@tonic-gate static module_t *module_head = NULL;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /* name_to_major list used in utility function */
1907c478bd9Sstevel@tonic-gate static n2m_t *n2m_list = NULL;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /* cache of some links used for performance */
1937c478bd9Sstevel@tonic-gate static linkhead_t *headlinkhead = NULL;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /* locking variables to prevent multiples writes to /dev */
1967c478bd9Sstevel@tonic-gate static int hold_dev_lock = FALSE;
1977c478bd9Sstevel@tonic-gate static int hold_daemon_lock = FALSE;
1987c478bd9Sstevel@tonic-gate static int dev_lock_fd;
1997c478bd9Sstevel@tonic-gate static int daemon_lock_fd;
2007c478bd9Sstevel@tonic-gate static char dev_lockfile[PATH_MAX + 1];
2017c478bd9Sstevel@tonic-gate static char daemon_lockfile[PATH_MAX + 1];
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate /* last devinfo node/minor processed. used for performance */
2047c478bd9Sstevel@tonic-gate static di_node_t lnode;
2057c478bd9Sstevel@tonic-gate static di_minor_t lminor;
2067c478bd9Sstevel@tonic-gate static char lphy_path[PATH_MAX + 1] = {""};
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /* Globals used by the link database */
2097c478bd9Sstevel@tonic-gate static di_devlink_handle_t devlink_cache;
2107c478bd9Sstevel@tonic-gate static int update_database = FALSE;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /* Globals used to set logindev perms */
2137c478bd9Sstevel@tonic-gate static struct login_dev *login_dev_cache = NULL;
2147c478bd9Sstevel@tonic-gate static int login_dev_enable = FALSE;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /* Global to use devinfo snapshot cache */
2177c478bd9Sstevel@tonic-gate static int use_snapshot_cache = FALSE;
2187c478bd9Sstevel@tonic-gate 
219aa646b9dSvikram /* Global for no-further-processing hash */
220aa646b9dSvikram static item_t **nfp_hash;
221aa646b9dSvikram static mutex_t  nfp_mutex = DEFAULTMUTEX;
222aa646b9dSvikram 
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate  * Packaged directories - not removed even when empty.
2257c478bd9Sstevel@tonic-gate  * The dirs must be listed in canonical form
2267c478bd9Sstevel@tonic-gate  * i.e. without leading "/dev/"
2277c478bd9Sstevel@tonic-gate  */
2287c478bd9Sstevel@tonic-gate static char *packaged_dirs[] =
2297c478bd9Sstevel@tonic-gate 	{"dsk", "rdsk", "term", NULL};
2307c478bd9Sstevel@tonic-gate 
231facf4a8dSllai /* Devname globals */
232facf4a8dSllai static int lookup_door_fd = -1;
233facf4a8dSllai static char *lookup_door_path;
234facf4a8dSllai 
2357c478bd9Sstevel@tonic-gate static void load_dev_acl(void);
2367c478bd9Sstevel@tonic-gate static void update_drvconf(major_t);
237facf4a8dSllai static void check_reconfig_state(void);
238facf4a8dSllai static int s_stat(const char *, struct stat *);
2397c478bd9Sstevel@tonic-gate 
2401ca93273Seota static int is_blank(char *);
2411ca93273Seota 
242f05faa4eSjacobs /* sysevent queue related globals */
243f05faa4eSjacobs static mutex_t  syseventq_mutex = DEFAULTMUTEX;
244f05faa4eSjacobs static syseventq_t *syseventq_front;
245f05faa4eSjacobs static syseventq_t *syseventq_back;
246f05faa4eSjacobs static void process_syseventq();
247f05faa4eSjacobs 
2487c478bd9Sstevel@tonic-gate int
2497c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
2507c478bd9Sstevel@tonic-gate {
2517c478bd9Sstevel@tonic-gate 	struct passwd *pw;
2527c478bd9Sstevel@tonic-gate 	struct group *gp;
2537c478bd9Sstevel@tonic-gate 	pid_t pid;
2546638bf60Saj 	int cond = 0;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2577c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
2607c478bd9Sstevel@tonic-gate 		prog = argv[0];
2617c478bd9Sstevel@tonic-gate 	} else {
2627c478bd9Sstevel@tonic-gate 		prog++;
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	if (getuid() != 0) {
2667c478bd9Sstevel@tonic-gate 		err_print(MUST_BE_ROOT);
2677c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
268537714daSvikram 		/*NOTREACHED*/
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 
271*17e9b2b7SDhanaraj M 	if (getzoneid() != GLOBAL_ZONEID) {
272*17e9b2b7SDhanaraj M 		err_print(MUST_BE_GLOBAL_ZONE);
273*17e9b2b7SDhanaraj M 		devfsadm_exit(1);
274*17e9b2b7SDhanaraj M 	}
275*17e9b2b7SDhanaraj M 
2767c478bd9Sstevel@tonic-gate 	/*
2777c478bd9Sstevel@tonic-gate 	 * Close all files except stdin/stdout/stderr
2787c478bd9Sstevel@tonic-gate 	 */
2797c478bd9Sstevel@tonic-gate 	closefrom(3);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
2827c478bd9Sstevel@tonic-gate 		root_uid = pw->pw_uid;
2837c478bd9Sstevel@tonic-gate 	} else {
2847c478bd9Sstevel@tonic-gate 		err_print(CANT_FIND_USER, DEFAULT_DEV_USER);
2857c478bd9Sstevel@tonic-gate 		root_uid = (uid_t)0;	/* assume 0 is root */
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	/* the default group is sys */
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
2917c478bd9Sstevel@tonic-gate 		sys_gid = gp->gr_gid;
2927c478bd9Sstevel@tonic-gate 	} else {
2937c478bd9Sstevel@tonic-gate 		err_print(CANT_FIND_GROUP, DEFAULT_DEV_GROUP);
2947c478bd9Sstevel@tonic-gate 		sys_gid = (gid_t)3;	/* assume 3 is sys */
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	(void) umask(0);
2987c478bd9Sstevel@tonic-gate 
29945916cd2Sjpk 	system_labeled = is_system_labeled();
30045916cd2Sjpk 	if (system_labeled == FALSE) {
30145916cd2Sjpk 		/*
30245916cd2Sjpk 		 * is_system_labeled() will return false in case we are
30345916cd2Sjpk 		 * starting before the first reboot after Trusted Extensions
304f875b4ebSrica 		 * is enabled.  Check the setting in /etc/system to see if
305f875b4ebSrica 		 * TX is enabled (even if not yet booted).
30645916cd2Sjpk 		 */
307f875b4ebSrica 		if (defopen("/etc/system") == 0) {
308f875b4ebSrica 			if (defread("set sys_labeling=1") != NULL)
309f875b4ebSrica 				system_labeled = TRUE;
310f875b4ebSrica 
311f875b4ebSrica 			/* close defaults file */
312f875b4ebSrica 			(void) defopen(NULL);
313facf4a8dSllai 		}
31445916cd2Sjpk 	}
3156638bf60Saj 	/*
3166638bf60Saj 	 * Check if device allocation is enabled.
3176638bf60Saj 	 */
3186638bf60Saj 	if (system_labeled) {
3196638bf60Saj 		/*
3206638bf60Saj 		 * In TX, the first line in /etc/security/device_allocate has
3216638bf60Saj 		 * DEVICE_ALLOCATION=ON if the feature is enabled.
3226638bf60Saj 		 */
3236638bf60Saj 		devalloc_is_on = da_is_on();
3246638bf60Saj 	} else if (auditon(A_GETCOND, (caddr_t)&cond, sizeof (cond)) == 0) {
3256638bf60Saj 		/*
326d0ca2f1bSJan Parcel 		 * auditon returns -1 and sets errno to EINVAL if BSM
327d0ca2f1bSJan Parcel 		 * is not enabled, so devalloc_is_on must be 0 if no BSM.
328d0ca2f1bSJan Parcel 		 *
329d0ca2f1bSJan Parcel 		 * Device allocation (and auditing) is enabled by default
330d0ca2f1bSJan Parcel 		 * if BSM is enabled, but by default DEVICE_ALLOCATE=
331d0ca2f1bSJan Parcel 		 * string is absent from device_allocate, so da_is_on
332d0ca2f1bSJan Parcel 		 * will return -1.  To preserve the historical default
333d0ca2f1bSJan Parcel 		 * behavior but allow disabling device allocation where needed,
334d0ca2f1bSJan Parcel 		 * set devalloc_is_on to true for non-0, false only for 0.
335d0ca2f1bSJan Parcel 		 *
3366638bf60Saj 		 */
337d0ca2f1bSJan Parcel 		devalloc_is_on = (da_is_on() == 0) ? 0 : 1;
3386638bf60Saj 	}
33945916cd2Sjpk 
340f875b4ebSrica #ifdef DEBUG
341f875b4ebSrica 	if (system_labeled == FALSE) {
342f875b4ebSrica 		struct stat tx_stat;
343f875b4ebSrica 
344f875b4ebSrica 		/* test hook: see also mkdevalloc.c and allocate.c */
345f875b4ebSrica 		system_labeled = is_system_labeled_debug(&tx_stat);
346f875b4ebSrica 	}
347f875b4ebSrica #endif
348f875b4ebSrica 
3497c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	(void) sema_init(&dev_sema, 1, USYNC_THREAD, NULL);
3527c478bd9Sstevel@tonic-gate 
35345916cd2Sjpk 	/* Initialize device allocation list */
35445916cd2Sjpk 	devlist.audio = devlist.cd = devlist.floppy = devlist.tape =
3550a653502Swroche 	    devlist.rmdisk = NULL;
35645916cd2Sjpk 
3577c478bd9Sstevel@tonic-gate 	if (daemon_mode == TRUE) {
3587c478bd9Sstevel@tonic-gate 		/*
3597c478bd9Sstevel@tonic-gate 		 * Build /dev and /devices before daemonizing if
3607c478bd9Sstevel@tonic-gate 		 * reconfig booting and daemon invoked with alternate
3617c478bd9Sstevel@tonic-gate 		 * root. This is to support install.
3627c478bd9Sstevel@tonic-gate 		 */
3637c478bd9Sstevel@tonic-gate 		if (getenv(RECONFIG_BOOT) != NULL && root_dir[0] != '\0') {
3647c478bd9Sstevel@tonic-gate 			vprint(INFO_MID, CONFIGURING);
3657c478bd9Sstevel@tonic-gate 			load_dev_acl();
3667c478bd9Sstevel@tonic-gate 			update_drvconf((major_t)-1);
3677c478bd9Sstevel@tonic-gate 			process_devinfo_tree();
3687c478bd9Sstevel@tonic-gate 			(void) modctl(MODSETMINIROOT);
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 		/*
3727c478bd9Sstevel@tonic-gate 		 * fork before detaching from tty in order to print error
3737c478bd9Sstevel@tonic-gate 		 * message if unable to acquire file lock.  locks not preserved
3747c478bd9Sstevel@tonic-gate 		 * across forks.  Even under debug we want to fork so that
3757c478bd9Sstevel@tonic-gate 		 * when executed at boot we don't hang.
3767c478bd9Sstevel@tonic-gate 		 */
3777c478bd9Sstevel@tonic-gate 		if (fork() != 0) {
3787c478bd9Sstevel@tonic-gate 			devfsadm_exit(0);
379537714daSvikram 			/*NOTREACHED*/
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 		/* set directory to / so it coredumps there */
3837c478bd9Sstevel@tonic-gate 		if (chdir("/") == -1) {
3847c478bd9Sstevel@tonic-gate 			err_print(CHROOT_FAILED, strerror(errno));
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 		/* only one daemon can run at a time */
3887c478bd9Sstevel@tonic-gate 		if ((pid = enter_daemon_lock()) == getpid()) {
3897c478bd9Sstevel@tonic-gate 			detachfromtty();
3907c478bd9Sstevel@tonic-gate 			(void) cond_init(&cv, USYNC_THREAD, 0);
3917c478bd9Sstevel@tonic-gate 			(void) mutex_init(&count_lock, USYNC_THREAD, 0);
3927c478bd9Sstevel@tonic-gate 			if (thr_create(NULL, NULL,
393ff2aee48Scth 			    (void *(*)(void *))instance_flush_thread,
394ff2aee48Scth 			    NULL, THR_DETACHED, NULL) != 0) {
3957c478bd9Sstevel@tonic-gate 				err_print(CANT_CREATE_THREAD, "daemon",
3960a653502Swroche 				    strerror(errno));
3977c478bd9Sstevel@tonic-gate 				devfsadm_exit(1);
398537714daSvikram 				/*NOTREACHED*/
3997c478bd9Sstevel@tonic-gate 			}
4007c478bd9Sstevel@tonic-gate 
401ff2aee48Scth 			/* start the minor_fini_thread */
402ff2aee48Scth 			(void) mutex_init(&minor_fini_mutex, USYNC_THREAD, 0);
403ff2aee48Scth 			(void) cond_init(&minor_fini_cv, USYNC_THREAD, 0);
404ff2aee48Scth 			if (thr_create(NULL, NULL,
405ff2aee48Scth 			    (void *(*)(void *))minor_fini_thread,
406ff2aee48Scth 			    NULL, THR_DETACHED, NULL)) {
407ff2aee48Scth 				err_print(CANT_CREATE_THREAD, "minor_fini",
408ff2aee48Scth 				    strerror(errno));
409ff2aee48Scth 				devfsadm_exit(1);
410537714daSvikram 				/*NOTREACHED*/
411ff2aee48Scth 			}
412ff2aee48Scth 
413ff2aee48Scth 
4147c478bd9Sstevel@tonic-gate 			/*
4150dc974a9SCathy Zhou 			 * logindevperms need only be set
4167c478bd9Sstevel@tonic-gate 			 * in daemon mode and when root dir is "/".
4177c478bd9Sstevel@tonic-gate 			 */
4180dc974a9SCathy Zhou 			if (root_dir[0] == '\0')
4197c478bd9Sstevel@tonic-gate 				login_dev_enable = TRUE;
4207c478bd9Sstevel@tonic-gate 			daemon_update();
421537714daSvikram 			devfsadm_exit(0);
422537714daSvikram 			/*NOTREACHED*/
4237c478bd9Sstevel@tonic-gate 		} else {
4247c478bd9Sstevel@tonic-gate 			err_print(DAEMON_RUNNING, pid);
4257c478bd9Sstevel@tonic-gate 			devfsadm_exit(1);
426537714daSvikram 			/*NOTREACHED*/
4277c478bd9Sstevel@tonic-gate 		}
4287c478bd9Sstevel@tonic-gate 	} else {
4297c478bd9Sstevel@tonic-gate 		/* not a daemon, so just build /dev and /devices */
4308c206d17Spr 
4318c206d17Spr 		/*
4328c206d17Spr 		 * If turning off device allocation, load the
4338c206d17Spr 		 * minor_perm file because process_devinfo_tree() will
4348c206d17Spr 		 * need this in order to reset the permissions of the
4358c206d17Spr 		 * device files.
4368c206d17Spr 		 */
4378c206d17Spr 		if (devalloc_flag == DA_OFF) {
4388c206d17Spr 			read_minor_perm_file();
4398c206d17Spr 		}
4408c206d17Spr 
4417c478bd9Sstevel@tonic-gate 		process_devinfo_tree();
44245916cd2Sjpk 		if (devalloc_flag != 0)
44345916cd2Sjpk 			/* Enable/disable device allocation */
44445916cd2Sjpk 			_reset_devalloc(devalloc_flag);
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate 	return (0);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate static void
4507c478bd9Sstevel@tonic-gate update_drvconf(major_t major)
4517c478bd9Sstevel@tonic-gate {
4527c478bd9Sstevel@tonic-gate 	if (modctl(MODLOADDRVCONF, major) != 0)
4537c478bd9Sstevel@tonic-gate 		err_print(gettext("update_drvconf failed for major %d\n"),
4547c478bd9Sstevel@tonic-gate 		    major);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
457aa646b9dSvikram 
4587c478bd9Sstevel@tonic-gate static void
4597c478bd9Sstevel@tonic-gate load_dev_acl()
4607c478bd9Sstevel@tonic-gate {
4617c478bd9Sstevel@tonic-gate 	if (load_devpolicy() != 0)
4627c478bd9Sstevel@tonic-gate 		err_print(gettext("device policy load failed\n"));
4637c478bd9Sstevel@tonic-gate 	load_minor_perm_file();
4647c478bd9Sstevel@tonic-gate }
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate /*
467facf4a8dSllai  * As devfsadm is run early in boot to provide the kernel with
468facf4a8dSllai  * minor_perm info, we might as well check for reconfig at the
469facf4a8dSllai  * same time to avoid running devfsadm twice.  This gets invoked
470facf4a8dSllai  * earlier than the env variable RECONFIG_BOOT is set up.
4717c478bd9Sstevel@tonic-gate  */
472facf4a8dSllai static void
473facf4a8dSllai check_reconfig_state()
4747c478bd9Sstevel@tonic-gate {
475facf4a8dSllai 	struct stat sb;
4767c478bd9Sstevel@tonic-gate 
477facf4a8dSllai 	if (s_stat("/reconfigure", &sb) == 0) {
478facf4a8dSllai 		(void) modctl(MODDEVNAME, MODDEVNAME_RECONFIG, 0);
4797c478bd9Sstevel@tonic-gate 	}
480facf4a8dSllai }
4817c478bd9Sstevel@tonic-gate 
482facf4a8dSllai static void
483facf4a8dSllai modctl_sysavail()
484facf4a8dSllai {
485facf4a8dSllai 	/*
486facf4a8dSllai 	 * Inform /dev that system is available, that
487facf4a8dSllai 	 * implicit reconfig can now be performed.
488facf4a8dSllai 	 */
489facf4a8dSllai 	(void) modctl(MODDEVNAME, MODDEVNAME_SYSAVAIL, 0);
490facf4a8dSllai }
4917c478bd9Sstevel@tonic-gate 
492facf4a8dSllai static void
493facf4a8dSllai set_lock_root(void)
494facf4a8dSllai {
495facf4a8dSllai 	struct stat sb;
496facf4a8dSllai 	char *lock_root;
497facf4a8dSllai 	size_t len;
4987c478bd9Sstevel@tonic-gate 
499facf4a8dSllai 	lock_root = attr_root ? attr_root : root_dir;
5007c478bd9Sstevel@tonic-gate 
501facf4a8dSllai 	len = strlen(lock_root) + strlen(ETCDEV) + 1;
502facf4a8dSllai 	etc_dev_dir = s_malloc(len);
503facf4a8dSllai 	(void) snprintf(etc_dev_dir, len, "%s%s", lock_root, ETCDEV);
5047c478bd9Sstevel@tonic-gate 
505facf4a8dSllai 	if (s_stat(etc_dev_dir, &sb) != 0) {
506facf4a8dSllai 		s_mkdirp(etc_dev_dir, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
507facf4a8dSllai 	} else if (!S_ISDIR(sb.st_mode)) {
508facf4a8dSllai 		err_print(NOT_DIR, etc_dev_dir);
509facf4a8dSllai 		devfsadm_exit(1);
510537714daSvikram 		/*NOTREACHED*/
511facf4a8dSllai 	}
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
514facf4a8dSllai 
5157c478bd9Sstevel@tonic-gate /*
5167c478bd9Sstevel@tonic-gate  * Parse arguments for all 6 programs handled from devfsadm.
5177c478bd9Sstevel@tonic-gate  */
5187c478bd9Sstevel@tonic-gate static void
5197c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[])
5207c478bd9Sstevel@tonic-gate {
5217c478bd9Sstevel@tonic-gate 	char opt;
5227c478bd9Sstevel@tonic-gate 	char get_linkcompat_opts = FALSE;
5237c478bd9Sstevel@tonic-gate 	char *compat_class;
5247c478bd9Sstevel@tonic-gate 	int num_aliases = 0;
5257c478bd9Sstevel@tonic-gate 	int len;
5267c478bd9Sstevel@tonic-gate 	int retval;
5276532b960SJerry Gilliam 	int config = TRUE;
5286532b960SJerry Gilliam 	int bind = FALSE;
5296532b960SJerry Gilliam 	int force_flag = FALSE;
5307c478bd9Sstevel@tonic-gate 	struct aliases *ap = NULL;
5317c478bd9Sstevel@tonic-gate 	struct aliases *a_head = NULL;
5327c478bd9Sstevel@tonic-gate 	struct aliases *a_tail = NULL;
5337c478bd9Sstevel@tonic-gate 	struct modconfig mc;
5347c478bd9Sstevel@tonic-gate 
5356532b960SJerry Gilliam 	(void) bzero(&mc, sizeof (mc));
5366532b960SJerry Gilliam 
5377c478bd9Sstevel@tonic-gate 	if (strcmp(prog, DISKS) == 0) {
5387c478bd9Sstevel@tonic-gate 		compat_class = "disk";
5397c478bd9Sstevel@tonic-gate 		get_linkcompat_opts = TRUE;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, TAPES) == 0) {
5427c478bd9Sstevel@tonic-gate 		compat_class = "tape";
5437c478bd9Sstevel@tonic-gate 		get_linkcompat_opts = TRUE;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, PORTS) == 0) {
5467c478bd9Sstevel@tonic-gate 		compat_class = "port";
5477c478bd9Sstevel@tonic-gate 		get_linkcompat_opts = TRUE;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, AUDLINKS) == 0) {
5507c478bd9Sstevel@tonic-gate 		compat_class = "audio";
5517c478bd9Sstevel@tonic-gate 		get_linkcompat_opts = TRUE;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, DEVLINKS) == 0) {
5547c478bd9Sstevel@tonic-gate 		devlinktab_file = DEVLINKTAB_FILE;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 		build_devices = FALSE;
5577c478bd9Sstevel@tonic-gate 		load_attach_drv = FALSE;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 		while ((opt = getopt(argc, argv, "dnr:st:vV:")) != EOF) {
5607c478bd9Sstevel@tonic-gate 			switch (opt) {
5617c478bd9Sstevel@tonic-gate 			case 'd':
5627c478bd9Sstevel@tonic-gate 				file_mods = FALSE;
5637c478bd9Sstevel@tonic-gate 				flush_path_to_inst_enable = FALSE;
5647c478bd9Sstevel@tonic-gate 				devlinks_debug = TRUE;
5657c478bd9Sstevel@tonic-gate 				break;
5667c478bd9Sstevel@tonic-gate 			case 'n':
5677c478bd9Sstevel@tonic-gate 				/* prevent driver loading and deferred attach */
5687c478bd9Sstevel@tonic-gate 				load_attach_drv = FALSE;
5697c478bd9Sstevel@tonic-gate 				break;
5707c478bd9Sstevel@tonic-gate 			case 'r':
571facf4a8dSllai 				set_root_devices_dev_dir(optarg);
5727c478bd9Sstevel@tonic-gate 				if (zone_pathcheck(root_dir) !=
5737c478bd9Sstevel@tonic-gate 				    DEVFSADM_SUCCESS)
5747c478bd9Sstevel@tonic-gate 					devfsadm_exit(1);
575537714daSvikram 					/*NOTREACHED*/
5767c478bd9Sstevel@tonic-gate 				break;
5777c478bd9Sstevel@tonic-gate 			case 's':
5787c478bd9Sstevel@tonic-gate 				/*
5797c478bd9Sstevel@tonic-gate 				 * suppress.  don't create/remove links/nodes
5807c478bd9Sstevel@tonic-gate 				 * useful with -v or -V
5817c478bd9Sstevel@tonic-gate 				 */
5827c478bd9Sstevel@tonic-gate 				file_mods = FALSE;
5837c478bd9Sstevel@tonic-gate 				flush_path_to_inst_enable = FALSE;
5847c478bd9Sstevel@tonic-gate 				break;
5857c478bd9Sstevel@tonic-gate 			case 't':
5867c478bd9Sstevel@tonic-gate 				/* supply a non-default table file */
5877c478bd9Sstevel@tonic-gate 				devlinktab_file = optarg;
5887c478bd9Sstevel@tonic-gate 				break;
5897c478bd9Sstevel@tonic-gate 			case 'v':
5907c478bd9Sstevel@tonic-gate 				/* documented verbose flag */
5917c478bd9Sstevel@tonic-gate 				add_verbose_id(VERBOSE_MID);
5927c478bd9Sstevel@tonic-gate 				break;
5937c478bd9Sstevel@tonic-gate 			case 'V':
5947c478bd9Sstevel@tonic-gate 				/* undocumented for extra verbose levels */
5957c478bd9Sstevel@tonic-gate 				add_verbose_id(optarg);
5967c478bd9Sstevel@tonic-gate 				break;
5977c478bd9Sstevel@tonic-gate 			default:
5987c478bd9Sstevel@tonic-gate 				usage();
5997c478bd9Sstevel@tonic-gate 				break;
6007c478bd9Sstevel@tonic-gate 			}
6017c478bd9Sstevel@tonic-gate 		}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		if (optind < argc) {
6047c478bd9Sstevel@tonic-gate 			usage();
6057c478bd9Sstevel@tonic-gate 		}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, DRVCONFIG) == 0) {
6087c478bd9Sstevel@tonic-gate 		build_dev = FALSE;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 		while ((opt =
611b8630552SJerry Gilliam 		    getopt(argc, argv, "a:bc:dfi:m:np:R:r:suvV:")) != EOF) {
6127c478bd9Sstevel@tonic-gate 			switch (opt) {
6137c478bd9Sstevel@tonic-gate 			case 'a':
6147c478bd9Sstevel@tonic-gate 				ap = calloc(sizeof (struct aliases), 1);
6157c478bd9Sstevel@tonic-gate 				ap->a_name = dequote(optarg);
6167c478bd9Sstevel@tonic-gate 				len = strlen(ap->a_name) + 1;
6177c478bd9Sstevel@tonic-gate 				if (len > MAXMODCONFNAME) {
6187c478bd9Sstevel@tonic-gate 					err_print(ALIAS_TOO_LONG,
6197c478bd9Sstevel@tonic-gate 					    MAXMODCONFNAME, ap->a_name);
6207c478bd9Sstevel@tonic-gate 					devfsadm_exit(1);
621537714daSvikram 					/*NOTREACHED*/
6227c478bd9Sstevel@tonic-gate 				}
6237c478bd9Sstevel@tonic-gate 				ap->a_len = len;
6247c478bd9Sstevel@tonic-gate 				if (a_tail == NULL) {
6257c478bd9Sstevel@tonic-gate 					a_head = ap;
6267c478bd9Sstevel@tonic-gate 				} else {
6277c478bd9Sstevel@tonic-gate 					a_tail->a_next = ap;
6287c478bd9Sstevel@tonic-gate 				}
6297c478bd9Sstevel@tonic-gate 				a_tail = ap;
6307c478bd9Sstevel@tonic-gate 				num_aliases++;
6316532b960SJerry Gilliam 				bind = TRUE;
6327c478bd9Sstevel@tonic-gate 				break;
6337c478bd9Sstevel@tonic-gate 			case 'b':
6346532b960SJerry Gilliam 				bind = TRUE;
6357c478bd9Sstevel@tonic-gate 				break;
6367c478bd9Sstevel@tonic-gate 			case 'c':
6377c478bd9Sstevel@tonic-gate 				(void) strcpy(mc.drvclass, optarg);
6387c478bd9Sstevel@tonic-gate 				break;
6397c478bd9Sstevel@tonic-gate 			case 'd':
6407c478bd9Sstevel@tonic-gate 				/*
6417c478bd9Sstevel@tonic-gate 				 * need to keep for compatibility, but
6427c478bd9Sstevel@tonic-gate 				 * do nothing.
6437c478bd9Sstevel@tonic-gate 				 */
6447c478bd9Sstevel@tonic-gate 				break;
6456532b960SJerry Gilliam 			case 'f':
6466532b960SJerry Gilliam 				force_flag = TRUE;
6476532b960SJerry Gilliam 				break;
6487c478bd9Sstevel@tonic-gate 			case 'i':
6497c478bd9Sstevel@tonic-gate 				single_drv = TRUE;
6507c478bd9Sstevel@tonic-gate 				(void) strcpy(mc.drvname, optarg);
6517c478bd9Sstevel@tonic-gate 				driver = s_strdup(optarg);
6527c478bd9Sstevel@tonic-gate 				break;
6537c478bd9Sstevel@tonic-gate 			case 'm':
6547c478bd9Sstevel@tonic-gate 				mc.major = atoi(optarg);
6557c478bd9Sstevel@tonic-gate 				break;
6567c478bd9Sstevel@tonic-gate 			case 'n':
6577c478bd9Sstevel@tonic-gate 				/* prevent driver loading and deferred attach */
6587c478bd9Sstevel@tonic-gate 				load_attach_drv = FALSE;
6597c478bd9Sstevel@tonic-gate 				break;
6607c478bd9Sstevel@tonic-gate 			case 'p':
6617c478bd9Sstevel@tonic-gate 				/* specify alternate path_to_inst file */
6627c478bd9Sstevel@tonic-gate 				inst_file = s_strdup(optarg);
6637c478bd9Sstevel@tonic-gate 				break;
6647c478bd9Sstevel@tonic-gate 			case 'R':
6657c478bd9Sstevel@tonic-gate 				/*
6667c478bd9Sstevel@tonic-gate 				 * Private flag for suninstall to populate
6677c478bd9Sstevel@tonic-gate 				 * device information on the installed root.
6687c478bd9Sstevel@tonic-gate 				 */
6697c478bd9Sstevel@tonic-gate 				root_dir = s_strdup(optarg);
6707c478bd9Sstevel@tonic-gate 				if (zone_pathcheck(root_dir) !=
6717c478bd9Sstevel@tonic-gate 				    DEVFSADM_SUCCESS)
6727c478bd9Sstevel@tonic-gate 				devfsadm_exit(devfsadm_copy());
673537714daSvikram 				/*NOTREACHED*/
6747c478bd9Sstevel@tonic-gate 				break;
6757c478bd9Sstevel@tonic-gate 			case 'r':
6767c478bd9Sstevel@tonic-gate 				devices_dir = s_strdup(optarg);
6777c478bd9Sstevel@tonic-gate 				if (zone_pathcheck(devices_dir) !=
6787c478bd9Sstevel@tonic-gate 				    DEVFSADM_SUCCESS)
6797c478bd9Sstevel@tonic-gate 					devfsadm_exit(1);
680537714daSvikram 					/*NOTREACHED*/
6817c478bd9Sstevel@tonic-gate 				break;
6827c478bd9Sstevel@tonic-gate 			case 's':
6837c478bd9Sstevel@tonic-gate 				/*
6847c478bd9Sstevel@tonic-gate 				 * suppress.  don't create nodes
6857c478bd9Sstevel@tonic-gate 				 * useful with -v or -V
6867c478bd9Sstevel@tonic-gate 				 */
6877c478bd9Sstevel@tonic-gate 				file_mods = FALSE;
6887c478bd9Sstevel@tonic-gate 				flush_path_to_inst_enable = FALSE;
6897c478bd9Sstevel@tonic-gate 				break;
6906532b960SJerry Gilliam 			case 'u':
6916532b960SJerry Gilliam 				/*
6926532b960SJerry Gilliam 				 * Invoked via update_drv(1m) to update
6936532b960SJerry Gilliam 				 * the kernel's driver/alias binding
6946532b960SJerry Gilliam 				 * when removing one or more aliases.
6956532b960SJerry Gilliam 				 */
6966532b960SJerry Gilliam 				config = FALSE;
6976532b960SJerry Gilliam 				break;
6987c478bd9Sstevel@tonic-gate 			case 'v':
6997c478bd9Sstevel@tonic-gate 				/* documented verbose flag */
7007c478bd9Sstevel@tonic-gate 				add_verbose_id(VERBOSE_MID);
7017c478bd9Sstevel@tonic-gate 				break;
7027c478bd9Sstevel@tonic-gate 			case 'V':
7037c478bd9Sstevel@tonic-gate 				/* undocumented for extra verbose levels */
7047c478bd9Sstevel@tonic-gate 				add_verbose_id(optarg);
7057c478bd9Sstevel@tonic-gate 				break;
7067c478bd9Sstevel@tonic-gate 			default:
7077c478bd9Sstevel@tonic-gate 				usage();
7087c478bd9Sstevel@tonic-gate 			}
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 		if (optind < argc) {
7127c478bd9Sstevel@tonic-gate 			usage();
7137c478bd9Sstevel@tonic-gate 		}
7147c478bd9Sstevel@tonic-gate 
7156532b960SJerry Gilliam 		if (bind == TRUE) {
7166532b960SJerry Gilliam 			if ((mc.major == -1) || (mc.drvname[0] == NULL)) {
7176532b960SJerry Gilliam 				err_print(MAJOR_AND_B_FLAG);
7186532b960SJerry Gilliam 				devfsadm_exit(1);
7196532b960SJerry Gilliam 				/*NOTREACHED*/
7206532b960SJerry Gilliam 			}
7216532b960SJerry Gilliam 			mc.flags = (force_flag) ? MOD_UNBIND_OVERRIDE : 0;
7227c478bd9Sstevel@tonic-gate 			mc.num_aliases = num_aliases;
7237c478bd9Sstevel@tonic-gate 			mc.ap = a_head;
7246532b960SJerry Gilliam 			retval =  modctl((config == TRUE) ? MODADDMAJBIND :
7256532b960SJerry Gilliam 			    MODREMDRVALIAS, NULL, (caddr_t)&mc);
7267c478bd9Sstevel@tonic-gate 			if (retval < 0) {
7276532b960SJerry Gilliam 				err_print((config == TRUE) ? MODCTL_ADDMAJBIND :
7286532b960SJerry Gilliam 				    MODCTL_REMMAJBIND);
7297c478bd9Sstevel@tonic-gate 			}
7307c478bd9Sstevel@tonic-gate 			devfsadm_exit(retval);
731537714daSvikram 			/*NOTREACHED*/
7327c478bd9Sstevel@tonic-gate 		}
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	} else if ((strcmp(prog, DEVFSADM) == 0) ||
7357c478bd9Sstevel@tonic-gate 	    (strcmp(prog, DEVFSADMD) == 0)) {
7367c478bd9Sstevel@tonic-gate 		char *zonename = NULL;
7377c478bd9Sstevel@tonic-gate 		int init_drvconf = 0;
7387c478bd9Sstevel@tonic-gate 		int init_perm = 0;
7397c478bd9Sstevel@tonic-gate 		int public_mode = 0;
740facf4a8dSllai 		int init_sysavail = 0;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		if (strcmp(prog, DEVFSADMD) == 0) {
7437c478bd9Sstevel@tonic-gate 			daemon_mode = TRUE;
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 		devlinktab_file = DEVLINKTAB_FILE;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 		while ((opt = getopt(argc, argv,
7493c5e027bSEric Taylor 		    "a:Cc:deIi:l:np:PR:r:sSt:vV:x:")) != EOF) {
750facf4a8dSllai 			if (opt == 'I' || opt == 'P' || opt == 'S') {
7517c478bd9Sstevel@tonic-gate 				if (public_mode)
7527c478bd9Sstevel@tonic-gate 					usage();
7537c478bd9Sstevel@tonic-gate 			} else {
754facf4a8dSllai 				if (init_perm || init_drvconf || init_sysavail)
7557c478bd9Sstevel@tonic-gate 					usage();
7567c478bd9Sstevel@tonic-gate 				public_mode = 1;
7577c478bd9Sstevel@tonic-gate 			}
7587c478bd9Sstevel@tonic-gate 			switch (opt) {
759facf4a8dSllai 			case 'a':
760facf4a8dSllai 				attr_root = s_strdup(optarg);
761facf4a8dSllai 				break;
7627c478bd9Sstevel@tonic-gate 			case 'C':
7637c478bd9Sstevel@tonic-gate 				cleanup = TRUE;
7647c478bd9Sstevel@tonic-gate 				break;
7657c478bd9Sstevel@tonic-gate 			case 'c':
7667c478bd9Sstevel@tonic-gate 				num_classes++;
7670a653502Swroche 				classes = s_realloc(classes,
7680a653502Swroche 				    num_classes * sizeof (char *));
7697c478bd9Sstevel@tonic-gate 				classes[num_classes - 1] = optarg;
7707c478bd9Sstevel@tonic-gate 				break;
77145916cd2Sjpk 			case 'd':
7727c478bd9Sstevel@tonic-gate 				if (daemon_mode == FALSE) {
77345916cd2Sjpk 					/*
77445916cd2Sjpk 					 * Device allocation to be disabled.
77545916cd2Sjpk 					 */
77645916cd2Sjpk 					devalloc_flag = DA_OFF;
77745916cd2Sjpk 					build_dev = FALSE;
77845916cd2Sjpk 				}
77945916cd2Sjpk 				break;
78045916cd2Sjpk 			case 'e':
78145916cd2Sjpk 				if (daemon_mode == FALSE) {
78245916cd2Sjpk 					/*
78345916cd2Sjpk 					 * Device allocation to be enabled.
78445916cd2Sjpk 					 */
78545916cd2Sjpk 					devalloc_flag = DA_ON;
7867c478bd9Sstevel@tonic-gate 					build_dev = FALSE;
7877c478bd9Sstevel@tonic-gate 				}
7887c478bd9Sstevel@tonic-gate 				break;
7897c478bd9Sstevel@tonic-gate 			case 'I':	/* update kernel driver.conf cache */
7907c478bd9Sstevel@tonic-gate 				if (daemon_mode == TRUE)
7917c478bd9Sstevel@tonic-gate 					usage();
7927c478bd9Sstevel@tonic-gate 				init_drvconf = 1;
7937c478bd9Sstevel@tonic-gate 				break;
7947c478bd9Sstevel@tonic-gate 			case 'i':
7957c478bd9Sstevel@tonic-gate 				single_drv = TRUE;
7967c478bd9Sstevel@tonic-gate 				driver = s_strdup(optarg);
7977c478bd9Sstevel@tonic-gate 				break;
7987c478bd9Sstevel@tonic-gate 			case 'l':
7997c478bd9Sstevel@tonic-gate 				/* specify an alternate module load path */
8007c478bd9Sstevel@tonic-gate 				module_dirs = s_strdup(optarg);
8017c478bd9Sstevel@tonic-gate 				break;
8027c478bd9Sstevel@tonic-gate 			case 'n':
8037c478bd9Sstevel@tonic-gate 				/* prevent driver loading and deferred attach */
8047c478bd9Sstevel@tonic-gate 				load_attach_drv = FALSE;
8057c478bd9Sstevel@tonic-gate 				break;
8067c478bd9Sstevel@tonic-gate 			case 'p':
8077c478bd9Sstevel@tonic-gate 				/* specify alternate path_to_inst file */
8087c478bd9Sstevel@tonic-gate 				inst_file = s_strdup(optarg);
8097c478bd9Sstevel@tonic-gate 				break;
8107c478bd9Sstevel@tonic-gate 			case 'P':
8117c478bd9Sstevel@tonic-gate 				if (daemon_mode == TRUE)
8127c478bd9Sstevel@tonic-gate 					usage();
8137c478bd9Sstevel@tonic-gate 				/* load minor_perm and device_policy */
8147c478bd9Sstevel@tonic-gate 				init_perm = 1;
8157c478bd9Sstevel@tonic-gate 				break;
8167c478bd9Sstevel@tonic-gate 			case 'R':
8177c478bd9Sstevel@tonic-gate 				/*
8187c478bd9Sstevel@tonic-gate 				 * Private flag for suninstall to populate
8197c478bd9Sstevel@tonic-gate 				 * device information on the installed root.
8207c478bd9Sstevel@tonic-gate 				 */
8217c478bd9Sstevel@tonic-gate 				root_dir = s_strdup(optarg);
8227c478bd9Sstevel@tonic-gate 				devfsadm_exit(devfsadm_copy());
823537714daSvikram 				/*NOTREACHED*/
8247c478bd9Sstevel@tonic-gate 				break;
8257c478bd9Sstevel@tonic-gate 			case 'r':
826facf4a8dSllai 				set_root_devices_dev_dir(optarg);
8277c478bd9Sstevel@tonic-gate 				break;
8287c478bd9Sstevel@tonic-gate 			case 's':
8297c478bd9Sstevel@tonic-gate 				/*
8307c478bd9Sstevel@tonic-gate 				 * suppress. don't create/remove links/nodes
8317c478bd9Sstevel@tonic-gate 				 * useful with -v or -V
8327c478bd9Sstevel@tonic-gate 				 */
8337c478bd9Sstevel@tonic-gate 				file_mods = FALSE;
8347c478bd9Sstevel@tonic-gate 				flush_path_to_inst_enable = FALSE;
8357c478bd9Sstevel@tonic-gate 				break;
836facf4a8dSllai 			case 'S':
837facf4a8dSllai 				if (daemon_mode == TRUE)
838facf4a8dSllai 					usage();
839facf4a8dSllai 				init_sysavail = 1;
840facf4a8dSllai 				break;
8417c478bd9Sstevel@tonic-gate 			case 't':
8427c478bd9Sstevel@tonic-gate 				devlinktab_file = optarg;
8437c478bd9Sstevel@tonic-gate 				break;
8447c478bd9Sstevel@tonic-gate 			case 'v':
8457c478bd9Sstevel@tonic-gate 				/* documented verbose flag */
8467c478bd9Sstevel@tonic-gate 				add_verbose_id(VERBOSE_MID);
8477c478bd9Sstevel@tonic-gate 				break;
8487c478bd9Sstevel@tonic-gate 			case 'V':
8497c478bd9Sstevel@tonic-gate 				/* undocumented: specify verbose lvl */
8507c478bd9Sstevel@tonic-gate 				add_verbose_id(optarg);
8517c478bd9Sstevel@tonic-gate 				break;
8527c478bd9Sstevel@tonic-gate 			case 'x':
8537c478bd9Sstevel@tonic-gate 				/*
8547c478bd9Sstevel@tonic-gate 				 * x is the "private switch" option.  The
8557c478bd9Sstevel@tonic-gate 				 * goal is to not suck up all the other
8567c478bd9Sstevel@tonic-gate 				 * option letters.
8577c478bd9Sstevel@tonic-gate 				 */
8587c478bd9Sstevel@tonic-gate 				if (strcmp(optarg, "update_devlinksdb") == 0) {
8597c478bd9Sstevel@tonic-gate 					update_database = TRUE;
8607c478bd9Sstevel@tonic-gate 				} else if (strcmp(optarg, "no_dev") == 0) {
8617c478bd9Sstevel@tonic-gate 					/* don't build /dev */
8627c478bd9Sstevel@tonic-gate 					build_dev = FALSE;
8637c478bd9Sstevel@tonic-gate 				} else if (strcmp(optarg, "no_devices") == 0) {
8647c478bd9Sstevel@tonic-gate 					/* don't build /devices */
8657c478bd9Sstevel@tonic-gate 					build_devices = FALSE;
8667c478bd9Sstevel@tonic-gate 				} else if (strcmp(optarg, "no_p2i") == 0) {
8677c478bd9Sstevel@tonic-gate 					/* don't flush path_to_inst */
8687c478bd9Sstevel@tonic-gate 					flush_path_to_inst_enable = FALSE;
8697c478bd9Sstevel@tonic-gate 				} else if (strcmp(optarg, "use_dicache") == 0) {
8707c478bd9Sstevel@tonic-gate 					use_snapshot_cache = TRUE;
8717c478bd9Sstevel@tonic-gate 				} else {
8727c478bd9Sstevel@tonic-gate 					usage();
8737c478bd9Sstevel@tonic-gate 				}
8747c478bd9Sstevel@tonic-gate 				break;
8757c478bd9Sstevel@tonic-gate 			default:
8767c478bd9Sstevel@tonic-gate 				usage();
8777c478bd9Sstevel@tonic-gate 				break;
8787c478bd9Sstevel@tonic-gate 			}
8797c478bd9Sstevel@tonic-gate 		}
8807c478bd9Sstevel@tonic-gate 		if (optind < argc) {
8817c478bd9Sstevel@tonic-gate 			usage();
8827c478bd9Sstevel@tonic-gate 		}
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 		/*
8857c478bd9Sstevel@tonic-gate 		 * We're not in zone mode; Check to see if the rootpath
8867c478bd9Sstevel@tonic-gate 		 * collides with any zonepaths.
8877c478bd9Sstevel@tonic-gate 		 */
8887c478bd9Sstevel@tonic-gate 		if (zonename == NULL) {
8897c478bd9Sstevel@tonic-gate 			if (zone_pathcheck(root_dir) != DEVFSADM_SUCCESS)
8907c478bd9Sstevel@tonic-gate 				devfsadm_exit(1);
891537714daSvikram 				/*NOTREACHED*/
8927c478bd9Sstevel@tonic-gate 		}
8937c478bd9Sstevel@tonic-gate 
894facf4a8dSllai 		if (init_drvconf || init_perm || init_sysavail) {
8957c478bd9Sstevel@tonic-gate 			/*
8967c478bd9Sstevel@tonic-gate 			 * Load minor perm before force-loading drivers
8977c478bd9Sstevel@tonic-gate 			 * so the correct permissions are picked up.
8987c478bd9Sstevel@tonic-gate 			 */
899facf4a8dSllai 			if (init_perm) {
900facf4a8dSllai 				check_reconfig_state();
9017c478bd9Sstevel@tonic-gate 				load_dev_acl();
902facf4a8dSllai 			}
9037c478bd9Sstevel@tonic-gate 			if (init_drvconf)
9047c478bd9Sstevel@tonic-gate 				update_drvconf((major_t)-1);
905facf4a8dSllai 			if (init_sysavail)
906facf4a8dSllai 				modctl_sysavail();
9077c478bd9Sstevel@tonic-gate 			devfsadm_exit(0);
908537714daSvikram 			/*NOTREACHED*/
9097c478bd9Sstevel@tonic-gate 		}
9107c478bd9Sstevel@tonic-gate 	}
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 	if (get_linkcompat_opts == TRUE) {
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 		build_devices = FALSE;
9167c478bd9Sstevel@tonic-gate 		load_attach_drv = FALSE;
9177c478bd9Sstevel@tonic-gate 		num_classes++;
9187c478bd9Sstevel@tonic-gate 		classes = s_realloc(classes, num_classes *
9197c478bd9Sstevel@tonic-gate 		    sizeof (char *));
9207c478bd9Sstevel@tonic-gate 		classes[num_classes - 1] = compat_class;
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 		while ((opt = getopt(argc, argv, "Cnr:svV:")) != EOF) {
9237c478bd9Sstevel@tonic-gate 			switch (opt) {
9247c478bd9Sstevel@tonic-gate 			case 'C':
9257c478bd9Sstevel@tonic-gate 				cleanup = TRUE;
9267c478bd9Sstevel@tonic-gate 				break;
9277c478bd9Sstevel@tonic-gate 			case 'n':
9287c478bd9Sstevel@tonic-gate 				/* prevent driver loading or deferred attach */
9297c478bd9Sstevel@tonic-gate 				load_attach_drv = FALSE;
9307c478bd9Sstevel@tonic-gate 				break;
9317c478bd9Sstevel@tonic-gate 			case 'r':
932facf4a8dSllai 				set_root_devices_dev_dir(optarg);
9337c478bd9Sstevel@tonic-gate 				if (zone_pathcheck(root_dir) !=
9347c478bd9Sstevel@tonic-gate 				    DEVFSADM_SUCCESS)
9357c478bd9Sstevel@tonic-gate 					devfsadm_exit(1);
936537714daSvikram 					/*NOTREACHED*/
9377c478bd9Sstevel@tonic-gate 				break;
9387c478bd9Sstevel@tonic-gate 			case 's':
9397c478bd9Sstevel@tonic-gate 				/* suppress.  don't create/remove links/nodes */
9407c478bd9Sstevel@tonic-gate 				/* useful with -v or -V */
9417c478bd9Sstevel@tonic-gate 				file_mods = FALSE;
9427c478bd9Sstevel@tonic-gate 				flush_path_to_inst_enable = FALSE;
9437c478bd9Sstevel@tonic-gate 				break;
9447c478bd9Sstevel@tonic-gate 			case 'v':
9457c478bd9Sstevel@tonic-gate 				/* documented verbose flag */
9467c478bd9Sstevel@tonic-gate 				add_verbose_id(VERBOSE_MID);
9477c478bd9Sstevel@tonic-gate 				break;
9487c478bd9Sstevel@tonic-gate 			case 'V':
9497c478bd9Sstevel@tonic-gate 				/* undocumented for extra verbose levels */
9507c478bd9Sstevel@tonic-gate 				add_verbose_id(optarg);
9517c478bd9Sstevel@tonic-gate 				break;
9527c478bd9Sstevel@tonic-gate 			default:
9537c478bd9Sstevel@tonic-gate 				usage();
9547c478bd9Sstevel@tonic-gate 			}
9557c478bd9Sstevel@tonic-gate 		}
9567c478bd9Sstevel@tonic-gate 		if (optind < argc) {
9577c478bd9Sstevel@tonic-gate 			usage();
9587c478bd9Sstevel@tonic-gate 		}
9597c478bd9Sstevel@tonic-gate 	}
960facf4a8dSllai 	set_lock_root();
9617c478bd9Sstevel@tonic-gate }
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate void
9647c478bd9Sstevel@tonic-gate usage(void)
9657c478bd9Sstevel@tonic-gate {
9667c478bd9Sstevel@tonic-gate 	if (strcmp(prog, DEVLINKS) == 0) {
9677c478bd9Sstevel@tonic-gate 		err_print(DEVLINKS_USAGE);
9687c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, DRVCONFIG) == 0) {
9697c478bd9Sstevel@tonic-gate 		err_print(DRVCONFIG_USAGE);
9707c478bd9Sstevel@tonic-gate 	} else if ((strcmp(prog, DEVFSADM) == 0) ||
9710a653502Swroche 	    (strcmp(prog, DEVFSADMD) == 0)) {
9727c478bd9Sstevel@tonic-gate 		err_print(DEVFSADM_USAGE);
9737c478bd9Sstevel@tonic-gate 	} else {
9747c478bd9Sstevel@tonic-gate 		err_print(COMPAT_LINK_USAGE);
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 	devfsadm_exit(1);
978537714daSvikram 	/*NOTREACHED*/
9797c478bd9Sstevel@tonic-gate }
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate static void
9827c478bd9Sstevel@tonic-gate devi_tree_walk(struct dca_impl *dcip, int flags, char *ev_subclass)
9837c478bd9Sstevel@tonic-gate {
9847c478bd9Sstevel@tonic-gate 	char *msg, *name;
9857c478bd9Sstevel@tonic-gate 	struct mlist	mlist = {0};
9867c478bd9Sstevel@tonic-gate 	di_node_t	node;
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "devi_tree_walk: root=%s, minor=%s, driver=%s,"
9897c478bd9Sstevel@tonic-gate 	    " error=%d, flags=%u\n", dcip->dci_root,
9907c478bd9Sstevel@tonic-gate 	    dcip->dci_minor ? dcip->dci_minor : "<NULL>",
9917c478bd9Sstevel@tonic-gate 	    dcip->dci_driver ? dcip->dci_driver : "<NULL>", dcip->dci_error,
9927c478bd9Sstevel@tonic-gate 	    dcip->dci_flags);
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	assert(dcip->dci_root);
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	if (dcip->dci_flags & DCA_LOAD_DRV) {
9977c478bd9Sstevel@tonic-gate 		node = di_init_driver(dcip->dci_driver, flags);
9987c478bd9Sstevel@tonic-gate 		msg = DRIVER_FAILURE;
9997c478bd9Sstevel@tonic-gate 		name = dcip->dci_driver;
10007c478bd9Sstevel@tonic-gate 	} else {
10017c478bd9Sstevel@tonic-gate 		node = di_init(dcip->dci_root, flags);
10027c478bd9Sstevel@tonic-gate 		msg = DI_INIT_FAILED;
10037c478bd9Sstevel@tonic-gate 		name = dcip->dci_root;
10047c478bd9Sstevel@tonic-gate 	}
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
10077c478bd9Sstevel@tonic-gate 		dcip->dci_error = errno;
10087c478bd9Sstevel@tonic-gate 		/*
10097c478bd9Sstevel@tonic-gate 		 * Rapid hotplugging (commonly seen during USB testing),
10107c478bd9Sstevel@tonic-gate 		 * may remove a device before the create event for it
10117c478bd9Sstevel@tonic-gate 		 * has been processed. To prevent alarming users with
10127c478bd9Sstevel@tonic-gate 		 * a superfluous message, we suppress error messages
10137c478bd9Sstevel@tonic-gate 		 * for ENXIO and hotplug.
10147c478bd9Sstevel@tonic-gate 		 */
10157c478bd9Sstevel@tonic-gate 		if (!(errno == ENXIO && (dcip->dci_flags & DCA_HOT_PLUG)))
10167c478bd9Sstevel@tonic-gate 			err_print(msg, name, strerror(dcip->dci_error));
10177c478bd9Sstevel@tonic-gate 		return;
10187c478bd9Sstevel@tonic-gate 	}
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	if (dcip->dci_flags & DCA_FLUSH_PATHINST)
10217c478bd9Sstevel@tonic-gate 		flush_path_to_inst();
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	dcip->dci_arg = &mlist;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "walking device tree\n");
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	(void) di_walk_minor(node, NULL, DI_CHECK_ALIAS, dcip,
10287c478bd9Sstevel@tonic-gate 	    check_minor_type);
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	process_deferred_links(dcip, DCA_CREATE_LINK);
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	dcip->dci_arg = NULL;
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	/*
10357c478bd9Sstevel@tonic-gate 	 * Finished creating devfs files and dev links.
10360dc974a9SCathy Zhou 	 * Log sysevent.
10377c478bd9Sstevel@tonic-gate 	 */
10387c478bd9Sstevel@tonic-gate 	if (ev_subclass)
1039f05faa4eSjacobs 		build_and_enq_event(EC_DEV_ADD, ev_subclass, dcip->dci_root,
104030294554Sphitran 		    node, dcip->dci_minor);
10417c478bd9Sstevel@tonic-gate 
104245916cd2Sjpk 	/* Add new device to device allocation database */
104345916cd2Sjpk 	if (system_labeled && update_devdb) {
104445916cd2Sjpk 		_update_devalloc_db(&devlist, 0, DA_ADD, NULL, root_dir);
104545916cd2Sjpk 		update_devdb = 0;
104645916cd2Sjpk 	}
104745916cd2Sjpk 
10487c478bd9Sstevel@tonic-gate 	di_fini(node);
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate static void
10527c478bd9Sstevel@tonic-gate process_deferred_links(struct dca_impl *dcip, int flags)
10537c478bd9Sstevel@tonic-gate {
10547c478bd9Sstevel@tonic-gate 	struct mlist	*dep;
10557c478bd9Sstevel@tonic-gate 	struct minor	*mp, *smp;
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "processing deferred links\n");
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	dep = dcip->dci_arg;
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	/*
10627c478bd9Sstevel@tonic-gate 	 * The list head is not used during the deferred create phase
10637c478bd9Sstevel@tonic-gate 	 */
10647c478bd9Sstevel@tonic-gate 	dcip->dci_arg = NULL;
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	assert(dep);
10677c478bd9Sstevel@tonic-gate 	assert((dep->head == NULL) ^ (dep->tail != NULL));
10687c478bd9Sstevel@tonic-gate 	assert(flags == DCA_FREE_LIST || flags == DCA_CREATE_LINK);
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	for (smp = NULL, mp = dep->head; mp; mp = mp->next) {
10717c478bd9Sstevel@tonic-gate 		if (flags == DCA_CREATE_LINK)
10727c478bd9Sstevel@tonic-gate 			(void) check_minor_type(mp->node, mp->minor, dcip);
10737c478bd9Sstevel@tonic-gate 		free(smp);
10747c478bd9Sstevel@tonic-gate 		smp = mp;
10757c478bd9Sstevel@tonic-gate 	}
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	free(smp);
10787c478bd9Sstevel@tonic-gate }
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate /*
10817c478bd9Sstevel@tonic-gate  * Called in non-daemon mode to take a snap shot of the devinfo tree.
10827c478bd9Sstevel@tonic-gate  * Then it calls the appropriate functions to build /devices and /dev.
10837c478bd9Sstevel@tonic-gate  * It also flushes path_to_inst.
108498a9beefShtk  * Except in the devfsadm -i (single driver case), the flags used by devfsadm
108598a9beefShtk  * needs to match DI_CACHE_SNAPSHOT_FLAGS. That will make DINFOCACHE snapshot
108698a9beefShtk  * updated.
10877c478bd9Sstevel@tonic-gate  */
10887c478bd9Sstevel@tonic-gate void
10897c478bd9Sstevel@tonic-gate process_devinfo_tree()
10907c478bd9Sstevel@tonic-gate {
109198a9beefShtk 	uint_t		flags;
10927c478bd9Sstevel@tonic-gate 	struct dca_impl	dci;
10937c478bd9Sstevel@tonic-gate 	char		name[MAXNAMELEN];
10947c478bd9Sstevel@tonic-gate 	char		*fcn = "process_devinfo_tree: ";
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "%senter\n", fcn);
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	dca_impl_init("/", NULL, &dci);
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	lock_dev();
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	/*
11037c478bd9Sstevel@tonic-gate 	 * Update kernel driver.conf cache when devfsadm/drvconfig
11047c478bd9Sstevel@tonic-gate 	 * is invoked to build /devices and /dev.
11057c478bd9Sstevel@tonic-gate 	 */
11067c478bd9Sstevel@tonic-gate 	if (load_attach_drv == TRUE)
11077c478bd9Sstevel@tonic-gate 		update_drvconf((major_t)-1);
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	if (single_drv == TRUE) {
11107c478bd9Sstevel@tonic-gate 		/*
11117c478bd9Sstevel@tonic-gate 		 * load a single driver, but walk the entire devinfo tree
11127c478bd9Sstevel@tonic-gate 		 */
11137c478bd9Sstevel@tonic-gate 		if (load_attach_drv == FALSE)
11147c478bd9Sstevel@tonic-gate 			err_print(DRV_LOAD_REQD);
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 		vprint(CHATTY_MID, "%sattaching driver (%s)\n", fcn, driver);
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 		dci.dci_flags |= DCA_LOAD_DRV;
11197c478bd9Sstevel@tonic-gate 		(void) snprintf(name, sizeof (name), "%s", driver);
11207c478bd9Sstevel@tonic-gate 		dci.dci_driver = name;
112198a9beefShtk 		flags = DINFOCPYALL | DINFOPATH;
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	} else if (load_attach_drv == TRUE) {
11247c478bd9Sstevel@tonic-gate 		/*
112545916cd2Sjpk 		 * Load and attach all drivers, then walk the entire tree.
11267c478bd9Sstevel@tonic-gate 		 * If the cache flag is set, use DINFOCACHE to get cached
11277c478bd9Sstevel@tonic-gate 		 * data.
11287c478bd9Sstevel@tonic-gate 		 */
11297c478bd9Sstevel@tonic-gate 		if (use_snapshot_cache == TRUE) {
11307c478bd9Sstevel@tonic-gate 			flags = DINFOCACHE;
11317c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "%susing snapshot cache\n", fcn);
11327c478bd9Sstevel@tonic-gate 		} else {
11337c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "%sattaching all drivers\n", fcn);
113498a9beefShtk 			flags = DI_CACHE_SNAPSHOT_FLAGS;
11353c34adc5Sramat 			if (cleanup) {
11363c34adc5Sramat 				/*
11373c34adc5Sramat 				 * remove dangling entries from /etc/devices
11383c34adc5Sramat 				 * files.
11393c34adc5Sramat 				 */
11403c34adc5Sramat 				flags |= DINFOCLEANUP;
11413c34adc5Sramat 			}
11427c478bd9Sstevel@tonic-gate 		}
114334087db8Shtk 	} else {
114434087db8Shtk 		/*
114534087db8Shtk 		 * For devlinks, disks, ports, tapes and devfsadm -n,
114634087db8Shtk 		 * just need to take a snapshot with active devices.
114734087db8Shtk 		 */
114834087db8Shtk 		vprint(CHATTY_MID, "%staking snapshot of active devices\n",
11490a653502Swroche 		    fcn);
115034087db8Shtk 		flags = DINFOCPYALL;
11517c478bd9Sstevel@tonic-gate 	}
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	if (((load_attach_drv == TRUE) || (single_drv == TRUE)) &&
11547c478bd9Sstevel@tonic-gate 	    (build_devices == TRUE)) {
11557c478bd9Sstevel@tonic-gate 		dci.dci_flags |= DCA_FLUSH_PATHINST;
11567c478bd9Sstevel@tonic-gate 	}
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 	/* handle pre-cleanup operations desired by the modules. */
11597c478bd9Sstevel@tonic-gate 	pre_and_post_cleanup(RM_PRE);
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	devi_tree_walk(&dci, flags, NULL);
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	if (dci.dci_error) {
11647c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1165537714daSvikram 		/*NOTREACHED*/
11667c478bd9Sstevel@tonic-gate 	}
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate 	/* handle post-cleanup operations desired by the modules. */
11697c478bd9Sstevel@tonic-gate 	pre_and_post_cleanup(RM_POST);
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	unlock_dev(SYNC_STATE);
11727c478bd9Sstevel@tonic-gate }
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11757c478bd9Sstevel@tonic-gate static void
11767c478bd9Sstevel@tonic-gate print_cache_signal(int signo)
11777c478bd9Sstevel@tonic-gate {
11787c478bd9Sstevel@tonic-gate 	if (signal(SIGUSR1, print_cache_signal) == SIG_ERR) {
11797c478bd9Sstevel@tonic-gate 		err_print("signal SIGUSR1 failed: %s\n", strerror(errno));
11807c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1181537714daSvikram 		/*NOTREACHED*/
11827c478bd9Sstevel@tonic-gate 	}
11837c478bd9Sstevel@tonic-gate }
11847c478bd9Sstevel@tonic-gate 
1185facf4a8dSllai static void
1186facf4a8dSllai revoke_lookup_door(void)
1187facf4a8dSllai {
1188facf4a8dSllai 	if (lookup_door_fd != -1) {
1189facf4a8dSllai 		if (door_revoke(lookup_door_fd) == -1) {
1190facf4a8dSllai 			err_print("door_revoke of %s failed - %s\n",
1191facf4a8dSllai 			    lookup_door_path, strerror(errno));
1192facf4a8dSllai 		}
1193facf4a8dSllai 	}
1194facf4a8dSllai }
1195facf4a8dSllai 
1196facf4a8dSllai /*ARGSUSED*/
1197facf4a8dSllai static void
1198facf4a8dSllai catch_exit(int signo)
1199facf4a8dSllai {
1200facf4a8dSllai 	revoke_lookup_door();
1201facf4a8dSllai }
1202facf4a8dSllai 
12037c478bd9Sstevel@tonic-gate /*
12047c478bd9Sstevel@tonic-gate  * Register with eventd for messages. Create doors for synchronous
12057c478bd9Sstevel@tonic-gate  * link creation.
12067c478bd9Sstevel@tonic-gate  */
12077c478bd9Sstevel@tonic-gate static void
12087c478bd9Sstevel@tonic-gate daemon_update(void)
12097c478bd9Sstevel@tonic-gate {
12107c478bd9Sstevel@tonic-gate 	int fd;
12117c478bd9Sstevel@tonic-gate 	char *fcn = "daemon_update: ";
12127c478bd9Sstevel@tonic-gate 	char door_file[MAXPATHLEN];
12137c478bd9Sstevel@tonic-gate 	const char *subclass_list;
12147c478bd9Sstevel@tonic-gate 	sysevent_handle_t *sysevent_hp;
12157c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "%senter\n", fcn);
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	if (signal(SIGUSR1, print_cache_signal) == SIG_ERR) {
12187c478bd9Sstevel@tonic-gate 		err_print("signal SIGUSR1 failed: %s\n", strerror(errno));
12197c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1220537714daSvikram 		/*NOTREACHED*/
12217c478bd9Sstevel@tonic-gate 	}
1222facf4a8dSllai 	if (signal(SIGTERM, catch_exit) == SIG_ERR) {
1223facf4a8dSllai 		err_print("signal SIGTERM failed: %s\n", strerror(errno));
1224facf4a8dSllai 		devfsadm_exit(1);
1225537714daSvikram 		/*NOTREACHED*/
1226facf4a8dSllai 	}
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	if (snprintf(door_file, sizeof (door_file),
1229facf4a8dSllai 	    "%s%s", attr_root ? attr_root : root_dir, DEVFSADM_SERVICE_DOOR)
1230facf4a8dSllai 	    >= sizeof (door_file)) {
12317c478bd9Sstevel@tonic-gate 		err_print("update_daemon failed to open sysevent service "
12327c478bd9Sstevel@tonic-gate 		    "door\n");
12337c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1234537714daSvikram 		/*NOTREACHED*/
12357c478bd9Sstevel@tonic-gate 	}
12367c478bd9Sstevel@tonic-gate 	if ((sysevent_hp = sysevent_open_channel_alt(
12377c478bd9Sstevel@tonic-gate 	    door_file)) == NULL) {
12387c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR,
12390a653502Swroche 		    door_file, strerror(errno));
12407c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1241537714daSvikram 		/*NOTREACHED*/
12427c478bd9Sstevel@tonic-gate 	}
12437c478bd9Sstevel@tonic-gate 	if (sysevent_bind_subscriber(sysevent_hp, event_handler) != 0) {
12447c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR,
12457c478bd9Sstevel@tonic-gate 		    door_file, strerror(errno));
12467c478bd9Sstevel@tonic-gate 		(void) sysevent_close_channel(sysevent_hp);
12477c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1248537714daSvikram 		/*NOTREACHED*/
12497c478bd9Sstevel@tonic-gate 	}
12507c478bd9Sstevel@tonic-gate 	subclass_list = EC_SUB_ALL;
12517c478bd9Sstevel@tonic-gate 	if (sysevent_register_event(sysevent_hp, EC_ALL, &subclass_list, 1)
12527c478bd9Sstevel@tonic-gate 	    != 0) {
12537c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR,
12547c478bd9Sstevel@tonic-gate 		    door_file, strerror(errno));
12557c478bd9Sstevel@tonic-gate 		(void) sysevent_unbind_subscriber(sysevent_hp);
12567c478bd9Sstevel@tonic-gate 		(void) sysevent_close_channel(sysevent_hp);
12577c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1258537714daSvikram 		/*NOTREACHED*/
12597c478bd9Sstevel@tonic-gate 	}
1260facf4a8dSllai 	if (snprintf(door_file, sizeof (door_file), "%s/%s",
1261facf4a8dSllai 	    etc_dev_dir, DEVFSADM_SYNCH_DOOR) >= sizeof (door_file)) {
1262facf4a8dSllai 		err_print(CANT_CREATE_DOOR, DEVFSADM_SYNCH_DOOR,
12637c478bd9Sstevel@tonic-gate 		    strerror(ENAMETOOLONG));
12647c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1265537714daSvikram 		/*NOTREACHED*/
12667c478bd9Sstevel@tonic-gate 	}
1267facf4a8dSllai 
12687c478bd9Sstevel@tonic-gate 	(void) s_unlink(door_file);
1269facf4a8dSllai 	if ((fd = open(door_file, O_RDWR | O_CREAT, SYNCH_DOOR_PERMS)) == -1) {
1270facf4a8dSllai 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
12717c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1272537714daSvikram 		/*NOTREACHED*/
12737c478bd9Sstevel@tonic-gate 	}
12747c478bd9Sstevel@tonic-gate 	(void) close(fd);
1275facf4a8dSllai 
1276facf4a8dSllai 	if ((fd = door_create(sync_handler, NULL,
12777c478bd9Sstevel@tonic-gate 	    DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
1278facf4a8dSllai 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
12797c478bd9Sstevel@tonic-gate 		(void) s_unlink(door_file);
12807c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1281537714daSvikram 		/*NOTREACHED*/
12827c478bd9Sstevel@tonic-gate 	}
1283facf4a8dSllai 
12847c478bd9Sstevel@tonic-gate 	if (fattach(fd, door_file) == -1) {
1285facf4a8dSllai 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
12867c478bd9Sstevel@tonic-gate 		(void) s_unlink(door_file);
12877c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1288537714daSvikram 		/*NOTREACHED*/
12897c478bd9Sstevel@tonic-gate 	}
12907c478bd9Sstevel@tonic-gate 
1291facf4a8dSllai 	/*
1292facf4a8dSllai 	 * devname_lookup_door
1293facf4a8dSllai 	 */
1294facf4a8dSllai 	if (snprintf(door_file, sizeof (door_file), "%s/%s",
1295facf4a8dSllai 	    etc_dev_dir, DEVNAME_LOOKUP_DOOR) >= sizeof (door_file)) {
1296facf4a8dSllai 		err_print(CANT_CREATE_DOOR, DEVNAME_LOOKUP_DOOR,
1297facf4a8dSllai 		    strerror(ENAMETOOLONG));
1298facf4a8dSllai 		devfsadm_exit(1);
1299537714daSvikram 		/*NOTREACHED*/
1300facf4a8dSllai 	}
13017c478bd9Sstevel@tonic-gate 
13027c478bd9Sstevel@tonic-gate 	(void) s_unlink(door_file);
1303facf4a8dSllai 	if ((fd = open(door_file, O_RDWR | O_CREAT, S_IRUSR|S_IWUSR)) == -1) {
13047c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
13057c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1306537714daSvikram 		/*NOTREACHED*/
13077c478bd9Sstevel@tonic-gate 	}
13087c478bd9Sstevel@tonic-gate 	(void) close(fd);
13097c478bd9Sstevel@tonic-gate 
1310facf4a8dSllai 	if ((fd = door_create(devname_lookup_handler, NULL,
1311facf4a8dSllai 	    DOOR_REFUSE_DESC)) == -1) {
13127c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
13137c478bd9Sstevel@tonic-gate 		(void) s_unlink(door_file);
13147c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1315537714daSvikram 		/*NOTREACHED*/
13167c478bd9Sstevel@tonic-gate 	}
13177c478bd9Sstevel@tonic-gate 
1318facf4a8dSllai 	(void) fdetach(door_file);
1319facf4a8dSllai 	lookup_door_path = s_strdup(door_file);
1320facf4a8dSllai retry:
13217c478bd9Sstevel@tonic-gate 	if (fattach(fd, door_file) == -1) {
1322facf4a8dSllai 		if (errno == EBUSY)
1323facf4a8dSllai 			goto retry;
13247c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
13257c478bd9Sstevel@tonic-gate 		(void) s_unlink(door_file);
13267c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1327537714daSvikram 		/*NOTREACHED*/
13287c478bd9Sstevel@tonic-gate 	}
1329facf4a8dSllai 	lookup_door_fd = fd;
13307c478bd9Sstevel@tonic-gate 
1331facf4a8dSllai 	/* pass down the door name to kernel for door_ki_open */
1332facf4a8dSllai 	if (devname_kcall(MODDEVNAME_LOOKUPDOOR, (void *)door_file) != 0)
1333facf4a8dSllai 		err_print(DEVNAME_CONTACT_FAILED, strerror(errno));
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "%spausing\n", fcn);
13367c478bd9Sstevel@tonic-gate 	for (;;) {
13377c478bd9Sstevel@tonic-gate 		(void) pause();
13387c478bd9Sstevel@tonic-gate 	}
13397c478bd9Sstevel@tonic-gate }
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13427c478bd9Sstevel@tonic-gate static void
13437c478bd9Sstevel@tonic-gate sync_handler(void *cookie, char *ap, size_t asize,
13447c478bd9Sstevel@tonic-gate     door_desc_t *dp, uint_t ndesc)
13457c478bd9Sstevel@tonic-gate {
13467c478bd9Sstevel@tonic-gate 	door_cred_t	dcred;
13477c478bd9Sstevel@tonic-gate 	struct dca_off	*dcp, rdca;
13487c478bd9Sstevel@tonic-gate 	struct dca_impl dci;
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	/*
13517c478bd9Sstevel@tonic-gate 	 * Must be root to make this call
13527c478bd9Sstevel@tonic-gate 	 * If caller is not root, don't touch its data.
13537c478bd9Sstevel@tonic-gate 	 */
13547c478bd9Sstevel@tonic-gate 	if (door_cred(&dcred) != 0 || dcred.dc_euid != 0) {
13557c478bd9Sstevel@tonic-gate 		dcp = &rdca;
13567c478bd9Sstevel@tonic-gate 		dcp->dca_error = EPERM;
13577c478bd9Sstevel@tonic-gate 		goto out;
13587c478bd9Sstevel@tonic-gate 	}
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	assert(ap);
13617c478bd9Sstevel@tonic-gate 	assert(asize == sizeof (*dcp));
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 	dcp = (void *)ap;
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	/*
13667c478bd9Sstevel@tonic-gate 	 * Root is always present and is the first component of "name" member
13677c478bd9Sstevel@tonic-gate 	 */
13687c478bd9Sstevel@tonic-gate 	assert(dcp->dca_root == 0);
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	/*
13717c478bd9Sstevel@tonic-gate 	 * The structure passed in by the door_client uses offsets
13727c478bd9Sstevel@tonic-gate 	 * instead of pointers to work across address space boundaries.
13737c478bd9Sstevel@tonic-gate 	 * Now copy the data into a structure (dca_impl) which uses
13747c478bd9Sstevel@tonic-gate 	 * pointers.
13757c478bd9Sstevel@tonic-gate 	 */
13767c478bd9Sstevel@tonic-gate 	dci.dci_root = &dcp->dca_name[dcp->dca_root];
13777c478bd9Sstevel@tonic-gate 	dci.dci_minor = dcp->dca_minor ? &dcp->dca_name[dcp->dca_minor] : NULL;
13787c478bd9Sstevel@tonic-gate 	dci.dci_driver =
13797c478bd9Sstevel@tonic-gate 	    dcp->dca_driver ? &dcp->dca_name[dcp->dca_driver] : NULL;
13807c478bd9Sstevel@tonic-gate 	dci.dci_error = 0;
13817c478bd9Sstevel@tonic-gate 	dci.dci_flags = dcp->dca_flags | (dci.dci_driver ? DCA_LOAD_DRV : 0);
13827c478bd9Sstevel@tonic-gate 	dci.dci_arg = NULL;
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	lock_dev();
13857c478bd9Sstevel@tonic-gate 	devi_tree_walk(&dci, DINFOCPYALL, NULL);
13867c478bd9Sstevel@tonic-gate 	dcp->dca_error = dci.dci_error;
13877c478bd9Sstevel@tonic-gate 
1388ff2aee48Scth 	if (dcp->dca_flags & DCA_DEVLINK_SYNC)
1389ff2aee48Scth 		unlock_dev(SYNC_STATE);
1390ff2aee48Scth 	else
1391ff2aee48Scth 		unlock_dev(CACHE_STATE);
13927c478bd9Sstevel@tonic-gate 
1393ff2aee48Scth out:	(void) door_return((char *)dcp, sizeof (*dcp), NULL, 0);
13947c478bd9Sstevel@tonic-gate }
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate static void
13977c478bd9Sstevel@tonic-gate lock_dev(void)
13987c478bd9Sstevel@tonic-gate {
13997c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "lock_dev(): entered\n");
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate 	if (build_dev == FALSE)
14027c478bd9Sstevel@tonic-gate 		return;
14037c478bd9Sstevel@tonic-gate 
14047c478bd9Sstevel@tonic-gate 	/* lockout other threads from /dev */
14050a653502Swroche 	while (sema_wait(&dev_sema) != 0)
14060a653502Swroche 		;
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	/*
14097c478bd9Sstevel@tonic-gate 	 * Lock out other devfsadm processes from /dev.
14107c478bd9Sstevel@tonic-gate 	 * If this wasn't the last process to run,
14117c478bd9Sstevel@tonic-gate 	 * clear caches
14127c478bd9Sstevel@tonic-gate 	 */
14137c478bd9Sstevel@tonic-gate 	if (enter_dev_lock() != getpid()) {
14147c478bd9Sstevel@tonic-gate 		invalidate_enumerate_cache();
14157c478bd9Sstevel@tonic-gate 		rm_all_links_from_cache();
14167c478bd9Sstevel@tonic-gate 		(void) di_devlink_close(&devlink_cache, DI_LINK_ERROR);
1417f05faa4eSjacobs 
1418f05faa4eSjacobs 		/* send any sysevents that were queued up. */
1419f05faa4eSjacobs 		process_syseventq();
14207c478bd9Sstevel@tonic-gate 	}
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 	/*
14237c478bd9Sstevel@tonic-gate 	 * (re)load the  reverse links database if not
14247c478bd9Sstevel@tonic-gate 	 * already cached.
14257c478bd9Sstevel@tonic-gate 	 */
14267c478bd9Sstevel@tonic-gate 	if (devlink_cache == NULL)
14277c478bd9Sstevel@tonic-gate 		devlink_cache = di_devlink_open(root_dir, 0);
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	/*
14307c478bd9Sstevel@tonic-gate 	 * If modules were unloaded, reload them.  Also use module status
14317c478bd9Sstevel@tonic-gate 	 * as an indication that we should check to see if other binding
14327c478bd9Sstevel@tonic-gate 	 * files need to be reloaded.
14337c478bd9Sstevel@tonic-gate 	 */
14347c478bd9Sstevel@tonic-gate 	if (module_head == NULL) {
14357c478bd9Sstevel@tonic-gate 		load_modules();
14367c478bd9Sstevel@tonic-gate 		read_minor_perm_file();
14377c478bd9Sstevel@tonic-gate 		read_driver_aliases_file();
14387c478bd9Sstevel@tonic-gate 		read_devlinktab_file();
14397c478bd9Sstevel@tonic-gate 		read_logindevperm_file();
14408d483882Smlf 		read_enumerate_file();
14417c478bd9Sstevel@tonic-gate 	}
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 	if (module_head != NULL)
14447c478bd9Sstevel@tonic-gate 		return;
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate 	if (strcmp(prog, DEVLINKS) == 0) {
14477c478bd9Sstevel@tonic-gate 		if (devlinktab_list == NULL) {
14487c478bd9Sstevel@tonic-gate 			err_print(NO_LINKTAB, devlinktab_file);
14497c478bd9Sstevel@tonic-gate 			err_print(NO_MODULES, module_dirs);
14507c478bd9Sstevel@tonic-gate 			err_print(ABORTING);
14517c478bd9Sstevel@tonic-gate 			devfsadm_exit(1);
1452537714daSvikram 			/*NOTREACHED*/
14537c478bd9Sstevel@tonic-gate 		}
14547c478bd9Sstevel@tonic-gate 	} else {
14557c478bd9Sstevel@tonic-gate 		err_print(NO_MODULES, module_dirs);
14567c478bd9Sstevel@tonic-gate 		if (strcmp(prog, DEVFSADM) == 0) {
14577c478bd9Sstevel@tonic-gate 			err_print(MODIFY_PATH);
14587c478bd9Sstevel@tonic-gate 		}
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate }
14617c478bd9Sstevel@tonic-gate 
1462ff2aee48Scth /*
1463ff2aee48Scth  * Unlock the device.  If we are processing a CACHE_STATE call, we signal a
1464ff2aee48Scth  * minor_fini_thread delayed SYNC_STATE at the end of the call.  If we are
1465ff2aee48Scth  * processing a SYNC_STATE call, we cancel any minor_fini_thread SYNC_STATE
1466ff2aee48Scth  * at both the start and end of the call since we will be doing the SYNC_STATE.
1467ff2aee48Scth  */
14687c478bd9Sstevel@tonic-gate static void
14697c478bd9Sstevel@tonic-gate unlock_dev(int flag)
14707c478bd9Sstevel@tonic-gate {
1471ff2aee48Scth 	assert(flag == SYNC_STATE || flag == CACHE_STATE);
1472ff2aee48Scth 
14737c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "unlock_dev(): entered\n");
14747c478bd9Sstevel@tonic-gate 
1475ff2aee48Scth 	/* If we are starting a SYNC_STATE, cancel minor_fini_thread SYNC */
1476ff2aee48Scth 	if (flag == SYNC_STATE) {
1477ff2aee48Scth 		(void) mutex_lock(&minor_fini_mutex);
1478ff2aee48Scth 		minor_fini_canceled = TRUE;
1479ff2aee48Scth 		minor_fini_delayed = FALSE;
1480ff2aee48Scth 		(void) mutex_unlock(&minor_fini_mutex);
1481ff2aee48Scth 	}
1482ff2aee48Scth 
14837c478bd9Sstevel@tonic-gate 	if (build_dev == FALSE)
14847c478bd9Sstevel@tonic-gate 		return;
14857c478bd9Sstevel@tonic-gate 
1486568e756aSvikram 	if (devlink_cache == NULL) {
1487568e756aSvikram 		err_print(NO_DEVLINK_CACHE);
1488568e756aSvikram 	}
14897c478bd9Sstevel@tonic-gate 	assert(devlink_cache);
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	if (flag == SYNC_STATE) {
14927c478bd9Sstevel@tonic-gate 		unload_modules();
14937c478bd9Sstevel@tonic-gate 		if (update_database)
14947c478bd9Sstevel@tonic-gate 			(void) di_devlink_update(devlink_cache);
14957c478bd9Sstevel@tonic-gate 		(void) di_devlink_close(&devlink_cache, 0);
1496f05faa4eSjacobs 
1497f05faa4eSjacobs 		/*
1498f05faa4eSjacobs 		 * now that the devlinks db cache has been flushed, it is safe
1499f05faa4eSjacobs 		 * to send any sysevents that were queued up.
1500f05faa4eSjacobs 		 */
1501f05faa4eSjacobs 		process_syseventq();
15027c478bd9Sstevel@tonic-gate 	}
15037c478bd9Sstevel@tonic-gate 
1504537714daSvikram 	exit_dev_lock(0);
15057c478bd9Sstevel@tonic-gate 
1506ff2aee48Scth 	(void) mutex_lock(&minor_fini_mutex);
1507ff2aee48Scth 	if (flag == SYNC_STATE) {
1508ff2aee48Scth 		/* We did a SYNC_STATE, cancel minor_fini_thread SYNC */
1509ff2aee48Scth 		minor_fini_canceled = TRUE;
1510ff2aee48Scth 		minor_fini_delayed = FALSE;
1511ff2aee48Scth 	} else {
1512ff2aee48Scth 		/* We did a CACHE_STATE, start delayed minor_fini_thread SYNC */
1513ff2aee48Scth 		minor_fini_canceled = FALSE;
1514ff2aee48Scth 		minor_fini_delayed = TRUE;
1515ff2aee48Scth 		(void) cond_signal(&minor_fini_cv);
1516ff2aee48Scth 	}
1517ff2aee48Scth 	(void) mutex_unlock(&minor_fini_mutex);
1518ff2aee48Scth 
15197c478bd9Sstevel@tonic-gate 	(void) sema_post(&dev_sema);
15207c478bd9Sstevel@tonic-gate }
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate /*
15237c478bd9Sstevel@tonic-gate  * Check that if -r is set, it is not any part of a zone--- that is, that
15247c478bd9Sstevel@tonic-gate  * the zonepath is not a substring of the root path.
15257c478bd9Sstevel@tonic-gate  */
15267c478bd9Sstevel@tonic-gate static int
15277c478bd9Sstevel@tonic-gate zone_pathcheck(char *checkpath)
15287c478bd9Sstevel@tonic-gate {
15297c478bd9Sstevel@tonic-gate 	void		*dlhdl = NULL;
15307c478bd9Sstevel@tonic-gate 	char		*name;
15317c478bd9Sstevel@tonic-gate 	char		root[MAXPATHLEN]; /* resolved devfsadm root path */
15327c478bd9Sstevel@tonic-gate 	char		zroot[MAXPATHLEN]; /* zone root path */
15337c478bd9Sstevel@tonic-gate 	char		rzroot[MAXPATHLEN]; /* resolved zone root path */
1534facf4a8dSllai 	char		tmp[MAXPATHLEN];
15357c478bd9Sstevel@tonic-gate 	FILE		*cookie;
15367c478bd9Sstevel@tonic-gate 	int		err = DEVFSADM_SUCCESS;
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 	if (checkpath[0] == '\0')
15397c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	/*
15427c478bd9Sstevel@tonic-gate 	 * Check if zones is available on this system.
15437c478bd9Sstevel@tonic-gate 	 */
15447c478bd9Sstevel@tonic-gate 	if ((dlhdl = dlopen(LIBZONECFG_PATH, RTLD_LAZY)) == NULL) {
15457c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
15467c478bd9Sstevel@tonic-gate 	}
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate 	bzero(root, sizeof (root));
15497c478bd9Sstevel@tonic-gate 	if (resolvepath(checkpath, root, sizeof (root) - 1) == -1) {
15507c478bd9Sstevel@tonic-gate 		/*
1551facf4a8dSllai 		 * In this case the user has done "devfsadm -r" on some path
15527c478bd9Sstevel@tonic-gate 		 * which does not yet exist, or we got some other misc. error.
15537c478bd9Sstevel@tonic-gate 		 * We punt and don't resolve the path in this case.
15547c478bd9Sstevel@tonic-gate 		 */
15557c478bd9Sstevel@tonic-gate 		(void) strlcpy(root, checkpath, sizeof (root));
15567c478bd9Sstevel@tonic-gate 	}
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	if (strlen(root) > 0 && (root[strlen(root) - 1] != '/')) {
15597c478bd9Sstevel@tonic-gate 		(void) snprintf(tmp, sizeof (tmp), "%s/", root);
15607c478bd9Sstevel@tonic-gate 		(void) strlcpy(root, tmp, sizeof (root));
15617c478bd9Sstevel@tonic-gate 	}
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
15647c478bd9Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
15657c478bd9Sstevel@tonic-gate 		/* Skip the global zone */
15667c478bd9Sstevel@tonic-gate 		if (strcmp(name, GLOBAL_ZONENAME) == 0) {
15677c478bd9Sstevel@tonic-gate 			free(name);
15687c478bd9Sstevel@tonic-gate 			continue;
15697c478bd9Sstevel@tonic-gate 		}
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 		if (zone_get_zonepath(name, zroot, sizeof (zroot)) != Z_OK) {
15727c478bd9Sstevel@tonic-gate 			free(name);
15737c478bd9Sstevel@tonic-gate 			continue;
15747c478bd9Sstevel@tonic-gate 		}
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 		bzero(rzroot, sizeof (rzroot));
15777c478bd9Sstevel@tonic-gate 		if (resolvepath(zroot, rzroot, sizeof (rzroot) - 1) == -1) {
15787c478bd9Sstevel@tonic-gate 			/*
15797c478bd9Sstevel@tonic-gate 			 * Zone path doesn't exist, or other misc error,
15807c478bd9Sstevel@tonic-gate 			 * so we try using the non-resolved pathname.
15817c478bd9Sstevel@tonic-gate 			 */
15827c478bd9Sstevel@tonic-gate 			(void) strlcpy(rzroot, zroot, sizeof (rzroot));
15837c478bd9Sstevel@tonic-gate 		}
15847c478bd9Sstevel@tonic-gate 		if (strlen(rzroot) > 0 && (rzroot[strlen(rzroot) - 1] != '/')) {
15857c478bd9Sstevel@tonic-gate 			(void) snprintf(tmp, sizeof (tmp), "%s/", rzroot);
15867c478bd9Sstevel@tonic-gate 			(void) strlcpy(rzroot, tmp, sizeof (rzroot));
15877c478bd9Sstevel@tonic-gate 		}
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate 		/*
15907c478bd9Sstevel@tonic-gate 		 * Finally, the comparison.  If the zone root path is a
15917c478bd9Sstevel@tonic-gate 		 * leading substring of the root path, fail.
15927c478bd9Sstevel@tonic-gate 		 */
15937c478bd9Sstevel@tonic-gate 		if (strncmp(rzroot, root, strlen(rzroot)) == 0) {
15947c478bd9Sstevel@tonic-gate 			err_print(ZONE_PATHCHECK, root, name);
15957c478bd9Sstevel@tonic-gate 			err = DEVFSADM_FAILURE;
15967c478bd9Sstevel@tonic-gate 			free(name);
15977c478bd9Sstevel@tonic-gate 			break;
15987c478bd9Sstevel@tonic-gate 		}
15997c478bd9Sstevel@tonic-gate 		free(name);
16007c478bd9Sstevel@tonic-gate 	}
16017c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
16027c478bd9Sstevel@tonic-gate 	(void) dlclose(dlhdl);
16037c478bd9Sstevel@tonic-gate 	return (err);
16047c478bd9Sstevel@tonic-gate }
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate /*
16077c478bd9Sstevel@tonic-gate  *  Called by the daemon when it receives an event from the devfsadm SLM
16087c478bd9Sstevel@tonic-gate  *  to syseventd.
16097c478bd9Sstevel@tonic-gate  *
16107c478bd9Sstevel@tonic-gate  *  The devfsadm SLM uses a private event channel for communication to
16117c478bd9Sstevel@tonic-gate  *  devfsadmd set-up via private libsysevent interfaces.  This handler is
16127c478bd9Sstevel@tonic-gate  *  used to bind to the devfsadmd channel for event delivery.
16137c478bd9Sstevel@tonic-gate  *  The devfsadmd SLM insures single calls to this routine as well as
16147c478bd9Sstevel@tonic-gate  *  synchronized event delivery.
16157c478bd9Sstevel@tonic-gate  *
16167c478bd9Sstevel@tonic-gate  */
16177c478bd9Sstevel@tonic-gate static void
16187c478bd9Sstevel@tonic-gate event_handler(sysevent_t *ev)
16197c478bd9Sstevel@tonic-gate {
16207c478bd9Sstevel@tonic-gate 	char *path;
16217c478bd9Sstevel@tonic-gate 	char *minor;
16227c478bd9Sstevel@tonic-gate 	char *subclass;
16237c478bd9Sstevel@tonic-gate 	char *dev_ev_subclass;
16247c478bd9Sstevel@tonic-gate 	char *driver_name;
16257c478bd9Sstevel@tonic-gate 	nvlist_t *attr_list = NULL;
16267c478bd9Sstevel@tonic-gate 	int err = 0;
16277c478bd9Sstevel@tonic-gate 	int instance;
16287c478bd9Sstevel@tonic-gate 	int branch_event = 0;
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	subclass = sysevent_get_subclass_name(ev);
16317c478bd9Sstevel@tonic-gate 	vprint(EVENT_MID, "event_handler: %s id:0X%llx\n",
16327c478bd9Sstevel@tonic-gate 	    subclass, sysevent_get_seq(ev));
16337c478bd9Sstevel@tonic-gate 
1634facf4a8dSllai 	if (strcmp(subclass, ESC_DEVFS_START) == 0) {
1635facf4a8dSllai 		return;
1636facf4a8dSllai 	}
1637facf4a8dSllai 
16387c478bd9Sstevel@tonic-gate 	/* Check if event is an instance modification */
16397c478bd9Sstevel@tonic-gate 	if (strcmp(subclass, ESC_DEVFS_INSTANCE_MOD) == 0) {
16407c478bd9Sstevel@tonic-gate 		devfs_instance_mod();
16417c478bd9Sstevel@tonic-gate 		return;
16427c478bd9Sstevel@tonic-gate 	}
16437c478bd9Sstevel@tonic-gate 	if (sysevent_get_attr_list(ev, &attr_list) != 0) {
16447c478bd9Sstevel@tonic-gate 		vprint(EVENT_MID, "event_handler: can not get attr list\n");
16457c478bd9Sstevel@tonic-gate 		return;
16467c478bd9Sstevel@tonic-gate 	}
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate 	if (strcmp(subclass, ESC_DEVFS_DEVI_ADD) == 0 ||
16497c478bd9Sstevel@tonic-gate 	    strcmp(subclass, ESC_DEVFS_DEVI_REMOVE) == 0 ||
16507c478bd9Sstevel@tonic-gate 	    strcmp(subclass, ESC_DEVFS_MINOR_CREATE) == 0 ||
16517c478bd9Sstevel@tonic-gate 	    strcmp(subclass, ESC_DEVFS_MINOR_REMOVE) == 0) {
16527c478bd9Sstevel@tonic-gate 		if ((err = nvlist_lookup_string(attr_list, DEVFS_PATHNAME,
16537c478bd9Sstevel@tonic-gate 		    &path)) != 0)
16547c478bd9Sstevel@tonic-gate 			goto out;
16557c478bd9Sstevel@tonic-gate 
165630294554Sphitran 		if (nvlist_lookup_string(attr_list, DEVFS_DEVI_CLASS,
165730294554Sphitran 		    &dev_ev_subclass) != 0)
165830294554Sphitran 			dev_ev_subclass = NULL;
16597c478bd9Sstevel@tonic-gate 
166030294554Sphitran 		if (nvlist_lookup_string(attr_list, DEVFS_DRIVER_NAME,
166130294554Sphitran 		    &driver_name) != 0)
166230294554Sphitran 			driver_name = NULL;
16637c478bd9Sstevel@tonic-gate 
166430294554Sphitran 		if (nvlist_lookup_int32(attr_list, DEVFS_INSTANCE,
166530294554Sphitran 		    &instance) != 0)
166630294554Sphitran 			instance = -1;
16677c478bd9Sstevel@tonic-gate 
166830294554Sphitran 		if (nvlist_lookup_int32(attr_list, DEVFS_BRANCH_EVENT,
166930294554Sphitran 		    &branch_event) != 0)
167030294554Sphitran 			branch_event = 0;
16717c478bd9Sstevel@tonic-gate 
167230294554Sphitran 		if (nvlist_lookup_string(attr_list, DEVFS_MINOR_NAME,
167330294554Sphitran 		    &minor) != 0)
167430294554Sphitran 			minor = NULL;
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 		lock_dev();
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 		if (strcmp(ESC_DEVFS_DEVI_ADD, subclass) == 0) {
16797c478bd9Sstevel@tonic-gate 			add_minor_pathname(path, NULL, dev_ev_subclass);
16807c478bd9Sstevel@tonic-gate 			if (branch_event) {
1681f05faa4eSjacobs 				build_and_enq_event(EC_DEV_BRANCH,
168230294554Sphitran 				    ESC_DEV_BRANCH_ADD, path, DI_NODE_NIL,
168330294554Sphitran 				    NULL);
16847c478bd9Sstevel@tonic-gate 			}
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 		} else if (strcmp(ESC_DEVFS_MINOR_CREATE, subclass) == 0) {
168730294554Sphitran 			add_minor_pathname(path, minor, dev_ev_subclass);
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate 		} else if (strcmp(ESC_DEVFS_MINOR_REMOVE, subclass) == 0) {
169030294554Sphitran 			hot_cleanup(path, minor, dev_ev_subclass, driver_name,
169130294554Sphitran 			    instance);
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 		} else { /* ESC_DEVFS_DEVI_REMOVE */
16947c478bd9Sstevel@tonic-gate 			hot_cleanup(path, NULL, dev_ev_subclass,
16957c478bd9Sstevel@tonic-gate 			    driver_name, instance);
16967c478bd9Sstevel@tonic-gate 			if (branch_event) {
1697f05faa4eSjacobs 				build_and_enq_event(EC_DEV_BRANCH,
169830294554Sphitran 				    ESC_DEV_BRANCH_REMOVE, path, DI_NODE_NIL,
169930294554Sphitran 				    NULL);
17007c478bd9Sstevel@tonic-gate 			}
17017c478bd9Sstevel@tonic-gate 		}
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 		unlock_dev(CACHE_STATE);
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 	} else if (strcmp(subclass, ESC_DEVFS_BRANCH_ADD) == 0 ||
17067c478bd9Sstevel@tonic-gate 	    strcmp(subclass, ESC_DEVFS_BRANCH_REMOVE) == 0) {
17077c478bd9Sstevel@tonic-gate 		if ((err = nvlist_lookup_string(attr_list,
17087c478bd9Sstevel@tonic-gate 		    DEVFS_PATHNAME, &path)) != 0)
17097c478bd9Sstevel@tonic-gate 			goto out;
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 		/* just log ESC_DEV_BRANCH... event */
17127c478bd9Sstevel@tonic-gate 		if (strcmp(subclass, ESC_DEVFS_BRANCH_ADD) == 0)
17137c478bd9Sstevel@tonic-gate 			dev_ev_subclass = ESC_DEV_BRANCH_ADD;
17147c478bd9Sstevel@tonic-gate 		else
17157c478bd9Sstevel@tonic-gate 			dev_ev_subclass = ESC_DEV_BRANCH_REMOVE;
17167c478bd9Sstevel@tonic-gate 
1717d2596142Scth 		lock_dev();
1718f05faa4eSjacobs 		build_and_enq_event(EC_DEV_BRANCH, dev_ev_subclass, path,
171930294554Sphitran 		    DI_NODE_NIL, NULL);
1720d2596142Scth 		unlock_dev(CACHE_STATE);
17217c478bd9Sstevel@tonic-gate 	} else
17227c478bd9Sstevel@tonic-gate 		err_print(UNKNOWN_EVENT, subclass);
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate out:
17257c478bd9Sstevel@tonic-gate 	if (err)
17267c478bd9Sstevel@tonic-gate 		err_print(EVENT_ATTR_LOOKUP_FAILED, strerror(err));
17277c478bd9Sstevel@tonic-gate 	nvlist_free(attr_list);
17287c478bd9Sstevel@tonic-gate }
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate static void
17317c478bd9Sstevel@tonic-gate dca_impl_init(char *root, char *minor, struct dca_impl *dcip)
17327c478bd9Sstevel@tonic-gate {
17337c478bd9Sstevel@tonic-gate 	assert(root);
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 	dcip->dci_root = root;
17367c478bd9Sstevel@tonic-gate 	dcip->dci_minor = minor;
17377c478bd9Sstevel@tonic-gate 	dcip->dci_driver = NULL;
17387c478bd9Sstevel@tonic-gate 	dcip->dci_error = 0;
17397c478bd9Sstevel@tonic-gate 	dcip->dci_flags = 0;
17407c478bd9Sstevel@tonic-gate 	dcip->dci_arg = NULL;
17417c478bd9Sstevel@tonic-gate }
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate /*
17447c478bd9Sstevel@tonic-gate  *  Kernel logs a message when a devinfo node is attached.  Try to create
17457c478bd9Sstevel@tonic-gate  *  /dev and /devices for each minor node.  minorname can be NULL.
17467c478bd9Sstevel@tonic-gate  */
17477c478bd9Sstevel@tonic-gate void
17487c478bd9Sstevel@tonic-gate add_minor_pathname(char *node, char *minor, char *ev_subclass)
17497c478bd9Sstevel@tonic-gate {
17507c478bd9Sstevel@tonic-gate 	struct dca_impl	dci;
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "add_minor_pathname: node_path=%s minor=%s\n",
17537c478bd9Sstevel@tonic-gate 	    node, minor ? minor : "NULL");
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	dca_impl_init(node, minor, &dci);
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	/*
17587c478bd9Sstevel@tonic-gate 	 * Restrict hotplug link creation if daemon
17597c478bd9Sstevel@tonic-gate 	 * started  with -i option.
17607c478bd9Sstevel@tonic-gate 	 */
17617c478bd9Sstevel@tonic-gate 	if (single_drv == TRUE) {
17627c478bd9Sstevel@tonic-gate 		dci.dci_driver = driver;
17637c478bd9Sstevel@tonic-gate 	}
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	/*
17660dc974a9SCathy Zhou 	 * We are being invoked in response to a hotplug event.
17677c478bd9Sstevel@tonic-gate 	 */
17687c478bd9Sstevel@tonic-gate 	dci.dci_flags = DCA_HOT_PLUG | DCA_CHECK_TYPE;
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate 	devi_tree_walk(&dci, DINFOPROP|DINFOMINOR, ev_subclass);
17717c478bd9Sstevel@tonic-gate }
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate static di_node_t
17747c478bd9Sstevel@tonic-gate find_clone_node()
17757c478bd9Sstevel@tonic-gate {
17767c478bd9Sstevel@tonic-gate 	static di_node_t clone_node = DI_NODE_NIL;
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	if (clone_node == DI_NODE_NIL)
17797c478bd9Sstevel@tonic-gate 		clone_node = di_init("/pseudo/clone@0", DINFOPROP);
17807c478bd9Sstevel@tonic-gate 	return (clone_node);
17817c478bd9Sstevel@tonic-gate }
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate static int
17847c478bd9Sstevel@tonic-gate is_descendent_of(di_node_t node, char *driver)
17857c478bd9Sstevel@tonic-gate {
17867c478bd9Sstevel@tonic-gate 	while (node != DI_NODE_NIL) {
17877c478bd9Sstevel@tonic-gate 		char *drv = di_driver_name(node);
17887c478bd9Sstevel@tonic-gate 		if (strcmp(drv, driver) == 0)
17897c478bd9Sstevel@tonic-gate 			return (1);
17907c478bd9Sstevel@tonic-gate 		node = di_parent_node(node);
17917c478bd9Sstevel@tonic-gate 	}
17927c478bd9Sstevel@tonic-gate 	return (0);
17937c478bd9Sstevel@tonic-gate }
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate /*
17967c478bd9Sstevel@tonic-gate  * Checks the minor type.  If it is an alias node, then lookup
17977c478bd9Sstevel@tonic-gate  * the real node/minor first, then call minor_process() to
17987c478bd9Sstevel@tonic-gate  * do the real work.
17997c478bd9Sstevel@tonic-gate  */
18007c478bd9Sstevel@tonic-gate static int
18017c478bd9Sstevel@tonic-gate check_minor_type(di_node_t node, di_minor_t minor, void *arg)
18027c478bd9Sstevel@tonic-gate {
18037c478bd9Sstevel@tonic-gate 	ddi_minor_type	minor_type;
18047c478bd9Sstevel@tonic-gate 	di_node_t	clone_node;
18057c478bd9Sstevel@tonic-gate 	char		*mn;
18067c478bd9Sstevel@tonic-gate 	char		*nt;
18077c478bd9Sstevel@tonic-gate 	struct mlist	*dep;
18087c478bd9Sstevel@tonic-gate 	struct dca_impl	*dcip = arg;
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	assert(dcip);
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	dep = dcip->dci_arg;
18137c478bd9Sstevel@tonic-gate 
18147c478bd9Sstevel@tonic-gate 	mn = di_minor_name(minor);
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	/*
18177c478bd9Sstevel@tonic-gate 	 * We match driver here instead of in minor_process
18187c478bd9Sstevel@tonic-gate 	 * as we want the actual driver name. This check is
18197c478bd9Sstevel@tonic-gate 	 * unnecessary during deferred processing.
18207c478bd9Sstevel@tonic-gate 	 */
18217c478bd9Sstevel@tonic-gate 	if (dep &&
18227c478bd9Sstevel@tonic-gate 	    ((dcip->dci_driver && !is_descendent_of(node, dcip->dci_driver)) ||
18237c478bd9Sstevel@tonic-gate 	    (dcip->dci_minor && strcmp(mn, dcip->dci_minor)))) {
18247c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
18257c478bd9Sstevel@tonic-gate 	}
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 	if ((dcip->dci_flags & DCA_CHECK_TYPE) &&
18287c478bd9Sstevel@tonic-gate 	    (nt = di_minor_nodetype(minor)) &&
1829210db224Sericheng 	    (strcmp(nt, DDI_NT_NET) == 0)) {
18307c478bd9Sstevel@tonic-gate 		dcip->dci_flags &= ~DCA_CHECK_TYPE;
18317c478bd9Sstevel@tonic-gate 	}
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 	minor_type = di_minor_type(minor);
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 	if (minor_type == DDM_MINOR) {
18367c478bd9Sstevel@tonic-gate 		minor_process(node, minor, dep);
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 	} else if (minor_type == DDM_ALIAS) {
18397c478bd9Sstevel@tonic-gate 		struct mlist *cdep, clone_del = {0};
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate 		clone_node = find_clone_node();
18427c478bd9Sstevel@tonic-gate 		if (clone_node == DI_NODE_NIL) {
18437c478bd9Sstevel@tonic-gate 			err_print(DI_INIT_FAILED, "clone", strerror(errno));
18447c478bd9Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
18457c478bd9Sstevel@tonic-gate 		}
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 		cdep = dep ? &clone_del : NULL;
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate 		minor_process(clone_node, minor, cdep);
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 		/*
18527c478bd9Sstevel@tonic-gate 		 * cache "alias" minor node and free "clone" minor
18537c478bd9Sstevel@tonic-gate 		 */
18547c478bd9Sstevel@tonic-gate 		if (cdep != NULL && cdep->head != NULL) {
18557c478bd9Sstevel@tonic-gate 			assert(cdep->tail != NULL);
18567c478bd9Sstevel@tonic-gate 			cache_deferred_minor(dep, node, minor);
18577c478bd9Sstevel@tonic-gate 			dcip->dci_arg = cdep;
18587c478bd9Sstevel@tonic-gate 			process_deferred_links(dcip, DCA_FREE_LIST);
18597c478bd9Sstevel@tonic-gate 			dcip->dci_arg = dep;
18607c478bd9Sstevel@tonic-gate 		}
18617c478bd9Sstevel@tonic-gate 	}
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
18647c478bd9Sstevel@tonic-gate }
18657c478bd9Sstevel@tonic-gate 
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate /*
18687c478bd9Sstevel@tonic-gate  *  This is the entry point for each minor node, whether walking
18697c478bd9Sstevel@tonic-gate  *  the entire tree via di_walk_minor() or processing a hotplug event
18707c478bd9Sstevel@tonic-gate  *  for a single devinfo node (via hotplug ndi_devi_online()).
18717c478bd9Sstevel@tonic-gate  */
18727c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18737c478bd9Sstevel@tonic-gate static void
18747c478bd9Sstevel@tonic-gate minor_process(di_node_t node, di_minor_t minor, struct mlist *dep)
18757c478bd9Sstevel@tonic-gate {
18767c478bd9Sstevel@tonic-gate 	create_list_t	*create;
18777c478bd9Sstevel@tonic-gate 	int		defer;
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "minor_process: node=%s, minor=%s\n",
18800a653502Swroche 	    di_node_name(node), di_minor_name(minor));
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	if (dep != NULL) {
18837c478bd9Sstevel@tonic-gate 
18847c478bd9Sstevel@tonic-gate 		/*
18857c478bd9Sstevel@tonic-gate 		 * Reset /devices node to minor_perm perm/ownership
18867c478bd9Sstevel@tonic-gate 		 * if we are here to deactivate device allocation
18877c478bd9Sstevel@tonic-gate 		 */
18887c478bd9Sstevel@tonic-gate 		if (build_devices == TRUE) {
18897c478bd9Sstevel@tonic-gate 			reset_node_permissions(node, minor);
18907c478bd9Sstevel@tonic-gate 		}
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate 		if (build_dev == FALSE) {
18937c478bd9Sstevel@tonic-gate 			return;
18947c478bd9Sstevel@tonic-gate 		}
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 		/*
18977c478bd9Sstevel@tonic-gate 		 * This function will create any nodes for /etc/devlink.tab.
18987c478bd9Sstevel@tonic-gate 		 * If devlink.tab handles link creation, we don't call any
18997c478bd9Sstevel@tonic-gate 		 * devfsadm modules since that could cause duplicate caching
19007c478bd9Sstevel@tonic-gate 		 * in the enumerate functions if different re strings are
19017c478bd9Sstevel@tonic-gate 		 * passed that are logically identical.  I'm still not
19027c478bd9Sstevel@tonic-gate 		 * convinced this would cause any harm, but better to be safe.
19037c478bd9Sstevel@tonic-gate 		 *
19047c478bd9Sstevel@tonic-gate 		 * Deferred processing is available only for devlinks
19057c478bd9Sstevel@tonic-gate 		 * created through devfsadm modules.
19067c478bd9Sstevel@tonic-gate 		 */
19077c478bd9Sstevel@tonic-gate 		if (process_devlink_compat(minor, node) == TRUE) {
19087c478bd9Sstevel@tonic-gate 			return;
19097c478bd9Sstevel@tonic-gate 		}
19107c478bd9Sstevel@tonic-gate 	} else {
19117c478bd9Sstevel@tonic-gate 		vprint(CHATTY_MID, "minor_process: deferred processing\n");
19127c478bd9Sstevel@tonic-gate 	}
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 	/*
19157c478bd9Sstevel@tonic-gate 	 * look for relevant link create rules in the modules, and
19167c478bd9Sstevel@tonic-gate 	 * invoke the link create callback function to build a link
19177c478bd9Sstevel@tonic-gate 	 * if there is a match.
19187c478bd9Sstevel@tonic-gate 	 */
19197c478bd9Sstevel@tonic-gate 	defer = 0;
19207c478bd9Sstevel@tonic-gate 	for (create = create_head; create != NULL; create = create->next) {
19217c478bd9Sstevel@tonic-gate 		if ((minor_matches_rule(node, minor, create) == TRUE) &&
19227c478bd9Sstevel@tonic-gate 		    class_ok(create->create->device_class) ==
19237c478bd9Sstevel@tonic-gate 		    DEVFSADM_SUCCESS) {
19247c478bd9Sstevel@tonic-gate 			if (call_minor_init(create->modptr) ==
19257c478bd9Sstevel@tonic-gate 			    DEVFSADM_FAILURE) {
19267c478bd9Sstevel@tonic-gate 				continue;
19277c478bd9Sstevel@tonic-gate 			}
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 			/*
19307c478bd9Sstevel@tonic-gate 			 * If NOT doing the deferred creates (i.e. 1st pass) and
19317c478bd9Sstevel@tonic-gate 			 * rule requests deferred processing cache the minor
19327c478bd9Sstevel@tonic-gate 			 * data.
19337c478bd9Sstevel@tonic-gate 			 *
19347c478bd9Sstevel@tonic-gate 			 * If deferred processing (2nd pass), create links
19357c478bd9Sstevel@tonic-gate 			 * ONLY if rule requests deferred processing.
19367c478bd9Sstevel@tonic-gate 			 */
19377c478bd9Sstevel@tonic-gate 			if (dep && ((create->create->flags & CREATE_MASK) ==
19387c478bd9Sstevel@tonic-gate 			    CREATE_DEFER)) {
19397c478bd9Sstevel@tonic-gate 				defer = 1;
19407c478bd9Sstevel@tonic-gate 				continue;
19417c478bd9Sstevel@tonic-gate 			} else if (dep == NULL &&
19427c478bd9Sstevel@tonic-gate 			    ((create->create->flags & CREATE_MASK) !=
19437c478bd9Sstevel@tonic-gate 			    CREATE_DEFER)) {
19447c478bd9Sstevel@tonic-gate 				continue;
19457c478bd9Sstevel@tonic-gate 			}
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 			if ((*(create->create->callback_fcn))
19487c478bd9Sstevel@tonic-gate 			    (minor, node) == DEVFSADM_TERMINATE) {
19497c478bd9Sstevel@tonic-gate 				break;
19507c478bd9Sstevel@tonic-gate 			}
19517c478bd9Sstevel@tonic-gate 		}
19527c478bd9Sstevel@tonic-gate 	}
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	if (defer)
19557c478bd9Sstevel@tonic-gate 		cache_deferred_minor(dep, node, minor);
19567c478bd9Sstevel@tonic-gate }
19577c478bd9Sstevel@tonic-gate 
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate /*
19607c478bd9Sstevel@tonic-gate  * Cache node and minor in defer list.
19617c478bd9Sstevel@tonic-gate  */
19627c478bd9Sstevel@tonic-gate static void
19637c478bd9Sstevel@tonic-gate cache_deferred_minor(
19647c478bd9Sstevel@tonic-gate 	struct mlist *dep,
19657c478bd9Sstevel@tonic-gate 	di_node_t node,
19667c478bd9Sstevel@tonic-gate 	di_minor_t minor)
19677c478bd9Sstevel@tonic-gate {
19687c478bd9Sstevel@tonic-gate 	struct minor	*mp;
19697c478bd9Sstevel@tonic-gate 	const char	*fcn = "cache_deferred_minor";
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "%s node=%s, minor=%s\n", fcn,
19727c478bd9Sstevel@tonic-gate 	    di_node_name(node), di_minor_name(minor));
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	if (dep == NULL) {
19757c478bd9Sstevel@tonic-gate 		vprint(CHATTY_MID, "%s: cannot cache during "
19767c478bd9Sstevel@tonic-gate 		    "deferred processing. Ignoring minor\n", fcn);
19777c478bd9Sstevel@tonic-gate 		return;
19787c478bd9Sstevel@tonic-gate 	}
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 	mp = (struct minor *)s_zalloc(sizeof (struct minor));
19817c478bd9Sstevel@tonic-gate 	mp->node = node;
19827c478bd9Sstevel@tonic-gate 	mp->minor = minor;
19837c478bd9Sstevel@tonic-gate 	mp->next = NULL;
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate 	assert(dep->head == NULL || dep->tail != NULL);
19867c478bd9Sstevel@tonic-gate 	if (dep->head == NULL) {
19877c478bd9Sstevel@tonic-gate 		dep->head = mp;
19887c478bd9Sstevel@tonic-gate 	} else {
19897c478bd9Sstevel@tonic-gate 		dep->tail->next = mp;
19907c478bd9Sstevel@tonic-gate 	}
19917c478bd9Sstevel@tonic-gate 	dep->tail = mp;
19927c478bd9Sstevel@tonic-gate }
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate /*
19957c478bd9Sstevel@tonic-gate  *  Check to see if "create" link creation rule matches this node/minor.
19967c478bd9Sstevel@tonic-gate  *  If it does, return TRUE.
19977c478bd9Sstevel@tonic-gate  */
19987c478bd9Sstevel@tonic-gate static int
19997c478bd9Sstevel@tonic-gate minor_matches_rule(di_node_t node, di_minor_t minor, create_list_t *create)
20007c478bd9Sstevel@tonic-gate {
20017c478bd9Sstevel@tonic-gate 	char *m_nodetype, *m_drvname;
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 	if (create->create->node_type != NULL) {
20047c478bd9Sstevel@tonic-gate 
20057c478bd9Sstevel@tonic-gate 		m_nodetype = di_minor_nodetype(minor);
20067c478bd9Sstevel@tonic-gate 		assert(m_nodetype != NULL);
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 		switch (create->create->flags & TYPE_MASK) {
20097c478bd9Sstevel@tonic-gate 		case TYPE_EXACT:
20107c478bd9Sstevel@tonic-gate 			if (strcmp(create->create->node_type, m_nodetype) !=
20117c478bd9Sstevel@tonic-gate 			    0) {
20127c478bd9Sstevel@tonic-gate 				return (FALSE);
20137c478bd9Sstevel@tonic-gate 			}
20147c478bd9Sstevel@tonic-gate 			break;
20157c478bd9Sstevel@tonic-gate 		case TYPE_PARTIAL:
20167c478bd9Sstevel@tonic-gate 			if (strncmp(create->create->node_type, m_nodetype,
20177c478bd9Sstevel@tonic-gate 			    strlen(create->create->node_type)) != 0) {
20187c478bd9Sstevel@tonic-gate 				return (FALSE);
20197c478bd9Sstevel@tonic-gate 			}
20207c478bd9Sstevel@tonic-gate 			break;
20217c478bd9Sstevel@tonic-gate 		case TYPE_RE:
20227c478bd9Sstevel@tonic-gate 			if (regexec(&(create->node_type_comp), m_nodetype,
20237c478bd9Sstevel@tonic-gate 			    0, NULL, 0) != 0) {
20247c478bd9Sstevel@tonic-gate 				return (FALSE);
20257c478bd9Sstevel@tonic-gate 			}
20267c478bd9Sstevel@tonic-gate 			break;
20277c478bd9Sstevel@tonic-gate 		}
20287c478bd9Sstevel@tonic-gate 	}
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 	if (create->create->drv_name != NULL) {
20317c478bd9Sstevel@tonic-gate 		m_drvname = di_driver_name(node);
20327c478bd9Sstevel@tonic-gate 		switch (create->create->flags & DRV_MASK) {
20337c478bd9Sstevel@tonic-gate 		case DRV_EXACT:
20347c478bd9Sstevel@tonic-gate 			if (strcmp(create->create->drv_name, m_drvname) != 0) {
20357c478bd9Sstevel@tonic-gate 				return (FALSE);
20367c478bd9Sstevel@tonic-gate 			}
20377c478bd9Sstevel@tonic-gate 			break;
20387c478bd9Sstevel@tonic-gate 		case DRV_RE:
20397c478bd9Sstevel@tonic-gate 			if (regexec(&(create->drv_name_comp), m_drvname,
20407c478bd9Sstevel@tonic-gate 			    0, NULL, 0) != 0) {
20417c478bd9Sstevel@tonic-gate 				return (FALSE);
20427c478bd9Sstevel@tonic-gate 			}
20437c478bd9Sstevel@tonic-gate 			break;
20447c478bd9Sstevel@tonic-gate 		}
20457c478bd9Sstevel@tonic-gate 	}
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	return (TRUE);
20487c478bd9Sstevel@tonic-gate }
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate /*
20517c478bd9Sstevel@tonic-gate  * If no classes were given on the command line, then return DEVFSADM_SUCCESS.
20527c478bd9Sstevel@tonic-gate  * Otherwise, return DEVFSADM_SUCCESS if the device "class" from the module
20537c478bd9Sstevel@tonic-gate  * matches one of the device classes given on the command line,
20547c478bd9Sstevel@tonic-gate  * otherwise, return DEVFSADM_FAILURE.
20557c478bd9Sstevel@tonic-gate  */
20567c478bd9Sstevel@tonic-gate static int
20577c478bd9Sstevel@tonic-gate class_ok(char *class)
20587c478bd9Sstevel@tonic-gate {
20597c478bd9Sstevel@tonic-gate 	int i;
20607c478bd9Sstevel@tonic-gate 
20617c478bd9Sstevel@tonic-gate 	if (num_classes == 0) {
20627c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
20637c478bd9Sstevel@tonic-gate 	}
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_classes; i++) {
20667c478bd9Sstevel@tonic-gate 		if (strcmp(class, classes[i]) == 0) {
20677c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
20687c478bd9Sstevel@tonic-gate 		}
20697c478bd9Sstevel@tonic-gate 	}
20707c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
20717c478bd9Sstevel@tonic-gate }
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate /*
20747c478bd9Sstevel@tonic-gate  * call minor_fini on active modules, then unload ALL modules
20757c478bd9Sstevel@tonic-gate  */
20767c478bd9Sstevel@tonic-gate static void
20777c478bd9Sstevel@tonic-gate unload_modules(void)
20787c478bd9Sstevel@tonic-gate {
20797c478bd9Sstevel@tonic-gate 	module_t *module_free;
20807c478bd9Sstevel@tonic-gate 	create_list_t *create_free;
20817c478bd9Sstevel@tonic-gate 	remove_list_t *remove_free;
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	while (create_head != NULL) {
20847c478bd9Sstevel@tonic-gate 		create_free = create_head;
20857c478bd9Sstevel@tonic-gate 		create_head = create_head->next;
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 		if ((create_free->create->flags & TYPE_RE) == TYPE_RE) {
20887c478bd9Sstevel@tonic-gate 			regfree(&(create_free->node_type_comp));
20897c478bd9Sstevel@tonic-gate 		}
20907c478bd9Sstevel@tonic-gate 		if ((create_free->create->flags & DRV_RE) == DRV_RE) {
20917c478bd9Sstevel@tonic-gate 			regfree(&(create_free->drv_name_comp));
20927c478bd9Sstevel@tonic-gate 		}
20937c478bd9Sstevel@tonic-gate 		free(create_free);
20947c478bd9Sstevel@tonic-gate 	}
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 	while (remove_head != NULL) {
20977c478bd9Sstevel@tonic-gate 		remove_free = remove_head;
20987c478bd9Sstevel@tonic-gate 		remove_head = remove_head->next;
20997c478bd9Sstevel@tonic-gate 		free(remove_free);
21007c478bd9Sstevel@tonic-gate 	}
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate 	while (module_head != NULL) {
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate 		if ((module_head->minor_fini != NULL) &&
21057c478bd9Sstevel@tonic-gate 		    ((module_head->flags & MODULE_ACTIVE) == MODULE_ACTIVE)) {
21067c478bd9Sstevel@tonic-gate 			(void) (*(module_head->minor_fini))();
21077c478bd9Sstevel@tonic-gate 		}
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 		vprint(MODLOAD_MID, "unloading module %s\n", module_head->name);
21107c478bd9Sstevel@tonic-gate 		free(module_head->name);
21117c478bd9Sstevel@tonic-gate 		(void) dlclose(module_head->dlhandle);
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 		module_free = module_head;
21147c478bd9Sstevel@tonic-gate 		module_head = module_head->next;
21157c478bd9Sstevel@tonic-gate 		free(module_free);
21167c478bd9Sstevel@tonic-gate 	}
21177c478bd9Sstevel@tonic-gate }
21187c478bd9Sstevel@tonic-gate 
21197c478bd9Sstevel@tonic-gate /*
21207c478bd9Sstevel@tonic-gate  * Load devfsadm logical link processing modules.
21217c478bd9Sstevel@tonic-gate  */
21227c478bd9Sstevel@tonic-gate static void
21237c478bd9Sstevel@tonic-gate load_modules(void)
21247c478bd9Sstevel@tonic-gate {
21257c478bd9Sstevel@tonic-gate 	DIR *mod_dir;
21264bc0a2efScasper 	struct dirent *entp;
21277c478bd9Sstevel@tonic-gate 	char cdir[PATH_MAX + 1];
21287c478bd9Sstevel@tonic-gate 	char *last;
21297c478bd9Sstevel@tonic-gate 	char *mdir = module_dirs;
21307c478bd9Sstevel@tonic-gate 	char *fcn = "load_modules: ";
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 	while (*mdir != '\0') {
21337c478bd9Sstevel@tonic-gate 
21347c478bd9Sstevel@tonic-gate 		while (*mdir == ':') {
21357c478bd9Sstevel@tonic-gate 			mdir++;
21367c478bd9Sstevel@tonic-gate 		}
21377c478bd9Sstevel@tonic-gate 
21387c478bd9Sstevel@tonic-gate 		if (*mdir == '\0') {
21397c478bd9Sstevel@tonic-gate 			continue;
21407c478bd9Sstevel@tonic-gate 		}
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 		last = strchr(mdir, ':');
21437c478bd9Sstevel@tonic-gate 
21447c478bd9Sstevel@tonic-gate 		if (last == NULL) {
21457c478bd9Sstevel@tonic-gate 			last = mdir + strlen(mdir);
21467c478bd9Sstevel@tonic-gate 		}
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate 		(void) strncpy(cdir, mdir, last - mdir);
21497c478bd9Sstevel@tonic-gate 		cdir[last - mdir] = '\0';
21507c478bd9Sstevel@tonic-gate 		mdir += strlen(cdir);
21517c478bd9Sstevel@tonic-gate 
21527c478bd9Sstevel@tonic-gate 		if ((mod_dir = opendir(cdir)) == NULL) {
21537c478bd9Sstevel@tonic-gate 			vprint(MODLOAD_MID, "%sopendir(%s): %s\n",
21540a653502Swroche 			    fcn, cdir, strerror(errno));
21557c478bd9Sstevel@tonic-gate 			continue;
21567c478bd9Sstevel@tonic-gate 		}
21577c478bd9Sstevel@tonic-gate 
21584bc0a2efScasper 		while ((entp = readdir(mod_dir)) != NULL) {
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 			if ((strcmp(entp->d_name, ".") == 0) ||
21617c478bd9Sstevel@tonic-gate 			    (strcmp(entp->d_name, "..") == 0)) {
21627c478bd9Sstevel@tonic-gate 				continue;
21637c478bd9Sstevel@tonic-gate 			}
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 			load_module(entp->d_name, cdir);
21667c478bd9Sstevel@tonic-gate 		}
21677c478bd9Sstevel@tonic-gate 		s_closedir(mod_dir);
21687c478bd9Sstevel@tonic-gate 	}
21697c478bd9Sstevel@tonic-gate }
21707c478bd9Sstevel@tonic-gate 
21717c478bd9Sstevel@tonic-gate static void
21727c478bd9Sstevel@tonic-gate load_module(char *mname, char *cdir)
21737c478bd9Sstevel@tonic-gate {
21747c478bd9Sstevel@tonic-gate 	_devfsadm_create_reg_t *create_reg;
2175aa646b9dSvikram 	_devfsadm_remove_reg_V1_t *remove_reg;
21767c478bd9Sstevel@tonic-gate 	create_list_t *create_list_element;
21777c478bd9Sstevel@tonic-gate 	create_list_t **create_list_next;
21787c478bd9Sstevel@tonic-gate 	remove_list_t *remove_list_element;
21797c478bd9Sstevel@tonic-gate 	remove_list_t **remove_list_next;
21807c478bd9Sstevel@tonic-gate 	char epath[PATH_MAX + 1], *end;
21817c478bd9Sstevel@tonic-gate 	char *fcn = "load_module: ";
21827c478bd9Sstevel@tonic-gate 	char *dlerrstr;
21837c478bd9Sstevel@tonic-gate 	void *dlhandle;
21847c478bd9Sstevel@tonic-gate 	module_t *module;
2185aa646b9dSvikram 	int flags;
21867c478bd9Sstevel@tonic-gate 	int n;
21877c478bd9Sstevel@tonic-gate 	int i;
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate 	/* ignore any file which does not end in '.so' */
21907c478bd9Sstevel@tonic-gate 	if ((end = strstr(mname, MODULE_SUFFIX)) != NULL) {
21917c478bd9Sstevel@tonic-gate 		if (end[strlen(MODULE_SUFFIX)] != '\0') {
21927c478bd9Sstevel@tonic-gate 			return;
21937c478bd9Sstevel@tonic-gate 		}
21947c478bd9Sstevel@tonic-gate 	} else {
21957c478bd9Sstevel@tonic-gate 		return;
21967c478bd9Sstevel@tonic-gate 	}
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 	(void) snprintf(epath, sizeof (epath), "%s/%s", cdir, mname);
21997c478bd9Sstevel@tonic-gate 
22007c478bd9Sstevel@tonic-gate 	if ((dlhandle = dlopen(epath, RTLD_LAZY)) == NULL) {
22017c478bd9Sstevel@tonic-gate 		dlerrstr = dlerror();
22027c478bd9Sstevel@tonic-gate 		err_print(DLOPEN_FAILED, epath,
22030a653502Swroche 		    dlerrstr ? dlerrstr : "unknown error");
22047c478bd9Sstevel@tonic-gate 		return;
22057c478bd9Sstevel@tonic-gate 	}
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate 	/* dlsym the _devfsadm_create_reg structure */
22087c478bd9Sstevel@tonic-gate 	if (NULL == (create_reg = (_devfsadm_create_reg_t *)
22090a653502Swroche 	    dlsym(dlhandle, _DEVFSADM_CREATE_REG))) {
22107c478bd9Sstevel@tonic-gate 		vprint(MODLOAD_MID, "dlsym(%s, %s): symbol not found\n", epath,
22110a653502Swroche 		    _DEVFSADM_CREATE_REG);
22127c478bd9Sstevel@tonic-gate 	} else {
22137c478bd9Sstevel@tonic-gate 		vprint(MODLOAD_MID, "%sdlsym(%s, %s) succeeded\n",
22140a653502Swroche 		    fcn, epath, _DEVFSADM_CREATE_REG);
22157c478bd9Sstevel@tonic-gate 	}
22167c478bd9Sstevel@tonic-gate 
22177c478bd9Sstevel@tonic-gate 	/* dlsym the _devfsadm_remove_reg structure */
2218aa646b9dSvikram 	if (NULL == (remove_reg = (_devfsadm_remove_reg_V1_t *)
22197c478bd9Sstevel@tonic-gate 	    dlsym(dlhandle, _DEVFSADM_REMOVE_REG))) {
22207c478bd9Sstevel@tonic-gate 		vprint(MODLOAD_MID, "dlsym(%s,\n\t%s): symbol not found\n",
22210a653502Swroche 		    epath, _DEVFSADM_REMOVE_REG);
22227c478bd9Sstevel@tonic-gate 	} else {
22237c478bd9Sstevel@tonic-gate 		vprint(MODLOAD_MID, "dlsym(%s, %s): succeeded\n",
22240a653502Swroche 		    epath, _DEVFSADM_REMOVE_REG);
22257c478bd9Sstevel@tonic-gate 	}
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 	vprint(MODLOAD_MID, "module %s loaded\n", epath);
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 	module = (module_t *)s_malloc(sizeof (module_t));
22307c478bd9Sstevel@tonic-gate 	module->name = s_strdup(epath);
22317c478bd9Sstevel@tonic-gate 	module->dlhandle = dlhandle;
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 	/* dlsym other module functions, to be called later */
22347c478bd9Sstevel@tonic-gate 	module->minor_fini = (int (*)())dlsym(dlhandle, MINOR_FINI);
22357c478bd9Sstevel@tonic-gate 	module->minor_init = (int (*)())dlsym(dlhandle, MINOR_INIT);
22367c478bd9Sstevel@tonic-gate 	module->flags = 0;
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate 	/*
22397c478bd9Sstevel@tonic-gate 	 *  put a ptr to each struct devfsadm_create on "create_head"
22407c478bd9Sstevel@tonic-gate 	 *  list sorted in interpose_lvl.
22417c478bd9Sstevel@tonic-gate 	 */
22427c478bd9Sstevel@tonic-gate 	if (create_reg != NULL) {
22437c478bd9Sstevel@tonic-gate 		for (i = 0; i < create_reg->count; i++) {
22447c478bd9Sstevel@tonic-gate 			int flags = create_reg->tblp[i].flags;
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate 			create_list_element = (create_list_t *)
22470a653502Swroche 			    s_malloc(sizeof (create_list_t));
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 			create_list_element->create = &(create_reg->tblp[i]);
22507c478bd9Sstevel@tonic-gate 			create_list_element->modptr = module;
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 			if (((flags & CREATE_MASK) != 0) &&
22537c478bd9Sstevel@tonic-gate 			    ((flags & CREATE_MASK) != CREATE_DEFER)) {
22547c478bd9Sstevel@tonic-gate 				free(create_list_element);
22557c478bd9Sstevel@tonic-gate 				err_print("illegal flag combination in "
22560a653502Swroche 				    "module create\n");
22577c478bd9Sstevel@tonic-gate 				err_print(IGNORING_ENTRY, i, epath);
22587c478bd9Sstevel@tonic-gate 				continue;
22597c478bd9Sstevel@tonic-gate 			}
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate 			if (((flags & TYPE_MASK) == 0) ^
22627c478bd9Sstevel@tonic-gate 			    (create_reg->tblp[i].node_type == NULL)) {
22637c478bd9Sstevel@tonic-gate 				free(create_list_element);
22647c478bd9Sstevel@tonic-gate 				err_print("flags value incompatible with "
22650a653502Swroche 				    "node_type value in module create\n");
22667c478bd9Sstevel@tonic-gate 				err_print(IGNORING_ENTRY, i, epath);
22677c478bd9Sstevel@tonic-gate 				continue;
22687c478bd9Sstevel@tonic-gate 			}
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 			if (((flags & TYPE_MASK) != 0) &&
22717c478bd9Sstevel@tonic-gate 			    ((flags & TYPE_MASK) != TYPE_EXACT) &&
22727c478bd9Sstevel@tonic-gate 			    ((flags & TYPE_MASK) != TYPE_RE) &&
22737c478bd9Sstevel@tonic-gate 			    ((flags & TYPE_MASK) != TYPE_PARTIAL)) {
22747c478bd9Sstevel@tonic-gate 				free(create_list_element);
22757c478bd9Sstevel@tonic-gate 				err_print("illegal TYPE_* flag combination in "
22760a653502Swroche 				    "module create\n");
22777c478bd9Sstevel@tonic-gate 				err_print(IGNORING_ENTRY, i, epath);
22787c478bd9Sstevel@tonic-gate 				continue;
22797c478bd9Sstevel@tonic-gate 			}
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate 			/* precompile regular expression for efficiency */
22827c478bd9Sstevel@tonic-gate 			if ((flags & TYPE_RE) == TYPE_RE) {
22837c478bd9Sstevel@tonic-gate 				if ((n = regcomp(&(create_list_element->
22847c478bd9Sstevel@tonic-gate 				    node_type_comp),
22857c478bd9Sstevel@tonic-gate 				    create_reg->tblp[i].node_type,
22867c478bd9Sstevel@tonic-gate 				    REG_EXTENDED)) != 0) {
22877c478bd9Sstevel@tonic-gate 					free(create_list_element);
22887c478bd9Sstevel@tonic-gate 					err_print(REGCOMP_FAILED,
22890a653502Swroche 					    create_reg->tblp[i].node_type, n);
22907c478bd9Sstevel@tonic-gate 					err_print(IGNORING_ENTRY, i, epath);
22917c478bd9Sstevel@tonic-gate 					continue;
22927c478bd9Sstevel@tonic-gate 				}
22937c478bd9Sstevel@tonic-gate 			}
22947c478bd9Sstevel@tonic-gate 
22957c478bd9Sstevel@tonic-gate 			if (((flags & DRV_MASK) == 0) ^
22967c478bd9Sstevel@tonic-gate 			    (create_reg->tblp[i].drv_name == NULL)) {
22977c478bd9Sstevel@tonic-gate 				if ((flags & TYPE_RE) == TYPE_RE) {
22987c478bd9Sstevel@tonic-gate 					regfree(&(create_list_element->
22997c478bd9Sstevel@tonic-gate 					    node_type_comp));
23007c478bd9Sstevel@tonic-gate 				}
23017c478bd9Sstevel@tonic-gate 				free(create_list_element);
23027c478bd9Sstevel@tonic-gate 				err_print("flags value incompatible with "
23030a653502Swroche 				    "drv_name value in module create\n");
23047c478bd9Sstevel@tonic-gate 				err_print(IGNORING_ENTRY, i, epath);
23057c478bd9Sstevel@tonic-gate 				continue;
23067c478bd9Sstevel@tonic-gate 			}
23077c478bd9Sstevel@tonic-gate 
23087c478bd9Sstevel@tonic-gate 			if (((flags & DRV_MASK) != 0) &&
23097c478bd9Sstevel@tonic-gate 			    ((flags & DRV_MASK) != DRV_EXACT) &&
23107c478bd9Sstevel@tonic-gate 			    ((flags & DRV_MASK) !=  DRV_RE)) {
23117c478bd9Sstevel@tonic-gate 				if ((flags & TYPE_RE) == TYPE_RE) {
23127c478bd9Sstevel@tonic-gate 					regfree(&(create_list_element->
23137c478bd9Sstevel@tonic-gate 					    node_type_comp));
23147c478bd9Sstevel@tonic-gate 				}
23157c478bd9Sstevel@tonic-gate 				free(create_list_element);
23167c478bd9Sstevel@tonic-gate 				err_print("illegal DRV_* flag combination in "
23170a653502Swroche 				    "module create\n");
23187c478bd9Sstevel@tonic-gate 				err_print(IGNORING_ENTRY, i, epath);
23197c478bd9Sstevel@tonic-gate 				continue;
23207c478bd9Sstevel@tonic-gate 			}
23217c478bd9Sstevel@tonic-gate 
23227c478bd9Sstevel@tonic-gate 			/* precompile regular expression for efficiency */
23237c478bd9Sstevel@tonic-gate 			if ((create_reg->tblp[i].flags & DRV_RE) == DRV_RE) {
23247c478bd9Sstevel@tonic-gate 				if ((n = regcomp(&(create_list_element->
23257c478bd9Sstevel@tonic-gate 				    drv_name_comp),
23267c478bd9Sstevel@tonic-gate 				    create_reg->tblp[i].drv_name,
23277c478bd9Sstevel@tonic-gate 				    REG_EXTENDED)) != 0) {
23287c478bd9Sstevel@tonic-gate 					if ((flags & TYPE_RE) == TYPE_RE) {
23297c478bd9Sstevel@tonic-gate 						regfree(&(create_list_element->
23307c478bd9Sstevel@tonic-gate 						    node_type_comp));
23317c478bd9Sstevel@tonic-gate 					}
23327c478bd9Sstevel@tonic-gate 					free(create_list_element);
23337c478bd9Sstevel@tonic-gate 					err_print(REGCOMP_FAILED,
23340a653502Swroche 					    create_reg->tblp[i].drv_name, n);
23357c478bd9Sstevel@tonic-gate 					err_print(IGNORING_ENTRY, i, epath);
23367c478bd9Sstevel@tonic-gate 					continue;
23377c478bd9Sstevel@tonic-gate 				}
23387c478bd9Sstevel@tonic-gate 			}
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate 			/* add to list sorted by interpose level */
23427c478bd9Sstevel@tonic-gate 			for (create_list_next = &(create_head);
23430a653502Swroche 			    (*create_list_next != NULL) &&
23440a653502Swroche 			    (*create_list_next)->create->interpose_lvl >=
23450a653502Swroche 			    create_list_element->create->interpose_lvl;
23460a653502Swroche 			    create_list_next = &((*create_list_next)->next))
23470a653502Swroche 				;
23487c478bd9Sstevel@tonic-gate 			create_list_element->next = *create_list_next;
23497c478bd9Sstevel@tonic-gate 			*create_list_next = create_list_element;
23507c478bd9Sstevel@tonic-gate 		}
23517c478bd9Sstevel@tonic-gate 	}
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate 	/*
23547c478bd9Sstevel@tonic-gate 	 *  put a ptr to each struct devfsadm_remove on "remove_head"
23557c478bd9Sstevel@tonic-gate 	 *  list sorted by interpose_lvl.
23567c478bd9Sstevel@tonic-gate 	 */
2357aa646b9dSvikram 	flags = 0;
23587c478bd9Sstevel@tonic-gate 	if (remove_reg != NULL) {
2359aa646b9dSvikram 		if (remove_reg->version < DEVFSADM_V1)
2360aa646b9dSvikram 			flags |= RM_NOINTERPOSE;
23617c478bd9Sstevel@tonic-gate 		for (i = 0; i < remove_reg->count; i++) {
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 			remove_list_element = (remove_list_t *)
23640a653502Swroche 			    s_malloc(sizeof (remove_list_t));
23657c478bd9Sstevel@tonic-gate 
23667c478bd9Sstevel@tonic-gate 			remove_list_element->remove = &(remove_reg->tblp[i]);
2367aa646b9dSvikram 			remove_list_element->remove->flags |= flags;
23687c478bd9Sstevel@tonic-gate 			remove_list_element->modptr = module;
23697c478bd9Sstevel@tonic-gate 
23707c478bd9Sstevel@tonic-gate 			for (remove_list_next = &(remove_head);
23710a653502Swroche 			    (*remove_list_next != NULL) &&
23720a653502Swroche 			    (*remove_list_next)->remove->interpose_lvl >=
23730a653502Swroche 			    remove_list_element->remove->interpose_lvl;
23740a653502Swroche 			    remove_list_next = &((*remove_list_next)->next))
23750a653502Swroche 				;
23767c478bd9Sstevel@tonic-gate 			remove_list_element->next = *remove_list_next;
23777c478bd9Sstevel@tonic-gate 			*remove_list_next = remove_list_element;
23787c478bd9Sstevel@tonic-gate 		}
23797c478bd9Sstevel@tonic-gate 	}
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate 	module->next = module_head;
23827c478bd9Sstevel@tonic-gate 	module_head = module;
23837c478bd9Sstevel@tonic-gate }
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate /*
2386ff2aee48Scth  * After we have completed a CACHE_STATE, if a SYNC_STATE does not occur
2387ff2aee48Scth  * within 'timeout' secs the minor_fini_thread needs to do a SYNC_STATE
2388ff2aee48Scth  * so that we still call the minor_fini routines.
23897c478bd9Sstevel@tonic-gate  */
23907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
23917c478bd9Sstevel@tonic-gate static void
2392ff2aee48Scth minor_fini_thread(void *arg)
23937c478bd9Sstevel@tonic-gate {
2394ff2aee48Scth 	timestruc_t	abstime;
23957c478bd9Sstevel@tonic-gate 
2396ff2aee48Scth 	vprint(INITFINI_MID, "minor_fini_thread starting\n");
23977c478bd9Sstevel@tonic-gate 
2398ff2aee48Scth 	(void) mutex_lock(&minor_fini_mutex);
2399ff2aee48Scth 	for (;;) {
2400ff2aee48Scth 		/* wait the gather period, or until signaled */
2401ff2aee48Scth 		abstime.tv_sec = time(NULL) + minor_fini_timeout;
2402ff2aee48Scth 		abstime.tv_nsec = 0;
2403ff2aee48Scth 		(void) cond_timedwait(&minor_fini_cv,
2404ff2aee48Scth 		    &minor_fini_mutex, &abstime);
2405ff2aee48Scth 
2406ff2aee48Scth 		/* if minor_fini was canceled, go wait again */
2407ff2aee48Scth 		if (minor_fini_canceled == TRUE)
2408ff2aee48Scth 			continue;
24097c478bd9Sstevel@tonic-gate 
2410ff2aee48Scth 		/* if minor_fini was delayed, go wait again */
2411ff2aee48Scth 		if (minor_fini_delayed == TRUE) {
2412ff2aee48Scth 			minor_fini_delayed = FALSE;
2413ff2aee48Scth 			continue;
2414ff2aee48Scth 		}
24157c478bd9Sstevel@tonic-gate 
2416ff2aee48Scth 		/* done with cancellations and delays, do the SYNC_STATE */
24177c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&minor_fini_mutex);
24187c478bd9Sstevel@tonic-gate 
2419ff2aee48Scth 		lock_dev();
2420ff2aee48Scth 		unlock_dev(SYNC_STATE);
2421ff2aee48Scth 		vprint(INITFINI_MID, "minor_fini sync done\n");
24227c478bd9Sstevel@tonic-gate 
2423ff2aee48Scth 		(void) mutex_lock(&minor_fini_mutex);
2424ff2aee48Scth 	}
24257c478bd9Sstevel@tonic-gate }
24267c478bd9Sstevel@tonic-gate 
2427ff2aee48Scth 
24287c478bd9Sstevel@tonic-gate /*
24297c478bd9Sstevel@tonic-gate  * Attempt to initialize module, if a minor_init routine exists.  Set
24307c478bd9Sstevel@tonic-gate  * the active flag if the routine exists and succeeds.	If it doesn't
24317c478bd9Sstevel@tonic-gate  * exist, just set the active flag.
24327c478bd9Sstevel@tonic-gate  */
24337c478bd9Sstevel@tonic-gate static int
24347c478bd9Sstevel@tonic-gate call_minor_init(module_t *module)
24357c478bd9Sstevel@tonic-gate {
24367c478bd9Sstevel@tonic-gate 	char *fcn = "call_minor_init: ";
24377c478bd9Sstevel@tonic-gate 
24387c478bd9Sstevel@tonic-gate 	if ((module->flags & MODULE_ACTIVE) == MODULE_ACTIVE) {
24397c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
24407c478bd9Sstevel@tonic-gate 	}
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate 	vprint(INITFINI_MID, "%smodule %s.  current state: inactive\n",
24430a653502Swroche 	    fcn, module->name);
24447c478bd9Sstevel@tonic-gate 
24457c478bd9Sstevel@tonic-gate 	if (module->minor_init == NULL) {
24467c478bd9Sstevel@tonic-gate 		module->flags |= MODULE_ACTIVE;
24477c478bd9Sstevel@tonic-gate 		vprint(INITFINI_MID, "minor_init not defined\n");
24487c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
24497c478bd9Sstevel@tonic-gate 	}
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate 	if ((*(module->minor_init))() == DEVFSADM_FAILURE) {
24527c478bd9Sstevel@tonic-gate 		err_print(FAILED_FOR_MODULE, MINOR_INIT, module->name);
24537c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
24547c478bd9Sstevel@tonic-gate 	}
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate 	vprint(INITFINI_MID, "minor_init() returns DEVFSADM_SUCCESS. "
24570a653502Swroche 	    "new state: active\n");
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 	module->flags |= MODULE_ACTIVE;
24607c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
24617c478bd9Sstevel@tonic-gate }
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate /*
24647c478bd9Sstevel@tonic-gate  * Creates a symlink 'link' to the physical path of node:minor.
24657c478bd9Sstevel@tonic-gate  * Construct link contents, then call create_link_common().
24667c478bd9Sstevel@tonic-gate  */
24677c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24687c478bd9Sstevel@tonic-gate int
2469facf4a8dSllai devfsadm_mklink(char *link, di_node_t node, di_minor_t minor, int flags)
24707c478bd9Sstevel@tonic-gate {
24717c478bd9Sstevel@tonic-gate 	char rcontents[PATH_MAX];
24727c478bd9Sstevel@tonic-gate 	char devlink[PATH_MAX];
24737c478bd9Sstevel@tonic-gate 	char phy_path[PATH_MAX];
24747c478bd9Sstevel@tonic-gate 	char *acontents;
24757c478bd9Sstevel@tonic-gate 	char *dev_path;
24767c478bd9Sstevel@tonic-gate 	int numslashes;
24777c478bd9Sstevel@tonic-gate 	int rv;
24787c478bd9Sstevel@tonic-gate 	int i, link_exists;
24797c478bd9Sstevel@tonic-gate 	int last_was_slash = FALSE;
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 	/*
24827c478bd9Sstevel@tonic-gate 	 * try to use devices path
24837c478bd9Sstevel@tonic-gate 	 */
24847c478bd9Sstevel@tonic-gate 	if ((node == lnode) && (minor == lminor)) {
24857c478bd9Sstevel@tonic-gate 		acontents = lphy_path;
24867c478bd9Sstevel@tonic-gate 	} else if (di_minor_type(minor) == DDM_ALIAS) {
24877c478bd9Sstevel@tonic-gate 		/* use /pseudo/clone@0:<driver> as the phys path */
24887c478bd9Sstevel@tonic-gate 		(void) snprintf(phy_path, sizeof (phy_path),
24897c478bd9Sstevel@tonic-gate 		    "/pseudo/clone@0:%s",
24907c478bd9Sstevel@tonic-gate 		    di_driver_name(di_minor_devinfo(minor)));
24917c478bd9Sstevel@tonic-gate 		acontents = phy_path;
24927c478bd9Sstevel@tonic-gate 	} else {
24937c478bd9Sstevel@tonic-gate 		if ((dev_path = di_devfs_path(node)) == NULL) {
24947c478bd9Sstevel@tonic-gate 			err_print(DI_DEVFS_PATH_FAILED, strerror(errno));
24957c478bd9Sstevel@tonic-gate 			devfsadm_exit(1);
2496537714daSvikram 			/*NOTREACHED*/
24977c478bd9Sstevel@tonic-gate 		}
24987c478bd9Sstevel@tonic-gate 		(void) snprintf(phy_path, sizeof (phy_path), "%s:%s",
24997c478bd9Sstevel@tonic-gate 		    dev_path, di_minor_name(minor));
25007c478bd9Sstevel@tonic-gate 		di_devfs_path_free(dev_path);
25017c478bd9Sstevel@tonic-gate 		acontents = phy_path;
25027c478bd9Sstevel@tonic-gate 	}
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate 	/* prepend link with dev_dir contents */
25057c478bd9Sstevel@tonic-gate 	(void) strlcpy(devlink, dev_dir, sizeof (devlink));
25067c478bd9Sstevel@tonic-gate 	(void) strlcat(devlink, "/", sizeof (devlink));
25077c478bd9Sstevel@tonic-gate 	(void) strlcat(devlink, link, sizeof (devlink));
25087c478bd9Sstevel@tonic-gate 
25097c478bd9Sstevel@tonic-gate 	/*
25107c478bd9Sstevel@tonic-gate 	 * Calculate # of ../ to add.  Account for double '//' in path.
25117c478bd9Sstevel@tonic-gate 	 * Ignore all leading slashes.
25127c478bd9Sstevel@tonic-gate 	 */
25137c478bd9Sstevel@tonic-gate 	for (i = 0; link[i] == '/'; i++)
25147c478bd9Sstevel@tonic-gate 		;
25157c478bd9Sstevel@tonic-gate 	for (numslashes = 0; link[i] != '\0'; i++) {
25167c478bd9Sstevel@tonic-gate 		if (link[i] == '/') {
25177c478bd9Sstevel@tonic-gate 			if (last_was_slash == FALSE) {
25187c478bd9Sstevel@tonic-gate 				numslashes++;
25197c478bd9Sstevel@tonic-gate 				last_was_slash = TRUE;
25207c478bd9Sstevel@tonic-gate 			}
25217c478bd9Sstevel@tonic-gate 		} else {
25227c478bd9Sstevel@tonic-gate 			last_was_slash = FALSE;
25237c478bd9Sstevel@tonic-gate 		}
25247c478bd9Sstevel@tonic-gate 	}
25257c478bd9Sstevel@tonic-gate 	/* Don't count any trailing '/' */
25267c478bd9Sstevel@tonic-gate 	if (link[i-1] == '/') {
25277c478bd9Sstevel@tonic-gate 		numslashes--;
25287c478bd9Sstevel@tonic-gate 	}
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate 	rcontents[0] = '\0';
25317c478bd9Sstevel@tonic-gate 	do {
25327c478bd9Sstevel@tonic-gate 		(void) strlcat(rcontents, "../", sizeof (rcontents));
25337c478bd9Sstevel@tonic-gate 	} while (numslashes-- != 0);
25347c478bd9Sstevel@tonic-gate 
25357c478bd9Sstevel@tonic-gate 	(void) strlcat(rcontents, "devices", sizeof (rcontents));
25367c478bd9Sstevel@tonic-gate 	(void) strlcat(rcontents, acontents, sizeof (rcontents));
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate 	if (devlinks_debug == TRUE) {
25397c478bd9Sstevel@tonic-gate 		vprint(INFO_MID, "adding link %s ==> %s\n", devlink, rcontents);
25407c478bd9Sstevel@tonic-gate 	}
25417c478bd9Sstevel@tonic-gate 
25427c478bd9Sstevel@tonic-gate 	if ((rv = create_link_common(devlink, rcontents, &link_exists))
25437c478bd9Sstevel@tonic-gate 	    == DEVFSADM_SUCCESS) {
25447c478bd9Sstevel@tonic-gate 		linknew = TRUE;
25457c478bd9Sstevel@tonic-gate 		add_link_to_cache(link, acontents);
25467c478bd9Sstevel@tonic-gate 	} else {
25477c478bd9Sstevel@tonic-gate 		linknew = FALSE;
25487c478bd9Sstevel@tonic-gate 	}
25497c478bd9Sstevel@tonic-gate 
25507c478bd9Sstevel@tonic-gate 	if (link_exists == TRUE) {
25516e670f77Saj 		/* Link exists or was just created */
25526e670f77Saj 		(void) di_devlink_add_link(devlink_cache, link, rcontents,
25536e670f77Saj 		    DI_PRIMARY_LINK);
25546e670f77Saj 
25556e670f77Saj 		if (system_labeled && (flags & DA_ADD)) {
255645916cd2Sjpk 			/*
25576e670f77Saj 			 * Add this to the list of allocatable devices. If this
25586e670f77Saj 			 * is a hotplugged, removable disk, add it as rmdisk.
255945916cd2Sjpk 			 */
25606e670f77Saj 			int instance = di_instance(node);
256145916cd2Sjpk 
25626e670f77Saj 			if ((flags & DA_CD) &&
25636e670f77Saj 			    (_da_check_for_usb(devlink, root_dir) == 1)) {
256445916cd2Sjpk 				(void) da_add_list(&devlist, devlink, instance,
256545916cd2Sjpk 				    DA_ADD|DA_RMDISK);
256645916cd2Sjpk 				update_devdb = DA_RMDISK;
25676e670f77Saj 			} else if (linknew == TRUE) {
25686e670f77Saj 				(void) da_add_list(&devlist, devlink, instance,
25696e670f77Saj 				    flags);
25706e670f77Saj 				update_devdb = flags;
257145916cd2Sjpk 			}
257245916cd2Sjpk 		}
25737c478bd9Sstevel@tonic-gate 	}
25747c478bd9Sstevel@tonic-gate 
25757c478bd9Sstevel@tonic-gate 	return (rv);
25767c478bd9Sstevel@tonic-gate }
25777c478bd9Sstevel@tonic-gate 
25787c478bd9Sstevel@tonic-gate /*
25797c478bd9Sstevel@tonic-gate  * Creates a symlink link to primary_link.  Calculates relative
25807c478bd9Sstevel@tonic-gate  * directory offsets, then calls link_common().
25817c478bd9Sstevel@tonic-gate  */
25827c478bd9Sstevel@tonic-gate /*ARGSUSED*/
25837c478bd9Sstevel@tonic-gate int
25847c478bd9Sstevel@tonic-gate devfsadm_secondary_link(char *link, char *primary_link, int flags)
25857c478bd9Sstevel@tonic-gate {
25867c478bd9Sstevel@tonic-gate 	char contents[PATH_MAX + 1];
25877c478bd9Sstevel@tonic-gate 	char devlink[PATH_MAX + 1];
25887c478bd9Sstevel@tonic-gate 	int rv, link_exists;
25897c478bd9Sstevel@tonic-gate 	char *fpath;
25907c478bd9Sstevel@tonic-gate 	char *tpath;
25917c478bd9Sstevel@tonic-gate 	char *op;
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate 	/* prepend link with dev_dir contents */
25947c478bd9Sstevel@tonic-gate 	(void) strcpy(devlink, dev_dir);
25957c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, "/");
25967c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, link);
25977c478bd9Sstevel@tonic-gate 	/*
25987c478bd9Sstevel@tonic-gate 	 * building extra link, so use first link as link contents, but first
25997c478bd9Sstevel@tonic-gate 	 * make it relative.
26007c478bd9Sstevel@tonic-gate 	 */
26017c478bd9Sstevel@tonic-gate 	fpath = link;
26027c478bd9Sstevel@tonic-gate 	tpath = primary_link;
26037c478bd9Sstevel@tonic-gate 	op = contents;
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 	while (*fpath == *tpath && *fpath != '\0') {
26067c478bd9Sstevel@tonic-gate 		fpath++, tpath++;
26077c478bd9Sstevel@tonic-gate 	}
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate 	/* Count directories to go up, if any, and add "../" */
26107c478bd9Sstevel@tonic-gate 	while (*fpath != '\0') {
26117c478bd9Sstevel@tonic-gate 		if (*fpath == '/') {
26127c478bd9Sstevel@tonic-gate 			(void) strcpy(op, "../");
26137c478bd9Sstevel@tonic-gate 			op += 3;
26147c478bd9Sstevel@tonic-gate 		}
26157c478bd9Sstevel@tonic-gate 		fpath++;
26167c478bd9Sstevel@tonic-gate 	}
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 	/*
26197c478bd9Sstevel@tonic-gate 	 * Back up to the start of the current path component, in
26207c478bd9Sstevel@tonic-gate 	 * case in the middle
26217c478bd9Sstevel@tonic-gate 	 */
26227c478bd9Sstevel@tonic-gate 	while (tpath != primary_link && *(tpath-1) != '/') {
26237c478bd9Sstevel@tonic-gate 		tpath--;
26247c478bd9Sstevel@tonic-gate 	}
26257c478bd9Sstevel@tonic-gate 	(void) strcpy(op, tpath);
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 	if (devlinks_debug == TRUE) {
26287c478bd9Sstevel@tonic-gate 		vprint(INFO_MID, "adding extra link %s ==> %s\n",
26290a653502Swroche 		    devlink, contents);
26307c478bd9Sstevel@tonic-gate 	}
26317c478bd9Sstevel@tonic-gate 
26327c478bd9Sstevel@tonic-gate 	if ((rv = create_link_common(devlink, contents, &link_exists))
26337c478bd9Sstevel@tonic-gate 	    == DEVFSADM_SUCCESS) {
26347c478bd9Sstevel@tonic-gate 		/*
26357c478bd9Sstevel@tonic-gate 		 * we need to save the ultimate /devices contents, and not the
26367c478bd9Sstevel@tonic-gate 		 * secondary link, since hotcleanup only looks at /devices path.
26377c478bd9Sstevel@tonic-gate 		 * Since we don't have devices path here, we can try to get it
26387c478bd9Sstevel@tonic-gate 		 * by readlink'ing the secondary link.  This assumes the primary
26397c478bd9Sstevel@tonic-gate 		 * link was created first.
26407c478bd9Sstevel@tonic-gate 		 */
26417c478bd9Sstevel@tonic-gate 		add_link_to_cache(link, lphy_path);
26427c478bd9Sstevel@tonic-gate 		linknew = TRUE;
264345916cd2Sjpk 		if (system_labeled &&
264445916cd2Sjpk 		    ((flags & DA_AUDIO) && (flags & DA_ADD))) {
264545916cd2Sjpk 			/*
264645916cd2Sjpk 			 * Add this device to the list of allocatable devices.
264745916cd2Sjpk 			 */
264845916cd2Sjpk 			int	instance = 0;
264945916cd2Sjpk 
265045916cd2Sjpk 			op = strrchr(contents, '/');
265145916cd2Sjpk 			op++;
265245916cd2Sjpk 			(void) sscanf(op, "%d", &instance);
265345916cd2Sjpk 			(void) da_add_list(&devlist, devlink, instance, flags);
265445916cd2Sjpk 			update_devdb = flags;
265545916cd2Sjpk 		}
26567c478bd9Sstevel@tonic-gate 	} else {
26577c478bd9Sstevel@tonic-gate 		linknew = FALSE;
26587c478bd9Sstevel@tonic-gate 	}
26597c478bd9Sstevel@tonic-gate 
26607c478bd9Sstevel@tonic-gate 	/*
26617c478bd9Sstevel@tonic-gate 	 * If link exists or was just created, add it to the database
26627c478bd9Sstevel@tonic-gate 	 */
26637c478bd9Sstevel@tonic-gate 	if (link_exists == TRUE) {
26647c478bd9Sstevel@tonic-gate 		(void) di_devlink_add_link(devlink_cache, link, contents,
26657c478bd9Sstevel@tonic-gate 		    DI_SECONDARY_LINK);
26667c478bd9Sstevel@tonic-gate 	}
26677c478bd9Sstevel@tonic-gate 
26687c478bd9Sstevel@tonic-gate 	return (rv);
26697c478bd9Sstevel@tonic-gate }
26707c478bd9Sstevel@tonic-gate 
26717c478bd9Sstevel@tonic-gate /* returns pointer to the devices directory */
26727c478bd9Sstevel@tonic-gate char *
26737c478bd9Sstevel@tonic-gate devfsadm_get_devices_dir()
26747c478bd9Sstevel@tonic-gate {
26757c478bd9Sstevel@tonic-gate 	return (devices_dir);
26767c478bd9Sstevel@tonic-gate }
26777c478bd9Sstevel@tonic-gate 
26787c478bd9Sstevel@tonic-gate /*
26797c478bd9Sstevel@tonic-gate  * Does the actual link creation.  VERBOSE_MID only used if there is
26807c478bd9Sstevel@tonic-gate  * a change.  CHATTY_MID used otherwise.
26817c478bd9Sstevel@tonic-gate  */
26827c478bd9Sstevel@tonic-gate static int
26837c478bd9Sstevel@tonic-gate create_link_common(char *devlink, char *contents, int *exists)
26847c478bd9Sstevel@tonic-gate {
26857c478bd9Sstevel@tonic-gate 	int try;
26867c478bd9Sstevel@tonic-gate 	int linksize;
26877c478bd9Sstevel@tonic-gate 	int max_tries = 0;
26887c478bd9Sstevel@tonic-gate 	static int prev_link_existed = TRUE;
26897c478bd9Sstevel@tonic-gate 	char checkcontents[PATH_MAX + 1];
26907c478bd9Sstevel@tonic-gate 	char *hide;
26917c478bd9Sstevel@tonic-gate 
26927c478bd9Sstevel@tonic-gate 	*exists = FALSE;
26937c478bd9Sstevel@tonic-gate 
26947c478bd9Sstevel@tonic-gate 	/* Database is not updated when file_mods == FALSE */
26957c478bd9Sstevel@tonic-gate 	if (file_mods == FALSE) {
26967c478bd9Sstevel@tonic-gate 		linksize = readlink(devlink, checkcontents, PATH_MAX);
26977c478bd9Sstevel@tonic-gate 		if (linksize > 0) {
26987c478bd9Sstevel@tonic-gate 			checkcontents[linksize] = '\0';
26997c478bd9Sstevel@tonic-gate 			if (strcmp(checkcontents, contents) != 0) {
27007c478bd9Sstevel@tonic-gate 				vprint(CHATTY_MID, REMOVING_LINK,
27010a653502Swroche 				    devlink, checkcontents);
27027c478bd9Sstevel@tonic-gate 				return (DEVFSADM_SUCCESS);
27037c478bd9Sstevel@tonic-gate 			} else {
27047c478bd9Sstevel@tonic-gate 				vprint(CHATTY_MID, "link exists and is correct:"
27050a653502Swroche 				    " %s -> %s\n", devlink, contents);
27067c478bd9Sstevel@tonic-gate 				/* failure only in that the link existed */
27077c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
27087c478bd9Sstevel@tonic-gate 			}
27097c478bd9Sstevel@tonic-gate 		} else {
27107c478bd9Sstevel@tonic-gate 			vprint(VERBOSE_MID, CREATING_LINK, devlink, contents);
27117c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
27127c478bd9Sstevel@tonic-gate 		}
27137c478bd9Sstevel@tonic-gate 	}
27147c478bd9Sstevel@tonic-gate 
27157c478bd9Sstevel@tonic-gate 	/*
27167c478bd9Sstevel@tonic-gate 	 * systems calls are expensive, so predict whether to readlink
27177c478bd9Sstevel@tonic-gate 	 * or symlink first, based on previous attempt
27187c478bd9Sstevel@tonic-gate 	 */
27197c478bd9Sstevel@tonic-gate 	if (prev_link_existed == FALSE) {
27207c478bd9Sstevel@tonic-gate 		try = CREATE_LINK;
27217c478bd9Sstevel@tonic-gate 	} else {
27227c478bd9Sstevel@tonic-gate 		try = READ_LINK;
27237c478bd9Sstevel@tonic-gate 	}
27247c478bd9Sstevel@tonic-gate 
27257c478bd9Sstevel@tonic-gate 	while (++max_tries <= 3) {
27267c478bd9Sstevel@tonic-gate 
27277c478bd9Sstevel@tonic-gate 		switch (try) {
27287c478bd9Sstevel@tonic-gate 		case  CREATE_LINK:
27297c478bd9Sstevel@tonic-gate 
27307c478bd9Sstevel@tonic-gate 			if (symlink(contents, devlink) == 0) {
27317c478bd9Sstevel@tonic-gate 				vprint(VERBOSE_MID, CREATING_LINK, devlink,
27320a653502Swroche 				    contents);
27337c478bd9Sstevel@tonic-gate 				prev_link_existed = FALSE;
27347c478bd9Sstevel@tonic-gate 				/* link successfully created */
27357c478bd9Sstevel@tonic-gate 				*exists = TRUE;
27367c478bd9Sstevel@tonic-gate 				set_logindev_perms(devlink);
27377c478bd9Sstevel@tonic-gate 				return (DEVFSADM_SUCCESS);
27387c478bd9Sstevel@tonic-gate 			} else {
27397c478bd9Sstevel@tonic-gate 				switch (errno) {
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate 				case ENOENT:
27427c478bd9Sstevel@tonic-gate 					/* dirpath to node doesn't exist */
27437c478bd9Sstevel@tonic-gate 					hide = strrchr(devlink, '/');
27447c478bd9Sstevel@tonic-gate 					*hide = '\0';
27457c478bd9Sstevel@tonic-gate 					s_mkdirp(devlink, S_IRWXU|S_IRGRP|
27460a653502Swroche 					    S_IXGRP|S_IROTH|S_IXOTH);
27477c478bd9Sstevel@tonic-gate 					*hide = '/';
27487c478bd9Sstevel@tonic-gate 					break;
27497c478bd9Sstevel@tonic-gate 				case EEXIST:
27507c478bd9Sstevel@tonic-gate 					try = READ_LINK;
27517c478bd9Sstevel@tonic-gate 					break;
27527c478bd9Sstevel@tonic-gate 				default:
27537c478bd9Sstevel@tonic-gate 					err_print(SYMLINK_FAILED, devlink,
27540a653502Swroche 					    contents, strerror(errno));
27557c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
27567c478bd9Sstevel@tonic-gate 				}
27577c478bd9Sstevel@tonic-gate 			}
27587c478bd9Sstevel@tonic-gate 			break;
27597c478bd9Sstevel@tonic-gate 
27607c478bd9Sstevel@tonic-gate 		case READ_LINK:
27617c478bd9Sstevel@tonic-gate 
27627c478bd9Sstevel@tonic-gate 			linksize = readlink(devlink, checkcontents, PATH_MAX);
27637c478bd9Sstevel@tonic-gate 			if (linksize >= 0) {
27647c478bd9Sstevel@tonic-gate 				checkcontents[linksize] = '\0';
27657c478bd9Sstevel@tonic-gate 				if (strcmp(checkcontents, contents) != 0) {
27667c478bd9Sstevel@tonic-gate 					s_unlink(devlink);
27677c478bd9Sstevel@tonic-gate 					vprint(VERBOSE_MID, REMOVING_LINK,
27680a653502Swroche 					    devlink, checkcontents);
27697c478bd9Sstevel@tonic-gate 					try = CREATE_LINK;
27707c478bd9Sstevel@tonic-gate 				} else {
27717c478bd9Sstevel@tonic-gate 					prev_link_existed = TRUE;
27727c478bd9Sstevel@tonic-gate 					vprint(CHATTY_MID,
27730a653502Swroche 					    "link exists and is correct:"
27740a653502Swroche 					    " %s -> %s\n", devlink, contents);
27757c478bd9Sstevel@tonic-gate 					*exists = TRUE;
27767c478bd9Sstevel@tonic-gate 					/* failure in that the link existed */
27777c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
27787c478bd9Sstevel@tonic-gate 				}
27797c478bd9Sstevel@tonic-gate 			} else {
27807c478bd9Sstevel@tonic-gate 				switch (errno) {
27817c478bd9Sstevel@tonic-gate 				case EINVAL:
27827c478bd9Sstevel@tonic-gate 					/* not a symlink, remove and create */
27837c478bd9Sstevel@tonic-gate 					s_unlink(devlink);
27847c478bd9Sstevel@tonic-gate 				default:
27857c478bd9Sstevel@tonic-gate 					/* maybe it didn't exist at all */
27867c478bd9Sstevel@tonic-gate 					try = CREATE_LINK;
27877c478bd9Sstevel@tonic-gate 					break;
27887c478bd9Sstevel@tonic-gate 				}
27897c478bd9Sstevel@tonic-gate 			}
27907c478bd9Sstevel@tonic-gate 			break;
27917c478bd9Sstevel@tonic-gate 		}
27927c478bd9Sstevel@tonic-gate 	}
27937c478bd9Sstevel@tonic-gate 	err_print(MAX_ATTEMPTS, devlink, contents);
27947c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
27957c478bd9Sstevel@tonic-gate }
27967c478bd9Sstevel@tonic-gate 
27977c478bd9Sstevel@tonic-gate static void
27987c478bd9Sstevel@tonic-gate set_logindev_perms(char *devlink)
27997c478bd9Sstevel@tonic-gate {
28007c478bd9Sstevel@tonic-gate 	struct login_dev *newdev;
28017c478bd9Sstevel@tonic-gate 	struct passwd pwd, *resp;
28027c478bd9Sstevel@tonic-gate 	char pwd_buf[PATH_MAX];
28037c478bd9Sstevel@tonic-gate 	int rv;
28047c478bd9Sstevel@tonic-gate 	struct stat sb;
28057c478bd9Sstevel@tonic-gate 	char *devfs_path = NULL;
28067c478bd9Sstevel@tonic-gate 
28077c478bd9Sstevel@tonic-gate 	/*
28087c478bd9Sstevel@tonic-gate 	 * We only want logindev perms to be set when a device is
28097c478bd9Sstevel@tonic-gate 	 * hotplugged or an application requests synchronous creates.
28107c478bd9Sstevel@tonic-gate 	 * So we enable this only in daemon mode. In addition,
28117c478bd9Sstevel@tonic-gate 	 * login(1) only fixes the std. /dev dir. So we don't
28127c478bd9Sstevel@tonic-gate 	 * change perms if alternate root is set.
28137c478bd9Sstevel@tonic-gate 	 * login_dev_enable is TRUE only in these cases.
28147c478bd9Sstevel@tonic-gate 	 */
28157c478bd9Sstevel@tonic-gate 	if (login_dev_enable != TRUE)
28167c478bd9Sstevel@tonic-gate 		return;
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate 	/*
28197c478bd9Sstevel@tonic-gate 	 * Normally, /etc/logindevperm has few (8 - 10 entries) which
28207c478bd9Sstevel@tonic-gate 	 * may be regular expressions (globs were converted to RE).
28217c478bd9Sstevel@tonic-gate 	 * So just do a linear search through the list.
28227c478bd9Sstevel@tonic-gate 	 */
28237c478bd9Sstevel@tonic-gate 	for (newdev = login_dev_cache; newdev; newdev = newdev->ldev_next) {
28247c478bd9Sstevel@tonic-gate 		vprint(FILES_MID, "matching %s with %s\n", devlink,
28257c478bd9Sstevel@tonic-gate 		    newdev->ldev_device);
28267c478bd9Sstevel@tonic-gate 
28277c478bd9Sstevel@tonic-gate 		if (regexec(&newdev->ldev_device_regex, devlink, 0,
28287c478bd9Sstevel@tonic-gate 		    NULL, 0) == 0)  {
28297c478bd9Sstevel@tonic-gate 			vprint(FILES_MID, "matched %s with %s\n", devlink,
28307c478bd9Sstevel@tonic-gate 			    newdev->ldev_device);
28317c478bd9Sstevel@tonic-gate 			break;
28327c478bd9Sstevel@tonic-gate 		}
28337c478bd9Sstevel@tonic-gate 	}
28347c478bd9Sstevel@tonic-gate 
28357c478bd9Sstevel@tonic-gate 	if (newdev == NULL)
28367c478bd9Sstevel@tonic-gate 		return;
28377c478bd9Sstevel@tonic-gate 
28387c478bd9Sstevel@tonic-gate 	/*
28397c478bd9Sstevel@tonic-gate 	 * we have a match, now find the driver associated with this
28407c478bd9Sstevel@tonic-gate 	 * minor node using a snapshot on the physical path
28417c478bd9Sstevel@tonic-gate 	 */
28427c478bd9Sstevel@tonic-gate 	(void) resolve_link(devlink, NULL, NULL, &devfs_path, 0);
28437c478bd9Sstevel@tonic-gate 	if (devfs_path) {
28447c478bd9Sstevel@tonic-gate 		di_node_t node;
28457c478bd9Sstevel@tonic-gate 		char *drv = NULL;
28467c478bd9Sstevel@tonic-gate 		struct driver_list *list;
28477c478bd9Sstevel@tonic-gate 		char *p;
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate 		/* truncate on : so we can take a snapshot */
28507c478bd9Sstevel@tonic-gate 		(void) strcpy(pwd_buf, devfs_path);
28517c478bd9Sstevel@tonic-gate 		p = strrchr(pwd_buf, ':');
28527c478bd9Sstevel@tonic-gate 		if (p == NULL) {
28537c478bd9Sstevel@tonic-gate 			free(devfs_path);
28547c478bd9Sstevel@tonic-gate 			return;
28557c478bd9Sstevel@tonic-gate 		}
28567c478bd9Sstevel@tonic-gate 		*p = '\0';
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate 		vprint(FILES_MID, "link=%s->physpath=%s\n",
28597c478bd9Sstevel@tonic-gate 		    devlink, pwd_buf);
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate 		node = di_init(pwd_buf, DINFOMINOR);
28627c478bd9Sstevel@tonic-gate 
28637c478bd9Sstevel@tonic-gate 		if (node) {
28647c478bd9Sstevel@tonic-gate 			drv = di_driver_name(node);
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate 			if (drv) {
28677c478bd9Sstevel@tonic-gate 				vprint(FILES_MID, "%s: driver is %s\n",
28687c478bd9Sstevel@tonic-gate 				    devlink, drv);
28697c478bd9Sstevel@tonic-gate 			}
28707c478bd9Sstevel@tonic-gate 			di_fini(node);
28717c478bd9Sstevel@tonic-gate 		}
28727c478bd9Sstevel@tonic-gate 		/* search thru the driver list specified in logindevperm */
28737c478bd9Sstevel@tonic-gate 		list = newdev->ldev_driver_list;
28747c478bd9Sstevel@tonic-gate 		if ((drv != NULL) && (list != NULL)) {
28757c478bd9Sstevel@tonic-gate 			while (list) {
28767c478bd9Sstevel@tonic-gate 				if (strcmp(list->driver_name,
28777c478bd9Sstevel@tonic-gate 				    drv) == 0) {
28787c478bd9Sstevel@tonic-gate 					vprint(FILES_MID,
28797c478bd9Sstevel@tonic-gate 					    "driver %s match!\n", drv);
28807c478bd9Sstevel@tonic-gate 					break;
28817c478bd9Sstevel@tonic-gate 				}
28827c478bd9Sstevel@tonic-gate 				list = list->next;
28837c478bd9Sstevel@tonic-gate 			}
28847c478bd9Sstevel@tonic-gate 			if (list == NULL) {
28857c478bd9Sstevel@tonic-gate 				vprint(FILES_MID, "no driver match!\n");
28867c478bd9Sstevel@tonic-gate 				free(devfs_path);
28877c478bd9Sstevel@tonic-gate 				return;
28887c478bd9Sstevel@tonic-gate 			}
28897c478bd9Sstevel@tonic-gate 		}
28907c478bd9Sstevel@tonic-gate 		free(devfs_path);
28917c478bd9Sstevel@tonic-gate 	} else {
28927c478bd9Sstevel@tonic-gate 		return;
28937c478bd9Sstevel@tonic-gate 	}
28947c478bd9Sstevel@tonic-gate 
28957c478bd9Sstevel@tonic-gate 	vprint(FILES_MID, "changing permissions of %s\n", devlink);
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate 	/*
28987c478bd9Sstevel@tonic-gate 	 * We have a match. We now attempt to determine the
28997c478bd9Sstevel@tonic-gate 	 * owner and group of the console user.
29007c478bd9Sstevel@tonic-gate 	 *
29017c478bd9Sstevel@tonic-gate 	 * stat() the console device newdev->ldev_console
29027c478bd9Sstevel@tonic-gate 	 * which will always exist - it will have the right owner but
29037c478bd9Sstevel@tonic-gate 	 * not the right group. Use getpwuid_r() to determine group for this
29047c478bd9Sstevel@tonic-gate 	 * uid.
29057c478bd9Sstevel@tonic-gate 	 * Note, it is safe to use name service here since if name services
29067c478bd9Sstevel@tonic-gate 	 * are not available (during boot or in single-user mode), then
29077c478bd9Sstevel@tonic-gate 	 * console owner will be root and its gid can be found in
29087c478bd9Sstevel@tonic-gate 	 * local files.
29097c478bd9Sstevel@tonic-gate 	 */
29107c478bd9Sstevel@tonic-gate 	if (stat(newdev->ldev_console, &sb) == -1) {
29117c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, STAT_FAILED, newdev->ldev_console,
29127c478bd9Sstevel@tonic-gate 		    strerror(errno));
29137c478bd9Sstevel@tonic-gate 		return;
29147c478bd9Sstevel@tonic-gate 	}
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate 	resp = NULL;
29177c478bd9Sstevel@tonic-gate 	rv = getpwuid_r(sb.st_uid, &pwd, pwd_buf, sizeof (pwd_buf), &resp);
29187c478bd9Sstevel@tonic-gate 	if (rv || resp == NULL) {
29197c478bd9Sstevel@tonic-gate 		rv = rv ? rv : EINVAL;
29207c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, GID_FAILED, sb.st_uid,
29217c478bd9Sstevel@tonic-gate 		    strerror(rv));
29227c478bd9Sstevel@tonic-gate 		return;
29237c478bd9Sstevel@tonic-gate 	}
29247c478bd9Sstevel@tonic-gate 
29257c478bd9Sstevel@tonic-gate 	assert(&pwd == resp);
29267c478bd9Sstevel@tonic-gate 
29277c478bd9Sstevel@tonic-gate 	sb.st_gid = resp->pw_gid;
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate 	if (chmod(devlink, newdev->ldev_perms) == -1) {
29307c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, CHMOD_FAILED, devlink,
29317c478bd9Sstevel@tonic-gate 		    strerror(errno));
29327c478bd9Sstevel@tonic-gate 		return;
29337c478bd9Sstevel@tonic-gate 	}
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate 	if (chown(devlink, sb.st_uid, sb.st_gid)  == -1) {
29367c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, CHOWN_FAILED, devlink,
29377c478bd9Sstevel@tonic-gate 		    strerror(errno));
29387c478bd9Sstevel@tonic-gate 	}
29397c478bd9Sstevel@tonic-gate }
29407c478bd9Sstevel@tonic-gate 
29417c478bd9Sstevel@tonic-gate /*
29427c478bd9Sstevel@tonic-gate  * Reset /devices node with appropriate permissions and
29437c478bd9Sstevel@tonic-gate  * ownership as specified in /etc/minor_perm.
29447c478bd9Sstevel@tonic-gate  */
29457c478bd9Sstevel@tonic-gate static void
29467c478bd9Sstevel@tonic-gate reset_node_permissions(di_node_t node, di_minor_t minor)
29477c478bd9Sstevel@tonic-gate {
29487c478bd9Sstevel@tonic-gate 	int spectype;
29497c478bd9Sstevel@tonic-gate 	char phy_path[PATH_MAX + 1];
29507c478bd9Sstevel@tonic-gate 	mode_t mode;
29517c478bd9Sstevel@tonic-gate 	dev_t dev;
29527c478bd9Sstevel@tonic-gate 	uid_t uid;
29537c478bd9Sstevel@tonic-gate 	gid_t gid;
29547c478bd9Sstevel@tonic-gate 	struct stat sb;
29557c478bd9Sstevel@tonic-gate 	char *dev_path, *aminor = NULL;
29567c478bd9Sstevel@tonic-gate 
29577c478bd9Sstevel@tonic-gate 	/* lphy_path starts with / */
29587c478bd9Sstevel@tonic-gate 	if ((dev_path = di_devfs_path(node)) == NULL) {
29597c478bd9Sstevel@tonic-gate 		err_print(DI_DEVFS_PATH_FAILED, strerror(errno));
29607c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
2961537714daSvikram 		/*NOTREACHED*/
29627c478bd9Sstevel@tonic-gate 	}
29637c478bd9Sstevel@tonic-gate 	(void) strcpy(lphy_path, dev_path);
29647c478bd9Sstevel@tonic-gate 	di_devfs_path_free(dev_path);
29657c478bd9Sstevel@tonic-gate 
29667c478bd9Sstevel@tonic-gate 	(void) strcat(lphy_path, ":");
29677c478bd9Sstevel@tonic-gate 	if (di_minor_type(minor) == DDM_ALIAS) {
29687c478bd9Sstevel@tonic-gate 		char *driver;
29697c478bd9Sstevel@tonic-gate 		aminor = di_minor_name(minor);
29707c478bd9Sstevel@tonic-gate 		driver = di_driver_name(di_minor_devinfo(minor));
29717c478bd9Sstevel@tonic-gate 		(void) strcat(lphy_path, driver);
29727c478bd9Sstevel@tonic-gate 	} else
29737c478bd9Sstevel@tonic-gate 		(void) strcat(lphy_path, di_minor_name(minor));
29747c478bd9Sstevel@tonic-gate 
29757c478bd9Sstevel@tonic-gate 	(void) strcpy(phy_path, devices_dir);
29767c478bd9Sstevel@tonic-gate 	(void) strcat(phy_path, lphy_path);
29777c478bd9Sstevel@tonic-gate 
29787c478bd9Sstevel@tonic-gate 	lnode = node;
29797c478bd9Sstevel@tonic-gate 	lminor = minor;
29807c478bd9Sstevel@tonic-gate 
29817c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "reset_node_permissions: phy_path=%s lphy_path=%s\n",
29820a653502Swroche 	    phy_path, lphy_path);
29837c478bd9Sstevel@tonic-gate 
29847c478bd9Sstevel@tonic-gate 	dev = di_minor_devt(minor);
29857c478bd9Sstevel@tonic-gate 	spectype = di_minor_spectype(minor); /* block or char */
29867c478bd9Sstevel@tonic-gate 
29877c478bd9Sstevel@tonic-gate 	getattr(phy_path, aminor, spectype, dev, &mode, &uid, &gid);
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate 	/*
29907c478bd9Sstevel@tonic-gate 	 * compare and set permissions and ownership
29917c478bd9Sstevel@tonic-gate 	 *
29927c478bd9Sstevel@tonic-gate 	 * Under devfs, a quick insertion and removal of USB devices
29937c478bd9Sstevel@tonic-gate 	 * would cause stat of physical path to fail. In this case,
29947c478bd9Sstevel@tonic-gate 	 * we emit a verbose message, but don't print errors.
29957c478bd9Sstevel@tonic-gate 	 */
29967c478bd9Sstevel@tonic-gate 	if ((stat(phy_path, &sb) == -1) || (sb.st_rdev != dev)) {
29977c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, NO_DEVFS_NODE, phy_path);
29987c478bd9Sstevel@tonic-gate 		return;
29997c478bd9Sstevel@tonic-gate 	}
30007c478bd9Sstevel@tonic-gate 
30017c478bd9Sstevel@tonic-gate 	/*
300245916cd2Sjpk 	 * If we are here for a new device
300345916cd2Sjpk 	 *	If device allocation is on
300445916cd2Sjpk 	 *	then
300545916cd2Sjpk 	 *		set ownership to root:other and permissions to 0000
300645916cd2Sjpk 	 *	else
300745916cd2Sjpk 	 *		set ownership and permissions as specified in minor_perm
300845916cd2Sjpk 	 * If we are here for an existing device
300945916cd2Sjpk 	 *	If device allocation is to be turned on
301045916cd2Sjpk 	 *	then
301145916cd2Sjpk 	 *		reset ownership to root:other and permissions to 0000
301245916cd2Sjpk 	 *	else if device allocation is to be turned off
301345916cd2Sjpk 	 *		reset ownership and permissions to those specified in
301445916cd2Sjpk 	 *		minor_perm
301545916cd2Sjpk 	 *	else
3016ff2aee48Scth 	 *		preserve existing/user-modified ownership and
301745916cd2Sjpk 	 *		permissions
301845916cd2Sjpk 	 *
301945916cd2Sjpk 	 * devfs indicates a new device by faking access time to be zero.
30207c478bd9Sstevel@tonic-gate 	 */
30217c478bd9Sstevel@tonic-gate 	if (sb.st_atime != 0) {
30227c478bd9Sstevel@tonic-gate 		int  i;
30237c478bd9Sstevel@tonic-gate 		char *nt;
30247c478bd9Sstevel@tonic-gate 
302545916cd2Sjpk 		if ((devalloc_flag == 0) && (devalloc_is_on != 1))
302645916cd2Sjpk 			/*
302745916cd2Sjpk 			 * Leave existing devices as they are if we are not
302845916cd2Sjpk 			 * turning device allocation on/off.
302945916cd2Sjpk 			 */
30307c478bd9Sstevel@tonic-gate 			return;
30317c478bd9Sstevel@tonic-gate 
30327c478bd9Sstevel@tonic-gate 		nt = di_minor_nodetype(minor);
303345916cd2Sjpk 
30347c478bd9Sstevel@tonic-gate 		if (nt == NULL)
30357c478bd9Sstevel@tonic-gate 			return;
303645916cd2Sjpk 
303745916cd2Sjpk 		for (i = 0; devalloc_list[i]; i++) {
303845916cd2Sjpk 			if (strcmp(nt, devalloc_list[i]) == 0)
303945916cd2Sjpk 				/*
304045916cd2Sjpk 				 * One of the types recognized by devalloc,
304145916cd2Sjpk 				 * reset attrs.
304245916cd2Sjpk 				 */
30437c478bd9Sstevel@tonic-gate 				break;
30447c478bd9Sstevel@tonic-gate 		}
304545916cd2Sjpk 		if (devalloc_list[i] == NULL)
30467c478bd9Sstevel@tonic-gate 			return;
30477c478bd9Sstevel@tonic-gate 	}
30487c478bd9Sstevel@tonic-gate 
30497c478bd9Sstevel@tonic-gate 	if (file_mods == FALSE) {
30507c478bd9Sstevel@tonic-gate 		/* Nothing more to do if simulating */
30517c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, PERM_MSG, phy_path, uid, gid, mode);
30527c478bd9Sstevel@tonic-gate 		return;
30537c478bd9Sstevel@tonic-gate 	}
30547c478bd9Sstevel@tonic-gate 
30558c206d17Spr 	if ((devalloc_flag == DA_ON) ||
30568c206d17Spr 	    ((devalloc_is_on == 1) && (devalloc_flag != DA_OFF))) {
305745916cd2Sjpk 		/*
30588c206d17Spr 		 * we are here either to turn device allocation on or
30598c206d17Spr 		 * to add a new device while device allocation is on
30608c206d17Spr 		 * (and we've confirmed that we're not turning it
30618c206d17Spr 		 * off).
306245916cd2Sjpk 		 */
306345916cd2Sjpk 		mode = DEALLOC_MODE;
306445916cd2Sjpk 		uid = DA_UID;
306545916cd2Sjpk 		gid = DA_GID;
306645916cd2Sjpk 	}
306745916cd2Sjpk 
306845916cd2Sjpk 	if ((devalloc_is_on == 1) || (devalloc_flag == DA_ON) ||
306945916cd2Sjpk 	    (sb.st_mode != mode)) {
30707c478bd9Sstevel@tonic-gate 		if (chmod(phy_path, mode) == -1)
30717c478bd9Sstevel@tonic-gate 			vprint(VERBOSE_MID, CHMOD_FAILED,
30727c478bd9Sstevel@tonic-gate 			    phy_path, strerror(errno));
30737c478bd9Sstevel@tonic-gate 	}
307445916cd2Sjpk 	if ((devalloc_is_on == 1) || (devalloc_flag == DA_ON) ||
307545916cd2Sjpk 	    (sb.st_uid != uid || sb.st_gid != gid)) {
30767c478bd9Sstevel@tonic-gate 		if (chown(phy_path, uid, gid) == -1)
30777c478bd9Sstevel@tonic-gate 			vprint(VERBOSE_MID, CHOWN_FAILED,
30787c478bd9Sstevel@tonic-gate 			    phy_path, strerror(errno));
30797c478bd9Sstevel@tonic-gate 	}
30807c478bd9Sstevel@tonic-gate 
30817c478bd9Sstevel@tonic-gate 	/* Report that we actually did something */
30827c478bd9Sstevel@tonic-gate 	vprint(VERBOSE_MID, PERM_MSG, phy_path, uid, gid, mode);
30837c478bd9Sstevel@tonic-gate }
30847c478bd9Sstevel@tonic-gate 
30857c478bd9Sstevel@tonic-gate /*
30867c478bd9Sstevel@tonic-gate  * Removes logical link and the minor node it refers to.  If file is a
30877c478bd9Sstevel@tonic-gate  * link, we recurse and try to remove the minor node (or link if path is
30887c478bd9Sstevel@tonic-gate  * a double link) that file's link contents refer to.
30897c478bd9Sstevel@tonic-gate  */
30907c478bd9Sstevel@tonic-gate static void
30917c478bd9Sstevel@tonic-gate devfsadm_rm_work(char *file, int recurse, int file_type)
30927c478bd9Sstevel@tonic-gate {
30937c478bd9Sstevel@tonic-gate 	char *fcn = "devfsadm_rm_work: ";
30947c478bd9Sstevel@tonic-gate 	int linksize;
30957c478bd9Sstevel@tonic-gate 	char contents[PATH_MAX + 1];
30967c478bd9Sstevel@tonic-gate 	char nextfile[PATH_MAX + 1];
30977c478bd9Sstevel@tonic-gate 	char newfile[PATH_MAX + 1];
30987c478bd9Sstevel@tonic-gate 	char *ptr;
30997c478bd9Sstevel@tonic-gate 
31007c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%s%s\n", fcn, file);
31017c478bd9Sstevel@tonic-gate 
31027c478bd9Sstevel@tonic-gate 	/* TYPE_LINK split into multiple if's due to excessive indentations */
31037c478bd9Sstevel@tonic-gate 	if (file_type == TYPE_LINK) {
31047c478bd9Sstevel@tonic-gate 		(void) strcpy(newfile, dev_dir);
31057c478bd9Sstevel@tonic-gate 		(void) strcat(newfile, "/");
31067c478bd9Sstevel@tonic-gate 		(void) strcat(newfile, file);
31077c478bd9Sstevel@tonic-gate 	}
31087c478bd9Sstevel@tonic-gate 
31097c478bd9Sstevel@tonic-gate 	if ((file_type == TYPE_LINK) && (recurse == TRUE) &&
31107c478bd9Sstevel@tonic-gate 	    ((linksize = readlink(newfile, contents, PATH_MAX)) > 0)) {
31117c478bd9Sstevel@tonic-gate 		contents[linksize] = '\0';
31127c478bd9Sstevel@tonic-gate 
31137c478bd9Sstevel@tonic-gate 		if (is_minor_node(contents, &ptr) == DEVFSADM_TRUE) {
31147c478bd9Sstevel@tonic-gate 			devfsadm_rm_work(++ptr, FALSE, TYPE_DEVICES);
31157c478bd9Sstevel@tonic-gate 		} else {
31167c478bd9Sstevel@tonic-gate 			if (strncmp(contents, DEV "/", strlen(DEV) + 1) == 0) {
31177c478bd9Sstevel@tonic-gate 				devfsadm_rm_work(&contents[strlen(DEV) + 1],
31180a653502Swroche 				    TRUE, TYPE_LINK);
31197c478bd9Sstevel@tonic-gate 			} else {
31207c478bd9Sstevel@tonic-gate 				if ((ptr = strrchr(file, '/')) != NULL) {
31217c478bd9Sstevel@tonic-gate 					*ptr = '\0';
31227c478bd9Sstevel@tonic-gate 					(void) strcpy(nextfile, file);
31237c478bd9Sstevel@tonic-gate 					*ptr = '/';
31247c478bd9Sstevel@tonic-gate 					(void) strcat(nextfile, "/");
31257c478bd9Sstevel@tonic-gate 				} else {
31267c478bd9Sstevel@tonic-gate 					(void) strcpy(nextfile, "");
31277c478bd9Sstevel@tonic-gate 				}
31287c478bd9Sstevel@tonic-gate 				(void) strcat(nextfile, contents);
31297c478bd9Sstevel@tonic-gate 				devfsadm_rm_work(nextfile, TRUE, TYPE_LINK);
31307c478bd9Sstevel@tonic-gate 			}
31317c478bd9Sstevel@tonic-gate 		}
31327c478bd9Sstevel@tonic-gate 	}
31337c478bd9Sstevel@tonic-gate 
31347c478bd9Sstevel@tonic-gate 	if (file_type == TYPE_LINK) {
31357c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, DEVFSADM_UNLINK, newfile);
31367c478bd9Sstevel@tonic-gate 		if (file_mods == TRUE) {
31377c478bd9Sstevel@tonic-gate 			rm_link_from_cache(file);
31387c478bd9Sstevel@tonic-gate 			s_unlink(newfile);
31397c478bd9Sstevel@tonic-gate 			rm_parent_dir_if_empty(newfile);
31407c478bd9Sstevel@tonic-gate 			invalidate_enumerate_cache();
31417c478bd9Sstevel@tonic-gate 			(void) di_devlink_rm_link(devlink_cache, file);
31427c478bd9Sstevel@tonic-gate 		}
31437c478bd9Sstevel@tonic-gate 	}
31447c478bd9Sstevel@tonic-gate 
31457c478bd9Sstevel@tonic-gate 	/*
31467c478bd9Sstevel@tonic-gate 	 * Note: we don't remove /devices entries because they are
31477c478bd9Sstevel@tonic-gate 	 *	covered by devfs.
31487c478bd9Sstevel@tonic-gate 	 */
31497c478bd9Sstevel@tonic-gate }
31507c478bd9Sstevel@tonic-gate 
31517c478bd9Sstevel@tonic-gate void
31527c478bd9Sstevel@tonic-gate devfsadm_rm_link(char *file)
31537c478bd9Sstevel@tonic-gate {
31547c478bd9Sstevel@tonic-gate 	devfsadm_rm_work(file, FALSE, TYPE_LINK);
31557c478bd9Sstevel@tonic-gate }
31567c478bd9Sstevel@tonic-gate 
31577c478bd9Sstevel@tonic-gate void
31587c478bd9Sstevel@tonic-gate devfsadm_rm_all(char *file)
31597c478bd9Sstevel@tonic-gate {
31607c478bd9Sstevel@tonic-gate 	devfsadm_rm_work(file, TRUE, TYPE_LINK);
31617c478bd9Sstevel@tonic-gate }
31627c478bd9Sstevel@tonic-gate 
31634bc0a2efScasper static int
31647c478bd9Sstevel@tonic-gate s_rmdir(char *path)
31657c478bd9Sstevel@tonic-gate {
31667c478bd9Sstevel@tonic-gate 	int	i;
31677c478bd9Sstevel@tonic-gate 	char	*rpath, *dir;
31687c478bd9Sstevel@tonic-gate 	const char *fcn = "s_rmdir";
31697c478bd9Sstevel@tonic-gate 
31707c478bd9Sstevel@tonic-gate 	/*
31717c478bd9Sstevel@tonic-gate 	 * Certain directories are created at install time by packages.
31727c478bd9Sstevel@tonic-gate 	 * Some of them (listed in packaged_dirs[]) are required by apps
31737c478bd9Sstevel@tonic-gate 	 * and need to be present even when empty.
31747c478bd9Sstevel@tonic-gate 	 */
31757c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%s: checking if %s is packaged\n", fcn, path);
31767c478bd9Sstevel@tonic-gate 
31777c478bd9Sstevel@tonic-gate 	rpath = path + strlen(dev_dir) + 1;
31787c478bd9Sstevel@tonic-gate 
31797c478bd9Sstevel@tonic-gate 	for (i = 0; (dir = packaged_dirs[i]) != NULL; i++) {
31807c478bd9Sstevel@tonic-gate 		if (*rpath == *dir) {
31817c478bd9Sstevel@tonic-gate 			if (strcmp(rpath, dir) == 0) {
31827c478bd9Sstevel@tonic-gate 				vprint(REMOVE_MID, "%s: skipping packaged dir: "
31837c478bd9Sstevel@tonic-gate 				    "%s\n", fcn, path);
31844bc0a2efScasper 				errno = EEXIST;
31854bc0a2efScasper 				return (-1);
31867c478bd9Sstevel@tonic-gate 			}
31877c478bd9Sstevel@tonic-gate 		}
31887c478bd9Sstevel@tonic-gate 	}
31897c478bd9Sstevel@tonic-gate 
31904bc0a2efScasper 	return (rmdir(path));
31917c478bd9Sstevel@tonic-gate }
31927c478bd9Sstevel@tonic-gate 
31937c478bd9Sstevel@tonic-gate /*
31947c478bd9Sstevel@tonic-gate  * Try to remove any empty directories up the tree.  It is assumed that
31957c478bd9Sstevel@tonic-gate  * pathname is a file that was removed, so start with its parent, and
31967c478bd9Sstevel@tonic-gate  * work up the tree.
31977c478bd9Sstevel@tonic-gate  */
31987c478bd9Sstevel@tonic-gate static void
31997c478bd9Sstevel@tonic-gate rm_parent_dir_if_empty(char *pathname)
32007c478bd9Sstevel@tonic-gate {
32017c478bd9Sstevel@tonic-gate 	char *ptr, path[PATH_MAX + 1];
32027c478bd9Sstevel@tonic-gate 	char *fcn = "rm_parent_dir_if_empty: ";
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%schecking %s if empty\n", fcn, pathname);
32057c478bd9Sstevel@tonic-gate 
32067c478bd9Sstevel@tonic-gate 	(void) strcpy(path, pathname);
32077c478bd9Sstevel@tonic-gate 
32087c478bd9Sstevel@tonic-gate 	/*
32097c478bd9Sstevel@tonic-gate 	 * ascend up the dir tree, deleting all empty dirs.
32107c478bd9Sstevel@tonic-gate 	 * Return immediately if a dir is not empty.
32117c478bd9Sstevel@tonic-gate 	 */
32127c478bd9Sstevel@tonic-gate 	for (;;) {
32137c478bd9Sstevel@tonic-gate 
32147c478bd9Sstevel@tonic-gate 		if ((ptr = strrchr(path, '/')) == NULL) {
32157c478bd9Sstevel@tonic-gate 			return;
32167c478bd9Sstevel@tonic-gate 		}
32177c478bd9Sstevel@tonic-gate 
32187c478bd9Sstevel@tonic-gate 		*ptr = '\0';
32197c478bd9Sstevel@tonic-gate 
3220e37c6c37Scth 		if (finddev_emptydir(path)) {
3221e37c6c37Scth 			/* directory is empty */
3222facf4a8dSllai 			if (s_rmdir(path) == 0) {
3223facf4a8dSllai 				vprint(REMOVE_MID,
3224facf4a8dSllai 				    "%sremoving empty dir %s\n", fcn, path);
3225facf4a8dSllai 			} else if (errno == EEXIST) {
3226facf4a8dSllai 				vprint(REMOVE_MID,
3227facf4a8dSllai 				    "%sfailed to remove dir: %s\n", fcn, path);
3228facf4a8dSllai 				return;
3229facf4a8dSllai 			}
3230facf4a8dSllai 		} else {
3231facf4a8dSllai 			/* some other file is here, so return */
32327c478bd9Sstevel@tonic-gate 			vprint(REMOVE_MID, "%sdir not empty: %s\n", fcn, path);
32337c478bd9Sstevel@tonic-gate 			return;
32347c478bd9Sstevel@tonic-gate 		}
32357c478bd9Sstevel@tonic-gate 	}
32367c478bd9Sstevel@tonic-gate }
32377c478bd9Sstevel@tonic-gate 
32387c478bd9Sstevel@tonic-gate /*
32397c478bd9Sstevel@tonic-gate  * This function and all the functions it calls below were added to
32407c478bd9Sstevel@tonic-gate  * handle the unique problem with world wide names (WWN).  The problem is
32417c478bd9Sstevel@tonic-gate  * that if a WWN device is moved to another address on the same controller
32427c478bd9Sstevel@tonic-gate  * its logical link will change, while the physical node remains the same.
32437c478bd9Sstevel@tonic-gate  * The result is that two logical links will point to the same physical path
32447c478bd9Sstevel@tonic-gate  * in /devices, the valid link and a stale link. This function will
32457c478bd9Sstevel@tonic-gate  * find all the stale nodes, though at a significant performance cost.
32467c478bd9Sstevel@tonic-gate  *
32477c478bd9Sstevel@tonic-gate  * Caching is used to increase performance.
32487c478bd9Sstevel@tonic-gate  * A cache will be built from disk if the cache tag doesn't already exist.
32497c478bd9Sstevel@tonic-gate  * The cache tag is a regular expression "dir_re", which selects a
32507c478bd9Sstevel@tonic-gate  * subset of disks to search from typically something like
32517c478bd9Sstevel@tonic-gate  * "dev/cXt[0-9]+d[0-9]+s[0-9]+".  After the cache is built, consistency must
32527c478bd9Sstevel@tonic-gate  * be maintained, so entries are added as new links are created, and removed
32537c478bd9Sstevel@tonic-gate  * as old links are deleted.  The whole cache is flushed if we are a daemon,
32547c478bd9Sstevel@tonic-gate  * and another devfsadm process ran in between.
32557c478bd9Sstevel@tonic-gate  *
32567c478bd9Sstevel@tonic-gate  * Once the cache is built, this function finds the cache which matches
32577c478bd9Sstevel@tonic-gate  * dir_re, and then it searches all links in that cache looking for
32587c478bd9Sstevel@tonic-gate  * any link whose contents match "valid_link_contents" with a corresponding link
32597c478bd9Sstevel@tonic-gate  * which does not match "valid_link".  Any such matches are stale and removed.
32607c478bd9Sstevel@tonic-gate  */
32617c478bd9Sstevel@tonic-gate void
32627c478bd9Sstevel@tonic-gate devfsadm_rm_stale_links(char *dir_re, char *valid_link, di_node_t node,
32637c478bd9Sstevel@tonic-gate 			di_minor_t minor)
32647c478bd9Sstevel@tonic-gate {
32657c478bd9Sstevel@tonic-gate 	link_t *link;
32667c478bd9Sstevel@tonic-gate 	linkhead_t *head;
32677c478bd9Sstevel@tonic-gate 	char phy_path[PATH_MAX + 1];
32687c478bd9Sstevel@tonic-gate 	char *valid_link_contents;
32697c478bd9Sstevel@tonic-gate 	char *dev_path;
32707c478bd9Sstevel@tonic-gate 	char rmlink[PATH_MAX + 1];
32717c478bd9Sstevel@tonic-gate 
32727c478bd9Sstevel@tonic-gate 	/*
32737c478bd9Sstevel@tonic-gate 	 * try to use devices path
32747c478bd9Sstevel@tonic-gate 	 */
32757c478bd9Sstevel@tonic-gate 	if ((node == lnode) && (minor == lminor)) {
32767c478bd9Sstevel@tonic-gate 		valid_link_contents = lphy_path;
32777c478bd9Sstevel@tonic-gate 	} else {
32787c478bd9Sstevel@tonic-gate 		if ((dev_path = di_devfs_path(node)) == NULL) {
32797c478bd9Sstevel@tonic-gate 			err_print(DI_DEVFS_PATH_FAILED, strerror(errno));
32807c478bd9Sstevel@tonic-gate 			devfsadm_exit(1);
3281537714daSvikram 			/*NOTREACHED*/
32827c478bd9Sstevel@tonic-gate 		}
32837c478bd9Sstevel@tonic-gate 		(void) strcpy(phy_path, dev_path);
32847c478bd9Sstevel@tonic-gate 		di_devfs_path_free(dev_path);
32857c478bd9Sstevel@tonic-gate 
32867c478bd9Sstevel@tonic-gate 		(void) strcat(phy_path, ":");
32877c478bd9Sstevel@tonic-gate 		(void) strcat(phy_path, di_minor_name(minor));
32887c478bd9Sstevel@tonic-gate 		valid_link_contents = phy_path;
32897c478bd9Sstevel@tonic-gate 	}
32907c478bd9Sstevel@tonic-gate 
32917c478bd9Sstevel@tonic-gate 	/*
32927c478bd9Sstevel@tonic-gate 	 * As an optimization, check to make sure the corresponding
32937c478bd9Sstevel@tonic-gate 	 * devlink was just created before continuing.
32947c478bd9Sstevel@tonic-gate 	 */
32957c478bd9Sstevel@tonic-gate 
32967c478bd9Sstevel@tonic-gate 	if (linknew == FALSE) {
32977c478bd9Sstevel@tonic-gate 		return;
32987c478bd9Sstevel@tonic-gate 	}
32997c478bd9Sstevel@tonic-gate 
33007c478bd9Sstevel@tonic-gate 	head = get_cached_links(dir_re);
33017c478bd9Sstevel@tonic-gate 
33027c478bd9Sstevel@tonic-gate 	assert(head->nextlink == NULL);
33037c478bd9Sstevel@tonic-gate 
33047c478bd9Sstevel@tonic-gate 	for (link = head->link; link != NULL; link = head->nextlink) {
33057c478bd9Sstevel@tonic-gate 		/*
33067c478bd9Sstevel@tonic-gate 		 * See hot_cleanup() for why we do this
33077c478bd9Sstevel@tonic-gate 		 */
33087c478bd9Sstevel@tonic-gate 		head->nextlink = link->next;
33097c478bd9Sstevel@tonic-gate 		if ((strcmp(link->contents, valid_link_contents) == 0) &&
33107c478bd9Sstevel@tonic-gate 		    (strcmp(link->devlink, valid_link) != 0)) {
33117c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "removing %s -> %s\n"
33120a653502Swroche 			    "valid link is: %s -> %s\n",
33130a653502Swroche 			    link->devlink, link->contents,
33140a653502Swroche 			    valid_link, valid_link_contents);
33157c478bd9Sstevel@tonic-gate 			/*
33167c478bd9Sstevel@tonic-gate 			 * Use a copy of the cached link name as the
33177c478bd9Sstevel@tonic-gate 			 * cache entry will go away during link removal
33187c478bd9Sstevel@tonic-gate 			 */
33197c478bd9Sstevel@tonic-gate 			(void) snprintf(rmlink, sizeof (rmlink), "%s",
33207c478bd9Sstevel@tonic-gate 			    link->devlink);
33217c478bd9Sstevel@tonic-gate 			devfsadm_rm_link(rmlink);
33227c478bd9Sstevel@tonic-gate 		}
33237c478bd9Sstevel@tonic-gate 	}
33247c478bd9Sstevel@tonic-gate }
33257c478bd9Sstevel@tonic-gate 
33267c478bd9Sstevel@tonic-gate /*
33277c478bd9Sstevel@tonic-gate  * Return previously created cache, or create cache.
33287c478bd9Sstevel@tonic-gate  */
33297c478bd9Sstevel@tonic-gate static linkhead_t *
33307c478bd9Sstevel@tonic-gate get_cached_links(char *dir_re)
33317c478bd9Sstevel@tonic-gate {
33327c478bd9Sstevel@tonic-gate 	recurse_dev_t rd;
33337c478bd9Sstevel@tonic-gate 	linkhead_t *linkhead;
33347c478bd9Sstevel@tonic-gate 	int n;
33357c478bd9Sstevel@tonic-gate 
33367c478bd9Sstevel@tonic-gate 	vprint(BUILDCACHE_MID, "get_cached_links: %s\n", dir_re);
33377c478bd9Sstevel@tonic-gate 
33387c478bd9Sstevel@tonic-gate 	for (linkhead = headlinkhead; linkhead != NULL;
33390a653502Swroche 	    linkhead = linkhead->nexthead) {
33407c478bd9Sstevel@tonic-gate 		if (strcmp(linkhead->dir_re, dir_re) == 0) {
33417c478bd9Sstevel@tonic-gate 			return (linkhead);
33427c478bd9Sstevel@tonic-gate 		}
33437c478bd9Sstevel@tonic-gate 	}
33447c478bd9Sstevel@tonic-gate 
33457c478bd9Sstevel@tonic-gate 	/*
33467c478bd9Sstevel@tonic-gate 	 * This tag is not in cache, so add it, along with all its
33477c478bd9Sstevel@tonic-gate 	 * matching /dev entries.  This is the only time we go to disk.
33487c478bd9Sstevel@tonic-gate 	 */
33497c478bd9Sstevel@tonic-gate 	linkhead = s_malloc(sizeof (linkhead_t));
33507c478bd9Sstevel@tonic-gate 	linkhead->nexthead = headlinkhead;
33517c478bd9Sstevel@tonic-gate 	headlinkhead = linkhead;
33527c478bd9Sstevel@tonic-gate 	linkhead->dir_re = s_strdup(dir_re);
33537c478bd9Sstevel@tonic-gate 
33547c478bd9Sstevel@tonic-gate 	if ((n = regcomp(&(linkhead->dir_re_compiled), dir_re,
33550a653502Swroche 	    REG_EXTENDED)) != 0) {
33567c478bd9Sstevel@tonic-gate 		err_print(REGCOMP_FAILED,  dir_re, n);
33577c478bd9Sstevel@tonic-gate 	}
33587c478bd9Sstevel@tonic-gate 
33597c478bd9Sstevel@tonic-gate 	linkhead->nextlink = NULL;
33607c478bd9Sstevel@tonic-gate 	linkhead->link = NULL;
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate 	rd.fcn = build_devlink_list;
33637c478bd9Sstevel@tonic-gate 	rd.data = (void *)linkhead;
33647c478bd9Sstevel@tonic-gate 
33657c478bd9Sstevel@tonic-gate 	vprint(BUILDCACHE_MID, "get_cached_links: calling recurse_dev_re\n");
33667c478bd9Sstevel@tonic-gate 
33677c478bd9Sstevel@tonic-gate 	/* call build_devlink_list for each directory in the dir_re RE */
33687c478bd9Sstevel@tonic-gate 	if (dir_re[0] == '/') {
33697c478bd9Sstevel@tonic-gate 		recurse_dev_re("/", &dir_re[1], &rd);
33707c478bd9Sstevel@tonic-gate 	} else {
33717c478bd9Sstevel@tonic-gate 		recurse_dev_re(dev_dir, dir_re, &rd);
33727c478bd9Sstevel@tonic-gate 	}
33737c478bd9Sstevel@tonic-gate 
33747c478bd9Sstevel@tonic-gate 	return (linkhead);
33757c478bd9Sstevel@tonic-gate }
33767c478bd9Sstevel@tonic-gate 
33777c478bd9Sstevel@tonic-gate static void
33787c478bd9Sstevel@tonic-gate build_devlink_list(char *devlink, void *data)
33797c478bd9Sstevel@tonic-gate {
33807c478bd9Sstevel@tonic-gate 	char *fcn = "build_devlink_list: ";
33817c478bd9Sstevel@tonic-gate 	char *ptr;
33827c478bd9Sstevel@tonic-gate 	char *r_contents;
33837c478bd9Sstevel@tonic-gate 	char *r_devlink;
33847c478bd9Sstevel@tonic-gate 	char contents[PATH_MAX + 1];
33857c478bd9Sstevel@tonic-gate 	char newlink[PATH_MAX + 1];
33867c478bd9Sstevel@tonic-gate 	char stage_link[PATH_MAX + 1];
33877c478bd9Sstevel@tonic-gate 	int linksize;
33887c478bd9Sstevel@tonic-gate 	linkhead_t *linkhead = (linkhead_t *)data;
33897c478bd9Sstevel@tonic-gate 	link_t *link;
33907c478bd9Sstevel@tonic-gate 	int i = 0;
33917c478bd9Sstevel@tonic-gate 
33927c478bd9Sstevel@tonic-gate 	vprint(BUILDCACHE_MID, "%scheck_link: %s\n", fcn, devlink);
33937c478bd9Sstevel@tonic-gate 
33947c478bd9Sstevel@tonic-gate 	(void) strcpy(newlink, devlink);
33957c478bd9Sstevel@tonic-gate 
33967c478bd9Sstevel@tonic-gate 	do {
33977c478bd9Sstevel@tonic-gate 		linksize = readlink(newlink, contents, PATH_MAX);
33987c478bd9Sstevel@tonic-gate 		if (linksize <= 0) {
33997c478bd9Sstevel@tonic-gate 			/*
34007c478bd9Sstevel@tonic-gate 			 * The first pass through the do loop we may readlink()
34017c478bd9Sstevel@tonic-gate 			 * non-symlink files(EINVAL) from false regexec matches.
34027c478bd9Sstevel@tonic-gate 			 * Suppress error messages in those cases or if the link
34037c478bd9Sstevel@tonic-gate 			 * content is the empty string.
34047c478bd9Sstevel@tonic-gate 			 */
34057c478bd9Sstevel@tonic-gate 			if (linksize < 0 && (i || errno != EINVAL))
34067c478bd9Sstevel@tonic-gate 				err_print(READLINK_FAILED, "build_devlink_list",
34077c478bd9Sstevel@tonic-gate 				    newlink, strerror(errno));
34087c478bd9Sstevel@tonic-gate 			return;
34097c478bd9Sstevel@tonic-gate 		}
34107c478bd9Sstevel@tonic-gate 		contents[linksize] = '\0';
34117c478bd9Sstevel@tonic-gate 		i = 1;
34127c478bd9Sstevel@tonic-gate 
34137c478bd9Sstevel@tonic-gate 		if (is_minor_node(contents, &r_contents) == DEVFSADM_FALSE) {
34147c478bd9Sstevel@tonic-gate 			/*
34157c478bd9Sstevel@tonic-gate 			 * assume that link contents is really a pointer to
34167c478bd9Sstevel@tonic-gate 			 * another link, so recurse and read its link contents.
34177c478bd9Sstevel@tonic-gate 			 *
34187c478bd9Sstevel@tonic-gate 			 * some link contents are absolute:
34197c478bd9Sstevel@tonic-gate 			 *	/dev/audio -> /dev/sound/0
34207c478bd9Sstevel@tonic-gate 			 */
34217c478bd9Sstevel@tonic-gate 			if (strncmp(contents, DEV "/",
34220a653502Swroche 			    strlen(DEV) + strlen("/")) != 0) {
34237c478bd9Sstevel@tonic-gate 
34247c478bd9Sstevel@tonic-gate 				if ((ptr = strrchr(newlink, '/')) == NULL) {
34257c478bd9Sstevel@tonic-gate 					vprint(REMOVE_MID, "%s%s -> %s invalid "
34260a653502Swroche 					    "link. missing '/'\n", fcn,
34270a653502Swroche 					    newlink, contents);
34280a653502Swroche 					return;
34297c478bd9Sstevel@tonic-gate 				}
34307c478bd9Sstevel@tonic-gate 				*ptr = '\0';
34317c478bd9Sstevel@tonic-gate 				(void) strcpy(stage_link, newlink);
34327c478bd9Sstevel@tonic-gate 				*ptr = '/';
34337c478bd9Sstevel@tonic-gate 				(void) strcat(stage_link, "/");
34347c478bd9Sstevel@tonic-gate 				(void) strcat(stage_link, contents);
34357c478bd9Sstevel@tonic-gate 				(void) strcpy(newlink, stage_link);
34367c478bd9Sstevel@tonic-gate 			} else {
34377c478bd9Sstevel@tonic-gate 				(void) strcpy(newlink, dev_dir);
34387c478bd9Sstevel@tonic-gate 				(void) strcat(newlink, "/");
34397c478bd9Sstevel@tonic-gate 				(void) strcat(newlink,
34400a653502Swroche 				    &contents[strlen(DEV) + strlen("/")]);
34417c478bd9Sstevel@tonic-gate 			}
34427c478bd9Sstevel@tonic-gate 
34437c478bd9Sstevel@tonic-gate 		} else {
34447c478bd9Sstevel@tonic-gate 			newlink[0] = '\0';
34457c478bd9Sstevel@tonic-gate 		}
34467c478bd9Sstevel@tonic-gate 	} while (newlink[0] != '\0');
34477c478bd9Sstevel@tonic-gate 
34487c478bd9Sstevel@tonic-gate 	if (strncmp(devlink, dev_dir, strlen(dev_dir)) != 0) {
34497c478bd9Sstevel@tonic-gate 		vprint(BUILDCACHE_MID, "%sinvalid link: %s\n", fcn, devlink);
34507c478bd9Sstevel@tonic-gate 		return;
34517c478bd9Sstevel@tonic-gate 	}
34527c478bd9Sstevel@tonic-gate 
34537c478bd9Sstevel@tonic-gate 	r_devlink = devlink + strlen(dev_dir);
34547c478bd9Sstevel@tonic-gate 
34557c478bd9Sstevel@tonic-gate 	if (r_devlink[0] != '/')
34567c478bd9Sstevel@tonic-gate 		return;
34577c478bd9Sstevel@tonic-gate 
34587c478bd9Sstevel@tonic-gate 	link = s_malloc(sizeof (link_t));
34597c478bd9Sstevel@tonic-gate 
34607c478bd9Sstevel@tonic-gate 	/* don't store the '/' after rootdir/dev */
34617c478bd9Sstevel@tonic-gate 	r_devlink += 1;
34627c478bd9Sstevel@tonic-gate 
34637c478bd9Sstevel@tonic-gate 	vprint(BUILDCACHE_MID, "%scaching link: %s\n", fcn, r_devlink);
34647c478bd9Sstevel@tonic-gate 	link->devlink = s_strdup(r_devlink);
34657c478bd9Sstevel@tonic-gate 
34667c478bd9Sstevel@tonic-gate 	link->contents = s_strdup(r_contents);
34677c478bd9Sstevel@tonic-gate 
34687c478bd9Sstevel@tonic-gate 	link->next = linkhead->link;
34697c478bd9Sstevel@tonic-gate 	linkhead->link = link;
34707c478bd9Sstevel@tonic-gate }
34717c478bd9Sstevel@tonic-gate 
34727c478bd9Sstevel@tonic-gate /*
34737c478bd9Sstevel@tonic-gate  * to be consistent, devlink must not begin with / and must be
34747c478bd9Sstevel@tonic-gate  * relative to /dev/, whereas physpath must contain / and be
34757c478bd9Sstevel@tonic-gate  * relative to /devices.
34767c478bd9Sstevel@tonic-gate  */
34777c478bd9Sstevel@tonic-gate static void
34787c478bd9Sstevel@tonic-gate add_link_to_cache(char *devlink, char *physpath)
34797c478bd9Sstevel@tonic-gate {
34807c478bd9Sstevel@tonic-gate 	linkhead_t *linkhead;
34817c478bd9Sstevel@tonic-gate 	link_t *link;
34827c478bd9Sstevel@tonic-gate 	int added = 0;
34837c478bd9Sstevel@tonic-gate 
34847c478bd9Sstevel@tonic-gate 	if (file_mods == FALSE) {
34857c478bd9Sstevel@tonic-gate 		return;
34867c478bd9Sstevel@tonic-gate 	}
34877c478bd9Sstevel@tonic-gate 
34887c478bd9Sstevel@tonic-gate 	vprint(CACHE_MID, "add_link_to_cache: %s -> %s ",
34890a653502Swroche 	    devlink, physpath);
34907c478bd9Sstevel@tonic-gate 
34917c478bd9Sstevel@tonic-gate 	for (linkhead = headlinkhead; linkhead != NULL;
34920a653502Swroche 	    linkhead = linkhead->nexthead) {
34930a653502Swroche 		if (regexec(&(linkhead->dir_re_compiled), devlink, 0, NULL, 0)
34940a653502Swroche 		    == 0) {
34957c478bd9Sstevel@tonic-gate 			added++;
34967c478bd9Sstevel@tonic-gate 			link = s_malloc(sizeof (link_t));
34977c478bd9Sstevel@tonic-gate 			link->devlink = s_strdup(devlink);
34987c478bd9Sstevel@tonic-gate 			link->contents = s_strdup(physpath);
34997c478bd9Sstevel@tonic-gate 			link->next = linkhead->link;
35007c478bd9Sstevel@tonic-gate 			linkhead->link = link;
35017c478bd9Sstevel@tonic-gate 		}
35027c478bd9Sstevel@tonic-gate 	}
35037c478bd9Sstevel@tonic-gate 
35047c478bd9Sstevel@tonic-gate 	vprint(CACHE_MID,
35050a653502Swroche 	    " %d %s\n", added, added == 0 ? "NOT ADDED" : "ADDED");
35067c478bd9Sstevel@tonic-gate }
35077c478bd9Sstevel@tonic-gate 
35087c478bd9Sstevel@tonic-gate /*
35097c478bd9Sstevel@tonic-gate  * Remove devlink from cache.  Devlink must be relative to /dev/ and not start
35107c478bd9Sstevel@tonic-gate  * with /.
35117c478bd9Sstevel@tonic-gate  */
35127c478bd9Sstevel@tonic-gate static void
35137c478bd9Sstevel@tonic-gate rm_link_from_cache(char *devlink)
35147c478bd9Sstevel@tonic-gate {
35157c478bd9Sstevel@tonic-gate 	linkhead_t *linkhead;
35167c478bd9Sstevel@tonic-gate 	link_t **linkp;
35177c478bd9Sstevel@tonic-gate 	link_t *save;
35187c478bd9Sstevel@tonic-gate 
35197c478bd9Sstevel@tonic-gate 	vprint(CACHE_MID, "rm_link_from_cache enter: %s\n", devlink);
35207c478bd9Sstevel@tonic-gate 
35217c478bd9Sstevel@tonic-gate 	for (linkhead = headlinkhead; linkhead != NULL;
35227c478bd9Sstevel@tonic-gate 	    linkhead = linkhead->nexthead) {
35230a653502Swroche 		if (regexec(&(linkhead->dir_re_compiled), devlink, 0, NULL, 0)
35240a653502Swroche 		    == 0) {
35257c478bd9Sstevel@tonic-gate 
35267c478bd9Sstevel@tonic-gate 			for (linkp = &(linkhead->link); *linkp != NULL; ) {
35277c478bd9Sstevel@tonic-gate 				if ((strcmp((*linkp)->devlink, devlink) == 0)) {
35287c478bd9Sstevel@tonic-gate 					save = *linkp;
35297c478bd9Sstevel@tonic-gate 					*linkp = (*linkp)->next;
35307c478bd9Sstevel@tonic-gate 					/*
35317c478bd9Sstevel@tonic-gate 					 * We are removing our caller's
35327c478bd9Sstevel@tonic-gate 					 * "next" link. Update the nextlink
35337c478bd9Sstevel@tonic-gate 					 * field in the head so that our
35347c478bd9Sstevel@tonic-gate 					 * callers accesses the next valid
35357c478bd9Sstevel@tonic-gate 					 * link
35367c478bd9Sstevel@tonic-gate 					 */
35377c478bd9Sstevel@tonic-gate 					if (linkhead->nextlink == save)
35387c478bd9Sstevel@tonic-gate 						linkhead->nextlink = *linkp;
35397c478bd9Sstevel@tonic-gate 					free(save->devlink);
35407c478bd9Sstevel@tonic-gate 					free(save->contents);
35417c478bd9Sstevel@tonic-gate 					free(save);
35427c478bd9Sstevel@tonic-gate 					vprint(CACHE_MID, " %s FREED FROM "
35430a653502Swroche 					    "CACHE\n", devlink);
35447c478bd9Sstevel@tonic-gate 				} else {
35457c478bd9Sstevel@tonic-gate 					linkp = &((*linkp)->next);
35467c478bd9Sstevel@tonic-gate 				}
35477c478bd9Sstevel@tonic-gate 			}
35487c478bd9Sstevel@tonic-gate 		}
35497c478bd9Sstevel@tonic-gate 	}
35507c478bd9Sstevel@tonic-gate }
35517c478bd9Sstevel@tonic-gate 
35527c478bd9Sstevel@tonic-gate static void
35537c478bd9Sstevel@tonic-gate rm_all_links_from_cache()
35547c478bd9Sstevel@tonic-gate {
35557c478bd9Sstevel@tonic-gate 	linkhead_t *linkhead;
35567c478bd9Sstevel@tonic-gate 	linkhead_t *nextlinkhead;
35577c478bd9Sstevel@tonic-gate 	link_t *link;
35587c478bd9Sstevel@tonic-gate 	link_t *nextlink;
35597c478bd9Sstevel@tonic-gate 
35607c478bd9Sstevel@tonic-gate 	vprint(CACHE_MID, "rm_all_links_from_cache\n");
35617c478bd9Sstevel@tonic-gate 
35627c478bd9Sstevel@tonic-gate 	for (linkhead = headlinkhead; linkhead != NULL;
35630a653502Swroche 	    linkhead = nextlinkhead) {
35647c478bd9Sstevel@tonic-gate 
35657c478bd9Sstevel@tonic-gate 		nextlinkhead = linkhead->nexthead;
35667c478bd9Sstevel@tonic-gate 		assert(linkhead->nextlink == NULL);
35677c478bd9Sstevel@tonic-gate 		for (link = linkhead->link; link != NULL; link = nextlink) {
35687c478bd9Sstevel@tonic-gate 			nextlink = link->next;
35697c478bd9Sstevel@tonic-gate 			free(link->devlink);
35707c478bd9Sstevel@tonic-gate 			free(link->contents);
35717c478bd9Sstevel@tonic-gate 			free(link);
35727c478bd9Sstevel@tonic-gate 		}
35737c478bd9Sstevel@tonic-gate 		regfree(&(linkhead->dir_re_compiled));
35747c478bd9Sstevel@tonic-gate 		free(linkhead->dir_re);
35757c478bd9Sstevel@tonic-gate 		free(linkhead);
35767c478bd9Sstevel@tonic-gate 	}
35777c478bd9Sstevel@tonic-gate 	headlinkhead = NULL;
35787c478bd9Sstevel@tonic-gate }
35797c478bd9Sstevel@tonic-gate 
35807c478bd9Sstevel@tonic-gate /*
35817c478bd9Sstevel@tonic-gate  * Called when the kernel has modified the incore path_to_inst data.  This
35827c478bd9Sstevel@tonic-gate  * function will schedule a flush of the data to the filesystem.
35837c478bd9Sstevel@tonic-gate  */
35847c478bd9Sstevel@tonic-gate static void
35857c478bd9Sstevel@tonic-gate devfs_instance_mod(void)
35867c478bd9Sstevel@tonic-gate {
35877c478bd9Sstevel@tonic-gate 	char *fcn = "devfs_instance_mod: ";
35887c478bd9Sstevel@tonic-gate 	vprint(PATH2INST_MID, "%senter\n", fcn);
35897c478bd9Sstevel@tonic-gate 
35907c478bd9Sstevel@tonic-gate 	/* signal instance thread */
35917c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&count_lock);
35927c478bd9Sstevel@tonic-gate 	inst_count++;
35937c478bd9Sstevel@tonic-gate 	(void) cond_signal(&cv);
35947c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&count_lock);
35957c478bd9Sstevel@tonic-gate }
35967c478bd9Sstevel@tonic-gate 
35977c478bd9Sstevel@tonic-gate static void
35987c478bd9Sstevel@tonic-gate instance_flush_thread(void)
35997c478bd9Sstevel@tonic-gate {
36007c478bd9Sstevel@tonic-gate 	int i;
36017c478bd9Sstevel@tonic-gate 	int idle;
36027c478bd9Sstevel@tonic-gate 
36037c478bd9Sstevel@tonic-gate 	for (;;) {
36047c478bd9Sstevel@tonic-gate 
36057c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&count_lock);
36067c478bd9Sstevel@tonic-gate 		while (inst_count == 0) {
36077c478bd9Sstevel@tonic-gate 			(void) cond_wait(&cv, &count_lock);
36087c478bd9Sstevel@tonic-gate 		}
36097c478bd9Sstevel@tonic-gate 		inst_count = 0;
36107c478bd9Sstevel@tonic-gate 
36117c478bd9Sstevel@tonic-gate 		vprint(PATH2INST_MID, "signaled to flush path_to_inst."
36120a653502Swroche 		    " Enter delay loop\n");
36137c478bd9Sstevel@tonic-gate 		/*
36147c478bd9Sstevel@tonic-gate 		 * Wait MAX_IDLE_DELAY seconds after getting the last flush
36157c478bd9Sstevel@tonic-gate 		 * path_to_inst event before invoking a flush, but never wait
36167c478bd9Sstevel@tonic-gate 		 * more than MAX_DELAY seconds after getting the first event.
36177c478bd9Sstevel@tonic-gate 		 */
36187c478bd9Sstevel@tonic-gate 		for (idle = 0, i = 0; i < MAX_DELAY; i++) {
36197c478bd9Sstevel@tonic-gate 
36207c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&count_lock);
36217c478bd9Sstevel@tonic-gate 			(void) sleep(1);
36227c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&count_lock);
36237c478bd9Sstevel@tonic-gate 
36247c478bd9Sstevel@tonic-gate 			/* shorten the delay if we are idle */
36257c478bd9Sstevel@tonic-gate 			if (inst_count == 0) {
36267c478bd9Sstevel@tonic-gate 				idle++;
36277c478bd9Sstevel@tonic-gate 				if (idle > MAX_IDLE_DELAY) {
36287c478bd9Sstevel@tonic-gate 					break;
36297c478bd9Sstevel@tonic-gate 				}
36307c478bd9Sstevel@tonic-gate 			} else {
36317c478bd9Sstevel@tonic-gate 				inst_count = idle = 0;
36327c478bd9Sstevel@tonic-gate 			}
36337c478bd9Sstevel@tonic-gate 		}
36347c478bd9Sstevel@tonic-gate 
36357c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&count_lock);
36367c478bd9Sstevel@tonic-gate 
36377c478bd9Sstevel@tonic-gate 		flush_path_to_inst();
36387c478bd9Sstevel@tonic-gate 	}
36397c478bd9Sstevel@tonic-gate }
36407c478bd9Sstevel@tonic-gate 
36417c478bd9Sstevel@tonic-gate /*
36427c478bd9Sstevel@tonic-gate  * Helper function for flush_path_to_inst() below; this routine calls the
36437c478bd9Sstevel@tonic-gate  * inst_sync syscall to flush the path_to_inst database to the given file.
36447c478bd9Sstevel@tonic-gate  */
36457c478bd9Sstevel@tonic-gate static int
36460a653502Swroche do_inst_sync(char *filename, char *instfilename)
36477c478bd9Sstevel@tonic-gate {
36487c478bd9Sstevel@tonic-gate 	void (*sigsaved)(int);
36490a653502Swroche 	int err = 0, flags = INST_SYNC_IF_REQUIRED;
36500a653502Swroche 	struct stat sb;
36510a653502Swroche 
36520a653502Swroche 	if (stat(instfilename, &sb) == -1 && errno == ENOENT)
36530a653502Swroche 		flags = INST_SYNC_ALWAYS;
36547c478bd9Sstevel@tonic-gate 
36557c478bd9Sstevel@tonic-gate 	vprint(INSTSYNC_MID, "do_inst_sync: about to flush %s\n", filename);
36567c478bd9Sstevel@tonic-gate 	sigsaved = sigset(SIGSYS, SIG_IGN);
36570a653502Swroche 	if (inst_sync(filename, flags) == -1)
36587c478bd9Sstevel@tonic-gate 		err = errno;
36597c478bd9Sstevel@tonic-gate 	(void) sigset(SIGSYS, sigsaved);
36607c478bd9Sstevel@tonic-gate 
36617c478bd9Sstevel@tonic-gate 	switch (err) {
36627c478bd9Sstevel@tonic-gate 	case 0:
36637c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
36647c478bd9Sstevel@tonic-gate 	case EALREADY:	/* no-op, path_to_inst already up to date */
36657c478bd9Sstevel@tonic-gate 		return (EALREADY);
36667c478bd9Sstevel@tonic-gate 	case ENOSYS:
36677c478bd9Sstevel@tonic-gate 		err_print(CANT_LOAD_SYSCALL);
36687c478bd9Sstevel@tonic-gate 		break;
36697c478bd9Sstevel@tonic-gate 	case EPERM:
36707c478bd9Sstevel@tonic-gate 		err_print(SUPER_TO_SYNC);
36717c478bd9Sstevel@tonic-gate 		break;
36727c478bd9Sstevel@tonic-gate 	default:
36737c478bd9Sstevel@tonic-gate 		err_print(INSTSYNC_FAILED, filename, strerror(err));
36747c478bd9Sstevel@tonic-gate 		break;
36757c478bd9Sstevel@tonic-gate 	}
36767c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
36777c478bd9Sstevel@tonic-gate }
36787c478bd9Sstevel@tonic-gate 
36797c478bd9Sstevel@tonic-gate /*
36807c478bd9Sstevel@tonic-gate  * Flush the kernel's path_to_inst database to /etc/path_to_inst.  To do so
36817c478bd9Sstevel@tonic-gate  * safely, the database is flushed to a temporary file, then moved into place.
36827c478bd9Sstevel@tonic-gate  *
36837c478bd9Sstevel@tonic-gate  * The following files are used during this process:
36847c478bd9Sstevel@tonic-gate  * 	/etc/path_to_inst:	The path_to_inst file
36857c478bd9Sstevel@tonic-gate  * 	/etc/path_to_inst.<pid>: Contains data flushed from the kernel
36867c478bd9Sstevel@tonic-gate  * 	/etc/path_to_inst.old:  The backup file
36877c478bd9Sstevel@tonic-gate  * 	/etc/path_to_inst.old.<pid>: Temp file for creating backup
36887c478bd9Sstevel@tonic-gate  *
36897c478bd9Sstevel@tonic-gate  */
36907c478bd9Sstevel@tonic-gate static void
36917c478bd9Sstevel@tonic-gate flush_path_to_inst(void)
36927c478bd9Sstevel@tonic-gate {
36937c478bd9Sstevel@tonic-gate 	char *new_inst_file = NULL;
36947c478bd9Sstevel@tonic-gate 	char *old_inst_file = NULL;
36957c478bd9Sstevel@tonic-gate 	char *old_inst_file_npid = NULL;
36967c478bd9Sstevel@tonic-gate 	FILE *inst_file_fp = NULL;
36977c478bd9Sstevel@tonic-gate 	FILE *old_inst_file_fp = NULL;
36987c478bd9Sstevel@tonic-gate 	struct stat sb;
36997c478bd9Sstevel@tonic-gate 	int err = 0;
37007c478bd9Sstevel@tonic-gate 	int c;
37017c478bd9Sstevel@tonic-gate 	int inst_strlen;
37027c478bd9Sstevel@tonic-gate 
37037c478bd9Sstevel@tonic-gate 	vprint(PATH2INST_MID, "flush_path_to_inst: %s\n",
37047c478bd9Sstevel@tonic-gate 	    (flush_path_to_inst_enable == TRUE) ? "ENABLED" : "DISABLED");
37057c478bd9Sstevel@tonic-gate 
37067c478bd9Sstevel@tonic-gate 	if (flush_path_to_inst_enable == FALSE) {
37077c478bd9Sstevel@tonic-gate 		return;
37087c478bd9Sstevel@tonic-gate 	}
37097c478bd9Sstevel@tonic-gate 
37107c478bd9Sstevel@tonic-gate 	inst_strlen = strlen(inst_file);
37117c478bd9Sstevel@tonic-gate 	new_inst_file = s_malloc(inst_strlen + PID_STR_LEN + 2);
37127c478bd9Sstevel@tonic-gate 	old_inst_file = s_malloc(inst_strlen + PID_STR_LEN + 6);
37137c478bd9Sstevel@tonic-gate 	old_inst_file_npid = s_malloc(inst_strlen +
37147c478bd9Sstevel@tonic-gate 	    sizeof (INSTANCE_FILE_SUFFIX));
37157c478bd9Sstevel@tonic-gate 
37167c478bd9Sstevel@tonic-gate 	(void) snprintf(new_inst_file, inst_strlen + PID_STR_LEN + 2,
37170a653502Swroche 	    "%s.%ld", inst_file, getpid());
37187c478bd9Sstevel@tonic-gate 
37197c478bd9Sstevel@tonic-gate 	if (stat(new_inst_file, &sb) == 0) {
37207c478bd9Sstevel@tonic-gate 		s_unlink(new_inst_file);
37217c478bd9Sstevel@tonic-gate 	}
37227c478bd9Sstevel@tonic-gate 
37230a653502Swroche 	err = do_inst_sync(new_inst_file, inst_file);
37240a653502Swroche 	if (err != DEVFSADM_SUCCESS) {
37257c478bd9Sstevel@tonic-gate 		goto out;
37267c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
37277c478bd9Sstevel@tonic-gate 	}
37287c478bd9Sstevel@tonic-gate 
37297c478bd9Sstevel@tonic-gate 	/*
37307c478bd9Sstevel@tonic-gate 	 * Now we deal with the somewhat tricky updating and renaming
37317c478bd9Sstevel@tonic-gate 	 * of this critical piece of kernel state.
37327c478bd9Sstevel@tonic-gate 	 */
37337c478bd9Sstevel@tonic-gate 
37347c478bd9Sstevel@tonic-gate 	/*
37357c478bd9Sstevel@tonic-gate 	 * Copy the current instance file into a temporary file.
37367c478bd9Sstevel@tonic-gate 	 * Then rename the temporary file into the backup (.old)
37377c478bd9Sstevel@tonic-gate 	 * file and rename the newly flushed kernel data into
37387c478bd9Sstevel@tonic-gate 	 * the instance file.
37397c478bd9Sstevel@tonic-gate 	 * Of course if 'inst_file' doesn't exist, there's much
37407c478bd9Sstevel@tonic-gate 	 * less for us to do .. tee hee.
37417c478bd9Sstevel@tonic-gate 	 */
37427c478bd9Sstevel@tonic-gate 	if ((inst_file_fp = fopen(inst_file, "r")) == NULL) {
37437c478bd9Sstevel@tonic-gate 		/*
37447c478bd9Sstevel@tonic-gate 		 * No such file.  Rename the new onto the old
37457c478bd9Sstevel@tonic-gate 		 */
37467c478bd9Sstevel@tonic-gate 		if ((err = rename(new_inst_file, inst_file)) != 0)
37477c478bd9Sstevel@tonic-gate 			err_print(RENAME_FAILED, inst_file, strerror(errno));
37487c478bd9Sstevel@tonic-gate 		goto out;
37497c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
37507c478bd9Sstevel@tonic-gate 	}
37517c478bd9Sstevel@tonic-gate 
37527c478bd9Sstevel@tonic-gate 	(void) snprintf(old_inst_file, inst_strlen + PID_STR_LEN + 6,
37530a653502Swroche 	    "%s.old.%ld", inst_file, getpid());
37547c478bd9Sstevel@tonic-gate 
37557c478bd9Sstevel@tonic-gate 	if (stat(old_inst_file, &sb) == 0) {
37567c478bd9Sstevel@tonic-gate 		s_unlink(old_inst_file);
37577c478bd9Sstevel@tonic-gate 	}
37587c478bd9Sstevel@tonic-gate 
37597c478bd9Sstevel@tonic-gate 	if ((old_inst_file_fp = fopen(old_inst_file, "w")) == NULL) {
37607c478bd9Sstevel@tonic-gate 		/*
37617c478bd9Sstevel@tonic-gate 		 * Can't open the 'old_inst_file' file for writing.
37627c478bd9Sstevel@tonic-gate 		 * This is somewhat strange given that the syscall
37637c478bd9Sstevel@tonic-gate 		 * just succeeded to write a file out.. hmm.. maybe
37647c478bd9Sstevel@tonic-gate 		 * the fs just filled up or something nasty.
37657c478bd9Sstevel@tonic-gate 		 *
37667c478bd9Sstevel@tonic-gate 		 * Anyway, abort what we've done so far.
37677c478bd9Sstevel@tonic-gate 		 */
37687c478bd9Sstevel@tonic-gate 		err_print(CANT_UPDATE, old_inst_file);
37697c478bd9Sstevel@tonic-gate 		err = DEVFSADM_FAILURE;
37707c478bd9Sstevel@tonic-gate 		goto out;
37717c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
37727c478bd9Sstevel@tonic-gate 	}
37737c478bd9Sstevel@tonic-gate 
37747c478bd9Sstevel@tonic-gate 	/*
37757c478bd9Sstevel@tonic-gate 	 * Copy current instance file into the temporary file
37767c478bd9Sstevel@tonic-gate 	 */
37777c478bd9Sstevel@tonic-gate 	err = 0;
37787c478bd9Sstevel@tonic-gate 	while ((c = getc(inst_file_fp)) != EOF) {
37797c478bd9Sstevel@tonic-gate 		if ((err = putc(c, old_inst_file_fp)) == EOF) {
37807c478bd9Sstevel@tonic-gate 			break;
37817c478bd9Sstevel@tonic-gate 		}
37827c478bd9Sstevel@tonic-gate 	}
37837c478bd9Sstevel@tonic-gate 
37847c478bd9Sstevel@tonic-gate 	if (fclose(old_inst_file_fp) == EOF || err == EOF) {
37857c478bd9Sstevel@tonic-gate 		vprint(INFO_MID, CANT_UPDATE, old_inst_file);
37867c478bd9Sstevel@tonic-gate 		err = DEVFSADM_FAILURE;
37877c478bd9Sstevel@tonic-gate 		goto out;
37887c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
37897c478bd9Sstevel@tonic-gate 	}
37907c478bd9Sstevel@tonic-gate 
37917c478bd9Sstevel@tonic-gate 	/*
37927c478bd9Sstevel@tonic-gate 	 * Set permissions to be the same on the backup as
37937c478bd9Sstevel@tonic-gate 	 * /etc/path_to_inst.
37947c478bd9Sstevel@tonic-gate 	 */
37957c478bd9Sstevel@tonic-gate 	(void) chmod(old_inst_file, 0444);
37967c478bd9Sstevel@tonic-gate 
37977c478bd9Sstevel@tonic-gate 	/*
37987c478bd9Sstevel@tonic-gate 	 * So far, everything we've done is more or less reversible.
37997c478bd9Sstevel@tonic-gate 	 * But now we're going to commit ourselves.
38007c478bd9Sstevel@tonic-gate 	 */
38017c478bd9Sstevel@tonic-gate 
38027c478bd9Sstevel@tonic-gate 	(void) snprintf(old_inst_file_npid,
38037c478bd9Sstevel@tonic-gate 	    inst_strlen + sizeof (INSTANCE_FILE_SUFFIX),
38047c478bd9Sstevel@tonic-gate 	    "%s%s", inst_file, INSTANCE_FILE_SUFFIX);
38057c478bd9Sstevel@tonic-gate 
38067c478bd9Sstevel@tonic-gate 	if ((err = rename(old_inst_file, old_inst_file_npid)) != 0) {
38077c478bd9Sstevel@tonic-gate 		err_print(RENAME_FAILED, old_inst_file_npid,
38080a653502Swroche 		    strerror(errno));
38097c478bd9Sstevel@tonic-gate 	} else if ((err = rename(new_inst_file, inst_file)) != 0) {
38107c478bd9Sstevel@tonic-gate 		err_print(RENAME_FAILED, inst_file, strerror(errno));
38117c478bd9Sstevel@tonic-gate 	}
38127c478bd9Sstevel@tonic-gate 
38137c478bd9Sstevel@tonic-gate out:
38147c478bd9Sstevel@tonic-gate 	if (inst_file_fp != NULL) {
38157c478bd9Sstevel@tonic-gate 		if (fclose(inst_file_fp) == EOF) {
38167c478bd9Sstevel@tonic-gate 			err_print(FCLOSE_FAILED, inst_file, strerror(errno));
38177c478bd9Sstevel@tonic-gate 		}
38187c478bd9Sstevel@tonic-gate 	}
38197c478bd9Sstevel@tonic-gate 
38207c478bd9Sstevel@tonic-gate 	if (stat(new_inst_file, &sb) == 0) {
38217c478bd9Sstevel@tonic-gate 		s_unlink(new_inst_file);
38227c478bd9Sstevel@tonic-gate 	}
38237c478bd9Sstevel@tonic-gate 	free(new_inst_file);
38247c478bd9Sstevel@tonic-gate 
38257c478bd9Sstevel@tonic-gate 	if (stat(old_inst_file, &sb) == 0) {
38267c478bd9Sstevel@tonic-gate 		s_unlink(old_inst_file);
38277c478bd9Sstevel@tonic-gate 	}
38287c478bd9Sstevel@tonic-gate 	free(old_inst_file);
38297c478bd9Sstevel@tonic-gate 
38307c478bd9Sstevel@tonic-gate 	free(old_inst_file_npid);
38317c478bd9Sstevel@tonic-gate 
38327c478bd9Sstevel@tonic-gate 	if (err != 0 && err != EALREADY) {
38337c478bd9Sstevel@tonic-gate 		err_print(FAILED_TO_UPDATE, inst_file);
38347c478bd9Sstevel@tonic-gate 	}
38357c478bd9Sstevel@tonic-gate }
38367c478bd9Sstevel@tonic-gate 
38377c478bd9Sstevel@tonic-gate /*
38387c478bd9Sstevel@tonic-gate  * detach from tty.  For daemon mode.
38397c478bd9Sstevel@tonic-gate  */
38407c478bd9Sstevel@tonic-gate void
38417c478bd9Sstevel@tonic-gate detachfromtty()
38427c478bd9Sstevel@tonic-gate {
38437c478bd9Sstevel@tonic-gate 	(void) setsid();
38447c478bd9Sstevel@tonic-gate 	if (DEVFSADM_DEBUG_ON == TRUE) {
38457c478bd9Sstevel@tonic-gate 		return;
38467c478bd9Sstevel@tonic-gate 	}
38477c478bd9Sstevel@tonic-gate 
38487c478bd9Sstevel@tonic-gate 	(void) close(0);
38497c478bd9Sstevel@tonic-gate 	(void) close(1);
38507c478bd9Sstevel@tonic-gate 	(void) close(2);
38517c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDWR, 0);
38527c478bd9Sstevel@tonic-gate 	(void) dup(0);
38537c478bd9Sstevel@tonic-gate 	(void) dup(0);
38547c478bd9Sstevel@tonic-gate 	openlog(DEVFSADMD, LOG_PID, LOG_DAEMON);
38557c478bd9Sstevel@tonic-gate 	(void) setlogmask(LOG_UPTO(LOG_INFO));
38567c478bd9Sstevel@tonic-gate 	logflag = TRUE;
38577c478bd9Sstevel@tonic-gate }
38587c478bd9Sstevel@tonic-gate 
38597c478bd9Sstevel@tonic-gate /*
38607c478bd9Sstevel@tonic-gate  * Use an advisory lock to synchronize updates to /dev.  If the lock is
38617c478bd9Sstevel@tonic-gate  * held by another process, block in the fcntl() system call until that
38627c478bd9Sstevel@tonic-gate  * process drops the lock or exits.  The lock file itself is
38637c478bd9Sstevel@tonic-gate  * DEV_LOCK_FILE.  The process id of the current and last process owning
38647c478bd9Sstevel@tonic-gate  * the lock is kept in the lock file.  After acquiring the lock, read the
38657c478bd9Sstevel@tonic-gate  * process id and return it.  It is the process ID which last owned the
38667c478bd9Sstevel@tonic-gate  * lock, and will be used to determine if caches need to be flushed.
38677c478bd9Sstevel@tonic-gate  *
38687c478bd9Sstevel@tonic-gate  * NOTE: if the devlink database is held open by the caller, it may
38697c478bd9Sstevel@tonic-gate  * be closed by this routine. This is to enforce the following lock ordering:
38707c478bd9Sstevel@tonic-gate  *	1) /dev lock 2) database open
38717c478bd9Sstevel@tonic-gate  */
38727c478bd9Sstevel@tonic-gate pid_t
38737c478bd9Sstevel@tonic-gate enter_dev_lock()
38747c478bd9Sstevel@tonic-gate {
38757c478bd9Sstevel@tonic-gate 	struct flock lock;
38767c478bd9Sstevel@tonic-gate 	int n;
38777c478bd9Sstevel@tonic-gate 	pid_t pid;
38787c478bd9Sstevel@tonic-gate 	pid_t last_owner_pid;
38797c478bd9Sstevel@tonic-gate 
38807c478bd9Sstevel@tonic-gate 	if (file_mods == FALSE) {
38817c478bd9Sstevel@tonic-gate 		return (0);
38827c478bd9Sstevel@tonic-gate 	}
38837c478bd9Sstevel@tonic-gate 
38847c478bd9Sstevel@tonic-gate 	(void) snprintf(dev_lockfile, sizeof (dev_lockfile),
3885facf4a8dSllai 	    "%s/%s", etc_dev_dir, DEV_LOCK_FILE);
38867c478bd9Sstevel@tonic-gate 
38877c478bd9Sstevel@tonic-gate 	vprint(LOCK_MID, "enter_dev_lock: lock file %s\n", dev_lockfile);
38887c478bd9Sstevel@tonic-gate 
38897c478bd9Sstevel@tonic-gate 	dev_lock_fd = open(dev_lockfile, O_CREAT|O_RDWR, 0644);
38907c478bd9Sstevel@tonic-gate 	if (dev_lock_fd < 0) {
38917c478bd9Sstevel@tonic-gate 		err_print(OPEN_FAILED, dev_lockfile, strerror(errno));
38927c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
3893537714daSvikram 		/*NOTREACHED*/
38947c478bd9Sstevel@tonic-gate 	}
38957c478bd9Sstevel@tonic-gate 
38967c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
38977c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
38987c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
38997c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate 	/* try for the lock, but don't wait */
39027c478bd9Sstevel@tonic-gate 	if (fcntl(dev_lock_fd, F_SETLK, &lock) == -1) {
39037c478bd9Sstevel@tonic-gate 		if ((errno == EACCES) || (errno == EAGAIN)) {
39047c478bd9Sstevel@tonic-gate 			pid = 0;
39057c478bd9Sstevel@tonic-gate 			n = read(dev_lock_fd, &pid, sizeof (pid_t));
39067c478bd9Sstevel@tonic-gate 			vprint(LOCK_MID, "waiting for PID %d to complete\n",
39070a653502Swroche 			    (int)pid);
39087c478bd9Sstevel@tonic-gate 			if (lseek(dev_lock_fd, 0, SEEK_SET) == (off_t)-1) {
39097c478bd9Sstevel@tonic-gate 				err_print(LSEEK_FAILED, dev_lockfile,
39100a653502Swroche 				    strerror(errno));
39117c478bd9Sstevel@tonic-gate 				devfsadm_exit(1);
3912537714daSvikram 				/*NOTREACHED*/
39137c478bd9Sstevel@tonic-gate 			}
39147c478bd9Sstevel@tonic-gate 			/*
39157c478bd9Sstevel@tonic-gate 			 * wait for the dev lock. If we have the database open,
39167c478bd9Sstevel@tonic-gate 			 * close it first - the order of lock acquisition should
39177c478bd9Sstevel@tonic-gate 			 * always be:  1) dev_lock 2) database
39187c478bd9Sstevel@tonic-gate 			 * This is to prevent deadlocks with any locks the
39197c478bd9Sstevel@tonic-gate 			 * database code may hold.
39207c478bd9Sstevel@tonic-gate 			 */
39217c478bd9Sstevel@tonic-gate 			(void) di_devlink_close(&devlink_cache, 0);
3922f05faa4eSjacobs 
3923f05faa4eSjacobs 			/* send any sysevents that were queued up. */
3924f05faa4eSjacobs 			process_syseventq();
3925f05faa4eSjacobs 
39267c478bd9Sstevel@tonic-gate 			if (fcntl(dev_lock_fd, F_SETLKW, &lock) == -1) {
39277c478bd9Sstevel@tonic-gate 				err_print(LOCK_FAILED, dev_lockfile,
39280a653502Swroche 				    strerror(errno));
39297c478bd9Sstevel@tonic-gate 				devfsadm_exit(1);
3930537714daSvikram 				/*NOTREACHED*/
39317c478bd9Sstevel@tonic-gate 			}
39327c478bd9Sstevel@tonic-gate 		}
39337c478bd9Sstevel@tonic-gate 	}
39347c478bd9Sstevel@tonic-gate 
39357c478bd9Sstevel@tonic-gate 	hold_dev_lock = TRUE;
39367c478bd9Sstevel@tonic-gate 	pid = 0;
39377c478bd9Sstevel@tonic-gate 	n = read(dev_lock_fd, &pid, sizeof (pid_t));
39387c478bd9Sstevel@tonic-gate 	if (n == sizeof (pid_t) && pid == getpid()) {
39397c478bd9Sstevel@tonic-gate 		return (pid);
39407c478bd9Sstevel@tonic-gate 	}
39417c478bd9Sstevel@tonic-gate 
39427c478bd9Sstevel@tonic-gate 	last_owner_pid = pid;
39437c478bd9Sstevel@tonic-gate 
39447c478bd9Sstevel@tonic-gate 	if (lseek(dev_lock_fd, 0, SEEK_SET) == (off_t)-1) {
39457c478bd9Sstevel@tonic-gate 		err_print(LSEEK_FAILED, dev_lockfile, strerror(errno));
39467c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
3947537714daSvikram 		/*NOTREACHED*/
39487c478bd9Sstevel@tonic-gate 	}
39497c478bd9Sstevel@tonic-gate 	pid = getpid();
39507c478bd9Sstevel@tonic-gate 	n = write(dev_lock_fd, &pid, sizeof (pid_t));
39517c478bd9Sstevel@tonic-gate 	if (n != sizeof (pid_t)) {
39527c478bd9Sstevel@tonic-gate 		err_print(WRITE_FAILED, dev_lockfile, strerror(errno));
39537c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
3954537714daSvikram 		/*NOTREACHED*/
39557c478bd9Sstevel@tonic-gate 	}
39567c478bd9Sstevel@tonic-gate 
39577c478bd9Sstevel@tonic-gate 	return (last_owner_pid);
39587c478bd9Sstevel@tonic-gate }
39597c478bd9Sstevel@tonic-gate 
39607c478bd9Sstevel@tonic-gate /*
39617c478bd9Sstevel@tonic-gate  * Drop the advisory /dev lock, close lock file.  Close and re-open the
39627c478bd9Sstevel@tonic-gate  * file every time so to ensure a resync if for some reason the lock file
39637c478bd9Sstevel@tonic-gate  * gets removed.
39647c478bd9Sstevel@tonic-gate  */
39657c478bd9Sstevel@tonic-gate void
3966537714daSvikram exit_dev_lock(int exiting)
39677c478bd9Sstevel@tonic-gate {
39687c478bd9Sstevel@tonic-gate 	struct flock unlock;
39697c478bd9Sstevel@tonic-gate 
39707c478bd9Sstevel@tonic-gate 	if (hold_dev_lock == FALSE) {
39717c478bd9Sstevel@tonic-gate 		return;
39727c478bd9Sstevel@tonic-gate 	}
39737c478bd9Sstevel@tonic-gate 
3974537714daSvikram 	vprint(LOCK_MID, "exit_dev_lock: lock file %s, exiting = %d\n",
3975537714daSvikram 	    dev_lockfile, exiting);
39767c478bd9Sstevel@tonic-gate 
39777c478bd9Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
39787c478bd9Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
39797c478bd9Sstevel@tonic-gate 	unlock.l_start = 0;
39807c478bd9Sstevel@tonic-gate 	unlock.l_len = 0;
39817c478bd9Sstevel@tonic-gate 
39827c478bd9Sstevel@tonic-gate 	if (fcntl(dev_lock_fd, F_SETLK, &unlock) == -1) {
39837c478bd9Sstevel@tonic-gate 		err_print(UNLOCK_FAILED, dev_lockfile, strerror(errno));
39847c478bd9Sstevel@tonic-gate 	}
39857c478bd9Sstevel@tonic-gate 
39867c478bd9Sstevel@tonic-gate 	hold_dev_lock = FALSE;
39877c478bd9Sstevel@tonic-gate 
39887c478bd9Sstevel@tonic-gate 	if (close(dev_lock_fd) == -1) {
39897c478bd9Sstevel@tonic-gate 		err_print(CLOSE_FAILED, dev_lockfile, strerror(errno));
3990537714daSvikram 		if (!exiting)
3991537714daSvikram 			devfsadm_exit(1);
3992537714daSvikram 			/*NOTREACHED*/
39937c478bd9Sstevel@tonic-gate 	}
39947c478bd9Sstevel@tonic-gate }
39957c478bd9Sstevel@tonic-gate 
39967c478bd9Sstevel@tonic-gate /*
39977c478bd9Sstevel@tonic-gate  *
39987c478bd9Sstevel@tonic-gate  * Use an advisory lock to ensure that only one daemon process is active
39997c478bd9Sstevel@tonic-gate  * in the system at any point in time.	If the lock is held by another
40007c478bd9Sstevel@tonic-gate  * process, do not block but return the pid owner of the lock to the
40017c478bd9Sstevel@tonic-gate  * caller immediately.	The lock is cleared if the holding daemon process
40027c478bd9Sstevel@tonic-gate  * exits for any reason even if the lock file remains, so the daemon can
40037c478bd9Sstevel@tonic-gate  * be restarted if necessary.  The lock file is DAEMON_LOCK_FILE.
40047c478bd9Sstevel@tonic-gate  */
40057c478bd9Sstevel@tonic-gate pid_t
40067c478bd9Sstevel@tonic-gate enter_daemon_lock(void)
40077c478bd9Sstevel@tonic-gate {
40087c478bd9Sstevel@tonic-gate 	struct flock lock;
40097c478bd9Sstevel@tonic-gate 
40107c478bd9Sstevel@tonic-gate 	(void) snprintf(daemon_lockfile, sizeof (daemon_lockfile),
4011facf4a8dSllai 	    "%s/%s", etc_dev_dir, DAEMON_LOCK_FILE);
40127c478bd9Sstevel@tonic-gate 
40137c478bd9Sstevel@tonic-gate 	vprint(LOCK_MID, "enter_daemon_lock: lock file %s\n", daemon_lockfile);
40147c478bd9Sstevel@tonic-gate 
40157c478bd9Sstevel@tonic-gate 	daemon_lock_fd = open(daemon_lockfile, O_CREAT|O_RDWR, 0644);
40167c478bd9Sstevel@tonic-gate 	if (daemon_lock_fd < 0) {
40177c478bd9Sstevel@tonic-gate 		err_print(OPEN_FAILED, daemon_lockfile, strerror(errno));
40187c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
4019537714daSvikram 		/*NOTREACHED*/
40207c478bd9Sstevel@tonic-gate 	}
40217c478bd9Sstevel@tonic-gate 
40227c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
40237c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
40247c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
40257c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
40267c478bd9Sstevel@tonic-gate 
40277c478bd9Sstevel@tonic-gate 	if (fcntl(daemon_lock_fd, F_SETLK, &lock) == -1) {
40287c478bd9Sstevel@tonic-gate 
40297c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN || errno == EDEADLK) {
40307c478bd9Sstevel@tonic-gate 			if (fcntl(daemon_lock_fd, F_GETLK, &lock) == -1) {
40317c478bd9Sstevel@tonic-gate 				err_print(LOCK_FAILED, daemon_lockfile,
40320a653502Swroche 				    strerror(errno));
40337c478bd9Sstevel@tonic-gate 				devfsadm_exit(1);
4034537714daSvikram 				/*NOTREACHED*/
40357c478bd9Sstevel@tonic-gate 			}
40367c478bd9Sstevel@tonic-gate 			return (lock.l_pid);
40377c478bd9Sstevel@tonic-gate 		}
40387c478bd9Sstevel@tonic-gate 	}
40397c478bd9Sstevel@tonic-gate 	hold_daemon_lock = TRUE;
40407c478bd9Sstevel@tonic-gate 	return (getpid());
40417c478bd9Sstevel@tonic-gate }
40427c478bd9Sstevel@tonic-gate 
40437c478bd9Sstevel@tonic-gate /*
40447c478bd9Sstevel@tonic-gate  * Drop the advisory daemon lock, close lock file
40457c478bd9Sstevel@tonic-gate  */
40467c478bd9Sstevel@tonic-gate void
4047537714daSvikram exit_daemon_lock(int exiting)
40487c478bd9Sstevel@tonic-gate {
40497c478bd9Sstevel@tonic-gate 	struct flock lock;
40507c478bd9Sstevel@tonic-gate 
40517c478bd9Sstevel@tonic-gate 	if (hold_daemon_lock == FALSE) {
40527c478bd9Sstevel@tonic-gate 		return;
40537c478bd9Sstevel@tonic-gate 	}
40547c478bd9Sstevel@tonic-gate 
4055537714daSvikram 	vprint(LOCK_MID, "exit_daemon_lock: lock file %s, exiting = %d\n",
4056537714daSvikram 	    daemon_lockfile, exiting);
40577c478bd9Sstevel@tonic-gate 
40587c478bd9Sstevel@tonic-gate 	lock.l_type = F_UNLCK;
40597c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
40607c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
40617c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
40627c478bd9Sstevel@tonic-gate 
40637c478bd9Sstevel@tonic-gate 	if (fcntl(daemon_lock_fd, F_SETLK, &lock) == -1) {
40647c478bd9Sstevel@tonic-gate 		err_print(UNLOCK_FAILED, daemon_lockfile, strerror(errno));
40657c478bd9Sstevel@tonic-gate 	}
40667c478bd9Sstevel@tonic-gate 
40677c478bd9Sstevel@tonic-gate 	if (close(daemon_lock_fd) == -1) {
40687c478bd9Sstevel@tonic-gate 		err_print(CLOSE_FAILED, daemon_lockfile, strerror(errno));
4069537714daSvikram 		if (!exiting)
4070537714daSvikram 			devfsadm_exit(1);
4071537714daSvikram 			/*NOTREACHED*/
40727c478bd9Sstevel@tonic-gate 	}
40737c478bd9Sstevel@tonic-gate }
40747c478bd9Sstevel@tonic-gate 
40757c478bd9Sstevel@tonic-gate /*
40767c478bd9Sstevel@tonic-gate  * Called to removed danging nodes in two different modes: RM_PRE, RM_POST.
40777c478bd9Sstevel@tonic-gate  * RM_PRE mode is called before processing the entire devinfo tree, and RM_POST
40787c478bd9Sstevel@tonic-gate  * is called after processing the entire devinfo tree.
40797c478bd9Sstevel@tonic-gate  */
40807c478bd9Sstevel@tonic-gate static void
40817c478bd9Sstevel@tonic-gate pre_and_post_cleanup(int flags)
40827c478bd9Sstevel@tonic-gate {
40837c478bd9Sstevel@tonic-gate 	remove_list_t *rm;
40847c478bd9Sstevel@tonic-gate 	recurse_dev_t rd;
40857c478bd9Sstevel@tonic-gate 	cleanup_data_t cleanup_data;
40867c478bd9Sstevel@tonic-gate 	char *fcn = "pre_and_post_cleanup: ";
40877c478bd9Sstevel@tonic-gate 
40887c478bd9Sstevel@tonic-gate 	if (build_dev == FALSE)
40897c478bd9Sstevel@tonic-gate 		return;
40907c478bd9Sstevel@tonic-gate 
40917c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "attempting %s-cleanup\n",
40927c478bd9Sstevel@tonic-gate 	    flags == RM_PRE ? "pre" : "post");
40937c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%sflags = %d\n", fcn, flags);
40947c478bd9Sstevel@tonic-gate 
40957c478bd9Sstevel@tonic-gate 	/*
40967c478bd9Sstevel@tonic-gate 	 * the generic function recurse_dev_re is shared among different
40977c478bd9Sstevel@tonic-gate 	 * functions, so set the method and data that it should use for
40987c478bd9Sstevel@tonic-gate 	 * matches.
40997c478bd9Sstevel@tonic-gate 	 */
41007c478bd9Sstevel@tonic-gate 	rd.fcn = matching_dev;
41017c478bd9Sstevel@tonic-gate 	rd.data = (void *)&cleanup_data;
41027c478bd9Sstevel@tonic-gate 	cleanup_data.flags = flags;
41037c478bd9Sstevel@tonic-gate 
4104aa646b9dSvikram 	(void) mutex_lock(&nfp_mutex);
4105aa646b9dSvikram 	nfphash_create();
4106aa646b9dSvikram 
41077c478bd9Sstevel@tonic-gate 	for (rm = remove_head; rm != NULL; rm = rm->next) {
41087c478bd9Sstevel@tonic-gate 		if ((flags & rm->remove->flags) == flags) {
41097c478bd9Sstevel@tonic-gate 			cleanup_data.rm = rm;
41107c478bd9Sstevel@tonic-gate 			/*
41117c478bd9Sstevel@tonic-gate 			 * If reached this point, RM_PRE or RM_POST cleanup is
41127c478bd9Sstevel@tonic-gate 			 * desired.  clean_ok() decides whether to clean
41137c478bd9Sstevel@tonic-gate 			 * under the given circumstances.
41147c478bd9Sstevel@tonic-gate 			 */
41157c478bd9Sstevel@tonic-gate 			vprint(REMOVE_MID, "%scleanup: PRE or POST\n", fcn);
41167c478bd9Sstevel@tonic-gate 			if (clean_ok(rm->remove) == DEVFSADM_SUCCESS) {
41177c478bd9Sstevel@tonic-gate 				vprint(REMOVE_MID, "cleanup: cleanup OK\n");
41180a653502Swroche 				recurse_dev_re(dev_dir,
41190a653502Swroche 				    rm->remove->dev_dirs_re, &rd);
41207c478bd9Sstevel@tonic-gate 			}
41217c478bd9Sstevel@tonic-gate 		}
41227c478bd9Sstevel@tonic-gate 	}
4123aa646b9dSvikram 	nfphash_destroy();
4124aa646b9dSvikram 	(void) mutex_unlock(&nfp_mutex);
41257c478bd9Sstevel@tonic-gate }
41267c478bd9Sstevel@tonic-gate 
41277c478bd9Sstevel@tonic-gate /*
41287c478bd9Sstevel@tonic-gate  * clean_ok() determines whether cleanup should be done according
41297c478bd9Sstevel@tonic-gate  * to the following matrix:
41307c478bd9Sstevel@tonic-gate  *
41317c478bd9Sstevel@tonic-gate  * command line arguments RM_PRE    RM_POST	  RM_PRE &&    RM_POST &&
41327c478bd9Sstevel@tonic-gate  *						  RM_ALWAYS    RM_ALWAYS
41337c478bd9Sstevel@tonic-gate  * ---------------------- ------     -----	  ---------    ----------
41347c478bd9Sstevel@tonic-gate  *
41357c478bd9Sstevel@tonic-gate  * <neither -c nor -C>	  -	    -		  pre-clean    post-clean
41367c478bd9Sstevel@tonic-gate  *
41377c478bd9Sstevel@tonic-gate  * -C			  pre-clean  post-clean   pre-clean    post-clean
41387c478bd9Sstevel@tonic-gate  *
41397c478bd9Sstevel@tonic-gate  * -C -c class		  pre-clean  post-clean   pre-clean    post-clean
41407c478bd9Sstevel@tonic-gate  *			  if class  if class	  if class     if class
41417c478bd9Sstevel@tonic-gate  *			  matches   matches	  matches      matches
41427c478bd9Sstevel@tonic-gate  *
41437c478bd9Sstevel@tonic-gate  * -c class		   -	       -	  pre-clean    post-clean
41447c478bd9Sstevel@tonic-gate  *						  if class     if class
41457c478bd9Sstevel@tonic-gate  *						  matches      matches
41467c478bd9Sstevel@tonic-gate  *
41477c478bd9Sstevel@tonic-gate  */
41487c478bd9Sstevel@tonic-gate static int
4149aa646b9dSvikram clean_ok(devfsadm_remove_V1_t *remove)
41507c478bd9Sstevel@tonic-gate {
41517c478bd9Sstevel@tonic-gate 	int i;
41527c478bd9Sstevel@tonic-gate 
41537c478bd9Sstevel@tonic-gate 	if (single_drv == TRUE) {
41547c478bd9Sstevel@tonic-gate 		/* no cleanup at all when using -i option */
41557c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
41567c478bd9Sstevel@tonic-gate 	}
41577c478bd9Sstevel@tonic-gate 
41587c478bd9Sstevel@tonic-gate 	/*
41597c478bd9Sstevel@tonic-gate 	 * no cleanup if drivers are not loaded. We make an exception
41607c478bd9Sstevel@tonic-gate 	 * for the "disks" program however, since disks has a public
41617c478bd9Sstevel@tonic-gate 	 * cleanup flag (-C) and disk drivers are usually never
41627c478bd9Sstevel@tonic-gate 	 * unloaded.
41637c478bd9Sstevel@tonic-gate 	 */
41647c478bd9Sstevel@tonic-gate 	if (load_attach_drv == FALSE && strcmp(prog, DISKS) != 0) {
41657c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
41667c478bd9Sstevel@tonic-gate 	}
41677c478bd9Sstevel@tonic-gate 
41687c478bd9Sstevel@tonic-gate 	/* if the cleanup flag was not specified, return false */
41697c478bd9Sstevel@tonic-gate 	if ((cleanup == FALSE) && ((remove->flags & RM_ALWAYS) == 0)) {
41707c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
41717c478bd9Sstevel@tonic-gate 	}
41727c478bd9Sstevel@tonic-gate 
41737c478bd9Sstevel@tonic-gate 	if (num_classes == 0) {
41747c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
41757c478bd9Sstevel@tonic-gate 	}
41767c478bd9Sstevel@tonic-gate 
41777c478bd9Sstevel@tonic-gate 	/*
41787c478bd9Sstevel@tonic-gate 	 * if reached this point, check to see if the class in the given
41797c478bd9Sstevel@tonic-gate 	 * remove structure matches a class given on the command line
41807c478bd9Sstevel@tonic-gate 	 */
41817c478bd9Sstevel@tonic-gate 
41827c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_classes; i++) {
41837c478bd9Sstevel@tonic-gate 		if (strcmp(remove->device_class, classes[i]) == 0) {
41847c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
41857c478bd9Sstevel@tonic-gate 		}
41867c478bd9Sstevel@tonic-gate 	}
41877c478bd9Sstevel@tonic-gate 
41887c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
41897c478bd9Sstevel@tonic-gate }
41907c478bd9Sstevel@tonic-gate 
41917c478bd9Sstevel@tonic-gate /*
41927c478bd9Sstevel@tonic-gate  * Called to remove dangling nodes after receiving a hotplug event
41937c478bd9Sstevel@tonic-gate  * containing the physical node pathname to be removed.
41947c478bd9Sstevel@tonic-gate  */
41957c478bd9Sstevel@tonic-gate void
41967c478bd9Sstevel@tonic-gate hot_cleanup(char *node_path, char *minor_name, char *ev_subclass,
41977c478bd9Sstevel@tonic-gate     char *driver_name, int instance)
41987c478bd9Sstevel@tonic-gate {
41997c478bd9Sstevel@tonic-gate 	link_t *link;
42007c478bd9Sstevel@tonic-gate 	linkhead_t *head;
42017c478bd9Sstevel@tonic-gate 	remove_list_t *rm;
42027c478bd9Sstevel@tonic-gate 	char *fcn = "hot_cleanup: ";
42037c478bd9Sstevel@tonic-gate 	char path[PATH_MAX + 1];
42047c478bd9Sstevel@tonic-gate 	int path_len;
42057c478bd9Sstevel@tonic-gate 	char rmlink[PATH_MAX + 1];
42067c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
4207fb9251a6Scth 	int skip;
4208aa646b9dSvikram 	int ret;
42097c478bd9Sstevel@tonic-gate 
42107c478bd9Sstevel@tonic-gate 	/*
42117c478bd9Sstevel@tonic-gate 	 * dev links can go away as part of hot cleanup.
42127c478bd9Sstevel@tonic-gate 	 * So first build event attributes in order capture dev links.
42137c478bd9Sstevel@tonic-gate 	 */
42147c478bd9Sstevel@tonic-gate 	if (ev_subclass != NULL)
42157c478bd9Sstevel@tonic-gate 		nvl = build_event_attributes(EC_DEV_REMOVE, ev_subclass,
421630294554Sphitran 		    node_path, DI_NODE_NIL, driver_name, instance, minor_name);
42177c478bd9Sstevel@tonic-gate 
42187c478bd9Sstevel@tonic-gate 	(void) strcpy(path, node_path);
42197c478bd9Sstevel@tonic-gate 	(void) strcat(path, ":");
42207c478bd9Sstevel@tonic-gate 	(void) strcat(path, minor_name == NULL ? "" : minor_name);
42217c478bd9Sstevel@tonic-gate 
42227c478bd9Sstevel@tonic-gate 	path_len = strlen(path);
42237c478bd9Sstevel@tonic-gate 
42247c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%spath=%s\n", fcn, path);
42257c478bd9Sstevel@tonic-gate 
4226aa646b9dSvikram 	(void) mutex_lock(&nfp_mutex);
4227aa646b9dSvikram 	nfphash_create();
4228aa646b9dSvikram 
42297c478bd9Sstevel@tonic-gate 	for (rm = remove_head; rm != NULL; rm = rm->next) {
42307c478bd9Sstevel@tonic-gate 		if ((RM_HOT & rm->remove->flags) == RM_HOT) {
42317c478bd9Sstevel@tonic-gate 			head = get_cached_links(rm->remove->dev_dirs_re);
42327c478bd9Sstevel@tonic-gate 			assert(head->nextlink == NULL);
42337c478bd9Sstevel@tonic-gate 			for (link = head->link;
42347c478bd9Sstevel@tonic-gate 			    link != NULL; link = head->nextlink) {
42357c478bd9Sstevel@tonic-gate 				/*
42367c478bd9Sstevel@tonic-gate 				 * The remove callback below may remove
42377c478bd9Sstevel@tonic-gate 				 * the current and/or any or all of the
42387c478bd9Sstevel@tonic-gate 				 * subsequent links in the list.
42397c478bd9Sstevel@tonic-gate 				 * Save the next link in the head. If
42407c478bd9Sstevel@tonic-gate 				 * the callback removes the next link
42417c478bd9Sstevel@tonic-gate 				 * the saved pointer in the head will be
42427c478bd9Sstevel@tonic-gate 				 * updated by the callback to point at
42437c478bd9Sstevel@tonic-gate 				 * the next valid link.
42447c478bd9Sstevel@tonic-gate 				 */
42457c478bd9Sstevel@tonic-gate 				head->nextlink = link->next;
4246aa646b9dSvikram 
4247aa646b9dSvikram 				/*
4248aa646b9dSvikram 				 * if devlink is in no-further-process hash,
4249aa646b9dSvikram 				 * skip its remove
4250aa646b9dSvikram 				 */
4251aa646b9dSvikram 				if (nfphash_lookup(link->devlink) != NULL)
4252aa646b9dSvikram 					continue;
4253aa646b9dSvikram 
4254fb9251a6Scth 				if (minor_name)
4255fb9251a6Scth 					skip = strcmp(link->contents, path);
4256fb9251a6Scth 				else
4257fb9251a6Scth 					skip = strncmp(link->contents, path,
4258fb9251a6Scth 					    path_len);
4259fb9251a6Scth 				if (skip ||
4260fb9251a6Scth 				    (call_minor_init(rm->modptr) ==
4261fb9251a6Scth 				    DEVFSADM_FAILURE))
4262fb9251a6Scth 					continue;
42637c478bd9Sstevel@tonic-gate 
4264fb9251a6Scth 				vprint(REMOVE_MID,
42650a653502Swroche 				    "%sremoving %s -> %s\n", fcn,
42660a653502Swroche 				    link->devlink, link->contents);
4267fb9251a6Scth 				/*
4268fb9251a6Scth 				 * Use a copy of the cached link name
4269fb9251a6Scth 				 * as the cache entry will go away
4270fb9251a6Scth 				 * during link removal
4271fb9251a6Scth 				 */
4272fb9251a6Scth 				(void) snprintf(rmlink, sizeof (rmlink),
4273fb9251a6Scth 				    "%s", link->devlink);
4274aa646b9dSvikram 				if (rm->remove->flags & RM_NOINTERPOSE) {
4275aa646b9dSvikram 					((void (*)(char *))
42760a653502Swroche 					    (rm->remove->callback_fcn))(rmlink);
4277aa646b9dSvikram 				} else {
4278aa646b9dSvikram 					ret = ((int (*)(char *))
4279aa646b9dSvikram 					    (rm->remove->callback_fcn))(rmlink);
4280aa646b9dSvikram 					if (ret == DEVFSADM_TERMINATE)
4281aa646b9dSvikram 						nfphash_insert(rmlink);
4282aa646b9dSvikram 				}
42837c478bd9Sstevel@tonic-gate 			}
42847c478bd9Sstevel@tonic-gate 		}
42857c478bd9Sstevel@tonic-gate 	}
42867c478bd9Sstevel@tonic-gate 
4287aa646b9dSvikram 	nfphash_destroy();
4288aa646b9dSvikram 	(void) mutex_unlock(&nfp_mutex);
4289aa646b9dSvikram 
429045916cd2Sjpk 	/* update device allocation database */
429145916cd2Sjpk 	if (system_labeled) {
429245916cd2Sjpk 		int	ret = 0;
429345916cd2Sjpk 		int	devtype = 0;
429445916cd2Sjpk 		char	devname[MAXNAMELEN];
429545916cd2Sjpk 
429645916cd2Sjpk 		devname[0] = '\0';
429745916cd2Sjpk 		if (strstr(node_path, DA_SOUND_NAME))
429845916cd2Sjpk 			devtype = DA_AUDIO;
429945916cd2Sjpk 		else if (strstr(node_path, "disk"))
430045916cd2Sjpk 			devtype = DA_RMDISK;
430145916cd2Sjpk 		else
430245916cd2Sjpk 			goto out;
430345916cd2Sjpk 		ret = da_remove_list(&devlist, NULL, devtype, devname,
430445916cd2Sjpk 		    sizeof (devname));
430545916cd2Sjpk 		if (ret != -1)
430645916cd2Sjpk 			(void) _update_devalloc_db(&devlist, devtype, DA_REMOVE,
430745916cd2Sjpk 			    devname, root_dir);
430845916cd2Sjpk 	}
430945916cd2Sjpk 
431045916cd2Sjpk out:
43117c478bd9Sstevel@tonic-gate 	/* now log an event */
43127c478bd9Sstevel@tonic-gate 	if (nvl) {
43137c478bd9Sstevel@tonic-gate 		log_event(EC_DEV_REMOVE, ev_subclass, nvl);
43147c478bd9Sstevel@tonic-gate 		free(nvl);
43157c478bd9Sstevel@tonic-gate 	}
43167c478bd9Sstevel@tonic-gate }
43177c478bd9Sstevel@tonic-gate 
43187c478bd9Sstevel@tonic-gate /*
43197c478bd9Sstevel@tonic-gate  * Open the dir current_dir.  For every file which matches the first dir
43207c478bd9Sstevel@tonic-gate  * component of path_re, recurse.  If there are no more *dir* path
43217c478bd9Sstevel@tonic-gate  * components left in path_re (ie no more /), then call function rd->fcn.
43227c478bd9Sstevel@tonic-gate  */
43237c478bd9Sstevel@tonic-gate static void
43247c478bd9Sstevel@tonic-gate recurse_dev_re(char *current_dir, char *path_re, recurse_dev_t *rd)
43257c478bd9Sstevel@tonic-gate {
43267c478bd9Sstevel@tonic-gate 	regex_t re1;
43277c478bd9Sstevel@tonic-gate 	char *slash;
43287c478bd9Sstevel@tonic-gate 	char new_path[PATH_MAX + 1];
43297c478bd9Sstevel@tonic-gate 	char *anchored_path_re;
43307c478bd9Sstevel@tonic-gate 	size_t len;
433173de625bSjg 	finddevhdl_t fhandle;
433273de625bSjg 	const char *fp;
43337c478bd9Sstevel@tonic-gate 
43347c478bd9Sstevel@tonic-gate 	vprint(RECURSEDEV_MID, "recurse_dev_re: curr = %s path=%s\n",
43350a653502Swroche 	    current_dir, path_re);
43367c478bd9Sstevel@tonic-gate 
433773de625bSjg 	if (finddev_readdir(current_dir, &fhandle) != 0)
43387c478bd9Sstevel@tonic-gate 		return;
43397c478bd9Sstevel@tonic-gate 
43407c478bd9Sstevel@tonic-gate 	len = strlen(path_re);
43417c478bd9Sstevel@tonic-gate 	if ((slash = strchr(path_re, '/')) != NULL) {
43427c478bd9Sstevel@tonic-gate 		len = (slash - path_re);
43437c478bd9Sstevel@tonic-gate 	}
43447c478bd9Sstevel@tonic-gate 
43457c478bd9Sstevel@tonic-gate 	anchored_path_re = s_malloc(len + 3);
43467c478bd9Sstevel@tonic-gate 	(void) sprintf(anchored_path_re, "^%.*s$", len, path_re);
43477c478bd9Sstevel@tonic-gate 
43487c478bd9Sstevel@tonic-gate 	if (regcomp(&re1, anchored_path_re, REG_EXTENDED) != 0) {
43497c478bd9Sstevel@tonic-gate 		free(anchored_path_re);
43507c478bd9Sstevel@tonic-gate 		goto out;
43517c478bd9Sstevel@tonic-gate 	}
43527c478bd9Sstevel@tonic-gate 
43537c478bd9Sstevel@tonic-gate 	free(anchored_path_re);
43547c478bd9Sstevel@tonic-gate 
435573de625bSjg 	while ((fp = finddev_next(fhandle)) != NULL) {
43567c478bd9Sstevel@tonic-gate 
435773de625bSjg 		if (regexec(&re1, fp, 0, NULL, 0) == 0) {
43587c478bd9Sstevel@tonic-gate 			/* match */
43597c478bd9Sstevel@tonic-gate 			(void) strcpy(new_path, current_dir);
43607c478bd9Sstevel@tonic-gate 			(void) strcat(new_path, "/");
436173de625bSjg 			(void) strcat(new_path, fp);
43627c478bd9Sstevel@tonic-gate 
43637c478bd9Sstevel@tonic-gate 			vprint(RECURSEDEV_MID, "recurse_dev_re: match, new "
43640a653502Swroche 			    "path = %s\n", new_path);
43657c478bd9Sstevel@tonic-gate 
43667c478bd9Sstevel@tonic-gate 			if (slash != NULL) {
43677c478bd9Sstevel@tonic-gate 				recurse_dev_re(new_path, slash + 1, rd);
43687c478bd9Sstevel@tonic-gate 			} else {
43697c478bd9Sstevel@tonic-gate 				/* reached the leaf component of path_re */
43707c478bd9Sstevel@tonic-gate 				vprint(RECURSEDEV_MID,
43710a653502Swroche 				    "recurse_dev_re: calling fcn\n");
43727c478bd9Sstevel@tonic-gate 				(*(rd->fcn))(new_path, rd->data);
43737c478bd9Sstevel@tonic-gate 			}
43747c478bd9Sstevel@tonic-gate 		}
43757c478bd9Sstevel@tonic-gate 	}
43767c478bd9Sstevel@tonic-gate 
43777c478bd9Sstevel@tonic-gate 	regfree(&re1);
43787c478bd9Sstevel@tonic-gate 
43797c478bd9Sstevel@tonic-gate out:
438073de625bSjg 	finddev_close(fhandle);
43817c478bd9Sstevel@tonic-gate }
43827c478bd9Sstevel@tonic-gate 
43837c478bd9Sstevel@tonic-gate /*
43847c478bd9Sstevel@tonic-gate  *  Found a devpath which matches a RE in the remove structure.
43857c478bd9Sstevel@tonic-gate  *  Now check to see if it is dangling.
43867c478bd9Sstevel@tonic-gate  */
43877c478bd9Sstevel@tonic-gate static void
43887c478bd9Sstevel@tonic-gate matching_dev(char *devpath, void *data)
43897c478bd9Sstevel@tonic-gate {
43907c478bd9Sstevel@tonic-gate 	cleanup_data_t *cleanup_data = data;
4391aa646b9dSvikram 	int norm_len = strlen(dev_dir) + strlen("/");
4392aa646b9dSvikram 	int ret;
43937c478bd9Sstevel@tonic-gate 	char *fcn = "matching_dev: ";
43947c478bd9Sstevel@tonic-gate 
43957c478bd9Sstevel@tonic-gate 	vprint(RECURSEDEV_MID, "%sexamining devpath = '%s'\n", fcn,
43960a653502Swroche 	    devpath);
43977c478bd9Sstevel@tonic-gate 
4398aa646b9dSvikram 	/*
4399aa646b9dSvikram 	 * If the link is in the no-further-process hash
4400aa646b9dSvikram 	 * don't do any remove operation on it.
4401aa646b9dSvikram 	 */
4402aa646b9dSvikram 	if (nfphash_lookup(devpath + norm_len) != NULL)
4403aa646b9dSvikram 		return;
4404aa646b9dSvikram 
44057c478bd9Sstevel@tonic-gate 	if (resolve_link(devpath, NULL, NULL, NULL, 1) == TRUE) {
44067c478bd9Sstevel@tonic-gate 		if (call_minor_init(cleanup_data->rm->modptr) ==
44070a653502Swroche 		    DEVFSADM_FAILURE) {
44087c478bd9Sstevel@tonic-gate 			return;
44097c478bd9Sstevel@tonic-gate 		}
44107c478bd9Sstevel@tonic-gate 
4411aa646b9dSvikram 		devpath += norm_len;
44127c478bd9Sstevel@tonic-gate 
44130a653502Swroche 		vprint(RECURSEDEV_MID, "%scalling callback %s\n", fcn, devpath);
4414aa646b9dSvikram 		if (cleanup_data->rm->remove->flags & RM_NOINTERPOSE)
4415aa646b9dSvikram 			((void (*)(char *))
44160a653502Swroche 			    (cleanup_data->rm->remove->callback_fcn))(devpath);
4417aa646b9dSvikram 		else {
4418aa646b9dSvikram 			ret = ((int (*)(char *))
4419aa646b9dSvikram 			    (cleanup_data->rm->remove->callback_fcn))(devpath);
4420aa646b9dSvikram 			if (ret == DEVFSADM_TERMINATE) {
4421aa646b9dSvikram 				/*
4422aa646b9dSvikram 				 * We want no further remove processing for
4423aa646b9dSvikram 				 * this link. Add it to the nfp_hash;
4424aa646b9dSvikram 				 */
4425aa646b9dSvikram 				nfphash_insert(devpath);
4426aa646b9dSvikram 			}
4427aa646b9dSvikram 		}
44287c478bd9Sstevel@tonic-gate 	}
44297c478bd9Sstevel@tonic-gate }
44307c478bd9Sstevel@tonic-gate 
44317c478bd9Sstevel@tonic-gate int
44327c478bd9Sstevel@tonic-gate devfsadm_read_link(char *link, char **devfs_path)
44337c478bd9Sstevel@tonic-gate {
44347c478bd9Sstevel@tonic-gate 	char devlink[PATH_MAX];
44357c478bd9Sstevel@tonic-gate 
44367c478bd9Sstevel@tonic-gate 	*devfs_path = NULL;
44377c478bd9Sstevel@tonic-gate 
44387c478bd9Sstevel@tonic-gate 	/* prepend link with dev_dir contents */
44397c478bd9Sstevel@tonic-gate 	(void) strcpy(devlink, dev_dir);
44407c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, "/");
44417c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, link);
44427c478bd9Sstevel@tonic-gate 
44437c478bd9Sstevel@tonic-gate 	/* We *don't* want a stat of the /devices node */
44447c478bd9Sstevel@tonic-gate 	(void) resolve_link(devlink, NULL, NULL, devfs_path, 0);
44457c478bd9Sstevel@tonic-gate 
44467c478bd9Sstevel@tonic-gate 	return (*devfs_path ? DEVFSADM_SUCCESS : DEVFSADM_FAILURE);
44477c478bd9Sstevel@tonic-gate }
44487c478bd9Sstevel@tonic-gate 
44497c478bd9Sstevel@tonic-gate int
44507c478bd9Sstevel@tonic-gate devfsadm_link_valid(char *link)
44517c478bd9Sstevel@tonic-gate {
44527c478bd9Sstevel@tonic-gate 	struct stat sb;
445345916cd2Sjpk 	char devlink[PATH_MAX + 1], *contents = NULL;
44547c478bd9Sstevel@tonic-gate 	int rv, type;
445545916cd2Sjpk 	int instance = 0;
44567c478bd9Sstevel@tonic-gate 
44577c478bd9Sstevel@tonic-gate 	/* prepend link with dev_dir contents */
44587c478bd9Sstevel@tonic-gate 	(void) strcpy(devlink, dev_dir);
44597c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, "/");
44607c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, link);
44617c478bd9Sstevel@tonic-gate 
4462facf4a8dSllai 	if (!device_exists(devlink) || lstat(devlink, &sb) != 0) {
44637c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FALSE);
44647c478bd9Sstevel@tonic-gate 	}
44657c478bd9Sstevel@tonic-gate 
44667c478bd9Sstevel@tonic-gate 	contents = NULL;
44677c478bd9Sstevel@tonic-gate 	type = 0;
44687c478bd9Sstevel@tonic-gate 	if (resolve_link(devlink, &contents, &type, NULL, 1) == TRUE) {
44697c478bd9Sstevel@tonic-gate 		rv = DEVFSADM_FALSE;
44707c478bd9Sstevel@tonic-gate 	} else {
44717c478bd9Sstevel@tonic-gate 		rv = DEVFSADM_TRUE;
44727c478bd9Sstevel@tonic-gate 	}
44737c478bd9Sstevel@tonic-gate 
44747c478bd9Sstevel@tonic-gate 	/*
44757c478bd9Sstevel@tonic-gate 	 * The link exists. Add it to the database
44767c478bd9Sstevel@tonic-gate 	 */
44777c478bd9Sstevel@tonic-gate 	(void) di_devlink_add_link(devlink_cache, link, contents, type);
447845916cd2Sjpk 	if (system_labeled && (rv == DEVFSADM_TRUE) &&
447945916cd2Sjpk 	    strstr(devlink, DA_AUDIO_NAME) && contents) {
448045916cd2Sjpk 		(void) sscanf(contents, "%*[a-z]%d", &instance);
448145916cd2Sjpk 		(void) da_add_list(&devlist, devlink, instance,
448245916cd2Sjpk 		    DA_ADD|DA_AUDIO);
448345916cd2Sjpk 		_update_devalloc_db(&devlist, 0, DA_ADD, NULL, root_dir);
448445916cd2Sjpk 	}
44857c478bd9Sstevel@tonic-gate 	free(contents);
44867c478bd9Sstevel@tonic-gate 
44877c478bd9Sstevel@tonic-gate 	return (rv);
44887c478bd9Sstevel@tonic-gate }
44897c478bd9Sstevel@tonic-gate 
44907c478bd9Sstevel@tonic-gate /*
44917c478bd9Sstevel@tonic-gate  * devpath: Absolute path to /dev link
44927c478bd9Sstevel@tonic-gate  * content_p: Returns malloced string (link content)
44937c478bd9Sstevel@tonic-gate  * type_p: Returns link type: primary or secondary
44947c478bd9Sstevel@tonic-gate  * devfs_path: Returns malloced string: /devices path w/out "/devices"
44957c478bd9Sstevel@tonic-gate  * dangle: if set, check if link is dangling
44967c478bd9Sstevel@tonic-gate  * Returns:
44977c478bd9Sstevel@tonic-gate  *	TRUE if dangling
44987c478bd9Sstevel@tonic-gate  *	FALSE if not or if caller doesn't care
44997c478bd9Sstevel@tonic-gate  * Caller is assumed to have initialized pointer contents to NULL
45007c478bd9Sstevel@tonic-gate  */
45017c478bd9Sstevel@tonic-gate static int
45027c478bd9Sstevel@tonic-gate resolve_link(char *devpath, char **content_p, int *type_p, char **devfs_path,
45037c478bd9Sstevel@tonic-gate     int dangle)
45047c478bd9Sstevel@tonic-gate {
45057c478bd9Sstevel@tonic-gate 	char contents[PATH_MAX + 1];
45067c478bd9Sstevel@tonic-gate 	char stage_link[PATH_MAX + 1];
45077c478bd9Sstevel@tonic-gate 	char *fcn = "resolve_link: ";
45087c478bd9Sstevel@tonic-gate 	char *ptr;
45097c478bd9Sstevel@tonic-gate 	int linksize;
45107c478bd9Sstevel@tonic-gate 	int rv = TRUE;
45117c478bd9Sstevel@tonic-gate 	struct stat sb;
45127c478bd9Sstevel@tonic-gate 
45137c478bd9Sstevel@tonic-gate 	linksize = readlink(devpath, contents, PATH_MAX);
45147c478bd9Sstevel@tonic-gate 
45157c478bd9Sstevel@tonic-gate 	if (linksize <= 0) {
45167c478bd9Sstevel@tonic-gate 		return (FALSE);
45177c478bd9Sstevel@tonic-gate 	} else {
45187c478bd9Sstevel@tonic-gate 		contents[linksize] = '\0';
45197c478bd9Sstevel@tonic-gate 	}
45207c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%s %s -> %s\n", fcn, devpath, contents);
45217c478bd9Sstevel@tonic-gate 
45227c478bd9Sstevel@tonic-gate 	if (content_p) {
45237c478bd9Sstevel@tonic-gate 		*content_p = s_strdup(contents);
45247c478bd9Sstevel@tonic-gate 	}
45257c478bd9Sstevel@tonic-gate 
45267c478bd9Sstevel@tonic-gate 	/*
45277c478bd9Sstevel@tonic-gate 	 * Check to see if this is a link pointing to another link in /dev.  The
45287c478bd9Sstevel@tonic-gate 	 * cheap way to do this is to look for a lack of ../devices/.
45297c478bd9Sstevel@tonic-gate 	 */
45307c478bd9Sstevel@tonic-gate 
45317c478bd9Sstevel@tonic-gate 	if (is_minor_node(contents, &ptr) == DEVFSADM_FALSE) {
45327c478bd9Sstevel@tonic-gate 
45337c478bd9Sstevel@tonic-gate 		if (type_p) {
45347c478bd9Sstevel@tonic-gate 			*type_p = DI_SECONDARY_LINK;
45357c478bd9Sstevel@tonic-gate 		}
45367c478bd9Sstevel@tonic-gate 
45377c478bd9Sstevel@tonic-gate 		/*
45387c478bd9Sstevel@tonic-gate 		 * assume that linkcontents is really a pointer to another
45397c478bd9Sstevel@tonic-gate 		 * link, and if so recurse and read its link contents.
45407c478bd9Sstevel@tonic-gate 		 */
45417c478bd9Sstevel@tonic-gate 		if (strncmp(contents, DEV "/", strlen(DEV) + 1) == 0)  {
45427c478bd9Sstevel@tonic-gate 			(void) strcpy(stage_link, dev_dir);
45437c478bd9Sstevel@tonic-gate 			(void) strcat(stage_link, "/");
45447c478bd9Sstevel@tonic-gate 			(void) strcpy(stage_link,
45450a653502Swroche 			    &contents[strlen(DEV) + strlen("/")]);
45467c478bd9Sstevel@tonic-gate 		} else {
45477c478bd9Sstevel@tonic-gate 			if ((ptr = strrchr(devpath, '/')) == NULL) {
45487c478bd9Sstevel@tonic-gate 				vprint(REMOVE_MID, "%s%s -> %s invalid link. "
45490a653502Swroche 				    "missing '/'\n", fcn, devpath, contents);
45507c478bd9Sstevel@tonic-gate 				return (TRUE);
45517c478bd9Sstevel@tonic-gate 			}
45527c478bd9Sstevel@tonic-gate 			*ptr = '\0';
45537c478bd9Sstevel@tonic-gate 			(void) strcpy(stage_link, devpath);
45547c478bd9Sstevel@tonic-gate 			*ptr = '/';
45557c478bd9Sstevel@tonic-gate 			(void) strcat(stage_link, "/");
45567c478bd9Sstevel@tonic-gate 			(void) strcat(stage_link, contents);
45577c478bd9Sstevel@tonic-gate 		}
45587c478bd9Sstevel@tonic-gate 		return (resolve_link(stage_link, NULL, NULL, devfs_path,
45597c478bd9Sstevel@tonic-gate 		    dangle));
45607c478bd9Sstevel@tonic-gate 	}
45617c478bd9Sstevel@tonic-gate 
45627c478bd9Sstevel@tonic-gate 	/* Current link points at a /devices minor node */
45637c478bd9Sstevel@tonic-gate 	if (type_p) {
45647c478bd9Sstevel@tonic-gate 		*type_p = DI_PRIMARY_LINK;
45657c478bd9Sstevel@tonic-gate 	}
45667c478bd9Sstevel@tonic-gate 
45677c478bd9Sstevel@tonic-gate 	if (devfs_path)
45687c478bd9Sstevel@tonic-gate 		*devfs_path = s_strdup(ptr);
45697c478bd9Sstevel@tonic-gate 
45707c478bd9Sstevel@tonic-gate 	rv = FALSE;
45717c478bd9Sstevel@tonic-gate 	if (dangle)
45727c478bd9Sstevel@tonic-gate 		rv = (stat(ptr - strlen(DEVICES), &sb) == -1);
45737c478bd9Sstevel@tonic-gate 
45747c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%slink=%s, returning %s\n", fcn,
45750a653502Swroche 	    devpath, ((rv == TRUE) ? "TRUE" : "FALSE"));
45767c478bd9Sstevel@tonic-gate 
45777c478bd9Sstevel@tonic-gate 	return (rv);
45787c478bd9Sstevel@tonic-gate }
45797c478bd9Sstevel@tonic-gate 
45807c478bd9Sstevel@tonic-gate /*
45817c478bd9Sstevel@tonic-gate  * Returns the substring of interest, given a path.
45827c478bd9Sstevel@tonic-gate  */
45837c478bd9Sstevel@tonic-gate static char *
45847c478bd9Sstevel@tonic-gate alloc_cmp_str(const char *path, devfsadm_enumerate_t *dep)
45857c478bd9Sstevel@tonic-gate {
45867c478bd9Sstevel@tonic-gate 	uint_t match;
45877c478bd9Sstevel@tonic-gate 	char *np, *ap, *mp;
45887c478bd9Sstevel@tonic-gate 	char *cmp_str = NULL;
45897c478bd9Sstevel@tonic-gate 	char at[] = "@";
45907c478bd9Sstevel@tonic-gate 	char *fcn = "alloc_cmp_str";
45917c478bd9Sstevel@tonic-gate 
45927c478bd9Sstevel@tonic-gate 	np = ap = mp = NULL;
45937c478bd9Sstevel@tonic-gate 
45947c478bd9Sstevel@tonic-gate 	/*
45957c478bd9Sstevel@tonic-gate 	 * extract match flags from the flags argument.
45967c478bd9Sstevel@tonic-gate 	 */
45977c478bd9Sstevel@tonic-gate 	match = (dep->flags & MATCH_MASK);
45987c478bd9Sstevel@tonic-gate 
45997c478bd9Sstevel@tonic-gate 	vprint(ENUM_MID, "%s: enumeration match type: 0x%x"
46007c478bd9Sstevel@tonic-gate 	    " path: %s\n", fcn, match, path);
46017c478bd9Sstevel@tonic-gate 
46027c478bd9Sstevel@tonic-gate 	/*
46037c478bd9Sstevel@tonic-gate 	 * MATCH_CALLBACK and MATCH_ALL are the only flags
46047c478bd9Sstevel@tonic-gate 	 * which may be used if "path" is a /dev path
46057c478bd9Sstevel@tonic-gate 	 */
46067c478bd9Sstevel@tonic-gate 	if (match == MATCH_CALLBACK) {
46077c478bd9Sstevel@tonic-gate 		if (dep->sel_fcn == NULL) {
46087c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid enumerate"
46097c478bd9Sstevel@tonic-gate 			    " callback: path: %s\n", fcn, path);
46107c478bd9Sstevel@tonic-gate 			return (NULL);
46117c478bd9Sstevel@tonic-gate 		}
46127c478bd9Sstevel@tonic-gate 		cmp_str = dep->sel_fcn(path, dep->cb_arg);
46137c478bd9Sstevel@tonic-gate 		return (cmp_str);
46147c478bd9Sstevel@tonic-gate 	}
46157c478bd9Sstevel@tonic-gate 
46167c478bd9Sstevel@tonic-gate 	cmp_str = s_strdup(path);
46177c478bd9Sstevel@tonic-gate 
46187c478bd9Sstevel@tonic-gate 	if (match == MATCH_ALL) {
46197c478bd9Sstevel@tonic-gate 		return (cmp_str);
46207c478bd9Sstevel@tonic-gate 	}
46217c478bd9Sstevel@tonic-gate 
46227c478bd9Sstevel@tonic-gate 	/*
46237c478bd9Sstevel@tonic-gate 	 * The remaining flags make sense only for /devices
46247c478bd9Sstevel@tonic-gate 	 * paths
46257c478bd9Sstevel@tonic-gate 	 */
46267c478bd9Sstevel@tonic-gate 	if ((mp = strrchr(cmp_str, ':')) == NULL) {
46277c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: invalid path: %s\n",
46287c478bd9Sstevel@tonic-gate 		    fcn, path);
46297c478bd9Sstevel@tonic-gate 		goto err;
46307c478bd9Sstevel@tonic-gate 	}
46317c478bd9Sstevel@tonic-gate 
46327c478bd9Sstevel@tonic-gate 	if (match == MATCH_MINOR) {
46337c478bd9Sstevel@tonic-gate 		/* A NULL "match_arg" values implies entire minor */
46347c478bd9Sstevel@tonic-gate 		if (get_component(mp + 1, dep->match_arg) == NULL) {
46357c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid minor component:"
46367c478bd9Sstevel@tonic-gate 			    " path: %s\n", fcn, path);
46377c478bd9Sstevel@tonic-gate 			goto err;
46387c478bd9Sstevel@tonic-gate 		}
46397c478bd9Sstevel@tonic-gate 		return (cmp_str);
46407c478bd9Sstevel@tonic-gate 	}
46417c478bd9Sstevel@tonic-gate 
46427c478bd9Sstevel@tonic-gate 	if ((np = strrchr(cmp_str, '/')) == NULL) {
46437c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: invalid path: %s\n", fcn, path);
46447c478bd9Sstevel@tonic-gate 		goto err;
46457c478bd9Sstevel@tonic-gate 	}
46467c478bd9Sstevel@tonic-gate 
46477c478bd9Sstevel@tonic-gate 	if (match == MATCH_PARENT) {
46487c478bd9Sstevel@tonic-gate 		if (strcmp(cmp_str, "/") == 0) {
46497c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid path: %s\n",
46507c478bd9Sstevel@tonic-gate 			    fcn, path);
46517c478bd9Sstevel@tonic-gate 			goto err;
46527c478bd9Sstevel@tonic-gate 		}
46537c478bd9Sstevel@tonic-gate 
46547c478bd9Sstevel@tonic-gate 		if (np == cmp_str) {
46557c478bd9Sstevel@tonic-gate 			*(np + 1) = '\0';
46567c478bd9Sstevel@tonic-gate 		} else {
46577c478bd9Sstevel@tonic-gate 			*np = '\0';
46587c478bd9Sstevel@tonic-gate 		}
46597c478bd9Sstevel@tonic-gate 		return (cmp_str);
46607c478bd9Sstevel@tonic-gate 	}
46617c478bd9Sstevel@tonic-gate 
46627c478bd9Sstevel@tonic-gate 	/* ap can be NULL - Leaf address may not exist or be empty string */
46637c478bd9Sstevel@tonic-gate 	ap = strchr(np+1, '@');
46647c478bd9Sstevel@tonic-gate 
46657c478bd9Sstevel@tonic-gate 	/* minor is no longer of interest */
46667c478bd9Sstevel@tonic-gate 	*mp = '\0';
46677c478bd9Sstevel@tonic-gate 
46687c478bd9Sstevel@tonic-gate 	if (match == MATCH_NODE) {
46697c478bd9Sstevel@tonic-gate 		if (ap)
46707c478bd9Sstevel@tonic-gate 			*ap = '\0';
46717c478bd9Sstevel@tonic-gate 		return (cmp_str);
46727c478bd9Sstevel@tonic-gate 	} else if (match == MATCH_ADDR) {
46737c478bd9Sstevel@tonic-gate 		/*
46747c478bd9Sstevel@tonic-gate 		 * The empty string is a valid address. The only MATCH_ADDR
46757c478bd9Sstevel@tonic-gate 		 * allowed in this case is against the whole address or
46767c478bd9Sstevel@tonic-gate 		 * the first component of the address (match_arg=NULL/"0"/"1")
46777c478bd9Sstevel@tonic-gate 		 * Note that in this case, the path won't have an "@"
46787c478bd9Sstevel@tonic-gate 		 * As a result ap will be NULL. We fake up an ap = @'\0'
46797c478bd9Sstevel@tonic-gate 		 * so that get_component() will work correctly.
46807c478bd9Sstevel@tonic-gate 		 */
46817c478bd9Sstevel@tonic-gate 		if (ap == NULL) {
46827c478bd9Sstevel@tonic-gate 			ap = at;
46837c478bd9Sstevel@tonic-gate 		}
46847c478bd9Sstevel@tonic-gate 
46857c478bd9Sstevel@tonic-gate 		if (get_component(ap + 1, dep->match_arg) == NULL) {
46867c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid leaf addr. component:"
46877c478bd9Sstevel@tonic-gate 			    " path: %s\n", fcn, path);
46887c478bd9Sstevel@tonic-gate 			goto err;
46897c478bd9Sstevel@tonic-gate 		}
46907c478bd9Sstevel@tonic-gate 		return (cmp_str);
46917c478bd9Sstevel@tonic-gate 	}
46927c478bd9Sstevel@tonic-gate 
46937c478bd9Sstevel@tonic-gate 	vprint(ENUM_MID, "%s: invalid enumeration flags: 0x%x"
46940a653502Swroche 	    " path: %s\n", fcn, dep->flags, path);
46957c478bd9Sstevel@tonic-gate 
46967c478bd9Sstevel@tonic-gate 	/*FALLTHRU*/
46977c478bd9Sstevel@tonic-gate err:
46987c478bd9Sstevel@tonic-gate 	free(cmp_str);
46997c478bd9Sstevel@tonic-gate 	return (NULL);
47007c478bd9Sstevel@tonic-gate }
47017c478bd9Sstevel@tonic-gate 
47027c478bd9Sstevel@tonic-gate 
47037c478bd9Sstevel@tonic-gate /*
47047c478bd9Sstevel@tonic-gate  * "str" is expected to be a string with components separated by ','
47057c478bd9Sstevel@tonic-gate  * The terminating null char is considered a separator.
47067c478bd9Sstevel@tonic-gate  * get_component() will remove the portion of the string beyond
47077c478bd9Sstevel@tonic-gate  * the component indicated.
47087c478bd9Sstevel@tonic-gate  * If comp_str is NULL, the entire "str" is returned.
47097c478bd9Sstevel@tonic-gate  */
47107c478bd9Sstevel@tonic-gate static char *
47117c478bd9Sstevel@tonic-gate get_component(char *str, const char *comp_str)
47127c478bd9Sstevel@tonic-gate {
47137c478bd9Sstevel@tonic-gate 	long comp;
47147c478bd9Sstevel@tonic-gate 	char *cp;
47157c478bd9Sstevel@tonic-gate 
47167c478bd9Sstevel@tonic-gate 	if (str == NULL) {
47177c478bd9Sstevel@tonic-gate 		return (NULL);
47187c478bd9Sstevel@tonic-gate 	}
47197c478bd9Sstevel@tonic-gate 
47207c478bd9Sstevel@tonic-gate 	if (comp_str == NULL) {
47217c478bd9Sstevel@tonic-gate 		return (str);
47227c478bd9Sstevel@tonic-gate 	}
47237c478bd9Sstevel@tonic-gate 
47247c478bd9Sstevel@tonic-gate 	errno = 0;
47257c478bd9Sstevel@tonic-gate 	comp = strtol(comp_str, &cp, 10);
47267c478bd9Sstevel@tonic-gate 	if (errno != 0 || *cp != '\0' || comp < 0) {
47277c478bd9Sstevel@tonic-gate 		return (NULL);
47287c478bd9Sstevel@tonic-gate 	}
47297c478bd9Sstevel@tonic-gate 
47307c478bd9Sstevel@tonic-gate 	if (comp == 0)
47317c478bd9Sstevel@tonic-gate 		return (str);
47327c478bd9Sstevel@tonic-gate 
47337c478bd9Sstevel@tonic-gate 	for (cp = str; ; cp++) {
47347c478bd9Sstevel@tonic-gate 		if (*cp == ',' || *cp == '\0')
47357c478bd9Sstevel@tonic-gate 			comp--;
47367c478bd9Sstevel@tonic-gate 		if (*cp == '\0' || comp <= 0) {
47377c478bd9Sstevel@tonic-gate 			break;
47387c478bd9Sstevel@tonic-gate 		}
47397c478bd9Sstevel@tonic-gate 	}
47407c478bd9Sstevel@tonic-gate 
47417c478bd9Sstevel@tonic-gate 	if (comp == 0) {
47427c478bd9Sstevel@tonic-gate 		*cp = '\0';
47437c478bd9Sstevel@tonic-gate 	} else {
47447c478bd9Sstevel@tonic-gate 		str = NULL;
47457c478bd9Sstevel@tonic-gate 	}
47467c478bd9Sstevel@tonic-gate 
47477c478bd9Sstevel@tonic-gate 	return (str);
47487c478bd9Sstevel@tonic-gate }
47497c478bd9Sstevel@tonic-gate 
47507c478bd9Sstevel@tonic-gate 
47517c478bd9Sstevel@tonic-gate /*
47527c478bd9Sstevel@tonic-gate  * Enumerate serves as a generic counter as well as a means to determine
47537c478bd9Sstevel@tonic-gate  * logical unit/controller numbers for such items as disk and tape
47547c478bd9Sstevel@tonic-gate  * drives.
47557c478bd9Sstevel@tonic-gate  *
47567c478bd9Sstevel@tonic-gate  * rules[] is an array of  devfsadm_enumerate_t structures which defines
47577c478bd9Sstevel@tonic-gate  * the enumeration rules to be used for a specified set of links in /dev.
47587c478bd9Sstevel@tonic-gate  * The set of links is specified through regular expressions (of the flavor
47597c478bd9Sstevel@tonic-gate  * described in regex(5)). These regular expressions are used to determine
47607c478bd9Sstevel@tonic-gate  * the set of links in /dev to examine. The last path component in these
47617c478bd9Sstevel@tonic-gate  * regular expressions MUST contain a parenthesized subexpression surrounding
47627c478bd9Sstevel@tonic-gate  * the RE which is to be considered the enumerating component. The subexp
47637c478bd9Sstevel@tonic-gate  * member in a rule is the subexpression number of the enumerating
47647c478bd9Sstevel@tonic-gate  * component. Subexpressions in the last path component are numbered starting
47657c478bd9Sstevel@tonic-gate  * from 1.
47667c478bd9Sstevel@tonic-gate  *
47677c478bd9Sstevel@tonic-gate  * A cache of current id assignments is built up from existing symlinks and
47687c478bd9Sstevel@tonic-gate  * new assignments use the lowest unused id. Assignments are based on a
47697c478bd9Sstevel@tonic-gate  * match of a specified substring of a symlink's contents. If the specified
47707c478bd9Sstevel@tonic-gate  * component for the devfs_path argument matches the corresponding substring
47717c478bd9Sstevel@tonic-gate  * for a existing symlink's contents, the cached id is returned. Else, a new
47727c478bd9Sstevel@tonic-gate  * id is created and returned in *buf. *buf must be freed by the caller.
47737c478bd9Sstevel@tonic-gate  *
47747c478bd9Sstevel@tonic-gate  * An id assignment may be governed by a combination of rules, each rule
47757c478bd9Sstevel@tonic-gate  * applicable to a different subset of links in /dev. For example, controller
47767c478bd9Sstevel@tonic-gate  * numbers may be determined by a combination of disk symlinks in /dev/[r]dsk
47777c478bd9Sstevel@tonic-gate  * and controller symlinks in /dev/cfg, with the two sets requiring different
47787c478bd9Sstevel@tonic-gate  * rules to derive the "substring of interest". In such cases, the rules
47797c478bd9Sstevel@tonic-gate  * array will have more than one element.
47807c478bd9Sstevel@tonic-gate  */
47817c478bd9Sstevel@tonic-gate int
47827c478bd9Sstevel@tonic-gate devfsadm_enumerate_int(char *devfs_path, int index, char **buf,
47837c478bd9Sstevel@tonic-gate 			devfsadm_enumerate_t rules[], int nrules)
47847c478bd9Sstevel@tonic-gate {
47857c478bd9Sstevel@tonic-gate 	return (find_enum_id(rules, nrules,
47867c478bd9Sstevel@tonic-gate 	    devfs_path, index, "0", INTEGER, buf, 0));
47877c478bd9Sstevel@tonic-gate }
47887c478bd9Sstevel@tonic-gate 
47897c478bd9Sstevel@tonic-gate int
47907c478bd9Sstevel@tonic-gate disk_enumerate_int(char *devfs_path, int index, char **buf,
47917c478bd9Sstevel@tonic-gate     devfsadm_enumerate_t rules[], int nrules)
47927c478bd9Sstevel@tonic-gate {
47937c478bd9Sstevel@tonic-gate 	return (find_enum_id(rules, nrules,
47947c478bd9Sstevel@tonic-gate 	    devfs_path, index, "0", INTEGER, buf, 1));
47957c478bd9Sstevel@tonic-gate }
47967c478bd9Sstevel@tonic-gate 
47977c478bd9Sstevel@tonic-gate /*
47987c478bd9Sstevel@tonic-gate  * Same as above, but allows a starting value to be specified.
47997c478bd9Sstevel@tonic-gate  * Private to devfsadm.... used by devlinks.
48007c478bd9Sstevel@tonic-gate  */
48017c478bd9Sstevel@tonic-gate static int
48027c478bd9Sstevel@tonic-gate devfsadm_enumerate_int_start(char *devfs_path, int index, char **buf,
48037c478bd9Sstevel@tonic-gate 		devfsadm_enumerate_t rules[], int nrules, char *start)
48047c478bd9Sstevel@tonic-gate {
48057c478bd9Sstevel@tonic-gate 	return (find_enum_id(rules, nrules,
48067c478bd9Sstevel@tonic-gate 	    devfs_path, index, start, INTEGER, buf, 0));
48077c478bd9Sstevel@tonic-gate }
48087c478bd9Sstevel@tonic-gate 
48097c478bd9Sstevel@tonic-gate /*
48107c478bd9Sstevel@tonic-gate  *  devfsadm_enumerate_char serves as a generic counter returning
48117c478bd9Sstevel@tonic-gate  *  a single letter.
48127c478bd9Sstevel@tonic-gate  */
48137c478bd9Sstevel@tonic-gate int
48147c478bd9Sstevel@tonic-gate devfsadm_enumerate_char(char *devfs_path, int index, char **buf,
48157c478bd9Sstevel@tonic-gate 			devfsadm_enumerate_t rules[], int nrules)
48167c478bd9Sstevel@tonic-gate {
48177c478bd9Sstevel@tonic-gate 	return (find_enum_id(rules, nrules,
48187c478bd9Sstevel@tonic-gate 	    devfs_path, index, "a", LETTER, buf, 0));
48197c478bd9Sstevel@tonic-gate }
48207c478bd9Sstevel@tonic-gate 
48217c478bd9Sstevel@tonic-gate /*
48227c478bd9Sstevel@tonic-gate  * Same as above, but allows a starting char to be specified.
48237c478bd9Sstevel@tonic-gate  * Private to devfsadm - used by ports module (port_link.c)
48247c478bd9Sstevel@tonic-gate  */
48257c478bd9Sstevel@tonic-gate int
48267c478bd9Sstevel@tonic-gate devfsadm_enumerate_char_start(char *devfs_path, int index, char **buf,
48277c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[], int nrules, char *start)
48287c478bd9Sstevel@tonic-gate {
48297c478bd9Sstevel@tonic-gate 	return (find_enum_id(rules, nrules,
48307c478bd9Sstevel@tonic-gate 	    devfs_path, index, start, LETTER, buf, 0));
48317c478bd9Sstevel@tonic-gate }
48327c478bd9Sstevel@tonic-gate 
48337c478bd9Sstevel@tonic-gate 
48347c478bd9Sstevel@tonic-gate /*
48357c478bd9Sstevel@tonic-gate  * For a given numeral_set (see get_cached_set for desc of numeral_set),
48367c478bd9Sstevel@tonic-gate  * search all cached entries looking for matches on a specified substring
48377c478bd9Sstevel@tonic-gate  * of devfs_path. The substring is derived from devfs_path based on the
48387c478bd9Sstevel@tonic-gate  * rule specified by "index". If a match is found on a cached entry,
48397c478bd9Sstevel@tonic-gate  * return the enumerated id in buf. Otherwise, create a new id by calling
48407c478bd9Sstevel@tonic-gate  * new_id, then cache and return that entry.
48417c478bd9Sstevel@tonic-gate  */
48427c478bd9Sstevel@tonic-gate static int
48437c478bd9Sstevel@tonic-gate find_enum_id(devfsadm_enumerate_t rules[], int nrules,
48447c478bd9Sstevel@tonic-gate 	char *devfs_path, int index, char *min, int type, char **buf,
48457c478bd9Sstevel@tonic-gate 	int multiple)
48467c478bd9Sstevel@tonic-gate {
48477c478bd9Sstevel@tonic-gate 	numeral_t *matchnp;
48487c478bd9Sstevel@tonic-gate 	numeral_t *numeral;
48497c478bd9Sstevel@tonic-gate 	int matchcount = 0;
48507c478bd9Sstevel@tonic-gate 	char *cmp_str;
48517c478bd9Sstevel@tonic-gate 	char *fcn = "find_enum_id";
48527c478bd9Sstevel@tonic-gate 	numeral_set_t *set;
48537c478bd9Sstevel@tonic-gate 
48547c478bd9Sstevel@tonic-gate 	if (rules == NULL) {
48557c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: no rules. path: %s\n",
48567c478bd9Sstevel@tonic-gate 		    fcn, devfs_path ? devfs_path : "<NULL path>");
48577c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
48587c478bd9Sstevel@tonic-gate 	}
48597c478bd9Sstevel@tonic-gate 
48607c478bd9Sstevel@tonic-gate 	if (devfs_path == NULL) {
48617c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: NULL path\n", fcn);
48627c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
48637c478bd9Sstevel@tonic-gate 	}
48647c478bd9Sstevel@tonic-gate 
48657c478bd9Sstevel@tonic-gate 	if (nrules <= 0 || index < 0 || index >= nrules || buf == NULL) {
48667c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: invalid arguments. path: %s\n",
48677c478bd9Sstevel@tonic-gate 		    fcn, devfs_path);
48687c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
48697c478bd9Sstevel@tonic-gate 	}
48707c478bd9Sstevel@tonic-gate 
48717c478bd9Sstevel@tonic-gate 	*buf = NULL;
48727c478bd9Sstevel@tonic-gate 
48737c478bd9Sstevel@tonic-gate 
48747c478bd9Sstevel@tonic-gate 	cmp_str = alloc_cmp_str(devfs_path, &rules[index]);
48757c478bd9Sstevel@tonic-gate 	if (cmp_str == NULL) {
48767c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
48777c478bd9Sstevel@tonic-gate 	}
48787c478bd9Sstevel@tonic-gate 
48797c478bd9Sstevel@tonic-gate 	if ((set = get_enum_cache(rules, nrules)) == NULL) {
48807c478bd9Sstevel@tonic-gate 		free(cmp_str);
48817c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
48827c478bd9Sstevel@tonic-gate 	}
48837c478bd9Sstevel@tonic-gate 
48847c478bd9Sstevel@tonic-gate 	assert(nrules == set->re_count);
48857c478bd9Sstevel@tonic-gate 
48867c478bd9Sstevel@tonic-gate 	/*
48877c478bd9Sstevel@tonic-gate 	 * Check and see if a matching entry is already cached.
48887c478bd9Sstevel@tonic-gate 	 */
48897c478bd9Sstevel@tonic-gate 	matchcount = lookup_enum_cache(set, cmp_str, rules, index,
48907c478bd9Sstevel@tonic-gate 	    &matchnp);
48917c478bd9Sstevel@tonic-gate 
48927c478bd9Sstevel@tonic-gate 	if (matchcount < 0 || matchcount > 1) {
48937c478bd9Sstevel@tonic-gate 		free(cmp_str);
48947c478bd9Sstevel@tonic-gate 		if (multiple && matchcount > 1)
48957c478bd9Sstevel@tonic-gate 			return (DEVFSADM_MULTIPLE);
48967c478bd9Sstevel@tonic-gate 		else
48977c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
48987c478bd9Sstevel@tonic-gate 	}
48997c478bd9Sstevel@tonic-gate 
49007c478bd9Sstevel@tonic-gate 	/* if matching entry already cached, return it */
49017c478bd9Sstevel@tonic-gate 	if (matchcount == 1) {
49028d483882Smlf 		/* should never create a link with a reserved ID */
49038d483882Smlf 		vprint(ENUM_MID, "%s: 1 match w/ ID: %s\n", fcn, matchnp->id);
49048d483882Smlf 		assert(matchnp->flags == 0);
49057c478bd9Sstevel@tonic-gate 		*buf = s_strdup(matchnp->id);
49067c478bd9Sstevel@tonic-gate 		free(cmp_str);
49077c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
49087c478bd9Sstevel@tonic-gate 	}
49097c478bd9Sstevel@tonic-gate 
49107c478bd9Sstevel@tonic-gate 	/*
49117c478bd9Sstevel@tonic-gate 	 * no cached entry, initialize a numeral struct
49127c478bd9Sstevel@tonic-gate 	 * by calling new_id() and cache onto the numeral_set
49137c478bd9Sstevel@tonic-gate 	 */
49147c478bd9Sstevel@tonic-gate 	numeral = s_malloc(sizeof (numeral_t));
49157c478bd9Sstevel@tonic-gate 	numeral->id = new_id(set->headnumeral, type, min);
49167c478bd9Sstevel@tonic-gate 	numeral->full_path = s_strdup(devfs_path);
49177c478bd9Sstevel@tonic-gate 	numeral->rule_index = index;
49187c478bd9Sstevel@tonic-gate 	numeral->cmp_str = cmp_str;
49197c478bd9Sstevel@tonic-gate 	cmp_str = NULL;
49208d483882Smlf 	numeral->flags = 0;
49218d483882Smlf 	vprint(RSRV_MID, "%s: alloc new_id: %s numeral flags = %d\n",
49228d483882Smlf 	    fcn, numeral->id, numeral->flags);
49238d483882Smlf 
49247c478bd9Sstevel@tonic-gate 
49257c478bd9Sstevel@tonic-gate 	/* insert to head of list for fast lookups */
49267c478bd9Sstevel@tonic-gate 	numeral->next = set->headnumeral;
49277c478bd9Sstevel@tonic-gate 	set->headnumeral = numeral;
49287c478bd9Sstevel@tonic-gate 
49297c478bd9Sstevel@tonic-gate 	*buf = s_strdup(numeral->id);
49307c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
49317c478bd9Sstevel@tonic-gate }
49327c478bd9Sstevel@tonic-gate 
49337c478bd9Sstevel@tonic-gate 
49347c478bd9Sstevel@tonic-gate /*
49357c478bd9Sstevel@tonic-gate  * Looks up the specified cache for a match with a specified string
49367c478bd9Sstevel@tonic-gate  * Returns:
49377c478bd9Sstevel@tonic-gate  *	-1	: on error.
49387c478bd9Sstevel@tonic-gate  *	0/1/2	: Number of matches.
49397c478bd9Sstevel@tonic-gate  * Returns the matching element only if there is a single match.
49407c478bd9Sstevel@tonic-gate  * If the "uncached" flag is set, derives the "cmp_str" afresh
49417c478bd9Sstevel@tonic-gate  * for the match instead of using cached values.
49427c478bd9Sstevel@tonic-gate  */
49437c478bd9Sstevel@tonic-gate static int
49447c478bd9Sstevel@tonic-gate lookup_enum_cache(numeral_set_t *set, char *cmp_str,
49457c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[], int index, numeral_t **matchnpp)
49467c478bd9Sstevel@tonic-gate {
49477c478bd9Sstevel@tonic-gate 	int matchcount = 0, rv = -1;
49487c478bd9Sstevel@tonic-gate 	int uncached;
49497c478bd9Sstevel@tonic-gate 	numeral_t *np;
49507c478bd9Sstevel@tonic-gate 	char *fcn = "lookup_enum_cache";
49517c478bd9Sstevel@tonic-gate 	char *cp;
49527c478bd9Sstevel@tonic-gate 
49537c478bd9Sstevel@tonic-gate 	*matchnpp = NULL;
49547c478bd9Sstevel@tonic-gate 
49557c478bd9Sstevel@tonic-gate 	assert(index < set->re_count);
49567c478bd9Sstevel@tonic-gate 
49577c478bd9Sstevel@tonic-gate 	if (cmp_str == NULL) {
49587c478bd9Sstevel@tonic-gate 		return (-1);
49597c478bd9Sstevel@tonic-gate 	}
49607c478bd9Sstevel@tonic-gate 
49617c478bd9Sstevel@tonic-gate 	uncached = 0;
49627c478bd9Sstevel@tonic-gate 	if ((rules[index].flags & MATCH_UNCACHED) == MATCH_UNCACHED) {
49637c478bd9Sstevel@tonic-gate 		uncached = 1;
49647c478bd9Sstevel@tonic-gate 	}
49657c478bd9Sstevel@tonic-gate 
49667c478bd9Sstevel@tonic-gate 	/*
49677c478bd9Sstevel@tonic-gate 	 * Check and see if a matching entry is already cached.
49687c478bd9Sstevel@tonic-gate 	 */
49697c478bd9Sstevel@tonic-gate 	for (np = set->headnumeral; np != NULL; np = np->next) {
49708d483882Smlf 
49718d483882Smlf 		/*
49728d483882Smlf 		 * Skip reserved IDs
49738d483882Smlf 		 */
49748d483882Smlf 		if (np->flags & NUMERAL_RESERVED) {
49758d483882Smlf 			vprint(RSRV_MID, "lookup_enum_cache: "
49768d483882Smlf 			    "Cannot Match with reserved ID (%s), "
49778d483882Smlf 			    "skipping\n", np->id);
49788d483882Smlf 			assert(np->flags == NUMERAL_RESERVED);
49798d483882Smlf 			continue;
49808d483882Smlf 		} else {
49818d483882Smlf 			vprint(RSRV_MID, "lookup_enum_cache: "
49828d483882Smlf 			    "Attempting match with numeral ID: %s"
49838d483882Smlf 			    " numeral flags = %d\n", np->id, np->flags);
49848d483882Smlf 			assert(np->flags == 0);
49858d483882Smlf 		}
49868d483882Smlf 
49877c478bd9Sstevel@tonic-gate 		if (np->cmp_str == NULL) {
49887c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid entry in enumerate"
49897c478bd9Sstevel@tonic-gate 			    " cache. path: %s\n", fcn, np->full_path);
49907c478bd9Sstevel@tonic-gate 			return (-1);
49917c478bd9Sstevel@tonic-gate 		}
49927c478bd9Sstevel@tonic-gate 
49937c478bd9Sstevel@tonic-gate 		if (uncached) {
49947c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "%s: bypassing enumerate cache."
49957c478bd9Sstevel@tonic-gate 			    " path: %s\n", fcn, cmp_str);
49967c478bd9Sstevel@tonic-gate 			cp = alloc_cmp_str(np->full_path,
49977c478bd9Sstevel@tonic-gate 			    &rules[np->rule_index]);
49987c478bd9Sstevel@tonic-gate 			if (cp == NULL)
49997c478bd9Sstevel@tonic-gate 				return (-1);
50007c478bd9Sstevel@tonic-gate 			rv = strcmp(cmp_str, cp);
50017c478bd9Sstevel@tonic-gate 			free(cp);
50027c478bd9Sstevel@tonic-gate 		} else {
50037c478bd9Sstevel@tonic-gate 			rv = strcmp(cmp_str, np->cmp_str);
50047c478bd9Sstevel@tonic-gate 		}
50057c478bd9Sstevel@tonic-gate 
50067c478bd9Sstevel@tonic-gate 		if (rv == 0) {
50077c478bd9Sstevel@tonic-gate 			if (matchcount++ != 0) {
50087c478bd9Sstevel@tonic-gate 				break; /* more than 1 match. */
50097c478bd9Sstevel@tonic-gate 			}
50107c478bd9Sstevel@tonic-gate 			*matchnpp = np;
50117c478bd9Sstevel@tonic-gate 		}
50127c478bd9Sstevel@tonic-gate 	}
50137c478bd9Sstevel@tonic-gate 
50147c478bd9Sstevel@tonic-gate 	return (matchcount);
50157c478bd9Sstevel@tonic-gate }
50167c478bd9Sstevel@tonic-gate 
50177c478bd9Sstevel@tonic-gate #ifdef	DEBUG
50187c478bd9Sstevel@tonic-gate static void
50197c478bd9Sstevel@tonic-gate dump_enum_cache(numeral_set_t *setp)
50207c478bd9Sstevel@tonic-gate {
50217c478bd9Sstevel@tonic-gate 	int i;
50227c478bd9Sstevel@tonic-gate 	numeral_t *np;
50237c478bd9Sstevel@tonic-gate 	char *fcn = "dump_enum_cache";
50247c478bd9Sstevel@tonic-gate 
50257c478bd9Sstevel@tonic-gate 	vprint(ENUM_MID, "%s: re_count = %d\n", fcn, setp->re_count);
50267c478bd9Sstevel@tonic-gate 	for (i = 0; i < setp->re_count; i++) {
50277c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: re[%d] = %s\n", fcn, i, setp->re[i]);
50287c478bd9Sstevel@tonic-gate 	}
50297c478bd9Sstevel@tonic-gate 
50307c478bd9Sstevel@tonic-gate 	for (np = setp->headnumeral; np != NULL; np = np->next) {
50317c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: id: %s\n", fcn, np->id);
50327c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: full_path: %s\n", fcn, np->full_path);
50337c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: rule_index: %d\n", fcn, np->rule_index);
50347c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: cmp_str: %s\n", fcn, np->cmp_str);
50358d483882Smlf 		vprint(ENUM_MID, "%s: flags: %d\n", fcn, np->flags);
50367c478bd9Sstevel@tonic-gate 	}
50377c478bd9Sstevel@tonic-gate }
50387c478bd9Sstevel@tonic-gate #endif
50397c478bd9Sstevel@tonic-gate 
50407c478bd9Sstevel@tonic-gate /*
50417c478bd9Sstevel@tonic-gate  * For a given set of regular expressions in rules[], this function returns
50427c478bd9Sstevel@tonic-gate  * either a previously cached struct numeral_set or it will create and
50437c478bd9Sstevel@tonic-gate  * cache a new struct numeral_set.  There is only one struct numeral_set
50447c478bd9Sstevel@tonic-gate  * for the combination of REs present in rules[].  Each numeral_set contains
50457c478bd9Sstevel@tonic-gate  * the regular expressions in rules[] used for cache selection AND a linked
50467c478bd9Sstevel@tonic-gate  * list of struct numerals, ONE FOR EACH *UNIQUE* numeral or character ID
50477c478bd9Sstevel@tonic-gate  * selected by the grouping parenthesized subexpression found in the last
50487c478bd9Sstevel@tonic-gate  * path component of each rules[].re.  For example, the RE: "rmt/([0-9]+)"
50497c478bd9Sstevel@tonic-gate  * selects all the logical nodes of the correct form in dev/rmt/.
50507c478bd9Sstevel@tonic-gate  * Each rmt/X will store a *single* struct numeral... ie 0, 1, 2 each get a
50517c478bd9Sstevel@tonic-gate  * single struct numeral. There is no need to store more than a single logical
50527c478bd9Sstevel@tonic-gate  * node matching X since the information desired in the devfspath would be
50537c478bd9Sstevel@tonic-gate  * identical for the portion of the devfspath of interest. (the part up to,
50547c478bd9Sstevel@tonic-gate  * but not including the minor name in this example.)
50557c478bd9Sstevel@tonic-gate  *
50567c478bd9Sstevel@tonic-gate  * If the given numeral_set is not yet cached, call enumerate_recurse to
50577c478bd9Sstevel@tonic-gate  * create it.
50587c478bd9Sstevel@tonic-gate  */
50597c478bd9Sstevel@tonic-gate static numeral_set_t *
50607c478bd9Sstevel@tonic-gate get_enum_cache(devfsadm_enumerate_t rules[], int nrules)
50617c478bd9Sstevel@tonic-gate {
50627c478bd9Sstevel@tonic-gate 	/* linked list of numeral sets */
50637c478bd9Sstevel@tonic-gate 	numeral_set_t *setp;
50647c478bd9Sstevel@tonic-gate 	int i;
50658d483882Smlf 	int ret;
50667c478bd9Sstevel@tonic-gate 	char *path_left;
50678d483882Smlf 	enumerate_file_t *entry;
50687c478bd9Sstevel@tonic-gate 	char *fcn = "get_enum_cache";
50697c478bd9Sstevel@tonic-gate 
50707c478bd9Sstevel@tonic-gate 	/*
50717c478bd9Sstevel@tonic-gate 	 * See if we've already cached this numeral set.
50727c478bd9Sstevel@tonic-gate 	 */
50737c478bd9Sstevel@tonic-gate 	for (setp = head_numeral_set; setp != NULL; setp = setp->next) {
50747c478bd9Sstevel@tonic-gate 		/*
50757c478bd9Sstevel@tonic-gate 		 *  check all regexp's passed in function against
50767c478bd9Sstevel@tonic-gate 		 *  those in cached set.
50777c478bd9Sstevel@tonic-gate 		 */
50787c478bd9Sstevel@tonic-gate 		if (nrules != setp->re_count) {
50797c478bd9Sstevel@tonic-gate 			continue;
50807c478bd9Sstevel@tonic-gate 		}
50817c478bd9Sstevel@tonic-gate 
50827c478bd9Sstevel@tonic-gate 		for (i = 0; i < nrules; i++) {
50837c478bd9Sstevel@tonic-gate 			if (strcmp(setp->re[i], rules[i].re) != 0) {
50847c478bd9Sstevel@tonic-gate 				break;
50857c478bd9Sstevel@tonic-gate 			}
50867c478bd9Sstevel@tonic-gate 		}
50877c478bd9Sstevel@tonic-gate 
50887c478bd9Sstevel@tonic-gate 		if (i == nrules) {
50897c478bd9Sstevel@tonic-gate 			return (setp);
50907c478bd9Sstevel@tonic-gate 		}
50917c478bd9Sstevel@tonic-gate 	}
50927c478bd9Sstevel@tonic-gate 
50937c478bd9Sstevel@tonic-gate 	/*
50947c478bd9Sstevel@tonic-gate 	 * If the MATCH_UNCACHED flag is set, we should not  be here.
50957c478bd9Sstevel@tonic-gate 	 */
50967c478bd9Sstevel@tonic-gate 	for (i = 0; i < nrules; i++) {
50977c478bd9Sstevel@tonic-gate 		if ((rules[i].flags & MATCH_UNCACHED) == MATCH_UNCACHED) {
50987c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid enumeration flags: "
50997c478bd9Sstevel@tonic-gate 			    "0x%x\n", fcn, rules[i].flags);
51007c478bd9Sstevel@tonic-gate 			return (NULL);
51017c478bd9Sstevel@tonic-gate 		}
51027c478bd9Sstevel@tonic-gate 	}
51037c478bd9Sstevel@tonic-gate 
51047c478bd9Sstevel@tonic-gate 	/*
51057c478bd9Sstevel@tonic-gate 	 *  Since we made it here, we have not yet cached the given set of
51067c478bd9Sstevel@tonic-gate 	 *  logical nodes matching the passed re.  Create a cached entry
51077c478bd9Sstevel@tonic-gate 	 *  struct numeral_set and populate it with a minimal set of
51087c478bd9Sstevel@tonic-gate 	 *  logical nodes from /dev.
51097c478bd9Sstevel@tonic-gate 	 */
51107c478bd9Sstevel@tonic-gate 
51117c478bd9Sstevel@tonic-gate 	setp = s_malloc(sizeof (numeral_set_t));
51127c478bd9Sstevel@tonic-gate 	setp->re = s_malloc(sizeof (char *) * nrules);
51137c478bd9Sstevel@tonic-gate 	for (i = 0; i < nrules; i++) {
51147c478bd9Sstevel@tonic-gate 		setp->re[i] = s_strdup(rules[i].re);
51157c478bd9Sstevel@tonic-gate 	}
51167c478bd9Sstevel@tonic-gate 	setp->re_count = nrules;
51177c478bd9Sstevel@tonic-gate 	setp->headnumeral = NULL;
51187c478bd9Sstevel@tonic-gate 
51197c478bd9Sstevel@tonic-gate 	/* put this new cached set on the cached set list */
51207c478bd9Sstevel@tonic-gate 	setp->next = head_numeral_set;
51217c478bd9Sstevel@tonic-gate 	head_numeral_set = setp;
51227c478bd9Sstevel@tonic-gate 
51238d483882Smlf 	/*
51248d483882Smlf 	 * For each RE, search the "reserved" list to create numeral IDs that
51258d483882Smlf 	 * are reserved.
51268d483882Smlf 	 */
51278d483882Smlf 	for (entry = enumerate_reserved; entry; entry = entry->er_next) {
51288d483882Smlf 
51298d483882Smlf 		vprint(RSRV_MID, "parsing rstring: %s\n", entry->er_file);
51308d483882Smlf 
51318d483882Smlf 		for (i = 0; i < nrules; i++) {
51328d483882Smlf 			path_left = s_strdup(setp->re[i]);
51338d483882Smlf 			vprint(RSRV_MID, "parsing rule RE: %s\n", path_left);
51348d483882Smlf 			ret = enumerate_parse(entry->er_file, path_left,
51358d483882Smlf 			    setp, rules, i);
51368d483882Smlf 			free(path_left);
51378d483882Smlf 			if (ret == 1) {
51388d483882Smlf 				/*
51398d483882Smlf 				 * We found the reserved ID for this entry.
51408d483882Smlf 				 * We still keep the entry since it is needed
51418d483882Smlf 				 * by the new link bypass code in disks
51428d483882Smlf 				 */
51438d483882Smlf 				vprint(RSRV_MID, "found rsv ID: rstring: %s "
51448d483882Smlf 				    "rule RE: %s\n", entry->er_file, path_left);
51458d483882Smlf 				break;
51468d483882Smlf 			}
51478d483882Smlf 		}
51488d483882Smlf 	}
51498d483882Smlf 
51507c478bd9Sstevel@tonic-gate 	/*
51517c478bd9Sstevel@tonic-gate 	 * For each RE, search disk and cache any matches on the
5152facf4a8dSllai 	 * numeral list.
51537c478bd9Sstevel@tonic-gate 	 */
51547c478bd9Sstevel@tonic-gate 	for (i = 0; i < nrules; i++) {
51557c478bd9Sstevel@tonic-gate 		path_left = s_strdup(setp->re[i]);
5156facf4a8dSllai 		enumerate_recurse(dev_dir, path_left, setp, rules, i);
51577c478bd9Sstevel@tonic-gate 		free(path_left);
51587c478bd9Sstevel@tonic-gate 	}
51597c478bd9Sstevel@tonic-gate 
51607c478bd9Sstevel@tonic-gate #ifdef	DEBUG
51617c478bd9Sstevel@tonic-gate 	dump_enum_cache(setp);
51627c478bd9Sstevel@tonic-gate #endif
51637c478bd9Sstevel@tonic-gate 
51647c478bd9Sstevel@tonic-gate 	return (setp);
51657c478bd9Sstevel@tonic-gate }
51667c478bd9Sstevel@tonic-gate 
51677c478bd9Sstevel@tonic-gate 
51687c478bd9Sstevel@tonic-gate /*
51697c478bd9Sstevel@tonic-gate  * This function stats the pathname namebuf.  If this is a directory
51707c478bd9Sstevel@tonic-gate  * entry, we recurse down dname/fname until we find the first symbolic
51717c478bd9Sstevel@tonic-gate  * link, and then stat and return it.  This is valid for the same reason
51727c478bd9Sstevel@tonic-gate  * that we only need to read a single pathname for multiple matching
51737c478bd9Sstevel@tonic-gate  * logical ID's... ie, all the logical nodes should contain identical
51747c478bd9Sstevel@tonic-gate  * physical paths for the parts we are interested.
51757c478bd9Sstevel@tonic-gate  */
51767c478bd9Sstevel@tonic-gate int
51777c478bd9Sstevel@tonic-gate get_stat_info(char *namebuf, struct stat *sb)
51787c478bd9Sstevel@tonic-gate {
51797c478bd9Sstevel@tonic-gate 	char *cp;
518073de625bSjg 	finddevhdl_t fhandle;
518173de625bSjg 	const char *fp;
51827c478bd9Sstevel@tonic-gate 
51837c478bd9Sstevel@tonic-gate 	if (lstat(namebuf, sb) < 0) {
51847c478bd9Sstevel@tonic-gate 		(void) err_print(LSTAT_FAILED, namebuf, strerror(errno));
51857c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
51867c478bd9Sstevel@tonic-gate 	}
51877c478bd9Sstevel@tonic-gate 
51887c478bd9Sstevel@tonic-gate 	if ((sb->st_mode & S_IFMT) == S_IFLNK) {
51897c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
51907c478bd9Sstevel@tonic-gate 	}
51917c478bd9Sstevel@tonic-gate 
51927c478bd9Sstevel@tonic-gate 	/*
51937c478bd9Sstevel@tonic-gate 	 * If it is a dir, recurse down until we find a link and
51947c478bd9Sstevel@tonic-gate 	 * then use the link.
51957c478bd9Sstevel@tonic-gate 	 */
51967c478bd9Sstevel@tonic-gate 	if ((sb->st_mode & S_IFMT) == S_IFDIR) {
51977c478bd9Sstevel@tonic-gate 
519873de625bSjg 		if (finddev_readdir(namebuf, &fhandle) != 0) {
51997c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
52007c478bd9Sstevel@tonic-gate 		}
52017c478bd9Sstevel@tonic-gate 
52027c478bd9Sstevel@tonic-gate 		/*
52037c478bd9Sstevel@tonic-gate 		 *  Search each dir entry looking for a symlink.  Return
52047c478bd9Sstevel@tonic-gate 		 *  the first symlink found in namebuf.  Recurse dirs.
52057c478bd9Sstevel@tonic-gate 		 */
520673de625bSjg 		while ((fp = finddev_next(fhandle)) != NULL) {
52077c478bd9Sstevel@tonic-gate 			cp = namebuf + strlen(namebuf);
5208facf4a8dSllai 			if ((strlcat(namebuf, "/", PATH_MAX) >= PATH_MAX) ||
520973de625bSjg 			    (strlcat(namebuf, fp, PATH_MAX) >= PATH_MAX)) {
5210facf4a8dSllai 				*cp = '\0';
521173de625bSjg 				finddev_close(fhandle);
5212facf4a8dSllai 				return (DEVFSADM_FAILURE);
5213facf4a8dSllai 			}
52147c478bd9Sstevel@tonic-gate 			if (get_stat_info(namebuf, sb) == DEVFSADM_SUCCESS) {
521573de625bSjg 				finddev_close(fhandle);
52167c478bd9Sstevel@tonic-gate 				return (DEVFSADM_SUCCESS);
52177c478bd9Sstevel@tonic-gate 			}
52187c478bd9Sstevel@tonic-gate 			*cp = '\0';
52197c478bd9Sstevel@tonic-gate 		}
522073de625bSjg 		finddev_close(fhandle);
52217c478bd9Sstevel@tonic-gate 	}
52227c478bd9Sstevel@tonic-gate 
52237c478bd9Sstevel@tonic-gate 	/* no symlink found, so return error */
52247c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
52257c478bd9Sstevel@tonic-gate }
52267c478bd9Sstevel@tonic-gate 
52277c478bd9Sstevel@tonic-gate /*
52287c478bd9Sstevel@tonic-gate  * An existing matching ID was not found, so this function is called to
52297c478bd9Sstevel@tonic-gate  * create the next lowest ID.  In the INTEGER case, return the next
52307c478bd9Sstevel@tonic-gate  * lowest unused integer.  In the case of LETTER, return the next lowest
52317c478bd9Sstevel@tonic-gate  * unused letter.  Return empty string if all 26 are used.
52327c478bd9Sstevel@tonic-gate  * Only IDs >= min will be returned.
52337c478bd9Sstevel@tonic-gate  */
52347c478bd9Sstevel@tonic-gate char *
52357c478bd9Sstevel@tonic-gate new_id(numeral_t *numeral, int type, char *min)
52367c478bd9Sstevel@tonic-gate {
52377c478bd9Sstevel@tonic-gate 	int imin;
52387c478bd9Sstevel@tonic-gate 	temp_t *temp;
52397c478bd9Sstevel@tonic-gate 	temp_t *ptr;
52407c478bd9Sstevel@tonic-gate 	temp_t **previous;
52417c478bd9Sstevel@tonic-gate 	temp_t *head = NULL;
52427c478bd9Sstevel@tonic-gate 	char *retval;
52437c478bd9Sstevel@tonic-gate 	static char tempbuff[8];
52447c478bd9Sstevel@tonic-gate 	numeral_t *np;
52457c478bd9Sstevel@tonic-gate 
52467c478bd9Sstevel@tonic-gate 	if (type == LETTER) {
52477c478bd9Sstevel@tonic-gate 
52487c478bd9Sstevel@tonic-gate 		char letter[26], i;
52497c478bd9Sstevel@tonic-gate 
52507c478bd9Sstevel@tonic-gate 		if (numeral == NULL) {
52517c478bd9Sstevel@tonic-gate 			return (s_strdup(min));
52527c478bd9Sstevel@tonic-gate 		}
52537c478bd9Sstevel@tonic-gate 
52547c478bd9Sstevel@tonic-gate 		for (i = 0; i < 26; i++) {
52557c478bd9Sstevel@tonic-gate 			letter[i] = 0;
52567c478bd9Sstevel@tonic-gate 		}
52577c478bd9Sstevel@tonic-gate 
52587c478bd9Sstevel@tonic-gate 		for (np = numeral; np != NULL; np = np->next) {
52598d483882Smlf 			assert(np->flags == 0 ||
52608d483882Smlf 			    np->flags == NUMERAL_RESERVED);
52617c478bd9Sstevel@tonic-gate 			letter[*np->id - 'a']++;
52627c478bd9Sstevel@tonic-gate 		}
52637c478bd9Sstevel@tonic-gate 
52647c478bd9Sstevel@tonic-gate 		imin = *min - 'a';
52657c478bd9Sstevel@tonic-gate 
52667c478bd9Sstevel@tonic-gate 		for (i = imin; i < 26; i++) {
52677c478bd9Sstevel@tonic-gate 			if (letter[i] == 0) {
52687c478bd9Sstevel@tonic-gate 				retval = s_malloc(2);
52697c478bd9Sstevel@tonic-gate 				retval[0] = 'a' + i;
52707c478bd9Sstevel@tonic-gate 				retval[1] = '\0';
52717c478bd9Sstevel@tonic-gate 				return (retval);
52727c478bd9Sstevel@tonic-gate 			}
52737c478bd9Sstevel@tonic-gate 		}
52747c478bd9Sstevel@tonic-gate 
52757c478bd9Sstevel@tonic-gate 		return (s_strdup(""));
52767c478bd9Sstevel@tonic-gate 	}
52777c478bd9Sstevel@tonic-gate 
52787c478bd9Sstevel@tonic-gate 	if (type == INTEGER) {
52797c478bd9Sstevel@tonic-gate 
52807c478bd9Sstevel@tonic-gate 		if (numeral == NULL) {
52817c478bd9Sstevel@tonic-gate 			return (s_strdup(min));
52827c478bd9Sstevel@tonic-gate 		}
52837c478bd9Sstevel@tonic-gate 
52847c478bd9Sstevel@tonic-gate 		imin = atoi(min);
52857c478bd9Sstevel@tonic-gate 
52867c478bd9Sstevel@tonic-gate 		/* sort list */
52877c478bd9Sstevel@tonic-gate 		for (np = numeral; np != NULL; np = np->next) {
52888d483882Smlf 			assert(np->flags == 0 ||
52898d483882Smlf 			    np->flags == NUMERAL_RESERVED);
52907c478bd9Sstevel@tonic-gate 			temp = s_malloc(sizeof (temp_t));
52917c478bd9Sstevel@tonic-gate 			temp->integer = atoi(np->id);
52927c478bd9Sstevel@tonic-gate 			temp->next = NULL;
52937c478bd9Sstevel@tonic-gate 
52947c478bd9Sstevel@tonic-gate 			previous = &head;
52957c478bd9Sstevel@tonic-gate 			for (ptr = head; ptr != NULL; ptr = ptr->next) {
52967c478bd9Sstevel@tonic-gate 				if (temp->integer < ptr->integer) {
52977c478bd9Sstevel@tonic-gate 					temp->next = ptr;
52987c478bd9Sstevel@tonic-gate 					*previous = temp;
52997c478bd9Sstevel@tonic-gate 					break;
53007c478bd9Sstevel@tonic-gate 				}
53017c478bd9Sstevel@tonic-gate 				previous = &(ptr->next);
53027c478bd9Sstevel@tonic-gate 			}
53037c478bd9Sstevel@tonic-gate 			if (ptr == NULL) {
53047c478bd9Sstevel@tonic-gate 				*previous = temp;
53057c478bd9Sstevel@tonic-gate 			}
53067c478bd9Sstevel@tonic-gate 		}
53077c478bd9Sstevel@tonic-gate 
53087c478bd9Sstevel@tonic-gate 		/* now search sorted list for first hole >= imin */
53097c478bd9Sstevel@tonic-gate 		for (ptr = head; ptr != NULL; ptr = ptr->next) {
53107c478bd9Sstevel@tonic-gate 			if (imin == ptr->integer) {
53117c478bd9Sstevel@tonic-gate 				imin++;
53127c478bd9Sstevel@tonic-gate 			} else {
53137c478bd9Sstevel@tonic-gate 				if (imin < ptr->integer) {
53147c478bd9Sstevel@tonic-gate 					break;
53157c478bd9Sstevel@tonic-gate 				}
53167c478bd9Sstevel@tonic-gate 			}
53177c478bd9Sstevel@tonic-gate 
53187c478bd9Sstevel@tonic-gate 		}
53197c478bd9Sstevel@tonic-gate 
53207c478bd9Sstevel@tonic-gate 		/* free temp list */
53217c478bd9Sstevel@tonic-gate 		for (ptr = head; ptr != NULL; ) {
53227c478bd9Sstevel@tonic-gate 			temp = ptr;
53237c478bd9Sstevel@tonic-gate 			ptr = ptr->next;
53247c478bd9Sstevel@tonic-gate 			free(temp);
53257c478bd9Sstevel@tonic-gate 		}
53267c478bd9Sstevel@tonic-gate 
53277c478bd9Sstevel@tonic-gate 		(void) sprintf(tempbuff, "%d", imin);
53287c478bd9Sstevel@tonic-gate 		return (s_strdup(tempbuff));
53297c478bd9Sstevel@tonic-gate 	}
53307c478bd9Sstevel@tonic-gate 
53317c478bd9Sstevel@tonic-gate 	return (s_strdup(""));
53327c478bd9Sstevel@tonic-gate }
53337c478bd9Sstevel@tonic-gate 
53348d483882Smlf static int
53358d483882Smlf enumerate_parse(char *rsvstr, char *path_left, numeral_set_t *setp,
53368d483882Smlf 	    devfsadm_enumerate_t rules[], int index)
53378d483882Smlf {
53388d483882Smlf 	char	*slash1 = NULL;
53398d483882Smlf 	char	*slash2 = NULL;
53408d483882Smlf 	char	*numeral_id;
53418d483882Smlf 	char	*path_left_save;
53428d483882Smlf 	char	*rsvstr_save;
53438d483882Smlf 	int	ret = 0;
53448d483882Smlf 	static int warned = 0;
53458d483882Smlf 
53468d483882Smlf 	rsvstr_save = rsvstr;
53478d483882Smlf 	path_left_save = path_left;
53488d483882Smlf 
53498d483882Smlf 	if (rsvstr == NULL || rsvstr[0] == '\0' || rsvstr[0] == '/') {
53508d483882Smlf 		if (!warned) {
53518d483882Smlf 			err_print("invalid reserved filepath: %s\n",
53528d483882Smlf 			    rsvstr ? rsvstr : "<NULL>");
53538d483882Smlf 			warned = 1;
53548d483882Smlf 		}
53558d483882Smlf 		return (0);
53568d483882Smlf 	}
53578d483882Smlf 
53588d483882Smlf 	vprint(RSRV_MID, "processing rule: %s, rstring: %s\n",
53598d483882Smlf 	    path_left, rsvstr);
53608d483882Smlf 
53618d483882Smlf 
53628d483882Smlf 	for (;;) {
53638d483882Smlf 		/* get rid of any extra '/' in the reserve string */
53648d483882Smlf 		while (*rsvstr == '/') {
53658d483882Smlf 			rsvstr++;
53668d483882Smlf 		}
53678d483882Smlf 
53688d483882Smlf 		/* get rid of any extra '/' in the RE */
53698d483882Smlf 		while (*path_left == '/') {
53708d483882Smlf 			path_left++;
53718d483882Smlf 		}
53728d483882Smlf 
53738d483882Smlf 		if (slash1 = strchr(path_left, '/')) {
53748d483882Smlf 			*slash1 = '\0';
53758d483882Smlf 		}
53768d483882Smlf 		if (slash2 = strchr(rsvstr, '/')) {
53778d483882Smlf 			*slash2 = '\0';
53788d483882Smlf 		}
53798d483882Smlf 
53808d483882Smlf 		if ((slash1 != NULL) ^ (slash2 != NULL)) {
53818d483882Smlf 			ret = 0;
53828d483882Smlf 			vprint(RSRV_MID, "mismatch in # of path components\n");
53838d483882Smlf 			goto out;
53848d483882Smlf 		}
53858d483882Smlf 
53868d483882Smlf 		/*
53878d483882Smlf 		 *  Returns true if path_left matches the list entry.
53888d483882Smlf 		 *  If it is the last path component, pass subexp
53898d483882Smlf 		 *  so that it will return the corresponding ID in
53908d483882Smlf 		 *  numeral_id.
53918d483882Smlf 		 */
53928d483882Smlf 		numeral_id = NULL;
53938d483882Smlf 		if (match_path_component(path_left, rsvstr, &numeral_id,
53940a653502Swroche 		    slash1 ? 0 : rules[index].subexp)) {
53958d483882Smlf 
53968d483882Smlf 			/* We have a match. */
53978d483882Smlf 			if (slash1 == NULL) {
53988d483882Smlf 				/* Is last path component */
53998d483882Smlf 				vprint(RSRV_MID, "match and last component\n");
54008d483882Smlf 				create_reserved_numeral(setp, numeral_id);
54018d483882Smlf 				if (numeral_id != NULL) {
54028d483882Smlf 					free(numeral_id);
54038d483882Smlf 				}
54048d483882Smlf 				ret = 1;
54058d483882Smlf 				goto out;
54068d483882Smlf 			} else {
54078d483882Smlf 				/* Not last path component. Continue parsing */
54088d483882Smlf 				*slash1 = '/';
54098d483882Smlf 				*slash2 = '/';
54108d483882Smlf 				path_left = slash1 + 1;
54118d483882Smlf 				rsvstr = slash2 + 1;
54128d483882Smlf 				vprint(RSRV_MID,
54138d483882Smlf 				    "match and NOT last component\n");
54148d483882Smlf 				continue;
54158d483882Smlf 			}
54168d483882Smlf 		} else {
54178d483882Smlf 			/* No match */
54188d483882Smlf 			ret = 0;
54198d483882Smlf 			vprint(RSRV_MID, "No match: rule RE = %s, "
54208d483882Smlf 			    "rstring = %s\n", path_left, rsvstr);
54218d483882Smlf 			goto out;
54228d483882Smlf 		}
54238d483882Smlf 	}
54248d483882Smlf 
54258d483882Smlf out:
54268d483882Smlf 	if (slash1)
54278d483882Smlf 		*slash1 = '/';
54288d483882Smlf 	if (slash2)
54298d483882Smlf 		*slash2 = '/';
54308d483882Smlf 
54318d483882Smlf 	if (ret == 1) {
54328d483882Smlf 		vprint(RSRV_MID, "match: rule RE: %s, rstring: %s\n",
54338d483882Smlf 		    path_left_save, rsvstr_save);
54348d483882Smlf 	} else {
54358d483882Smlf 		vprint(RSRV_MID, "NO match: rule RE: %s, rstring: %s\n",
54368d483882Smlf 		    path_left_save, rsvstr_save);
54378d483882Smlf 	}
54388d483882Smlf 
54398d483882Smlf 	return (ret);
54408d483882Smlf }
54418d483882Smlf 
54427c478bd9Sstevel@tonic-gate /*
54437c478bd9Sstevel@tonic-gate  * Search current_dir for all files which match the first path component
54447c478bd9Sstevel@tonic-gate  * of path_left, which is an RE.  If a match is found, but there are more
54457c478bd9Sstevel@tonic-gate  * components of path_left, then recurse, otherwise, if we have reached
54467c478bd9Sstevel@tonic-gate  * the last component of path_left, call create_cached_numerals for each
54477c478bd9Sstevel@tonic-gate  * file.   At some point, recurse_dev_re() should be rewritten so that this
54487c478bd9Sstevel@tonic-gate  * function can be eliminated.
54497c478bd9Sstevel@tonic-gate  */
54507c478bd9Sstevel@tonic-gate static void
54517c478bd9Sstevel@tonic-gate enumerate_recurse(char *current_dir, char *path_left, numeral_set_t *setp,
54527c478bd9Sstevel@tonic-gate 	    devfsadm_enumerate_t rules[], int index)
54537c478bd9Sstevel@tonic-gate {
54547c478bd9Sstevel@tonic-gate 	char *slash;
54557c478bd9Sstevel@tonic-gate 	char *new_path;
54567c478bd9Sstevel@tonic-gate 	char *numeral_id;
545773de625bSjg 	finddevhdl_t fhandle;
545873de625bSjg 	const char *fp;
54597c478bd9Sstevel@tonic-gate 
546073de625bSjg 	if (finddev_readdir(current_dir, &fhandle) != 0) {
54617c478bd9Sstevel@tonic-gate 		return;
54627c478bd9Sstevel@tonic-gate 	}
54637c478bd9Sstevel@tonic-gate 
54647c478bd9Sstevel@tonic-gate 	/* get rid of any extra '/' */
54657c478bd9Sstevel@tonic-gate 	while (*path_left == '/') {
54667c478bd9Sstevel@tonic-gate 		path_left++;
54677c478bd9Sstevel@tonic-gate 	}
54687c478bd9Sstevel@tonic-gate 
54697c478bd9Sstevel@tonic-gate 	if (slash = strchr(path_left, '/')) {
54707c478bd9Sstevel@tonic-gate 		*slash = '\0';
54717c478bd9Sstevel@tonic-gate 	}
54727c478bd9Sstevel@tonic-gate 
547373de625bSjg 	while ((fp = finddev_next(fhandle)) != NULL) {
54747c478bd9Sstevel@tonic-gate 
54757c478bd9Sstevel@tonic-gate 		/*
5476facf4a8dSllai 		 *  Returns true if path_left matches the list entry.
54777c478bd9Sstevel@tonic-gate 		 *  If it is the last path component, pass subexp
54787c478bd9Sstevel@tonic-gate 		 *  so that it will return the corresponding ID in
54797c478bd9Sstevel@tonic-gate 		 *  numeral_id.
54807c478bd9Sstevel@tonic-gate 		 */
54817c478bd9Sstevel@tonic-gate 		numeral_id = NULL;
548273de625bSjg 		if (match_path_component(path_left, (char *)fp, &numeral_id,
54830a653502Swroche 		    slash ? 0 : rules[index].subexp)) {
54847c478bd9Sstevel@tonic-gate 
54857c478bd9Sstevel@tonic-gate 			new_path = s_malloc(strlen(current_dir) +
548673de625bSjg 			    strlen(fp) + 2);
54877c478bd9Sstevel@tonic-gate 
54887c478bd9Sstevel@tonic-gate 			(void) strcpy(new_path, current_dir);
54897c478bd9Sstevel@tonic-gate 			(void) strcat(new_path, "/");
549073de625bSjg 			(void) strcat(new_path, fp);
54917c478bd9Sstevel@tonic-gate 
54927c478bd9Sstevel@tonic-gate 			if (slash != NULL) {
54937c478bd9Sstevel@tonic-gate 				enumerate_recurse(new_path, slash + 1,
54947c478bd9Sstevel@tonic-gate 				    setp, rules, index);
54957c478bd9Sstevel@tonic-gate 			} else {
54967c478bd9Sstevel@tonic-gate 				create_cached_numeral(new_path, setp,
54977c478bd9Sstevel@tonic-gate 				    numeral_id, rules, index);
54987c478bd9Sstevel@tonic-gate 				if (numeral_id != NULL) {
54997c478bd9Sstevel@tonic-gate 					free(numeral_id);
55007c478bd9Sstevel@tonic-gate 				}
55017c478bd9Sstevel@tonic-gate 			}
55027c478bd9Sstevel@tonic-gate 			free(new_path);
55037c478bd9Sstevel@tonic-gate 		}
55047c478bd9Sstevel@tonic-gate 	}
55057c478bd9Sstevel@tonic-gate 
55067c478bd9Sstevel@tonic-gate 	if (slash != NULL) {
55077c478bd9Sstevel@tonic-gate 		*slash = '/';
55087c478bd9Sstevel@tonic-gate 	}
550973de625bSjg 	finddev_close(fhandle);
55107c478bd9Sstevel@tonic-gate }
55117c478bd9Sstevel@tonic-gate 
55127c478bd9Sstevel@tonic-gate 
55137c478bd9Sstevel@tonic-gate /*
55147c478bd9Sstevel@tonic-gate  * Returns true if file matches file_re.  If subexp is non-zero, it means
55157c478bd9Sstevel@tonic-gate  * we are searching the last path component and need to return the
55167c478bd9Sstevel@tonic-gate  * parenthesized subexpression subexp in id.
55177c478bd9Sstevel@tonic-gate  *
55187c478bd9Sstevel@tonic-gate  */
55197c478bd9Sstevel@tonic-gate static int
55207c478bd9Sstevel@tonic-gate match_path_component(char *file_re,  char *file,  char **id, int subexp)
55217c478bd9Sstevel@tonic-gate {
55227c478bd9Sstevel@tonic-gate 	regex_t re1;
55237c478bd9Sstevel@tonic-gate 	int match = 0;
55247c478bd9Sstevel@tonic-gate 	int nelements;
55257c478bd9Sstevel@tonic-gate 	regmatch_t *pmatch;
55267c478bd9Sstevel@tonic-gate 
55277c478bd9Sstevel@tonic-gate 	if (subexp != 0) {
55287c478bd9Sstevel@tonic-gate 		nelements = subexp + 1;
55290a653502Swroche 		pmatch =
55300a653502Swroche 		    (regmatch_t *)s_malloc(sizeof (regmatch_t) * nelements);
55317c478bd9Sstevel@tonic-gate 	} else {
55327c478bd9Sstevel@tonic-gate 		pmatch = NULL;
55337c478bd9Sstevel@tonic-gate 		nelements = 0;
55347c478bd9Sstevel@tonic-gate 	}
55357c478bd9Sstevel@tonic-gate 
55367c478bd9Sstevel@tonic-gate 	if (regcomp(&re1, file_re, REG_EXTENDED) != 0) {
55377c478bd9Sstevel@tonic-gate 		if (pmatch != NULL) {
55387c478bd9Sstevel@tonic-gate 			free(pmatch);
55397c478bd9Sstevel@tonic-gate 		}
55407c478bd9Sstevel@tonic-gate 		return (0);
55417c478bd9Sstevel@tonic-gate 	}
55427c478bd9Sstevel@tonic-gate 
55437c478bd9Sstevel@tonic-gate 	if (regexec(&re1, file, nelements, pmatch, 0) == 0) {
55447c478bd9Sstevel@tonic-gate 		match = 1;
55457c478bd9Sstevel@tonic-gate 	}
55467c478bd9Sstevel@tonic-gate 
55477c478bd9Sstevel@tonic-gate 	if ((match != 0) && (subexp != 0)) {
55487c478bd9Sstevel@tonic-gate 		int size = pmatch[subexp].rm_eo - pmatch[subexp].rm_so;
55497c478bd9Sstevel@tonic-gate 		*id = s_malloc(size + 1);
55507c478bd9Sstevel@tonic-gate 		(void) strncpy(*id, &file[pmatch[subexp].rm_so], size);
55517c478bd9Sstevel@tonic-gate 		(*id)[size] = '\0';
55527c478bd9Sstevel@tonic-gate 	}
55537c478bd9Sstevel@tonic-gate 
55547c478bd9Sstevel@tonic-gate 	if (pmatch != NULL) {
55557c478bd9Sstevel@tonic-gate 		free(pmatch);
55567c478bd9Sstevel@tonic-gate 	}
55577c478bd9Sstevel@tonic-gate 	regfree(&re1);
55587c478bd9Sstevel@tonic-gate 	return (match);
55597c478bd9Sstevel@tonic-gate }
55607c478bd9Sstevel@tonic-gate 
55618d483882Smlf static void
55628d483882Smlf create_reserved_numeral(numeral_set_t *setp, char *numeral_id)
55638d483882Smlf {
55648d483882Smlf 	numeral_t *np;
55658d483882Smlf 
55668d483882Smlf 	vprint(RSRV_MID, "Attempting to create reserved numeral: %s\n",
55678d483882Smlf 	    numeral_id);
55688d483882Smlf 
55698d483882Smlf 	/*
55708d483882Smlf 	 * We found a numeral_id from an entry in the enumerate_reserved file
55718d483882Smlf 	 * which matched the re passed in from devfsadm_enumerate.  We only
55728d483882Smlf 	 * need to make sure ONE copy of numeral_id exists on the numeral list.
55738d483882Smlf 	 * We only need to store /dev/dsk/cNtod0s0 and no other entries
55748d483882Smlf 	 * hanging off of controller N.
55758d483882Smlf 	 */
55768d483882Smlf 	for (np = setp->headnumeral; np != NULL; np = np->next) {
55778d483882Smlf 		if (strcmp(numeral_id, np->id) == 0) {
55788d483882Smlf 			vprint(RSRV_MID, "ID: %s, already reserved\n", np->id);
55798d483882Smlf 			assert(np->flags == NUMERAL_RESERVED);
55808d483882Smlf 			return;
55818d483882Smlf 		} else {
55828d483882Smlf 			assert(np->flags == 0 ||
55838d483882Smlf 			    np->flags == NUMERAL_RESERVED);
55848d483882Smlf 		}
55858d483882Smlf 	}
55868d483882Smlf 
55878d483882Smlf 	/* NOT on list, so add it */
55888d483882Smlf 	np = s_malloc(sizeof (numeral_t));
55898d483882Smlf 	np->id = s_strdup(numeral_id);
55908d483882Smlf 	np->full_path = NULL;
55918d483882Smlf 	np->rule_index = 0;
55928d483882Smlf 	np->cmp_str = NULL;
55938d483882Smlf 	np->flags = NUMERAL_RESERVED;
55948d483882Smlf 	np->next = setp->headnumeral;
55958d483882Smlf 	setp->headnumeral = np;
55968d483882Smlf 
55978d483882Smlf 	vprint(RSRV_MID, "Reserved numeral ID: %s\n", np->id);
55988d483882Smlf }
55998d483882Smlf 
56007c478bd9Sstevel@tonic-gate /*
56017c478bd9Sstevel@tonic-gate  * This function is called for every file which matched the leaf
56027c478bd9Sstevel@tonic-gate  * component of the RE.  If the "numeral_id" is not already on the
56037c478bd9Sstevel@tonic-gate  * numeral set's numeral list, add it and its physical path.
56047c478bd9Sstevel@tonic-gate  */
56057c478bd9Sstevel@tonic-gate static void
56067c478bd9Sstevel@tonic-gate create_cached_numeral(char *path, numeral_set_t *setp, char *numeral_id,
56077c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[], int index)
56087c478bd9Sstevel@tonic-gate {
56097c478bd9Sstevel@tonic-gate 	char linkbuf[PATH_MAX + 1];
56107c478bd9Sstevel@tonic-gate 	char lpath[PATH_MAX + 1];
56117c478bd9Sstevel@tonic-gate 	char *linkptr, *cmp_str;
56127c478bd9Sstevel@tonic-gate 	numeral_t *np;
56137c478bd9Sstevel@tonic-gate 	int linksize;
56147c478bd9Sstevel@tonic-gate 	struct stat sb;
56157c478bd9Sstevel@tonic-gate 	const char *fcn = "create_cached_numeral";
56167c478bd9Sstevel@tonic-gate 
56177c478bd9Sstevel@tonic-gate 	assert(index >= 0 && index < setp->re_count);
56187c478bd9Sstevel@tonic-gate 	assert(strcmp(rules[index].re, setp->re[index]) == 0);
56197c478bd9Sstevel@tonic-gate 
56207c478bd9Sstevel@tonic-gate 	/*
56217c478bd9Sstevel@tonic-gate 	 *  We found a numeral_id from an entry in /dev which matched
56227c478bd9Sstevel@tonic-gate 	 *  the re passed in from devfsadm_enumerate.  We only need to make sure
56237c478bd9Sstevel@tonic-gate 	 *  ONE copy of numeral_id exists on the numeral list.  We only need
56247c478bd9Sstevel@tonic-gate 	 *  to store /dev/dsk/cNtod0s0 and no other entries hanging off
56257c478bd9Sstevel@tonic-gate 	 *  of controller N.
56267c478bd9Sstevel@tonic-gate 	 */
56277c478bd9Sstevel@tonic-gate 	for (np = setp->headnumeral; np != NULL; np = np->next) {
56288d483882Smlf 		assert(np->flags == 0 || np->flags == NUMERAL_RESERVED);
56297c478bd9Sstevel@tonic-gate 		if (strcmp(numeral_id, np->id) == 0) {
56308d483882Smlf 			/*
56318d483882Smlf 			 * Note that we can't assert that the flags field
56328d483882Smlf 			 * of the numeral is 0, since both reserved and
56338d483882Smlf 			 * unreserved links in /dev come here
56348d483882Smlf 			 */
56358d483882Smlf 			if (np->flags == NUMERAL_RESERVED) {
56368d483882Smlf 				vprint(RSRV_MID, "ID derived from /dev link is"
56378d483882Smlf 				    " reserved: %s\n", np->id);
56388d483882Smlf 			} else {
56398d483882Smlf 				vprint(RSRV_MID, "ID derived from /dev link is"
56408d483882Smlf 				    " NOT reserved: %s\n", np->id);
56418d483882Smlf 			}
56427c478bd9Sstevel@tonic-gate 			return;
56437c478bd9Sstevel@tonic-gate 		}
56447c478bd9Sstevel@tonic-gate 	}
56457c478bd9Sstevel@tonic-gate 
56467c478bd9Sstevel@tonic-gate 	/* NOT on list, so add it */
56477c478bd9Sstevel@tonic-gate 
56487c478bd9Sstevel@tonic-gate 	(void) strcpy(lpath, path);
56497c478bd9Sstevel@tonic-gate 	/*
56507c478bd9Sstevel@tonic-gate 	 * If path is a dir, it is changed to the first symbolic link it find
56517c478bd9Sstevel@tonic-gate 	 * if it finds one.
56527c478bd9Sstevel@tonic-gate 	 */
56537c478bd9Sstevel@tonic-gate 	if (get_stat_info(lpath, &sb) == DEVFSADM_FAILURE) {
56547c478bd9Sstevel@tonic-gate 		return;
56557c478bd9Sstevel@tonic-gate 	}
56567c478bd9Sstevel@tonic-gate 
56577c478bd9Sstevel@tonic-gate 	/* If we get here, we found a symlink */
56587c478bd9Sstevel@tonic-gate 	linksize = readlink(lpath, linkbuf, PATH_MAX);
56597c478bd9Sstevel@tonic-gate 
56607c478bd9Sstevel@tonic-gate 	if (linksize <= 0) {
56617c478bd9Sstevel@tonic-gate 		err_print(READLINK_FAILED, fcn, lpath, strerror(errno));
56627c478bd9Sstevel@tonic-gate 		return;
56637c478bd9Sstevel@tonic-gate 	}
56647c478bd9Sstevel@tonic-gate 
56657c478bd9Sstevel@tonic-gate 	linkbuf[linksize] = '\0';
56667c478bd9Sstevel@tonic-gate 
56677c478bd9Sstevel@tonic-gate 	/*
56687c478bd9Sstevel@tonic-gate 	 * the following just points linkptr to the root of the /devices
56697c478bd9Sstevel@tonic-gate 	 * node if it is a minor node, otherwise, to the first char of
56707c478bd9Sstevel@tonic-gate 	 * linkbuf if it is a link.
56717c478bd9Sstevel@tonic-gate 	 */
56727c478bd9Sstevel@tonic-gate 	(void) is_minor_node(linkbuf, &linkptr);
56737c478bd9Sstevel@tonic-gate 
56747c478bd9Sstevel@tonic-gate 	cmp_str = alloc_cmp_str(linkptr, &rules[index]);
56757c478bd9Sstevel@tonic-gate 	if (cmp_str == NULL) {
56767c478bd9Sstevel@tonic-gate 		return;
56777c478bd9Sstevel@tonic-gate 	}
56787c478bd9Sstevel@tonic-gate 
56797c478bd9Sstevel@tonic-gate 	np = s_malloc(sizeof (numeral_t));
56807c478bd9Sstevel@tonic-gate 
56817c478bd9Sstevel@tonic-gate 	np->id = s_strdup(numeral_id);
56827c478bd9Sstevel@tonic-gate 	np->full_path = s_strdup(linkptr);
56837c478bd9Sstevel@tonic-gate 	np->rule_index = index;
56847c478bd9Sstevel@tonic-gate 	np->cmp_str = cmp_str;
56858d483882Smlf 	np->flags = 0;
56867c478bd9Sstevel@tonic-gate 
56877c478bd9Sstevel@tonic-gate 	np->next = setp->headnumeral;
56887c478bd9Sstevel@tonic-gate 	setp->headnumeral = np;
56897c478bd9Sstevel@tonic-gate }
56907c478bd9Sstevel@tonic-gate 
56917c478bd9Sstevel@tonic-gate 
56927c478bd9Sstevel@tonic-gate /*
56937c478bd9Sstevel@tonic-gate  * This should be called either before or after granting access to a
56947c478bd9Sstevel@tonic-gate  * command line version of devfsadm running, since it may have changed
56957c478bd9Sstevel@tonic-gate  * the state of /dev.  It forces future enumerate calls to re-build
56967c478bd9Sstevel@tonic-gate  * cached information from /dev.
56977c478bd9Sstevel@tonic-gate  */
56987c478bd9Sstevel@tonic-gate void
56997c478bd9Sstevel@tonic-gate invalidate_enumerate_cache(void)
57007c478bd9Sstevel@tonic-gate {
57017c478bd9Sstevel@tonic-gate 	numeral_set_t *setp;
57027c478bd9Sstevel@tonic-gate 	numeral_set_t *savedsetp;
57037c478bd9Sstevel@tonic-gate 	numeral_t *savednumset;
57047c478bd9Sstevel@tonic-gate 	numeral_t *numset;
57057c478bd9Sstevel@tonic-gate 	int i;
57067c478bd9Sstevel@tonic-gate 
57077c478bd9Sstevel@tonic-gate 	for (setp = head_numeral_set; setp != NULL; ) {
57087c478bd9Sstevel@tonic-gate 		/*
57097c478bd9Sstevel@tonic-gate 		 *  check all regexp's passed in function against
57107c478bd9Sstevel@tonic-gate 		 *  those in cached set.
57117c478bd9Sstevel@tonic-gate 		 */
57127c478bd9Sstevel@tonic-gate 
57137c478bd9Sstevel@tonic-gate 		savedsetp = setp;
57147c478bd9Sstevel@tonic-gate 		setp = setp->next;
57157c478bd9Sstevel@tonic-gate 
57167c478bd9Sstevel@tonic-gate 		for (i = 0; i < savedsetp->re_count; i++) {
57177c478bd9Sstevel@tonic-gate 			free(savedsetp->re[i]);
57187c478bd9Sstevel@tonic-gate 		}
57197c478bd9Sstevel@tonic-gate 		free(savedsetp->re);
57207c478bd9Sstevel@tonic-gate 
57217c478bd9Sstevel@tonic-gate 		for (numset = savedsetp->headnumeral; numset != NULL; ) {
57227c478bd9Sstevel@tonic-gate 			savednumset = numset;
57237c478bd9Sstevel@tonic-gate 			numset = numset->next;
57247c478bd9Sstevel@tonic-gate 			assert(savednumset->rule_index < savedsetp->re_count);
57257c478bd9Sstevel@tonic-gate 			free(savednumset->id);
57267c478bd9Sstevel@tonic-gate 			free(savednumset->full_path);
57277c478bd9Sstevel@tonic-gate 			free(savednumset->cmp_str);
57287c478bd9Sstevel@tonic-gate 			free(savednumset);
57297c478bd9Sstevel@tonic-gate 		}
57307c478bd9Sstevel@tonic-gate 		free(savedsetp);
57317c478bd9Sstevel@tonic-gate 	}
57327c478bd9Sstevel@tonic-gate 	head_numeral_set = NULL;
57337c478bd9Sstevel@tonic-gate }
57347c478bd9Sstevel@tonic-gate 
57357c478bd9Sstevel@tonic-gate /*
57367c478bd9Sstevel@tonic-gate  * Copies over links from /dev to <root>/dev and device special files in
57377c478bd9Sstevel@tonic-gate  * /devices to <root>/devices, preserving the existing file modes.  If
57387c478bd9Sstevel@tonic-gate  * the link or special file already exists on <root>, skip the copy.  (it
57397c478bd9Sstevel@tonic-gate  * would exist only if a package hard coded it there, so assume package
57407c478bd9Sstevel@tonic-gate  * knows best?).  Use /etc/name_to_major and <root>/etc/name_to_major to
57417c478bd9Sstevel@tonic-gate  * make translations for major numbers on device special files.	No need to
57427c478bd9Sstevel@tonic-gate  * make a translation on minor_perm since if the file was created in the
57437c478bd9Sstevel@tonic-gate  * miniroot then it would presumably have the same minor_perm entry in
57447c478bd9Sstevel@tonic-gate  *  <root>/etc/minor_perm.  To be used only by install.
57457c478bd9Sstevel@tonic-gate  */
57467c478bd9Sstevel@tonic-gate int
57477c478bd9Sstevel@tonic-gate devfsadm_copy(void)
57487c478bd9Sstevel@tonic-gate {
57497c478bd9Sstevel@tonic-gate 	char filename[PATH_MAX + 1];
57507c478bd9Sstevel@tonic-gate 
57517c478bd9Sstevel@tonic-gate 	/* load the installed root's name_to_major for translations */
57527c478bd9Sstevel@tonic-gate 	(void) snprintf(filename, sizeof (filename), "%s%s", root_dir,
57537c478bd9Sstevel@tonic-gate 	    NAME_TO_MAJOR);
57547c478bd9Sstevel@tonic-gate 	if (load_n2m_table(filename) == DEVFSADM_FAILURE) {
57557c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
57567c478bd9Sstevel@tonic-gate 	}
57577c478bd9Sstevel@tonic-gate 
57587c478bd9Sstevel@tonic-gate 	/* Copy /dev to target disk. No need to copy /devices with devfs */
57597c478bd9Sstevel@tonic-gate 	(void) nftw(DEV, devfsadm_copy_file, 20, FTW_PHYS);
57607c478bd9Sstevel@tonic-gate 
57617c478bd9Sstevel@tonic-gate 	/* Let install handle copying over path_to_inst */
57627c478bd9Sstevel@tonic-gate 
57637c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
57647c478bd9Sstevel@tonic-gate }
57657c478bd9Sstevel@tonic-gate 
57667c478bd9Sstevel@tonic-gate /*
57677c478bd9Sstevel@tonic-gate  * This function copies links, dirs, and device special files.
57687c478bd9Sstevel@tonic-gate  * Note that it always returns DEVFSADM_SUCCESS, so that nftw doesn't
57697c478bd9Sstevel@tonic-gate  * abort.
57707c478bd9Sstevel@tonic-gate  */
57717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
57727c478bd9Sstevel@tonic-gate static int
57737c478bd9Sstevel@tonic-gate devfsadm_copy_file(const char *file, const struct stat *stat,
57747c478bd9Sstevel@tonic-gate 		    int flags, struct FTW *ftw)
57757c478bd9Sstevel@tonic-gate {
57767c478bd9Sstevel@tonic-gate 	struct stat sp;
57777c478bd9Sstevel@tonic-gate 	dev_t newdev;
57787c478bd9Sstevel@tonic-gate 	char newfile[PATH_MAX + 1];
57797c478bd9Sstevel@tonic-gate 	char linkcontents[PATH_MAX + 1];
57807c478bd9Sstevel@tonic-gate 	int bytes;
57817c478bd9Sstevel@tonic-gate 	const char *fcn = "devfsadm_copy_file";
57827c478bd9Sstevel@tonic-gate 
57837c478bd9Sstevel@tonic-gate 	(void) strcpy(newfile, root_dir);
57847c478bd9Sstevel@tonic-gate 	(void) strcat(newfile, "/");
57857c478bd9Sstevel@tonic-gate 	(void) strcat(newfile, file);
57867c478bd9Sstevel@tonic-gate 
57877c478bd9Sstevel@tonic-gate 	if (lstat(newfile, &sp) == 0) {
57887c478bd9Sstevel@tonic-gate 		/* newfile already exists, so no need to continue */
57897c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
57907c478bd9Sstevel@tonic-gate 	}
57917c478bd9Sstevel@tonic-gate 
57927c478bd9Sstevel@tonic-gate 	if (((stat->st_mode & S_IFMT) == S_IFBLK) ||
57937c478bd9Sstevel@tonic-gate 	    ((stat->st_mode & S_IFMT) == S_IFCHR)) {
57947c478bd9Sstevel@tonic-gate 		if (translate_major(stat->st_rdev, &newdev) ==
57957c478bd9Sstevel@tonic-gate 		    DEVFSADM_FAILURE) {
57967c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
57977c478bd9Sstevel@tonic-gate 		}
57987c478bd9Sstevel@tonic-gate 		if (mknod(newfile, stat->st_mode, newdev) == -1) {
57997c478bd9Sstevel@tonic-gate 			err_print(MKNOD_FAILED, newfile, strerror(errno));
58007c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
58017c478bd9Sstevel@tonic-gate 		}
58027c478bd9Sstevel@tonic-gate 	} else if ((stat->st_mode & S_IFMT) == S_IFDIR) {
58037c478bd9Sstevel@tonic-gate 		if (mknod(newfile, stat->st_mode, 0) == -1) {
58047c478bd9Sstevel@tonic-gate 			err_print(MKNOD_FAILED, newfile, strerror(errno));
58057c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
58067c478bd9Sstevel@tonic-gate 		}
58077c478bd9Sstevel@tonic-gate 	} else if ((stat->st_mode & S_IFMT) == S_IFLNK)  {
58087c478bd9Sstevel@tonic-gate 		if ((bytes = readlink(file, linkcontents, PATH_MAX)) == -1)  {
58097c478bd9Sstevel@tonic-gate 			err_print(READLINK_FAILED, fcn, file, strerror(errno));
58107c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
58117c478bd9Sstevel@tonic-gate 		}
58127c478bd9Sstevel@tonic-gate 		linkcontents[bytes] = '\0';
58137c478bd9Sstevel@tonic-gate 		if (symlink(linkcontents, newfile) == -1) {
58147c478bd9Sstevel@tonic-gate 			err_print(SYMLINK_FAILED, newfile, newfile,
58150a653502Swroche 			    strerror(errno));
58167c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
58177c478bd9Sstevel@tonic-gate 		}
58187c478bd9Sstevel@tonic-gate 	}
58197c478bd9Sstevel@tonic-gate 
58207c478bd9Sstevel@tonic-gate 	(void) lchown(newfile, stat->st_uid, stat->st_gid);
58217c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
58227c478bd9Sstevel@tonic-gate }
58237c478bd9Sstevel@tonic-gate 
58247c478bd9Sstevel@tonic-gate /*
58257c478bd9Sstevel@tonic-gate  *  Given a dev_t from the running kernel, return the new_dev_t
58267c478bd9Sstevel@tonic-gate  *  by translating to the major number found on the installed
58277c478bd9Sstevel@tonic-gate  *  target's root name_to_major file.
58287c478bd9Sstevel@tonic-gate  */
58297c478bd9Sstevel@tonic-gate static int
58307c478bd9Sstevel@tonic-gate translate_major(dev_t old_dev, dev_t *new_dev)
58317c478bd9Sstevel@tonic-gate {
58327c478bd9Sstevel@tonic-gate 	major_t oldmajor;
58337c478bd9Sstevel@tonic-gate 	major_t newmajor;
58347c478bd9Sstevel@tonic-gate 	minor_t oldminor;
58357c478bd9Sstevel@tonic-gate 	minor_t newminor;
58367c478bd9Sstevel@tonic-gate 	char cdriver[FILENAME_MAX + 1];
58377c478bd9Sstevel@tonic-gate 	char driver[FILENAME_MAX + 1];
58387c478bd9Sstevel@tonic-gate 	char *fcn = "translate_major: ";
58397c478bd9Sstevel@tonic-gate 
58407c478bd9Sstevel@tonic-gate 	oldmajor = major(old_dev);
58410a653502Swroche 	if (modctl(MODGETNAME, driver, sizeof (driver), &oldmajor) != 0) {
58427c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
58437c478bd9Sstevel@tonic-gate 	}
58447c478bd9Sstevel@tonic-gate 
58457c478bd9Sstevel@tonic-gate 	if (strcmp(driver, "clone") != 0) {
58467c478bd9Sstevel@tonic-gate 		/* non-clone case */
58477c478bd9Sstevel@tonic-gate 
58487c478bd9Sstevel@tonic-gate 		/* look up major number is target's name2major */
58497c478bd9Sstevel@tonic-gate 		if (get_major_no(driver, &newmajor) == DEVFSADM_FAILURE) {
58507c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
58517c478bd9Sstevel@tonic-gate 		}
58527c478bd9Sstevel@tonic-gate 
58537c478bd9Sstevel@tonic-gate 		*new_dev = makedev(newmajor, minor(old_dev));
58547c478bd9Sstevel@tonic-gate 		if (old_dev != *new_dev) {
58557c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "%sdriver: %s old: %lu,%lu "
58560a653502Swroche 			    "new: %lu,%lu\n", fcn, driver, major(old_dev),
58570a653502Swroche 			    minor(old_dev), major(*new_dev), minor(*new_dev));
58587c478bd9Sstevel@tonic-gate 		}
58597c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
58607c478bd9Sstevel@tonic-gate 	} else {
58617c478bd9Sstevel@tonic-gate 		/*
58627c478bd9Sstevel@tonic-gate 		 *  The clone is a special case.  Look at its minor
58637c478bd9Sstevel@tonic-gate 		 *  number since it is the major number of the real driver.
58647c478bd9Sstevel@tonic-gate 		 */
58657c478bd9Sstevel@tonic-gate 		if (get_major_no(driver, &newmajor) == DEVFSADM_FAILURE) {
58667c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
58677c478bd9Sstevel@tonic-gate 		}
58687c478bd9Sstevel@tonic-gate 
58697c478bd9Sstevel@tonic-gate 		oldminor = minor(old_dev);
58707c478bd9Sstevel@tonic-gate 		if (modctl(MODGETNAME, cdriver, sizeof (cdriver),
58710a653502Swroche 		    &oldminor) != 0) {
58727c478bd9Sstevel@tonic-gate 			err_print(MODGETNAME_FAILED, oldminor);
58737c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
58747c478bd9Sstevel@tonic-gate 		}
58757c478bd9Sstevel@tonic-gate 
58767c478bd9Sstevel@tonic-gate 		if (get_major_no(cdriver, &newminor) == DEVFSADM_FAILURE) {
58777c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
58787c478bd9Sstevel@tonic-gate 		}
58797c478bd9Sstevel@tonic-gate 
58807c478bd9Sstevel@tonic-gate 		*new_dev = makedev(newmajor, newminor);
58817c478bd9Sstevel@tonic-gate 		if (old_dev != *new_dev) {
58827c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "%sdriver: %s old: "
58830a653502Swroche 			    "%lu,%lu  new: %lu,%lu\n", fcn, driver,
58840a653502Swroche 			    major(old_dev), minor(old_dev),
58850a653502Swroche 			    major(*new_dev), minor(*new_dev));
58867c478bd9Sstevel@tonic-gate 		}
58877c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
58887c478bd9Sstevel@tonic-gate 	}
58897c478bd9Sstevel@tonic-gate }
58907c478bd9Sstevel@tonic-gate 
58917c478bd9Sstevel@tonic-gate /*
58927c478bd9Sstevel@tonic-gate  *
58937c478bd9Sstevel@tonic-gate  * Find the major number for driver, searching the n2m_list that was
58947c478bd9Sstevel@tonic-gate  * built in load_n2m_table().
58957c478bd9Sstevel@tonic-gate  */
58967c478bd9Sstevel@tonic-gate static int
58977c478bd9Sstevel@tonic-gate get_major_no(char *driver, major_t *major)
58987c478bd9Sstevel@tonic-gate {
58997c478bd9Sstevel@tonic-gate 	n2m_t *ptr;
59007c478bd9Sstevel@tonic-gate 
59017c478bd9Sstevel@tonic-gate 	for (ptr = n2m_list; ptr != NULL; ptr = ptr->next) {
59027c478bd9Sstevel@tonic-gate 		if (strcmp(ptr->driver, driver) == 0) {
59037c478bd9Sstevel@tonic-gate 			*major = ptr->major;
59047c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
59057c478bd9Sstevel@tonic-gate 		}
59067c478bd9Sstevel@tonic-gate 	}
59077c478bd9Sstevel@tonic-gate 	err_print(FIND_MAJOR_FAILED, driver);
59087c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
59097c478bd9Sstevel@tonic-gate }
59107c478bd9Sstevel@tonic-gate 
59117c478bd9Sstevel@tonic-gate /*
59127c478bd9Sstevel@tonic-gate  * Loads a name_to_major table into memory.  Used only for suninstall's
59137c478bd9Sstevel@tonic-gate  * private -R option to devfsadm, to translate major numbers from the
59147c478bd9Sstevel@tonic-gate  * running to the installed target disk.
59157c478bd9Sstevel@tonic-gate  */
59167c478bd9Sstevel@tonic-gate static int
59177c478bd9Sstevel@tonic-gate load_n2m_table(char *file)
59187c478bd9Sstevel@tonic-gate {
59197c478bd9Sstevel@tonic-gate 	FILE *fp;
59201ca93273Seota 	char line[1024], *cp;
59217c478bd9Sstevel@tonic-gate 	char driver[PATH_MAX + 1];
59227c478bd9Sstevel@tonic-gate 	major_t major;
59237c478bd9Sstevel@tonic-gate 	n2m_t *ptr;
59247c478bd9Sstevel@tonic-gate 	int ln = 0;
59257c478bd9Sstevel@tonic-gate 
59267c478bd9Sstevel@tonic-gate 	if ((fp = fopen(file, "r")) == NULL) {
59277c478bd9Sstevel@tonic-gate 		err_print(FOPEN_FAILED, file, strerror(errno));
59287c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
59297c478bd9Sstevel@tonic-gate 	}
59307c478bd9Sstevel@tonic-gate 
59317c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line), fp) != NULL) {
59327c478bd9Sstevel@tonic-gate 		ln++;
59331ca93273Seota 		/* cut off comments starting with '#' */
59341ca93273Seota 		if ((cp = strchr(line, '#')) != NULL)
59351ca93273Seota 			*cp = '\0';
59361ca93273Seota 		/* ignore comment or blank lines */
59371ca93273Seota 		if (is_blank(line))
59387c478bd9Sstevel@tonic-gate 			continue;
59391ca93273Seota 		/* sanity-check */
59407c478bd9Sstevel@tonic-gate 		if (sscanf(line, "%1024s%lu", driver, &major) != 2) {
59417c478bd9Sstevel@tonic-gate 			err_print(IGNORING_LINE_IN, ln, file);
59427c478bd9Sstevel@tonic-gate 			continue;
59437c478bd9Sstevel@tonic-gate 		}
59447c478bd9Sstevel@tonic-gate 		ptr = (n2m_t *)s_malloc(sizeof (n2m_t));
59457c478bd9Sstevel@tonic-gate 		ptr->major = major;
59467c478bd9Sstevel@tonic-gate 		ptr->driver = s_strdup(driver);
59477c478bd9Sstevel@tonic-gate 		ptr->next = n2m_list;
59487c478bd9Sstevel@tonic-gate 		n2m_list = ptr;
59497c478bd9Sstevel@tonic-gate 	}
59507c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
59517c478bd9Sstevel@tonic-gate 		err_print(FCLOSE_FAILED, file, strerror(errno));
59527c478bd9Sstevel@tonic-gate 	}
59537c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
59547c478bd9Sstevel@tonic-gate }
59557c478bd9Sstevel@tonic-gate 
59568d483882Smlf /*
59578d483882Smlf  * Called at devfsadm startup to read the file /etc/dev/enumerate_reserved
59588d483882Smlf  * Creates a linked list of devlinks from which reserved IDs can be derived
59598d483882Smlf  */
59608d483882Smlf static void
59618d483882Smlf read_enumerate_file(void)
59628d483882Smlf {
59638d483882Smlf 	FILE *fp;
59648d483882Smlf 	int linenum;
59658d483882Smlf 	char line[PATH_MAX+1];
59668d483882Smlf 	enumerate_file_t *entry;
59678d483882Smlf 	struct stat current_sb;
59688d483882Smlf 	static struct stat cached_sb;
59698d483882Smlf 	static int cached = FALSE;
59708d483882Smlf 
59718d483882Smlf 	assert(enumerate_file);
59728d483882Smlf 
59738d483882Smlf 	if (stat(enumerate_file, &current_sb) == -1) {
59748d483882Smlf 		vprint(RSRV_MID, "No reserved file: %s\n", enumerate_file);
59758d483882Smlf 		cached = FALSE;
59768d483882Smlf 		if (enumerate_reserved != NULL) {
59778d483882Smlf 			vprint(RSRV_MID, "invalidating %s cache\n",
59788d483882Smlf 			    enumerate_file);
59798d483882Smlf 		}
59808d483882Smlf 		while (enumerate_reserved != NULL) {
59818d483882Smlf 			entry = enumerate_reserved;
59828d483882Smlf 			enumerate_reserved = entry->er_next;
59838d483882Smlf 			free(entry->er_file);
59848d483882Smlf 			free(entry->er_id);
59858d483882Smlf 			free(entry);
59868d483882Smlf 		}
59878d483882Smlf 		return;
59888d483882Smlf 	}
59898d483882Smlf 
59908d483882Smlf 	/* if already cached, check to see if it is still valid */
59918d483882Smlf 	if (cached == TRUE) {
59928d483882Smlf 
59938d483882Smlf 		if (current_sb.st_mtime == cached_sb.st_mtime) {
59948d483882Smlf 			vprint(RSRV_MID, "%s cache valid\n", enumerate_file);
59958d483882Smlf 			vprint(FILES_MID, "%s cache valid\n", enumerate_file);
59968d483882Smlf 			return;
59978d483882Smlf 		}
59988d483882Smlf 
59998d483882Smlf 		vprint(RSRV_MID, "invalidating %s cache\n", enumerate_file);
60008d483882Smlf 		vprint(FILES_MID, "invalidating %s cache\n", enumerate_file);
60018d483882Smlf 
60028d483882Smlf 		while (enumerate_reserved != NULL) {
60038d483882Smlf 			entry = enumerate_reserved;
60048d483882Smlf 			enumerate_reserved = entry->er_next;
60058d483882Smlf 			free(entry->er_file);
60068d483882Smlf 			free(entry->er_id);
60078d483882Smlf 			free(entry);
60088d483882Smlf 		}
60098d483882Smlf 		vprint(RSRV_MID, "Recaching file: %s\n", enumerate_file);
60108d483882Smlf 	} else {
60118d483882Smlf 		vprint(RSRV_MID, "Caching file (first time): %s\n",
60128d483882Smlf 		    enumerate_file);
60138d483882Smlf 		cached = TRUE;
60148d483882Smlf 	}
60158d483882Smlf 
60168d483882Smlf 	(void) stat(enumerate_file, &cached_sb);
60178d483882Smlf 
60188d483882Smlf 	if ((fp = fopen(enumerate_file, "r")) == NULL) {
60198d483882Smlf 		err_print(FOPEN_FAILED, enumerate_file, strerror(errno));
60208d483882Smlf 		return;
60218d483882Smlf 	}
60228d483882Smlf 
60238d483882Smlf 	vprint(RSRV_MID, "Reading reserve file: %s\n", enumerate_file);
60248d483882Smlf 	linenum = 0;
60258d483882Smlf 	while (fgets(line, sizeof (line), fp) != NULL) {
60268d483882Smlf 		char	*cp, *ncp;
60278d483882Smlf 
60288d483882Smlf 		linenum++;
60298d483882Smlf 
60308d483882Smlf 		/* remove newline */
60318d483882Smlf 		cp = strchr(line, '\n');
60328d483882Smlf 		if (cp)
60338d483882Smlf 			*cp = '\0';
60348d483882Smlf 
60350a653502Swroche 		vprint(RSRV_MID, "Reserve file: line %d: %s\n", linenum, line);
60368d483882Smlf 
60378d483882Smlf 		/* skip over space and tab */
60380a653502Swroche 		for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
60390a653502Swroche 			;
60408d483882Smlf 
60418d483882Smlf 		if (*cp == '\0' || *cp == '#') {
60428d483882Smlf 			vprint(RSRV_MID, "Skipping line: '%s'\n", line);
60438d483882Smlf 			continue; /* blank line or comment line */
60448d483882Smlf 		}
60458d483882Smlf 
60468d483882Smlf 		ncp = cp;
60478d483882Smlf 
60488d483882Smlf 		/* delete trailing blanks */
60490a653502Swroche 		for (; *cp != ' ' && *cp != '\t' && *cp != '\0'; cp++)
60500a653502Swroche 			;
60518d483882Smlf 		*cp = '\0';
60528d483882Smlf 
60538d483882Smlf 		entry = s_zalloc(sizeof (enumerate_file_t));
60548d483882Smlf 		entry->er_file = s_strdup(ncp);
60558d483882Smlf 		entry->er_id = NULL;
60568d483882Smlf 		entry->er_next = enumerate_reserved;
60578d483882Smlf 		enumerate_reserved = entry;
60588d483882Smlf 	}
60598d483882Smlf 
60608d483882Smlf 	if (fclose(fp) == EOF) {
60618d483882Smlf 		err_print(FCLOSE_FAILED, enumerate_file, strerror(errno));
60628d483882Smlf 	}
60638d483882Smlf }
60648d483882Smlf 
60657c478bd9Sstevel@tonic-gate /*
60667c478bd9Sstevel@tonic-gate  * Called at devfsadm startup to read in the devlink.tab file.	Creates
60677c478bd9Sstevel@tonic-gate  * a linked list of devlinktab_list structures which will be
60687c478bd9Sstevel@tonic-gate  * searched for every minor node.
60697c478bd9Sstevel@tonic-gate  */
60707c478bd9Sstevel@tonic-gate static void
60717c478bd9Sstevel@tonic-gate read_devlinktab_file(void)
60727c478bd9Sstevel@tonic-gate {
60737c478bd9Sstevel@tonic-gate 	devlinktab_list_t *headp = NULL;
60747c478bd9Sstevel@tonic-gate 	devlinktab_list_t *entryp;
60757c478bd9Sstevel@tonic-gate 	devlinktab_list_t **previous;
60767c478bd9Sstevel@tonic-gate 	devlinktab_list_t *save;
60771ca93273Seota 	char line[MAX_DEVLINK_LINE], *cp;
60787c478bd9Sstevel@tonic-gate 	char *selector;
60797c478bd9Sstevel@tonic-gate 	char *p_link;
60807c478bd9Sstevel@tonic-gate 	char *s_link;
60817c478bd9Sstevel@tonic-gate 	FILE *fp;
60827c478bd9Sstevel@tonic-gate 	int i;
60837c478bd9Sstevel@tonic-gate 	static struct stat cached_sb;
60847c478bd9Sstevel@tonic-gate 	struct stat current_sb;
60857c478bd9Sstevel@tonic-gate 	static int cached = FALSE;
60867c478bd9Sstevel@tonic-gate 
60877c478bd9Sstevel@tonic-gate 	if (devlinktab_file == NULL) {
60887c478bd9Sstevel@tonic-gate 		return;
60897c478bd9Sstevel@tonic-gate 	}
60907c478bd9Sstevel@tonic-gate 
60917c478bd9Sstevel@tonic-gate 	(void) stat(devlinktab_file, &current_sb);
60927c478bd9Sstevel@tonic-gate 
60937c478bd9Sstevel@tonic-gate 	/* if already cached, check to see if it is still valid */
60947c478bd9Sstevel@tonic-gate 	if (cached == TRUE) {
60957c478bd9Sstevel@tonic-gate 
60967c478bd9Sstevel@tonic-gate 		if (current_sb.st_mtime == cached_sb.st_mtime) {
60977c478bd9Sstevel@tonic-gate 			vprint(FILES_MID, "%s cache valid\n", devlinktab_file);
60987c478bd9Sstevel@tonic-gate 			return;
60997c478bd9Sstevel@tonic-gate 		}
61007c478bd9Sstevel@tonic-gate 
61017c478bd9Sstevel@tonic-gate 		vprint(FILES_MID, "invalidating %s cache\n", devlinktab_file);
61027c478bd9Sstevel@tonic-gate 
61037c478bd9Sstevel@tonic-gate 		while (devlinktab_list != NULL) {
61047c478bd9Sstevel@tonic-gate 			free_link_list(devlinktab_list->p_link);
61057c478bd9Sstevel@tonic-gate 			free_link_list(devlinktab_list->s_link);
61067c478bd9Sstevel@tonic-gate 			free_selector_list(devlinktab_list->selector);
61077c478bd9Sstevel@tonic-gate 			free(devlinktab_list->selector_pattern);
61087c478bd9Sstevel@tonic-gate 			free(devlinktab_list->p_link_pattern);
61097c478bd9Sstevel@tonic-gate 			if (devlinktab_list->s_link_pattern != NULL) {
61107c478bd9Sstevel@tonic-gate 				free(devlinktab_list->s_link_pattern);
61117c478bd9Sstevel@tonic-gate 			}
61127c478bd9Sstevel@tonic-gate 			save = devlinktab_list;
61137c478bd9Sstevel@tonic-gate 			devlinktab_list = devlinktab_list->next;
61147c478bd9Sstevel@tonic-gate 			free(save);
61157c478bd9Sstevel@tonic-gate 		}
61167c478bd9Sstevel@tonic-gate 	} else {
61177c478bd9Sstevel@tonic-gate 		cached = TRUE;
61187c478bd9Sstevel@tonic-gate 	}
61197c478bd9Sstevel@tonic-gate 
61207c478bd9Sstevel@tonic-gate 	(void) stat(devlinktab_file, &cached_sb);
61217c478bd9Sstevel@tonic-gate 
61227c478bd9Sstevel@tonic-gate 	if ((fp = fopen(devlinktab_file, "r")) == NULL) {
61237c478bd9Sstevel@tonic-gate 		err_print(FOPEN_FAILED, devlinktab_file, strerror(errno));
61247c478bd9Sstevel@tonic-gate 		return;
61257c478bd9Sstevel@tonic-gate 	}
61267c478bd9Sstevel@tonic-gate 
61277c478bd9Sstevel@tonic-gate 	previous = &headp;
61287c478bd9Sstevel@tonic-gate 
61297c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line), fp) != NULL) {
61307c478bd9Sstevel@tonic-gate 		devlinktab_line++;
61317c478bd9Sstevel@tonic-gate 		i = strlen(line);
61327c478bd9Sstevel@tonic-gate 		if (line[i-1] == NEWLINE) {
61337c478bd9Sstevel@tonic-gate 			line[i-1] = '\0';
61347c478bd9Sstevel@tonic-gate 		} else if (i == sizeof (line-1)) {
61357c478bd9Sstevel@tonic-gate 			err_print(LINE_TOO_LONG, devlinktab_line,
61367c478bd9Sstevel@tonic-gate 			    devlinktab_file, sizeof (line)-1);
61370a653502Swroche 			while (((i = getc(fp)) != '\n') && (i != EOF))
61380a653502Swroche 				;
61397c478bd9Sstevel@tonic-gate 			continue;
61407c478bd9Sstevel@tonic-gate 		}
61417c478bd9Sstevel@tonic-gate 
61421ca93273Seota 		/* cut off comments starting with '#' */
61431ca93273Seota 		if ((cp = strchr(line, '#')) != NULL)
61441ca93273Seota 			*cp = '\0';
61451ca93273Seota 		/* ignore comment or blank lines */
61461ca93273Seota 		if (is_blank(line))
61477c478bd9Sstevel@tonic-gate 			continue;
61487c478bd9Sstevel@tonic-gate 
61497c478bd9Sstevel@tonic-gate 		vprint(DEVLINK_MID, "table: %s line %d: '%s'\n",
61500a653502Swroche 		    devlinktab_file, devlinktab_line, line);
61517c478bd9Sstevel@tonic-gate 
61527c478bd9Sstevel@tonic-gate 		/* break each entry into fields.  s_link may be NULL */
61537c478bd9Sstevel@tonic-gate 		if (split_devlinktab_entry(line, &selector, &p_link,
61547c478bd9Sstevel@tonic-gate 		    &s_link) == DEVFSADM_FAILURE) {
61557c478bd9Sstevel@tonic-gate 			vprint(DEVLINK_MID, "split_entry returns failure\n");
61567c478bd9Sstevel@tonic-gate 			continue;
61577c478bd9Sstevel@tonic-gate 		} else {
61587c478bd9Sstevel@tonic-gate 			vprint(DEVLINK_MID, "split_entry selector='%s' "
61590a653502Swroche 			    "p_link='%s' s_link='%s'\n\n", selector,
61600a653502Swroche 			    p_link, (s_link == NULL) ? "" : s_link);
61617c478bd9Sstevel@tonic-gate 		}
61627c478bd9Sstevel@tonic-gate 
61630a653502Swroche 		entryp =
61640a653502Swroche 		    (devlinktab_list_t *)s_malloc(sizeof (devlinktab_list_t));
61657c478bd9Sstevel@tonic-gate 
61667c478bd9Sstevel@tonic-gate 		entryp->line_number = devlinktab_line;
61677c478bd9Sstevel@tonic-gate 
61680a653502Swroche 		if ((entryp->selector = create_selector_list(selector))
61690a653502Swroche 		    == NULL) {
61707c478bd9Sstevel@tonic-gate 			free(entryp);
61717c478bd9Sstevel@tonic-gate 			continue;
61727c478bd9Sstevel@tonic-gate 		}
61737c478bd9Sstevel@tonic-gate 		entryp->selector_pattern = s_strdup(selector);
61747c478bd9Sstevel@tonic-gate 
61757c478bd9Sstevel@tonic-gate 		if ((entryp->p_link = create_link_list(p_link)) == NULL) {
61767c478bd9Sstevel@tonic-gate 			free_selector_list(entryp->selector);
61777c478bd9Sstevel@tonic-gate 			free(entryp->selector_pattern);
61787c478bd9Sstevel@tonic-gate 			free(entryp);
61797c478bd9Sstevel@tonic-gate 			continue;
61807c478bd9Sstevel@tonic-gate 		}
61817c478bd9Sstevel@tonic-gate 
61827c478bd9Sstevel@tonic-gate 		entryp->p_link_pattern = s_strdup(p_link);
61837c478bd9Sstevel@tonic-gate 
61847c478bd9Sstevel@tonic-gate 		if (s_link != NULL) {
61857c478bd9Sstevel@tonic-gate 			if ((entryp->s_link =
61867c478bd9Sstevel@tonic-gate 			    create_link_list(s_link)) == NULL) {
61877c478bd9Sstevel@tonic-gate 				free_selector_list(entryp->selector);
61887c478bd9Sstevel@tonic-gate 				free_link_list(entryp->p_link);
61897c478bd9Sstevel@tonic-gate 				free(entryp->selector_pattern);
61907c478bd9Sstevel@tonic-gate 				free(entryp->p_link_pattern);
61917c478bd9Sstevel@tonic-gate 				free(entryp);
61927c478bd9Sstevel@tonic-gate 				continue;
61937c478bd9Sstevel@tonic-gate 			}
61940a653502Swroche 			entryp->s_link_pattern = s_strdup(s_link);
61957c478bd9Sstevel@tonic-gate 		} else {
61967c478bd9Sstevel@tonic-gate 			entryp->s_link = NULL;
61977c478bd9Sstevel@tonic-gate 			entryp->s_link_pattern = NULL;
61987c478bd9Sstevel@tonic-gate 
61997c478bd9Sstevel@tonic-gate 		}
62007c478bd9Sstevel@tonic-gate 
62017c478bd9Sstevel@tonic-gate 		/* append to end of list */
62027c478bd9Sstevel@tonic-gate 
62037c478bd9Sstevel@tonic-gate 		entryp->next = NULL;
62047c478bd9Sstevel@tonic-gate 		*previous = entryp;
62057c478bd9Sstevel@tonic-gate 		previous = &(entryp->next);
62067c478bd9Sstevel@tonic-gate 	}
62077c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
62087c478bd9Sstevel@tonic-gate 		err_print(FCLOSE_FAILED, devlinktab_file, strerror(errno));
62097c478bd9Sstevel@tonic-gate 	}
62107c478bd9Sstevel@tonic-gate 	devlinktab_list = headp;
62117c478bd9Sstevel@tonic-gate }
62127c478bd9Sstevel@tonic-gate 
62137c478bd9Sstevel@tonic-gate /*
62147c478bd9Sstevel@tonic-gate  *
62157c478bd9Sstevel@tonic-gate  * For a single line entry in devlink.tab, split the line into fields
62167c478bd9Sstevel@tonic-gate  * selector, p_link, and an optionally s_link.	If s_link field is not
62177c478bd9Sstevel@tonic-gate  * present, then return NULL in s_link (not NULL string).
62187c478bd9Sstevel@tonic-gate  */
62197c478bd9Sstevel@tonic-gate static int
62207c478bd9Sstevel@tonic-gate split_devlinktab_entry(char *entry, char **selector, char **p_link,
62217c478bd9Sstevel@tonic-gate 			char **s_link)
62227c478bd9Sstevel@tonic-gate {
62237c478bd9Sstevel@tonic-gate 	char *tab;
62247c478bd9Sstevel@tonic-gate 
62257c478bd9Sstevel@tonic-gate 	*selector = entry;
62267c478bd9Sstevel@tonic-gate 
62277c478bd9Sstevel@tonic-gate 	if ((tab = strchr(entry, TAB)) != NULL) {
62287c478bd9Sstevel@tonic-gate 		*tab = '\0';
62297c478bd9Sstevel@tonic-gate 		*p_link = ++tab;
62307c478bd9Sstevel@tonic-gate 	} else {
62317c478bd9Sstevel@tonic-gate 		err_print(MISSING_TAB, devlinktab_line, devlinktab_file);
62327c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
62337c478bd9Sstevel@tonic-gate 	}
62347c478bd9Sstevel@tonic-gate 
62357c478bd9Sstevel@tonic-gate 	if (*p_link == '\0') {
62367c478bd9Sstevel@tonic-gate 		err_print(MISSING_DEVNAME, devlinktab_line, devlinktab_file);
62377c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
62387c478bd9Sstevel@tonic-gate 	}
62397c478bd9Sstevel@tonic-gate 
62407c478bd9Sstevel@tonic-gate 	if ((tab = strchr(*p_link, TAB)) != NULL) {
62417c478bd9Sstevel@tonic-gate 		*tab = '\0';
62427c478bd9Sstevel@tonic-gate 		*s_link = ++tab;
62437c478bd9Sstevel@tonic-gate 		if (strchr(*s_link, TAB) != NULL) {
62447c478bd9Sstevel@tonic-gate 			err_print(TOO_MANY_FIELDS, devlinktab_line,
62450a653502Swroche 			    devlinktab_file);
62467c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
62477c478bd9Sstevel@tonic-gate 		}
62487c478bd9Sstevel@tonic-gate 	} else {
62497c478bd9Sstevel@tonic-gate 		*s_link = NULL;
62507c478bd9Sstevel@tonic-gate 	}
62517c478bd9Sstevel@tonic-gate 
62527c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
62537c478bd9Sstevel@tonic-gate }
62547c478bd9Sstevel@tonic-gate 
62557c478bd9Sstevel@tonic-gate /*
62567c478bd9Sstevel@tonic-gate  * For a given devfs_spec field, for each element in the field, add it to
62577c478bd9Sstevel@tonic-gate  * a linked list of devfs_spec structures.  Return the linked list in
62587c478bd9Sstevel@tonic-gate  * devfs_spec_list.
62597c478bd9Sstevel@tonic-gate  */
62607c478bd9Sstevel@tonic-gate static selector_list_t *
62617c478bd9Sstevel@tonic-gate create_selector_list(char *selector)
62627c478bd9Sstevel@tonic-gate {
62630a653502Swroche 	char *key;
62640a653502Swroche 	char *val;
62650a653502Swroche 	int error = FALSE;
62660a653502Swroche 	selector_list_t *head_selector_list = NULL;
62670a653502Swroche 	selector_list_t *selector_list;
62680a653502Swroche 
62690a653502Swroche 	/* parse_devfs_spec splits the next field into keyword & value */
62700a653502Swroche 	while ((*selector != NULL) && (error == FALSE)) {
62710a653502Swroche 		if (parse_selector(&selector, &key, &val) == DEVFSADM_FAILURE) {
62720a653502Swroche 			error = TRUE;
62730a653502Swroche 			break;
62740a653502Swroche 		} else {
62750a653502Swroche 			selector_list = (selector_list_t *)
62760a653502Swroche 			    s_malloc(sizeof (selector_list_t));
62770a653502Swroche 			if (strcmp(NAME_S, key) == 0) {
62780a653502Swroche 				selector_list->key = NAME;
62790a653502Swroche 			} else if (strcmp(TYPE_S, key) == 0) {
62800a653502Swroche 				selector_list->key = TYPE;
62810a653502Swroche 			} else if (strncmp(ADDR_S, key, ADDR_S_LEN) == 0) {
62820a653502Swroche 				selector_list->key = ADDR;
62830a653502Swroche 				if (key[ADDR_S_LEN] == '\0') {
62840a653502Swroche 					selector_list->arg = 0;
62850a653502Swroche 				} else if (isdigit(key[ADDR_S_LEN]) != FALSE) {
62860a653502Swroche 					selector_list->arg =
62870a653502Swroche 					    atoi(&key[ADDR_S_LEN]);
62880a653502Swroche 				} else {
62890a653502Swroche 					error = TRUE;
62900a653502Swroche 					free(selector_list);
62910a653502Swroche 					err_print(BADKEYWORD, key,
62920a653502Swroche 					    devlinktab_line, devlinktab_file);
62930a653502Swroche 					break;
62940a653502Swroche 				}
62950a653502Swroche 			} else if (strncmp(MINOR_S, key, MINOR_S_LEN) == 0) {
62960a653502Swroche 				selector_list->key = MINOR;
62970a653502Swroche 				if (key[MINOR_S_LEN] == '\0') {
62980a653502Swroche 					selector_list->arg = 0;
62990a653502Swroche 				} else if (isdigit(key[MINOR_S_LEN]) != FALSE) {
63000a653502Swroche 					selector_list->arg =
63010a653502Swroche 					    atoi(&key[MINOR_S_LEN]);
63020a653502Swroche 				} else {
63030a653502Swroche 					error = TRUE;
63040a653502Swroche 					free(selector_list);
63050a653502Swroche 					err_print(BADKEYWORD, key,
63060a653502Swroche 					    devlinktab_line, devlinktab_file);
63070a653502Swroche 					break;
63080a653502Swroche 				}
63090a653502Swroche 				vprint(DEVLINK_MID, "MINOR = %s\n", val);
63100a653502Swroche 			} else {
63110a653502Swroche 				err_print(UNRECOGNIZED_KEY, key,
63120a653502Swroche 				    devlinktab_line, devlinktab_file);
63130a653502Swroche 				error = TRUE;
63140a653502Swroche 				free(selector_list);
63150a653502Swroche 				break;
63160a653502Swroche 			}
63170a653502Swroche 			selector_list->val = s_strdup(val);
63180a653502Swroche 			selector_list->next = head_selector_list;
63190a653502Swroche 			head_selector_list = selector_list;
63200a653502Swroche 			vprint(DEVLINK_MID, "key='%s' val='%s' arg=%d\n",
63210a653502Swroche 			    key, val, selector_list->arg);
63220a653502Swroche 		}
63230a653502Swroche 	}
63240a653502Swroche 
63250a653502Swroche 	if ((error == FALSE) && (head_selector_list != NULL)) {
63260a653502Swroche 		return (head_selector_list);
63270a653502Swroche 	} else {
63280a653502Swroche 		/* parse failed.  Free any allocated structs */
63290a653502Swroche 		free_selector_list(head_selector_list);
63300a653502Swroche 		return (NULL);
63310a653502Swroche 	}
63327c478bd9Sstevel@tonic-gate }
63337c478bd9Sstevel@tonic-gate 
63347c478bd9Sstevel@tonic-gate /*
63357c478bd9Sstevel@tonic-gate  * Takes a semicolon separated list of selector elements and breaks up
63367c478bd9Sstevel@tonic-gate  * into a keyword-value pair.	semicolon and equal characters are
63377c478bd9Sstevel@tonic-gate  * replaced with NULL's.  On success, selector is updated to point to the
63387c478bd9Sstevel@tonic-gate  * terminating NULL character terminating the keyword-value pair, and the
63397c478bd9Sstevel@tonic-gate  * function returns DEVFSADM_SUCCESS.	If there is a syntax error,
63407c478bd9Sstevel@tonic-gate  * devfs_spec is not modified and function returns DEVFSADM_FAILURE.
63417c478bd9Sstevel@tonic-gate  */
63427c478bd9Sstevel@tonic-gate static int
63437c478bd9Sstevel@tonic-gate parse_selector(char **selector, char **key, char **val)
63447c478bd9Sstevel@tonic-gate {
63457c478bd9Sstevel@tonic-gate 	char *equal;
63467c478bd9Sstevel@tonic-gate 	char *semi_colon;
63477c478bd9Sstevel@tonic-gate 
63487c478bd9Sstevel@tonic-gate 	*key = *selector;
63497c478bd9Sstevel@tonic-gate 
63507c478bd9Sstevel@tonic-gate 	if ((equal = strchr(*key, '=')) != NULL) {
63517c478bd9Sstevel@tonic-gate 		*equal = '\0';
63527c478bd9Sstevel@tonic-gate 	} else {
63537c478bd9Sstevel@tonic-gate 		err_print(MISSING_EQUAL, devlinktab_line, devlinktab_file);
63547c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
63557c478bd9Sstevel@tonic-gate 	}
63567c478bd9Sstevel@tonic-gate 
63577c478bd9Sstevel@tonic-gate 	*val = ++equal;
63587c478bd9Sstevel@tonic-gate 	if ((semi_colon = strchr(equal, ';')) != NULL) {
63597c478bd9Sstevel@tonic-gate 		*semi_colon = '\0';
63607c478bd9Sstevel@tonic-gate 		*selector = semi_colon + 1;
63617c478bd9Sstevel@tonic-gate 	} else {
63627c478bd9Sstevel@tonic-gate 		*selector = equal + strlen(equal);
63637c478bd9Sstevel@tonic-gate 	}
63647c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
63657c478bd9Sstevel@tonic-gate }
63667c478bd9Sstevel@tonic-gate 
63677c478bd9Sstevel@tonic-gate /*
63687c478bd9Sstevel@tonic-gate  * link is either the second or third field of devlink.tab.  Parse link
63697c478bd9Sstevel@tonic-gate  * into a linked list of devlink structures and return ptr to list.  Each
63707c478bd9Sstevel@tonic-gate  * list element is either a constant string, or one of the following
63717c478bd9Sstevel@tonic-gate  * escape sequences: \M, \A, \N, or \D.  The first three escape sequences
63727c478bd9Sstevel@tonic-gate  * take a numerical argument.
63737c478bd9Sstevel@tonic-gate  */
63747c478bd9Sstevel@tonic-gate static link_list_t *
63757c478bd9Sstevel@tonic-gate create_link_list(char *link)
63767c478bd9Sstevel@tonic-gate {
63777c478bd9Sstevel@tonic-gate 	int x = 0;
63787c478bd9Sstevel@tonic-gate 	int error = FALSE;
63797c478bd9Sstevel@tonic-gate 	int counter_found = FALSE;
63807c478bd9Sstevel@tonic-gate 	link_list_t *head = NULL;
63817c478bd9Sstevel@tonic-gate 	link_list_t **ptr;
63827c478bd9Sstevel@tonic-gate 	link_list_t *link_list;
63837c478bd9Sstevel@tonic-gate 	char constant[MAX_DEVLINK_LINE];
63847c478bd9Sstevel@tonic-gate 	char *error_str;
63857c478bd9Sstevel@tonic-gate 
63867c478bd9Sstevel@tonic-gate 	if (link == NULL) {
63877c478bd9Sstevel@tonic-gate 		return (NULL);
63887c478bd9Sstevel@tonic-gate 	}
63897c478bd9Sstevel@tonic-gate 
63907c478bd9Sstevel@tonic-gate 	while ((*link != '\0') && (error == FALSE)) {
63917c478bd9Sstevel@tonic-gate 		link_list = (link_list_t *)s_malloc(sizeof (link_list_t));
63927c478bd9Sstevel@tonic-gate 		link_list->next = NULL;
63937c478bd9Sstevel@tonic-gate 
63947c478bd9Sstevel@tonic-gate 		while ((*link != '\0') && (*link != '\\')) {
63957c478bd9Sstevel@tonic-gate 			/* a non-escaped string */
63967c478bd9Sstevel@tonic-gate 			constant[x++] = *(link++);
63977c478bd9Sstevel@tonic-gate 		}
63987c478bd9Sstevel@tonic-gate 		if (x != 0) {
63997c478bd9Sstevel@tonic-gate 			constant[x] = '\0';
64007c478bd9Sstevel@tonic-gate 			link_list->type = CONSTANT;
64017c478bd9Sstevel@tonic-gate 			link_list->constant = s_strdup(constant);
64027c478bd9Sstevel@tonic-gate 			x = 0;
64037c478bd9Sstevel@tonic-gate 			vprint(DEVLINK_MID, "CONSTANT FOUND %s\n", constant);
64047c478bd9Sstevel@tonic-gate 		} else {
64057c478bd9Sstevel@tonic-gate 			switch (*(++link)) {
64067c478bd9Sstevel@tonic-gate 			case 'M':
64077c478bd9Sstevel@tonic-gate 				link_list->type = MINOR;
64087c478bd9Sstevel@tonic-gate 				break;
64097c478bd9Sstevel@tonic-gate 			case 'A':
64107c478bd9Sstevel@tonic-gate 				link_list->type = ADDR;
64117c478bd9Sstevel@tonic-gate 				break;
64127c478bd9Sstevel@tonic-gate 			case 'N':
64137c478bd9Sstevel@tonic-gate 				if (counter_found == TRUE) {
64147c478bd9Sstevel@tonic-gate 					error = TRUE;
64150a653502Swroche 					error_str =
64160a653502Swroche 					    "multiple counters not permitted";
64177c478bd9Sstevel@tonic-gate 					free(link_list);
64187c478bd9Sstevel@tonic-gate 				} else {
64197c478bd9Sstevel@tonic-gate 					counter_found = TRUE;
64207c478bd9Sstevel@tonic-gate 					link_list->type = COUNTER;
64217c478bd9Sstevel@tonic-gate 				}
64227c478bd9Sstevel@tonic-gate 				break;
64237c478bd9Sstevel@tonic-gate 			case 'D':
64247c478bd9Sstevel@tonic-gate 				link_list->type = NAME;
64257c478bd9Sstevel@tonic-gate 				break;
64267c478bd9Sstevel@tonic-gate 			default:
64277c478bd9Sstevel@tonic-gate 				error = TRUE;
64287c478bd9Sstevel@tonic-gate 				free(link_list);
64297c478bd9Sstevel@tonic-gate 				error_str = "unrecognized escape sequence";
64307c478bd9Sstevel@tonic-gate 				break;
64317c478bd9Sstevel@tonic-gate 			}
64327c478bd9Sstevel@tonic-gate 			if (*(link++) != 'D') {
64337c478bd9Sstevel@tonic-gate 				if (isdigit(*link) == FALSE) {
64347c478bd9Sstevel@tonic-gate 					error_str = "escape sequence must be "
64350a653502Swroche 					    "followed by a digit\n";
64367c478bd9Sstevel@tonic-gate 					error = TRUE;
64377c478bd9Sstevel@tonic-gate 					free(link_list);
64387c478bd9Sstevel@tonic-gate 				} else {
64397c478bd9Sstevel@tonic-gate 					link_list->arg =
64400a653502Swroche 					    (int)strtoul(link, &link, 10);
64417c478bd9Sstevel@tonic-gate 					vprint(DEVLINK_MID, "link_list->arg = "
64420a653502Swroche 					    "%d\n", link_list->arg);
64437c478bd9Sstevel@tonic-gate 				}
64447c478bd9Sstevel@tonic-gate 			}
64457c478bd9Sstevel@tonic-gate 		}
64467c478bd9Sstevel@tonic-gate 		/* append link_list struct to end of list */
64477c478bd9Sstevel@tonic-gate 		if (error == FALSE) {
64480a653502Swroche 			for (ptr = &head; *ptr != NULL; ptr = &((*ptr)->next))
64490a653502Swroche 				;
64507c478bd9Sstevel@tonic-gate 			*ptr = link_list;
64517c478bd9Sstevel@tonic-gate 		}
64527c478bd9Sstevel@tonic-gate 	}
64537c478bd9Sstevel@tonic-gate 
64547c478bd9Sstevel@tonic-gate 	if (error == FALSE) {
64557c478bd9Sstevel@tonic-gate 		return (head);
64567c478bd9Sstevel@tonic-gate 	} else {
64577c478bd9Sstevel@tonic-gate 		err_print(CONFIG_INCORRECT, devlinktab_line, devlinktab_file,
64587c478bd9Sstevel@tonic-gate 		    error_str);
64597c478bd9Sstevel@tonic-gate 		free_link_list(head);
64607c478bd9Sstevel@tonic-gate 		return (NULL);
64617c478bd9Sstevel@tonic-gate 	}
64627c478bd9Sstevel@tonic-gate }
64637c478bd9Sstevel@tonic-gate 
64647c478bd9Sstevel@tonic-gate /*
64657c478bd9Sstevel@tonic-gate  * Called for each minor node devfsadm processes; for each minor node,
64667c478bd9Sstevel@tonic-gate  * look for matches in the devlinktab_list list which was created on
64677c478bd9Sstevel@tonic-gate  * startup read_devlinktab_file().  If there is a match, call build_links()
64687c478bd9Sstevel@tonic-gate  * to build a logical devlink and a possible extra devlink.
64697c478bd9Sstevel@tonic-gate  */
64707c478bd9Sstevel@tonic-gate static int
64717c478bd9Sstevel@tonic-gate process_devlink_compat(di_minor_t minor, di_node_t node)
64727c478bd9Sstevel@tonic-gate {
64737c478bd9Sstevel@tonic-gate 	int link_built = FALSE;
64747c478bd9Sstevel@tonic-gate 	devlinktab_list_t *entry;
64757c478bd9Sstevel@tonic-gate 	char *nodetype;
64767c478bd9Sstevel@tonic-gate 	char *dev_path;
64777c478bd9Sstevel@tonic-gate 
64787c478bd9Sstevel@tonic-gate 	if (devlinks_debug == TRUE) {
64797c478bd9Sstevel@tonic-gate 		nodetype =  di_minor_nodetype(minor);
64807c478bd9Sstevel@tonic-gate 		assert(nodetype != NULL);
64817c478bd9Sstevel@tonic-gate 		if ((dev_path = di_devfs_path(node)) != NULL) {
64820a653502Swroche 			vprint(INFO_MID, "'%s' entry: %s:%s\n",
64830a653502Swroche 			    nodetype, dev_path,
64840a653502Swroche 			    di_minor_name(minor) ? di_minor_name(minor) : "");
64857c478bd9Sstevel@tonic-gate 			di_devfs_path_free(dev_path);
64867c478bd9Sstevel@tonic-gate 		}
64877c478bd9Sstevel@tonic-gate 
64887c478bd9Sstevel@tonic-gate 	}
64897c478bd9Sstevel@tonic-gate 
64907c478bd9Sstevel@tonic-gate 
64917c478bd9Sstevel@tonic-gate 	/* don't process devlink.tab if devfsadm invoked with -c <class> */
64927c478bd9Sstevel@tonic-gate 	if (num_classes > 0) {
64937c478bd9Sstevel@tonic-gate 		return (FALSE);
64947c478bd9Sstevel@tonic-gate 	}
64957c478bd9Sstevel@tonic-gate 
64967c478bd9Sstevel@tonic-gate 	for (entry = devlinktab_list; entry != NULL; entry = entry->next) {
64977c478bd9Sstevel@tonic-gate 		if (devlink_matches(entry, minor, node) == DEVFSADM_SUCCESS) {
64987c478bd9Sstevel@tonic-gate 			link_built = TRUE;
64997c478bd9Sstevel@tonic-gate 			(void) build_links(entry, minor, node);
65007c478bd9Sstevel@tonic-gate 		}
65017c478bd9Sstevel@tonic-gate 	}
65027c478bd9Sstevel@tonic-gate 	return (link_built);
65037c478bd9Sstevel@tonic-gate }
65047c478bd9Sstevel@tonic-gate 
65057c478bd9Sstevel@tonic-gate /*
65067c478bd9Sstevel@tonic-gate  * For a given devlink.tab devlinktab_list entry, see if the selector
65077c478bd9Sstevel@tonic-gate  * field matches this minor node.  If it does, return DEVFSADM_SUCCESS,
65087c478bd9Sstevel@tonic-gate  * otherwise DEVFSADM_FAILURE.
65097c478bd9Sstevel@tonic-gate  */
65107c478bd9Sstevel@tonic-gate static int
65117c478bd9Sstevel@tonic-gate devlink_matches(devlinktab_list_t *entry, di_minor_t minor, di_node_t node)
65127c478bd9Sstevel@tonic-gate {
65137c478bd9Sstevel@tonic-gate 	selector_list_t *selector = entry->selector;
65147c478bd9Sstevel@tonic-gate 	char *addr;
65157c478bd9Sstevel@tonic-gate 	char *minor_name;
65167c478bd9Sstevel@tonic-gate 	char *node_type;
65177c478bd9Sstevel@tonic-gate 
65187c478bd9Sstevel@tonic-gate 	for (; selector != NULL; selector = selector->next) {
65197c478bd9Sstevel@tonic-gate 		switch (selector->key) {
65207c478bd9Sstevel@tonic-gate 		case NAME:
65217c478bd9Sstevel@tonic-gate 			if (strcmp(di_node_name(node), selector->val) != 0) {
65227c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
65237c478bd9Sstevel@tonic-gate 			}
65247c478bd9Sstevel@tonic-gate 			break;
65257c478bd9Sstevel@tonic-gate 		case TYPE:
65267c478bd9Sstevel@tonic-gate 			node_type = di_minor_nodetype(minor);
65277c478bd9Sstevel@tonic-gate 			assert(node_type != NULL);
65287c478bd9Sstevel@tonic-gate 			if (strcmp(node_type, selector->val) != 0) {
65297c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
65307c478bd9Sstevel@tonic-gate 			}
65317c478bd9Sstevel@tonic-gate 			break;
65327c478bd9Sstevel@tonic-gate 		case ADDR:
65337c478bd9Sstevel@tonic-gate 			if ((addr = di_bus_addr(node)) == NULL) {
65347c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
65357c478bd9Sstevel@tonic-gate 			}
65367c478bd9Sstevel@tonic-gate 			if (selector->arg == 0) {
65377c478bd9Sstevel@tonic-gate 				if (strcmp(addr, selector->val) != 0) {
65387c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
65397c478bd9Sstevel@tonic-gate 				}
65407c478bd9Sstevel@tonic-gate 			} else {
65417c478bd9Sstevel@tonic-gate 				if (compare_field(addr, selector->val,
65427c478bd9Sstevel@tonic-gate 				    selector->arg) == DEVFSADM_FAILURE) {
65437c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
65447c478bd9Sstevel@tonic-gate 				}
65457c478bd9Sstevel@tonic-gate 			}
65467c478bd9Sstevel@tonic-gate 			break;
65477c478bd9Sstevel@tonic-gate 		case MINOR:
65487c478bd9Sstevel@tonic-gate 			if ((minor_name = di_minor_name(minor)) == NULL) {
65497c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
65507c478bd9Sstevel@tonic-gate 			}
65517c478bd9Sstevel@tonic-gate 			if (selector->arg == 0) {
65527c478bd9Sstevel@tonic-gate 				if (strcmp(minor_name, selector->val) != 0) {
65537c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
65547c478bd9Sstevel@tonic-gate 				}
65557c478bd9Sstevel@tonic-gate 			} else {
65567c478bd9Sstevel@tonic-gate 				if (compare_field(minor_name, selector->val,
65570a653502Swroche 				    selector->arg) == DEVFSADM_FAILURE) {
65587c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
65597c478bd9Sstevel@tonic-gate 				}
65607c478bd9Sstevel@tonic-gate 			}
65617c478bd9Sstevel@tonic-gate 			break;
65627c478bd9Sstevel@tonic-gate 		default:
65637c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
65647c478bd9Sstevel@tonic-gate 		}
65657c478bd9Sstevel@tonic-gate 	}
65667c478bd9Sstevel@tonic-gate 
65677c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
65687c478bd9Sstevel@tonic-gate }
65697c478bd9Sstevel@tonic-gate 
65707c478bd9Sstevel@tonic-gate /*
65717c478bd9Sstevel@tonic-gate  * For the given minor node and devlinktab_list entry from devlink.tab,
65727c478bd9Sstevel@tonic-gate  * build a logical dev link and a possible extra devlink.
65737c478bd9Sstevel@tonic-gate  * Return DEVFSADM_SUCCESS if link is created, otherwise DEVFSADM_FAILURE.
65747c478bd9Sstevel@tonic-gate  */
65757c478bd9Sstevel@tonic-gate static int
65767c478bd9Sstevel@tonic-gate build_links(devlinktab_list_t *entry, di_minor_t minor, di_node_t node)
65777c478bd9Sstevel@tonic-gate {
65787c478bd9Sstevel@tonic-gate 	char secondary_link[PATH_MAX + 1];
65797c478bd9Sstevel@tonic-gate 	char primary_link[PATH_MAX + 1];
65807c478bd9Sstevel@tonic-gate 	char contents[PATH_MAX + 1];
65817c478bd9Sstevel@tonic-gate 	char *dev_path;
65827c478bd9Sstevel@tonic-gate 
65837c478bd9Sstevel@tonic-gate 	if ((dev_path = di_devfs_path(node)) == NULL) {
65847c478bd9Sstevel@tonic-gate 		err_print(DI_DEVFS_PATH_FAILED, strerror(errno));
65857c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
6586537714daSvikram 		/*NOTREACHED*/
65877c478bd9Sstevel@tonic-gate 	}
65887c478bd9Sstevel@tonic-gate 	(void) strcpy(contents, dev_path);
65897c478bd9Sstevel@tonic-gate 	di_devfs_path_free(dev_path);
65907c478bd9Sstevel@tonic-gate 
65917c478bd9Sstevel@tonic-gate 	(void) strcat(contents, ":");
65927c478bd9Sstevel@tonic-gate 	(void) strcat(contents, di_minor_name(minor));
65937c478bd9Sstevel@tonic-gate 
65947c478bd9Sstevel@tonic-gate 	if (construct_devlink(primary_link, entry->p_link, contents,
65950a653502Swroche 	    minor, node, entry->p_link_pattern) == DEVFSADM_FAILURE) {
65967c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
65977c478bd9Sstevel@tonic-gate 	}
65987c478bd9Sstevel@tonic-gate 	(void) devfsadm_mklink(primary_link, node, minor, 0);
65997c478bd9Sstevel@tonic-gate 
66007c478bd9Sstevel@tonic-gate 	if (entry->s_link == NULL) {
66017c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
66027c478bd9Sstevel@tonic-gate 	}
66037c478bd9Sstevel@tonic-gate 
66040a653502Swroche 	if (construct_devlink(secondary_link, entry->s_link, primary_link,
66050a653502Swroche 	    minor, node, entry->s_link_pattern) == DEVFSADM_FAILURE) {
66067c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
66077c478bd9Sstevel@tonic-gate 	}
66087c478bd9Sstevel@tonic-gate 
66097c478bd9Sstevel@tonic-gate 	(void) devfsadm_secondary_link(secondary_link, primary_link, 0);
66107c478bd9Sstevel@tonic-gate 
66117c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
66127c478bd9Sstevel@tonic-gate }
66137c478bd9Sstevel@tonic-gate 
66147c478bd9Sstevel@tonic-gate /*
66157c478bd9Sstevel@tonic-gate  * The counter rule for devlink.tab entries is implemented via
66167c478bd9Sstevel@tonic-gate  * devfsadm_enumerate_int_start(). One of the arguments to this function
66177c478bd9Sstevel@tonic-gate  * is a path, where each path component is treated as a regular expression.
66187c478bd9Sstevel@tonic-gate  * For devlink.tab entries, this path regular expression is derived from
66197c478bd9Sstevel@tonic-gate  * the devlink spec. get_anchored_re() accepts path regular expressions derived
66207c478bd9Sstevel@tonic-gate  * from devlink.tab entries and inserts the anchors '^' and '$' at the beginning
66217c478bd9Sstevel@tonic-gate  * and end respectively of each path component. This is done to prevent
66227c478bd9Sstevel@tonic-gate  * false matches. For example, without anchors, "a/([0-9]+)" will match "ab/c9"
66237c478bd9Sstevel@tonic-gate  * and incorrect links will be generated.
66247c478bd9Sstevel@tonic-gate  */
66257c478bd9Sstevel@tonic-gate static int
66267c478bd9Sstevel@tonic-gate get_anchored_re(char *link, char *anchored_re, char *pattern)
66277c478bd9Sstevel@tonic-gate {
66287c478bd9Sstevel@tonic-gate 	if (*link == '/' || *link == '\0') {
66297c478bd9Sstevel@tonic-gate 		err_print(INVALID_DEVLINK_SPEC, pattern);
66307c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
66317c478bd9Sstevel@tonic-gate 	}
66327c478bd9Sstevel@tonic-gate 
66337c478bd9Sstevel@tonic-gate 	*anchored_re++ = '^';
66347c478bd9Sstevel@tonic-gate 	for (; *link != '\0'; ) {
66357c478bd9Sstevel@tonic-gate 		if (*link == '/') {
66367c478bd9Sstevel@tonic-gate 			while (*link == '/')
66377c478bd9Sstevel@tonic-gate 				link++;
66387c478bd9Sstevel@tonic-gate 			*anchored_re++ = '$';
66397c478bd9Sstevel@tonic-gate 			*anchored_re++ = '/';
66407c478bd9Sstevel@tonic-gate 			if (*link != '\0') {
66417c478bd9Sstevel@tonic-gate 				*anchored_re++ = '^';
66427c478bd9Sstevel@tonic-gate 			}
66437c478bd9Sstevel@tonic-gate 		} else {
66447c478bd9Sstevel@tonic-gate 			*anchored_re++ = *link++;
66457c478bd9Sstevel@tonic-gate 			if (*link == '\0') {
66467c478bd9Sstevel@tonic-gate 				*anchored_re++ = '$';
66477c478bd9Sstevel@tonic-gate 			}
66487c478bd9Sstevel@tonic-gate 		}
66497c478bd9Sstevel@tonic-gate 	}
66507c478bd9Sstevel@tonic-gate 	*anchored_re = '\0';
66517c478bd9Sstevel@tonic-gate 
66527c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
66537c478bd9Sstevel@tonic-gate }
66547c478bd9Sstevel@tonic-gate 
66557c478bd9Sstevel@tonic-gate static int
66567c478bd9Sstevel@tonic-gate construct_devlink(char *link, link_list_t *link_build, char *contents,
66577c478bd9Sstevel@tonic-gate 			di_minor_t minor, di_node_t node, char *pattern)
66587c478bd9Sstevel@tonic-gate {
66597c478bd9Sstevel@tonic-gate 	int counter_offset = -1;
66607c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[1] = {NULL};
66617c478bd9Sstevel@tonic-gate 	char templink[PATH_MAX + 1];
66627c478bd9Sstevel@tonic-gate 	char *buff;
66637c478bd9Sstevel@tonic-gate 	char start[10];
66647c478bd9Sstevel@tonic-gate 	char *node_path;
66657c478bd9Sstevel@tonic-gate 	char anchored_re[PATH_MAX + 1];
66667c478bd9Sstevel@tonic-gate 
66677c478bd9Sstevel@tonic-gate 	link[0] = '\0';
66687c478bd9Sstevel@tonic-gate 
66697c478bd9Sstevel@tonic-gate 	for (; link_build != NULL; link_build = link_build->next) {
66707c478bd9Sstevel@tonic-gate 		switch (link_build->type) {
66717c478bd9Sstevel@tonic-gate 		case NAME:
66727c478bd9Sstevel@tonic-gate 			(void) strcat(link, di_node_name(node));
66737c478bd9Sstevel@tonic-gate 			break;
66747c478bd9Sstevel@tonic-gate 		case CONSTANT:
66757c478bd9Sstevel@tonic-gate 			(void) strcat(link, link_build->constant);
66767c478bd9Sstevel@tonic-gate 			break;
66777c478bd9Sstevel@tonic-gate 		case ADDR:
66787c478bd9Sstevel@tonic-gate 			if (component_cat(link, di_bus_addr(node),
66790a653502Swroche 			    link_build->arg) == DEVFSADM_FAILURE) {
66807c478bd9Sstevel@tonic-gate 				node_path = di_devfs_path(node);
66817c478bd9Sstevel@tonic-gate 				err_print(CANNOT_BE_USED, pattern, node_path,
66820a653502Swroche 				    di_minor_name(minor));
66837c478bd9Sstevel@tonic-gate 				di_devfs_path_free(node_path);
66847c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
66857c478bd9Sstevel@tonic-gate 			}
66867c478bd9Sstevel@tonic-gate 			break;
66877c478bd9Sstevel@tonic-gate 		case MINOR:
66887c478bd9Sstevel@tonic-gate 			if (component_cat(link, di_minor_name(minor),
66890a653502Swroche 			    link_build->arg) == DEVFSADM_FAILURE) {
66907c478bd9Sstevel@tonic-gate 				node_path = di_devfs_path(node);
66917c478bd9Sstevel@tonic-gate 				err_print(CANNOT_BE_USED, pattern, node_path,
66920a653502Swroche 				    di_minor_name(minor));
66937c478bd9Sstevel@tonic-gate 				di_devfs_path_free(node_path);
66947c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
66957c478bd9Sstevel@tonic-gate 			}
66967c478bd9Sstevel@tonic-gate 			break;
66977c478bd9Sstevel@tonic-gate 		case COUNTER:
66987c478bd9Sstevel@tonic-gate 			counter_offset = strlen(link);
66997c478bd9Sstevel@tonic-gate 			(void) strcat(link, "([0-9]+)");
67007c478bd9Sstevel@tonic-gate 			(void) sprintf(start, "%d", link_build->arg);
67017c478bd9Sstevel@tonic-gate 			break;
67027c478bd9Sstevel@tonic-gate 		default:
67037c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
67047c478bd9Sstevel@tonic-gate 		}
67057c478bd9Sstevel@tonic-gate 	}
67067c478bd9Sstevel@tonic-gate 
67077c478bd9Sstevel@tonic-gate 	if (counter_offset != -1) {
67087c478bd9Sstevel@tonic-gate 		/*
67097c478bd9Sstevel@tonic-gate 		 * copy anything appended after "([0-9]+)" into
67107c478bd9Sstevel@tonic-gate 		 * templink
67117c478bd9Sstevel@tonic-gate 		 */
67127c478bd9Sstevel@tonic-gate 
67137c478bd9Sstevel@tonic-gate 		(void) strcpy(templink,
67140a653502Swroche 		    &link[counter_offset + strlen("([0-9]+)")]);
67157c478bd9Sstevel@tonic-gate 		if (get_anchored_re(link, anchored_re, pattern)
67167c478bd9Sstevel@tonic-gate 		    != DEVFSADM_SUCCESS) {
67177c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
67187c478bd9Sstevel@tonic-gate 		}
67197c478bd9Sstevel@tonic-gate 		rules[0].re = anchored_re;
67207c478bd9Sstevel@tonic-gate 		rules[0].subexp = 1;
67217c478bd9Sstevel@tonic-gate 		rules[0].flags = MATCH_ALL;
67227c478bd9Sstevel@tonic-gate 		if (devfsadm_enumerate_int_start(contents, 0, &buff,
67237c478bd9Sstevel@tonic-gate 		    rules, 1, start) == DEVFSADM_FAILURE) {
67247c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
67257c478bd9Sstevel@tonic-gate 		}
67267c478bd9Sstevel@tonic-gate 		(void) strcpy(&link[counter_offset], buff);
67277c478bd9Sstevel@tonic-gate 		free(buff);
67287c478bd9Sstevel@tonic-gate 		(void) strcat(link, templink);
67297c478bd9Sstevel@tonic-gate 		vprint(DEVLINK_MID, "COUNTER is	%s\n", link);
67307c478bd9Sstevel@tonic-gate 	}
67317c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
67327c478bd9Sstevel@tonic-gate }
67337c478bd9Sstevel@tonic-gate 
67347c478bd9Sstevel@tonic-gate /*
67357c478bd9Sstevel@tonic-gate  * Compares "field" number of the comma separated list "full_name" with
67367c478bd9Sstevel@tonic-gate  * field_item.	Returns DEVFSADM_SUCCESS for match,
67377c478bd9Sstevel@tonic-gate  * DEVFSADM_FAILURE for no match.
67387c478bd9Sstevel@tonic-gate  */
67397c478bd9Sstevel@tonic-gate static int
67407c478bd9Sstevel@tonic-gate compare_field(char *full_name, char *field_item, int field)
67417c478bd9Sstevel@tonic-gate {
67427c478bd9Sstevel@tonic-gate 	--field;
67437c478bd9Sstevel@tonic-gate 	while ((*full_name != '\0') && (field != 0)) {
67447c478bd9Sstevel@tonic-gate 		if (*(full_name++) == ',') {
67457c478bd9Sstevel@tonic-gate 			field--;
67467c478bd9Sstevel@tonic-gate 		}
67477c478bd9Sstevel@tonic-gate 	}
67487c478bd9Sstevel@tonic-gate 
67497c478bd9Sstevel@tonic-gate 	if (field != 0) {
67507c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
67517c478bd9Sstevel@tonic-gate 	}
67527c478bd9Sstevel@tonic-gate 
67537c478bd9Sstevel@tonic-gate 	while ((*full_name != '\0') && (*field_item != '\0') &&
67540a653502Swroche 	    (*full_name != ',')) {
67557c478bd9Sstevel@tonic-gate 		if (*(full_name++) != *(field_item++)) {
67567c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
67577c478bd9Sstevel@tonic-gate 		}
67587c478bd9Sstevel@tonic-gate 	}
67597c478bd9Sstevel@tonic-gate 
67607c478bd9Sstevel@tonic-gate 	if (*field_item != '\0') {
67617c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
67627c478bd9Sstevel@tonic-gate 	}
67637c478bd9Sstevel@tonic-gate 
67647c478bd9Sstevel@tonic-gate 	if ((*full_name == '\0') || (*full_name == ','))
67657c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
67667c478bd9Sstevel@tonic-gate 
67677c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
67687c478bd9Sstevel@tonic-gate }
67697c478bd9Sstevel@tonic-gate 
67707c478bd9Sstevel@tonic-gate /*
67717c478bd9Sstevel@tonic-gate  * strcat() field # "field" of comma separated list "name" to "link".
67727c478bd9Sstevel@tonic-gate  * Field 0 is the entire name.
67737c478bd9Sstevel@tonic-gate  * Return DEVFSADM_SUCCESS or DEVFSADM_FAILURE.
67747c478bd9Sstevel@tonic-gate  */
67757c478bd9Sstevel@tonic-gate static int
67767c478bd9Sstevel@tonic-gate component_cat(char *link, char *name, int field)
67777c478bd9Sstevel@tonic-gate {
67787c478bd9Sstevel@tonic-gate 
67797c478bd9Sstevel@tonic-gate 	if (name == NULL) {
67807c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
67817c478bd9Sstevel@tonic-gate 	}
67827c478bd9Sstevel@tonic-gate 
67837c478bd9Sstevel@tonic-gate 	if (field == 0) {
67847c478bd9Sstevel@tonic-gate 		(void) strcat(link, name);
67857c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
67867c478bd9Sstevel@tonic-gate 	}
67877c478bd9Sstevel@tonic-gate 
67887c478bd9Sstevel@tonic-gate 	while (*link != '\0') {
67897c478bd9Sstevel@tonic-gate 		link++;
67907c478bd9Sstevel@tonic-gate 	}
67917c478bd9Sstevel@tonic-gate 
67927c478bd9Sstevel@tonic-gate 	--field;
67937c478bd9Sstevel@tonic-gate 	while ((*name != '\0') && (field != 0)) {
67947c478bd9Sstevel@tonic-gate 		if (*(name++) == ',') {
67957c478bd9Sstevel@tonic-gate 			--field;
67967c478bd9Sstevel@tonic-gate 		}
67977c478bd9Sstevel@tonic-gate 	}
67987c478bd9Sstevel@tonic-gate 
67997c478bd9Sstevel@tonic-gate 	if (field != 0) {
68007c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
68017c478bd9Sstevel@tonic-gate 	}
68027c478bd9Sstevel@tonic-gate 
68037c478bd9Sstevel@tonic-gate 	while ((*name != '\0') && (*name != ',')) {
68047c478bd9Sstevel@tonic-gate 		*(link++) = *(name++);
68057c478bd9Sstevel@tonic-gate 	}
68067c478bd9Sstevel@tonic-gate 
68077c478bd9Sstevel@tonic-gate 	*link = '\0';
68087c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
68097c478bd9Sstevel@tonic-gate }
68107c478bd9Sstevel@tonic-gate 
68117c478bd9Sstevel@tonic-gate static void
68127c478bd9Sstevel@tonic-gate free_selector_list(selector_list_t *head)
68137c478bd9Sstevel@tonic-gate {
68147c478bd9Sstevel@tonic-gate 	selector_list_t *temp;
68157c478bd9Sstevel@tonic-gate 
68167c478bd9Sstevel@tonic-gate 	while (head != NULL) {
68177c478bd9Sstevel@tonic-gate 		temp = head;
68187c478bd9Sstevel@tonic-gate 		head = head->next;
68197c478bd9Sstevel@tonic-gate 		free(temp->val);
68207c478bd9Sstevel@tonic-gate 		free(temp);
68217c478bd9Sstevel@tonic-gate 	}
68227c478bd9Sstevel@tonic-gate }
68237c478bd9Sstevel@tonic-gate 
68247c478bd9Sstevel@tonic-gate static void
68257c478bd9Sstevel@tonic-gate free_link_list(link_list_t *head)
68267c478bd9Sstevel@tonic-gate {
68277c478bd9Sstevel@tonic-gate 	link_list_t *temp;
68287c478bd9Sstevel@tonic-gate 
68297c478bd9Sstevel@tonic-gate 	while (head != NULL) {
68307c478bd9Sstevel@tonic-gate 		temp = head;
68317c478bd9Sstevel@tonic-gate 		head = head->next;
68327c478bd9Sstevel@tonic-gate 		if (temp->type == CONSTANT) {
68337c478bd9Sstevel@tonic-gate 			free(temp->constant);
68347c478bd9Sstevel@tonic-gate 		}
68357c478bd9Sstevel@tonic-gate 		free(temp);
68367c478bd9Sstevel@tonic-gate 	}
68377c478bd9Sstevel@tonic-gate }
68387c478bd9Sstevel@tonic-gate 
68397c478bd9Sstevel@tonic-gate /*
68407c478bd9Sstevel@tonic-gate  * Prints only if level matches one of the debug levels
68417c478bd9Sstevel@tonic-gate  * given on command line.  INFO_MID is always printed.
68427c478bd9Sstevel@tonic-gate  *
68437c478bd9Sstevel@tonic-gate  * See devfsadm.h for a listing of globally defined levels and
68447c478bd9Sstevel@tonic-gate  * meanings.  Modules should prefix the level with their
68457c478bd9Sstevel@tonic-gate  * module name to prevent collisions.
68467c478bd9Sstevel@tonic-gate  */
68477c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
68487c478bd9Sstevel@tonic-gate void
68497c478bd9Sstevel@tonic-gate devfsadm_print(char *msgid, char *message, ...)
68507c478bd9Sstevel@tonic-gate {
68517c478bd9Sstevel@tonic-gate 	va_list ap;
68527c478bd9Sstevel@tonic-gate 	static int newline = TRUE;
68537c478bd9Sstevel@tonic-gate 	int x;
68547c478bd9Sstevel@tonic-gate 
68557c478bd9Sstevel@tonic-gate 	if (msgid != NULL) {
68567c478bd9Sstevel@tonic-gate 		for (x = 0; x < num_verbose; x++) {
68577c478bd9Sstevel@tonic-gate 			if (strcmp(verbose[x], msgid) == 0) {
68587c478bd9Sstevel@tonic-gate 				break;
68597c478bd9Sstevel@tonic-gate 			}
68607c478bd9Sstevel@tonic-gate 			if (strcmp(verbose[x], ALL_MID) == 0) {
68617c478bd9Sstevel@tonic-gate 				break;
68627c478bd9Sstevel@tonic-gate 			}
68637c478bd9Sstevel@tonic-gate 		}
68647c478bd9Sstevel@tonic-gate 		if (x == num_verbose) {
68657c478bd9Sstevel@tonic-gate 			return;
68667c478bd9Sstevel@tonic-gate 		}
68677c478bd9Sstevel@tonic-gate 	}
68687c478bd9Sstevel@tonic-gate 
68697c478bd9Sstevel@tonic-gate 	va_start(ap, message);
68707c478bd9Sstevel@tonic-gate 
68717c478bd9Sstevel@tonic-gate 	if (msgid == NULL) {
68727c478bd9Sstevel@tonic-gate 		if (logflag == TRUE) {
68737c478bd9Sstevel@tonic-gate 			(void) vsyslog(LOG_NOTICE, message, ap);
68747c478bd9Sstevel@tonic-gate 		} else {
68757c478bd9Sstevel@tonic-gate 			(void) vfprintf(stdout, message, ap);
68767c478bd9Sstevel@tonic-gate 		}
68777c478bd9Sstevel@tonic-gate 
68787c478bd9Sstevel@tonic-gate 	} else {
68797c478bd9Sstevel@tonic-gate 		if (logflag == TRUE) {
68807c478bd9Sstevel@tonic-gate 			(void) syslog(LOG_DEBUG, "%s[%ld]: %s: ",
68810a653502Swroche 			    prog, getpid(), msgid);
68827c478bd9Sstevel@tonic-gate 			(void) vsyslog(LOG_DEBUG, message, ap);
68837c478bd9Sstevel@tonic-gate 		} else {
68847c478bd9Sstevel@tonic-gate 			if (newline == TRUE) {
68857c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "%s[%ld]: %s: ",
68860a653502Swroche 				    prog, getpid(), msgid);
68877c478bd9Sstevel@tonic-gate 			}
68887c478bd9Sstevel@tonic-gate 			(void) vfprintf(stdout, message, ap);
68897c478bd9Sstevel@tonic-gate 		}
68907c478bd9Sstevel@tonic-gate 	}
68917c478bd9Sstevel@tonic-gate 
68927c478bd9Sstevel@tonic-gate 	if (message[strlen(message) - 1] == '\n') {
68937c478bd9Sstevel@tonic-gate 		newline = TRUE;
68947c478bd9Sstevel@tonic-gate 	} else {
68957c478bd9Sstevel@tonic-gate 		newline = FALSE;
68967c478bd9Sstevel@tonic-gate 	}
68977c478bd9Sstevel@tonic-gate 	va_end(ap);
68987c478bd9Sstevel@tonic-gate }
68997c478bd9Sstevel@tonic-gate 
69007c478bd9Sstevel@tonic-gate /*
69017c478bd9Sstevel@tonic-gate  * print error messages to the terminal or to syslog
69027c478bd9Sstevel@tonic-gate  */
69037c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
69047c478bd9Sstevel@tonic-gate void
69057c478bd9Sstevel@tonic-gate devfsadm_errprint(char *message, ...)
69067c478bd9Sstevel@tonic-gate {
69077c478bd9Sstevel@tonic-gate 	va_list ap;
69087c478bd9Sstevel@tonic-gate 
69097c478bd9Sstevel@tonic-gate 	va_start(ap, message);
69107c478bd9Sstevel@tonic-gate 
69117c478bd9Sstevel@tonic-gate 	if (logflag == TRUE) {
69127c478bd9Sstevel@tonic-gate 		(void) vsyslog(LOG_ERR, message, ap);
69137c478bd9Sstevel@tonic-gate 	} else {
69147c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: ", prog);
69157c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, message, ap);
69167c478bd9Sstevel@tonic-gate 	}
69177c478bd9Sstevel@tonic-gate 	va_end(ap);
69187c478bd9Sstevel@tonic-gate }
69197c478bd9Sstevel@tonic-gate 
69207c478bd9Sstevel@tonic-gate /*
69217c478bd9Sstevel@tonic-gate  * return noupdate state (-s)
69227c478bd9Sstevel@tonic-gate  */
69237c478bd9Sstevel@tonic-gate int
69247c478bd9Sstevel@tonic-gate devfsadm_noupdate(void)
69257c478bd9Sstevel@tonic-gate {
69267c478bd9Sstevel@tonic-gate 	return (file_mods == TRUE ? DEVFSADM_TRUE : DEVFSADM_FALSE);
69277c478bd9Sstevel@tonic-gate }
69287c478bd9Sstevel@tonic-gate 
69297c478bd9Sstevel@tonic-gate /*
69307c478bd9Sstevel@tonic-gate  * return current root update path (-r)
69317c478bd9Sstevel@tonic-gate  */
69327c478bd9Sstevel@tonic-gate const char *
69337c478bd9Sstevel@tonic-gate devfsadm_root_path(void)
69347c478bd9Sstevel@tonic-gate {
69357c478bd9Sstevel@tonic-gate 	if (root_dir[0] == '\0') {
69367c478bd9Sstevel@tonic-gate 		return ("/");
69377c478bd9Sstevel@tonic-gate 	} else {
69387c478bd9Sstevel@tonic-gate 		return ((const char *)root_dir);
69397c478bd9Sstevel@tonic-gate 	}
69407c478bd9Sstevel@tonic-gate }
69417c478bd9Sstevel@tonic-gate 
69423c4226f9Spjha void
69433c4226f9Spjha devfsadm_free_dev_names(char **dev_names, int len)
69443c4226f9Spjha {
69453c4226f9Spjha 	int i;
69463c4226f9Spjha 
69473c4226f9Spjha 	for (i = 0; i < len; i++)
69483c4226f9Spjha 		free(dev_names[i]);
69493c4226f9Spjha 	free(dev_names);
69503c4226f9Spjha }
69513c4226f9Spjha 
69523c4226f9Spjha /*
69533c4226f9Spjha  * Return all devlinks corresponding to phys_path as an array of strings.
69543c4226f9Spjha  * The number of entries in the array is returned through lenp.
69553c4226f9Spjha  * devfsadm_free_dev_names() is used to free the returned array.
69563c4226f9Spjha  * NULL is returned on failure or when there are no matching devlinks.
69573c4226f9Spjha  *
69583c4226f9Spjha  * re is an extended regular expression in regex(5) format used to further
69593c4226f9Spjha  * match devlinks pointing to phys_path; it may be NULL to match all
69603c4226f9Spjha  */
69613c4226f9Spjha char **
69623c4226f9Spjha devfsadm_lookup_dev_names(char *phys_path, char *re, int *lenp)
69633c4226f9Spjha {
69643c4226f9Spjha 	struct devlink_cb_arg cb_arg;
69653c4226f9Spjha 	char **dev_names = NULL;
69663c4226f9Spjha 	int i;
69673c4226f9Spjha 
69683c4226f9Spjha 	*lenp = 0;
69693c4226f9Spjha 	cb_arg.count = 0;
69703c4226f9Spjha 	cb_arg.rv = 0;
69713c4226f9Spjha 	(void) di_devlink_cache_walk(devlink_cache, re, phys_path,
69723c4226f9Spjha 	    DI_PRIMARY_LINK, &cb_arg, devlink_cb);
69733c4226f9Spjha 
69743c4226f9Spjha 	if (cb_arg.rv == -1 || cb_arg.count <= 0)
69753c4226f9Spjha 		return (NULL);
69763c4226f9Spjha 
69773c4226f9Spjha 	dev_names = s_malloc(cb_arg.count * sizeof (char *));
69783c4226f9Spjha 	if (dev_names == NULL)
69793c4226f9Spjha 		goto out;
69803c4226f9Spjha 
69813c4226f9Spjha 	for (i = 0; i < cb_arg.count; i++) {
69823c4226f9Spjha 		dev_names[i] = s_strdup(cb_arg.dev_names[i]);
69833c4226f9Spjha 		if (dev_names[i] == NULL) {
69843c4226f9Spjha 			devfsadm_free_dev_names(dev_names, i);
69853c4226f9Spjha 			dev_names = NULL;
69863c4226f9Spjha 			goto out;
69873c4226f9Spjha 		}
69883c4226f9Spjha 	}
69893c4226f9Spjha 	*lenp = cb_arg.count;
69903c4226f9Spjha 
69913c4226f9Spjha out:
69923c4226f9Spjha 	free_dev_names(&cb_arg);
69933c4226f9Spjha 	return (dev_names);
69943c4226f9Spjha }
69953c4226f9Spjha 
69967c478bd9Sstevel@tonic-gate /* common exit function which ensures releasing locks */
69977c478bd9Sstevel@tonic-gate static void
69987c478bd9Sstevel@tonic-gate devfsadm_exit(int status)
69997c478bd9Sstevel@tonic-gate {
70007c478bd9Sstevel@tonic-gate 	if (DEVFSADM_DEBUG_ON) {
70017c478bd9Sstevel@tonic-gate 		vprint(INFO_MID, "exit status = %d\n", status);
70027c478bd9Sstevel@tonic-gate 	}
70037c478bd9Sstevel@tonic-gate 
7004537714daSvikram 	exit_dev_lock(1);
7005537714daSvikram 	exit_daemon_lock(1);
70067c478bd9Sstevel@tonic-gate 
70077c478bd9Sstevel@tonic-gate 	if (logflag == TRUE) {
70087c478bd9Sstevel@tonic-gate 		closelog();
70097c478bd9Sstevel@tonic-gate 	}
70107c478bd9Sstevel@tonic-gate 
70117c478bd9Sstevel@tonic-gate 	exit(status);
7012537714daSvikram 	/*NOTREACHED*/
70137c478bd9Sstevel@tonic-gate }
70147c478bd9Sstevel@tonic-gate 
70157c478bd9Sstevel@tonic-gate /*
7016facf4a8dSllai  * set root_dir, devices_dir, dev_dir using optarg.
70177c478bd9Sstevel@tonic-gate  */
70187c478bd9Sstevel@tonic-gate static void
7019facf4a8dSllai set_root_devices_dev_dir(char *dir)
70207c478bd9Sstevel@tonic-gate {
70217c478bd9Sstevel@tonic-gate 	size_t len;
70227c478bd9Sstevel@tonic-gate 
70237c478bd9Sstevel@tonic-gate 	root_dir = s_strdup(dir);
70247c478bd9Sstevel@tonic-gate 	len = strlen(dir) + strlen(DEVICES) + 1;
70257c478bd9Sstevel@tonic-gate 	devices_dir = s_malloc(len);
70267c478bd9Sstevel@tonic-gate 	(void) snprintf(devices_dir, len, "%s%s", root_dir, DEVICES);
70277c478bd9Sstevel@tonic-gate 	len = strlen(root_dir) + strlen(DEV) + 1;
70287c478bd9Sstevel@tonic-gate 	dev_dir = s_malloc(len);
70297c478bd9Sstevel@tonic-gate 	(void) snprintf(dev_dir, len, "%s%s", root_dir, DEV);
70307c478bd9Sstevel@tonic-gate }
70317c478bd9Sstevel@tonic-gate 
70327c478bd9Sstevel@tonic-gate /*
70337c478bd9Sstevel@tonic-gate  * Removes quotes.
70347c478bd9Sstevel@tonic-gate  */
70357c478bd9Sstevel@tonic-gate static char *
70367c478bd9Sstevel@tonic-gate dequote(char *src)
70377c478bd9Sstevel@tonic-gate {
70387c478bd9Sstevel@tonic-gate 	char	*dst;
70397c478bd9Sstevel@tonic-gate 	int	len;
70407c478bd9Sstevel@tonic-gate 
70417c478bd9Sstevel@tonic-gate 	len = strlen(src);
70427c478bd9Sstevel@tonic-gate 	dst = s_malloc(len + 1);
70437c478bd9Sstevel@tonic-gate 	if (src[0] == '\"' && src[len - 1] == '\"') {
70447c478bd9Sstevel@tonic-gate 		len -= 2;
70457c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, &src[1], len);
70467c478bd9Sstevel@tonic-gate 		dst[len] = '\0';
70477c478bd9Sstevel@tonic-gate 	} else {
70487c478bd9Sstevel@tonic-gate 		(void) strcpy(dst, src);
70497c478bd9Sstevel@tonic-gate 	}
70507c478bd9Sstevel@tonic-gate 	return (dst);
70517c478bd9Sstevel@tonic-gate }
70527c478bd9Sstevel@tonic-gate 
70537c478bd9Sstevel@tonic-gate /*
70547c478bd9Sstevel@tonic-gate  * For a given physical device pathname and spectype, return the
70557c478bd9Sstevel@tonic-gate  * ownership and permissions attributes by looking in data from
70567c478bd9Sstevel@tonic-gate  * /etc/minor_perm.  If currently in installation mode, check for
70577c478bd9Sstevel@tonic-gate  * possible major number translations from the miniroot to the installed
70587c478bd9Sstevel@tonic-gate  * root's name_to_major table. Note that there can be multiple matches,
70597c478bd9Sstevel@tonic-gate  * but the last match takes effect.  pts seems to rely on this
70607c478bd9Sstevel@tonic-gate  * implementation behavior.
70617c478bd9Sstevel@tonic-gate  */
70627c478bd9Sstevel@tonic-gate static void
70637c478bd9Sstevel@tonic-gate getattr(char *phy_path, char *aminor, int spectype, dev_t dev, mode_t *mode,
70647c478bd9Sstevel@tonic-gate 	uid_t *uid, gid_t *gid)
70657c478bd9Sstevel@tonic-gate {
70667c478bd9Sstevel@tonic-gate 	char devname[PATH_MAX + 1];
70677c478bd9Sstevel@tonic-gate 	char *node_name;
70687c478bd9Sstevel@tonic-gate 	char *minor_name;
70697c478bd9Sstevel@tonic-gate 	int match = FALSE;
70707c478bd9Sstevel@tonic-gate 	int is_clone;
70717c478bd9Sstevel@tonic-gate 	int mp_drvname_matches_node_name;
70727c478bd9Sstevel@tonic-gate 	int mp_drvname_matches_minor_name;
70737c478bd9Sstevel@tonic-gate 	int mp_drvname_is_clone;
70747c478bd9Sstevel@tonic-gate 	int mp_drvname_matches_drvname;
70757c478bd9Sstevel@tonic-gate 	struct mperm *mp;
70767c478bd9Sstevel@tonic-gate 	major_t major_no;
70777c478bd9Sstevel@tonic-gate 	char driver[PATH_MAX + 1];
70787c478bd9Sstevel@tonic-gate 
70797c478bd9Sstevel@tonic-gate 	/*
70807c478bd9Sstevel@tonic-gate 	 * Get the driver name based on the major number since the name
70817c478bd9Sstevel@tonic-gate 	 * in /devices may be generic.  Could be running with more major
70827c478bd9Sstevel@tonic-gate 	 * numbers than are in /etc/name_to_major, so get it from the kernel
70837c478bd9Sstevel@tonic-gate 	 */
70847c478bd9Sstevel@tonic-gate 	major_no = major(dev);
70857c478bd9Sstevel@tonic-gate 
70867c478bd9Sstevel@tonic-gate 	if (modctl(MODGETNAME, driver, sizeof (driver), &major_no) != 0) {
70877c478bd9Sstevel@tonic-gate 		/* return default values */
70887c478bd9Sstevel@tonic-gate 		goto use_defaults;
70897c478bd9Sstevel@tonic-gate 	}
70907c478bd9Sstevel@tonic-gate 
70917c478bd9Sstevel@tonic-gate 	(void) strcpy(devname, phy_path);
70927c478bd9Sstevel@tonic-gate 
70937c478bd9Sstevel@tonic-gate 	node_name = strrchr(devname, '/'); /* node name is the last */
70947c478bd9Sstevel@tonic-gate 					/* component */
70957c478bd9Sstevel@tonic-gate 	if (node_name == NULL) {
70967c478bd9Sstevel@tonic-gate 		err_print(NO_NODE, devname);
70977c478bd9Sstevel@tonic-gate 		goto use_defaults;
70987c478bd9Sstevel@tonic-gate 	}
70997c478bd9Sstevel@tonic-gate 
71007c478bd9Sstevel@tonic-gate 	minor_name = strchr(++node_name, '@'); /* see if it has address part */
71017c478bd9Sstevel@tonic-gate 
71027c478bd9Sstevel@tonic-gate 	if (minor_name != NULL) {
71037c478bd9Sstevel@tonic-gate 		*minor_name++ = '\0';
71047c478bd9Sstevel@tonic-gate 	} else {
71057c478bd9Sstevel@tonic-gate 		minor_name = node_name;
71067c478bd9Sstevel@tonic-gate 	}
71077c478bd9Sstevel@tonic-gate 
71087c478bd9Sstevel@tonic-gate 	minor_name = strchr(minor_name, ':'); /* look for minor name */
71097c478bd9Sstevel@tonic-gate 
71107c478bd9Sstevel@tonic-gate 	if (minor_name == NULL) {
71117c478bd9Sstevel@tonic-gate 		err_print(NO_MINOR, devname);
71127c478bd9Sstevel@tonic-gate 		goto use_defaults;
71137c478bd9Sstevel@tonic-gate 	}
71147c478bd9Sstevel@tonic-gate 	*minor_name++ = '\0';
71157c478bd9Sstevel@tonic-gate 
71167c478bd9Sstevel@tonic-gate 	/*
71177c478bd9Sstevel@tonic-gate 	 * mp->mp_drvname = device name from minor_perm
71187c478bd9Sstevel@tonic-gate 	 * mp->mp_minorname = minor part of device name from
71197c478bd9Sstevel@tonic-gate 	 * minor_perm
71207c478bd9Sstevel@tonic-gate 	 * drvname = name of driver for this device
71217c478bd9Sstevel@tonic-gate 	 */
71227c478bd9Sstevel@tonic-gate 
71237c478bd9Sstevel@tonic-gate 	is_clone = (strcmp(node_name, "clone") == 0 ? TRUE : FALSE);
71247c478bd9Sstevel@tonic-gate 	for (mp = minor_perms; mp != NULL; mp = mp->mp_next) {
71257c478bd9Sstevel@tonic-gate 		mp_drvname_matches_node_name =
71260a653502Swroche 		    (strcmp(mp->mp_drvname, node_name) == 0 ? TRUE : FALSE);
71277c478bd9Sstevel@tonic-gate 		mp_drvname_matches_minor_name =
71280a653502Swroche 		    (strcmp(mp->mp_drvname, minor_name) == 0  ? TRUE:FALSE);
71297c478bd9Sstevel@tonic-gate 		mp_drvname_is_clone =
71300a653502Swroche 		    (strcmp(mp->mp_drvname, "clone") == 0  ? TRUE : FALSE);
71317c478bd9Sstevel@tonic-gate 		mp_drvname_matches_drvname =
71320a653502Swroche 		    (strcmp(mp->mp_drvname, driver) == 0  ? TRUE : FALSE);
71337c478bd9Sstevel@tonic-gate 
71347c478bd9Sstevel@tonic-gate 		/*
71357c478bd9Sstevel@tonic-gate 		 * If one of the following cases is true, then we try to change
71367c478bd9Sstevel@tonic-gate 		 * the permissions if a "shell global pattern match" of
71377c478bd9Sstevel@tonic-gate 		 * mp_>mp_minorname matches minor_name.
71387c478bd9Sstevel@tonic-gate 		 *
71397c478bd9Sstevel@tonic-gate 		 * 1.  mp->mp_drvname matches driver.
71407c478bd9Sstevel@tonic-gate 		 *
71417c478bd9Sstevel@tonic-gate 		 * OR
71427c478bd9Sstevel@tonic-gate 		 *
71437c478bd9Sstevel@tonic-gate 		 * 2.  mp->mp_drvname matches node_name and this
71447c478bd9Sstevel@tonic-gate 		 *	name is an alias of the driver name
71457c478bd9Sstevel@tonic-gate 		 *
71467c478bd9Sstevel@tonic-gate 		 * OR
71477c478bd9Sstevel@tonic-gate 		 *
71487c478bd9Sstevel@tonic-gate 		 * 3.  /devices entry is the clone device and either
71497c478bd9Sstevel@tonic-gate 		 *	minor_perm entry is the clone device or matches
71507c478bd9Sstevel@tonic-gate 		 *	the minor part of the clone device.
71517c478bd9Sstevel@tonic-gate 		 */
71527c478bd9Sstevel@tonic-gate 
71537c478bd9Sstevel@tonic-gate 		if ((mp_drvname_matches_drvname == TRUE)||
71547c478bd9Sstevel@tonic-gate 		    ((mp_drvname_matches_node_name == TRUE) &&
71557c478bd9Sstevel@tonic-gate 		    (alias(driver, node_name) == TRUE)) ||
71567c478bd9Sstevel@tonic-gate 		    ((is_clone == TRUE) &&
71577c478bd9Sstevel@tonic-gate 		    ((mp_drvname_is_clone == TRUE) ||
71587c478bd9Sstevel@tonic-gate 		    (mp_drvname_matches_minor_name == TRUE)))) {
71597c478bd9Sstevel@tonic-gate 			/*
71607c478bd9Sstevel@tonic-gate 			 * Check that the minor part of the
71617c478bd9Sstevel@tonic-gate 			 * device name from the minor_perm
71627c478bd9Sstevel@tonic-gate 			 * entry matches and if so, set the
71637c478bd9Sstevel@tonic-gate 			 * permissions.
71647c478bd9Sstevel@tonic-gate 			 *
71657c478bd9Sstevel@tonic-gate 			 * Under real devfs, clone minor name is changed
71667c478bd9Sstevel@tonic-gate 			 * to match the driver name, but minor_perm may
71677c478bd9Sstevel@tonic-gate 			 * not match. We reconcile it here.
71687c478bd9Sstevel@tonic-gate 			 */
71697c478bd9Sstevel@tonic-gate 			if (aminor != NULL)
71707c478bd9Sstevel@tonic-gate 				minor_name = aminor;
71717c478bd9Sstevel@tonic-gate 
71727c478bd9Sstevel@tonic-gate 			if (gmatch(minor_name, mp->mp_minorname) != 0) {
71737c478bd9Sstevel@tonic-gate 				*uid = mp->mp_uid;
71747c478bd9Sstevel@tonic-gate 				*gid = mp->mp_gid;
71757c478bd9Sstevel@tonic-gate 				*mode = spectype | mp->mp_mode;
71767c478bd9Sstevel@tonic-gate 				match = TRUE;
71777c478bd9Sstevel@tonic-gate 			}
71787c478bd9Sstevel@tonic-gate 		}
71797c478bd9Sstevel@tonic-gate 	}
71807c478bd9Sstevel@tonic-gate 
71817c478bd9Sstevel@tonic-gate 	if (match == TRUE) {
71827c478bd9Sstevel@tonic-gate 		return;
71837c478bd9Sstevel@tonic-gate 	}
71847c478bd9Sstevel@tonic-gate 
71857c478bd9Sstevel@tonic-gate 	use_defaults:
71867c478bd9Sstevel@tonic-gate 	/* not found in minor_perm, so just use default values */
71877c478bd9Sstevel@tonic-gate 	*uid = root_uid;
71887c478bd9Sstevel@tonic-gate 	*gid = sys_gid;
71897c478bd9Sstevel@tonic-gate 	*mode = (spectype | 0600);
71907c478bd9Sstevel@tonic-gate }
71917c478bd9Sstevel@tonic-gate 
71927c478bd9Sstevel@tonic-gate /*
71937c478bd9Sstevel@tonic-gate  * Called by devfs_read_minor_perm() to report errors
71947c478bd9Sstevel@tonic-gate  * key is:
71957c478bd9Sstevel@tonic-gate  *	line number: ignoring line number error
71967c478bd9Sstevel@tonic-gate  *	errno: open/close errors
71977c478bd9Sstevel@tonic-gate  *	size: alloc errors
71987c478bd9Sstevel@tonic-gate  */
71997c478bd9Sstevel@tonic-gate static void
72007c478bd9Sstevel@tonic-gate minorperm_err_cb(minorperm_err_t mp_err, int key)
72017c478bd9Sstevel@tonic-gate {
72027c478bd9Sstevel@tonic-gate 	switch (mp_err) {
72037c478bd9Sstevel@tonic-gate 	case MP_FOPEN_ERR:
72047c478bd9Sstevel@tonic-gate 		err_print(FOPEN_FAILED, MINOR_PERM_FILE, strerror(key));
72057c478bd9Sstevel@tonic-gate 		break;
72067c478bd9Sstevel@tonic-gate 	case MP_FCLOSE_ERR:
72077c478bd9Sstevel@tonic-gate 		err_print(FCLOSE_FAILED, MINOR_PERM_FILE, strerror(key));
72087c478bd9Sstevel@tonic-gate 		break;
72097c478bd9Sstevel@tonic-gate 	case MP_IGNORING_LINE_ERR:
72107c478bd9Sstevel@tonic-gate 		err_print(IGNORING_LINE_IN, key, MINOR_PERM_FILE);
72117c478bd9Sstevel@tonic-gate 		break;
72127c478bd9Sstevel@tonic-gate 	case MP_ALLOC_ERR:
72137c478bd9Sstevel@tonic-gate 		err_print(MALLOC_FAILED, key);
72147c478bd9Sstevel@tonic-gate 		break;
72157c478bd9Sstevel@tonic-gate 	case MP_NVLIST_ERR:
72167c478bd9Sstevel@tonic-gate 		err_print(NVLIST_ERROR, MINOR_PERM_FILE, strerror(key));
72177c478bd9Sstevel@tonic-gate 		break;
72187c478bd9Sstevel@tonic-gate 	case MP_CANT_FIND_USER_ERR:
72197c478bd9Sstevel@tonic-gate 		err_print(CANT_FIND_USER, DEFAULT_DEV_USER);
72207c478bd9Sstevel@tonic-gate 		break;
72217c478bd9Sstevel@tonic-gate 	case MP_CANT_FIND_GROUP_ERR:
72227c478bd9Sstevel@tonic-gate 		err_print(CANT_FIND_GROUP, DEFAULT_DEV_GROUP);
72237c478bd9Sstevel@tonic-gate 		break;
72247c478bd9Sstevel@tonic-gate 	}
72257c478bd9Sstevel@tonic-gate }
72267c478bd9Sstevel@tonic-gate 
72277c478bd9Sstevel@tonic-gate static void
72287c478bd9Sstevel@tonic-gate read_minor_perm_file(void)
72297c478bd9Sstevel@tonic-gate {
72307c478bd9Sstevel@tonic-gate 	static int cached = FALSE;
72317c478bd9Sstevel@tonic-gate 	static struct stat cached_sb;
72327c478bd9Sstevel@tonic-gate 	struct stat current_sb;
72337c478bd9Sstevel@tonic-gate 
72347c478bd9Sstevel@tonic-gate 	(void) stat(MINOR_PERM_FILE, &current_sb);
72357c478bd9Sstevel@tonic-gate 
72367c478bd9Sstevel@tonic-gate 	/* If already cached, check to see if it is still valid */
72377c478bd9Sstevel@tonic-gate 	if (cached == TRUE) {
72387c478bd9Sstevel@tonic-gate 
72397c478bd9Sstevel@tonic-gate 		if (current_sb.st_mtime == cached_sb.st_mtime) {
72407c478bd9Sstevel@tonic-gate 			vprint(FILES_MID, "%s cache valid\n", MINOR_PERM_FILE);
72417c478bd9Sstevel@tonic-gate 			return;
72427c478bd9Sstevel@tonic-gate 		}
72437c478bd9Sstevel@tonic-gate 		devfs_free_minor_perm(minor_perms);
72447c478bd9Sstevel@tonic-gate 		minor_perms = NULL;
72457c478bd9Sstevel@tonic-gate 	} else {
72467c478bd9Sstevel@tonic-gate 		cached = TRUE;
72477c478bd9Sstevel@tonic-gate 	}
72487c478bd9Sstevel@tonic-gate 
72497c478bd9Sstevel@tonic-gate 	(void) stat(MINOR_PERM_FILE, &cached_sb);
72507c478bd9Sstevel@tonic-gate 
72517c478bd9Sstevel@tonic-gate 	vprint(FILES_MID, "loading binding file: %s\n", MINOR_PERM_FILE);
72527c478bd9Sstevel@tonic-gate 
72537c478bd9Sstevel@tonic-gate 	minor_perms = devfs_read_minor_perm(minorperm_err_cb);
72547c478bd9Sstevel@tonic-gate }
72557c478bd9Sstevel@tonic-gate 
72567c478bd9Sstevel@tonic-gate static void
72577c478bd9Sstevel@tonic-gate load_minor_perm_file(void)
72587c478bd9Sstevel@tonic-gate {
72597c478bd9Sstevel@tonic-gate 	read_minor_perm_file();
72607c478bd9Sstevel@tonic-gate 	if (devfs_load_minor_perm(minor_perms, minorperm_err_cb) != 0)
72617c478bd9Sstevel@tonic-gate 		err_print(gettext("minor_perm load failed\n"));
72627c478bd9Sstevel@tonic-gate }
72637c478bd9Sstevel@tonic-gate 
72647c478bd9Sstevel@tonic-gate static char *
72657c478bd9Sstevel@tonic-gate convert_to_re(char *dev)
72667c478bd9Sstevel@tonic-gate {
72677c478bd9Sstevel@tonic-gate 	char *p, *l, *out;
72687c478bd9Sstevel@tonic-gate 	int i;
72697c478bd9Sstevel@tonic-gate 
72707c478bd9Sstevel@tonic-gate 	out = s_malloc(PATH_MAX);
72717c478bd9Sstevel@tonic-gate 
72727c478bd9Sstevel@tonic-gate 	for (l = p = dev, i = 0; (*p != '\0') && (i < (PATH_MAX - 1));
72737c478bd9Sstevel@tonic-gate 	    ++p, i++) {
72747c478bd9Sstevel@tonic-gate 		if ((*p == '*') && ((l != p) && (*l == '/'))) {
72757c478bd9Sstevel@tonic-gate 			out[i++] = '.';
72767c478bd9Sstevel@tonic-gate 			out[i] = '+';
72777c478bd9Sstevel@tonic-gate 		} else {
72787c478bd9Sstevel@tonic-gate 			out[i] = *p;
72797c478bd9Sstevel@tonic-gate 		}
72807c478bd9Sstevel@tonic-gate 		l = p;
72817c478bd9Sstevel@tonic-gate 	}
72827c478bd9Sstevel@tonic-gate 	out[i] = '\0';
72837c478bd9Sstevel@tonic-gate 	p = (char *)s_malloc(strlen(out) + 1);
72847c478bd9Sstevel@tonic-gate 	(void) strlcpy(p, out, strlen(out) + 1);
72857c478bd9Sstevel@tonic-gate 	free(out);
72867c478bd9Sstevel@tonic-gate 
72877c478bd9Sstevel@tonic-gate 	vprint(FILES_MID, "converted %s -> %s\n", dev, p);
72887c478bd9Sstevel@tonic-gate 
72897c478bd9Sstevel@tonic-gate 	return (p);
72907c478bd9Sstevel@tonic-gate }
72917c478bd9Sstevel@tonic-gate 
72927c478bd9Sstevel@tonic-gate static void
72937c478bd9Sstevel@tonic-gate read_logindevperm_file(void)
72947c478bd9Sstevel@tonic-gate {
72957c478bd9Sstevel@tonic-gate 	static int cached = FALSE;
72967c478bd9Sstevel@tonic-gate 	static struct stat cached_sb;
72977c478bd9Sstevel@tonic-gate 	struct stat current_sb;
72987c478bd9Sstevel@tonic-gate 	struct login_dev *ldev;
72997c478bd9Sstevel@tonic-gate 	FILE *fp;
73007c478bd9Sstevel@tonic-gate 	char line[MAX_LDEV_LINE];
73017c478bd9Sstevel@tonic-gate 	int ln, perm, rv;
73026e670f77Saj 	char *cp, *console, *dlist, *dev;
73037c478bd9Sstevel@tonic-gate 	char *lasts, *devlasts, *permstr, *drv;
73047c478bd9Sstevel@tonic-gate 	struct driver_list *list, *next;
73057c478bd9Sstevel@tonic-gate 
73067c478bd9Sstevel@tonic-gate 	/* Read logindevperm only when enabled */
73077c478bd9Sstevel@tonic-gate 	if (login_dev_enable != TRUE)
73087c478bd9Sstevel@tonic-gate 		return;
73097c478bd9Sstevel@tonic-gate 
73107c478bd9Sstevel@tonic-gate 	if (cached == TRUE) {
73117c478bd9Sstevel@tonic-gate 		if (stat(LDEV_FILE, &current_sb) == 0 &&
73127c478bd9Sstevel@tonic-gate 		    current_sb.st_mtime == cached_sb.st_mtime) {
73137c478bd9Sstevel@tonic-gate 			vprint(FILES_MID, "%s cache valid\n", LDEV_FILE);
73147c478bd9Sstevel@tonic-gate 			return;
73157c478bd9Sstevel@tonic-gate 		}
73167c478bd9Sstevel@tonic-gate 		vprint(FILES_MID, "invalidating %s cache\n", LDEV_FILE);
73177c478bd9Sstevel@tonic-gate 		while (login_dev_cache != NULL) {
73187c478bd9Sstevel@tonic-gate 
73197c478bd9Sstevel@tonic-gate 			ldev = login_dev_cache;
73207c478bd9Sstevel@tonic-gate 			login_dev_cache = ldev->ldev_next;
73217c478bd9Sstevel@tonic-gate 			free(ldev->ldev_console);
73227c478bd9Sstevel@tonic-gate 			free(ldev->ldev_device);
73237c478bd9Sstevel@tonic-gate 			regfree(&ldev->ldev_device_regex);
73247c478bd9Sstevel@tonic-gate 			list = ldev->ldev_driver_list;
73257c478bd9Sstevel@tonic-gate 			while (list) {
73267c478bd9Sstevel@tonic-gate 				next = list->next;
73277c478bd9Sstevel@tonic-gate 				free(list);
73287c478bd9Sstevel@tonic-gate 				list = next;
73297c478bd9Sstevel@tonic-gate 			}
73307c478bd9Sstevel@tonic-gate 			free(ldev);
73317c478bd9Sstevel@tonic-gate 		}
73327c478bd9Sstevel@tonic-gate 	} else {
73337c478bd9Sstevel@tonic-gate 		cached = TRUE;
73347c478bd9Sstevel@tonic-gate 	}
73357c478bd9Sstevel@tonic-gate 
73367c478bd9Sstevel@tonic-gate 	assert(login_dev_cache == NULL);
73377c478bd9Sstevel@tonic-gate 
73387c478bd9Sstevel@tonic-gate 	if (stat(LDEV_FILE, &cached_sb) != 0) {
73397c478bd9Sstevel@tonic-gate 		cached = FALSE;
73407c478bd9Sstevel@tonic-gate 		return;
73417c478bd9Sstevel@tonic-gate 	}
73427c478bd9Sstevel@tonic-gate 
73437c478bd9Sstevel@tonic-gate 	vprint(FILES_MID, "loading file: %s\n", LDEV_FILE);
73447c478bd9Sstevel@tonic-gate 
73457c478bd9Sstevel@tonic-gate 	if ((fp = fopen(LDEV_FILE, "r")) == NULL) {
73467c478bd9Sstevel@tonic-gate 		/* Not fatal to devfsadm */
73477c478bd9Sstevel@tonic-gate 		cached = FALSE;
73487c478bd9Sstevel@tonic-gate 		err_print(FOPEN_FAILED, LDEV_FILE, strerror(errno));
73497c478bd9Sstevel@tonic-gate 		return;
73507c478bd9Sstevel@tonic-gate 	}
73517c478bd9Sstevel@tonic-gate 
73527c478bd9Sstevel@tonic-gate 	ln = 0;
73537c478bd9Sstevel@tonic-gate 	while (fgets(line, MAX_LDEV_LINE, fp) != NULL) {
73547c478bd9Sstevel@tonic-gate 		ln++;
73557c478bd9Sstevel@tonic-gate 
73567c478bd9Sstevel@tonic-gate 		/* Remove comments */
73577c478bd9Sstevel@tonic-gate 		if ((cp = strchr(line, '#')) != NULL)
73587c478bd9Sstevel@tonic-gate 			*cp = '\0';
73597c478bd9Sstevel@tonic-gate 
73607c478bd9Sstevel@tonic-gate 		if ((console = strtok_r(line, LDEV_DELIMS, &lasts)) == NULL)
73617c478bd9Sstevel@tonic-gate 			continue;	/* Blank line */
73627c478bd9Sstevel@tonic-gate 
73637c478bd9Sstevel@tonic-gate 		if ((permstr =  strtok_r(NULL, LDEV_DELIMS, &lasts)) == NULL) {
73647c478bd9Sstevel@tonic-gate 			err_print(IGNORING_LINE_IN, ln, LDEV_FILE);
73657c478bd9Sstevel@tonic-gate 			continue;	/* Malformed line */
73667c478bd9Sstevel@tonic-gate 		}
73677c478bd9Sstevel@tonic-gate 
73687c478bd9Sstevel@tonic-gate 		/*
73697c478bd9Sstevel@tonic-gate 		 * permstr is string in octal format. Convert to int
73707c478bd9Sstevel@tonic-gate 		 */
73717c478bd9Sstevel@tonic-gate 		cp = NULL;
73727c478bd9Sstevel@tonic-gate 		errno = 0;
73737c478bd9Sstevel@tonic-gate 		perm = strtol(permstr, &cp, 8);
73747c478bd9Sstevel@tonic-gate 		if (errno || perm < 0 || perm > 0777 || *cp != '\0') {
73757c478bd9Sstevel@tonic-gate 			err_print(IGNORING_LINE_IN, ln, LDEV_FILE);
73767c478bd9Sstevel@tonic-gate 			continue;
73777c478bd9Sstevel@tonic-gate 		}
73787c478bd9Sstevel@tonic-gate 
73796e670f77Saj 		if ((dlist = strtok_r(NULL, LDEV_DELIMS, &lasts)) == NULL) {
73807c478bd9Sstevel@tonic-gate 			err_print(IGNORING_LINE_IN, ln, LDEV_FILE);
73817c478bd9Sstevel@tonic-gate 			continue;
73827c478bd9Sstevel@tonic-gate 		}
73837c478bd9Sstevel@tonic-gate 
73846e670f77Saj 		dev = strtok_r(dlist, LDEV_DEV_DELIM, &devlasts);
73857c478bd9Sstevel@tonic-gate 		while (dev) {
73867c478bd9Sstevel@tonic-gate 
73877c478bd9Sstevel@tonic-gate 			ldev = (struct login_dev *)s_zalloc(
73887c478bd9Sstevel@tonic-gate 			    sizeof (struct login_dev));
73897c478bd9Sstevel@tonic-gate 			ldev->ldev_console = s_strdup(console);
73907c478bd9Sstevel@tonic-gate 			ldev->ldev_perms = perm;
73917c478bd9Sstevel@tonic-gate 
73927c478bd9Sstevel@tonic-gate 			/*
73937c478bd9Sstevel@tonic-gate 			 * the logical device name may contain '*' which
73947c478bd9Sstevel@tonic-gate 			 * we convert to a regular expression
73957c478bd9Sstevel@tonic-gate 			 */
73967c478bd9Sstevel@tonic-gate 			ldev->ldev_device = convert_to_re(dev);
73977c478bd9Sstevel@tonic-gate 			if (ldev->ldev_device &&
73987c478bd9Sstevel@tonic-gate 			    (rv = regcomp(&ldev->ldev_device_regex,
73997c478bd9Sstevel@tonic-gate 			    ldev->ldev_device, REG_EXTENDED))) {
74007c478bd9Sstevel@tonic-gate 				bzero(&ldev->ldev_device_regex,
74017c478bd9Sstevel@tonic-gate 				    sizeof (ldev->ldev_device_regex));
74027c478bd9Sstevel@tonic-gate 				err_print(REGCOMP_FAILED,
74037c478bd9Sstevel@tonic-gate 				    ldev->ldev_device, rv);
74047c478bd9Sstevel@tonic-gate 			}
74057c478bd9Sstevel@tonic-gate 			ldev->ldev_next = login_dev_cache;
74067c478bd9Sstevel@tonic-gate 			login_dev_cache = ldev;
74077c478bd9Sstevel@tonic-gate 			dev = strtok_r(NULL, LDEV_DEV_DELIM, &devlasts);
74087c478bd9Sstevel@tonic-gate 		}
74097c478bd9Sstevel@tonic-gate 
74107c478bd9Sstevel@tonic-gate 		drv = strtok_r(NULL, LDEV_DRVLIST_DELIMS, &lasts);
74117c478bd9Sstevel@tonic-gate 		if (drv) {
74127c478bd9Sstevel@tonic-gate 			if (strcmp(drv, LDEV_DRVLIST_NAME) == 0) {
74137c478bd9Sstevel@tonic-gate 
74140a653502Swroche 				drv = strtok_r(NULL, LDEV_DRV_DELIMS, &lasts);
74157c478bd9Sstevel@tonic-gate 
74167c478bd9Sstevel@tonic-gate 				while (drv) {
74177c478bd9Sstevel@tonic-gate 					vprint(FILES_MID,
74180a653502Swroche 					    "logindevperm driver=%s\n", drv);
74197c478bd9Sstevel@tonic-gate 
74207c478bd9Sstevel@tonic-gate 					/*
74217c478bd9Sstevel@tonic-gate 					 * create a linked list of driver
74227c478bd9Sstevel@tonic-gate 					 * names
74237c478bd9Sstevel@tonic-gate 					 */
74247c478bd9Sstevel@tonic-gate 					list = (struct driver_list *)
74257c478bd9Sstevel@tonic-gate 					    s_zalloc(
74267c478bd9Sstevel@tonic-gate 					    sizeof (struct driver_list));
74277c478bd9Sstevel@tonic-gate 					(void) strlcpy(list->driver_name, drv,
74287c478bd9Sstevel@tonic-gate 					    sizeof (list->driver_name));
74297c478bd9Sstevel@tonic-gate 					list->next = ldev->ldev_driver_list;
74307c478bd9Sstevel@tonic-gate 					ldev->ldev_driver_list = list;
74317c478bd9Sstevel@tonic-gate 					drv = strtok_r(NULL, LDEV_DRV_DELIMS,
74327c478bd9Sstevel@tonic-gate 					    &lasts);
74337c478bd9Sstevel@tonic-gate 				}
74347c478bd9Sstevel@tonic-gate 			}
74357c478bd9Sstevel@tonic-gate 		}
74367c478bd9Sstevel@tonic-gate 	}
74377c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
74387c478bd9Sstevel@tonic-gate }
74397c478bd9Sstevel@tonic-gate 
74407c478bd9Sstevel@tonic-gate /*
74417c478bd9Sstevel@tonic-gate  * Tokens are separated by ' ', '\t', ':', '=', '&', '|', ';', '\n', or '\0'
74427c478bd9Sstevel@tonic-gate  *
74437c478bd9Sstevel@tonic-gate  * Returns DEVFSADM_SUCCESS if token found, DEVFSADM_FAILURE otherwise.
74447c478bd9Sstevel@tonic-gate  */
74457c478bd9Sstevel@tonic-gate static int
74467c478bd9Sstevel@tonic-gate getnexttoken(char *next, char **nextp, char **tokenpp, char *tchar)
74477c478bd9Sstevel@tonic-gate {
74487c478bd9Sstevel@tonic-gate 	char *cp;
74497c478bd9Sstevel@tonic-gate 	char *cp1;
74507c478bd9Sstevel@tonic-gate 	char *tokenp;
74517c478bd9Sstevel@tonic-gate 
74527c478bd9Sstevel@tonic-gate 	cp = next;
74537c478bd9Sstevel@tonic-gate 	while (*cp == ' ' || *cp == '\t') {
74547c478bd9Sstevel@tonic-gate 		cp++;			/* skip leading spaces */
74557c478bd9Sstevel@tonic-gate 	}
74567c478bd9Sstevel@tonic-gate 	tokenp = cp;			/* start of token */
74577c478bd9Sstevel@tonic-gate 	while (*cp != '\0' && *cp != '\n' && *cp != ' ' && *cp != '\t' &&
74580a653502Swroche 	    *cp != ':' && *cp != '=' && *cp != '&' &&
74590a653502Swroche 	    *cp != '|' && *cp != ';') {
74607c478bd9Sstevel@tonic-gate 		cp++;			/* point to next character */
74617c478bd9Sstevel@tonic-gate 	}
74627c478bd9Sstevel@tonic-gate 	/*
74637c478bd9Sstevel@tonic-gate 	 * If terminating character is a space or tab, look ahead to see if
74647c478bd9Sstevel@tonic-gate 	 * there's another terminator that's not a space or a tab.
74657c478bd9Sstevel@tonic-gate 	 * (This code handles trailing spaces.)
74667c478bd9Sstevel@tonic-gate 	 */
74677c478bd9Sstevel@tonic-gate 	if (*cp == ' ' || *cp == '\t') {
74687c478bd9Sstevel@tonic-gate 		cp1 = cp;
74697c478bd9Sstevel@tonic-gate 		while (*++cp1 == ' ' || *cp1 == '\t')
74707c478bd9Sstevel@tonic-gate 			;
74717c478bd9Sstevel@tonic-gate 		if (*cp1 == '=' || *cp1 == ':' || *cp1 == '&' || *cp1 == '|' ||
74720a653502Swroche 		    *cp1 == ';' || *cp1 == '\n' || *cp1 == '\0') {
74737c478bd9Sstevel@tonic-gate 			*cp = NULL;	/* terminate token */
74747c478bd9Sstevel@tonic-gate 			cp = cp1;
74757c478bd9Sstevel@tonic-gate 		}
74767c478bd9Sstevel@tonic-gate 	}
74777c478bd9Sstevel@tonic-gate 	if (tchar != NULL) {
74787c478bd9Sstevel@tonic-gate 		*tchar = *cp;		/* save terminating character */
74797c478bd9Sstevel@tonic-gate 		if (*tchar == '\0') {
74807c478bd9Sstevel@tonic-gate 			*tchar = '\n';
74817c478bd9Sstevel@tonic-gate 		}
74827c478bd9Sstevel@tonic-gate 	}
74837c478bd9Sstevel@tonic-gate 	*cp++ = '\0';			/* terminate token, point to next */
74847c478bd9Sstevel@tonic-gate 	*nextp = cp;			/* set pointer to next character */
74857c478bd9Sstevel@tonic-gate 	if (cp - tokenp - 1 == 0) {
74867c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
74877c478bd9Sstevel@tonic-gate 	}
74887c478bd9Sstevel@tonic-gate 	*tokenpp = tokenp;
74897c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
74907c478bd9Sstevel@tonic-gate }
74917c478bd9Sstevel@tonic-gate 
74927c478bd9Sstevel@tonic-gate /*
74937c478bd9Sstevel@tonic-gate  * read or reread the driver aliases file
74947c478bd9Sstevel@tonic-gate  */
74957c478bd9Sstevel@tonic-gate static void
74967c478bd9Sstevel@tonic-gate read_driver_aliases_file(void)
74977c478bd9Sstevel@tonic-gate {
74987c478bd9Sstevel@tonic-gate 
74997c478bd9Sstevel@tonic-gate 	driver_alias_t *save;
75007c478bd9Sstevel@tonic-gate 	driver_alias_t *lst_tail;
75017c478bd9Sstevel@tonic-gate 	driver_alias_t *ap;
75027c478bd9Sstevel@tonic-gate 	static int cached = FALSE;
75037c478bd9Sstevel@tonic-gate 	FILE *afd;
75047c478bd9Sstevel@tonic-gate 	char line[256];
75057c478bd9Sstevel@tonic-gate 	char *cp;
75067c478bd9Sstevel@tonic-gate 	char *p;
75077c478bd9Sstevel@tonic-gate 	char t;
75087c478bd9Sstevel@tonic-gate 	int ln = 0;
75097c478bd9Sstevel@tonic-gate 	static struct stat cached_sb;
75107c478bd9Sstevel@tonic-gate 	struct stat current_sb;
75117c478bd9Sstevel@tonic-gate 
75127c478bd9Sstevel@tonic-gate 	(void) stat(ALIASFILE, &current_sb);
75137c478bd9Sstevel@tonic-gate 
75147c478bd9Sstevel@tonic-gate 	/* If already cached, check to see if it is still valid */
75157c478bd9Sstevel@tonic-gate 	if (cached == TRUE) {
75167c478bd9Sstevel@tonic-gate 
75177c478bd9Sstevel@tonic-gate 		if (current_sb.st_mtime == cached_sb.st_mtime) {
75187c478bd9Sstevel@tonic-gate 			vprint(FILES_MID, "%s cache valid\n", ALIASFILE);
75197c478bd9Sstevel@tonic-gate 			return;
75207c478bd9Sstevel@tonic-gate 		}
75217c478bd9Sstevel@tonic-gate 
75227c478bd9Sstevel@tonic-gate 		vprint(FILES_MID, "invalidating %s cache\n", ALIASFILE);
75237c478bd9Sstevel@tonic-gate 		while (driver_aliases != NULL) {
75247c478bd9Sstevel@tonic-gate 			free(driver_aliases->alias_name);
75257c478bd9Sstevel@tonic-gate 			free(driver_aliases->driver_name);
75267c478bd9Sstevel@tonic-gate 			save = driver_aliases;
75277c478bd9Sstevel@tonic-gate 			driver_aliases = driver_aliases->next;
75287c478bd9Sstevel@tonic-gate 			free(save);
75297c478bd9Sstevel@tonic-gate 		}
75307c478bd9Sstevel@tonic-gate 	} else {
75317c478bd9Sstevel@tonic-gate 		cached = TRUE;
75327c478bd9Sstevel@tonic-gate 	}
75337c478bd9Sstevel@tonic-gate 
75347c478bd9Sstevel@tonic-gate 	(void) stat(ALIASFILE, &cached_sb);
75357c478bd9Sstevel@tonic-gate 
75367c478bd9Sstevel@tonic-gate 	vprint(FILES_MID, "loading binding file: %s\n", ALIASFILE);
75377c478bd9Sstevel@tonic-gate 
75387c478bd9Sstevel@tonic-gate 	if ((afd = fopen(ALIASFILE, "r")) == NULL) {
75397c478bd9Sstevel@tonic-gate 		err_print(FOPEN_FAILED, ALIASFILE, strerror(errno));
75407c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
7541537714daSvikram 		/*NOTREACHED*/
75427c478bd9Sstevel@tonic-gate 	}
75437c478bd9Sstevel@tonic-gate 
75441ca93273Seota 	while (fgets(line, sizeof (line), afd) != NULL) {
75457c478bd9Sstevel@tonic-gate 		ln++;
75461ca93273Seota 		/* cut off comments starting with '#' */
75471ca93273Seota 		if ((cp = strchr(line, '#')) != NULL)
75481ca93273Seota 			*cp = '\0';
75491ca93273Seota 		/* ignore comment or blank lines */
75501ca93273Seota 		if (is_blank(line))
75511ca93273Seota 			continue;
75527c478bd9Sstevel@tonic-gate 		cp = line;
75537c478bd9Sstevel@tonic-gate 		if (getnexttoken(cp, &cp, &p, &t) == DEVFSADM_FAILURE) {
75547c478bd9Sstevel@tonic-gate 			err_print(IGNORING_LINE_IN, ln, ALIASFILE);
75557c478bd9Sstevel@tonic-gate 			continue;
75567c478bd9Sstevel@tonic-gate 		}
75577c478bd9Sstevel@tonic-gate 		if (t == '\n' || t == '\0') {
75587c478bd9Sstevel@tonic-gate 			err_print(DRV_BUT_NO_ALIAS, ln, ALIASFILE);
75597c478bd9Sstevel@tonic-gate 			continue;
75607c478bd9Sstevel@tonic-gate 		}
75617c478bd9Sstevel@tonic-gate 		ap = (struct driver_alias *)
75620a653502Swroche 		    s_zalloc(sizeof (struct driver_alias));
75637c478bd9Sstevel@tonic-gate 		ap->driver_name = s_strdup(p);
75647c478bd9Sstevel@tonic-gate 		if (getnexttoken(cp, &cp, &p, &t) == DEVFSADM_FAILURE) {
75657c478bd9Sstevel@tonic-gate 			err_print(DRV_BUT_NO_ALIAS, ln, ALIASFILE);
75667c478bd9Sstevel@tonic-gate 			free(ap->driver_name);
75677c478bd9Sstevel@tonic-gate 			free(ap);
75687c478bd9Sstevel@tonic-gate 			continue;
75697c478bd9Sstevel@tonic-gate 		}
75707c478bd9Sstevel@tonic-gate 		if (*p == '"') {
75717c478bd9Sstevel@tonic-gate 			if (p[strlen(p) - 1] == '"') {
75727c478bd9Sstevel@tonic-gate 				p[strlen(p) - 1] = '\0';
75737c478bd9Sstevel@tonic-gate 				p++;
75747c478bd9Sstevel@tonic-gate 			}
75757c478bd9Sstevel@tonic-gate 		}
75767c478bd9Sstevel@tonic-gate 		ap->alias_name = s_strdup(p);
75777c478bd9Sstevel@tonic-gate 		if (driver_aliases == NULL) {
75787c478bd9Sstevel@tonic-gate 			driver_aliases = ap;
75797c478bd9Sstevel@tonic-gate 			lst_tail = ap;
75807c478bd9Sstevel@tonic-gate 		} else {
75817c478bd9Sstevel@tonic-gate 			lst_tail->next = ap;
75827c478bd9Sstevel@tonic-gate 			lst_tail = ap;
75837c478bd9Sstevel@tonic-gate 		}
75847c478bd9Sstevel@tonic-gate 	}
75857c478bd9Sstevel@tonic-gate 	if (fclose(afd) == EOF) {
75867c478bd9Sstevel@tonic-gate 		err_print(FCLOSE_FAILED, ALIASFILE, strerror(errno));
75877c478bd9Sstevel@tonic-gate 	}
75887c478bd9Sstevel@tonic-gate }
75897c478bd9Sstevel@tonic-gate 
75907c478bd9Sstevel@tonic-gate /*
75917c478bd9Sstevel@tonic-gate  * return TRUE if alias_name is an alias for driver_name, otherwise
75927c478bd9Sstevel@tonic-gate  * return FALSE.
75937c478bd9Sstevel@tonic-gate  */
75947c478bd9Sstevel@tonic-gate static int
75957c478bd9Sstevel@tonic-gate alias(char *driver_name, char *alias_name)
75967c478bd9Sstevel@tonic-gate {
75977c478bd9Sstevel@tonic-gate 	driver_alias_t *alias;
75987c478bd9Sstevel@tonic-gate 
75997c478bd9Sstevel@tonic-gate 	/*
76007c478bd9Sstevel@tonic-gate 	 * check for a match
76017c478bd9Sstevel@tonic-gate 	 */
76027c478bd9Sstevel@tonic-gate 	for (alias = driver_aliases; alias != NULL; alias = alias->next) {
76037c478bd9Sstevel@tonic-gate 		if ((strcmp(alias->driver_name, driver_name) == 0) &&
76047c478bd9Sstevel@tonic-gate 		    (strcmp(alias->alias_name, alias_name) == 0)) {
76057c478bd9Sstevel@tonic-gate 			return (TRUE);
76067c478bd9Sstevel@tonic-gate 		}
76077c478bd9Sstevel@tonic-gate 	}
76087c478bd9Sstevel@tonic-gate 	return (FALSE);
76097c478bd9Sstevel@tonic-gate }
76107c478bd9Sstevel@tonic-gate 
76117c478bd9Sstevel@tonic-gate /*
76127c478bd9Sstevel@tonic-gate  * convenience functions
76137c478bd9Sstevel@tonic-gate  */
7614facf4a8dSllai static int
7615facf4a8dSllai s_stat(const char *path, struct stat *sbufp)
7616facf4a8dSllai {
7617facf4a8dSllai 	int rv;
7618facf4a8dSllai retry:
7619facf4a8dSllai 	if ((rv = stat(path, sbufp)) == -1) {
7620facf4a8dSllai 		if (errno == EINTR)
7621facf4a8dSllai 			goto retry;
7622facf4a8dSllai 	}
7623facf4a8dSllai 	return (rv);
7624facf4a8dSllai }
7625facf4a8dSllai 
76267c478bd9Sstevel@tonic-gate static void *
76277c478bd9Sstevel@tonic-gate s_malloc(const size_t size)
76287c478bd9Sstevel@tonic-gate {
76297c478bd9Sstevel@tonic-gate 	void *rp;
76307c478bd9Sstevel@tonic-gate 
76317c478bd9Sstevel@tonic-gate 	rp = malloc(size);
76327c478bd9Sstevel@tonic-gate 	if (rp == NULL) {
76337c478bd9Sstevel@tonic-gate 		err_print(MALLOC_FAILED, size);
76347c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
7635537714daSvikram 		/*NOTREACHED*/
76367c478bd9Sstevel@tonic-gate 	}
76377c478bd9Sstevel@tonic-gate 	return (rp);
76387c478bd9Sstevel@tonic-gate }
76397c478bd9Sstevel@tonic-gate 
76407c478bd9Sstevel@tonic-gate /*
76417c478bd9Sstevel@tonic-gate  * convenience functions
76427c478bd9Sstevel@tonic-gate  */
76437c478bd9Sstevel@tonic-gate static void *
76447c478bd9Sstevel@tonic-gate s_realloc(void *ptr, const size_t size)
76457c478bd9Sstevel@tonic-gate {
76467c478bd9Sstevel@tonic-gate 	ptr = realloc(ptr, size);
76477c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
76487c478bd9Sstevel@tonic-gate 		err_print(REALLOC_FAILED, size);
76497c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
7650537714daSvikram 		/*NOTREACHED*/
76517c478bd9Sstevel@tonic-gate 	}
76527c478bd9Sstevel@tonic-gate 	return (ptr);
76537c478bd9Sstevel@tonic-gate }
76547c478bd9Sstevel@tonic-gate 
76557c478bd9Sstevel@tonic-gate static void *
76567c478bd9Sstevel@tonic-gate s_zalloc(const size_t size)
76577c478bd9Sstevel@tonic-gate {
76587c478bd9Sstevel@tonic-gate 	void *rp;
76597c478bd9Sstevel@tonic-gate 
76607c478bd9Sstevel@tonic-gate 	rp = calloc(1, size);
76617c478bd9Sstevel@tonic-gate 	if (rp == NULL) {
76627c478bd9Sstevel@tonic-gate 		err_print(CALLOC_FAILED, size);
76637c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
7664537714daSvikram 		/*NOTREACHED*/
76657c478bd9Sstevel@tonic-gate 	}
76667c478bd9Sstevel@tonic-gate 	return (rp);
76677c478bd9Sstevel@tonic-gate }
76687c478bd9Sstevel@tonic-gate 
76697c478bd9Sstevel@tonic-gate char *
76707c478bd9Sstevel@tonic-gate s_strdup(const char *ptr)
76717c478bd9Sstevel@tonic-gate {
76727c478bd9Sstevel@tonic-gate 	void *rp;
76737c478bd9Sstevel@tonic-gate 
76747c478bd9Sstevel@tonic-gate 	rp = strdup(ptr);
76757c478bd9Sstevel@tonic-gate 	if (rp == NULL) {
76767c478bd9Sstevel@tonic-gate 		err_print(STRDUP_FAILED, ptr);
76777c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
7678537714daSvikram 		/*NOTREACHED*/
76797c478bd9Sstevel@tonic-gate 	}
76807c478bd9Sstevel@tonic-gate 	return (rp);
76817c478bd9Sstevel@tonic-gate }
76827c478bd9Sstevel@tonic-gate 
76837c478bd9Sstevel@tonic-gate static void
76847c478bd9Sstevel@tonic-gate s_closedir(DIR *dirp)
76857c478bd9Sstevel@tonic-gate {
76867c478bd9Sstevel@tonic-gate retry:
76877c478bd9Sstevel@tonic-gate 	if (closedir(dirp) != 0) {
76887c478bd9Sstevel@tonic-gate 		if (errno == EINTR)
76897c478bd9Sstevel@tonic-gate 			goto retry;
76907c478bd9Sstevel@tonic-gate 		err_print(CLOSEDIR_FAILED, strerror(errno));
76917c478bd9Sstevel@tonic-gate 	}
76927c478bd9Sstevel@tonic-gate }
76937c478bd9Sstevel@tonic-gate 
76947c478bd9Sstevel@tonic-gate static void
76957c478bd9Sstevel@tonic-gate s_mkdirp(const char *path, const mode_t mode)
76967c478bd9Sstevel@tonic-gate {
76977c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "mkdirp(%s, 0x%lx)\n", path, mode);
76987c478bd9Sstevel@tonic-gate 	if (mkdirp(path, mode) == -1) {
76997c478bd9Sstevel@tonic-gate 		if (errno != EEXIST) {
77007c478bd9Sstevel@tonic-gate 			err_print(MKDIR_FAILED, path, mode, strerror(errno));
77017c478bd9Sstevel@tonic-gate 		}
77027c478bd9Sstevel@tonic-gate 	}
77037c478bd9Sstevel@tonic-gate }
77047c478bd9Sstevel@tonic-gate 
77057c478bd9Sstevel@tonic-gate static void
77067c478bd9Sstevel@tonic-gate s_unlink(const char *file)
77077c478bd9Sstevel@tonic-gate {
77087c478bd9Sstevel@tonic-gate retry:
77097c478bd9Sstevel@tonic-gate 	if (unlink(file) == -1) {
77107c478bd9Sstevel@tonic-gate 		if (errno == EINTR || errno == EAGAIN)
77117c478bd9Sstevel@tonic-gate 			goto retry;
77127c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
77137c478bd9Sstevel@tonic-gate 			err_print(UNLINK_FAILED, file, strerror(errno));
77147c478bd9Sstevel@tonic-gate 		}
77157c478bd9Sstevel@tonic-gate 	}
77167c478bd9Sstevel@tonic-gate }
77177c478bd9Sstevel@tonic-gate 
77187c478bd9Sstevel@tonic-gate static void
77197c478bd9Sstevel@tonic-gate add_verbose_id(char *mid)
77207c478bd9Sstevel@tonic-gate {
77217c478bd9Sstevel@tonic-gate 	num_verbose++;
77227c478bd9Sstevel@tonic-gate 	verbose = s_realloc(verbose, num_verbose * sizeof (char *));
77237c478bd9Sstevel@tonic-gate 	verbose[num_verbose - 1] = mid;
77247c478bd9Sstevel@tonic-gate }
77257c478bd9Sstevel@tonic-gate 
77267c478bd9Sstevel@tonic-gate /*
77277c478bd9Sstevel@tonic-gate  * returns DEVFSADM_TRUE if contents is a minor node in /devices.
77287c478bd9Sstevel@tonic-gate  * If mn_root is not NULL, mn_root is set to:
77297c478bd9Sstevel@tonic-gate  *	if contents is a /dev node, mn_root = contents
77307c478bd9Sstevel@tonic-gate  * 			OR
77317c478bd9Sstevel@tonic-gate  *	if contents is a /devices node, mn_root set to the '/'
77327c478bd9Sstevel@tonic-gate  *	following /devices.
77337c478bd9Sstevel@tonic-gate  */
77347c478bd9Sstevel@tonic-gate static int
77357c478bd9Sstevel@tonic-gate is_minor_node(char *contents, char **mn_root)
77367c478bd9Sstevel@tonic-gate {
77377c478bd9Sstevel@tonic-gate 	char *ptr;
77387c478bd9Sstevel@tonic-gate 	char device_prefix[100];
77397c478bd9Sstevel@tonic-gate 
77407c478bd9Sstevel@tonic-gate 	(void) snprintf(device_prefix, sizeof (device_prefix), "../devices/");
77417c478bd9Sstevel@tonic-gate 
77427c478bd9Sstevel@tonic-gate 	if ((ptr = strstr(contents, device_prefix)) != NULL) {
77437c478bd9Sstevel@tonic-gate 		if (mn_root != NULL) {
77447c478bd9Sstevel@tonic-gate 			/* mn_root should point to the / following /devices */
77457c478bd9Sstevel@tonic-gate 			*mn_root = ptr += strlen(device_prefix) - 1;
77467c478bd9Sstevel@tonic-gate 		}
77477c478bd9Sstevel@tonic-gate 		return (DEVFSADM_TRUE);
77487c478bd9Sstevel@tonic-gate 	}
77497c478bd9Sstevel@tonic-gate 
77507c478bd9Sstevel@tonic-gate 	(void) snprintf(device_prefix, sizeof (device_prefix), "/devices/");
77517c478bd9Sstevel@tonic-gate 
77527c478bd9Sstevel@tonic-gate 	if (strncmp(contents, device_prefix, strlen(device_prefix)) == 0) {
77537c478bd9Sstevel@tonic-gate 		if (mn_root != NULL) {
77547c478bd9Sstevel@tonic-gate 			/* mn_root should point to the / following /devices */
77557c478bd9Sstevel@tonic-gate 			*mn_root = contents + strlen(device_prefix) - 1;
77567c478bd9Sstevel@tonic-gate 		}
77577c478bd9Sstevel@tonic-gate 		return (DEVFSADM_TRUE);
77587c478bd9Sstevel@tonic-gate 	}
77597c478bd9Sstevel@tonic-gate 
77607c478bd9Sstevel@tonic-gate 	if (mn_root != NULL) {
77617c478bd9Sstevel@tonic-gate 		*mn_root = contents;
77627c478bd9Sstevel@tonic-gate 	}
77637c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FALSE);
77647c478bd9Sstevel@tonic-gate }
77657c478bd9Sstevel@tonic-gate 
77667c478bd9Sstevel@tonic-gate /*
77677c478bd9Sstevel@tonic-gate  * Add the specified property to nvl.
77687c478bd9Sstevel@tonic-gate  * Returns:
77697c478bd9Sstevel@tonic-gate  *   0	successfully added
77707c478bd9Sstevel@tonic-gate  *   -1	an error occurred
77717c478bd9Sstevel@tonic-gate  *   1	could not add the property for reasons not due to errors.
77727c478bd9Sstevel@tonic-gate  */
77737c478bd9Sstevel@tonic-gate static int
77747c478bd9Sstevel@tonic-gate add_property(nvlist_t *nvl, di_prop_t prop)
77757c478bd9Sstevel@tonic-gate {
77767c478bd9Sstevel@tonic-gate 	char *name;
77777c478bd9Sstevel@tonic-gate 	char *attr_name;
77787c478bd9Sstevel@tonic-gate 	int n, len;
77797c478bd9Sstevel@tonic-gate 	int32_t *int32p;
77807c478bd9Sstevel@tonic-gate 	int64_t *int64p;
77817c478bd9Sstevel@tonic-gate 	char *str;
77827c478bd9Sstevel@tonic-gate 	char **strarray;
77837c478bd9Sstevel@tonic-gate 	uchar_t *bytep;
77847c478bd9Sstevel@tonic-gate 	int rv = 0;
77857c478bd9Sstevel@tonic-gate 	int i;
77867c478bd9Sstevel@tonic-gate 
77877c478bd9Sstevel@tonic-gate 	if ((name = di_prop_name(prop)) == NULL)
77887c478bd9Sstevel@tonic-gate 		return (-1);
77897c478bd9Sstevel@tonic-gate 
77907c478bd9Sstevel@tonic-gate 	len = sizeof (DEV_PROP_PREFIX) + strlen(name);
77917c478bd9Sstevel@tonic-gate 	if ((attr_name = malloc(len)) == NULL)
77927c478bd9Sstevel@tonic-gate 		return (-1);
77937c478bd9Sstevel@tonic-gate 
77947c478bd9Sstevel@tonic-gate 	(void) strlcpy(attr_name, DEV_PROP_PREFIX, len);
77957c478bd9Sstevel@tonic-gate 	(void) strlcat(attr_name, name, len);
77967c478bd9Sstevel@tonic-gate 
77977c478bd9Sstevel@tonic-gate 	switch (di_prop_type(prop)) {
77987c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_BOOLEAN:
77997c478bd9Sstevel@tonic-gate 		if (nvlist_add_boolean(nvl, attr_name) != 0)
78007c478bd9Sstevel@tonic-gate 			goto out;
78017c478bd9Sstevel@tonic-gate 		break;
78027c478bd9Sstevel@tonic-gate 
78037c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_INT:
78047c478bd9Sstevel@tonic-gate 		if ((n = di_prop_ints(prop, &int32p)) < 1)
78057c478bd9Sstevel@tonic-gate 			goto out;
78067c478bd9Sstevel@tonic-gate 
78077c478bd9Sstevel@tonic-gate 		if (n <= (PROP_LEN_LIMIT / sizeof (int32_t))) {
78087c478bd9Sstevel@tonic-gate 			if (nvlist_add_int32_array(nvl, attr_name, int32p,
78097c478bd9Sstevel@tonic-gate 			    n) != 0)
78107c478bd9Sstevel@tonic-gate 				goto out;
78117c478bd9Sstevel@tonic-gate 		} else
78127c478bd9Sstevel@tonic-gate 			rv = 1;
78137c478bd9Sstevel@tonic-gate 		break;
78147c478bd9Sstevel@tonic-gate 
78157c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_INT64:
78167c478bd9Sstevel@tonic-gate 		if ((n = di_prop_int64(prop, &int64p)) < 1)
78177c478bd9Sstevel@tonic-gate 			goto out;
78187c478bd9Sstevel@tonic-gate 
78197c478bd9Sstevel@tonic-gate 		if (n <= (PROP_LEN_LIMIT / sizeof (int64_t))) {
78207c478bd9Sstevel@tonic-gate 			if (nvlist_add_int64_array(nvl, attr_name, int64p,
78217c478bd9Sstevel@tonic-gate 			    n) != 0)
78227c478bd9Sstevel@tonic-gate 				goto out;
78237c478bd9Sstevel@tonic-gate 		} else
78247c478bd9Sstevel@tonic-gate 			rv = 1;
78257c478bd9Sstevel@tonic-gate 		break;
78267c478bd9Sstevel@tonic-gate 
78277c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_BYTE:
78287c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_UNKNOWN:
78297c478bd9Sstevel@tonic-gate 		if ((n = di_prop_bytes(prop, &bytep)) < 1)
78307c478bd9Sstevel@tonic-gate 			goto out;
78317c478bd9Sstevel@tonic-gate 
78327c478bd9Sstevel@tonic-gate 		if (n <= PROP_LEN_LIMIT) {
78337c478bd9Sstevel@tonic-gate 			if (nvlist_add_byte_array(nvl, attr_name, bytep, n)
78347c478bd9Sstevel@tonic-gate 			    != 0)
78357c478bd9Sstevel@tonic-gate 				goto out;
78367c478bd9Sstevel@tonic-gate 		} else
78377c478bd9Sstevel@tonic-gate 			rv = 1;
78387c478bd9Sstevel@tonic-gate 		break;
78397c478bd9Sstevel@tonic-gate 
78407c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_STRING:
78417c478bd9Sstevel@tonic-gate 		if ((n = di_prop_strings(prop, &str)) < 1)
78427c478bd9Sstevel@tonic-gate 			goto out;
78437c478bd9Sstevel@tonic-gate 
78447c478bd9Sstevel@tonic-gate 		if ((strarray = malloc(n * sizeof (char *))) == NULL)
78457c478bd9Sstevel@tonic-gate 			goto out;
78467c478bd9Sstevel@tonic-gate 
78477c478bd9Sstevel@tonic-gate 		len = 0;
78487c478bd9Sstevel@tonic-gate 		for (i = 0; i < n; i++) {
78497c478bd9Sstevel@tonic-gate 			strarray[i] = str + len;
78507c478bd9Sstevel@tonic-gate 			len += strlen(strarray[i]) + 1;
78517c478bd9Sstevel@tonic-gate 		}
78527c478bd9Sstevel@tonic-gate 
78537c478bd9Sstevel@tonic-gate 		if (len <= PROP_LEN_LIMIT) {
78547c478bd9Sstevel@tonic-gate 			if (nvlist_add_string_array(nvl, attr_name, strarray,
78557c478bd9Sstevel@tonic-gate 			    n) != 0) {
78567c478bd9Sstevel@tonic-gate 				free(strarray);
78577c478bd9Sstevel@tonic-gate 				goto out;
78587c478bd9Sstevel@tonic-gate 			}
78597c478bd9Sstevel@tonic-gate 		} else
78607c478bd9Sstevel@tonic-gate 			rv = 1;
78617c478bd9Sstevel@tonic-gate 		free(strarray);
78627c478bd9Sstevel@tonic-gate 		break;
78637c478bd9Sstevel@tonic-gate 
78647c478bd9Sstevel@tonic-gate 	default:
78657c478bd9Sstevel@tonic-gate 		rv = 1;
78667c478bd9Sstevel@tonic-gate 		break;
78677c478bd9Sstevel@tonic-gate 	}
78687c478bd9Sstevel@tonic-gate 
78697c478bd9Sstevel@tonic-gate 	free(attr_name);
78707c478bd9Sstevel@tonic-gate 	return (rv);
78717c478bd9Sstevel@tonic-gate 
78727c478bd9Sstevel@tonic-gate out:
78737c478bd9Sstevel@tonic-gate 	free(attr_name);
78747c478bd9Sstevel@tonic-gate 	return (-1);
78757c478bd9Sstevel@tonic-gate }
78767c478bd9Sstevel@tonic-gate 
78777c478bd9Sstevel@tonic-gate static void
78787c478bd9Sstevel@tonic-gate free_dev_names(struct devlink_cb_arg *x)
78797c478bd9Sstevel@tonic-gate {
78807c478bd9Sstevel@tonic-gate 	int i;
78817c478bd9Sstevel@tonic-gate 
78827c478bd9Sstevel@tonic-gate 	for (i = 0; i < x->count; i++) {
78837c478bd9Sstevel@tonic-gate 		free(x->dev_names[i]);
78847c478bd9Sstevel@tonic-gate 		free(x->link_contents[i]);
78857c478bd9Sstevel@tonic-gate 	}
78867c478bd9Sstevel@tonic-gate }
78877c478bd9Sstevel@tonic-gate 
78887c478bd9Sstevel@tonic-gate /* callback function for di_devlink_cache_walk */
78897c478bd9Sstevel@tonic-gate static int
78907c478bd9Sstevel@tonic-gate devlink_cb(di_devlink_t dl, void *arg)
78917c478bd9Sstevel@tonic-gate {
78927c478bd9Sstevel@tonic-gate 	struct devlink_cb_arg *x = (struct devlink_cb_arg *)arg;
78937c478bd9Sstevel@tonic-gate 	const char *path;
78947c478bd9Sstevel@tonic-gate 	const char *content;
78957c478bd9Sstevel@tonic-gate 
78967c478bd9Sstevel@tonic-gate 	if ((path = di_devlink_path(dl)) == NULL ||
78977c478bd9Sstevel@tonic-gate 	    (content = di_devlink_content(dl)) == NULL ||
789812d8cf2aSjg 	    (x->dev_names[x->count] = s_strdup(path)) == NULL)
78997c478bd9Sstevel@tonic-gate 		goto out;
79007c478bd9Sstevel@tonic-gate 
790112d8cf2aSjg 	if ((x->link_contents[x->count] = s_strdup(content)) == NULL) {
79027c478bd9Sstevel@tonic-gate 		free(x->dev_names[x->count]);
79037c478bd9Sstevel@tonic-gate 		goto out;
79047c478bd9Sstevel@tonic-gate 	}
79057c478bd9Sstevel@tonic-gate 
79067c478bd9Sstevel@tonic-gate 	x->count++;
79077c478bd9Sstevel@tonic-gate 	if (x->count >= MAX_DEV_NAME_COUNT)
79087c478bd9Sstevel@tonic-gate 		return (DI_WALK_TERMINATE);
79097c478bd9Sstevel@tonic-gate 
79107c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
79117c478bd9Sstevel@tonic-gate 
79127c478bd9Sstevel@tonic-gate out:
79137c478bd9Sstevel@tonic-gate 	x->rv = -1;
79147c478bd9Sstevel@tonic-gate 	free_dev_names(x);
79157c478bd9Sstevel@tonic-gate 	return (DI_WALK_TERMINATE);
79167c478bd9Sstevel@tonic-gate }
79177c478bd9Sstevel@tonic-gate 
79187c478bd9Sstevel@tonic-gate /*
79197c478bd9Sstevel@tonic-gate  * Lookup dev name corresponding to the phys_path.
79207c478bd9Sstevel@tonic-gate  * phys_path is path to a node or minor node.
79217c478bd9Sstevel@tonic-gate  * Returns:
79227c478bd9Sstevel@tonic-gate  *	0 with *dev_name set to the dev name
79237c478bd9Sstevel@tonic-gate  *		Lookup succeeded and dev_name found
79247c478bd9Sstevel@tonic-gate  *	0 with *dev_name set to NULL
79257c478bd9Sstevel@tonic-gate  *		Lookup encountered no errors but dev name not found
79267c478bd9Sstevel@tonic-gate  *	-1
79277c478bd9Sstevel@tonic-gate  *		Lookup failed
79287c478bd9Sstevel@tonic-gate  */
79297c478bd9Sstevel@tonic-gate static int
79307c478bd9Sstevel@tonic-gate lookup_dev_name(char *phys_path, char **dev_name)
79317c478bd9Sstevel@tonic-gate {
79327c478bd9Sstevel@tonic-gate 	struct devlink_cb_arg cb_arg;
79337c478bd9Sstevel@tonic-gate 
79347c478bd9Sstevel@tonic-gate 	*dev_name = NULL;
79357c478bd9Sstevel@tonic-gate 
79367c478bd9Sstevel@tonic-gate 	cb_arg.count = 0;
79377c478bd9Sstevel@tonic-gate 	cb_arg.rv = 0;
79387c478bd9Sstevel@tonic-gate 	(void) di_devlink_cache_walk(devlink_cache, NULL, phys_path,
79397c478bd9Sstevel@tonic-gate 	    DI_PRIMARY_LINK, &cb_arg, devlink_cb);
79407c478bd9Sstevel@tonic-gate 
79417c478bd9Sstevel@tonic-gate 	if (cb_arg.rv == -1)
79427c478bd9Sstevel@tonic-gate 		return (-1);
79437c478bd9Sstevel@tonic-gate 
79447c478bd9Sstevel@tonic-gate 	if (cb_arg.count > 0) {
794512d8cf2aSjg 		*dev_name = s_strdup(cb_arg.dev_names[0]);
79467c478bd9Sstevel@tonic-gate 		free_dev_names(&cb_arg);
79477c478bd9Sstevel@tonic-gate 		if (*dev_name == NULL)
79487c478bd9Sstevel@tonic-gate 			return (-1);
79497c478bd9Sstevel@tonic-gate 	}
79507c478bd9Sstevel@tonic-gate 
79517c478bd9Sstevel@tonic-gate 	return (0);
79527c478bd9Sstevel@tonic-gate }
79537c478bd9Sstevel@tonic-gate 
79547c478bd9Sstevel@tonic-gate static char *
79557c478bd9Sstevel@tonic-gate lookup_disk_dev_name(char *node_path)
79567c478bd9Sstevel@tonic-gate {
79577c478bd9Sstevel@tonic-gate 	struct devlink_cb_arg cb_arg;
79587c478bd9Sstevel@tonic-gate 	char *dev_name = NULL;
79597c478bd9Sstevel@tonic-gate 	int i;
79607c478bd9Sstevel@tonic-gate 	char *p;
79617c478bd9Sstevel@tonic-gate 	int len1, len2;
79627c478bd9Sstevel@tonic-gate 
79637c478bd9Sstevel@tonic-gate #define	DEV_RDSK	"/dev/rdsk/"
79647c478bd9Sstevel@tonic-gate #define	DISK_RAW_MINOR	",raw"
79657c478bd9Sstevel@tonic-gate 
79667c478bd9Sstevel@tonic-gate 	cb_arg.count = 0;
79677c478bd9Sstevel@tonic-gate 	cb_arg.rv = 0;
79687c478bd9Sstevel@tonic-gate 	(void) di_devlink_cache_walk(devlink_cache, NULL, node_path,
79697c478bd9Sstevel@tonic-gate 	    DI_PRIMARY_LINK, &cb_arg, devlink_cb);
79707c478bd9Sstevel@tonic-gate 
79717c478bd9Sstevel@tonic-gate 	if (cb_arg.rv == -1 || cb_arg.count == 0)
79727c478bd9Sstevel@tonic-gate 		return (NULL);
79737c478bd9Sstevel@tonic-gate 
79747c478bd9Sstevel@tonic-gate 	/* first try lookup based on /dev/rdsk name */
79757c478bd9Sstevel@tonic-gate 	for (i = 0; i < cb_arg.count; i++) {
79767c478bd9Sstevel@tonic-gate 		if (strncmp(cb_arg.dev_names[i], DEV_RDSK,
79777c478bd9Sstevel@tonic-gate 		    sizeof (DEV_RDSK) - 1) == 0) {
797812d8cf2aSjg 			dev_name = s_strdup(cb_arg.dev_names[i]);
79797c478bd9Sstevel@tonic-gate 			break;
79807c478bd9Sstevel@tonic-gate 		}
79817c478bd9Sstevel@tonic-gate 	}
79827c478bd9Sstevel@tonic-gate 
79837c478bd9Sstevel@tonic-gate 	if (dev_name == NULL) {
79847c478bd9Sstevel@tonic-gate 		/* now try lookup based on a minor name ending with ",raw" */
79857c478bd9Sstevel@tonic-gate 		len1 = sizeof (DISK_RAW_MINOR) - 1;
79867c478bd9Sstevel@tonic-gate 		for (i = 0; i < cb_arg.count; i++) {
79877c478bd9Sstevel@tonic-gate 			len2 = strlen(cb_arg.link_contents[i]);
79887c478bd9Sstevel@tonic-gate 			if (len2 >= len1 &&
79897c478bd9Sstevel@tonic-gate 			    strcmp(cb_arg.link_contents[i] + len2 - len1,
79907c478bd9Sstevel@tonic-gate 			    DISK_RAW_MINOR) == 0) {
799112d8cf2aSjg 				dev_name = s_strdup(cb_arg.dev_names[i]);
79927c478bd9Sstevel@tonic-gate 				break;
79937c478bd9Sstevel@tonic-gate 			}
79947c478bd9Sstevel@tonic-gate 		}
79957c478bd9Sstevel@tonic-gate 	}
79967c478bd9Sstevel@tonic-gate 
79977c478bd9Sstevel@tonic-gate 	free_dev_names(&cb_arg);
79987c478bd9Sstevel@tonic-gate 
799912d8cf2aSjg 	if (dev_name == NULL)
800012d8cf2aSjg 		return (NULL);
80017c478bd9Sstevel@tonic-gate 	if (strlen(dev_name) == 0) {
80027c478bd9Sstevel@tonic-gate 		free(dev_name);
80037c478bd9Sstevel@tonic-gate 		return (NULL);
80047c478bd9Sstevel@tonic-gate 	}
80057c478bd9Sstevel@tonic-gate 
80067c478bd9Sstevel@tonic-gate 	/* if the name contains slice or partition number strip it */
80077c478bd9Sstevel@tonic-gate 	p = dev_name + strlen(dev_name) - 1;
80087c478bd9Sstevel@tonic-gate 	if (isdigit(*p)) {
80097c478bd9Sstevel@tonic-gate 		while (p != dev_name && isdigit(*p))
80107c478bd9Sstevel@tonic-gate 			p--;
80117c478bd9Sstevel@tonic-gate 		if (*p == 's' || *p == 'p')
80127c478bd9Sstevel@tonic-gate 			*p = '\0';
80137c478bd9Sstevel@tonic-gate 	}
80147c478bd9Sstevel@tonic-gate 
80157c478bd9Sstevel@tonic-gate 	return (dev_name);
80167c478bd9Sstevel@tonic-gate }
80177c478bd9Sstevel@tonic-gate 
801830294554Sphitran static char *
801930294554Sphitran lookup_lofi_dev_name(char *node_path, char *minor)
802030294554Sphitran {
802130294554Sphitran 	struct devlink_cb_arg cb_arg;
802230294554Sphitran 	char *dev_name = NULL;
802330294554Sphitran 	int i;
802430294554Sphitran 	int len1, len2;
802530294554Sphitran 
802630294554Sphitran 	cb_arg.count = 0;
802730294554Sphitran 	cb_arg.rv = 0;
802830294554Sphitran 	(void) di_devlink_cache_walk(devlink_cache, NULL, node_path,
802930294554Sphitran 	    DI_PRIMARY_LINK, &cb_arg, devlink_cb);
803030294554Sphitran 
803130294554Sphitran 	if (cb_arg.rv == -1 || cb_arg.count == 0)
803230294554Sphitran 		return (NULL);
803330294554Sphitran 
803430294554Sphitran 	/* lookup based on a minor name ending with ",raw" */
803530294554Sphitran 	len1 = strlen(minor);
803630294554Sphitran 	for (i = 0; i < cb_arg.count; i++) {
803730294554Sphitran 		len2 = strlen(cb_arg.link_contents[i]);
803830294554Sphitran 		if (len2 >= len1 &&
803930294554Sphitran 		    strcmp(cb_arg.link_contents[i] + len2 - len1,
804030294554Sphitran 		    minor) == 0) {
804130294554Sphitran 			dev_name = s_strdup(cb_arg.dev_names[i]);
804230294554Sphitran 			break;
804330294554Sphitran 		}
804430294554Sphitran 	}
804530294554Sphitran 
804630294554Sphitran 	free_dev_names(&cb_arg);
804730294554Sphitran 
804830294554Sphitran 	if (dev_name == NULL)
804930294554Sphitran 		return (NULL);
805030294554Sphitran 	if (strlen(dev_name) == 0) {
805130294554Sphitran 		free(dev_name);
805230294554Sphitran 		return (NULL);
805330294554Sphitran 	}
805430294554Sphitran 
805530294554Sphitran 	return (dev_name);
805630294554Sphitran }
805730294554Sphitran 
80587c478bd9Sstevel@tonic-gate static char *
80597c478bd9Sstevel@tonic-gate lookup_network_dev_name(char *node_path, char *driver_name)
80607c478bd9Sstevel@tonic-gate {
80617c478bd9Sstevel@tonic-gate 	char *dev_name = NULL;
80627c478bd9Sstevel@tonic-gate 	char phys_path[MAXPATHLEN];
80637c478bd9Sstevel@tonic-gate 
80647c478bd9Sstevel@tonic-gate 	if (lookup_dev_name(node_path, &dev_name) == -1)
80657c478bd9Sstevel@tonic-gate 		return (NULL);
80667c478bd9Sstevel@tonic-gate 
80677c478bd9Sstevel@tonic-gate 	if (dev_name == NULL) {
80687c478bd9Sstevel@tonic-gate 		/* dlpi style-2 only interface */
80697c478bd9Sstevel@tonic-gate 		(void) snprintf(phys_path, sizeof (phys_path),
80707c478bd9Sstevel@tonic-gate 		    "/pseudo/clone@0:%s", driver_name);
80717c478bd9Sstevel@tonic-gate 		if (lookup_dev_name(phys_path, &dev_name) == -1 ||
80727c478bd9Sstevel@tonic-gate 		    dev_name == NULL)
80737c478bd9Sstevel@tonic-gate 			return (NULL);
80747c478bd9Sstevel@tonic-gate 	}
80757c478bd9Sstevel@tonic-gate 
80767c478bd9Sstevel@tonic-gate 	return (dev_name);
80777c478bd9Sstevel@tonic-gate }
80787c478bd9Sstevel@tonic-gate 
8079db11e6feSjacobs static char *
8080db11e6feSjacobs lookup_printer_dev_name(char *node_path)
8081db11e6feSjacobs {
8082db11e6feSjacobs 	struct devlink_cb_arg cb_arg;
8083db11e6feSjacobs 	char *dev_name = NULL;
8084db11e6feSjacobs 	int i;
8085db11e6feSjacobs 
8086db11e6feSjacobs #define	DEV_PRINTERS	"/dev/printers/"
8087db11e6feSjacobs 
8088db11e6feSjacobs 	cb_arg.count = 0;
8089db11e6feSjacobs 	cb_arg.rv = 0;
8090db11e6feSjacobs 	(void) di_devlink_cache_walk(devlink_cache, NULL, node_path,
8091db11e6feSjacobs 	    DI_PRIMARY_LINK, &cb_arg, devlink_cb);
8092db11e6feSjacobs 
8093db11e6feSjacobs 	if (cb_arg.rv == -1 || cb_arg.count == 0)
8094db11e6feSjacobs 		return (NULL);
8095db11e6feSjacobs 
8096db11e6feSjacobs 	/* first try lookup based on /dev/printers name */
8097db11e6feSjacobs 	for (i = 0; i < cb_arg.count; i++) {
8098db11e6feSjacobs 		if (strncmp(cb_arg.dev_names[i], DEV_PRINTERS,
8099db11e6feSjacobs 		    sizeof (DEV_PRINTERS) - 1) == 0) {
8100db11e6feSjacobs 			dev_name = s_strdup(cb_arg.dev_names[i]);
8101db11e6feSjacobs 			break;
8102db11e6feSjacobs 		}
8103db11e6feSjacobs 	}
8104db11e6feSjacobs 
8105db11e6feSjacobs 	/* fallback to the first name */
8106db11e6feSjacobs 	if ((dev_name == NULL) && (cb_arg.count > 0))
8107db11e6feSjacobs 		dev_name = s_strdup(cb_arg.dev_names[0]);
8108db11e6feSjacobs 
8109db11e6feSjacobs 	free_dev_names(&cb_arg);
8110db11e6feSjacobs 
8111db11e6feSjacobs 	return (dev_name);
8112db11e6feSjacobs }
8113db11e6feSjacobs 
81147c478bd9Sstevel@tonic-gate /*
81157c478bd9Sstevel@tonic-gate  * Build an nvlist containing all attributes for devfs events.
81167c478bd9Sstevel@tonic-gate  * Returns nvlist pointer on success, NULL on failure.
81177c478bd9Sstevel@tonic-gate  */
81187c478bd9Sstevel@tonic-gate static nvlist_t *
81197c478bd9Sstevel@tonic-gate build_event_attributes(char *class, char *subclass, char *node_path,
812030294554Sphitran     di_node_t node, char *driver_name, int instance, char *minor)
81217c478bd9Sstevel@tonic-gate {
81227c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
81237c478bd9Sstevel@tonic-gate 	int err = 0;
81247c478bd9Sstevel@tonic-gate 	di_prop_t prop;
81257c478bd9Sstevel@tonic-gate 	int count;
81267c478bd9Sstevel@tonic-gate 	char *prop_name;
81277c478bd9Sstevel@tonic-gate 	int x;
81287c478bd9Sstevel@tonic-gate 	char *dev_name = NULL;
81297c478bd9Sstevel@tonic-gate 	int dev_name_lookup_err = 0;
81307c478bd9Sstevel@tonic-gate 
81317c478bd9Sstevel@tonic-gate 	if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, 0)) != 0) {
81327c478bd9Sstevel@tonic-gate 		nvl = NULL;
81337c478bd9Sstevel@tonic-gate 		goto out;
81347c478bd9Sstevel@tonic-gate 	}
81357c478bd9Sstevel@tonic-gate 
81367c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_int32(nvl, EV_VERSION, EV_V1)) != 0)
81377c478bd9Sstevel@tonic-gate 		goto out;
81387c478bd9Sstevel@tonic-gate 
81397c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_string(nvl, DEV_PHYS_PATH, node_path)) != 0)
81407c478bd9Sstevel@tonic-gate 		goto out;
81417c478bd9Sstevel@tonic-gate 
81427c478bd9Sstevel@tonic-gate 	if (strcmp(class, EC_DEV_ADD) != 0 &&
81437c478bd9Sstevel@tonic-gate 	    strcmp(class, EC_DEV_REMOVE) != 0)
81447c478bd9Sstevel@tonic-gate 		return (nvl);
81457c478bd9Sstevel@tonic-gate 
81467c478bd9Sstevel@tonic-gate 	if (driver_name == NULL || instance == -1)
81477c478bd9Sstevel@tonic-gate 		goto out;
81487c478bd9Sstevel@tonic-gate 
81497c478bd9Sstevel@tonic-gate 	if (strcmp(subclass, ESC_DISK) == 0) {
81507c478bd9Sstevel@tonic-gate 		if ((dev_name = lookup_disk_dev_name(node_path)) == NULL) {
81517c478bd9Sstevel@tonic-gate 			dev_name_lookup_err = 1;
81527c478bd9Sstevel@tonic-gate 			goto out;
81537c478bd9Sstevel@tonic-gate 		}
81547c478bd9Sstevel@tonic-gate 	} else if (strcmp(subclass, ESC_NETWORK) == 0) {
81557c478bd9Sstevel@tonic-gate 		if ((dev_name = lookup_network_dev_name(node_path, driver_name))
81567c478bd9Sstevel@tonic-gate 		    == NULL) {
81577c478bd9Sstevel@tonic-gate 			dev_name_lookup_err = 1;
81587c478bd9Sstevel@tonic-gate 			goto out;
81597c478bd9Sstevel@tonic-gate 		}
8160db11e6feSjacobs 	} else if (strcmp(subclass, ESC_PRINTER) == 0) {
8161db11e6feSjacobs 		if ((dev_name = lookup_printer_dev_name(node_path)) == NULL) {
8162db11e6feSjacobs 			dev_name_lookup_err = 1;
8163db11e6feSjacobs 			goto out;
8164db11e6feSjacobs 		}
816530294554Sphitran 	} else if (strcmp(subclass, ESC_LOFI) == 0) {
816630294554Sphitran 		/*
816730294554Sphitran 		 * The raw minor node is created or removed after the block
816830294554Sphitran 		 * node.  Lofi devfs events are dependent on this behavior.
816930294554Sphitran 		 * Generate the sysevent only for the raw minor node.
817030294554Sphitran 		 */
817130294554Sphitran 		if (strstr(minor, "raw") == NULL) {
817230294554Sphitran 			if (nvl) {
817330294554Sphitran 				nvlist_free(nvl);
817430294554Sphitran 			}
817530294554Sphitran 			return (NULL);
817630294554Sphitran 		}
817730294554Sphitran 		if ((dev_name = lookup_lofi_dev_name(node_path, minor)) ==
817830294554Sphitran 		    NULL) {
817930294554Sphitran 			dev_name_lookup_err = 1;
818030294554Sphitran 			goto out;
818130294554Sphitran 		}
81827c478bd9Sstevel@tonic-gate 	}
81837c478bd9Sstevel@tonic-gate 
81847c478bd9Sstevel@tonic-gate 	if (dev_name) {
81857c478bd9Sstevel@tonic-gate 		if ((err = nvlist_add_string(nvl, DEV_NAME, dev_name)) != 0)
81867c478bd9Sstevel@tonic-gate 			goto out;
81877c478bd9Sstevel@tonic-gate 		free(dev_name);
81887c478bd9Sstevel@tonic-gate 		dev_name = NULL;
81897c478bd9Sstevel@tonic-gate 	}
81907c478bd9Sstevel@tonic-gate 
81917c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_string(nvl, DEV_DRIVER_NAME, driver_name)) != 0)
81927c478bd9Sstevel@tonic-gate 		goto out;
81937c478bd9Sstevel@tonic-gate 
81947c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_int32(nvl, DEV_INSTANCE, instance)) != 0)
81957c478bd9Sstevel@tonic-gate 		goto out;
81967c478bd9Sstevel@tonic-gate 
81977c478bd9Sstevel@tonic-gate 	if (strcmp(class, EC_DEV_ADD) == 0) {
81987c478bd9Sstevel@tonic-gate 		/* add properties */
81997c478bd9Sstevel@tonic-gate 		count = 0;
82007c478bd9Sstevel@tonic-gate 		for (prop = di_prop_next(node, DI_PROP_NIL);
82017c478bd9Sstevel@tonic-gate 		    prop != DI_PROP_NIL && count < MAX_PROP_COUNT;
82027c478bd9Sstevel@tonic-gate 		    prop = di_prop_next(node, prop)) {
82037c478bd9Sstevel@tonic-gate 
82047c478bd9Sstevel@tonic-gate 			if (di_prop_devt(prop) != DDI_DEV_T_NONE)
82057c478bd9Sstevel@tonic-gate 				continue;
82067c478bd9Sstevel@tonic-gate 
82077c478bd9Sstevel@tonic-gate 			if ((x = add_property(nvl, prop)) == 0)
82087c478bd9Sstevel@tonic-gate 				count++;
82097c478bd9Sstevel@tonic-gate 			else if (x == -1) {
82107c478bd9Sstevel@tonic-gate 				if ((prop_name = di_prop_name(prop)) == NULL)
82117c478bd9Sstevel@tonic-gate 					prop_name = "";
82127c478bd9Sstevel@tonic-gate 				err_print(PROP_ADD_FAILED, prop_name);
82137c478bd9Sstevel@tonic-gate 				goto out;
82147c478bd9Sstevel@tonic-gate 			}
82157c478bd9Sstevel@tonic-gate 		}
82167c478bd9Sstevel@tonic-gate 	}
82177c478bd9Sstevel@tonic-gate 
82187c478bd9Sstevel@tonic-gate 	return (nvl);
82197c478bd9Sstevel@tonic-gate 
82207c478bd9Sstevel@tonic-gate out:
82217c478bd9Sstevel@tonic-gate 	if (nvl)
82227c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
82237c478bd9Sstevel@tonic-gate 
82247c478bd9Sstevel@tonic-gate 	if (dev_name)
82257c478bd9Sstevel@tonic-gate 		free(dev_name);
82267c478bd9Sstevel@tonic-gate 
822793239addSjohnlev 	if (dev_name_lookup_err) {
822893239addSjohnlev 		/*
822993239addSjohnlev 		 * If a lofi mount fails, the /devices node may well have
823093239addSjohnlev 		 * disappeared by the time we run, so let's not complain.
823193239addSjohnlev 		 */
823293239addSjohnlev 		if (strcmp(subclass, ESC_LOFI) != 0)
823393239addSjohnlev 			err_print(DEV_NAME_LOOKUP_FAILED, node_path);
823493239addSjohnlev 	} else {
82357c478bd9Sstevel@tonic-gate 		err_print(BUILD_EVENT_ATTR_FAILED, (err) ? strerror(err) : "");
823693239addSjohnlev 	}
82377c478bd9Sstevel@tonic-gate 	return (NULL);
82387c478bd9Sstevel@tonic-gate }
82397c478bd9Sstevel@tonic-gate 
82407c478bd9Sstevel@tonic-gate static void
82417c478bd9Sstevel@tonic-gate log_event(char *class, char *subclass, nvlist_t *nvl)
82427c478bd9Sstevel@tonic-gate {
82437c478bd9Sstevel@tonic-gate 	sysevent_id_t eid;
82447c478bd9Sstevel@tonic-gate 
82457c478bd9Sstevel@tonic-gate 	if (sysevent_post_event(class, subclass, "SUNW", DEVFSADMD,
82467c478bd9Sstevel@tonic-gate 	    nvl, &eid) != 0) {
82477c478bd9Sstevel@tonic-gate 		err_print(LOG_EVENT_FAILED, strerror(errno));
82487c478bd9Sstevel@tonic-gate 	}
82497c478bd9Sstevel@tonic-gate }
82507c478bd9Sstevel@tonic-gate 
8251f05faa4eSjacobs /*
8252f05faa4eSjacobs  * When devfsadmd needs to generate sysevents, they are queued for later
8253f05faa4eSjacobs  * delivery this allows them to be delivered after the devlinks db cache has
8254f05faa4eSjacobs  * been flushed guaranteeing that applications consuming these events have
8255f05faa4eSjacobs  * access to an accurate devlinks db.  The queue is a FIFO, sysevents to be
8256f05faa4eSjacobs  * inserted in the front of the queue and consumed off the back.
8257f05faa4eSjacobs  */
8258f05faa4eSjacobs static void
8259f05faa4eSjacobs enqueue_sysevent(char *class, char *subclass, nvlist_t *nvl)
8260f05faa4eSjacobs {
8261f05faa4eSjacobs 	syseventq_t *tmp;
8262f05faa4eSjacobs 
8263f05faa4eSjacobs 	if ((tmp = s_zalloc(sizeof (*tmp))) == NULL)
8264f05faa4eSjacobs 		return;
8265f05faa4eSjacobs 
8266f05faa4eSjacobs 	tmp->class = s_strdup(class);
8267f05faa4eSjacobs 	tmp->subclass = s_strdup(subclass);
8268f05faa4eSjacobs 	tmp->nvl = nvl;
8269f05faa4eSjacobs 
8270f05faa4eSjacobs 	(void) mutex_lock(&syseventq_mutex);
8271f05faa4eSjacobs 	if (syseventq_front != NULL)
8272f05faa4eSjacobs 		syseventq_front->next = tmp;
8273f05faa4eSjacobs 	else
8274f05faa4eSjacobs 		syseventq_back = tmp;
8275f05faa4eSjacobs 	syseventq_front = tmp;
8276f05faa4eSjacobs 	(void) mutex_unlock(&syseventq_mutex);
8277f05faa4eSjacobs }
8278f05faa4eSjacobs 
8279f05faa4eSjacobs static void
8280f05faa4eSjacobs process_syseventq()
8281f05faa4eSjacobs {
8282f05faa4eSjacobs 	(void) mutex_lock(&syseventq_mutex);
8283f05faa4eSjacobs 	while (syseventq_back != NULL) {
8284f05faa4eSjacobs 		syseventq_t *tmp = syseventq_back;
8285f05faa4eSjacobs 
8286f05faa4eSjacobs 		vprint(CHATTY_MID, "sending queued event: %s, %s\n",
82870a653502Swroche 		    tmp->class, tmp->subclass);
8288f05faa4eSjacobs 
8289f05faa4eSjacobs 		log_event(tmp->class, tmp->subclass, tmp->nvl);
8290f05faa4eSjacobs 
8291f05faa4eSjacobs 		if (tmp->class != NULL)
8292f05faa4eSjacobs 			free(tmp->class);
8293f05faa4eSjacobs 		if (tmp->subclass != NULL)
8294f05faa4eSjacobs 			free(tmp->subclass);
8295f05faa4eSjacobs 		if (tmp->nvl != NULL)
8296f05faa4eSjacobs 			nvlist_free(tmp->nvl);
8297f05faa4eSjacobs 		syseventq_back = syseventq_back->next;
8298f05faa4eSjacobs 		if (syseventq_back == NULL)
8299f05faa4eSjacobs 			syseventq_front = NULL;
8300f05faa4eSjacobs 		free(tmp);
8301f05faa4eSjacobs 	}
8302f05faa4eSjacobs 	(void) mutex_unlock(&syseventq_mutex);
8303f05faa4eSjacobs }
8304f05faa4eSjacobs 
83057c478bd9Sstevel@tonic-gate static void
8306f05faa4eSjacobs build_and_enq_event(char *class, char *subclass, char *node_path,
830730294554Sphitran 	di_node_t node, char *minor)
83087c478bd9Sstevel@tonic-gate {
83097c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
83107c478bd9Sstevel@tonic-gate 
8311f05faa4eSjacobs 	vprint(CHATTY_MID, "build_and_enq_event(%s, %s, %s, 0x%8.8x)\n",
83120a653502Swroche 	    class, subclass, node_path, (int)node);
8313f05faa4eSjacobs 
83147c478bd9Sstevel@tonic-gate 	if (node != DI_NODE_NIL)
83157c478bd9Sstevel@tonic-gate 		nvl = build_event_attributes(class, subclass, node_path, node,
831630294554Sphitran 		    di_driver_name(node), di_instance(node), minor);
83177c478bd9Sstevel@tonic-gate 	else
83187c478bd9Sstevel@tonic-gate 		nvl = build_event_attributes(class, subclass, node_path, node,
831930294554Sphitran 		    NULL, -1, minor);
83207c478bd9Sstevel@tonic-gate 
83217c478bd9Sstevel@tonic-gate 	if (nvl) {
8322f05faa4eSjacobs 		enqueue_sysevent(class, subclass, nvl);
83237c478bd9Sstevel@tonic-gate 	}
83247c478bd9Sstevel@tonic-gate }
8325facf4a8dSllai 
83261ca93273Seota /*
83271ca93273Seota  * is_blank() returns 1 (true) if a line specified is composed of
83281ca93273Seota  * whitespace characters only. otherwise, it returns 0 (false).
83291ca93273Seota  *
83301ca93273Seota  * Note. the argument (line) must be null-terminated.
83311ca93273Seota  */
83321ca93273Seota static int
83331ca93273Seota is_blank(char *line)
83341ca93273Seota {
83351ca93273Seota 	for (/* nothing */; *line != '\0'; line++)
83361ca93273Seota 		if (!isspace(*line))
83371ca93273Seota 			return (0);
83381ca93273Seota 	return (1);
83391ca93273Seota }
83401ca93273Seota 
8341aa646b9dSvikram /*
8342aa646b9dSvikram  * Functions to deal with the no-further-processing hash
8343aa646b9dSvikram  */
8344aa646b9dSvikram 
8345aa646b9dSvikram static void
8346aa646b9dSvikram nfphash_create(void)
8347aa646b9dSvikram {
8348aa646b9dSvikram 	assert(nfp_hash == NULL);
8349aa646b9dSvikram 	nfp_hash = s_zalloc(NFP_HASH_SZ * sizeof (item_t *));
8350aa646b9dSvikram }
8351aa646b9dSvikram 
8352aa646b9dSvikram static int
8353aa646b9dSvikram nfphash_fcn(char *key)
8354aa646b9dSvikram {
8355aa646b9dSvikram 	int i;
8356aa646b9dSvikram 	uint64_t sum = 0;
8357aa646b9dSvikram 
8358aa646b9dSvikram 	for (i = 0; key[i] != '\0'; i++) {
8359aa646b9dSvikram 		sum += (uchar_t)key[i];
8360aa646b9dSvikram 	}
8361aa646b9dSvikram 
8362aa646b9dSvikram 	return (sum % NFP_HASH_SZ);
8363aa646b9dSvikram }
8364aa646b9dSvikram 
8365aa646b9dSvikram static item_t *
8366aa646b9dSvikram nfphash_lookup(char *key)
8367aa646b9dSvikram {
8368aa646b9dSvikram 	int	index;
8369aa646b9dSvikram 	item_t  *ip;
8370aa646b9dSvikram 
8371aa646b9dSvikram 	index = nfphash_fcn(key);
8372aa646b9dSvikram 
8373aa646b9dSvikram 	assert(index >= 0);
8374aa646b9dSvikram 
8375aa646b9dSvikram 	for (ip = nfp_hash[index]; ip; ip = ip->i_next) {
8376aa646b9dSvikram 		if (strcmp(ip->i_key, key) == 0)
8377aa646b9dSvikram 			return (ip);
8378aa646b9dSvikram 	}
8379aa646b9dSvikram 
8380aa646b9dSvikram 	return (NULL);
8381aa646b9dSvikram }
8382aa646b9dSvikram 
8383aa646b9dSvikram static void
8384aa646b9dSvikram nfphash_insert(char *key)
8385aa646b9dSvikram {
8386aa646b9dSvikram 	item_t	*ip;
8387aa646b9dSvikram 	int	index;
8388aa646b9dSvikram 
8389aa646b9dSvikram 	index = nfphash_fcn(key);
8390aa646b9dSvikram 
8391aa646b9dSvikram 	assert(index >= 0);
8392aa646b9dSvikram 
8393aa646b9dSvikram 	ip = s_zalloc(sizeof (item_t));
8394aa646b9dSvikram 	ip->i_key = s_strdup(key);
8395aa646b9dSvikram 
8396aa646b9dSvikram 	ip->i_next = nfp_hash[index];
8397aa646b9dSvikram 	nfp_hash[index] = ip;
8398aa646b9dSvikram }
8399aa646b9dSvikram 
8400aa646b9dSvikram static void
8401aa646b9dSvikram nfphash_destroy(void)
8402aa646b9dSvikram {
8403aa646b9dSvikram 	int	i;
8404aa646b9dSvikram 	item_t	*ip;
8405aa646b9dSvikram 
8406aa646b9dSvikram 	for (i = 0; i < NFP_HASH_SZ; i++) {
8407aa646b9dSvikram 		/*LINTED*/
8408aa646b9dSvikram 		while (ip = nfp_hash[i]) {
8409aa646b9dSvikram 			nfp_hash[i] = ip->i_next;
8410aa646b9dSvikram 			free(ip->i_key);
8411aa646b9dSvikram 			free(ip);
8412aa646b9dSvikram 		}
8413aa646b9dSvikram 	}
8414aa646b9dSvikram 
8415aa646b9dSvikram 	free(nfp_hash);
8416aa646b9dSvikram 	nfp_hash = NULL;
8417aa646b9dSvikram }
8418aa646b9dSvikram 
8419facf4a8dSllai static int
8420facf4a8dSllai devname_kcall(int subcmd, void *args)
8421facf4a8dSllai {
8422facf4a8dSllai 	int error = 0;
8423facf4a8dSllai 
8424facf4a8dSllai 	switch (subcmd) {
8425facf4a8dSllai 	case MODDEVNAME_LOOKUPDOOR:
8426facf4a8dSllai 		error = modctl(MODDEVNAME, subcmd, (uintptr_t)args);
8427facf4a8dSllai 		if (error) {
8428facf4a8dSllai 			vprint(INFO_MID, "modctl(MODDEVNAME, "
8429facf4a8dSllai 			    "MODDEVNAME_LOOKUPDOOR) failed - %s\n",
8430facf4a8dSllai 			    strerror(errno));
8431facf4a8dSllai 		}
8432facf4a8dSllai 		break;
8433facf4a8dSllai 	default:
8434facf4a8dSllai 		error = EINVAL;
8435facf4a8dSllai 		break;
8436facf4a8dSllai 	}
8437facf4a8dSllai 	return (error);
8438facf4a8dSllai }
8439facf4a8dSllai 
8440facf4a8dSllai /* ARGSUSED */
8441facf4a8dSllai static void
8442facf4a8dSllai devname_lookup_handler(void *cookie, char *argp, size_t arg_size,
8443facf4a8dSllai     door_desc_t *dp, uint_t n_desc)
8444facf4a8dSllai {
8445facf4a8dSllai 	int32_t error = 0;
8446facf4a8dSllai 	door_cred_t dcred;
8447facf4a8dSllai 	struct dca_impl	dci;
8448facf4a8dSllai 	uint8_t	cmd;
8449facf4a8dSllai 	sdev_door_res_t res;
8450facf4a8dSllai 	sdev_door_arg_t *args;
8451facf4a8dSllai 
8452facf4a8dSllai 	if (argp == NULL || arg_size == 0) {
8453facf4a8dSllai 		vprint(DEVNAME_MID, "devname_lookup_handler: argp wrong\n");
8454facf4a8dSllai 		error = DEVFSADM_RUN_INVALID;
8455facf4a8dSllai 		goto done;
8456facf4a8dSllai 	}
8457facf4a8dSllai 	vprint(DEVNAME_MID, "devname_lookup_handler\n");
8458facf4a8dSllai 
8459facf4a8dSllai 	if (door_cred(&dcred) != 0 || dcred.dc_euid != 0) {
8460facf4a8dSllai 		vprint(DEVNAME_MID, "devname_lookup_handler: cred wrong\n");
8461facf4a8dSllai 		error = DEVFSADM_RUN_EPERM;
8462facf4a8dSllai 		goto done;
8463facf4a8dSllai 	}
8464facf4a8dSllai 
8465facf4a8dSllai 	args = (sdev_door_arg_t *)argp;
8466facf4a8dSllai 	cmd = args->devfsadm_cmd;
8467facf4a8dSllai 
8468facf4a8dSllai 	vprint(DEVNAME_MID, "devname_lookup_handler: cmd %d\n", cmd);
8469facf4a8dSllai 	switch (cmd) {
8470facf4a8dSllai 	case DEVFSADMD_RUN_ALL:
8471facf4a8dSllai 		/*
8472facf4a8dSllai 		 * run "devfsadm"
8473facf4a8dSllai 		 */
8474facf4a8dSllai 		dci.dci_root = "/";
8475facf4a8dSllai 		dci.dci_minor = NULL;
8476facf4a8dSllai 		dci.dci_driver = NULL;
8477facf4a8dSllai 		dci.dci_error = 0;
8478facf4a8dSllai 		dci.dci_flags = 0;
8479facf4a8dSllai 		dci.dci_arg = NULL;
8480facf4a8dSllai 
8481facf4a8dSllai 		lock_dev();
8482facf4a8dSllai 		update_drvconf((major_t)-1);
8483facf4a8dSllai 		dci.dci_flags |= DCA_FLUSH_PATHINST;
8484facf4a8dSllai 
8485facf4a8dSllai 		pre_and_post_cleanup(RM_PRE);
848666ea8494SVikram Hegde 		devi_tree_walk(&dci, DI_CACHE_SNAPSHOT_FLAGS, NULL);
8487facf4a8dSllai 		error = (int32_t)dci.dci_error;
8488facf4a8dSllai 		if (!error) {
8489facf4a8dSllai 			pre_and_post_cleanup(RM_POST);
8490facf4a8dSllai 			update_database = TRUE;
8491facf4a8dSllai 			unlock_dev(SYNC_STATE);
8492facf4a8dSllai 			update_database = FALSE;
8493facf4a8dSllai 		} else {
8494facf4a8dSllai 			if (DEVFSADM_DEBUG_ON) {
8495facf4a8dSllai 				vprint(INFO_MID, "devname_lookup_handler: "
8496facf4a8dSllai 				    "DEVFSADMD_RUN_ALL failed\n");
8497facf4a8dSllai 			}
8498facf4a8dSllai 
8499facf4a8dSllai 			unlock_dev(SYNC_STATE);
8500facf4a8dSllai 		}
8501facf4a8dSllai 		break;
8502facf4a8dSllai 	default:
8503facf4a8dSllai 		/* log an error here? */
8504facf4a8dSllai 		error = DEVFSADM_RUN_NOTSUP;
8505facf4a8dSllai 		break;
8506facf4a8dSllai 	}
8507facf4a8dSllai 
8508facf4a8dSllai done:
8509facf4a8dSllai 	vprint(DEVNAME_MID, "devname_lookup_handler: error %d\n", error);
8510facf4a8dSllai 	res.devfsadm_error = error;
8511facf4a8dSllai 	(void) door_return((char *)&res, sizeof (struct sdev_door_res),
8512facf4a8dSllai 	    NULL, 0);
8513facf4a8dSllai }
85148d483882Smlf 
85158d483882Smlf 
85168d483882Smlf di_devlink_handle_t
85178d483882Smlf devfsadm_devlink_cache(void)
85188d483882Smlf {
85198d483882Smlf 	return (devlink_cache);
85208d483882Smlf }
85218d483882Smlf 
85228d483882Smlf int
85238d483882Smlf devfsadm_reserve_id_cache(devlink_re_t re_array[], enumerate_file_t *head)
85248d483882Smlf {
85258d483882Smlf 	enumerate_file_t *entry;
85268d483882Smlf 	int nelem;
85278d483882Smlf 	int i;
85288d483882Smlf 	int subex;
85298d483882Smlf 	char *re;
85308d483882Smlf 	size_t size;
85318d483882Smlf 	regmatch_t *pmch;
85328d483882Smlf 
85338d483882Smlf 	/*
85348d483882Smlf 	 * Check the <RE, subexp> array passed in and compile it.
85358d483882Smlf 	 */
85368d483882Smlf 	for (i = 0; re_array[i].d_re; i++) {
85378d483882Smlf 		if (re_array[i].d_subexp == 0) {
85388d483882Smlf 			err_print("bad subexp value in RE: %s\n",
85398d483882Smlf 			    re_array[i].d_re);
85408d483882Smlf 			goto bad_re;
85418d483882Smlf 		}
85428d483882Smlf 
85438d483882Smlf 		re = re_array[i].d_re;
85448d483882Smlf 		if (regcomp(&re_array[i].d_rcomp, re, REG_EXTENDED) != 0) {
85458d483882Smlf 			err_print("reg. exp. failed to compile: %s\n", re);
85468d483882Smlf 			goto bad_re;
85478d483882Smlf 		}
85488d483882Smlf 		subex = re_array[i].d_subexp;
85498d483882Smlf 		nelem = subex + 1;
85508d483882Smlf 		re_array[i].d_pmatch = s_malloc(sizeof (regmatch_t) * nelem);
85518d483882Smlf 	}
85528d483882Smlf 
85538d483882Smlf 	entry = head ? head : enumerate_reserved;
85548d483882Smlf 	for (; entry; entry = entry->er_next) {
85558d483882Smlf 		if (entry->er_id) {
85568d483882Smlf 			vprint(RSBY_MID, "entry %s already has ID %s\n",
85578d483882Smlf 			    entry->er_file, entry->er_id);
85588d483882Smlf 			continue;
85598d483882Smlf 		}
85608d483882Smlf 		for (i = 0; re_array[i].d_re; i++) {
85618d483882Smlf 			subex = re_array[i].d_subexp;
85628d483882Smlf 			pmch = re_array[i].d_pmatch;
85638d483882Smlf 			if (regexec(&re_array[i].d_rcomp, entry->er_file,
85648d483882Smlf 			    subex + 1, pmch, 0) != 0) {
85658d483882Smlf 				/* No match */
85668d483882Smlf 				continue;
85678d483882Smlf 			}
85688d483882Smlf 			size = pmch[subex].rm_eo - pmch[subex].rm_so;
85698d483882Smlf 			entry->er_id = s_malloc(size + 1);
85708d483882Smlf 			(void) strncpy(entry->er_id,
85718d483882Smlf 			    &entry->er_file[pmch[subex].rm_so], size);
85728d483882Smlf 			entry->er_id[size] = '\0';
85738d483882Smlf 			if (head) {
85748d483882Smlf 				vprint(RSBY_MID, "devlink(%s) matches RE(%s). "
85758d483882Smlf 				    "ID is %s\n", entry->er_file,
85768d483882Smlf 				    re_array[i].d_re, entry->er_id);
85778d483882Smlf 			} else {
85788d483882Smlf 				vprint(RSBY_MID, "rsrv entry(%s) matches "
85798d483882Smlf 				    "RE(%s) ID is %s\n", entry->er_file,
85808d483882Smlf 				    re_array[i].d_re, entry->er_id);
85818d483882Smlf 			}
85828d483882Smlf 			break;
85838d483882Smlf 		}
85848d483882Smlf 	}
85858d483882Smlf 
85868d483882Smlf 	for (i = 0; re_array[i].d_re; i++) {
85878d483882Smlf 		regfree(&re_array[i].d_rcomp);
85888d483882Smlf 		assert(re_array[i].d_pmatch);
85898d483882Smlf 		free(re_array[i].d_pmatch);
85908d483882Smlf 	}
85918d483882Smlf 
85928d483882Smlf 	entry = head ? head : enumerate_reserved;
85938d483882Smlf 	for (; entry; entry = entry->er_next) {
85948d483882Smlf 		if (entry->er_id == NULL)
85958d483882Smlf 			continue;
85968d483882Smlf 		if (head) {
85978d483882Smlf 			vprint(RSBY_MID, "devlink: %s\n", entry->er_file);
85988d483882Smlf 			vprint(RSBY_MID, "ID: %s\n", entry->er_id);
85998d483882Smlf 		} else {
86008d483882Smlf 			vprint(RSBY_MID, "reserve file entry: %s\n",
86018d483882Smlf 			    entry->er_file);
86028d483882Smlf 			vprint(RSBY_MID, "reserve file id: %s\n",
86038d483882Smlf 			    entry->er_id);
86048d483882Smlf 		}
86058d483882Smlf 	}
86068d483882Smlf 
86078d483882Smlf 	return (DEVFSADM_SUCCESS);
86088d483882Smlf 
86098d483882Smlf bad_re:
86108d483882Smlf 	for (i = i-1; i >= 0; i--) {
86118d483882Smlf 		regfree(&re_array[i].d_rcomp);
86128d483882Smlf 		assert(re_array[i].d_pmatch);
86138d483882Smlf 		free(re_array[i].d_pmatch);
86148d483882Smlf 	}
86158d483882Smlf 	return (DEVFSADM_FAILURE);
86168d483882Smlf }
8617e37c6c37Scth 
8618e37c6c37Scth /*
8619e37c6c37Scth  * Return 1 if we have reserved links.
8620e37c6c37Scth  */
8621e37c6c37Scth int
8622e37c6c37Scth devfsadm_have_reserved()
8623e37c6c37Scth {
8624e37c6c37Scth 	return (enumerate_reserved ? 1 : 0);
8625e37c6c37Scth }
86268d483882Smlf 
86278d483882Smlf /*
86288d483882Smlf  * This functions errs on the side of caution. If there is any error
86298d483882Smlf  * we assume that the devlink is  *not* reserved
86308d483882Smlf  */
86318d483882Smlf int
86328d483882Smlf devfsadm_is_reserved(devlink_re_t re_array[], char *devlink)
86338d483882Smlf {
86348d483882Smlf 	int match;
86358d483882Smlf 	enumerate_file_t estruct = {NULL};
86368d483882Smlf 	enumerate_file_t *entry;
86378d483882Smlf 
86388d483882Smlf 	match = 0;
86398d483882Smlf 	estruct.er_file = devlink;
86408d483882Smlf 	estruct.er_id = NULL;
86418d483882Smlf 	estruct.er_next = NULL;
86428d483882Smlf 
86438d483882Smlf 	if (devfsadm_reserve_id_cache(re_array, &estruct) != DEVFSADM_SUCCESS) {
86448d483882Smlf 		err_print("devfsadm_is_reserved: devlink (%s) does not "
86458d483882Smlf 		    "match RE\n", devlink);
86468d483882Smlf 		return (0);
86478d483882Smlf 	}
86488d483882Smlf 	if (estruct.er_id == NULL) {
86498d483882Smlf 		err_print("devfsadm_is_reserved: ID derived from devlink %s "
86508d483882Smlf 		    "is NULL\n", devlink);
86518d483882Smlf 		return (0);
86528d483882Smlf 	}
86538d483882Smlf 
86548d483882Smlf 	entry = enumerate_reserved;
86558d483882Smlf 	for (; entry; entry = entry->er_next) {
86568d483882Smlf 		if (entry->er_id == NULL)
86578d483882Smlf 			continue;
86588d483882Smlf 		if (strcmp(entry->er_id, estruct.er_id) != 0)
86598d483882Smlf 			continue;
86608d483882Smlf 		match = 1;
86618d483882Smlf 		vprint(RSBY_MID, "reserve file entry (%s) and devlink (%s) "
86628d483882Smlf 		    "match\n", entry->er_file, devlink);
86638d483882Smlf 		break;
86648d483882Smlf 	}
86658d483882Smlf 
86668d483882Smlf 	free(estruct.er_id);
86678d483882Smlf 	return (match);
86688d483882Smlf }
8669