xref: /illumos-gate/usr/src/cmd/devfsadm/devfsadm.c (revision f2dbfd32)
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 /*
23406fc510SToomas Soome  * Copyright 2016 Toomas Soome <tsoome@me.com>
24540b2cc7SHans Rosenfeld  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
2594c894bbSVikram Hegde  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
26*f2dbfd32SRobert Mustacchi  * Copyright 2019, Joyent, Inc.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Devfsadm replaces drvconfig, audlinks, disks, tapes, ports, devlinks
317c478bd9Sstevel@tonic-gate  * as a general purpose device administrative utility.	It creates
327c478bd9Sstevel@tonic-gate  * devices special files in /devices and logical links in /dev, and
337c478bd9Sstevel@tonic-gate  * coordinates updates to /etc/path_to_instance with the kernel.  It
347c478bd9Sstevel@tonic-gate  * operates in both command line mode to handle user or script invoked
357c478bd9Sstevel@tonic-gate  * reconfiguration updates, and operates in daemon mode to handle dynamic
367c478bd9Sstevel@tonic-gate  * reconfiguration for hotplugging support.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
3945916cd2Sjpk #include <string.h>
40f875b4ebSrica #include <deflt.h>
4145916cd2Sjpk #include <tsol/label.h>
4245916cd2Sjpk #include <bsm/devices.h>
4345916cd2Sjpk #include <bsm/devalloc.h>
447c478bd9Sstevel@tonic-gate #include <utime.h>
456638bf60Saj #include <sys/param.h>
466638bf60Saj #include <bsm/libbsm.h>
4717e9b2b7SDhanaraj M #include <zone.h>
4845916cd2Sjpk #include "devfsadm_impl.h"
497c478bd9Sstevel@tonic-gate 
5045916cd2Sjpk /* externs from devalloc.c */
5145916cd2Sjpk extern void  _reset_devalloc(int);
5245916cd2Sjpk extern void _update_devalloc_db(devlist_t *, int, int, char *, char *);
5345916cd2Sjpk extern int _da_check_for_usb(char *, char *);
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* create or remove nodes or links. unset with -n */
567c478bd9Sstevel@tonic-gate static int file_mods = TRUE;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /* cleanup mode.  Set with -C */
597c478bd9Sstevel@tonic-gate static int cleanup = FALSE;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /* devlinks -d compatibility */
627c478bd9Sstevel@tonic-gate static int devlinks_debug = FALSE;
637c478bd9Sstevel@tonic-gate 
6445916cd2Sjpk /* flag to check if system is labeled */
6545916cd2Sjpk int system_labeled = FALSE;
6645916cd2Sjpk 
6745916cd2Sjpk /* flag to enable/disable device allocation with -e/-d */
6845916cd2Sjpk static int devalloc_flag = 0;
6945916cd2Sjpk 
706638bf60Saj /* flag that indicates if device allocation is on or not */
716638bf60Saj static int devalloc_is_on = 0;
726638bf60Saj 
7345916cd2Sjpk /* flag to update device allocation database for this device type */
7445916cd2Sjpk static int update_devdb = 0;
7545916cd2Sjpk 
7645916cd2Sjpk /*
7745916cd2Sjpk  * devices to be deallocated with -d :
7845916cd2Sjpk  *	audio, floppy, cd, floppy, tape, rmdisk.
7945916cd2Sjpk  */
8045916cd2Sjpk static char *devalloc_list[10] = {DDI_NT_AUDIO, DDI_NT_CD, DDI_NT_CD_CHAN,
8145916cd2Sjpk 				    DDI_NT_FD, DDI_NT_TAPE, DDI_NT_BLOCK_CHAN,
8245916cd2Sjpk 				    DDI_NT_UGEN, DDI_NT_USB_ATTACHMENT_POINT,
8345916cd2Sjpk 				    DDI_NT_SCSI_NEXUS, NULL};
847c478bd9Sstevel@tonic-gate 
8545916cd2Sjpk /* list of allocatable devices */
8645916cd2Sjpk static devlist_t devlist;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /* load a single driver only.  set with -i */
897c478bd9Sstevel@tonic-gate static int single_drv = FALSE;
907c478bd9Sstevel@tonic-gate static char *driver = NULL;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /* attempt to load drivers or defer attach nodes */
937c478bd9Sstevel@tonic-gate static int load_attach_drv = TRUE;
947c478bd9Sstevel@tonic-gate 
95c9cc1492SJerry Gilliam /* reload all driver.conf files */
96c9cc1492SJerry Gilliam static int update_all_drivers = FALSE;
97c9cc1492SJerry Gilliam 
987c478bd9Sstevel@tonic-gate /* set if invoked via /usr/lib/devfsadm/devfsadmd */
997c478bd9Sstevel@tonic-gate static int daemon_mode = FALSE;
1007c478bd9Sstevel@tonic-gate 
1017e3e5701SJan Parcel /* set if event_handler triggered */
1027e3e5701SJan Parcel int event_driven = FALSE;
1037e3e5701SJan Parcel 
1047c478bd9Sstevel@tonic-gate /* output directed to syslog during daemon mode if set */
1057c478bd9Sstevel@tonic-gate static int logflag = FALSE;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /* build links in /dev.  -x to turn off */
1087c478bd9Sstevel@tonic-gate static int build_dev = TRUE;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /* build nodes in /devices.  -y to turn off */
1117c478bd9Sstevel@tonic-gate static int build_devices = TRUE;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate /* -z to turn off */
1147c478bd9Sstevel@tonic-gate static int flush_path_to_inst_enable = TRUE;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /* variables used for path_to_inst flushing */
1177c478bd9Sstevel@tonic-gate static int inst_count = 0;
1187c478bd9Sstevel@tonic-gate static mutex_t count_lock;
1197c478bd9Sstevel@tonic-gate static cond_t cv;
1207c478bd9Sstevel@tonic-gate 
121ff2aee48Scth /* variables for minor_fini thread */
1227c478bd9Sstevel@tonic-gate static mutex_t minor_fini_mutex;
123ff2aee48Scth static int minor_fini_canceled = TRUE;
124ff2aee48Scth static int minor_fini_delayed = FALSE;
125ff2aee48Scth static cond_t minor_fini_cv;
126ff2aee48Scth static int minor_fini_timeout = MINOR_FINI_TIMEOUT_DEFAULT;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /* single-threads /dev modification */
1297c478bd9Sstevel@tonic-gate static sema_t dev_sema;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /* the program we were invoked as; ie argv[0] */
1327c478bd9Sstevel@tonic-gate static char *prog;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /* pointers to create/remove link lists */
1357c478bd9Sstevel@tonic-gate static create_list_t *create_head = NULL;
1367c478bd9Sstevel@tonic-gate static remove_list_t *remove_head = NULL;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate /*  supports the class -c option */
1397c478bd9Sstevel@tonic-gate static char **classes = NULL;
1407c478bd9Sstevel@tonic-gate static int num_classes = 0;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /* used with verbose option -v or -V */
1437c478bd9Sstevel@tonic-gate static int num_verbose = 0;
1447c478bd9Sstevel@tonic-gate static char **verbose = NULL;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate static struct mperm *minor_perms = NULL;
1477c478bd9Sstevel@tonic-gate static driver_alias_t *driver_aliases = NULL;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /* set if -r alternate root given */
1507c478bd9Sstevel@tonic-gate static char *root_dir = "";
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /* /devices or <rootdir>/devices */
1537c478bd9Sstevel@tonic-gate static char *devices_dir  = DEVICES;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /* /dev or <rootdir>/dev */
1567c478bd9Sstevel@tonic-gate static char *dev_dir = DEV;
1577c478bd9Sstevel@tonic-gate 
158facf4a8dSllai /* /etc/dev or <rootdir>/etc/dev */
159facf4a8dSllai static char *etc_dev_dir = ETCDEV;
160facf4a8dSllai 
161facf4a8dSllai /*
162facf4a8dSllai  * writable root (for lock files and doors during install).
163facf4a8dSllai  * This is also root dir for /dev attr dir during install.
164facf4a8dSllai  */
165facf4a8dSllai static char *attr_root = NULL;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate /* /etc/path_to_inst unless -p used */
1687c478bd9Sstevel@tonic-gate static char *inst_file = INSTANCE_FILE;
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /* /usr/lib/devfsadm/linkmods unless -l used */
1717c478bd9Sstevel@tonic-gate static char *module_dirs = MODULE_DIRS;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate /* default uid/gid used if /etc/minor_perm entry not found */
1747c478bd9Sstevel@tonic-gate static uid_t root_uid;
1757c478bd9Sstevel@tonic-gate static gid_t sys_gid;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /* /etc/devlink.tab unless devlinks -t used */
1787c478bd9Sstevel@tonic-gate static char *devlinktab_file = NULL;
1797c478bd9Sstevel@tonic-gate 
1808d483882Smlf /* File and data structure to reserve enumerate IDs */
1818d483882Smlf static char *enumerate_file = ENUMERATE_RESERVED;
1828d483882Smlf static enumerate_file_t *enumerate_reserved = NULL;
1838d483882Smlf 
1847c478bd9Sstevel@tonic-gate /* set if /dev link is new. speeds up rm_stale_links */
1857c478bd9Sstevel@tonic-gate static int linknew = TRUE;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /* variables for devlink.tab compat processing */
1887c478bd9Sstevel@tonic-gate static devlinktab_list_t *devlinktab_list = NULL;
1897c478bd9Sstevel@tonic-gate static unsigned int devlinktab_line = 0;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /* cache head for devfsadm_enumerate*() functions */
1927c478bd9Sstevel@tonic-gate static numeral_set_t *head_numeral_set = NULL;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /* list list of devfsadm modules */
1957c478bd9Sstevel@tonic-gate static module_t *module_head = NULL;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /* name_to_major list used in utility function */
1987c478bd9Sstevel@tonic-gate static n2m_t *n2m_list = NULL;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate /* cache of some links used for performance */
2017c478bd9Sstevel@tonic-gate static linkhead_t *headlinkhead = NULL;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate /* locking variables to prevent multiples writes to /dev */
2047c478bd9Sstevel@tonic-gate static int hold_dev_lock = FALSE;
2057c478bd9Sstevel@tonic-gate static int hold_daemon_lock = FALSE;
2067c478bd9Sstevel@tonic-gate static int dev_lock_fd;
2077c478bd9Sstevel@tonic-gate static int daemon_lock_fd;
2087c478bd9Sstevel@tonic-gate static char dev_lockfile[PATH_MAX + 1];
2097c478bd9Sstevel@tonic-gate static char daemon_lockfile[PATH_MAX + 1];
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate /* last devinfo node/minor processed. used for performance */
2127c478bd9Sstevel@tonic-gate static di_node_t lnode;
2137c478bd9Sstevel@tonic-gate static di_minor_t lminor;
2147c478bd9Sstevel@tonic-gate static char lphy_path[PATH_MAX + 1] = {""};
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /* Globals used by the link database */
2177c478bd9Sstevel@tonic-gate static di_devlink_handle_t devlink_cache;
2187c478bd9Sstevel@tonic-gate static int update_database = FALSE;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate /* Globals used to set logindev perms */
2217c478bd9Sstevel@tonic-gate static struct login_dev *login_dev_cache = NULL;
2227c478bd9Sstevel@tonic-gate static int login_dev_enable = FALSE;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /* Global to use devinfo snapshot cache */
2257c478bd9Sstevel@tonic-gate static int use_snapshot_cache = FALSE;
2267c478bd9Sstevel@tonic-gate 
227aa646b9dSvikram /* Global for no-further-processing hash */
228aa646b9dSvikram static item_t **nfp_hash;
229aa646b9dSvikram static mutex_t  nfp_mutex = DEFAULTMUTEX;
230aa646b9dSvikram 
2317c478bd9Sstevel@tonic-gate /*
23267323fc4SJohn Levon  * Directories not removed even when empty.  They are packaged, or may
23367323fc4SJohn Levon  * be referred to from a non-global zone.  The dirs must be listed in
23467323fc4SJohn Levon  * canonical form i.e. without leading "/dev/"
2357c478bd9Sstevel@tonic-gate  */
23667323fc4SJohn Levon static char *sticky_dirs[] =
23767323fc4SJohn Levon 	{"dsk", "rdsk", "term", "lofi", "rlofi", NULL};
2387c478bd9Sstevel@tonic-gate 
239facf4a8dSllai /* Devname globals */
240facf4a8dSllai static int lookup_door_fd = -1;
241facf4a8dSllai static char *lookup_door_path;
242facf4a8dSllai 
2437c478bd9Sstevel@tonic-gate static void load_dev_acl(void);
244c9cc1492SJerry Gilliam static void update_drvconf(major_t, int);
245facf4a8dSllai static void check_reconfig_state(void);
246facf4a8dSllai static int s_stat(const char *, struct stat *);
2477c478bd9Sstevel@tonic-gate 
2481ca93273Seota static int is_blank(char *);
2491ca93273Seota 
250f05faa4eSjacobs /* sysevent queue related globals */
251f05faa4eSjacobs static mutex_t  syseventq_mutex = DEFAULTMUTEX;
252f05faa4eSjacobs static syseventq_t *syseventq_front;
253f05faa4eSjacobs static syseventq_t *syseventq_back;
254f05faa4eSjacobs static void process_syseventq();
255f05faa4eSjacobs 
25694c894bbSVikram Hegde static di_node_t devi_root_node = DI_NODE_NIL;
25794c894bbSVikram Hegde 
2587c478bd9Sstevel@tonic-gate int
2597c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
2607c478bd9Sstevel@tonic-gate {
2617c478bd9Sstevel@tonic-gate 	struct passwd *pw;
2627c478bd9Sstevel@tonic-gate 	struct group *gp;
2637c478bd9Sstevel@tonic-gate 	pid_t pid;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2667c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
2697c478bd9Sstevel@tonic-gate 		prog = argv[0];
2707c478bd9Sstevel@tonic-gate 	} else {
2717c478bd9Sstevel@tonic-gate 		prog++;
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	if (getuid() != 0) {
2757c478bd9Sstevel@tonic-gate 		err_print(MUST_BE_ROOT);
2767c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
277537714daSvikram 		/*NOTREACHED*/
2787c478bd9Sstevel@tonic-gate 	}
2797c478bd9Sstevel@tonic-gate 
28017e9b2b7SDhanaraj M 	if (getzoneid() != GLOBAL_ZONEID) {
28117e9b2b7SDhanaraj M 		err_print(MUST_BE_GLOBAL_ZONE);
28217e9b2b7SDhanaraj M 		devfsadm_exit(1);
28317e9b2b7SDhanaraj M 	}
28417e9b2b7SDhanaraj M 
2857c478bd9Sstevel@tonic-gate 	/*
2867c478bd9Sstevel@tonic-gate 	 * Close all files except stdin/stdout/stderr
2877c478bd9Sstevel@tonic-gate 	 */
2887c478bd9Sstevel@tonic-gate 	closefrom(3);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
2917c478bd9Sstevel@tonic-gate 		root_uid = pw->pw_uid;
2927c478bd9Sstevel@tonic-gate 	} else {
2937c478bd9Sstevel@tonic-gate 		err_print(CANT_FIND_USER, DEFAULT_DEV_USER);
2947c478bd9Sstevel@tonic-gate 		root_uid = (uid_t)0;	/* assume 0 is root */
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	/* the default group is sys */
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
3007c478bd9Sstevel@tonic-gate 		sys_gid = gp->gr_gid;
3017c478bd9Sstevel@tonic-gate 	} else {
3027c478bd9Sstevel@tonic-gate 		err_print(CANT_FIND_GROUP, DEFAULT_DEV_GROUP);
3037c478bd9Sstevel@tonic-gate 		sys_gid = (gid_t)3;	/* assume 3 is sys */
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	(void) umask(0);
3077c478bd9Sstevel@tonic-gate 
30845916cd2Sjpk 	system_labeled = is_system_labeled();
30945916cd2Sjpk 	if (system_labeled == FALSE) {
31045916cd2Sjpk 		/*
31145916cd2Sjpk 		 * is_system_labeled() will return false in case we are
31245916cd2Sjpk 		 * starting before the first reboot after Trusted Extensions
313f875b4ebSrica 		 * is enabled.  Check the setting in /etc/system to see if
314f875b4ebSrica 		 * TX is enabled (even if not yet booted).
31545916cd2Sjpk 		 */
316f875b4ebSrica 		if (defopen("/etc/system") == 0) {
317f875b4ebSrica 			if (defread("set sys_labeling=1") != NULL)
318f875b4ebSrica 				system_labeled = TRUE;
319f875b4ebSrica 
320f875b4ebSrica 			/* close defaults file */
321f875b4ebSrica 			(void) defopen(NULL);
322facf4a8dSllai 		}
32345916cd2Sjpk 	}
3246638bf60Saj 	/*
3256638bf60Saj 	 * Check if device allocation is enabled.
3266638bf60Saj 	 */
3274dd25bdcSJerry Gilliam 	devalloc_is_on = (da_is_on() == 1) ? 1 : 0;
32845916cd2Sjpk 
329f875b4ebSrica #ifdef DEBUG
330f875b4ebSrica 	if (system_labeled == FALSE) {
331f875b4ebSrica 		struct stat tx_stat;
332f875b4ebSrica 
333f875b4ebSrica 		/* test hook: see also mkdevalloc.c and allocate.c */
334f875b4ebSrica 		system_labeled = is_system_labeled_debug(&tx_stat);
335f875b4ebSrica 	}
336f875b4ebSrica #endif
337f875b4ebSrica 
3387c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	(void) sema_init(&dev_sema, 1, USYNC_THREAD, NULL);
3417c478bd9Sstevel@tonic-gate 
34245916cd2Sjpk 	/* Initialize device allocation list */
34345916cd2Sjpk 	devlist.audio = devlist.cd = devlist.floppy = devlist.tape =
3440a653502Swroche 	    devlist.rmdisk = NULL;
34545916cd2Sjpk 
3467c478bd9Sstevel@tonic-gate 	if (daemon_mode == TRUE) {
3477c478bd9Sstevel@tonic-gate 		/*
3487c478bd9Sstevel@tonic-gate 		 * Build /dev and /devices before daemonizing if
3497c478bd9Sstevel@tonic-gate 		 * reconfig booting and daemon invoked with alternate
3507c478bd9Sstevel@tonic-gate 		 * root. This is to support install.
3517c478bd9Sstevel@tonic-gate 		 */
3527c478bd9Sstevel@tonic-gate 		if (getenv(RECONFIG_BOOT) != NULL && root_dir[0] != '\0') {
3537c478bd9Sstevel@tonic-gate 			vprint(INFO_MID, CONFIGURING);
3547c478bd9Sstevel@tonic-gate 			load_dev_acl();
355c9cc1492SJerry Gilliam 			update_drvconf((major_t)-1, 0);
3567c478bd9Sstevel@tonic-gate 			process_devinfo_tree();
3577c478bd9Sstevel@tonic-gate 			(void) modctl(MODSETMINIROOT);
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 		/*
3617c478bd9Sstevel@tonic-gate 		 * fork before detaching from tty in order to print error
3627c478bd9Sstevel@tonic-gate 		 * message if unable to acquire file lock.  locks not preserved
3637c478bd9Sstevel@tonic-gate 		 * across forks.  Even under debug we want to fork so that
3647c478bd9Sstevel@tonic-gate 		 * when executed at boot we don't hang.
3657c478bd9Sstevel@tonic-gate 		 */
3667c478bd9Sstevel@tonic-gate 		if (fork() != 0) {
3677c478bd9Sstevel@tonic-gate 			devfsadm_exit(0);
368537714daSvikram 			/*NOTREACHED*/
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 		/* set directory to / so it coredumps there */
3727c478bd9Sstevel@tonic-gate 		if (chdir("/") == -1) {
3737c478bd9Sstevel@tonic-gate 			err_print(CHROOT_FAILED, strerror(errno));
3747c478bd9Sstevel@tonic-gate 		}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 		/* only one daemon can run at a time */
3777c478bd9Sstevel@tonic-gate 		if ((pid = enter_daemon_lock()) == getpid()) {
3787c478bd9Sstevel@tonic-gate 			detachfromtty();
3797c478bd9Sstevel@tonic-gate 			(void) cond_init(&cv, USYNC_THREAD, 0);
3807c478bd9Sstevel@tonic-gate 			(void) mutex_init(&count_lock, USYNC_THREAD, 0);
3817c478bd9Sstevel@tonic-gate 			if (thr_create(NULL, NULL,
382ff2aee48Scth 			    (void *(*)(void *))instance_flush_thread,
383ff2aee48Scth 			    NULL, THR_DETACHED, NULL) != 0) {
3847c478bd9Sstevel@tonic-gate 				err_print(CANT_CREATE_THREAD, "daemon",
3850a653502Swroche 				    strerror(errno));
3867c478bd9Sstevel@tonic-gate 				devfsadm_exit(1);
387537714daSvikram 				/*NOTREACHED*/
3887c478bd9Sstevel@tonic-gate 			}
3897c478bd9Sstevel@tonic-gate 
390ff2aee48Scth 			/* start the minor_fini_thread */
391ff2aee48Scth 			(void) mutex_init(&minor_fini_mutex, USYNC_THREAD, 0);
392ff2aee48Scth 			(void) cond_init(&minor_fini_cv, USYNC_THREAD, 0);
393ff2aee48Scth 			if (thr_create(NULL, NULL,
394ff2aee48Scth 			    (void *(*)(void *))minor_fini_thread,
395ff2aee48Scth 			    NULL, THR_DETACHED, NULL)) {
396ff2aee48Scth 				err_print(CANT_CREATE_THREAD, "minor_fini",
397ff2aee48Scth 				    strerror(errno));
398ff2aee48Scth 				devfsadm_exit(1);
399537714daSvikram 				/*NOTREACHED*/
400ff2aee48Scth 			}
401ff2aee48Scth 
402ff2aee48Scth 
4037c478bd9Sstevel@tonic-gate 			/*
4040dc974a9SCathy Zhou 			 * logindevperms need only be set
4057c478bd9Sstevel@tonic-gate 			 * in daemon mode and when root dir is "/".
4067c478bd9Sstevel@tonic-gate 			 */
4070dc974a9SCathy Zhou 			if (root_dir[0] == '\0')
4087c478bd9Sstevel@tonic-gate 				login_dev_enable = TRUE;
4097c478bd9Sstevel@tonic-gate 			daemon_update();
410537714daSvikram 			devfsadm_exit(0);
411537714daSvikram 			/*NOTREACHED*/
4127c478bd9Sstevel@tonic-gate 		} else {
4137c478bd9Sstevel@tonic-gate 			err_print(DAEMON_RUNNING, pid);
4147c478bd9Sstevel@tonic-gate 			devfsadm_exit(1);
415537714daSvikram 			/*NOTREACHED*/
4167c478bd9Sstevel@tonic-gate 		}
4177c478bd9Sstevel@tonic-gate 	} else {
4187c478bd9Sstevel@tonic-gate 		/* not a daemon, so just build /dev and /devices */
4198c206d17Spr 
4208c206d17Spr 		/*
4218c206d17Spr 		 * If turning off device allocation, load the
4228c206d17Spr 		 * minor_perm file because process_devinfo_tree() will
4238c206d17Spr 		 * need this in order to reset the permissions of the
4248c206d17Spr 		 * device files.
4258c206d17Spr 		 */
4268c206d17Spr 		if (devalloc_flag == DA_OFF) {
4278c206d17Spr 			read_minor_perm_file();
4288c206d17Spr 		}
4298c206d17Spr 
4307c478bd9Sstevel@tonic-gate 		process_devinfo_tree();
43145916cd2Sjpk 		if (devalloc_flag != 0)
43245916cd2Sjpk 			/* Enable/disable device allocation */
43345916cd2Sjpk 			_reset_devalloc(devalloc_flag);
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 	return (0);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate static void
439c9cc1492SJerry Gilliam update_drvconf(major_t major, int flags)
4407c478bd9Sstevel@tonic-gate {
441c9cc1492SJerry Gilliam 	if (modctl(MODLOADDRVCONF, major, flags) != 0)
4427c478bd9Sstevel@tonic-gate 		err_print(gettext("update_drvconf failed for major %d\n"),
4437c478bd9Sstevel@tonic-gate 		    major);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate static void
4477c478bd9Sstevel@tonic-gate load_dev_acl()
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	if (load_devpolicy() != 0)
4507c478bd9Sstevel@tonic-gate 		err_print(gettext("device policy load failed\n"));
4517c478bd9Sstevel@tonic-gate 	load_minor_perm_file();
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate /*
455facf4a8dSllai  * As devfsadm is run early in boot to provide the kernel with
456facf4a8dSllai  * minor_perm info, we might as well check for reconfig at the
457facf4a8dSllai  * same time to avoid running devfsadm twice.  This gets invoked
458facf4a8dSllai  * earlier than the env variable RECONFIG_BOOT is set up.
4597c478bd9Sstevel@tonic-gate  */
460facf4a8dSllai static void
461facf4a8dSllai check_reconfig_state()
4627c478bd9Sstevel@tonic-gate {
463facf4a8dSllai 	struct stat sb;
4647c478bd9Sstevel@tonic-gate 
465facf4a8dSllai 	if (s_stat("/reconfigure", &sb) == 0) {
466facf4a8dSllai 		(void) modctl(MODDEVNAME, MODDEVNAME_RECONFIG, 0);
4677c478bd9Sstevel@tonic-gate 	}
468facf4a8dSllai }
4697c478bd9Sstevel@tonic-gate 
470facf4a8dSllai static void
471facf4a8dSllai modctl_sysavail()
472facf4a8dSllai {
473facf4a8dSllai 	/*
474facf4a8dSllai 	 * Inform /dev that system is available, that
475facf4a8dSllai 	 * implicit reconfig can now be performed.
476facf4a8dSllai 	 */
477facf4a8dSllai 	(void) modctl(MODDEVNAME, MODDEVNAME_SYSAVAIL, 0);
478facf4a8dSllai }
4797c478bd9Sstevel@tonic-gate 
480facf4a8dSllai static void
481facf4a8dSllai set_lock_root(void)
482facf4a8dSllai {
483facf4a8dSllai 	struct stat sb;
484facf4a8dSllai 	char *lock_root;
485facf4a8dSllai 	size_t len;
4867c478bd9Sstevel@tonic-gate 
487facf4a8dSllai 	lock_root = attr_root ? attr_root : root_dir;
4887c478bd9Sstevel@tonic-gate 
489facf4a8dSllai 	len = strlen(lock_root) + strlen(ETCDEV) + 1;
490facf4a8dSllai 	etc_dev_dir = s_malloc(len);
491facf4a8dSllai 	(void) snprintf(etc_dev_dir, len, "%s%s", lock_root, ETCDEV);
4927c478bd9Sstevel@tonic-gate 
493facf4a8dSllai 	if (s_stat(etc_dev_dir, &sb) != 0) {
494facf4a8dSllai 		s_mkdirp(etc_dev_dir, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
495facf4a8dSllai 	} else if (!S_ISDIR(sb.st_mode)) {
496facf4a8dSllai 		err_print(NOT_DIR, etc_dev_dir);
497facf4a8dSllai 		devfsadm_exit(1);
498537714daSvikram 		/*NOTREACHED*/
499facf4a8dSllai 	}
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
502facf4a8dSllai 
5037c478bd9Sstevel@tonic-gate /*
5047c478bd9Sstevel@tonic-gate  * Parse arguments for all 6 programs handled from devfsadm.
5057c478bd9Sstevel@tonic-gate  */
5067c478bd9Sstevel@tonic-gate static void
5077c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[])
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	char opt;
5107c478bd9Sstevel@tonic-gate 	char get_linkcompat_opts = FALSE;
5117c478bd9Sstevel@tonic-gate 	char *compat_class;
5127c478bd9Sstevel@tonic-gate 	int num_aliases = 0;
5137c478bd9Sstevel@tonic-gate 	int len;
5147c478bd9Sstevel@tonic-gate 	int retval;
5156532b960SJerry Gilliam 	int config = TRUE;
5166532b960SJerry Gilliam 	int bind = FALSE;
5176532b960SJerry Gilliam 	int force_flag = FALSE;
5187c478bd9Sstevel@tonic-gate 	struct aliases *ap = NULL;
5197c478bd9Sstevel@tonic-gate 	struct aliases *a_head = NULL;
5207c478bd9Sstevel@tonic-gate 	struct aliases *a_tail = NULL;
5217c478bd9Sstevel@tonic-gate 	struct modconfig mc;
5227c478bd9Sstevel@tonic-gate 
5236532b960SJerry Gilliam 	(void) bzero(&mc, sizeof (mc));
5246532b960SJerry Gilliam 
5257c478bd9Sstevel@tonic-gate 	if (strcmp(prog, DISKS) == 0) {
5267c478bd9Sstevel@tonic-gate 		compat_class = "disk";
5277c478bd9Sstevel@tonic-gate 		get_linkcompat_opts = TRUE;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, TAPES) == 0) {
5307c478bd9Sstevel@tonic-gate 		compat_class = "tape";
5317c478bd9Sstevel@tonic-gate 		get_linkcompat_opts = TRUE;
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, PORTS) == 0) {
5347c478bd9Sstevel@tonic-gate 		compat_class = "port";
5357c478bd9Sstevel@tonic-gate 		get_linkcompat_opts = TRUE;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, AUDLINKS) == 0) {
5387c478bd9Sstevel@tonic-gate 		compat_class = "audio";
5397c478bd9Sstevel@tonic-gate 		get_linkcompat_opts = TRUE;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, DEVLINKS) == 0) {
5427c478bd9Sstevel@tonic-gate 		devlinktab_file = DEVLINKTAB_FILE;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 		build_devices = FALSE;
5457c478bd9Sstevel@tonic-gate 		load_attach_drv = FALSE;
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		while ((opt = getopt(argc, argv, "dnr:st:vV:")) != EOF) {
5487c478bd9Sstevel@tonic-gate 			switch (opt) {
5497c478bd9Sstevel@tonic-gate 			case 'd':
5507c478bd9Sstevel@tonic-gate 				file_mods = FALSE;
5517c478bd9Sstevel@tonic-gate 				flush_path_to_inst_enable = FALSE;
5527c478bd9Sstevel@tonic-gate 				devlinks_debug = TRUE;
5537c478bd9Sstevel@tonic-gate 				break;
5547c478bd9Sstevel@tonic-gate 			case 'n':
5557c478bd9Sstevel@tonic-gate 				/* prevent driver loading and deferred attach */
5567c478bd9Sstevel@tonic-gate 				load_attach_drv = FALSE;
5577c478bd9Sstevel@tonic-gate 				break;
5587c478bd9Sstevel@tonic-gate 			case 'r':
559facf4a8dSllai 				set_root_devices_dev_dir(optarg);
5607c478bd9Sstevel@tonic-gate 				if (zone_pathcheck(root_dir) !=
5617c478bd9Sstevel@tonic-gate 				    DEVFSADM_SUCCESS)
5627c478bd9Sstevel@tonic-gate 					devfsadm_exit(1);
563537714daSvikram 					/*NOTREACHED*/
5647c478bd9Sstevel@tonic-gate 				break;
5657c478bd9Sstevel@tonic-gate 			case 's':
5667c478bd9Sstevel@tonic-gate 				/*
5677c478bd9Sstevel@tonic-gate 				 * suppress.  don't create/remove links/nodes
5687c478bd9Sstevel@tonic-gate 				 * useful with -v or -V
5697c478bd9Sstevel@tonic-gate 				 */
5707c478bd9Sstevel@tonic-gate 				file_mods = FALSE;
5717c478bd9Sstevel@tonic-gate 				flush_path_to_inst_enable = FALSE;
5727c478bd9Sstevel@tonic-gate 				break;
5737c478bd9Sstevel@tonic-gate 			case 't':
5747c478bd9Sstevel@tonic-gate 				/* supply a non-default table file */
5757c478bd9Sstevel@tonic-gate 				devlinktab_file = optarg;
5767c478bd9Sstevel@tonic-gate 				break;
5777c478bd9Sstevel@tonic-gate 			case 'v':
5787c478bd9Sstevel@tonic-gate 				/* documented verbose flag */
5797c478bd9Sstevel@tonic-gate 				add_verbose_id(VERBOSE_MID);
5807c478bd9Sstevel@tonic-gate 				break;
5817c478bd9Sstevel@tonic-gate 			case 'V':
5827c478bd9Sstevel@tonic-gate 				/* undocumented for extra verbose levels */
5837c478bd9Sstevel@tonic-gate 				add_verbose_id(optarg);
5847c478bd9Sstevel@tonic-gate 				break;
5857c478bd9Sstevel@tonic-gate 			default:
5867c478bd9Sstevel@tonic-gate 				usage();
5877c478bd9Sstevel@tonic-gate 				break;
5887c478bd9Sstevel@tonic-gate 			}
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 		if (optind < argc) {
5927c478bd9Sstevel@tonic-gate 			usage();
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, DRVCONFIG) == 0) {
596c9cc1492SJerry Gilliam 		int update_only = 0;
5977c478bd9Sstevel@tonic-gate 		build_dev = FALSE;
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 		while ((opt =
600c9cc1492SJerry Gilliam 		    getopt(argc, argv, "a:bc:dfi:m:np:R:r:suvV:x")) != EOF) {
6017c478bd9Sstevel@tonic-gate 			switch (opt) {
6027c478bd9Sstevel@tonic-gate 			case 'a':
6037c478bd9Sstevel@tonic-gate 				ap = calloc(sizeof (struct aliases), 1);
6047c478bd9Sstevel@tonic-gate 				ap->a_name = dequote(optarg);
6057c478bd9Sstevel@tonic-gate 				len = strlen(ap->a_name) + 1;
6067c478bd9Sstevel@tonic-gate 				if (len > MAXMODCONFNAME) {
6077c478bd9Sstevel@tonic-gate 					err_print(ALIAS_TOO_LONG,
6087c478bd9Sstevel@tonic-gate 					    MAXMODCONFNAME, ap->a_name);
6097c478bd9Sstevel@tonic-gate 					devfsadm_exit(1);
610537714daSvikram 					/*NOTREACHED*/
6117c478bd9Sstevel@tonic-gate 				}
6127c478bd9Sstevel@tonic-gate 				ap->a_len = len;
6137c478bd9Sstevel@tonic-gate 				if (a_tail == NULL) {
6147c478bd9Sstevel@tonic-gate 					a_head = ap;
6157c478bd9Sstevel@tonic-gate 				} else {
6167c478bd9Sstevel@tonic-gate 					a_tail->a_next = ap;
6177c478bd9Sstevel@tonic-gate 				}
6187c478bd9Sstevel@tonic-gate 				a_tail = ap;
6197c478bd9Sstevel@tonic-gate 				num_aliases++;
6206532b960SJerry Gilliam 				bind = TRUE;
6217c478bd9Sstevel@tonic-gate 				break;
6227c478bd9Sstevel@tonic-gate 			case 'b':
6236532b960SJerry Gilliam 				bind = TRUE;
6247c478bd9Sstevel@tonic-gate 				break;
6257c478bd9Sstevel@tonic-gate 			case 'c':
6267c478bd9Sstevel@tonic-gate 				(void) strcpy(mc.drvclass, optarg);
6277c478bd9Sstevel@tonic-gate 				break;
6287c478bd9Sstevel@tonic-gate 			case 'd':
6297c478bd9Sstevel@tonic-gate 				/*
6307c478bd9Sstevel@tonic-gate 				 * need to keep for compatibility, but
6317c478bd9Sstevel@tonic-gate 				 * do nothing.
6327c478bd9Sstevel@tonic-gate 				 */
6337c478bd9Sstevel@tonic-gate 				break;
6346532b960SJerry Gilliam 			case 'f':
6356532b960SJerry Gilliam 				force_flag = TRUE;
6366532b960SJerry Gilliam 				break;
6377c478bd9Sstevel@tonic-gate 			case 'i':
6387c478bd9Sstevel@tonic-gate 				single_drv = TRUE;
6397c478bd9Sstevel@tonic-gate 				(void) strcpy(mc.drvname, optarg);
6407c478bd9Sstevel@tonic-gate 				driver = s_strdup(optarg);
6417c478bd9Sstevel@tonic-gate 				break;
6427c478bd9Sstevel@tonic-gate 			case 'm':
6437c478bd9Sstevel@tonic-gate 				mc.major = atoi(optarg);
6447c478bd9Sstevel@tonic-gate 				break;
6457c478bd9Sstevel@tonic-gate 			case 'n':
6467c478bd9Sstevel@tonic-gate 				/* prevent driver loading and deferred attach */
6477c478bd9Sstevel@tonic-gate 				load_attach_drv = FALSE;
6487c478bd9Sstevel@tonic-gate 				break;
6497c478bd9Sstevel@tonic-gate 			case 'p':
6507c478bd9Sstevel@tonic-gate 				/* specify alternate path_to_inst file */
6517c478bd9Sstevel@tonic-gate 				inst_file = s_strdup(optarg);
6527c478bd9Sstevel@tonic-gate 				break;
6537c478bd9Sstevel@tonic-gate 			case 'R':
6547c478bd9Sstevel@tonic-gate 				/*
6557c478bd9Sstevel@tonic-gate 				 * Private flag for suninstall to populate
6567c478bd9Sstevel@tonic-gate 				 * device information on the installed root.
6577c478bd9Sstevel@tonic-gate 				 */
6587c478bd9Sstevel@tonic-gate 				root_dir = s_strdup(optarg);
6597c478bd9Sstevel@tonic-gate 				if (zone_pathcheck(root_dir) !=
6607c478bd9Sstevel@tonic-gate 				    DEVFSADM_SUCCESS)
6617c478bd9Sstevel@tonic-gate 				devfsadm_exit(devfsadm_copy());
662537714daSvikram 				/*NOTREACHED*/
6637c478bd9Sstevel@tonic-gate 				break;
6647c478bd9Sstevel@tonic-gate 			case 'r':
6657c478bd9Sstevel@tonic-gate 				devices_dir = s_strdup(optarg);
6667c478bd9Sstevel@tonic-gate 				if (zone_pathcheck(devices_dir) !=
6677c478bd9Sstevel@tonic-gate 				    DEVFSADM_SUCCESS)
6687c478bd9Sstevel@tonic-gate 					devfsadm_exit(1);
669537714daSvikram 					/*NOTREACHED*/
6707c478bd9Sstevel@tonic-gate 				break;
6717c478bd9Sstevel@tonic-gate 			case 's':
6727c478bd9Sstevel@tonic-gate 				/*
6737c478bd9Sstevel@tonic-gate 				 * suppress.  don't create nodes
6747c478bd9Sstevel@tonic-gate 				 * useful with -v or -V
6757c478bd9Sstevel@tonic-gate 				 */
6767c478bd9Sstevel@tonic-gate 				file_mods = FALSE;
6777c478bd9Sstevel@tonic-gate 				flush_path_to_inst_enable = FALSE;
6787c478bd9Sstevel@tonic-gate 				break;
6796532b960SJerry Gilliam 			case 'u':
6806532b960SJerry Gilliam 				/*
6816532b960SJerry Gilliam 				 * Invoked via update_drv(1m) to update
6826532b960SJerry Gilliam 				 * the kernel's driver/alias binding
6836532b960SJerry Gilliam 				 * when removing one or more aliases.
6846532b960SJerry Gilliam 				 */
6856532b960SJerry Gilliam 				config = FALSE;
6866532b960SJerry Gilliam 				break;
6877c478bd9Sstevel@tonic-gate 			case 'v':
6887c478bd9Sstevel@tonic-gate 				/* documented verbose flag */
6897c478bd9Sstevel@tonic-gate 				add_verbose_id(VERBOSE_MID);
6907c478bd9Sstevel@tonic-gate 				break;
6917c478bd9Sstevel@tonic-gate 			case 'V':
6927c478bd9Sstevel@tonic-gate 				/* undocumented for extra verbose levels */
6937c478bd9Sstevel@tonic-gate 				add_verbose_id(optarg);
6947c478bd9Sstevel@tonic-gate 				break;
695c9cc1492SJerry Gilliam 			case 'x':
696c9cc1492SJerry Gilliam 				update_only = 1;
697c9cc1492SJerry Gilliam 				break;
6987c478bd9Sstevel@tonic-gate 			default:
6997c478bd9Sstevel@tonic-gate 				usage();
7007c478bd9Sstevel@tonic-gate 			}
7017c478bd9Sstevel@tonic-gate 		}
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 		if (optind < argc) {
7047c478bd9Sstevel@tonic-gate 			usage();
7057c478bd9Sstevel@tonic-gate 		}
7067c478bd9Sstevel@tonic-gate 
7076532b960SJerry Gilliam 		if (bind == TRUE) {
7086532b960SJerry Gilliam 			if ((mc.major == -1) || (mc.drvname[0] == NULL)) {
7096532b960SJerry Gilliam 				err_print(MAJOR_AND_B_FLAG);
7106532b960SJerry Gilliam 				devfsadm_exit(1);
7116532b960SJerry Gilliam 				/*NOTREACHED*/
7126532b960SJerry Gilliam 			}
713c9cc1492SJerry Gilliam 			mc.flags = 0;
714c9cc1492SJerry Gilliam 			if (force_flag)
715c9cc1492SJerry Gilliam 				mc.flags |= MOD_UNBIND_OVERRIDE;
716c9cc1492SJerry Gilliam 			if (update_only)
717c9cc1492SJerry Gilliam 				mc.flags |= MOD_ADDMAJBIND_UPDATE;
7187c478bd9Sstevel@tonic-gate 			mc.num_aliases = num_aliases;
7197c478bd9Sstevel@tonic-gate 			mc.ap = a_head;
7206532b960SJerry Gilliam 			retval =  modctl((config == TRUE) ? MODADDMAJBIND :
7216532b960SJerry Gilliam 			    MODREMDRVALIAS, NULL, (caddr_t)&mc);
7227c478bd9Sstevel@tonic-gate 			if (retval < 0) {
7236532b960SJerry Gilliam 				err_print((config == TRUE) ? MODCTL_ADDMAJBIND :
7246532b960SJerry Gilliam 				    MODCTL_REMMAJBIND);
7257c478bd9Sstevel@tonic-gate 			}
7267c478bd9Sstevel@tonic-gate 			devfsadm_exit(retval);
727537714daSvikram 			/*NOTREACHED*/
7287c478bd9Sstevel@tonic-gate 		}
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	} else if ((strcmp(prog, DEVFSADM) == 0) ||
7317c478bd9Sstevel@tonic-gate 	    (strcmp(prog, DEVFSADMD) == 0)) {
7327c478bd9Sstevel@tonic-gate 		char *zonename = NULL;
7337c478bd9Sstevel@tonic-gate 		int init_drvconf = 0;
7347c478bd9Sstevel@tonic-gate 		int init_perm = 0;
7357c478bd9Sstevel@tonic-gate 		int public_mode = 0;
736facf4a8dSllai 		int init_sysavail = 0;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 		if (strcmp(prog, DEVFSADMD) == 0) {
7397c478bd9Sstevel@tonic-gate 			daemon_mode = TRUE;
7407c478bd9Sstevel@tonic-gate 		}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		devlinktab_file = DEVLINKTAB_FILE;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 		while ((opt = getopt(argc, argv,
745c9cc1492SJerry Gilliam 		    "a:Cc:deIi:l:np:PR:r:sSt:uvV:x:")) != EOF) {
746facf4a8dSllai 			if (opt == 'I' || opt == 'P' || opt == 'S') {
7477c478bd9Sstevel@tonic-gate 				if (public_mode)
7487c478bd9Sstevel@tonic-gate 					usage();
7497c478bd9Sstevel@tonic-gate 			} else {
750facf4a8dSllai 				if (init_perm || init_drvconf || init_sysavail)
7517c478bd9Sstevel@tonic-gate 					usage();
7527c478bd9Sstevel@tonic-gate 				public_mode = 1;
7537c478bd9Sstevel@tonic-gate 			}
7547c478bd9Sstevel@tonic-gate 			switch (opt) {
755facf4a8dSllai 			case 'a':
756facf4a8dSllai 				attr_root = s_strdup(optarg);
757facf4a8dSllai 				break;
7587c478bd9Sstevel@tonic-gate 			case 'C':
7597c478bd9Sstevel@tonic-gate 				cleanup = TRUE;
7607c478bd9Sstevel@tonic-gate 				break;
7617c478bd9Sstevel@tonic-gate 			case 'c':
7627c478bd9Sstevel@tonic-gate 				num_classes++;
7630a653502Swroche 				classes = s_realloc(classes,
7640a653502Swroche 				    num_classes * sizeof (char *));
7657c478bd9Sstevel@tonic-gate 				classes[num_classes - 1] = optarg;
7667c478bd9Sstevel@tonic-gate 				break;
76745916cd2Sjpk 			case 'd':
7687c478bd9Sstevel@tonic-gate 				if (daemon_mode == FALSE) {
76945916cd2Sjpk 					/*
77045916cd2Sjpk 					 * Device allocation to be disabled.
77145916cd2Sjpk 					 */
77245916cd2Sjpk 					devalloc_flag = DA_OFF;
77345916cd2Sjpk 					build_dev = FALSE;
77445916cd2Sjpk 				}
77545916cd2Sjpk 				break;
77645916cd2Sjpk 			case 'e':
77745916cd2Sjpk 				if (daemon_mode == FALSE) {
77845916cd2Sjpk 					/*
77945916cd2Sjpk 					 * Device allocation to be enabled.
78045916cd2Sjpk 					 */
78145916cd2Sjpk 					devalloc_flag = DA_ON;
7827c478bd9Sstevel@tonic-gate 					build_dev = FALSE;
7837c478bd9Sstevel@tonic-gate 				}
7847c478bd9Sstevel@tonic-gate 				break;
7857c478bd9Sstevel@tonic-gate 			case 'I':	/* update kernel driver.conf cache */
7867c478bd9Sstevel@tonic-gate 				if (daemon_mode == TRUE)
7877c478bd9Sstevel@tonic-gate 					usage();
7887c478bd9Sstevel@tonic-gate 				init_drvconf = 1;
7897c478bd9Sstevel@tonic-gate 				break;
7907c478bd9Sstevel@tonic-gate 			case 'i':
7917c478bd9Sstevel@tonic-gate 				single_drv = TRUE;
7927c478bd9Sstevel@tonic-gate 				driver = s_strdup(optarg);
7937c478bd9Sstevel@tonic-gate 				break;
7947c478bd9Sstevel@tonic-gate 			case 'l':
7957c478bd9Sstevel@tonic-gate 				/* specify an alternate module load path */
7967c478bd9Sstevel@tonic-gate 				module_dirs = s_strdup(optarg);
7977c478bd9Sstevel@tonic-gate 				break;
7987c478bd9Sstevel@tonic-gate 			case 'n':
7997c478bd9Sstevel@tonic-gate 				/* prevent driver loading and deferred attach */
8007c478bd9Sstevel@tonic-gate 				load_attach_drv = FALSE;
8017c478bd9Sstevel@tonic-gate 				break;
8027c478bd9Sstevel@tonic-gate 			case 'p':
8037c478bd9Sstevel@tonic-gate 				/* specify alternate path_to_inst file */
8047c478bd9Sstevel@tonic-gate 				inst_file = s_strdup(optarg);
8057c478bd9Sstevel@tonic-gate 				break;
8067c478bd9Sstevel@tonic-gate 			case 'P':
8077c478bd9Sstevel@tonic-gate 				if (daemon_mode == TRUE)
8087c478bd9Sstevel@tonic-gate 					usage();
8097c478bd9Sstevel@tonic-gate 				/* load minor_perm and device_policy */
8107c478bd9Sstevel@tonic-gate 				init_perm = 1;
8117c478bd9Sstevel@tonic-gate 				break;
8127c478bd9Sstevel@tonic-gate 			case 'R':
8137c478bd9Sstevel@tonic-gate 				/*
8147c478bd9Sstevel@tonic-gate 				 * Private flag for suninstall to populate
8157c478bd9Sstevel@tonic-gate 				 * device information on the installed root.
8167c478bd9Sstevel@tonic-gate 				 */
8177c478bd9Sstevel@tonic-gate 				root_dir = s_strdup(optarg);
8187c478bd9Sstevel@tonic-gate 				devfsadm_exit(devfsadm_copy());
819537714daSvikram 				/*NOTREACHED*/
8207c478bd9Sstevel@tonic-gate 				break;
8217c478bd9Sstevel@tonic-gate 			case 'r':
822facf4a8dSllai 				set_root_devices_dev_dir(optarg);
8237c478bd9Sstevel@tonic-gate 				break;
8247c478bd9Sstevel@tonic-gate 			case 's':
8257c478bd9Sstevel@tonic-gate 				/*
8267c478bd9Sstevel@tonic-gate 				 * suppress. don't create/remove links/nodes
8277c478bd9Sstevel@tonic-gate 				 * useful with -v or -V
8287c478bd9Sstevel@tonic-gate 				 */
8297c478bd9Sstevel@tonic-gate 				file_mods = FALSE;
8307c478bd9Sstevel@tonic-gate 				flush_path_to_inst_enable = FALSE;
8317c478bd9Sstevel@tonic-gate 				break;
832facf4a8dSllai 			case 'S':
833facf4a8dSllai 				if (daemon_mode == TRUE)
834facf4a8dSllai 					usage();
835facf4a8dSllai 				init_sysavail = 1;
836facf4a8dSllai 				break;
8377c478bd9Sstevel@tonic-gate 			case 't':
8387c478bd9Sstevel@tonic-gate 				devlinktab_file = optarg;
8397c478bd9Sstevel@tonic-gate 				break;
840c9cc1492SJerry Gilliam 			case 'u':	/* complete configuration after */
841c9cc1492SJerry Gilliam 					/* adding a driver update-only */
842c9cc1492SJerry Gilliam 				if (daemon_mode == TRUE)
843c9cc1492SJerry Gilliam 					usage();
844c9cc1492SJerry Gilliam 				update_all_drivers = TRUE;
845c9cc1492SJerry Gilliam 				break;
8467c478bd9Sstevel@tonic-gate 			case 'v':
8477c478bd9Sstevel@tonic-gate 				/* documented verbose flag */
8487c478bd9Sstevel@tonic-gate 				add_verbose_id(VERBOSE_MID);
8497c478bd9Sstevel@tonic-gate 				break;
8507c478bd9Sstevel@tonic-gate 			case 'V':
8517c478bd9Sstevel@tonic-gate 				/* undocumented: specify verbose lvl */
8527c478bd9Sstevel@tonic-gate 				add_verbose_id(optarg);
8537c478bd9Sstevel@tonic-gate 				break;
8547c478bd9Sstevel@tonic-gate 			case 'x':
8557c478bd9Sstevel@tonic-gate 				/*
8567c478bd9Sstevel@tonic-gate 				 * x is the "private switch" option.  The
8577c478bd9Sstevel@tonic-gate 				 * goal is to not suck up all the other
8587c478bd9Sstevel@tonic-gate 				 * option letters.
8597c478bd9Sstevel@tonic-gate 				 */
8607c478bd9Sstevel@tonic-gate 				if (strcmp(optarg, "update_devlinksdb") == 0) {
8617c478bd9Sstevel@tonic-gate 					update_database = TRUE;
8627c478bd9Sstevel@tonic-gate 				} else if (strcmp(optarg, "no_dev") == 0) {
8637c478bd9Sstevel@tonic-gate 					/* don't build /dev */
8647c478bd9Sstevel@tonic-gate 					build_dev = FALSE;
8657c478bd9Sstevel@tonic-gate 				} else if (strcmp(optarg, "no_devices") == 0) {
8667c478bd9Sstevel@tonic-gate 					/* don't build /devices */
8677c478bd9Sstevel@tonic-gate 					build_devices = FALSE;
8687c478bd9Sstevel@tonic-gate 				} else if (strcmp(optarg, "no_p2i") == 0) {
8697c478bd9Sstevel@tonic-gate 					/* don't flush path_to_inst */
8707c478bd9Sstevel@tonic-gate 					flush_path_to_inst_enable = FALSE;
8717c478bd9Sstevel@tonic-gate 				} else if (strcmp(optarg, "use_dicache") == 0) {
8727c478bd9Sstevel@tonic-gate 					use_snapshot_cache = TRUE;
8737c478bd9Sstevel@tonic-gate 				} else {
8747c478bd9Sstevel@tonic-gate 					usage();
8757c478bd9Sstevel@tonic-gate 				}
8767c478bd9Sstevel@tonic-gate 				break;
8777c478bd9Sstevel@tonic-gate 			default:
8787c478bd9Sstevel@tonic-gate 				usage();
8797c478bd9Sstevel@tonic-gate 				break;
8807c478bd9Sstevel@tonic-gate 			}
8817c478bd9Sstevel@tonic-gate 		}
8827c478bd9Sstevel@tonic-gate 		if (optind < argc) {
8837c478bd9Sstevel@tonic-gate 			usage();
8847c478bd9Sstevel@tonic-gate 		}
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 		/*
8877c478bd9Sstevel@tonic-gate 		 * We're not in zone mode; Check to see if the rootpath
8887c478bd9Sstevel@tonic-gate 		 * collides with any zonepaths.
8897c478bd9Sstevel@tonic-gate 		 */
8907c478bd9Sstevel@tonic-gate 		if (zonename == NULL) {
8917c478bd9Sstevel@tonic-gate 			if (zone_pathcheck(root_dir) != DEVFSADM_SUCCESS)
8927c478bd9Sstevel@tonic-gate 				devfsadm_exit(1);
893537714daSvikram 				/*NOTREACHED*/
8947c478bd9Sstevel@tonic-gate 		}
8957c478bd9Sstevel@tonic-gate 
896facf4a8dSllai 		if (init_drvconf || init_perm || init_sysavail) {
8977c478bd9Sstevel@tonic-gate 			/*
8987c478bd9Sstevel@tonic-gate 			 * Load minor perm before force-loading drivers
8997c478bd9Sstevel@tonic-gate 			 * so the correct permissions are picked up.
9007c478bd9Sstevel@tonic-gate 			 */
901facf4a8dSllai 			if (init_perm) {
902facf4a8dSllai 				check_reconfig_state();
9037c478bd9Sstevel@tonic-gate 				load_dev_acl();
904facf4a8dSllai 			}
9057c478bd9Sstevel@tonic-gate 			if (init_drvconf)
906c9cc1492SJerry Gilliam 				update_drvconf((major_t)-1, 0);
907facf4a8dSllai 			if (init_sysavail)
908facf4a8dSllai 				modctl_sysavail();
9097c478bd9Sstevel@tonic-gate 			devfsadm_exit(0);
910537714daSvikram 			/*NOTREACHED*/
9117c478bd9Sstevel@tonic-gate 		}
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	if (get_linkcompat_opts == TRUE) {
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 		build_devices = FALSE;
9187c478bd9Sstevel@tonic-gate 		load_attach_drv = FALSE;
9197c478bd9Sstevel@tonic-gate 		num_classes++;
9207c478bd9Sstevel@tonic-gate 		classes = s_realloc(classes, num_classes *
9217c478bd9Sstevel@tonic-gate 		    sizeof (char *));
9227c478bd9Sstevel@tonic-gate 		classes[num_classes - 1] = compat_class;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 		while ((opt = getopt(argc, argv, "Cnr:svV:")) != EOF) {
9257c478bd9Sstevel@tonic-gate 			switch (opt) {
9267c478bd9Sstevel@tonic-gate 			case 'C':
9277c478bd9Sstevel@tonic-gate 				cleanup = TRUE;
9287c478bd9Sstevel@tonic-gate 				break;
9297c478bd9Sstevel@tonic-gate 			case 'n':
9307c478bd9Sstevel@tonic-gate 				/* prevent driver loading or deferred attach */
9317c478bd9Sstevel@tonic-gate 				load_attach_drv = FALSE;
9327c478bd9Sstevel@tonic-gate 				break;
9337c478bd9Sstevel@tonic-gate 			case 'r':
934facf4a8dSllai 				set_root_devices_dev_dir(optarg);
9357c478bd9Sstevel@tonic-gate 				if (zone_pathcheck(root_dir) !=
9367c478bd9Sstevel@tonic-gate 				    DEVFSADM_SUCCESS)
9377c478bd9Sstevel@tonic-gate 					devfsadm_exit(1);
938537714daSvikram 					/*NOTREACHED*/
9397c478bd9Sstevel@tonic-gate 				break;
9407c478bd9Sstevel@tonic-gate 			case 's':
9417c478bd9Sstevel@tonic-gate 				/* suppress.  don't create/remove links/nodes */
9427c478bd9Sstevel@tonic-gate 				/* useful with -v or -V */
9437c478bd9Sstevel@tonic-gate 				file_mods = FALSE;
9447c478bd9Sstevel@tonic-gate 				flush_path_to_inst_enable = FALSE;
9457c478bd9Sstevel@tonic-gate 				break;
9467c478bd9Sstevel@tonic-gate 			case 'v':
9477c478bd9Sstevel@tonic-gate 				/* documented verbose flag */
9487c478bd9Sstevel@tonic-gate 				add_verbose_id(VERBOSE_MID);
9497c478bd9Sstevel@tonic-gate 				break;
9507c478bd9Sstevel@tonic-gate 			case 'V':
9517c478bd9Sstevel@tonic-gate 				/* undocumented for extra verbose levels */
9527c478bd9Sstevel@tonic-gate 				add_verbose_id(optarg);
9537c478bd9Sstevel@tonic-gate 				break;
9547c478bd9Sstevel@tonic-gate 			default:
9557c478bd9Sstevel@tonic-gate 				usage();
9567c478bd9Sstevel@tonic-gate 			}
9577c478bd9Sstevel@tonic-gate 		}
9587c478bd9Sstevel@tonic-gate 		if (optind < argc) {
9597c478bd9Sstevel@tonic-gate 			usage();
9607c478bd9Sstevel@tonic-gate 		}
9617c478bd9Sstevel@tonic-gate 	}
962facf4a8dSllai 	set_lock_root();
9637c478bd9Sstevel@tonic-gate }
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate void
9667c478bd9Sstevel@tonic-gate usage(void)
9677c478bd9Sstevel@tonic-gate {
9687c478bd9Sstevel@tonic-gate 	if (strcmp(prog, DEVLINKS) == 0) {
9697c478bd9Sstevel@tonic-gate 		err_print(DEVLINKS_USAGE);
9707c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, DRVCONFIG) == 0) {
9717c478bd9Sstevel@tonic-gate 		err_print(DRVCONFIG_USAGE);
9727c478bd9Sstevel@tonic-gate 	} else if ((strcmp(prog, DEVFSADM) == 0) ||
9730a653502Swroche 	    (strcmp(prog, DEVFSADMD) == 0)) {
9747c478bd9Sstevel@tonic-gate 		err_print(DEVFSADM_USAGE);
9757c478bd9Sstevel@tonic-gate 	} else {
9767c478bd9Sstevel@tonic-gate 		err_print(COMPAT_LINK_USAGE);
9777c478bd9Sstevel@tonic-gate 	}
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	devfsadm_exit(1);
980537714daSvikram 	/*NOTREACHED*/
9817c478bd9Sstevel@tonic-gate }
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate static void
9847c478bd9Sstevel@tonic-gate devi_tree_walk(struct dca_impl *dcip, int flags, char *ev_subclass)
9857c478bd9Sstevel@tonic-gate {
9867c478bd9Sstevel@tonic-gate 	char *msg, *name;
9877c478bd9Sstevel@tonic-gate 	struct mlist	mlist = {0};
9887c478bd9Sstevel@tonic-gate 	di_node_t	node;
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "devi_tree_walk: root=%s, minor=%s, driver=%s,"
9917c478bd9Sstevel@tonic-gate 	    " error=%d, flags=%u\n", dcip->dci_root,
9927c478bd9Sstevel@tonic-gate 	    dcip->dci_minor ? dcip->dci_minor : "<NULL>",
9937c478bd9Sstevel@tonic-gate 	    dcip->dci_driver ? dcip->dci_driver : "<NULL>", dcip->dci_error,
9947c478bd9Sstevel@tonic-gate 	    dcip->dci_flags);
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate 	assert(dcip->dci_root);
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	if (dcip->dci_flags & DCA_LOAD_DRV) {
9997c478bd9Sstevel@tonic-gate 		node = di_init_driver(dcip->dci_driver, flags);
10007c478bd9Sstevel@tonic-gate 		msg = DRIVER_FAILURE;
10017c478bd9Sstevel@tonic-gate 		name = dcip->dci_driver;
10027c478bd9Sstevel@tonic-gate 	} else {
10037c478bd9Sstevel@tonic-gate 		node = di_init(dcip->dci_root, flags);
10047c478bd9Sstevel@tonic-gate 		msg = DI_INIT_FAILED;
10057c478bd9Sstevel@tonic-gate 		name = dcip->dci_root;
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
10097c478bd9Sstevel@tonic-gate 		dcip->dci_error = errno;
10107c478bd9Sstevel@tonic-gate 		/*
10117c478bd9Sstevel@tonic-gate 		 * Rapid hotplugging (commonly seen during USB testing),
10127c478bd9Sstevel@tonic-gate 		 * may remove a device before the create event for it
10137c478bd9Sstevel@tonic-gate 		 * has been processed. To prevent alarming users with
10147c478bd9Sstevel@tonic-gate 		 * a superfluous message, we suppress error messages
10157c478bd9Sstevel@tonic-gate 		 * for ENXIO and hotplug.
10167c478bd9Sstevel@tonic-gate 		 */
10177c478bd9Sstevel@tonic-gate 		if (!(errno == ENXIO && (dcip->dci_flags & DCA_HOT_PLUG)))
10187c478bd9Sstevel@tonic-gate 			err_print(msg, name, strerror(dcip->dci_error));
10197c478bd9Sstevel@tonic-gate 		return;
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	if (dcip->dci_flags & DCA_FLUSH_PATHINST)
10237c478bd9Sstevel@tonic-gate 		flush_path_to_inst();
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	dcip->dci_arg = &mlist;
102694c894bbSVikram Hegde 	devi_root_node = node;	/* protected by lock_dev() */
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "walking device tree\n");
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 	(void) di_walk_minor(node, NULL, DI_CHECK_ALIAS, dcip,
10317c478bd9Sstevel@tonic-gate 	    check_minor_type);
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	process_deferred_links(dcip, DCA_CREATE_LINK);
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	dcip->dci_arg = NULL;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	/*
10387c478bd9Sstevel@tonic-gate 	 * Finished creating devfs files and dev links.
10390dc974a9SCathy Zhou 	 * Log sysevent.
10407c478bd9Sstevel@tonic-gate 	 */
10417c478bd9Sstevel@tonic-gate 	if (ev_subclass)
1042f05faa4eSjacobs 		build_and_enq_event(EC_DEV_ADD, ev_subclass, dcip->dci_root,
104330294554Sphitran 		    node, dcip->dci_minor);
10447c478bd9Sstevel@tonic-gate 
104545916cd2Sjpk 	/* Add new device to device allocation database */
104645916cd2Sjpk 	if (system_labeled && update_devdb) {
104745916cd2Sjpk 		_update_devalloc_db(&devlist, 0, DA_ADD, NULL, root_dir);
104845916cd2Sjpk 		update_devdb = 0;
104945916cd2Sjpk 	}
105045916cd2Sjpk 
105194c894bbSVikram Hegde 	devi_root_node = DI_NODE_NIL;	/* protected by lock_dev() */
10527c478bd9Sstevel@tonic-gate 	di_fini(node);
10537c478bd9Sstevel@tonic-gate }
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate static void
10567c478bd9Sstevel@tonic-gate process_deferred_links(struct dca_impl *dcip, int flags)
10577c478bd9Sstevel@tonic-gate {
10587c478bd9Sstevel@tonic-gate 	struct mlist	*dep;
10597c478bd9Sstevel@tonic-gate 	struct minor	*mp, *smp;
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "processing deferred links\n");
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	dep = dcip->dci_arg;
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 	/*
10667c478bd9Sstevel@tonic-gate 	 * The list head is not used during the deferred create phase
10677c478bd9Sstevel@tonic-gate 	 */
10687c478bd9Sstevel@tonic-gate 	dcip->dci_arg = NULL;
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate 	assert(dep);
10717c478bd9Sstevel@tonic-gate 	assert((dep->head == NULL) ^ (dep->tail != NULL));
10727c478bd9Sstevel@tonic-gate 	assert(flags == DCA_FREE_LIST || flags == DCA_CREATE_LINK);
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	for (smp = NULL, mp = dep->head; mp; mp = mp->next) {
10757c478bd9Sstevel@tonic-gate 		if (flags == DCA_CREATE_LINK)
10767c478bd9Sstevel@tonic-gate 			(void) check_minor_type(mp->node, mp->minor, dcip);
10777c478bd9Sstevel@tonic-gate 		free(smp);
10787c478bd9Sstevel@tonic-gate 		smp = mp;
10797c478bd9Sstevel@tonic-gate 	}
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	free(smp);
10827c478bd9Sstevel@tonic-gate }
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate /*
10857c478bd9Sstevel@tonic-gate  * Called in non-daemon mode to take a snap shot of the devinfo tree.
10867c478bd9Sstevel@tonic-gate  * Then it calls the appropriate functions to build /devices and /dev.
10877c478bd9Sstevel@tonic-gate  * It also flushes path_to_inst.
108898a9beefShtk  * Except in the devfsadm -i (single driver case), the flags used by devfsadm
108998a9beefShtk  * needs to match DI_CACHE_SNAPSHOT_FLAGS. That will make DINFOCACHE snapshot
109098a9beefShtk  * updated.
10917c478bd9Sstevel@tonic-gate  */
10927c478bd9Sstevel@tonic-gate void
10937c478bd9Sstevel@tonic-gate process_devinfo_tree()
10947c478bd9Sstevel@tonic-gate {
109598a9beefShtk 	uint_t		flags;
10967c478bd9Sstevel@tonic-gate 	struct dca_impl	dci;
10977c478bd9Sstevel@tonic-gate 	char		name[MAXNAMELEN];
10987c478bd9Sstevel@tonic-gate 	char		*fcn = "process_devinfo_tree: ";
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "%senter\n", fcn);
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	dca_impl_init("/", NULL, &dci);
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate 	lock_dev();
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 	/*
11077c478bd9Sstevel@tonic-gate 	 * Update kernel driver.conf cache when devfsadm/drvconfig
11087c478bd9Sstevel@tonic-gate 	 * is invoked to build /devices and /dev.
11097c478bd9Sstevel@tonic-gate 	 */
1110c9cc1492SJerry Gilliam 	if (update_all_drivers || load_attach_drv) {
1111c9cc1492SJerry Gilliam 		update_drvconf((major_t)-1,
1112c9cc1492SJerry Gilliam 		    update_all_drivers ? MOD_LOADDRVCONF_RECONF : 0);
1113c9cc1492SJerry Gilliam 	}
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	if (single_drv == TRUE) {
11167c478bd9Sstevel@tonic-gate 		/*
11177c478bd9Sstevel@tonic-gate 		 * load a single driver, but walk the entire devinfo tree
11187c478bd9Sstevel@tonic-gate 		 */
11197c478bd9Sstevel@tonic-gate 		if (load_attach_drv == FALSE)
11207c478bd9Sstevel@tonic-gate 			err_print(DRV_LOAD_REQD);
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 		vprint(CHATTY_MID, "%sattaching driver (%s)\n", fcn, driver);
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 		dci.dci_flags |= DCA_LOAD_DRV;
11257c478bd9Sstevel@tonic-gate 		(void) snprintf(name, sizeof (name), "%s", driver);
11267c478bd9Sstevel@tonic-gate 		dci.dci_driver = name;
112798a9beefShtk 		flags = DINFOCPYALL | DINFOPATH;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	} else if (load_attach_drv == TRUE) {
11307c478bd9Sstevel@tonic-gate 		/*
113145916cd2Sjpk 		 * Load and attach all drivers, then walk the entire tree.
11327c478bd9Sstevel@tonic-gate 		 * If the cache flag is set, use DINFOCACHE to get cached
11337c478bd9Sstevel@tonic-gate 		 * data.
11347c478bd9Sstevel@tonic-gate 		 */
11357c478bd9Sstevel@tonic-gate 		if (use_snapshot_cache == TRUE) {
11367c478bd9Sstevel@tonic-gate 			flags = DINFOCACHE;
11377c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "%susing snapshot cache\n", fcn);
11387c478bd9Sstevel@tonic-gate 		} else {
11397c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "%sattaching all drivers\n", fcn);
114098a9beefShtk 			flags = DI_CACHE_SNAPSHOT_FLAGS;
11413c34adc5Sramat 			if (cleanup) {
11423c34adc5Sramat 				/*
11433c34adc5Sramat 				 * remove dangling entries from /etc/devices
11443c34adc5Sramat 				 * files.
11453c34adc5Sramat 				 */
11463c34adc5Sramat 				flags |= DINFOCLEANUP;
11473c34adc5Sramat 			}
11487c478bd9Sstevel@tonic-gate 		}
114934087db8Shtk 	} else {
115034087db8Shtk 		/*
115134087db8Shtk 		 * For devlinks, disks, ports, tapes and devfsadm -n,
115234087db8Shtk 		 * just need to take a snapshot with active devices.
115334087db8Shtk 		 */
115434087db8Shtk 		vprint(CHATTY_MID, "%staking snapshot of active devices\n",
11550a653502Swroche 		    fcn);
115634087db8Shtk 		flags = DINFOCPYALL;
11577c478bd9Sstevel@tonic-gate 	}
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 	if (((load_attach_drv == TRUE) || (single_drv == TRUE)) &&
11607c478bd9Sstevel@tonic-gate 	    (build_devices == TRUE)) {
11617c478bd9Sstevel@tonic-gate 		dci.dci_flags |= DCA_FLUSH_PATHINST;
11627c478bd9Sstevel@tonic-gate 	}
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	/* handle pre-cleanup operations desired by the modules. */
11657c478bd9Sstevel@tonic-gate 	pre_and_post_cleanup(RM_PRE);
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	devi_tree_walk(&dci, flags, NULL);
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate 	if (dci.dci_error) {
11707c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1171537714daSvikram 		/*NOTREACHED*/
11727c478bd9Sstevel@tonic-gate 	}
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate 	/* handle post-cleanup operations desired by the modules. */
11757c478bd9Sstevel@tonic-gate 	pre_and_post_cleanup(RM_POST);
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	unlock_dev(SYNC_STATE);
11787c478bd9Sstevel@tonic-gate }
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11817c478bd9Sstevel@tonic-gate static void
11827c478bd9Sstevel@tonic-gate print_cache_signal(int signo)
11837c478bd9Sstevel@tonic-gate {
11847c478bd9Sstevel@tonic-gate 	if (signal(SIGUSR1, print_cache_signal) == SIG_ERR) {
11857c478bd9Sstevel@tonic-gate 		err_print("signal SIGUSR1 failed: %s\n", strerror(errno));
11867c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1187537714daSvikram 		/*NOTREACHED*/
11887c478bd9Sstevel@tonic-gate 	}
11897c478bd9Sstevel@tonic-gate }
11907c478bd9Sstevel@tonic-gate 
1191facf4a8dSllai static void
1192facf4a8dSllai revoke_lookup_door(void)
1193facf4a8dSllai {
1194facf4a8dSllai 	if (lookup_door_fd != -1) {
1195facf4a8dSllai 		if (door_revoke(lookup_door_fd) == -1) {
1196facf4a8dSllai 			err_print("door_revoke of %s failed - %s\n",
1197facf4a8dSllai 			    lookup_door_path, strerror(errno));
1198facf4a8dSllai 		}
1199facf4a8dSllai 	}
1200facf4a8dSllai }
1201facf4a8dSllai 
1202facf4a8dSllai /*ARGSUSED*/
1203facf4a8dSllai static void
1204facf4a8dSllai catch_exit(int signo)
1205facf4a8dSllai {
1206facf4a8dSllai 	revoke_lookup_door();
1207facf4a8dSllai }
1208facf4a8dSllai 
12097c478bd9Sstevel@tonic-gate /*
12107c478bd9Sstevel@tonic-gate  * Register with eventd for messages. Create doors for synchronous
12117c478bd9Sstevel@tonic-gate  * link creation.
12127c478bd9Sstevel@tonic-gate  */
12137c478bd9Sstevel@tonic-gate static void
12147c478bd9Sstevel@tonic-gate daemon_update(void)
12157c478bd9Sstevel@tonic-gate {
12167c478bd9Sstevel@tonic-gate 	int fd;
12177c478bd9Sstevel@tonic-gate 	char *fcn = "daemon_update: ";
12187c478bd9Sstevel@tonic-gate 	char door_file[MAXPATHLEN];
12197c478bd9Sstevel@tonic-gate 	const char *subclass_list;
12207c478bd9Sstevel@tonic-gate 	sysevent_handle_t *sysevent_hp;
12217c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "%senter\n", fcn);
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	if (signal(SIGUSR1, print_cache_signal) == SIG_ERR) {
12247c478bd9Sstevel@tonic-gate 		err_print("signal SIGUSR1 failed: %s\n", strerror(errno));
12257c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1226537714daSvikram 		/*NOTREACHED*/
12277c478bd9Sstevel@tonic-gate 	}
1228facf4a8dSllai 	if (signal(SIGTERM, catch_exit) == SIG_ERR) {
1229facf4a8dSllai 		err_print("signal SIGTERM failed: %s\n", strerror(errno));
1230facf4a8dSllai 		devfsadm_exit(1);
1231537714daSvikram 		/*NOTREACHED*/
1232facf4a8dSllai 	}
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 	if (snprintf(door_file, sizeof (door_file),
1235facf4a8dSllai 	    "%s%s", attr_root ? attr_root : root_dir, DEVFSADM_SERVICE_DOOR)
1236facf4a8dSllai 	    >= sizeof (door_file)) {
12377c478bd9Sstevel@tonic-gate 		err_print("update_daemon failed to open sysevent service "
12387c478bd9Sstevel@tonic-gate 		    "door\n");
12397c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1240537714daSvikram 		/*NOTREACHED*/
12417c478bd9Sstevel@tonic-gate 	}
12427c478bd9Sstevel@tonic-gate 	if ((sysevent_hp = sysevent_open_channel_alt(
12437c478bd9Sstevel@tonic-gate 	    door_file)) == NULL) {
12447c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR,
12450a653502Swroche 		    door_file, strerror(errno));
12467c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1247537714daSvikram 		/*NOTREACHED*/
12487c478bd9Sstevel@tonic-gate 	}
12497c478bd9Sstevel@tonic-gate 	if (sysevent_bind_subscriber(sysevent_hp, event_handler) != 0) {
12507c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR,
12517c478bd9Sstevel@tonic-gate 		    door_file, strerror(errno));
12527c478bd9Sstevel@tonic-gate 		(void) sysevent_close_channel(sysevent_hp);
12537c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1254537714daSvikram 		/*NOTREACHED*/
12557c478bd9Sstevel@tonic-gate 	}
12567c478bd9Sstevel@tonic-gate 	subclass_list = EC_SUB_ALL;
12577c478bd9Sstevel@tonic-gate 	if (sysevent_register_event(sysevent_hp, EC_ALL, &subclass_list, 1)
12587c478bd9Sstevel@tonic-gate 	    != 0) {
12597c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR,
12607c478bd9Sstevel@tonic-gate 		    door_file, strerror(errno));
12617c478bd9Sstevel@tonic-gate 		(void) sysevent_unbind_subscriber(sysevent_hp);
12627c478bd9Sstevel@tonic-gate 		(void) sysevent_close_channel(sysevent_hp);
12637c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1264537714daSvikram 		/*NOTREACHED*/
12657c478bd9Sstevel@tonic-gate 	}
1266facf4a8dSllai 	if (snprintf(door_file, sizeof (door_file), "%s/%s",
1267facf4a8dSllai 	    etc_dev_dir, DEVFSADM_SYNCH_DOOR) >= sizeof (door_file)) {
1268facf4a8dSllai 		err_print(CANT_CREATE_DOOR, DEVFSADM_SYNCH_DOOR,
12697c478bd9Sstevel@tonic-gate 		    strerror(ENAMETOOLONG));
12707c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1271537714daSvikram 		/*NOTREACHED*/
12727c478bd9Sstevel@tonic-gate 	}
1273facf4a8dSllai 
12747c478bd9Sstevel@tonic-gate 	(void) s_unlink(door_file);
1275facf4a8dSllai 	if ((fd = open(door_file, O_RDWR | O_CREAT, SYNCH_DOOR_PERMS)) == -1) {
1276facf4a8dSllai 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
12777c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1278537714daSvikram 		/*NOTREACHED*/
12797c478bd9Sstevel@tonic-gate 	}
12807c478bd9Sstevel@tonic-gate 	(void) close(fd);
1281facf4a8dSllai 
1282facf4a8dSllai 	if ((fd = door_create(sync_handler, NULL,
12837c478bd9Sstevel@tonic-gate 	    DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
1284facf4a8dSllai 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
12857c478bd9Sstevel@tonic-gate 		(void) s_unlink(door_file);
12867c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1287537714daSvikram 		/*NOTREACHED*/
12887c478bd9Sstevel@tonic-gate 	}
1289facf4a8dSllai 
12907c478bd9Sstevel@tonic-gate 	if (fattach(fd, door_file) == -1) {
1291facf4a8dSllai 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
12927c478bd9Sstevel@tonic-gate 		(void) s_unlink(door_file);
12937c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1294537714daSvikram 		/*NOTREACHED*/
12957c478bd9Sstevel@tonic-gate 	}
12967c478bd9Sstevel@tonic-gate 
1297facf4a8dSllai 	/*
1298facf4a8dSllai 	 * devname_lookup_door
1299facf4a8dSllai 	 */
1300facf4a8dSllai 	if (snprintf(door_file, sizeof (door_file), "%s/%s",
1301facf4a8dSllai 	    etc_dev_dir, DEVNAME_LOOKUP_DOOR) >= sizeof (door_file)) {
1302facf4a8dSllai 		err_print(CANT_CREATE_DOOR, DEVNAME_LOOKUP_DOOR,
1303facf4a8dSllai 		    strerror(ENAMETOOLONG));
1304facf4a8dSllai 		devfsadm_exit(1);
1305537714daSvikram 		/*NOTREACHED*/
1306facf4a8dSllai 	}
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 	(void) s_unlink(door_file);
1309facf4a8dSllai 	if ((fd = open(door_file, O_RDWR | O_CREAT, S_IRUSR|S_IWUSR)) == -1) {
13107c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
13117c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1312537714daSvikram 		/*NOTREACHED*/
13137c478bd9Sstevel@tonic-gate 	}
13147c478bd9Sstevel@tonic-gate 	(void) close(fd);
13157c478bd9Sstevel@tonic-gate 
1316facf4a8dSllai 	if ((fd = door_create(devname_lookup_handler, NULL,
1317facf4a8dSllai 	    DOOR_REFUSE_DESC)) == -1) {
13187c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
13197c478bd9Sstevel@tonic-gate 		(void) s_unlink(door_file);
13207c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1321537714daSvikram 		/*NOTREACHED*/
13227c478bd9Sstevel@tonic-gate 	}
13237c478bd9Sstevel@tonic-gate 
1324facf4a8dSllai 	(void) fdetach(door_file);
1325facf4a8dSllai 	lookup_door_path = s_strdup(door_file);
1326facf4a8dSllai retry:
13277c478bd9Sstevel@tonic-gate 	if (fattach(fd, door_file) == -1) {
1328facf4a8dSllai 		if (errno == EBUSY)
1329facf4a8dSllai 			goto retry;
13307c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
13317c478bd9Sstevel@tonic-gate 		(void) s_unlink(door_file);
13327c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
1333537714daSvikram 		/*NOTREACHED*/
13347c478bd9Sstevel@tonic-gate 	}
1335facf4a8dSllai 	lookup_door_fd = fd;
13367c478bd9Sstevel@tonic-gate 
1337facf4a8dSllai 	/* pass down the door name to kernel for door_ki_open */
1338facf4a8dSllai 	if (devname_kcall(MODDEVNAME_LOOKUPDOOR, (void *)door_file) != 0)
1339facf4a8dSllai 		err_print(DEVNAME_CONTACT_FAILED, strerror(errno));
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "%spausing\n", fcn);
13427c478bd9Sstevel@tonic-gate 	for (;;) {
13437c478bd9Sstevel@tonic-gate 		(void) pause();
13447c478bd9Sstevel@tonic-gate 	}
13457c478bd9Sstevel@tonic-gate }
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13487c478bd9Sstevel@tonic-gate static void
13497c478bd9Sstevel@tonic-gate sync_handler(void *cookie, char *ap, size_t asize,
13507c478bd9Sstevel@tonic-gate     door_desc_t *dp, uint_t ndesc)
13517c478bd9Sstevel@tonic-gate {
13527c478bd9Sstevel@tonic-gate 	door_cred_t	dcred;
13537c478bd9Sstevel@tonic-gate 	struct dca_off	*dcp, rdca;
13547c478bd9Sstevel@tonic-gate 	struct dca_impl dci;
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	/*
13577c478bd9Sstevel@tonic-gate 	 * Must be root to make this call
13587c478bd9Sstevel@tonic-gate 	 * If caller is not root, don't touch its data.
13597c478bd9Sstevel@tonic-gate 	 */
13607c478bd9Sstevel@tonic-gate 	if (door_cred(&dcred) != 0 || dcred.dc_euid != 0) {
13617c478bd9Sstevel@tonic-gate 		dcp = &rdca;
13627c478bd9Sstevel@tonic-gate 		dcp->dca_error = EPERM;
13637c478bd9Sstevel@tonic-gate 		goto out;
13647c478bd9Sstevel@tonic-gate 	}
13657c478bd9Sstevel@tonic-gate 
13667c478bd9Sstevel@tonic-gate 	assert(ap);
13677c478bd9Sstevel@tonic-gate 	assert(asize == sizeof (*dcp));
13687c478bd9Sstevel@tonic-gate 
13697c478bd9Sstevel@tonic-gate 	dcp = (void *)ap;
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	/*
13727c478bd9Sstevel@tonic-gate 	 * Root is always present and is the first component of "name" member
13737c478bd9Sstevel@tonic-gate 	 */
13747c478bd9Sstevel@tonic-gate 	assert(dcp->dca_root == 0);
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate 	/*
13777c478bd9Sstevel@tonic-gate 	 * The structure passed in by the door_client uses offsets
13787c478bd9Sstevel@tonic-gate 	 * instead of pointers to work across address space boundaries.
13797c478bd9Sstevel@tonic-gate 	 * Now copy the data into a structure (dca_impl) which uses
13807c478bd9Sstevel@tonic-gate 	 * pointers.
13817c478bd9Sstevel@tonic-gate 	 */
13827c478bd9Sstevel@tonic-gate 	dci.dci_root = &dcp->dca_name[dcp->dca_root];
13837c478bd9Sstevel@tonic-gate 	dci.dci_minor = dcp->dca_minor ? &dcp->dca_name[dcp->dca_minor] : NULL;
13847c478bd9Sstevel@tonic-gate 	dci.dci_driver =
13857c478bd9Sstevel@tonic-gate 	    dcp->dca_driver ? &dcp->dca_name[dcp->dca_driver] : NULL;
13867c478bd9Sstevel@tonic-gate 	dci.dci_error = 0;
13877c478bd9Sstevel@tonic-gate 	dci.dci_flags = dcp->dca_flags | (dci.dci_driver ? DCA_LOAD_DRV : 0);
13887c478bd9Sstevel@tonic-gate 	dci.dci_arg = NULL;
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	lock_dev();
13917c478bd9Sstevel@tonic-gate 	devi_tree_walk(&dci, DINFOCPYALL, NULL);
13927c478bd9Sstevel@tonic-gate 	dcp->dca_error = dci.dci_error;
13937c478bd9Sstevel@tonic-gate 
1394ff2aee48Scth 	if (dcp->dca_flags & DCA_DEVLINK_SYNC)
1395ff2aee48Scth 		unlock_dev(SYNC_STATE);
1396ff2aee48Scth 	else
1397ff2aee48Scth 		unlock_dev(CACHE_STATE);
13987c478bd9Sstevel@tonic-gate 
1399ff2aee48Scth out:	(void) door_return((char *)dcp, sizeof (*dcp), NULL, 0);
14007c478bd9Sstevel@tonic-gate }
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate static void
14037c478bd9Sstevel@tonic-gate lock_dev(void)
14047c478bd9Sstevel@tonic-gate {
14057c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "lock_dev(): entered\n");
14067c478bd9Sstevel@tonic-gate 
14077c478bd9Sstevel@tonic-gate 	if (build_dev == FALSE)
14087c478bd9Sstevel@tonic-gate 		return;
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	/* lockout other threads from /dev */
14110a653502Swroche 	while (sema_wait(&dev_sema) != 0)
14120a653502Swroche 		;
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	/*
14157c478bd9Sstevel@tonic-gate 	 * Lock out other devfsadm processes from /dev.
14167c478bd9Sstevel@tonic-gate 	 * If this wasn't the last process to run,
14177c478bd9Sstevel@tonic-gate 	 * clear caches
14187c478bd9Sstevel@tonic-gate 	 */
14197c478bd9Sstevel@tonic-gate 	if (enter_dev_lock() != getpid()) {
14207c478bd9Sstevel@tonic-gate 		invalidate_enumerate_cache();
14217c478bd9Sstevel@tonic-gate 		rm_all_links_from_cache();
14227c478bd9Sstevel@tonic-gate 		(void) di_devlink_close(&devlink_cache, DI_LINK_ERROR);
1423f05faa4eSjacobs 
1424f05faa4eSjacobs 		/* send any sysevents that were queued up. */
1425f05faa4eSjacobs 		process_syseventq();
14267c478bd9Sstevel@tonic-gate 	}
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	/*
14297c478bd9Sstevel@tonic-gate 	 * (re)load the  reverse links database if not
14307c478bd9Sstevel@tonic-gate 	 * already cached.
14317c478bd9Sstevel@tonic-gate 	 */
14327c478bd9Sstevel@tonic-gate 	if (devlink_cache == NULL)
14337c478bd9Sstevel@tonic-gate 		devlink_cache = di_devlink_open(root_dir, 0);
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	/*
14367c478bd9Sstevel@tonic-gate 	 * If modules were unloaded, reload them.  Also use module status
14377c478bd9Sstevel@tonic-gate 	 * as an indication that we should check to see if other binding
14387c478bd9Sstevel@tonic-gate 	 * files need to be reloaded.
14397c478bd9Sstevel@tonic-gate 	 */
14407c478bd9Sstevel@tonic-gate 	if (module_head == NULL) {
14417c478bd9Sstevel@tonic-gate 		load_modules();
14427c478bd9Sstevel@tonic-gate 		read_minor_perm_file();
14437c478bd9Sstevel@tonic-gate 		read_driver_aliases_file();
14447c478bd9Sstevel@tonic-gate 		read_devlinktab_file();
14457c478bd9Sstevel@tonic-gate 		read_logindevperm_file();
14468d483882Smlf 		read_enumerate_file();
14477c478bd9Sstevel@tonic-gate 	}
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	if (module_head != NULL)
14507c478bd9Sstevel@tonic-gate 		return;
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 	if (strcmp(prog, DEVLINKS) == 0) {
14537c478bd9Sstevel@tonic-gate 		if (devlinktab_list == NULL) {
14547c478bd9Sstevel@tonic-gate 			err_print(NO_LINKTAB, devlinktab_file);
14557c478bd9Sstevel@tonic-gate 			err_print(NO_MODULES, module_dirs);
14567c478bd9Sstevel@tonic-gate 			err_print(ABORTING);
14577c478bd9Sstevel@tonic-gate 			devfsadm_exit(1);
1458537714daSvikram 			/*NOTREACHED*/
14597c478bd9Sstevel@tonic-gate 		}
14607c478bd9Sstevel@tonic-gate 	} else {
14617c478bd9Sstevel@tonic-gate 		err_print(NO_MODULES, module_dirs);
14627c478bd9Sstevel@tonic-gate 		if (strcmp(prog, DEVFSADM) == 0) {
14637c478bd9Sstevel@tonic-gate 			err_print(MODIFY_PATH);
14647c478bd9Sstevel@tonic-gate 		}
14657c478bd9Sstevel@tonic-gate 	}
14667c478bd9Sstevel@tonic-gate }
14677c478bd9Sstevel@tonic-gate 
1468ff2aee48Scth /*
1469ff2aee48Scth  * Unlock the device.  If we are processing a CACHE_STATE call, we signal a
1470ff2aee48Scth  * minor_fini_thread delayed SYNC_STATE at the end of the call.  If we are
1471ff2aee48Scth  * processing a SYNC_STATE call, we cancel any minor_fini_thread SYNC_STATE
1472ff2aee48Scth  * at both the start and end of the call since we will be doing the SYNC_STATE.
1473ff2aee48Scth  */
14747c478bd9Sstevel@tonic-gate static void
14757c478bd9Sstevel@tonic-gate unlock_dev(int flag)
14767c478bd9Sstevel@tonic-gate {
1477ff2aee48Scth 	assert(flag == SYNC_STATE || flag == CACHE_STATE);
1478ff2aee48Scth 
14797c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "unlock_dev(): entered\n");
14807c478bd9Sstevel@tonic-gate 
1481ff2aee48Scth 	/* If we are starting a SYNC_STATE, cancel minor_fini_thread SYNC */
1482ff2aee48Scth 	if (flag == SYNC_STATE) {
1483ff2aee48Scth 		(void) mutex_lock(&minor_fini_mutex);
1484ff2aee48Scth 		minor_fini_canceled = TRUE;
1485ff2aee48Scth 		minor_fini_delayed = FALSE;
1486ff2aee48Scth 		(void) mutex_unlock(&minor_fini_mutex);
1487ff2aee48Scth 	}
1488ff2aee48Scth 
14897c478bd9Sstevel@tonic-gate 	if (build_dev == FALSE)
14907c478bd9Sstevel@tonic-gate 		return;
14917c478bd9Sstevel@tonic-gate 
1492568e756aSvikram 	if (devlink_cache == NULL) {
1493568e756aSvikram 		err_print(NO_DEVLINK_CACHE);
1494568e756aSvikram 	}
14957c478bd9Sstevel@tonic-gate 	assert(devlink_cache);
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	if (flag == SYNC_STATE) {
14987c478bd9Sstevel@tonic-gate 		unload_modules();
14997c478bd9Sstevel@tonic-gate 		if (update_database)
15007c478bd9Sstevel@tonic-gate 			(void) di_devlink_update(devlink_cache);
15017c478bd9Sstevel@tonic-gate 		(void) di_devlink_close(&devlink_cache, 0);
1502f05faa4eSjacobs 
1503f05faa4eSjacobs 		/*
1504f05faa4eSjacobs 		 * now that the devlinks db cache has been flushed, it is safe
1505f05faa4eSjacobs 		 * to send any sysevents that were queued up.
1506f05faa4eSjacobs 		 */
1507f05faa4eSjacobs 		process_syseventq();
15087c478bd9Sstevel@tonic-gate 	}
15097c478bd9Sstevel@tonic-gate 
1510537714daSvikram 	exit_dev_lock(0);
15117c478bd9Sstevel@tonic-gate 
1512ff2aee48Scth 	(void) mutex_lock(&minor_fini_mutex);
1513ff2aee48Scth 	if (flag == SYNC_STATE) {
1514ff2aee48Scth 		/* We did a SYNC_STATE, cancel minor_fini_thread SYNC */
1515ff2aee48Scth 		minor_fini_canceled = TRUE;
1516ff2aee48Scth 		minor_fini_delayed = FALSE;
1517ff2aee48Scth 	} else {
1518ff2aee48Scth 		/* We did a CACHE_STATE, start delayed minor_fini_thread SYNC */
1519ff2aee48Scth 		minor_fini_canceled = FALSE;
1520ff2aee48Scth 		minor_fini_delayed = TRUE;
1521ff2aee48Scth 		(void) cond_signal(&minor_fini_cv);
1522ff2aee48Scth 	}
1523ff2aee48Scth 	(void) mutex_unlock(&minor_fini_mutex);
1524ff2aee48Scth 
15257c478bd9Sstevel@tonic-gate 	(void) sema_post(&dev_sema);
15267c478bd9Sstevel@tonic-gate }
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate /*
15297c478bd9Sstevel@tonic-gate  * Check that if -r is set, it is not any part of a zone--- that is, that
15307c478bd9Sstevel@tonic-gate  * the zonepath is not a substring of the root path.
15317c478bd9Sstevel@tonic-gate  */
15327c478bd9Sstevel@tonic-gate static int
15337c478bd9Sstevel@tonic-gate zone_pathcheck(char *checkpath)
15347c478bd9Sstevel@tonic-gate {
15357c478bd9Sstevel@tonic-gate 	void		*dlhdl = NULL;
15367c478bd9Sstevel@tonic-gate 	char		*name;
15377c478bd9Sstevel@tonic-gate 	char		root[MAXPATHLEN]; /* resolved devfsadm root path */
15387c478bd9Sstevel@tonic-gate 	char		zroot[MAXPATHLEN]; /* zone root path */
15397c478bd9Sstevel@tonic-gate 	char		rzroot[MAXPATHLEN]; /* resolved zone root path */
1540facf4a8dSllai 	char		tmp[MAXPATHLEN];
15417c478bd9Sstevel@tonic-gate 	FILE		*cookie;
15427c478bd9Sstevel@tonic-gate 	int		err = DEVFSADM_SUCCESS;
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	if (checkpath[0] == '\0')
15457c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	/*
15487c478bd9Sstevel@tonic-gate 	 * Check if zones is available on this system.
15497c478bd9Sstevel@tonic-gate 	 */
15507c478bd9Sstevel@tonic-gate 	if ((dlhdl = dlopen(LIBZONECFG_PATH, RTLD_LAZY)) == NULL) {
15517c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
15527c478bd9Sstevel@tonic-gate 	}
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate 	bzero(root, sizeof (root));
15557c478bd9Sstevel@tonic-gate 	if (resolvepath(checkpath, root, sizeof (root) - 1) == -1) {
15567c478bd9Sstevel@tonic-gate 		/*
1557facf4a8dSllai 		 * In this case the user has done "devfsadm -r" on some path
15587c478bd9Sstevel@tonic-gate 		 * which does not yet exist, or we got some other misc. error.
15597c478bd9Sstevel@tonic-gate 		 * We punt and don't resolve the path in this case.
15607c478bd9Sstevel@tonic-gate 		 */
15617c478bd9Sstevel@tonic-gate 		(void) strlcpy(root, checkpath, sizeof (root));
15627c478bd9Sstevel@tonic-gate 	}
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	if (strlen(root) > 0 && (root[strlen(root) - 1] != '/')) {
15657c478bd9Sstevel@tonic-gate 		(void) snprintf(tmp, sizeof (tmp), "%s/", root);
15667c478bd9Sstevel@tonic-gate 		(void) strlcpy(root, tmp, sizeof (root));
15677c478bd9Sstevel@tonic-gate 	}
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
15707c478bd9Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
15717c478bd9Sstevel@tonic-gate 		/* Skip the global zone */
15727c478bd9Sstevel@tonic-gate 		if (strcmp(name, GLOBAL_ZONENAME) == 0) {
15737c478bd9Sstevel@tonic-gate 			free(name);
15747c478bd9Sstevel@tonic-gate 			continue;
15757c478bd9Sstevel@tonic-gate 		}
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 		if (zone_get_zonepath(name, zroot, sizeof (zroot)) != Z_OK) {
15787c478bd9Sstevel@tonic-gate 			free(name);
15797c478bd9Sstevel@tonic-gate 			continue;
15807c478bd9Sstevel@tonic-gate 		}
15817c478bd9Sstevel@tonic-gate 
15827c478bd9Sstevel@tonic-gate 		bzero(rzroot, sizeof (rzroot));
15837c478bd9Sstevel@tonic-gate 		if (resolvepath(zroot, rzroot, sizeof (rzroot) - 1) == -1) {
15847c478bd9Sstevel@tonic-gate 			/*
15857c478bd9Sstevel@tonic-gate 			 * Zone path doesn't exist, or other misc error,
15867c478bd9Sstevel@tonic-gate 			 * so we try using the non-resolved pathname.
15877c478bd9Sstevel@tonic-gate 			 */
15887c478bd9Sstevel@tonic-gate 			(void) strlcpy(rzroot, zroot, sizeof (rzroot));
15897c478bd9Sstevel@tonic-gate 		}
15907c478bd9Sstevel@tonic-gate 		if (strlen(rzroot) > 0 && (rzroot[strlen(rzroot) - 1] != '/')) {
15917c478bd9Sstevel@tonic-gate 			(void) snprintf(tmp, sizeof (tmp), "%s/", rzroot);
15927c478bd9Sstevel@tonic-gate 			(void) strlcpy(rzroot, tmp, sizeof (rzroot));
15937c478bd9Sstevel@tonic-gate 		}
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 		/*
15967c478bd9Sstevel@tonic-gate 		 * Finally, the comparison.  If the zone root path is a
15977c478bd9Sstevel@tonic-gate 		 * leading substring of the root path, fail.
15987c478bd9Sstevel@tonic-gate 		 */
15997c478bd9Sstevel@tonic-gate 		if (strncmp(rzroot, root, strlen(rzroot)) == 0) {
16007c478bd9Sstevel@tonic-gate 			err_print(ZONE_PATHCHECK, root, name);
16017c478bd9Sstevel@tonic-gate 			err = DEVFSADM_FAILURE;
16027c478bd9Sstevel@tonic-gate 			free(name);
16037c478bd9Sstevel@tonic-gate 			break;
16047c478bd9Sstevel@tonic-gate 		}
16057c478bd9Sstevel@tonic-gate 		free(name);
16067c478bd9Sstevel@tonic-gate 	}
16077c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
16087c478bd9Sstevel@tonic-gate 	(void) dlclose(dlhdl);
16097c478bd9Sstevel@tonic-gate 	return (err);
16107c478bd9Sstevel@tonic-gate }
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate /*
16137c478bd9Sstevel@tonic-gate  *  Called by the daemon when it receives an event from the devfsadm SLM
16147c478bd9Sstevel@tonic-gate  *  to syseventd.
16157c478bd9Sstevel@tonic-gate  *
16167c478bd9Sstevel@tonic-gate  *  The devfsadm SLM uses a private event channel for communication to
16177c478bd9Sstevel@tonic-gate  *  devfsadmd set-up via private libsysevent interfaces.  This handler is
16187c478bd9Sstevel@tonic-gate  *  used to bind to the devfsadmd channel for event delivery.
16197c478bd9Sstevel@tonic-gate  *  The devfsadmd SLM insures single calls to this routine as well as
16207c478bd9Sstevel@tonic-gate  *  synchronized event delivery.
16217c478bd9Sstevel@tonic-gate  *
16227c478bd9Sstevel@tonic-gate  */
16237c478bd9Sstevel@tonic-gate static void
16247c478bd9Sstevel@tonic-gate event_handler(sysevent_t *ev)
16257c478bd9Sstevel@tonic-gate {
16267c478bd9Sstevel@tonic-gate 	char *path;
16277c478bd9Sstevel@tonic-gate 	char *minor;
16287c478bd9Sstevel@tonic-gate 	char *subclass;
16297c478bd9Sstevel@tonic-gate 	char *dev_ev_subclass;
16307c478bd9Sstevel@tonic-gate 	char *driver_name;
16317c478bd9Sstevel@tonic-gate 	nvlist_t *attr_list = NULL;
16327c478bd9Sstevel@tonic-gate 	int err = 0;
16337c478bd9Sstevel@tonic-gate 	int instance;
16347c478bd9Sstevel@tonic-gate 	int branch_event = 0;
16357c478bd9Sstevel@tonic-gate 
16367e3e5701SJan Parcel 	/*
16377e3e5701SJan Parcel 	 * If this is event-driven, then we cannot trust the static devlist
16387e3e5701SJan Parcel 	 * to be correct.
16397e3e5701SJan Parcel 	 */
16407e3e5701SJan Parcel 
16417e3e5701SJan Parcel 	event_driven = TRUE;
16427c478bd9Sstevel@tonic-gate 	subclass = sysevent_get_subclass_name(ev);
16437c478bd9Sstevel@tonic-gate 	vprint(EVENT_MID, "event_handler: %s id:0X%llx\n",
16447c478bd9Sstevel@tonic-gate 	    subclass, sysevent_get_seq(ev));
16457c478bd9Sstevel@tonic-gate 
1646facf4a8dSllai 	if (strcmp(subclass, ESC_DEVFS_START) == 0) {
1647facf4a8dSllai 		return;
1648facf4a8dSllai 	}
1649facf4a8dSllai 
16507c478bd9Sstevel@tonic-gate 	/* Check if event is an instance modification */
16517c478bd9Sstevel@tonic-gate 	if (strcmp(subclass, ESC_DEVFS_INSTANCE_MOD) == 0) {
16527c478bd9Sstevel@tonic-gate 		devfs_instance_mod();
16537c478bd9Sstevel@tonic-gate 		return;
16547c478bd9Sstevel@tonic-gate 	}
16557c478bd9Sstevel@tonic-gate 	if (sysevent_get_attr_list(ev, &attr_list) != 0) {
16567c478bd9Sstevel@tonic-gate 		vprint(EVENT_MID, "event_handler: can not get attr list\n");
16577c478bd9Sstevel@tonic-gate 		return;
16587c478bd9Sstevel@tonic-gate 	}
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 	if (strcmp(subclass, ESC_DEVFS_DEVI_ADD) == 0 ||
16617c478bd9Sstevel@tonic-gate 	    strcmp(subclass, ESC_DEVFS_DEVI_REMOVE) == 0 ||
16627c478bd9Sstevel@tonic-gate 	    strcmp(subclass, ESC_DEVFS_MINOR_CREATE) == 0 ||
16637c478bd9Sstevel@tonic-gate 	    strcmp(subclass, ESC_DEVFS_MINOR_REMOVE) == 0) {
16647c478bd9Sstevel@tonic-gate 		if ((err = nvlist_lookup_string(attr_list, DEVFS_PATHNAME,
16657c478bd9Sstevel@tonic-gate 		    &path)) != 0)
16667c478bd9Sstevel@tonic-gate 			goto out;
16677c478bd9Sstevel@tonic-gate 
166830294554Sphitran 		if (nvlist_lookup_string(attr_list, DEVFS_DEVI_CLASS,
166930294554Sphitran 		    &dev_ev_subclass) != 0)
167030294554Sphitran 			dev_ev_subclass = NULL;
16717c478bd9Sstevel@tonic-gate 
167230294554Sphitran 		if (nvlist_lookup_string(attr_list, DEVFS_DRIVER_NAME,
167330294554Sphitran 		    &driver_name) != 0)
167430294554Sphitran 			driver_name = NULL;
16757c478bd9Sstevel@tonic-gate 
167630294554Sphitran 		if (nvlist_lookup_int32(attr_list, DEVFS_INSTANCE,
167730294554Sphitran 		    &instance) != 0)
167830294554Sphitran 			instance = -1;
16797c478bd9Sstevel@tonic-gate 
168030294554Sphitran 		if (nvlist_lookup_int32(attr_list, DEVFS_BRANCH_EVENT,
168130294554Sphitran 		    &branch_event) != 0)
168230294554Sphitran 			branch_event = 0;
16837c478bd9Sstevel@tonic-gate 
168430294554Sphitran 		if (nvlist_lookup_string(attr_list, DEVFS_MINOR_NAME,
168530294554Sphitran 		    &minor) != 0)
168630294554Sphitran 			minor = NULL;
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 		lock_dev();
16897c478bd9Sstevel@tonic-gate 
16907c478bd9Sstevel@tonic-gate 		if (strcmp(ESC_DEVFS_DEVI_ADD, subclass) == 0) {
16917c478bd9Sstevel@tonic-gate 			add_minor_pathname(path, NULL, dev_ev_subclass);
16927c478bd9Sstevel@tonic-gate 			if (branch_event) {
1693f05faa4eSjacobs 				build_and_enq_event(EC_DEV_BRANCH,
169430294554Sphitran 				    ESC_DEV_BRANCH_ADD, path, DI_NODE_NIL,
169530294554Sphitran 				    NULL);
16967c478bd9Sstevel@tonic-gate 			}
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 		} else if (strcmp(ESC_DEVFS_MINOR_CREATE, subclass) == 0) {
169930294554Sphitran 			add_minor_pathname(path, minor, dev_ev_subclass);
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate 		} else if (strcmp(ESC_DEVFS_MINOR_REMOVE, subclass) == 0) {
170230294554Sphitran 			hot_cleanup(path, minor, dev_ev_subclass, driver_name,
170330294554Sphitran 			    instance);
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate 		} else { /* ESC_DEVFS_DEVI_REMOVE */
17067c478bd9Sstevel@tonic-gate 			hot_cleanup(path, NULL, dev_ev_subclass,
17077c478bd9Sstevel@tonic-gate 			    driver_name, instance);
17087c478bd9Sstevel@tonic-gate 			if (branch_event) {
1709f05faa4eSjacobs 				build_and_enq_event(EC_DEV_BRANCH,
171030294554Sphitran 				    ESC_DEV_BRANCH_REMOVE, path, DI_NODE_NIL,
171130294554Sphitran 				    NULL);
17127c478bd9Sstevel@tonic-gate 			}
17137c478bd9Sstevel@tonic-gate 		}
17147c478bd9Sstevel@tonic-gate 
17157c478bd9Sstevel@tonic-gate 		unlock_dev(CACHE_STATE);
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate 	} else if (strcmp(subclass, ESC_DEVFS_BRANCH_ADD) == 0 ||
17187c478bd9Sstevel@tonic-gate 	    strcmp(subclass, ESC_DEVFS_BRANCH_REMOVE) == 0) {
17197c478bd9Sstevel@tonic-gate 		if ((err = nvlist_lookup_string(attr_list,
17207c478bd9Sstevel@tonic-gate 		    DEVFS_PATHNAME, &path)) != 0)
17217c478bd9Sstevel@tonic-gate 			goto out;
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 		/* just log ESC_DEV_BRANCH... event */
17247c478bd9Sstevel@tonic-gate 		if (strcmp(subclass, ESC_DEVFS_BRANCH_ADD) == 0)
17257c478bd9Sstevel@tonic-gate 			dev_ev_subclass = ESC_DEV_BRANCH_ADD;
17267c478bd9Sstevel@tonic-gate 		else
17277c478bd9Sstevel@tonic-gate 			dev_ev_subclass = ESC_DEV_BRANCH_REMOVE;
17287c478bd9Sstevel@tonic-gate 
1729d2596142Scth 		lock_dev();
1730f05faa4eSjacobs 		build_and_enq_event(EC_DEV_BRANCH, dev_ev_subclass, path,
173130294554Sphitran 		    DI_NODE_NIL, NULL);
1732d2596142Scth 		unlock_dev(CACHE_STATE);
17337c478bd9Sstevel@tonic-gate 	} else
17347c478bd9Sstevel@tonic-gate 		err_print(UNKNOWN_EVENT, subclass);
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate out:
17377c478bd9Sstevel@tonic-gate 	if (err)
17387c478bd9Sstevel@tonic-gate 		err_print(EVENT_ATTR_LOOKUP_FAILED, strerror(err));
17397c478bd9Sstevel@tonic-gate 	nvlist_free(attr_list);
17407c478bd9Sstevel@tonic-gate }
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate static void
17437c478bd9Sstevel@tonic-gate dca_impl_init(char *root, char *minor, struct dca_impl *dcip)
17447c478bd9Sstevel@tonic-gate {
17457c478bd9Sstevel@tonic-gate 	assert(root);
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 	dcip->dci_root = root;
17487c478bd9Sstevel@tonic-gate 	dcip->dci_minor = minor;
17497c478bd9Sstevel@tonic-gate 	dcip->dci_driver = NULL;
17507c478bd9Sstevel@tonic-gate 	dcip->dci_error = 0;
17517c478bd9Sstevel@tonic-gate 	dcip->dci_flags = 0;
17527c478bd9Sstevel@tonic-gate 	dcip->dci_arg = NULL;
17537c478bd9Sstevel@tonic-gate }
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate /*
17567c478bd9Sstevel@tonic-gate  *  Kernel logs a message when a devinfo node is attached.  Try to create
17577c478bd9Sstevel@tonic-gate  *  /dev and /devices for each minor node.  minorname can be NULL.
17587c478bd9Sstevel@tonic-gate  */
17597c478bd9Sstevel@tonic-gate void
17607c478bd9Sstevel@tonic-gate add_minor_pathname(char *node, char *minor, char *ev_subclass)
17617c478bd9Sstevel@tonic-gate {
17627c478bd9Sstevel@tonic-gate 	struct dca_impl	dci;
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "add_minor_pathname: node_path=%s minor=%s\n",
17657c478bd9Sstevel@tonic-gate 	    node, minor ? minor : "NULL");
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 	dca_impl_init(node, minor, &dci);
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	/*
17707c478bd9Sstevel@tonic-gate 	 * Restrict hotplug link creation if daemon
17717c478bd9Sstevel@tonic-gate 	 * started  with -i option.
17727c478bd9Sstevel@tonic-gate 	 */
17737c478bd9Sstevel@tonic-gate 	if (single_drv == TRUE) {
17747c478bd9Sstevel@tonic-gate 		dci.dci_driver = driver;
17757c478bd9Sstevel@tonic-gate 	}
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 	/*
17780dc974a9SCathy Zhou 	 * We are being invoked in response to a hotplug event.
17797c478bd9Sstevel@tonic-gate 	 */
17807c478bd9Sstevel@tonic-gate 	dci.dci_flags = DCA_HOT_PLUG | DCA_CHECK_TYPE;
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	devi_tree_walk(&dci, DINFOPROP|DINFOMINOR, ev_subclass);
17837c478bd9Sstevel@tonic-gate }
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate static di_node_t
17867c478bd9Sstevel@tonic-gate find_clone_node()
17877c478bd9Sstevel@tonic-gate {
17887c478bd9Sstevel@tonic-gate 	static di_node_t clone_node = DI_NODE_NIL;
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate 	if (clone_node == DI_NODE_NIL)
17917c478bd9Sstevel@tonic-gate 		clone_node = di_init("/pseudo/clone@0", DINFOPROP);
17927c478bd9Sstevel@tonic-gate 	return (clone_node);
17937c478bd9Sstevel@tonic-gate }
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate static int
17967c478bd9Sstevel@tonic-gate is_descendent_of(di_node_t node, char *driver)
17977c478bd9Sstevel@tonic-gate {
17987c478bd9Sstevel@tonic-gate 	while (node != DI_NODE_NIL) {
17997c478bd9Sstevel@tonic-gate 		char *drv = di_driver_name(node);
18007c478bd9Sstevel@tonic-gate 		if (strcmp(drv, driver) == 0)
18017c478bd9Sstevel@tonic-gate 			return (1);
18027c478bd9Sstevel@tonic-gate 		node = di_parent_node(node);
18037c478bd9Sstevel@tonic-gate 	}
18047c478bd9Sstevel@tonic-gate 	return (0);
18057c478bd9Sstevel@tonic-gate }
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate /*
18087c478bd9Sstevel@tonic-gate  * Checks the minor type.  If it is an alias node, then lookup
18097c478bd9Sstevel@tonic-gate  * the real node/minor first, then call minor_process() to
18107c478bd9Sstevel@tonic-gate  * do the real work.
18117c478bd9Sstevel@tonic-gate  */
18127c478bd9Sstevel@tonic-gate static int
18137c478bd9Sstevel@tonic-gate check_minor_type(di_node_t node, di_minor_t minor, void *arg)
18147c478bd9Sstevel@tonic-gate {
18157c478bd9Sstevel@tonic-gate 	ddi_minor_type	minor_type;
18167c478bd9Sstevel@tonic-gate 	di_node_t	clone_node;
18177c478bd9Sstevel@tonic-gate 	char		*mn;
18187c478bd9Sstevel@tonic-gate 	char		*nt;
18197c478bd9Sstevel@tonic-gate 	struct mlist	*dep;
18207c478bd9Sstevel@tonic-gate 	struct dca_impl	*dcip = arg;
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	assert(dcip);
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 	dep = dcip->dci_arg;
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 	mn = di_minor_name(minor);
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate 	/*
18297c478bd9Sstevel@tonic-gate 	 * We match driver here instead of in minor_process
18307c478bd9Sstevel@tonic-gate 	 * as we want the actual driver name. This check is
18317c478bd9Sstevel@tonic-gate 	 * unnecessary during deferred processing.
18327c478bd9Sstevel@tonic-gate 	 */
18337c478bd9Sstevel@tonic-gate 	if (dep &&
18347c478bd9Sstevel@tonic-gate 	    ((dcip->dci_driver && !is_descendent_of(node, dcip->dci_driver)) ||
18357c478bd9Sstevel@tonic-gate 	    (dcip->dci_minor && strcmp(mn, dcip->dci_minor)))) {
18367c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
18377c478bd9Sstevel@tonic-gate 	}
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate 	if ((dcip->dci_flags & DCA_CHECK_TYPE) &&
18407c478bd9Sstevel@tonic-gate 	    (nt = di_minor_nodetype(minor)) &&
1841210db224Sericheng 	    (strcmp(nt, DDI_NT_NET) == 0)) {
18427c478bd9Sstevel@tonic-gate 		dcip->dci_flags &= ~DCA_CHECK_TYPE;
18437c478bd9Sstevel@tonic-gate 	}
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 	minor_type = di_minor_type(minor);
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate 	if (minor_type == DDM_MINOR) {
18487c478bd9Sstevel@tonic-gate 		minor_process(node, minor, dep);
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	} else if (minor_type == DDM_ALIAS) {
18517c478bd9Sstevel@tonic-gate 		struct mlist *cdep, clone_del = {0};
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate 		clone_node = find_clone_node();
18547c478bd9Sstevel@tonic-gate 		if (clone_node == DI_NODE_NIL) {
18557c478bd9Sstevel@tonic-gate 			err_print(DI_INIT_FAILED, "clone", strerror(errno));
18567c478bd9Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
18577c478bd9Sstevel@tonic-gate 		}
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 		cdep = dep ? &clone_del : NULL;
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 		minor_process(clone_node, minor, cdep);
18627c478bd9Sstevel@tonic-gate 
18637c478bd9Sstevel@tonic-gate 		/*
18647c478bd9Sstevel@tonic-gate 		 * cache "alias" minor node and free "clone" minor
18657c478bd9Sstevel@tonic-gate 		 */
18667c478bd9Sstevel@tonic-gate 		if (cdep != NULL && cdep->head != NULL) {
18677c478bd9Sstevel@tonic-gate 			assert(cdep->tail != NULL);
18687c478bd9Sstevel@tonic-gate 			cache_deferred_minor(dep, node, minor);
18697c478bd9Sstevel@tonic-gate 			dcip->dci_arg = cdep;
18707c478bd9Sstevel@tonic-gate 			process_deferred_links(dcip, DCA_FREE_LIST);
18717c478bd9Sstevel@tonic-gate 			dcip->dci_arg = dep;
18727c478bd9Sstevel@tonic-gate 		}
18737c478bd9Sstevel@tonic-gate 	}
18747c478bd9Sstevel@tonic-gate 
18757c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
18767c478bd9Sstevel@tonic-gate }
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate /*
18807c478bd9Sstevel@tonic-gate  *  This is the entry point for each minor node, whether walking
18817c478bd9Sstevel@tonic-gate  *  the entire tree via di_walk_minor() or processing a hotplug event
18827c478bd9Sstevel@tonic-gate  *  for a single devinfo node (via hotplug ndi_devi_online()).
18837c478bd9Sstevel@tonic-gate  */
18847c478bd9Sstevel@tonic-gate /*ARGSUSED*/
18857c478bd9Sstevel@tonic-gate static void
18867c478bd9Sstevel@tonic-gate minor_process(di_node_t node, di_minor_t minor, struct mlist *dep)
18877c478bd9Sstevel@tonic-gate {
18887c478bd9Sstevel@tonic-gate 	create_list_t	*create;
18897c478bd9Sstevel@tonic-gate 	int		defer;
18907c478bd9Sstevel@tonic-gate 
18917c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "minor_process: node=%s, minor=%s\n",
18920a653502Swroche 	    di_node_name(node), di_minor_name(minor));
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate 	if (dep != NULL) {
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 		/*
18977c478bd9Sstevel@tonic-gate 		 * Reset /devices node to minor_perm perm/ownership
18987c478bd9Sstevel@tonic-gate 		 * if we are here to deactivate device allocation
18997c478bd9Sstevel@tonic-gate 		 */
19007c478bd9Sstevel@tonic-gate 		if (build_devices == TRUE) {
19017c478bd9Sstevel@tonic-gate 			reset_node_permissions(node, minor);
19027c478bd9Sstevel@tonic-gate 		}
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 		if (build_dev == FALSE) {
19057c478bd9Sstevel@tonic-gate 			return;
19067c478bd9Sstevel@tonic-gate 		}
19077c478bd9Sstevel@tonic-gate 
19087c478bd9Sstevel@tonic-gate 		/*
19097c478bd9Sstevel@tonic-gate 		 * This function will create any nodes for /etc/devlink.tab.
19107c478bd9Sstevel@tonic-gate 		 * If devlink.tab handles link creation, we don't call any
19117c478bd9Sstevel@tonic-gate 		 * devfsadm modules since that could cause duplicate caching
19127c478bd9Sstevel@tonic-gate 		 * in the enumerate functions if different re strings are
19137c478bd9Sstevel@tonic-gate 		 * passed that are logically identical.  I'm still not
19147c478bd9Sstevel@tonic-gate 		 * convinced this would cause any harm, but better to be safe.
19157c478bd9Sstevel@tonic-gate 		 *
19167c478bd9Sstevel@tonic-gate 		 * Deferred processing is available only for devlinks
19177c478bd9Sstevel@tonic-gate 		 * created through devfsadm modules.
19187c478bd9Sstevel@tonic-gate 		 */
19197c478bd9Sstevel@tonic-gate 		if (process_devlink_compat(minor, node) == TRUE) {
19207c478bd9Sstevel@tonic-gate 			return;
19217c478bd9Sstevel@tonic-gate 		}
19227c478bd9Sstevel@tonic-gate 	} else {
19237c478bd9Sstevel@tonic-gate 		vprint(CHATTY_MID, "minor_process: deferred processing\n");
19247c478bd9Sstevel@tonic-gate 	}
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	/*
19277c478bd9Sstevel@tonic-gate 	 * look for relevant link create rules in the modules, and
19287c478bd9Sstevel@tonic-gate 	 * invoke the link create callback function to build a link
19297c478bd9Sstevel@tonic-gate 	 * if there is a match.
19307c478bd9Sstevel@tonic-gate 	 */
19317c478bd9Sstevel@tonic-gate 	defer = 0;
19327c478bd9Sstevel@tonic-gate 	for (create = create_head; create != NULL; create = create->next) {
19337c478bd9Sstevel@tonic-gate 		if ((minor_matches_rule(node, minor, create) == TRUE) &&
19347c478bd9Sstevel@tonic-gate 		    class_ok(create->create->device_class) ==
19357c478bd9Sstevel@tonic-gate 		    DEVFSADM_SUCCESS) {
19367c478bd9Sstevel@tonic-gate 			if (call_minor_init(create->modptr) ==
19377c478bd9Sstevel@tonic-gate 			    DEVFSADM_FAILURE) {
19387c478bd9Sstevel@tonic-gate 				continue;
19397c478bd9Sstevel@tonic-gate 			}
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 			/*
19427c478bd9Sstevel@tonic-gate 			 * If NOT doing the deferred creates (i.e. 1st pass) and
19437c478bd9Sstevel@tonic-gate 			 * rule requests deferred processing cache the minor
19447c478bd9Sstevel@tonic-gate 			 * data.
19457c478bd9Sstevel@tonic-gate 			 *
19467c478bd9Sstevel@tonic-gate 			 * If deferred processing (2nd pass), create links
19477c478bd9Sstevel@tonic-gate 			 * ONLY if rule requests deferred processing.
19487c478bd9Sstevel@tonic-gate 			 */
19497c478bd9Sstevel@tonic-gate 			if (dep && ((create->create->flags & CREATE_MASK) ==
19507c478bd9Sstevel@tonic-gate 			    CREATE_DEFER)) {
19517c478bd9Sstevel@tonic-gate 				defer = 1;
19527c478bd9Sstevel@tonic-gate 				continue;
19537c478bd9Sstevel@tonic-gate 			} else if (dep == NULL &&
19547c478bd9Sstevel@tonic-gate 			    ((create->create->flags & CREATE_MASK) !=
19557c478bd9Sstevel@tonic-gate 			    CREATE_DEFER)) {
19567c478bd9Sstevel@tonic-gate 				continue;
19577c478bd9Sstevel@tonic-gate 			}
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 			if ((*(create->create->callback_fcn))
19607c478bd9Sstevel@tonic-gate 			    (minor, node) == DEVFSADM_TERMINATE) {
19617c478bd9Sstevel@tonic-gate 				break;
19627c478bd9Sstevel@tonic-gate 			}
19637c478bd9Sstevel@tonic-gate 		}
19647c478bd9Sstevel@tonic-gate 	}
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 	if (defer)
19677c478bd9Sstevel@tonic-gate 		cache_deferred_minor(dep, node, minor);
19687c478bd9Sstevel@tonic-gate }
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate /*
19727c478bd9Sstevel@tonic-gate  * Cache node and minor in defer list.
19737c478bd9Sstevel@tonic-gate  */
19747c478bd9Sstevel@tonic-gate static void
19757c478bd9Sstevel@tonic-gate cache_deferred_minor(
19767c478bd9Sstevel@tonic-gate 	struct mlist *dep,
19777c478bd9Sstevel@tonic-gate 	di_node_t node,
19787c478bd9Sstevel@tonic-gate 	di_minor_t minor)
19797c478bd9Sstevel@tonic-gate {
19807c478bd9Sstevel@tonic-gate 	struct minor	*mp;
19817c478bd9Sstevel@tonic-gate 	const char	*fcn = "cache_deferred_minor";
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "%s node=%s, minor=%s\n", fcn,
19847c478bd9Sstevel@tonic-gate 	    di_node_name(node), di_minor_name(minor));
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate 	if (dep == NULL) {
19877c478bd9Sstevel@tonic-gate 		vprint(CHATTY_MID, "%s: cannot cache during "
19887c478bd9Sstevel@tonic-gate 		    "deferred processing. Ignoring minor\n", fcn);
19897c478bd9Sstevel@tonic-gate 		return;
19907c478bd9Sstevel@tonic-gate 	}
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 	mp = (struct minor *)s_zalloc(sizeof (struct minor));
19937c478bd9Sstevel@tonic-gate 	mp->node = node;
19947c478bd9Sstevel@tonic-gate 	mp->minor = minor;
19957c478bd9Sstevel@tonic-gate 	mp->next = NULL;
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 	assert(dep->head == NULL || dep->tail != NULL);
19987c478bd9Sstevel@tonic-gate 	if (dep->head == NULL) {
19997c478bd9Sstevel@tonic-gate 		dep->head = mp;
20007c478bd9Sstevel@tonic-gate 	} else {
20017c478bd9Sstevel@tonic-gate 		dep->tail->next = mp;
20027c478bd9Sstevel@tonic-gate 	}
20037c478bd9Sstevel@tonic-gate 	dep->tail = mp;
20047c478bd9Sstevel@tonic-gate }
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate /*
20077c478bd9Sstevel@tonic-gate  *  Check to see if "create" link creation rule matches this node/minor.
20087c478bd9Sstevel@tonic-gate  *  If it does, return TRUE.
20097c478bd9Sstevel@tonic-gate  */
20107c478bd9Sstevel@tonic-gate static int
20117c478bd9Sstevel@tonic-gate minor_matches_rule(di_node_t node, di_minor_t minor, create_list_t *create)
20127c478bd9Sstevel@tonic-gate {
20137c478bd9Sstevel@tonic-gate 	char *m_nodetype, *m_drvname;
20147c478bd9Sstevel@tonic-gate 
20157c478bd9Sstevel@tonic-gate 	if (create->create->node_type != NULL) {
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 		m_nodetype = di_minor_nodetype(minor);
20187c478bd9Sstevel@tonic-gate 		assert(m_nodetype != NULL);
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate 		switch (create->create->flags & TYPE_MASK) {
20217c478bd9Sstevel@tonic-gate 		case TYPE_EXACT:
20227c478bd9Sstevel@tonic-gate 			if (strcmp(create->create->node_type, m_nodetype) !=
20237c478bd9Sstevel@tonic-gate 			    0) {
20247c478bd9Sstevel@tonic-gate 				return (FALSE);
20257c478bd9Sstevel@tonic-gate 			}
20267c478bd9Sstevel@tonic-gate 			break;
20277c478bd9Sstevel@tonic-gate 		case TYPE_PARTIAL:
20287c478bd9Sstevel@tonic-gate 			if (strncmp(create->create->node_type, m_nodetype,
20297c478bd9Sstevel@tonic-gate 			    strlen(create->create->node_type)) != 0) {
20307c478bd9Sstevel@tonic-gate 				return (FALSE);
20317c478bd9Sstevel@tonic-gate 			}
20327c478bd9Sstevel@tonic-gate 			break;
20337c478bd9Sstevel@tonic-gate 		case TYPE_RE:
20347c478bd9Sstevel@tonic-gate 			if (regexec(&(create->node_type_comp), m_nodetype,
20357c478bd9Sstevel@tonic-gate 			    0, NULL, 0) != 0) {
20367c478bd9Sstevel@tonic-gate 				return (FALSE);
20377c478bd9Sstevel@tonic-gate 			}
20387c478bd9Sstevel@tonic-gate 			break;
20397c478bd9Sstevel@tonic-gate 		}
20407c478bd9Sstevel@tonic-gate 	}
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	if (create->create->drv_name != NULL) {
20437c478bd9Sstevel@tonic-gate 		m_drvname = di_driver_name(node);
20447c478bd9Sstevel@tonic-gate 		switch (create->create->flags & DRV_MASK) {
20457c478bd9Sstevel@tonic-gate 		case DRV_EXACT:
20467c478bd9Sstevel@tonic-gate 			if (strcmp(create->create->drv_name, m_drvname) != 0) {
20477c478bd9Sstevel@tonic-gate 				return (FALSE);
20487c478bd9Sstevel@tonic-gate 			}
20497c478bd9Sstevel@tonic-gate 			break;
20507c478bd9Sstevel@tonic-gate 		case DRV_RE:
20517c478bd9Sstevel@tonic-gate 			if (regexec(&(create->drv_name_comp), m_drvname,
20527c478bd9Sstevel@tonic-gate 			    0, NULL, 0) != 0) {
20537c478bd9Sstevel@tonic-gate 				return (FALSE);
20547c478bd9Sstevel@tonic-gate 			}
20557c478bd9Sstevel@tonic-gate 			break;
20567c478bd9Sstevel@tonic-gate 		}
20577c478bd9Sstevel@tonic-gate 	}
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate 	return (TRUE);
20607c478bd9Sstevel@tonic-gate }
20617c478bd9Sstevel@tonic-gate 
20627c478bd9Sstevel@tonic-gate /*
20637c478bd9Sstevel@tonic-gate  * If no classes were given on the command line, then return DEVFSADM_SUCCESS.
20647c478bd9Sstevel@tonic-gate  * Otherwise, return DEVFSADM_SUCCESS if the device "class" from the module
20657c478bd9Sstevel@tonic-gate  * matches one of the device classes given on the command line,
20667c478bd9Sstevel@tonic-gate  * otherwise, return DEVFSADM_FAILURE.
20677c478bd9Sstevel@tonic-gate  */
20687c478bd9Sstevel@tonic-gate static int
20697c478bd9Sstevel@tonic-gate class_ok(char *class)
20707c478bd9Sstevel@tonic-gate {
20717c478bd9Sstevel@tonic-gate 	int i;
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate 	if (num_classes == 0) {
20747c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
20757c478bd9Sstevel@tonic-gate 	}
20767c478bd9Sstevel@tonic-gate 
2077*f2dbfd32SRobert Mustacchi 	/*
2078*f2dbfd32SRobert Mustacchi 	 * Some create tabs operate on multiple classes of devices because the
2079*f2dbfd32SRobert Mustacchi 	 * kernel doesn't have a good way for a driver to indicate that a
2080*f2dbfd32SRobert Mustacchi 	 * particular minor's class is different from that of the dev_info_t
2081*f2dbfd32SRobert Mustacchi 	 * it belongs to. As such, we'll always fail to match those here.
2082*f2dbfd32SRobert Mustacchi 	 */
2083*f2dbfd32SRobert Mustacchi 	if (class == NULL) {
2084*f2dbfd32SRobert Mustacchi 		return (DEVFSADM_FAILURE);
2085*f2dbfd32SRobert Mustacchi 	}
2086*f2dbfd32SRobert Mustacchi 
20877c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_classes; i++) {
20887c478bd9Sstevel@tonic-gate 		if (strcmp(class, classes[i]) == 0) {
20897c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
20907c478bd9Sstevel@tonic-gate 		}
20917c478bd9Sstevel@tonic-gate 	}
20927c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
20937c478bd9Sstevel@tonic-gate }
20947c478bd9Sstevel@tonic-gate 
20957c478bd9Sstevel@tonic-gate /*
20967c478bd9Sstevel@tonic-gate  * call minor_fini on active modules, then unload ALL modules
20977c478bd9Sstevel@tonic-gate  */
20987c478bd9Sstevel@tonic-gate static void
20997c478bd9Sstevel@tonic-gate unload_modules(void)
21007c478bd9Sstevel@tonic-gate {
21017c478bd9Sstevel@tonic-gate 	module_t *module_free;
21027c478bd9Sstevel@tonic-gate 	create_list_t *create_free;
21037c478bd9Sstevel@tonic-gate 	remove_list_t *remove_free;
21047c478bd9Sstevel@tonic-gate 
21057c478bd9Sstevel@tonic-gate 	while (create_head != NULL) {
21067c478bd9Sstevel@tonic-gate 		create_free = create_head;
21077c478bd9Sstevel@tonic-gate 		create_head = create_head->next;
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 		if ((create_free->create->flags & TYPE_RE) == TYPE_RE) {
21107c478bd9Sstevel@tonic-gate 			regfree(&(create_free->node_type_comp));
21117c478bd9Sstevel@tonic-gate 		}
21127c478bd9Sstevel@tonic-gate 		if ((create_free->create->flags & DRV_RE) == DRV_RE) {
21137c478bd9Sstevel@tonic-gate 			regfree(&(create_free->drv_name_comp));
21147c478bd9Sstevel@tonic-gate 		}
21157c478bd9Sstevel@tonic-gate 		free(create_free);
21167c478bd9Sstevel@tonic-gate 	}
21177c478bd9Sstevel@tonic-gate 
21187c478bd9Sstevel@tonic-gate 	while (remove_head != NULL) {
21197c478bd9Sstevel@tonic-gate 		remove_free = remove_head;
21207c478bd9Sstevel@tonic-gate 		remove_head = remove_head->next;
21217c478bd9Sstevel@tonic-gate 		free(remove_free);
21227c478bd9Sstevel@tonic-gate 	}
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 	while (module_head != NULL) {
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 		if ((module_head->minor_fini != NULL) &&
21277c478bd9Sstevel@tonic-gate 		    ((module_head->flags & MODULE_ACTIVE) == MODULE_ACTIVE)) {
21287c478bd9Sstevel@tonic-gate 			(void) (*(module_head->minor_fini))();
21297c478bd9Sstevel@tonic-gate 		}
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 		vprint(MODLOAD_MID, "unloading module %s\n", module_head->name);
21327c478bd9Sstevel@tonic-gate 		free(module_head->name);
21337c478bd9Sstevel@tonic-gate 		(void) dlclose(module_head->dlhandle);
21347c478bd9Sstevel@tonic-gate 
21357c478bd9Sstevel@tonic-gate 		module_free = module_head;
21367c478bd9Sstevel@tonic-gate 		module_head = module_head->next;
21377c478bd9Sstevel@tonic-gate 		free(module_free);
21387c478bd9Sstevel@tonic-gate 	}
21397c478bd9Sstevel@tonic-gate }
21407c478bd9Sstevel@tonic-gate 
21417c478bd9Sstevel@tonic-gate /*
21427c478bd9Sstevel@tonic-gate  * Load devfsadm logical link processing modules.
21437c478bd9Sstevel@tonic-gate  */
21447c478bd9Sstevel@tonic-gate static void
21457c478bd9Sstevel@tonic-gate load_modules(void)
21467c478bd9Sstevel@tonic-gate {
21477c478bd9Sstevel@tonic-gate 	DIR *mod_dir;
21484bc0a2efScasper 	struct dirent *entp;
21497c478bd9Sstevel@tonic-gate 	char cdir[PATH_MAX + 1];
21507c478bd9Sstevel@tonic-gate 	char *last;
21517c478bd9Sstevel@tonic-gate 	char *mdir = module_dirs;
21527c478bd9Sstevel@tonic-gate 	char *fcn = "load_modules: ";
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate 	while (*mdir != '\0') {
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 		while (*mdir == ':') {
21577c478bd9Sstevel@tonic-gate 			mdir++;
21587c478bd9Sstevel@tonic-gate 		}
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate 		if (*mdir == '\0') {
21617c478bd9Sstevel@tonic-gate 			continue;
21627c478bd9Sstevel@tonic-gate 		}
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate 		last = strchr(mdir, ':');
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 		if (last == NULL) {
21677c478bd9Sstevel@tonic-gate 			last = mdir + strlen(mdir);
21687c478bd9Sstevel@tonic-gate 		}
21697c478bd9Sstevel@tonic-gate 
21707c478bd9Sstevel@tonic-gate 		(void) strncpy(cdir, mdir, last - mdir);
21717c478bd9Sstevel@tonic-gate 		cdir[last - mdir] = '\0';
21727c478bd9Sstevel@tonic-gate 		mdir += strlen(cdir);
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 		if ((mod_dir = opendir(cdir)) == NULL) {
21757c478bd9Sstevel@tonic-gate 			vprint(MODLOAD_MID, "%sopendir(%s): %s\n",
21760a653502Swroche 			    fcn, cdir, strerror(errno));
21777c478bd9Sstevel@tonic-gate 			continue;
21787c478bd9Sstevel@tonic-gate 		}
21797c478bd9Sstevel@tonic-gate 
21804bc0a2efScasper 		while ((entp = readdir(mod_dir)) != NULL) {
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 			if ((strcmp(entp->d_name, ".") == 0) ||
21837c478bd9Sstevel@tonic-gate 			    (strcmp(entp->d_name, "..") == 0)) {
21847c478bd9Sstevel@tonic-gate 				continue;
21857c478bd9Sstevel@tonic-gate 			}
21867c478bd9Sstevel@tonic-gate 
21877c478bd9Sstevel@tonic-gate 			load_module(entp->d_name, cdir);
21887c478bd9Sstevel@tonic-gate 		}
21897c478bd9Sstevel@tonic-gate 		s_closedir(mod_dir);
21907c478bd9Sstevel@tonic-gate 	}
21917c478bd9Sstevel@tonic-gate }
21927c478bd9Sstevel@tonic-gate 
21937c478bd9Sstevel@tonic-gate static void
21947c478bd9Sstevel@tonic-gate load_module(char *mname, char *cdir)
21957c478bd9Sstevel@tonic-gate {
21967c478bd9Sstevel@tonic-gate 	_devfsadm_create_reg_t *create_reg;
2197aa646b9dSvikram 	_devfsadm_remove_reg_V1_t *remove_reg;
21987c478bd9Sstevel@tonic-gate 	create_list_t *create_list_element;
21997c478bd9Sstevel@tonic-gate 	create_list_t **create_list_next;
22007c478bd9Sstevel@tonic-gate 	remove_list_t *remove_list_element;
22017c478bd9Sstevel@tonic-gate 	remove_list_t **remove_list_next;
22027c478bd9Sstevel@tonic-gate 	char epath[PATH_MAX + 1], *end;
22037c478bd9Sstevel@tonic-gate 	char *fcn = "load_module: ";
22047c478bd9Sstevel@tonic-gate 	char *dlerrstr;
22057c478bd9Sstevel@tonic-gate 	void *dlhandle;
22067c478bd9Sstevel@tonic-gate 	module_t *module;
2207aa646b9dSvikram 	int flags;
22087c478bd9Sstevel@tonic-gate 	int n;
22097c478bd9Sstevel@tonic-gate 	int i;
22107c478bd9Sstevel@tonic-gate 
22117c478bd9Sstevel@tonic-gate 	/* ignore any file which does not end in '.so' */
22127c478bd9Sstevel@tonic-gate 	if ((end = strstr(mname, MODULE_SUFFIX)) != NULL) {
22137c478bd9Sstevel@tonic-gate 		if (end[strlen(MODULE_SUFFIX)] != '\0') {
22147c478bd9Sstevel@tonic-gate 			return;
22157c478bd9Sstevel@tonic-gate 		}
22167c478bd9Sstevel@tonic-gate 	} else {
22177c478bd9Sstevel@tonic-gate 		return;
22187c478bd9Sstevel@tonic-gate 	}
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	(void) snprintf(epath, sizeof (epath), "%s/%s", cdir, mname);
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 	if ((dlhandle = dlopen(epath, RTLD_LAZY)) == NULL) {
22237c478bd9Sstevel@tonic-gate 		dlerrstr = dlerror();
22247c478bd9Sstevel@tonic-gate 		err_print(DLOPEN_FAILED, epath,
22250a653502Swroche 		    dlerrstr ? dlerrstr : "unknown error");
22267c478bd9Sstevel@tonic-gate 		return;
22277c478bd9Sstevel@tonic-gate 	}
22287c478bd9Sstevel@tonic-gate 
22297c478bd9Sstevel@tonic-gate 	/* dlsym the _devfsadm_create_reg structure */
22307c478bd9Sstevel@tonic-gate 	if (NULL == (create_reg = (_devfsadm_create_reg_t *)
22310a653502Swroche 	    dlsym(dlhandle, _DEVFSADM_CREATE_REG))) {
22327c478bd9Sstevel@tonic-gate 		vprint(MODLOAD_MID, "dlsym(%s, %s): symbol not found\n", epath,
22330a653502Swroche 		    _DEVFSADM_CREATE_REG);
22347c478bd9Sstevel@tonic-gate 	} else {
22357c478bd9Sstevel@tonic-gate 		vprint(MODLOAD_MID, "%sdlsym(%s, %s) succeeded\n",
22360a653502Swroche 		    fcn, epath, _DEVFSADM_CREATE_REG);
22377c478bd9Sstevel@tonic-gate 	}
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate 	/* dlsym the _devfsadm_remove_reg structure */
2240aa646b9dSvikram 	if (NULL == (remove_reg = (_devfsadm_remove_reg_V1_t *)
22417c478bd9Sstevel@tonic-gate 	    dlsym(dlhandle, _DEVFSADM_REMOVE_REG))) {
22427c478bd9Sstevel@tonic-gate 		vprint(MODLOAD_MID, "dlsym(%s,\n\t%s): symbol not found\n",
22430a653502Swroche 		    epath, _DEVFSADM_REMOVE_REG);
22447c478bd9Sstevel@tonic-gate 	} else {
22457c478bd9Sstevel@tonic-gate 		vprint(MODLOAD_MID, "dlsym(%s, %s): succeeded\n",
22460a653502Swroche 		    epath, _DEVFSADM_REMOVE_REG);
22477c478bd9Sstevel@tonic-gate 	}
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 	vprint(MODLOAD_MID, "module %s loaded\n", epath);
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 	module = (module_t *)s_malloc(sizeof (module_t));
22527c478bd9Sstevel@tonic-gate 	module->name = s_strdup(epath);
22537c478bd9Sstevel@tonic-gate 	module->dlhandle = dlhandle;
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate 	/* dlsym other module functions, to be called later */
22567c478bd9Sstevel@tonic-gate 	module->minor_fini = (int (*)())dlsym(dlhandle, MINOR_FINI);
22577c478bd9Sstevel@tonic-gate 	module->minor_init = (int (*)())dlsym(dlhandle, MINOR_INIT);
22587c478bd9Sstevel@tonic-gate 	module->flags = 0;
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 	/*
22617c478bd9Sstevel@tonic-gate 	 *  put a ptr to each struct devfsadm_create on "create_head"
22627c478bd9Sstevel@tonic-gate 	 *  list sorted in interpose_lvl.
22637c478bd9Sstevel@tonic-gate 	 */
22647c478bd9Sstevel@tonic-gate 	if (create_reg != NULL) {
22657c478bd9Sstevel@tonic-gate 		for (i = 0; i < create_reg->count; i++) {
22667c478bd9Sstevel@tonic-gate 			int flags = create_reg->tblp[i].flags;
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 			create_list_element = (create_list_t *)
22690a653502Swroche 			    s_malloc(sizeof (create_list_t));
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate 			create_list_element->create = &(create_reg->tblp[i]);
22727c478bd9Sstevel@tonic-gate 			create_list_element->modptr = module;
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate 			if (((flags & CREATE_MASK) != 0) &&
22757c478bd9Sstevel@tonic-gate 			    ((flags & CREATE_MASK) != CREATE_DEFER)) {
22767c478bd9Sstevel@tonic-gate 				free(create_list_element);
22777c478bd9Sstevel@tonic-gate 				err_print("illegal flag combination in "
22780a653502Swroche 				    "module create\n");
22797c478bd9Sstevel@tonic-gate 				err_print(IGNORING_ENTRY, i, epath);
22807c478bd9Sstevel@tonic-gate 				continue;
22817c478bd9Sstevel@tonic-gate 			}
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 			if (((flags & TYPE_MASK) == 0) ^
22847c478bd9Sstevel@tonic-gate 			    (create_reg->tblp[i].node_type == NULL)) {
22857c478bd9Sstevel@tonic-gate 				free(create_list_element);
22867c478bd9Sstevel@tonic-gate 				err_print("flags value incompatible with "
22870a653502Swroche 				    "node_type value in module create\n");
22887c478bd9Sstevel@tonic-gate 				err_print(IGNORING_ENTRY, i, epath);
22897c478bd9Sstevel@tonic-gate 				continue;
22907c478bd9Sstevel@tonic-gate 			}
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate 			if (((flags & TYPE_MASK) != 0) &&
22937c478bd9Sstevel@tonic-gate 			    ((flags & TYPE_MASK) != TYPE_EXACT) &&
22947c478bd9Sstevel@tonic-gate 			    ((flags & TYPE_MASK) != TYPE_RE) &&
22957c478bd9Sstevel@tonic-gate 			    ((flags & TYPE_MASK) != TYPE_PARTIAL)) {
22967c478bd9Sstevel@tonic-gate 				free(create_list_element);
22977c478bd9Sstevel@tonic-gate 				err_print("illegal TYPE_* flag combination in "
22980a653502Swroche 				    "module create\n");
22997c478bd9Sstevel@tonic-gate 				err_print(IGNORING_ENTRY, i, epath);
23007c478bd9Sstevel@tonic-gate 				continue;
23017c478bd9Sstevel@tonic-gate 			}
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate 			/* precompile regular expression for efficiency */
23047c478bd9Sstevel@tonic-gate 			if ((flags & TYPE_RE) == TYPE_RE) {
23057c478bd9Sstevel@tonic-gate 				if ((n = regcomp(&(create_list_element->
23067c478bd9Sstevel@tonic-gate 				    node_type_comp),
23077c478bd9Sstevel@tonic-gate 				    create_reg->tblp[i].node_type,
23087c478bd9Sstevel@tonic-gate 				    REG_EXTENDED)) != 0) {
23097c478bd9Sstevel@tonic-gate 					free(create_list_element);
23107c478bd9Sstevel@tonic-gate 					err_print(REGCOMP_FAILED,
23110a653502Swroche 					    create_reg->tblp[i].node_type, n);
23127c478bd9Sstevel@tonic-gate 					err_print(IGNORING_ENTRY, i, epath);
23137c478bd9Sstevel@tonic-gate 					continue;
23147c478bd9Sstevel@tonic-gate 				}
23157c478bd9Sstevel@tonic-gate 			}
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 			if (((flags & DRV_MASK) == 0) ^
23187c478bd9Sstevel@tonic-gate 			    (create_reg->tblp[i].drv_name == NULL)) {
23197c478bd9Sstevel@tonic-gate 				if ((flags & TYPE_RE) == TYPE_RE) {
23207c478bd9Sstevel@tonic-gate 					regfree(&(create_list_element->
23217c478bd9Sstevel@tonic-gate 					    node_type_comp));
23227c478bd9Sstevel@tonic-gate 				}
23237c478bd9Sstevel@tonic-gate 				free(create_list_element);
23247c478bd9Sstevel@tonic-gate 				err_print("flags value incompatible with "
23250a653502Swroche 				    "drv_name value in module create\n");
23267c478bd9Sstevel@tonic-gate 				err_print(IGNORING_ENTRY, i, epath);
23277c478bd9Sstevel@tonic-gate 				continue;
23287c478bd9Sstevel@tonic-gate 			}
23297c478bd9Sstevel@tonic-gate 
23307c478bd9Sstevel@tonic-gate 			if (((flags & DRV_MASK) != 0) &&
23317c478bd9Sstevel@tonic-gate 			    ((flags & DRV_MASK) != DRV_EXACT) &&
23327c478bd9Sstevel@tonic-gate 			    ((flags & DRV_MASK) !=  DRV_RE)) {
23337c478bd9Sstevel@tonic-gate 				if ((flags & TYPE_RE) == TYPE_RE) {
23347c478bd9Sstevel@tonic-gate 					regfree(&(create_list_element->
23357c478bd9Sstevel@tonic-gate 					    node_type_comp));
23367c478bd9Sstevel@tonic-gate 				}
23377c478bd9Sstevel@tonic-gate 				free(create_list_element);
23387c478bd9Sstevel@tonic-gate 				err_print("illegal DRV_* flag combination in "
23390a653502Swroche 				    "module create\n");
23407c478bd9Sstevel@tonic-gate 				err_print(IGNORING_ENTRY, i, epath);
23417c478bd9Sstevel@tonic-gate 				continue;
23427c478bd9Sstevel@tonic-gate 			}
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate 			/* precompile regular expression for efficiency */
23457c478bd9Sstevel@tonic-gate 			if ((create_reg->tblp[i].flags & DRV_RE) == DRV_RE) {
23467c478bd9Sstevel@tonic-gate 				if ((n = regcomp(&(create_list_element->
23477c478bd9Sstevel@tonic-gate 				    drv_name_comp),
23487c478bd9Sstevel@tonic-gate 				    create_reg->tblp[i].drv_name,
23497c478bd9Sstevel@tonic-gate 				    REG_EXTENDED)) != 0) {
23507c478bd9Sstevel@tonic-gate 					if ((flags & TYPE_RE) == TYPE_RE) {
23517c478bd9Sstevel@tonic-gate 						regfree(&(create_list_element->
23527c478bd9Sstevel@tonic-gate 						    node_type_comp));
23537c478bd9Sstevel@tonic-gate 					}
23547c478bd9Sstevel@tonic-gate 					free(create_list_element);
23557c478bd9Sstevel@tonic-gate 					err_print(REGCOMP_FAILED,
23560a653502Swroche 					    create_reg->tblp[i].drv_name, n);
23577c478bd9Sstevel@tonic-gate 					err_print(IGNORING_ENTRY, i, epath);
23587c478bd9Sstevel@tonic-gate 					continue;
23597c478bd9Sstevel@tonic-gate 				}
23607c478bd9Sstevel@tonic-gate 			}
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 			/* add to list sorted by interpose level */
23647c478bd9Sstevel@tonic-gate 			for (create_list_next = &(create_head);
23650a653502Swroche 			    (*create_list_next != NULL) &&
23660a653502Swroche 			    (*create_list_next)->create->interpose_lvl >=
23670a653502Swroche 			    create_list_element->create->interpose_lvl;
23680a653502Swroche 			    create_list_next = &((*create_list_next)->next))
23690a653502Swroche 				;
23707c478bd9Sstevel@tonic-gate 			create_list_element->next = *create_list_next;
23717c478bd9Sstevel@tonic-gate 			*create_list_next = create_list_element;
23727c478bd9Sstevel@tonic-gate 		}
23737c478bd9Sstevel@tonic-gate 	}
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	/*
23767c478bd9Sstevel@tonic-gate 	 *  put a ptr to each struct devfsadm_remove on "remove_head"
23777c478bd9Sstevel@tonic-gate 	 *  list sorted by interpose_lvl.
23787c478bd9Sstevel@tonic-gate 	 */
2379aa646b9dSvikram 	flags = 0;
23807c478bd9Sstevel@tonic-gate 	if (remove_reg != NULL) {
2381aa646b9dSvikram 		if (remove_reg->version < DEVFSADM_V1)
2382aa646b9dSvikram 			flags |= RM_NOINTERPOSE;
23837c478bd9Sstevel@tonic-gate 		for (i = 0; i < remove_reg->count; i++) {
23847c478bd9Sstevel@tonic-gate 
23857c478bd9Sstevel@tonic-gate 			remove_list_element = (remove_list_t *)
23860a653502Swroche 			    s_malloc(sizeof (remove_list_t));
23877c478bd9Sstevel@tonic-gate 
23887c478bd9Sstevel@tonic-gate 			remove_list_element->remove = &(remove_reg->tblp[i]);
2389aa646b9dSvikram 			remove_list_element->remove->flags |= flags;
23907c478bd9Sstevel@tonic-gate 			remove_list_element->modptr = module;
23917c478bd9Sstevel@tonic-gate 
23927c478bd9Sstevel@tonic-gate 			for (remove_list_next = &(remove_head);
23930a653502Swroche 			    (*remove_list_next != NULL) &&
23940a653502Swroche 			    (*remove_list_next)->remove->interpose_lvl >=
23950a653502Swroche 			    remove_list_element->remove->interpose_lvl;
23960a653502Swroche 			    remove_list_next = &((*remove_list_next)->next))
23970a653502Swroche 				;
23987c478bd9Sstevel@tonic-gate 			remove_list_element->next = *remove_list_next;
23997c478bd9Sstevel@tonic-gate 			*remove_list_next = remove_list_element;
24007c478bd9Sstevel@tonic-gate 		}
24017c478bd9Sstevel@tonic-gate 	}
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 	module->next = module_head;
24047c478bd9Sstevel@tonic-gate 	module_head = module;
24057c478bd9Sstevel@tonic-gate }
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate /*
2408ff2aee48Scth  * After we have completed a CACHE_STATE, if a SYNC_STATE does not occur
2409ff2aee48Scth  * within 'timeout' secs the minor_fini_thread needs to do a SYNC_STATE
2410ff2aee48Scth  * so that we still call the minor_fini routines.
24117c478bd9Sstevel@tonic-gate  */
24127c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24137c478bd9Sstevel@tonic-gate static void
2414ff2aee48Scth minor_fini_thread(void *arg)
24157c478bd9Sstevel@tonic-gate {
2416ff2aee48Scth 	timestruc_t	abstime;
24177c478bd9Sstevel@tonic-gate 
2418ff2aee48Scth 	vprint(INITFINI_MID, "minor_fini_thread starting\n");
24197c478bd9Sstevel@tonic-gate 
2420ff2aee48Scth 	(void) mutex_lock(&minor_fini_mutex);
2421ff2aee48Scth 	for (;;) {
2422ff2aee48Scth 		/* wait the gather period, or until signaled */
2423ff2aee48Scth 		abstime.tv_sec = time(NULL) + minor_fini_timeout;
2424ff2aee48Scth 		abstime.tv_nsec = 0;
2425ff2aee48Scth 		(void) cond_timedwait(&minor_fini_cv,
2426ff2aee48Scth 		    &minor_fini_mutex, &abstime);
2427ff2aee48Scth 
2428ff2aee48Scth 		/* if minor_fini was canceled, go wait again */
2429ff2aee48Scth 		if (minor_fini_canceled == TRUE)
2430ff2aee48Scth 			continue;
24317c478bd9Sstevel@tonic-gate 
2432ff2aee48Scth 		/* if minor_fini was delayed, go wait again */
2433ff2aee48Scth 		if (minor_fini_delayed == TRUE) {
2434ff2aee48Scth 			minor_fini_delayed = FALSE;
2435ff2aee48Scth 			continue;
2436ff2aee48Scth 		}
24377c478bd9Sstevel@tonic-gate 
2438ff2aee48Scth 		/* done with cancellations and delays, do the SYNC_STATE */
24397c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&minor_fini_mutex);
24407c478bd9Sstevel@tonic-gate 
2441ff2aee48Scth 		lock_dev();
2442ff2aee48Scth 		unlock_dev(SYNC_STATE);
2443ff2aee48Scth 		vprint(INITFINI_MID, "minor_fini sync done\n");
24447c478bd9Sstevel@tonic-gate 
2445ff2aee48Scth 		(void) mutex_lock(&minor_fini_mutex);
2446ff2aee48Scth 	}
24477c478bd9Sstevel@tonic-gate }
24487c478bd9Sstevel@tonic-gate 
2449ff2aee48Scth 
24507c478bd9Sstevel@tonic-gate /*
24517c478bd9Sstevel@tonic-gate  * Attempt to initialize module, if a minor_init routine exists.  Set
24527c478bd9Sstevel@tonic-gate  * the active flag if the routine exists and succeeds.	If it doesn't
24537c478bd9Sstevel@tonic-gate  * exist, just set the active flag.
24547c478bd9Sstevel@tonic-gate  */
24557c478bd9Sstevel@tonic-gate static int
24567c478bd9Sstevel@tonic-gate call_minor_init(module_t *module)
24577c478bd9Sstevel@tonic-gate {
24587c478bd9Sstevel@tonic-gate 	char *fcn = "call_minor_init: ";
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 	if ((module->flags & MODULE_ACTIVE) == MODULE_ACTIVE) {
24617c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
24627c478bd9Sstevel@tonic-gate 	}
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 	vprint(INITFINI_MID, "%smodule %s.  current state: inactive\n",
24650a653502Swroche 	    fcn, module->name);
24667c478bd9Sstevel@tonic-gate 
24677c478bd9Sstevel@tonic-gate 	if (module->minor_init == NULL) {
24687c478bd9Sstevel@tonic-gate 		module->flags |= MODULE_ACTIVE;
24697c478bd9Sstevel@tonic-gate 		vprint(INITFINI_MID, "minor_init not defined\n");
24707c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
24717c478bd9Sstevel@tonic-gate 	}
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate 	if ((*(module->minor_init))() == DEVFSADM_FAILURE) {
24747c478bd9Sstevel@tonic-gate 		err_print(FAILED_FOR_MODULE, MINOR_INIT, module->name);
24757c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
24767c478bd9Sstevel@tonic-gate 	}
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate 	vprint(INITFINI_MID, "minor_init() returns DEVFSADM_SUCCESS. "
24790a653502Swroche 	    "new state: active\n");
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 	module->flags |= MODULE_ACTIVE;
24827c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
24837c478bd9Sstevel@tonic-gate }
24847c478bd9Sstevel@tonic-gate 
24857c478bd9Sstevel@tonic-gate /*
24867c478bd9Sstevel@tonic-gate  * Creates a symlink 'link' to the physical path of node:minor.
24877c478bd9Sstevel@tonic-gate  * Construct link contents, then call create_link_common().
24887c478bd9Sstevel@tonic-gate  */
24897c478bd9Sstevel@tonic-gate /*ARGSUSED*/
24907c478bd9Sstevel@tonic-gate int
2491facf4a8dSllai devfsadm_mklink(char *link, di_node_t node, di_minor_t minor, int flags)
24927c478bd9Sstevel@tonic-gate {
24937c478bd9Sstevel@tonic-gate 	char rcontents[PATH_MAX];
24947c478bd9Sstevel@tonic-gate 	char devlink[PATH_MAX];
24957c478bd9Sstevel@tonic-gate 	char phy_path[PATH_MAX];
24967c478bd9Sstevel@tonic-gate 	char *acontents;
24977c478bd9Sstevel@tonic-gate 	char *dev_path;
24987c478bd9Sstevel@tonic-gate 	int numslashes;
24997c478bd9Sstevel@tonic-gate 	int rv;
25007c478bd9Sstevel@tonic-gate 	int i, link_exists;
25017c478bd9Sstevel@tonic-gate 	int last_was_slash = FALSE;
25027c478bd9Sstevel@tonic-gate 
25037c478bd9Sstevel@tonic-gate 	/*
25047c478bd9Sstevel@tonic-gate 	 * try to use devices path
25057c478bd9Sstevel@tonic-gate 	 */
25067c478bd9Sstevel@tonic-gate 	if ((node == lnode) && (minor == lminor)) {
25077c478bd9Sstevel@tonic-gate 		acontents = lphy_path;
25087c478bd9Sstevel@tonic-gate 	} else if (di_minor_type(minor) == DDM_ALIAS) {
25097c478bd9Sstevel@tonic-gate 		/* use /pseudo/clone@0:<driver> as the phys path */
25107c478bd9Sstevel@tonic-gate 		(void) snprintf(phy_path, sizeof (phy_path),
25117c478bd9Sstevel@tonic-gate 		    "/pseudo/clone@0:%s",
25127c478bd9Sstevel@tonic-gate 		    di_driver_name(di_minor_devinfo(minor)));
25137c478bd9Sstevel@tonic-gate 		acontents = phy_path;
25147c478bd9Sstevel@tonic-gate 	} else {
25157c478bd9Sstevel@tonic-gate 		if ((dev_path = di_devfs_path(node)) == NULL) {
25167c478bd9Sstevel@tonic-gate 			err_print(DI_DEVFS_PATH_FAILED, strerror(errno));
25177c478bd9Sstevel@tonic-gate 			devfsadm_exit(1);
2518537714daSvikram 			/*NOTREACHED*/
25197c478bd9Sstevel@tonic-gate 		}
25207c478bd9Sstevel@tonic-gate 		(void) snprintf(phy_path, sizeof (phy_path), "%s:%s",
25217c478bd9Sstevel@tonic-gate 		    dev_path, di_minor_name(minor));
25227c478bd9Sstevel@tonic-gate 		di_devfs_path_free(dev_path);
25237c478bd9Sstevel@tonic-gate 		acontents = phy_path;
25247c478bd9Sstevel@tonic-gate 	}
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 	/* prepend link with dev_dir contents */
25277c478bd9Sstevel@tonic-gate 	(void) strlcpy(devlink, dev_dir, sizeof (devlink));
25287c478bd9Sstevel@tonic-gate 	(void) strlcat(devlink, "/", sizeof (devlink));
25297c478bd9Sstevel@tonic-gate 	(void) strlcat(devlink, link, sizeof (devlink));
25307c478bd9Sstevel@tonic-gate 
25317c478bd9Sstevel@tonic-gate 	/*
25327c478bd9Sstevel@tonic-gate 	 * Calculate # of ../ to add.  Account for double '//' in path.
25337c478bd9Sstevel@tonic-gate 	 * Ignore all leading slashes.
25347c478bd9Sstevel@tonic-gate 	 */
25357c478bd9Sstevel@tonic-gate 	for (i = 0; link[i] == '/'; i++)
25367c478bd9Sstevel@tonic-gate 		;
25377c478bd9Sstevel@tonic-gate 	for (numslashes = 0; link[i] != '\0'; i++) {
25387c478bd9Sstevel@tonic-gate 		if (link[i] == '/') {
25397c478bd9Sstevel@tonic-gate 			if (last_was_slash == FALSE) {
25407c478bd9Sstevel@tonic-gate 				numslashes++;
25417c478bd9Sstevel@tonic-gate 				last_was_slash = TRUE;
25427c478bd9Sstevel@tonic-gate 			}
25437c478bd9Sstevel@tonic-gate 		} else {
25447c478bd9Sstevel@tonic-gate 			last_was_slash = FALSE;
25457c478bd9Sstevel@tonic-gate 		}
25467c478bd9Sstevel@tonic-gate 	}
25477c478bd9Sstevel@tonic-gate 	/* Don't count any trailing '/' */
25487c478bd9Sstevel@tonic-gate 	if (link[i-1] == '/') {
25497c478bd9Sstevel@tonic-gate 		numslashes--;
25507c478bd9Sstevel@tonic-gate 	}
25517c478bd9Sstevel@tonic-gate 
25527c478bd9Sstevel@tonic-gate 	rcontents[0] = '\0';
25537c478bd9Sstevel@tonic-gate 	do {
25547c478bd9Sstevel@tonic-gate 		(void) strlcat(rcontents, "../", sizeof (rcontents));
25557c478bd9Sstevel@tonic-gate 	} while (numslashes-- != 0);
25567c478bd9Sstevel@tonic-gate 
25577c478bd9Sstevel@tonic-gate 	(void) strlcat(rcontents, "devices", sizeof (rcontents));
25587c478bd9Sstevel@tonic-gate 	(void) strlcat(rcontents, acontents, sizeof (rcontents));
25597c478bd9Sstevel@tonic-gate 
25607c478bd9Sstevel@tonic-gate 	if (devlinks_debug == TRUE) {
25617c478bd9Sstevel@tonic-gate 		vprint(INFO_MID, "adding link %s ==> %s\n", devlink, rcontents);
25627c478bd9Sstevel@tonic-gate 	}
25637c478bd9Sstevel@tonic-gate 
25647c478bd9Sstevel@tonic-gate 	if ((rv = create_link_common(devlink, rcontents, &link_exists))
25657c478bd9Sstevel@tonic-gate 	    == DEVFSADM_SUCCESS) {
25667c478bd9Sstevel@tonic-gate 		linknew = TRUE;
25677c478bd9Sstevel@tonic-gate 		add_link_to_cache(link, acontents);
25687c478bd9Sstevel@tonic-gate 	} else {
25697c478bd9Sstevel@tonic-gate 		linknew = FALSE;
25707c478bd9Sstevel@tonic-gate 	}
25717c478bd9Sstevel@tonic-gate 
25727c478bd9Sstevel@tonic-gate 	if (link_exists == TRUE) {
25736e670f77Saj 		/* Link exists or was just created */
25746e670f77Saj 		(void) di_devlink_add_link(devlink_cache, link, rcontents,
25756e670f77Saj 		    DI_PRIMARY_LINK);
25766e670f77Saj 
25776e670f77Saj 		if (system_labeled && (flags & DA_ADD)) {
257845916cd2Sjpk 			/*
25796e670f77Saj 			 * Add this to the list of allocatable devices. If this
25806e670f77Saj 			 * is a hotplugged, removable disk, add it as rmdisk.
258145916cd2Sjpk 			 */
25826e670f77Saj 			int instance = di_instance(node);
258345916cd2Sjpk 
25846e670f77Saj 			if ((flags & DA_CD) &&
25856e670f77Saj 			    (_da_check_for_usb(devlink, root_dir) == 1)) {
258645916cd2Sjpk 				(void) da_add_list(&devlist, devlink, instance,
258745916cd2Sjpk 				    DA_ADD|DA_RMDISK);
258845916cd2Sjpk 				update_devdb = DA_RMDISK;
25896e670f77Saj 			} else if (linknew == TRUE) {
25906e670f77Saj 				(void) da_add_list(&devlist, devlink, instance,
25916e670f77Saj 				    flags);
25926e670f77Saj 				update_devdb = flags;
259345916cd2Sjpk 			}
259445916cd2Sjpk 		}
25957c478bd9Sstevel@tonic-gate 	}
25967c478bd9Sstevel@tonic-gate 
25977c478bd9Sstevel@tonic-gate 	return (rv);
25987c478bd9Sstevel@tonic-gate }
25997c478bd9Sstevel@tonic-gate 
26007c478bd9Sstevel@tonic-gate /*
26017c478bd9Sstevel@tonic-gate  * Creates a symlink link to primary_link.  Calculates relative
26027c478bd9Sstevel@tonic-gate  * directory offsets, then calls link_common().
26037c478bd9Sstevel@tonic-gate  */
26047c478bd9Sstevel@tonic-gate /*ARGSUSED*/
26057c478bd9Sstevel@tonic-gate int
26067c478bd9Sstevel@tonic-gate devfsadm_secondary_link(char *link, char *primary_link, int flags)
26077c478bd9Sstevel@tonic-gate {
26087c478bd9Sstevel@tonic-gate 	char contents[PATH_MAX + 1];
26097c478bd9Sstevel@tonic-gate 	char devlink[PATH_MAX + 1];
26107c478bd9Sstevel@tonic-gate 	int rv, link_exists;
26117c478bd9Sstevel@tonic-gate 	char *fpath;
26127c478bd9Sstevel@tonic-gate 	char *tpath;
26137c478bd9Sstevel@tonic-gate 	char *op;
26147c478bd9Sstevel@tonic-gate 
26157c478bd9Sstevel@tonic-gate 	/* prepend link with dev_dir contents */
26167c478bd9Sstevel@tonic-gate 	(void) strcpy(devlink, dev_dir);
26177c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, "/");
26187c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, link);
26197c478bd9Sstevel@tonic-gate 	/*
26207c478bd9Sstevel@tonic-gate 	 * building extra link, so use first link as link contents, but first
26217c478bd9Sstevel@tonic-gate 	 * make it relative.
26227c478bd9Sstevel@tonic-gate 	 */
26237c478bd9Sstevel@tonic-gate 	fpath = link;
26247c478bd9Sstevel@tonic-gate 	tpath = primary_link;
26257c478bd9Sstevel@tonic-gate 	op = contents;
26267c478bd9Sstevel@tonic-gate 
26277c478bd9Sstevel@tonic-gate 	while (*fpath == *tpath && *fpath != '\0') {
26287c478bd9Sstevel@tonic-gate 		fpath++, tpath++;
26297c478bd9Sstevel@tonic-gate 	}
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 	/* Count directories to go up, if any, and add "../" */
26327c478bd9Sstevel@tonic-gate 	while (*fpath != '\0') {
26337c478bd9Sstevel@tonic-gate 		if (*fpath == '/') {
26347c478bd9Sstevel@tonic-gate 			(void) strcpy(op, "../");
26357c478bd9Sstevel@tonic-gate 			op += 3;
26367c478bd9Sstevel@tonic-gate 		}
26377c478bd9Sstevel@tonic-gate 		fpath++;
26387c478bd9Sstevel@tonic-gate 	}
26397c478bd9Sstevel@tonic-gate 
26407c478bd9Sstevel@tonic-gate 	/*
26417c478bd9Sstevel@tonic-gate 	 * Back up to the start of the current path component, in
26427c478bd9Sstevel@tonic-gate 	 * case in the middle
26437c478bd9Sstevel@tonic-gate 	 */
26447c478bd9Sstevel@tonic-gate 	while (tpath != primary_link && *(tpath-1) != '/') {
26457c478bd9Sstevel@tonic-gate 		tpath--;
26467c478bd9Sstevel@tonic-gate 	}
26477c478bd9Sstevel@tonic-gate 	(void) strcpy(op, tpath);
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate 	if (devlinks_debug == TRUE) {
26507c478bd9Sstevel@tonic-gate 		vprint(INFO_MID, "adding extra link %s ==> %s\n",
26510a653502Swroche 		    devlink, contents);
26527c478bd9Sstevel@tonic-gate 	}
26537c478bd9Sstevel@tonic-gate 
26547c478bd9Sstevel@tonic-gate 	if ((rv = create_link_common(devlink, contents, &link_exists))
26557c478bd9Sstevel@tonic-gate 	    == DEVFSADM_SUCCESS) {
26567c478bd9Sstevel@tonic-gate 		/*
26577c478bd9Sstevel@tonic-gate 		 * we need to save the ultimate /devices contents, and not the
26587c478bd9Sstevel@tonic-gate 		 * secondary link, since hotcleanup only looks at /devices path.
26597c478bd9Sstevel@tonic-gate 		 * Since we don't have devices path here, we can try to get it
26607c478bd9Sstevel@tonic-gate 		 * by readlink'ing the secondary link.  This assumes the primary
26617c478bd9Sstevel@tonic-gate 		 * link was created first.
26627c478bd9Sstevel@tonic-gate 		 */
26637c478bd9Sstevel@tonic-gate 		add_link_to_cache(link, lphy_path);
26647c478bd9Sstevel@tonic-gate 		linknew = TRUE;
266545916cd2Sjpk 		if (system_labeled &&
266645916cd2Sjpk 		    ((flags & DA_AUDIO) && (flags & DA_ADD))) {
266745916cd2Sjpk 			/*
266845916cd2Sjpk 			 * Add this device to the list of allocatable devices.
266945916cd2Sjpk 			 */
267045916cd2Sjpk 			int	instance = 0;
267145916cd2Sjpk 
267245916cd2Sjpk 			op = strrchr(contents, '/');
267345916cd2Sjpk 			op++;
267445916cd2Sjpk 			(void) sscanf(op, "%d", &instance);
267545916cd2Sjpk 			(void) da_add_list(&devlist, devlink, instance, flags);
267645916cd2Sjpk 			update_devdb = flags;
267745916cd2Sjpk 		}
26787c478bd9Sstevel@tonic-gate 	} else {
26797c478bd9Sstevel@tonic-gate 		linknew = FALSE;
26807c478bd9Sstevel@tonic-gate 	}
26817c478bd9Sstevel@tonic-gate 
26827c478bd9Sstevel@tonic-gate 	/*
26837c478bd9Sstevel@tonic-gate 	 * If link exists or was just created, add it to the database
26847c478bd9Sstevel@tonic-gate 	 */
26857c478bd9Sstevel@tonic-gate 	if (link_exists == TRUE) {
26867c478bd9Sstevel@tonic-gate 		(void) di_devlink_add_link(devlink_cache, link, contents,
26877c478bd9Sstevel@tonic-gate 		    DI_SECONDARY_LINK);
26887c478bd9Sstevel@tonic-gate 	}
26897c478bd9Sstevel@tonic-gate 
26907c478bd9Sstevel@tonic-gate 	return (rv);
26917c478bd9Sstevel@tonic-gate }
26927c478bd9Sstevel@tonic-gate 
26937c478bd9Sstevel@tonic-gate /* returns pointer to the devices directory */
26947c478bd9Sstevel@tonic-gate char *
26957c478bd9Sstevel@tonic-gate devfsadm_get_devices_dir()
26967c478bd9Sstevel@tonic-gate {
26977c478bd9Sstevel@tonic-gate 	return (devices_dir);
26987c478bd9Sstevel@tonic-gate }
26997c478bd9Sstevel@tonic-gate 
27007c478bd9Sstevel@tonic-gate /*
27017c478bd9Sstevel@tonic-gate  * Does the actual link creation.  VERBOSE_MID only used if there is
27027c478bd9Sstevel@tonic-gate  * a change.  CHATTY_MID used otherwise.
27037c478bd9Sstevel@tonic-gate  */
27047c478bd9Sstevel@tonic-gate static int
27057c478bd9Sstevel@tonic-gate create_link_common(char *devlink, char *contents, int *exists)
27067c478bd9Sstevel@tonic-gate {
27077c478bd9Sstevel@tonic-gate 	int try;
27087c478bd9Sstevel@tonic-gate 	int linksize;
27097c478bd9Sstevel@tonic-gate 	int max_tries = 0;
27107c478bd9Sstevel@tonic-gate 	static int prev_link_existed = TRUE;
27117c478bd9Sstevel@tonic-gate 	char checkcontents[PATH_MAX + 1];
27127c478bd9Sstevel@tonic-gate 	char *hide;
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate 	*exists = FALSE;
27157c478bd9Sstevel@tonic-gate 
27167c478bd9Sstevel@tonic-gate 	/* Database is not updated when file_mods == FALSE */
27177c478bd9Sstevel@tonic-gate 	if (file_mods == FALSE) {
271894c894bbSVikram Hegde 		/* we want *actual* link contents so no alias redirection */
27197c478bd9Sstevel@tonic-gate 		linksize = readlink(devlink, checkcontents, PATH_MAX);
27207c478bd9Sstevel@tonic-gate 		if (linksize > 0) {
27217c478bd9Sstevel@tonic-gate 			checkcontents[linksize] = '\0';
27227c478bd9Sstevel@tonic-gate 			if (strcmp(checkcontents, contents) != 0) {
27237c478bd9Sstevel@tonic-gate 				vprint(CHATTY_MID, REMOVING_LINK,
27240a653502Swroche 				    devlink, checkcontents);
27257c478bd9Sstevel@tonic-gate 				return (DEVFSADM_SUCCESS);
27267c478bd9Sstevel@tonic-gate 			} else {
27277c478bd9Sstevel@tonic-gate 				vprint(CHATTY_MID, "link exists and is correct:"
27280a653502Swroche 				    " %s -> %s\n", devlink, contents);
27297c478bd9Sstevel@tonic-gate 				/* failure only in that the link existed */
27307c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
27317c478bd9Sstevel@tonic-gate 			}
27327c478bd9Sstevel@tonic-gate 		} else {
27337c478bd9Sstevel@tonic-gate 			vprint(VERBOSE_MID, CREATING_LINK, devlink, contents);
27347c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
27357c478bd9Sstevel@tonic-gate 		}
27367c478bd9Sstevel@tonic-gate 	}
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate 	/*
27397c478bd9Sstevel@tonic-gate 	 * systems calls are expensive, so predict whether to readlink
27407c478bd9Sstevel@tonic-gate 	 * or symlink first, based on previous attempt
27417c478bd9Sstevel@tonic-gate 	 */
27427c478bd9Sstevel@tonic-gate 	if (prev_link_existed == FALSE) {
27437c478bd9Sstevel@tonic-gate 		try = CREATE_LINK;
27447c478bd9Sstevel@tonic-gate 	} else {
27457c478bd9Sstevel@tonic-gate 		try = READ_LINK;
27467c478bd9Sstevel@tonic-gate 	}
27477c478bd9Sstevel@tonic-gate 
27487c478bd9Sstevel@tonic-gate 	while (++max_tries <= 3) {
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate 		switch (try) {
27517c478bd9Sstevel@tonic-gate 		case  CREATE_LINK:
27527c478bd9Sstevel@tonic-gate 
27537c478bd9Sstevel@tonic-gate 			if (symlink(contents, devlink) == 0) {
27547c478bd9Sstevel@tonic-gate 				vprint(VERBOSE_MID, CREATING_LINK, devlink,
27550a653502Swroche 				    contents);
27567c478bd9Sstevel@tonic-gate 				prev_link_existed = FALSE;
27577c478bd9Sstevel@tonic-gate 				/* link successfully created */
27587c478bd9Sstevel@tonic-gate 				*exists = TRUE;
27597c478bd9Sstevel@tonic-gate 				set_logindev_perms(devlink);
27607c478bd9Sstevel@tonic-gate 				return (DEVFSADM_SUCCESS);
27617c478bd9Sstevel@tonic-gate 			} else {
27627c478bd9Sstevel@tonic-gate 				switch (errno) {
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate 				case ENOENT:
27657c478bd9Sstevel@tonic-gate 					/* dirpath to node doesn't exist */
27667c478bd9Sstevel@tonic-gate 					hide = strrchr(devlink, '/');
27677c478bd9Sstevel@tonic-gate 					*hide = '\0';
27687c478bd9Sstevel@tonic-gate 					s_mkdirp(devlink, S_IRWXU|S_IRGRP|
27690a653502Swroche 					    S_IXGRP|S_IROTH|S_IXOTH);
27707c478bd9Sstevel@tonic-gate 					*hide = '/';
27717c478bd9Sstevel@tonic-gate 					break;
27727c478bd9Sstevel@tonic-gate 				case EEXIST:
27737c478bd9Sstevel@tonic-gate 					try = READ_LINK;
27747c478bd9Sstevel@tonic-gate 					break;
27757c478bd9Sstevel@tonic-gate 				default:
27767c478bd9Sstevel@tonic-gate 					err_print(SYMLINK_FAILED, devlink,
27770a653502Swroche 					    contents, strerror(errno));
27787c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
27797c478bd9Sstevel@tonic-gate 				}
27807c478bd9Sstevel@tonic-gate 			}
27817c478bd9Sstevel@tonic-gate 			break;
27827c478bd9Sstevel@tonic-gate 
27837c478bd9Sstevel@tonic-gate 		case READ_LINK:
27847c478bd9Sstevel@tonic-gate 
278594c894bbSVikram Hegde 			/*
278694c894bbSVikram Hegde 			 * If there is redirection, new phys path
278794c894bbSVikram Hegde 			 * and old phys path will not match and the
278894c894bbSVikram Hegde 			 * link will be created with new phys path
278994c894bbSVikram Hegde 			 * which is what we want. So we want real
279094c894bbSVikram Hegde 			 * contents.
279194c894bbSVikram Hegde 			 */
27927c478bd9Sstevel@tonic-gate 			linksize = readlink(devlink, checkcontents, PATH_MAX);
27937c478bd9Sstevel@tonic-gate 			if (linksize >= 0) {
27947c478bd9Sstevel@tonic-gate 				checkcontents[linksize] = '\0';
27957c478bd9Sstevel@tonic-gate 				if (strcmp(checkcontents, contents) != 0) {
27967c478bd9Sstevel@tonic-gate 					s_unlink(devlink);
27977c478bd9Sstevel@tonic-gate 					vprint(VERBOSE_MID, REMOVING_LINK,
27980a653502Swroche 					    devlink, checkcontents);
27997c478bd9Sstevel@tonic-gate 					try = CREATE_LINK;
28007c478bd9Sstevel@tonic-gate 				} else {
28017c478bd9Sstevel@tonic-gate 					prev_link_existed = TRUE;
28027c478bd9Sstevel@tonic-gate 					vprint(CHATTY_MID,
28030a653502Swroche 					    "link exists and is correct:"
28040a653502Swroche 					    " %s -> %s\n", devlink, contents);
28057c478bd9Sstevel@tonic-gate 					*exists = TRUE;
28067c478bd9Sstevel@tonic-gate 					/* failure in that the link existed */
28077c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
28087c478bd9Sstevel@tonic-gate 				}
28097c478bd9Sstevel@tonic-gate 			} else {
28107c478bd9Sstevel@tonic-gate 				switch (errno) {
28117c478bd9Sstevel@tonic-gate 				case EINVAL:
28127c478bd9Sstevel@tonic-gate 					/* not a symlink, remove and create */
28137c478bd9Sstevel@tonic-gate 					s_unlink(devlink);
2814902eb929SToomas Soome 					/* FALLTHROUGH */
28157c478bd9Sstevel@tonic-gate 				default:
28167c478bd9Sstevel@tonic-gate 					/* maybe it didn't exist at all */
28177c478bd9Sstevel@tonic-gate 					try = CREATE_LINK;
28187c478bd9Sstevel@tonic-gate 					break;
28197c478bd9Sstevel@tonic-gate 				}
28207c478bd9Sstevel@tonic-gate 			}
28217c478bd9Sstevel@tonic-gate 			break;
28227c478bd9Sstevel@tonic-gate 		}
28237c478bd9Sstevel@tonic-gate 	}
28247c478bd9Sstevel@tonic-gate 	err_print(MAX_ATTEMPTS, devlink, contents);
28257c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
28267c478bd9Sstevel@tonic-gate }
28277c478bd9Sstevel@tonic-gate 
28287c478bd9Sstevel@tonic-gate static void
28297c478bd9Sstevel@tonic-gate set_logindev_perms(char *devlink)
28307c478bd9Sstevel@tonic-gate {
28317c478bd9Sstevel@tonic-gate 	struct login_dev *newdev;
28327c478bd9Sstevel@tonic-gate 	struct passwd pwd, *resp;
28337c478bd9Sstevel@tonic-gate 	char pwd_buf[PATH_MAX];
28347c478bd9Sstevel@tonic-gate 	int rv;
28357c478bd9Sstevel@tonic-gate 	struct stat sb;
28367c478bd9Sstevel@tonic-gate 	char *devfs_path = NULL;
28377c478bd9Sstevel@tonic-gate 
28387c478bd9Sstevel@tonic-gate 	/*
28397c478bd9Sstevel@tonic-gate 	 * We only want logindev perms to be set when a device is
28407c478bd9Sstevel@tonic-gate 	 * hotplugged or an application requests synchronous creates.
28417c478bd9Sstevel@tonic-gate 	 * So we enable this only in daemon mode. In addition,
28427c478bd9Sstevel@tonic-gate 	 * login(1) only fixes the std. /dev dir. So we don't
28437c478bd9Sstevel@tonic-gate 	 * change perms if alternate root is set.
28447c478bd9Sstevel@tonic-gate 	 * login_dev_enable is TRUE only in these cases.
28457c478bd9Sstevel@tonic-gate 	 */
28467c478bd9Sstevel@tonic-gate 	if (login_dev_enable != TRUE)
28477c478bd9Sstevel@tonic-gate 		return;
28487c478bd9Sstevel@tonic-gate 
28497c478bd9Sstevel@tonic-gate 	/*
28507c478bd9Sstevel@tonic-gate 	 * Normally, /etc/logindevperm has few (8 - 10 entries) which
28517c478bd9Sstevel@tonic-gate 	 * may be regular expressions (globs were converted to RE).
28527c478bd9Sstevel@tonic-gate 	 * So just do a linear search through the list.
28537c478bd9Sstevel@tonic-gate 	 */
28547c478bd9Sstevel@tonic-gate 	for (newdev = login_dev_cache; newdev; newdev = newdev->ldev_next) {
28557c478bd9Sstevel@tonic-gate 		vprint(FILES_MID, "matching %s with %s\n", devlink,
28567c478bd9Sstevel@tonic-gate 		    newdev->ldev_device);
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate 		if (regexec(&newdev->ldev_device_regex, devlink, 0,
28597c478bd9Sstevel@tonic-gate 		    NULL, 0) == 0)  {
28607c478bd9Sstevel@tonic-gate 			vprint(FILES_MID, "matched %s with %s\n", devlink,
28617c478bd9Sstevel@tonic-gate 			    newdev->ldev_device);
28627c478bd9Sstevel@tonic-gate 			break;
28637c478bd9Sstevel@tonic-gate 		}
28647c478bd9Sstevel@tonic-gate 	}
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate 	if (newdev == NULL)
28677c478bd9Sstevel@tonic-gate 		return;
28687c478bd9Sstevel@tonic-gate 
28697c478bd9Sstevel@tonic-gate 	/*
28707c478bd9Sstevel@tonic-gate 	 * we have a match, now find the driver associated with this
28717c478bd9Sstevel@tonic-gate 	 * minor node using a snapshot on the physical path
28727c478bd9Sstevel@tonic-gate 	 */
28737c478bd9Sstevel@tonic-gate 	(void) resolve_link(devlink, NULL, NULL, &devfs_path, 0);
287494c894bbSVikram Hegde 	/*
287594c894bbSVikram Hegde 	 * We dont need redirection here - the actual link contents
287694c894bbSVikram Hegde 	 * whether "alias" or "current" are fine
287794c894bbSVikram Hegde 	 */
28787c478bd9Sstevel@tonic-gate 	if (devfs_path) {
28797c478bd9Sstevel@tonic-gate 		di_node_t node;
288094c894bbSVikram Hegde 		char *drv;
28817c478bd9Sstevel@tonic-gate 		struct driver_list *list;
28827c478bd9Sstevel@tonic-gate 		char *p;
28837c478bd9Sstevel@tonic-gate 
28847c478bd9Sstevel@tonic-gate 		/* truncate on : so we can take a snapshot */
28857c478bd9Sstevel@tonic-gate 		(void) strcpy(pwd_buf, devfs_path);
28867c478bd9Sstevel@tonic-gate 		p = strrchr(pwd_buf, ':');
28877c478bd9Sstevel@tonic-gate 		if (p == NULL) {
28887c478bd9Sstevel@tonic-gate 			free(devfs_path);
28897c478bd9Sstevel@tonic-gate 			return;
28907c478bd9Sstevel@tonic-gate 		}
28917c478bd9Sstevel@tonic-gate 		*p = '\0';
28927c478bd9Sstevel@tonic-gate 
28937c478bd9Sstevel@tonic-gate 		vprint(FILES_MID, "link=%s->physpath=%s\n",
28947c478bd9Sstevel@tonic-gate 		    devlink, pwd_buf);
28957c478bd9Sstevel@tonic-gate 
28967c478bd9Sstevel@tonic-gate 		node = di_init(pwd_buf, DINFOMINOR);
28977c478bd9Sstevel@tonic-gate 
289894c894bbSVikram Hegde 		drv = NULL;
28997c478bd9Sstevel@tonic-gate 		if (node) {
29007c478bd9Sstevel@tonic-gate 			drv = di_driver_name(node);
29017c478bd9Sstevel@tonic-gate 
29027c478bd9Sstevel@tonic-gate 			if (drv) {
29037c478bd9Sstevel@tonic-gate 				vprint(FILES_MID, "%s: driver is %s\n",
29047c478bd9Sstevel@tonic-gate 				    devlink, drv);
29057c478bd9Sstevel@tonic-gate 			}
29067c478bd9Sstevel@tonic-gate 		}
29077c478bd9Sstevel@tonic-gate 		/* search thru the driver list specified in logindevperm */
29087c478bd9Sstevel@tonic-gate 		list = newdev->ldev_driver_list;
29097c478bd9Sstevel@tonic-gate 		if ((drv != NULL) && (list != NULL)) {
29107c478bd9Sstevel@tonic-gate 			while (list) {
29117c478bd9Sstevel@tonic-gate 				if (strcmp(list->driver_name,
29127c478bd9Sstevel@tonic-gate 				    drv) == 0) {
29137c478bd9Sstevel@tonic-gate 					vprint(FILES_MID,
29147c478bd9Sstevel@tonic-gate 					    "driver %s match!\n", drv);
29157c478bd9Sstevel@tonic-gate 					break;
29167c478bd9Sstevel@tonic-gate 				}
29177c478bd9Sstevel@tonic-gate 				list = list->next;
29187c478bd9Sstevel@tonic-gate 			}
29197c478bd9Sstevel@tonic-gate 			if (list == NULL) {
29207c478bd9Sstevel@tonic-gate 				vprint(FILES_MID, "no driver match!\n");
29217c478bd9Sstevel@tonic-gate 				free(devfs_path);
29227c478bd9Sstevel@tonic-gate 				return;
29237c478bd9Sstevel@tonic-gate 			}
29247c478bd9Sstevel@tonic-gate 		}
29257c478bd9Sstevel@tonic-gate 		free(devfs_path);
292694c894bbSVikram Hegde 		di_fini(node);
29277c478bd9Sstevel@tonic-gate 	} else {
29287c478bd9Sstevel@tonic-gate 		return;
29297c478bd9Sstevel@tonic-gate 	}
29307c478bd9Sstevel@tonic-gate 
29317c478bd9Sstevel@tonic-gate 	vprint(FILES_MID, "changing permissions of %s\n", devlink);
29327c478bd9Sstevel@tonic-gate 
29337c478bd9Sstevel@tonic-gate 	/*
29347c478bd9Sstevel@tonic-gate 	 * We have a match. We now attempt to determine the
29357c478bd9Sstevel@tonic-gate 	 * owner and group of the console user.
29367c478bd9Sstevel@tonic-gate 	 *
29377c478bd9Sstevel@tonic-gate 	 * stat() the console device newdev->ldev_console
29387c478bd9Sstevel@tonic-gate 	 * which will always exist - it will have the right owner but
29397c478bd9Sstevel@tonic-gate 	 * not the right group. Use getpwuid_r() to determine group for this
29407c478bd9Sstevel@tonic-gate 	 * uid.
29417c478bd9Sstevel@tonic-gate 	 * Note, it is safe to use name service here since if name services
29427c478bd9Sstevel@tonic-gate 	 * are not available (during boot or in single-user mode), then
29437c478bd9Sstevel@tonic-gate 	 * console owner will be root and its gid can be found in
29447c478bd9Sstevel@tonic-gate 	 * local files.
29457c478bd9Sstevel@tonic-gate 	 */
29467c478bd9Sstevel@tonic-gate 	if (stat(newdev->ldev_console, &sb) == -1) {
29477c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, STAT_FAILED, newdev->ldev_console,
29487c478bd9Sstevel@tonic-gate 		    strerror(errno));
29497c478bd9Sstevel@tonic-gate 		return;
29507c478bd9Sstevel@tonic-gate 	}
29517c478bd9Sstevel@tonic-gate 
29527c478bd9Sstevel@tonic-gate 	resp = NULL;
29537c478bd9Sstevel@tonic-gate 	rv = getpwuid_r(sb.st_uid, &pwd, pwd_buf, sizeof (pwd_buf), &resp);
29547c478bd9Sstevel@tonic-gate 	if (rv || resp == NULL) {
29557c478bd9Sstevel@tonic-gate 		rv = rv ? rv : EINVAL;
29567c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, GID_FAILED, sb.st_uid,
29577c478bd9Sstevel@tonic-gate 		    strerror(rv));
29587c478bd9Sstevel@tonic-gate 		return;
29597c478bd9Sstevel@tonic-gate 	}
29607c478bd9Sstevel@tonic-gate 
29617c478bd9Sstevel@tonic-gate 	assert(&pwd == resp);
29627c478bd9Sstevel@tonic-gate 
29637c478bd9Sstevel@tonic-gate 	sb.st_gid = resp->pw_gid;
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate 	if (chmod(devlink, newdev->ldev_perms) == -1) {
29667c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, CHMOD_FAILED, devlink,
29677c478bd9Sstevel@tonic-gate 		    strerror(errno));
29687c478bd9Sstevel@tonic-gate 		return;
29697c478bd9Sstevel@tonic-gate 	}
29707c478bd9Sstevel@tonic-gate 
29717c478bd9Sstevel@tonic-gate 	if (chown(devlink, sb.st_uid, sb.st_gid)  == -1) {
29727c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, CHOWN_FAILED, devlink,
29737c478bd9Sstevel@tonic-gate 		    strerror(errno));
29747c478bd9Sstevel@tonic-gate 	}
29757c478bd9Sstevel@tonic-gate }
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate /*
29787c478bd9Sstevel@tonic-gate  * Reset /devices node with appropriate permissions and
29797c478bd9Sstevel@tonic-gate  * ownership as specified in /etc/minor_perm.
29807c478bd9Sstevel@tonic-gate  */
29817c478bd9Sstevel@tonic-gate static void
29827c478bd9Sstevel@tonic-gate reset_node_permissions(di_node_t node, di_minor_t minor)
29837c478bd9Sstevel@tonic-gate {
29847c478bd9Sstevel@tonic-gate 	int spectype;
29857c478bd9Sstevel@tonic-gate 	char phy_path[PATH_MAX + 1];
29867c478bd9Sstevel@tonic-gate 	mode_t mode;
29877c478bd9Sstevel@tonic-gate 	dev_t dev;
29887c478bd9Sstevel@tonic-gate 	uid_t uid;
29897c478bd9Sstevel@tonic-gate 	gid_t gid;
29907c478bd9Sstevel@tonic-gate 	struct stat sb;
29917c478bd9Sstevel@tonic-gate 	char *dev_path, *aminor = NULL;
29927c478bd9Sstevel@tonic-gate 
29937c478bd9Sstevel@tonic-gate 	/* lphy_path starts with / */
29947c478bd9Sstevel@tonic-gate 	if ((dev_path = di_devfs_path(node)) == NULL) {
29957c478bd9Sstevel@tonic-gate 		err_print(DI_DEVFS_PATH_FAILED, strerror(errno));
29967c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
2997537714daSvikram 		/*NOTREACHED*/
29987c478bd9Sstevel@tonic-gate 	}
29997c478bd9Sstevel@tonic-gate 	(void) strcpy(lphy_path, dev_path);
30007c478bd9Sstevel@tonic-gate 	di_devfs_path_free(dev_path);
30017c478bd9Sstevel@tonic-gate 
30027c478bd9Sstevel@tonic-gate 	(void) strcat(lphy_path, ":");
30037c478bd9Sstevel@tonic-gate 	if (di_minor_type(minor) == DDM_ALIAS) {
30047c478bd9Sstevel@tonic-gate 		char *driver;
30057c478bd9Sstevel@tonic-gate 		aminor = di_minor_name(minor);
30067c478bd9Sstevel@tonic-gate 		driver = di_driver_name(di_minor_devinfo(minor));
30077c478bd9Sstevel@tonic-gate 		(void) strcat(lphy_path, driver);
30087c478bd9Sstevel@tonic-gate 	} else
30097c478bd9Sstevel@tonic-gate 		(void) strcat(lphy_path, di_minor_name(minor));
30107c478bd9Sstevel@tonic-gate 
30117c478bd9Sstevel@tonic-gate 	(void) strcpy(phy_path, devices_dir);
30127c478bd9Sstevel@tonic-gate 	(void) strcat(phy_path, lphy_path);
30137c478bd9Sstevel@tonic-gate 
30147c478bd9Sstevel@tonic-gate 	lnode = node;
30157c478bd9Sstevel@tonic-gate 	lminor = minor;
30167c478bd9Sstevel@tonic-gate 
30177c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "reset_node_permissions: phy_path=%s lphy_path=%s\n",
30180a653502Swroche 	    phy_path, lphy_path);
30197c478bd9Sstevel@tonic-gate 
30207c478bd9Sstevel@tonic-gate 	dev = di_minor_devt(minor);
30217c478bd9Sstevel@tonic-gate 	spectype = di_minor_spectype(minor); /* block or char */
30227c478bd9Sstevel@tonic-gate 
30237c478bd9Sstevel@tonic-gate 	getattr(phy_path, aminor, spectype, dev, &mode, &uid, &gid);
30247c478bd9Sstevel@tonic-gate 
30257c478bd9Sstevel@tonic-gate 	/*
30267c478bd9Sstevel@tonic-gate 	 * compare and set permissions and ownership
30277c478bd9Sstevel@tonic-gate 	 *
30287c478bd9Sstevel@tonic-gate 	 * Under devfs, a quick insertion and removal of USB devices
30297c478bd9Sstevel@tonic-gate 	 * would cause stat of physical path to fail. In this case,
30307c478bd9Sstevel@tonic-gate 	 * we emit a verbose message, but don't print errors.
30317c478bd9Sstevel@tonic-gate 	 */
30327c478bd9Sstevel@tonic-gate 	if ((stat(phy_path, &sb) == -1) || (sb.st_rdev != dev)) {
30337c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, NO_DEVFS_NODE, phy_path);
30347c478bd9Sstevel@tonic-gate 		return;
30357c478bd9Sstevel@tonic-gate 	}
30367c478bd9Sstevel@tonic-gate 
30377c478bd9Sstevel@tonic-gate 	/*
303845916cd2Sjpk 	 * If we are here for a new device
303945916cd2Sjpk 	 *	If device allocation is on
304045916cd2Sjpk 	 *	then
304145916cd2Sjpk 	 *		set ownership to root:other and permissions to 0000
304245916cd2Sjpk 	 *	else
304345916cd2Sjpk 	 *		set ownership and permissions as specified in minor_perm
304445916cd2Sjpk 	 * If we are here for an existing device
304545916cd2Sjpk 	 *	If device allocation is to be turned on
304645916cd2Sjpk 	 *	then
304745916cd2Sjpk 	 *		reset ownership to root:other and permissions to 0000
304845916cd2Sjpk 	 *	else if device allocation is to be turned off
304945916cd2Sjpk 	 *		reset ownership and permissions to those specified in
305045916cd2Sjpk 	 *		minor_perm
305145916cd2Sjpk 	 *	else
3052ff2aee48Scth 	 *		preserve existing/user-modified ownership and
305345916cd2Sjpk 	 *		permissions
305445916cd2Sjpk 	 *
305545916cd2Sjpk 	 * devfs indicates a new device by faking access time to be zero.
30567c478bd9Sstevel@tonic-gate 	 */
30577c478bd9Sstevel@tonic-gate 	if (sb.st_atime != 0) {
30587c478bd9Sstevel@tonic-gate 		int  i;
30597c478bd9Sstevel@tonic-gate 		char *nt;
30607c478bd9Sstevel@tonic-gate 
306145916cd2Sjpk 		if ((devalloc_flag == 0) && (devalloc_is_on != 1))
306245916cd2Sjpk 			/*
306345916cd2Sjpk 			 * Leave existing devices as they are if we are not
306445916cd2Sjpk 			 * turning device allocation on/off.
306545916cd2Sjpk 			 */
30667c478bd9Sstevel@tonic-gate 			return;
30677c478bd9Sstevel@tonic-gate 
30687c478bd9Sstevel@tonic-gate 		nt = di_minor_nodetype(minor);
306945916cd2Sjpk 
30707c478bd9Sstevel@tonic-gate 		if (nt == NULL)
30717c478bd9Sstevel@tonic-gate 			return;
307245916cd2Sjpk 
307345916cd2Sjpk 		for (i = 0; devalloc_list[i]; i++) {
307445916cd2Sjpk 			if (strcmp(nt, devalloc_list[i]) == 0)
307545916cd2Sjpk 				/*
307645916cd2Sjpk 				 * One of the types recognized by devalloc,
307745916cd2Sjpk 				 * reset attrs.
307845916cd2Sjpk 				 */
30797c478bd9Sstevel@tonic-gate 				break;
30807c478bd9Sstevel@tonic-gate 		}
308145916cd2Sjpk 		if (devalloc_list[i] == NULL)
30827c478bd9Sstevel@tonic-gate 			return;
30837c478bd9Sstevel@tonic-gate 	}
30847c478bd9Sstevel@tonic-gate 
30857c478bd9Sstevel@tonic-gate 	if (file_mods == FALSE) {
30867c478bd9Sstevel@tonic-gate 		/* Nothing more to do if simulating */
30877c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, PERM_MSG, phy_path, uid, gid, mode);
30887c478bd9Sstevel@tonic-gate 		return;
30897c478bd9Sstevel@tonic-gate 	}
30907c478bd9Sstevel@tonic-gate 
30918c206d17Spr 	if ((devalloc_flag == DA_ON) ||
30928c206d17Spr 	    ((devalloc_is_on == 1) && (devalloc_flag != DA_OFF))) {
309345916cd2Sjpk 		/*
30948c206d17Spr 		 * we are here either to turn device allocation on or
30958c206d17Spr 		 * to add a new device while device allocation is on
30968c206d17Spr 		 * (and we've confirmed that we're not turning it
30978c206d17Spr 		 * off).
309845916cd2Sjpk 		 */
309945916cd2Sjpk 		mode = DEALLOC_MODE;
310045916cd2Sjpk 		uid = DA_UID;
310145916cd2Sjpk 		gid = DA_GID;
310245916cd2Sjpk 	}
310345916cd2Sjpk 
310445916cd2Sjpk 	if ((devalloc_is_on == 1) || (devalloc_flag == DA_ON) ||
310545916cd2Sjpk 	    (sb.st_mode != mode)) {
31067c478bd9Sstevel@tonic-gate 		if (chmod(phy_path, mode) == -1)
31077c478bd9Sstevel@tonic-gate 			vprint(VERBOSE_MID, CHMOD_FAILED,
31087c478bd9Sstevel@tonic-gate 			    phy_path, strerror(errno));
31097c478bd9Sstevel@tonic-gate 	}
311045916cd2Sjpk 	if ((devalloc_is_on == 1) || (devalloc_flag == DA_ON) ||
311145916cd2Sjpk 	    (sb.st_uid != uid || sb.st_gid != gid)) {
31127c478bd9Sstevel@tonic-gate 		if (chown(phy_path, uid, gid) == -1)
31137c478bd9Sstevel@tonic-gate 			vprint(VERBOSE_MID, CHOWN_FAILED,
31147c478bd9Sstevel@tonic-gate 			    phy_path, strerror(errno));
31157c478bd9Sstevel@tonic-gate 	}
31167c478bd9Sstevel@tonic-gate 
31177c478bd9Sstevel@tonic-gate 	/* Report that we actually did something */
31187c478bd9Sstevel@tonic-gate 	vprint(VERBOSE_MID, PERM_MSG, phy_path, uid, gid, mode);
31197c478bd9Sstevel@tonic-gate }
31207c478bd9Sstevel@tonic-gate 
31217c478bd9Sstevel@tonic-gate /*
31227c478bd9Sstevel@tonic-gate  * Removes logical link and the minor node it refers to.  If file is a
31237c478bd9Sstevel@tonic-gate  * link, we recurse and try to remove the minor node (or link if path is
31247c478bd9Sstevel@tonic-gate  * a double link) that file's link contents refer to.
31257c478bd9Sstevel@tonic-gate  */
31267c478bd9Sstevel@tonic-gate static void
31277c478bd9Sstevel@tonic-gate devfsadm_rm_work(char *file, int recurse, int file_type)
31287c478bd9Sstevel@tonic-gate {
31297c478bd9Sstevel@tonic-gate 	char *fcn = "devfsadm_rm_work: ";
31307c478bd9Sstevel@tonic-gate 	int linksize;
31317c478bd9Sstevel@tonic-gate 	char contents[PATH_MAX + 1];
31327c478bd9Sstevel@tonic-gate 	char nextfile[PATH_MAX + 1];
31337c478bd9Sstevel@tonic-gate 	char newfile[PATH_MAX + 1];
31347c478bd9Sstevel@tonic-gate 	char *ptr;
31357c478bd9Sstevel@tonic-gate 
31367c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%s%s\n", fcn, file);
31377c478bd9Sstevel@tonic-gate 
313894c894bbSVikram Hegde 	/*
313994c894bbSVikram Hegde 	 * Note: we don't remove /devices (non-links) entries because they are
314094c894bbSVikram Hegde 	 *	covered by devfs.
314194c894bbSVikram Hegde 	 */
314294c894bbSVikram Hegde 	if (file_type != TYPE_LINK) {
314394c894bbSVikram Hegde 		return;
31447c478bd9Sstevel@tonic-gate 	}
31457c478bd9Sstevel@tonic-gate 
314694c894bbSVikram Hegde 	/* split into multiple if's due to excessive indentations */
314794c894bbSVikram Hegde 	(void) strcpy(newfile, dev_dir);
314894c894bbSVikram Hegde 	(void) strcat(newfile, "/");
314994c894bbSVikram Hegde 	(void) strcat(newfile, file);
315094c894bbSVikram Hegde 
315194c894bbSVikram Hegde 	/*
315294c894bbSVikram Hegde 	 * we dont care about the content of the symlink, so
315394c894bbSVikram Hegde 	 * redirection is not needed.
315494c894bbSVikram Hegde 	 */
315594c894bbSVikram Hegde 	if ((recurse == TRUE) &&
31567c478bd9Sstevel@tonic-gate 	    ((linksize = readlink(newfile, contents, PATH_MAX)) > 0)) {
31577c478bd9Sstevel@tonic-gate 		contents[linksize] = '\0';
31587c478bd9Sstevel@tonic-gate 
315994c894bbSVikram Hegde 		/*
316094c894bbSVikram Hegde 		 * recurse if link points to another link
316194c894bbSVikram Hegde 		 */
316294c894bbSVikram Hegde 		if (is_minor_node(contents, &ptr) != DEVFSADM_TRUE) {
31637c478bd9Sstevel@tonic-gate 			if (strncmp(contents, DEV "/", strlen(DEV) + 1) == 0) {
31647c478bd9Sstevel@tonic-gate 				devfsadm_rm_work(&contents[strlen(DEV) + 1],
31650a653502Swroche 				    TRUE, TYPE_LINK);
31667c478bd9Sstevel@tonic-gate 			} else {
31677c478bd9Sstevel@tonic-gate 				if ((ptr = strrchr(file, '/')) != NULL) {
31687c478bd9Sstevel@tonic-gate 					*ptr = '\0';
31697c478bd9Sstevel@tonic-gate 					(void) strcpy(nextfile, file);
31707c478bd9Sstevel@tonic-gate 					*ptr = '/';
31717c478bd9Sstevel@tonic-gate 					(void) strcat(nextfile, "/");
31727c478bd9Sstevel@tonic-gate 				} else {
31737c478bd9Sstevel@tonic-gate 					(void) strcpy(nextfile, "");
31747c478bd9Sstevel@tonic-gate 				}
31757c478bd9Sstevel@tonic-gate 				(void) strcat(nextfile, contents);
31767c478bd9Sstevel@tonic-gate 				devfsadm_rm_work(nextfile, TRUE, TYPE_LINK);
31777c478bd9Sstevel@tonic-gate 			}
31787c478bd9Sstevel@tonic-gate 		}
31797c478bd9Sstevel@tonic-gate 	}
31807c478bd9Sstevel@tonic-gate 
318194c894bbSVikram Hegde 	vprint(VERBOSE_MID, DEVFSADM_UNLINK, newfile);
318294c894bbSVikram Hegde 	if (file_mods == TRUE) {
318394c894bbSVikram Hegde 		rm_link_from_cache(file);
318494c894bbSVikram Hegde 		s_unlink(newfile);
318594c894bbSVikram Hegde 		rm_parent_dir_if_empty(newfile);
318694c894bbSVikram Hegde 		invalidate_enumerate_cache();
318794c894bbSVikram Hegde 		(void) di_devlink_rm_link(devlink_cache, file);
31887c478bd9Sstevel@tonic-gate 	}
31897c478bd9Sstevel@tonic-gate }
31907c478bd9Sstevel@tonic-gate 
31917c478bd9Sstevel@tonic-gate void
31927c478bd9Sstevel@tonic-gate devfsadm_rm_link(char *file)
31937c478bd9Sstevel@tonic-gate {
31947c478bd9Sstevel@tonic-gate 	devfsadm_rm_work(file, FALSE, TYPE_LINK);
31957c478bd9Sstevel@tonic-gate }
31967c478bd9Sstevel@tonic-gate 
31977c478bd9Sstevel@tonic-gate void
31987c478bd9Sstevel@tonic-gate devfsadm_rm_all(char *file)
31997c478bd9Sstevel@tonic-gate {
32007c478bd9Sstevel@tonic-gate 	devfsadm_rm_work(file, TRUE, TYPE_LINK);
32017c478bd9Sstevel@tonic-gate }
32027c478bd9Sstevel@tonic-gate 
32034bc0a2efScasper static int
32047c478bd9Sstevel@tonic-gate s_rmdir(char *path)
32057c478bd9Sstevel@tonic-gate {
32067c478bd9Sstevel@tonic-gate 	int	i;
32077c478bd9Sstevel@tonic-gate 	char	*rpath, *dir;
32087c478bd9Sstevel@tonic-gate 	const char *fcn = "s_rmdir";
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate 	/*
32117c478bd9Sstevel@tonic-gate 	 * Certain directories are created at install time by packages.
321267323fc4SJohn Levon 	 * Some of them (listed in sticky_dirs[]) are required by apps
32137c478bd9Sstevel@tonic-gate 	 * and need to be present even when empty.
32147c478bd9Sstevel@tonic-gate 	 */
321567323fc4SJohn Levon 	vprint(REMOVE_MID, "%s: checking if %s is sticky\n", fcn, path);
32167c478bd9Sstevel@tonic-gate 
32177c478bd9Sstevel@tonic-gate 	rpath = path + strlen(dev_dir) + 1;
32187c478bd9Sstevel@tonic-gate 
321967323fc4SJohn Levon 	for (i = 0; (dir = sticky_dirs[i]) != NULL; i++) {
32207c478bd9Sstevel@tonic-gate 		if (*rpath == *dir) {
32217c478bd9Sstevel@tonic-gate 			if (strcmp(rpath, dir) == 0) {
322267323fc4SJohn Levon 				vprint(REMOVE_MID, "%s: skipping sticky dir: "
32237c478bd9Sstevel@tonic-gate 				    "%s\n", fcn, path);
32244bc0a2efScasper 				errno = EEXIST;
32254bc0a2efScasper 				return (-1);
32267c478bd9Sstevel@tonic-gate 			}
32277c478bd9Sstevel@tonic-gate 		}
32287c478bd9Sstevel@tonic-gate 	}
32297c478bd9Sstevel@tonic-gate 
32304bc0a2efScasper 	return (rmdir(path));
32317c478bd9Sstevel@tonic-gate }
32327c478bd9Sstevel@tonic-gate 
32337c478bd9Sstevel@tonic-gate /*
32347c478bd9Sstevel@tonic-gate  * Try to remove any empty directories up the tree.  It is assumed that
32357c478bd9Sstevel@tonic-gate  * pathname is a file that was removed, so start with its parent, and
32367c478bd9Sstevel@tonic-gate  * work up the tree.
32377c478bd9Sstevel@tonic-gate  */
32387c478bd9Sstevel@tonic-gate static void
32397c478bd9Sstevel@tonic-gate rm_parent_dir_if_empty(char *pathname)
32407c478bd9Sstevel@tonic-gate {
32417c478bd9Sstevel@tonic-gate 	char *ptr, path[PATH_MAX + 1];
32427c478bd9Sstevel@tonic-gate 	char *fcn = "rm_parent_dir_if_empty: ";
32437c478bd9Sstevel@tonic-gate 
32447c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%schecking %s if empty\n", fcn, pathname);
32457c478bd9Sstevel@tonic-gate 
32467c478bd9Sstevel@tonic-gate 	(void) strcpy(path, pathname);
32477c478bd9Sstevel@tonic-gate 
32487c478bd9Sstevel@tonic-gate 	/*
32497c478bd9Sstevel@tonic-gate 	 * ascend up the dir tree, deleting all empty dirs.
32507c478bd9Sstevel@tonic-gate 	 * Return immediately if a dir is not empty.
32517c478bd9Sstevel@tonic-gate 	 */
32527c478bd9Sstevel@tonic-gate 	for (;;) {
32537c478bd9Sstevel@tonic-gate 
32547c478bd9Sstevel@tonic-gate 		if ((ptr = strrchr(path, '/')) == NULL) {
32557c478bd9Sstevel@tonic-gate 			return;
32567c478bd9Sstevel@tonic-gate 		}
32577c478bd9Sstevel@tonic-gate 
32587c478bd9Sstevel@tonic-gate 		*ptr = '\0';
32597c478bd9Sstevel@tonic-gate 
3260e37c6c37Scth 		if (finddev_emptydir(path)) {
3261e37c6c37Scth 			/* directory is empty */
3262facf4a8dSllai 			if (s_rmdir(path) == 0) {
3263facf4a8dSllai 				vprint(REMOVE_MID,
3264facf4a8dSllai 				    "%sremoving empty dir %s\n", fcn, path);
3265facf4a8dSllai 			} else if (errno == EEXIST) {
3266facf4a8dSllai 				vprint(REMOVE_MID,
3267facf4a8dSllai 				    "%sfailed to remove dir: %s\n", fcn, path);
3268facf4a8dSllai 				return;
3269facf4a8dSllai 			}
3270facf4a8dSllai 		} else {
3271facf4a8dSllai 			/* some other file is here, so return */
32727c478bd9Sstevel@tonic-gate 			vprint(REMOVE_MID, "%sdir not empty: %s\n", fcn, path);
32737c478bd9Sstevel@tonic-gate 			return;
32747c478bd9Sstevel@tonic-gate 		}
32757c478bd9Sstevel@tonic-gate 	}
32767c478bd9Sstevel@tonic-gate }
32777c478bd9Sstevel@tonic-gate 
32787c478bd9Sstevel@tonic-gate /*
32797c478bd9Sstevel@tonic-gate  * This function and all the functions it calls below were added to
32807c478bd9Sstevel@tonic-gate  * handle the unique problem with world wide names (WWN).  The problem is
32817c478bd9Sstevel@tonic-gate  * that if a WWN device is moved to another address on the same controller
32827c478bd9Sstevel@tonic-gate  * its logical link will change, while the physical node remains the same.
32837c478bd9Sstevel@tonic-gate  * The result is that two logical links will point to the same physical path
32847c478bd9Sstevel@tonic-gate  * in /devices, the valid link and a stale link. This function will
32857c478bd9Sstevel@tonic-gate  * find all the stale nodes, though at a significant performance cost.
32867c478bd9Sstevel@tonic-gate  *
32877c478bd9Sstevel@tonic-gate  * Caching is used to increase performance.
32887c478bd9Sstevel@tonic-gate  * A cache will be built from disk if the cache tag doesn't already exist.
32897c478bd9Sstevel@tonic-gate  * The cache tag is a regular expression "dir_re", which selects a
32907c478bd9Sstevel@tonic-gate  * subset of disks to search from typically something like
32917c478bd9Sstevel@tonic-gate  * "dev/cXt[0-9]+d[0-9]+s[0-9]+".  After the cache is built, consistency must
32927c478bd9Sstevel@tonic-gate  * be maintained, so entries are added as new links are created, and removed
32937c478bd9Sstevel@tonic-gate  * as old links are deleted.  The whole cache is flushed if we are a daemon,
32947c478bd9Sstevel@tonic-gate  * and another devfsadm process ran in between.
32957c478bd9Sstevel@tonic-gate  *
32967c478bd9Sstevel@tonic-gate  * Once the cache is built, this function finds the cache which matches
32977c478bd9Sstevel@tonic-gate  * dir_re, and then it searches all links in that cache looking for
32987c478bd9Sstevel@tonic-gate  * any link whose contents match "valid_link_contents" with a corresponding link
32997c478bd9Sstevel@tonic-gate  * which does not match "valid_link".  Any such matches are stale and removed.
330094c894bbSVikram Hegde  *
330194c894bbSVikram Hegde  * This happens outside the context of a "reparenting" so we dont need
330294c894bbSVikram Hegde  * redirection.
33037c478bd9Sstevel@tonic-gate  */
33047c478bd9Sstevel@tonic-gate void
33057c478bd9Sstevel@tonic-gate devfsadm_rm_stale_links(char *dir_re, char *valid_link, di_node_t node,
3306406fc510SToomas Soome     di_minor_t minor)
33077c478bd9Sstevel@tonic-gate {
33087c478bd9Sstevel@tonic-gate 	link_t *link;
33097c478bd9Sstevel@tonic-gate 	linkhead_t *head;
33107c478bd9Sstevel@tonic-gate 	char phy_path[PATH_MAX + 1];
33117c478bd9Sstevel@tonic-gate 	char *valid_link_contents;
33127c478bd9Sstevel@tonic-gate 	char *dev_path;
33137c478bd9Sstevel@tonic-gate 	char rmlink[PATH_MAX + 1];
33147c478bd9Sstevel@tonic-gate 
33157c478bd9Sstevel@tonic-gate 	/*
33167c478bd9Sstevel@tonic-gate 	 * try to use devices path
33177c478bd9Sstevel@tonic-gate 	 */
33187c478bd9Sstevel@tonic-gate 	if ((node == lnode) && (minor == lminor)) {
33197c478bd9Sstevel@tonic-gate 		valid_link_contents = lphy_path;
33207c478bd9Sstevel@tonic-gate 	} else {
33217c478bd9Sstevel@tonic-gate 		if ((dev_path = di_devfs_path(node)) == NULL) {
33227c478bd9Sstevel@tonic-gate 			err_print(DI_DEVFS_PATH_FAILED, strerror(errno));
33237c478bd9Sstevel@tonic-gate 			devfsadm_exit(1);
3324537714daSvikram 			/*NOTREACHED*/
33257c478bd9Sstevel@tonic-gate 		}
33267c478bd9Sstevel@tonic-gate 		(void) strcpy(phy_path, dev_path);
33277c478bd9Sstevel@tonic-gate 		di_devfs_path_free(dev_path);
33287c478bd9Sstevel@tonic-gate 
33297c478bd9Sstevel@tonic-gate 		(void) strcat(phy_path, ":");
33307c478bd9Sstevel@tonic-gate 		(void) strcat(phy_path, di_minor_name(minor));
33317c478bd9Sstevel@tonic-gate 		valid_link_contents = phy_path;
33327c478bd9Sstevel@tonic-gate 	}
33337c478bd9Sstevel@tonic-gate 
33347c478bd9Sstevel@tonic-gate 	/*
33357c478bd9Sstevel@tonic-gate 	 * As an optimization, check to make sure the corresponding
33367c478bd9Sstevel@tonic-gate 	 * devlink was just created before continuing.
33377c478bd9Sstevel@tonic-gate 	 */
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate 	if (linknew == FALSE) {
33407c478bd9Sstevel@tonic-gate 		return;
33417c478bd9Sstevel@tonic-gate 	}
33427c478bd9Sstevel@tonic-gate 
33437c478bd9Sstevel@tonic-gate 	head = get_cached_links(dir_re);
33447c478bd9Sstevel@tonic-gate 
33457c478bd9Sstevel@tonic-gate 	assert(head->nextlink == NULL);
33467c478bd9Sstevel@tonic-gate 
33477c478bd9Sstevel@tonic-gate 	for (link = head->link; link != NULL; link = head->nextlink) {
33487c478bd9Sstevel@tonic-gate 		/*
33497c478bd9Sstevel@tonic-gate 		 * See hot_cleanup() for why we do this
33507c478bd9Sstevel@tonic-gate 		 */
33517c478bd9Sstevel@tonic-gate 		head->nextlink = link->next;
33527c478bd9Sstevel@tonic-gate 		if ((strcmp(link->contents, valid_link_contents) == 0) &&
33537c478bd9Sstevel@tonic-gate 		    (strcmp(link->devlink, valid_link) != 0)) {
33547c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "removing %s -> %s\n"
33550a653502Swroche 			    "valid link is: %s -> %s\n",
33560a653502Swroche 			    link->devlink, link->contents,
33570a653502Swroche 			    valid_link, valid_link_contents);
33587c478bd9Sstevel@tonic-gate 			/*
33597c478bd9Sstevel@tonic-gate 			 * Use a copy of the cached link name as the
33607c478bd9Sstevel@tonic-gate 			 * cache entry will go away during link removal
33617c478bd9Sstevel@tonic-gate 			 */
33627c478bd9Sstevel@tonic-gate 			(void) snprintf(rmlink, sizeof (rmlink), "%s",
33637c478bd9Sstevel@tonic-gate 			    link->devlink);
33647c478bd9Sstevel@tonic-gate 			devfsadm_rm_link(rmlink);
33657c478bd9Sstevel@tonic-gate 		}
33667c478bd9Sstevel@tonic-gate 	}
33677c478bd9Sstevel@tonic-gate }
33687c478bd9Sstevel@tonic-gate 
33697c478bd9Sstevel@tonic-gate /*
33707c478bd9Sstevel@tonic-gate  * Return previously created cache, or create cache.
33717c478bd9Sstevel@tonic-gate  */
33727c478bd9Sstevel@tonic-gate static linkhead_t *
33737c478bd9Sstevel@tonic-gate get_cached_links(char *dir_re)
33747c478bd9Sstevel@tonic-gate {
33757c478bd9Sstevel@tonic-gate 	recurse_dev_t rd;
33767c478bd9Sstevel@tonic-gate 	linkhead_t *linkhead;
33777c478bd9Sstevel@tonic-gate 	int n;
33787c478bd9Sstevel@tonic-gate 
33797c478bd9Sstevel@tonic-gate 	vprint(BUILDCACHE_MID, "get_cached_links: %s\n", dir_re);
33807c478bd9Sstevel@tonic-gate 
33817c478bd9Sstevel@tonic-gate 	for (linkhead = headlinkhead; linkhead != NULL;
33820a653502Swroche 	    linkhead = linkhead->nexthead) {
33837c478bd9Sstevel@tonic-gate 		if (strcmp(linkhead->dir_re, dir_re) == 0) {
33847c478bd9Sstevel@tonic-gate 			return (linkhead);
33857c478bd9Sstevel@tonic-gate 		}
33867c478bd9Sstevel@tonic-gate 	}
33877c478bd9Sstevel@tonic-gate 
33887c478bd9Sstevel@tonic-gate 	/*
33897c478bd9Sstevel@tonic-gate 	 * This tag is not in cache, so add it, along with all its
33907c478bd9Sstevel@tonic-gate 	 * matching /dev entries.  This is the only time we go to disk.
33917c478bd9Sstevel@tonic-gate 	 */
33927c478bd9Sstevel@tonic-gate 	linkhead = s_malloc(sizeof (linkhead_t));
33937c478bd9Sstevel@tonic-gate 	linkhead->nexthead = headlinkhead;
33947c478bd9Sstevel@tonic-gate 	headlinkhead = linkhead;
33957c478bd9Sstevel@tonic-gate 	linkhead->dir_re = s_strdup(dir_re);
33967c478bd9Sstevel@tonic-gate 
33977c478bd9Sstevel@tonic-gate 	if ((n = regcomp(&(linkhead->dir_re_compiled), dir_re,
33980a653502Swroche 	    REG_EXTENDED)) != 0) {
33997c478bd9Sstevel@tonic-gate 		err_print(REGCOMP_FAILED,  dir_re, n);
34007c478bd9Sstevel@tonic-gate 	}
34017c478bd9Sstevel@tonic-gate 
34027c478bd9Sstevel@tonic-gate 	linkhead->nextlink = NULL;
34037c478bd9Sstevel@tonic-gate 	linkhead->link = NULL;
34047c478bd9Sstevel@tonic-gate 
34057c478bd9Sstevel@tonic-gate 	rd.fcn = build_devlink_list;
34067c478bd9Sstevel@tonic-gate 	rd.data = (void *)linkhead;
34077c478bd9Sstevel@tonic-gate 
34087c478bd9Sstevel@tonic-gate 	vprint(BUILDCACHE_MID, "get_cached_links: calling recurse_dev_re\n");
34097c478bd9Sstevel@tonic-gate 
34107c478bd9Sstevel@tonic-gate 	/* call build_devlink_list for each directory in the dir_re RE */
34117c478bd9Sstevel@tonic-gate 	if (dir_re[0] == '/') {
34127c478bd9Sstevel@tonic-gate 		recurse_dev_re("/", &dir_re[1], &rd);
34137c478bd9Sstevel@tonic-gate 	} else {
34147c478bd9Sstevel@tonic-gate 		recurse_dev_re(dev_dir, dir_re, &rd);
34157c478bd9Sstevel@tonic-gate 	}
34167c478bd9Sstevel@tonic-gate 
34177c478bd9Sstevel@tonic-gate 	return (linkhead);
34187c478bd9Sstevel@tonic-gate }
34197c478bd9Sstevel@tonic-gate 
34207c478bd9Sstevel@tonic-gate static void
34217c478bd9Sstevel@tonic-gate build_devlink_list(char *devlink, void *data)
34227c478bd9Sstevel@tonic-gate {
34237c478bd9Sstevel@tonic-gate 	char *fcn = "build_devlink_list: ";
34247c478bd9Sstevel@tonic-gate 	char *ptr;
34257c478bd9Sstevel@tonic-gate 	char *r_contents;
34267c478bd9Sstevel@tonic-gate 	char *r_devlink;
34277c478bd9Sstevel@tonic-gate 	char contents[PATH_MAX + 1];
34287c478bd9Sstevel@tonic-gate 	char newlink[PATH_MAX + 1];
34297c478bd9Sstevel@tonic-gate 	char stage_link[PATH_MAX + 1];
34307c478bd9Sstevel@tonic-gate 	int linksize;
34317c478bd9Sstevel@tonic-gate 	linkhead_t *linkhead = (linkhead_t *)data;
34327c478bd9Sstevel@tonic-gate 	link_t *link;
34337c478bd9Sstevel@tonic-gate 	int i = 0;
34347c478bd9Sstevel@tonic-gate 
34357c478bd9Sstevel@tonic-gate 	vprint(BUILDCACHE_MID, "%scheck_link: %s\n", fcn, devlink);
34367c478bd9Sstevel@tonic-gate 
34377c478bd9Sstevel@tonic-gate 	(void) strcpy(newlink, devlink);
34387c478bd9Sstevel@tonic-gate 
34397c478bd9Sstevel@tonic-gate 	do {
344094c894bbSVikram Hegde 		/*
344194c894bbSVikram Hegde 		 * None of the consumers of this function need redirection
344294c894bbSVikram Hegde 		 * so this readlink gets the "current" contents
344394c894bbSVikram Hegde 		 */
34447c478bd9Sstevel@tonic-gate 		linksize = readlink(newlink, contents, PATH_MAX);
34457c478bd9Sstevel@tonic-gate 		if (linksize <= 0) {
34467c478bd9Sstevel@tonic-gate 			/*
34477c478bd9Sstevel@tonic-gate 			 * The first pass through the do loop we may readlink()
34487c478bd9Sstevel@tonic-gate 			 * non-symlink files(EINVAL) from false regexec matches.
34497c478bd9Sstevel@tonic-gate 			 * Suppress error messages in those cases or if the link
34507c478bd9Sstevel@tonic-gate 			 * content is the empty string.
34517c478bd9Sstevel@tonic-gate 			 */
34527c478bd9Sstevel@tonic-gate 			if (linksize < 0 && (i || errno != EINVAL))
34537c478bd9Sstevel@tonic-gate 				err_print(READLINK_FAILED, "build_devlink_list",
34547c478bd9Sstevel@tonic-gate 				    newlink, strerror(errno));
34557c478bd9Sstevel@tonic-gate 			return;
34567c478bd9Sstevel@tonic-gate 		}
34577c478bd9Sstevel@tonic-gate 		contents[linksize] = '\0';
34587c478bd9Sstevel@tonic-gate 		i = 1;
34597c478bd9Sstevel@tonic-gate 
34607c478bd9Sstevel@tonic-gate 		if (is_minor_node(contents, &r_contents) == DEVFSADM_FALSE) {
34617c478bd9Sstevel@tonic-gate 			/*
34627c478bd9Sstevel@tonic-gate 			 * assume that link contents is really a pointer to
34637c478bd9Sstevel@tonic-gate 			 * another link, so recurse and read its link contents.
34647c478bd9Sstevel@tonic-gate 			 *
34657c478bd9Sstevel@tonic-gate 			 * some link contents are absolute:
34667c478bd9Sstevel@tonic-gate 			 *	/dev/audio -> /dev/sound/0
34677c478bd9Sstevel@tonic-gate 			 */
34687c478bd9Sstevel@tonic-gate 			if (strncmp(contents, DEV "/",
34690a653502Swroche 			    strlen(DEV) + strlen("/")) != 0) {
34707c478bd9Sstevel@tonic-gate 
34717c478bd9Sstevel@tonic-gate 				if ((ptr = strrchr(newlink, '/')) == NULL) {
34727c478bd9Sstevel@tonic-gate 					vprint(REMOVE_MID, "%s%s -> %s invalid "
34730a653502Swroche 					    "link. missing '/'\n", fcn,
34740a653502Swroche 					    newlink, contents);
34750a653502Swroche 					return;
34767c478bd9Sstevel@tonic-gate 				}
34777c478bd9Sstevel@tonic-gate 				*ptr = '\0';
34787c478bd9Sstevel@tonic-gate 				(void) strcpy(stage_link, newlink);
34797c478bd9Sstevel@tonic-gate 				*ptr = '/';
34807c478bd9Sstevel@tonic-gate 				(void) strcat(stage_link, "/");
34817c478bd9Sstevel@tonic-gate 				(void) strcat(stage_link, contents);
34827c478bd9Sstevel@tonic-gate 				(void) strcpy(newlink, stage_link);
34837c478bd9Sstevel@tonic-gate 			} else {
34847c478bd9Sstevel@tonic-gate 				(void) strcpy(newlink, dev_dir);
34857c478bd9Sstevel@tonic-gate 				(void) strcat(newlink, "/");
34867c478bd9Sstevel@tonic-gate 				(void) strcat(newlink,
34870a653502Swroche 				    &contents[strlen(DEV) + strlen("/")]);
34887c478bd9Sstevel@tonic-gate 			}
34897c478bd9Sstevel@tonic-gate 
34907c478bd9Sstevel@tonic-gate 		} else {
34917c478bd9Sstevel@tonic-gate 			newlink[0] = '\0';
34927c478bd9Sstevel@tonic-gate 		}
34937c478bd9Sstevel@tonic-gate 	} while (newlink[0] != '\0');
34947c478bd9Sstevel@tonic-gate 
34957c478bd9Sstevel@tonic-gate 	if (strncmp(devlink, dev_dir, strlen(dev_dir)) != 0) {
34967c478bd9Sstevel@tonic-gate 		vprint(BUILDCACHE_MID, "%sinvalid link: %s\n", fcn, devlink);
34977c478bd9Sstevel@tonic-gate 		return;
34987c478bd9Sstevel@tonic-gate 	}
34997c478bd9Sstevel@tonic-gate 
35007c478bd9Sstevel@tonic-gate 	r_devlink = devlink + strlen(dev_dir);
35017c478bd9Sstevel@tonic-gate 
35027c478bd9Sstevel@tonic-gate 	if (r_devlink[0] != '/')
35037c478bd9Sstevel@tonic-gate 		return;
35047c478bd9Sstevel@tonic-gate 
35057c478bd9Sstevel@tonic-gate 	link = s_malloc(sizeof (link_t));
35067c478bd9Sstevel@tonic-gate 
35077c478bd9Sstevel@tonic-gate 	/* don't store the '/' after rootdir/dev */
35087c478bd9Sstevel@tonic-gate 	r_devlink += 1;
35097c478bd9Sstevel@tonic-gate 
35107c478bd9Sstevel@tonic-gate 	vprint(BUILDCACHE_MID, "%scaching link: %s\n", fcn, r_devlink);
35117c478bd9Sstevel@tonic-gate 	link->devlink = s_strdup(r_devlink);
35127c478bd9Sstevel@tonic-gate 
35137c478bd9Sstevel@tonic-gate 	link->contents = s_strdup(r_contents);
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate 	link->next = linkhead->link;
35167c478bd9Sstevel@tonic-gate 	linkhead->link = link;
35177c478bd9Sstevel@tonic-gate }
35187c478bd9Sstevel@tonic-gate 
35197c478bd9Sstevel@tonic-gate /*
35207c478bd9Sstevel@tonic-gate  * to be consistent, devlink must not begin with / and must be
35217c478bd9Sstevel@tonic-gate  * relative to /dev/, whereas physpath must contain / and be
35227c478bd9Sstevel@tonic-gate  * relative to /devices.
35237c478bd9Sstevel@tonic-gate  */
35247c478bd9Sstevel@tonic-gate static void
35257c478bd9Sstevel@tonic-gate add_link_to_cache(char *devlink, char *physpath)
35267c478bd9Sstevel@tonic-gate {
35277c478bd9Sstevel@tonic-gate 	linkhead_t *linkhead;
35287c478bd9Sstevel@tonic-gate 	link_t *link;
35297c478bd9Sstevel@tonic-gate 	int added = 0;
35307c478bd9Sstevel@tonic-gate 
35317c478bd9Sstevel@tonic-gate 	if (file_mods == FALSE) {
35327c478bd9Sstevel@tonic-gate 		return;
35337c478bd9Sstevel@tonic-gate 	}
35347c478bd9Sstevel@tonic-gate 
35357c478bd9Sstevel@tonic-gate 	vprint(CACHE_MID, "add_link_to_cache: %s -> %s ",
35360a653502Swroche 	    devlink, physpath);
35377c478bd9Sstevel@tonic-gate 
35387c478bd9Sstevel@tonic-gate 	for (linkhead = headlinkhead; linkhead != NULL;
35390a653502Swroche 	    linkhead = linkhead->nexthead) {
35400a653502Swroche 		if (regexec(&(linkhead->dir_re_compiled), devlink, 0, NULL, 0)
35410a653502Swroche 		    == 0) {
35427c478bd9Sstevel@tonic-gate 			added++;
35437c478bd9Sstevel@tonic-gate 			link = s_malloc(sizeof (link_t));
35447c478bd9Sstevel@tonic-gate 			link->devlink = s_strdup(devlink);
35457c478bd9Sstevel@tonic-gate 			link->contents = s_strdup(physpath);
35467c478bd9Sstevel@tonic-gate 			link->next = linkhead->link;
35477c478bd9Sstevel@tonic-gate 			linkhead->link = link;
35487c478bd9Sstevel@tonic-gate 		}
35497c478bd9Sstevel@tonic-gate 	}
35507c478bd9Sstevel@tonic-gate 
35517c478bd9Sstevel@tonic-gate 	vprint(CACHE_MID,
35520a653502Swroche 	    " %d %s\n", added, added == 0 ? "NOT ADDED" : "ADDED");
35537c478bd9Sstevel@tonic-gate }
35547c478bd9Sstevel@tonic-gate 
35557c478bd9Sstevel@tonic-gate /*
35567c478bd9Sstevel@tonic-gate  * Remove devlink from cache.  Devlink must be relative to /dev/ and not start
35577c478bd9Sstevel@tonic-gate  * with /.
35587c478bd9Sstevel@tonic-gate  */
35597c478bd9Sstevel@tonic-gate static void
35607c478bd9Sstevel@tonic-gate rm_link_from_cache(char *devlink)
35617c478bd9Sstevel@tonic-gate {
35627c478bd9Sstevel@tonic-gate 	linkhead_t *linkhead;
35637c478bd9Sstevel@tonic-gate 	link_t **linkp;
35647c478bd9Sstevel@tonic-gate 	link_t *save;
35657c478bd9Sstevel@tonic-gate 
35667c478bd9Sstevel@tonic-gate 	vprint(CACHE_MID, "rm_link_from_cache enter: %s\n", devlink);
35677c478bd9Sstevel@tonic-gate 
35687c478bd9Sstevel@tonic-gate 	for (linkhead = headlinkhead; linkhead != NULL;
35697c478bd9Sstevel@tonic-gate 	    linkhead = linkhead->nexthead) {
35700a653502Swroche 		if (regexec(&(linkhead->dir_re_compiled), devlink, 0, NULL, 0)
35710a653502Swroche 		    == 0) {
35727c478bd9Sstevel@tonic-gate 
35737c478bd9Sstevel@tonic-gate 			for (linkp = &(linkhead->link); *linkp != NULL; ) {
35747c478bd9Sstevel@tonic-gate 				if ((strcmp((*linkp)->devlink, devlink) == 0)) {
35757c478bd9Sstevel@tonic-gate 					save = *linkp;
35767c478bd9Sstevel@tonic-gate 					*linkp = (*linkp)->next;
35777c478bd9Sstevel@tonic-gate 					/*
35787c478bd9Sstevel@tonic-gate 					 * We are removing our caller's
35797c478bd9Sstevel@tonic-gate 					 * "next" link. Update the nextlink
35807c478bd9Sstevel@tonic-gate 					 * field in the head so that our
35817c478bd9Sstevel@tonic-gate 					 * callers accesses the next valid
35827c478bd9Sstevel@tonic-gate 					 * link
35837c478bd9Sstevel@tonic-gate 					 */
35847c478bd9Sstevel@tonic-gate 					if (linkhead->nextlink == save)
35857c478bd9Sstevel@tonic-gate 						linkhead->nextlink = *linkp;
35867c478bd9Sstevel@tonic-gate 					free(save->devlink);
35877c478bd9Sstevel@tonic-gate 					free(save->contents);
35887c478bd9Sstevel@tonic-gate 					free(save);
35897c478bd9Sstevel@tonic-gate 					vprint(CACHE_MID, " %s FREED FROM "
35900a653502Swroche 					    "CACHE\n", devlink);
35917c478bd9Sstevel@tonic-gate 				} else {
35927c478bd9Sstevel@tonic-gate 					linkp = &((*linkp)->next);
35937c478bd9Sstevel@tonic-gate 				}
35947c478bd9Sstevel@tonic-gate 			}
35957c478bd9Sstevel@tonic-gate 		}
35967c478bd9Sstevel@tonic-gate 	}
35977c478bd9Sstevel@tonic-gate }
35987c478bd9Sstevel@tonic-gate 
35997c478bd9Sstevel@tonic-gate static void
36007c478bd9Sstevel@tonic-gate rm_all_links_from_cache()
36017c478bd9Sstevel@tonic-gate {
36027c478bd9Sstevel@tonic-gate 	linkhead_t *linkhead;
36037c478bd9Sstevel@tonic-gate 	linkhead_t *nextlinkhead;
36047c478bd9Sstevel@tonic-gate 	link_t *link;
36057c478bd9Sstevel@tonic-gate 	link_t *nextlink;
36067c478bd9Sstevel@tonic-gate 
36077c478bd9Sstevel@tonic-gate 	vprint(CACHE_MID, "rm_all_links_from_cache\n");
36087c478bd9Sstevel@tonic-gate 
36097c478bd9Sstevel@tonic-gate 	for (linkhead = headlinkhead; linkhead != NULL;
36100a653502Swroche 	    linkhead = nextlinkhead) {
36117c478bd9Sstevel@tonic-gate 
36127c478bd9Sstevel@tonic-gate 		nextlinkhead = linkhead->nexthead;
36137c478bd9Sstevel@tonic-gate 		assert(linkhead->nextlink == NULL);
36147c478bd9Sstevel@tonic-gate 		for (link = linkhead->link; link != NULL; link = nextlink) {
36157c478bd9Sstevel@tonic-gate 			nextlink = link->next;
36167c478bd9Sstevel@tonic-gate 			free(link->devlink);
36177c478bd9Sstevel@tonic-gate 			free(link->contents);
36187c478bd9Sstevel@tonic-gate 			free(link);
36197c478bd9Sstevel@tonic-gate 		}
36207c478bd9Sstevel@tonic-gate 		regfree(&(linkhead->dir_re_compiled));
36217c478bd9Sstevel@tonic-gate 		free(linkhead->dir_re);
36227c478bd9Sstevel@tonic-gate 		free(linkhead);
36237c478bd9Sstevel@tonic-gate 	}
36247c478bd9Sstevel@tonic-gate 	headlinkhead = NULL;
36257c478bd9Sstevel@tonic-gate }
36267c478bd9Sstevel@tonic-gate 
36277c478bd9Sstevel@tonic-gate /*
36287c478bd9Sstevel@tonic-gate  * Called when the kernel has modified the incore path_to_inst data.  This
36297c478bd9Sstevel@tonic-gate  * function will schedule a flush of the data to the filesystem.
36307c478bd9Sstevel@tonic-gate  */
36317c478bd9Sstevel@tonic-gate static void
36327c478bd9Sstevel@tonic-gate devfs_instance_mod(void)
36337c478bd9Sstevel@tonic-gate {
36347c478bd9Sstevel@tonic-gate 	char *fcn = "devfs_instance_mod: ";
36357c478bd9Sstevel@tonic-gate 	vprint(PATH2INST_MID, "%senter\n", fcn);
36367c478bd9Sstevel@tonic-gate 
36377c478bd9Sstevel@tonic-gate 	/* signal instance thread */
36387c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&count_lock);
36397c478bd9Sstevel@tonic-gate 	inst_count++;
36407c478bd9Sstevel@tonic-gate 	(void) cond_signal(&cv);
36417c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&count_lock);
36427c478bd9Sstevel@tonic-gate }
36437c478bd9Sstevel@tonic-gate 
36447c478bd9Sstevel@tonic-gate static void
36457c478bd9Sstevel@tonic-gate instance_flush_thread(void)
36467c478bd9Sstevel@tonic-gate {
36477c478bd9Sstevel@tonic-gate 	int i;
36487c478bd9Sstevel@tonic-gate 	int idle;
36497c478bd9Sstevel@tonic-gate 
36507c478bd9Sstevel@tonic-gate 	for (;;) {
36517c478bd9Sstevel@tonic-gate 
36527c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&count_lock);
36537c478bd9Sstevel@tonic-gate 		while (inst_count == 0) {
36547c478bd9Sstevel@tonic-gate 			(void) cond_wait(&cv, &count_lock);
36557c478bd9Sstevel@tonic-gate 		}
36567c478bd9Sstevel@tonic-gate 		inst_count = 0;
36577c478bd9Sstevel@tonic-gate 
36587c478bd9Sstevel@tonic-gate 		vprint(PATH2INST_MID, "signaled to flush path_to_inst."
36590a653502Swroche 		    " Enter delay loop\n");
36607c478bd9Sstevel@tonic-gate 		/*
36617c478bd9Sstevel@tonic-gate 		 * Wait MAX_IDLE_DELAY seconds after getting the last flush
36627c478bd9Sstevel@tonic-gate 		 * path_to_inst event before invoking a flush, but never wait
36637c478bd9Sstevel@tonic-gate 		 * more than MAX_DELAY seconds after getting the first event.
36647c478bd9Sstevel@tonic-gate 		 */
36657c478bd9Sstevel@tonic-gate 		for (idle = 0, i = 0; i < MAX_DELAY; i++) {
36667c478bd9Sstevel@tonic-gate 
36677c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&count_lock);
36687c478bd9Sstevel@tonic-gate 			(void) sleep(1);
36697c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&count_lock);
36707c478bd9Sstevel@tonic-gate 
36717c478bd9Sstevel@tonic-gate 			/* shorten the delay if we are idle */
36727c478bd9Sstevel@tonic-gate 			if (inst_count == 0) {
36737c478bd9Sstevel@tonic-gate 				idle++;
36747c478bd9Sstevel@tonic-gate 				if (idle > MAX_IDLE_DELAY) {
36757c478bd9Sstevel@tonic-gate 					break;
36767c478bd9Sstevel@tonic-gate 				}
36777c478bd9Sstevel@tonic-gate 			} else {
36787c478bd9Sstevel@tonic-gate 				inst_count = idle = 0;
36797c478bd9Sstevel@tonic-gate 			}
36807c478bd9Sstevel@tonic-gate 		}
36817c478bd9Sstevel@tonic-gate 
36827c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&count_lock);
36837c478bd9Sstevel@tonic-gate 
36847c478bd9Sstevel@tonic-gate 		flush_path_to_inst();
36857c478bd9Sstevel@tonic-gate 	}
36867c478bd9Sstevel@tonic-gate }
36877c478bd9Sstevel@tonic-gate 
36887c478bd9Sstevel@tonic-gate /*
36897c478bd9Sstevel@tonic-gate  * Helper function for flush_path_to_inst() below; this routine calls the
36907c478bd9Sstevel@tonic-gate  * inst_sync syscall to flush the path_to_inst database to the given file.
36917c478bd9Sstevel@tonic-gate  */
36927c478bd9Sstevel@tonic-gate static int
36930a653502Swroche do_inst_sync(char *filename, char *instfilename)
36947c478bd9Sstevel@tonic-gate {
36957c478bd9Sstevel@tonic-gate 	void (*sigsaved)(int);
36960a653502Swroche 	int err = 0, flags = INST_SYNC_IF_REQUIRED;
36970a653502Swroche 	struct stat sb;
36980a653502Swroche 
36990a653502Swroche 	if (stat(instfilename, &sb) == -1 && errno == ENOENT)
37000a653502Swroche 		flags = INST_SYNC_ALWAYS;
37017c478bd9Sstevel@tonic-gate 
37027c478bd9Sstevel@tonic-gate 	vprint(INSTSYNC_MID, "do_inst_sync: about to flush %s\n", filename);
37037c478bd9Sstevel@tonic-gate 	sigsaved = sigset(SIGSYS, SIG_IGN);
37040a653502Swroche 	if (inst_sync(filename, flags) == -1)
37057c478bd9Sstevel@tonic-gate 		err = errno;
37067c478bd9Sstevel@tonic-gate 	(void) sigset(SIGSYS, sigsaved);
37077c478bd9Sstevel@tonic-gate 
37087c478bd9Sstevel@tonic-gate 	switch (err) {
37097c478bd9Sstevel@tonic-gate 	case 0:
37107c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
37117c478bd9Sstevel@tonic-gate 	case EALREADY:	/* no-op, path_to_inst already up to date */
37127c478bd9Sstevel@tonic-gate 		return (EALREADY);
37137c478bd9Sstevel@tonic-gate 	case ENOSYS:
37147c478bd9Sstevel@tonic-gate 		err_print(CANT_LOAD_SYSCALL);
37157c478bd9Sstevel@tonic-gate 		break;
37167c478bd9Sstevel@tonic-gate 	case EPERM:
37177c478bd9Sstevel@tonic-gate 		err_print(SUPER_TO_SYNC);
37187c478bd9Sstevel@tonic-gate 		break;
37197c478bd9Sstevel@tonic-gate 	default:
37207c478bd9Sstevel@tonic-gate 		err_print(INSTSYNC_FAILED, filename, strerror(err));
37217c478bd9Sstevel@tonic-gate 		break;
37227c478bd9Sstevel@tonic-gate 	}
37237c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
37247c478bd9Sstevel@tonic-gate }
37257c478bd9Sstevel@tonic-gate 
37267c478bd9Sstevel@tonic-gate /*
37277c478bd9Sstevel@tonic-gate  * Flush the kernel's path_to_inst database to /etc/path_to_inst.  To do so
37287c478bd9Sstevel@tonic-gate  * safely, the database is flushed to a temporary file, then moved into place.
37297c478bd9Sstevel@tonic-gate  *
37307c478bd9Sstevel@tonic-gate  * The following files are used during this process:
3731*f2dbfd32SRobert Mustacchi  *	/etc/path_to_inst:	The path_to_inst file
3732*f2dbfd32SRobert Mustacchi  *	/etc/path_to_inst.<pid>: Contains data flushed from the kernel
3733*f2dbfd32SRobert Mustacchi  *	/etc/path_to_inst.old:  The backup file
3734*f2dbfd32SRobert Mustacchi  *	/etc/path_to_inst.old.<pid>: Temp file for creating backup
37357c478bd9Sstevel@tonic-gate  *
37367c478bd9Sstevel@tonic-gate  */
37377c478bd9Sstevel@tonic-gate static void
37387c478bd9Sstevel@tonic-gate flush_path_to_inst(void)
37397c478bd9Sstevel@tonic-gate {
37407c478bd9Sstevel@tonic-gate 	char *new_inst_file = NULL;
37417c478bd9Sstevel@tonic-gate 	char *old_inst_file = NULL;
37427c478bd9Sstevel@tonic-gate 	char *old_inst_file_npid = NULL;
37437c478bd9Sstevel@tonic-gate 	FILE *inst_file_fp = NULL;
37447c478bd9Sstevel@tonic-gate 	FILE *old_inst_file_fp = NULL;
37457c478bd9Sstevel@tonic-gate 	struct stat sb;
37467c478bd9Sstevel@tonic-gate 	int err = 0;
37477c478bd9Sstevel@tonic-gate 	int c;
37487c478bd9Sstevel@tonic-gate 	int inst_strlen;
37497c478bd9Sstevel@tonic-gate 
37507c478bd9Sstevel@tonic-gate 	vprint(PATH2INST_MID, "flush_path_to_inst: %s\n",
37517c478bd9Sstevel@tonic-gate 	    (flush_path_to_inst_enable == TRUE) ? "ENABLED" : "DISABLED");
37527c478bd9Sstevel@tonic-gate 
37537c478bd9Sstevel@tonic-gate 	if (flush_path_to_inst_enable == FALSE) {
37547c478bd9Sstevel@tonic-gate 		return;
37557c478bd9Sstevel@tonic-gate 	}
37567c478bd9Sstevel@tonic-gate 
37577c478bd9Sstevel@tonic-gate 	inst_strlen = strlen(inst_file);
37587c478bd9Sstevel@tonic-gate 	new_inst_file = s_malloc(inst_strlen + PID_STR_LEN + 2);
37597c478bd9Sstevel@tonic-gate 	old_inst_file = s_malloc(inst_strlen + PID_STR_LEN + 6);
37607c478bd9Sstevel@tonic-gate 	old_inst_file_npid = s_malloc(inst_strlen +
37617c478bd9Sstevel@tonic-gate 	    sizeof (INSTANCE_FILE_SUFFIX));
37627c478bd9Sstevel@tonic-gate 
37637c478bd9Sstevel@tonic-gate 	(void) snprintf(new_inst_file, inst_strlen + PID_STR_LEN + 2,
37640a653502Swroche 	    "%s.%ld", inst_file, getpid());
37657c478bd9Sstevel@tonic-gate 
37667c478bd9Sstevel@tonic-gate 	if (stat(new_inst_file, &sb) == 0) {
37677c478bd9Sstevel@tonic-gate 		s_unlink(new_inst_file);
37687c478bd9Sstevel@tonic-gate 	}
37697c478bd9Sstevel@tonic-gate 
37700a653502Swroche 	err = do_inst_sync(new_inst_file, inst_file);
37710a653502Swroche 	if (err != DEVFSADM_SUCCESS) {
37727c478bd9Sstevel@tonic-gate 		goto out;
37737c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
37747c478bd9Sstevel@tonic-gate 	}
37757c478bd9Sstevel@tonic-gate 
37767c478bd9Sstevel@tonic-gate 	/*
37777c478bd9Sstevel@tonic-gate 	 * Now we deal with the somewhat tricky updating and renaming
37787c478bd9Sstevel@tonic-gate 	 * of this critical piece of kernel state.
37797c478bd9Sstevel@tonic-gate 	 */
37807c478bd9Sstevel@tonic-gate 
37817c478bd9Sstevel@tonic-gate 	/*
37827c478bd9Sstevel@tonic-gate 	 * Copy the current instance file into a temporary file.
37837c478bd9Sstevel@tonic-gate 	 * Then rename the temporary file into the backup (.old)
37847c478bd9Sstevel@tonic-gate 	 * file and rename the newly flushed kernel data into
37857c478bd9Sstevel@tonic-gate 	 * the instance file.
37867c478bd9Sstevel@tonic-gate 	 * Of course if 'inst_file' doesn't exist, there's much
37877c478bd9Sstevel@tonic-gate 	 * less for us to do .. tee hee.
37887c478bd9Sstevel@tonic-gate 	 */
37897c478bd9Sstevel@tonic-gate 	if ((inst_file_fp = fopen(inst_file, "r")) == NULL) {
37907c478bd9Sstevel@tonic-gate 		/*
37917c478bd9Sstevel@tonic-gate 		 * No such file.  Rename the new onto the old
37927c478bd9Sstevel@tonic-gate 		 */
37937c478bd9Sstevel@tonic-gate 		if ((err = rename(new_inst_file, inst_file)) != 0)
37947c478bd9Sstevel@tonic-gate 			err_print(RENAME_FAILED, inst_file, strerror(errno));
37957c478bd9Sstevel@tonic-gate 		goto out;
37967c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
37977c478bd9Sstevel@tonic-gate 	}
37987c478bd9Sstevel@tonic-gate 
37997c478bd9Sstevel@tonic-gate 	(void) snprintf(old_inst_file, inst_strlen + PID_STR_LEN + 6,
38000a653502Swroche 	    "%s.old.%ld", inst_file, getpid());
38017c478bd9Sstevel@tonic-gate 
38027c478bd9Sstevel@tonic-gate 	if (stat(old_inst_file, &sb) == 0) {
38037c478bd9Sstevel@tonic-gate 		s_unlink(old_inst_file);
38047c478bd9Sstevel@tonic-gate 	}
38057c478bd9Sstevel@tonic-gate 
38067c478bd9Sstevel@tonic-gate 	if ((old_inst_file_fp = fopen(old_inst_file, "w")) == NULL) {
38077c478bd9Sstevel@tonic-gate 		/*
38087c478bd9Sstevel@tonic-gate 		 * Can't open the 'old_inst_file' file for writing.
38097c478bd9Sstevel@tonic-gate 		 * This is somewhat strange given that the syscall
38107c478bd9Sstevel@tonic-gate 		 * just succeeded to write a file out.. hmm.. maybe
38117c478bd9Sstevel@tonic-gate 		 * the fs just filled up or something nasty.
38127c478bd9Sstevel@tonic-gate 		 *
38137c478bd9Sstevel@tonic-gate 		 * Anyway, abort what we've done so far.
38147c478bd9Sstevel@tonic-gate 		 */
38157c478bd9Sstevel@tonic-gate 		err_print(CANT_UPDATE, old_inst_file);
38167c478bd9Sstevel@tonic-gate 		err = DEVFSADM_FAILURE;
38177c478bd9Sstevel@tonic-gate 		goto out;
38187c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
38197c478bd9Sstevel@tonic-gate 	}
38207c478bd9Sstevel@tonic-gate 
38217c478bd9Sstevel@tonic-gate 	/*
38227c478bd9Sstevel@tonic-gate 	 * Copy current instance file into the temporary file
38237c478bd9Sstevel@tonic-gate 	 */
38247c478bd9Sstevel@tonic-gate 	err = 0;
38257c478bd9Sstevel@tonic-gate 	while ((c = getc(inst_file_fp)) != EOF) {
38267c478bd9Sstevel@tonic-gate 		if ((err = putc(c, old_inst_file_fp)) == EOF) {
38277c478bd9Sstevel@tonic-gate 			break;
38287c478bd9Sstevel@tonic-gate 		}
38297c478bd9Sstevel@tonic-gate 	}
38307c478bd9Sstevel@tonic-gate 
38317c478bd9Sstevel@tonic-gate 	if (fclose(old_inst_file_fp) == EOF || err == EOF) {
38327c478bd9Sstevel@tonic-gate 		vprint(INFO_MID, CANT_UPDATE, old_inst_file);
38337c478bd9Sstevel@tonic-gate 		err = DEVFSADM_FAILURE;
38347c478bd9Sstevel@tonic-gate 		goto out;
38357c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
38367c478bd9Sstevel@tonic-gate 	}
38377c478bd9Sstevel@tonic-gate 
38387c478bd9Sstevel@tonic-gate 	/*
38397c478bd9Sstevel@tonic-gate 	 * Set permissions to be the same on the backup as
38407c478bd9Sstevel@tonic-gate 	 * /etc/path_to_inst.
38417c478bd9Sstevel@tonic-gate 	 */
38427c478bd9Sstevel@tonic-gate 	(void) chmod(old_inst_file, 0444);
38437c478bd9Sstevel@tonic-gate 
38447c478bd9Sstevel@tonic-gate 	/*
38457c478bd9Sstevel@tonic-gate 	 * So far, everything we've done is more or less reversible.
38467c478bd9Sstevel@tonic-gate 	 * But now we're going to commit ourselves.
38477c478bd9Sstevel@tonic-gate 	 */
38487c478bd9Sstevel@tonic-gate 
38497c478bd9Sstevel@tonic-gate 	(void) snprintf(old_inst_file_npid,
38507c478bd9Sstevel@tonic-gate 	    inst_strlen + sizeof (INSTANCE_FILE_SUFFIX),
38517c478bd9Sstevel@tonic-gate 	    "%s%s", inst_file, INSTANCE_FILE_SUFFIX);
38527c478bd9Sstevel@tonic-gate 
38537c478bd9Sstevel@tonic-gate 	if ((err = rename(old_inst_file, old_inst_file_npid)) != 0) {
38547c478bd9Sstevel@tonic-gate 		err_print(RENAME_FAILED, old_inst_file_npid,
38550a653502Swroche 		    strerror(errno));
38567c478bd9Sstevel@tonic-gate 	} else if ((err = rename(new_inst_file, inst_file)) != 0) {
38577c478bd9Sstevel@tonic-gate 		err_print(RENAME_FAILED, inst_file, strerror(errno));
38587c478bd9Sstevel@tonic-gate 	}
38597c478bd9Sstevel@tonic-gate 
38607c478bd9Sstevel@tonic-gate out:
38617c478bd9Sstevel@tonic-gate 	if (inst_file_fp != NULL) {
38627c478bd9Sstevel@tonic-gate 		if (fclose(inst_file_fp) == EOF) {
38637c478bd9Sstevel@tonic-gate 			err_print(FCLOSE_FAILED, inst_file, strerror(errno));
38647c478bd9Sstevel@tonic-gate 		}
38657c478bd9Sstevel@tonic-gate 	}
38667c478bd9Sstevel@tonic-gate 
38677c478bd9Sstevel@tonic-gate 	if (stat(new_inst_file, &sb) == 0) {
38687c478bd9Sstevel@tonic-gate 		s_unlink(new_inst_file);
38697c478bd9Sstevel@tonic-gate 	}
38707c478bd9Sstevel@tonic-gate 	free(new_inst_file);
38717c478bd9Sstevel@tonic-gate 
38727c478bd9Sstevel@tonic-gate 	if (stat(old_inst_file, &sb) == 0) {
38737c478bd9Sstevel@tonic-gate 		s_unlink(old_inst_file);
38747c478bd9Sstevel@tonic-gate 	}
38757c478bd9Sstevel@tonic-gate 	free(old_inst_file);
38767c478bd9Sstevel@tonic-gate 
38777c478bd9Sstevel@tonic-gate 	free(old_inst_file_npid);
38787c478bd9Sstevel@tonic-gate 
38797c478bd9Sstevel@tonic-gate 	if (err != 0 && err != EALREADY) {
38807c478bd9Sstevel@tonic-gate 		err_print(FAILED_TO_UPDATE, inst_file);
38817c478bd9Sstevel@tonic-gate 	}
38827c478bd9Sstevel@tonic-gate }
38837c478bd9Sstevel@tonic-gate 
38847c478bd9Sstevel@tonic-gate /*
38857c478bd9Sstevel@tonic-gate  * detach from tty.  For daemon mode.
38867c478bd9Sstevel@tonic-gate  */
38877c478bd9Sstevel@tonic-gate void
38887c478bd9Sstevel@tonic-gate detachfromtty()
38897c478bd9Sstevel@tonic-gate {
38907c478bd9Sstevel@tonic-gate 	(void) setsid();
38917c478bd9Sstevel@tonic-gate 	if (DEVFSADM_DEBUG_ON == TRUE) {
38927c478bd9Sstevel@tonic-gate 		return;
38937c478bd9Sstevel@tonic-gate 	}
38947c478bd9Sstevel@tonic-gate 
38957c478bd9Sstevel@tonic-gate 	(void) close(0);
38967c478bd9Sstevel@tonic-gate 	(void) close(1);
38977c478bd9Sstevel@tonic-gate 	(void) close(2);
38987c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDWR, 0);
38997c478bd9Sstevel@tonic-gate 	(void) dup(0);
39007c478bd9Sstevel@tonic-gate 	(void) dup(0);
39017c478bd9Sstevel@tonic-gate 	openlog(DEVFSADMD, LOG_PID, LOG_DAEMON);
39027c478bd9Sstevel@tonic-gate 	(void) setlogmask(LOG_UPTO(LOG_INFO));
39037c478bd9Sstevel@tonic-gate 	logflag = TRUE;
39047c478bd9Sstevel@tonic-gate }
39057c478bd9Sstevel@tonic-gate 
39067c478bd9Sstevel@tonic-gate /*
39077c478bd9Sstevel@tonic-gate  * Use an advisory lock to synchronize updates to /dev.  If the lock is
39087c478bd9Sstevel@tonic-gate  * held by another process, block in the fcntl() system call until that
39097c478bd9Sstevel@tonic-gate  * process drops the lock or exits.  The lock file itself is
39107c478bd9Sstevel@tonic-gate  * DEV_LOCK_FILE.  The process id of the current and last process owning
39117c478bd9Sstevel@tonic-gate  * the lock is kept in the lock file.  After acquiring the lock, read the
39127c478bd9Sstevel@tonic-gate  * process id and return it.  It is the process ID which last owned the
39137c478bd9Sstevel@tonic-gate  * lock, and will be used to determine if caches need to be flushed.
39147c478bd9Sstevel@tonic-gate  *
39157c478bd9Sstevel@tonic-gate  * NOTE: if the devlink database is held open by the caller, it may
39167c478bd9Sstevel@tonic-gate  * be closed by this routine. This is to enforce the following lock ordering:
39177c478bd9Sstevel@tonic-gate  *	1) /dev lock 2) database open
39187c478bd9Sstevel@tonic-gate  */
39197c478bd9Sstevel@tonic-gate pid_t
39207c478bd9Sstevel@tonic-gate enter_dev_lock()
39217c478bd9Sstevel@tonic-gate {
39227c478bd9Sstevel@tonic-gate 	struct flock lock;
39237c478bd9Sstevel@tonic-gate 	int n;
39247c478bd9Sstevel@tonic-gate 	pid_t pid;
39257c478bd9Sstevel@tonic-gate 	pid_t last_owner_pid;
39267c478bd9Sstevel@tonic-gate 
39277c478bd9Sstevel@tonic-gate 	if (file_mods == FALSE) {
39287c478bd9Sstevel@tonic-gate 		return (0);
39297c478bd9Sstevel@tonic-gate 	}
39307c478bd9Sstevel@tonic-gate 
39317c478bd9Sstevel@tonic-gate 	(void) snprintf(dev_lockfile, sizeof (dev_lockfile),
3932facf4a8dSllai 	    "%s/%s", etc_dev_dir, DEV_LOCK_FILE);
39337c478bd9Sstevel@tonic-gate 
39347c478bd9Sstevel@tonic-gate 	vprint(LOCK_MID, "enter_dev_lock: lock file %s\n", dev_lockfile);
39357c478bd9Sstevel@tonic-gate 
39367c478bd9Sstevel@tonic-gate 	dev_lock_fd = open(dev_lockfile, O_CREAT|O_RDWR, 0644);
39377c478bd9Sstevel@tonic-gate 	if (dev_lock_fd < 0) {
39387c478bd9Sstevel@tonic-gate 		err_print(OPEN_FAILED, dev_lockfile, strerror(errno));
39397c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
3940537714daSvikram 		/*NOTREACHED*/
39417c478bd9Sstevel@tonic-gate 	}
39427c478bd9Sstevel@tonic-gate 
39437c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
39447c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
39457c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
39467c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
39477c478bd9Sstevel@tonic-gate 
39487c478bd9Sstevel@tonic-gate 	/* try for the lock, but don't wait */
39497c478bd9Sstevel@tonic-gate 	if (fcntl(dev_lock_fd, F_SETLK, &lock) == -1) {
39507c478bd9Sstevel@tonic-gate 		if ((errno == EACCES) || (errno == EAGAIN)) {
39517c478bd9Sstevel@tonic-gate 			pid = 0;
39527c478bd9Sstevel@tonic-gate 			n = read(dev_lock_fd, &pid, sizeof (pid_t));
39537c478bd9Sstevel@tonic-gate 			vprint(LOCK_MID, "waiting for PID %d to complete\n",
39540a653502Swroche 			    (int)pid);
39557c478bd9Sstevel@tonic-gate 			if (lseek(dev_lock_fd, 0, SEEK_SET) == (off_t)-1) {
39567c478bd9Sstevel@tonic-gate 				err_print(LSEEK_FAILED, dev_lockfile,
39570a653502Swroche 				    strerror(errno));
39587c478bd9Sstevel@tonic-gate 				devfsadm_exit(1);
3959537714daSvikram 				/*NOTREACHED*/
39607c478bd9Sstevel@tonic-gate 			}
39617c478bd9Sstevel@tonic-gate 			/*
39627c478bd9Sstevel@tonic-gate 			 * wait for the dev lock. If we have the database open,
39637c478bd9Sstevel@tonic-gate 			 * close it first - the order of lock acquisition should
39647c478bd9Sstevel@tonic-gate 			 * always be:  1) dev_lock 2) database
39657c478bd9Sstevel@tonic-gate 			 * This is to prevent deadlocks with any locks the
39667c478bd9Sstevel@tonic-gate 			 * database code may hold.
39677c478bd9Sstevel@tonic-gate 			 */
39687c478bd9Sstevel@tonic-gate 			(void) di_devlink_close(&devlink_cache, 0);
3969f05faa4eSjacobs 
3970f05faa4eSjacobs 			/* send any sysevents that were queued up. */
3971f05faa4eSjacobs 			process_syseventq();
3972f05faa4eSjacobs 
39737c478bd9Sstevel@tonic-gate 			if (fcntl(dev_lock_fd, F_SETLKW, &lock) == -1) {
39747c478bd9Sstevel@tonic-gate 				err_print(LOCK_FAILED, dev_lockfile,
39750a653502Swroche 				    strerror(errno));
39767c478bd9Sstevel@tonic-gate 				devfsadm_exit(1);
3977537714daSvikram 				/*NOTREACHED*/
39787c478bd9Sstevel@tonic-gate 			}
39797c478bd9Sstevel@tonic-gate 		}
39807c478bd9Sstevel@tonic-gate 	}
39817c478bd9Sstevel@tonic-gate 
39827c478bd9Sstevel@tonic-gate 	hold_dev_lock = TRUE;
39837c478bd9Sstevel@tonic-gate 	pid = 0;
39847c478bd9Sstevel@tonic-gate 	n = read(dev_lock_fd, &pid, sizeof (pid_t));
39857c478bd9Sstevel@tonic-gate 	if (n == sizeof (pid_t) && pid == getpid()) {
39867c478bd9Sstevel@tonic-gate 		return (pid);
39877c478bd9Sstevel@tonic-gate 	}
39887c478bd9Sstevel@tonic-gate 
39897c478bd9Sstevel@tonic-gate 	last_owner_pid = pid;
39907c478bd9Sstevel@tonic-gate 
39917c478bd9Sstevel@tonic-gate 	if (lseek(dev_lock_fd, 0, SEEK_SET) == (off_t)-1) {
39927c478bd9Sstevel@tonic-gate 		err_print(LSEEK_FAILED, dev_lockfile, strerror(errno));
39937c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
3994537714daSvikram 		/*NOTREACHED*/
39957c478bd9Sstevel@tonic-gate 	}
39967c478bd9Sstevel@tonic-gate 	pid = getpid();
39977c478bd9Sstevel@tonic-gate 	n = write(dev_lock_fd, &pid, sizeof (pid_t));
39987c478bd9Sstevel@tonic-gate 	if (n != sizeof (pid_t)) {
39997c478bd9Sstevel@tonic-gate 		err_print(WRITE_FAILED, dev_lockfile, strerror(errno));
40007c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
4001537714daSvikram 		/*NOTREACHED*/
40027c478bd9Sstevel@tonic-gate 	}
40037c478bd9Sstevel@tonic-gate 
40047c478bd9Sstevel@tonic-gate 	return (last_owner_pid);
40057c478bd9Sstevel@tonic-gate }
40067c478bd9Sstevel@tonic-gate 
40077c478bd9Sstevel@tonic-gate /*
40087c478bd9Sstevel@tonic-gate  * Drop the advisory /dev lock, close lock file.  Close and re-open the
40097c478bd9Sstevel@tonic-gate  * file every time so to ensure a resync if for some reason the lock file
40107c478bd9Sstevel@tonic-gate  * gets removed.
40117c478bd9Sstevel@tonic-gate  */
40127c478bd9Sstevel@tonic-gate void
4013537714daSvikram exit_dev_lock(int exiting)
40147c478bd9Sstevel@tonic-gate {
40157c478bd9Sstevel@tonic-gate 	struct flock unlock;
40167c478bd9Sstevel@tonic-gate 
40177c478bd9Sstevel@tonic-gate 	if (hold_dev_lock == FALSE) {
40187c478bd9Sstevel@tonic-gate 		return;
40197c478bd9Sstevel@tonic-gate 	}
40207c478bd9Sstevel@tonic-gate 
4021537714daSvikram 	vprint(LOCK_MID, "exit_dev_lock: lock file %s, exiting = %d\n",
4022537714daSvikram 	    dev_lockfile, exiting);
40237c478bd9Sstevel@tonic-gate 
40247c478bd9Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
40257c478bd9Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
40267c478bd9Sstevel@tonic-gate 	unlock.l_start = 0;
40277c478bd9Sstevel@tonic-gate 	unlock.l_len = 0;
40287c478bd9Sstevel@tonic-gate 
40297c478bd9Sstevel@tonic-gate 	if (fcntl(dev_lock_fd, F_SETLK, &unlock) == -1) {
40307c478bd9Sstevel@tonic-gate 		err_print(UNLOCK_FAILED, dev_lockfile, strerror(errno));
40317c478bd9Sstevel@tonic-gate 	}
40327c478bd9Sstevel@tonic-gate 
40337c478bd9Sstevel@tonic-gate 	hold_dev_lock = FALSE;
40347c478bd9Sstevel@tonic-gate 
40357c478bd9Sstevel@tonic-gate 	if (close(dev_lock_fd) == -1) {
40367c478bd9Sstevel@tonic-gate 		err_print(CLOSE_FAILED, dev_lockfile, strerror(errno));
4037537714daSvikram 		if (!exiting)
4038537714daSvikram 			devfsadm_exit(1);
4039537714daSvikram 			/*NOTREACHED*/
40407c478bd9Sstevel@tonic-gate 	}
40417c478bd9Sstevel@tonic-gate }
40427c478bd9Sstevel@tonic-gate 
40437c478bd9Sstevel@tonic-gate /*
40447c478bd9Sstevel@tonic-gate  *
40457c478bd9Sstevel@tonic-gate  * Use an advisory lock to ensure that only one daemon process is active
40467c478bd9Sstevel@tonic-gate  * in the system at any point in time.	If the lock is held by another
40477c478bd9Sstevel@tonic-gate  * process, do not block but return the pid owner of the lock to the
40487c478bd9Sstevel@tonic-gate  * caller immediately.	The lock is cleared if the holding daemon process
40497c478bd9Sstevel@tonic-gate  * exits for any reason even if the lock file remains, so the daemon can
40507c478bd9Sstevel@tonic-gate  * be restarted if necessary.  The lock file is DAEMON_LOCK_FILE.
40517c478bd9Sstevel@tonic-gate  */
40527c478bd9Sstevel@tonic-gate pid_t
40537c478bd9Sstevel@tonic-gate enter_daemon_lock(void)
40547c478bd9Sstevel@tonic-gate {
40557c478bd9Sstevel@tonic-gate 	struct flock lock;
40567c478bd9Sstevel@tonic-gate 
40577c478bd9Sstevel@tonic-gate 	(void) snprintf(daemon_lockfile, sizeof (daemon_lockfile),
4058facf4a8dSllai 	    "%s/%s", etc_dev_dir, DAEMON_LOCK_FILE);
40597c478bd9Sstevel@tonic-gate 
40607c478bd9Sstevel@tonic-gate 	vprint(LOCK_MID, "enter_daemon_lock: lock file %s\n", daemon_lockfile);
40617c478bd9Sstevel@tonic-gate 
40627c478bd9Sstevel@tonic-gate 	daemon_lock_fd = open(daemon_lockfile, O_CREAT|O_RDWR, 0644);
40637c478bd9Sstevel@tonic-gate 	if (daemon_lock_fd < 0) {
40647c478bd9Sstevel@tonic-gate 		err_print(OPEN_FAILED, daemon_lockfile, strerror(errno));
40657c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
4066537714daSvikram 		/*NOTREACHED*/
40677c478bd9Sstevel@tonic-gate 	}
40687c478bd9Sstevel@tonic-gate 
40697c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
40707c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
40717c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
40727c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
40737c478bd9Sstevel@tonic-gate 
40747c478bd9Sstevel@tonic-gate 	if (fcntl(daemon_lock_fd, F_SETLK, &lock) == -1) {
40757c478bd9Sstevel@tonic-gate 
40767c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN || errno == EDEADLK) {
40777c478bd9Sstevel@tonic-gate 			if (fcntl(daemon_lock_fd, F_GETLK, &lock) == -1) {
40787c478bd9Sstevel@tonic-gate 				err_print(LOCK_FAILED, daemon_lockfile,
40790a653502Swroche 				    strerror(errno));
40807c478bd9Sstevel@tonic-gate 				devfsadm_exit(1);
4081537714daSvikram 				/*NOTREACHED*/
40827c478bd9Sstevel@tonic-gate 			}
40837c478bd9Sstevel@tonic-gate 			return (lock.l_pid);
40847c478bd9Sstevel@tonic-gate 		}
40857c478bd9Sstevel@tonic-gate 	}
40867c478bd9Sstevel@tonic-gate 	hold_daemon_lock = TRUE;
40877c478bd9Sstevel@tonic-gate 	return (getpid());
40887c478bd9Sstevel@tonic-gate }
40897c478bd9Sstevel@tonic-gate 
40907c478bd9Sstevel@tonic-gate /*
40917c478bd9Sstevel@tonic-gate  * Drop the advisory daemon lock, close lock file
40927c478bd9Sstevel@tonic-gate  */
40937c478bd9Sstevel@tonic-gate void
4094537714daSvikram exit_daemon_lock(int exiting)
40957c478bd9Sstevel@tonic-gate {
40967c478bd9Sstevel@tonic-gate 	struct flock lock;
40977c478bd9Sstevel@tonic-gate 
40987c478bd9Sstevel@tonic-gate 	if (hold_daemon_lock == FALSE) {
40997c478bd9Sstevel@tonic-gate 		return;
41007c478bd9Sstevel@tonic-gate 	}
41017c478bd9Sstevel@tonic-gate 
4102537714daSvikram 	vprint(LOCK_MID, "exit_daemon_lock: lock file %s, exiting = %d\n",
4103537714daSvikram 	    daemon_lockfile, exiting);
41047c478bd9Sstevel@tonic-gate 
41057c478bd9Sstevel@tonic-gate 	lock.l_type = F_UNLCK;
41067c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
41077c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
41087c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
41097c478bd9Sstevel@tonic-gate 
41107c478bd9Sstevel@tonic-gate 	if (fcntl(daemon_lock_fd, F_SETLK, &lock) == -1) {
41117c478bd9Sstevel@tonic-gate 		err_print(UNLOCK_FAILED, daemon_lockfile, strerror(errno));
41127c478bd9Sstevel@tonic-gate 	}
41137c478bd9Sstevel@tonic-gate 
41147c478bd9Sstevel@tonic-gate 	if (close(daemon_lock_fd) == -1) {
41157c478bd9Sstevel@tonic-gate 		err_print(CLOSE_FAILED, daemon_lockfile, strerror(errno));
4116537714daSvikram 		if (!exiting)
4117537714daSvikram 			devfsadm_exit(1);
4118537714daSvikram 			/*NOTREACHED*/
41197c478bd9Sstevel@tonic-gate 	}
41207c478bd9Sstevel@tonic-gate }
41217c478bd9Sstevel@tonic-gate 
41227c478bd9Sstevel@tonic-gate /*
41237c478bd9Sstevel@tonic-gate  * Called to removed danging nodes in two different modes: RM_PRE, RM_POST.
41247c478bd9Sstevel@tonic-gate  * RM_PRE mode is called before processing the entire devinfo tree, and RM_POST
41257c478bd9Sstevel@tonic-gate  * is called after processing the entire devinfo tree.
41267c478bd9Sstevel@tonic-gate  */
41277c478bd9Sstevel@tonic-gate static void
41287c478bd9Sstevel@tonic-gate pre_and_post_cleanup(int flags)
41297c478bd9Sstevel@tonic-gate {
41307c478bd9Sstevel@tonic-gate 	remove_list_t *rm;
41317c478bd9Sstevel@tonic-gate 	recurse_dev_t rd;
41327c478bd9Sstevel@tonic-gate 	cleanup_data_t cleanup_data;
41337c478bd9Sstevel@tonic-gate 	char *fcn = "pre_and_post_cleanup: ";
41347c478bd9Sstevel@tonic-gate 
41357c478bd9Sstevel@tonic-gate 	if (build_dev == FALSE)
41367c478bd9Sstevel@tonic-gate 		return;
41377c478bd9Sstevel@tonic-gate 
41387c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "attempting %s-cleanup\n",
41397c478bd9Sstevel@tonic-gate 	    flags == RM_PRE ? "pre" : "post");
41407c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%sflags = %d\n", fcn, flags);
41417c478bd9Sstevel@tonic-gate 
41427c478bd9Sstevel@tonic-gate 	/*
41437c478bd9Sstevel@tonic-gate 	 * the generic function recurse_dev_re is shared among different
41447c478bd9Sstevel@tonic-gate 	 * functions, so set the method and data that it should use for
41457c478bd9Sstevel@tonic-gate 	 * matches.
41467c478bd9Sstevel@tonic-gate 	 */
41477c478bd9Sstevel@tonic-gate 	rd.fcn = matching_dev;
41487c478bd9Sstevel@tonic-gate 	rd.data = (void *)&cleanup_data;
41497c478bd9Sstevel@tonic-gate 	cleanup_data.flags = flags;
41507c478bd9Sstevel@tonic-gate 
4151aa646b9dSvikram 	(void) mutex_lock(&nfp_mutex);
4152aa646b9dSvikram 	nfphash_create();
4153aa646b9dSvikram 
41547c478bd9Sstevel@tonic-gate 	for (rm = remove_head; rm != NULL; rm = rm->next) {
41557c478bd9Sstevel@tonic-gate 		if ((flags & rm->remove->flags) == flags) {
41567c478bd9Sstevel@tonic-gate 			cleanup_data.rm = rm;
41577c478bd9Sstevel@tonic-gate 			/*
41587c478bd9Sstevel@tonic-gate 			 * If reached this point, RM_PRE or RM_POST cleanup is
41597c478bd9Sstevel@tonic-gate 			 * desired.  clean_ok() decides whether to clean
41607c478bd9Sstevel@tonic-gate 			 * under the given circumstances.
41617c478bd9Sstevel@tonic-gate 			 */
41627c478bd9Sstevel@tonic-gate 			vprint(REMOVE_MID, "%scleanup: PRE or POST\n", fcn);
41637c478bd9Sstevel@tonic-gate 			if (clean_ok(rm->remove) == DEVFSADM_SUCCESS) {
41647c478bd9Sstevel@tonic-gate 				vprint(REMOVE_MID, "cleanup: cleanup OK\n");
41650a653502Swroche 				recurse_dev_re(dev_dir,
41660a653502Swroche 				    rm->remove->dev_dirs_re, &rd);
41677c478bd9Sstevel@tonic-gate 			}
41687c478bd9Sstevel@tonic-gate 		}
41697c478bd9Sstevel@tonic-gate 	}
4170aa646b9dSvikram 	nfphash_destroy();
4171aa646b9dSvikram 	(void) mutex_unlock(&nfp_mutex);
41727c478bd9Sstevel@tonic-gate }
41737c478bd9Sstevel@tonic-gate 
41747c478bd9Sstevel@tonic-gate /*
41757c478bd9Sstevel@tonic-gate  * clean_ok() determines whether cleanup should be done according
41767c478bd9Sstevel@tonic-gate  * to the following matrix:
41777c478bd9Sstevel@tonic-gate  *
41787c478bd9Sstevel@tonic-gate  * command line arguments RM_PRE    RM_POST	  RM_PRE &&    RM_POST &&
41797c478bd9Sstevel@tonic-gate  *						  RM_ALWAYS    RM_ALWAYS
41807c478bd9Sstevel@tonic-gate  * ---------------------- ------     -----	  ---------    ----------
41817c478bd9Sstevel@tonic-gate  *
41827c478bd9Sstevel@tonic-gate  * <neither -c nor -C>	  -	    -		  pre-clean    post-clean
41837c478bd9Sstevel@tonic-gate  *
41847c478bd9Sstevel@tonic-gate  * -C			  pre-clean  post-clean   pre-clean    post-clean
41857c478bd9Sstevel@tonic-gate  *
41867c478bd9Sstevel@tonic-gate  * -C -c class		  pre-clean  post-clean   pre-clean    post-clean
41877c478bd9Sstevel@tonic-gate  *			  if class  if class	  if class     if class
41887c478bd9Sstevel@tonic-gate  *			  matches   matches	  matches      matches
41897c478bd9Sstevel@tonic-gate  *
41907c478bd9Sstevel@tonic-gate  * -c class		   -	       -	  pre-clean    post-clean
41917c478bd9Sstevel@tonic-gate  *						  if class     if class
41927c478bd9Sstevel@tonic-gate  *						  matches      matches
41937c478bd9Sstevel@tonic-gate  *
41947c478bd9Sstevel@tonic-gate  */
41957c478bd9Sstevel@tonic-gate static int
4196aa646b9dSvikram clean_ok(devfsadm_remove_V1_t *remove)
41977c478bd9Sstevel@tonic-gate {
41987c478bd9Sstevel@tonic-gate 	int i;
41997c478bd9Sstevel@tonic-gate 
42007c478bd9Sstevel@tonic-gate 	if (single_drv == TRUE) {
42017c478bd9Sstevel@tonic-gate 		/* no cleanup at all when using -i option */
42027c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
42037c478bd9Sstevel@tonic-gate 	}
42047c478bd9Sstevel@tonic-gate 
42057c478bd9Sstevel@tonic-gate 	/*
42067c478bd9Sstevel@tonic-gate 	 * no cleanup if drivers are not loaded. We make an exception
42077c478bd9Sstevel@tonic-gate 	 * for the "disks" program however, since disks has a public
42087c478bd9Sstevel@tonic-gate 	 * cleanup flag (-C) and disk drivers are usually never
42097c478bd9Sstevel@tonic-gate 	 * unloaded.
42107c478bd9Sstevel@tonic-gate 	 */
42117c478bd9Sstevel@tonic-gate 	if (load_attach_drv == FALSE && strcmp(prog, DISKS) != 0) {
42127c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
42137c478bd9Sstevel@tonic-gate 	}
42147c478bd9Sstevel@tonic-gate 
42157c478bd9Sstevel@tonic-gate 	/* if the cleanup flag was not specified, return false */
42167c478bd9Sstevel@tonic-gate 	if ((cleanup == FALSE) && ((remove->flags & RM_ALWAYS) == 0)) {
42177c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
42187c478bd9Sstevel@tonic-gate 	}
42197c478bd9Sstevel@tonic-gate 
42207c478bd9Sstevel@tonic-gate 	if (num_classes == 0) {
42217c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
42227c478bd9Sstevel@tonic-gate 	}
42237c478bd9Sstevel@tonic-gate 
42247c478bd9Sstevel@tonic-gate 	/*
42257c478bd9Sstevel@tonic-gate 	 * if reached this point, check to see if the class in the given
42267c478bd9Sstevel@tonic-gate 	 * remove structure matches a class given on the command line
42277c478bd9Sstevel@tonic-gate 	 */
42287c478bd9Sstevel@tonic-gate 
42297c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_classes; i++) {
42307c478bd9Sstevel@tonic-gate 		if (strcmp(remove->device_class, classes[i]) == 0) {
42317c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
42327c478bd9Sstevel@tonic-gate 		}
42337c478bd9Sstevel@tonic-gate 	}
42347c478bd9Sstevel@tonic-gate 
42357c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
42367c478bd9Sstevel@tonic-gate }
42377c478bd9Sstevel@tonic-gate 
42387c478bd9Sstevel@tonic-gate /*
42397c478bd9Sstevel@tonic-gate  * Called to remove dangling nodes after receiving a hotplug event
42407c478bd9Sstevel@tonic-gate  * containing the physical node pathname to be removed.
42417c478bd9Sstevel@tonic-gate  */
42427c478bd9Sstevel@tonic-gate void
42437c478bd9Sstevel@tonic-gate hot_cleanup(char *node_path, char *minor_name, char *ev_subclass,
42447c478bd9Sstevel@tonic-gate     char *driver_name, int instance)
42457c478bd9Sstevel@tonic-gate {
42467c478bd9Sstevel@tonic-gate 	link_t *link;
42477c478bd9Sstevel@tonic-gate 	linkhead_t *head;
42487c478bd9Sstevel@tonic-gate 	remove_list_t *rm;
42497c478bd9Sstevel@tonic-gate 	char *fcn = "hot_cleanup: ";
42507c478bd9Sstevel@tonic-gate 	char path[PATH_MAX + 1];
42517c478bd9Sstevel@tonic-gate 	int path_len;
42527c478bd9Sstevel@tonic-gate 	char rmlink[PATH_MAX + 1];
42537c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
4254fb9251a6Scth 	int skip;
4255aa646b9dSvikram 	int ret;
42567c478bd9Sstevel@tonic-gate 
42577c478bd9Sstevel@tonic-gate 	/*
42587c478bd9Sstevel@tonic-gate 	 * dev links can go away as part of hot cleanup.
42597c478bd9Sstevel@tonic-gate 	 * So first build event attributes in order capture dev links.
42607c478bd9Sstevel@tonic-gate 	 */
42617c478bd9Sstevel@tonic-gate 	if (ev_subclass != NULL)
42627c478bd9Sstevel@tonic-gate 		nvl = build_event_attributes(EC_DEV_REMOVE, ev_subclass,
426330294554Sphitran 		    node_path, DI_NODE_NIL, driver_name, instance, minor_name);
42647c478bd9Sstevel@tonic-gate 
42657c478bd9Sstevel@tonic-gate 	(void) strcpy(path, node_path);
42667c478bd9Sstevel@tonic-gate 	(void) strcat(path, ":");
42677c478bd9Sstevel@tonic-gate 	(void) strcat(path, minor_name == NULL ? "" : minor_name);
42687c478bd9Sstevel@tonic-gate 
42697c478bd9Sstevel@tonic-gate 	path_len = strlen(path);
42707c478bd9Sstevel@tonic-gate 
42717c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%spath=%s\n", fcn, path);
42727c478bd9Sstevel@tonic-gate 
4273aa646b9dSvikram 	(void) mutex_lock(&nfp_mutex);
4274aa646b9dSvikram 	nfphash_create();
4275aa646b9dSvikram 
42767c478bd9Sstevel@tonic-gate 	for (rm = remove_head; rm != NULL; rm = rm->next) {
42777c478bd9Sstevel@tonic-gate 		if ((RM_HOT & rm->remove->flags) == RM_HOT) {
42787c478bd9Sstevel@tonic-gate 			head = get_cached_links(rm->remove->dev_dirs_re);
42797c478bd9Sstevel@tonic-gate 			assert(head->nextlink == NULL);
42807c478bd9Sstevel@tonic-gate 			for (link = head->link;
42817c478bd9Sstevel@tonic-gate 			    link != NULL; link = head->nextlink) {
42827c478bd9Sstevel@tonic-gate 				/*
42837c478bd9Sstevel@tonic-gate 				 * The remove callback below may remove
42847c478bd9Sstevel@tonic-gate 				 * the current and/or any or all of the
42857c478bd9Sstevel@tonic-gate 				 * subsequent links in the list.
42867c478bd9Sstevel@tonic-gate 				 * Save the next link in the head. If
42877c478bd9Sstevel@tonic-gate 				 * the callback removes the next link
42887c478bd9Sstevel@tonic-gate 				 * the saved pointer in the head will be
42897c478bd9Sstevel@tonic-gate 				 * updated by the callback to point at
42907c478bd9Sstevel@tonic-gate 				 * the next valid link.
42917c478bd9Sstevel@tonic-gate 				 */
42927c478bd9Sstevel@tonic-gate 				head->nextlink = link->next;
4293aa646b9dSvikram 
4294aa646b9dSvikram 				/*
4295aa646b9dSvikram 				 * if devlink is in no-further-process hash,
4296aa646b9dSvikram 				 * skip its remove
4297aa646b9dSvikram 				 */
4298aa646b9dSvikram 				if (nfphash_lookup(link->devlink) != NULL)
4299aa646b9dSvikram 					continue;
4300aa646b9dSvikram 
4301fb9251a6Scth 				if (minor_name)
4302fb9251a6Scth 					skip = strcmp(link->contents, path);
4303fb9251a6Scth 				else
4304fb9251a6Scth 					skip = strncmp(link->contents, path,
4305fb9251a6Scth 					    path_len);
4306fb9251a6Scth 				if (skip ||
4307fb9251a6Scth 				    (call_minor_init(rm->modptr) ==
4308fb9251a6Scth 				    DEVFSADM_FAILURE))
4309fb9251a6Scth 					continue;
43107c478bd9Sstevel@tonic-gate 
4311fb9251a6Scth 				vprint(REMOVE_MID,
43120a653502Swroche 				    "%sremoving %s -> %s\n", fcn,
43130a653502Swroche 				    link->devlink, link->contents);
4314fb9251a6Scth 				/*
4315fb9251a6Scth 				 * Use a copy of the cached link name
4316fb9251a6Scth 				 * as the cache entry will go away
4317fb9251a6Scth 				 * during link removal
4318fb9251a6Scth 				 */
4319fb9251a6Scth 				(void) snprintf(rmlink, sizeof (rmlink),
4320fb9251a6Scth 				    "%s", link->devlink);
4321aa646b9dSvikram 				if (rm->remove->flags & RM_NOINTERPOSE) {
4322aa646b9dSvikram 					((void (*)(char *))
43230a653502Swroche 					    (rm->remove->callback_fcn))(rmlink);
4324aa646b9dSvikram 				} else {
4325aa646b9dSvikram 					ret = ((int (*)(char *))
4326aa646b9dSvikram 					    (rm->remove->callback_fcn))(rmlink);
4327aa646b9dSvikram 					if (ret == DEVFSADM_TERMINATE)
4328aa646b9dSvikram 						nfphash_insert(rmlink);
4329aa646b9dSvikram 				}
43307c478bd9Sstevel@tonic-gate 			}
43317c478bd9Sstevel@tonic-gate 		}
43327c478bd9Sstevel@tonic-gate 	}
43337c478bd9Sstevel@tonic-gate 
4334aa646b9dSvikram 	nfphash_destroy();
4335aa646b9dSvikram 	(void) mutex_unlock(&nfp_mutex);
4336aa646b9dSvikram 
433745916cd2Sjpk 	/* update device allocation database */
433845916cd2Sjpk 	if (system_labeled) {
433945916cd2Sjpk 		int	devtype = 0;
434045916cd2Sjpk 
43417e3e5701SJan Parcel 		if (strstr(path, DA_SOUND_NAME))
434245916cd2Sjpk 			devtype = DA_AUDIO;
43437e3e5701SJan Parcel 		else if (strstr(path, "storage"))
43447e3e5701SJan Parcel 			devtype = DA_RMDISK;
43457e3e5701SJan Parcel 		else if (strstr(path, "disk"))
43467e3e5701SJan Parcel 			devtype = DA_RMDISK;
43477e3e5701SJan Parcel 		else if (strstr(path, "floppy"))
43487e3e5701SJan Parcel 			/* TODO: detect usb cds and floppies at insert time */
434945916cd2Sjpk 			devtype = DA_RMDISK;
435045916cd2Sjpk 		else
435145916cd2Sjpk 			goto out;
43527e3e5701SJan Parcel 
43537e3e5701SJan Parcel 		(void) _update_devalloc_db(&devlist, devtype, DA_REMOVE,
43547e3e5701SJan Parcel 		    node_path, root_dir);
435545916cd2Sjpk 	}
435645916cd2Sjpk 
435745916cd2Sjpk out:
43587c478bd9Sstevel@tonic-gate 	/* now log an event */
43597c478bd9Sstevel@tonic-gate 	if (nvl) {
43607c478bd9Sstevel@tonic-gate 		log_event(EC_DEV_REMOVE, ev_subclass, nvl);
43617c478bd9Sstevel@tonic-gate 		free(nvl);
43627c478bd9Sstevel@tonic-gate 	}
43637c478bd9Sstevel@tonic-gate }
43647c478bd9Sstevel@tonic-gate 
43657c478bd9Sstevel@tonic-gate /*
43667c478bd9Sstevel@tonic-gate  * Open the dir current_dir.  For every file which matches the first dir
43677c478bd9Sstevel@tonic-gate  * component of path_re, recurse.  If there are no more *dir* path
43687c478bd9Sstevel@tonic-gate  * components left in path_re (ie no more /), then call function rd->fcn.
43697c478bd9Sstevel@tonic-gate  */
43707c478bd9Sstevel@tonic-gate static void
43717c478bd9Sstevel@tonic-gate recurse_dev_re(char *current_dir, char *path_re, recurse_dev_t *rd)
43727c478bd9Sstevel@tonic-gate {
43737c478bd9Sstevel@tonic-gate 	regex_t re1;
43747c478bd9Sstevel@tonic-gate 	char *slash;
43757c478bd9Sstevel@tonic-gate 	char new_path[PATH_MAX + 1];
43767c478bd9Sstevel@tonic-gate 	char *anchored_path_re;
43777c478bd9Sstevel@tonic-gate 	size_t len;
437873de625bSjg 	finddevhdl_t fhandle;
437973de625bSjg 	const char *fp;
43807c478bd9Sstevel@tonic-gate 
43817c478bd9Sstevel@tonic-gate 	vprint(RECURSEDEV_MID, "recurse_dev_re: curr = %s path=%s\n",
43820a653502Swroche 	    current_dir, path_re);
43837c478bd9Sstevel@tonic-gate 
438473de625bSjg 	if (finddev_readdir(current_dir, &fhandle) != 0)
43857c478bd9Sstevel@tonic-gate 		return;
43867c478bd9Sstevel@tonic-gate 
43877c478bd9Sstevel@tonic-gate 	len = strlen(path_re);
43887c478bd9Sstevel@tonic-gate 	if ((slash = strchr(path_re, '/')) != NULL) {
43897c478bd9Sstevel@tonic-gate 		len = (slash - path_re);
43907c478bd9Sstevel@tonic-gate 	}
43917c478bd9Sstevel@tonic-gate 
43927c478bd9Sstevel@tonic-gate 	anchored_path_re = s_malloc(len + 3);
43937c478bd9Sstevel@tonic-gate 	(void) sprintf(anchored_path_re, "^%.*s$", len, path_re);
43947c478bd9Sstevel@tonic-gate 
43957c478bd9Sstevel@tonic-gate 	if (regcomp(&re1, anchored_path_re, REG_EXTENDED) != 0) {
43967c478bd9Sstevel@tonic-gate 		free(anchored_path_re);
43977c478bd9Sstevel@tonic-gate 		goto out;
43987c478bd9Sstevel@tonic-gate 	}
43997c478bd9Sstevel@tonic-gate 
44007c478bd9Sstevel@tonic-gate 	free(anchored_path_re);
44017c478bd9Sstevel@tonic-gate 
440273de625bSjg 	while ((fp = finddev_next(fhandle)) != NULL) {
44037c478bd9Sstevel@tonic-gate 
440473de625bSjg 		if (regexec(&re1, fp, 0, NULL, 0) == 0) {
44057c478bd9Sstevel@tonic-gate 			/* match */
44067c478bd9Sstevel@tonic-gate 			(void) strcpy(new_path, current_dir);
44077c478bd9Sstevel@tonic-gate 			(void) strcat(new_path, "/");
440873de625bSjg 			(void) strcat(new_path, fp);
44097c478bd9Sstevel@tonic-gate 
44107c478bd9Sstevel@tonic-gate 			vprint(RECURSEDEV_MID, "recurse_dev_re: match, new "
44110a653502Swroche 			    "path = %s\n", new_path);
44127c478bd9Sstevel@tonic-gate 
44137c478bd9Sstevel@tonic-gate 			if (slash != NULL) {
44147c478bd9Sstevel@tonic-gate 				recurse_dev_re(new_path, slash + 1, rd);
44157c478bd9Sstevel@tonic-gate 			} else {
44167c478bd9Sstevel@tonic-gate 				/* reached the leaf component of path_re */
44177c478bd9Sstevel@tonic-gate 				vprint(RECURSEDEV_MID,
44180a653502Swroche 				    "recurse_dev_re: calling fcn\n");
44197c478bd9Sstevel@tonic-gate 				(*(rd->fcn))(new_path, rd->data);
44207c478bd9Sstevel@tonic-gate 			}
44217c478bd9Sstevel@tonic-gate 		}
44227c478bd9Sstevel@tonic-gate 	}
44237c478bd9Sstevel@tonic-gate 
44247c478bd9Sstevel@tonic-gate 	regfree(&re1);
44257c478bd9Sstevel@tonic-gate 
44267c478bd9Sstevel@tonic-gate out:
442773de625bSjg 	finddev_close(fhandle);
44287c478bd9Sstevel@tonic-gate }
44297c478bd9Sstevel@tonic-gate 
44307c478bd9Sstevel@tonic-gate /*
44317c478bd9Sstevel@tonic-gate  *  Found a devpath which matches a RE in the remove structure.
44327c478bd9Sstevel@tonic-gate  *  Now check to see if it is dangling.
44337c478bd9Sstevel@tonic-gate  */
44347c478bd9Sstevel@tonic-gate static void
44357c478bd9Sstevel@tonic-gate matching_dev(char *devpath, void *data)
44367c478bd9Sstevel@tonic-gate {
44377c478bd9Sstevel@tonic-gate 	cleanup_data_t *cleanup_data = data;
4438aa646b9dSvikram 	int norm_len = strlen(dev_dir) + strlen("/");
4439aa646b9dSvikram 	int ret;
44407c478bd9Sstevel@tonic-gate 	char *fcn = "matching_dev: ";
44417c478bd9Sstevel@tonic-gate 
44427c478bd9Sstevel@tonic-gate 	vprint(RECURSEDEV_MID, "%sexamining devpath = '%s'\n", fcn,
44430a653502Swroche 	    devpath);
44447c478bd9Sstevel@tonic-gate 
4445aa646b9dSvikram 	/*
4446aa646b9dSvikram 	 * If the link is in the no-further-process hash
4447aa646b9dSvikram 	 * don't do any remove operation on it.
4448aa646b9dSvikram 	 */
4449aa646b9dSvikram 	if (nfphash_lookup(devpath + norm_len) != NULL)
4450aa646b9dSvikram 		return;
4451aa646b9dSvikram 
445294c894bbSVikram Hegde 	/*
445394c894bbSVikram Hegde 	 * Dangling check will work whether "alias" or "current"
445494c894bbSVikram Hegde 	 * so no need to redirect.
445594c894bbSVikram Hegde 	 */
44567c478bd9Sstevel@tonic-gate 	if (resolve_link(devpath, NULL, NULL, NULL, 1) == TRUE) {
44577c478bd9Sstevel@tonic-gate 		if (call_minor_init(cleanup_data->rm->modptr) ==
44580a653502Swroche 		    DEVFSADM_FAILURE) {
44597c478bd9Sstevel@tonic-gate 			return;
44607c478bd9Sstevel@tonic-gate 		}
44617c478bd9Sstevel@tonic-gate 
4462aa646b9dSvikram 		devpath += norm_len;
44637c478bd9Sstevel@tonic-gate 
44640a653502Swroche 		vprint(RECURSEDEV_MID, "%scalling callback %s\n", fcn, devpath);
4465aa646b9dSvikram 		if (cleanup_data->rm->remove->flags & RM_NOINTERPOSE)
4466aa646b9dSvikram 			((void (*)(char *))
44670a653502Swroche 			    (cleanup_data->rm->remove->callback_fcn))(devpath);
4468aa646b9dSvikram 		else {
4469aa646b9dSvikram 			ret = ((int (*)(char *))
4470aa646b9dSvikram 			    (cleanup_data->rm->remove->callback_fcn))(devpath);
4471aa646b9dSvikram 			if (ret == DEVFSADM_TERMINATE) {
4472aa646b9dSvikram 				/*
4473aa646b9dSvikram 				 * We want no further remove processing for
4474aa646b9dSvikram 				 * this link. Add it to the nfp_hash;
4475aa646b9dSvikram 				 */
4476aa646b9dSvikram 				nfphash_insert(devpath);
4477aa646b9dSvikram 			}
4478aa646b9dSvikram 		}
44797c478bd9Sstevel@tonic-gate 	}
44807c478bd9Sstevel@tonic-gate }
44817c478bd9Sstevel@tonic-gate 
44827c478bd9Sstevel@tonic-gate int
448394c894bbSVikram Hegde devfsadm_read_link(di_node_t anynode, char *link, char **devfs_path)
44847c478bd9Sstevel@tonic-gate {
44857c478bd9Sstevel@tonic-gate 	char devlink[PATH_MAX];
448694c894bbSVikram Hegde 	char *path;
44877c478bd9Sstevel@tonic-gate 
44887c478bd9Sstevel@tonic-gate 	*devfs_path = NULL;
44897c478bd9Sstevel@tonic-gate 
44907c478bd9Sstevel@tonic-gate 	/* prepend link with dev_dir contents */
44917c478bd9Sstevel@tonic-gate 	(void) strcpy(devlink, dev_dir);
44927c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, "/");
44937c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, link);
44947c478bd9Sstevel@tonic-gate 
44957c478bd9Sstevel@tonic-gate 	/* We *don't* want a stat of the /devices node */
449694c894bbSVikram Hegde 	path = NULL;
449794c894bbSVikram Hegde 	(void) resolve_link(devlink, NULL, NULL, &path, 0);
44986c70e1f8SJerry Gilliam 	if (path != NULL) {
44996c70e1f8SJerry Gilliam 		/* redirect if alias to current */
45006c70e1f8SJerry Gilliam 		*devfs_path = di_alias2curr(anynode, path);
45016c70e1f8SJerry Gilliam 		free(path);
45026c70e1f8SJerry Gilliam 	}
45037c478bd9Sstevel@tonic-gate 	return (*devfs_path ? DEVFSADM_SUCCESS : DEVFSADM_FAILURE);
45047c478bd9Sstevel@tonic-gate }
45057c478bd9Sstevel@tonic-gate 
45067c478bd9Sstevel@tonic-gate int
450794c894bbSVikram Hegde devfsadm_link_valid(di_node_t anynode, char *link)
45087c478bd9Sstevel@tonic-gate {
45097c478bd9Sstevel@tonic-gate 	struct stat sb;
451094c894bbSVikram Hegde 	char devlink[PATH_MAX + 1], *contents, *raw_contents;
45117c478bd9Sstevel@tonic-gate 	int rv, type;
451245916cd2Sjpk 	int instance = 0;
45137c478bd9Sstevel@tonic-gate 
45147c478bd9Sstevel@tonic-gate 	/* prepend link with dev_dir contents */
45157c478bd9Sstevel@tonic-gate 	(void) strcpy(devlink, dev_dir);
45167c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, "/");
45177c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, link);
45187c478bd9Sstevel@tonic-gate 
4519facf4a8dSllai 	if (!device_exists(devlink) || lstat(devlink, &sb) != 0) {
45207c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FALSE);
45217c478bd9Sstevel@tonic-gate 	}
45227c478bd9Sstevel@tonic-gate 
452394c894bbSVikram Hegde 	raw_contents = NULL;
45247c478bd9Sstevel@tonic-gate 	type = 0;
452594c894bbSVikram Hegde 	if (resolve_link(devlink, &raw_contents, &type, NULL, 1) == TRUE) {
45267c478bd9Sstevel@tonic-gate 		rv = DEVFSADM_FALSE;
45277c478bd9Sstevel@tonic-gate 	} else {
45287c478bd9Sstevel@tonic-gate 		rv = DEVFSADM_TRUE;
45297c478bd9Sstevel@tonic-gate 	}
45307c478bd9Sstevel@tonic-gate 
453194c894bbSVikram Hegde 	/*
453294c894bbSVikram Hegde 	 * resolve alias paths for primary links
453394c894bbSVikram Hegde 	 */
453494c894bbSVikram Hegde 	contents = raw_contents;
453594c894bbSVikram Hegde 	if (type == DI_PRIMARY_LINK) {
453694c894bbSVikram Hegde 		contents = di_alias2curr(anynode, raw_contents);
453794c894bbSVikram Hegde 		free(raw_contents);
453894c894bbSVikram Hegde 	}
453994c894bbSVikram Hegde 
45407c478bd9Sstevel@tonic-gate 	/*
45417c478bd9Sstevel@tonic-gate 	 * The link exists. Add it to the database
45427c478bd9Sstevel@tonic-gate 	 */
45437c478bd9Sstevel@tonic-gate 	(void) di_devlink_add_link(devlink_cache, link, contents, type);
454445916cd2Sjpk 	if (system_labeled && (rv == DEVFSADM_TRUE) &&
454545916cd2Sjpk 	    strstr(devlink, DA_AUDIO_NAME) && contents) {
454645916cd2Sjpk 		(void) sscanf(contents, "%*[a-z]%d", &instance);
454745916cd2Sjpk 		(void) da_add_list(&devlist, devlink, instance,
454845916cd2Sjpk 		    DA_ADD|DA_AUDIO);
454945916cd2Sjpk 		_update_devalloc_db(&devlist, 0, DA_ADD, NULL, root_dir);
455045916cd2Sjpk 	}
45517c478bd9Sstevel@tonic-gate 	free(contents);
45527c478bd9Sstevel@tonic-gate 
45537c478bd9Sstevel@tonic-gate 	return (rv);
45547c478bd9Sstevel@tonic-gate }
45557c478bd9Sstevel@tonic-gate 
45567c478bd9Sstevel@tonic-gate /*
45577c478bd9Sstevel@tonic-gate  * devpath: Absolute path to /dev link
45587c478bd9Sstevel@tonic-gate  * content_p: Returns malloced string (link content)
45597c478bd9Sstevel@tonic-gate  * type_p: Returns link type: primary or secondary
45607c478bd9Sstevel@tonic-gate  * devfs_path: Returns malloced string: /devices path w/out "/devices"
45617c478bd9Sstevel@tonic-gate  * dangle: if set, check if link is dangling
45627c478bd9Sstevel@tonic-gate  * Returns:
45637c478bd9Sstevel@tonic-gate  *	TRUE if dangling
45647c478bd9Sstevel@tonic-gate  *	FALSE if not or if caller doesn't care
45657c478bd9Sstevel@tonic-gate  * Caller is assumed to have initialized pointer contents to NULL
456694c894bbSVikram Hegde  *
45677c478bd9Sstevel@tonic-gate  */
45687c478bd9Sstevel@tonic-gate static int
45697c478bd9Sstevel@tonic-gate resolve_link(char *devpath, char **content_p, int *type_p, char **devfs_path,
45707c478bd9Sstevel@tonic-gate     int dangle)
45717c478bd9Sstevel@tonic-gate {
45727c478bd9Sstevel@tonic-gate 	char contents[PATH_MAX + 1];
45737c478bd9Sstevel@tonic-gate 	char stage_link[PATH_MAX + 1];
45747c478bd9Sstevel@tonic-gate 	char *fcn = "resolve_link: ";
45757c478bd9Sstevel@tonic-gate 	char *ptr;
45767c478bd9Sstevel@tonic-gate 	int linksize;
45777c478bd9Sstevel@tonic-gate 	int rv = TRUE;
45787c478bd9Sstevel@tonic-gate 	struct stat sb;
45797c478bd9Sstevel@tonic-gate 
458094c894bbSVikram Hegde 	/*
458194c894bbSVikram Hegde 	 * This routine will return the "raw" contents. It is upto the
458294c894bbSVikram Hegde 	 * the caller to redirect "alias" to "current" (or vice versa)
458394c894bbSVikram Hegde 	 */
45847c478bd9Sstevel@tonic-gate 	linksize = readlink(devpath, contents, PATH_MAX);
45857c478bd9Sstevel@tonic-gate 
45867c478bd9Sstevel@tonic-gate 	if (linksize <= 0) {
45877c478bd9Sstevel@tonic-gate 		return (FALSE);
45887c478bd9Sstevel@tonic-gate 	} else {
45897c478bd9Sstevel@tonic-gate 		contents[linksize] = '\0';
45907c478bd9Sstevel@tonic-gate 	}
45917c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%s %s -> %s\n", fcn, devpath, contents);
45927c478bd9Sstevel@tonic-gate 
45937c478bd9Sstevel@tonic-gate 	if (content_p) {
45947c478bd9Sstevel@tonic-gate 		*content_p = s_strdup(contents);
45957c478bd9Sstevel@tonic-gate 	}
45967c478bd9Sstevel@tonic-gate 
45977c478bd9Sstevel@tonic-gate 	/*
45987c478bd9Sstevel@tonic-gate 	 * Check to see if this is a link pointing to another link in /dev.  The
45997c478bd9Sstevel@tonic-gate 	 * cheap way to do this is to look for a lack of ../devices/.
46007c478bd9Sstevel@tonic-gate 	 */
46017c478bd9Sstevel@tonic-gate 
46027c478bd9Sstevel@tonic-gate 	if (is_minor_node(contents, &ptr) == DEVFSADM_FALSE) {
46037c478bd9Sstevel@tonic-gate 
46047c478bd9Sstevel@tonic-gate 		if (type_p) {
46057c478bd9Sstevel@tonic-gate 			*type_p = DI_SECONDARY_LINK;
46067c478bd9Sstevel@tonic-gate 		}
46077c478bd9Sstevel@tonic-gate 
46087c478bd9Sstevel@tonic-gate 		/*
46097c478bd9Sstevel@tonic-gate 		 * assume that linkcontents is really a pointer to another
46107c478bd9Sstevel@tonic-gate 		 * link, and if so recurse and read its link contents.
46117c478bd9Sstevel@tonic-gate 		 */
46127c478bd9Sstevel@tonic-gate 		if (strncmp(contents, DEV "/", strlen(DEV) + 1) == 0)  {
46137c478bd9Sstevel@tonic-gate 			(void) strcpy(stage_link, dev_dir);
46147c478bd9Sstevel@tonic-gate 			(void) strcat(stage_link, "/");
46157c478bd9Sstevel@tonic-gate 			(void) strcpy(stage_link,
46160a653502Swroche 			    &contents[strlen(DEV) + strlen("/")]);
46177c478bd9Sstevel@tonic-gate 		} else {
46187c478bd9Sstevel@tonic-gate 			if ((ptr = strrchr(devpath, '/')) == NULL) {
46197c478bd9Sstevel@tonic-gate 				vprint(REMOVE_MID, "%s%s -> %s invalid link. "
46200a653502Swroche 				    "missing '/'\n", fcn, devpath, contents);
46217c478bd9Sstevel@tonic-gate 				return (TRUE);
46227c478bd9Sstevel@tonic-gate 			}
46237c478bd9Sstevel@tonic-gate 			*ptr = '\0';
46247c478bd9Sstevel@tonic-gate 			(void) strcpy(stage_link, devpath);
46257c478bd9Sstevel@tonic-gate 			*ptr = '/';
46267c478bd9Sstevel@tonic-gate 			(void) strcat(stage_link, "/");
46277c478bd9Sstevel@tonic-gate 			(void) strcat(stage_link, contents);
46287c478bd9Sstevel@tonic-gate 		}
46297c478bd9Sstevel@tonic-gate 		return (resolve_link(stage_link, NULL, NULL, devfs_path,
46307c478bd9Sstevel@tonic-gate 		    dangle));
46317c478bd9Sstevel@tonic-gate 	}
46327c478bd9Sstevel@tonic-gate 
46337c478bd9Sstevel@tonic-gate 	/* Current link points at a /devices minor node */
46347c478bd9Sstevel@tonic-gate 	if (type_p) {
46357c478bd9Sstevel@tonic-gate 		*type_p = DI_PRIMARY_LINK;
46367c478bd9Sstevel@tonic-gate 	}
46377c478bd9Sstevel@tonic-gate 
46387c478bd9Sstevel@tonic-gate 	if (devfs_path)
46397c478bd9Sstevel@tonic-gate 		*devfs_path = s_strdup(ptr);
46407c478bd9Sstevel@tonic-gate 
46417c478bd9Sstevel@tonic-gate 	rv = FALSE;
46427c478bd9Sstevel@tonic-gate 	if (dangle)
46437c478bd9Sstevel@tonic-gate 		rv = (stat(ptr - strlen(DEVICES), &sb) == -1);
46447c478bd9Sstevel@tonic-gate 
46457c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%slink=%s, returning %s\n", fcn,
46460a653502Swroche 	    devpath, ((rv == TRUE) ? "TRUE" : "FALSE"));
46477c478bd9Sstevel@tonic-gate 
46487c478bd9Sstevel@tonic-gate 	return (rv);
46497c478bd9Sstevel@tonic-gate }
46507c478bd9Sstevel@tonic-gate 
46517c478bd9Sstevel@tonic-gate /*
46527c478bd9Sstevel@tonic-gate  * Returns the substring of interest, given a path.
46537c478bd9Sstevel@tonic-gate  */
46547c478bd9Sstevel@tonic-gate static char *
46557c478bd9Sstevel@tonic-gate alloc_cmp_str(const char *path, devfsadm_enumerate_t *dep)
46567c478bd9Sstevel@tonic-gate {
46577c478bd9Sstevel@tonic-gate 	uint_t match;
46587c478bd9Sstevel@tonic-gate 	char *np, *ap, *mp;
46597c478bd9Sstevel@tonic-gate 	char *cmp_str = NULL;
46607c478bd9Sstevel@tonic-gate 	char at[] = "@";
46617c478bd9Sstevel@tonic-gate 	char *fcn = "alloc_cmp_str";
46627c478bd9Sstevel@tonic-gate 
46637c478bd9Sstevel@tonic-gate 	np = ap = mp = NULL;
46647c478bd9Sstevel@tonic-gate 
46657c478bd9Sstevel@tonic-gate 	/*
46667c478bd9Sstevel@tonic-gate 	 * extract match flags from the flags argument.
46677c478bd9Sstevel@tonic-gate 	 */
46687c478bd9Sstevel@tonic-gate 	match = (dep->flags & MATCH_MASK);
46697c478bd9Sstevel@tonic-gate 
46707c478bd9Sstevel@tonic-gate 	vprint(ENUM_MID, "%s: enumeration match type: 0x%x"
46717c478bd9Sstevel@tonic-gate 	    " path: %s\n", fcn, match, path);
46727c478bd9Sstevel@tonic-gate 
46737c478bd9Sstevel@tonic-gate 	/*
46747c478bd9Sstevel@tonic-gate 	 * MATCH_CALLBACK and MATCH_ALL are the only flags
46757c478bd9Sstevel@tonic-gate 	 * which may be used if "path" is a /dev path
46767c478bd9Sstevel@tonic-gate 	 */
46777c478bd9Sstevel@tonic-gate 	if (match == MATCH_CALLBACK) {
46787c478bd9Sstevel@tonic-gate 		if (dep->sel_fcn == NULL) {
46797c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid enumerate"
46807c478bd9Sstevel@tonic-gate 			    " callback: path: %s\n", fcn, path);
46817c478bd9Sstevel@tonic-gate 			return (NULL);
46827c478bd9Sstevel@tonic-gate 		}
46837c478bd9Sstevel@tonic-gate 		cmp_str = dep->sel_fcn(path, dep->cb_arg);
46847c478bd9Sstevel@tonic-gate 		return (cmp_str);
46857c478bd9Sstevel@tonic-gate 	}
46867c478bd9Sstevel@tonic-gate 
46877c478bd9Sstevel@tonic-gate 	cmp_str = s_strdup(path);
46887c478bd9Sstevel@tonic-gate 
46897c478bd9Sstevel@tonic-gate 	if (match == MATCH_ALL) {
46907c478bd9Sstevel@tonic-gate 		return (cmp_str);
46917c478bd9Sstevel@tonic-gate 	}
46927c478bd9Sstevel@tonic-gate 
46937c478bd9Sstevel@tonic-gate 	/*
46947c478bd9Sstevel@tonic-gate 	 * The remaining flags make sense only for /devices
46957c478bd9Sstevel@tonic-gate 	 * paths
46967c478bd9Sstevel@tonic-gate 	 */
46977c478bd9Sstevel@tonic-gate 	if ((mp = strrchr(cmp_str, ':')) == NULL) {
46987c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: invalid path: %s\n",
46997c478bd9Sstevel@tonic-gate 		    fcn, path);
47007c478bd9Sstevel@tonic-gate 		goto err;
47017c478bd9Sstevel@tonic-gate 	}
47027c478bd9Sstevel@tonic-gate 
47037c478bd9Sstevel@tonic-gate 	if (match == MATCH_MINOR) {
47047c478bd9Sstevel@tonic-gate 		/* A NULL "match_arg" values implies entire minor */
47057c478bd9Sstevel@tonic-gate 		if (get_component(mp + 1, dep->match_arg) == NULL) {
47067c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid minor component:"
47077c478bd9Sstevel@tonic-gate 			    " path: %s\n", fcn, path);
47087c478bd9Sstevel@tonic-gate 			goto err;
47097c478bd9Sstevel@tonic-gate 		}
47107c478bd9Sstevel@tonic-gate 		return (cmp_str);
47117c478bd9Sstevel@tonic-gate 	}
47127c478bd9Sstevel@tonic-gate 
47137c478bd9Sstevel@tonic-gate 	if ((np = strrchr(cmp_str, '/')) == NULL) {
47147c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: invalid path: %s\n", fcn, path);
47157c478bd9Sstevel@tonic-gate 		goto err;
47167c478bd9Sstevel@tonic-gate 	}
47177c478bd9Sstevel@tonic-gate 
47187c478bd9Sstevel@tonic-gate 	if (match == MATCH_PARENT) {
47197c478bd9Sstevel@tonic-gate 		if (strcmp(cmp_str, "/") == 0) {
47207c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid path: %s\n",
47217c478bd9Sstevel@tonic-gate 			    fcn, path);
47227c478bd9Sstevel@tonic-gate 			goto err;
47237c478bd9Sstevel@tonic-gate 		}
47247c478bd9Sstevel@tonic-gate 
47257c478bd9Sstevel@tonic-gate 		if (np == cmp_str) {
47267c478bd9Sstevel@tonic-gate 			*(np + 1) = '\0';
47277c478bd9Sstevel@tonic-gate 		} else {
47287c478bd9Sstevel@tonic-gate 			*np = '\0';
47297c478bd9Sstevel@tonic-gate 		}
47307c478bd9Sstevel@tonic-gate 		return (cmp_str);
47317c478bd9Sstevel@tonic-gate 	}
47327c478bd9Sstevel@tonic-gate 
47337c478bd9Sstevel@tonic-gate 	/* ap can be NULL - Leaf address may not exist or be empty string */
47347c478bd9Sstevel@tonic-gate 	ap = strchr(np+1, '@');
47357c478bd9Sstevel@tonic-gate 
47367c478bd9Sstevel@tonic-gate 	/* minor is no longer of interest */
47377c478bd9Sstevel@tonic-gate 	*mp = '\0';
47387c478bd9Sstevel@tonic-gate 
47397c478bd9Sstevel@tonic-gate 	if (match == MATCH_NODE) {
47407c478bd9Sstevel@tonic-gate 		if (ap)
47417c478bd9Sstevel@tonic-gate 			*ap = '\0';
47427c478bd9Sstevel@tonic-gate 		return (cmp_str);
47437c478bd9Sstevel@tonic-gate 	} else if (match == MATCH_ADDR) {
47447c478bd9Sstevel@tonic-gate 		/*
47457c478bd9Sstevel@tonic-gate 		 * The empty string is a valid address. The only MATCH_ADDR
47467c478bd9Sstevel@tonic-gate 		 * allowed in this case is against the whole address or
47477c478bd9Sstevel@tonic-gate 		 * the first component of the address (match_arg=NULL/"0"/"1")
47487c478bd9Sstevel@tonic-gate 		 * Note that in this case, the path won't have an "@"
47497c478bd9Sstevel@tonic-gate 		 * As a result ap will be NULL. We fake up an ap = @'\0'
47507c478bd9Sstevel@tonic-gate 		 * so that get_component() will work correctly.
47517c478bd9Sstevel@tonic-gate 		 */
47527c478bd9Sstevel@tonic-gate 		if (ap == NULL) {
47537c478bd9Sstevel@tonic-gate 			ap = at;
47547c478bd9Sstevel@tonic-gate 		}
47557c478bd9Sstevel@tonic-gate 
47567c478bd9Sstevel@tonic-gate 		if (get_component(ap + 1, dep->match_arg) == NULL) {
47577c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid leaf addr. component:"
47587c478bd9Sstevel@tonic-gate 			    " path: %s\n", fcn, path);
47597c478bd9Sstevel@tonic-gate 			goto err;
47607c478bd9Sstevel@tonic-gate 		}
47617c478bd9Sstevel@tonic-gate 		return (cmp_str);
47627c478bd9Sstevel@tonic-gate 	}
47637c478bd9Sstevel@tonic-gate 
47647c478bd9Sstevel@tonic-gate 	vprint(ENUM_MID, "%s: invalid enumeration flags: 0x%x"
47650a653502Swroche 	    " path: %s\n", fcn, dep->flags, path);
47667c478bd9Sstevel@tonic-gate 
47677c478bd9Sstevel@tonic-gate 	/*FALLTHRU*/
47687c478bd9Sstevel@tonic-gate err:
47697c478bd9Sstevel@tonic-gate 	free(cmp_str);
47707c478bd9Sstevel@tonic-gate 	return (NULL);
47717c478bd9Sstevel@tonic-gate }
47727c478bd9Sstevel@tonic-gate 
47737c478bd9Sstevel@tonic-gate 
47747c478bd9Sstevel@tonic-gate /*
47757c478bd9Sstevel@tonic-gate  * "str" is expected to be a string with components separated by ','
47767c478bd9Sstevel@tonic-gate  * The terminating null char is considered a separator.
47777c478bd9Sstevel@tonic-gate  * get_component() will remove the portion of the string beyond
47787c478bd9Sstevel@tonic-gate  * the component indicated.
47797c478bd9Sstevel@tonic-gate  * If comp_str is NULL, the entire "str" is returned.
47807c478bd9Sstevel@tonic-gate  */
47817c478bd9Sstevel@tonic-gate static char *
47827c478bd9Sstevel@tonic-gate get_component(char *str, const char *comp_str)
47837c478bd9Sstevel@tonic-gate {
47847c478bd9Sstevel@tonic-gate 	long comp;
47857c478bd9Sstevel@tonic-gate 	char *cp;
47867c478bd9Sstevel@tonic-gate 
47877c478bd9Sstevel@tonic-gate 	if (str == NULL) {
47887c478bd9Sstevel@tonic-gate 		return (NULL);
47897c478bd9Sstevel@tonic-gate 	}
47907c478bd9Sstevel@tonic-gate 
47917c478bd9Sstevel@tonic-gate 	if (comp_str == NULL) {
47927c478bd9Sstevel@tonic-gate 		return (str);
47937c478bd9Sstevel@tonic-gate 	}
47947c478bd9Sstevel@tonic-gate 
47957c478bd9Sstevel@tonic-gate 	errno = 0;
47967c478bd9Sstevel@tonic-gate 	comp = strtol(comp_str, &cp, 10);
47977c478bd9Sstevel@tonic-gate 	if (errno != 0 || *cp != '\0' || comp < 0) {
47987c478bd9Sstevel@tonic-gate 		return (NULL);
47997c478bd9Sstevel@tonic-gate 	}
48007c478bd9Sstevel@tonic-gate 
48017c478bd9Sstevel@tonic-gate 	if (comp == 0)
48027c478bd9Sstevel@tonic-gate 		return (str);
48037c478bd9Sstevel@tonic-gate 
48047c478bd9Sstevel@tonic-gate 	for (cp = str; ; cp++) {
48057c478bd9Sstevel@tonic-gate 		if (*cp == ',' || *cp == '\0')
48067c478bd9Sstevel@tonic-gate 			comp--;
48077c478bd9Sstevel@tonic-gate 		if (*cp == '\0' || comp <= 0) {
48087c478bd9Sstevel@tonic-gate 			break;
48097c478bd9Sstevel@tonic-gate 		}
48107c478bd9Sstevel@tonic-gate 	}
48117c478bd9Sstevel@tonic-gate 
48127c478bd9Sstevel@tonic-gate 	if (comp == 0) {
48137c478bd9Sstevel@tonic-gate 		*cp = '\0';
48147c478bd9Sstevel@tonic-gate 	} else {
48157c478bd9Sstevel@tonic-gate 		str = NULL;
48167c478bd9Sstevel@tonic-gate 	}
48177c478bd9Sstevel@tonic-gate 
48187c478bd9Sstevel@tonic-gate 	return (str);
48197c478bd9Sstevel@tonic-gate }
48207c478bd9Sstevel@tonic-gate 
48217c478bd9Sstevel@tonic-gate 
48227c478bd9Sstevel@tonic-gate /*
48237c478bd9Sstevel@tonic-gate  * Enumerate serves as a generic counter as well as a means to determine
48247c478bd9Sstevel@tonic-gate  * logical unit/controller numbers for such items as disk and tape
48257c478bd9Sstevel@tonic-gate  * drives.
48267c478bd9Sstevel@tonic-gate  *
48277c478bd9Sstevel@tonic-gate  * rules[] is an array of  devfsadm_enumerate_t structures which defines
48287c478bd9Sstevel@tonic-gate  * the enumeration rules to be used for a specified set of links in /dev.
48297c478bd9Sstevel@tonic-gate  * The set of links is specified through regular expressions (of the flavor
48307c478bd9Sstevel@tonic-gate  * described in regex(5)). These regular expressions are used to determine
48317c478bd9Sstevel@tonic-gate  * the set of links in /dev to examine. The last path component in these
48327c478bd9Sstevel@tonic-gate  * regular expressions MUST contain a parenthesized subexpression surrounding
48337c478bd9Sstevel@tonic-gate  * the RE which is to be considered the enumerating component. The subexp
48347c478bd9Sstevel@tonic-gate  * member in a rule is the subexpression number of the enumerating
48357c478bd9Sstevel@tonic-gate  * component. Subexpressions in the last path component are numbered starting
48367c478bd9Sstevel@tonic-gate  * from 1.
48377c478bd9Sstevel@tonic-gate  *
48387c478bd9Sstevel@tonic-gate  * A cache of current id assignments is built up from existing symlinks and
48397c478bd9Sstevel@tonic-gate  * new assignments use the lowest unused id. Assignments are based on a
48407c478bd9Sstevel@tonic-gate  * match of a specified substring of a symlink's contents. If the specified
48417c478bd9Sstevel@tonic-gate  * component for the devfs_path argument matches the corresponding substring
48427c478bd9Sstevel@tonic-gate  * for a existing symlink's contents, the cached id is returned. Else, a new
48437c478bd9Sstevel@tonic-gate  * id is created and returned in *buf. *buf must be freed by the caller.
48447c478bd9Sstevel@tonic-gate  *
48457c478bd9Sstevel@tonic-gate  * An id assignment may be governed by a combination of rules, each rule
48467c478bd9Sstevel@tonic-gate  * applicable to a different subset of links in /dev. For example, controller
48477c478bd9Sstevel@tonic-gate  * numbers may be determined by a combination of disk symlinks in /dev/[r]dsk
48487c478bd9Sstevel@tonic-gate  * and controller symlinks in /dev/cfg, with the two sets requiring different
48497c478bd9Sstevel@tonic-gate  * rules to derive the "substring of interest". In such cases, the rules
48507c478bd9Sstevel@tonic-gate  * array will have more than one element.
48517c478bd9Sstevel@tonic-gate  */
48527c478bd9Sstevel@tonic-gate int
48537c478bd9Sstevel@tonic-gate devfsadm_enumerate_int(char *devfs_path, int index, char **buf,
4854406fc510SToomas Soome     devfsadm_enumerate_t rules[], int nrules)
48557c478bd9Sstevel@tonic-gate {
48567c478bd9Sstevel@tonic-gate 	return (find_enum_id(rules, nrules,
48577c478bd9Sstevel@tonic-gate 	    devfs_path, index, "0", INTEGER, buf, 0));
48587c478bd9Sstevel@tonic-gate }
48597c478bd9Sstevel@tonic-gate 
48607c478bd9Sstevel@tonic-gate int
4861540b2cc7SHans Rosenfeld ctrl_enumerate_int(char *devfs_path, int index, char **buf,
4862540b2cc7SHans Rosenfeld     devfsadm_enumerate_t rules[], int nrules, int multiple,
4863540b2cc7SHans Rosenfeld     boolean_t scsi_vhci)
48647c478bd9Sstevel@tonic-gate {
48657c478bd9Sstevel@tonic-gate 	return (find_enum_id(rules, nrules,
4866540b2cc7SHans Rosenfeld 	    devfs_path, index, scsi_vhci ? "0" : "1", INTEGER, buf, multiple));
48677c478bd9Sstevel@tonic-gate }
48687c478bd9Sstevel@tonic-gate 
48697c478bd9Sstevel@tonic-gate /*
48707c478bd9Sstevel@tonic-gate  * Same as above, but allows a starting value to be specified.
48717c478bd9Sstevel@tonic-gate  * Private to devfsadm.... used by devlinks.
48727c478bd9Sstevel@tonic-gate  */
48737c478bd9Sstevel@tonic-gate static int
48747c478bd9Sstevel@tonic-gate devfsadm_enumerate_int_start(char *devfs_path, int index, char **buf,
4875406fc510SToomas Soome     devfsadm_enumerate_t rules[], int nrules, char *start)
48767c478bd9Sstevel@tonic-gate {
48777c478bd9Sstevel@tonic-gate 	return (find_enum_id(rules, nrules,
48787c478bd9Sstevel@tonic-gate 	    devfs_path, index, start, INTEGER, buf, 0));
48797c478bd9Sstevel@tonic-gate }
48807c478bd9Sstevel@tonic-gate 
48817c478bd9Sstevel@tonic-gate /*
48827c478bd9Sstevel@tonic-gate  *  devfsadm_enumerate_char serves as a generic counter returning
48837c478bd9Sstevel@tonic-gate  *  a single letter.
48847c478bd9Sstevel@tonic-gate  */
48857c478bd9Sstevel@tonic-gate int
48867c478bd9Sstevel@tonic-gate devfsadm_enumerate_char(char *devfs_path, int index, char **buf,
4887406fc510SToomas Soome     devfsadm_enumerate_t rules[], int nrules)
48887c478bd9Sstevel@tonic-gate {
48897c478bd9Sstevel@tonic-gate 	return (find_enum_id(rules, nrules,
48907c478bd9Sstevel@tonic-gate 	    devfs_path, index, "a", LETTER, buf, 0));
48917c478bd9Sstevel@tonic-gate }
48927c478bd9Sstevel@tonic-gate 
48937c478bd9Sstevel@tonic-gate /*
48947c478bd9Sstevel@tonic-gate  * Same as above, but allows a starting char to be specified.
48957c478bd9Sstevel@tonic-gate  * Private to devfsadm - used by ports module (port_link.c)
48967c478bd9Sstevel@tonic-gate  */
48977c478bd9Sstevel@tonic-gate int
48987c478bd9Sstevel@tonic-gate devfsadm_enumerate_char_start(char *devfs_path, int index, char **buf,
4899406fc510SToomas Soome     devfsadm_enumerate_t rules[], int nrules, char *start)
49007c478bd9Sstevel@tonic-gate {
49017c478bd9Sstevel@tonic-gate 	return (find_enum_id(rules, nrules,
49027c478bd9Sstevel@tonic-gate 	    devfs_path, index, start, LETTER, buf, 0));
49037c478bd9Sstevel@tonic-gate }
49047c478bd9Sstevel@tonic-gate 
49057c478bd9Sstevel@tonic-gate 
49067c478bd9Sstevel@tonic-gate /*
49077c478bd9Sstevel@tonic-gate  * For a given numeral_set (see get_cached_set for desc of numeral_set),
49087c478bd9Sstevel@tonic-gate  * search all cached entries looking for matches on a specified substring
49097c478bd9Sstevel@tonic-gate  * of devfs_path. The substring is derived from devfs_path based on the
49107c478bd9Sstevel@tonic-gate  * rule specified by "index". If a match is found on a cached entry,
49117c478bd9Sstevel@tonic-gate  * return the enumerated id in buf. Otherwise, create a new id by calling
49127c478bd9Sstevel@tonic-gate  * new_id, then cache and return that entry.
49137c478bd9Sstevel@tonic-gate  */
49147c478bd9Sstevel@tonic-gate static int
49157c478bd9Sstevel@tonic-gate find_enum_id(devfsadm_enumerate_t rules[], int nrules,
4916406fc510SToomas Soome     char *devfs_path, int index, char *min, int type, char **buf,
4917406fc510SToomas Soome     int multiple)
49187c478bd9Sstevel@tonic-gate {
49197c478bd9Sstevel@tonic-gate 	numeral_t *matchnp;
49207c478bd9Sstevel@tonic-gate 	numeral_t *numeral;
49217c478bd9Sstevel@tonic-gate 	int matchcount = 0;
49227c478bd9Sstevel@tonic-gate 	char *cmp_str;
49237c478bd9Sstevel@tonic-gate 	char *fcn = "find_enum_id";
49247c478bd9Sstevel@tonic-gate 	numeral_set_t *set;
49257c478bd9Sstevel@tonic-gate 
49267c478bd9Sstevel@tonic-gate 	if (rules == NULL) {
49277c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: no rules. path: %s\n",
49287c478bd9Sstevel@tonic-gate 		    fcn, devfs_path ? devfs_path : "<NULL path>");
49297c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
49307c478bd9Sstevel@tonic-gate 	}
49317c478bd9Sstevel@tonic-gate 
49327c478bd9Sstevel@tonic-gate 	if (devfs_path == NULL) {
49337c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: NULL path\n", fcn);
49347c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
49357c478bd9Sstevel@tonic-gate 	}
49367c478bd9Sstevel@tonic-gate 
49377c478bd9Sstevel@tonic-gate 	if (nrules <= 0 || index < 0 || index >= nrules || buf == NULL) {
49387c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: invalid arguments. path: %s\n",
49397c478bd9Sstevel@tonic-gate 		    fcn, devfs_path);
49407c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
49417c478bd9Sstevel@tonic-gate 	}
49427c478bd9Sstevel@tonic-gate 
49437c478bd9Sstevel@tonic-gate 	*buf = NULL;
49447c478bd9Sstevel@tonic-gate 
49457c478bd9Sstevel@tonic-gate 
49467c478bd9Sstevel@tonic-gate 	cmp_str = alloc_cmp_str(devfs_path, &rules[index]);
49477c478bd9Sstevel@tonic-gate 	if (cmp_str == NULL) {
49487c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
49497c478bd9Sstevel@tonic-gate 	}
49507c478bd9Sstevel@tonic-gate 
49517c478bd9Sstevel@tonic-gate 	if ((set = get_enum_cache(rules, nrules)) == NULL) {
49527c478bd9Sstevel@tonic-gate 		free(cmp_str);
49537c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
49547c478bd9Sstevel@tonic-gate 	}
49557c478bd9Sstevel@tonic-gate 
49567c478bd9Sstevel@tonic-gate 	assert(nrules == set->re_count);
49577c478bd9Sstevel@tonic-gate 
49587c478bd9Sstevel@tonic-gate 	/*
49597c478bd9Sstevel@tonic-gate 	 * Check and see if a matching entry is already cached.
49607c478bd9Sstevel@tonic-gate 	 */
49617c478bd9Sstevel@tonic-gate 	matchcount = lookup_enum_cache(set, cmp_str, rules, index,
49627c478bd9Sstevel@tonic-gate 	    &matchnp);
49637c478bd9Sstevel@tonic-gate 
49647c478bd9Sstevel@tonic-gate 	if (matchcount < 0 || matchcount > 1) {
49657c478bd9Sstevel@tonic-gate 		free(cmp_str);
49667c478bd9Sstevel@tonic-gate 		if (multiple && matchcount > 1)
49677c478bd9Sstevel@tonic-gate 			return (DEVFSADM_MULTIPLE);
49687c478bd9Sstevel@tonic-gate 		else
49697c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
49707c478bd9Sstevel@tonic-gate 	}
49717c478bd9Sstevel@tonic-gate 
49727c478bd9Sstevel@tonic-gate 	/* if matching entry already cached, return it */
49737c478bd9Sstevel@tonic-gate 	if (matchcount == 1) {
49748d483882Smlf 		/* should never create a link with a reserved ID */
49758d483882Smlf 		vprint(ENUM_MID, "%s: 1 match w/ ID: %s\n", fcn, matchnp->id);
49768d483882Smlf 		assert(matchnp->flags == 0);
49777c478bd9Sstevel@tonic-gate 		*buf = s_strdup(matchnp->id);
49787c478bd9Sstevel@tonic-gate 		free(cmp_str);
49797c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
49807c478bd9Sstevel@tonic-gate 	}
49817c478bd9Sstevel@tonic-gate 
49827c478bd9Sstevel@tonic-gate 	/*
49837c478bd9Sstevel@tonic-gate 	 * no cached entry, initialize a numeral struct
49847c478bd9Sstevel@tonic-gate 	 * by calling new_id() and cache onto the numeral_set
49857c478bd9Sstevel@tonic-gate 	 */
49867c478bd9Sstevel@tonic-gate 	numeral = s_malloc(sizeof (numeral_t));
49877c478bd9Sstevel@tonic-gate 	numeral->id = new_id(set->headnumeral, type, min);
49887c478bd9Sstevel@tonic-gate 	numeral->full_path = s_strdup(devfs_path);
49897c478bd9Sstevel@tonic-gate 	numeral->rule_index = index;
49907c478bd9Sstevel@tonic-gate 	numeral->cmp_str = cmp_str;
49917c478bd9Sstevel@tonic-gate 	cmp_str = NULL;
49928d483882Smlf 	numeral->flags = 0;
49938d483882Smlf 	vprint(RSRV_MID, "%s: alloc new_id: %s numeral flags = %d\n",
49948d483882Smlf 	    fcn, numeral->id, numeral->flags);
49958d483882Smlf 
49967c478bd9Sstevel@tonic-gate 
49977c478bd9Sstevel@tonic-gate 	/* insert to head of list for fast lookups */
49987c478bd9Sstevel@tonic-gate 	numeral->next = set->headnumeral;
49997c478bd9Sstevel@tonic-gate 	set->headnumeral = numeral;
50007c478bd9Sstevel@tonic-gate 
50017c478bd9Sstevel@tonic-gate 	*buf = s_strdup(numeral->id);
50027c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
50037c478bd9Sstevel@tonic-gate }
50047c478bd9Sstevel@tonic-gate 
50057c478bd9Sstevel@tonic-gate 
50067c478bd9Sstevel@tonic-gate /*
50077c478bd9Sstevel@tonic-gate  * Looks up the specified cache for a match with a specified string
50087c478bd9Sstevel@tonic-gate  * Returns:
50097c478bd9Sstevel@tonic-gate  *	-1	: on error.
50107c478bd9Sstevel@tonic-gate  *	0/1/2	: Number of matches.
50117c478bd9Sstevel@tonic-gate  * Returns the matching element only if there is a single match.
50127c478bd9Sstevel@tonic-gate  * If the "uncached" flag is set, derives the "cmp_str" afresh
50137c478bd9Sstevel@tonic-gate  * for the match instead of using cached values.
50147c478bd9Sstevel@tonic-gate  */
50157c478bd9Sstevel@tonic-gate static int
50167c478bd9Sstevel@tonic-gate lookup_enum_cache(numeral_set_t *set, char *cmp_str,
5017406fc510SToomas Soome     devfsadm_enumerate_t rules[], int index, numeral_t **matchnpp)
50187c478bd9Sstevel@tonic-gate {
50197c478bd9Sstevel@tonic-gate 	int matchcount = 0, rv = -1;
50207c478bd9Sstevel@tonic-gate 	int uncached;
50217c478bd9Sstevel@tonic-gate 	numeral_t *np;
50227c478bd9Sstevel@tonic-gate 	char *fcn = "lookup_enum_cache";
50237c478bd9Sstevel@tonic-gate 	char *cp;
50247c478bd9Sstevel@tonic-gate 
50257c478bd9Sstevel@tonic-gate 	*matchnpp = NULL;
50267c478bd9Sstevel@tonic-gate 
50277c478bd9Sstevel@tonic-gate 	assert(index < set->re_count);
50287c478bd9Sstevel@tonic-gate 
50297c478bd9Sstevel@tonic-gate 	if (cmp_str == NULL) {
50307c478bd9Sstevel@tonic-gate 		return (-1);
50317c478bd9Sstevel@tonic-gate 	}
50327c478bd9Sstevel@tonic-gate 
50337c478bd9Sstevel@tonic-gate 	uncached = 0;
50347c478bd9Sstevel@tonic-gate 	if ((rules[index].flags & MATCH_UNCACHED) == MATCH_UNCACHED) {
50357c478bd9Sstevel@tonic-gate 		uncached = 1;
50367c478bd9Sstevel@tonic-gate 	}
50377c478bd9Sstevel@tonic-gate 
50387c478bd9Sstevel@tonic-gate 	/*
50397c478bd9Sstevel@tonic-gate 	 * Check and see if a matching entry is already cached.
50407c478bd9Sstevel@tonic-gate 	 */
50417c478bd9Sstevel@tonic-gate 	for (np = set->headnumeral; np != NULL; np = np->next) {
50428d483882Smlf 
50438d483882Smlf 		/*
50448d483882Smlf 		 * Skip reserved IDs
50458d483882Smlf 		 */
50468d483882Smlf 		if (np->flags & NUMERAL_RESERVED) {
50478d483882Smlf 			vprint(RSRV_MID, "lookup_enum_cache: "
50488d483882Smlf 			    "Cannot Match with reserved ID (%s), "
50498d483882Smlf 			    "skipping\n", np->id);
50508d483882Smlf 			assert(np->flags == NUMERAL_RESERVED);
50518d483882Smlf 			continue;
50528d483882Smlf 		} else {
50538d483882Smlf 			vprint(RSRV_MID, "lookup_enum_cache: "
50548d483882Smlf 			    "Attempting match with numeral ID: %s"
50558d483882Smlf 			    " numeral flags = %d\n", np->id, np->flags);
50568d483882Smlf 			assert(np->flags == 0);
50578d483882Smlf 		}
50588d483882Smlf 
50597c478bd9Sstevel@tonic-gate 		if (np->cmp_str == NULL) {
50607c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid entry in enumerate"
50617c478bd9Sstevel@tonic-gate 			    " cache. path: %s\n", fcn, np->full_path);
50627c478bd9Sstevel@tonic-gate 			return (-1);
50637c478bd9Sstevel@tonic-gate 		}
50647c478bd9Sstevel@tonic-gate 
50657c478bd9Sstevel@tonic-gate 		if (uncached) {
50667c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "%s: bypassing enumerate cache."
50677c478bd9Sstevel@tonic-gate 			    " path: %s\n", fcn, cmp_str);
50687c478bd9Sstevel@tonic-gate 			cp = alloc_cmp_str(np->full_path,
50697c478bd9Sstevel@tonic-gate 			    &rules[np->rule_index]);
50707c478bd9Sstevel@tonic-gate 			if (cp == NULL)
50717c478bd9Sstevel@tonic-gate 				return (-1);
50727c478bd9Sstevel@tonic-gate 			rv = strcmp(cmp_str, cp);
50737c478bd9Sstevel@tonic-gate 			free(cp);
50747c478bd9Sstevel@tonic-gate 		} else {
50757c478bd9Sstevel@tonic-gate 			rv = strcmp(cmp_str, np->cmp_str);
50767c478bd9Sstevel@tonic-gate 		}
50777c478bd9Sstevel@tonic-gate 
50787c478bd9Sstevel@tonic-gate 		if (rv == 0) {
50797c478bd9Sstevel@tonic-gate 			if (matchcount++ != 0) {
50807c478bd9Sstevel@tonic-gate 				break; /* more than 1 match. */
50817c478bd9Sstevel@tonic-gate 			}
50827c478bd9Sstevel@tonic-gate 			*matchnpp = np;
50837c478bd9Sstevel@tonic-gate 		}
50847c478bd9Sstevel@tonic-gate 	}
50857c478bd9Sstevel@tonic-gate 
50867c478bd9Sstevel@tonic-gate 	return (matchcount);
50877c478bd9Sstevel@tonic-gate }
50887c478bd9Sstevel@tonic-gate 
50897c478bd9Sstevel@tonic-gate #ifdef	DEBUG
50907c478bd9Sstevel@tonic-gate static void
50917c478bd9Sstevel@tonic-gate dump_enum_cache(numeral_set_t *setp)
50927c478bd9Sstevel@tonic-gate {
50937c478bd9Sstevel@tonic-gate 	int i;
50947c478bd9Sstevel@tonic-gate 	numeral_t *np;
50957c478bd9Sstevel@tonic-gate 	char *fcn = "dump_enum_cache";
50967c478bd9Sstevel@tonic-gate 
50977c478bd9Sstevel@tonic-gate 	vprint(ENUM_MID, "%s: re_count = %d\n", fcn, setp->re_count);
50987c478bd9Sstevel@tonic-gate 	for (i = 0; i < setp->re_count; i++) {
50997c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: re[%d] = %s\n", fcn, i, setp->re[i]);
51007c478bd9Sstevel@tonic-gate 	}
51017c478bd9Sstevel@tonic-gate 
51027c478bd9Sstevel@tonic-gate 	for (np = setp->headnumeral; np != NULL; np = np->next) {
51037c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: id: %s\n", fcn, np->id);
51047c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: full_path: %s\n", fcn, np->full_path);
51057c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: rule_index: %d\n", fcn, np->rule_index);
51067c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: cmp_str: %s\n", fcn, np->cmp_str);
51078d483882Smlf 		vprint(ENUM_MID, "%s: flags: %d\n", fcn, np->flags);
51087c478bd9Sstevel@tonic-gate 	}
51097c478bd9Sstevel@tonic-gate }
51107c478bd9Sstevel@tonic-gate #endif
51117c478bd9Sstevel@tonic-gate 
51127c478bd9Sstevel@tonic-gate /*
51137c478bd9Sstevel@tonic-gate  * For a given set of regular expressions in rules[], this function returns
51147c478bd9Sstevel@tonic-gate  * either a previously cached struct numeral_set or it will create and
51157c478bd9Sstevel@tonic-gate  * cache a new struct numeral_set.  There is only one struct numeral_set
51167c478bd9Sstevel@tonic-gate  * for the combination of REs present in rules[].  Each numeral_set contains
51177c478bd9Sstevel@tonic-gate  * the regular expressions in rules[] used for cache selection AND a linked
51187c478bd9Sstevel@tonic-gate  * list of struct numerals, ONE FOR EACH *UNIQUE* numeral or character ID
51197c478bd9Sstevel@tonic-gate  * selected by the grouping parenthesized subexpression found in the last
51207c478bd9Sstevel@tonic-gate  * path component of each rules[].re.  For example, the RE: "rmt/([0-9]+)"
51217c478bd9Sstevel@tonic-gate  * selects all the logical nodes of the correct form in dev/rmt/.
51227c478bd9Sstevel@tonic-gate  * Each rmt/X will store a *single* struct numeral... ie 0, 1, 2 each get a
51237c478bd9Sstevel@tonic-gate  * single struct numeral. There is no need to store more than a single logical
51247c478bd9Sstevel@tonic-gate  * node matching X since the information desired in the devfspath would be
51257c478bd9Sstevel@tonic-gate  * identical for the portion of the devfspath of interest. (the part up to,
51267c478bd9Sstevel@tonic-gate  * but not including the minor name in this example.)
51277c478bd9Sstevel@tonic-gate  *
51287c478bd9Sstevel@tonic-gate  * If the given numeral_set is not yet cached, call enumerate_recurse to
51297c478bd9Sstevel@tonic-gate  * create it.
51307c478bd9Sstevel@tonic-gate  */
51317c478bd9Sstevel@tonic-gate static numeral_set_t *
51327c478bd9Sstevel@tonic-gate get_enum_cache(devfsadm_enumerate_t rules[], int nrules)
51337c478bd9Sstevel@tonic-gate {
51347c478bd9Sstevel@tonic-gate 	/* linked list of numeral sets */
51357c478bd9Sstevel@tonic-gate 	numeral_set_t *setp;
51367c478bd9Sstevel@tonic-gate 	int i;
51378d483882Smlf 	int ret;
51387c478bd9Sstevel@tonic-gate 	char *path_left;
51398d483882Smlf 	enumerate_file_t *entry;
51407c478bd9Sstevel@tonic-gate 	char *fcn = "get_enum_cache";
51417c478bd9Sstevel@tonic-gate 
51427c478bd9Sstevel@tonic-gate 	/*
51437c478bd9Sstevel@tonic-gate 	 * See if we've already cached this numeral set.
51447c478bd9Sstevel@tonic-gate 	 */
51457c478bd9Sstevel@tonic-gate 	for (setp = head_numeral_set; setp != NULL; setp = setp->next) {
51467c478bd9Sstevel@tonic-gate 		/*
51477c478bd9Sstevel@tonic-gate 		 *  check all regexp's passed in function against
51487c478bd9Sstevel@tonic-gate 		 *  those in cached set.
51497c478bd9Sstevel@tonic-gate 		 */
51507c478bd9Sstevel@tonic-gate 		if (nrules != setp->re_count) {
51517c478bd9Sstevel@tonic-gate 			continue;
51527c478bd9Sstevel@tonic-gate 		}
51537c478bd9Sstevel@tonic-gate 
51547c478bd9Sstevel@tonic-gate 		for (i = 0; i < nrules; i++) {
51557c478bd9Sstevel@tonic-gate 			if (strcmp(setp->re[i], rules[i].re) != 0) {
51567c478bd9Sstevel@tonic-gate 				break;
51577c478bd9Sstevel@tonic-gate 			}
51587c478bd9Sstevel@tonic-gate 		}
51597c478bd9Sstevel@tonic-gate 
51607c478bd9Sstevel@tonic-gate 		if (i == nrules) {
51617c478bd9Sstevel@tonic-gate 			return (setp);
51627c478bd9Sstevel@tonic-gate 		}
51637c478bd9Sstevel@tonic-gate 	}
51647c478bd9Sstevel@tonic-gate 
51657c478bd9Sstevel@tonic-gate 	/*
51667c478bd9Sstevel@tonic-gate 	 * If the MATCH_UNCACHED flag is set, we should not  be here.
51677c478bd9Sstevel@tonic-gate 	 */
51687c478bd9Sstevel@tonic-gate 	for (i = 0; i < nrules; i++) {
51697c478bd9Sstevel@tonic-gate 		if ((rules[i].flags & MATCH_UNCACHED) == MATCH_UNCACHED) {
51707c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid enumeration flags: "
51717c478bd9Sstevel@tonic-gate 			    "0x%x\n", fcn, rules[i].flags);
51727c478bd9Sstevel@tonic-gate 			return (NULL);
51737c478bd9Sstevel@tonic-gate 		}
51747c478bd9Sstevel@tonic-gate 	}
51757c478bd9Sstevel@tonic-gate 
51767c478bd9Sstevel@tonic-gate 	/*
51777c478bd9Sstevel@tonic-gate 	 *  Since we made it here, we have not yet cached the given set of
51787c478bd9Sstevel@tonic-gate 	 *  logical nodes matching the passed re.  Create a cached entry
51797c478bd9Sstevel@tonic-gate 	 *  struct numeral_set and populate it with a minimal set of
51807c478bd9Sstevel@tonic-gate 	 *  logical nodes from /dev.
51817c478bd9Sstevel@tonic-gate 	 */
51827c478bd9Sstevel@tonic-gate 
51837c478bd9Sstevel@tonic-gate 	setp = s_malloc(sizeof (numeral_set_t));
51847c478bd9Sstevel@tonic-gate 	setp->re = s_malloc(sizeof (char *) * nrules);
51857c478bd9Sstevel@tonic-gate 	for (i = 0; i < nrules; i++) {
51867c478bd9Sstevel@tonic-gate 		setp->re[i] = s_strdup(rules[i].re);
51877c478bd9Sstevel@tonic-gate 	}
51887c478bd9Sstevel@tonic-gate 	setp->re_count = nrules;
51897c478bd9Sstevel@tonic-gate 	setp->headnumeral = NULL;
51907c478bd9Sstevel@tonic-gate 
51917c478bd9Sstevel@tonic-gate 	/* put this new cached set on the cached set list */
51927c478bd9Sstevel@tonic-gate 	setp->next = head_numeral_set;
51937c478bd9Sstevel@tonic-gate 	head_numeral_set = setp;
51947c478bd9Sstevel@tonic-gate 
51958d483882Smlf 	/*
51968d483882Smlf 	 * For each RE, search the "reserved" list to create numeral IDs that
51978d483882Smlf 	 * are reserved.
51988d483882Smlf 	 */
51998d483882Smlf 	for (entry = enumerate_reserved; entry; entry = entry->er_next) {
52008d483882Smlf 
52018d483882Smlf 		vprint(RSRV_MID, "parsing rstring: %s\n", entry->er_file);
52028d483882Smlf 
52038d483882Smlf 		for (i = 0; i < nrules; i++) {
52048d483882Smlf 			path_left = s_strdup(setp->re[i]);
52058d483882Smlf 			vprint(RSRV_MID, "parsing rule RE: %s\n", path_left);
52068d483882Smlf 			ret = enumerate_parse(entry->er_file, path_left,
52078d483882Smlf 			    setp, rules, i);
52088d483882Smlf 			free(path_left);
52098d483882Smlf 			if (ret == 1) {
52108d483882Smlf 				/*
52118d483882Smlf 				 * We found the reserved ID for this entry.
52128d483882Smlf 				 * We still keep the entry since it is needed
52138d483882Smlf 				 * by the new link bypass code in disks
52148d483882Smlf 				 */
52158d483882Smlf 				vprint(RSRV_MID, "found rsv ID: rstring: %s "
52168d483882Smlf 				    "rule RE: %s\n", entry->er_file, path_left);
52178d483882Smlf 				break;
52188d483882Smlf 			}
52198d483882Smlf 		}
52208d483882Smlf 	}
52218d483882Smlf 
52227c478bd9Sstevel@tonic-gate 	/*
52237c478bd9Sstevel@tonic-gate 	 * For each RE, search disk and cache any matches on the
5224facf4a8dSllai 	 * numeral list.
52257c478bd9Sstevel@tonic-gate 	 */
52267c478bd9Sstevel@tonic-gate 	for (i = 0; i < nrules; i++) {
52277c478bd9Sstevel@tonic-gate 		path_left = s_strdup(setp->re[i]);
5228facf4a8dSllai 		enumerate_recurse(dev_dir, path_left, setp, rules, i);
52297c478bd9Sstevel@tonic-gate 		free(path_left);
52307c478bd9Sstevel@tonic-gate 	}
52317c478bd9Sstevel@tonic-gate 
52327c478bd9Sstevel@tonic-gate #ifdef	DEBUG
52337c478bd9Sstevel@tonic-gate 	dump_enum_cache(setp);
52347c478bd9Sstevel@tonic-gate #endif
52357c478bd9Sstevel@tonic-gate 
52367c478bd9Sstevel@tonic-gate 	return (setp);
52377c478bd9Sstevel@tonic-gate }
52387c478bd9Sstevel@tonic-gate 
52397c478bd9Sstevel@tonic-gate 
52407c478bd9Sstevel@tonic-gate /*
52417c478bd9Sstevel@tonic-gate  * This function stats the pathname namebuf.  If this is a directory
52427c478bd9Sstevel@tonic-gate  * entry, we recurse down dname/fname until we find the first symbolic
52437c478bd9Sstevel@tonic-gate  * link, and then stat and return it.  This is valid for the same reason
52447c478bd9Sstevel@tonic-gate  * that we only need to read a single pathname for multiple matching
52457c478bd9Sstevel@tonic-gate  * logical ID's... ie, all the logical nodes should contain identical
52467c478bd9Sstevel@tonic-gate  * physical paths for the parts we are interested.
52477c478bd9Sstevel@tonic-gate  */
52487c478bd9Sstevel@tonic-gate int
52497c478bd9Sstevel@tonic-gate get_stat_info(char *namebuf, struct stat *sb)
52507c478bd9Sstevel@tonic-gate {
52517c478bd9Sstevel@tonic-gate 	char *cp;
525273de625bSjg 	finddevhdl_t fhandle;
525373de625bSjg 	const char *fp;
52547c478bd9Sstevel@tonic-gate 
52557c478bd9Sstevel@tonic-gate 	if (lstat(namebuf, sb) < 0) {
52567c478bd9Sstevel@tonic-gate 		(void) err_print(LSTAT_FAILED, namebuf, strerror(errno));
52577c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
52587c478bd9Sstevel@tonic-gate 	}
52597c478bd9Sstevel@tonic-gate 
52607c478bd9Sstevel@tonic-gate 	if ((sb->st_mode & S_IFMT) == S_IFLNK) {
52617c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
52627c478bd9Sstevel@tonic-gate 	}
52637c478bd9Sstevel@tonic-gate 
52647c478bd9Sstevel@tonic-gate 	/*
52657c478bd9Sstevel@tonic-gate 	 * If it is a dir, recurse down until we find a link and
52667c478bd9Sstevel@tonic-gate 	 * then use the link.
52677c478bd9Sstevel@tonic-gate 	 */
52687c478bd9Sstevel@tonic-gate 	if ((sb->st_mode & S_IFMT) == S_IFDIR) {
52697c478bd9Sstevel@tonic-gate 
527073de625bSjg 		if (finddev_readdir(namebuf, &fhandle) != 0) {
52717c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
52727c478bd9Sstevel@tonic-gate 		}
52737c478bd9Sstevel@tonic-gate 
52747c478bd9Sstevel@tonic-gate 		/*
52757c478bd9Sstevel@tonic-gate 		 *  Search each dir entry looking for a symlink.  Return
52767c478bd9Sstevel@tonic-gate 		 *  the first symlink found in namebuf.  Recurse dirs.
52777c478bd9Sstevel@tonic-gate 		 */
527873de625bSjg 		while ((fp = finddev_next(fhandle)) != NULL) {
52797c478bd9Sstevel@tonic-gate 			cp = namebuf + strlen(namebuf);
5280facf4a8dSllai 			if ((strlcat(namebuf, "/", PATH_MAX) >= PATH_MAX) ||
528173de625bSjg 			    (strlcat(namebuf, fp, PATH_MAX) >= PATH_MAX)) {
5282facf4a8dSllai 				*cp = '\0';
528373de625bSjg 				finddev_close(fhandle);
5284facf4a8dSllai 				return (DEVFSADM_FAILURE);
5285facf4a8dSllai 			}
52867c478bd9Sstevel@tonic-gate 			if (get_stat_info(namebuf, sb) == DEVFSADM_SUCCESS) {
528773de625bSjg 				finddev_close(fhandle);
52887c478bd9Sstevel@tonic-gate 				return (DEVFSADM_SUCCESS);
52897c478bd9Sstevel@tonic-gate 			}
52907c478bd9Sstevel@tonic-gate 			*cp = '\0';
52917c478bd9Sstevel@tonic-gate 		}
529273de625bSjg 		finddev_close(fhandle);
52937c478bd9Sstevel@tonic-gate 	}
52947c478bd9Sstevel@tonic-gate 
52957c478bd9Sstevel@tonic-gate 	/* no symlink found, so return error */
52967c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
52977c478bd9Sstevel@tonic-gate }
52987c478bd9Sstevel@tonic-gate 
52997c478bd9Sstevel@tonic-gate /*
53007c478bd9Sstevel@tonic-gate  * An existing matching ID was not found, so this function is called to
53017c478bd9Sstevel@tonic-gate  * create the next lowest ID.  In the INTEGER case, return the next
53027c478bd9Sstevel@tonic-gate  * lowest unused integer.  In the case of LETTER, return the next lowest
53037c478bd9Sstevel@tonic-gate  * unused letter.  Return empty string if all 26 are used.
53047c478bd9Sstevel@tonic-gate  * Only IDs >= min will be returned.
53057c478bd9Sstevel@tonic-gate  */
53067c478bd9Sstevel@tonic-gate char *
53077c478bd9Sstevel@tonic-gate new_id(numeral_t *numeral, int type, char *min)
53087c478bd9Sstevel@tonic-gate {
53097c478bd9Sstevel@tonic-gate 	int imin;
53107c478bd9Sstevel@tonic-gate 	temp_t *temp;
53117c478bd9Sstevel@tonic-gate 	temp_t *ptr;
53127c478bd9Sstevel@tonic-gate 	temp_t **previous;
53137c478bd9Sstevel@tonic-gate 	temp_t *head = NULL;
53147c478bd9Sstevel@tonic-gate 	char *retval;
53157c478bd9Sstevel@tonic-gate 	static char tempbuff[8];
53167c478bd9Sstevel@tonic-gate 	numeral_t *np;
53177c478bd9Sstevel@tonic-gate 
53187c478bd9Sstevel@tonic-gate 	if (type == LETTER) {
53197c478bd9Sstevel@tonic-gate 
53207c478bd9Sstevel@tonic-gate 		char letter[26], i;
53217c478bd9Sstevel@tonic-gate 
53227c478bd9Sstevel@tonic-gate 		if (numeral == NULL) {
53237c478bd9Sstevel@tonic-gate 			return (s_strdup(min));
53247c478bd9Sstevel@tonic-gate 		}
53257c478bd9Sstevel@tonic-gate 
53267c478bd9Sstevel@tonic-gate 		for (i = 0; i < 26; i++) {
53277c478bd9Sstevel@tonic-gate 			letter[i] = 0;
53287c478bd9Sstevel@tonic-gate 		}
53297c478bd9Sstevel@tonic-gate 
53307c478bd9Sstevel@tonic-gate 		for (np = numeral; np != NULL; np = np->next) {
53318d483882Smlf 			assert(np->flags == 0 ||
53328d483882Smlf 			    np->flags == NUMERAL_RESERVED);
53337c478bd9Sstevel@tonic-gate 			letter[*np->id - 'a']++;
53347c478bd9Sstevel@tonic-gate 		}
53357c478bd9Sstevel@tonic-gate 
53367c478bd9Sstevel@tonic-gate 		imin = *min - 'a';
53377c478bd9Sstevel@tonic-gate 
53387c478bd9Sstevel@tonic-gate 		for (i = imin; i < 26; i++) {
53397c478bd9Sstevel@tonic-gate 			if (letter[i] == 0) {
53407c478bd9Sstevel@tonic-gate 				retval = s_malloc(2);
53417c478bd9Sstevel@tonic-gate 				retval[0] = 'a' + i;
53427c478bd9Sstevel@tonic-gate 				retval[1] = '\0';
53437c478bd9Sstevel@tonic-gate 				return (retval);
53447c478bd9Sstevel@tonic-gate 			}
53457c478bd9Sstevel@tonic-gate 		}
53467c478bd9Sstevel@tonic-gate 
53477c478bd9Sstevel@tonic-gate 		return (s_strdup(""));
53487c478bd9Sstevel@tonic-gate 	}
53497c478bd9Sstevel@tonic-gate 
53507c478bd9Sstevel@tonic-gate 	if (type == INTEGER) {
53517c478bd9Sstevel@tonic-gate 
53527c478bd9Sstevel@tonic-gate 		if (numeral == NULL) {
53537c478bd9Sstevel@tonic-gate 			return (s_strdup(min));
53547c478bd9Sstevel@tonic-gate 		}
53557c478bd9Sstevel@tonic-gate 
53567c478bd9Sstevel@tonic-gate 		imin = atoi(min);
53577c478bd9Sstevel@tonic-gate 
53587c478bd9Sstevel@tonic-gate 		/* sort list */
53597c478bd9Sstevel@tonic-gate 		for (np = numeral; np != NULL; np = np->next) {
53608d483882Smlf 			assert(np->flags == 0 ||
53618d483882Smlf 			    np->flags == NUMERAL_RESERVED);
53627c478bd9Sstevel@tonic-gate 			temp = s_malloc(sizeof (temp_t));
53637c478bd9Sstevel@tonic-gate 			temp->integer = atoi(np->id);
53647c478bd9Sstevel@tonic-gate 			temp->next = NULL;
53657c478bd9Sstevel@tonic-gate 
53667c478bd9Sstevel@tonic-gate 			previous = &head;
53677c478bd9Sstevel@tonic-gate 			for (ptr = head; ptr != NULL; ptr = ptr->next) {
53687c478bd9Sstevel@tonic-gate 				if (temp->integer < ptr->integer) {
53697c478bd9Sstevel@tonic-gate 					temp->next = ptr;
53707c478bd9Sstevel@tonic-gate 					*previous = temp;
53717c478bd9Sstevel@tonic-gate 					break;
53727c478bd9Sstevel@tonic-gate 				}
53737c478bd9Sstevel@tonic-gate 				previous = &(ptr->next);
53747c478bd9Sstevel@tonic-gate 			}
53757c478bd9Sstevel@tonic-gate 			if (ptr == NULL) {
53767c478bd9Sstevel@tonic-gate 				*previous = temp;
53777c478bd9Sstevel@tonic-gate 			}
53787c478bd9Sstevel@tonic-gate 		}
53797c478bd9Sstevel@tonic-gate 
53807c478bd9Sstevel@tonic-gate 		/* now search sorted list for first hole >= imin */
53817c478bd9Sstevel@tonic-gate 		for (ptr = head; ptr != NULL; ptr = ptr->next) {
53827c478bd9Sstevel@tonic-gate 			if (imin == ptr->integer) {
53837c478bd9Sstevel@tonic-gate 				imin++;
53847c478bd9Sstevel@tonic-gate 			} else {
53857c478bd9Sstevel@tonic-gate 				if (imin < ptr->integer) {
53867c478bd9Sstevel@tonic-gate 					break;
53877c478bd9Sstevel@tonic-gate 				}
53887c478bd9Sstevel@tonic-gate 			}
53897c478bd9Sstevel@tonic-gate 
53907c478bd9Sstevel@tonic-gate 		}
53917c478bd9Sstevel@tonic-gate 
53927c478bd9Sstevel@tonic-gate 		/* free temp list */
53937c478bd9Sstevel@tonic-gate 		for (ptr = head; ptr != NULL; ) {
53947c478bd9Sstevel@tonic-gate 			temp = ptr;
53957c478bd9Sstevel@tonic-gate 			ptr = ptr->next;
53967c478bd9Sstevel@tonic-gate 			free(temp);
53977c478bd9Sstevel@tonic-gate 		}
53987c478bd9Sstevel@tonic-gate 
53997c478bd9Sstevel@tonic-gate 		(void) sprintf(tempbuff, "%d", imin);
54007c478bd9Sstevel@tonic-gate 		return (s_strdup(tempbuff));
54017c478bd9Sstevel@tonic-gate 	}
54027c478bd9Sstevel@tonic-gate 
54037c478bd9Sstevel@tonic-gate 	return (s_strdup(""));
54047c478bd9Sstevel@tonic-gate }
54057c478bd9Sstevel@tonic-gate 
54068d483882Smlf static int
54078d483882Smlf enumerate_parse(char *rsvstr, char *path_left, numeral_set_t *setp,
5408406fc510SToomas Soome     devfsadm_enumerate_t rules[], int index)
54098d483882Smlf {
54108d483882Smlf 	char	*slash1 = NULL;
54118d483882Smlf 	char	*slash2 = NULL;
54128d483882Smlf 	char	*numeral_id;
54138d483882Smlf 	char	*path_left_save;
54148d483882Smlf 	char	*rsvstr_save;
54158d483882Smlf 	int	ret = 0;
54168d483882Smlf 	static int warned = 0;
54178d483882Smlf 
54188d483882Smlf 	rsvstr_save = rsvstr;
54198d483882Smlf 	path_left_save = path_left;
54208d483882Smlf 
54218d483882Smlf 	if (rsvstr == NULL || rsvstr[0] == '\0' || rsvstr[0] == '/') {
54228d483882Smlf 		if (!warned) {
54238d483882Smlf 			err_print("invalid reserved filepath: %s\n",
54248d483882Smlf 			    rsvstr ? rsvstr : "<NULL>");
54258d483882Smlf 			warned = 1;
54268d483882Smlf 		}
54278d483882Smlf 		return (0);
54288d483882Smlf 	}
54298d483882Smlf 
54308d483882Smlf 	vprint(RSRV_MID, "processing rule: %s, rstring: %s\n",
54318d483882Smlf 	    path_left, rsvstr);
54328d483882Smlf 
54338d483882Smlf 
54348d483882Smlf 	for (;;) {
54358d483882Smlf 		/* get rid of any extra '/' in the reserve string */
54368d483882Smlf 		while (*rsvstr == '/') {
54378d483882Smlf 			rsvstr++;
54388d483882Smlf 		}
54398d483882Smlf 
54408d483882Smlf 		/* get rid of any extra '/' in the RE */
54418d483882Smlf 		while (*path_left == '/') {
54428d483882Smlf 			path_left++;
54438d483882Smlf 		}
54448d483882Smlf 
54458d483882Smlf 		if (slash1 = strchr(path_left, '/')) {
54468d483882Smlf 			*slash1 = '\0';
54478d483882Smlf 		}
54488d483882Smlf 		if (slash2 = strchr(rsvstr, '/')) {
54498d483882Smlf 			*slash2 = '\0';
54508d483882Smlf 		}
54518d483882Smlf 
54528d483882Smlf 		if ((slash1 != NULL) ^ (slash2 != NULL)) {
54538d483882Smlf 			ret = 0;
54548d483882Smlf 			vprint(RSRV_MID, "mismatch in # of path components\n");
54558d483882Smlf 			goto out;
54568d483882Smlf 		}
54578d483882Smlf 
54588d483882Smlf 		/*
54598d483882Smlf 		 *  Returns true if path_left matches the list entry.
54608d483882Smlf 		 *  If it is the last path component, pass subexp
54618d483882Smlf 		 *  so that it will return the corresponding ID in
54628d483882Smlf 		 *  numeral_id.
54638d483882Smlf 		 */
54648d483882Smlf 		numeral_id = NULL;
54658d483882Smlf 		if (match_path_component(path_left, rsvstr, &numeral_id,
54660a653502Swroche 		    slash1 ? 0 : rules[index].subexp)) {
54678d483882Smlf 
54688d483882Smlf 			/* We have a match. */
54698d483882Smlf 			if (slash1 == NULL) {
54708d483882Smlf 				/* Is last path component */
54718d483882Smlf 				vprint(RSRV_MID, "match and last component\n");
54728d483882Smlf 				create_reserved_numeral(setp, numeral_id);
54738d483882Smlf 				if (numeral_id != NULL) {
54748d483882Smlf 					free(numeral_id);
54758d483882Smlf 				}
54768d483882Smlf 				ret = 1;
54778d483882Smlf 				goto out;
54788d483882Smlf 			} else {
54798d483882Smlf 				/* Not last path component. Continue parsing */
54808d483882Smlf 				*slash1 = '/';
54818d483882Smlf 				*slash2 = '/';
54828d483882Smlf 				path_left = slash1 + 1;
54838d483882Smlf 				rsvstr = slash2 + 1;
54848d483882Smlf 				vprint(RSRV_MID,
54858d483882Smlf 				    "match and NOT last component\n");
54868d483882Smlf 				continue;
54878d483882Smlf 			}
54888d483882Smlf 		} else {
54898d483882Smlf 			/* No match */
54908d483882Smlf 			ret = 0;
54918d483882Smlf 			vprint(RSRV_MID, "No match: rule RE = %s, "
54928d483882Smlf 			    "rstring = %s\n", path_left, rsvstr);
54938d483882Smlf 			goto out;
54948d483882Smlf 		}
54958d483882Smlf 	}
54968d483882Smlf 
54978d483882Smlf out:
54988d483882Smlf 	if (slash1)
54998d483882Smlf 		*slash1 = '/';
55008d483882Smlf 	if (slash2)
55018d483882Smlf 		*slash2 = '/';
55028d483882Smlf 
55038d483882Smlf 	if (ret == 1) {
55048d483882Smlf 		vprint(RSRV_MID, "match: rule RE: %s, rstring: %s\n",
55058d483882Smlf 		    path_left_save, rsvstr_save);
55068d483882Smlf 	} else {
55078d483882Smlf 		vprint(RSRV_MID, "NO match: rule RE: %s, rstring: %s\n",
55088d483882Smlf 		    path_left_save, rsvstr_save);
55098d483882Smlf 	}
55108d483882Smlf 
55118d483882Smlf 	return (ret);
55128d483882Smlf }
55138d483882Smlf 
55147c478bd9Sstevel@tonic-gate /*
55157c478bd9Sstevel@tonic-gate  * Search current_dir for all files which match the first path component
55167c478bd9Sstevel@tonic-gate  * of path_left, which is an RE.  If a match is found, but there are more
55177c478bd9Sstevel@tonic-gate  * components of path_left, then recurse, otherwise, if we have reached
55187c478bd9Sstevel@tonic-gate  * the last component of path_left, call create_cached_numerals for each
55197c478bd9Sstevel@tonic-gate  * file.   At some point, recurse_dev_re() should be rewritten so that this
55207c478bd9Sstevel@tonic-gate  * function can be eliminated.
55217c478bd9Sstevel@tonic-gate  */
55227c478bd9Sstevel@tonic-gate static void
55237c478bd9Sstevel@tonic-gate enumerate_recurse(char *current_dir, char *path_left, numeral_set_t *setp,
5524406fc510SToomas Soome     devfsadm_enumerate_t rules[], int index)
55257c478bd9Sstevel@tonic-gate {
55267c478bd9Sstevel@tonic-gate 	char *slash;
55277c478bd9Sstevel@tonic-gate 	char *new_path;
55287c478bd9Sstevel@tonic-gate 	char *numeral_id;
552973de625bSjg 	finddevhdl_t fhandle;
553073de625bSjg 	const char *fp;
55317c478bd9Sstevel@tonic-gate 
553273de625bSjg 	if (finddev_readdir(current_dir, &fhandle) != 0) {
55337c478bd9Sstevel@tonic-gate 		return;
55347c478bd9Sstevel@tonic-gate 	}
55357c478bd9Sstevel@tonic-gate 
55367c478bd9Sstevel@tonic-gate 	/* get rid of any extra '/' */
55377c478bd9Sstevel@tonic-gate 	while (*path_left == '/') {
55387c478bd9Sstevel@tonic-gate 		path_left++;
55397c478bd9Sstevel@tonic-gate 	}
55407c478bd9Sstevel@tonic-gate 
55417c478bd9Sstevel@tonic-gate 	if (slash = strchr(path_left, '/')) {
55427c478bd9Sstevel@tonic-gate 		*slash = '\0';
55437c478bd9Sstevel@tonic-gate 	}
55447c478bd9Sstevel@tonic-gate 
554573de625bSjg 	while ((fp = finddev_next(fhandle)) != NULL) {
55467c478bd9Sstevel@tonic-gate 
55477c478bd9Sstevel@tonic-gate 		/*
5548facf4a8dSllai 		 *  Returns true if path_left matches the list entry.
55497c478bd9Sstevel@tonic-gate 		 *  If it is the last path component, pass subexp
55507c478bd9Sstevel@tonic-gate 		 *  so that it will return the corresponding ID in
55517c478bd9Sstevel@tonic-gate 		 *  numeral_id.
55527c478bd9Sstevel@tonic-gate 		 */
55537c478bd9Sstevel@tonic-gate 		numeral_id = NULL;
555473de625bSjg 		if (match_path_component(path_left, (char *)fp, &numeral_id,
55550a653502Swroche 		    slash ? 0 : rules[index].subexp)) {
55567c478bd9Sstevel@tonic-gate 
55577c478bd9Sstevel@tonic-gate 			new_path = s_malloc(strlen(current_dir) +
555873de625bSjg 			    strlen(fp) + 2);
55597c478bd9Sstevel@tonic-gate 
55607c478bd9Sstevel@tonic-gate 			(void) strcpy(new_path, current_dir);
55617c478bd9Sstevel@tonic-gate 			(void) strcat(new_path, "/");
556273de625bSjg 			(void) strcat(new_path, fp);
55637c478bd9Sstevel@tonic-gate 
55647c478bd9Sstevel@tonic-gate 			if (slash != NULL) {
55657c478bd9Sstevel@tonic-gate 				enumerate_recurse(new_path, slash + 1,
55667c478bd9Sstevel@tonic-gate 				    setp, rules, index);
55677c478bd9Sstevel@tonic-gate 			} else {
55687c478bd9Sstevel@tonic-gate 				create_cached_numeral(new_path, setp,
55697c478bd9Sstevel@tonic-gate 				    numeral_id, rules, index);
55707c478bd9Sstevel@tonic-gate 				if (numeral_id != NULL) {
55717c478bd9Sstevel@tonic-gate 					free(numeral_id);
55727c478bd9Sstevel@tonic-gate 				}
55737c478bd9Sstevel@tonic-gate 			}
55747c478bd9Sstevel@tonic-gate 			free(new_path);
55757c478bd9Sstevel@tonic-gate 		}
55767c478bd9Sstevel@tonic-gate 	}
55777c478bd9Sstevel@tonic-gate 
55787c478bd9Sstevel@tonic-gate 	if (slash != NULL) {
55797c478bd9Sstevel@tonic-gate 		*slash = '/';
55807c478bd9Sstevel@tonic-gate 	}
558173de625bSjg 	finddev_close(fhandle);
55827c478bd9Sstevel@tonic-gate }
55837c478bd9Sstevel@tonic-gate 
55847c478bd9Sstevel@tonic-gate 
55857c478bd9Sstevel@tonic-gate /*
55867c478bd9Sstevel@tonic-gate  * Returns true if file matches file_re.  If subexp is non-zero, it means
55877c478bd9Sstevel@tonic-gate  * we are searching the last path component and need to return the
55887c478bd9Sstevel@tonic-gate  * parenthesized subexpression subexp in id.
55897c478bd9Sstevel@tonic-gate  *
55907c478bd9Sstevel@tonic-gate  */
55917c478bd9Sstevel@tonic-gate static int
55927c478bd9Sstevel@tonic-gate match_path_component(char *file_re,  char *file,  char **id, int subexp)
55937c478bd9Sstevel@tonic-gate {
55947c478bd9Sstevel@tonic-gate 	regex_t re1;
55957c478bd9Sstevel@tonic-gate 	int match = 0;
55967c478bd9Sstevel@tonic-gate 	int nelements;
55977c478bd9Sstevel@tonic-gate 	regmatch_t *pmatch;
55987c478bd9Sstevel@tonic-gate 
55997c478bd9Sstevel@tonic-gate 	if (subexp != 0) {
56007c478bd9Sstevel@tonic-gate 		nelements = subexp + 1;
56010a653502Swroche 		pmatch =
56020a653502Swroche 		    (regmatch_t *)s_malloc(sizeof (regmatch_t) * nelements);
56037c478bd9Sstevel@tonic-gate 	} else {
56047c478bd9Sstevel@tonic-gate 		pmatch = NULL;
56057c478bd9Sstevel@tonic-gate 		nelements = 0;
56067c478bd9Sstevel@tonic-gate 	}
56077c478bd9Sstevel@tonic-gate 
56087c478bd9Sstevel@tonic-gate 	if (regcomp(&re1, file_re, REG_EXTENDED) != 0) {
56097c478bd9Sstevel@tonic-gate 		if (pmatch != NULL) {
56107c478bd9Sstevel@tonic-gate 			free(pmatch);
56117c478bd9Sstevel@tonic-gate 		}
56127c478bd9Sstevel@tonic-gate 		return (0);
56137c478bd9Sstevel@tonic-gate 	}
56147c478bd9Sstevel@tonic-gate 
56157c478bd9Sstevel@tonic-gate 	if (regexec(&re1, file, nelements, pmatch, 0) == 0) {
56167c478bd9Sstevel@tonic-gate 		match = 1;
56177c478bd9Sstevel@tonic-gate 	}
56187c478bd9Sstevel@tonic-gate 
56197c478bd9Sstevel@tonic-gate 	if ((match != 0) && (subexp != 0)) {
56207c478bd9Sstevel@tonic-gate 		int size = pmatch[subexp].rm_eo - pmatch[subexp].rm_so;
56217c478bd9Sstevel@tonic-gate 		*id = s_malloc(size + 1);
56227c478bd9Sstevel@tonic-gate 		(void) strncpy(*id, &file[pmatch[subexp].rm_so], size);
56237c478bd9Sstevel@tonic-gate 		(*id)[size] = '\0';
56247c478bd9Sstevel@tonic-gate 	}
56257c478bd9Sstevel@tonic-gate 
56267c478bd9Sstevel@tonic-gate 	if (pmatch != NULL) {
56277c478bd9Sstevel@tonic-gate 		free(pmatch);
56287c478bd9Sstevel@tonic-gate 	}
56297c478bd9Sstevel@tonic-gate 	regfree(&re1);
56307c478bd9Sstevel@tonic-gate 	return (match);
56317c478bd9Sstevel@tonic-gate }
56327c478bd9Sstevel@tonic-gate 
56338d483882Smlf static void
56348d483882Smlf create_reserved_numeral(numeral_set_t *setp, char *numeral_id)
56358d483882Smlf {
56368d483882Smlf 	numeral_t *np;
56378d483882Smlf 
56388d483882Smlf 	vprint(RSRV_MID, "Attempting to create reserved numeral: %s\n",
56398d483882Smlf 	    numeral_id);
56408d483882Smlf 
56418d483882Smlf 	/*
56428d483882Smlf 	 * We found a numeral_id from an entry in the enumerate_reserved file
56438d483882Smlf 	 * which matched the re passed in from devfsadm_enumerate.  We only
56448d483882Smlf 	 * need to make sure ONE copy of numeral_id exists on the numeral list.
56458d483882Smlf 	 * We only need to store /dev/dsk/cNtod0s0 and no other entries
56468d483882Smlf 	 * hanging off of controller N.
56478d483882Smlf 	 */
56488d483882Smlf 	for (np = setp->headnumeral; np != NULL; np = np->next) {
56498d483882Smlf 		if (strcmp(numeral_id, np->id) == 0) {
56508d483882Smlf 			vprint(RSRV_MID, "ID: %s, already reserved\n", np->id);
56518d483882Smlf 			assert(np->flags == NUMERAL_RESERVED);
56528d483882Smlf 			return;
56538d483882Smlf 		} else {
56548d483882Smlf 			assert(np->flags == 0 ||
56558d483882Smlf 			    np->flags == NUMERAL_RESERVED);
56568d483882Smlf 		}
56578d483882Smlf 	}
56588d483882Smlf 
56598d483882Smlf 	/* NOT on list, so add it */
56608d483882Smlf 	np = s_malloc(sizeof (numeral_t));
56618d483882Smlf 	np->id = s_strdup(numeral_id);
56628d483882Smlf 	np->full_path = NULL;
56638d483882Smlf 	np->rule_index = 0;
56648d483882Smlf 	np->cmp_str = NULL;
56658d483882Smlf 	np->flags = NUMERAL_RESERVED;
56668d483882Smlf 	np->next = setp->headnumeral;
56678d483882Smlf 	setp->headnumeral = np;
56688d483882Smlf 
56698d483882Smlf 	vprint(RSRV_MID, "Reserved numeral ID: %s\n", np->id);
56708d483882Smlf }
56718d483882Smlf 
56727c478bd9Sstevel@tonic-gate /*
56737c478bd9Sstevel@tonic-gate  * This function is called for every file which matched the leaf
56747c478bd9Sstevel@tonic-gate  * component of the RE.  If the "numeral_id" is not already on the
56757c478bd9Sstevel@tonic-gate  * numeral set's numeral list, add it and its physical path.
56767c478bd9Sstevel@tonic-gate  */
56777c478bd9Sstevel@tonic-gate static void
56787c478bd9Sstevel@tonic-gate create_cached_numeral(char *path, numeral_set_t *setp, char *numeral_id,
5679406fc510SToomas Soome     devfsadm_enumerate_t rules[], int index)
56807c478bd9Sstevel@tonic-gate {
56817c478bd9Sstevel@tonic-gate 	char linkbuf[PATH_MAX + 1];
56827c478bd9Sstevel@tonic-gate 	char lpath[PATH_MAX + 1];
56837c478bd9Sstevel@tonic-gate 	char *linkptr, *cmp_str;
56847c478bd9Sstevel@tonic-gate 	numeral_t *np;
56857c478bd9Sstevel@tonic-gate 	int linksize;
56867c478bd9Sstevel@tonic-gate 	struct stat sb;
568794c894bbSVikram Hegde 	char *contents;
56887c478bd9Sstevel@tonic-gate 	const char *fcn = "create_cached_numeral";
56897c478bd9Sstevel@tonic-gate 
56907c478bd9Sstevel@tonic-gate 	assert(index >= 0 && index < setp->re_count);
56917c478bd9Sstevel@tonic-gate 	assert(strcmp(rules[index].re, setp->re[index]) == 0);
56927c478bd9Sstevel@tonic-gate 
56937c478bd9Sstevel@tonic-gate 	/*
56947c478bd9Sstevel@tonic-gate 	 *  We found a numeral_id from an entry in /dev which matched
56957c478bd9Sstevel@tonic-gate 	 *  the re passed in from devfsadm_enumerate.  We only need to make sure
56967c478bd9Sstevel@tonic-gate 	 *  ONE copy of numeral_id exists on the numeral list.  We only need
56977c478bd9Sstevel@tonic-gate 	 *  to store /dev/dsk/cNtod0s0 and no other entries hanging off
56987c478bd9Sstevel@tonic-gate 	 *  of controller N.
56997c478bd9Sstevel@tonic-gate 	 */
57007c478bd9Sstevel@tonic-gate 	for (np = setp->headnumeral; np != NULL; np = np->next) {
57018d483882Smlf 		assert(np->flags == 0 || np->flags == NUMERAL_RESERVED);
57027c478bd9Sstevel@tonic-gate 		if (strcmp(numeral_id, np->id) == 0) {
57038d483882Smlf 			/*
57048d483882Smlf 			 * Note that we can't assert that the flags field
57058d483882Smlf 			 * of the numeral is 0, since both reserved and
57068d483882Smlf 			 * unreserved links in /dev come here
57078d483882Smlf 			 */
57088d483882Smlf 			if (np->flags == NUMERAL_RESERVED) {
57098d483882Smlf 				vprint(RSRV_MID, "ID derived from /dev link is"
57108d483882Smlf 				    " reserved: %s\n", np->id);
57118d483882Smlf 			} else {
57128d483882Smlf 				vprint(RSRV_MID, "ID derived from /dev link is"
57138d483882Smlf 				    " NOT reserved: %s\n", np->id);
57148d483882Smlf 			}
57157c478bd9Sstevel@tonic-gate 			return;
57167c478bd9Sstevel@tonic-gate 		}
57177c478bd9Sstevel@tonic-gate 	}
57187c478bd9Sstevel@tonic-gate 
57197c478bd9Sstevel@tonic-gate 	/* NOT on list, so add it */
57207c478bd9Sstevel@tonic-gate 
57217c478bd9Sstevel@tonic-gate 	(void) strcpy(lpath, path);
57227c478bd9Sstevel@tonic-gate 	/*
57237c478bd9Sstevel@tonic-gate 	 * If path is a dir, it is changed to the first symbolic link it find
57247c478bd9Sstevel@tonic-gate 	 * if it finds one.
57257c478bd9Sstevel@tonic-gate 	 */
57267c478bd9Sstevel@tonic-gate 	if (get_stat_info(lpath, &sb) == DEVFSADM_FAILURE) {
57277c478bd9Sstevel@tonic-gate 		return;
57287c478bd9Sstevel@tonic-gate 	}
57297c478bd9Sstevel@tonic-gate 
57307c478bd9Sstevel@tonic-gate 	/* If we get here, we found a symlink */
57317c478bd9Sstevel@tonic-gate 	linksize = readlink(lpath, linkbuf, PATH_MAX);
57327c478bd9Sstevel@tonic-gate 
57337c478bd9Sstevel@tonic-gate 	if (linksize <= 0) {
57347c478bd9Sstevel@tonic-gate 		err_print(READLINK_FAILED, fcn, lpath, strerror(errno));
57357c478bd9Sstevel@tonic-gate 		return;
57367c478bd9Sstevel@tonic-gate 	}
57377c478bd9Sstevel@tonic-gate 
57387c478bd9Sstevel@tonic-gate 	linkbuf[linksize] = '\0';
57397c478bd9Sstevel@tonic-gate 
574094c894bbSVikram Hegde 	/*
574194c894bbSVikram Hegde 	 * redirect alias path to current path
574294c894bbSVikram Hegde 	 * devi_root_node is protected by lock_dev()
574394c894bbSVikram Hegde 	 */
574494c894bbSVikram Hegde 	contents = di_alias2curr(devi_root_node, linkbuf);
574594c894bbSVikram Hegde 
57467c478bd9Sstevel@tonic-gate 	/*
57477c478bd9Sstevel@tonic-gate 	 * the following just points linkptr to the root of the /devices
57487c478bd9Sstevel@tonic-gate 	 * node if it is a minor node, otherwise, to the first char of
57497c478bd9Sstevel@tonic-gate 	 * linkbuf if it is a link.
57507c478bd9Sstevel@tonic-gate 	 */
575194c894bbSVikram Hegde 	(void) is_minor_node(contents, &linkptr);
57527c478bd9Sstevel@tonic-gate 
57537c478bd9Sstevel@tonic-gate 	cmp_str = alloc_cmp_str(linkptr, &rules[index]);
57547c478bd9Sstevel@tonic-gate 	if (cmp_str == NULL) {
575594c894bbSVikram Hegde 		free(contents);
57567c478bd9Sstevel@tonic-gate 		return;
57577c478bd9Sstevel@tonic-gate 	}
57587c478bd9Sstevel@tonic-gate 
57597c478bd9Sstevel@tonic-gate 	np = s_malloc(sizeof (numeral_t));
57607c478bd9Sstevel@tonic-gate 
57617c478bd9Sstevel@tonic-gate 	np->id = s_strdup(numeral_id);
57627c478bd9Sstevel@tonic-gate 	np->full_path = s_strdup(linkptr);
57637c478bd9Sstevel@tonic-gate 	np->rule_index = index;
57647c478bd9Sstevel@tonic-gate 	np->cmp_str = cmp_str;
57658d483882Smlf 	np->flags = 0;
57667c478bd9Sstevel@tonic-gate 
57677c478bd9Sstevel@tonic-gate 	np->next = setp->headnumeral;
57687c478bd9Sstevel@tonic-gate 	setp->headnumeral = np;
576994c894bbSVikram Hegde 
577094c894bbSVikram Hegde 	free(contents);
57717c478bd9Sstevel@tonic-gate }
57727c478bd9Sstevel@tonic-gate 
57737c478bd9Sstevel@tonic-gate 
57747c478bd9Sstevel@tonic-gate /*
57757c478bd9Sstevel@tonic-gate  * This should be called either before or after granting access to a
57767c478bd9Sstevel@tonic-gate  * command line version of devfsadm running, since it may have changed
57777c478bd9Sstevel@tonic-gate  * the state of /dev.  It forces future enumerate calls to re-build
57787c478bd9Sstevel@tonic-gate  * cached information from /dev.
57797c478bd9Sstevel@tonic-gate  */
57807c478bd9Sstevel@tonic-gate void
57817c478bd9Sstevel@tonic-gate invalidate_enumerate_cache(void)
57827c478bd9Sstevel@tonic-gate {
57837c478bd9Sstevel@tonic-gate 	numeral_set_t *setp;
57847c478bd9Sstevel@tonic-gate 	numeral_set_t *savedsetp;
57857c478bd9Sstevel@tonic-gate 	numeral_t *savednumset;
57867c478bd9Sstevel@tonic-gate 	numeral_t *numset;
57877c478bd9Sstevel@tonic-gate 	int i;
57887c478bd9Sstevel@tonic-gate 
57897c478bd9Sstevel@tonic-gate 	for (setp = head_numeral_set; setp != NULL; ) {
57907c478bd9Sstevel@tonic-gate 		/*
57917c478bd9Sstevel@tonic-gate 		 *  check all regexp's passed in function against
57927c478bd9Sstevel@tonic-gate 		 *  those in cached set.
57937c478bd9Sstevel@tonic-gate 		 */
57947c478bd9Sstevel@tonic-gate 
57957c478bd9Sstevel@tonic-gate 		savedsetp = setp;
57967c478bd9Sstevel@tonic-gate 		setp = setp->next;
57977c478bd9Sstevel@tonic-gate 
57987c478bd9Sstevel@tonic-gate 		for (i = 0; i < savedsetp->re_count; i++) {
57997c478bd9Sstevel@tonic-gate 			free(savedsetp->re[i]);
58007c478bd9Sstevel@tonic-gate 		}
58017c478bd9Sstevel@tonic-gate 		free(savedsetp->re);
58027c478bd9Sstevel@tonic-gate 
58037c478bd9Sstevel@tonic-gate 		for (numset = savedsetp->headnumeral; numset != NULL; ) {
58047c478bd9Sstevel@tonic-gate 			savednumset = numset;
58057c478bd9Sstevel@tonic-gate 			numset = numset->next;
58067c478bd9Sstevel@tonic-gate 			assert(savednumset->rule_index < savedsetp->re_count);
58077c478bd9Sstevel@tonic-gate 			free(savednumset->id);
58087c478bd9Sstevel@tonic-gate 			free(savednumset->full_path);
58097c478bd9Sstevel@tonic-gate 			free(savednumset->cmp_str);
58107c478bd9Sstevel@tonic-gate 			free(savednumset);
58117c478bd9Sstevel@tonic-gate 		}
58127c478bd9Sstevel@tonic-gate 		free(savedsetp);
58137c478bd9Sstevel@tonic-gate 	}
58147c478bd9Sstevel@tonic-gate 	head_numeral_set = NULL;
58157c478bd9Sstevel@tonic-gate }
58167c478bd9Sstevel@tonic-gate 
58177c478bd9Sstevel@tonic-gate /*
58187c478bd9Sstevel@tonic-gate  * Copies over links from /dev to <root>/dev and device special files in
58197c478bd9Sstevel@tonic-gate  * /devices to <root>/devices, preserving the existing file modes.  If
58207c478bd9Sstevel@tonic-gate  * the link or special file already exists on <root>, skip the copy.  (it
58217c478bd9Sstevel@tonic-gate  * would exist only if a package hard coded it there, so assume package
58227c478bd9Sstevel@tonic-gate  * knows best?).  Use /etc/name_to_major and <root>/etc/name_to_major to
58237c478bd9Sstevel@tonic-gate  * make translations for major numbers on device special files.	No need to
58247c478bd9Sstevel@tonic-gate  * make a translation on minor_perm since if the file was created in the
58257c478bd9Sstevel@tonic-gate  * miniroot then it would presumably have the same minor_perm entry in
58267c478bd9Sstevel@tonic-gate  *  <root>/etc/minor_perm.  To be used only by install.
58277c478bd9Sstevel@tonic-gate  */
58287c478bd9Sstevel@tonic-gate int
58297c478bd9Sstevel@tonic-gate devfsadm_copy(void)
58307c478bd9Sstevel@tonic-gate {
58317c478bd9Sstevel@tonic-gate 	char filename[PATH_MAX + 1];
58327c478bd9Sstevel@tonic-gate 
58337c478bd9Sstevel@tonic-gate 	/* load the installed root's name_to_major for translations */
58347c478bd9Sstevel@tonic-gate 	(void) snprintf(filename, sizeof (filename), "%s%s", root_dir,
58357c478bd9Sstevel@tonic-gate 	    NAME_TO_MAJOR);
58367c478bd9Sstevel@tonic-gate 	if (load_n2m_table(filename) == DEVFSADM_FAILURE) {
58377c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
58387c478bd9Sstevel@tonic-gate 	}
58397c478bd9Sstevel@tonic-gate 
58407c478bd9Sstevel@tonic-gate 	/* Copy /dev to target disk. No need to copy /devices with devfs */
58417c478bd9Sstevel@tonic-gate 	(void) nftw(DEV, devfsadm_copy_file, 20, FTW_PHYS);
58427c478bd9Sstevel@tonic-gate 
58437c478bd9Sstevel@tonic-gate 	/* Let install handle copying over path_to_inst */
58447c478bd9Sstevel@tonic-gate 
58457c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
58467c478bd9Sstevel@tonic-gate }
58477c478bd9Sstevel@tonic-gate 
58487c478bd9Sstevel@tonic-gate /*
58497c478bd9Sstevel@tonic-gate  * This function copies links, dirs, and device special files.
58507c478bd9Sstevel@tonic-gate  * Note that it always returns DEVFSADM_SUCCESS, so that nftw doesn't
58517c478bd9Sstevel@tonic-gate  * abort.
58527c478bd9Sstevel@tonic-gate  */
58537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
58547c478bd9Sstevel@tonic-gate static int
58557c478bd9Sstevel@tonic-gate devfsadm_copy_file(const char *file, const struct stat *stat,
5856406fc510SToomas Soome     int flags, struct FTW *ftw)
58577c478bd9Sstevel@tonic-gate {
58587c478bd9Sstevel@tonic-gate 	struct stat sp;
58597c478bd9Sstevel@tonic-gate 	dev_t newdev;
58607c478bd9Sstevel@tonic-gate 	char newfile[PATH_MAX + 1];
58617c478bd9Sstevel@tonic-gate 	char linkcontents[PATH_MAX + 1];
58627c478bd9Sstevel@tonic-gate 	int bytes;
58637c478bd9Sstevel@tonic-gate 	const char *fcn = "devfsadm_copy_file";
58647c478bd9Sstevel@tonic-gate 
58657c478bd9Sstevel@tonic-gate 	(void) strcpy(newfile, root_dir);
58667c478bd9Sstevel@tonic-gate 	(void) strcat(newfile, "/");
58677c478bd9Sstevel@tonic-gate 	(void) strcat(newfile, file);
58687c478bd9Sstevel@tonic-gate 
58697c478bd9Sstevel@tonic-gate 	if (lstat(newfile, &sp) == 0) {
58707c478bd9Sstevel@tonic-gate 		/* newfile already exists, so no need to continue */
58717c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
58727c478bd9Sstevel@tonic-gate 	}
58737c478bd9Sstevel@tonic-gate 
58747c478bd9Sstevel@tonic-gate 	if (((stat->st_mode & S_IFMT) == S_IFBLK) ||
58757c478bd9Sstevel@tonic-gate 	    ((stat->st_mode & S_IFMT) == S_IFCHR)) {
58767c478bd9Sstevel@tonic-gate 		if (translate_major(stat->st_rdev, &newdev) ==
58777c478bd9Sstevel@tonic-gate 		    DEVFSADM_FAILURE) {
58787c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
58797c478bd9Sstevel@tonic-gate 		}
58807c478bd9Sstevel@tonic-gate 		if (mknod(newfile, stat->st_mode, newdev) == -1) {
58817c478bd9Sstevel@tonic-gate 			err_print(MKNOD_FAILED, newfile, strerror(errno));
58827c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
58837c478bd9Sstevel@tonic-gate 		}
58847c478bd9Sstevel@tonic-gate 	} else if ((stat->st_mode & S_IFMT) == S_IFDIR) {
58857c478bd9Sstevel@tonic-gate 		if (mknod(newfile, stat->st_mode, 0) == -1) {
58867c478bd9Sstevel@tonic-gate 			err_print(MKNOD_FAILED, newfile, strerror(errno));
58877c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
58887c478bd9Sstevel@tonic-gate 		}
58897c478bd9Sstevel@tonic-gate 	} else if ((stat->st_mode & S_IFMT) == S_IFLNK)  {
589094c894bbSVikram Hegde 		/*
589194c894bbSVikram Hegde 		 * No need to redirect alias paths. We want a
589294c894bbSVikram Hegde 		 * true copy. The system on first boot after install
589394c894bbSVikram Hegde 		 * will redirect paths
589494c894bbSVikram Hegde 		 */
58957c478bd9Sstevel@tonic-gate 		if ((bytes = readlink(file, linkcontents, PATH_MAX)) == -1)  {
58967c478bd9Sstevel@tonic-gate 			err_print(READLINK_FAILED, fcn, file, strerror(errno));
58977c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
58987c478bd9Sstevel@tonic-gate 		}
58997c478bd9Sstevel@tonic-gate 		linkcontents[bytes] = '\0';
59007c478bd9Sstevel@tonic-gate 		if (symlink(linkcontents, newfile) == -1) {
59017c478bd9Sstevel@tonic-gate 			err_print(SYMLINK_FAILED, newfile, newfile,
59020a653502Swroche 			    strerror(errno));
59037c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
59047c478bd9Sstevel@tonic-gate 		}
59057c478bd9Sstevel@tonic-gate 	}
59067c478bd9Sstevel@tonic-gate 
59077c478bd9Sstevel@tonic-gate 	(void) lchown(newfile, stat->st_uid, stat->st_gid);
59087c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
59097c478bd9Sstevel@tonic-gate }
59107c478bd9Sstevel@tonic-gate 
59117c478bd9Sstevel@tonic-gate /*
59127c478bd9Sstevel@tonic-gate  *  Given a dev_t from the running kernel, return the new_dev_t
59137c478bd9Sstevel@tonic-gate  *  by translating to the major number found on the installed
59147c478bd9Sstevel@tonic-gate  *  target's root name_to_major file.
59157c478bd9Sstevel@tonic-gate  */
59167c478bd9Sstevel@tonic-gate static int
59177c478bd9Sstevel@tonic-gate translate_major(dev_t old_dev, dev_t *new_dev)
59187c478bd9Sstevel@tonic-gate {
59197c478bd9Sstevel@tonic-gate 	major_t oldmajor;
59207c478bd9Sstevel@tonic-gate 	major_t newmajor;
59217c478bd9Sstevel@tonic-gate 	minor_t oldminor;
59227c478bd9Sstevel@tonic-gate 	minor_t newminor;
59237c478bd9Sstevel@tonic-gate 	char cdriver[FILENAME_MAX + 1];
59247c478bd9Sstevel@tonic-gate 	char driver[FILENAME_MAX + 1];
59257c478bd9Sstevel@tonic-gate 	char *fcn = "translate_major: ";
59267c478bd9Sstevel@tonic-gate 
59277c478bd9Sstevel@tonic-gate 	oldmajor = major(old_dev);
59280a653502Swroche 	if (modctl(MODGETNAME, driver, sizeof (driver), &oldmajor) != 0) {
59297c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
59307c478bd9Sstevel@tonic-gate 	}
59317c478bd9Sstevel@tonic-gate 
59327c478bd9Sstevel@tonic-gate 	if (strcmp(driver, "clone") != 0) {
59337c478bd9Sstevel@tonic-gate 		/* non-clone case */
59347c478bd9Sstevel@tonic-gate 
59357c478bd9Sstevel@tonic-gate 		/* look up major number is target's name2major */
59367c478bd9Sstevel@tonic-gate 		if (get_major_no(driver, &newmajor) == DEVFSADM_FAILURE) {
59377c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
59387c478bd9Sstevel@tonic-gate 		}
59397c478bd9Sstevel@tonic-gate 
59407c478bd9Sstevel@tonic-gate 		*new_dev = makedev(newmajor, minor(old_dev));
59417c478bd9Sstevel@tonic-gate 		if (old_dev != *new_dev) {
59427c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "%sdriver: %s old: %lu,%lu "
59430a653502Swroche 			    "new: %lu,%lu\n", fcn, driver, major(old_dev),
59440a653502Swroche 			    minor(old_dev), major(*new_dev), minor(*new_dev));
59457c478bd9Sstevel@tonic-gate 		}
59467c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
59477c478bd9Sstevel@tonic-gate 	} else {
59487c478bd9Sstevel@tonic-gate 		/*
59497c478bd9Sstevel@tonic-gate 		 *  The clone is a special case.  Look at its minor
59507c478bd9Sstevel@tonic-gate 		 *  number since it is the major number of the real driver.
59517c478bd9Sstevel@tonic-gate 		 */
59527c478bd9Sstevel@tonic-gate 		if (get_major_no(driver, &newmajor) == DEVFSADM_FAILURE) {
59537c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
59547c478bd9Sstevel@tonic-gate 		}
59557c478bd9Sstevel@tonic-gate 
59567c478bd9Sstevel@tonic-gate 		oldminor = minor(old_dev);
59577c478bd9Sstevel@tonic-gate 		if (modctl(MODGETNAME, cdriver, sizeof (cdriver),
59580a653502Swroche 		    &oldminor) != 0) {
59597c478bd9Sstevel@tonic-gate 			err_print(MODGETNAME_FAILED, oldminor);
59607c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
59617c478bd9Sstevel@tonic-gate 		}
59627c478bd9Sstevel@tonic-gate 
59637c478bd9Sstevel@tonic-gate 		if (get_major_no(cdriver, &newminor) == DEVFSADM_FAILURE) {
59647c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
59657c478bd9Sstevel@tonic-gate 		}
59667c478bd9Sstevel@tonic-gate 
59677c478bd9Sstevel@tonic-gate 		*new_dev = makedev(newmajor, newminor);
59687c478bd9Sstevel@tonic-gate 		if (old_dev != *new_dev) {
59697c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "%sdriver: %s old: "
59700a653502Swroche 			    "%lu,%lu  new: %lu,%lu\n", fcn, driver,
59710a653502Swroche 			    major(old_dev), minor(old_dev),
59720a653502Swroche 			    major(*new_dev), minor(*new_dev));
59737c478bd9Sstevel@tonic-gate 		}
59747c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
59757c478bd9Sstevel@tonic-gate 	}
59767c478bd9Sstevel@tonic-gate }
59777c478bd9Sstevel@tonic-gate 
59787c478bd9Sstevel@tonic-gate /*
59797c478bd9Sstevel@tonic-gate  *
59807c478bd9Sstevel@tonic-gate  * Find the major number for driver, searching the n2m_list that was
59817c478bd9Sstevel@tonic-gate  * built in load_n2m_table().
59827c478bd9Sstevel@tonic-gate  */
59837c478bd9Sstevel@tonic-gate static int
59847c478bd9Sstevel@tonic-gate get_major_no(char *driver, major_t *major)
59857c478bd9Sstevel@tonic-gate {
59867c478bd9Sstevel@tonic-gate 	n2m_t *ptr;
59877c478bd9Sstevel@tonic-gate 
59887c478bd9Sstevel@tonic-gate 	for (ptr = n2m_list; ptr != NULL; ptr = ptr->next) {
59897c478bd9Sstevel@tonic-gate 		if (strcmp(ptr->driver, driver) == 0) {
59907c478bd9Sstevel@tonic-gate 			*major = ptr->major;
59917c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
59927c478bd9Sstevel@tonic-gate 		}
59937c478bd9Sstevel@tonic-gate 	}
59947c478bd9Sstevel@tonic-gate 	err_print(FIND_MAJOR_FAILED, driver);
59957c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
59967c478bd9Sstevel@tonic-gate }
59977c478bd9Sstevel@tonic-gate 
59987c478bd9Sstevel@tonic-gate /*
59997c478bd9Sstevel@tonic-gate  * Loads a name_to_major table into memory.  Used only for suninstall's
60007c478bd9Sstevel@tonic-gate  * private -R option to devfsadm, to translate major numbers from the
60017c478bd9Sstevel@tonic-gate  * running to the installed target disk.
60027c478bd9Sstevel@tonic-gate  */
60037c478bd9Sstevel@tonic-gate static int
60047c478bd9Sstevel@tonic-gate load_n2m_table(char *file)
60057c478bd9Sstevel@tonic-gate {
60067c478bd9Sstevel@tonic-gate 	FILE *fp;
60071ca93273Seota 	char line[1024], *cp;
60087c478bd9Sstevel@tonic-gate 	char driver[PATH_MAX + 1];
60097c478bd9Sstevel@tonic-gate 	major_t major;
60107c478bd9Sstevel@tonic-gate 	n2m_t *ptr;
60117c478bd9Sstevel@tonic-gate 	int ln = 0;
60127c478bd9Sstevel@tonic-gate 
60137c478bd9Sstevel@tonic-gate 	if ((fp = fopen(file, "r")) == NULL) {
60147c478bd9Sstevel@tonic-gate 		err_print(FOPEN_FAILED, file, strerror(errno));
60157c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
60167c478bd9Sstevel@tonic-gate 	}
60177c478bd9Sstevel@tonic-gate 
60187c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line), fp) != NULL) {
60197c478bd9Sstevel@tonic-gate 		ln++;
60201ca93273Seota 		/* cut off comments starting with '#' */
60211ca93273Seota 		if ((cp = strchr(line, '#')) != NULL)
60221ca93273Seota 			*cp = '\0';
60231ca93273Seota 		/* ignore comment or blank lines */
60241ca93273Seota 		if (is_blank(line))
60257c478bd9Sstevel@tonic-gate 			continue;
60261ca93273Seota 		/* sanity-check */
60277c478bd9Sstevel@tonic-gate 		if (sscanf(line, "%1024s%lu", driver, &major) != 2) {
60287c478bd9Sstevel@tonic-gate 			err_print(IGNORING_LINE_IN, ln, file);
60297c478bd9Sstevel@tonic-gate 			continue;
60307c478bd9Sstevel@tonic-gate 		}
60317c478bd9Sstevel@tonic-gate 		ptr = (n2m_t *)s_malloc(sizeof (n2m_t));
60327c478bd9Sstevel@tonic-gate 		ptr->major = major;
60337c478bd9Sstevel@tonic-gate 		ptr->driver = s_strdup(driver);
60347c478bd9Sstevel@tonic-gate 		ptr->next = n2m_list;
60357c478bd9Sstevel@tonic-gate 		n2m_list = ptr;
60367c478bd9Sstevel@tonic-gate 	}
60377c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
60387c478bd9Sstevel@tonic-gate 		err_print(FCLOSE_FAILED, file, strerror(errno));
60397c478bd9Sstevel@tonic-gate 	}
60407c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
60417c478bd9Sstevel@tonic-gate }
60427c478bd9Sstevel@tonic-gate 
60438d483882Smlf /*
60448d483882Smlf  * Called at devfsadm startup to read the file /etc/dev/enumerate_reserved
60458d483882Smlf  * Creates a linked list of devlinks from which reserved IDs can be derived
60468d483882Smlf  */
60478d483882Smlf static void
60488d483882Smlf read_enumerate_file(void)
60498d483882Smlf {
60508d483882Smlf 	FILE *fp;
60518d483882Smlf 	int linenum;
60528d483882Smlf 	char line[PATH_MAX+1];
60538d483882Smlf 	enumerate_file_t *entry;
60548d483882Smlf 	struct stat current_sb;
60558d483882Smlf 	static struct stat cached_sb;
60568d483882Smlf 	static int cached = FALSE;
60578d483882Smlf 
60588d483882Smlf 	assert(enumerate_file);
60598d483882Smlf 
60608d483882Smlf 	if (stat(enumerate_file, &current_sb) == -1) {
60618d483882Smlf 		vprint(RSRV_MID, "No reserved file: %s\n", enumerate_file);
60628d483882Smlf 		cached = FALSE;
60638d483882Smlf 		if (enumerate_reserved != NULL) {
60648d483882Smlf 			vprint(RSRV_MID, "invalidating %s cache\n",
60658d483882Smlf 			    enumerate_file);
60668d483882Smlf 		}
60678d483882Smlf 		while (enumerate_reserved != NULL) {
60688d483882Smlf 			entry = enumerate_reserved;
60698d483882Smlf 			enumerate_reserved = entry->er_next;
60708d483882Smlf 			free(entry->er_file);
60718d483882Smlf 			free(entry->er_id);
60728d483882Smlf 			free(entry);
60738d483882Smlf 		}
60748d483882Smlf 		return;
60758d483882Smlf 	}
60768d483882Smlf 
60778d483882Smlf 	/* if already cached, check to see if it is still valid */
60788d483882Smlf 	if (cached == TRUE) {
60798d483882Smlf 
60808d483882Smlf 		if (current_sb.st_mtime == cached_sb.st_mtime) {
60818d483882Smlf 			vprint(RSRV_MID, "%s cache valid\n", enumerate_file);
60828d483882Smlf 			vprint(FILES_MID, "%s cache valid\n", enumerate_file);
60838d483882Smlf 			return;
60848d483882Smlf 		}
60858d483882Smlf 
60868d483882Smlf 		vprint(RSRV_MID, "invalidating %s cache\n", enumerate_file);
60878d483882Smlf 		vprint(FILES_MID, "invalidating %s cache\n", enumerate_file);
60888d483882Smlf 
60898d483882Smlf 		while (enumerate_reserved != NULL) {
60908d483882Smlf 			entry = enumerate_reserved;
60918d483882Smlf 			enumerate_reserved = entry->er_next;
60928d483882Smlf 			free(entry->er_file);
60938d483882Smlf 			free(entry->er_id);
60948d483882Smlf 			free(entry);
60958d483882Smlf 		}
60968d483882Smlf 		vprint(RSRV_MID, "Recaching file: %s\n", enumerate_file);
60978d483882Smlf 	} else {
60988d483882Smlf 		vprint(RSRV_MID, "Caching file (first time): %s\n",
60998d483882Smlf 		    enumerate_file);
61008d483882Smlf 		cached = TRUE;
61018d483882Smlf 	}
61028d483882Smlf 
61038d483882Smlf 	(void) stat(enumerate_file, &cached_sb);
61048d483882Smlf 
61058d483882Smlf 	if ((fp = fopen(enumerate_file, "r")) == NULL) {
61068d483882Smlf 		err_print(FOPEN_FAILED, enumerate_file, strerror(errno));
61078d483882Smlf 		return;
61088d483882Smlf 	}
61098d483882Smlf 
61108d483882Smlf 	vprint(RSRV_MID, "Reading reserve file: %s\n", enumerate_file);
61118d483882Smlf 	linenum = 0;
61128d483882Smlf 	while (fgets(line, sizeof (line), fp) != NULL) {
61138d483882Smlf 		char	*cp, *ncp;
61148d483882Smlf 
61158d483882Smlf 		linenum++;
61168d483882Smlf 
61178d483882Smlf 		/* remove newline */
61188d483882Smlf 		cp = strchr(line, '\n');
61198d483882Smlf 		if (cp)
61208d483882Smlf 			*cp = '\0';
61218d483882Smlf 
61220a653502Swroche 		vprint(RSRV_MID, "Reserve file: line %d: %s\n", linenum, line);
61238d483882Smlf 
61248d483882Smlf 		/* skip over space and tab */
61250a653502Swroche 		for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
61260a653502Swroche 			;
61278d483882Smlf 
61288d483882Smlf 		if (*cp == '\0' || *cp == '#') {
61298d483882Smlf 			vprint(RSRV_MID, "Skipping line: '%s'\n", line);
61308d483882Smlf 			continue; /* blank line or comment line */
61318d483882Smlf 		}
61328d483882Smlf 
61338d483882Smlf 		ncp = cp;
61348d483882Smlf 
61358d483882Smlf 		/* delete trailing blanks */
61360a653502Swroche 		for (; *cp != ' ' && *cp != '\t' && *cp != '\0'; cp++)
61370a653502Swroche 			;
61388d483882Smlf 		*cp = '\0';
61398d483882Smlf 
61408d483882Smlf 		entry = s_zalloc(sizeof (enumerate_file_t));
61418d483882Smlf 		entry->er_file = s_strdup(ncp);
61428d483882Smlf 		entry->er_id = NULL;
61438d483882Smlf 		entry->er_next = enumerate_reserved;
61448d483882Smlf 		enumerate_reserved = entry;
61458d483882Smlf 	}
61468d483882Smlf 
61478d483882Smlf 	if (fclose(fp) == EOF) {
61488d483882Smlf 		err_print(FCLOSE_FAILED, enumerate_file, strerror(errno));
61498d483882Smlf 	}
61508d483882Smlf }
61518d483882Smlf 
61527c478bd9Sstevel@tonic-gate /*
61537c478bd9Sstevel@tonic-gate  * Called at devfsadm startup to read in the devlink.tab file.	Creates
61547c478bd9Sstevel@tonic-gate  * a linked list of devlinktab_list structures which will be
61557c478bd9Sstevel@tonic-gate  * searched for every minor node.
61567c478bd9Sstevel@tonic-gate  */
61577c478bd9Sstevel@tonic-gate static void
61587c478bd9Sstevel@tonic-gate read_devlinktab_file(void)
61597c478bd9Sstevel@tonic-gate {
61607c478bd9Sstevel@tonic-gate 	devlinktab_list_t *headp = NULL;
61617c478bd9Sstevel@tonic-gate 	devlinktab_list_t *entryp;
61627c478bd9Sstevel@tonic-gate 	devlinktab_list_t **previous;
61637c478bd9Sstevel@tonic-gate 	devlinktab_list_t *save;
61641ca93273Seota 	char line[MAX_DEVLINK_LINE], *cp;
61657c478bd9Sstevel@tonic-gate 	char *selector;
61667c478bd9Sstevel@tonic-gate 	char *p_link;
61677c478bd9Sstevel@tonic-gate 	char *s_link;
61687c478bd9Sstevel@tonic-gate 	FILE *fp;
61697c478bd9Sstevel@tonic-gate 	int i;
61707c478bd9Sstevel@tonic-gate 	static struct stat cached_sb;
61717c478bd9Sstevel@tonic-gate 	struct stat current_sb;
61727c478bd9Sstevel@tonic-gate 	static int cached = FALSE;
61737c478bd9Sstevel@tonic-gate 
61747c478bd9Sstevel@tonic-gate 	if (devlinktab_file == NULL) {
61757c478bd9Sstevel@tonic-gate 		return;
61767c478bd9Sstevel@tonic-gate 	}
61777c478bd9Sstevel@tonic-gate 
61787c478bd9Sstevel@tonic-gate 	(void) stat(devlinktab_file, &current_sb);
61797c478bd9Sstevel@tonic-gate 
61807c478bd9Sstevel@tonic-gate 	/* if already cached, check to see if it is still valid */
61817c478bd9Sstevel@tonic-gate 	if (cached == TRUE) {
61827c478bd9Sstevel@tonic-gate 
61837c478bd9Sstevel@tonic-gate 		if (current_sb.st_mtime == cached_sb.st_mtime) {
61847c478bd9Sstevel@tonic-gate 			vprint(FILES_MID, "%s cache valid\n", devlinktab_file);
61857c478bd9Sstevel@tonic-gate 			return;
61867c478bd9Sstevel@tonic-gate 		}
61877c478bd9Sstevel@tonic-gate 
61887c478bd9Sstevel@tonic-gate 		vprint(FILES_MID, "invalidating %s cache\n", devlinktab_file);
61897c478bd9Sstevel@tonic-gate 
61907c478bd9Sstevel@tonic-gate 		while (devlinktab_list != NULL) {
61917c478bd9Sstevel@tonic-gate 			free_link_list(devlinktab_list->p_link);
61927c478bd9Sstevel@tonic-gate 			free_link_list(devlinktab_list->s_link);
61937c478bd9Sstevel@tonic-gate 			free_selector_list(devlinktab_list->selector);
61947c478bd9Sstevel@tonic-gate 			free(devlinktab_list->selector_pattern);
61957c478bd9Sstevel@tonic-gate 			free(devlinktab_list->p_link_pattern);
61967c478bd9Sstevel@tonic-gate 			if (devlinktab_list->s_link_pattern != NULL) {
61977c478bd9Sstevel@tonic-gate 				free(devlinktab_list->s_link_pattern);
61987c478bd9Sstevel@tonic-gate 			}
61997c478bd9Sstevel@tonic-gate 			save = devlinktab_list;
62007c478bd9Sstevel@tonic-gate 			devlinktab_list = devlinktab_list->next;
62017c478bd9Sstevel@tonic-gate 			free(save);
62027c478bd9Sstevel@tonic-gate 		}
62037c478bd9Sstevel@tonic-gate 	} else {
62047c478bd9Sstevel@tonic-gate 		cached = TRUE;
62057c478bd9Sstevel@tonic-gate 	}
62067c478bd9Sstevel@tonic-gate 
62077c478bd9Sstevel@tonic-gate 	(void) stat(devlinktab_file, &cached_sb);
62087c478bd9Sstevel@tonic-gate 
62097c478bd9Sstevel@tonic-gate 	if ((fp = fopen(devlinktab_file, "r")) == NULL) {
62107c478bd9Sstevel@tonic-gate 		err_print(FOPEN_FAILED, devlinktab_file, strerror(errno));
62117c478bd9Sstevel@tonic-gate 		return;
62127c478bd9Sstevel@tonic-gate 	}
62137c478bd9Sstevel@tonic-gate 
62147c478bd9Sstevel@tonic-gate 	previous = &headp;
62157c478bd9Sstevel@tonic-gate 
62167c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line), fp) != NULL) {
62177c478bd9Sstevel@tonic-gate 		devlinktab_line++;
62187c478bd9Sstevel@tonic-gate 		i = strlen(line);
62197c478bd9Sstevel@tonic-gate 		if (line[i-1] == NEWLINE) {
62207c478bd9Sstevel@tonic-gate 			line[i-1] = '\0';
62217c478bd9Sstevel@tonic-gate 		} else if (i == sizeof (line-1)) {
62227c478bd9Sstevel@tonic-gate 			err_print(LINE_TOO_LONG, devlinktab_line,
62237c478bd9Sstevel@tonic-gate 			    devlinktab_file, sizeof (line)-1);
62240a653502Swroche 			while (((i = getc(fp)) != '\n') && (i != EOF))
62250a653502Swroche 				;
62267c478bd9Sstevel@tonic-gate 			continue;
62277c478bd9Sstevel@tonic-gate 		}
62287c478bd9Sstevel@tonic-gate 
62291ca93273Seota 		/* cut off comments starting with '#' */
62301ca93273Seota 		if ((cp = strchr(line, '#')) != NULL)
62311ca93273Seota 			*cp = '\0';
62321ca93273Seota 		/* ignore comment or blank lines */
62331ca93273Seota 		if (is_blank(line))
62347c478bd9Sstevel@tonic-gate 			continue;
62357c478bd9Sstevel@tonic-gate 
62367c478bd9Sstevel@tonic-gate 		vprint(DEVLINK_MID, "table: %s line %d: '%s'\n",
62370a653502Swroche 		    devlinktab_file, devlinktab_line, line);
62387c478bd9Sstevel@tonic-gate 
62397c478bd9Sstevel@tonic-gate 		/* break each entry into fields.  s_link may be NULL */
62407c478bd9Sstevel@tonic-gate 		if (split_devlinktab_entry(line, &selector, &p_link,
62417c478bd9Sstevel@tonic-gate 		    &s_link) == DEVFSADM_FAILURE) {
62427c478bd9Sstevel@tonic-gate 			vprint(DEVLINK_MID, "split_entry returns failure\n");
62437c478bd9Sstevel@tonic-gate 			continue;
62447c478bd9Sstevel@tonic-gate 		} else {
62457c478bd9Sstevel@tonic-gate 			vprint(DEVLINK_MID, "split_entry selector='%s' "
62460a653502Swroche 			    "p_link='%s' s_link='%s'\n\n", selector,
62470a653502Swroche 			    p_link, (s_link == NULL) ? "" : s_link);
62487c478bd9Sstevel@tonic-gate 		}
62497c478bd9Sstevel@tonic-gate 
62500a653502Swroche 		entryp =
62510a653502Swroche 		    (devlinktab_list_t *)s_malloc(sizeof (devlinktab_list_t));
62527c478bd9Sstevel@tonic-gate 
62537c478bd9Sstevel@tonic-gate 		entryp->line_number = devlinktab_line;
62547c478bd9Sstevel@tonic-gate 
62550a653502Swroche 		if ((entryp->selector = create_selector_list(selector))
62560a653502Swroche 		    == NULL) {
62577c478bd9Sstevel@tonic-gate 			free(entryp);
62587c478bd9Sstevel@tonic-gate 			continue;
62597c478bd9Sstevel@tonic-gate 		}
62607c478bd9Sstevel@tonic-gate 		entryp->selector_pattern = s_strdup(selector);
62617c478bd9Sstevel@tonic-gate 
62627c478bd9Sstevel@tonic-gate 		if ((entryp->p_link = create_link_list(p_link)) == NULL) {
62637c478bd9Sstevel@tonic-gate 			free_selector_list(entryp->selector);
62647c478bd9Sstevel@tonic-gate 			free(entryp->selector_pattern);
62657c478bd9Sstevel@tonic-gate 			free(entryp);
62667c478bd9Sstevel@tonic-gate 			continue;
62677c478bd9Sstevel@tonic-gate 		}
62687c478bd9Sstevel@tonic-gate 
62697c478bd9Sstevel@tonic-gate 		entryp->p_link_pattern = s_strdup(p_link);
62707c478bd9Sstevel@tonic-gate 
62717c478bd9Sstevel@tonic-gate 		if (s_link != NULL) {
62727c478bd9Sstevel@tonic-gate 			if ((entryp->s_link =
62737c478bd9Sstevel@tonic-gate 			    create_link_list(s_link)) == NULL) {
62747c478bd9Sstevel@tonic-gate 				free_selector_list(entryp->selector);
62757c478bd9Sstevel@tonic-gate 				free_link_list(entryp->p_link);
62767c478bd9Sstevel@tonic-gate 				free(entryp->selector_pattern);
62777c478bd9Sstevel@tonic-gate 				free(entryp->p_link_pattern);
62787c478bd9Sstevel@tonic-gate 				free(entryp);
62797c478bd9Sstevel@tonic-gate 				continue;
62807c478bd9Sstevel@tonic-gate 			}
62810a653502Swroche 			entryp->s_link_pattern = s_strdup(s_link);
62827c478bd9Sstevel@tonic-gate 		} else {
62837c478bd9Sstevel@tonic-gate 			entryp->s_link = NULL;
62847c478bd9Sstevel@tonic-gate 			entryp->s_link_pattern = NULL;
62857c478bd9Sstevel@tonic-gate 
62867c478bd9Sstevel@tonic-gate 		}
62877c478bd9Sstevel@tonic-gate 
62887c478bd9Sstevel@tonic-gate 		/* append to end of list */
62897c478bd9Sstevel@tonic-gate 
62907c478bd9Sstevel@tonic-gate 		entryp->next = NULL;
62917c478bd9Sstevel@tonic-gate 		*previous = entryp;
62927c478bd9Sstevel@tonic-gate 		previous = &(entryp->next);
62937c478bd9Sstevel@tonic-gate 	}
62947c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
62957c478bd9Sstevel@tonic-gate 		err_print(FCLOSE_FAILED, devlinktab_file, strerror(errno));
62967c478bd9Sstevel@tonic-gate 	}
62977c478bd9Sstevel@tonic-gate 	devlinktab_list = headp;
62987c478bd9Sstevel@tonic-gate }
62997c478bd9Sstevel@tonic-gate 
63007c478bd9Sstevel@tonic-gate /*
63017c478bd9Sstevel@tonic-gate  *
63027c478bd9Sstevel@tonic-gate  * For a single line entry in devlink.tab, split the line into fields
63037c478bd9Sstevel@tonic-gate  * selector, p_link, and an optionally s_link.	If s_link field is not
63047c478bd9Sstevel@tonic-gate  * present, then return NULL in s_link (not NULL string).
63057c478bd9Sstevel@tonic-gate  */
63067c478bd9Sstevel@tonic-gate static int
63077c478bd9Sstevel@tonic-gate split_devlinktab_entry(char *entry, char **selector, char **p_link,
6308406fc510SToomas Soome     char **s_link)
63097c478bd9Sstevel@tonic-gate {
63107c478bd9Sstevel@tonic-gate 	char *tab;
63117c478bd9Sstevel@tonic-gate 
63127c478bd9Sstevel@tonic-gate 	*selector = entry;
63137c478bd9Sstevel@tonic-gate 
63147c478bd9Sstevel@tonic-gate 	if ((tab = strchr(entry, TAB)) != NULL) {
63157c478bd9Sstevel@tonic-gate 		*tab = '\0';
63167c478bd9Sstevel@tonic-gate 		*p_link = ++tab;
63177c478bd9Sstevel@tonic-gate 	} else {
63187c478bd9Sstevel@tonic-gate 		err_print(MISSING_TAB, devlinktab_line, devlinktab_file);
63197c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
63207c478bd9Sstevel@tonic-gate 	}
63217c478bd9Sstevel@tonic-gate 
63229813518fSToomas Soome 	if (**p_link == '\0') {
63237c478bd9Sstevel@tonic-gate 		err_print(MISSING_DEVNAME, devlinktab_line, devlinktab_file);
63247c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
63257c478bd9Sstevel@tonic-gate 	}
63267c478bd9Sstevel@tonic-gate 
63277c478bd9Sstevel@tonic-gate 	if ((tab = strchr(*p_link, TAB)) != NULL) {
63287c478bd9Sstevel@tonic-gate 		*tab = '\0';
63297c478bd9Sstevel@tonic-gate 		*s_link = ++tab;
63307c478bd9Sstevel@tonic-gate 		if (strchr(*s_link, TAB) != NULL) {
63317c478bd9Sstevel@tonic-gate 			err_print(TOO_MANY_FIELDS, devlinktab_line,
63320a653502Swroche 			    devlinktab_file);
63337c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
63347c478bd9Sstevel@tonic-gate 		}
63357c478bd9Sstevel@tonic-gate 	} else {
63367c478bd9Sstevel@tonic-gate 		*s_link = NULL;
63377c478bd9Sstevel@tonic-gate 	}
63387c478bd9Sstevel@tonic-gate 
63397c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
63407c478bd9Sstevel@tonic-gate }
63417c478bd9Sstevel@tonic-gate 
63427c478bd9Sstevel@tonic-gate /*
63437c478bd9Sstevel@tonic-gate  * For a given devfs_spec field, for each element in the field, add it to
63447c478bd9Sstevel@tonic-gate  * a linked list of devfs_spec structures.  Return the linked list in
63457c478bd9Sstevel@tonic-gate  * devfs_spec_list.
63467c478bd9Sstevel@tonic-gate  */
63477c478bd9Sstevel@tonic-gate static selector_list_t *
63487c478bd9Sstevel@tonic-gate create_selector_list(char *selector)
63497c478bd9Sstevel@tonic-gate {
63500a653502Swroche 	char *key;
63510a653502Swroche 	char *val;
63520a653502Swroche 	int error = FALSE;
63530a653502Swroche 	selector_list_t *head_selector_list = NULL;
63540a653502Swroche 	selector_list_t *selector_list;
63550a653502Swroche 
63560a653502Swroche 	/* parse_devfs_spec splits the next field into keyword & value */
63570a653502Swroche 	while ((*selector != NULL) && (error == FALSE)) {
63580a653502Swroche 		if (parse_selector(&selector, &key, &val) == DEVFSADM_FAILURE) {
63590a653502Swroche 			error = TRUE;
63600a653502Swroche 			break;
63610a653502Swroche 		} else {
63620a653502Swroche 			selector_list = (selector_list_t *)
63630a653502Swroche 			    s_malloc(sizeof (selector_list_t));
63640a653502Swroche 			if (strcmp(NAME_S, key) == 0) {
63650a653502Swroche 				selector_list->key = NAME;
63660a653502Swroche 			} else if (strcmp(TYPE_S, key) == 0) {
63670a653502Swroche 				selector_list->key = TYPE;
63680a653502Swroche 			} else if (strncmp(ADDR_S, key, ADDR_S_LEN) == 0) {
63690a653502Swroche 				selector_list->key = ADDR;
63700a653502Swroche 				if (key[ADDR_S_LEN] == '\0') {
63710a653502Swroche 					selector_list->arg = 0;
63720a653502Swroche 				} else if (isdigit(key[ADDR_S_LEN]) != FALSE) {
63730a653502Swroche 					selector_list->arg =
63740a653502Swroche 					    atoi(&key[ADDR_S_LEN]);
63750a653502Swroche 				} else {
63760a653502Swroche 					error = TRUE;
63770a653502Swroche 					free(selector_list);
63780a653502Swroche 					err_print(BADKEYWORD, key,
63790a653502Swroche 					    devlinktab_line, devlinktab_file);
63800a653502Swroche 					break;
63810a653502Swroche 				}
63820a653502Swroche 			} else if (strncmp(MINOR_S, key, MINOR_S_LEN) == 0) {
63830a653502Swroche 				selector_list->key = MINOR;
63840a653502Swroche 				if (key[MINOR_S_LEN] == '\0') {
63850a653502Swroche 					selector_list->arg = 0;
63860a653502Swroche 				} else if (isdigit(key[MINOR_S_LEN]) != FALSE) {
63870a653502Swroche 					selector_list->arg =
63880a653502Swroche 					    atoi(&key[MINOR_S_LEN]);
63890a653502Swroche 				} else {
63900a653502Swroche 					error = TRUE;
63910a653502Swroche 					free(selector_list);
63920a653502Swroche 					err_print(BADKEYWORD, key,
63930a653502Swroche 					    devlinktab_line, devlinktab_file);
63940a653502Swroche 					break;
63950a653502Swroche 				}
63960a653502Swroche 				vprint(DEVLINK_MID, "MINOR = %s\n", val);
63970a653502Swroche 			} else {
63980a653502Swroche 				err_print(UNRECOGNIZED_KEY, key,
63990a653502Swroche 				    devlinktab_line, devlinktab_file);
64000a653502Swroche 				error = TRUE;
64010a653502Swroche 				free(selector_list);
64020a653502Swroche 				break;
64030a653502Swroche 			}
64040a653502Swroche 			selector_list->val = s_strdup(val);
64050a653502Swroche 			selector_list->next = head_selector_list;
64060a653502Swroche 			head_selector_list = selector_list;
64070a653502Swroche 			vprint(DEVLINK_MID, "key='%s' val='%s' arg=%d\n",
64080a653502Swroche 			    key, val, selector_list->arg);
64090a653502Swroche 		}
64100a653502Swroche 	}
64110a653502Swroche 
64120a653502Swroche 	if ((error == FALSE) && (head_selector_list != NULL)) {
64130a653502Swroche 		return (head_selector_list);
64140a653502Swroche 	} else {
64150a653502Swroche 		/* parse failed.  Free any allocated structs */
64160a653502Swroche 		free_selector_list(head_selector_list);
64170a653502Swroche 		return (NULL);
64180a653502Swroche 	}
64197c478bd9Sstevel@tonic-gate }
64207c478bd9Sstevel@tonic-gate 
64217c478bd9Sstevel@tonic-gate /*
64227c478bd9Sstevel@tonic-gate  * Takes a semicolon separated list of selector elements and breaks up
64237c478bd9Sstevel@tonic-gate  * into a keyword-value pair.	semicolon and equal characters are
64247c478bd9Sstevel@tonic-gate  * replaced with NULL's.  On success, selector is updated to point to the
64257c478bd9Sstevel@tonic-gate  * terminating NULL character terminating the keyword-value pair, and the
64267c478bd9Sstevel@tonic-gate  * function returns DEVFSADM_SUCCESS.	If there is a syntax error,
64277c478bd9Sstevel@tonic-gate  * devfs_spec is not modified and function returns DEVFSADM_FAILURE.
64287c478bd9Sstevel@tonic-gate  */
64297c478bd9Sstevel@tonic-gate static int
64307c478bd9Sstevel@tonic-gate parse_selector(char **selector, char **key, char **val)
64317c478bd9Sstevel@tonic-gate {
64327c478bd9Sstevel@tonic-gate 	char *equal;
64337c478bd9Sstevel@tonic-gate 	char *semi_colon;
64347c478bd9Sstevel@tonic-gate 
64357c478bd9Sstevel@tonic-gate 	*key = *selector;
64367c478bd9Sstevel@tonic-gate 
64377c478bd9Sstevel@tonic-gate 	if ((equal = strchr(*key, '=')) != NULL) {
64387c478bd9Sstevel@tonic-gate 		*equal = '\0';
64397c478bd9Sstevel@tonic-gate 	} else {
64407c478bd9Sstevel@tonic-gate 		err_print(MISSING_EQUAL, devlinktab_line, devlinktab_file);
64417c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
64427c478bd9Sstevel@tonic-gate 	}
64437c478bd9Sstevel@tonic-gate 
64447c478bd9Sstevel@tonic-gate 	*val = ++equal;
64457c478bd9Sstevel@tonic-gate 	if ((semi_colon = strchr(equal, ';')) != NULL) {
64467c478bd9Sstevel@tonic-gate 		*semi_colon = '\0';
64477c478bd9Sstevel@tonic-gate 		*selector = semi_colon + 1;
64487c478bd9Sstevel@tonic-gate 	} else {
64497c478bd9Sstevel@tonic-gate 		*selector = equal + strlen(equal);
64507c478bd9Sstevel@tonic-gate 	}
64517c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
64527c478bd9Sstevel@tonic-gate }
64537c478bd9Sstevel@tonic-gate 
64547c478bd9Sstevel@tonic-gate /*
64557c478bd9Sstevel@tonic-gate  * link is either the second or third field of devlink.tab.  Parse link
64567c478bd9Sstevel@tonic-gate  * into a linked list of devlink structures and return ptr to list.  Each
64577c478bd9Sstevel@tonic-gate  * list element is either a constant string, or one of the following
64587c478bd9Sstevel@tonic-gate  * escape sequences: \M, \A, \N, or \D.  The first three escape sequences
64597c478bd9Sstevel@tonic-gate  * take a numerical argument.
64607c478bd9Sstevel@tonic-gate  */
64617c478bd9Sstevel@tonic-gate static link_list_t *
64627c478bd9Sstevel@tonic-gate create_link_list(char *link)
64637c478bd9Sstevel@tonic-gate {
64647c478bd9Sstevel@tonic-gate 	int x = 0;
64657c478bd9Sstevel@tonic-gate 	int error = FALSE;
64667c478bd9Sstevel@tonic-gate 	int counter_found = FALSE;
64677c478bd9Sstevel@tonic-gate 	link_list_t *head = NULL;
64687c478bd9Sstevel@tonic-gate 	link_list_t **ptr;
64697c478bd9Sstevel@tonic-gate 	link_list_t *link_list;
64707c478bd9Sstevel@tonic-gate 	char constant[MAX_DEVLINK_LINE];
64717c478bd9Sstevel@tonic-gate 	char *error_str;
64727c478bd9Sstevel@tonic-gate 
64737c478bd9Sstevel@tonic-gate 	if (link == NULL) {
64747c478bd9Sstevel@tonic-gate 		return (NULL);
64757c478bd9Sstevel@tonic-gate 	}
64767c478bd9Sstevel@tonic-gate 
64777c478bd9Sstevel@tonic-gate 	while ((*link != '\0') && (error == FALSE)) {
64787c478bd9Sstevel@tonic-gate 		link_list = (link_list_t *)s_malloc(sizeof (link_list_t));
64797c478bd9Sstevel@tonic-gate 		link_list->next = NULL;
64807c478bd9Sstevel@tonic-gate 
64817c478bd9Sstevel@tonic-gate 		while ((*link != '\0') && (*link != '\\')) {
64827c478bd9Sstevel@tonic-gate 			/* a non-escaped string */
64837c478bd9Sstevel@tonic-gate 			constant[x++] = *(link++);
64847c478bd9Sstevel@tonic-gate 		}
64857c478bd9Sstevel@tonic-gate 		if (x != 0) {
64867c478bd9Sstevel@tonic-gate 			constant[x] = '\0';
64877c478bd9Sstevel@tonic-gate 			link_list->type = CONSTANT;
64887c478bd9Sstevel@tonic-gate 			link_list->constant = s_strdup(constant);
64897c478bd9Sstevel@tonic-gate 			x = 0;
64907c478bd9Sstevel@tonic-gate 			vprint(DEVLINK_MID, "CONSTANT FOUND %s\n", constant);
64917c478bd9Sstevel@tonic-gate 		} else {
64927c478bd9Sstevel@tonic-gate 			switch (*(++link)) {
64937c478bd9Sstevel@tonic-gate 			case 'M':
64947c478bd9Sstevel@tonic-gate 				link_list->type = MINOR;
64957c478bd9Sstevel@tonic-gate 				break;
64967c478bd9Sstevel@tonic-gate 			case 'A':
64977c478bd9Sstevel@tonic-gate 				link_list->type = ADDR;
64987c478bd9Sstevel@tonic-gate 				break;
64997c478bd9Sstevel@tonic-gate 			case 'N':
65007c478bd9Sstevel@tonic-gate 				if (counter_found == TRUE) {
65017c478bd9Sstevel@tonic-gate 					error = TRUE;
65020a653502Swroche 					error_str =
65030a653502Swroche 					    "multiple counters not permitted";
65047c478bd9Sstevel@tonic-gate 					free(link_list);
65057c478bd9Sstevel@tonic-gate 				} else {
65067c478bd9Sstevel@tonic-gate 					counter_found = TRUE;
65077c478bd9Sstevel@tonic-gate 					link_list->type = COUNTER;
65087c478bd9Sstevel@tonic-gate 				}
65097c478bd9Sstevel@tonic-gate 				break;
65107c478bd9Sstevel@tonic-gate 			case 'D':
65117c478bd9Sstevel@tonic-gate 				link_list->type = NAME;
65127c478bd9Sstevel@tonic-gate 				break;
65137c478bd9Sstevel@tonic-gate 			default:
65147c478bd9Sstevel@tonic-gate 				error = TRUE;
65157c478bd9Sstevel@tonic-gate 				free(link_list);
65167c478bd9Sstevel@tonic-gate 				error_str = "unrecognized escape sequence";
65177c478bd9Sstevel@tonic-gate 				break;
65187c478bd9Sstevel@tonic-gate 			}
65197c478bd9Sstevel@tonic-gate 			if (*(link++) != 'D') {
65207c478bd9Sstevel@tonic-gate 				if (isdigit(*link) == FALSE) {
65217c478bd9Sstevel@tonic-gate 					error_str = "escape sequence must be "
65220a653502Swroche 					    "followed by a digit\n";
65237c478bd9Sstevel@tonic-gate 					error = TRUE;
65247c478bd9Sstevel@tonic-gate 					free(link_list);
65257c478bd9Sstevel@tonic-gate 				} else {
65267c478bd9Sstevel@tonic-gate 					link_list->arg =
65270a653502Swroche 					    (int)strtoul(link, &link, 10);
65287c478bd9Sstevel@tonic-gate 					vprint(DEVLINK_MID, "link_list->arg = "
65290a653502Swroche 					    "%d\n", link_list->arg);
65307c478bd9Sstevel@tonic-gate 				}
65317c478bd9Sstevel@tonic-gate 			}
65327c478bd9Sstevel@tonic-gate 		}
65337c478bd9Sstevel@tonic-gate 		/* append link_list struct to end of list */
65347c478bd9Sstevel@tonic-gate 		if (error == FALSE) {
65350a653502Swroche 			for (ptr = &head; *ptr != NULL; ptr = &((*ptr)->next))
65360a653502Swroche 				;
65377c478bd9Sstevel@tonic-gate 			*ptr = link_list;
65387c478bd9Sstevel@tonic-gate 		}
65397c478bd9Sstevel@tonic-gate 	}
65407c478bd9Sstevel@tonic-gate 
65417c478bd9Sstevel@tonic-gate 	if (error == FALSE) {
65427c478bd9Sstevel@tonic-gate 		return (head);
65437c478bd9Sstevel@tonic-gate 	} else {
65447c478bd9Sstevel@tonic-gate 		err_print(CONFIG_INCORRECT, devlinktab_line, devlinktab_file,
65457c478bd9Sstevel@tonic-gate 		    error_str);
65467c478bd9Sstevel@tonic-gate 		free_link_list(head);
65477c478bd9Sstevel@tonic-gate 		return (NULL);
65487c478bd9Sstevel@tonic-gate 	}
65497c478bd9Sstevel@tonic-gate }
65507c478bd9Sstevel@tonic-gate 
65517c478bd9Sstevel@tonic-gate /*
65527c478bd9Sstevel@tonic-gate  * Called for each minor node devfsadm processes; for each minor node,
65537c478bd9Sstevel@tonic-gate  * look for matches in the devlinktab_list list which was created on
65547c478bd9Sstevel@tonic-gate  * startup read_devlinktab_file().  If there is a match, call build_links()
65557c478bd9Sstevel@tonic-gate  * to build a logical devlink and a possible extra devlink.
65567c478bd9Sstevel@tonic-gate  */
65577c478bd9Sstevel@tonic-gate static int
65587c478bd9Sstevel@tonic-gate process_devlink_compat(di_minor_t minor, di_node_t node)
65597c478bd9Sstevel@tonic-gate {
65607c478bd9Sstevel@tonic-gate 	int link_built = FALSE;
65617c478bd9Sstevel@tonic-gate 	devlinktab_list_t *entry;
65627c478bd9Sstevel@tonic-gate 	char *nodetype;
65637c478bd9Sstevel@tonic-gate 	char *dev_path;
65647c478bd9Sstevel@tonic-gate 
65657c478bd9Sstevel@tonic-gate 	if (devlinks_debug == TRUE) {
65667c478bd9Sstevel@tonic-gate 		nodetype =  di_minor_nodetype(minor);
65677c478bd9Sstevel@tonic-gate 		assert(nodetype != NULL);
65687c478bd9Sstevel@tonic-gate 		if ((dev_path = di_devfs_path(node)) != NULL) {
65690a653502Swroche 			vprint(INFO_MID, "'%s' entry: %s:%s\n",
65700a653502Swroche 			    nodetype, dev_path,
65710a653502Swroche 			    di_minor_name(minor) ? di_minor_name(minor) : "");
65727c478bd9Sstevel@tonic-gate 			di_devfs_path_free(dev_path);
65737c478bd9Sstevel@tonic-gate 		}
65747c478bd9Sstevel@tonic-gate 
65757c478bd9Sstevel@tonic-gate 	}
65767c478bd9Sstevel@tonic-gate 
65777c478bd9Sstevel@tonic-gate 
65787c478bd9Sstevel@tonic-gate 	/* don't process devlink.tab if devfsadm invoked with -c <class> */
65797c478bd9Sstevel@tonic-gate 	if (num_classes > 0) {
65807c478bd9Sstevel@tonic-gate 		return (FALSE);
65817c478bd9Sstevel@tonic-gate 	}
65827c478bd9Sstevel@tonic-gate 
65837c478bd9Sstevel@tonic-gate 	for (entry = devlinktab_list; entry != NULL; entry = entry->next) {
65847c478bd9Sstevel@tonic-gate 		if (devlink_matches(entry, minor, node) == DEVFSADM_SUCCESS) {
65857c478bd9Sstevel@tonic-gate 			link_built = TRUE;
65867c478bd9Sstevel@tonic-gate 			(void) build_links(entry, minor, node);
65877c478bd9Sstevel@tonic-gate 		}
65887c478bd9Sstevel@tonic-gate 	}
65897c478bd9Sstevel@tonic-gate 	return (link_built);
65907c478bd9Sstevel@tonic-gate }
65917c478bd9Sstevel@tonic-gate 
65927c478bd9Sstevel@tonic-gate /*
65937c478bd9Sstevel@tonic-gate  * For a given devlink.tab devlinktab_list entry, see if the selector
65947c478bd9Sstevel@tonic-gate  * field matches this minor node.  If it does, return DEVFSADM_SUCCESS,
65957c478bd9Sstevel@tonic-gate  * otherwise DEVFSADM_FAILURE.
65967c478bd9Sstevel@tonic-gate  */
65977c478bd9Sstevel@tonic-gate static int
65987c478bd9Sstevel@tonic-gate devlink_matches(devlinktab_list_t *entry, di_minor_t minor, di_node_t node)
65997c478bd9Sstevel@tonic-gate {
66007c478bd9Sstevel@tonic-gate 	selector_list_t *selector = entry->selector;
66017c478bd9Sstevel@tonic-gate 	char *addr;
66027c478bd9Sstevel@tonic-gate 	char *minor_name;
66037c478bd9Sstevel@tonic-gate 	char *node_type;
66047c478bd9Sstevel@tonic-gate 
66057c478bd9Sstevel@tonic-gate 	for (; selector != NULL; selector = selector->next) {
66067c478bd9Sstevel@tonic-gate 		switch (selector->key) {
66077c478bd9Sstevel@tonic-gate 		case NAME:
66087c478bd9Sstevel@tonic-gate 			if (strcmp(di_node_name(node), selector->val) != 0) {
66097c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
66107c478bd9Sstevel@tonic-gate 			}
66117c478bd9Sstevel@tonic-gate 			break;
66127c478bd9Sstevel@tonic-gate 		case TYPE:
66137c478bd9Sstevel@tonic-gate 			node_type = di_minor_nodetype(minor);
66147c478bd9Sstevel@tonic-gate 			assert(node_type != NULL);
66157c478bd9Sstevel@tonic-gate 			if (strcmp(node_type, selector->val) != 0) {
66167c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
66177c478bd9Sstevel@tonic-gate 			}
66187c478bd9Sstevel@tonic-gate 			break;
66197c478bd9Sstevel@tonic-gate 		case ADDR:
66207c478bd9Sstevel@tonic-gate 			if ((addr = di_bus_addr(node)) == NULL) {
66217c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
66227c478bd9Sstevel@tonic-gate 			}
66237c478bd9Sstevel@tonic-gate 			if (selector->arg == 0) {
66247c478bd9Sstevel@tonic-gate 				if (strcmp(addr, selector->val) != 0) {
66257c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
66267c478bd9Sstevel@tonic-gate 				}
66277c478bd9Sstevel@tonic-gate 			} else {
66287c478bd9Sstevel@tonic-gate 				if (compare_field(addr, selector->val,
66297c478bd9Sstevel@tonic-gate 				    selector->arg) == DEVFSADM_FAILURE) {
66307c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
66317c478bd9Sstevel@tonic-gate 				}
66327c478bd9Sstevel@tonic-gate 			}
66337c478bd9Sstevel@tonic-gate 			break;
66347c478bd9Sstevel@tonic-gate 		case MINOR:
66357c478bd9Sstevel@tonic-gate 			if ((minor_name = di_minor_name(minor)) == NULL) {
66367c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
66377c478bd9Sstevel@tonic-gate 			}
66387c478bd9Sstevel@tonic-gate 			if (selector->arg == 0) {
66397c478bd9Sstevel@tonic-gate 				if (strcmp(minor_name, selector->val) != 0) {
66407c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
66417c478bd9Sstevel@tonic-gate 				}
66427c478bd9Sstevel@tonic-gate 			} else {
66437c478bd9Sstevel@tonic-gate 				if (compare_field(minor_name, selector->val,
66440a653502Swroche 				    selector->arg) == DEVFSADM_FAILURE) {
66457c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
66467c478bd9Sstevel@tonic-gate 				}
66477c478bd9Sstevel@tonic-gate 			}
66487c478bd9Sstevel@tonic-gate 			break;
66497c478bd9Sstevel@tonic-gate 		default:
66507c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
66517c478bd9Sstevel@tonic-gate 		}
66527c478bd9Sstevel@tonic-gate 	}
66537c478bd9Sstevel@tonic-gate 
66547c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
66557c478bd9Sstevel@tonic-gate }
66567c478bd9Sstevel@tonic-gate 
66577c478bd9Sstevel@tonic-gate /*
66587c478bd9Sstevel@tonic-gate  * For the given minor node and devlinktab_list entry from devlink.tab,
66597c478bd9Sstevel@tonic-gate  * build a logical dev link and a possible extra devlink.
66607c478bd9Sstevel@tonic-gate  * Return DEVFSADM_SUCCESS if link is created, otherwise DEVFSADM_FAILURE.
66617c478bd9Sstevel@tonic-gate  */
66627c478bd9Sstevel@tonic-gate static int
66637c478bd9Sstevel@tonic-gate build_links(devlinktab_list_t *entry, di_minor_t minor, di_node_t node)
66647c478bd9Sstevel@tonic-gate {
66657c478bd9Sstevel@tonic-gate 	char secondary_link[PATH_MAX + 1];
66667c478bd9Sstevel@tonic-gate 	char primary_link[PATH_MAX + 1];
66677c478bd9Sstevel@tonic-gate 	char contents[PATH_MAX + 1];
66687c478bd9Sstevel@tonic-gate 	char *dev_path;
66697c478bd9Sstevel@tonic-gate 
66707c478bd9Sstevel@tonic-gate 	if ((dev_path = di_devfs_path(node)) == NULL) {
66717c478bd9Sstevel@tonic-gate 		err_print(DI_DEVFS_PATH_FAILED, strerror(errno));
66727c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
6673537714daSvikram 		/*NOTREACHED*/
66747c478bd9Sstevel@tonic-gate 	}
66757c478bd9Sstevel@tonic-gate 	(void) strcpy(contents, dev_path);
66767c478bd9Sstevel@tonic-gate 	di_devfs_path_free(dev_path);
66777c478bd9Sstevel@tonic-gate 
66787c478bd9Sstevel@tonic-gate 	(void) strcat(contents, ":");
66797c478bd9Sstevel@tonic-gate 	(void) strcat(contents, di_minor_name(minor));
66807c478bd9Sstevel@tonic-gate 
66817c478bd9Sstevel@tonic-gate 	if (construct_devlink(primary_link, entry->p_link, contents,
66820a653502Swroche 	    minor, node, entry->p_link_pattern) == DEVFSADM_FAILURE) {
66837c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
66847c478bd9Sstevel@tonic-gate 	}
66857c478bd9Sstevel@tonic-gate 	(void) devfsadm_mklink(primary_link, node, minor, 0);
66867c478bd9Sstevel@tonic-gate 
66877c478bd9Sstevel@tonic-gate 	if (entry->s_link == NULL) {
66887c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
66897c478bd9Sstevel@tonic-gate 	}
66907c478bd9Sstevel@tonic-gate 
66910a653502Swroche 	if (construct_devlink(secondary_link, entry->s_link, primary_link,
66920a653502Swroche 	    minor, node, entry->s_link_pattern) == DEVFSADM_FAILURE) {
66937c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
66947c478bd9Sstevel@tonic-gate 	}
66957c478bd9Sstevel@tonic-gate 
66967c478bd9Sstevel@tonic-gate 	(void) devfsadm_secondary_link(secondary_link, primary_link, 0);
66977c478bd9Sstevel@tonic-gate 
66987c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
66997c478bd9Sstevel@tonic-gate }
67007c478bd9Sstevel@tonic-gate 
67017c478bd9Sstevel@tonic-gate /*
67027c478bd9Sstevel@tonic-gate  * The counter rule for devlink.tab entries is implemented via
67037c478bd9Sstevel@tonic-gate  * devfsadm_enumerate_int_start(). One of the arguments to this function
67047c478bd9Sstevel@tonic-gate  * is a path, where each path component is treated as a regular expression.
67057c478bd9Sstevel@tonic-gate  * For devlink.tab entries, this path regular expression is derived from
67067c478bd9Sstevel@tonic-gate  * the devlink spec. get_anchored_re() accepts path regular expressions derived
67077c478bd9Sstevel@tonic-gate  * from devlink.tab entries and inserts the anchors '^' and '$' at the beginning
67087c478bd9Sstevel@tonic-gate  * and end respectively of each path component. This is done to prevent
67097c478bd9Sstevel@tonic-gate  * false matches. For example, without anchors, "a/([0-9]+)" will match "ab/c9"
67107c478bd9Sstevel@tonic-gate  * and incorrect links will be generated.
67117c478bd9Sstevel@tonic-gate  */
67127c478bd9Sstevel@tonic-gate static int
67137c478bd9Sstevel@tonic-gate get_anchored_re(char *link, char *anchored_re, char *pattern)
67147c478bd9Sstevel@tonic-gate {
67157c478bd9Sstevel@tonic-gate 	if (*link == '/' || *link == '\0') {
67167c478bd9Sstevel@tonic-gate 		err_print(INVALID_DEVLINK_SPEC, pattern);
67177c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
67187c478bd9Sstevel@tonic-gate 	}
67197c478bd9Sstevel@tonic-gate 
67207c478bd9Sstevel@tonic-gate 	*anchored_re++ = '^';
67217c478bd9Sstevel@tonic-gate 	for (; *link != '\0'; ) {
67227c478bd9Sstevel@tonic-gate 		if (*link == '/') {
67237c478bd9Sstevel@tonic-gate 			while (*link == '/')
67247c478bd9Sstevel@tonic-gate 				link++;
67257c478bd9Sstevel@tonic-gate 			*anchored_re++ = '$';
67267c478bd9Sstevel@tonic-gate 			*anchored_re++ = '/';
67277c478bd9Sstevel@tonic-gate 			if (*link != '\0') {
67287c478bd9Sstevel@tonic-gate 				*anchored_re++ = '^';
67297c478bd9Sstevel@tonic-gate 			}
67307c478bd9Sstevel@tonic-gate 		} else {
67317c478bd9Sstevel@tonic-gate 			*anchored_re++ = *link++;
67327c478bd9Sstevel@tonic-gate 			if (*link == '\0') {
67337c478bd9Sstevel@tonic-gate 				*anchored_re++ = '$';
67347c478bd9Sstevel@tonic-gate 			}
67357c478bd9Sstevel@tonic-gate 		}
67367c478bd9Sstevel@tonic-gate 	}
67377c478bd9Sstevel@tonic-gate 	*anchored_re = '\0';
67387c478bd9Sstevel@tonic-gate 
67397c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
67407c478bd9Sstevel@tonic-gate }
67417c478bd9Sstevel@tonic-gate 
67427c478bd9Sstevel@tonic-gate static int
67437c478bd9Sstevel@tonic-gate construct_devlink(char *link, link_list_t *link_build, char *contents,
6744406fc510SToomas Soome     di_minor_t minor, di_node_t node, char *pattern)
67457c478bd9Sstevel@tonic-gate {
67467c478bd9Sstevel@tonic-gate 	int counter_offset = -1;
67477c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[1] = {NULL};
67487c478bd9Sstevel@tonic-gate 	char templink[PATH_MAX + 1];
67497c478bd9Sstevel@tonic-gate 	char *buff;
67507c478bd9Sstevel@tonic-gate 	char start[10];
67517c478bd9Sstevel@tonic-gate 	char *node_path;
67527c478bd9Sstevel@tonic-gate 	char anchored_re[PATH_MAX + 1];
67537c478bd9Sstevel@tonic-gate 
67547c478bd9Sstevel@tonic-gate 	link[0] = '\0';
67557c478bd9Sstevel@tonic-gate 
67567c478bd9Sstevel@tonic-gate 	for (; link_build != NULL; link_build = link_build->next) {
67577c478bd9Sstevel@tonic-gate 		switch (link_build->type) {
67587c478bd9Sstevel@tonic-gate 		case NAME:
67597c478bd9Sstevel@tonic-gate 			(void) strcat(link, di_node_name(node));
67607c478bd9Sstevel@tonic-gate 			break;
67617c478bd9Sstevel@tonic-gate 		case CONSTANT:
67627c478bd9Sstevel@tonic-gate 			(void) strcat(link, link_build->constant);
67637c478bd9Sstevel@tonic-gate 			break;
67647c478bd9Sstevel@tonic-gate 		case ADDR:
67657c478bd9Sstevel@tonic-gate 			if (component_cat(link, di_bus_addr(node),
67660a653502Swroche 			    link_build->arg) == DEVFSADM_FAILURE) {
67677c478bd9Sstevel@tonic-gate 				node_path = di_devfs_path(node);
67687c478bd9Sstevel@tonic-gate 				err_print(CANNOT_BE_USED, pattern, node_path,
67690a653502Swroche 				    di_minor_name(minor));
67707c478bd9Sstevel@tonic-gate 				di_devfs_path_free(node_path);
67717c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
67727c478bd9Sstevel@tonic-gate 			}
67737c478bd9Sstevel@tonic-gate 			break;
67747c478bd9Sstevel@tonic-gate 		case MINOR:
67757c478bd9Sstevel@tonic-gate 			if (component_cat(link, di_minor_name(minor),
67760a653502Swroche 			    link_build->arg) == DEVFSADM_FAILURE) {
67777c478bd9Sstevel@tonic-gate 				node_path = di_devfs_path(node);
67787c478bd9Sstevel@tonic-gate 				err_print(CANNOT_BE_USED, pattern, node_path,
67790a653502Swroche 				    di_minor_name(minor));
67807c478bd9Sstevel@tonic-gate 				di_devfs_path_free(node_path);
67817c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
67827c478bd9Sstevel@tonic-gate 			}
67837c478bd9Sstevel@tonic-gate 			break;
67847c478bd9Sstevel@tonic-gate 		case COUNTER:
67857c478bd9Sstevel@tonic-gate 			counter_offset = strlen(link);
67867c478bd9Sstevel@tonic-gate 			(void) strcat(link, "([0-9]+)");
67877c478bd9Sstevel@tonic-gate 			(void) sprintf(start, "%d", link_build->arg);
67887c478bd9Sstevel@tonic-gate 			break;
67897c478bd9Sstevel@tonic-gate 		default:
67907c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
67917c478bd9Sstevel@tonic-gate 		}
67927c478bd9Sstevel@tonic-gate 	}
67937c478bd9Sstevel@tonic-gate 
67947c478bd9Sstevel@tonic-gate 	if (counter_offset != -1) {
67957c478bd9Sstevel@tonic-gate 		/*
67967c478bd9Sstevel@tonic-gate 		 * copy anything appended after "([0-9]+)" into
67977c478bd9Sstevel@tonic-gate 		 * templink
67987c478bd9Sstevel@tonic-gate 		 */
67997c478bd9Sstevel@tonic-gate 
68007c478bd9Sstevel@tonic-gate 		(void) strcpy(templink,
68010a653502Swroche 		    &link[counter_offset + strlen("([0-9]+)")]);
68027c478bd9Sstevel@tonic-gate 		if (get_anchored_re(link, anchored_re, pattern)
68037c478bd9Sstevel@tonic-gate 		    != DEVFSADM_SUCCESS) {
68047c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
68057c478bd9Sstevel@tonic-gate 		}
68067c478bd9Sstevel@tonic-gate 		rules[0].re = anchored_re;
68077c478bd9Sstevel@tonic-gate 		rules[0].subexp = 1;
68087c478bd9Sstevel@tonic-gate 		rules[0].flags = MATCH_ALL;
68097c478bd9Sstevel@tonic-gate 		if (devfsadm_enumerate_int_start(contents, 0, &buff,
68107c478bd9Sstevel@tonic-gate 		    rules, 1, start) == DEVFSADM_FAILURE) {
68117c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
68127c478bd9Sstevel@tonic-gate 		}
68137c478bd9Sstevel@tonic-gate 		(void) strcpy(&link[counter_offset], buff);
68147c478bd9Sstevel@tonic-gate 		free(buff);
68157c478bd9Sstevel@tonic-gate 		(void) strcat(link, templink);
68167c478bd9Sstevel@tonic-gate 		vprint(DEVLINK_MID, "COUNTER is	%s\n", link);
68177c478bd9Sstevel@tonic-gate 	}
68187c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
68197c478bd9Sstevel@tonic-gate }
68207c478bd9Sstevel@tonic-gate 
68217c478bd9Sstevel@tonic-gate /*
68227c478bd9Sstevel@tonic-gate  * Compares "field" number of the comma separated list "full_name" with
68237c478bd9Sstevel@tonic-gate  * field_item.	Returns DEVFSADM_SUCCESS for match,
68247c478bd9Sstevel@tonic-gate  * DEVFSADM_FAILURE for no match.
68257c478bd9Sstevel@tonic-gate  */
68267c478bd9Sstevel@tonic-gate static int
68277c478bd9Sstevel@tonic-gate compare_field(char *full_name, char *field_item, int field)
68287c478bd9Sstevel@tonic-gate {
68297c478bd9Sstevel@tonic-gate 	--field;
68307c478bd9Sstevel@tonic-gate 	while ((*full_name != '\0') && (field != 0)) {
68317c478bd9Sstevel@tonic-gate 		if (*(full_name++) == ',') {
68327c478bd9Sstevel@tonic-gate 			field--;
68337c478bd9Sstevel@tonic-gate 		}
68347c478bd9Sstevel@tonic-gate 	}
68357c478bd9Sstevel@tonic-gate 
68367c478bd9Sstevel@tonic-gate 	if (field != 0) {
68377c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
68387c478bd9Sstevel@tonic-gate 	}
68397c478bd9Sstevel@tonic-gate 
68407c478bd9Sstevel@tonic-gate 	while ((*full_name != '\0') && (*field_item != '\0') &&
68410a653502Swroche 	    (*full_name != ',')) {
68427c478bd9Sstevel@tonic-gate 		if (*(full_name++) != *(field_item++)) {
68437c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
68447c478bd9Sstevel@tonic-gate 		}
68457c478bd9Sstevel@tonic-gate 	}
68467c478bd9Sstevel@tonic-gate 
68477c478bd9Sstevel@tonic-gate 	if (*field_item != '\0') {
68487c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
68497c478bd9Sstevel@tonic-gate 	}
68507c478bd9Sstevel@tonic-gate 
68517c478bd9Sstevel@tonic-gate 	if ((*full_name == '\0') || (*full_name == ','))
68527c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
68537c478bd9Sstevel@tonic-gate 
68547c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
68557c478bd9Sstevel@tonic-gate }
68567c478bd9Sstevel@tonic-gate 
68577c478bd9Sstevel@tonic-gate /*
68587c478bd9Sstevel@tonic-gate  * strcat() field # "field" of comma separated list "name" to "link".
68597c478bd9Sstevel@tonic-gate  * Field 0 is the entire name.
68607c478bd9Sstevel@tonic-gate  * Return DEVFSADM_SUCCESS or DEVFSADM_FAILURE.
68617c478bd9Sstevel@tonic-gate  */
68627c478bd9Sstevel@tonic-gate static int
68637c478bd9Sstevel@tonic-gate component_cat(char *link, char *name, int field)
68647c478bd9Sstevel@tonic-gate {
68657c478bd9Sstevel@tonic-gate 
68667c478bd9Sstevel@tonic-gate 	if (name == NULL) {
68677c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
68687c478bd9Sstevel@tonic-gate 	}
68697c478bd9Sstevel@tonic-gate 
68707c478bd9Sstevel@tonic-gate 	if (field == 0) {
68717c478bd9Sstevel@tonic-gate 		(void) strcat(link, name);
68727c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
68737c478bd9Sstevel@tonic-gate 	}
68747c478bd9Sstevel@tonic-gate 
68757c478bd9Sstevel@tonic-gate 	while (*link != '\0') {
68767c478bd9Sstevel@tonic-gate 		link++;
68777c478bd9Sstevel@tonic-gate 	}
68787c478bd9Sstevel@tonic-gate 
68797c478bd9Sstevel@tonic-gate 	--field;
68807c478bd9Sstevel@tonic-gate 	while ((*name != '\0') && (field != 0)) {
68817c478bd9Sstevel@tonic-gate 		if (*(name++) == ',') {
68827c478bd9Sstevel@tonic-gate 			--field;
68837c478bd9Sstevel@tonic-gate 		}
68847c478bd9Sstevel@tonic-gate 	}
68857c478bd9Sstevel@tonic-gate 
68867c478bd9Sstevel@tonic-gate 	if (field != 0) {
68877c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
68887c478bd9Sstevel@tonic-gate 	}
68897c478bd9Sstevel@tonic-gate 
68907c478bd9Sstevel@tonic-gate 	while ((*name != '\0') && (*name != ',')) {
68917c478bd9Sstevel@tonic-gate 		*(link++) = *(name++);
68927c478bd9Sstevel@tonic-gate 	}
68937c478bd9Sstevel@tonic-gate 
68947c478bd9Sstevel@tonic-gate 	*link = '\0';
68957c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
68967c478bd9Sstevel@tonic-gate }
68977c478bd9Sstevel@tonic-gate 
68987c478bd9Sstevel@tonic-gate static void
68997c478bd9Sstevel@tonic-gate free_selector_list(selector_list_t *head)
69007c478bd9Sstevel@tonic-gate {
69017c478bd9Sstevel@tonic-gate 	selector_list_t *temp;
69027c478bd9Sstevel@tonic-gate 
69037c478bd9Sstevel@tonic-gate 	while (head != NULL) {
69047c478bd9Sstevel@tonic-gate 		temp = head;
69057c478bd9Sstevel@tonic-gate 		head = head->next;
69067c478bd9Sstevel@tonic-gate 		free(temp->val);
69077c478bd9Sstevel@tonic-gate 		free(temp);
69087c478bd9Sstevel@tonic-gate 	}
69097c478bd9Sstevel@tonic-gate }
69107c478bd9Sstevel@tonic-gate 
69117c478bd9Sstevel@tonic-gate static void
69127c478bd9Sstevel@tonic-gate free_link_list(link_list_t *head)
69137c478bd9Sstevel@tonic-gate {
69147c478bd9Sstevel@tonic-gate 	link_list_t *temp;
69157c478bd9Sstevel@tonic-gate 
69167c478bd9Sstevel@tonic-gate 	while (head != NULL) {
69177c478bd9Sstevel@tonic-gate 		temp = head;
69187c478bd9Sstevel@tonic-gate 		head = head->next;
69197c478bd9Sstevel@tonic-gate 		if (temp->type == CONSTANT) {
69207c478bd9Sstevel@tonic-gate 			free(temp->constant);
69217c478bd9Sstevel@tonic-gate 		}
69227c478bd9Sstevel@tonic-gate 		free(temp);
69237c478bd9Sstevel@tonic-gate 	}
69247c478bd9Sstevel@tonic-gate }
69257c478bd9Sstevel@tonic-gate 
69267c478bd9Sstevel@tonic-gate /*
69277c478bd9Sstevel@tonic-gate  * Prints only if level matches one of the debug levels
69287c478bd9Sstevel@tonic-gate  * given on command line.  INFO_MID is always printed.
69297c478bd9Sstevel@tonic-gate  *
69307c478bd9Sstevel@tonic-gate  * See devfsadm.h for a listing of globally defined levels and
69317c478bd9Sstevel@tonic-gate  * meanings.  Modules should prefix the level with their
69327c478bd9Sstevel@tonic-gate  * module name to prevent collisions.
69337c478bd9Sstevel@tonic-gate  */
69347c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
69357c478bd9Sstevel@tonic-gate void
69367c478bd9Sstevel@tonic-gate devfsadm_print(char *msgid, char *message, ...)
69377c478bd9Sstevel@tonic-gate {
69387c478bd9Sstevel@tonic-gate 	va_list ap;
69397c478bd9Sstevel@tonic-gate 	static int newline = TRUE;
69407c478bd9Sstevel@tonic-gate 	int x;
69417c478bd9Sstevel@tonic-gate 
69427c478bd9Sstevel@tonic-gate 	if (msgid != NULL) {
69437c478bd9Sstevel@tonic-gate 		for (x = 0; x < num_verbose; x++) {
69447c478bd9Sstevel@tonic-gate 			if (strcmp(verbose[x], msgid) == 0) {
69457c478bd9Sstevel@tonic-gate 				break;
69467c478bd9Sstevel@tonic-gate 			}
69477c478bd9Sstevel@tonic-gate 			if (strcmp(verbose[x], ALL_MID) == 0) {
69487c478bd9Sstevel@tonic-gate 				break;
69497c478bd9Sstevel@tonic-gate 			}
69507c478bd9Sstevel@tonic-gate 		}
69517c478bd9Sstevel@tonic-gate 		if (x == num_verbose) {
69527c478bd9Sstevel@tonic-gate 			return;
69537c478bd9Sstevel@tonic-gate 		}
69547c478bd9Sstevel@tonic-gate 	}
69557c478bd9Sstevel@tonic-gate 
69567c478bd9Sstevel@tonic-gate 	va_start(ap, message);
69577c478bd9Sstevel@tonic-gate 
69587c478bd9Sstevel@tonic-gate 	if (msgid == NULL) {
69597c478bd9Sstevel@tonic-gate 		if (logflag == TRUE) {
69607c478bd9Sstevel@tonic-gate 			(void) vsyslog(LOG_NOTICE, message, ap);
69617c478bd9Sstevel@tonic-gate 		} else {
69627c478bd9Sstevel@tonic-gate 			(void) vfprintf(stdout, message, ap);
69637c478bd9Sstevel@tonic-gate 		}
69647c478bd9Sstevel@tonic-gate 
69657c478bd9Sstevel@tonic-gate 	} else {
69667c478bd9Sstevel@tonic-gate 		if (logflag == TRUE) {
69677c478bd9Sstevel@tonic-gate 			(void) syslog(LOG_DEBUG, "%s[%ld]: %s: ",
69680a653502Swroche 			    prog, getpid(), msgid);
69697c478bd9Sstevel@tonic-gate 			(void) vsyslog(LOG_DEBUG, message, ap);
69707c478bd9Sstevel@tonic-gate 		} else {
69717c478bd9Sstevel@tonic-gate 			if (newline == TRUE) {
69727c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "%s[%ld]: %s: ",
69730a653502Swroche 				    prog, getpid(), msgid);
69747c478bd9Sstevel@tonic-gate 			}
69757c478bd9Sstevel@tonic-gate 			(void) vfprintf(stdout, message, ap);
69767c478bd9Sstevel@tonic-gate 		}
69777c478bd9Sstevel@tonic-gate 	}
69787c478bd9Sstevel@tonic-gate 
69797c478bd9Sstevel@tonic-gate 	if (message[strlen(message) - 1] == '\n') {
69807c478bd9Sstevel@tonic-gate 		newline = TRUE;
69817c478bd9Sstevel@tonic-gate 	} else {
69827c478bd9Sstevel@tonic-gate 		newline = FALSE;
69837c478bd9Sstevel@tonic-gate 	}
69847c478bd9Sstevel@tonic-gate 	va_end(ap);
69857c478bd9Sstevel@tonic-gate }
69867c478bd9Sstevel@tonic-gate 
69877c478bd9Sstevel@tonic-gate /*
69887c478bd9Sstevel@tonic-gate  * print error messages to the terminal or to syslog
69897c478bd9Sstevel@tonic-gate  */
69907c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
69917c478bd9Sstevel@tonic-gate void
69927c478bd9Sstevel@tonic-gate devfsadm_errprint(char *message, ...)
69937c478bd9Sstevel@tonic-gate {
69947c478bd9Sstevel@tonic-gate 	va_list ap;
69957c478bd9Sstevel@tonic-gate 
69967c478bd9Sstevel@tonic-gate 	va_start(ap, message);
69977c478bd9Sstevel@tonic-gate 
69987c478bd9Sstevel@tonic-gate 	if (logflag == TRUE) {
69997c478bd9Sstevel@tonic-gate 		(void) vsyslog(LOG_ERR, message, ap);
70007c478bd9Sstevel@tonic-gate 	} else {
70017c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: ", prog);
70027c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, message, ap);
70037c478bd9Sstevel@tonic-gate 	}
70047c478bd9Sstevel@tonic-gate 	va_end(ap);
70057c478bd9Sstevel@tonic-gate }
70067c478bd9Sstevel@tonic-gate 
70077c478bd9Sstevel@tonic-gate /*
70087c478bd9Sstevel@tonic-gate  * return noupdate state (-s)
70097c478bd9Sstevel@tonic-gate  */
70107c478bd9Sstevel@tonic-gate int
70117c478bd9Sstevel@tonic-gate devfsadm_noupdate(void)
70127c478bd9Sstevel@tonic-gate {
70137c478bd9Sstevel@tonic-gate 	return (file_mods == TRUE ? DEVFSADM_TRUE : DEVFSADM_FALSE);
70147c478bd9Sstevel@tonic-gate }
70157c478bd9Sstevel@tonic-gate 
70167c478bd9Sstevel@tonic-gate /*
70177c478bd9Sstevel@tonic-gate  * return current root update path (-r)
70187c478bd9Sstevel@tonic-gate  */
70197c478bd9Sstevel@tonic-gate const char *
70207c478bd9Sstevel@tonic-gate devfsadm_root_path(void)
70217c478bd9Sstevel@tonic-gate {
70227c478bd9Sstevel@tonic-gate 	if (root_dir[0] == '\0') {
70237c478bd9Sstevel@tonic-gate 		return ("/");
70247c478bd9Sstevel@tonic-gate 	} else {
70257c478bd9Sstevel@tonic-gate 		return ((const char *)root_dir);
70267c478bd9Sstevel@tonic-gate 	}
70277c478bd9Sstevel@tonic-gate }
70287c478bd9Sstevel@tonic-gate 
70293c4226f9Spjha void
70303c4226f9Spjha devfsadm_free_dev_names(char **dev_names, int len)
70313c4226f9Spjha {
70323c4226f9Spjha 	int i;
70333c4226f9Spjha 
70343c4226f9Spjha 	for (i = 0; i < len; i++)
70353c4226f9Spjha 		free(dev_names[i]);
70363c4226f9Spjha 	free(dev_names);
70373c4226f9Spjha }
70383c4226f9Spjha 
70393c4226f9Spjha /*
70403c4226f9Spjha  * Return all devlinks corresponding to phys_path as an array of strings.
70413c4226f9Spjha  * The number of entries in the array is returned through lenp.
70423c4226f9Spjha  * devfsadm_free_dev_names() is used to free the returned array.
70433c4226f9Spjha  * NULL is returned on failure or when there are no matching devlinks.
70443c4226f9Spjha  *
70453c4226f9Spjha  * re is an extended regular expression in regex(5) format used to further
70463c4226f9Spjha  * match devlinks pointing to phys_path; it may be NULL to match all
70473c4226f9Spjha  */
70483c4226f9Spjha char **
70493c4226f9Spjha devfsadm_lookup_dev_names(char *phys_path, char *re, int *lenp)
70503c4226f9Spjha {
70513c4226f9Spjha 	struct devlink_cb_arg cb_arg;
70523c4226f9Spjha 	char **dev_names = NULL;
70533c4226f9Spjha 	int i;
70543c4226f9Spjha 
70553c4226f9Spjha 	*lenp = 0;
70563c4226f9Spjha 	cb_arg.count = 0;
70573c4226f9Spjha 	cb_arg.rv = 0;
70583c4226f9Spjha 	(void) di_devlink_cache_walk(devlink_cache, re, phys_path,
70593c4226f9Spjha 	    DI_PRIMARY_LINK, &cb_arg, devlink_cb);
70603c4226f9Spjha 
70613c4226f9Spjha 	if (cb_arg.rv == -1 || cb_arg.count <= 0)
70623c4226f9Spjha 		return (NULL);
70633c4226f9Spjha 
70643c4226f9Spjha 	dev_names = s_malloc(cb_arg.count * sizeof (char *));
70653c4226f9Spjha 	if (dev_names == NULL)
70663c4226f9Spjha 		goto out;
70673c4226f9Spjha 
70683c4226f9Spjha 	for (i = 0; i < cb_arg.count; i++) {
70693c4226f9Spjha 		dev_names[i] = s_strdup(cb_arg.dev_names[i]);
70703c4226f9Spjha 		if (dev_names[i] == NULL) {
70713c4226f9Spjha 			devfsadm_free_dev_names(dev_names, i);
70723c4226f9Spjha 			dev_names = NULL;
70733c4226f9Spjha 			goto out;
70743c4226f9Spjha 		}
70753c4226f9Spjha 	}
70763c4226f9Spjha 	*lenp = cb_arg.count;
70773c4226f9Spjha 
70783c4226f9Spjha out:
70793c4226f9Spjha 	free_dev_names(&cb_arg);
70803c4226f9Spjha 	return (dev_names);
70813c4226f9Spjha }
70823c4226f9Spjha 
70837c478bd9Sstevel@tonic-gate /* common exit function which ensures releasing locks */
70847c478bd9Sstevel@tonic-gate static void
70857c478bd9Sstevel@tonic-gate devfsadm_exit(int status)
70867c478bd9Sstevel@tonic-gate {
70877c478bd9Sstevel@tonic-gate 	if (DEVFSADM_DEBUG_ON) {
70887c478bd9Sstevel@tonic-gate 		vprint(INFO_MID, "exit status = %d\n", status);
70897c478bd9Sstevel@tonic-gate 	}
70907c478bd9Sstevel@tonic-gate 
7091537714daSvikram 	exit_dev_lock(1);
7092537714daSvikram 	exit_daemon_lock(1);
70937c478bd9Sstevel@tonic-gate 
70947c478bd9Sstevel@tonic-gate 	if (logflag == TRUE) {
70957c478bd9Sstevel@tonic-gate 		closelog();
70967c478bd9Sstevel@tonic-gate 	}
70977c478bd9Sstevel@tonic-gate 
70987c478bd9Sstevel@tonic-gate 	exit(status);
7099537714daSvikram 	/*NOTREACHED*/
71007c478bd9Sstevel@tonic-gate }
71017c478bd9Sstevel@tonic-gate 
71027c478bd9Sstevel@tonic-gate /*
7103facf4a8dSllai  * set root_dir, devices_dir, dev_dir using optarg.
71047c478bd9Sstevel@tonic-gate  */
71057c478bd9Sstevel@tonic-gate static void
7106facf4a8dSllai set_root_devices_dev_dir(char *dir)
71077c478bd9Sstevel@tonic-gate {
71087c478bd9Sstevel@tonic-gate 	size_t len;
71097c478bd9Sstevel@tonic-gate 
71107c478bd9Sstevel@tonic-gate 	root_dir = s_strdup(dir);
71117c478bd9Sstevel@tonic-gate 	len = strlen(dir) + strlen(DEVICES) + 1;
71127c478bd9Sstevel@tonic-gate 	devices_dir = s_malloc(len);
71137c478bd9Sstevel@tonic-gate 	(void) snprintf(devices_dir, len, "%s%s", root_dir, DEVICES);
71147c478bd9Sstevel@tonic-gate 	len = strlen(root_dir) + strlen(DEV) + 1;
71157c478bd9Sstevel@tonic-gate 	dev_dir = s_malloc(len);
71167c478bd9Sstevel@tonic-gate 	(void) snprintf(dev_dir, len, "%s%s", root_dir, DEV);
71177c478bd9Sstevel@tonic-gate }
71187c478bd9Sstevel@tonic-gate 
71197c478bd9Sstevel@tonic-gate /*
71207c478bd9Sstevel@tonic-gate  * Removes quotes.
71217c478bd9Sstevel@tonic-gate  */
71227c478bd9Sstevel@tonic-gate static char *
71237c478bd9Sstevel@tonic-gate dequote(char *src)
71247c478bd9Sstevel@tonic-gate {
71257c478bd9Sstevel@tonic-gate 	char	*dst;
71267c478bd9Sstevel@tonic-gate 	int	len;
71277c478bd9Sstevel@tonic-gate 
71287c478bd9Sstevel@tonic-gate 	len = strlen(src);
71297c478bd9Sstevel@tonic-gate 	dst = s_malloc(len + 1);
71307c478bd9Sstevel@tonic-gate 	if (src[0] == '\"' && src[len - 1] == '\"') {
71317c478bd9Sstevel@tonic-gate 		len -= 2;
71327c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, &src[1], len);
71337c478bd9Sstevel@tonic-gate 		dst[len] = '\0';
71347c478bd9Sstevel@tonic-gate 	} else {
71357c478bd9Sstevel@tonic-gate 		(void) strcpy(dst, src);
71367c478bd9Sstevel@tonic-gate 	}
71377c478bd9Sstevel@tonic-gate 	return (dst);
71387c478bd9Sstevel@tonic-gate }
71397c478bd9Sstevel@tonic-gate 
71407c478bd9Sstevel@tonic-gate /*
71417c478bd9Sstevel@tonic-gate  * For a given physical device pathname and spectype, return the
71427c478bd9Sstevel@tonic-gate  * ownership and permissions attributes by looking in data from
71437c478bd9Sstevel@tonic-gate  * /etc/minor_perm.  If currently in installation mode, check for
71447c478bd9Sstevel@tonic-gate  * possible major number translations from the miniroot to the installed
71457c478bd9Sstevel@tonic-gate  * root's name_to_major table. Note that there can be multiple matches,
71467c478bd9Sstevel@tonic-gate  * but the last match takes effect.  pts seems to rely on this
71477c478bd9Sstevel@tonic-gate  * implementation behavior.
71487c478bd9Sstevel@tonic-gate  */
71497c478bd9Sstevel@tonic-gate static void
71507c478bd9Sstevel@tonic-gate getattr(char *phy_path, char *aminor, int spectype, dev_t dev, mode_t *mode,
7151406fc510SToomas Soome     uid_t *uid, gid_t *gid)
71527c478bd9Sstevel@tonic-gate {
71537c478bd9Sstevel@tonic-gate 	char devname[PATH_MAX + 1];
71547c478bd9Sstevel@tonic-gate 	char *node_name;
71557c478bd9Sstevel@tonic-gate 	char *minor_name;
71567c478bd9Sstevel@tonic-gate 	int match = FALSE;
71577c478bd9Sstevel@tonic-gate 	int is_clone;
71587c478bd9Sstevel@tonic-gate 	int mp_drvname_matches_node_name;
71597c478bd9Sstevel@tonic-gate 	int mp_drvname_matches_minor_name;
71607c478bd9Sstevel@tonic-gate 	int mp_drvname_is_clone;
71617c478bd9Sstevel@tonic-gate 	int mp_drvname_matches_drvname;
71627c478bd9Sstevel@tonic-gate 	struct mperm *mp;
71637c478bd9Sstevel@tonic-gate 	major_t major_no;
71647c478bd9Sstevel@tonic-gate 	char driver[PATH_MAX + 1];
71657c478bd9Sstevel@tonic-gate 
71667c478bd9Sstevel@tonic-gate 	/*
71677c478bd9Sstevel@tonic-gate 	 * Get the driver name based on the major number since the name
71687c478bd9Sstevel@tonic-gate 	 * in /devices may be generic.  Could be running with more major
71697c478bd9Sstevel@tonic-gate 	 * numbers than are in /etc/name_to_major, so get it from the kernel
71707c478bd9Sstevel@tonic-gate 	 */
71717c478bd9Sstevel@tonic-gate 	major_no = major(dev);
71727c478bd9Sstevel@tonic-gate 
71737c478bd9Sstevel@tonic-gate 	if (modctl(MODGETNAME, driver, sizeof (driver), &major_no) != 0) {
71747c478bd9Sstevel@tonic-gate 		/* return default values */
71757c478bd9Sstevel@tonic-gate 		goto use_defaults;
71767c478bd9Sstevel@tonic-gate 	}
71777c478bd9Sstevel@tonic-gate 
71787c478bd9Sstevel@tonic-gate 	(void) strcpy(devname, phy_path);
71797c478bd9Sstevel@tonic-gate 
71807c478bd9Sstevel@tonic-gate 	node_name = strrchr(devname, '/'); /* node name is the last */
71817c478bd9Sstevel@tonic-gate 					/* component */
71827c478bd9Sstevel@tonic-gate 	if (node_name == NULL) {
71837c478bd9Sstevel@tonic-gate 		err_print(NO_NODE, devname);
71847c478bd9Sstevel@tonic-gate 		goto use_defaults;
71857c478bd9Sstevel@tonic-gate 	}
71867c478bd9Sstevel@tonic-gate 
71877c478bd9Sstevel@tonic-gate 	minor_name = strchr(++node_name, '@'); /* see if it has address part */
71887c478bd9Sstevel@tonic-gate 
71897c478bd9Sstevel@tonic-gate 	if (minor_name != NULL) {
71907c478bd9Sstevel@tonic-gate 		*minor_name++ = '\0';
71917c478bd9Sstevel@tonic-gate 	} else {
71927c478bd9Sstevel@tonic-gate 		minor_name = node_name;
71937c478bd9Sstevel@tonic-gate 	}
71947c478bd9Sstevel@tonic-gate 
71957c478bd9Sstevel@tonic-gate 	minor_name = strchr(minor_name, ':'); /* look for minor name */
71967c478bd9Sstevel@tonic-gate 
71977c478bd9Sstevel@tonic-gate 	if (minor_name == NULL) {
71987c478bd9Sstevel@tonic-gate 		err_print(NO_MINOR, devname);
71997c478bd9Sstevel@tonic-gate 		goto use_defaults;
72007c478bd9Sstevel@tonic-gate 	}
72017c478bd9Sstevel@tonic-gate 	*minor_name++ = '\0';
72027c478bd9Sstevel@tonic-gate 
72037c478bd9Sstevel@tonic-gate 	/*
72047c478bd9Sstevel@tonic-gate 	 * mp->mp_drvname = device name from minor_perm
72057c478bd9Sstevel@tonic-gate 	 * mp->mp_minorname = minor part of device name from
72067c478bd9Sstevel@tonic-gate 	 * minor_perm
72077c478bd9Sstevel@tonic-gate 	 * drvname = name of driver for this device
72087c478bd9Sstevel@tonic-gate 	 */
72097c478bd9Sstevel@tonic-gate 
72107c478bd9Sstevel@tonic-gate 	is_clone = (strcmp(node_name, "clone") == 0 ? TRUE : FALSE);
72117c478bd9Sstevel@tonic-gate 	for (mp = minor_perms; mp != NULL; mp = mp->mp_next) {
72127c478bd9Sstevel@tonic-gate 		mp_drvname_matches_node_name =
72130a653502Swroche 		    (strcmp(mp->mp_drvname, node_name) == 0 ? TRUE : FALSE);
72147c478bd9Sstevel@tonic-gate 		mp_drvname_matches_minor_name =
72150a653502Swroche 		    (strcmp(mp->mp_drvname, minor_name) == 0  ? TRUE:FALSE);
72167c478bd9Sstevel@tonic-gate 		mp_drvname_is_clone =
72170a653502Swroche 		    (strcmp(mp->mp_drvname, "clone") == 0  ? TRUE : FALSE);
72187c478bd9Sstevel@tonic-gate 		mp_drvname_matches_drvname =
72190a653502Swroche 		    (strcmp(mp->mp_drvname, driver) == 0  ? TRUE : FALSE);
72207c478bd9Sstevel@tonic-gate 
72217c478bd9Sstevel@tonic-gate 		/*
72227c478bd9Sstevel@tonic-gate 		 * If one of the following cases is true, then we try to change
72237c478bd9Sstevel@tonic-gate 		 * the permissions if a "shell global pattern match" of
72247c478bd9Sstevel@tonic-gate 		 * mp_>mp_minorname matches minor_name.
72257c478bd9Sstevel@tonic-gate 		 *
72267c478bd9Sstevel@tonic-gate 		 * 1.  mp->mp_drvname matches driver.
72277c478bd9Sstevel@tonic-gate 		 *
72287c478bd9Sstevel@tonic-gate 		 * OR
72297c478bd9Sstevel@tonic-gate 		 *
72307c478bd9Sstevel@tonic-gate 		 * 2.  mp->mp_drvname matches node_name and this
72317c478bd9Sstevel@tonic-gate 		 *	name is an alias of the driver name
72327c478bd9Sstevel@tonic-gate 		 *
72337c478bd9Sstevel@tonic-gate 		 * OR
72347c478bd9Sstevel@tonic-gate 		 *
72357c478bd9Sstevel@tonic-gate 		 * 3.  /devices entry is the clone device and either
72367c478bd9Sstevel@tonic-gate 		 *	minor_perm entry is the clone device or matches
72377c478bd9Sstevel@tonic-gate 		 *	the minor part of the clone device.
72387c478bd9Sstevel@tonic-gate 		 */
72397c478bd9Sstevel@tonic-gate 
72407c478bd9Sstevel@tonic-gate 		if ((mp_drvname_matches_drvname == TRUE)||
72417c478bd9Sstevel@tonic-gate 		    ((mp_drvname_matches_node_name == TRUE) &&
72427c478bd9Sstevel@tonic-gate 		    (alias(driver, node_name) == TRUE)) ||
72437c478bd9Sstevel@tonic-gate 		    ((is_clone == TRUE) &&
72447c478bd9Sstevel@tonic-gate 		    ((mp_drvname_is_clone == TRUE) ||
72457c478bd9Sstevel@tonic-gate 		    (mp_drvname_matches_minor_name == TRUE)))) {
72467c478bd9Sstevel@tonic-gate 			/*
72477c478bd9Sstevel@tonic-gate 			 * Check that the minor part of the
72487c478bd9Sstevel@tonic-gate 			 * device name from the minor_perm
72497c478bd9Sstevel@tonic-gate 			 * entry matches and if so, set the
72507c478bd9Sstevel@tonic-gate 			 * permissions.
72517c478bd9Sstevel@tonic-gate 			 *
72527c478bd9Sstevel@tonic-gate 			 * Under real devfs, clone minor name is changed
72537c478bd9Sstevel@tonic-gate 			 * to match the driver name, but minor_perm may
72547c478bd9Sstevel@tonic-gate 			 * not match. We reconcile it here.
72557c478bd9Sstevel@tonic-gate 			 */
72567c478bd9Sstevel@tonic-gate 			if (aminor != NULL)
72577c478bd9Sstevel@tonic-gate 				minor_name = aminor;
72587c478bd9Sstevel@tonic-gate 
72597c478bd9Sstevel@tonic-gate 			if (gmatch(minor_name, mp->mp_minorname) != 0) {
72607c478bd9Sstevel@tonic-gate 				*uid = mp->mp_uid;
72617c478bd9Sstevel@tonic-gate 				*gid = mp->mp_gid;
72627c478bd9Sstevel@tonic-gate 				*mode = spectype | mp->mp_mode;
72637c478bd9Sstevel@tonic-gate 				match = TRUE;
72647c478bd9Sstevel@tonic-gate 			}
72657c478bd9Sstevel@tonic-gate 		}
72667c478bd9Sstevel@tonic-gate 	}
72677c478bd9Sstevel@tonic-gate 
72687c478bd9Sstevel@tonic-gate 	if (match == TRUE) {
72697c478bd9Sstevel@tonic-gate 		return;
72707c478bd9Sstevel@tonic-gate 	}
72717c478bd9Sstevel@tonic-gate 
72727c478bd9Sstevel@tonic-gate 	use_defaults:
72737c478bd9Sstevel@tonic-gate 	/* not found in minor_perm, so just use default values */
72747c478bd9Sstevel@tonic-gate 	*uid = root_uid;
72757c478bd9Sstevel@tonic-gate 	*gid = sys_gid;
72767c478bd9Sstevel@tonic-gate 	*mode = (spectype | 0600);
72777c478bd9Sstevel@tonic-gate }
72787c478bd9Sstevel@tonic-gate 
72797c478bd9Sstevel@tonic-gate /*
72807c478bd9Sstevel@tonic-gate  * Called by devfs_read_minor_perm() to report errors
72817c478bd9Sstevel@tonic-gate  * key is:
72827c478bd9Sstevel@tonic-gate  *	line number: ignoring line number error
72837c478bd9Sstevel@tonic-gate  *	errno: open/close errors
72847c478bd9Sstevel@tonic-gate  *	size: alloc errors
72857c478bd9Sstevel@tonic-gate  */
72867c478bd9Sstevel@tonic-gate static void
72877c478bd9Sstevel@tonic-gate minorperm_err_cb(minorperm_err_t mp_err, int key)
72887c478bd9Sstevel@tonic-gate {
72897c478bd9Sstevel@tonic-gate 	switch (mp_err) {
72907c478bd9Sstevel@tonic-gate 	case MP_FOPEN_ERR:
72917c478bd9Sstevel@tonic-gate 		err_print(FOPEN_FAILED, MINOR_PERM_FILE, strerror(key));
72927c478bd9Sstevel@tonic-gate 		break;
72937c478bd9Sstevel@tonic-gate 	case MP_FCLOSE_ERR:
72947c478bd9Sstevel@tonic-gate 		err_print(FCLOSE_FAILED, MINOR_PERM_FILE, strerror(key));
72957c478bd9Sstevel@tonic-gate 		break;
72967c478bd9Sstevel@tonic-gate 	case MP_IGNORING_LINE_ERR:
72977c478bd9Sstevel@tonic-gate 		err_print(IGNORING_LINE_IN, key, MINOR_PERM_FILE);
72987c478bd9Sstevel@tonic-gate 		break;
72997c478bd9Sstevel@tonic-gate 	case MP_ALLOC_ERR:
73007c478bd9Sstevel@tonic-gate 		err_print(MALLOC_FAILED, key);
73017c478bd9Sstevel@tonic-gate 		break;
73027c478bd9Sstevel@tonic-gate 	case MP_NVLIST_ERR:
73037c478bd9Sstevel@tonic-gate 		err_print(NVLIST_ERROR, MINOR_PERM_FILE, strerror(key));
73047c478bd9Sstevel@tonic-gate 		break;
73057c478bd9Sstevel@tonic-gate 	case MP_CANT_FIND_USER_ERR:
73067c478bd9Sstevel@tonic-gate 		err_print(CANT_FIND_USER, DEFAULT_DEV_USER);
73077c478bd9Sstevel@tonic-gate 		break;
73087c478bd9Sstevel@tonic-gate 	case MP_CANT_FIND_GROUP_ERR:
73097c478bd9Sstevel@tonic-gate 		err_print(CANT_FIND_GROUP, DEFAULT_DEV_GROUP);
73107c478bd9Sstevel@tonic-gate 		break;
73117c478bd9Sstevel@tonic-gate 	}
73127c478bd9Sstevel@tonic-gate }
73137c478bd9Sstevel@tonic-gate 
73147c478bd9Sstevel@tonic-gate static void
73157c478bd9Sstevel@tonic-gate read_minor_perm_file(void)
73167c478bd9Sstevel@tonic-gate {
73177c478bd9Sstevel@tonic-gate 	static int cached = FALSE;
73187c478bd9Sstevel@tonic-gate 	static struct stat cached_sb;
73197c478bd9Sstevel@tonic-gate 	struct stat current_sb;
73207c478bd9Sstevel@tonic-gate 
73217c478bd9Sstevel@tonic-gate 	(void) stat(MINOR_PERM_FILE, &current_sb);
73227c478bd9Sstevel@tonic-gate 
73237c478bd9Sstevel@tonic-gate 	/* If already cached, check to see if it is still valid */
73247c478bd9Sstevel@tonic-gate 	if (cached == TRUE) {
73257c478bd9Sstevel@tonic-gate 
73267c478bd9Sstevel@tonic-gate 		if (current_sb.st_mtime == cached_sb.st_mtime) {
73277c478bd9Sstevel@tonic-gate 			vprint(FILES_MID, "%s cache valid\n", MINOR_PERM_FILE);
73287c478bd9Sstevel@tonic-gate 			return;
73297c478bd9Sstevel@tonic-gate 		}
73307c478bd9Sstevel@tonic-gate 		devfs_free_minor_perm(minor_perms);
73317c478bd9Sstevel@tonic-gate 		minor_perms = NULL;
73327c478bd9Sstevel@tonic-gate 	} else {
73337c478bd9Sstevel@tonic-gate 		cached = TRUE;
73347c478bd9Sstevel@tonic-gate 	}
73357c478bd9Sstevel@tonic-gate 
73367c478bd9Sstevel@tonic-gate 	(void) stat(MINOR_PERM_FILE, &cached_sb);
73377c478bd9Sstevel@tonic-gate 
73387c478bd9Sstevel@tonic-gate 	vprint(FILES_MID, "loading binding file: %s\n", MINOR_PERM_FILE);
73397c478bd9Sstevel@tonic-gate 
73407c478bd9Sstevel@tonic-gate 	minor_perms = devfs_read_minor_perm(minorperm_err_cb);
73417c478bd9Sstevel@tonic-gate }
73427c478bd9Sstevel@tonic-gate 
73437c478bd9Sstevel@tonic-gate static void
73447c478bd9Sstevel@tonic-gate load_minor_perm_file(void)
73457c478bd9Sstevel@tonic-gate {
73467c478bd9Sstevel@tonic-gate 	read_minor_perm_file();
73477c478bd9Sstevel@tonic-gate 	if (devfs_load_minor_perm(minor_perms, minorperm_err_cb) != 0)
73487c478bd9Sstevel@tonic-gate 		err_print(gettext("minor_perm load failed\n"));
73497c478bd9Sstevel@tonic-gate }
73507c478bd9Sstevel@tonic-gate 
73517c478bd9Sstevel@tonic-gate static char *
73527c478bd9Sstevel@tonic-gate convert_to_re(char *dev)
73537c478bd9Sstevel@tonic-gate {
73547c478bd9Sstevel@tonic-gate 	char *p, *l, *out;
73557c478bd9Sstevel@tonic-gate 	int i;
73567c478bd9Sstevel@tonic-gate 
73577c478bd9Sstevel@tonic-gate 	out = s_malloc(PATH_MAX);
73587c478bd9Sstevel@tonic-gate 
73597c478bd9Sstevel@tonic-gate 	for (l = p = dev, i = 0; (*p != '\0') && (i < (PATH_MAX - 1));
73607c478bd9Sstevel@tonic-gate 	    ++p, i++) {
73617c478bd9Sstevel@tonic-gate 		if ((*p == '*') && ((l != p) && (*l == '/'))) {
73627c478bd9Sstevel@tonic-gate 			out[i++] = '.';
73637c478bd9Sstevel@tonic-gate 			out[i] = '+';
73647c478bd9Sstevel@tonic-gate 		} else {
73657c478bd9Sstevel@tonic-gate 			out[i] = *p;
73667c478bd9Sstevel@tonic-gate 		}
73677c478bd9Sstevel@tonic-gate 		l = p;
73687c478bd9Sstevel@tonic-gate 	}
73697c478bd9Sstevel@tonic-gate 	out[i] = '\0';
73707c478bd9Sstevel@tonic-gate 	p = (char *)s_malloc(strlen(out) + 1);
73717c478bd9Sstevel@tonic-gate 	(void) strlcpy(p, out, strlen(out) + 1);
73727c478bd9Sstevel@tonic-gate 	free(out);
73737c478bd9Sstevel@tonic-gate 
73747c478bd9Sstevel@tonic-gate 	vprint(FILES_MID, "converted %s -> %s\n", dev, p);
73757c478bd9Sstevel@tonic-gate 
73767c478bd9Sstevel@tonic-gate 	return (p);
73777c478bd9Sstevel@tonic-gate }
73787c478bd9Sstevel@tonic-gate 
73797c478bd9Sstevel@tonic-gate static void
73807c478bd9Sstevel@tonic-gate read_logindevperm_file(void)
73817c478bd9Sstevel@tonic-gate {
73827c478bd9Sstevel@tonic-gate 	static int cached = FALSE;
73837c478bd9Sstevel@tonic-gate 	static struct stat cached_sb;
73847c478bd9Sstevel@tonic-gate 	struct stat current_sb;
73857c478bd9Sstevel@tonic-gate 	struct login_dev *ldev;
73867c478bd9Sstevel@tonic-gate 	FILE *fp;
73877c478bd9Sstevel@tonic-gate 	char line[MAX_LDEV_LINE];
73887c478bd9Sstevel@tonic-gate 	int ln, perm, rv;
73896e670f77Saj 	char *cp, *console, *dlist, *dev;
73907c478bd9Sstevel@tonic-gate 	char *lasts, *devlasts, *permstr, *drv;
73917c478bd9Sstevel@tonic-gate 	struct driver_list *list, *next;
73927c478bd9Sstevel@tonic-gate 
73937c478bd9Sstevel@tonic-gate 	/* Read logindevperm only when enabled */
73947c478bd9Sstevel@tonic-gate 	if (login_dev_enable != TRUE)
73957c478bd9Sstevel@tonic-gate 		return;
73967c478bd9Sstevel@tonic-gate 
73977c478bd9Sstevel@tonic-gate 	if (cached == TRUE) {
73987c478bd9Sstevel@tonic-gate 		if (stat(LDEV_FILE, &current_sb) == 0 &&
73997c478bd9Sstevel@tonic-gate 		    current_sb.st_mtime == cached_sb.st_mtime) {
74007c478bd9Sstevel@tonic-gate 			vprint(FILES_MID, "%s cache valid\n", LDEV_FILE);
74017c478bd9Sstevel@tonic-gate 			return;
74027c478bd9Sstevel@tonic-gate 		}
74037c478bd9Sstevel@tonic-gate 		vprint(FILES_MID, "invalidating %s cache\n", LDEV_FILE);
74047c478bd9Sstevel@tonic-gate 		while (login_dev_cache != NULL) {
74057c478bd9Sstevel@tonic-gate 
74067c478bd9Sstevel@tonic-gate 			ldev = login_dev_cache;
74077c478bd9Sstevel@tonic-gate 			login_dev_cache = ldev->ldev_next;
74087c478bd9Sstevel@tonic-gate 			free(ldev->ldev_console);
74097c478bd9Sstevel@tonic-gate 			free(ldev->ldev_device);
74107c478bd9Sstevel@tonic-gate 			regfree(&ldev->ldev_device_regex);
74117c478bd9Sstevel@tonic-gate 			list = ldev->ldev_driver_list;
74127c478bd9Sstevel@tonic-gate 			while (list) {
74137c478bd9Sstevel@tonic-gate 				next = list->next;
74147c478bd9Sstevel@tonic-gate 				free(list);
74157c478bd9Sstevel@tonic-gate 				list = next;
74167c478bd9Sstevel@tonic-gate 			}
74177c478bd9Sstevel@tonic-gate 			free(ldev);
74187c478bd9Sstevel@tonic-gate 		}
74197c478bd9Sstevel@tonic-gate 	} else {
74207c478bd9Sstevel@tonic-gate 		cached = TRUE;
74217c478bd9Sstevel@tonic-gate 	}
74227c478bd9Sstevel@tonic-gate 
74237c478bd9Sstevel@tonic-gate 	assert(login_dev_cache == NULL);
74247c478bd9Sstevel@tonic-gate 
74257c478bd9Sstevel@tonic-gate 	if (stat(LDEV_FILE, &cached_sb) != 0) {
74267c478bd9Sstevel@tonic-gate 		cached = FALSE;
74277c478bd9Sstevel@tonic-gate 		return;
74287c478bd9Sstevel@tonic-gate 	}
74297c478bd9Sstevel@tonic-gate 
74307c478bd9Sstevel@tonic-gate 	vprint(FILES_MID, "loading file: %s\n", LDEV_FILE);
74317c478bd9Sstevel@tonic-gate 
74327c478bd9Sstevel@tonic-gate 	if ((fp = fopen(LDEV_FILE, "r")) == NULL) {
74337c478bd9Sstevel@tonic-gate 		/* Not fatal to devfsadm */
74347c478bd9Sstevel@tonic-gate 		cached = FALSE;
74357c478bd9Sstevel@tonic-gate 		err_print(FOPEN_FAILED, LDEV_FILE, strerror(errno));
74367c478bd9Sstevel@tonic-gate 		return;
74377c478bd9Sstevel@tonic-gate 	}
74387c478bd9Sstevel@tonic-gate 
74397c478bd9Sstevel@tonic-gate 	ln = 0;
74407c478bd9Sstevel@tonic-gate 	while (fgets(line, MAX_LDEV_LINE, fp) != NULL) {
74417c478bd9Sstevel@tonic-gate 		ln++;
74427c478bd9Sstevel@tonic-gate 
74437c478bd9Sstevel@tonic-gate 		/* Remove comments */
74447c478bd9Sstevel@tonic-gate 		if ((cp = strchr(line, '#')) != NULL)
74457c478bd9Sstevel@tonic-gate 			*cp = '\0';
74467c478bd9Sstevel@tonic-gate 
74477c478bd9Sstevel@tonic-gate 		if ((console = strtok_r(line, LDEV_DELIMS, &lasts)) == NULL)
74487c478bd9Sstevel@tonic-gate 			continue;	/* Blank line */
74497c478bd9Sstevel@tonic-gate 
74507c478bd9Sstevel@tonic-gate 		if ((permstr =  strtok_r(NULL, LDEV_DELIMS, &lasts)) == NULL) {
74517c478bd9Sstevel@tonic-gate 			err_print(IGNORING_LINE_IN, ln, LDEV_FILE);
74527c478bd9Sstevel@tonic-gate 			continue;	/* Malformed line */
74537c478bd9Sstevel@tonic-gate 		}
74547c478bd9Sstevel@tonic-gate 
74557c478bd9Sstevel@tonic-gate 		/*
74567c478bd9Sstevel@tonic-gate 		 * permstr is string in octal format. Convert to int
74577c478bd9Sstevel@tonic-gate 		 */
74587c478bd9Sstevel@tonic-gate 		cp = NULL;
74597c478bd9Sstevel@tonic-gate 		errno = 0;
74607c478bd9Sstevel@tonic-gate 		perm = strtol(permstr, &cp, 8);
74617c478bd9Sstevel@tonic-gate 		if (errno || perm < 0 || perm > 0777 || *cp != '\0') {
74627c478bd9Sstevel@tonic-gate 			err_print(IGNORING_LINE_IN, ln, LDEV_FILE);
74637c478bd9Sstevel@tonic-gate 			continue;
74647c478bd9Sstevel@tonic-gate 		}
74657c478bd9Sstevel@tonic-gate 
74666e670f77Saj 		if ((dlist = strtok_r(NULL, LDEV_DELIMS, &lasts)) == NULL) {
74677c478bd9Sstevel@tonic-gate 			err_print(IGNORING_LINE_IN, ln, LDEV_FILE);
74687c478bd9Sstevel@tonic-gate 			continue;
74697c478bd9Sstevel@tonic-gate 		}
74707c478bd9Sstevel@tonic-gate 
74716e670f77Saj 		dev = strtok_r(dlist, LDEV_DEV_DELIM, &devlasts);
74727c478bd9Sstevel@tonic-gate 		while (dev) {
74737c478bd9Sstevel@tonic-gate 
74747c478bd9Sstevel@tonic-gate 			ldev = (struct login_dev *)s_zalloc(
74757c478bd9Sstevel@tonic-gate 			    sizeof (struct login_dev));
74767c478bd9Sstevel@tonic-gate 			ldev->ldev_console = s_strdup(console);
74777c478bd9Sstevel@tonic-gate 			ldev->ldev_perms = perm;
74787c478bd9Sstevel@tonic-gate 
74797c478bd9Sstevel@tonic-gate 			/*
74807c478bd9Sstevel@tonic-gate 			 * the logical device name may contain '*' which
74817c478bd9Sstevel@tonic-gate 			 * we convert to a regular expression
74827c478bd9Sstevel@tonic-gate 			 */
74837c478bd9Sstevel@tonic-gate 			ldev->ldev_device = convert_to_re(dev);
74847c478bd9Sstevel@tonic-gate 			if (ldev->ldev_device &&
74857c478bd9Sstevel@tonic-gate 			    (rv = regcomp(&ldev->ldev_device_regex,
74867c478bd9Sstevel@tonic-gate 			    ldev->ldev_device, REG_EXTENDED))) {
74877c478bd9Sstevel@tonic-gate 				bzero(&ldev->ldev_device_regex,
74887c478bd9Sstevel@tonic-gate 				    sizeof (ldev->ldev_device_regex));
74897c478bd9Sstevel@tonic-gate 				err_print(REGCOMP_FAILED,
74907c478bd9Sstevel@tonic-gate 				    ldev->ldev_device, rv);
74917c478bd9Sstevel@tonic-gate 			}
74927c478bd9Sstevel@tonic-gate 			ldev->ldev_next = login_dev_cache;
74937c478bd9Sstevel@tonic-gate 			login_dev_cache = ldev;
74947c478bd9Sstevel@tonic-gate 			dev = strtok_r(NULL, LDEV_DEV_DELIM, &devlasts);
74957c478bd9Sstevel@tonic-gate 		}
74967c478bd9Sstevel@tonic-gate 
74977c478bd9Sstevel@tonic-gate 		drv = strtok_r(NULL, LDEV_DRVLIST_DELIMS, &lasts);
74987c478bd9Sstevel@tonic-gate 		if (drv) {
74997c478bd9Sstevel@tonic-gate 			if (strcmp(drv, LDEV_DRVLIST_NAME) == 0) {
75007c478bd9Sstevel@tonic-gate 
75010a653502Swroche 				drv = strtok_r(NULL, LDEV_DRV_DELIMS, &lasts);
75027c478bd9Sstevel@tonic-gate 
75037c478bd9Sstevel@tonic-gate 				while (drv) {
75047c478bd9Sstevel@tonic-gate 					vprint(FILES_MID,
75050a653502Swroche 					    "logindevperm driver=%s\n", drv);
75067c478bd9Sstevel@tonic-gate 
75077c478bd9Sstevel@tonic-gate 					/*
75087c478bd9Sstevel@tonic-gate 					 * create a linked list of driver
75097c478bd9Sstevel@tonic-gate 					 * names
75107c478bd9Sstevel@tonic-gate 					 */
75117c478bd9Sstevel@tonic-gate 					list = (struct driver_list *)
75127c478bd9Sstevel@tonic-gate 					    s_zalloc(
75137c478bd9Sstevel@tonic-gate 					    sizeof (struct driver_list));
75147c478bd9Sstevel@tonic-gate 					(void) strlcpy(list->driver_name, drv,
75157c478bd9Sstevel@tonic-gate 					    sizeof (list->driver_name));
75167c478bd9Sstevel@tonic-gate 					list->next = ldev->ldev_driver_list;
75177c478bd9Sstevel@tonic-gate 					ldev->ldev_driver_list = list;
75187c478bd9Sstevel@tonic-gate 					drv = strtok_r(NULL, LDEV_DRV_DELIMS,
75197c478bd9Sstevel@tonic-gate 					    &lasts);
75207c478bd9Sstevel@tonic-gate 				}
75217c478bd9Sstevel@tonic-gate 			}
75227c478bd9Sstevel@tonic-gate 		}
75237c478bd9Sstevel@tonic-gate 	}
75247c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
75257c478bd9Sstevel@tonic-gate }
75267c478bd9Sstevel@tonic-gate 
75277c478bd9Sstevel@tonic-gate /*
75287c478bd9Sstevel@tonic-gate  * Tokens are separated by ' ', '\t', ':', '=', '&', '|', ';', '\n', or '\0'
75297c478bd9Sstevel@tonic-gate  *
75307c478bd9Sstevel@tonic-gate  * Returns DEVFSADM_SUCCESS if token found, DEVFSADM_FAILURE otherwise.
75317c478bd9Sstevel@tonic-gate  */
75327c478bd9Sstevel@tonic-gate static int
75337c478bd9Sstevel@tonic-gate getnexttoken(char *next, char **nextp, char **tokenpp, char *tchar)
75347c478bd9Sstevel@tonic-gate {
75357c478bd9Sstevel@tonic-gate 	char *cp;
75367c478bd9Sstevel@tonic-gate 	char *cp1;
75377c478bd9Sstevel@tonic-gate 	char *tokenp;
75387c478bd9Sstevel@tonic-gate 
75397c478bd9Sstevel@tonic-gate 	cp = next;
75407c478bd9Sstevel@tonic-gate 	while (*cp == ' ' || *cp == '\t') {
75417c478bd9Sstevel@tonic-gate 		cp++;			/* skip leading spaces */
75427c478bd9Sstevel@tonic-gate 	}
75437c478bd9Sstevel@tonic-gate 	tokenp = cp;			/* start of token */
75447c478bd9Sstevel@tonic-gate 	while (*cp != '\0' && *cp != '\n' && *cp != ' ' && *cp != '\t' &&
75450a653502Swroche 	    *cp != ':' && *cp != '=' && *cp != '&' &&
75460a653502Swroche 	    *cp != '|' && *cp != ';') {
75477c478bd9Sstevel@tonic-gate 		cp++;			/* point to next character */
75487c478bd9Sstevel@tonic-gate 	}
75497c478bd9Sstevel@tonic-gate 	/*
75507c478bd9Sstevel@tonic-gate 	 * If terminating character is a space or tab, look ahead to see if
75517c478bd9Sstevel@tonic-gate 	 * there's another terminator that's not a space or a tab.
75527c478bd9Sstevel@tonic-gate 	 * (This code handles trailing spaces.)
75537c478bd9Sstevel@tonic-gate 	 */
75547c478bd9Sstevel@tonic-gate 	if (*cp == ' ' || *cp == '\t') {
75557c478bd9Sstevel@tonic-gate 		cp1 = cp;
75567c478bd9Sstevel@tonic-gate 		while (*++cp1 == ' ' || *cp1 == '\t')
75577c478bd9Sstevel@tonic-gate 			;
75587c478bd9Sstevel@tonic-gate 		if (*cp1 == '=' || *cp1 == ':' || *cp1 == '&' || *cp1 == '|' ||
75590a653502Swroche 		    *cp1 == ';' || *cp1 == '\n' || *cp1 == '\0') {
75607c478bd9Sstevel@tonic-gate 			*cp = NULL;	/* terminate token */
75617c478bd9Sstevel@tonic-gate 			cp = cp1;
75627c478bd9Sstevel@tonic-gate 		}
75637c478bd9Sstevel@tonic-gate 	}
75647c478bd9Sstevel@tonic-gate 	if (tchar != NULL) {
75657c478bd9Sstevel@tonic-gate 		*tchar = *cp;		/* save terminating character */
75667c478bd9Sstevel@tonic-gate 		if (*tchar == '\0') {
75677c478bd9Sstevel@tonic-gate 			*tchar = '\n';
75687c478bd9Sstevel@tonic-gate 		}
75697c478bd9Sstevel@tonic-gate 	}
75707c478bd9Sstevel@tonic-gate 	*cp++ = '\0';			/* terminate token, point to next */
75717c478bd9Sstevel@tonic-gate 	*nextp = cp;			/* set pointer to next character */
75727c478bd9Sstevel@tonic-gate 	if (cp - tokenp - 1 == 0) {
75737c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
75747c478bd9Sstevel@tonic-gate 	}
75757c478bd9Sstevel@tonic-gate 	*tokenpp = tokenp;
75767c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
75777c478bd9Sstevel@tonic-gate }
75787c478bd9Sstevel@tonic-gate 
75797c478bd9Sstevel@tonic-gate /*
75807c478bd9Sstevel@tonic-gate  * read or reread the driver aliases file
75817c478bd9Sstevel@tonic-gate  */
75827c478bd9Sstevel@tonic-gate static void
75837c478bd9Sstevel@tonic-gate read_driver_aliases_file(void)
75847c478bd9Sstevel@tonic-gate {
75857c478bd9Sstevel@tonic-gate 
75867c478bd9Sstevel@tonic-gate 	driver_alias_t *save;
75877c478bd9Sstevel@tonic-gate 	driver_alias_t *lst_tail;
75887c478bd9Sstevel@tonic-gate 	driver_alias_t *ap;
75897c478bd9Sstevel@tonic-gate 	static int cached = FALSE;
75907c478bd9Sstevel@tonic-gate 	FILE *afd;
75917c478bd9Sstevel@tonic-gate 	char line[256];
75927c478bd9Sstevel@tonic-gate 	char *cp;
75937c478bd9Sstevel@tonic-gate 	char *p;
75947c478bd9Sstevel@tonic-gate 	char t;
75957c478bd9Sstevel@tonic-gate 	int ln = 0;
75967c478bd9Sstevel@tonic-gate 	static struct stat cached_sb;
75977c478bd9Sstevel@tonic-gate 	struct stat current_sb;
75987c478bd9Sstevel@tonic-gate 
75997c478bd9Sstevel@tonic-gate 	(void) stat(ALIASFILE, &current_sb);
76007c478bd9Sstevel@tonic-gate 
76017c478bd9Sstevel@tonic-gate 	/* If already cached, check to see if it is still valid */
76027c478bd9Sstevel@tonic-gate 	if (cached == TRUE) {
76037c478bd9Sstevel@tonic-gate 
76047c478bd9Sstevel@tonic-gate 		if (current_sb.st_mtime == cached_sb.st_mtime) {
76057c478bd9Sstevel@tonic-gate 			vprint(FILES_MID, "%s cache valid\n", ALIASFILE);
76067c478bd9Sstevel@tonic-gate 			return;
76077c478bd9Sstevel@tonic-gate 		}
76087c478bd9Sstevel@tonic-gate 
76097c478bd9Sstevel@tonic-gate 		vprint(FILES_MID, "invalidating %s cache\n", ALIASFILE);
76107c478bd9Sstevel@tonic-gate 		while (driver_aliases != NULL) {
76117c478bd9Sstevel@tonic-gate 			free(driver_aliases->alias_name);
76127c478bd9Sstevel@tonic-gate 			free(driver_aliases->driver_name);
76137c478bd9Sstevel@tonic-gate 			save = driver_aliases;
76147c478bd9Sstevel@tonic-gate 			driver_aliases = driver_aliases->next;
76157c478bd9Sstevel@tonic-gate 			free(save);
76167c478bd9Sstevel@tonic-gate 		}
76177c478bd9Sstevel@tonic-gate 	} else {
76187c478bd9Sstevel@tonic-gate 		cached = TRUE;
76197c478bd9Sstevel@tonic-gate 	}
76207c478bd9Sstevel@tonic-gate 
76217c478bd9Sstevel@tonic-gate 	(void) stat(ALIASFILE, &cached_sb);
76227c478bd9Sstevel@tonic-gate 
76237c478bd9Sstevel@tonic-gate 	vprint(FILES_MID, "loading binding file: %s\n", ALIASFILE);
76247c478bd9Sstevel@tonic-gate 
76257c478bd9Sstevel@tonic-gate 	if ((afd = fopen(ALIASFILE, "r")) == NULL) {
76267c478bd9Sstevel@tonic-gate 		err_print(FOPEN_FAILED, ALIASFILE, strerror(errno));
76277c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
7628537714daSvikram 		/*NOTREACHED*/
76297c478bd9Sstevel@tonic-gate 	}
76307c478bd9Sstevel@tonic-gate 
76311ca93273Seota 	while (fgets(line, sizeof (line), afd) != NULL) {
76327c478bd9Sstevel@tonic-gate 		ln++;
76331ca93273Seota 		/* cut off comments starting with '#' */
76341ca93273Seota 		if ((cp = strchr(line, '#')) != NULL)
76351ca93273Seota 			*cp = '\0';
76361ca93273Seota 		/* ignore comment or blank lines */
76371ca93273Seota 		if (is_blank(line))
76381ca93273Seota 			continue;
76397c478bd9Sstevel@tonic-gate 		cp = line;
76407c478bd9Sstevel@tonic-gate 		if (getnexttoken(cp, &cp, &p, &t) == DEVFSADM_FAILURE) {
76417c478bd9Sstevel@tonic-gate 			err_print(IGNORING_LINE_IN, ln, ALIASFILE);
76427c478bd9Sstevel@tonic-gate 			continue;
76437c478bd9Sstevel@tonic-gate 		}
76447c478bd9Sstevel@tonic-gate 		if (t == '\n' || t == '\0') {
76457c478bd9Sstevel@tonic-gate 			err_print(DRV_BUT_NO_ALIAS, ln, ALIASFILE);
76467c478bd9Sstevel@tonic-gate 			continue;
76477c478bd9Sstevel@tonic-gate 		}
76487c478bd9Sstevel@tonic-gate 		ap = (struct driver_alias *)
76490a653502Swroche 		    s_zalloc(sizeof (struct driver_alias));
76507c478bd9Sstevel@tonic-gate 		ap->driver_name = s_strdup(p);
76517c478bd9Sstevel@tonic-gate 		if (getnexttoken(cp, &cp, &p, &t) == DEVFSADM_FAILURE) {
76527c478bd9Sstevel@tonic-gate 			err_print(DRV_BUT_NO_ALIAS, ln, ALIASFILE);
76537c478bd9Sstevel@tonic-gate 			free(ap->driver_name);
76547c478bd9Sstevel@tonic-gate 			free(ap);
76557c478bd9Sstevel@tonic-gate 			continue;
76567c478bd9Sstevel@tonic-gate 		}
76577c478bd9Sstevel@tonic-gate 		if (*p == '"') {
76587c478bd9Sstevel@tonic-gate 			if (p[strlen(p) - 1] == '"') {
76597c478bd9Sstevel@tonic-gate 				p[strlen(p) - 1] = '\0';
76607c478bd9Sstevel@tonic-gate 				p++;
76617c478bd9Sstevel@tonic-gate 			}
76627c478bd9Sstevel@tonic-gate 		}
76637c478bd9Sstevel@tonic-gate 		ap->alias_name = s_strdup(p);
76647c478bd9Sstevel@tonic-gate 		if (driver_aliases == NULL) {
76657c478bd9Sstevel@tonic-gate 			driver_aliases = ap;
76667c478bd9Sstevel@tonic-gate 			lst_tail = ap;
76677c478bd9Sstevel@tonic-gate 		} else {
76687c478bd9Sstevel@tonic-gate 			lst_tail->next = ap;
76697c478bd9Sstevel@tonic-gate 			lst_tail = ap;
76707c478bd9Sstevel@tonic-gate 		}
76717c478bd9Sstevel@tonic-gate 	}
76727c478bd9Sstevel@tonic-gate 	if (fclose(afd) == EOF) {
76737c478bd9Sstevel@tonic-gate 		err_print(FCLOSE_FAILED, ALIASFILE, strerror(errno));
76747c478bd9Sstevel@tonic-gate 	}
76757c478bd9Sstevel@tonic-gate }
76767c478bd9Sstevel@tonic-gate 
76777c478bd9Sstevel@tonic-gate /*
76787c478bd9Sstevel@tonic-gate  * return TRUE if alias_name is an alias for driver_name, otherwise
76797c478bd9Sstevel@tonic-gate  * return FALSE.
76807c478bd9Sstevel@tonic-gate  */
76817c478bd9Sstevel@tonic-gate static int
76827c478bd9Sstevel@tonic-gate alias(char *driver_name, char *alias_name)
76837c478bd9Sstevel@tonic-gate {
76847c478bd9Sstevel@tonic-gate 	driver_alias_t *alias;
76857c478bd9Sstevel@tonic-gate 
76867c478bd9Sstevel@tonic-gate 	/*
76877c478bd9Sstevel@tonic-gate 	 * check for a match
76887c478bd9Sstevel@tonic-gate 	 */
76897c478bd9Sstevel@tonic-gate 	for (alias = driver_aliases; alias != NULL; alias = alias->next) {
76907c478bd9Sstevel@tonic-gate 		if ((strcmp(alias->driver_name, driver_name) == 0) &&
76917c478bd9Sstevel@tonic-gate 		    (strcmp(alias->alias_name, alias_name) == 0)) {
76927c478bd9Sstevel@tonic-gate 			return (TRUE);
76937c478bd9Sstevel@tonic-gate 		}
76947c478bd9Sstevel@tonic-gate 	}
76957c478bd9Sstevel@tonic-gate 	return (FALSE);
76967c478bd9Sstevel@tonic-gate }
76977c478bd9Sstevel@tonic-gate 
76987c478bd9Sstevel@tonic-gate /*
76997c478bd9Sstevel@tonic-gate  * convenience functions
77007c478bd9Sstevel@tonic-gate  */
7701facf4a8dSllai static int
7702facf4a8dSllai s_stat(const char *path, struct stat *sbufp)
7703facf4a8dSllai {
7704facf4a8dSllai 	int rv;
7705facf4a8dSllai retry:
7706facf4a8dSllai 	if ((rv = stat(path, sbufp)) == -1) {
7707facf4a8dSllai 		if (errno == EINTR)
7708facf4a8dSllai 			goto retry;
7709facf4a8dSllai 	}
7710facf4a8dSllai 	return (rv);
7711facf4a8dSllai }
7712facf4a8dSllai 
77137c478bd9Sstevel@tonic-gate static void *
77147c478bd9Sstevel@tonic-gate s_malloc(const size_t size)
77157c478bd9Sstevel@tonic-gate {
77167c478bd9Sstevel@tonic-gate 	void *rp;
77177c478bd9Sstevel@tonic-gate 
77187c478bd9Sstevel@tonic-gate 	rp = malloc(size);
77197c478bd9Sstevel@tonic-gate 	if (rp == NULL) {
77207c478bd9Sstevel@tonic-gate 		err_print(MALLOC_FAILED, size);
77217c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
7722537714daSvikram 		/*NOTREACHED*/
77237c478bd9Sstevel@tonic-gate 	}
77247c478bd9Sstevel@tonic-gate 	return (rp);
77257c478bd9Sstevel@tonic-gate }
77267c478bd9Sstevel@tonic-gate 
77277c478bd9Sstevel@tonic-gate /*
77287c478bd9Sstevel@tonic-gate  * convenience functions
77297c478bd9Sstevel@tonic-gate  */
77307c478bd9Sstevel@tonic-gate static void *
77317c478bd9Sstevel@tonic-gate s_realloc(void *ptr, const size_t size)
77327c478bd9Sstevel@tonic-gate {
77337c478bd9Sstevel@tonic-gate 	ptr = realloc(ptr, size);
77347c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
77357c478bd9Sstevel@tonic-gate 		err_print(REALLOC_FAILED, size);
77367c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
7737537714daSvikram 		/*NOTREACHED*/
77387c478bd9Sstevel@tonic-gate 	}
77397c478bd9Sstevel@tonic-gate 	return (ptr);
77407c478bd9Sstevel@tonic-gate }
77417c478bd9Sstevel@tonic-gate 
77427c478bd9Sstevel@tonic-gate static void *
77437c478bd9Sstevel@tonic-gate s_zalloc(const size_t size)
77447c478bd9Sstevel@tonic-gate {
77457c478bd9Sstevel@tonic-gate 	void *rp;
77467c478bd9Sstevel@tonic-gate 
77477c478bd9Sstevel@tonic-gate 	rp = calloc(1, size);
77487c478bd9Sstevel@tonic-gate 	if (rp == NULL) {
77497c478bd9Sstevel@tonic-gate 		err_print(CALLOC_FAILED, size);
77507c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
7751537714daSvikram 		/*NOTREACHED*/
77527c478bd9Sstevel@tonic-gate 	}
77537c478bd9Sstevel@tonic-gate 	return (rp);
77547c478bd9Sstevel@tonic-gate }
77557c478bd9Sstevel@tonic-gate 
77567c478bd9Sstevel@tonic-gate char *
77577c478bd9Sstevel@tonic-gate s_strdup(const char *ptr)
77587c478bd9Sstevel@tonic-gate {
77597c478bd9Sstevel@tonic-gate 	void *rp;
77607c478bd9Sstevel@tonic-gate 
77617c478bd9Sstevel@tonic-gate 	rp = strdup(ptr);
77627c478bd9Sstevel@tonic-gate 	if (rp == NULL) {
77637c478bd9Sstevel@tonic-gate 		err_print(STRDUP_FAILED, ptr);
77647c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
7765537714daSvikram 		/*NOTREACHED*/
77667c478bd9Sstevel@tonic-gate 	}
77677c478bd9Sstevel@tonic-gate 	return (rp);
77687c478bd9Sstevel@tonic-gate }
77697c478bd9Sstevel@tonic-gate 
77707c478bd9Sstevel@tonic-gate static void
77717c478bd9Sstevel@tonic-gate s_closedir(DIR *dirp)
77727c478bd9Sstevel@tonic-gate {
77737c478bd9Sstevel@tonic-gate retry:
77747c478bd9Sstevel@tonic-gate 	if (closedir(dirp) != 0) {
77757c478bd9Sstevel@tonic-gate 		if (errno == EINTR)
77767c478bd9Sstevel@tonic-gate 			goto retry;
77777c478bd9Sstevel@tonic-gate 		err_print(CLOSEDIR_FAILED, strerror(errno));
77787c478bd9Sstevel@tonic-gate 	}
77797c478bd9Sstevel@tonic-gate }
77807c478bd9Sstevel@tonic-gate 
77817c478bd9Sstevel@tonic-gate static void
77827c478bd9Sstevel@tonic-gate s_mkdirp(const char *path, const mode_t mode)
77837c478bd9Sstevel@tonic-gate {
77847c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "mkdirp(%s, 0x%lx)\n", path, mode);
77857c478bd9Sstevel@tonic-gate 	if (mkdirp(path, mode) == -1) {
77867c478bd9Sstevel@tonic-gate 		if (errno != EEXIST) {
77877c478bd9Sstevel@tonic-gate 			err_print(MKDIR_FAILED, path, mode, strerror(errno));
77887c478bd9Sstevel@tonic-gate 		}
77897c478bd9Sstevel@tonic-gate 	}
77907c478bd9Sstevel@tonic-gate }
77917c478bd9Sstevel@tonic-gate 
77927c478bd9Sstevel@tonic-gate static void
77937c478bd9Sstevel@tonic-gate s_unlink(const char *file)
77947c478bd9Sstevel@tonic-gate {
77957c478bd9Sstevel@tonic-gate retry:
77967c478bd9Sstevel@tonic-gate 	if (unlink(file) == -1) {
77977c478bd9Sstevel@tonic-gate 		if (errno == EINTR || errno == EAGAIN)
77987c478bd9Sstevel@tonic-gate 			goto retry;
77997c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
78007c478bd9Sstevel@tonic-gate 			err_print(UNLINK_FAILED, file, strerror(errno));
78017c478bd9Sstevel@tonic-gate 		}
78027c478bd9Sstevel@tonic-gate 	}
78037c478bd9Sstevel@tonic-gate }
78047c478bd9Sstevel@tonic-gate 
78057c478bd9Sstevel@tonic-gate static void
78067c478bd9Sstevel@tonic-gate add_verbose_id(char *mid)
78077c478bd9Sstevel@tonic-gate {
78087c478bd9Sstevel@tonic-gate 	num_verbose++;
78097c478bd9Sstevel@tonic-gate 	verbose = s_realloc(verbose, num_verbose * sizeof (char *));
78107c478bd9Sstevel@tonic-gate 	verbose[num_verbose - 1] = mid;
78117c478bd9Sstevel@tonic-gate }
78127c478bd9Sstevel@tonic-gate 
78137c478bd9Sstevel@tonic-gate /*
78147c478bd9Sstevel@tonic-gate  * returns DEVFSADM_TRUE if contents is a minor node in /devices.
78157c478bd9Sstevel@tonic-gate  * If mn_root is not NULL, mn_root is set to:
78167c478bd9Sstevel@tonic-gate  *	if contents is a /dev node, mn_root = contents
7817*f2dbfd32SRobert Mustacchi  *			OR
78187c478bd9Sstevel@tonic-gate  *	if contents is a /devices node, mn_root set to the '/'
78197c478bd9Sstevel@tonic-gate  *	following /devices.
78207c478bd9Sstevel@tonic-gate  */
78217c478bd9Sstevel@tonic-gate static int
78227c478bd9Sstevel@tonic-gate is_minor_node(char *contents, char **mn_root)
78237c478bd9Sstevel@tonic-gate {
78247c478bd9Sstevel@tonic-gate 	char *ptr;
78257c478bd9Sstevel@tonic-gate 	char device_prefix[100];
78267c478bd9Sstevel@tonic-gate 
78277c478bd9Sstevel@tonic-gate 	(void) snprintf(device_prefix, sizeof (device_prefix), "../devices/");
78287c478bd9Sstevel@tonic-gate 
78297c478bd9Sstevel@tonic-gate 	if ((ptr = strstr(contents, device_prefix)) != NULL) {
78307c478bd9Sstevel@tonic-gate 		if (mn_root != NULL) {
78317c478bd9Sstevel@tonic-gate 			/* mn_root should point to the / following /devices */
78327c478bd9Sstevel@tonic-gate 			*mn_root = ptr += strlen(device_prefix) - 1;
78337c478bd9Sstevel@tonic-gate 		}
78347c478bd9Sstevel@tonic-gate 		return (DEVFSADM_TRUE);
78357c478bd9Sstevel@tonic-gate 	}
78367c478bd9Sstevel@tonic-gate 
78377c478bd9Sstevel@tonic-gate 	(void) snprintf(device_prefix, sizeof (device_prefix), "/devices/");
78387c478bd9Sstevel@tonic-gate 
78397c478bd9Sstevel@tonic-gate 	if (strncmp(contents, device_prefix, strlen(device_prefix)) == 0) {
78407c478bd9Sstevel@tonic-gate 		if (mn_root != NULL) {
78417c478bd9Sstevel@tonic-gate 			/* mn_root should point to the / following /devices */
78427c478bd9Sstevel@tonic-gate 			*mn_root = contents + strlen(device_prefix) - 1;
78437c478bd9Sstevel@tonic-gate 		}
78447c478bd9Sstevel@tonic-gate 		return (DEVFSADM_TRUE);
78457c478bd9Sstevel@tonic-gate 	}
78467c478bd9Sstevel@tonic-gate 
78477c478bd9Sstevel@tonic-gate 	if (mn_root != NULL) {
78487c478bd9Sstevel@tonic-gate 		*mn_root = contents;
78497c478bd9Sstevel@tonic-gate 	}
78507c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FALSE);
78517c478bd9Sstevel@tonic-gate }
78527c478bd9Sstevel@tonic-gate 
78537c478bd9Sstevel@tonic-gate /*
78547c478bd9Sstevel@tonic-gate  * Add the specified property to nvl.
78557c478bd9Sstevel@tonic-gate  * Returns:
78567c478bd9Sstevel@tonic-gate  *   0	successfully added
78577c478bd9Sstevel@tonic-gate  *   -1	an error occurred
78587c478bd9Sstevel@tonic-gate  *   1	could not add the property for reasons not due to errors.
78597c478bd9Sstevel@tonic-gate  */
78607c478bd9Sstevel@tonic-gate static int
78617c478bd9Sstevel@tonic-gate add_property(nvlist_t *nvl, di_prop_t prop)
78627c478bd9Sstevel@tonic-gate {
78637c478bd9Sstevel@tonic-gate 	char *name;
78647c478bd9Sstevel@tonic-gate 	char *attr_name;
78657c478bd9Sstevel@tonic-gate 	int n, len;
78667c478bd9Sstevel@tonic-gate 	int32_t *int32p;
78677c478bd9Sstevel@tonic-gate 	int64_t *int64p;
78687c478bd9Sstevel@tonic-gate 	char *str;
78697c478bd9Sstevel@tonic-gate 	char **strarray;
78707c478bd9Sstevel@tonic-gate 	uchar_t *bytep;
78717c478bd9Sstevel@tonic-gate 	int rv = 0;
78727c478bd9Sstevel@tonic-gate 	int i;
78737c478bd9Sstevel@tonic-gate 
78747c478bd9Sstevel@tonic-gate 	if ((name = di_prop_name(prop)) == NULL)
78757c478bd9Sstevel@tonic-gate 		return (-1);
78767c478bd9Sstevel@tonic-gate 
78777c478bd9Sstevel@tonic-gate 	len = sizeof (DEV_PROP_PREFIX) + strlen(name);
78787c478bd9Sstevel@tonic-gate 	if ((attr_name = malloc(len)) == NULL)
78797c478bd9Sstevel@tonic-gate 		return (-1);
78807c478bd9Sstevel@tonic-gate 
78817c478bd9Sstevel@tonic-gate 	(void) strlcpy(attr_name, DEV_PROP_PREFIX, len);
78827c478bd9Sstevel@tonic-gate 	(void) strlcat(attr_name, name, len);
78837c478bd9Sstevel@tonic-gate 
78847c478bd9Sstevel@tonic-gate 	switch (di_prop_type(prop)) {
78857c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_BOOLEAN:
78867c478bd9Sstevel@tonic-gate 		if (nvlist_add_boolean(nvl, attr_name) != 0)
78877c478bd9Sstevel@tonic-gate 			goto out;
78887c478bd9Sstevel@tonic-gate 		break;
78897c478bd9Sstevel@tonic-gate 
78907c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_INT:
78917c478bd9Sstevel@tonic-gate 		if ((n = di_prop_ints(prop, &int32p)) < 1)
78927c478bd9Sstevel@tonic-gate 			goto out;
78937c478bd9Sstevel@tonic-gate 
78947c478bd9Sstevel@tonic-gate 		if (n <= (PROP_LEN_LIMIT / sizeof (int32_t))) {
78957c478bd9Sstevel@tonic-gate 			if (nvlist_add_int32_array(nvl, attr_name, int32p,
78967c478bd9Sstevel@tonic-gate 			    n) != 0)
78977c478bd9Sstevel@tonic-gate 				goto out;
78987c478bd9Sstevel@tonic-gate 		} else
78997c478bd9Sstevel@tonic-gate 			rv = 1;
79007c478bd9Sstevel@tonic-gate 		break;
79017c478bd9Sstevel@tonic-gate 
79027c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_INT64:
79037c478bd9Sstevel@tonic-gate 		if ((n = di_prop_int64(prop, &int64p)) < 1)
79047c478bd9Sstevel@tonic-gate 			goto out;
79057c478bd9Sstevel@tonic-gate 
79067c478bd9Sstevel@tonic-gate 		if (n <= (PROP_LEN_LIMIT / sizeof (int64_t))) {
79077c478bd9Sstevel@tonic-gate 			if (nvlist_add_int64_array(nvl, attr_name, int64p,
79087c478bd9Sstevel@tonic-gate 			    n) != 0)
79097c478bd9Sstevel@tonic-gate 				goto out;
79107c478bd9Sstevel@tonic-gate 		} else
79117c478bd9Sstevel@tonic-gate 			rv = 1;
79127c478bd9Sstevel@tonic-gate 		break;
79137c478bd9Sstevel@tonic-gate 
79147c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_BYTE:
79157c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_UNKNOWN:
79167c478bd9Sstevel@tonic-gate 		if ((n = di_prop_bytes(prop, &bytep)) < 1)
79177c478bd9Sstevel@tonic-gate 			goto out;
79187c478bd9Sstevel@tonic-gate 
79197c478bd9Sstevel@tonic-gate 		if (n <= PROP_LEN_LIMIT) {
79207c478bd9Sstevel@tonic-gate 			if (nvlist_add_byte_array(nvl, attr_name, bytep, n)
79217c478bd9Sstevel@tonic-gate 			    != 0)
79227c478bd9Sstevel@tonic-gate 				goto out;
79237c478bd9Sstevel@tonic-gate 		} else
79247c478bd9Sstevel@tonic-gate 			rv = 1;
79257c478bd9Sstevel@tonic-gate 		break;
79267c478bd9Sstevel@tonic-gate 
79277c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_STRING:
79287c478bd9Sstevel@tonic-gate 		if ((n = di_prop_strings(prop, &str)) < 1)
79297c478bd9Sstevel@tonic-gate 			goto out;
79307c478bd9Sstevel@tonic-gate 
79317c478bd9Sstevel@tonic-gate 		if ((strarray = malloc(n * sizeof (char *))) == NULL)
79327c478bd9Sstevel@tonic-gate 			goto out;
79337c478bd9Sstevel@tonic-gate 
79347c478bd9Sstevel@tonic-gate 		len = 0;
79357c478bd9Sstevel@tonic-gate 		for (i = 0; i < n; i++) {
79367c478bd9Sstevel@tonic-gate 			strarray[i] = str + len;
79377c478bd9Sstevel@tonic-gate 			len += strlen(strarray[i]) + 1;
79387c478bd9Sstevel@tonic-gate 		}
79397c478bd9Sstevel@tonic-gate 
79407c478bd9Sstevel@tonic-gate 		if (len <= PROP_LEN_LIMIT) {
79417c478bd9Sstevel@tonic-gate 			if (nvlist_add_string_array(nvl, attr_name, strarray,
79427c478bd9Sstevel@tonic-gate 			    n) != 0) {
79437c478bd9Sstevel@tonic-gate 				free(strarray);
79447c478bd9Sstevel@tonic-gate 				goto out;
79457c478bd9Sstevel@tonic-gate 			}
79467c478bd9Sstevel@tonic-gate 		} else
79477c478bd9Sstevel@tonic-gate 			rv = 1;
79487c478bd9Sstevel@tonic-gate 		free(strarray);
79497c478bd9Sstevel@tonic-gate 		break;
79507c478bd9Sstevel@tonic-gate 
79517c478bd9Sstevel@tonic-gate 	default:
79527c478bd9Sstevel@tonic-gate 		rv = 1;
79537c478bd9Sstevel@tonic-gate 		break;
79547c478bd9Sstevel@tonic-gate 	}
79557c478bd9Sstevel@tonic-gate 
79567c478bd9Sstevel@tonic-gate 	free(attr_name);
79577c478bd9Sstevel@tonic-gate 	return (rv);
79587c478bd9Sstevel@tonic-gate 
79597c478bd9Sstevel@tonic-gate out:
79607c478bd9Sstevel@tonic-gate 	free(attr_name);
79617c478bd9Sstevel@tonic-gate 	return (-1);
79627c478bd9Sstevel@tonic-gate }
79637c478bd9Sstevel@tonic-gate 
79647c478bd9Sstevel@tonic-gate static void
79657c478bd9Sstevel@tonic-gate free_dev_names(struct devlink_cb_arg *x)
79667c478bd9Sstevel@tonic-gate {
79677c478bd9Sstevel@tonic-gate 	int i;
79687c478bd9Sstevel@tonic-gate 
79697c478bd9Sstevel@tonic-gate 	for (i = 0; i < x->count; i++) {
79707c478bd9Sstevel@tonic-gate 		free(x->dev_names[i]);
79717c478bd9Sstevel@tonic-gate 		free(x->link_contents[i]);
79727c478bd9Sstevel@tonic-gate 	}
79737c478bd9Sstevel@tonic-gate }
79747c478bd9Sstevel@tonic-gate 
79757c478bd9Sstevel@tonic-gate /* callback function for di_devlink_cache_walk */
79767c478bd9Sstevel@tonic-gate static int
79777c478bd9Sstevel@tonic-gate devlink_cb(di_devlink_t dl, void *arg)
79787c478bd9Sstevel@tonic-gate {
79797c478bd9Sstevel@tonic-gate 	struct devlink_cb_arg *x = (struct devlink_cb_arg *)arg;
79807c478bd9Sstevel@tonic-gate 	const char *path;
79817c478bd9Sstevel@tonic-gate 	const char *content;
79827c478bd9Sstevel@tonic-gate 
79837c478bd9Sstevel@tonic-gate 	if ((path = di_devlink_path(dl)) == NULL ||
79847c478bd9Sstevel@tonic-gate 	    (content = di_devlink_content(dl)) == NULL ||
798512d8cf2aSjg 	    (x->dev_names[x->count] = s_strdup(path)) == NULL)
79867c478bd9Sstevel@tonic-gate 		goto out;
79877c478bd9Sstevel@tonic-gate 
798812d8cf2aSjg 	if ((x->link_contents[x->count] = s_strdup(content)) == NULL) {
79897c478bd9Sstevel@tonic-gate 		free(x->dev_names[x->count]);
79907c478bd9Sstevel@tonic-gate 		goto out;
79917c478bd9Sstevel@tonic-gate 	}
79927c478bd9Sstevel@tonic-gate 
79937c478bd9Sstevel@tonic-gate 	x->count++;
79947c478bd9Sstevel@tonic-gate 	if (x->count >= MAX_DEV_NAME_COUNT)
79957c478bd9Sstevel@tonic-gate 		return (DI_WALK_TERMINATE);
79967c478bd9Sstevel@tonic-gate 
79977c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
79987c478bd9Sstevel@tonic-gate 
79997c478bd9Sstevel@tonic-gate out:
80007c478bd9Sstevel@tonic-gate 	x->rv = -1;
80017c478bd9Sstevel@tonic-gate 	free_dev_names(x);
80027c478bd9Sstevel@tonic-gate 	return (DI_WALK_TERMINATE);
80037c478bd9Sstevel@tonic-gate }
80047c478bd9Sstevel@tonic-gate 
80057c478bd9Sstevel@tonic-gate /*
80067c478bd9Sstevel@tonic-gate  * Lookup dev name corresponding to the phys_path.
80077c478bd9Sstevel@tonic-gate  * phys_path is path to a node or minor node.
80087c478bd9Sstevel@tonic-gate  * Returns:
80097c478bd9Sstevel@tonic-gate  *	0 with *dev_name set to the dev name
80107c478bd9Sstevel@tonic-gate  *		Lookup succeeded and dev_name found
80117c478bd9Sstevel@tonic-gate  *	0 with *dev_name set to NULL
80127c478bd9Sstevel@tonic-gate  *		Lookup encountered no errors but dev name not found
80137c478bd9Sstevel@tonic-gate  *	-1
80147c478bd9Sstevel@tonic-gate  *		Lookup failed
80157c478bd9Sstevel@tonic-gate  */
80167c478bd9Sstevel@tonic-gate static int
80177c478bd9Sstevel@tonic-gate lookup_dev_name(char *phys_path, char **dev_name)
80187c478bd9Sstevel@tonic-gate {
80197c478bd9Sstevel@tonic-gate 	struct devlink_cb_arg cb_arg;
80207c478bd9Sstevel@tonic-gate 
80217c478bd9Sstevel@tonic-gate 	*dev_name = NULL;
80227c478bd9Sstevel@tonic-gate 
80237c478bd9Sstevel@tonic-gate 	cb_arg.count = 0;
80247c478bd9Sstevel@tonic-gate 	cb_arg.rv = 0;
80257c478bd9Sstevel@tonic-gate 	(void) di_devlink_cache_walk(devlink_cache, NULL, phys_path,
80267c478bd9Sstevel@tonic-gate 	    DI_PRIMARY_LINK, &cb_arg, devlink_cb);
80277c478bd9Sstevel@tonic-gate 
80287c478bd9Sstevel@tonic-gate 	if (cb_arg.rv == -1)
80297c478bd9Sstevel@tonic-gate 		return (-1);
80307c478bd9Sstevel@tonic-gate 
80317c478bd9Sstevel@tonic-gate 	if (cb_arg.count > 0) {
803212d8cf2aSjg 		*dev_name = s_strdup(cb_arg.dev_names[0]);
80337c478bd9Sstevel@tonic-gate 		free_dev_names(&cb_arg);
80347c478bd9Sstevel@tonic-gate 		if (*dev_name == NULL)
80357c478bd9Sstevel@tonic-gate 			return (-1);
80367c478bd9Sstevel@tonic-gate 	}
80377c478bd9Sstevel@tonic-gate 
80387c478bd9Sstevel@tonic-gate 	return (0);
80397c478bd9Sstevel@tonic-gate }
80407c478bd9Sstevel@tonic-gate 
80417c478bd9Sstevel@tonic-gate static char *
80427c478bd9Sstevel@tonic-gate lookup_disk_dev_name(char *node_path)
80437c478bd9Sstevel@tonic-gate {
80447c478bd9Sstevel@tonic-gate 	struct devlink_cb_arg cb_arg;
80457c478bd9Sstevel@tonic-gate 	char *dev_name = NULL;
80467c478bd9Sstevel@tonic-gate 	int i;
80477c478bd9Sstevel@tonic-gate 	char *p;
80487c478bd9Sstevel@tonic-gate 	int len1, len2;
80497c478bd9Sstevel@tonic-gate 
80507c478bd9Sstevel@tonic-gate #define	DEV_RDSK	"/dev/rdsk/"
80517c478bd9Sstevel@tonic-gate #define	DISK_RAW_MINOR	",raw"
80527c478bd9Sstevel@tonic-gate 
80537c478bd9Sstevel@tonic-gate 	cb_arg.count = 0;
80547c478bd9Sstevel@tonic-gate 	cb_arg.rv = 0;
80557c478bd9Sstevel@tonic-gate 	(void) di_devlink_cache_walk(devlink_cache, NULL, node_path,
80567c478bd9Sstevel@tonic-gate 	    DI_PRIMARY_LINK, &cb_arg, devlink_cb);
80577c478bd9Sstevel@tonic-gate 
80587c478bd9Sstevel@tonic-gate 	if (cb_arg.rv == -1 || cb_arg.count == 0)
80597c478bd9Sstevel@tonic-gate 		return (NULL);
80607c478bd9Sstevel@tonic-gate 
80617c478bd9Sstevel@tonic-gate 	/* first try lookup based on /dev/rdsk name */
80627c478bd9Sstevel@tonic-gate 	for (i = 0; i < cb_arg.count; i++) {
80637c478bd9Sstevel@tonic-gate 		if (strncmp(cb_arg.dev_names[i], DEV_RDSK,
80647c478bd9Sstevel@tonic-gate 		    sizeof (DEV_RDSK) - 1) == 0) {
806512d8cf2aSjg 			dev_name = s_strdup(cb_arg.dev_names[i]);
80667c478bd9Sstevel@tonic-gate 			break;
80677c478bd9Sstevel@tonic-gate 		}
80687c478bd9Sstevel@tonic-gate 	}
80697c478bd9Sstevel@tonic-gate 
80707c478bd9Sstevel@tonic-gate 	if (dev_name == NULL) {
80717c478bd9Sstevel@tonic-gate 		/* now try lookup based on a minor name ending with ",raw" */
80727c478bd9Sstevel@tonic-gate 		len1 = sizeof (DISK_RAW_MINOR) - 1;
80737c478bd9Sstevel@tonic-gate 		for (i = 0; i < cb_arg.count; i++) {
80747c478bd9Sstevel@tonic-gate 			len2 = strlen(cb_arg.link_contents[i]);
80757c478bd9Sstevel@tonic-gate 			if (len2 >= len1 &&
80767c478bd9Sstevel@tonic-gate 			    strcmp(cb_arg.link_contents[i] + len2 - len1,
80777c478bd9Sstevel@tonic-gate 			    DISK_RAW_MINOR) == 0) {
807812d8cf2aSjg 				dev_name = s_strdup(cb_arg.dev_names[i]);
80797c478bd9Sstevel@tonic-gate 				break;
80807c478bd9Sstevel@tonic-gate 			}
80817c478bd9Sstevel@tonic-gate 		}
80827c478bd9Sstevel@tonic-gate 	}
80837c478bd9Sstevel@tonic-gate 
80847c478bd9Sstevel@tonic-gate 	free_dev_names(&cb_arg);
80857c478bd9Sstevel@tonic-gate 
808612d8cf2aSjg 	if (dev_name == NULL)
808712d8cf2aSjg 		return (NULL);
80887c478bd9Sstevel@tonic-gate 	if (strlen(dev_name) == 0) {
80897c478bd9Sstevel@tonic-gate 		free(dev_name);
80907c478bd9Sstevel@tonic-gate 		return (NULL);
80917c478bd9Sstevel@tonic-gate 	}
80927c478bd9Sstevel@tonic-gate 
80937c478bd9Sstevel@tonic-gate 	/* if the name contains slice or partition number strip it */
80947c478bd9Sstevel@tonic-gate 	p = dev_name + strlen(dev_name) - 1;
80957c478bd9Sstevel@tonic-gate 	if (isdigit(*p)) {
80967c478bd9Sstevel@tonic-gate 		while (p != dev_name && isdigit(*p))
80977c478bd9Sstevel@tonic-gate 			p--;
80987c478bd9Sstevel@tonic-gate 		if (*p == 's' || *p == 'p')
80997c478bd9Sstevel@tonic-gate 			*p = '\0';
81007c478bd9Sstevel@tonic-gate 	}
81017c478bd9Sstevel@tonic-gate 
81027c478bd9Sstevel@tonic-gate 	return (dev_name);
81037c478bd9Sstevel@tonic-gate }
81047c478bd9Sstevel@tonic-gate 
810530294554Sphitran static char *
810630294554Sphitran lookup_lofi_dev_name(char *node_path, char *minor)
810730294554Sphitran {
810830294554Sphitran 	struct devlink_cb_arg cb_arg;
810930294554Sphitran 	char *dev_name = NULL;
811030294554Sphitran 	int i;
811130294554Sphitran 	int len1, len2;
811230294554Sphitran 
811330294554Sphitran 	cb_arg.count = 0;
811430294554Sphitran 	cb_arg.rv = 0;
811530294554Sphitran 	(void) di_devlink_cache_walk(devlink_cache, NULL, node_path,
811630294554Sphitran 	    DI_PRIMARY_LINK, &cb_arg, devlink_cb);
811730294554Sphitran 
811830294554Sphitran 	if (cb_arg.rv == -1 || cb_arg.count == 0)
811930294554Sphitran 		return (NULL);
812030294554Sphitran 
812130294554Sphitran 	/* lookup based on a minor name ending with ",raw" */
812230294554Sphitran 	len1 = strlen(minor);
812330294554Sphitran 	for (i = 0; i < cb_arg.count; i++) {
812430294554Sphitran 		len2 = strlen(cb_arg.link_contents[i]);
812530294554Sphitran 		if (len2 >= len1 &&
812630294554Sphitran 		    strcmp(cb_arg.link_contents[i] + len2 - len1,
812730294554Sphitran 		    minor) == 0) {
812830294554Sphitran 			dev_name = s_strdup(cb_arg.dev_names[i]);
812930294554Sphitran 			break;
813030294554Sphitran 		}
813130294554Sphitran 	}
813230294554Sphitran 
813330294554Sphitran 	free_dev_names(&cb_arg);
813430294554Sphitran 
813530294554Sphitran 	if (dev_name == NULL)
813630294554Sphitran 		return (NULL);
813730294554Sphitran 	if (strlen(dev_name) == 0) {
813830294554Sphitran 		free(dev_name);
813930294554Sphitran 		return (NULL);
814030294554Sphitran 	}
814130294554Sphitran 
814230294554Sphitran 	return (dev_name);
814330294554Sphitran }
814430294554Sphitran 
81457c478bd9Sstevel@tonic-gate static char *
81467c478bd9Sstevel@tonic-gate lookup_network_dev_name(char *node_path, char *driver_name)
81477c478bd9Sstevel@tonic-gate {
81487c478bd9Sstevel@tonic-gate 	char *dev_name = NULL;
81497c478bd9Sstevel@tonic-gate 	char phys_path[MAXPATHLEN];
81507c478bd9Sstevel@tonic-gate 
81517c478bd9Sstevel@tonic-gate 	if (lookup_dev_name(node_path, &dev_name) == -1)
81527c478bd9Sstevel@tonic-gate 		return (NULL);
81537c478bd9Sstevel@tonic-gate 
81547c478bd9Sstevel@tonic-gate 	if (dev_name == NULL) {
81557c478bd9Sstevel@tonic-gate 		/* dlpi style-2 only interface */
81567c478bd9Sstevel@tonic-gate 		(void) snprintf(phys_path, sizeof (phys_path),
81577c478bd9Sstevel@tonic-gate 		    "/pseudo/clone@0:%s", driver_name);
81587c478bd9Sstevel@tonic-gate 		if (lookup_dev_name(phys_path, &dev_name) == -1 ||
81597c478bd9Sstevel@tonic-gate 		    dev_name == NULL)
81607c478bd9Sstevel@tonic-gate 			return (NULL);
81617c478bd9Sstevel@tonic-gate 	}
81627c478bd9Sstevel@tonic-gate 
81637c478bd9Sstevel@tonic-gate 	return (dev_name);
81647c478bd9Sstevel@tonic-gate }
81657c478bd9Sstevel@tonic-gate 
8166db11e6feSjacobs static char *
8167db11e6feSjacobs lookup_printer_dev_name(char *node_path)
8168db11e6feSjacobs {
8169db11e6feSjacobs 	struct devlink_cb_arg cb_arg;
8170db11e6feSjacobs 	char *dev_name = NULL;
8171db11e6feSjacobs 	int i;
8172db11e6feSjacobs 
8173db11e6feSjacobs #define	DEV_PRINTERS	"/dev/printers/"
8174db11e6feSjacobs 
8175db11e6feSjacobs 	cb_arg.count = 0;
8176db11e6feSjacobs 	cb_arg.rv = 0;
8177db11e6feSjacobs 	(void) di_devlink_cache_walk(devlink_cache, NULL, node_path,
8178db11e6feSjacobs 	    DI_PRIMARY_LINK, &cb_arg, devlink_cb);
8179db11e6feSjacobs 
8180db11e6feSjacobs 	if (cb_arg.rv == -1 || cb_arg.count == 0)
8181db11e6feSjacobs 		return (NULL);
8182db11e6feSjacobs 
8183db11e6feSjacobs 	/* first try lookup based on /dev/printers name */
8184db11e6feSjacobs 	for (i = 0; i < cb_arg.count; i++) {
8185db11e6feSjacobs 		if (strncmp(cb_arg.dev_names[i], DEV_PRINTERS,
8186db11e6feSjacobs 		    sizeof (DEV_PRINTERS) - 1) == 0) {
8187db11e6feSjacobs 			dev_name = s_strdup(cb_arg.dev_names[i]);
8188db11e6feSjacobs 			break;
8189db11e6feSjacobs 		}
8190db11e6feSjacobs 	}
8191db11e6feSjacobs 
8192db11e6feSjacobs 	/* fallback to the first name */
8193db11e6feSjacobs 	if ((dev_name == NULL) && (cb_arg.count > 0))
8194db11e6feSjacobs 		dev_name = s_strdup(cb_arg.dev_names[0]);
8195db11e6feSjacobs 
8196db11e6feSjacobs 	free_dev_names(&cb_arg);
8197db11e6feSjacobs 
8198db11e6feSjacobs 	return (dev_name);
8199db11e6feSjacobs }
8200db11e6feSjacobs 
82017c478bd9Sstevel@tonic-gate /*
82027c478bd9Sstevel@tonic-gate  * Build an nvlist containing all attributes for devfs events.
82037c478bd9Sstevel@tonic-gate  * Returns nvlist pointer on success, NULL on failure.
82047c478bd9Sstevel@tonic-gate  */
82057c478bd9Sstevel@tonic-gate static nvlist_t *
82067c478bd9Sstevel@tonic-gate build_event_attributes(char *class, char *subclass, char *node_path,
820730294554Sphitran     di_node_t node, char *driver_name, int instance, char *minor)
82087c478bd9Sstevel@tonic-gate {
82097c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
82107c478bd9Sstevel@tonic-gate 	int err = 0;
82117c478bd9Sstevel@tonic-gate 	di_prop_t prop;
82127c478bd9Sstevel@tonic-gate 	int count;
82137c478bd9Sstevel@tonic-gate 	char *prop_name;
82147c478bd9Sstevel@tonic-gate 	int x;
82157c478bd9Sstevel@tonic-gate 	char *dev_name = NULL;
82167c478bd9Sstevel@tonic-gate 	int dev_name_lookup_err = 0;
82177c478bd9Sstevel@tonic-gate 
82187c478bd9Sstevel@tonic-gate 	if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, 0)) != 0) {
82197c478bd9Sstevel@tonic-gate 		nvl = NULL;
82207c478bd9Sstevel@tonic-gate 		goto out;
82217c478bd9Sstevel@tonic-gate 	}
82227c478bd9Sstevel@tonic-gate 
82237c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_int32(nvl, EV_VERSION, EV_V1)) != 0)
82247c478bd9Sstevel@tonic-gate 		goto out;
82257c478bd9Sstevel@tonic-gate 
82267c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_string(nvl, DEV_PHYS_PATH, node_path)) != 0)
82277c478bd9Sstevel@tonic-gate 		goto out;
82287c478bd9Sstevel@tonic-gate 
82297c478bd9Sstevel@tonic-gate 	if (strcmp(class, EC_DEV_ADD) != 0 &&
82307c478bd9Sstevel@tonic-gate 	    strcmp(class, EC_DEV_REMOVE) != 0)
82317c478bd9Sstevel@tonic-gate 		return (nvl);
82327c478bd9Sstevel@tonic-gate 
82337c478bd9Sstevel@tonic-gate 	if (driver_name == NULL || instance == -1)
82347c478bd9Sstevel@tonic-gate 		goto out;
82357c478bd9Sstevel@tonic-gate 
82367c478bd9Sstevel@tonic-gate 	if (strcmp(subclass, ESC_DISK) == 0) {
8237406fc510SToomas Soome 		/*
8238406fc510SToomas Soome 		 * While we're removing labeled lofi device, we will receive
8239406fc510SToomas Soome 		 * event for every registered minor device and lastly,
8240406fc510SToomas Soome 		 * an event with minor set to NULL, as in following example:
8241406fc510SToomas Soome 		 * class: EC_dev_remove subclass: disk
8242406fc510SToomas Soome 		 * node_path: /pseudo/lofi@1 driver: lofi minor: u,raw
8243406fc510SToomas Soome 		 * class: EC_dev_remove subclass: disk
8244406fc510SToomas Soome 		 * node_path: /pseudo/lofi@1 driver: lofi minor: NULL
8245406fc510SToomas Soome 		 *
8246406fc510SToomas Soome 		 * When we receive this last event with minor set to NULL,
8247406fc510SToomas Soome 		 * all lofi minor devices are already removed and the call to
8248406fc510SToomas Soome 		 * lookup_disk_dev_name() would result in error.
8249406fc510SToomas Soome 		 * To prevent name lookup error messages for this case, we
8250406fc510SToomas Soome 		 * need to filter out that last event.
8251406fc510SToomas Soome 		 */
8252406fc510SToomas Soome 		if (strcmp(class, EC_DEV_REMOVE) == 0 &&
8253406fc510SToomas Soome 		    strcmp(driver_name, "lofi") ==  0 && minor == NULL) {
8254406fc510SToomas Soome 			nvlist_free(nvl);
8255406fc510SToomas Soome 			return (NULL);
8256406fc510SToomas Soome 		}
82577c478bd9Sstevel@tonic-gate 		if ((dev_name = lookup_disk_dev_name(node_path)) == NULL) {
82587c478bd9Sstevel@tonic-gate 			dev_name_lookup_err = 1;
82597c478bd9Sstevel@tonic-gate 			goto out;
82607c478bd9Sstevel@tonic-gate 		}
82617c478bd9Sstevel@tonic-gate 	} else if (strcmp(subclass, ESC_NETWORK) == 0) {
82627c478bd9Sstevel@tonic-gate 		if ((dev_name = lookup_network_dev_name(node_path, driver_name))
82637c478bd9Sstevel@tonic-gate 		    == NULL) {
82647c478bd9Sstevel@tonic-gate 			dev_name_lookup_err = 1;
82657c478bd9Sstevel@tonic-gate 			goto out;
82667c478bd9Sstevel@tonic-gate 		}
8267db11e6feSjacobs 	} else if (strcmp(subclass, ESC_PRINTER) == 0) {
8268db11e6feSjacobs 		if ((dev_name = lookup_printer_dev_name(node_path)) == NULL) {
8269db11e6feSjacobs 			dev_name_lookup_err = 1;
8270db11e6feSjacobs 			goto out;
8271db11e6feSjacobs 		}
827230294554Sphitran 	} else if (strcmp(subclass, ESC_LOFI) == 0) {
827330294554Sphitran 		/*
827430294554Sphitran 		 * The raw minor node is created or removed after the block
827530294554Sphitran 		 * node.  Lofi devfs events are dependent on this behavior.
827630294554Sphitran 		 * Generate the sysevent only for the raw minor node.
8277406fc510SToomas Soome 		 *
8278406fc510SToomas Soome 		 * If the lofi mapping is created, we will receive the following
8279406fc510SToomas Soome 		 * event: class: EC_dev_add subclass: lofi minor: NULL
8280406fc510SToomas Soome 		 *
8281406fc510SToomas Soome 		 * As in case of EC_dev_add, the minor is NULL pointer,
8282406fc510SToomas Soome 		 * to get device links created, we will need to provide the
8283406fc510SToomas Soome 		 * type of minor node for lookup_lofi_dev_name()
8284406fc510SToomas Soome 		 *
8285406fc510SToomas Soome 		 * If the lofi device is unmapped, we will receive following
8286406fc510SToomas Soome 		 * events:
8287406fc510SToomas Soome 		 * class: EC_dev_remove subclass: lofi minor: disk
8288406fc510SToomas Soome 		 * class: EC_dev_remove subclass: lofi minor: disk,raw
8289406fc510SToomas Soome 		 * class: EC_dev_remove subclass: lofi minor: NULL
829030294554Sphitran 		 */
8291406fc510SToomas Soome 
8292406fc510SToomas Soome 		if (strcmp(class, EC_DEV_ADD) == 0 && minor == NULL)
8293406fc510SToomas Soome 			minor = "disk,raw";
8294406fc510SToomas Soome 
8295406fc510SToomas Soome 		if (minor == NULL || strstr(minor, "raw") == NULL) {
8296406fc510SToomas Soome 			nvlist_free(nvl);
829730294554Sphitran 			return (NULL);
829830294554Sphitran 		}
829930294554Sphitran 		if ((dev_name = lookup_lofi_dev_name(node_path, minor)) ==
830030294554Sphitran 		    NULL) {
830130294554Sphitran 			dev_name_lookup_err = 1;
830230294554Sphitran 			goto out;
830330294554Sphitran 		}
83047c478bd9Sstevel@tonic-gate 	}
83057c478bd9Sstevel@tonic-gate 
83067c478bd9Sstevel@tonic-gate 	if (dev_name) {
83077c478bd9Sstevel@tonic-gate 		if ((err = nvlist_add_string(nvl, DEV_NAME, dev_name)) != 0)
83087c478bd9Sstevel@tonic-gate 			goto out;
83097c478bd9Sstevel@tonic-gate 		free(dev_name);
83107c478bd9Sstevel@tonic-gate 		dev_name = NULL;
83117c478bd9Sstevel@tonic-gate 	}
83127c478bd9Sstevel@tonic-gate 
83137c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_string(nvl, DEV_DRIVER_NAME, driver_name)) != 0)
83147c478bd9Sstevel@tonic-gate 		goto out;
83157c478bd9Sstevel@tonic-gate 
83167c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_int32(nvl, DEV_INSTANCE, instance)) != 0)
83177c478bd9Sstevel@tonic-gate 		goto out;
83187c478bd9Sstevel@tonic-gate 
83197c478bd9Sstevel@tonic-gate 	if (strcmp(class, EC_DEV_ADD) == 0) {
83207c478bd9Sstevel@tonic-gate 		/* add properties */
83217c478bd9Sstevel@tonic-gate 		count = 0;
83227c478bd9Sstevel@tonic-gate 		for (prop = di_prop_next(node, DI_PROP_NIL);
83237c478bd9Sstevel@tonic-gate 		    prop != DI_PROP_NIL && count < MAX_PROP_COUNT;
83247c478bd9Sstevel@tonic-gate 		    prop = di_prop_next(node, prop)) {
83257c478bd9Sstevel@tonic-gate 
83267c478bd9Sstevel@tonic-gate 			if (di_prop_devt(prop) != DDI_DEV_T_NONE)
83277c478bd9Sstevel@tonic-gate 				continue;
83287c478bd9Sstevel@tonic-gate 
83297c478bd9Sstevel@tonic-gate 			if ((x = add_property(nvl, prop)) == 0)
83307c478bd9Sstevel@tonic-gate 				count++;
83317c478bd9Sstevel@tonic-gate 			else if (x == -1) {
83327c478bd9Sstevel@tonic-gate 				if ((prop_name = di_prop_name(prop)) == NULL)
83337c478bd9Sstevel@tonic-gate 					prop_name = "";
83347c478bd9Sstevel@tonic-gate 				err_print(PROP_ADD_FAILED, prop_name);
83357c478bd9Sstevel@tonic-gate 				goto out;
83367c478bd9Sstevel@tonic-gate 			}
83377c478bd9Sstevel@tonic-gate 		}
83387c478bd9Sstevel@tonic-gate 	}
83397c478bd9Sstevel@tonic-gate 
83407c478bd9Sstevel@tonic-gate 	return (nvl);
83417c478bd9Sstevel@tonic-gate 
83427c478bd9Sstevel@tonic-gate out:
8343aab83bb8SJosef 'Jeff' Sipek 	nvlist_free(nvl);
83447c478bd9Sstevel@tonic-gate 
83457c478bd9Sstevel@tonic-gate 	if (dev_name)
83467c478bd9Sstevel@tonic-gate 		free(dev_name);
83477c478bd9Sstevel@tonic-gate 
834893239addSjohnlev 	if (dev_name_lookup_err) {
834993239addSjohnlev 		/*
835093239addSjohnlev 		 * If a lofi mount fails, the /devices node may well have
835193239addSjohnlev 		 * disappeared by the time we run, so let's not complain.
835293239addSjohnlev 		 */
835393239addSjohnlev 		if (strcmp(subclass, ESC_LOFI) != 0)
835493239addSjohnlev 			err_print(DEV_NAME_LOOKUP_FAILED, node_path);
835593239addSjohnlev 	} else {
83567c478bd9Sstevel@tonic-gate 		err_print(BUILD_EVENT_ATTR_FAILED, (err) ? strerror(err) : "");
835793239addSjohnlev 	}
83587c478bd9Sstevel@tonic-gate 	return (NULL);
83597c478bd9Sstevel@tonic-gate }
83607c478bd9Sstevel@tonic-gate 
83617c478bd9Sstevel@tonic-gate static void
83627c478bd9Sstevel@tonic-gate log_event(char *class, char *subclass, nvlist_t *nvl)
83637c478bd9Sstevel@tonic-gate {
83647c478bd9Sstevel@tonic-gate 	sysevent_id_t eid;
83657c478bd9Sstevel@tonic-gate 
83667c478bd9Sstevel@tonic-gate 	if (sysevent_post_event(class, subclass, "SUNW", DEVFSADMD,
83677c478bd9Sstevel@tonic-gate 	    nvl, &eid) != 0) {
83687c478bd9Sstevel@tonic-gate 		err_print(LOG_EVENT_FAILED, strerror(errno));
83697c478bd9Sstevel@tonic-gate 	}
83707c478bd9Sstevel@tonic-gate }
83717c478bd9Sstevel@tonic-gate 
8372f05faa4eSjacobs /*
8373f05faa4eSjacobs  * When devfsadmd needs to generate sysevents, they are queued for later
8374f05faa4eSjacobs  * delivery this allows them to be delivered after the devlinks db cache has
8375f05faa4eSjacobs  * been flushed guaranteeing that applications consuming these events have
8376f05faa4eSjacobs  * access to an accurate devlinks db.  The queue is a FIFO, sysevents to be
8377f05faa4eSjacobs  * inserted in the front of the queue and consumed off the back.
8378f05faa4eSjacobs  */
8379f05faa4eSjacobs static void
8380f05faa4eSjacobs enqueue_sysevent(char *class, char *subclass, nvlist_t *nvl)
8381f05faa4eSjacobs {
8382f05faa4eSjacobs 	syseventq_t *tmp;
8383f05faa4eSjacobs 
8384f05faa4eSjacobs 	if ((tmp = s_zalloc(sizeof (*tmp))) == NULL)
8385f05faa4eSjacobs 		return;
8386f05faa4eSjacobs 
8387f05faa4eSjacobs 	tmp->class = s_strdup(class);
8388f05faa4eSjacobs 	tmp->subclass = s_strdup(subclass);
8389f05faa4eSjacobs 	tmp->nvl = nvl;
8390f05faa4eSjacobs 
8391f05faa4eSjacobs 	(void) mutex_lock(&syseventq_mutex);
8392f05faa4eSjacobs 	if (syseventq_front != NULL)
8393f05faa4eSjacobs 		syseventq_front->next = tmp;
8394f05faa4eSjacobs 	else
8395f05faa4eSjacobs 		syseventq_back = tmp;
8396f05faa4eSjacobs 	syseventq_front = tmp;
8397f05faa4eSjacobs 	(void) mutex_unlock(&syseventq_mutex);
8398f05faa4eSjacobs }
8399f05faa4eSjacobs 
8400f05faa4eSjacobs static void
8401f05faa4eSjacobs process_syseventq()
8402f05faa4eSjacobs {
8403f05faa4eSjacobs 	(void) mutex_lock(&syseventq_mutex);
8404f05faa4eSjacobs 	while (syseventq_back != NULL) {
8405f05faa4eSjacobs 		syseventq_t *tmp = syseventq_back;
8406f05faa4eSjacobs 
8407f05faa4eSjacobs 		vprint(CHATTY_MID, "sending queued event: %s, %s\n",
84080a653502Swroche 		    tmp->class, tmp->subclass);
8409f05faa4eSjacobs 
8410f05faa4eSjacobs 		log_event(tmp->class, tmp->subclass, tmp->nvl);
8411f05faa4eSjacobs 
8412f05faa4eSjacobs 		if (tmp->class != NULL)
8413f05faa4eSjacobs 			free(tmp->class);
8414f05faa4eSjacobs 		if (tmp->subclass != NULL)
8415f05faa4eSjacobs 			free(tmp->subclass);
8416aab83bb8SJosef 'Jeff' Sipek 		nvlist_free(tmp->nvl);
8417f05faa4eSjacobs 		syseventq_back = syseventq_back->next;
8418f05faa4eSjacobs 		if (syseventq_back == NULL)
8419f05faa4eSjacobs 			syseventq_front = NULL;
8420f05faa4eSjacobs 		free(tmp);
8421f05faa4eSjacobs 	}
8422f05faa4eSjacobs 	(void) mutex_unlock(&syseventq_mutex);
8423f05faa4eSjacobs }
8424f05faa4eSjacobs 
84257c478bd9Sstevel@tonic-gate static void
8426f05faa4eSjacobs build_and_enq_event(char *class, char *subclass, char *node_path,
8427406fc510SToomas Soome     di_node_t node, char *minor)
84287c478bd9Sstevel@tonic-gate {
84297c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
84307c478bd9Sstevel@tonic-gate 
8431f05faa4eSjacobs 	vprint(CHATTY_MID, "build_and_enq_event(%s, %s, %s, 0x%8.8x)\n",
84320a653502Swroche 	    class, subclass, node_path, (int)node);
8433f05faa4eSjacobs 
84347c478bd9Sstevel@tonic-gate 	if (node != DI_NODE_NIL)
84357c478bd9Sstevel@tonic-gate 		nvl = build_event_attributes(class, subclass, node_path, node,
843630294554Sphitran 		    di_driver_name(node), di_instance(node), minor);
84377c478bd9Sstevel@tonic-gate 	else
84387c478bd9Sstevel@tonic-gate 		nvl = build_event_attributes(class, subclass, node_path, node,
843930294554Sphitran 		    NULL, -1, minor);
84407c478bd9Sstevel@tonic-gate 
84417c478bd9Sstevel@tonic-gate 	if (nvl) {
8442f05faa4eSjacobs 		enqueue_sysevent(class, subclass, nvl);
84437c478bd9Sstevel@tonic-gate 	}
84447c478bd9Sstevel@tonic-gate }
8445facf4a8dSllai 
84461ca93273Seota /*
84471ca93273Seota  * is_blank() returns 1 (true) if a line specified is composed of
84481ca93273Seota  * whitespace characters only. otherwise, it returns 0 (false).
84491ca93273Seota  *
84501ca93273Seota  * Note. the argument (line) must be null-terminated.
84511ca93273Seota  */
84521ca93273Seota static int
84531ca93273Seota is_blank(char *line)
84541ca93273Seota {
84551ca93273Seota 	for (/* nothing */; *line != '\0'; line++)
84561ca93273Seota 		if (!isspace(*line))
84571ca93273Seota 			return (0);
84581ca93273Seota 	return (1);
84591ca93273Seota }
84601ca93273Seota 
8461aa646b9dSvikram /*
8462aa646b9dSvikram  * Functions to deal with the no-further-processing hash
8463aa646b9dSvikram  */
8464aa646b9dSvikram 
8465aa646b9dSvikram static void
8466aa646b9dSvikram nfphash_create(void)
8467aa646b9dSvikram {
8468aa646b9dSvikram 	assert(nfp_hash == NULL);
8469aa646b9dSvikram 	nfp_hash = s_zalloc(NFP_HASH_SZ * sizeof (item_t *));
8470aa646b9dSvikram }
8471aa646b9dSvikram 
8472aa646b9dSvikram static int
8473aa646b9dSvikram nfphash_fcn(char *key)
8474aa646b9dSvikram {
8475aa646b9dSvikram 	int i;
8476aa646b9dSvikram 	uint64_t sum = 0;
8477aa646b9dSvikram 
8478aa646b9dSvikram 	for (i = 0; key[i] != '\0'; i++) {
8479aa646b9dSvikram 		sum += (uchar_t)key[i];
8480aa646b9dSvikram 	}
8481aa646b9dSvikram 
8482aa646b9dSvikram 	return (sum % NFP_HASH_SZ);
8483aa646b9dSvikram }
8484aa646b9dSvikram 
8485aa646b9dSvikram static item_t *
8486aa646b9dSvikram nfphash_lookup(char *key)
8487aa646b9dSvikram {
8488aa646b9dSvikram 	int	index;
8489aa646b9dSvikram 	item_t  *ip;
8490aa646b9dSvikram 
8491aa646b9dSvikram 	index = nfphash_fcn(key);
8492aa646b9dSvikram 
8493aa646b9dSvikram 	assert(index >= 0);
8494aa646b9dSvikram 
8495aa646b9dSvikram 	for (ip = nfp_hash[index]; ip; ip = ip->i_next) {
8496aa646b9dSvikram 		if (strcmp(ip->i_key, key) == 0)
8497aa646b9dSvikram 			return (ip);
8498aa646b9dSvikram 	}
8499aa646b9dSvikram 
8500aa646b9dSvikram 	return (NULL);
8501aa646b9dSvikram }
8502aa646b9dSvikram 
8503aa646b9dSvikram static void
8504aa646b9dSvikram nfphash_insert(char *key)
8505aa646b9dSvikram {
8506aa646b9dSvikram 	item_t	*ip;
8507aa646b9dSvikram 	int	index;
8508aa646b9dSvikram 
8509aa646b9dSvikram 	index = nfphash_fcn(key);
8510aa646b9dSvikram 
8511aa646b9dSvikram 	assert(index >= 0);
8512aa646b9dSvikram 
8513aa646b9dSvikram 	ip = s_zalloc(sizeof (item_t));
8514aa646b9dSvikram 	ip->i_key = s_strdup(key);
8515aa646b9dSvikram 
8516aa646b9dSvikram 	ip->i_next = nfp_hash[index];
8517aa646b9dSvikram 	nfp_hash[index] = ip;
8518aa646b9dSvikram }
8519aa646b9dSvikram 
8520aa646b9dSvikram static void
8521aa646b9dSvikram nfphash_destroy(void)
8522aa646b9dSvikram {
8523aa646b9dSvikram 	int	i;
8524aa646b9dSvikram 	item_t	*ip;
8525aa646b9dSvikram 
8526aa646b9dSvikram 	for (i = 0; i < NFP_HASH_SZ; i++) {
8527aa646b9dSvikram 		/*LINTED*/
8528aa646b9dSvikram 		while (ip = nfp_hash[i]) {
8529aa646b9dSvikram 			nfp_hash[i] = ip->i_next;
8530aa646b9dSvikram 			free(ip->i_key);
8531aa646b9dSvikram 			free(ip);
8532aa646b9dSvikram 		}
8533aa646b9dSvikram 	}
8534aa646b9dSvikram 
8535aa646b9dSvikram 	free(nfp_hash);
8536aa646b9dSvikram 	nfp_hash = NULL;
8537aa646b9dSvikram }
8538aa646b9dSvikram 
8539facf4a8dSllai static int
8540facf4a8dSllai devname_kcall(int subcmd, void *args)
8541facf4a8dSllai {
8542facf4a8dSllai 	int error = 0;
8543facf4a8dSllai 
8544facf4a8dSllai 	switch (subcmd) {
8545facf4a8dSllai 	case MODDEVNAME_LOOKUPDOOR:
8546facf4a8dSllai 		error = modctl(MODDEVNAME, subcmd, (uintptr_t)args);
8547facf4a8dSllai 		if (error) {
8548facf4a8dSllai 			vprint(INFO_MID, "modctl(MODDEVNAME, "
8549facf4a8dSllai 			    "MODDEVNAME_LOOKUPDOOR) failed - %s\n",
8550facf4a8dSllai 			    strerror(errno));
8551facf4a8dSllai 		}
8552facf4a8dSllai 		break;
8553facf4a8dSllai 	default:
8554facf4a8dSllai 		error = EINVAL;
8555facf4a8dSllai 		break;
8556facf4a8dSllai 	}
8557facf4a8dSllai 	return (error);
8558facf4a8dSllai }
8559facf4a8dSllai 
8560facf4a8dSllai /* ARGSUSED */
8561facf4a8dSllai static void
8562facf4a8dSllai devname_lookup_handler(void *cookie, char *argp, size_t arg_size,
8563facf4a8dSllai     door_desc_t *dp, uint_t n_desc)
8564facf4a8dSllai {
8565facf4a8dSllai 	int32_t error = 0;
8566facf4a8dSllai 	door_cred_t dcred;
8567facf4a8dSllai 	struct dca_impl	dci;
8568facf4a8dSllai 	uint8_t	cmd;
8569facf4a8dSllai 	sdev_door_res_t res;
8570facf4a8dSllai 	sdev_door_arg_t *args;
8571facf4a8dSllai 
8572facf4a8dSllai 	if (argp == NULL || arg_size == 0) {
8573facf4a8dSllai 		vprint(DEVNAME_MID, "devname_lookup_handler: argp wrong\n");
8574facf4a8dSllai 		error = DEVFSADM_RUN_INVALID;
8575facf4a8dSllai 		goto done;
8576facf4a8dSllai 	}
8577facf4a8dSllai 	vprint(DEVNAME_MID, "devname_lookup_handler\n");
8578facf4a8dSllai 
8579facf4a8dSllai 	if (door_cred(&dcred) != 0 || dcred.dc_euid != 0) {
8580facf4a8dSllai 		vprint(DEVNAME_MID, "devname_lookup_handler: cred wrong\n");
8581facf4a8dSllai 		error = DEVFSADM_RUN_EPERM;
8582facf4a8dSllai 		goto done;
8583facf4a8dSllai 	}
8584facf4a8dSllai 
8585facf4a8dSllai 	args = (sdev_door_arg_t *)argp;
8586facf4a8dSllai 	cmd = args->devfsadm_cmd;
8587facf4a8dSllai 
8588facf4a8dSllai 	vprint(DEVNAME_MID, "devname_lookup_handler: cmd %d\n", cmd);
8589facf4a8dSllai 	switch (cmd) {
8590facf4a8dSllai 	case DEVFSADMD_RUN_ALL:
8591facf4a8dSllai 		/*
8592facf4a8dSllai 		 * run "devfsadm"
8593facf4a8dSllai 		 */
8594facf4a8dSllai 		dci.dci_root = "/";
8595facf4a8dSllai 		dci.dci_minor = NULL;
8596facf4a8dSllai 		dci.dci_driver = NULL;
8597facf4a8dSllai 		dci.dci_error = 0;
8598facf4a8dSllai 		dci.dci_flags = 0;
8599facf4a8dSllai 		dci.dci_arg = NULL;
8600facf4a8dSllai 
8601facf4a8dSllai 		lock_dev();
8602c9cc1492SJerry Gilliam 		update_drvconf((major_t)-1, 0);
8603facf4a8dSllai 		dci.dci_flags |= DCA_FLUSH_PATHINST;
8604facf4a8dSllai 
8605facf4a8dSllai 		pre_and_post_cleanup(RM_PRE);
860666ea8494SVikram Hegde 		devi_tree_walk(&dci, DI_CACHE_SNAPSHOT_FLAGS, NULL);
8607facf4a8dSllai 		error = (int32_t)dci.dci_error;
8608facf4a8dSllai 		if (!error) {
8609facf4a8dSllai 			pre_and_post_cleanup(RM_POST);
8610facf4a8dSllai 			update_database = TRUE;
8611facf4a8dSllai 			unlock_dev(SYNC_STATE);
8612facf4a8dSllai 			update_database = FALSE;
8613facf4a8dSllai 		} else {
8614facf4a8dSllai 			if (DEVFSADM_DEBUG_ON) {
8615facf4a8dSllai 				vprint(INFO_MID, "devname_lookup_handler: "
8616facf4a8dSllai 				    "DEVFSADMD_RUN_ALL failed\n");
8617facf4a8dSllai 			}
8618facf4a8dSllai 
8619facf4a8dSllai 			unlock_dev(SYNC_STATE);
8620facf4a8dSllai 		}
8621facf4a8dSllai 		break;
8622facf4a8dSllai 	default:
8623facf4a8dSllai 		/* log an error here? */
8624facf4a8dSllai 		error = DEVFSADM_RUN_NOTSUP;
8625facf4a8dSllai 		break;
8626facf4a8dSllai 	}
8627facf4a8dSllai 
8628facf4a8dSllai done:
8629facf4a8dSllai 	vprint(DEVNAME_MID, "devname_lookup_handler: error %d\n", error);
8630facf4a8dSllai 	res.devfsadm_error = error;
8631facf4a8dSllai 	(void) door_return((char *)&res, sizeof (struct sdev_door_res),
8632facf4a8dSllai 	    NULL, 0);
8633facf4a8dSllai }
86348d483882Smlf 
86358d483882Smlf 
86368d483882Smlf di_devlink_handle_t
86378d483882Smlf devfsadm_devlink_cache(void)
86388d483882Smlf {
86398d483882Smlf 	return (devlink_cache);
86408d483882Smlf }
86418d483882Smlf 
86428d483882Smlf int
86438d483882Smlf devfsadm_reserve_id_cache(devlink_re_t re_array[], enumerate_file_t *head)
86448d483882Smlf {
86458d483882Smlf 	enumerate_file_t *entry;
86468d483882Smlf 	int nelem;
86478d483882Smlf 	int i;
86488d483882Smlf 	int subex;
86498d483882Smlf 	char *re;
86508d483882Smlf 	size_t size;
86518d483882Smlf 	regmatch_t *pmch;
86528d483882Smlf 
86538d483882Smlf 	/*
86548d483882Smlf 	 * Check the <RE, subexp> array passed in and compile it.
86558d483882Smlf 	 */
86568d483882Smlf 	for (i = 0; re_array[i].d_re; i++) {
86578d483882Smlf 		if (re_array[i].d_subexp == 0) {
86588d483882Smlf 			err_print("bad subexp value in RE: %s\n",
86598d483882Smlf 			    re_array[i].d_re);
86608d483882Smlf 			goto bad_re;
86618d483882Smlf 		}
86628d483882Smlf 
86638d483882Smlf 		re = re_array[i].d_re;
86648d483882Smlf 		if (regcomp(&re_array[i].d_rcomp, re, REG_EXTENDED) != 0) {
86658d483882Smlf 			err_print("reg. exp. failed to compile: %s\n", re);
86668d483882Smlf 			goto bad_re;
86678d483882Smlf 		}
86688d483882Smlf 		subex = re_array[i].d_subexp;
86698d483882Smlf 		nelem = subex + 1;
86708d483882Smlf 		re_array[i].d_pmatch = s_malloc(sizeof (regmatch_t) * nelem);
86718d483882Smlf 	}
86728d483882Smlf 
86738d483882Smlf 	entry = head ? head : enumerate_reserved;
86748d483882Smlf 	for (; entry; entry = entry->er_next) {
86758d483882Smlf 		if (entry->er_id) {
86768d483882Smlf 			vprint(RSBY_MID, "entry %s already has ID %s\n",
86778d483882Smlf 			    entry->er_file, entry->er_id);
86788d483882Smlf 			continue;
86798d483882Smlf 		}
86808d483882Smlf 		for (i = 0; re_array[i].d_re; i++) {
86818d483882Smlf 			subex = re_array[i].d_subexp;
86828d483882Smlf 			pmch = re_array[i].d_pmatch;
86838d483882Smlf 			if (regexec(&re_array[i].d_rcomp, entry->er_file,
86848d483882Smlf 			    subex + 1, pmch, 0) != 0) {
86858d483882Smlf 				/* No match */
86868d483882Smlf 				continue;
86878d483882Smlf 			}
86888d483882Smlf 			size = pmch[subex].rm_eo - pmch[subex].rm_so;
86898d483882Smlf 			entry->er_id = s_malloc(size + 1);
86908d483882Smlf 			(void) strncpy(entry->er_id,
86918d483882Smlf 			    &entry->er_file[pmch[subex].rm_so], size);
86928d483882Smlf 			entry->er_id[size] = '\0';
86938d483882Smlf 			if (head) {
86948d483882Smlf 				vprint(RSBY_MID, "devlink(%s) matches RE(%s). "
86958d483882Smlf 				    "ID is %s\n", entry->er_file,
86968d483882Smlf 				    re_array[i].d_re, entry->er_id);
86978d483882Smlf 			} else {
86988d483882Smlf 				vprint(RSBY_MID, "rsrv entry(%s) matches "
86998d483882Smlf 				    "RE(%s) ID is %s\n", entry->er_file,
87008d483882Smlf 				    re_array[i].d_re, entry->er_id);
87018d483882Smlf 			}
87028d483882Smlf 			break;
87038d483882Smlf 		}
87048d483882Smlf 	}
87058d483882Smlf 
87068d483882Smlf 	for (i = 0; re_array[i].d_re; i++) {
87078d483882Smlf 		regfree(&re_array[i].d_rcomp);
87088d483882Smlf 		assert(re_array[i].d_pmatch);
87098d483882Smlf 		free(re_array[i].d_pmatch);
87108d483882Smlf 	}
87118d483882Smlf 
87128d483882Smlf 	entry = head ? head : enumerate_reserved;
87138d483882Smlf 	for (; entry; entry = entry->er_next) {
87148d483882Smlf 		if (entry->er_id == NULL)
87158d483882Smlf 			continue;
87168d483882Smlf 		if (head) {
87178d483882Smlf 			vprint(RSBY_MID, "devlink: %s\n", entry->er_file);
87188d483882Smlf 			vprint(RSBY_MID, "ID: %s\n", entry->er_id);
87198d483882Smlf 		} else {
87208d483882Smlf 			vprint(RSBY_MID, "reserve file entry: %s\n",
87218d483882Smlf 			    entry->er_file);
87228d483882Smlf 			vprint(RSBY_MID, "reserve file id: %s\n",
87238d483882Smlf 			    entry->er_id);
87248d483882Smlf 		}
87258d483882Smlf 	}
87268d483882Smlf 
87278d483882Smlf 	return (DEVFSADM_SUCCESS);
87288d483882Smlf 
87298d483882Smlf bad_re:
87308d483882Smlf 	for (i = i-1; i >= 0; i--) {
87318d483882Smlf 		regfree(&re_array[i].d_rcomp);
87328d483882Smlf 		assert(re_array[i].d_pmatch);
87338d483882Smlf 		free(re_array[i].d_pmatch);
87348d483882Smlf 	}
87358d483882Smlf 	return (DEVFSADM_FAILURE);
87368d483882Smlf }
8737e37c6c37Scth 
8738e37c6c37Scth /*
8739e37c6c37Scth  * Return 1 if we have reserved links.
8740e37c6c37Scth  */
8741e37c6c37Scth int
8742e37c6c37Scth devfsadm_have_reserved()
8743e37c6c37Scth {
8744e37c6c37Scth 	return (enumerate_reserved ? 1 : 0);
8745e37c6c37Scth }
87468d483882Smlf 
87478d483882Smlf /*
87488d483882Smlf  * This functions errs on the side of caution. If there is any error
87498d483882Smlf  * we assume that the devlink is  *not* reserved
87508d483882Smlf  */
87518d483882Smlf int
87528d483882Smlf devfsadm_is_reserved(devlink_re_t re_array[], char *devlink)
87538d483882Smlf {
87548d483882Smlf 	int match;
87558d483882Smlf 	enumerate_file_t estruct = {NULL};
87568d483882Smlf 	enumerate_file_t *entry;
87578d483882Smlf 
87588d483882Smlf 	match = 0;
87598d483882Smlf 	estruct.er_file = devlink;
87608d483882Smlf 	estruct.er_id = NULL;
87618d483882Smlf 	estruct.er_next = NULL;
87628d483882Smlf 
87638d483882Smlf 	if (devfsadm_reserve_id_cache(re_array, &estruct) != DEVFSADM_SUCCESS) {
87648d483882Smlf 		err_print("devfsadm_is_reserved: devlink (%s) does not "
87658d483882Smlf 		    "match RE\n", devlink);
87668d483882Smlf 		return (0);
87678d483882Smlf 	}
87688d483882Smlf 	if (estruct.er_id == NULL) {
87698d483882Smlf 		err_print("devfsadm_is_reserved: ID derived from devlink %s "
87708d483882Smlf 		    "is NULL\n", devlink);
87718d483882Smlf 		return (0);
87728d483882Smlf 	}
87738d483882Smlf 
87748d483882Smlf 	entry = enumerate_reserved;
87758d483882Smlf 	for (; entry; entry = entry->er_next) {
87768d483882Smlf 		if (entry->er_id == NULL)
87778d483882Smlf 			continue;
87788d483882Smlf 		if (strcmp(entry->er_id, estruct.er_id) != 0)
87798d483882Smlf 			continue;
87808d483882Smlf 		match = 1;
87818d483882Smlf 		vprint(RSBY_MID, "reserve file entry (%s) and devlink (%s) "
87828d483882Smlf 		    "match\n", entry->er_file, devlink);
87838d483882Smlf 		break;
87848d483882Smlf 	}
87858d483882Smlf 
87868d483882Smlf 	free(estruct.er_id);
87878d483882Smlf 	return (match);
87888d483882Smlf }
8789