xref: /illumos-gate/usr/src/cmd/devfsadm/devfsadm.c (revision 055e805a)
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
5*055e805aSedp  * Common Development and Distribution License (the "License").
6*055e805aSedp  * 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  */
21*055e805aSedp 
227c478bd9Sstevel@tonic-gate /*
23*055e805aSedp  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Devfsadm replaces drvconfig, audlinks, disks, tapes, ports, devlinks
317c478bd9Sstevel@tonic-gate  * as a general purpose device administrative utility.	It creates
327c478bd9Sstevel@tonic-gate  * devices special files in /devices and logical links in /dev, and
337c478bd9Sstevel@tonic-gate  * coordinates updates to /etc/path_to_instance with the kernel.  It
347c478bd9Sstevel@tonic-gate  * operates in both command line mode to handle user or script invoked
357c478bd9Sstevel@tonic-gate  * reconfiguration updates, and operates in daemon mode to handle dynamic
367c478bd9Sstevel@tonic-gate  * reconfiguration for hotplugging support.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "devfsadm_impl.h"
407c478bd9Sstevel@tonic-gate #include <utime.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /* globals */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /* create or remove nodes or links. unset with -n */
457c478bd9Sstevel@tonic-gate static int file_mods = TRUE;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /* cleanup mode.  Set with -C */
487c478bd9Sstevel@tonic-gate static int cleanup = FALSE;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /* devlinks -d compatibility */
517c478bd9Sstevel@tonic-gate static int devlinks_debug = FALSE;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /* turn off device allocation with devfsadm -d */
547c478bd9Sstevel@tonic-gate static int devalloc_off = FALSE;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* devices to be deallocated with -d */
577c478bd9Sstevel@tonic-gate static char *devalloc[5] =
587c478bd9Sstevel@tonic-gate 	{DDI_NT_AUDIO, DDI_NT_CD, DDI_NT_FD, DDI_NT_TAPE, NULL};
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /* load a single driver only.  set with -i */
617c478bd9Sstevel@tonic-gate static int single_drv = FALSE;
627c478bd9Sstevel@tonic-gate static char *driver = NULL;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /* attempt to load drivers or defer attach nodes */
657c478bd9Sstevel@tonic-gate static int load_attach_drv = TRUE;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /* set if invoked via /usr/lib/devfsadm/devfsadmd */
687c478bd9Sstevel@tonic-gate static int daemon_mode = FALSE;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /* output directed to syslog during daemon mode if set */
717c478bd9Sstevel@tonic-gate static int logflag = FALSE;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /* build links in /dev.  -x to turn off */
747c478bd9Sstevel@tonic-gate static int build_dev = TRUE;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /* build nodes in /devices.  -y to turn off */
777c478bd9Sstevel@tonic-gate static int build_devices = TRUE;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /* -z to turn off */
807c478bd9Sstevel@tonic-gate static int flush_path_to_inst_enable = TRUE;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /* variables used for path_to_inst flushing */
837c478bd9Sstevel@tonic-gate static int inst_count = 0;
847c478bd9Sstevel@tonic-gate static mutex_t count_lock;
857c478bd9Sstevel@tonic-gate static cond_t cv;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /* variables for minor_fini calling system */
887c478bd9Sstevel@tonic-gate static int minor_fini_timeout = MINOR_FINI_TIMEOUT_DEFAULT;
897c478bd9Sstevel@tonic-gate static mutex_t minor_fini_mutex;
907c478bd9Sstevel@tonic-gate static int minor_fini_thread_created = FALSE;
917c478bd9Sstevel@tonic-gate static int minor_fini_delay_restart = FALSE;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /* single-threads /dev modification */
947c478bd9Sstevel@tonic-gate static sema_t dev_sema;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /* the program we were invoked as; ie argv[0] */
977c478bd9Sstevel@tonic-gate static char *prog;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /* pointers to create/remove link lists */
1007c478bd9Sstevel@tonic-gate static create_list_t *create_head = NULL;
1017c478bd9Sstevel@tonic-gate static remove_list_t *remove_head = NULL;
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*  supports the class -c option */
1047c478bd9Sstevel@tonic-gate static char **classes = NULL;
1057c478bd9Sstevel@tonic-gate static int num_classes = 0;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /* used with verbose option -v or -V */
1087c478bd9Sstevel@tonic-gate static int num_verbose = 0;
1097c478bd9Sstevel@tonic-gate static char **verbose = NULL;
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static struct mperm *minor_perms = NULL;
1127c478bd9Sstevel@tonic-gate static driver_alias_t *driver_aliases = NULL;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /* set if -r alternate root given */
1157c478bd9Sstevel@tonic-gate static char *root_dir = "";
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate /* /devices or <rootdir>/devices */
1187c478bd9Sstevel@tonic-gate static char *devices_dir  = DEVICES;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /* /dev or <rootdir>/dev */
1217c478bd9Sstevel@tonic-gate static char *dev_dir = DEV;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate /* /dev for the global zone */
1247c478bd9Sstevel@tonic-gate static char *global_dev_dir = DEV;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate /* /etc/path_to_inst unless -p used */
1277c478bd9Sstevel@tonic-gate static char *inst_file = INSTANCE_FILE;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /* /usr/lib/devfsadm/linkmods unless -l used */
1307c478bd9Sstevel@tonic-gate static char *module_dirs = MODULE_DIRS;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /* default uid/gid used if /etc/minor_perm entry not found */
1337c478bd9Sstevel@tonic-gate static uid_t root_uid;
1347c478bd9Sstevel@tonic-gate static gid_t sys_gid;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /* /etc/devlink.tab unless devlinks -t used */
1377c478bd9Sstevel@tonic-gate static char *devlinktab_file = NULL;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /* set if /dev link is new. speeds up rm_stale_links */
1407c478bd9Sstevel@tonic-gate static int linknew = TRUE;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate /* variables for devlink.tab compat processing */
1437c478bd9Sstevel@tonic-gate static devlinktab_list_t *devlinktab_list = NULL;
1447c478bd9Sstevel@tonic-gate static unsigned int devlinktab_line = 0;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate /* cache head for devfsadm_enumerate*() functions */
1477c478bd9Sstevel@tonic-gate static numeral_set_t *head_numeral_set = NULL;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate /* list list of devfsadm modules */
1507c478bd9Sstevel@tonic-gate static module_t *module_head = NULL;
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /* name_to_major list used in utility function */
1537c478bd9Sstevel@tonic-gate static n2m_t *n2m_list = NULL;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /* cache of some links used for performance */
1567c478bd9Sstevel@tonic-gate static linkhead_t *headlinkhead = NULL;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /* locking variables to prevent multiples writes to /dev */
1597c478bd9Sstevel@tonic-gate static int hold_dev_lock = FALSE;
1607c478bd9Sstevel@tonic-gate static int hold_daemon_lock = FALSE;
1617c478bd9Sstevel@tonic-gate static int dev_lock_fd;
1627c478bd9Sstevel@tonic-gate static int daemon_lock_fd;
1637c478bd9Sstevel@tonic-gate static char dev_lockfile[PATH_MAX + 1];
1647c478bd9Sstevel@tonic-gate static char daemon_lockfile[PATH_MAX + 1];
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /* last devinfo node/minor processed. used for performance */
1677c478bd9Sstevel@tonic-gate static di_node_t lnode;
1687c478bd9Sstevel@tonic-gate static di_minor_t lminor;
1697c478bd9Sstevel@tonic-gate static char lphy_path[PATH_MAX + 1] = {""};
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /* Globals used by the link database */
1727c478bd9Sstevel@tonic-gate static di_devlink_handle_t devlink_cache;
1737c478bd9Sstevel@tonic-gate static int update_database = FALSE;
1747c478bd9Sstevel@tonic-gate static int devlink_door_fd = -1; /* fd of devlink handler door */
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate /* Globals used to set logindev perms */
1777c478bd9Sstevel@tonic-gate static struct login_dev *login_dev_cache = NULL;
1787c478bd9Sstevel@tonic-gate static int login_dev_enable = FALSE;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate /* Global to use devinfo snapshot cache */
1817c478bd9Sstevel@tonic-gate static int use_snapshot_cache = FALSE;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /* Zone-related information */
1847c478bd9Sstevel@tonic-gate static int zone_cmd_mode = 0;
1857c478bd9Sstevel@tonic-gate static mutex_t zone_mutex; /* protects zone registration/unregistration ops */
1867c478bd9Sstevel@tonic-gate static struct zone_devinfo *zone_head;	/* linked list of zones */
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  * Packaged directories - not removed even when empty.
1907c478bd9Sstevel@tonic-gate  * The dirs must be listed in canonical form
1917c478bd9Sstevel@tonic-gate  * i.e. without leading "/dev/"
1927c478bd9Sstevel@tonic-gate  */
1937c478bd9Sstevel@tonic-gate static char *packaged_dirs[] =
1947c478bd9Sstevel@tonic-gate 	{"dsk", "rdsk", "term", NULL};
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /* RCM related globals */
1977c478bd9Sstevel@tonic-gate static void *librcm_hdl;
1987c478bd9Sstevel@tonic-gate static rcm_handle_t *rcm_hdl = NULL;
1997c478bd9Sstevel@tonic-gate static thread_t process_rcm_events_tid;
2007c478bd9Sstevel@tonic-gate static struct rcm_eventq *volatile rcm_eventq_head = NULL;
2017c478bd9Sstevel@tonic-gate static struct rcm_eventq *rcm_eventq_tail = NULL;
2027c478bd9Sstevel@tonic-gate static mutex_t rcm_eventq_lock;
2037c478bd9Sstevel@tonic-gate static cond_t rcm_eventq_cv;
2047c478bd9Sstevel@tonic-gate static volatile int need_to_exit_rcm_event_thread = 0;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate static void load_dev_acl(void);
2077c478bd9Sstevel@tonic-gate static void update_drvconf(major_t);
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate int
2117c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	struct passwd *pw;
2147c478bd9Sstevel@tonic-gate 	struct group *gp;
2157c478bd9Sstevel@tonic-gate 	pid_t pid;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2187c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == NULL) {
2217c478bd9Sstevel@tonic-gate 		prog = argv[0];
2227c478bd9Sstevel@tonic-gate 	} else {
2237c478bd9Sstevel@tonic-gate 		prog++;
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if (getuid() != 0) {
2277c478bd9Sstevel@tonic-gate 		err_print(MUST_BE_ROOT);
2287c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/*
2327c478bd9Sstevel@tonic-gate 	 * Close all files except stdin/stdout/stderr
2337c478bd9Sstevel@tonic-gate 	 */
2347c478bd9Sstevel@tonic-gate 	closefrom(3);
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
2377c478bd9Sstevel@tonic-gate 		root_uid = pw->pw_uid;
2387c478bd9Sstevel@tonic-gate 	} else {
2397c478bd9Sstevel@tonic-gate 		err_print(CANT_FIND_USER, DEFAULT_DEV_USER);
2407c478bd9Sstevel@tonic-gate 		root_uid = (uid_t)0;	/* assume 0 is root */
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/* the default group is sys */
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
2467c478bd9Sstevel@tonic-gate 		sys_gid = gp->gr_gid;
2477c478bd9Sstevel@tonic-gate 	} else {
2487c478bd9Sstevel@tonic-gate 		err_print(CANT_FIND_GROUP, DEFAULT_DEV_GROUP);
2497c478bd9Sstevel@tonic-gate 		sys_gid = (gid_t)3;	/* assume 3 is sys */
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 	(void) umask(0);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	parse_args(argc, argv);
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	(void) sema_init(&dev_sema, 1, USYNC_THREAD, NULL);
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	if (daemon_mode == TRUE) {
2597c478bd9Sstevel@tonic-gate 		/*
2607c478bd9Sstevel@tonic-gate 		 * Build /dev and /devices before daemonizing if
2617c478bd9Sstevel@tonic-gate 		 * reconfig booting and daemon invoked with alternate
2627c478bd9Sstevel@tonic-gate 		 * root. This is to support install.
2637c478bd9Sstevel@tonic-gate 		 */
2647c478bd9Sstevel@tonic-gate 		if (getenv(RECONFIG_BOOT) != NULL && root_dir[0] != '\0') {
2657c478bd9Sstevel@tonic-gate 			vprint(INFO_MID, CONFIGURING);
2667c478bd9Sstevel@tonic-gate 			load_dev_acl();
2677c478bd9Sstevel@tonic-gate 			update_drvconf((major_t)-1);
2687c478bd9Sstevel@tonic-gate 			process_devinfo_tree();
2697c478bd9Sstevel@tonic-gate 			(void) modctl(MODSETMINIROOT);
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 		/*
2737c478bd9Sstevel@tonic-gate 		 * fork before detaching from tty in order to print error
2747c478bd9Sstevel@tonic-gate 		 * message if unable to acquire file lock.  locks not preserved
2757c478bd9Sstevel@tonic-gate 		 * across forks.  Even under debug we want to fork so that
2767c478bd9Sstevel@tonic-gate 		 * when executed at boot we don't hang.
2777c478bd9Sstevel@tonic-gate 		 */
2787c478bd9Sstevel@tonic-gate 		if (fork() != 0) {
2797c478bd9Sstevel@tonic-gate 			devfsadm_exit(0);
2807c478bd9Sstevel@tonic-gate 		}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 		/* set directory to / so it coredumps there */
2837c478bd9Sstevel@tonic-gate 		if (chdir("/") == -1) {
2847c478bd9Sstevel@tonic-gate 			err_print(CHROOT_FAILED, strerror(errno));
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 		/* only one daemon can run at a time */
2887c478bd9Sstevel@tonic-gate 		if ((pid = enter_daemon_lock()) == getpid()) {
2897c478bd9Sstevel@tonic-gate 			thread_t thread;
2907c478bd9Sstevel@tonic-gate 			detachfromtty();
2917c478bd9Sstevel@tonic-gate 			(void) cond_init(&cv, USYNC_THREAD, 0);
2927c478bd9Sstevel@tonic-gate 			(void) mutex_init(&count_lock, USYNC_THREAD, 0);
2937c478bd9Sstevel@tonic-gate 			if (thr_create(NULL, NULL,
2947c478bd9Sstevel@tonic-gate 				(void *(*)(void *))instance_flush_thread,
2957c478bd9Sstevel@tonic-gate 				    NULL,
2967c478bd9Sstevel@tonic-gate 				    THR_DETACHED,
2977c478bd9Sstevel@tonic-gate 				    &thread) != 0) {
2987c478bd9Sstevel@tonic-gate 				err_print(CANT_CREATE_THREAD, "daemon",
2997c478bd9Sstevel@tonic-gate 					strerror(errno));
3007c478bd9Sstevel@tonic-gate 				devfsadm_exit(1);
3017c478bd9Sstevel@tonic-gate 			}
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 			/*
3047c478bd9Sstevel@tonic-gate 			 * No need for rcm notifications when running
3057c478bd9Sstevel@tonic-gate 			 * with an alternate root. So initialize rcm only
3067c478bd9Sstevel@tonic-gate 			 * when devfsadm is running with root dir "/".
3077c478bd9Sstevel@tonic-gate 			 * Similarly, logindevperms need only be set
3087c478bd9Sstevel@tonic-gate 			 * in daemon mode and when root dir is "/".
3097c478bd9Sstevel@tonic-gate 			 */
3107c478bd9Sstevel@tonic-gate 			if (root_dir[0] == '\0') {
3117c478bd9Sstevel@tonic-gate 				(void) rcm_init();
3127c478bd9Sstevel@tonic-gate 				login_dev_enable = TRUE;
3137c478bd9Sstevel@tonic-gate 			}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 			daemon_update();
3167c478bd9Sstevel@tonic-gate 		} else {
3177c478bd9Sstevel@tonic-gate 			err_print(DAEMON_RUNNING, pid);
3187c478bd9Sstevel@tonic-gate 			devfsadm_exit(1);
3197c478bd9Sstevel@tonic-gate 		}
3207c478bd9Sstevel@tonic-gate 		exit_daemon_lock();
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	} else {
3237c478bd9Sstevel@tonic-gate 		/* not a daemon, so just build /dev and /devices */
3247c478bd9Sstevel@tonic-gate 		process_devinfo_tree();
3257c478bd9Sstevel@tonic-gate 	}
3267c478bd9Sstevel@tonic-gate 	return (0);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate static void
3307c478bd9Sstevel@tonic-gate update_drvconf(major_t major)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	if (modctl(MODLOADDRVCONF, major) != 0)
3337c478bd9Sstevel@tonic-gate 		err_print(gettext("update_drvconf failed for major %d\n"),
3347c478bd9Sstevel@tonic-gate 		    major);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate static void
3397c478bd9Sstevel@tonic-gate load_dev_acl()
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate 	if (load_devpolicy() != 0)
3427c478bd9Sstevel@tonic-gate 		err_print(gettext("device policy load failed\n"));
3437c478bd9Sstevel@tonic-gate 	load_minor_perm_file();
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate /*
3477c478bd9Sstevel@tonic-gate  * set_zone_params sets us up to run against a specific zone.  It should
3487c478bd9Sstevel@tonic-gate  * only be called from parse_args.
3497c478bd9Sstevel@tonic-gate  */
3507c478bd9Sstevel@tonic-gate static int
3517c478bd9Sstevel@tonic-gate set_zone_params(char *zone_name)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	char zpath[MAXPATHLEN];
3547c478bd9Sstevel@tonic-gate 	zone_dochandle_t hdl;
3557c478bd9Sstevel@tonic-gate 	void *dlhdl;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	assert(daemon_mode == FALSE);
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
3607c478bd9Sstevel@tonic-gate 		err_print(INVALID_ZONE, zone_name);
3617c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
3627c478bd9Sstevel@tonic-gate 	}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	if ((dlhdl = dlopen(LIBZONECFG_PATH, RTLD_LAZY)) == NULL) {
3657c478bd9Sstevel@tonic-gate 		err_print(ZONE_LIB_MISSING);
3667c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
3707c478bd9Sstevel@tonic-gate 		err_print(ZONE_ROOTPATH_FAILED, zone_name, strerror(errno));
3717c478bd9Sstevel@tonic-gate 		(void) dlclose(dlhdl);
3727c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 	set_root_devices_dev_dir(zpath, 1);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if ((hdl = zonecfg_init_handle()) == NULL) {
3777c478bd9Sstevel@tonic-gate 		err_print(ZONE_REP_FAILED, zone_name, strerror(errno));
3787c478bd9Sstevel@tonic-gate 		(void) dlclose(dlhdl);
3797c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate 	if ((zonecfg_get_snapshot_handle(zone_name, hdl)) != Z_OK) {
3837c478bd9Sstevel@tonic-gate 		err_print(ZONE_REP_FAILED, zone_name, strerror(errno));
3847c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(hdl);
3857c478bd9Sstevel@tonic-gate 		(void) dlclose(dlhdl);
3867c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 	(void) dlclose(dlhdl);
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	zone_head = s_malloc(sizeof (struct zone_devinfo));
3917c478bd9Sstevel@tonic-gate 	zone_head->zone_path = s_strdup(zpath);
3927c478bd9Sstevel@tonic-gate 	zone_head->zone_name = s_strdup(zone_name);
3937c478bd9Sstevel@tonic-gate 	zone_head->zone_dochdl = hdl;
3947c478bd9Sstevel@tonic-gate 	zone_head->zone_next = NULL;
3957c478bd9Sstevel@tonic-gate 	zone_cmd_mode = 1;
3967c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate /*
4007c478bd9Sstevel@tonic-gate  * Parse arguments for all 6 programs handled from devfsadm.
4017c478bd9Sstevel@tonic-gate  */
4027c478bd9Sstevel@tonic-gate static void
4037c478bd9Sstevel@tonic-gate parse_args(int argc, char *argv[])
4047c478bd9Sstevel@tonic-gate {
4057c478bd9Sstevel@tonic-gate 	char opt;
4067c478bd9Sstevel@tonic-gate 	char get_linkcompat_opts = FALSE;
4077c478bd9Sstevel@tonic-gate 	char *compat_class;
4087c478bd9Sstevel@tonic-gate 	int num_aliases = 0;
4097c478bd9Sstevel@tonic-gate 	int len;
4107c478bd9Sstevel@tonic-gate 	int retval;
4117c478bd9Sstevel@tonic-gate 	int add_bind = FALSE;
4127c478bd9Sstevel@tonic-gate 	struct aliases *ap = NULL;
4137c478bd9Sstevel@tonic-gate 	struct aliases *a_head = NULL;
4147c478bd9Sstevel@tonic-gate 	struct aliases *a_tail = NULL;
4157c478bd9Sstevel@tonic-gate 	struct modconfig mc;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if (strcmp(prog, DISKS) == 0) {
4187c478bd9Sstevel@tonic-gate 		compat_class = "disk";
4197c478bd9Sstevel@tonic-gate 		get_linkcompat_opts = TRUE;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, TAPES) == 0) {
4227c478bd9Sstevel@tonic-gate 		compat_class = "tape";
4237c478bd9Sstevel@tonic-gate 		get_linkcompat_opts = TRUE;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, PORTS) == 0) {
4267c478bd9Sstevel@tonic-gate 		compat_class = "port";
4277c478bd9Sstevel@tonic-gate 		get_linkcompat_opts = TRUE;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, AUDLINKS) == 0) {
4307c478bd9Sstevel@tonic-gate 		compat_class = "audio";
4317c478bd9Sstevel@tonic-gate 		get_linkcompat_opts = TRUE;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, DEVLINKS) == 0) {
4347c478bd9Sstevel@tonic-gate 		devlinktab_file = DEVLINKTAB_FILE;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 		build_devices = FALSE;
4377c478bd9Sstevel@tonic-gate 		load_attach_drv = FALSE;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 		while ((opt = getopt(argc, argv, "dnr:st:vV:")) != EOF) {
4407c478bd9Sstevel@tonic-gate 			switch (opt) {
4417c478bd9Sstevel@tonic-gate 			case 'd':
4427c478bd9Sstevel@tonic-gate 				file_mods = FALSE;
4437c478bd9Sstevel@tonic-gate 				flush_path_to_inst_enable = FALSE;
4447c478bd9Sstevel@tonic-gate 				devlinks_debug = TRUE;
4457c478bd9Sstevel@tonic-gate 				break;
4467c478bd9Sstevel@tonic-gate 			case 'n':
4477c478bd9Sstevel@tonic-gate 				/* prevent driver loading and deferred attach */
4487c478bd9Sstevel@tonic-gate 				load_attach_drv = FALSE;
4497c478bd9Sstevel@tonic-gate 				break;
4507c478bd9Sstevel@tonic-gate 			case 'r':
4517c478bd9Sstevel@tonic-gate 				set_root_devices_dev_dir(optarg, 0);
4527c478bd9Sstevel@tonic-gate 				if (zone_pathcheck(root_dir) !=
4537c478bd9Sstevel@tonic-gate 				    DEVFSADM_SUCCESS)
4547c478bd9Sstevel@tonic-gate 					devfsadm_exit(1);
4557c478bd9Sstevel@tonic-gate 				break;
4567c478bd9Sstevel@tonic-gate 			case 's':
4577c478bd9Sstevel@tonic-gate 				/*
4587c478bd9Sstevel@tonic-gate 				 * suppress.  don't create/remove links/nodes
4597c478bd9Sstevel@tonic-gate 				 * useful with -v or -V
4607c478bd9Sstevel@tonic-gate 				 */
4617c478bd9Sstevel@tonic-gate 				file_mods = FALSE;
4627c478bd9Sstevel@tonic-gate 				flush_path_to_inst_enable = FALSE;
4637c478bd9Sstevel@tonic-gate 				break;
4647c478bd9Sstevel@tonic-gate 			case 't':
4657c478bd9Sstevel@tonic-gate 				/* supply a non-default table file */
4667c478bd9Sstevel@tonic-gate 				devlinktab_file = optarg;
4677c478bd9Sstevel@tonic-gate 				break;
4687c478bd9Sstevel@tonic-gate 			case 'v':
4697c478bd9Sstevel@tonic-gate 				/* documented verbose flag */
4707c478bd9Sstevel@tonic-gate 				add_verbose_id(VERBOSE_MID);
4717c478bd9Sstevel@tonic-gate 				break;
4727c478bd9Sstevel@tonic-gate 			case 'V':
4737c478bd9Sstevel@tonic-gate 				/* undocumented for extra verbose levels */
4747c478bd9Sstevel@tonic-gate 				add_verbose_id(optarg);
4757c478bd9Sstevel@tonic-gate 				break;
4767c478bd9Sstevel@tonic-gate 			default:
4777c478bd9Sstevel@tonic-gate 				usage();
4787c478bd9Sstevel@tonic-gate 				break;
4797c478bd9Sstevel@tonic-gate 			}
4807c478bd9Sstevel@tonic-gate 		}
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 		if (optind < argc) {
4837c478bd9Sstevel@tonic-gate 			usage();
4847c478bd9Sstevel@tonic-gate 		}
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, DRVCONFIG) == 0) {
4877c478bd9Sstevel@tonic-gate 		build_dev = FALSE;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 		while ((opt =
4907c478bd9Sstevel@tonic-gate 			getopt(argc, argv, "a:bdc:i:m:np:R:r:svV:")) != EOF) {
4917c478bd9Sstevel@tonic-gate 			switch (opt) {
4927c478bd9Sstevel@tonic-gate 			case 'a':
4937c478bd9Sstevel@tonic-gate 				ap = calloc(sizeof (struct aliases), 1);
4947c478bd9Sstevel@tonic-gate 				ap->a_name = dequote(optarg);
4957c478bd9Sstevel@tonic-gate 				len = strlen(ap->a_name) + 1;
4967c478bd9Sstevel@tonic-gate 				if (len > MAXMODCONFNAME) {
4977c478bd9Sstevel@tonic-gate 					err_print(ALIAS_TOO_LONG,
4987c478bd9Sstevel@tonic-gate 					    MAXMODCONFNAME, ap->a_name);
4997c478bd9Sstevel@tonic-gate 					devfsadm_exit(1);
5007c478bd9Sstevel@tonic-gate 				}
5017c478bd9Sstevel@tonic-gate 				ap->a_len = len;
5027c478bd9Sstevel@tonic-gate 				if (a_tail == NULL) {
5037c478bd9Sstevel@tonic-gate 					a_head = ap;
5047c478bd9Sstevel@tonic-gate 				} else {
5057c478bd9Sstevel@tonic-gate 					a_tail->a_next = ap;
5067c478bd9Sstevel@tonic-gate 				}
5077c478bd9Sstevel@tonic-gate 				a_tail = ap;
5087c478bd9Sstevel@tonic-gate 				num_aliases++;
5097c478bd9Sstevel@tonic-gate 				add_bind = TRUE;
5107c478bd9Sstevel@tonic-gate 				break;
5117c478bd9Sstevel@tonic-gate 			case 'b':
5127c478bd9Sstevel@tonic-gate 				add_bind = TRUE;
5137c478bd9Sstevel@tonic-gate 				break;
5147c478bd9Sstevel@tonic-gate 			case 'c':
5157c478bd9Sstevel@tonic-gate 				(void) strcpy(mc.drvclass, optarg);
5167c478bd9Sstevel@tonic-gate 				break;
5177c478bd9Sstevel@tonic-gate 			case 'd':
5187c478bd9Sstevel@tonic-gate 				/*
5197c478bd9Sstevel@tonic-gate 				 * need to keep for compatibility, but
5207c478bd9Sstevel@tonic-gate 				 * do nothing.
5217c478bd9Sstevel@tonic-gate 				 */
5227c478bd9Sstevel@tonic-gate 				break;
5237c478bd9Sstevel@tonic-gate 			case 'i':
5247c478bd9Sstevel@tonic-gate 				single_drv = TRUE;
5257c478bd9Sstevel@tonic-gate 				(void) strcpy(mc.drvname, optarg);
5267c478bd9Sstevel@tonic-gate 				driver = s_strdup(optarg);
5277c478bd9Sstevel@tonic-gate 				break;
5287c478bd9Sstevel@tonic-gate 			case 'm':
5297c478bd9Sstevel@tonic-gate 				mc.major = atoi(optarg);
5307c478bd9Sstevel@tonic-gate 				break;
5317c478bd9Sstevel@tonic-gate 			case 'n':
5327c478bd9Sstevel@tonic-gate 				/* prevent driver loading and deferred attach */
5337c478bd9Sstevel@tonic-gate 				load_attach_drv = FALSE;
5347c478bd9Sstevel@tonic-gate 				break;
5357c478bd9Sstevel@tonic-gate 			case 'p':
5367c478bd9Sstevel@tonic-gate 				/* specify alternate path_to_inst file */
5377c478bd9Sstevel@tonic-gate 				inst_file = s_strdup(optarg);
5387c478bd9Sstevel@tonic-gate 				break;
5397c478bd9Sstevel@tonic-gate 			case 'R':
5407c478bd9Sstevel@tonic-gate 				/*
5417c478bd9Sstevel@tonic-gate 				 * Private flag for suninstall to populate
5427c478bd9Sstevel@tonic-gate 				 * device information on the installed root.
5437c478bd9Sstevel@tonic-gate 				 */
5447c478bd9Sstevel@tonic-gate 				root_dir = s_strdup(optarg);
5457c478bd9Sstevel@tonic-gate 				if (zone_pathcheck(root_dir) !=
5467c478bd9Sstevel@tonic-gate 				    DEVFSADM_SUCCESS)
5477c478bd9Sstevel@tonic-gate 				devfsadm_exit(devfsadm_copy());
5487c478bd9Sstevel@tonic-gate 				break;
5497c478bd9Sstevel@tonic-gate 			case 'r':
5507c478bd9Sstevel@tonic-gate 				devices_dir = s_strdup(optarg);
5517c478bd9Sstevel@tonic-gate 				if (zone_pathcheck(devices_dir) !=
5527c478bd9Sstevel@tonic-gate 				    DEVFSADM_SUCCESS)
5537c478bd9Sstevel@tonic-gate 					devfsadm_exit(1);
5547c478bd9Sstevel@tonic-gate 				break;
5557c478bd9Sstevel@tonic-gate 			case 's':
5567c478bd9Sstevel@tonic-gate 				/*
5577c478bd9Sstevel@tonic-gate 				 * suppress.  don't create nodes
5587c478bd9Sstevel@tonic-gate 				 * useful with -v or -V
5597c478bd9Sstevel@tonic-gate 				 */
5607c478bd9Sstevel@tonic-gate 				file_mods = FALSE;
5617c478bd9Sstevel@tonic-gate 				flush_path_to_inst_enable = FALSE;
5627c478bd9Sstevel@tonic-gate 				break;
5637c478bd9Sstevel@tonic-gate 			case 'v':
5647c478bd9Sstevel@tonic-gate 				/* documented verbose flag */
5657c478bd9Sstevel@tonic-gate 				add_verbose_id(VERBOSE_MID);
5667c478bd9Sstevel@tonic-gate 				break;
5677c478bd9Sstevel@tonic-gate 			case 'V':
5687c478bd9Sstevel@tonic-gate 				/* undocumented for extra verbose levels */
5697c478bd9Sstevel@tonic-gate 				add_verbose_id(optarg);
5707c478bd9Sstevel@tonic-gate 				break;
5717c478bd9Sstevel@tonic-gate 			default:
5727c478bd9Sstevel@tonic-gate 				usage();
5737c478bd9Sstevel@tonic-gate 			}
5747c478bd9Sstevel@tonic-gate 		}
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 		if (optind < argc) {
5777c478bd9Sstevel@tonic-gate 			usage();
5787c478bd9Sstevel@tonic-gate 		}
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 		if ((add_bind == TRUE) && (mc.major == -1 ||
5817c478bd9Sstevel@tonic-gate 		    mc.drvname[0] == NULL)) {
5827c478bd9Sstevel@tonic-gate 			err_print(MAJOR_AND_B_FLAG);
5837c478bd9Sstevel@tonic-gate 			devfsadm_exit(1);
5847c478bd9Sstevel@tonic-gate 		}
5857c478bd9Sstevel@tonic-gate 		if (add_bind == TRUE) {
5867c478bd9Sstevel@tonic-gate 			mc.num_aliases = num_aliases;
5877c478bd9Sstevel@tonic-gate 			mc.ap = a_head;
5887c478bd9Sstevel@tonic-gate 			retval =  modctl(MODADDMAJBIND, NULL, (caddr_t)&mc);
5897c478bd9Sstevel@tonic-gate 			if (retval < 0) {
5907c478bd9Sstevel@tonic-gate 				err_print(MODCTL_ADDMAJBIND);
5917c478bd9Sstevel@tonic-gate 			}
5927c478bd9Sstevel@tonic-gate 			devfsadm_exit(retval);
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	} else if ((strcmp(prog, DEVFSADM) == 0) ||
5967c478bd9Sstevel@tonic-gate 	    (strcmp(prog, DEVFSADMD) == 0)) {
5977c478bd9Sstevel@tonic-gate 		char *zonename = NULL;
5987c478bd9Sstevel@tonic-gate 		enum zreg_op zoneop;
5997c478bd9Sstevel@tonic-gate 		int init_drvconf = 0;
6007c478bd9Sstevel@tonic-gate 		int init_perm = 0;
6017c478bd9Sstevel@tonic-gate 		int public_mode = 0;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 		if (strcmp(prog, DEVFSADMD) == 0) {
6047c478bd9Sstevel@tonic-gate 			daemon_mode = TRUE;
6057c478bd9Sstevel@tonic-gate 		}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 		devlinktab_file = DEVLINKTAB_FILE;
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 		while ((opt = getopt(argc, argv,
6107c478bd9Sstevel@tonic-gate 		    "Cc:dIi:l:np:PR:r:st:vV:x:z:Z:")) != EOF) {
6117c478bd9Sstevel@tonic-gate 			if (opt == 'I' || opt == 'P') {
6127c478bd9Sstevel@tonic-gate 				if (public_mode)
6137c478bd9Sstevel@tonic-gate 					usage();
6147c478bd9Sstevel@tonic-gate 			} else {
6157c478bd9Sstevel@tonic-gate 				if (init_perm || init_drvconf)
6167c478bd9Sstevel@tonic-gate 					usage();
6177c478bd9Sstevel@tonic-gate 				public_mode = 1;
6187c478bd9Sstevel@tonic-gate 			}
6197c478bd9Sstevel@tonic-gate 			switch (opt) {
6207c478bd9Sstevel@tonic-gate 			case 'C':
6217c478bd9Sstevel@tonic-gate 				cleanup = TRUE;
6227c478bd9Sstevel@tonic-gate 				break;
6237c478bd9Sstevel@tonic-gate 			case 'c':
6247c478bd9Sstevel@tonic-gate 				num_classes++;
6257c478bd9Sstevel@tonic-gate 				classes = s_realloc(classes, num_classes *
6267c478bd9Sstevel@tonic-gate 						    sizeof (char *));
6277c478bd9Sstevel@tonic-gate 				classes[num_classes - 1] = optarg;
6287c478bd9Sstevel@tonic-gate 				break;
6297c478bd9Sstevel@tonic-gate 			case  'd':
6307c478bd9Sstevel@tonic-gate 				if (daemon_mode == FALSE) {
6317c478bd9Sstevel@tonic-gate 					devalloc_off = TRUE;
6327c478bd9Sstevel@tonic-gate 					build_dev = FALSE;
6337c478bd9Sstevel@tonic-gate 				}
6347c478bd9Sstevel@tonic-gate 				break;
6357c478bd9Sstevel@tonic-gate 			case 'I':	/* update kernel driver.conf cache */
6367c478bd9Sstevel@tonic-gate 				if (daemon_mode == TRUE)
6377c478bd9Sstevel@tonic-gate 					usage();
6387c478bd9Sstevel@tonic-gate 				init_drvconf = 1;
6397c478bd9Sstevel@tonic-gate 				break;
6407c478bd9Sstevel@tonic-gate 			case 'i':
6417c478bd9Sstevel@tonic-gate 				single_drv = TRUE;
6427c478bd9Sstevel@tonic-gate 				driver = s_strdup(optarg);
6437c478bd9Sstevel@tonic-gate 				break;
6447c478bd9Sstevel@tonic-gate 			case 'l':
6457c478bd9Sstevel@tonic-gate 				/* specify an alternate module load path */
6467c478bd9Sstevel@tonic-gate 				module_dirs = s_strdup(optarg);
6477c478bd9Sstevel@tonic-gate 				break;
6487c478bd9Sstevel@tonic-gate 			case 'n':
6497c478bd9Sstevel@tonic-gate 				/* prevent driver loading and deferred attach */
6507c478bd9Sstevel@tonic-gate 				load_attach_drv = FALSE;
6517c478bd9Sstevel@tonic-gate 				break;
6527c478bd9Sstevel@tonic-gate 			case 'p':
6537c478bd9Sstevel@tonic-gate 				/* specify alternate path_to_inst file */
6547c478bd9Sstevel@tonic-gate 				inst_file = s_strdup(optarg);
6557c478bd9Sstevel@tonic-gate 				break;
6567c478bd9Sstevel@tonic-gate 			case 'P':
6577c478bd9Sstevel@tonic-gate 				if (daemon_mode == TRUE)
6587c478bd9Sstevel@tonic-gate 					usage();
6597c478bd9Sstevel@tonic-gate 				/* load minor_perm and device_policy */
6607c478bd9Sstevel@tonic-gate 				init_perm = 1;
6617c478bd9Sstevel@tonic-gate 				break;
6627c478bd9Sstevel@tonic-gate 			case 'R':
6637c478bd9Sstevel@tonic-gate 				/*
6647c478bd9Sstevel@tonic-gate 				 * Private flag for suninstall to populate
6657c478bd9Sstevel@tonic-gate 				 * device information on the installed root.
6667c478bd9Sstevel@tonic-gate 				 */
6677c478bd9Sstevel@tonic-gate 				root_dir = s_strdup(optarg);
6687c478bd9Sstevel@tonic-gate 				devfsadm_exit(devfsadm_copy());
6697c478bd9Sstevel@tonic-gate 				break;
6707c478bd9Sstevel@tonic-gate 			case 'r':
6717c478bd9Sstevel@tonic-gate 				set_root_devices_dev_dir(optarg, 0);
6727c478bd9Sstevel@tonic-gate 				break;
6737c478bd9Sstevel@tonic-gate 			case 's':
6747c478bd9Sstevel@tonic-gate 				/*
6757c478bd9Sstevel@tonic-gate 				 * suppress. don't create/remove links/nodes
6767c478bd9Sstevel@tonic-gate 				 * useful with -v or -V
6777c478bd9Sstevel@tonic-gate 				 */
6787c478bd9Sstevel@tonic-gate 				file_mods = FALSE;
6797c478bd9Sstevel@tonic-gate 				flush_path_to_inst_enable = FALSE;
6807c478bd9Sstevel@tonic-gate 				break;
6817c478bd9Sstevel@tonic-gate 			case 't':
6827c478bd9Sstevel@tonic-gate 				devlinktab_file = optarg;
6837c478bd9Sstevel@tonic-gate 				break;
6847c478bd9Sstevel@tonic-gate 			case 'v':
6857c478bd9Sstevel@tonic-gate 				/* documented verbose flag */
6867c478bd9Sstevel@tonic-gate 				add_verbose_id(VERBOSE_MID);
6877c478bd9Sstevel@tonic-gate 				break;
6887c478bd9Sstevel@tonic-gate 			case 'V':
6897c478bd9Sstevel@tonic-gate 				/* undocumented: specify verbose lvl */
6907c478bd9Sstevel@tonic-gate 				add_verbose_id(optarg);
6917c478bd9Sstevel@tonic-gate 				break;
6927c478bd9Sstevel@tonic-gate 			case 'x':
6937c478bd9Sstevel@tonic-gate 				/*
6947c478bd9Sstevel@tonic-gate 				 * x is the "private switch" option.  The
6957c478bd9Sstevel@tonic-gate 				 * goal is to not suck up all the other
6967c478bd9Sstevel@tonic-gate 				 * option letters.
6977c478bd9Sstevel@tonic-gate 				 */
6987c478bd9Sstevel@tonic-gate 				if (strcmp(optarg, "update_devlinksdb") == 0) {
6997c478bd9Sstevel@tonic-gate 					update_database = TRUE;
7007c478bd9Sstevel@tonic-gate 				} else if (strcmp(optarg, "no_dev") == 0) {
7017c478bd9Sstevel@tonic-gate 					/* don't build /dev */
7027c478bd9Sstevel@tonic-gate 					build_dev = FALSE;
7037c478bd9Sstevel@tonic-gate 				} else if (strcmp(optarg, "no_devices") == 0) {
7047c478bd9Sstevel@tonic-gate 					/* don't build /devices */
7057c478bd9Sstevel@tonic-gate 					build_devices = FALSE;
7067c478bd9Sstevel@tonic-gate 				} else if (strcmp(optarg, "no_p2i") == 0) {
7077c478bd9Sstevel@tonic-gate 					/* don't flush path_to_inst */
7087c478bd9Sstevel@tonic-gate 					flush_path_to_inst_enable = FALSE;
7097c478bd9Sstevel@tonic-gate 				} else if (strcmp(optarg, "use_dicache") == 0) {
7107c478bd9Sstevel@tonic-gate 					use_snapshot_cache = TRUE;
7117c478bd9Sstevel@tonic-gate 				} else {
7127c478bd9Sstevel@tonic-gate 					usage();
7137c478bd9Sstevel@tonic-gate 				}
7147c478bd9Sstevel@tonic-gate 				break;
7157c478bd9Sstevel@tonic-gate 			case 'z':
7167c478bd9Sstevel@tonic-gate 				zonename = optarg;
7177c478bd9Sstevel@tonic-gate 				zoneop = ZONE_REG;
7187c478bd9Sstevel@tonic-gate 				break;
7197c478bd9Sstevel@tonic-gate 			case 'Z':
7207c478bd9Sstevel@tonic-gate 				zonename = optarg;
7217c478bd9Sstevel@tonic-gate 				zoneop = ZONE_UNREG;
7227c478bd9Sstevel@tonic-gate 				break;
7237c478bd9Sstevel@tonic-gate 			default:
7247c478bd9Sstevel@tonic-gate 				usage();
7257c478bd9Sstevel@tonic-gate 				break;
7267c478bd9Sstevel@tonic-gate 			}
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 		}
7297c478bd9Sstevel@tonic-gate 		if (optind < argc) {
7307c478bd9Sstevel@tonic-gate 			usage();
7317c478bd9Sstevel@tonic-gate 		}
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 		/*
7347c478bd9Sstevel@tonic-gate 		 * We're not in zone mode; Check to see if the rootpath
7357c478bd9Sstevel@tonic-gate 		 * collides with any zonepaths.
7367c478bd9Sstevel@tonic-gate 		 */
7377c478bd9Sstevel@tonic-gate 		if (zonename == NULL) {
7387c478bd9Sstevel@tonic-gate 			if (zone_pathcheck(root_dir) != DEVFSADM_SUCCESS)
7397c478bd9Sstevel@tonic-gate 				devfsadm_exit(1);
7407c478bd9Sstevel@tonic-gate 		}
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		if (zonename != NULL) {
7437c478bd9Sstevel@tonic-gate 			/*
7447c478bd9Sstevel@tonic-gate 			 * -z and -Z cannot be used if we're the daemon.  The
7457c478bd9Sstevel@tonic-gate 			 * daemon always manages all zones.
7467c478bd9Sstevel@tonic-gate 			 */
7477c478bd9Sstevel@tonic-gate 			if (daemon_mode == TRUE)
7487c478bd9Sstevel@tonic-gate 				usage();
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 			/*
7517c478bd9Sstevel@tonic-gate 			 * -z and -Z are private flags, but to be paranoid we
7527c478bd9Sstevel@tonic-gate 			 * check whether they have been combined with -r.
7537c478bd9Sstevel@tonic-gate 			 */
7547c478bd9Sstevel@tonic-gate 			if (*root_dir != '\0')
7557c478bd9Sstevel@tonic-gate 				usage();
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 			if (set_zone_params(optarg) != DEVFSADM_SUCCESS)
7587c478bd9Sstevel@tonic-gate 				devfsadm_exit(1);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 			call_zone_register(zonename, zoneop);
7617c478bd9Sstevel@tonic-gate 			if (zoneop == ZONE_UNREG)
7627c478bd9Sstevel@tonic-gate 				devfsadm_exit(0);
7637c478bd9Sstevel@tonic-gate 			/*
7647c478bd9Sstevel@tonic-gate 			 * If we are in ZONE_REG mode we plow on, laying out
7657c478bd9Sstevel@tonic-gate 			 * devices for this zone.
7667c478bd9Sstevel@tonic-gate 			 */
7677c478bd9Sstevel@tonic-gate 		}
7687c478bd9Sstevel@tonic-gate 		if (init_drvconf || init_perm) {
7697c478bd9Sstevel@tonic-gate 			/*
7707c478bd9Sstevel@tonic-gate 			 * Load minor perm before force-loading drivers
7717c478bd9Sstevel@tonic-gate 			 * so the correct permissions are picked up.
7727c478bd9Sstevel@tonic-gate 			 */
7737c478bd9Sstevel@tonic-gate 			if (init_perm)
7747c478bd9Sstevel@tonic-gate 				load_dev_acl();
7757c478bd9Sstevel@tonic-gate 			if (init_drvconf)
7767c478bd9Sstevel@tonic-gate 				update_drvconf((major_t)-1);
7777c478bd9Sstevel@tonic-gate 			devfsadm_exit(0);
7787c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
7797c478bd9Sstevel@tonic-gate 		}
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	if (get_linkcompat_opts == TRUE) {
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 		build_devices = FALSE;
7867c478bd9Sstevel@tonic-gate 		load_attach_drv = FALSE;
7877c478bd9Sstevel@tonic-gate 		num_classes++;
7887c478bd9Sstevel@tonic-gate 		classes = s_realloc(classes, num_classes *
7897c478bd9Sstevel@tonic-gate 		    sizeof (char *));
7907c478bd9Sstevel@tonic-gate 		classes[num_classes - 1] = compat_class;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 		while ((opt = getopt(argc, argv, "Cnr:svV:")) != EOF) {
7937c478bd9Sstevel@tonic-gate 			switch (opt) {
7947c478bd9Sstevel@tonic-gate 			case 'C':
7957c478bd9Sstevel@tonic-gate 				cleanup = TRUE;
7967c478bd9Sstevel@tonic-gate 				break;
7977c478bd9Sstevel@tonic-gate 			case 'n':
7987c478bd9Sstevel@tonic-gate 				/* prevent driver loading or deferred attach */
7997c478bd9Sstevel@tonic-gate 				load_attach_drv = FALSE;
8007c478bd9Sstevel@tonic-gate 				break;
8017c478bd9Sstevel@tonic-gate 			case 'r':
8027c478bd9Sstevel@tonic-gate 				set_root_devices_dev_dir(optarg, 0);
8037c478bd9Sstevel@tonic-gate 				if (zone_pathcheck(root_dir) !=
8047c478bd9Sstevel@tonic-gate 				    DEVFSADM_SUCCESS)
8057c478bd9Sstevel@tonic-gate 					devfsadm_exit(1);
8067c478bd9Sstevel@tonic-gate 				break;
8077c478bd9Sstevel@tonic-gate 			case 's':
8087c478bd9Sstevel@tonic-gate 				/* suppress.  don't create/remove links/nodes */
8097c478bd9Sstevel@tonic-gate 				/* useful with -v or -V */
8107c478bd9Sstevel@tonic-gate 				file_mods = FALSE;
8117c478bd9Sstevel@tonic-gate 				flush_path_to_inst_enable = FALSE;
8127c478bd9Sstevel@tonic-gate 				break;
8137c478bd9Sstevel@tonic-gate 			case 'v':
8147c478bd9Sstevel@tonic-gate 				/* documented verbose flag */
8157c478bd9Sstevel@tonic-gate 				add_verbose_id(VERBOSE_MID);
8167c478bd9Sstevel@tonic-gate 				break;
8177c478bd9Sstevel@tonic-gate 			case 'V':
8187c478bd9Sstevel@tonic-gate 				/* undocumented for extra verbose levels */
8197c478bd9Sstevel@tonic-gate 				add_verbose_id(optarg);
8207c478bd9Sstevel@tonic-gate 				break;
8217c478bd9Sstevel@tonic-gate 			default:
8227c478bd9Sstevel@tonic-gate 				usage();
8237c478bd9Sstevel@tonic-gate 			}
8247c478bd9Sstevel@tonic-gate 		}
8257c478bd9Sstevel@tonic-gate 		if (optind < argc) {
8267c478bd9Sstevel@tonic-gate 			usage();
8277c478bd9Sstevel@tonic-gate 		}
8287c478bd9Sstevel@tonic-gate 	}
8297c478bd9Sstevel@tonic-gate }
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate void
8327c478bd9Sstevel@tonic-gate usage(void)
8337c478bd9Sstevel@tonic-gate {
8347c478bd9Sstevel@tonic-gate 	if (strcmp(prog, DEVLINKS) == 0) {
8357c478bd9Sstevel@tonic-gate 		err_print(DEVLINKS_USAGE);
8367c478bd9Sstevel@tonic-gate 	} else if (strcmp(prog, DRVCONFIG) == 0) {
8377c478bd9Sstevel@tonic-gate 		err_print(DRVCONFIG_USAGE);
8387c478bd9Sstevel@tonic-gate 	} else if ((strcmp(prog, DEVFSADM) == 0) ||
8397c478bd9Sstevel@tonic-gate 			(strcmp(prog, DEVFSADMD) == 0)) {
8407c478bd9Sstevel@tonic-gate 		err_print(DEVFSADM_USAGE);
8417c478bd9Sstevel@tonic-gate 	} else {
8427c478bd9Sstevel@tonic-gate 		err_print(COMPAT_LINK_USAGE);
8437c478bd9Sstevel@tonic-gate 	}
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	devfsadm_exit(1);
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate static void
8497c478bd9Sstevel@tonic-gate devi_tree_walk(struct dca_impl *dcip, int flags, char *ev_subclass)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate 	char *msg, *name;
8527c478bd9Sstevel@tonic-gate 	struct mlist	mlist = {0};
8537c478bd9Sstevel@tonic-gate 	di_node_t	node;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "devi_tree_walk: root=%s, minor=%s, driver=%s,"
8567c478bd9Sstevel@tonic-gate 	    " error=%d, flags=%u\n", dcip->dci_root,
8577c478bd9Sstevel@tonic-gate 	    dcip->dci_minor ? dcip->dci_minor : "<NULL>",
8587c478bd9Sstevel@tonic-gate 	    dcip->dci_driver ? dcip->dci_driver : "<NULL>", dcip->dci_error,
8597c478bd9Sstevel@tonic-gate 	    dcip->dci_flags);
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	assert(dcip->dci_root);
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	if (dcip->dci_flags & DCA_LOAD_DRV) {
8647c478bd9Sstevel@tonic-gate 		node = di_init_driver(dcip->dci_driver, flags);
8657c478bd9Sstevel@tonic-gate 		msg = DRIVER_FAILURE;
8667c478bd9Sstevel@tonic-gate 		name = dcip->dci_driver;
8677c478bd9Sstevel@tonic-gate 	} else {
8687c478bd9Sstevel@tonic-gate 		node = di_init(dcip->dci_root, flags);
8697c478bd9Sstevel@tonic-gate 		msg = DI_INIT_FAILED;
8707c478bd9Sstevel@tonic-gate 		name = dcip->dci_root;
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	if (node == DI_NODE_NIL) {
8747c478bd9Sstevel@tonic-gate 		dcip->dci_error = errno;
8757c478bd9Sstevel@tonic-gate 		/*
8767c478bd9Sstevel@tonic-gate 		 * Rapid hotplugging (commonly seen during USB testing),
8777c478bd9Sstevel@tonic-gate 		 * may remove a device before the create event for it
8787c478bd9Sstevel@tonic-gate 		 * has been processed. To prevent alarming users with
8797c478bd9Sstevel@tonic-gate 		 * a superfluous message, we suppress error messages
8807c478bd9Sstevel@tonic-gate 		 * for ENXIO and hotplug.
8817c478bd9Sstevel@tonic-gate 		 */
8827c478bd9Sstevel@tonic-gate 		if (!(errno == ENXIO && (dcip->dci_flags & DCA_HOT_PLUG)))
8837c478bd9Sstevel@tonic-gate 			err_print(msg, name, strerror(dcip->dci_error));
8847c478bd9Sstevel@tonic-gate 		return;
8857c478bd9Sstevel@tonic-gate 	}
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	if (dcip->dci_flags & DCA_FLUSH_PATHINST)
8887c478bd9Sstevel@tonic-gate 		flush_path_to_inst();
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	dcip->dci_arg = &mlist;
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "walking device tree\n");
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	(void) di_walk_minor(node, NULL, DI_CHECK_ALIAS, dcip,
8957c478bd9Sstevel@tonic-gate 	    check_minor_type);
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	process_deferred_links(dcip, DCA_CREATE_LINK);
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	dcip->dci_arg = NULL;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	/*
9027c478bd9Sstevel@tonic-gate 	 * Finished creating devfs files and dev links.
9037c478bd9Sstevel@tonic-gate 	 * Log sysevent and notify RCM.
9047c478bd9Sstevel@tonic-gate 	 */
9057c478bd9Sstevel@tonic-gate 	if (ev_subclass)
9067c478bd9Sstevel@tonic-gate 		build_and_log_event(EC_DEV_ADD, ev_subclass, dcip->dci_root,
9077c478bd9Sstevel@tonic-gate 		    node);
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	if ((dcip->dci_flags & DCA_NOTIFY_RCM) && rcm_hdl)
9107c478bd9Sstevel@tonic-gate 		(void) notify_rcm(node, dcip->dci_minor);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	di_fini(node);
9137c478bd9Sstevel@tonic-gate }
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate static void
9167c478bd9Sstevel@tonic-gate process_deferred_links(struct dca_impl *dcip, int flags)
9177c478bd9Sstevel@tonic-gate {
9187c478bd9Sstevel@tonic-gate 	struct mlist	*dep;
9197c478bd9Sstevel@tonic-gate 	struct minor	*mp, *smp;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "processing deferred links\n");
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	dep = dcip->dci_arg;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	/*
9267c478bd9Sstevel@tonic-gate 	 * The list head is not used during the deferred create phase
9277c478bd9Sstevel@tonic-gate 	 */
9287c478bd9Sstevel@tonic-gate 	dcip->dci_arg = NULL;
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	assert(dep);
9317c478bd9Sstevel@tonic-gate 	assert((dep->head == NULL) ^ (dep->tail != NULL));
9327c478bd9Sstevel@tonic-gate 	assert(flags == DCA_FREE_LIST || flags == DCA_CREATE_LINK);
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	for (smp = NULL, mp = dep->head; mp; mp = mp->next) {
9357c478bd9Sstevel@tonic-gate 		if (flags == DCA_CREATE_LINK)
9367c478bd9Sstevel@tonic-gate 			(void) check_minor_type(mp->node, mp->minor, dcip);
9377c478bd9Sstevel@tonic-gate 		free(smp);
9387c478bd9Sstevel@tonic-gate 		smp = mp;
9397c478bd9Sstevel@tonic-gate 	}
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	free(smp);
9427c478bd9Sstevel@tonic-gate }
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate /*
9457c478bd9Sstevel@tonic-gate  * Called in non-daemon mode to take a snap shot of the devinfo tree.
9467c478bd9Sstevel@tonic-gate  * Then it calls the appropriate functions to build /devices and /dev.
9477c478bd9Sstevel@tonic-gate  * It also flushes path_to_inst.
9488c4f8890Srs  * DINFOCACHE snapshot needs to be updated when devfsadm is run.
9498c4f8890Srs  * This will only happen if the flags that devfsadm uses matches the flags
9508c4f8890Srs  * that DINFOCACHE uses and that is why flags is set to
9518c4f8890Srs  * DI_CACHE_SNAPSHOT_FLAGS.
9527c478bd9Sstevel@tonic-gate  */
9537c478bd9Sstevel@tonic-gate void
9547c478bd9Sstevel@tonic-gate process_devinfo_tree()
9557c478bd9Sstevel@tonic-gate {
9568c4f8890Srs 	uint_t		flags = DI_CACHE_SNAPSHOT_FLAGS;
9577c478bd9Sstevel@tonic-gate 	struct dca_impl	dci;
9587c478bd9Sstevel@tonic-gate 	char		name[MAXNAMELEN];
9597c478bd9Sstevel@tonic-gate 	char		*fcn = "process_devinfo_tree: ";
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "%senter\n", fcn);
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 	dca_impl_init("/", NULL, &dci);
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	lock_dev();
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate 	/*
9687c478bd9Sstevel@tonic-gate 	 * Update kernel driver.conf cache when devfsadm/drvconfig
9697c478bd9Sstevel@tonic-gate 	 * is invoked to build /devices and /dev.
9707c478bd9Sstevel@tonic-gate 	 */
9717c478bd9Sstevel@tonic-gate 	if (load_attach_drv == TRUE)
9727c478bd9Sstevel@tonic-gate 		update_drvconf((major_t)-1);
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	if (single_drv == TRUE) {
9757c478bd9Sstevel@tonic-gate 		/*
9767c478bd9Sstevel@tonic-gate 		 * load a single driver, but walk the entire devinfo tree
9777c478bd9Sstevel@tonic-gate 		 */
9787c478bd9Sstevel@tonic-gate 		if (load_attach_drv == FALSE)
9797c478bd9Sstevel@tonic-gate 			err_print(DRV_LOAD_REQD);
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 		vprint(CHATTY_MID, "%sattaching driver (%s)\n", fcn, driver);
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 		dci.dci_flags |= DCA_LOAD_DRV;
9847c478bd9Sstevel@tonic-gate 		(void) snprintf(name, sizeof (name), "%s", driver);
9857c478bd9Sstevel@tonic-gate 		dci.dci_driver = name;
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	} else if (load_attach_drv == TRUE) {
9887c478bd9Sstevel@tonic-gate 		/*
9897c478bd9Sstevel@tonic-gate 		 * load and attach all drivers, then walk the entire tree.
9907c478bd9Sstevel@tonic-gate 		 * If the cache flag is set, use DINFOCACHE to get cached
9917c478bd9Sstevel@tonic-gate 		 * data.
9927c478bd9Sstevel@tonic-gate 		 */
9937c478bd9Sstevel@tonic-gate 		if (use_snapshot_cache == TRUE) {
9947c478bd9Sstevel@tonic-gate 			flags = DINFOCACHE;
9957c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "%susing snapshot cache\n", fcn);
9967c478bd9Sstevel@tonic-gate 		} else {
9977c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "%sattaching all drivers\n", fcn);
9987c478bd9Sstevel@tonic-gate 			flags |= DINFOFORCE;
9993c34adc5Sramat 			if (cleanup) {
10003c34adc5Sramat 				/*
10013c34adc5Sramat 				 * remove dangling entries from /etc/devices
10023c34adc5Sramat 				 * files.
10033c34adc5Sramat 				 */
10043c34adc5Sramat 				flags |= DINFOCLEANUP;
10053c34adc5Sramat 			}
10067c478bd9Sstevel@tonic-gate 		}
10077c478bd9Sstevel@tonic-gate 	}
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	if (((load_attach_drv == TRUE) || (single_drv == TRUE)) &&
10107c478bd9Sstevel@tonic-gate 	    (build_devices == TRUE)) {
10117c478bd9Sstevel@tonic-gate 		dci.dci_flags |= DCA_FLUSH_PATHINST;
10127c478bd9Sstevel@tonic-gate 	}
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	/* handle pre-cleanup operations desired by the modules. */
10157c478bd9Sstevel@tonic-gate 	pre_and_post_cleanup(RM_PRE);
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	devi_tree_walk(&dci, flags, NULL);
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	if (dci.dci_error) {
10207c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
10217c478bd9Sstevel@tonic-gate 	}
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 	/* handle post-cleanup operations desired by the modules. */
10247c478bd9Sstevel@tonic-gate 	pre_and_post_cleanup(RM_POST);
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	unlock_dev(SYNC_STATE);
10277c478bd9Sstevel@tonic-gate }
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10307c478bd9Sstevel@tonic-gate static void
10317c478bd9Sstevel@tonic-gate print_cache_signal(int signo)
10327c478bd9Sstevel@tonic-gate {
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	if (signal(SIGUSR1, print_cache_signal) == SIG_ERR) {
10357c478bd9Sstevel@tonic-gate 		err_print("signal SIGUSR1 failed: %s\n", strerror(errno));
10367c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
10377c478bd9Sstevel@tonic-gate 	}
10387c478bd9Sstevel@tonic-gate }
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate /*
10417c478bd9Sstevel@tonic-gate  * Register with eventd for messages. Create doors for synchronous
10427c478bd9Sstevel@tonic-gate  * link creation.
10437c478bd9Sstevel@tonic-gate  */
10447c478bd9Sstevel@tonic-gate static void
10457c478bd9Sstevel@tonic-gate daemon_update(void)
10467c478bd9Sstevel@tonic-gate {
10477c478bd9Sstevel@tonic-gate 	int fd;
10487c478bd9Sstevel@tonic-gate 	char *fcn = "daemon_update: ";
10497c478bd9Sstevel@tonic-gate 	char door_file[MAXPATHLEN];
10507c478bd9Sstevel@tonic-gate 	const char *subclass_list;
10517c478bd9Sstevel@tonic-gate 	sysevent_handle_t *sysevent_hp;
10527c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "%senter\n", fcn);
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 	if (signal(SIGUSR1, print_cache_signal) == SIG_ERR) {
10557c478bd9Sstevel@tonic-gate 		err_print("signal SIGUSR1 failed: %s\n", strerror(errno));
10567c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
10577c478bd9Sstevel@tonic-gate 	}
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	if (snprintf(door_file, sizeof (door_file),
10607c478bd9Sstevel@tonic-gate 	    "%s%s", root_dir, DEVFSADM_SERVICE_DOOR) >= sizeof (door_file)) {
10617c478bd9Sstevel@tonic-gate 		err_print("update_daemon failed to open sysevent service "
10627c478bd9Sstevel@tonic-gate 		    "door\n");
10637c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
10647c478bd9Sstevel@tonic-gate 	}
10657c478bd9Sstevel@tonic-gate 	if ((sysevent_hp = sysevent_open_channel_alt(
10667c478bd9Sstevel@tonic-gate 	    door_file)) == NULL) {
10677c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR,
10687c478bd9Sstevel@tonic-gate 			door_file, strerror(errno));
10697c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
10707c478bd9Sstevel@tonic-gate 	}
10717c478bd9Sstevel@tonic-gate 	if (sysevent_bind_subscriber(sysevent_hp, event_handler) != 0) {
10727c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR,
10737c478bd9Sstevel@tonic-gate 		    door_file, strerror(errno));
10747c478bd9Sstevel@tonic-gate 		(void) sysevent_close_channel(sysevent_hp);
10757c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
10767c478bd9Sstevel@tonic-gate 	}
10777c478bd9Sstevel@tonic-gate 	subclass_list = EC_SUB_ALL;
10787c478bd9Sstevel@tonic-gate 	if (sysevent_register_event(sysevent_hp, EC_ALL, &subclass_list, 1)
10797c478bd9Sstevel@tonic-gate 	    != 0) {
10807c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR,
10817c478bd9Sstevel@tonic-gate 		    door_file, strerror(errno));
10827c478bd9Sstevel@tonic-gate 		(void) sysevent_unbind_subscriber(sysevent_hp);
10837c478bd9Sstevel@tonic-gate 		(void) sysevent_close_channel(sysevent_hp);
10847c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
10857c478bd9Sstevel@tonic-gate 	}
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate 	if (snprintf(door_file, sizeof (door_file),
10887c478bd9Sstevel@tonic-gate 	    "%s/%s", dev_dir, ZONE_REG_DOOR) >= sizeof (door_file)) {
10897c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_ZONE_DOOR, door_file,
10907c478bd9Sstevel@tonic-gate 		    strerror(ENAMETOOLONG));
10917c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
10927c478bd9Sstevel@tonic-gate 	}
10937c478bd9Sstevel@tonic-gate 	(void) s_unlink(door_file);
10947c478bd9Sstevel@tonic-gate 	if ((fd = open(door_file, O_RDWR | O_CREAT, ZONE_DOOR_PERMS)) == -1) {
10957c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_ZONE_DOOR, door_file, strerror(errno));
10967c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
10977c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 	(void) close(fd);
10997c478bd9Sstevel@tonic-gate 	if ((fd = door_create(zone_reg_handler, NULL,
11007c478bd9Sstevel@tonic-gate 	    DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
11017c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_ZONE_DOOR, door_file, strerror(errno));
11027c478bd9Sstevel@tonic-gate 		(void) s_unlink(door_file);
11037c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
11047c478bd9Sstevel@tonic-gate 	}
11057c478bd9Sstevel@tonic-gate 	if (fattach(fd, door_file) == -1) {
11067c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_ZONE_DOOR, door_file, strerror(errno));
11077c478bd9Sstevel@tonic-gate 		(void) s_unlink(door_file);
11087c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
11097c478bd9Sstevel@tonic-gate 	}
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	(void) snprintf(door_file, sizeof (door_file), "%s/%s", dev_dir,
11127c478bd9Sstevel@tonic-gate 	    DEVFSADM_SYNCH_DOOR);
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	(void) s_unlink(door_file);
11157c478bd9Sstevel@tonic-gate 	if ((fd = open(door_file, O_RDWR | O_CREAT, SYNCH_DOOR_PERMS)) == -1) {
11167c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
11177c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
11187c478bd9Sstevel@tonic-gate 	}
11197c478bd9Sstevel@tonic-gate 	(void) close(fd);
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	if ((fd = door_create(sync_handler, NULL,
11227c478bd9Sstevel@tonic-gate 	    DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
11237c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
11247c478bd9Sstevel@tonic-gate 		(void) s_unlink(door_file);
11257c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
11267c478bd9Sstevel@tonic-gate 	}
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	if (fattach(fd, door_file) == -1) {
11297c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
11307c478bd9Sstevel@tonic-gate 		(void) s_unlink(door_file);
11317c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate 	devlink_door_fd = fd;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	/*
11367c478bd9Sstevel@tonic-gate 	 * Make sure devfsadm is managing any and all configured system zones.
11377c478bd9Sstevel@tonic-gate 	 */
11387c478bd9Sstevel@tonic-gate 	if (register_all_zones() != DEVFSADM_SUCCESS) {
11397c478bd9Sstevel@tonic-gate 		err_print(ZONE_LIST_FAILED, strerror(errno));
11407c478bd9Sstevel@tonic-gate 	}
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "%spausing\n", fcn);
11437c478bd9Sstevel@tonic-gate 	for (;;) {
11447c478bd9Sstevel@tonic-gate 		(void) pause();
11457c478bd9Sstevel@tonic-gate 	}
11467c478bd9Sstevel@tonic-gate }
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11497c478bd9Sstevel@tonic-gate static void
11507c478bd9Sstevel@tonic-gate sync_handler(void *cookie, char *ap, size_t asize,
11517c478bd9Sstevel@tonic-gate     door_desc_t *dp, uint_t ndesc)
11527c478bd9Sstevel@tonic-gate {
11537c478bd9Sstevel@tonic-gate 	door_cred_t	dcred;
11547c478bd9Sstevel@tonic-gate 	struct dca_off	*dcp, rdca;
11557c478bd9Sstevel@tonic-gate 	struct dca_impl dci;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	/*
11587c478bd9Sstevel@tonic-gate 	 * Must be root to make this call
11597c478bd9Sstevel@tonic-gate 	 * If caller is not root, don't touch its data.
11607c478bd9Sstevel@tonic-gate 	 */
11617c478bd9Sstevel@tonic-gate 	if (door_cred(&dcred) != 0 || dcred.dc_euid != 0) {
11627c478bd9Sstevel@tonic-gate 		dcp = &rdca;
11637c478bd9Sstevel@tonic-gate 		dcp->dca_error = EPERM;
11647c478bd9Sstevel@tonic-gate 		goto out;
11657c478bd9Sstevel@tonic-gate 	}
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate 	assert(ap);
11687c478bd9Sstevel@tonic-gate 	assert(asize == sizeof (*dcp));
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	dcp = (void *)ap;
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	/*
11737c478bd9Sstevel@tonic-gate 	 * Root is always present and is the first component of "name" member
11747c478bd9Sstevel@tonic-gate 	 */
11757c478bd9Sstevel@tonic-gate 	assert(dcp->dca_root == 0);
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 	/*
11787c478bd9Sstevel@tonic-gate 	 * The structure passed in by the door_client uses offsets
11797c478bd9Sstevel@tonic-gate 	 * instead of pointers to work across address space boundaries.
11807c478bd9Sstevel@tonic-gate 	 * Now copy the data into a structure (dca_impl) which uses
11817c478bd9Sstevel@tonic-gate 	 * pointers.
11827c478bd9Sstevel@tonic-gate 	 */
11837c478bd9Sstevel@tonic-gate 	dci.dci_root = &dcp->dca_name[dcp->dca_root];
11847c478bd9Sstevel@tonic-gate 	dci.dci_minor = dcp->dca_minor ? &dcp->dca_name[dcp->dca_minor] : NULL;
11857c478bd9Sstevel@tonic-gate 	dci.dci_driver =
11867c478bd9Sstevel@tonic-gate 	    dcp->dca_driver ? &dcp->dca_name[dcp->dca_driver] : NULL;
11877c478bd9Sstevel@tonic-gate 	dci.dci_error = 0;
11887c478bd9Sstevel@tonic-gate 	dci.dci_flags = dcp->dca_flags | (dci.dci_driver ? DCA_LOAD_DRV : 0);
11897c478bd9Sstevel@tonic-gate 	dci.dci_arg = NULL;
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	lock_dev();
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	devi_tree_walk(&dci, DINFOCPYALL, NULL);
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	unlock_dev(CACHE_STATE);
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate 	dcp->dca_error = dci.dci_error;
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate 	startup_cache_sync_thread();
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate out:
12027c478bd9Sstevel@tonic-gate 	(void) door_return((char *)dcp, sizeof (*dcp), NULL, 0);
12037c478bd9Sstevel@tonic-gate }
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate static void
12067c478bd9Sstevel@tonic-gate lock_dev(void)
12077c478bd9Sstevel@tonic-gate {
12087c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "lock_dev(): entered\n");
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	if (build_dev == FALSE)
12117c478bd9Sstevel@tonic-gate 		return;
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	/* lockout other threads from /dev */
12147c478bd9Sstevel@tonic-gate 	while (sema_wait(&dev_sema) != 0);
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	/*
12177c478bd9Sstevel@tonic-gate 	 * Lock out other devfsadm processes from /dev.
12187c478bd9Sstevel@tonic-gate 	 * If this wasn't the last process to run,
12197c478bd9Sstevel@tonic-gate 	 * clear caches
12207c478bd9Sstevel@tonic-gate 	 */
12217c478bd9Sstevel@tonic-gate 	if (enter_dev_lock() != getpid()) {
12227c478bd9Sstevel@tonic-gate 		invalidate_enumerate_cache();
12237c478bd9Sstevel@tonic-gate 		rm_all_links_from_cache();
12247c478bd9Sstevel@tonic-gate 		(void) di_devlink_close(&devlink_cache, DI_LINK_ERROR);
12257c478bd9Sstevel@tonic-gate 		devlink_cache = NULL;
12267c478bd9Sstevel@tonic-gate 	}
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	/*
12297c478bd9Sstevel@tonic-gate 	 * (re)load the  reverse links database if not
12307c478bd9Sstevel@tonic-gate 	 * already cached.
12317c478bd9Sstevel@tonic-gate 	 */
12327c478bd9Sstevel@tonic-gate 	if (devlink_cache == NULL)
12337c478bd9Sstevel@tonic-gate 		devlink_cache = di_devlink_open(root_dir, 0);
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	/*
12377c478bd9Sstevel@tonic-gate 	 * If modules were unloaded, reload them.  Also use module status
12387c478bd9Sstevel@tonic-gate 	 * as an indication that we should check to see if other binding
12397c478bd9Sstevel@tonic-gate 	 * files need to be reloaded.
12407c478bd9Sstevel@tonic-gate 	 */
12417c478bd9Sstevel@tonic-gate 	if (module_head == NULL) {
12427c478bd9Sstevel@tonic-gate 		load_modules();
12437c478bd9Sstevel@tonic-gate 		read_minor_perm_file();
12447c478bd9Sstevel@tonic-gate 		read_driver_aliases_file();
12457c478bd9Sstevel@tonic-gate 		read_devlinktab_file();
12467c478bd9Sstevel@tonic-gate 		read_logindevperm_file();
12477c478bd9Sstevel@tonic-gate 	}
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 	if (module_head != NULL)
12507c478bd9Sstevel@tonic-gate 		return;
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	if (strcmp(prog, DEVLINKS) == 0) {
12537c478bd9Sstevel@tonic-gate 		if (devlinktab_list == NULL) {
12547c478bd9Sstevel@tonic-gate 			err_print(NO_LINKTAB, devlinktab_file);
12557c478bd9Sstevel@tonic-gate 			err_print(NO_MODULES, module_dirs);
12567c478bd9Sstevel@tonic-gate 			err_print(ABORTING);
12577c478bd9Sstevel@tonic-gate 			devfsadm_exit(1);
12587c478bd9Sstevel@tonic-gate 		}
12597c478bd9Sstevel@tonic-gate 	} else {
12607c478bd9Sstevel@tonic-gate 		err_print(NO_MODULES, module_dirs);
12617c478bd9Sstevel@tonic-gate 		if (strcmp(prog, DEVFSADM) == 0) {
12627c478bd9Sstevel@tonic-gate 			err_print(MODIFY_PATH);
12637c478bd9Sstevel@tonic-gate 		}
12647c478bd9Sstevel@tonic-gate 	}
12657c478bd9Sstevel@tonic-gate }
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate static void
12687c478bd9Sstevel@tonic-gate unlock_dev(int flag)
12697c478bd9Sstevel@tonic-gate {
12707c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "unlock_dev(): entered\n");
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	if (build_dev == FALSE)
12737c478bd9Sstevel@tonic-gate 		return;
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	assert(devlink_cache);
12767c478bd9Sstevel@tonic-gate 	assert(flag == SYNC_STATE || flag == CACHE_STATE);
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate 	if (flag == SYNC_STATE) {
12807c478bd9Sstevel@tonic-gate 		unload_modules();
12817c478bd9Sstevel@tonic-gate 		if (update_database)
12827c478bd9Sstevel@tonic-gate 			(void) di_devlink_update(devlink_cache);
12837c478bd9Sstevel@tonic-gate 		(void) di_devlink_close(&devlink_cache, 0);
12847c478bd9Sstevel@tonic-gate 		devlink_cache = NULL;
12857c478bd9Sstevel@tonic-gate 	}
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 	exit_dev_lock();
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	(void) sema_post(&dev_sema);
12907c478bd9Sstevel@tonic-gate }
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate /*
12937c478bd9Sstevel@tonic-gate  * Contact the daemon to register the identified zone.  We do everything with
12947c478bd9Sstevel@tonic-gate  * zone names, for simplicity.
12957c478bd9Sstevel@tonic-gate  */
12967c478bd9Sstevel@tonic-gate static void
12977c478bd9Sstevel@tonic-gate call_zone_register(char *zone_name, int regop)
12987c478bd9Sstevel@tonic-gate {
12997c478bd9Sstevel@tonic-gate 	int doorfd, ret, retries = 0;
13007c478bd9Sstevel@tonic-gate 	door_arg_t arg;
13017c478bd9Sstevel@tonic-gate 	struct zreg z;
13027c478bd9Sstevel@tonic-gate 	char path[MAXPATHLEN];
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate 	assert(regop == ZONE_REG || regop == ZONE_UNREG);
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	if (strcmp(zone_name, GLOBAL_ZONENAME) == 0) {
13077c478bd9Sstevel@tonic-gate 		err_print(INVALID_ZONE, zone_name);
13087c478bd9Sstevel@tonic-gate 		return;
13097c478bd9Sstevel@tonic-gate 	}
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	z.zreg_error = 0;
13127c478bd9Sstevel@tonic-gate 	z.zreg_op = regop;
13137c478bd9Sstevel@tonic-gate 	(void) strlcpy(z.zreg_zonename, zone_name, ZONENAME_MAX);
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "/dev/%s", ZONE_REG_DOOR);
13167c478bd9Sstevel@tonic-gate 	if ((doorfd = open(path, O_RDWR)) == -1) {
13177c478bd9Sstevel@tonic-gate 		return;
13187c478bd9Sstevel@tonic-gate 	}
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	bzero(&arg, sizeof (arg));
13217c478bd9Sstevel@tonic-gate 	arg.data_ptr = (char *)&z;
13227c478bd9Sstevel@tonic-gate 	arg.data_size = sizeof (z);
13237c478bd9Sstevel@tonic-gate 	arg.rbuf = (char *)&z;
13247c478bd9Sstevel@tonic-gate 	arg.rsize = sizeof (z);
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	/*
13277c478bd9Sstevel@tonic-gate 	 * If the daemon is running, tell it about the zone.  If not, it's
13287c478bd9Sstevel@tonic-gate 	 * ok.  When it next gets run by the system (because there is
13297c478bd9Sstevel@tonic-gate 	 * device-related work to do), it will load the list of zones from
13307c478bd9Sstevel@tonic-gate 	 * the kernel.
13317c478bd9Sstevel@tonic-gate 	 */
13327c478bd9Sstevel@tonic-gate 	while (((ret = door_call(doorfd, &arg)) == -1) && retries++ < 3) {
13337c478bd9Sstevel@tonic-gate 		(void) sleep(retries);
13347c478bd9Sstevel@tonic-gate 	}
13357c478bd9Sstevel@tonic-gate 	(void) close(doorfd);
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	if (ret != 0) {
13387c478bd9Sstevel@tonic-gate 		return;
13397c478bd9Sstevel@tonic-gate 	}
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 	switch (z.zreg_error) {
13427c478bd9Sstevel@tonic-gate 	case ZONE_SUCCESS:
13437c478bd9Sstevel@tonic-gate 		break;
13447c478bd9Sstevel@tonic-gate 	case ZONE_ERR_NOZONE:
13457c478bd9Sstevel@tonic-gate 		err_print(ZONE_REG_FAILED, zone_name, strerror(z.zreg_errno));
13467c478bd9Sstevel@tonic-gate 		break;
13477c478bd9Sstevel@tonic-gate 	case ZONE_ERR_DOOR:
13487c478bd9Sstevel@tonic-gate 		err_print(ZONE_DOOR_MKFAIL, zone_name, strerror(z.zreg_errno));
13497c478bd9Sstevel@tonic-gate 		break;
13507c478bd9Sstevel@tonic-gate 	case ZONE_ERR_REPOSITORY:
13517c478bd9Sstevel@tonic-gate 		err_print(ZONE_REP_FAILED, zone_name, strerror(z.zreg_errno));
13527c478bd9Sstevel@tonic-gate 		break;
13537c478bd9Sstevel@tonic-gate 	case ZONE_ERR_NOLIB:
13547c478bd9Sstevel@tonic-gate 		err_print(ZONE_LIB_MISSING);
13557c478bd9Sstevel@tonic-gate 		break;
13567c478bd9Sstevel@tonic-gate 	default:
13577c478bd9Sstevel@tonic-gate 		err_print(ZONE_REG_FAILED, zone_name, strerror(z.zreg_errno));
13587c478bd9Sstevel@tonic-gate 		break;
13597c478bd9Sstevel@tonic-gate 	}
13607c478bd9Sstevel@tonic-gate }
13617c478bd9Sstevel@tonic-gate 
13627c478bd9Sstevel@tonic-gate /*
13637c478bd9Sstevel@tonic-gate  * The following routines are the daemon-side code for managing the set of
13647c478bd9Sstevel@tonic-gate  * currently registered zones.
13657c478bd9Sstevel@tonic-gate  *
13667c478bd9Sstevel@tonic-gate  * TODO: improve brain-dead list performance--- use libuutil avl tree or hash?
13677c478bd9Sstevel@tonic-gate  */
13687c478bd9Sstevel@tonic-gate static void
13697c478bd9Sstevel@tonic-gate zlist_insert(struct zone_devinfo *newzone)
13707c478bd9Sstevel@tonic-gate {
13717c478bd9Sstevel@tonic-gate 	struct zone_devinfo *z;
13727c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&zone_mutex));
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	if (zone_head == NULL) {
13757c478bd9Sstevel@tonic-gate 		zone_head = newzone;
13767c478bd9Sstevel@tonic-gate 		return;
13777c478bd9Sstevel@tonic-gate 	}
13787c478bd9Sstevel@tonic-gate 	z = zlist_remove(newzone->zone_name);
13797c478bd9Sstevel@tonic-gate 	if (z != NULL)
13807c478bd9Sstevel@tonic-gate 		delete_zone(z);
13817c478bd9Sstevel@tonic-gate 	newzone->zone_next = zone_head;
13827c478bd9Sstevel@tonic-gate 	zone_head = newzone;
13837c478bd9Sstevel@tonic-gate }
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate static void
13867c478bd9Sstevel@tonic-gate delete_zone(struct zone_devinfo *z) {
13877c478bd9Sstevel@tonic-gate 	char door_file[PATH_MAX];
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	/*
13907c478bd9Sstevel@tonic-gate 	 * Tidy up by withdrawing our door from the zone.
13917c478bd9Sstevel@tonic-gate 	 */
13927c478bd9Sstevel@tonic-gate 	(void) snprintf(door_file, sizeof (door_file), "%s/dev/%s",
13937c478bd9Sstevel@tonic-gate 	    z->zone_path, DEVFSADM_SYNCH_DOOR);
13947c478bd9Sstevel@tonic-gate 	(void) s_unlink(door_file);
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	zonecfg_fini_handle(z->zone_dochdl);
13977c478bd9Sstevel@tonic-gate 	free(z->zone_path);
13987c478bd9Sstevel@tonic-gate 	free(z->zone_name);
13997c478bd9Sstevel@tonic-gate 	free(z);
14007c478bd9Sstevel@tonic-gate }
14017c478bd9Sstevel@tonic-gate 
14027c478bd9Sstevel@tonic-gate static struct zone_devinfo *
14037c478bd9Sstevel@tonic-gate zlist_remove(char *zone_name)
14047c478bd9Sstevel@tonic-gate {
14057c478bd9Sstevel@tonic-gate 	struct zone_devinfo *z, *unlinked = NULL, **prevnextp;
14067c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&zone_mutex));
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	prevnextp = &zone_head;
14097c478bd9Sstevel@tonic-gate 	for (z = zone_head; z != NULL; z = z->zone_next) {
14107c478bd9Sstevel@tonic-gate 		if (strcmp(zone_name, z->zone_name) == 0) {
14117c478bd9Sstevel@tonic-gate 			unlinked = z;
14127c478bd9Sstevel@tonic-gate 			*prevnextp = z->zone_next;
14137c478bd9Sstevel@tonic-gate 			return (unlinked);
14147c478bd9Sstevel@tonic-gate 		}
14157c478bd9Sstevel@tonic-gate 		prevnextp = &(z->zone_next);
14167c478bd9Sstevel@tonic-gate 	}
14177c478bd9Sstevel@tonic-gate 	return (NULL);
14187c478bd9Sstevel@tonic-gate }
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate /*
14217c478bd9Sstevel@tonic-gate  * Delete all zones.  Note that this should *only* be called in the exit
14227c478bd9Sstevel@tonic-gate  * path of the daemon, as it does not take the zone_mutex-- this is because
14237c478bd9Sstevel@tonic-gate  * we could wind up calling devfsadm_exit() with that zone_mutex_held.
14247c478bd9Sstevel@tonic-gate  */
14257c478bd9Sstevel@tonic-gate static void
14267c478bd9Sstevel@tonic-gate zlist_deleteall_unlocked(void)
14277c478bd9Sstevel@tonic-gate {
14287c478bd9Sstevel@tonic-gate 	struct zone_devinfo *tofree;
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 	while (zone_head != NULL) {
14317c478bd9Sstevel@tonic-gate 		tofree = zone_head;
14327c478bd9Sstevel@tonic-gate 		zone_head = zone_head->zone_next;
14337c478bd9Sstevel@tonic-gate 		delete_zone(tofree);
14347c478bd9Sstevel@tonic-gate 	}
14357c478bd9Sstevel@tonic-gate 	assert(zone_head == NULL);
14367c478bd9Sstevel@tonic-gate }
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate static int
14397c478bd9Sstevel@tonic-gate zone_register(char *zone_name)
14407c478bd9Sstevel@tonic-gate {
14417c478bd9Sstevel@tonic-gate 	char		door_file[MAXPATHLEN], zpath[MAXPATHLEN];
14427c478bd9Sstevel@tonic-gate 	int		fd;
14437c478bd9Sstevel@tonic-gate 	int 		need_unlink = 0, error = ZONE_SUCCESS, myerrno = 0;
14447c478bd9Sstevel@tonic-gate 	zone_dochandle_t hdl = NULL;
14457c478bd9Sstevel@tonic-gate 	void		*dlhdl = NULL;
14467c478bd9Sstevel@tonic-gate 	struct zone_devinfo *newzone = NULL;
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&zone_mutex));
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 	if ((dlhdl = dlopen(LIBZONECFG_PATH, RTLD_LAZY)) == NULL) {
14517c478bd9Sstevel@tonic-gate 		error = ZONE_ERR_NOLIB;
14527c478bd9Sstevel@tonic-gate 		goto bad;
14537c478bd9Sstevel@tonic-gate 	}
14547c478bd9Sstevel@tonic-gate 
14557c478bd9Sstevel@tonic-gate 	if (zone_get_zonepath(zone_name, zpath, sizeof (zpath)) != Z_OK) {
14567c478bd9Sstevel@tonic-gate 		error = ZONE_ERR_NOZONE;
14577c478bd9Sstevel@tonic-gate 		myerrno = errno;
14587c478bd9Sstevel@tonic-gate 		goto bad;
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate 	if (snprintf(door_file, sizeof (door_file), "%s/dev/%s",
14627c478bd9Sstevel@tonic-gate 	    zpath, DEVFSADM_SYNCH_DOOR) >= sizeof (door_file)) {
14637c478bd9Sstevel@tonic-gate 		myerrno = ENAMETOOLONG;	/* synthesize a reasonable errno */
14647c478bd9Sstevel@tonic-gate 		error = ZONE_ERR_DOOR;
14657c478bd9Sstevel@tonic-gate 		goto bad;
14667c478bd9Sstevel@tonic-gate 	}
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	(void) s_unlink(door_file);
14697c478bd9Sstevel@tonic-gate 	if ((fd = open(door_file, O_RDWR | O_CREAT, ZONE_DOOR_PERMS)) == -1) {
14707c478bd9Sstevel@tonic-gate 		myerrno = errno;
14717c478bd9Sstevel@tonic-gate 		error = ZONE_ERR_DOOR;
14727c478bd9Sstevel@tonic-gate 		goto bad;
14737c478bd9Sstevel@tonic-gate 	}
14747c478bd9Sstevel@tonic-gate 	need_unlink = 1;
14757c478bd9Sstevel@tonic-gate 	(void) close(fd);
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 	if (fattach(devlink_door_fd, door_file) == -1) {
14787c478bd9Sstevel@tonic-gate 		error = ZONE_ERR_DOOR;
14797c478bd9Sstevel@tonic-gate 		myerrno = errno;
14807c478bd9Sstevel@tonic-gate 		goto bad;
14817c478bd9Sstevel@tonic-gate 	}
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	if ((hdl = zonecfg_init_handle()) == NULL) {
14847c478bd9Sstevel@tonic-gate 		error = ZONE_ERR_REPOSITORY;
14857c478bd9Sstevel@tonic-gate 		myerrno = errno;
14867c478bd9Sstevel@tonic-gate 		goto bad;
14877c478bd9Sstevel@tonic-gate 	}
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	if ((zonecfg_get_snapshot_handle(zone_name, hdl)) != Z_OK) {
14907c478bd9Sstevel@tonic-gate 		error = ZONE_ERR_REPOSITORY;
14917c478bd9Sstevel@tonic-gate 		myerrno = errno;
14927c478bd9Sstevel@tonic-gate 		goto bad;
14937c478bd9Sstevel@tonic-gate 	}
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	newzone = s_malloc(sizeof (struct zone_devinfo));
14967c478bd9Sstevel@tonic-gate 	newzone->zone_path = s_strdup(zpath);
14977c478bd9Sstevel@tonic-gate 	newzone->zone_name = s_strdup(zone_name);
14987c478bd9Sstevel@tonic-gate 	newzone->zone_next = NULL;
14997c478bd9Sstevel@tonic-gate 	newzone->zone_dochdl = hdl;
15007c478bd9Sstevel@tonic-gate 	zlist_insert(newzone);
15017c478bd9Sstevel@tonic-gate 	(void) dlclose(dlhdl);
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate 	return (ZONE_SUCCESS);
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate bad:
15067c478bd9Sstevel@tonic-gate 	(void) devfsadm_errprint("%s[%ld]: failed to register zone %s: %s",
15077c478bd9Sstevel@tonic-gate 	    prog, getpid(), zone_name, strerror(myerrno));
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	assert(newzone == NULL);
15107c478bd9Sstevel@tonic-gate 	if (need_unlink)
15117c478bd9Sstevel@tonic-gate 		(void) s_unlink(door_file);
15127c478bd9Sstevel@tonic-gate 	if (hdl)
15137c478bd9Sstevel@tonic-gate 		zonecfg_fini_handle(hdl);
15147c478bd9Sstevel@tonic-gate 	if (dlhdl)
15157c478bd9Sstevel@tonic-gate 		(void) dlclose(dlhdl);
15167c478bd9Sstevel@tonic-gate 	errno = myerrno;
15177c478bd9Sstevel@tonic-gate 	return (error);
15187c478bd9Sstevel@tonic-gate }
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate static int
15217c478bd9Sstevel@tonic-gate zone_unregister(char *zone_name)
15227c478bd9Sstevel@tonic-gate {
15237c478bd9Sstevel@tonic-gate 	struct zone_devinfo *z;
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&zone_mutex));
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 	if ((z = zlist_remove(zone_name)) == NULL)
15287c478bd9Sstevel@tonic-gate 		return (ZONE_ERR_NOZONE);
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	delete_zone(z);
15317c478bd9Sstevel@tonic-gate 	return (ZONE_SUCCESS);
15327c478bd9Sstevel@tonic-gate }
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate /*
15357c478bd9Sstevel@tonic-gate  *  Called by the daemon when it receives a door call to the zone registration
15367c478bd9Sstevel@tonic-gate  *  door.
15377c478bd9Sstevel@tonic-gate  */
15387c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15397c478bd9Sstevel@tonic-gate static void
15407c478bd9Sstevel@tonic-gate zone_reg_handler(void *cookie, char *ap, size_t asize, door_desc_t *dp,
15417c478bd9Sstevel@tonic-gate     uint_t ndesc)
15427c478bd9Sstevel@tonic-gate {
15437c478bd9Sstevel@tonic-gate 	door_cred_t	dcred;
15447c478bd9Sstevel@tonic-gate 	struct zreg 	*zregp, rzreg;
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate 	/*
15477c478bd9Sstevel@tonic-gate 	 * We coarsely lock the whole registration process.
15487c478bd9Sstevel@tonic-gate 	 */
15497c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&zone_mutex);
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate 	/*
15527c478bd9Sstevel@tonic-gate 	 * Must be root to make this call
15537c478bd9Sstevel@tonic-gate 	 * If caller is not root, don't touch its data.
15547c478bd9Sstevel@tonic-gate 	 */
15557c478bd9Sstevel@tonic-gate 	if (door_cred(&dcred) != 0 || dcred.dc_euid != 0) {
15567c478bd9Sstevel@tonic-gate 		zregp = &rzreg;
15577c478bd9Sstevel@tonic-gate 		zregp->zreg_error = ZONE_ERR_REPOSITORY;
15587c478bd9Sstevel@tonic-gate 		zregp->zreg_errno = EPERM;
15597c478bd9Sstevel@tonic-gate 		goto out;
15607c478bd9Sstevel@tonic-gate 	}
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	assert(ap);
15637c478bd9Sstevel@tonic-gate 	assert(asize == sizeof (*zregp));
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 	zregp = (struct zreg *)(void *)ap;
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 	/*
15687c478bd9Sstevel@tonic-gate 	 * Kernel must know about this zone; one way of discovering this
15697c478bd9Sstevel@tonic-gate 	 * is by looking up the zone id.
15707c478bd9Sstevel@tonic-gate 	 */
15717c478bd9Sstevel@tonic-gate 	if (getzoneidbyname(zregp->zreg_zonename) == -1) {
15727c478bd9Sstevel@tonic-gate 		zregp->zreg_error = ZONE_ERR_REPOSITORY;
15737c478bd9Sstevel@tonic-gate 		zregp->zreg_errno = errno;
15747c478bd9Sstevel@tonic-gate 		goto out;
15757c478bd9Sstevel@tonic-gate 	}
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	if (zregp->zreg_op == ZONE_REG) {
15787c478bd9Sstevel@tonic-gate 		zregp->zreg_error = zone_register(zregp->zreg_zonename);
15797c478bd9Sstevel@tonic-gate 		zregp->zreg_errno = errno;
15807c478bd9Sstevel@tonic-gate 	} else {
15817c478bd9Sstevel@tonic-gate 		zregp->zreg_error = zone_unregister(zregp->zreg_zonename);
15827c478bd9Sstevel@tonic-gate 		zregp->zreg_errno = errno;
15837c478bd9Sstevel@tonic-gate 	}
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate out:
15867c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&zone_mutex);
15877c478bd9Sstevel@tonic-gate 	(void) door_return((char *)zregp, sizeof (*zregp), NULL, 0);
15887c478bd9Sstevel@tonic-gate }
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate static int
15917c478bd9Sstevel@tonic-gate register_all_zones(void)
15927c478bd9Sstevel@tonic-gate {
15937c478bd9Sstevel@tonic-gate 	zoneid_t *zids = NULL;
15947c478bd9Sstevel@tonic-gate 	uint_t nzents, nzents_saved;
15957c478bd9Sstevel@tonic-gate 	int i;
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&zone_mutex);
15987c478bd9Sstevel@tonic-gate 	if (zone_list(NULL, &nzents) != 0)
15997c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate again:
16027c478bd9Sstevel@tonic-gate 	assert(zids == NULL);
16037c478bd9Sstevel@tonic-gate 	assert(MUTEX_HELD(&zone_mutex));
16047c478bd9Sstevel@tonic-gate 	if (nzents == 0) {
16057c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&zone_mutex);
16067c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate 	zids = s_zalloc(nzents * sizeof (zoneid_t));
16097c478bd9Sstevel@tonic-gate 	nzents_saved = nzents;
16107c478bd9Sstevel@tonic-gate 	if (zone_list(zids, &nzents) != 0) {
16117c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&zone_mutex);
16127c478bd9Sstevel@tonic-gate 		free(zids);
16137c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
16147c478bd9Sstevel@tonic-gate 	}
16157c478bd9Sstevel@tonic-gate 	if (nzents != nzents_saved) {
16167c478bd9Sstevel@tonic-gate 		/* list changed, try again */
16177c478bd9Sstevel@tonic-gate 		free(zids);
16187c478bd9Sstevel@tonic-gate 		zids = NULL;
16197c478bd9Sstevel@tonic-gate 		goto again;
16207c478bd9Sstevel@tonic-gate 	}
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 	assert(zids != NULL);
16237c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzents; i++) {
16247c478bd9Sstevel@tonic-gate 		char name[ZONENAME_MAX];
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 		if (zids[i] == GLOBAL_ZONEID)
16277c478bd9Sstevel@tonic-gate 			continue;
16287c478bd9Sstevel@tonic-gate 		if (getzonenamebyid(zids[i], name, sizeof (name)) >= 0)
16297c478bd9Sstevel@tonic-gate 			(void) zone_register(name);
16307c478bd9Sstevel@tonic-gate 	}
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&zone_mutex);
16337c478bd9Sstevel@tonic-gate 	free(zids);
16347c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
16357c478bd9Sstevel@tonic-gate }
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate /*
16387c478bd9Sstevel@tonic-gate  * Check that if -r is set, it is not any part of a zone--- that is, that
16397c478bd9Sstevel@tonic-gate  * the zonepath is not a substring of the root path.
16407c478bd9Sstevel@tonic-gate  */
16417c478bd9Sstevel@tonic-gate static int
16427c478bd9Sstevel@tonic-gate zone_pathcheck(char *checkpath)
16437c478bd9Sstevel@tonic-gate {
16447c478bd9Sstevel@tonic-gate 	void		*dlhdl = NULL;
16457c478bd9Sstevel@tonic-gate 	char		*name;
16467c478bd9Sstevel@tonic-gate 	char		root[MAXPATHLEN]; /* resolved devfsadm root path */
16477c478bd9Sstevel@tonic-gate 	char		zroot[MAXPATHLEN]; /* zone root path */
16487c478bd9Sstevel@tonic-gate 	char		rzroot[MAXPATHLEN]; /* resolved zone root path */
16497c478bd9Sstevel@tonic-gate 	char 		tmp[MAXPATHLEN];
16507c478bd9Sstevel@tonic-gate 	FILE		*cookie;
16517c478bd9Sstevel@tonic-gate 	int		err = DEVFSADM_SUCCESS;
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 	if (checkpath[0] == '\0')
16547c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 	/*
16577c478bd9Sstevel@tonic-gate 	 * Check if zones is available on this system.
16587c478bd9Sstevel@tonic-gate 	 */
16597c478bd9Sstevel@tonic-gate 	if ((dlhdl = dlopen(LIBZONECFG_PATH, RTLD_LAZY)) == NULL) {
16607c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
16617c478bd9Sstevel@tonic-gate 	}
16627c478bd9Sstevel@tonic-gate 
16637c478bd9Sstevel@tonic-gate 	bzero(root, sizeof (root));
16647c478bd9Sstevel@tonic-gate 	if (resolvepath(checkpath, root, sizeof (root) - 1) == -1) {
16657c478bd9Sstevel@tonic-gate 		/*
16667c478bd9Sstevel@tonic-gate 		 * In this case the user has done 'devfsadm -r' on some path
16677c478bd9Sstevel@tonic-gate 		 * which does not yet exist, or we got some other misc. error.
16687c478bd9Sstevel@tonic-gate 		 * We punt and don't resolve the path in this case.
16697c478bd9Sstevel@tonic-gate 		 */
16707c478bd9Sstevel@tonic-gate 		(void) strlcpy(root, checkpath, sizeof (root));
16717c478bd9Sstevel@tonic-gate 	}
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	if (strlen(root) > 0 && (root[strlen(root) - 1] != '/')) {
16747c478bd9Sstevel@tonic-gate 		(void) snprintf(tmp, sizeof (tmp), "%s/", root);
16757c478bd9Sstevel@tonic-gate 		(void) strlcpy(root, tmp, sizeof (root));
16767c478bd9Sstevel@tonic-gate 	}
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 	cookie = setzoneent();
16797c478bd9Sstevel@tonic-gate 	while ((name = getzoneent(cookie)) != NULL) {
16807c478bd9Sstevel@tonic-gate 		/* Skip the global zone */
16817c478bd9Sstevel@tonic-gate 		if (strcmp(name, GLOBAL_ZONENAME) == 0) {
16827c478bd9Sstevel@tonic-gate 			free(name);
16837c478bd9Sstevel@tonic-gate 			continue;
16847c478bd9Sstevel@tonic-gate 		}
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 		if (zone_get_zonepath(name, zroot, sizeof (zroot)) != Z_OK) {
16877c478bd9Sstevel@tonic-gate 			free(name);
16887c478bd9Sstevel@tonic-gate 			continue;
16897c478bd9Sstevel@tonic-gate 		}
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate 		bzero(rzroot, sizeof (rzroot));
16927c478bd9Sstevel@tonic-gate 		if (resolvepath(zroot, rzroot, sizeof (rzroot) - 1) == -1) {
16937c478bd9Sstevel@tonic-gate 			/*
16947c478bd9Sstevel@tonic-gate 			 * Zone path doesn't exist, or other misc error,
16957c478bd9Sstevel@tonic-gate 			 * so we try using the non-resolved pathname.
16967c478bd9Sstevel@tonic-gate 			 */
16977c478bd9Sstevel@tonic-gate 			(void) strlcpy(rzroot, zroot, sizeof (rzroot));
16987c478bd9Sstevel@tonic-gate 		}
16997c478bd9Sstevel@tonic-gate 		if (strlen(rzroot) > 0 && (rzroot[strlen(rzroot) - 1] != '/')) {
17007c478bd9Sstevel@tonic-gate 			(void) snprintf(tmp, sizeof (tmp), "%s/", rzroot);
17017c478bd9Sstevel@tonic-gate 			(void) strlcpy(rzroot, tmp, sizeof (rzroot));
17027c478bd9Sstevel@tonic-gate 		}
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 		/*
17057c478bd9Sstevel@tonic-gate 		 * Finally, the comparison.  If the zone root path is a
17067c478bd9Sstevel@tonic-gate 		 * leading substring of the root path, fail.
17077c478bd9Sstevel@tonic-gate 		 */
17087c478bd9Sstevel@tonic-gate 		if (strncmp(rzroot, root, strlen(rzroot)) == 0) {
17097c478bd9Sstevel@tonic-gate 			err_print(ZONE_PATHCHECK, root, name);
17107c478bd9Sstevel@tonic-gate 			err = DEVFSADM_FAILURE;
17117c478bd9Sstevel@tonic-gate 			free(name);
17127c478bd9Sstevel@tonic-gate 			break;
17137c478bd9Sstevel@tonic-gate 		}
17147c478bd9Sstevel@tonic-gate 		free(name);
17157c478bd9Sstevel@tonic-gate 	}
17167c478bd9Sstevel@tonic-gate 	endzoneent(cookie);
17177c478bd9Sstevel@tonic-gate 	(void) dlclose(dlhdl);
17187c478bd9Sstevel@tonic-gate 	return (err);
17197c478bd9Sstevel@tonic-gate }
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate /*
17227c478bd9Sstevel@tonic-gate  *  Called by the daemon when it receives an event from the devfsadm SLM
17237c478bd9Sstevel@tonic-gate  *  to syseventd.
17247c478bd9Sstevel@tonic-gate  *
17257c478bd9Sstevel@tonic-gate  *  The devfsadm SLM uses a private event channel for communication to
17267c478bd9Sstevel@tonic-gate  *  devfsadmd set-up via private libsysevent interfaces.  This handler is
17277c478bd9Sstevel@tonic-gate  *  used to bind to the devfsadmd channel for event delivery.
17287c478bd9Sstevel@tonic-gate  *  The devfsadmd SLM insures single calls to this routine as well as
17297c478bd9Sstevel@tonic-gate  *  synchronized event delivery.
17307c478bd9Sstevel@tonic-gate  *
17317c478bd9Sstevel@tonic-gate  */
17327c478bd9Sstevel@tonic-gate static void
17337c478bd9Sstevel@tonic-gate event_handler(sysevent_t *ev)
17347c478bd9Sstevel@tonic-gate {
17357c478bd9Sstevel@tonic-gate 	char *path;
17367c478bd9Sstevel@tonic-gate 	char *minor;
17377c478bd9Sstevel@tonic-gate 	char *subclass;
17387c478bd9Sstevel@tonic-gate 	char *dev_ev_subclass;
17397c478bd9Sstevel@tonic-gate 	char *driver_name;
17407c478bd9Sstevel@tonic-gate 	nvlist_t *attr_list = NULL;
17417c478bd9Sstevel@tonic-gate 	int err = 0;
17427c478bd9Sstevel@tonic-gate 	int instance;
17437c478bd9Sstevel@tonic-gate 	int branch_event = 0;
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 	subclass = sysevent_get_subclass_name(ev);
17467c478bd9Sstevel@tonic-gate 	vprint(EVENT_MID, "event_handler: %s id:0X%llx\n",
17477c478bd9Sstevel@tonic-gate 	    subclass, sysevent_get_seq(ev));
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 	/* Check if event is an instance modification */
17507c478bd9Sstevel@tonic-gate 	if (strcmp(subclass, ESC_DEVFS_INSTANCE_MOD) == 0) {
17517c478bd9Sstevel@tonic-gate 		devfs_instance_mod();
17527c478bd9Sstevel@tonic-gate 		return;
17537c478bd9Sstevel@tonic-gate 	}
17547c478bd9Sstevel@tonic-gate 	if (sysevent_get_attr_list(ev, &attr_list) != 0) {
17557c478bd9Sstevel@tonic-gate 		vprint(EVENT_MID, "event_handler: can not get attr list\n");
17567c478bd9Sstevel@tonic-gate 		return;
17577c478bd9Sstevel@tonic-gate 	}
17587c478bd9Sstevel@tonic-gate 
17597c478bd9Sstevel@tonic-gate 	if (strcmp(subclass, ESC_DEVFS_DEVI_ADD) == 0 ||
17607c478bd9Sstevel@tonic-gate 	    strcmp(subclass, ESC_DEVFS_DEVI_REMOVE) == 0 ||
17617c478bd9Sstevel@tonic-gate 	    strcmp(subclass, ESC_DEVFS_MINOR_CREATE) == 0 ||
17627c478bd9Sstevel@tonic-gate 	    strcmp(subclass, ESC_DEVFS_MINOR_REMOVE) == 0) {
17637c478bd9Sstevel@tonic-gate 		if ((err = nvlist_lookup_string(attr_list, DEVFS_PATHNAME,
17647c478bd9Sstevel@tonic-gate 		    &path)) != 0)
17657c478bd9Sstevel@tonic-gate 			goto out;
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 		if (strcmp(subclass, ESC_DEVFS_DEVI_ADD) == 0 ||
17687c478bd9Sstevel@tonic-gate 		    strcmp(subclass, ESC_DEVFS_DEVI_REMOVE) == 0) {
17697c478bd9Sstevel@tonic-gate 			if (nvlist_lookup_string(attr_list, DEVFS_DEVI_CLASS,
17707c478bd9Sstevel@tonic-gate 			    &dev_ev_subclass) != 0)
17717c478bd9Sstevel@tonic-gate 				dev_ev_subclass = NULL;
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 			if (nvlist_lookup_string(attr_list, DEVFS_DRIVER_NAME,
17747c478bd9Sstevel@tonic-gate 			    &driver_name) != 0)
17757c478bd9Sstevel@tonic-gate 				driver_name = NULL;
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate 			if (nvlist_lookup_int32(attr_list, DEVFS_INSTANCE,
17787c478bd9Sstevel@tonic-gate 			    &instance) != 0)
17797c478bd9Sstevel@tonic-gate 				instance = -1;
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 			if (nvlist_lookup_int32(attr_list, DEVFS_BRANCH_EVENT,
17827c478bd9Sstevel@tonic-gate 			    &branch_event) != 0)
17837c478bd9Sstevel@tonic-gate 				branch_event = 0;
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 		} else {
17867c478bd9Sstevel@tonic-gate 			if (nvlist_lookup_string(attr_list, DEVFS_MINOR_NAME,
17877c478bd9Sstevel@tonic-gate 			    &minor) != 0)
17887c478bd9Sstevel@tonic-gate 				minor = NULL;
17897c478bd9Sstevel@tonic-gate 		}
17907c478bd9Sstevel@tonic-gate 
17917c478bd9Sstevel@tonic-gate 		lock_dev();
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 		if (strcmp(ESC_DEVFS_DEVI_ADD, subclass) == 0) {
17947c478bd9Sstevel@tonic-gate 			add_minor_pathname(path, NULL, dev_ev_subclass);
17957c478bd9Sstevel@tonic-gate 			if (branch_event) {
17967c478bd9Sstevel@tonic-gate 				build_and_log_event(EC_DEV_BRANCH,
17977c478bd9Sstevel@tonic-gate 				    ESC_DEV_BRANCH_ADD, path, DI_NODE_NIL);
17987c478bd9Sstevel@tonic-gate 			}
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 		} else if (strcmp(ESC_DEVFS_MINOR_CREATE, subclass) == 0) {
18017c478bd9Sstevel@tonic-gate 			add_minor_pathname(path, minor, NULL);
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 		} else if (strcmp(ESC_DEVFS_MINOR_REMOVE, subclass) == 0) {
18047c478bd9Sstevel@tonic-gate 			hot_cleanup(path, minor, NULL, NULL, -1);
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 		} else { /* ESC_DEVFS_DEVI_REMOVE */
18077c478bd9Sstevel@tonic-gate 			hot_cleanup(path, NULL, dev_ev_subclass,
18087c478bd9Sstevel@tonic-gate 			    driver_name, instance);
18097c478bd9Sstevel@tonic-gate 			if (branch_event) {
18107c478bd9Sstevel@tonic-gate 				build_and_log_event(EC_DEV_BRANCH,
18117c478bd9Sstevel@tonic-gate 				    ESC_DEV_BRANCH_REMOVE, path, DI_NODE_NIL);
18127c478bd9Sstevel@tonic-gate 			}
18137c478bd9Sstevel@tonic-gate 		}
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 		unlock_dev(CACHE_STATE);
18167c478bd9Sstevel@tonic-gate 		startup_cache_sync_thread();
18177c478bd9Sstevel@tonic-gate 
18187c478bd9Sstevel@tonic-gate 	} else if (strcmp(subclass, ESC_DEVFS_BRANCH_ADD) == 0 ||
18197c478bd9Sstevel@tonic-gate 	    strcmp(subclass, ESC_DEVFS_BRANCH_REMOVE) == 0) {
18207c478bd9Sstevel@tonic-gate 		if ((err = nvlist_lookup_string(attr_list,
18217c478bd9Sstevel@tonic-gate 		    DEVFS_PATHNAME, &path)) != 0)
18227c478bd9Sstevel@tonic-gate 			goto out;
18237c478bd9Sstevel@tonic-gate 
18247c478bd9Sstevel@tonic-gate 		/* just log ESC_DEV_BRANCH... event */
18257c478bd9Sstevel@tonic-gate 		if (strcmp(subclass, ESC_DEVFS_BRANCH_ADD) == 0)
18267c478bd9Sstevel@tonic-gate 			dev_ev_subclass = ESC_DEV_BRANCH_ADD;
18277c478bd9Sstevel@tonic-gate 		else
18287c478bd9Sstevel@tonic-gate 			dev_ev_subclass = ESC_DEV_BRANCH_REMOVE;
18297c478bd9Sstevel@tonic-gate 
1830d2596142Scth 		lock_dev();
18317c478bd9Sstevel@tonic-gate 		build_and_log_event(EC_DEV_BRANCH, dev_ev_subclass, path,
18327c478bd9Sstevel@tonic-gate 		    DI_NODE_NIL);
1833d2596142Scth 		unlock_dev(CACHE_STATE);
1834d2596142Scth 		startup_cache_sync_thread();
18357c478bd9Sstevel@tonic-gate 
18367c478bd9Sstevel@tonic-gate 	} else
18377c478bd9Sstevel@tonic-gate 		err_print(UNKNOWN_EVENT, subclass);
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate out:
18407c478bd9Sstevel@tonic-gate 	if (err)
18417c478bd9Sstevel@tonic-gate 		err_print(EVENT_ATTR_LOOKUP_FAILED, strerror(err));
18427c478bd9Sstevel@tonic-gate 	nvlist_free(attr_list);
18437c478bd9Sstevel@tonic-gate }
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate static void
18467c478bd9Sstevel@tonic-gate dca_impl_init(char *root, char *minor, struct dca_impl *dcip)
18477c478bd9Sstevel@tonic-gate {
18487c478bd9Sstevel@tonic-gate 	assert(root);
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 	dcip->dci_root = root;
18517c478bd9Sstevel@tonic-gate 	dcip->dci_minor = minor;
18527c478bd9Sstevel@tonic-gate 	dcip->dci_driver = NULL;
18537c478bd9Sstevel@tonic-gate 	dcip->dci_error = 0;
18547c478bd9Sstevel@tonic-gate 	dcip->dci_flags = 0;
18557c478bd9Sstevel@tonic-gate 	dcip->dci_arg = NULL;
18567c478bd9Sstevel@tonic-gate }
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate /*
18597c478bd9Sstevel@tonic-gate  *  Kernel logs a message when a devinfo node is attached.  Try to create
18607c478bd9Sstevel@tonic-gate  *  /dev and /devices for each minor node.  minorname can be NULL.
18617c478bd9Sstevel@tonic-gate  */
18627c478bd9Sstevel@tonic-gate void
18637c478bd9Sstevel@tonic-gate add_minor_pathname(char *node, char *minor, char *ev_subclass)
18647c478bd9Sstevel@tonic-gate {
18657c478bd9Sstevel@tonic-gate 	struct dca_impl	dci;
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "add_minor_pathname: node_path=%s minor=%s\n",
18687c478bd9Sstevel@tonic-gate 	    node, minor ? minor : "NULL");
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 	dca_impl_init(node, minor, &dci);
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	/*
18737c478bd9Sstevel@tonic-gate 	 * Restrict hotplug link creation if daemon
18747c478bd9Sstevel@tonic-gate 	 * started  with -i option.
18757c478bd9Sstevel@tonic-gate 	 */
18767c478bd9Sstevel@tonic-gate 	if (single_drv == TRUE) {
18777c478bd9Sstevel@tonic-gate 		dci.dci_driver = driver;
18787c478bd9Sstevel@tonic-gate 	}
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	/*
18817c478bd9Sstevel@tonic-gate 	 * We are being invoked in response to a hotplug
18827c478bd9Sstevel@tonic-gate 	 * event. Also, notify RCM if nodetype indicates
18837c478bd9Sstevel@tonic-gate 	 * a network device has been hotplugged.
18847c478bd9Sstevel@tonic-gate 	 */
18857c478bd9Sstevel@tonic-gate 	dci.dci_flags = DCA_HOT_PLUG | DCA_CHECK_TYPE;
18867c478bd9Sstevel@tonic-gate 
18877c478bd9Sstevel@tonic-gate 	devi_tree_walk(&dci, DINFOPROP|DINFOMINOR, ev_subclass);
18887c478bd9Sstevel@tonic-gate }
18897c478bd9Sstevel@tonic-gate 
18907c478bd9Sstevel@tonic-gate static di_node_t
18917c478bd9Sstevel@tonic-gate find_clone_node()
18927c478bd9Sstevel@tonic-gate {
18937c478bd9Sstevel@tonic-gate 	static di_node_t clone_node = DI_NODE_NIL;
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate 	if (clone_node == DI_NODE_NIL)
18967c478bd9Sstevel@tonic-gate 		clone_node = di_init("/pseudo/clone@0", DINFOPROP);
18977c478bd9Sstevel@tonic-gate 	return (clone_node);
18987c478bd9Sstevel@tonic-gate }
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate static int
19017c478bd9Sstevel@tonic-gate is_descendent_of(di_node_t node, char *driver)
19027c478bd9Sstevel@tonic-gate {
19037c478bd9Sstevel@tonic-gate 	while (node != DI_NODE_NIL) {
19047c478bd9Sstevel@tonic-gate 		char *drv = di_driver_name(node);
19057c478bd9Sstevel@tonic-gate 		if (strcmp(drv, driver) == 0)
19067c478bd9Sstevel@tonic-gate 			return (1);
19077c478bd9Sstevel@tonic-gate 		node = di_parent_node(node);
19087c478bd9Sstevel@tonic-gate 	}
19097c478bd9Sstevel@tonic-gate 	return (0);
19107c478bd9Sstevel@tonic-gate }
19117c478bd9Sstevel@tonic-gate 
19127c478bd9Sstevel@tonic-gate /*
19137c478bd9Sstevel@tonic-gate  * Checks the minor type.  If it is an alias node, then lookup
19147c478bd9Sstevel@tonic-gate  * the real node/minor first, then call minor_process() to
19157c478bd9Sstevel@tonic-gate  * do the real work.
19167c478bd9Sstevel@tonic-gate  */
19177c478bd9Sstevel@tonic-gate static int
19187c478bd9Sstevel@tonic-gate check_minor_type(di_node_t node, di_minor_t minor, void *arg)
19197c478bd9Sstevel@tonic-gate {
19207c478bd9Sstevel@tonic-gate 	ddi_minor_type	minor_type;
19217c478bd9Sstevel@tonic-gate 	di_node_t	clone_node;
19227c478bd9Sstevel@tonic-gate 	char		*mn;
19237c478bd9Sstevel@tonic-gate 	char		*nt;
19247c478bd9Sstevel@tonic-gate 	struct mlist	*dep;
19257c478bd9Sstevel@tonic-gate 	struct dca_impl	*dcip = arg;
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 	assert(dcip);
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 	dep = dcip->dci_arg;
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 	mn = di_minor_name(minor);
19327c478bd9Sstevel@tonic-gate 
19337c478bd9Sstevel@tonic-gate 	/*
19347c478bd9Sstevel@tonic-gate 	 * We match driver here instead of in minor_process
19357c478bd9Sstevel@tonic-gate 	 * as we want the actual driver name. This check is
19367c478bd9Sstevel@tonic-gate 	 * unnecessary during deferred processing.
19377c478bd9Sstevel@tonic-gate 	 */
19387c478bd9Sstevel@tonic-gate 	if (dep &&
19397c478bd9Sstevel@tonic-gate 	    ((dcip->dci_driver && !is_descendent_of(node, dcip->dci_driver)) ||
19407c478bd9Sstevel@tonic-gate 	    (dcip->dci_minor && strcmp(mn, dcip->dci_minor)))) {
19417c478bd9Sstevel@tonic-gate 		return (DI_WALK_CONTINUE);
19427c478bd9Sstevel@tonic-gate 	}
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	if ((dcip->dci_flags & DCA_CHECK_TYPE) &&
19457c478bd9Sstevel@tonic-gate 	    (nt = di_minor_nodetype(minor)) &&
1946210db224Sericheng 	    (strcmp(nt, DDI_NT_NET) == 0)) {
19477c478bd9Sstevel@tonic-gate 		dcip->dci_flags |= DCA_NOTIFY_RCM;
19487c478bd9Sstevel@tonic-gate 		dcip->dci_flags &= ~DCA_CHECK_TYPE;
19497c478bd9Sstevel@tonic-gate 	}
19507c478bd9Sstevel@tonic-gate 
19517c478bd9Sstevel@tonic-gate 	minor_type = di_minor_type(minor);
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	if (minor_type == DDM_MINOR) {
19547c478bd9Sstevel@tonic-gate 		minor_process(node, minor, dep);
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	} else if (minor_type == DDM_ALIAS) {
19577c478bd9Sstevel@tonic-gate 		struct mlist *cdep, clone_del = {0};
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 		clone_node = find_clone_node();
19607c478bd9Sstevel@tonic-gate 		if (clone_node == DI_NODE_NIL) {
19617c478bd9Sstevel@tonic-gate 			err_print(DI_INIT_FAILED, "clone", strerror(errno));
19627c478bd9Sstevel@tonic-gate 			return (DI_WALK_CONTINUE);
19637c478bd9Sstevel@tonic-gate 		}
19647c478bd9Sstevel@tonic-gate 
19657c478bd9Sstevel@tonic-gate 		cdep = dep ? &clone_del : NULL;
19667c478bd9Sstevel@tonic-gate 
19677c478bd9Sstevel@tonic-gate 		minor_process(clone_node, minor, cdep);
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 		/*
19707c478bd9Sstevel@tonic-gate 		 * cache "alias" minor node and free "clone" minor
19717c478bd9Sstevel@tonic-gate 		 */
19727c478bd9Sstevel@tonic-gate 		if (cdep != NULL && cdep->head != NULL) {
19737c478bd9Sstevel@tonic-gate 			assert(cdep->tail != NULL);
19747c478bd9Sstevel@tonic-gate 			cache_deferred_minor(dep, node, minor);
19757c478bd9Sstevel@tonic-gate 			dcip->dci_arg = cdep;
19767c478bd9Sstevel@tonic-gate 			process_deferred_links(dcip, DCA_FREE_LIST);
19777c478bd9Sstevel@tonic-gate 			dcip->dci_arg = dep;
19787c478bd9Sstevel@tonic-gate 		}
19797c478bd9Sstevel@tonic-gate 	}
19807c478bd9Sstevel@tonic-gate 
19817c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
19827c478bd9Sstevel@tonic-gate }
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 
19857c478bd9Sstevel@tonic-gate /*
19867c478bd9Sstevel@tonic-gate  *  This is the entry point for each minor node, whether walking
19877c478bd9Sstevel@tonic-gate  *  the entire tree via di_walk_minor() or processing a hotplug event
19887c478bd9Sstevel@tonic-gate  *  for a single devinfo node (via hotplug ndi_devi_online()).
19897c478bd9Sstevel@tonic-gate  */
19907c478bd9Sstevel@tonic-gate /*ARGSUSED*/
19917c478bd9Sstevel@tonic-gate static void
19927c478bd9Sstevel@tonic-gate minor_process(di_node_t node, di_minor_t minor, struct mlist *dep)
19937c478bd9Sstevel@tonic-gate {
19947c478bd9Sstevel@tonic-gate 	create_list_t	*create;
19957c478bd9Sstevel@tonic-gate 	int		defer;
19967c478bd9Sstevel@tonic-gate 
19977c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "minor_process: node=%s, minor=%s\n",
19987c478bd9Sstevel@tonic-gate 		di_node_name(node), di_minor_name(minor));
19997c478bd9Sstevel@tonic-gate 
20007c478bd9Sstevel@tonic-gate 	if (dep != NULL) {
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate 		/*
20037c478bd9Sstevel@tonic-gate 		 * Reset /devices node to minor_perm perm/ownership
20047c478bd9Sstevel@tonic-gate 		 * if we are here to deactivate device allocation
20057c478bd9Sstevel@tonic-gate 		 */
20067c478bd9Sstevel@tonic-gate 		if (build_devices == TRUE) {
20077c478bd9Sstevel@tonic-gate 			reset_node_permissions(node, minor);
20087c478bd9Sstevel@tonic-gate 		}
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate 		if (build_dev == FALSE) {
20117c478bd9Sstevel@tonic-gate 			return;
20127c478bd9Sstevel@tonic-gate 		}
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate 		/*
20157c478bd9Sstevel@tonic-gate 		 * This function will create any nodes for /etc/devlink.tab.
20167c478bd9Sstevel@tonic-gate 		 * If devlink.tab handles link creation, we don't call any
20177c478bd9Sstevel@tonic-gate 		 * devfsadm modules since that could cause duplicate caching
20187c478bd9Sstevel@tonic-gate 		 * in the enumerate functions if different re strings are
20197c478bd9Sstevel@tonic-gate 		 * passed that are logically identical.  I'm still not
20207c478bd9Sstevel@tonic-gate 		 * convinced this would cause any harm, but better to be safe.
20217c478bd9Sstevel@tonic-gate 		 *
20227c478bd9Sstevel@tonic-gate 		 * Deferred processing is available only for devlinks
20237c478bd9Sstevel@tonic-gate 		 * created through devfsadm modules.
20247c478bd9Sstevel@tonic-gate 		 */
20257c478bd9Sstevel@tonic-gate 		if (process_devlink_compat(minor, node) == TRUE) {
20267c478bd9Sstevel@tonic-gate 			return;
20277c478bd9Sstevel@tonic-gate 		}
20287c478bd9Sstevel@tonic-gate 	} else {
20297c478bd9Sstevel@tonic-gate 		vprint(CHATTY_MID, "minor_process: deferred processing\n");
20307c478bd9Sstevel@tonic-gate 	}
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 	/*
20337c478bd9Sstevel@tonic-gate 	 * look for relevant link create rules in the modules, and
20347c478bd9Sstevel@tonic-gate 	 * invoke the link create callback function to build a link
20357c478bd9Sstevel@tonic-gate 	 * if there is a match.
20367c478bd9Sstevel@tonic-gate 	 */
20377c478bd9Sstevel@tonic-gate 	defer = 0;
20387c478bd9Sstevel@tonic-gate 	for (create = create_head; create != NULL; create = create->next) {
20397c478bd9Sstevel@tonic-gate 		if ((minor_matches_rule(node, minor, create) == TRUE) &&
20407c478bd9Sstevel@tonic-gate 		    class_ok(create->create->device_class) ==
20417c478bd9Sstevel@tonic-gate 		    DEVFSADM_SUCCESS) {
20427c478bd9Sstevel@tonic-gate 			if (call_minor_init(create->modptr) ==
20437c478bd9Sstevel@tonic-gate 			    DEVFSADM_FAILURE) {
20447c478bd9Sstevel@tonic-gate 				continue;
20457c478bd9Sstevel@tonic-gate 			}
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 			/*
20487c478bd9Sstevel@tonic-gate 			 * If NOT doing the deferred creates (i.e. 1st pass) and
20497c478bd9Sstevel@tonic-gate 			 * rule requests deferred processing cache the minor
20507c478bd9Sstevel@tonic-gate 			 * data.
20517c478bd9Sstevel@tonic-gate 			 *
20527c478bd9Sstevel@tonic-gate 			 * If deferred processing (2nd pass), create links
20537c478bd9Sstevel@tonic-gate 			 * ONLY if rule requests deferred processing.
20547c478bd9Sstevel@tonic-gate 			 */
20557c478bd9Sstevel@tonic-gate 			if (dep && ((create->create->flags & CREATE_MASK) ==
20567c478bd9Sstevel@tonic-gate 			    CREATE_DEFER)) {
20577c478bd9Sstevel@tonic-gate 				defer = 1;
20587c478bd9Sstevel@tonic-gate 				continue;
20597c478bd9Sstevel@tonic-gate 			} else if (dep == NULL &&
20607c478bd9Sstevel@tonic-gate 			    ((create->create->flags & CREATE_MASK) !=
20617c478bd9Sstevel@tonic-gate 			    CREATE_DEFER)) {
20627c478bd9Sstevel@tonic-gate 				continue;
20637c478bd9Sstevel@tonic-gate 			}
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 			if ((*(create->create->callback_fcn))
20667c478bd9Sstevel@tonic-gate 			    (minor, node) == DEVFSADM_TERMINATE) {
20677c478bd9Sstevel@tonic-gate 				break;
20687c478bd9Sstevel@tonic-gate 			}
20697c478bd9Sstevel@tonic-gate 		}
20707c478bd9Sstevel@tonic-gate 	}
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 	if (defer)
20737c478bd9Sstevel@tonic-gate 		cache_deferred_minor(dep, node, minor);
20747c478bd9Sstevel@tonic-gate }
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate /*
20787c478bd9Sstevel@tonic-gate  * Cache node and minor in defer list.
20797c478bd9Sstevel@tonic-gate  */
20807c478bd9Sstevel@tonic-gate static void
20817c478bd9Sstevel@tonic-gate cache_deferred_minor(
20827c478bd9Sstevel@tonic-gate 	struct mlist *dep,
20837c478bd9Sstevel@tonic-gate 	di_node_t node,
20847c478bd9Sstevel@tonic-gate 	di_minor_t minor)
20857c478bd9Sstevel@tonic-gate {
20867c478bd9Sstevel@tonic-gate 	struct minor	*mp;
20877c478bd9Sstevel@tonic-gate 	const char	*fcn = "cache_deferred_minor";
20887c478bd9Sstevel@tonic-gate 
20897c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "%s node=%s, minor=%s\n", fcn,
20907c478bd9Sstevel@tonic-gate 	    di_node_name(node), di_minor_name(minor));
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate 	if (dep == NULL) {
20937c478bd9Sstevel@tonic-gate 		vprint(CHATTY_MID, "%s: cannot cache during "
20947c478bd9Sstevel@tonic-gate 		    "deferred processing. Ignoring minor\n", fcn);
20957c478bd9Sstevel@tonic-gate 		return;
20967c478bd9Sstevel@tonic-gate 	}
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	mp = (struct minor *)s_zalloc(sizeof (struct minor));
20997c478bd9Sstevel@tonic-gate 	mp->node = node;
21007c478bd9Sstevel@tonic-gate 	mp->minor = minor;
21017c478bd9Sstevel@tonic-gate 	mp->next = NULL;
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	assert(dep->head == NULL || dep->tail != NULL);
21047c478bd9Sstevel@tonic-gate 	if (dep->head == NULL) {
21057c478bd9Sstevel@tonic-gate 		dep->head = mp;
21067c478bd9Sstevel@tonic-gate 	} else {
21077c478bd9Sstevel@tonic-gate 		dep->tail->next = mp;
21087c478bd9Sstevel@tonic-gate 	}
21097c478bd9Sstevel@tonic-gate 	dep->tail = mp;
21107c478bd9Sstevel@tonic-gate }
21117c478bd9Sstevel@tonic-gate 
21127c478bd9Sstevel@tonic-gate /*
21137c478bd9Sstevel@tonic-gate  *  Check to see if "create" link creation rule matches this node/minor.
21147c478bd9Sstevel@tonic-gate  *  If it does, return TRUE.
21157c478bd9Sstevel@tonic-gate  */
21167c478bd9Sstevel@tonic-gate static int
21177c478bd9Sstevel@tonic-gate minor_matches_rule(di_node_t node, di_minor_t minor, create_list_t *create)
21187c478bd9Sstevel@tonic-gate {
21197c478bd9Sstevel@tonic-gate 	char *m_nodetype, *m_drvname;
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	if (create->create->node_type != NULL) {
21227c478bd9Sstevel@tonic-gate 
21237c478bd9Sstevel@tonic-gate 		m_nodetype = di_minor_nodetype(minor);
21247c478bd9Sstevel@tonic-gate 		assert(m_nodetype != NULL);
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 		switch (create->create->flags & TYPE_MASK) {
21277c478bd9Sstevel@tonic-gate 		case TYPE_EXACT:
21287c478bd9Sstevel@tonic-gate 			if (strcmp(create->create->node_type, m_nodetype) !=
21297c478bd9Sstevel@tonic-gate 			    0) {
21307c478bd9Sstevel@tonic-gate 				return (FALSE);
21317c478bd9Sstevel@tonic-gate 			}
21327c478bd9Sstevel@tonic-gate 			break;
21337c478bd9Sstevel@tonic-gate 		case TYPE_PARTIAL:
21347c478bd9Sstevel@tonic-gate 			if (strncmp(create->create->node_type, m_nodetype,
21357c478bd9Sstevel@tonic-gate 			    strlen(create->create->node_type)) != 0) {
21367c478bd9Sstevel@tonic-gate 				return (FALSE);
21377c478bd9Sstevel@tonic-gate 			}
21387c478bd9Sstevel@tonic-gate 			break;
21397c478bd9Sstevel@tonic-gate 		case TYPE_RE:
21407c478bd9Sstevel@tonic-gate 			if (regexec(&(create->node_type_comp), m_nodetype,
21417c478bd9Sstevel@tonic-gate 			    0, NULL, 0) != 0) {
21427c478bd9Sstevel@tonic-gate 				return (FALSE);
21437c478bd9Sstevel@tonic-gate 			}
21447c478bd9Sstevel@tonic-gate 			break;
21457c478bd9Sstevel@tonic-gate 		}
21467c478bd9Sstevel@tonic-gate 	}
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate 	if (create->create->drv_name != NULL) {
21497c478bd9Sstevel@tonic-gate 		m_drvname = di_driver_name(node);
21507c478bd9Sstevel@tonic-gate 		switch (create->create->flags & DRV_MASK) {
21517c478bd9Sstevel@tonic-gate 		case DRV_EXACT:
21527c478bd9Sstevel@tonic-gate 			if (strcmp(create->create->drv_name, m_drvname) != 0) {
21537c478bd9Sstevel@tonic-gate 				return (FALSE);
21547c478bd9Sstevel@tonic-gate 			}
21557c478bd9Sstevel@tonic-gate 			break;
21567c478bd9Sstevel@tonic-gate 		case DRV_RE:
21577c478bd9Sstevel@tonic-gate 			if (regexec(&(create->drv_name_comp), m_drvname,
21587c478bd9Sstevel@tonic-gate 			    0, NULL, 0) != 0) {
21597c478bd9Sstevel@tonic-gate 				return (FALSE);
21607c478bd9Sstevel@tonic-gate 			}
21617c478bd9Sstevel@tonic-gate 			break;
21627c478bd9Sstevel@tonic-gate 		}
21637c478bd9Sstevel@tonic-gate 	}
21647c478bd9Sstevel@tonic-gate 
21657c478bd9Sstevel@tonic-gate 	return (TRUE);
21667c478bd9Sstevel@tonic-gate }
21677c478bd9Sstevel@tonic-gate 
21687c478bd9Sstevel@tonic-gate /*
21697c478bd9Sstevel@tonic-gate  * If no classes were given on the command line, then return DEVFSADM_SUCCESS.
21707c478bd9Sstevel@tonic-gate  * Otherwise, return DEVFSADM_SUCCESS if the device "class" from the module
21717c478bd9Sstevel@tonic-gate  * matches one of the device classes given on the command line,
21727c478bd9Sstevel@tonic-gate  * otherwise, return DEVFSADM_FAILURE.
21737c478bd9Sstevel@tonic-gate  */
21747c478bd9Sstevel@tonic-gate static int
21757c478bd9Sstevel@tonic-gate class_ok(char *class)
21767c478bd9Sstevel@tonic-gate {
21777c478bd9Sstevel@tonic-gate 	int i;
21787c478bd9Sstevel@tonic-gate 
21797c478bd9Sstevel@tonic-gate 	if (num_classes == 0) {
21807c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
21817c478bd9Sstevel@tonic-gate 	}
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_classes; i++) {
21847c478bd9Sstevel@tonic-gate 		if (strcmp(class, classes[i]) == 0) {
21857c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
21867c478bd9Sstevel@tonic-gate 		}
21877c478bd9Sstevel@tonic-gate 	}
21887c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
21897c478bd9Sstevel@tonic-gate }
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate /*
21927c478bd9Sstevel@tonic-gate  * call minor_fini on active modules, then unload ALL modules
21937c478bd9Sstevel@tonic-gate  */
21947c478bd9Sstevel@tonic-gate static void
21957c478bd9Sstevel@tonic-gate unload_modules(void)
21967c478bd9Sstevel@tonic-gate {
21977c478bd9Sstevel@tonic-gate 	module_t *module_free;
21987c478bd9Sstevel@tonic-gate 	create_list_t *create_free;
21997c478bd9Sstevel@tonic-gate 	remove_list_t *remove_free;
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 	while (create_head != NULL) {
22027c478bd9Sstevel@tonic-gate 		create_free = create_head;
22037c478bd9Sstevel@tonic-gate 		create_head = create_head->next;
22047c478bd9Sstevel@tonic-gate 
22057c478bd9Sstevel@tonic-gate 		if ((create_free->create->flags & TYPE_RE) == TYPE_RE) {
22067c478bd9Sstevel@tonic-gate 			regfree(&(create_free->node_type_comp));
22077c478bd9Sstevel@tonic-gate 		}
22087c478bd9Sstevel@tonic-gate 		if ((create_free->create->flags & DRV_RE) == DRV_RE) {
22097c478bd9Sstevel@tonic-gate 			regfree(&(create_free->drv_name_comp));
22107c478bd9Sstevel@tonic-gate 		}
22117c478bd9Sstevel@tonic-gate 		free(create_free);
22127c478bd9Sstevel@tonic-gate 	}
22137c478bd9Sstevel@tonic-gate 
22147c478bd9Sstevel@tonic-gate 	while (remove_head != NULL) {
22157c478bd9Sstevel@tonic-gate 		remove_free = remove_head;
22167c478bd9Sstevel@tonic-gate 		remove_head = remove_head->next;
22177c478bd9Sstevel@tonic-gate 		free(remove_free);
22187c478bd9Sstevel@tonic-gate 	}
22197c478bd9Sstevel@tonic-gate 
22207c478bd9Sstevel@tonic-gate 	while (module_head != NULL) {
22217c478bd9Sstevel@tonic-gate 
22227c478bd9Sstevel@tonic-gate 		if ((module_head->minor_fini != NULL) &&
22237c478bd9Sstevel@tonic-gate 		    ((module_head->flags & MODULE_ACTIVE) == MODULE_ACTIVE)) {
22247c478bd9Sstevel@tonic-gate 			(void) (*(module_head->minor_fini))();
22257c478bd9Sstevel@tonic-gate 		}
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 		vprint(MODLOAD_MID, "unloading module %s\n", module_head->name);
22287c478bd9Sstevel@tonic-gate 		free(module_head->name);
22297c478bd9Sstevel@tonic-gate 		(void) dlclose(module_head->dlhandle);
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate 		module_free = module_head;
22327c478bd9Sstevel@tonic-gate 		module_head = module_head->next;
22337c478bd9Sstevel@tonic-gate 		free(module_free);
22347c478bd9Sstevel@tonic-gate 	}
22357c478bd9Sstevel@tonic-gate }
22367c478bd9Sstevel@tonic-gate 
22377c478bd9Sstevel@tonic-gate /*
22387c478bd9Sstevel@tonic-gate  * Load devfsadm logical link processing modules.
22397c478bd9Sstevel@tonic-gate  */
22407c478bd9Sstevel@tonic-gate static void
22417c478bd9Sstevel@tonic-gate load_modules(void)
22427c478bd9Sstevel@tonic-gate {
22437c478bd9Sstevel@tonic-gate 	DIR *mod_dir;
22444bc0a2efScasper 	struct dirent *entp;
22457c478bd9Sstevel@tonic-gate 	char cdir[PATH_MAX + 1];
22467c478bd9Sstevel@tonic-gate 	char *last;
22477c478bd9Sstevel@tonic-gate 	char *mdir = module_dirs;
22487c478bd9Sstevel@tonic-gate 	char *fcn = "load_modules: ";
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate 	while (*mdir != '\0') {
22517c478bd9Sstevel@tonic-gate 
22527c478bd9Sstevel@tonic-gate 		while (*mdir == ':') {
22537c478bd9Sstevel@tonic-gate 			mdir++;
22547c478bd9Sstevel@tonic-gate 		}
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate 		if (*mdir == '\0') {
22577c478bd9Sstevel@tonic-gate 			continue;
22587c478bd9Sstevel@tonic-gate 		}
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate 		last = strchr(mdir, ':');
22617c478bd9Sstevel@tonic-gate 
22627c478bd9Sstevel@tonic-gate 		if (last == NULL) {
22637c478bd9Sstevel@tonic-gate 			last = mdir + strlen(mdir);
22647c478bd9Sstevel@tonic-gate 		}
22657c478bd9Sstevel@tonic-gate 
22667c478bd9Sstevel@tonic-gate 		(void) strncpy(cdir, mdir, last - mdir);
22677c478bd9Sstevel@tonic-gate 		cdir[last - mdir] = '\0';
22687c478bd9Sstevel@tonic-gate 		mdir += strlen(cdir);
22697c478bd9Sstevel@tonic-gate 
22707c478bd9Sstevel@tonic-gate 		if ((mod_dir = opendir(cdir)) == NULL) {
22717c478bd9Sstevel@tonic-gate 			vprint(MODLOAD_MID, "%sopendir(%s): %s\n",
22727c478bd9Sstevel@tonic-gate 				fcn, cdir, strerror(errno));
22737c478bd9Sstevel@tonic-gate 			continue;
22747c478bd9Sstevel@tonic-gate 		}
22757c478bd9Sstevel@tonic-gate 
22764bc0a2efScasper 		while ((entp = readdir(mod_dir)) != NULL) {
22777c478bd9Sstevel@tonic-gate 
22787c478bd9Sstevel@tonic-gate 			if ((strcmp(entp->d_name, ".") == 0) ||
22797c478bd9Sstevel@tonic-gate 			    (strcmp(entp->d_name, "..") == 0)) {
22807c478bd9Sstevel@tonic-gate 				continue;
22817c478bd9Sstevel@tonic-gate 			}
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 			load_module(entp->d_name, cdir);
22847c478bd9Sstevel@tonic-gate 		}
22857c478bd9Sstevel@tonic-gate 		s_closedir(mod_dir);
22867c478bd9Sstevel@tonic-gate 	}
22877c478bd9Sstevel@tonic-gate }
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate static void
22907c478bd9Sstevel@tonic-gate load_module(char *mname, char *cdir)
22917c478bd9Sstevel@tonic-gate {
22927c478bd9Sstevel@tonic-gate 	_devfsadm_create_reg_t *create_reg;
22937c478bd9Sstevel@tonic-gate 	_devfsadm_remove_reg_t *remove_reg;
22947c478bd9Sstevel@tonic-gate 	create_list_t *create_list_element;
22957c478bd9Sstevel@tonic-gate 	create_list_t **create_list_next;
22967c478bd9Sstevel@tonic-gate 	remove_list_t *remove_list_element;
22977c478bd9Sstevel@tonic-gate 	remove_list_t **remove_list_next;
22987c478bd9Sstevel@tonic-gate 	char epath[PATH_MAX + 1], *end;
22997c478bd9Sstevel@tonic-gate 	char *fcn = "load_module: ";
23007c478bd9Sstevel@tonic-gate 	char *dlerrstr;
23017c478bd9Sstevel@tonic-gate 	void *dlhandle;
23027c478bd9Sstevel@tonic-gate 	module_t *module;
23037c478bd9Sstevel@tonic-gate 	int n;
23047c478bd9Sstevel@tonic-gate 	int i;
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 	/* ignore any file which does not end in '.so' */
23077c478bd9Sstevel@tonic-gate 	if ((end = strstr(mname, MODULE_SUFFIX)) != NULL) {
23087c478bd9Sstevel@tonic-gate 		if (end[strlen(MODULE_SUFFIX)] != '\0') {
23097c478bd9Sstevel@tonic-gate 			return;
23107c478bd9Sstevel@tonic-gate 		}
23117c478bd9Sstevel@tonic-gate 	} else {
23127c478bd9Sstevel@tonic-gate 		return;
23137c478bd9Sstevel@tonic-gate 	}
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 	(void) snprintf(epath, sizeof (epath), "%s/%s", cdir, mname);
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate 	if ((dlhandle = dlopen(epath, RTLD_LAZY)) == NULL) {
23187c478bd9Sstevel@tonic-gate 		dlerrstr = dlerror();
23197c478bd9Sstevel@tonic-gate 		err_print(DLOPEN_FAILED, epath,
23207c478bd9Sstevel@tonic-gate 			dlerrstr ? dlerrstr : "unknown error");
23217c478bd9Sstevel@tonic-gate 		return;
23227c478bd9Sstevel@tonic-gate 	}
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 	/* dlsym the _devfsadm_create_reg structure */
23257c478bd9Sstevel@tonic-gate 	if (NULL == (create_reg = (_devfsadm_create_reg_t *)
23267c478bd9Sstevel@tonic-gate 		    dlsym(dlhandle, _DEVFSADM_CREATE_REG))) {
23277c478bd9Sstevel@tonic-gate 		vprint(MODLOAD_MID, "dlsym(%s, %s): symbol not found\n", epath,
23287c478bd9Sstevel@tonic-gate 			_DEVFSADM_CREATE_REG);
23297c478bd9Sstevel@tonic-gate 	} else {
23307c478bd9Sstevel@tonic-gate 		vprint(MODLOAD_MID, "%sdlsym(%s, %s) succeeded\n",
23317c478bd9Sstevel@tonic-gate 			    fcn, epath, _DEVFSADM_CREATE_REG);
23327c478bd9Sstevel@tonic-gate 	}
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 	/* dlsym the _devfsadm_remove_reg structure */
23357c478bd9Sstevel@tonic-gate 	if (NULL == (remove_reg = (_devfsadm_remove_reg_t *)
23367c478bd9Sstevel@tonic-gate 	    dlsym(dlhandle, _DEVFSADM_REMOVE_REG))) {
23377c478bd9Sstevel@tonic-gate 		vprint(MODLOAD_MID, "dlsym(%s,\n\t%s): symbol not found\n",
23387c478bd9Sstevel@tonic-gate 			epath, _DEVFSADM_REMOVE_REG);
23397c478bd9Sstevel@tonic-gate 	} else {
23407c478bd9Sstevel@tonic-gate 		vprint(MODLOAD_MID, "dlsym(%s, %s): succeeded\n",
23417c478bd9Sstevel@tonic-gate 			    epath, _DEVFSADM_REMOVE_REG);
23427c478bd9Sstevel@tonic-gate 	}
23437c478bd9Sstevel@tonic-gate 
23447c478bd9Sstevel@tonic-gate 	vprint(MODLOAD_MID, "module %s loaded\n", epath);
23457c478bd9Sstevel@tonic-gate 
23467c478bd9Sstevel@tonic-gate 	module = (module_t *)s_malloc(sizeof (module_t));
23477c478bd9Sstevel@tonic-gate 	module->name = s_strdup(epath);
23487c478bd9Sstevel@tonic-gate 	module->dlhandle = dlhandle;
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate 	/* dlsym other module functions, to be called later */
23517c478bd9Sstevel@tonic-gate 	module->minor_fini = (int (*)())dlsym(dlhandle, MINOR_FINI);
23527c478bd9Sstevel@tonic-gate 	module->minor_init = (int (*)())dlsym(dlhandle, MINOR_INIT);
23537c478bd9Sstevel@tonic-gate 	module->flags = 0;
23547c478bd9Sstevel@tonic-gate 
23557c478bd9Sstevel@tonic-gate 	/*
23567c478bd9Sstevel@tonic-gate 	 *  put a ptr to each struct devfsadm_create on "create_head"
23577c478bd9Sstevel@tonic-gate 	 *  list sorted in interpose_lvl.
23587c478bd9Sstevel@tonic-gate 	 */
23597c478bd9Sstevel@tonic-gate 	if (create_reg != NULL) {
23607c478bd9Sstevel@tonic-gate 		for (i = 0; i < create_reg->count; i++) {
23617c478bd9Sstevel@tonic-gate 			int flags = create_reg->tblp[i].flags;
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate 			create_list_element = (create_list_t *)
23647c478bd9Sstevel@tonic-gate 				s_malloc(sizeof (create_list_t));
23657c478bd9Sstevel@tonic-gate 
23667c478bd9Sstevel@tonic-gate 			create_list_element->create = &(create_reg->tblp[i]);
23677c478bd9Sstevel@tonic-gate 			create_list_element->modptr = module;
23687c478bd9Sstevel@tonic-gate 
23697c478bd9Sstevel@tonic-gate 			if (((flags & CREATE_MASK) != 0) &&
23707c478bd9Sstevel@tonic-gate 			    ((flags & CREATE_MASK) != CREATE_DEFER)) {
23717c478bd9Sstevel@tonic-gate 				free(create_list_element);
23727c478bd9Sstevel@tonic-gate 				err_print("illegal flag combination in "
23737c478bd9Sstevel@tonic-gate 						"module create\n");
23747c478bd9Sstevel@tonic-gate 				err_print(IGNORING_ENTRY, i, epath);
23757c478bd9Sstevel@tonic-gate 				continue;
23767c478bd9Sstevel@tonic-gate 			}
23777c478bd9Sstevel@tonic-gate 
23787c478bd9Sstevel@tonic-gate 			if (((flags & TYPE_MASK) == 0) ^
23797c478bd9Sstevel@tonic-gate 			    (create_reg->tblp[i].node_type == NULL)) {
23807c478bd9Sstevel@tonic-gate 				free(create_list_element);
23817c478bd9Sstevel@tonic-gate 				err_print("flags value incompatible with "
23827c478bd9Sstevel@tonic-gate 					"node_type value in module create\n");
23837c478bd9Sstevel@tonic-gate 				err_print(IGNORING_ENTRY, i, epath);
23847c478bd9Sstevel@tonic-gate 				continue;
23857c478bd9Sstevel@tonic-gate 			}
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 			if (((flags & TYPE_MASK) != 0) &&
23887c478bd9Sstevel@tonic-gate 			    ((flags & TYPE_MASK) != TYPE_EXACT) &&
23897c478bd9Sstevel@tonic-gate 			    ((flags & TYPE_MASK) != TYPE_RE) &&
23907c478bd9Sstevel@tonic-gate 			    ((flags & TYPE_MASK) != TYPE_PARTIAL)) {
23917c478bd9Sstevel@tonic-gate 				free(create_list_element);
23927c478bd9Sstevel@tonic-gate 				err_print("illegal TYPE_* flag combination in "
23937c478bd9Sstevel@tonic-gate 						"module create\n");
23947c478bd9Sstevel@tonic-gate 				err_print(IGNORING_ENTRY, i, epath);
23957c478bd9Sstevel@tonic-gate 				continue;
23967c478bd9Sstevel@tonic-gate 			}
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate 			/* precompile regular expression for efficiency */
23997c478bd9Sstevel@tonic-gate 			if ((flags & TYPE_RE) == TYPE_RE) {
24007c478bd9Sstevel@tonic-gate 				if ((n = regcomp(&(create_list_element->
24017c478bd9Sstevel@tonic-gate 				    node_type_comp),
24027c478bd9Sstevel@tonic-gate 				    create_reg->tblp[i].node_type,
24037c478bd9Sstevel@tonic-gate 				    REG_EXTENDED)) != 0) {
24047c478bd9Sstevel@tonic-gate 					free(create_list_element);
24057c478bd9Sstevel@tonic-gate 					err_print(REGCOMP_FAILED,
24067c478bd9Sstevel@tonic-gate 						create_reg->tblp[i].node_type,
24077c478bd9Sstevel@tonic-gate 						n);
24087c478bd9Sstevel@tonic-gate 					err_print(IGNORING_ENTRY, i, epath);
24097c478bd9Sstevel@tonic-gate 					continue;
24107c478bd9Sstevel@tonic-gate 				}
24117c478bd9Sstevel@tonic-gate 			}
24127c478bd9Sstevel@tonic-gate 
24137c478bd9Sstevel@tonic-gate 			if (((flags & DRV_MASK) == 0) ^
24147c478bd9Sstevel@tonic-gate 			    (create_reg->tblp[i].drv_name == NULL)) {
24157c478bd9Sstevel@tonic-gate 				if ((flags & TYPE_RE) == TYPE_RE) {
24167c478bd9Sstevel@tonic-gate 					regfree(&(create_list_element->
24177c478bd9Sstevel@tonic-gate 					    node_type_comp));
24187c478bd9Sstevel@tonic-gate 				}
24197c478bd9Sstevel@tonic-gate 				free(create_list_element);
24207c478bd9Sstevel@tonic-gate 				err_print("flags value incompatible with "
24217c478bd9Sstevel@tonic-gate 					"drv_name value in module create\n");
24227c478bd9Sstevel@tonic-gate 				err_print(IGNORING_ENTRY, i, epath);
24237c478bd9Sstevel@tonic-gate 				continue;
24247c478bd9Sstevel@tonic-gate 			}
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate 			if (((flags & DRV_MASK) != 0) &&
24277c478bd9Sstevel@tonic-gate 			    ((flags & DRV_MASK) != DRV_EXACT) &&
24287c478bd9Sstevel@tonic-gate 			    ((flags & DRV_MASK) !=  DRV_RE)) {
24297c478bd9Sstevel@tonic-gate 				if ((flags & TYPE_RE) == TYPE_RE) {
24307c478bd9Sstevel@tonic-gate 					regfree(&(create_list_element->
24317c478bd9Sstevel@tonic-gate 					    node_type_comp));
24327c478bd9Sstevel@tonic-gate 				}
24337c478bd9Sstevel@tonic-gate 				free(create_list_element);
24347c478bd9Sstevel@tonic-gate 				err_print("illegal DRV_* flag combination in "
24357c478bd9Sstevel@tonic-gate 					"module create\n");
24367c478bd9Sstevel@tonic-gate 				err_print(IGNORING_ENTRY, i, epath);
24377c478bd9Sstevel@tonic-gate 				continue;
24387c478bd9Sstevel@tonic-gate 			}
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate 			/* precompile regular expression for efficiency */
24417c478bd9Sstevel@tonic-gate 			if ((create_reg->tblp[i].flags & DRV_RE) == DRV_RE) {
24427c478bd9Sstevel@tonic-gate 				if ((n = regcomp(&(create_list_element->
24437c478bd9Sstevel@tonic-gate 				    drv_name_comp),
24447c478bd9Sstevel@tonic-gate 				    create_reg->tblp[i].drv_name,
24457c478bd9Sstevel@tonic-gate 				    REG_EXTENDED)) != 0) {
24467c478bd9Sstevel@tonic-gate 					if ((flags & TYPE_RE) == TYPE_RE) {
24477c478bd9Sstevel@tonic-gate 						regfree(&(create_list_element->
24487c478bd9Sstevel@tonic-gate 						    node_type_comp));
24497c478bd9Sstevel@tonic-gate 					}
24507c478bd9Sstevel@tonic-gate 					free(create_list_element);
24517c478bd9Sstevel@tonic-gate 					err_print(REGCOMP_FAILED,
24527c478bd9Sstevel@tonic-gate 						create_reg->tblp[i].drv_name,
24537c478bd9Sstevel@tonic-gate 						n);
24547c478bd9Sstevel@tonic-gate 					err_print(IGNORING_ENTRY, i, epath);
24557c478bd9Sstevel@tonic-gate 					continue;
24567c478bd9Sstevel@tonic-gate 				}
24577c478bd9Sstevel@tonic-gate 			}
24587c478bd9Sstevel@tonic-gate 
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 			/* add to list sorted by interpose level */
24617c478bd9Sstevel@tonic-gate 			for (create_list_next = &(create_head);
24627c478bd9Sstevel@tonic-gate 				(*create_list_next != NULL) &&
24637c478bd9Sstevel@tonic-gate 				(*create_list_next)->create->interpose_lvl >=
24647c478bd9Sstevel@tonic-gate 				create_list_element->create->interpose_lvl;
24657c478bd9Sstevel@tonic-gate 				create_list_next =
24667c478bd9Sstevel@tonic-gate 					&((*create_list_next)->next));
24677c478bd9Sstevel@tonic-gate 			create_list_element->next = *create_list_next;
24687c478bd9Sstevel@tonic-gate 			*create_list_next = create_list_element;
24697c478bd9Sstevel@tonic-gate 		}
24707c478bd9Sstevel@tonic-gate 	}
24717c478bd9Sstevel@tonic-gate 
24727c478bd9Sstevel@tonic-gate 	/*
24737c478bd9Sstevel@tonic-gate 	 *  put a ptr to each struct devfsadm_remove on "remove_head"
24747c478bd9Sstevel@tonic-gate 	 *  list sorted by interpose_lvl.
24757c478bd9Sstevel@tonic-gate 	 */
24767c478bd9Sstevel@tonic-gate 	if (remove_reg != NULL) {
24777c478bd9Sstevel@tonic-gate 		for (i = 0; i < remove_reg->count; i++) {
24787c478bd9Sstevel@tonic-gate 
24797c478bd9Sstevel@tonic-gate 			remove_list_element = (remove_list_t *)
24807c478bd9Sstevel@tonic-gate 				s_malloc(sizeof (remove_list_t));
24817c478bd9Sstevel@tonic-gate 
24827c478bd9Sstevel@tonic-gate 			remove_list_element->remove = &(remove_reg->tblp[i]);
24837c478bd9Sstevel@tonic-gate 			remove_list_element->modptr = module;
24847c478bd9Sstevel@tonic-gate 
24857c478bd9Sstevel@tonic-gate 			for (remove_list_next = &(remove_head);
24867c478bd9Sstevel@tonic-gate 				(*remove_list_next != NULL) &&
24877c478bd9Sstevel@tonic-gate 				(*remove_list_next)->remove->interpose_lvl >=
24887c478bd9Sstevel@tonic-gate 				remove_list_element->remove->interpose_lvl;
24897c478bd9Sstevel@tonic-gate 				remove_list_next =
24907c478bd9Sstevel@tonic-gate 					&((*remove_list_next)->next));
24917c478bd9Sstevel@tonic-gate 			remove_list_element->next = *remove_list_next;
24927c478bd9Sstevel@tonic-gate 			*remove_list_next = remove_list_element;
24937c478bd9Sstevel@tonic-gate 		}
24947c478bd9Sstevel@tonic-gate 	}
24957c478bd9Sstevel@tonic-gate 
24967c478bd9Sstevel@tonic-gate 	module->next = module_head;
24977c478bd9Sstevel@tonic-gate 	module_head = module;
24987c478bd9Sstevel@tonic-gate }
24997c478bd9Sstevel@tonic-gate 
25007c478bd9Sstevel@tonic-gate /*
25017c478bd9Sstevel@tonic-gate  * Create a thread to call minor_fini after some delay
25027c478bd9Sstevel@tonic-gate  */
25037c478bd9Sstevel@tonic-gate static void
25047c478bd9Sstevel@tonic-gate startup_cache_sync_thread()
25057c478bd9Sstevel@tonic-gate {
25067c478bd9Sstevel@tonic-gate 	vprint(INITFINI_MID, "startup_cache_sync_thread\n");
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&minor_fini_mutex);
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 	minor_fini_delay_restart = TRUE;
25117c478bd9Sstevel@tonic-gate 
25127c478bd9Sstevel@tonic-gate 	if (minor_fini_thread_created == FALSE) {
25137c478bd9Sstevel@tonic-gate 
25147c478bd9Sstevel@tonic-gate 		if (thr_create(NULL, NULL,
25157c478bd9Sstevel@tonic-gate 		    (void *(*)(void *))call_minor_fini_thread, NULL,
25167c478bd9Sstevel@tonic-gate 		    THR_DETACHED, NULL)) {
25177c478bd9Sstevel@tonic-gate 			err_print(CANT_CREATE_THREAD, "minor_fini",
25187c478bd9Sstevel@tonic-gate 					strerror(errno));
25197c478bd9Sstevel@tonic-gate 
25207c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&minor_fini_mutex);
25217c478bd9Sstevel@tonic-gate 
25227c478bd9Sstevel@tonic-gate 			/*
25237c478bd9Sstevel@tonic-gate 			 * just sync state here instead of
25247c478bd9Sstevel@tonic-gate 			 * giving up
25257c478bd9Sstevel@tonic-gate 			 */
25267c478bd9Sstevel@tonic-gate 			lock_dev();
25277c478bd9Sstevel@tonic-gate 			unlock_dev(SYNC_STATE);
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate 			return;
25307c478bd9Sstevel@tonic-gate 		}
25317c478bd9Sstevel@tonic-gate 		minor_fini_thread_created = TRUE;
25327c478bd9Sstevel@tonic-gate 	} else {
25337c478bd9Sstevel@tonic-gate 		vprint(INITFINI_MID, "restarting delay\n");
25347c478bd9Sstevel@tonic-gate 	}
25357c478bd9Sstevel@tonic-gate 
25367c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&minor_fini_mutex);
25377c478bd9Sstevel@tonic-gate }
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate /*
25407c478bd9Sstevel@tonic-gate  * after not receiving an event for minor_fini_timeout secs, we need
25417c478bd9Sstevel@tonic-gate  * to call the minor_fini routines
25427c478bd9Sstevel@tonic-gate  */
25437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
25447c478bd9Sstevel@tonic-gate static void
25457c478bd9Sstevel@tonic-gate call_minor_fini_thread(void *arg)
25467c478bd9Sstevel@tonic-gate {
25477c478bd9Sstevel@tonic-gate 	int count = 0;
25487c478bd9Sstevel@tonic-gate 
25497c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&minor_fini_mutex);
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 	vprint(INITFINI_MID, "call_minor_fini_thread starting\n");
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 	do {
25547c478bd9Sstevel@tonic-gate 		minor_fini_delay_restart = FALSE;
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&minor_fini_mutex);
25577c478bd9Sstevel@tonic-gate 		(void) sleep(minor_fini_timeout);
25587c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&minor_fini_mutex);
25597c478bd9Sstevel@tonic-gate 
25607c478bd9Sstevel@tonic-gate 		/*
25617c478bd9Sstevel@tonic-gate 		 * if minor_fini_delay_restart is still false then
25627c478bd9Sstevel@tonic-gate 		 * we can call minor fini routines.
25637c478bd9Sstevel@tonic-gate 		 * ensure that at least periodically minor_fini gets
25647c478bd9Sstevel@tonic-gate 		 * called satisfying link generators depending on fini
25657c478bd9Sstevel@tonic-gate 		 * being eventually called
25667c478bd9Sstevel@tonic-gate 		 */
25677c478bd9Sstevel@tonic-gate 		if ((count++ >= FORCE_CALL_MINOR_FINI) ||
25687c478bd9Sstevel@tonic-gate 		    (minor_fini_delay_restart == FALSE)) {
25697c478bd9Sstevel@tonic-gate 			vprint(INITFINI_MID,
25707c478bd9Sstevel@tonic-gate 			    "call_minor_fini starting (%d)\n", count);
25717c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&minor_fini_mutex);
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 			lock_dev();
25747c478bd9Sstevel@tonic-gate 			unlock_dev(SYNC_STATE);
25757c478bd9Sstevel@tonic-gate 
25767c478bd9Sstevel@tonic-gate 			vprint(INITFINI_MID, "call_minor_fini done\n");
25777c478bd9Sstevel@tonic-gate 
25787c478bd9Sstevel@tonic-gate 			/*
25797c478bd9Sstevel@tonic-gate 			 * hang around before exiting just in case
25807c478bd9Sstevel@tonic-gate 			 * minor_fini_delay_restart is set again
25817c478bd9Sstevel@tonic-gate 			 */
25827c478bd9Sstevel@tonic-gate 			(void) sleep(1);
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate 			count = 0;
25857c478bd9Sstevel@tonic-gate 
25867c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&minor_fini_mutex);
25877c478bd9Sstevel@tonic-gate 		}
25887c478bd9Sstevel@tonic-gate 	} while (minor_fini_delay_restart);
25897c478bd9Sstevel@tonic-gate 
25907c478bd9Sstevel@tonic-gate 	minor_fini_thread_created = FALSE;
25917c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&minor_fini_mutex);
25927c478bd9Sstevel@tonic-gate 	vprint(INITFINI_MID, "call_minor_fini_thread exiting\n");
25937c478bd9Sstevel@tonic-gate }
25947c478bd9Sstevel@tonic-gate 
25957c478bd9Sstevel@tonic-gate /*
25967c478bd9Sstevel@tonic-gate  * Attempt to initialize module, if a minor_init routine exists.  Set
25977c478bd9Sstevel@tonic-gate  * the active flag if the routine exists and succeeds.	If it doesn't
25987c478bd9Sstevel@tonic-gate  * exist, just set the active flag.
25997c478bd9Sstevel@tonic-gate  */
26007c478bd9Sstevel@tonic-gate static int
26017c478bd9Sstevel@tonic-gate call_minor_init(module_t *module)
26027c478bd9Sstevel@tonic-gate {
26037c478bd9Sstevel@tonic-gate 	char *fcn = "call_minor_init: ";
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 	if ((module->flags & MODULE_ACTIVE) == MODULE_ACTIVE) {
26067c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
26077c478bd9Sstevel@tonic-gate 	}
26087c478bd9Sstevel@tonic-gate 
26097c478bd9Sstevel@tonic-gate 	vprint(INITFINI_MID, "%smodule %s.  current state: inactive\n",
26107c478bd9Sstevel@tonic-gate 		fcn, module->name);
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 	if (module->minor_init == NULL) {
26137c478bd9Sstevel@tonic-gate 		module->flags |= MODULE_ACTIVE;
26147c478bd9Sstevel@tonic-gate 		vprint(INITFINI_MID, "minor_init not defined\n");
26157c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
26167c478bd9Sstevel@tonic-gate 	}
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 	if ((*(module->minor_init))() == DEVFSADM_FAILURE) {
26197c478bd9Sstevel@tonic-gate 		err_print(FAILED_FOR_MODULE, MINOR_INIT, module->name);
26207c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
26217c478bd9Sstevel@tonic-gate 	}
26227c478bd9Sstevel@tonic-gate 
26237c478bd9Sstevel@tonic-gate 	vprint(INITFINI_MID, "minor_init() returns DEVFSADM_SUCCESS. "
26247c478bd9Sstevel@tonic-gate 		"new state: active\n");
26257c478bd9Sstevel@tonic-gate 
26267c478bd9Sstevel@tonic-gate 	module->flags |= MODULE_ACTIVE;
26277c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
26287c478bd9Sstevel@tonic-gate }
26297c478bd9Sstevel@tonic-gate 
2630*055e805aSedp static int
2631*055e805aSedp i_mknod(char *path, int stype, int mode, dev_t dev, uid_t uid, gid_t gid)
2632*055e805aSedp {
2633*055e805aSedp 	struct stat sbuf;
2634*055e805aSedp 
2635*055e805aSedp 	assert((stype & (S_IFCHR|S_IFBLK)) != 0);
2636*055e805aSedp 	assert((mode & S_IFMT) == 0);
2637*055e805aSedp 
2638*055e805aSedp 	if (stat(path, &sbuf) == 0) {
2639*055e805aSedp 		/*
2640*055e805aSedp 		 * the node already exists, check if it's device
2641*055e805aSedp 		 * information is correct
2642*055e805aSedp 		 */
2643*055e805aSedp 		if (((sbuf.st_mode & S_IFMT) == stype) &&
2644*055e805aSedp 		    (sbuf.st_rdev == dev)) {
2645*055e805aSedp 			/* the device node is correct, continue */
2646*055e805aSedp 			return (DEVFSADM_SUCCESS);
2647*055e805aSedp 		}
2648*055e805aSedp 		/*
2649*055e805aSedp 		 * the device node already exists but has the wrong
2650*055e805aSedp 		 * mode/dev_t value.  we need to delete the current
2651*055e805aSedp 		 * node and re-create it with the correct mode/dev_t
2652*055e805aSedp 		 * value, but we also want to preserve the current
2653*055e805aSedp 		 * owner and permission information.
2654*055e805aSedp 		 */
2655*055e805aSedp 		uid = sbuf.st_uid;
2656*055e805aSedp 		gid = sbuf.st_gid;
2657*055e805aSedp 		mode = sbuf.st_mode & ~S_IFMT;
2658*055e805aSedp 		s_unlink(path);
2659*055e805aSedp 	}
2660*055e805aSedp 
2661*055e805aSedp top:
2662*055e805aSedp 	if (mknod(path, stype | mode, dev) == -1) {
2663*055e805aSedp 		if (errno == ENOENT) {
2664*055e805aSedp 			/* dirpath to node doesn't exist, create it */
2665*055e805aSedp 			char *hide = strrchr(path, '/');
2666*055e805aSedp 			*hide = '\0';
2667*055e805aSedp 			s_mkdirp(path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
2668*055e805aSedp 			*hide = '/';
2669*055e805aSedp 			goto top;
2670*055e805aSedp 		}
2671*055e805aSedp 		err_print(MKNOD_FAILED, path, strerror(errno));
2672*055e805aSedp 		return (DEVFSADM_FAILURE);
2673*055e805aSedp 	} else {
2674*055e805aSedp 		/*
2675*055e805aSedp 		 * If we successfully made the node, then set its owner
2676*055e805aSedp 		 * and group.  Existing nodes will be unaffected.
2677*055e805aSedp 		 */
2678*055e805aSedp 		(void) chown(path, uid, gid);
2679*055e805aSedp 	}
2680*055e805aSedp 	return (DEVFSADM_SUCCESS);
2681*055e805aSedp }
2682*055e805aSedp 
26837c478bd9Sstevel@tonic-gate /*ARGSUSED*/
26847c478bd9Sstevel@tonic-gate int
26857c478bd9Sstevel@tonic-gate devfsadm_mklink_zone(struct zone_devinfo *z, char *link, di_node_t node,
26867c478bd9Sstevel@tonic-gate     di_minor_t minor, int flags)
26877c478bd9Sstevel@tonic-gate {
26887c478bd9Sstevel@tonic-gate 	char path[PATH_MAX];
26897c478bd9Sstevel@tonic-gate 	char phy_path[PATH_MAX];
26907c478bd9Sstevel@tonic-gate 	char *dev_pathp;
26917c478bd9Sstevel@tonic-gate 	char *acontents, *aminor = NULL;
26927c478bd9Sstevel@tonic-gate 	mode_t mode;
26937c478bd9Sstevel@tonic-gate 	uid_t uid;
26947c478bd9Sstevel@tonic-gate 	gid_t gid;
26957c478bd9Sstevel@tonic-gate 	dev_t dev;
26967c478bd9Sstevel@tonic-gate 	struct zone_devtab out_match;
26977c478bd9Sstevel@tonic-gate 
26987c478bd9Sstevel@tonic-gate 	if (zonecfg_match_dev(z->zone_dochdl, link, &out_match) != Z_OK) {
26997c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
27007c478bd9Sstevel@tonic-gate 	}
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate 	vprint(ZONE_MID, "zone device match: <device match=\"%s\"> "
27037c478bd9Sstevel@tonic-gate 	    "matches /dev/%s\n", out_match.zone_dev_match, link);
27047c478bd9Sstevel@tonic-gate 
27057c478bd9Sstevel@tonic-gate 	/*
27067c478bd9Sstevel@tonic-gate 	 * In daemon mode, zone_path will be non-empty.  In non-daemon mode
27077c478bd9Sstevel@tonic-gate 	 * it will be empty since we've already stuck the zone into dev_dir,
27087c478bd9Sstevel@tonic-gate 	 * etc.
27097c478bd9Sstevel@tonic-gate 	 */
27107c478bd9Sstevel@tonic-gate 	(void) snprintf(path, sizeof (path), "%s/dev/%s", z->zone_path, link);
27117c478bd9Sstevel@tonic-gate 	dev = di_minor_devt(minor);
27127c478bd9Sstevel@tonic-gate 
27137c478bd9Sstevel@tonic-gate 	/*
27147c478bd9Sstevel@tonic-gate 	 * If this is an alias node (i.e. a clone node), we have to figure
27157c478bd9Sstevel@tonic-gate 	 * out the minor name.
27167c478bd9Sstevel@tonic-gate 	 */
27177c478bd9Sstevel@tonic-gate 	if (di_minor_type(minor) == DDM_ALIAS) {
27187c478bd9Sstevel@tonic-gate 		/* use /pseudo/clone@0:<driver> as the phys path */
27197c478bd9Sstevel@tonic-gate 		(void) snprintf(phy_path, sizeof (phy_path),
27207c478bd9Sstevel@tonic-gate 		    "/pseudo/clone@0:%s",
27217c478bd9Sstevel@tonic-gate 		    di_driver_name(di_minor_devinfo(minor)));
27227c478bd9Sstevel@tonic-gate 		aminor = di_minor_name(minor);
27237c478bd9Sstevel@tonic-gate 		acontents = phy_path;
27247c478bd9Sstevel@tonic-gate 	} else {
27257c478bd9Sstevel@tonic-gate 		if ((dev_pathp = di_devfs_path(node)) == NULL) {
27267c478bd9Sstevel@tonic-gate 			err_print(DI_DEVFS_PATH_FAILED, strerror(errno));
27277c478bd9Sstevel@tonic-gate 			devfsadm_exit(1);
27287c478bd9Sstevel@tonic-gate 		}
27297c478bd9Sstevel@tonic-gate 		(void) snprintf(phy_path, sizeof (phy_path), "%s:%s",
27307c478bd9Sstevel@tonic-gate 		    dev_pathp, di_minor_name(minor));
27317c478bd9Sstevel@tonic-gate 		di_devfs_path_free(dev_pathp);
27327c478bd9Sstevel@tonic-gate 		acontents = phy_path;
27337c478bd9Sstevel@tonic-gate 	}
27347c478bd9Sstevel@tonic-gate 
27357c478bd9Sstevel@tonic-gate 
27367c478bd9Sstevel@tonic-gate 	getattr(acontents, aminor, di_minor_spectype(minor), dev,
27377c478bd9Sstevel@tonic-gate 	    &mode, &uid, &gid);
27387c478bd9Sstevel@tonic-gate 	vprint(ZONE_MID, "zone getattr(%s, %s, %d, %lu, 0%lo, %lu, %lu)\n",
27397c478bd9Sstevel@tonic-gate 	    acontents, aminor ? aminor : "<NULL>", di_minor_spectype(minor),
27407c478bd9Sstevel@tonic-gate 	    dev, mode, uid, gid);
27417c478bd9Sstevel@tonic-gate 
2742*055e805aSedp 	/* Create the node */
2743*055e805aSedp 	return (i_mknod(path, di_minor_spectype(minor), mode, dev, uid, gid));
27447c478bd9Sstevel@tonic-gate }
27457c478bd9Sstevel@tonic-gate 
27467c478bd9Sstevel@tonic-gate /*
27477c478bd9Sstevel@tonic-gate  * Creates a symlink 'link' to the physical path of node:minor.
27487c478bd9Sstevel@tonic-gate  * Construct link contents, then call create_link_common().
27497c478bd9Sstevel@tonic-gate  */
27507c478bd9Sstevel@tonic-gate /*ARGSUSED*/
27517c478bd9Sstevel@tonic-gate int
27527c478bd9Sstevel@tonic-gate devfsadm_mklink_default(char *link, di_node_t node, di_minor_t minor, int flags)
27537c478bd9Sstevel@tonic-gate {
27547c478bd9Sstevel@tonic-gate 	char rcontents[PATH_MAX];
27557c478bd9Sstevel@tonic-gate 	char devlink[PATH_MAX];
27567c478bd9Sstevel@tonic-gate 	char phy_path[PATH_MAX];
27577c478bd9Sstevel@tonic-gate 	char *acontents;
27587c478bd9Sstevel@tonic-gate 	char *dev_path;
27597c478bd9Sstevel@tonic-gate 	int numslashes;
27607c478bd9Sstevel@tonic-gate 	int rv;
27617c478bd9Sstevel@tonic-gate 	int i, link_exists;
27627c478bd9Sstevel@tonic-gate 	int last_was_slash = FALSE;
27637c478bd9Sstevel@tonic-gate 
27647c478bd9Sstevel@tonic-gate 	/*
27657c478bd9Sstevel@tonic-gate 	 * try to use devices path
27667c478bd9Sstevel@tonic-gate 	 */
27677c478bd9Sstevel@tonic-gate 	if ((node == lnode) && (minor == lminor)) {
27687c478bd9Sstevel@tonic-gate 		acontents = lphy_path;
27697c478bd9Sstevel@tonic-gate 	} else if (di_minor_type(minor) == DDM_ALIAS) {
27707c478bd9Sstevel@tonic-gate 		/* use /pseudo/clone@0:<driver> as the phys path */
27717c478bd9Sstevel@tonic-gate 		(void) snprintf(phy_path, sizeof (phy_path),
27727c478bd9Sstevel@tonic-gate 		    "/pseudo/clone@0:%s",
27737c478bd9Sstevel@tonic-gate 		    di_driver_name(di_minor_devinfo(minor)));
27747c478bd9Sstevel@tonic-gate 		acontents = phy_path;
27757c478bd9Sstevel@tonic-gate 	} else {
27767c478bd9Sstevel@tonic-gate 		if ((dev_path = di_devfs_path(node)) == NULL) {
27777c478bd9Sstevel@tonic-gate 			err_print(DI_DEVFS_PATH_FAILED, strerror(errno));
27787c478bd9Sstevel@tonic-gate 			devfsadm_exit(1);
27797c478bd9Sstevel@tonic-gate 		}
27807c478bd9Sstevel@tonic-gate 		(void) snprintf(phy_path, sizeof (phy_path), "%s:%s",
27817c478bd9Sstevel@tonic-gate 		    dev_path, di_minor_name(minor));
27827c478bd9Sstevel@tonic-gate 		di_devfs_path_free(dev_path);
27837c478bd9Sstevel@tonic-gate 		acontents = phy_path;
27847c478bd9Sstevel@tonic-gate 	}
27857c478bd9Sstevel@tonic-gate 
27867c478bd9Sstevel@tonic-gate 	/* prepend link with dev_dir contents */
27877c478bd9Sstevel@tonic-gate 	(void) strlcpy(devlink, dev_dir, sizeof (devlink));
27887c478bd9Sstevel@tonic-gate 	(void) strlcat(devlink, "/", sizeof (devlink));
27897c478bd9Sstevel@tonic-gate 	(void) strlcat(devlink, link, sizeof (devlink));
27907c478bd9Sstevel@tonic-gate 
27917c478bd9Sstevel@tonic-gate 	/*
27927c478bd9Sstevel@tonic-gate 	 * Calculate # of ../ to add.  Account for double '//' in path.
27937c478bd9Sstevel@tonic-gate 	 * Ignore all leading slashes.
27947c478bd9Sstevel@tonic-gate 	 */
27957c478bd9Sstevel@tonic-gate 	for (i = 0; link[i] == '/'; i++)
27967c478bd9Sstevel@tonic-gate 		;
27977c478bd9Sstevel@tonic-gate 	for (numslashes = 0; link[i] != '\0'; i++) {
27987c478bd9Sstevel@tonic-gate 		if (link[i] == '/') {
27997c478bd9Sstevel@tonic-gate 			if (last_was_slash == FALSE) {
28007c478bd9Sstevel@tonic-gate 				numslashes++;
28017c478bd9Sstevel@tonic-gate 				last_was_slash = TRUE;
28027c478bd9Sstevel@tonic-gate 			}
28037c478bd9Sstevel@tonic-gate 		} else {
28047c478bd9Sstevel@tonic-gate 			last_was_slash = FALSE;
28057c478bd9Sstevel@tonic-gate 		}
28067c478bd9Sstevel@tonic-gate 	}
28077c478bd9Sstevel@tonic-gate 	/* Don't count any trailing '/' */
28087c478bd9Sstevel@tonic-gate 	if (link[i-1] == '/') {
28097c478bd9Sstevel@tonic-gate 		numslashes--;
28107c478bd9Sstevel@tonic-gate 	}
28117c478bd9Sstevel@tonic-gate 
28127c478bd9Sstevel@tonic-gate 	rcontents[0] = '\0';
28137c478bd9Sstevel@tonic-gate 	do {
28147c478bd9Sstevel@tonic-gate 		(void) strlcat(rcontents, "../", sizeof (rcontents));
28157c478bd9Sstevel@tonic-gate 	} while (numslashes-- != 0);
28167c478bd9Sstevel@tonic-gate 
28177c478bd9Sstevel@tonic-gate 	(void) strlcat(rcontents, "devices", sizeof (rcontents));
28187c478bd9Sstevel@tonic-gate 	(void) strlcat(rcontents, acontents, sizeof (rcontents));
28197c478bd9Sstevel@tonic-gate 
28207c478bd9Sstevel@tonic-gate 	if (devlinks_debug == TRUE) {
28217c478bd9Sstevel@tonic-gate 		vprint(INFO_MID, "adding link %s ==> %s\n", devlink, rcontents);
28227c478bd9Sstevel@tonic-gate 	}
28237c478bd9Sstevel@tonic-gate 
28247c478bd9Sstevel@tonic-gate 	if ((rv = create_link_common(devlink, rcontents, &link_exists))
28257c478bd9Sstevel@tonic-gate 	    == DEVFSADM_SUCCESS) {
28267c478bd9Sstevel@tonic-gate 		linknew = TRUE;
28277c478bd9Sstevel@tonic-gate 		add_link_to_cache(link, acontents);
28287c478bd9Sstevel@tonic-gate 	} else {
28297c478bd9Sstevel@tonic-gate 		linknew = FALSE;
28307c478bd9Sstevel@tonic-gate 	}
28317c478bd9Sstevel@tonic-gate 
28327c478bd9Sstevel@tonic-gate 	if (link_exists == TRUE) {
28337c478bd9Sstevel@tonic-gate 		/* Link exists or was just created */
28347c478bd9Sstevel@tonic-gate 		(void) di_devlink_add_link(devlink_cache, link, rcontents,
28357c478bd9Sstevel@tonic-gate 		    DI_PRIMARY_LINK);
28367c478bd9Sstevel@tonic-gate 	}
28377c478bd9Sstevel@tonic-gate 
28387c478bd9Sstevel@tonic-gate 	return (rv);
28397c478bd9Sstevel@tonic-gate }
28407c478bd9Sstevel@tonic-gate 
28417c478bd9Sstevel@tonic-gate int
28427c478bd9Sstevel@tonic-gate devfsadm_mklink(char *link, di_node_t node, di_minor_t minor, int flags)
28437c478bd9Sstevel@tonic-gate {
28447c478bd9Sstevel@tonic-gate 	struct zone_devinfo *z;
28457c478bd9Sstevel@tonic-gate 	int error;
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 	/*
28487c478bd9Sstevel@tonic-gate 	 * If we're in zone mode (also implies !daemon_mode), then the
28497c478bd9Sstevel@tonic-gate 	 * zone devinfo list has only one element, the zone we're configuring,
28507c478bd9Sstevel@tonic-gate 	 * and we can just use zone_head.
28517c478bd9Sstevel@tonic-gate 	 */
28527c478bd9Sstevel@tonic-gate 	if (zone_cmd_mode)
28537c478bd9Sstevel@tonic-gate 		return (devfsadm_mklink_zone(zone_head, link, node,
28547c478bd9Sstevel@tonic-gate 		    minor, flags));
28557c478bd9Sstevel@tonic-gate 	else if (!daemon_mode)
28567c478bd9Sstevel@tonic-gate 		return (devfsadm_mklink_default(link, node, minor, flags));
28577c478bd9Sstevel@tonic-gate 
28587c478bd9Sstevel@tonic-gate 	/*
28597c478bd9Sstevel@tonic-gate 	 * We're in daemon mode, so we need to make the link in the global
28607c478bd9Sstevel@tonic-gate 	 * zone; then, walk the list of zones, creating the corresponding
28617c478bd9Sstevel@tonic-gate 	 * mknod'd nodes in each.
28627c478bd9Sstevel@tonic-gate 	 */
28637c478bd9Sstevel@tonic-gate 	error = devfsadm_mklink_default(link, node, minor, flags);
28647c478bd9Sstevel@tonic-gate 
28657c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&zone_mutex);
28667c478bd9Sstevel@tonic-gate 	for (z = zone_head; z != NULL; z = z->zone_next) {
28677c478bd9Sstevel@tonic-gate 		(void) devfsadm_mklink_zone(z, link, node, minor, flags);
28687c478bd9Sstevel@tonic-gate 	}
28697c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&zone_mutex);
28707c478bd9Sstevel@tonic-gate 	return (error);
28717c478bd9Sstevel@tonic-gate }
28727c478bd9Sstevel@tonic-gate 
28737c478bd9Sstevel@tonic-gate /*
28747c478bd9Sstevel@tonic-gate  * Creates a symlink link to primary_link.  Calculates relative
28757c478bd9Sstevel@tonic-gate  * directory offsets, then calls link_common().
28767c478bd9Sstevel@tonic-gate  */
28777c478bd9Sstevel@tonic-gate /*ARGSUSED*/
28787c478bd9Sstevel@tonic-gate int
28797c478bd9Sstevel@tonic-gate devfsadm_secondary_link(char *link, char *primary_link, int flags)
28807c478bd9Sstevel@tonic-gate {
28817c478bd9Sstevel@tonic-gate 	char contents[PATH_MAX + 1];
28827c478bd9Sstevel@tonic-gate 	char devlink[PATH_MAX + 1];
28837c478bd9Sstevel@tonic-gate 	int rv, link_exists;
28847c478bd9Sstevel@tonic-gate 	char *fpath;
28857c478bd9Sstevel@tonic-gate 	char *tpath;
28867c478bd9Sstevel@tonic-gate 	char *op;
28877c478bd9Sstevel@tonic-gate 
28887c478bd9Sstevel@tonic-gate 	/* prepend link with dev_dir contents */
28897c478bd9Sstevel@tonic-gate 	(void) strcpy(devlink, dev_dir);
28907c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, "/");
28917c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, link);
28927c478bd9Sstevel@tonic-gate 	/*
28937c478bd9Sstevel@tonic-gate 	 * building extra link, so use first link as link contents, but first
28947c478bd9Sstevel@tonic-gate 	 * make it relative.
28957c478bd9Sstevel@tonic-gate 	 */
28967c478bd9Sstevel@tonic-gate 	fpath = link;
28977c478bd9Sstevel@tonic-gate 	tpath = primary_link;
28987c478bd9Sstevel@tonic-gate 	op = contents;
28997c478bd9Sstevel@tonic-gate 
29007c478bd9Sstevel@tonic-gate 	while (*fpath == *tpath && *fpath != '\0') {
29017c478bd9Sstevel@tonic-gate 		fpath++, tpath++;
29027c478bd9Sstevel@tonic-gate 	}
29037c478bd9Sstevel@tonic-gate 
29047c478bd9Sstevel@tonic-gate 	/* Count directories to go up, if any, and add "../" */
29057c478bd9Sstevel@tonic-gate 	while (*fpath != '\0') {
29067c478bd9Sstevel@tonic-gate 		if (*fpath == '/') {
29077c478bd9Sstevel@tonic-gate 			(void) strcpy(op, "../");
29087c478bd9Sstevel@tonic-gate 			op += 3;
29097c478bd9Sstevel@tonic-gate 		}
29107c478bd9Sstevel@tonic-gate 		fpath++;
29117c478bd9Sstevel@tonic-gate 	}
29127c478bd9Sstevel@tonic-gate 
29137c478bd9Sstevel@tonic-gate 	/*
29147c478bd9Sstevel@tonic-gate 	 * Back up to the start of the current path component, in
29157c478bd9Sstevel@tonic-gate 	 * case in the middle
29167c478bd9Sstevel@tonic-gate 	 */
29177c478bd9Sstevel@tonic-gate 	while (tpath != primary_link && *(tpath-1) != '/') {
29187c478bd9Sstevel@tonic-gate 		tpath--;
29197c478bd9Sstevel@tonic-gate 	}
29207c478bd9Sstevel@tonic-gate 	(void) strcpy(op, tpath);
29217c478bd9Sstevel@tonic-gate 
29227c478bd9Sstevel@tonic-gate 	if (devlinks_debug == TRUE) {
29237c478bd9Sstevel@tonic-gate 		vprint(INFO_MID, "adding extra link %s ==> %s\n",
29247c478bd9Sstevel@tonic-gate 				devlink, contents);
29257c478bd9Sstevel@tonic-gate 	}
29267c478bd9Sstevel@tonic-gate 
29277c478bd9Sstevel@tonic-gate 	if ((rv = create_link_common(devlink, contents, &link_exists))
29287c478bd9Sstevel@tonic-gate 	    == DEVFSADM_SUCCESS) {
29297c478bd9Sstevel@tonic-gate 		/*
29307c478bd9Sstevel@tonic-gate 		 * we need to save the ultimate /devices contents, and not the
29317c478bd9Sstevel@tonic-gate 		 * secondary link, since hotcleanup only looks at /devices path.
29327c478bd9Sstevel@tonic-gate 		 * Since we don't have devices path here, we can try to get it
29337c478bd9Sstevel@tonic-gate 		 * by readlink'ing the secondary link.  This assumes the primary
29347c478bd9Sstevel@tonic-gate 		 * link was created first.
29357c478bd9Sstevel@tonic-gate 		 */
29367c478bd9Sstevel@tonic-gate 		add_link_to_cache(link, lphy_path);
29377c478bd9Sstevel@tonic-gate 		linknew = TRUE;
29387c478bd9Sstevel@tonic-gate 	} else {
29397c478bd9Sstevel@tonic-gate 		linknew = FALSE;
29407c478bd9Sstevel@tonic-gate 	}
29417c478bd9Sstevel@tonic-gate 
29427c478bd9Sstevel@tonic-gate 	/*
29437c478bd9Sstevel@tonic-gate 	 * If link exists or was just created, add it to the database
29447c478bd9Sstevel@tonic-gate 	 */
29457c478bd9Sstevel@tonic-gate 	if (link_exists == TRUE) {
29467c478bd9Sstevel@tonic-gate 		(void) di_devlink_add_link(devlink_cache, link, contents,
29477c478bd9Sstevel@tonic-gate 		    DI_SECONDARY_LINK);
29487c478bd9Sstevel@tonic-gate 	}
29497c478bd9Sstevel@tonic-gate 
29507c478bd9Sstevel@tonic-gate 	return (rv);
29517c478bd9Sstevel@tonic-gate }
29527c478bd9Sstevel@tonic-gate 
29537c478bd9Sstevel@tonic-gate /* returns pointer to the devices directory */
29547c478bd9Sstevel@tonic-gate char *
29557c478bd9Sstevel@tonic-gate devfsadm_get_devices_dir()
29567c478bd9Sstevel@tonic-gate {
29577c478bd9Sstevel@tonic-gate 	return (devices_dir);
29587c478bd9Sstevel@tonic-gate }
29597c478bd9Sstevel@tonic-gate 
29607c478bd9Sstevel@tonic-gate /*
29617c478bd9Sstevel@tonic-gate  * Does the actual link creation.  VERBOSE_MID only used if there is
29627c478bd9Sstevel@tonic-gate  * a change.  CHATTY_MID used otherwise.
29637c478bd9Sstevel@tonic-gate  */
29647c478bd9Sstevel@tonic-gate static int
29657c478bd9Sstevel@tonic-gate create_link_common(char *devlink, char *contents, int *exists)
29667c478bd9Sstevel@tonic-gate {
29677c478bd9Sstevel@tonic-gate 	int try;
29687c478bd9Sstevel@tonic-gate 	int linksize;
29697c478bd9Sstevel@tonic-gate 	int max_tries = 0;
29707c478bd9Sstevel@tonic-gate 	static int prev_link_existed = TRUE;
29717c478bd9Sstevel@tonic-gate 	char checkcontents[PATH_MAX + 1];
29727c478bd9Sstevel@tonic-gate 	char *hide;
29737c478bd9Sstevel@tonic-gate 
29747c478bd9Sstevel@tonic-gate 	*exists = FALSE;
29757c478bd9Sstevel@tonic-gate 
29767c478bd9Sstevel@tonic-gate 	/* Database is not updated when file_mods == FALSE */
29777c478bd9Sstevel@tonic-gate 	if (file_mods == FALSE) {
29787c478bd9Sstevel@tonic-gate 		linksize = readlink(devlink, checkcontents, PATH_MAX);
29797c478bd9Sstevel@tonic-gate 		if (linksize > 0) {
29807c478bd9Sstevel@tonic-gate 			checkcontents[linksize] = '\0';
29817c478bd9Sstevel@tonic-gate 			if (strcmp(checkcontents, contents) != 0) {
29827c478bd9Sstevel@tonic-gate 				vprint(CHATTY_MID, REMOVING_LINK,
29837c478bd9Sstevel@tonic-gate 						devlink, checkcontents);
29847c478bd9Sstevel@tonic-gate 				return (DEVFSADM_SUCCESS);
29857c478bd9Sstevel@tonic-gate 			} else {
29867c478bd9Sstevel@tonic-gate 				vprint(CHATTY_MID, "link exists and is correct:"
29877c478bd9Sstevel@tonic-gate 					" %s -> %s\n", devlink, contents);
29887c478bd9Sstevel@tonic-gate 				/* failure only in that the link existed */
29897c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
29907c478bd9Sstevel@tonic-gate 			}
29917c478bd9Sstevel@tonic-gate 		} else {
29927c478bd9Sstevel@tonic-gate 			vprint(VERBOSE_MID, CREATING_LINK, devlink, contents);
29937c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
29947c478bd9Sstevel@tonic-gate 		}
29957c478bd9Sstevel@tonic-gate 	}
29967c478bd9Sstevel@tonic-gate 
29977c478bd9Sstevel@tonic-gate 	/*
29987c478bd9Sstevel@tonic-gate 	 * systems calls are expensive, so predict whether to readlink
29997c478bd9Sstevel@tonic-gate 	 * or symlink first, based on previous attempt
30007c478bd9Sstevel@tonic-gate 	 */
30017c478bd9Sstevel@tonic-gate 	if (prev_link_existed == FALSE) {
30027c478bd9Sstevel@tonic-gate 		try = CREATE_LINK;
30037c478bd9Sstevel@tonic-gate 	} else {
30047c478bd9Sstevel@tonic-gate 		try = READ_LINK;
30057c478bd9Sstevel@tonic-gate 	}
30067c478bd9Sstevel@tonic-gate 
30077c478bd9Sstevel@tonic-gate 	while (++max_tries <= 3) {
30087c478bd9Sstevel@tonic-gate 
30097c478bd9Sstevel@tonic-gate 		switch (try) {
30107c478bd9Sstevel@tonic-gate 		case  CREATE_LINK:
30117c478bd9Sstevel@tonic-gate 
30127c478bd9Sstevel@tonic-gate 			if (symlink(contents, devlink) == 0) {
30137c478bd9Sstevel@tonic-gate 				vprint(VERBOSE_MID, CREATING_LINK, devlink,
30147c478bd9Sstevel@tonic-gate 						contents);
30157c478bd9Sstevel@tonic-gate 				prev_link_existed = FALSE;
30167c478bd9Sstevel@tonic-gate 				/* link successfully created */
30177c478bd9Sstevel@tonic-gate 				*exists = TRUE;
30187c478bd9Sstevel@tonic-gate 				set_logindev_perms(devlink);
30197c478bd9Sstevel@tonic-gate 				return (DEVFSADM_SUCCESS);
30207c478bd9Sstevel@tonic-gate 			} else {
30217c478bd9Sstevel@tonic-gate 				switch (errno) {
30227c478bd9Sstevel@tonic-gate 
30237c478bd9Sstevel@tonic-gate 				case ENOENT:
30247c478bd9Sstevel@tonic-gate 					/* dirpath to node doesn't exist */
30257c478bd9Sstevel@tonic-gate 					hide = strrchr(devlink, '/');
30267c478bd9Sstevel@tonic-gate 					*hide = '\0';
30277c478bd9Sstevel@tonic-gate 					s_mkdirp(devlink, S_IRWXU|S_IRGRP|
30287c478bd9Sstevel@tonic-gate 						S_IXGRP|S_IROTH|S_IXOTH);
30297c478bd9Sstevel@tonic-gate 					*hide = '/';
30307c478bd9Sstevel@tonic-gate 					break;
30317c478bd9Sstevel@tonic-gate 				case EEXIST:
30327c478bd9Sstevel@tonic-gate 					try = READ_LINK;
30337c478bd9Sstevel@tonic-gate 					break;
30347c478bd9Sstevel@tonic-gate 				default:
30357c478bd9Sstevel@tonic-gate 					err_print(SYMLINK_FAILED, devlink,
30367c478bd9Sstevel@tonic-gate 						contents, strerror(errno));
30377c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
30387c478bd9Sstevel@tonic-gate 				}
30397c478bd9Sstevel@tonic-gate 			}
30407c478bd9Sstevel@tonic-gate 			break;
30417c478bd9Sstevel@tonic-gate 
30427c478bd9Sstevel@tonic-gate 		case READ_LINK:
30437c478bd9Sstevel@tonic-gate 
30447c478bd9Sstevel@tonic-gate 			linksize = readlink(devlink, checkcontents, PATH_MAX);
30457c478bd9Sstevel@tonic-gate 			if (linksize >= 0) {
30467c478bd9Sstevel@tonic-gate 				checkcontents[linksize] = '\0';
30477c478bd9Sstevel@tonic-gate 				if (strcmp(checkcontents, contents) != 0) {
30487c478bd9Sstevel@tonic-gate 					s_unlink(devlink);
30497c478bd9Sstevel@tonic-gate 					vprint(VERBOSE_MID, REMOVING_LINK,
30507c478bd9Sstevel@tonic-gate 						devlink, checkcontents);
30517c478bd9Sstevel@tonic-gate 					try = CREATE_LINK;
30527c478bd9Sstevel@tonic-gate 				} else {
30537c478bd9Sstevel@tonic-gate 					prev_link_existed = TRUE;
30547c478bd9Sstevel@tonic-gate 					vprint(CHATTY_MID,
30557c478bd9Sstevel@tonic-gate 						"link exists and is correct:"
30567c478bd9Sstevel@tonic-gate 						" %s -> %s\n", devlink,
30577c478bd9Sstevel@tonic-gate 						contents);
30587c478bd9Sstevel@tonic-gate 					*exists = TRUE;
30597c478bd9Sstevel@tonic-gate 					/* failure in that the link existed */
30607c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
30617c478bd9Sstevel@tonic-gate 				}
30627c478bd9Sstevel@tonic-gate 			} else {
30637c478bd9Sstevel@tonic-gate 				switch (errno) {
30647c478bd9Sstevel@tonic-gate 				case EINVAL:
30657c478bd9Sstevel@tonic-gate 					/* not a symlink, remove and create */
30667c478bd9Sstevel@tonic-gate 					s_unlink(devlink);
30677c478bd9Sstevel@tonic-gate 				default:
30687c478bd9Sstevel@tonic-gate 					/* maybe it didn't exist at all */
30697c478bd9Sstevel@tonic-gate 					try = CREATE_LINK;
30707c478bd9Sstevel@tonic-gate 					break;
30717c478bd9Sstevel@tonic-gate 				}
30727c478bd9Sstevel@tonic-gate 			}
30737c478bd9Sstevel@tonic-gate 			break;
30747c478bd9Sstevel@tonic-gate 		}
30757c478bd9Sstevel@tonic-gate 	}
30767c478bd9Sstevel@tonic-gate 	err_print(MAX_ATTEMPTS, devlink, contents);
30777c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
30787c478bd9Sstevel@tonic-gate }
30797c478bd9Sstevel@tonic-gate 
30807c478bd9Sstevel@tonic-gate static void
30817c478bd9Sstevel@tonic-gate set_logindev_perms(char *devlink)
30827c478bd9Sstevel@tonic-gate {
30837c478bd9Sstevel@tonic-gate 	struct login_dev *newdev;
30847c478bd9Sstevel@tonic-gate 	struct passwd pwd, *resp;
30857c478bd9Sstevel@tonic-gate 	char pwd_buf[PATH_MAX];
30867c478bd9Sstevel@tonic-gate 	int rv;
30877c478bd9Sstevel@tonic-gate 	struct stat sb;
30887c478bd9Sstevel@tonic-gate 	char *devfs_path = NULL;
30897c478bd9Sstevel@tonic-gate 
30907c478bd9Sstevel@tonic-gate 	/*
30917c478bd9Sstevel@tonic-gate 	 * We only want logindev perms to be set when a device is
30927c478bd9Sstevel@tonic-gate 	 * hotplugged or an application requests synchronous creates.
30937c478bd9Sstevel@tonic-gate 	 * So we enable this only in daemon mode. In addition,
30947c478bd9Sstevel@tonic-gate 	 * login(1) only fixes the std. /dev dir. So we don't
30957c478bd9Sstevel@tonic-gate 	 * change perms if alternate root is set.
30967c478bd9Sstevel@tonic-gate 	 * login_dev_enable is TRUE only in these cases.
30977c478bd9Sstevel@tonic-gate 	 */
30987c478bd9Sstevel@tonic-gate 	if (login_dev_enable != TRUE)
30997c478bd9Sstevel@tonic-gate 		return;
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 	/*
31027c478bd9Sstevel@tonic-gate 	 * Normally, /etc/logindevperm has few (8 - 10 entries) which
31037c478bd9Sstevel@tonic-gate 	 * may be regular expressions (globs were converted to RE).
31047c478bd9Sstevel@tonic-gate 	 * So just do a linear search through the list.
31057c478bd9Sstevel@tonic-gate 	 */
31067c478bd9Sstevel@tonic-gate 	for (newdev = login_dev_cache; newdev; newdev = newdev->ldev_next) {
31077c478bd9Sstevel@tonic-gate 		vprint(FILES_MID, "matching %s with %s\n", devlink,
31087c478bd9Sstevel@tonic-gate 		    newdev->ldev_device);
31097c478bd9Sstevel@tonic-gate 
31107c478bd9Sstevel@tonic-gate 		if (regexec(&newdev->ldev_device_regex, devlink, 0,
31117c478bd9Sstevel@tonic-gate 		    NULL, 0) == 0)  {
31127c478bd9Sstevel@tonic-gate 			vprint(FILES_MID, "matched %s with %s\n", devlink,
31137c478bd9Sstevel@tonic-gate 			    newdev->ldev_device);
31147c478bd9Sstevel@tonic-gate 			break;
31157c478bd9Sstevel@tonic-gate 		}
31167c478bd9Sstevel@tonic-gate 	}
31177c478bd9Sstevel@tonic-gate 
31187c478bd9Sstevel@tonic-gate 	if (newdev == NULL)
31197c478bd9Sstevel@tonic-gate 		return;
31207c478bd9Sstevel@tonic-gate 
31217c478bd9Sstevel@tonic-gate 	/*
31227c478bd9Sstevel@tonic-gate 	 * we have a match, now find the driver associated with this
31237c478bd9Sstevel@tonic-gate 	 * minor node using a snapshot on the physical path
31247c478bd9Sstevel@tonic-gate 	 */
31257c478bd9Sstevel@tonic-gate 	(void) resolve_link(devlink, NULL, NULL, &devfs_path, 0);
31267c478bd9Sstevel@tonic-gate 	if (devfs_path) {
31277c478bd9Sstevel@tonic-gate 		di_node_t node;
31287c478bd9Sstevel@tonic-gate 		char *drv = NULL;
31297c478bd9Sstevel@tonic-gate 		struct driver_list *list;
31307c478bd9Sstevel@tonic-gate 		char *p;
31317c478bd9Sstevel@tonic-gate 
31327c478bd9Sstevel@tonic-gate 		/* truncate on : so we can take a snapshot */
31337c478bd9Sstevel@tonic-gate 		(void) strcpy(pwd_buf, devfs_path);
31347c478bd9Sstevel@tonic-gate 		p = strrchr(pwd_buf, ':');
31357c478bd9Sstevel@tonic-gate 		if (p == NULL) {
31367c478bd9Sstevel@tonic-gate 			free(devfs_path);
31377c478bd9Sstevel@tonic-gate 			return;
31387c478bd9Sstevel@tonic-gate 		}
31397c478bd9Sstevel@tonic-gate 		*p = '\0';
31407c478bd9Sstevel@tonic-gate 
31417c478bd9Sstevel@tonic-gate 		vprint(FILES_MID, "link=%s->physpath=%s\n",
31427c478bd9Sstevel@tonic-gate 		    devlink, pwd_buf);
31437c478bd9Sstevel@tonic-gate 
31447c478bd9Sstevel@tonic-gate 		node = di_init(pwd_buf, DINFOMINOR);
31457c478bd9Sstevel@tonic-gate 
31467c478bd9Sstevel@tonic-gate 		if (node) {
31477c478bd9Sstevel@tonic-gate 			drv = di_driver_name(node);
31487c478bd9Sstevel@tonic-gate 
31497c478bd9Sstevel@tonic-gate 			if (drv) {
31507c478bd9Sstevel@tonic-gate 				vprint(FILES_MID, "%s: driver is %s\n",
31517c478bd9Sstevel@tonic-gate 				    devlink, drv);
31527c478bd9Sstevel@tonic-gate 			}
31537c478bd9Sstevel@tonic-gate 			di_fini(node);
31547c478bd9Sstevel@tonic-gate 		}
31557c478bd9Sstevel@tonic-gate 		/* search thru the driver list specified in logindevperm */
31567c478bd9Sstevel@tonic-gate 		list = newdev->ldev_driver_list;
31577c478bd9Sstevel@tonic-gate 		if ((drv != NULL) && (list != NULL)) {
31587c478bd9Sstevel@tonic-gate 			while (list) {
31597c478bd9Sstevel@tonic-gate 				if (strcmp(list->driver_name,
31607c478bd9Sstevel@tonic-gate 				    drv) == 0) {
31617c478bd9Sstevel@tonic-gate 					vprint(FILES_MID,
31627c478bd9Sstevel@tonic-gate 					    "driver %s match!\n", drv);
31637c478bd9Sstevel@tonic-gate 					break;
31647c478bd9Sstevel@tonic-gate 				}
31657c478bd9Sstevel@tonic-gate 				list = list->next;
31667c478bd9Sstevel@tonic-gate 			}
31677c478bd9Sstevel@tonic-gate 			if (list == NULL) {
31687c478bd9Sstevel@tonic-gate 				vprint(FILES_MID, "no driver match!\n");
31697c478bd9Sstevel@tonic-gate 				free(devfs_path);
31707c478bd9Sstevel@tonic-gate 				return;
31717c478bd9Sstevel@tonic-gate 			}
31727c478bd9Sstevel@tonic-gate 		}
31737c478bd9Sstevel@tonic-gate 		free(devfs_path);
31747c478bd9Sstevel@tonic-gate 	} else {
31757c478bd9Sstevel@tonic-gate 		return;
31767c478bd9Sstevel@tonic-gate 	}
31777c478bd9Sstevel@tonic-gate 
31787c478bd9Sstevel@tonic-gate 	vprint(FILES_MID, "changing permissions of %s\n", devlink);
31797c478bd9Sstevel@tonic-gate 
31807c478bd9Sstevel@tonic-gate 	/*
31817c478bd9Sstevel@tonic-gate 	 * We have a match. We now attempt to determine the
31827c478bd9Sstevel@tonic-gate 	 * owner and group of the console user.
31837c478bd9Sstevel@tonic-gate 	 *
31847c478bd9Sstevel@tonic-gate 	 * stat() the console device newdev->ldev_console
31857c478bd9Sstevel@tonic-gate 	 * which will always exist - it will have the right owner but
31867c478bd9Sstevel@tonic-gate 	 * not the right group. Use getpwuid_r() to determine group for this
31877c478bd9Sstevel@tonic-gate 	 * uid.
31887c478bd9Sstevel@tonic-gate 	 * Note, it is safe to use name service here since if name services
31897c478bd9Sstevel@tonic-gate 	 * are not available (during boot or in single-user mode), then
31907c478bd9Sstevel@tonic-gate 	 * console owner will be root and its gid can be found in
31917c478bd9Sstevel@tonic-gate 	 * local files.
31927c478bd9Sstevel@tonic-gate 	 */
31937c478bd9Sstevel@tonic-gate 	if (stat(newdev->ldev_console, &sb) == -1) {
31947c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, STAT_FAILED, newdev->ldev_console,
31957c478bd9Sstevel@tonic-gate 		    strerror(errno));
31967c478bd9Sstevel@tonic-gate 		return;
31977c478bd9Sstevel@tonic-gate 	}
31987c478bd9Sstevel@tonic-gate 
31997c478bd9Sstevel@tonic-gate 	resp = NULL;
32007c478bd9Sstevel@tonic-gate 	rv = getpwuid_r(sb.st_uid, &pwd, pwd_buf, sizeof (pwd_buf), &resp);
32017c478bd9Sstevel@tonic-gate 	if (rv || resp == NULL) {
32027c478bd9Sstevel@tonic-gate 		rv = rv ? rv : EINVAL;
32037c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, GID_FAILED, sb.st_uid,
32047c478bd9Sstevel@tonic-gate 		    strerror(rv));
32057c478bd9Sstevel@tonic-gate 		return;
32067c478bd9Sstevel@tonic-gate 	}
32077c478bd9Sstevel@tonic-gate 
32087c478bd9Sstevel@tonic-gate 	assert(&pwd == resp);
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate 	sb.st_gid = resp->pw_gid;
32117c478bd9Sstevel@tonic-gate 
32127c478bd9Sstevel@tonic-gate 	if (chmod(devlink, newdev->ldev_perms) == -1) {
32137c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, CHMOD_FAILED, devlink,
32147c478bd9Sstevel@tonic-gate 		    strerror(errno));
32157c478bd9Sstevel@tonic-gate 		return;
32167c478bd9Sstevel@tonic-gate 	}
32177c478bd9Sstevel@tonic-gate 
32187c478bd9Sstevel@tonic-gate 	if (chown(devlink, sb.st_uid, sb.st_gid)  == -1) {
32197c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, CHOWN_FAILED, devlink,
32207c478bd9Sstevel@tonic-gate 		    strerror(errno));
32217c478bd9Sstevel@tonic-gate 	}
32227c478bd9Sstevel@tonic-gate }
32237c478bd9Sstevel@tonic-gate 
32247c478bd9Sstevel@tonic-gate /*
32257c478bd9Sstevel@tonic-gate  * Reset /devices node with appropriate permissions and
32267c478bd9Sstevel@tonic-gate  * ownership as specified in /etc/minor_perm.
32277c478bd9Sstevel@tonic-gate  */
32287c478bd9Sstevel@tonic-gate static void
32297c478bd9Sstevel@tonic-gate reset_node_permissions(di_node_t node, di_minor_t minor)
32307c478bd9Sstevel@tonic-gate {
32317c478bd9Sstevel@tonic-gate 	int spectype;
32327c478bd9Sstevel@tonic-gate 	char phy_path[PATH_MAX + 1];
32337c478bd9Sstevel@tonic-gate 	mode_t mode;
32347c478bd9Sstevel@tonic-gate 	dev_t dev;
32357c478bd9Sstevel@tonic-gate 	uid_t uid;
32367c478bd9Sstevel@tonic-gate 	gid_t gid;
32377c478bd9Sstevel@tonic-gate 	struct stat sb;
32387c478bd9Sstevel@tonic-gate 	char *dev_path, *aminor = NULL;
32397c478bd9Sstevel@tonic-gate 
32407c478bd9Sstevel@tonic-gate 	/* lphy_path starts with / */
32417c478bd9Sstevel@tonic-gate 	if ((dev_path = di_devfs_path(node)) == NULL) {
32427c478bd9Sstevel@tonic-gate 		err_print(DI_DEVFS_PATH_FAILED, strerror(errno));
32437c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
32447c478bd9Sstevel@tonic-gate 	}
32457c478bd9Sstevel@tonic-gate 	(void) strcpy(lphy_path, dev_path);
32467c478bd9Sstevel@tonic-gate 	di_devfs_path_free(dev_path);
32477c478bd9Sstevel@tonic-gate 
32487c478bd9Sstevel@tonic-gate 	(void) strcat(lphy_path, ":");
32497c478bd9Sstevel@tonic-gate 	if (di_minor_type(minor) == DDM_ALIAS) {
32507c478bd9Sstevel@tonic-gate 		char *driver;
32517c478bd9Sstevel@tonic-gate 		aminor = di_minor_name(minor);
32527c478bd9Sstevel@tonic-gate 		driver = di_driver_name(di_minor_devinfo(minor));
32537c478bd9Sstevel@tonic-gate 		(void) strcat(lphy_path, driver);
32547c478bd9Sstevel@tonic-gate 	} else
32557c478bd9Sstevel@tonic-gate 		(void) strcat(lphy_path, di_minor_name(minor));
32567c478bd9Sstevel@tonic-gate 
32577c478bd9Sstevel@tonic-gate 	(void) strcpy(phy_path, devices_dir);
32587c478bd9Sstevel@tonic-gate 	(void) strcat(phy_path, lphy_path);
32597c478bd9Sstevel@tonic-gate 
32607c478bd9Sstevel@tonic-gate 	lnode = node;
32617c478bd9Sstevel@tonic-gate 	lminor = minor;
32627c478bd9Sstevel@tonic-gate 
32637c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "reset_node_permissions: phy_path=%s lphy_path=%s\n",
32647c478bd9Sstevel@tonic-gate 			phy_path, lphy_path);
32657c478bd9Sstevel@tonic-gate 
32667c478bd9Sstevel@tonic-gate 	dev = di_minor_devt(minor);
32677c478bd9Sstevel@tonic-gate 	spectype = di_minor_spectype(minor); /* block or char */
32687c478bd9Sstevel@tonic-gate 
32697c478bd9Sstevel@tonic-gate 	getattr(phy_path, aminor, spectype, dev, &mode, &uid, &gid);
32707c478bd9Sstevel@tonic-gate 
32717c478bd9Sstevel@tonic-gate 	/*
32727c478bd9Sstevel@tonic-gate 	 * compare and set permissions and ownership
32737c478bd9Sstevel@tonic-gate 	 *
32747c478bd9Sstevel@tonic-gate 	 * Under devfs, a quick insertion and removal of USB devices
32757c478bd9Sstevel@tonic-gate 	 * would cause stat of physical path to fail. In this case,
32767c478bd9Sstevel@tonic-gate 	 * we emit a verbose message, but don't print errors.
32777c478bd9Sstevel@tonic-gate 	 */
32787c478bd9Sstevel@tonic-gate 	if ((stat(phy_path, &sb) == -1) || (sb.st_rdev != dev)) {
32797c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, NO_DEVFS_NODE, phy_path);
32807c478bd9Sstevel@tonic-gate 		return;
32817c478bd9Sstevel@tonic-gate 	}
32827c478bd9Sstevel@tonic-gate 
32837c478bd9Sstevel@tonic-gate 	/*
32847c478bd9Sstevel@tonic-gate 	 * If we are here for deactivating device allocation, set
32857c478bd9Sstevel@tonic-gate 	 * default permissions. Otherwise, set default permissions
32867c478bd9Sstevel@tonic-gate 	 * only if this is a new device because we want to preserve
32877c478bd9Sstevel@tonic-gate 	 * modified user permissions.
32887c478bd9Sstevel@tonic-gate 	 * Devfs indicates a new device by faking an access time
32897c478bd9Sstevel@tonic-gate 	 * of zero.
32907c478bd9Sstevel@tonic-gate 	 */
32917c478bd9Sstevel@tonic-gate 	if (sb.st_atime != 0) {
32927c478bd9Sstevel@tonic-gate 		int  i;
32937c478bd9Sstevel@tonic-gate 		char *nt;
32947c478bd9Sstevel@tonic-gate 
32957c478bd9Sstevel@tonic-gate 		if (devalloc_off == FALSE)
32967c478bd9Sstevel@tonic-gate 			return;
32977c478bd9Sstevel@tonic-gate 
32987c478bd9Sstevel@tonic-gate 		nt = di_minor_nodetype(minor);
32997c478bd9Sstevel@tonic-gate 		if (nt == NULL)
33007c478bd9Sstevel@tonic-gate 			return;
33017c478bd9Sstevel@tonic-gate 		for (i = 0; devalloc[i]; i++) {
33027c478bd9Sstevel@tonic-gate 			if (strcmp(nt, devalloc[i]) == 0)
33037c478bd9Sstevel@tonic-gate 				break;
33047c478bd9Sstevel@tonic-gate 		}
33057c478bd9Sstevel@tonic-gate 
33067c478bd9Sstevel@tonic-gate 		if (devalloc[i] == NULL)
33077c478bd9Sstevel@tonic-gate 			return;
33087c478bd9Sstevel@tonic-gate 
33097c478bd9Sstevel@tonic-gate 		/* One of the types recognized by devalloc, reset perms */
33107c478bd9Sstevel@tonic-gate 	}
33117c478bd9Sstevel@tonic-gate 
33127c478bd9Sstevel@tonic-gate 	if (file_mods == FALSE) {
33137c478bd9Sstevel@tonic-gate 		/* Nothing more to do if simulating */
33147c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, PERM_MSG, phy_path, uid, gid, mode);
33157c478bd9Sstevel@tonic-gate 		return;
33167c478bd9Sstevel@tonic-gate 	}
33177c478bd9Sstevel@tonic-gate 
33187c478bd9Sstevel@tonic-gate 	if (sb.st_mode != mode) {
33197c478bd9Sstevel@tonic-gate 		if (chmod(phy_path, mode) == -1)
33207c478bd9Sstevel@tonic-gate 			vprint(VERBOSE_MID, CHMOD_FAILED,
33217c478bd9Sstevel@tonic-gate 			    phy_path, strerror(errno));
33227c478bd9Sstevel@tonic-gate 	}
33237c478bd9Sstevel@tonic-gate 	if (sb.st_uid != uid || sb.st_gid != gid) {
33247c478bd9Sstevel@tonic-gate 		if (chown(phy_path, uid, gid) == -1)
33257c478bd9Sstevel@tonic-gate 			vprint(VERBOSE_MID, CHOWN_FAILED,
33267c478bd9Sstevel@tonic-gate 			    phy_path, strerror(errno));
33277c478bd9Sstevel@tonic-gate 	}
33287c478bd9Sstevel@tonic-gate 
33297c478bd9Sstevel@tonic-gate 	/* Report that we actually did something */
33307c478bd9Sstevel@tonic-gate 	vprint(VERBOSE_MID, PERM_MSG, phy_path, uid, gid, mode);
33317c478bd9Sstevel@tonic-gate }
33327c478bd9Sstevel@tonic-gate 
33337c478bd9Sstevel@tonic-gate /*
33347c478bd9Sstevel@tonic-gate  * Removes logical link and the minor node it refers to.  If file is a
33357c478bd9Sstevel@tonic-gate  * link, we recurse and try to remove the minor node (or link if path is
33367c478bd9Sstevel@tonic-gate  * a double link) that file's link contents refer to.
33377c478bd9Sstevel@tonic-gate  */
33387c478bd9Sstevel@tonic-gate static void
33397c478bd9Sstevel@tonic-gate devfsadm_rm_work(char *file, int recurse, int file_type)
33407c478bd9Sstevel@tonic-gate {
33417c478bd9Sstevel@tonic-gate 	char *fcn = "devfsadm_rm_work: ";
33427c478bd9Sstevel@tonic-gate 	int linksize;
33437c478bd9Sstevel@tonic-gate 	char contents[PATH_MAX + 1];
33447c478bd9Sstevel@tonic-gate 	char nextfile[PATH_MAX + 1];
33457c478bd9Sstevel@tonic-gate 	char newfile[PATH_MAX + 1];
33467c478bd9Sstevel@tonic-gate 	char *ptr;
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%s%s\n", fcn, file);
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate 	/* TYPE_LINK split into multiple if's due to excessive indentations */
33517c478bd9Sstevel@tonic-gate 	if (file_type == TYPE_LINK) {
33527c478bd9Sstevel@tonic-gate 		(void) strcpy(newfile, dev_dir);
33537c478bd9Sstevel@tonic-gate 		(void) strcat(newfile, "/");
33547c478bd9Sstevel@tonic-gate 		(void) strcat(newfile, file);
33557c478bd9Sstevel@tonic-gate 	}
33567c478bd9Sstevel@tonic-gate 
33577c478bd9Sstevel@tonic-gate 	if ((file_type == TYPE_LINK) && (recurse == TRUE) &&
33587c478bd9Sstevel@tonic-gate 	    ((linksize = readlink(newfile, contents, PATH_MAX)) > 0)) {
33597c478bd9Sstevel@tonic-gate 		contents[linksize] = '\0';
33607c478bd9Sstevel@tonic-gate 
33617c478bd9Sstevel@tonic-gate 		if (is_minor_node(contents, &ptr) == DEVFSADM_TRUE) {
33627c478bd9Sstevel@tonic-gate 			devfsadm_rm_work(++ptr, FALSE, TYPE_DEVICES);
33637c478bd9Sstevel@tonic-gate 		} else {
33647c478bd9Sstevel@tonic-gate 			if (strncmp(contents, DEV "/", strlen(DEV) + 1) == 0) {
33657c478bd9Sstevel@tonic-gate 				devfsadm_rm_work(&contents[strlen(DEV) + 1],
33667c478bd9Sstevel@tonic-gate 							TRUE, TYPE_LINK);
33677c478bd9Sstevel@tonic-gate 			} else {
33687c478bd9Sstevel@tonic-gate 				if ((ptr = strrchr(file, '/')) != NULL) {
33697c478bd9Sstevel@tonic-gate 					*ptr = '\0';
33707c478bd9Sstevel@tonic-gate 					(void) strcpy(nextfile, file);
33717c478bd9Sstevel@tonic-gate 					*ptr = '/';
33727c478bd9Sstevel@tonic-gate 					(void) strcat(nextfile, "/");
33737c478bd9Sstevel@tonic-gate 				} else {
33747c478bd9Sstevel@tonic-gate 					(void) strcpy(nextfile, "");
33757c478bd9Sstevel@tonic-gate 				}
33767c478bd9Sstevel@tonic-gate 				(void) strcat(nextfile, contents);
33777c478bd9Sstevel@tonic-gate 				devfsadm_rm_work(nextfile, TRUE, TYPE_LINK);
33787c478bd9Sstevel@tonic-gate 			}
33797c478bd9Sstevel@tonic-gate 		}
33807c478bd9Sstevel@tonic-gate 	}
33817c478bd9Sstevel@tonic-gate 
33827c478bd9Sstevel@tonic-gate 	if (file_type == TYPE_LINK) {
33837c478bd9Sstevel@tonic-gate 		vprint(VERBOSE_MID, DEVFSADM_UNLINK, newfile);
33847c478bd9Sstevel@tonic-gate 		if (file_mods == TRUE) {
33857c478bd9Sstevel@tonic-gate 			rm_link_from_cache(file);
33867c478bd9Sstevel@tonic-gate 			s_unlink(newfile);
33877c478bd9Sstevel@tonic-gate 			rm_parent_dir_if_empty(newfile);
33887c478bd9Sstevel@tonic-gate 			invalidate_enumerate_cache();
33897c478bd9Sstevel@tonic-gate 			(void) di_devlink_rm_link(devlink_cache, file);
33907c478bd9Sstevel@tonic-gate 		}
33917c478bd9Sstevel@tonic-gate 	}
33927c478bd9Sstevel@tonic-gate 
33937c478bd9Sstevel@tonic-gate 	/*
33947c478bd9Sstevel@tonic-gate 	 * Note: we don't remove /devices entries because they are
33957c478bd9Sstevel@tonic-gate 	 *	covered by devfs.
33967c478bd9Sstevel@tonic-gate 	 */
33977c478bd9Sstevel@tonic-gate }
33987c478bd9Sstevel@tonic-gate 
33997c478bd9Sstevel@tonic-gate void
34007c478bd9Sstevel@tonic-gate devfsadm_rm_link(char *file)
34017c478bd9Sstevel@tonic-gate {
34027c478bd9Sstevel@tonic-gate 	devfsadm_rm_work(file, FALSE, TYPE_LINK);
34037c478bd9Sstevel@tonic-gate }
34047c478bd9Sstevel@tonic-gate 
34057c478bd9Sstevel@tonic-gate void
34067c478bd9Sstevel@tonic-gate devfsadm_rm_all(char *file)
34077c478bd9Sstevel@tonic-gate {
34087c478bd9Sstevel@tonic-gate 	devfsadm_rm_work(file, TRUE, TYPE_LINK);
34097c478bd9Sstevel@tonic-gate }
34107c478bd9Sstevel@tonic-gate 
34114bc0a2efScasper static int
34127c478bd9Sstevel@tonic-gate s_rmdir(char *path)
34137c478bd9Sstevel@tonic-gate {
34147c478bd9Sstevel@tonic-gate 	int	i;
34157c478bd9Sstevel@tonic-gate 	char	*rpath, *dir;
34167c478bd9Sstevel@tonic-gate 	const char *fcn = "s_rmdir";
34177c478bd9Sstevel@tonic-gate 
34187c478bd9Sstevel@tonic-gate 	/*
34197c478bd9Sstevel@tonic-gate 	 * Certain directories are created at install time by packages.
34207c478bd9Sstevel@tonic-gate 	 * Some of them (listed in packaged_dirs[]) are required by apps
34217c478bd9Sstevel@tonic-gate 	 * and need to be present even when empty.
34227c478bd9Sstevel@tonic-gate 	 */
34237c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%s: checking if %s is packaged\n", fcn, path);
34247c478bd9Sstevel@tonic-gate 
34257c478bd9Sstevel@tonic-gate 	rpath = path + strlen(dev_dir) + 1;
34267c478bd9Sstevel@tonic-gate 
34277c478bd9Sstevel@tonic-gate 	for (i = 0; (dir = packaged_dirs[i]) != NULL; i++) {
34287c478bd9Sstevel@tonic-gate 		if (*rpath == *dir) {
34297c478bd9Sstevel@tonic-gate 			if (strcmp(rpath, dir) == 0) {
34307c478bd9Sstevel@tonic-gate 				vprint(REMOVE_MID, "%s: skipping packaged dir: "
34317c478bd9Sstevel@tonic-gate 				    "%s\n", fcn, path);
34324bc0a2efScasper 				errno = EEXIST;
34334bc0a2efScasper 				return (-1);
34347c478bd9Sstevel@tonic-gate 			}
34357c478bd9Sstevel@tonic-gate 		}
34367c478bd9Sstevel@tonic-gate 	}
34377c478bd9Sstevel@tonic-gate 
34384bc0a2efScasper 	return (rmdir(path));
34397c478bd9Sstevel@tonic-gate }
34407c478bd9Sstevel@tonic-gate 
34417c478bd9Sstevel@tonic-gate /*
34427c478bd9Sstevel@tonic-gate  * Try to remove any empty directories up the tree.  It is assumed that
34437c478bd9Sstevel@tonic-gate  * pathname is a file that was removed, so start with its parent, and
34447c478bd9Sstevel@tonic-gate  * work up the tree.
34457c478bd9Sstevel@tonic-gate  */
34467c478bd9Sstevel@tonic-gate static void
34477c478bd9Sstevel@tonic-gate rm_parent_dir_if_empty(char *pathname)
34487c478bd9Sstevel@tonic-gate {
34497c478bd9Sstevel@tonic-gate 	char *ptr, path[PATH_MAX + 1];
34507c478bd9Sstevel@tonic-gate 	char *fcn = "rm_parent_dir_if_empty: ";
34517c478bd9Sstevel@tonic-gate 
34527c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%schecking %s if empty\n", fcn, pathname);
34537c478bd9Sstevel@tonic-gate 
34547c478bd9Sstevel@tonic-gate 	(void) strcpy(path, pathname);
34557c478bd9Sstevel@tonic-gate 
34567c478bd9Sstevel@tonic-gate 	/*
34577c478bd9Sstevel@tonic-gate 	 * ascend up the dir tree, deleting all empty dirs.
34587c478bd9Sstevel@tonic-gate 	 * Return immediately if a dir is not empty.
34597c478bd9Sstevel@tonic-gate 	 */
34607c478bd9Sstevel@tonic-gate 	for (;;) {
34617c478bd9Sstevel@tonic-gate 
34627c478bd9Sstevel@tonic-gate 		if ((ptr = strrchr(path, '/')) == NULL) {
34637c478bd9Sstevel@tonic-gate 			return;
34647c478bd9Sstevel@tonic-gate 		}
34657c478bd9Sstevel@tonic-gate 
34667c478bd9Sstevel@tonic-gate 		*ptr = '\0';
34677c478bd9Sstevel@tonic-gate 
34684bc0a2efScasper 		if (s_rmdir(path) == 0) {
34694bc0a2efScasper 			vprint(REMOVE_MID, "%sremoving empty dir %s\n",
34704bc0a2efScasper 			    fcn, path);
34714bc0a2efScasper 			continue;
34727c478bd9Sstevel@tonic-gate 		}
34734bc0a2efScasper 		if (errno == EEXIST) {
34747c478bd9Sstevel@tonic-gate 			vprint(REMOVE_MID, "%sdir not empty: %s\n", fcn, path);
34757c478bd9Sstevel@tonic-gate 			return;
34767c478bd9Sstevel@tonic-gate 		}
34774bc0a2efScasper 		vprint(REMOVE_MID, "%s can't remove %s: %s\n", fcn, path,
34784bc0a2efScasper 		    strerror(errno));
34794bc0a2efScasper 		return;
34807c478bd9Sstevel@tonic-gate 	}
34817c478bd9Sstevel@tonic-gate }
34827c478bd9Sstevel@tonic-gate 
34837c478bd9Sstevel@tonic-gate /*
34847c478bd9Sstevel@tonic-gate  * This function and all the functions it calls below were added to
34857c478bd9Sstevel@tonic-gate  * handle the unique problem with world wide names (WWN).  The problem is
34867c478bd9Sstevel@tonic-gate  * that if a WWN device is moved to another address on the same controller
34877c478bd9Sstevel@tonic-gate  * its logical link will change, while the physical node remains the same.
34887c478bd9Sstevel@tonic-gate  * The result is that two logical links will point to the same physical path
34897c478bd9Sstevel@tonic-gate  * in /devices, the valid link and a stale link. This function will
34907c478bd9Sstevel@tonic-gate  * find all the stale nodes, though at a significant performance cost.
34917c478bd9Sstevel@tonic-gate  *
34927c478bd9Sstevel@tonic-gate  * Caching is used to increase performance.
34937c478bd9Sstevel@tonic-gate  * A cache will be built from disk if the cache tag doesn't already exist.
34947c478bd9Sstevel@tonic-gate  * The cache tag is a regular expression "dir_re", which selects a
34957c478bd9Sstevel@tonic-gate  * subset of disks to search from typically something like
34967c478bd9Sstevel@tonic-gate  * "dev/cXt[0-9]+d[0-9]+s[0-9]+".  After the cache is built, consistency must
34977c478bd9Sstevel@tonic-gate  * be maintained, so entries are added as new links are created, and removed
34987c478bd9Sstevel@tonic-gate  * as old links are deleted.  The whole cache is flushed if we are a daemon,
34997c478bd9Sstevel@tonic-gate  * and another devfsadm process ran in between.
35007c478bd9Sstevel@tonic-gate  *
35017c478bd9Sstevel@tonic-gate  * Once the cache is built, this function finds the cache which matches
35027c478bd9Sstevel@tonic-gate  * dir_re, and then it searches all links in that cache looking for
35037c478bd9Sstevel@tonic-gate  * any link whose contents match "valid_link_contents" with a corresponding link
35047c478bd9Sstevel@tonic-gate  * which does not match "valid_link".  Any such matches are stale and removed.
35057c478bd9Sstevel@tonic-gate  */
35067c478bd9Sstevel@tonic-gate void
35077c478bd9Sstevel@tonic-gate devfsadm_rm_stale_links(char *dir_re, char *valid_link, di_node_t node,
35087c478bd9Sstevel@tonic-gate 			di_minor_t minor)
35097c478bd9Sstevel@tonic-gate {
35107c478bd9Sstevel@tonic-gate 	link_t *link;
35117c478bd9Sstevel@tonic-gate 	linkhead_t *head;
35127c478bd9Sstevel@tonic-gate 	char phy_path[PATH_MAX + 1];
35137c478bd9Sstevel@tonic-gate 	char *valid_link_contents;
35147c478bd9Sstevel@tonic-gate 	char *dev_path;
35157c478bd9Sstevel@tonic-gate 	char rmlink[PATH_MAX + 1];
35167c478bd9Sstevel@tonic-gate 
35177c478bd9Sstevel@tonic-gate 	/*
35187c478bd9Sstevel@tonic-gate 	 * try to use devices path
35197c478bd9Sstevel@tonic-gate 	 */
35207c478bd9Sstevel@tonic-gate 	if ((node == lnode) && (minor == lminor)) {
35217c478bd9Sstevel@tonic-gate 		valid_link_contents = lphy_path;
35227c478bd9Sstevel@tonic-gate 	} else {
35237c478bd9Sstevel@tonic-gate 		if ((dev_path = di_devfs_path(node)) == NULL) {
35247c478bd9Sstevel@tonic-gate 			err_print(DI_DEVFS_PATH_FAILED, strerror(errno));
35257c478bd9Sstevel@tonic-gate 			devfsadm_exit(1);
35267c478bd9Sstevel@tonic-gate 		}
35277c478bd9Sstevel@tonic-gate 		(void) strcpy(phy_path, dev_path);
35287c478bd9Sstevel@tonic-gate 		di_devfs_path_free(dev_path);
35297c478bd9Sstevel@tonic-gate 
35307c478bd9Sstevel@tonic-gate 		(void) strcat(phy_path, ":");
35317c478bd9Sstevel@tonic-gate 		(void) strcat(phy_path, di_minor_name(minor));
35327c478bd9Sstevel@tonic-gate 		valid_link_contents = phy_path;
35337c478bd9Sstevel@tonic-gate 	}
35347c478bd9Sstevel@tonic-gate 
35357c478bd9Sstevel@tonic-gate 	/*
35367c478bd9Sstevel@tonic-gate 	 * As an optimization, check to make sure the corresponding
35377c478bd9Sstevel@tonic-gate 	 * devlink was just created before continuing.
35387c478bd9Sstevel@tonic-gate 	 */
35397c478bd9Sstevel@tonic-gate 
35407c478bd9Sstevel@tonic-gate 	if (linknew == FALSE) {
35417c478bd9Sstevel@tonic-gate 		return;
35427c478bd9Sstevel@tonic-gate 	}
35437c478bd9Sstevel@tonic-gate 
35447c478bd9Sstevel@tonic-gate 	head = get_cached_links(dir_re);
35457c478bd9Sstevel@tonic-gate 
35467c478bd9Sstevel@tonic-gate 	assert(head->nextlink == NULL);
35477c478bd9Sstevel@tonic-gate 
35487c478bd9Sstevel@tonic-gate 	for (link = head->link; link != NULL; link = head->nextlink) {
35497c478bd9Sstevel@tonic-gate 		/*
35507c478bd9Sstevel@tonic-gate 		 * See hot_cleanup() for why we do this
35517c478bd9Sstevel@tonic-gate 		 */
35527c478bd9Sstevel@tonic-gate 		head->nextlink = link->next;
35537c478bd9Sstevel@tonic-gate 		if ((strcmp(link->contents, valid_link_contents) == 0) &&
35547c478bd9Sstevel@tonic-gate 		    (strcmp(link->devlink, valid_link) != 0)) {
35557c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "removing %s -> %s\n"
35567c478bd9Sstevel@tonic-gate 				"valid link is: %s -> %s\n",
35577c478bd9Sstevel@tonic-gate 				link->devlink, link->contents,
35587c478bd9Sstevel@tonic-gate 				valid_link, valid_link_contents);
35597c478bd9Sstevel@tonic-gate 			/*
35607c478bd9Sstevel@tonic-gate 			 * Use a copy of the cached link name as the
35617c478bd9Sstevel@tonic-gate 			 * cache entry will go away during link removal
35627c478bd9Sstevel@tonic-gate 			 */
35637c478bd9Sstevel@tonic-gate 			(void) snprintf(rmlink, sizeof (rmlink), "%s",
35647c478bd9Sstevel@tonic-gate 			    link->devlink);
35657c478bd9Sstevel@tonic-gate 			devfsadm_rm_link(rmlink);
35667c478bd9Sstevel@tonic-gate 		}
35677c478bd9Sstevel@tonic-gate 	}
35687c478bd9Sstevel@tonic-gate }
35697c478bd9Sstevel@tonic-gate 
35707c478bd9Sstevel@tonic-gate /*
35717c478bd9Sstevel@tonic-gate  * Return previously created cache, or create cache.
35727c478bd9Sstevel@tonic-gate  */
35737c478bd9Sstevel@tonic-gate static linkhead_t *
35747c478bd9Sstevel@tonic-gate get_cached_links(char *dir_re)
35757c478bd9Sstevel@tonic-gate {
35767c478bd9Sstevel@tonic-gate 	recurse_dev_t rd;
35777c478bd9Sstevel@tonic-gate 	linkhead_t *linkhead;
35787c478bd9Sstevel@tonic-gate 	int n;
35797c478bd9Sstevel@tonic-gate 
35807c478bd9Sstevel@tonic-gate 	vprint(BUILDCACHE_MID, "get_cached_links: %s\n", dir_re);
35817c478bd9Sstevel@tonic-gate 
35827c478bd9Sstevel@tonic-gate 	for (linkhead = headlinkhead; linkhead != NULL;
35837c478bd9Sstevel@tonic-gate 		linkhead = linkhead->nexthead) {
35847c478bd9Sstevel@tonic-gate 		if (strcmp(linkhead->dir_re, dir_re) == 0) {
35857c478bd9Sstevel@tonic-gate 			return (linkhead);
35867c478bd9Sstevel@tonic-gate 		}
35877c478bd9Sstevel@tonic-gate 	}
35887c478bd9Sstevel@tonic-gate 
35897c478bd9Sstevel@tonic-gate 	/*
35907c478bd9Sstevel@tonic-gate 	 * This tag is not in cache, so add it, along with all its
35917c478bd9Sstevel@tonic-gate 	 * matching /dev entries.  This is the only time we go to disk.
35927c478bd9Sstevel@tonic-gate 	 */
35937c478bd9Sstevel@tonic-gate 	linkhead = s_malloc(sizeof (linkhead_t));
35947c478bd9Sstevel@tonic-gate 	linkhead->nexthead = headlinkhead;
35957c478bd9Sstevel@tonic-gate 	headlinkhead = linkhead;
35967c478bd9Sstevel@tonic-gate 	linkhead->dir_re = s_strdup(dir_re);
35977c478bd9Sstevel@tonic-gate 
35987c478bd9Sstevel@tonic-gate 	if ((n = regcomp(&(linkhead->dir_re_compiled), dir_re,
35997c478bd9Sstevel@tonic-gate 				REG_EXTENDED)) != 0) {
36007c478bd9Sstevel@tonic-gate 		err_print(REGCOMP_FAILED,  dir_re, n);
36017c478bd9Sstevel@tonic-gate 	}
36027c478bd9Sstevel@tonic-gate 
36037c478bd9Sstevel@tonic-gate 	linkhead->nextlink = NULL;
36047c478bd9Sstevel@tonic-gate 	linkhead->link = NULL;
36057c478bd9Sstevel@tonic-gate 
36067c478bd9Sstevel@tonic-gate 	rd.fcn = build_devlink_list;
36077c478bd9Sstevel@tonic-gate 	rd.data = (void *)linkhead;
36087c478bd9Sstevel@tonic-gate 
36097c478bd9Sstevel@tonic-gate 	vprint(BUILDCACHE_MID, "get_cached_links: calling recurse_dev_re\n");
36107c478bd9Sstevel@tonic-gate 
36117c478bd9Sstevel@tonic-gate 	/* call build_devlink_list for each directory in the dir_re RE */
36127c478bd9Sstevel@tonic-gate 	if (dir_re[0] == '/') {
36137c478bd9Sstevel@tonic-gate 		recurse_dev_re("/", &dir_re[1], &rd);
36147c478bd9Sstevel@tonic-gate 	} else {
36157c478bd9Sstevel@tonic-gate 		recurse_dev_re(dev_dir, dir_re, &rd);
36167c478bd9Sstevel@tonic-gate 	}
36177c478bd9Sstevel@tonic-gate 
36187c478bd9Sstevel@tonic-gate 	return (linkhead);
36197c478bd9Sstevel@tonic-gate }
36207c478bd9Sstevel@tonic-gate 
36217c478bd9Sstevel@tonic-gate static void
36227c478bd9Sstevel@tonic-gate build_devlink_list(char *devlink, void *data)
36237c478bd9Sstevel@tonic-gate {
36247c478bd9Sstevel@tonic-gate 	char *fcn = "build_devlink_list: ";
36257c478bd9Sstevel@tonic-gate 	char *ptr;
36267c478bd9Sstevel@tonic-gate 	char *r_contents;
36277c478bd9Sstevel@tonic-gate 	char *r_devlink;
36287c478bd9Sstevel@tonic-gate 	char contents[PATH_MAX + 1];
36297c478bd9Sstevel@tonic-gate 	char newlink[PATH_MAX + 1];
36307c478bd9Sstevel@tonic-gate 	char stage_link[PATH_MAX + 1];
36317c478bd9Sstevel@tonic-gate 	int linksize;
36327c478bd9Sstevel@tonic-gate 	linkhead_t *linkhead = (linkhead_t *)data;
36337c478bd9Sstevel@tonic-gate 	link_t *link;
36347c478bd9Sstevel@tonic-gate 	int i = 0;
36357c478bd9Sstevel@tonic-gate 
36367c478bd9Sstevel@tonic-gate 	vprint(BUILDCACHE_MID, "%scheck_link: %s\n", fcn, devlink);
36377c478bd9Sstevel@tonic-gate 
36387c478bd9Sstevel@tonic-gate 	(void) strcpy(newlink, devlink);
36397c478bd9Sstevel@tonic-gate 
36407c478bd9Sstevel@tonic-gate 	do {
36417c478bd9Sstevel@tonic-gate 		linksize = readlink(newlink, contents, PATH_MAX);
36427c478bd9Sstevel@tonic-gate 		if (linksize <= 0) {
36437c478bd9Sstevel@tonic-gate 			/*
36447c478bd9Sstevel@tonic-gate 			 * The first pass through the do loop we may readlink()
36457c478bd9Sstevel@tonic-gate 			 * non-symlink files(EINVAL) from false regexec matches.
36467c478bd9Sstevel@tonic-gate 			 * Suppress error messages in those cases or if the link
36477c478bd9Sstevel@tonic-gate 			 * content is the empty string.
36487c478bd9Sstevel@tonic-gate 			 */
36497c478bd9Sstevel@tonic-gate 			if (linksize < 0 && (i || errno != EINVAL))
36507c478bd9Sstevel@tonic-gate 				err_print(READLINK_FAILED, "build_devlink_list",
36517c478bd9Sstevel@tonic-gate 				    newlink, strerror(errno));
36527c478bd9Sstevel@tonic-gate 			return;
36537c478bd9Sstevel@tonic-gate 		}
36547c478bd9Sstevel@tonic-gate 		contents[linksize] = '\0';
36557c478bd9Sstevel@tonic-gate 		i = 1;
36567c478bd9Sstevel@tonic-gate 
36577c478bd9Sstevel@tonic-gate 		if (is_minor_node(contents, &r_contents) == DEVFSADM_FALSE) {
36587c478bd9Sstevel@tonic-gate 			/*
36597c478bd9Sstevel@tonic-gate 			 * assume that link contents is really a pointer to
36607c478bd9Sstevel@tonic-gate 			 * another link, so recurse and read its link contents.
36617c478bd9Sstevel@tonic-gate 			 *
36627c478bd9Sstevel@tonic-gate 			 * some link contents are absolute:
36637c478bd9Sstevel@tonic-gate 			 *	/dev/audio -> /dev/sound/0
36647c478bd9Sstevel@tonic-gate 			 */
36657c478bd9Sstevel@tonic-gate 			if (strncmp(contents, DEV "/",
36667c478bd9Sstevel@tonic-gate 				strlen(DEV) + strlen("/")) != 0) {
36677c478bd9Sstevel@tonic-gate 
36687c478bd9Sstevel@tonic-gate 				if ((ptr = strrchr(newlink, '/')) == NULL) {
36697c478bd9Sstevel@tonic-gate 					vprint(REMOVE_MID, "%s%s -> %s invalid "
36707c478bd9Sstevel@tonic-gate 						"link. missing '/'\n", fcn,
36717c478bd9Sstevel@tonic-gate 						newlink, contents);
36727c478bd9Sstevel@tonic-gate 						return;
36737c478bd9Sstevel@tonic-gate 				}
36747c478bd9Sstevel@tonic-gate 				*ptr = '\0';
36757c478bd9Sstevel@tonic-gate 				(void) strcpy(stage_link, newlink);
36767c478bd9Sstevel@tonic-gate 				*ptr = '/';
36777c478bd9Sstevel@tonic-gate 				(void) strcat(stage_link, "/");
36787c478bd9Sstevel@tonic-gate 				(void) strcat(stage_link, contents);
36797c478bd9Sstevel@tonic-gate 				(void) strcpy(newlink, stage_link);
36807c478bd9Sstevel@tonic-gate 			} else {
36817c478bd9Sstevel@tonic-gate 				(void) strcpy(newlink, dev_dir);
36827c478bd9Sstevel@tonic-gate 				(void) strcat(newlink, "/");
36837c478bd9Sstevel@tonic-gate 				(void) strcat(newlink,
36847c478bd9Sstevel@tonic-gate 					&contents[strlen(DEV) + strlen("/")]);
36857c478bd9Sstevel@tonic-gate 			}
36867c478bd9Sstevel@tonic-gate 
36877c478bd9Sstevel@tonic-gate 		} else {
36887c478bd9Sstevel@tonic-gate 			newlink[0] = '\0';
36897c478bd9Sstevel@tonic-gate 		}
36907c478bd9Sstevel@tonic-gate 	} while (newlink[0] != '\0');
36917c478bd9Sstevel@tonic-gate 
36927c478bd9Sstevel@tonic-gate 	if (strncmp(devlink, dev_dir, strlen(dev_dir)) != 0) {
36937c478bd9Sstevel@tonic-gate 		vprint(BUILDCACHE_MID, "%sinvalid link: %s\n", fcn, devlink);
36947c478bd9Sstevel@tonic-gate 		return;
36957c478bd9Sstevel@tonic-gate 	}
36967c478bd9Sstevel@tonic-gate 
36977c478bd9Sstevel@tonic-gate 	r_devlink = devlink + strlen(dev_dir);
36987c478bd9Sstevel@tonic-gate 
36997c478bd9Sstevel@tonic-gate 	if (r_devlink[0] != '/')
37007c478bd9Sstevel@tonic-gate 		return;
37017c478bd9Sstevel@tonic-gate 
37027c478bd9Sstevel@tonic-gate 	link = s_malloc(sizeof (link_t));
37037c478bd9Sstevel@tonic-gate 
37047c478bd9Sstevel@tonic-gate 	/* don't store the '/' after rootdir/dev */
37057c478bd9Sstevel@tonic-gate 	r_devlink += 1;
37067c478bd9Sstevel@tonic-gate 
37077c478bd9Sstevel@tonic-gate 	vprint(BUILDCACHE_MID, "%scaching link: %s\n", fcn, r_devlink);
37087c478bd9Sstevel@tonic-gate 	link->devlink = s_strdup(r_devlink);
37097c478bd9Sstevel@tonic-gate 
37107c478bd9Sstevel@tonic-gate 	link->contents = s_strdup(r_contents);
37117c478bd9Sstevel@tonic-gate 
37127c478bd9Sstevel@tonic-gate 	link->next = linkhead->link;
37137c478bd9Sstevel@tonic-gate 	linkhead->link = link;
37147c478bd9Sstevel@tonic-gate }
37157c478bd9Sstevel@tonic-gate 
37167c478bd9Sstevel@tonic-gate /*
37177c478bd9Sstevel@tonic-gate  * to be consistent, devlink must not begin with / and must be
37187c478bd9Sstevel@tonic-gate  * relative to /dev/, whereas physpath must contain / and be
37197c478bd9Sstevel@tonic-gate  * relative to /devices.
37207c478bd9Sstevel@tonic-gate  */
37217c478bd9Sstevel@tonic-gate static void
37227c478bd9Sstevel@tonic-gate add_link_to_cache(char *devlink, char *physpath)
37237c478bd9Sstevel@tonic-gate {
37247c478bd9Sstevel@tonic-gate 	linkhead_t *linkhead;
37257c478bd9Sstevel@tonic-gate 	link_t *link;
37267c478bd9Sstevel@tonic-gate 	int added = 0;
37277c478bd9Sstevel@tonic-gate 
37287c478bd9Sstevel@tonic-gate 	if (file_mods == FALSE) {
37297c478bd9Sstevel@tonic-gate 		return;
37307c478bd9Sstevel@tonic-gate 	}
37317c478bd9Sstevel@tonic-gate 
37327c478bd9Sstevel@tonic-gate 	vprint(CACHE_MID, "add_link_to_cache: %s -> %s ",
37337c478bd9Sstevel@tonic-gate 				devlink, physpath);
37347c478bd9Sstevel@tonic-gate 
37357c478bd9Sstevel@tonic-gate 	for (linkhead = headlinkhead; linkhead != NULL;
37367c478bd9Sstevel@tonic-gate 		linkhead = linkhead->nexthead) {
37377c478bd9Sstevel@tonic-gate 		if (regexec(&(linkhead->dir_re_compiled), devlink, 0, NULL,
37387c478bd9Sstevel@tonic-gate 			0) == 0) {
37397c478bd9Sstevel@tonic-gate 			added++;
37407c478bd9Sstevel@tonic-gate 			link = s_malloc(sizeof (link_t));
37417c478bd9Sstevel@tonic-gate 			link->devlink = s_strdup(devlink);
37427c478bd9Sstevel@tonic-gate 			link->contents = s_strdup(physpath);
37437c478bd9Sstevel@tonic-gate 			link->next = linkhead->link;
37447c478bd9Sstevel@tonic-gate 			linkhead->link = link;
37457c478bd9Sstevel@tonic-gate 		}
37467c478bd9Sstevel@tonic-gate 	}
37477c478bd9Sstevel@tonic-gate 
37487c478bd9Sstevel@tonic-gate 	vprint(CACHE_MID,
37497c478bd9Sstevel@tonic-gate 		" %d %s\n", added, added == 0 ? "NOT ADDED" : "ADDED");
37507c478bd9Sstevel@tonic-gate }
37517c478bd9Sstevel@tonic-gate 
37527c478bd9Sstevel@tonic-gate /*
37537c478bd9Sstevel@tonic-gate  * Remove devlink from cache.  Devlink must be relative to /dev/ and not start
37547c478bd9Sstevel@tonic-gate  * with /.
37557c478bd9Sstevel@tonic-gate  */
37567c478bd9Sstevel@tonic-gate static void
37577c478bd9Sstevel@tonic-gate rm_link_from_cache(char *devlink)
37587c478bd9Sstevel@tonic-gate {
37597c478bd9Sstevel@tonic-gate 	linkhead_t *linkhead;
37607c478bd9Sstevel@tonic-gate 	link_t **linkp;
37617c478bd9Sstevel@tonic-gate 	link_t *save;
37627c478bd9Sstevel@tonic-gate 
37637c478bd9Sstevel@tonic-gate 	vprint(CACHE_MID, "rm_link_from_cache enter: %s\n", devlink);
37647c478bd9Sstevel@tonic-gate 
37657c478bd9Sstevel@tonic-gate 	for (linkhead = headlinkhead; linkhead != NULL;
37667c478bd9Sstevel@tonic-gate 	    linkhead = linkhead->nexthead) {
37677c478bd9Sstevel@tonic-gate 		if (regexec(&(linkhead->dir_re_compiled), devlink, 0, NULL,
37687c478bd9Sstevel@tonic-gate 			0) == 0) {
37697c478bd9Sstevel@tonic-gate 
37707c478bd9Sstevel@tonic-gate 			for (linkp = &(linkhead->link); *linkp != NULL; ) {
37717c478bd9Sstevel@tonic-gate 				if ((strcmp((*linkp)->devlink, devlink) == 0)) {
37727c478bd9Sstevel@tonic-gate 					save = *linkp;
37737c478bd9Sstevel@tonic-gate 					*linkp = (*linkp)->next;
37747c478bd9Sstevel@tonic-gate 					/*
37757c478bd9Sstevel@tonic-gate 					 * We are removing our caller's
37767c478bd9Sstevel@tonic-gate 					 * "next" link. Update the nextlink
37777c478bd9Sstevel@tonic-gate 					 * field in the head so that our
37787c478bd9Sstevel@tonic-gate 					 * callers accesses the next valid
37797c478bd9Sstevel@tonic-gate 					 * link
37807c478bd9Sstevel@tonic-gate 					 */
37817c478bd9Sstevel@tonic-gate 					if (linkhead->nextlink == save)
37827c478bd9Sstevel@tonic-gate 						linkhead->nextlink = *linkp;
37837c478bd9Sstevel@tonic-gate 					free(save->devlink);
37847c478bd9Sstevel@tonic-gate 					free(save->contents);
37857c478bd9Sstevel@tonic-gate 					free(save);
37867c478bd9Sstevel@tonic-gate 					vprint(CACHE_MID, " %s FREED FROM "
37877c478bd9Sstevel@tonic-gate 						"CACHE\n", devlink);
37887c478bd9Sstevel@tonic-gate 				} else {
37897c478bd9Sstevel@tonic-gate 					linkp = &((*linkp)->next);
37907c478bd9Sstevel@tonic-gate 				}
37917c478bd9Sstevel@tonic-gate 			}
37927c478bd9Sstevel@tonic-gate 		}
37937c478bd9Sstevel@tonic-gate 	}
37947c478bd9Sstevel@tonic-gate }
37957c478bd9Sstevel@tonic-gate 
37967c478bd9Sstevel@tonic-gate static void
37977c478bd9Sstevel@tonic-gate rm_all_links_from_cache()
37987c478bd9Sstevel@tonic-gate {
37997c478bd9Sstevel@tonic-gate 	linkhead_t *linkhead;
38007c478bd9Sstevel@tonic-gate 	linkhead_t *nextlinkhead;
38017c478bd9Sstevel@tonic-gate 	link_t *link;
38027c478bd9Sstevel@tonic-gate 	link_t *nextlink;
38037c478bd9Sstevel@tonic-gate 
38047c478bd9Sstevel@tonic-gate 	vprint(CACHE_MID, "rm_all_links_from_cache\n");
38057c478bd9Sstevel@tonic-gate 
38067c478bd9Sstevel@tonic-gate 	for (linkhead = headlinkhead; linkhead != NULL;
38077c478bd9Sstevel@tonic-gate 		linkhead = nextlinkhead) {
38087c478bd9Sstevel@tonic-gate 
38097c478bd9Sstevel@tonic-gate 		nextlinkhead = linkhead->nexthead;
38107c478bd9Sstevel@tonic-gate 		assert(linkhead->nextlink == NULL);
38117c478bd9Sstevel@tonic-gate 		for (link = linkhead->link; link != NULL; link = nextlink) {
38127c478bd9Sstevel@tonic-gate 			nextlink = link->next;
38137c478bd9Sstevel@tonic-gate 			free(link->devlink);
38147c478bd9Sstevel@tonic-gate 			free(link->contents);
38157c478bd9Sstevel@tonic-gate 			free(link);
38167c478bd9Sstevel@tonic-gate 		}
38177c478bd9Sstevel@tonic-gate 		regfree(&(linkhead->dir_re_compiled));
38187c478bd9Sstevel@tonic-gate 		free(linkhead->dir_re);
38197c478bd9Sstevel@tonic-gate 		free(linkhead);
38207c478bd9Sstevel@tonic-gate 	}
38217c478bd9Sstevel@tonic-gate 	headlinkhead = NULL;
38227c478bd9Sstevel@tonic-gate }
38237c478bd9Sstevel@tonic-gate 
38247c478bd9Sstevel@tonic-gate /*
38257c478bd9Sstevel@tonic-gate  * Called when the kernel has modified the incore path_to_inst data.  This
38267c478bd9Sstevel@tonic-gate  * function will schedule a flush of the data to the filesystem.
38277c478bd9Sstevel@tonic-gate  */
38287c478bd9Sstevel@tonic-gate static void
38297c478bd9Sstevel@tonic-gate devfs_instance_mod(void)
38307c478bd9Sstevel@tonic-gate {
38317c478bd9Sstevel@tonic-gate 	char *fcn = "devfs_instance_mod: ";
38327c478bd9Sstevel@tonic-gate 	vprint(PATH2INST_MID, "%senter\n", fcn);
38337c478bd9Sstevel@tonic-gate 
38347c478bd9Sstevel@tonic-gate 	/* signal instance thread */
38357c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&count_lock);
38367c478bd9Sstevel@tonic-gate 	inst_count++;
38377c478bd9Sstevel@tonic-gate 	(void) cond_signal(&cv);
38387c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&count_lock);
38397c478bd9Sstevel@tonic-gate }
38407c478bd9Sstevel@tonic-gate 
38417c478bd9Sstevel@tonic-gate static void
38427c478bd9Sstevel@tonic-gate instance_flush_thread(void)
38437c478bd9Sstevel@tonic-gate {
38447c478bd9Sstevel@tonic-gate 	int i;
38457c478bd9Sstevel@tonic-gate 	int idle;
38467c478bd9Sstevel@tonic-gate 
38477c478bd9Sstevel@tonic-gate 	for (;;) {
38487c478bd9Sstevel@tonic-gate 
38497c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&count_lock);
38507c478bd9Sstevel@tonic-gate 		while (inst_count == 0) {
38517c478bd9Sstevel@tonic-gate 			(void) cond_wait(&cv, &count_lock);
38527c478bd9Sstevel@tonic-gate 		}
38537c478bd9Sstevel@tonic-gate 		inst_count = 0;
38547c478bd9Sstevel@tonic-gate 
38557c478bd9Sstevel@tonic-gate 		vprint(PATH2INST_MID, "signaled to flush path_to_inst."
38567c478bd9Sstevel@tonic-gate 			" Enter delay loop\n");
38577c478bd9Sstevel@tonic-gate 		/*
38587c478bd9Sstevel@tonic-gate 		 * Wait MAX_IDLE_DELAY seconds after getting the last flush
38597c478bd9Sstevel@tonic-gate 		 * path_to_inst event before invoking a flush, but never wait
38607c478bd9Sstevel@tonic-gate 		 * more than MAX_DELAY seconds after getting the first event.
38617c478bd9Sstevel@tonic-gate 		 */
38627c478bd9Sstevel@tonic-gate 		for (idle = 0, i = 0; i < MAX_DELAY; i++) {
38637c478bd9Sstevel@tonic-gate 
38647c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&count_lock);
38657c478bd9Sstevel@tonic-gate 			(void) sleep(1);
38667c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&count_lock);
38677c478bd9Sstevel@tonic-gate 
38687c478bd9Sstevel@tonic-gate 			/* shorten the delay if we are idle */
38697c478bd9Sstevel@tonic-gate 			if (inst_count == 0) {
38707c478bd9Sstevel@tonic-gate 				idle++;
38717c478bd9Sstevel@tonic-gate 				if (idle > MAX_IDLE_DELAY) {
38727c478bd9Sstevel@tonic-gate 					break;
38737c478bd9Sstevel@tonic-gate 				}
38747c478bd9Sstevel@tonic-gate 			} else {
38757c478bd9Sstevel@tonic-gate 				inst_count = idle = 0;
38767c478bd9Sstevel@tonic-gate 			}
38777c478bd9Sstevel@tonic-gate 		}
38787c478bd9Sstevel@tonic-gate 
38797c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&count_lock);
38807c478bd9Sstevel@tonic-gate 
38817c478bd9Sstevel@tonic-gate 		flush_path_to_inst();
38827c478bd9Sstevel@tonic-gate 	}
38837c478bd9Sstevel@tonic-gate }
38847c478bd9Sstevel@tonic-gate 
38857c478bd9Sstevel@tonic-gate /*
38867c478bd9Sstevel@tonic-gate  * Helper function for flush_path_to_inst() below; this routine calls the
38877c478bd9Sstevel@tonic-gate  * inst_sync syscall to flush the path_to_inst database to the given file.
38887c478bd9Sstevel@tonic-gate  */
38897c478bd9Sstevel@tonic-gate static int
38907c478bd9Sstevel@tonic-gate do_inst_sync(char *filename)
38917c478bd9Sstevel@tonic-gate {
38927c478bd9Sstevel@tonic-gate 	void (*sigsaved)(int);
38937c478bd9Sstevel@tonic-gate 	int err = 0;
38947c478bd9Sstevel@tonic-gate 
38957c478bd9Sstevel@tonic-gate 	vprint(INSTSYNC_MID, "do_inst_sync: about to flush %s\n", filename);
38967c478bd9Sstevel@tonic-gate 	sigsaved = sigset(SIGSYS, SIG_IGN);
38977c478bd9Sstevel@tonic-gate 	if (inst_sync(filename, 0) == -1)
38987c478bd9Sstevel@tonic-gate 		err = errno;
38997c478bd9Sstevel@tonic-gate 	(void) sigset(SIGSYS, sigsaved);
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate 	switch (err) {
39027c478bd9Sstevel@tonic-gate 	case 0:
39037c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
39047c478bd9Sstevel@tonic-gate 	case EALREADY:	/* no-op, path_to_inst already up to date */
39057c478bd9Sstevel@tonic-gate 		return (EALREADY);
39067c478bd9Sstevel@tonic-gate 	case ENOSYS:
39077c478bd9Sstevel@tonic-gate 		err_print(CANT_LOAD_SYSCALL);
39087c478bd9Sstevel@tonic-gate 		break;
39097c478bd9Sstevel@tonic-gate 	case EPERM:
39107c478bd9Sstevel@tonic-gate 		err_print(SUPER_TO_SYNC);
39117c478bd9Sstevel@tonic-gate 		break;
39127c478bd9Sstevel@tonic-gate 	default:
39137c478bd9Sstevel@tonic-gate 		err_print(INSTSYNC_FAILED, filename, strerror(err));
39147c478bd9Sstevel@tonic-gate 		break;
39157c478bd9Sstevel@tonic-gate 	}
39167c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
39177c478bd9Sstevel@tonic-gate }
39187c478bd9Sstevel@tonic-gate 
39197c478bd9Sstevel@tonic-gate /*
39207c478bd9Sstevel@tonic-gate  * Flush the kernel's path_to_inst database to /etc/path_to_inst.  To do so
39217c478bd9Sstevel@tonic-gate  * safely, the database is flushed to a temporary file, then moved into place.
39227c478bd9Sstevel@tonic-gate  *
39237c478bd9Sstevel@tonic-gate  * The following files are used during this process:
39247c478bd9Sstevel@tonic-gate  * 	/etc/path_to_inst:	The path_to_inst file
39257c478bd9Sstevel@tonic-gate  * 	/etc/path_to_inst.<pid>: Contains data flushed from the kernel
39267c478bd9Sstevel@tonic-gate  * 	/etc/path_to_inst.old:  The backup file
39277c478bd9Sstevel@tonic-gate  * 	/etc/path_to_inst.old.<pid>: Temp file for creating backup
39287c478bd9Sstevel@tonic-gate  *
39297c478bd9Sstevel@tonic-gate  */
39307c478bd9Sstevel@tonic-gate static void
39317c478bd9Sstevel@tonic-gate flush_path_to_inst(void)
39327c478bd9Sstevel@tonic-gate {
39337c478bd9Sstevel@tonic-gate 	char *new_inst_file = NULL;
39347c478bd9Sstevel@tonic-gate 	char *old_inst_file = NULL;
39357c478bd9Sstevel@tonic-gate 	char *old_inst_file_npid = NULL;
39367c478bd9Sstevel@tonic-gate 	FILE *inst_file_fp = NULL;
39377c478bd9Sstevel@tonic-gate 	FILE *old_inst_file_fp = NULL;
39387c478bd9Sstevel@tonic-gate 	struct stat sb;
39397c478bd9Sstevel@tonic-gate 	int err = 0;
39407c478bd9Sstevel@tonic-gate 	int c;
39417c478bd9Sstevel@tonic-gate 	int inst_strlen;
39427c478bd9Sstevel@tonic-gate 
39437c478bd9Sstevel@tonic-gate 	vprint(PATH2INST_MID, "flush_path_to_inst: %s\n",
39447c478bd9Sstevel@tonic-gate 	    (flush_path_to_inst_enable == TRUE) ? "ENABLED" : "DISABLED");
39457c478bd9Sstevel@tonic-gate 
39467c478bd9Sstevel@tonic-gate 	if (flush_path_to_inst_enable == FALSE) {
39477c478bd9Sstevel@tonic-gate 		return;
39487c478bd9Sstevel@tonic-gate 	}
39497c478bd9Sstevel@tonic-gate 
39507c478bd9Sstevel@tonic-gate 	inst_strlen = strlen(inst_file);
39517c478bd9Sstevel@tonic-gate 	new_inst_file = s_malloc(inst_strlen + PID_STR_LEN + 2);
39527c478bd9Sstevel@tonic-gate 	old_inst_file = s_malloc(inst_strlen + PID_STR_LEN + 6);
39537c478bd9Sstevel@tonic-gate 	old_inst_file_npid = s_malloc(inst_strlen +
39547c478bd9Sstevel@tonic-gate 	    sizeof (INSTANCE_FILE_SUFFIX));
39557c478bd9Sstevel@tonic-gate 
39567c478bd9Sstevel@tonic-gate 	(void) snprintf(new_inst_file, inst_strlen + PID_STR_LEN + 2,
39577c478bd9Sstevel@tonic-gate 		"%s.%ld", inst_file, getpid());
39587c478bd9Sstevel@tonic-gate 
39597c478bd9Sstevel@tonic-gate 	if (stat(new_inst_file, &sb) == 0) {
39607c478bd9Sstevel@tonic-gate 		s_unlink(new_inst_file);
39617c478bd9Sstevel@tonic-gate 	}
39627c478bd9Sstevel@tonic-gate 
39637c478bd9Sstevel@tonic-gate 	if ((err = do_inst_sync(new_inst_file)) != DEVFSADM_SUCCESS) {
39647c478bd9Sstevel@tonic-gate 		goto out;
39657c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
39667c478bd9Sstevel@tonic-gate 	}
39677c478bd9Sstevel@tonic-gate 
39687c478bd9Sstevel@tonic-gate 	/*
39697c478bd9Sstevel@tonic-gate 	 * Now we deal with the somewhat tricky updating and renaming
39707c478bd9Sstevel@tonic-gate 	 * of this critical piece of kernel state.
39717c478bd9Sstevel@tonic-gate 	 */
39727c478bd9Sstevel@tonic-gate 
39737c478bd9Sstevel@tonic-gate 	/*
39747c478bd9Sstevel@tonic-gate 	 * Copy the current instance file into a temporary file.
39757c478bd9Sstevel@tonic-gate 	 * Then rename the temporary file into the backup (.old)
39767c478bd9Sstevel@tonic-gate 	 * file and rename the newly flushed kernel data into
39777c478bd9Sstevel@tonic-gate 	 * the instance file.
39787c478bd9Sstevel@tonic-gate 	 * Of course if 'inst_file' doesn't exist, there's much
39797c478bd9Sstevel@tonic-gate 	 * less for us to do .. tee hee.
39807c478bd9Sstevel@tonic-gate 	 */
39817c478bd9Sstevel@tonic-gate 	if ((inst_file_fp = fopen(inst_file, "r")) == NULL) {
39827c478bd9Sstevel@tonic-gate 		/*
39837c478bd9Sstevel@tonic-gate 		 * No such file.  Rename the new onto the old
39847c478bd9Sstevel@tonic-gate 		 */
39857c478bd9Sstevel@tonic-gate 		if ((err = rename(new_inst_file, inst_file)) != 0)
39867c478bd9Sstevel@tonic-gate 			err_print(RENAME_FAILED, inst_file, strerror(errno));
39877c478bd9Sstevel@tonic-gate 		goto out;
39887c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
39897c478bd9Sstevel@tonic-gate 	}
39907c478bd9Sstevel@tonic-gate 
39917c478bd9Sstevel@tonic-gate 	(void) snprintf(old_inst_file, inst_strlen + PID_STR_LEN + 6,
39927c478bd9Sstevel@tonic-gate 		"%s.old.%ld", inst_file, getpid());
39937c478bd9Sstevel@tonic-gate 
39947c478bd9Sstevel@tonic-gate 	if (stat(old_inst_file, &sb) == 0) {
39957c478bd9Sstevel@tonic-gate 		s_unlink(old_inst_file);
39967c478bd9Sstevel@tonic-gate 	}
39977c478bd9Sstevel@tonic-gate 
39987c478bd9Sstevel@tonic-gate 	if ((old_inst_file_fp = fopen(old_inst_file, "w")) == NULL) {
39997c478bd9Sstevel@tonic-gate 		/*
40007c478bd9Sstevel@tonic-gate 		 * Can't open the 'old_inst_file' file for writing.
40017c478bd9Sstevel@tonic-gate 		 * This is somewhat strange given that the syscall
40027c478bd9Sstevel@tonic-gate 		 * just succeeded to write a file out.. hmm.. maybe
40037c478bd9Sstevel@tonic-gate 		 * the fs just filled up or something nasty.
40047c478bd9Sstevel@tonic-gate 		 *
40057c478bd9Sstevel@tonic-gate 		 * Anyway, abort what we've done so far.
40067c478bd9Sstevel@tonic-gate 		 */
40077c478bd9Sstevel@tonic-gate 		err_print(CANT_UPDATE, old_inst_file);
40087c478bd9Sstevel@tonic-gate 		err = DEVFSADM_FAILURE;
40097c478bd9Sstevel@tonic-gate 		goto out;
40107c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
40117c478bd9Sstevel@tonic-gate 	}
40127c478bd9Sstevel@tonic-gate 
40137c478bd9Sstevel@tonic-gate 	/*
40147c478bd9Sstevel@tonic-gate 	 * Copy current instance file into the temporary file
40157c478bd9Sstevel@tonic-gate 	 */
40167c478bd9Sstevel@tonic-gate 	err = 0;
40177c478bd9Sstevel@tonic-gate 	while ((c = getc(inst_file_fp)) != EOF) {
40187c478bd9Sstevel@tonic-gate 		if ((err = putc(c, old_inst_file_fp)) == EOF) {
40197c478bd9Sstevel@tonic-gate 			break;
40207c478bd9Sstevel@tonic-gate 		}
40217c478bd9Sstevel@tonic-gate 	}
40227c478bd9Sstevel@tonic-gate 
40237c478bd9Sstevel@tonic-gate 	if (fclose(old_inst_file_fp) == EOF || err == EOF) {
40247c478bd9Sstevel@tonic-gate 		vprint(INFO_MID, CANT_UPDATE, old_inst_file);
40257c478bd9Sstevel@tonic-gate 		err = DEVFSADM_FAILURE;
40267c478bd9Sstevel@tonic-gate 		goto out;
40277c478bd9Sstevel@tonic-gate 		/* NOTREACHED */
40287c478bd9Sstevel@tonic-gate 	}
40297c478bd9Sstevel@tonic-gate 
40307c478bd9Sstevel@tonic-gate 	/*
40317c478bd9Sstevel@tonic-gate 	 * Set permissions to be the same on the backup as
40327c478bd9Sstevel@tonic-gate 	 * /etc/path_to_inst.
40337c478bd9Sstevel@tonic-gate 	 */
40347c478bd9Sstevel@tonic-gate 	(void) chmod(old_inst_file, 0444);
40357c478bd9Sstevel@tonic-gate 
40367c478bd9Sstevel@tonic-gate 	/*
40377c478bd9Sstevel@tonic-gate 	 * So far, everything we've done is more or less reversible.
40387c478bd9Sstevel@tonic-gate 	 * But now we're going to commit ourselves.
40397c478bd9Sstevel@tonic-gate 	 */
40407c478bd9Sstevel@tonic-gate 
40417c478bd9Sstevel@tonic-gate 	(void) snprintf(old_inst_file_npid,
40427c478bd9Sstevel@tonic-gate 	    inst_strlen + sizeof (INSTANCE_FILE_SUFFIX),
40437c478bd9Sstevel@tonic-gate 	    "%s%s", inst_file, INSTANCE_FILE_SUFFIX);
40447c478bd9Sstevel@tonic-gate 
40457c478bd9Sstevel@tonic-gate 	if ((err = rename(old_inst_file, old_inst_file_npid)) != 0) {
40467c478bd9Sstevel@tonic-gate 		err_print(RENAME_FAILED, old_inst_file_npid,
40477c478bd9Sstevel@tonic-gate 				strerror(errno));
40487c478bd9Sstevel@tonic-gate 	} else if ((err = rename(new_inst_file, inst_file)) != 0) {
40497c478bd9Sstevel@tonic-gate 		err_print(RENAME_FAILED, inst_file, strerror(errno));
40507c478bd9Sstevel@tonic-gate 	}
40517c478bd9Sstevel@tonic-gate 
40527c478bd9Sstevel@tonic-gate out:
40537c478bd9Sstevel@tonic-gate 	if (inst_file_fp != NULL) {
40547c478bd9Sstevel@tonic-gate 		if (fclose(inst_file_fp) == EOF) {
40557c478bd9Sstevel@tonic-gate 			err_print(FCLOSE_FAILED, inst_file, strerror(errno));
40567c478bd9Sstevel@tonic-gate 		}
40577c478bd9Sstevel@tonic-gate 	}
40587c478bd9Sstevel@tonic-gate 
40597c478bd9Sstevel@tonic-gate 	if (stat(new_inst_file, &sb) == 0) {
40607c478bd9Sstevel@tonic-gate 		s_unlink(new_inst_file);
40617c478bd9Sstevel@tonic-gate 	}
40627c478bd9Sstevel@tonic-gate 	free(new_inst_file);
40637c478bd9Sstevel@tonic-gate 
40647c478bd9Sstevel@tonic-gate 	if (stat(old_inst_file, &sb) == 0) {
40657c478bd9Sstevel@tonic-gate 		s_unlink(old_inst_file);
40667c478bd9Sstevel@tonic-gate 	}
40677c478bd9Sstevel@tonic-gate 	free(old_inst_file);
40687c478bd9Sstevel@tonic-gate 
40697c478bd9Sstevel@tonic-gate 	free(old_inst_file_npid);
40707c478bd9Sstevel@tonic-gate 
40717c478bd9Sstevel@tonic-gate 	if (err != 0 && err != EALREADY) {
40727c478bd9Sstevel@tonic-gate 		err_print(FAILED_TO_UPDATE, inst_file);
40737c478bd9Sstevel@tonic-gate 	}
40747c478bd9Sstevel@tonic-gate }
40757c478bd9Sstevel@tonic-gate 
40767c478bd9Sstevel@tonic-gate /*
40777c478bd9Sstevel@tonic-gate  * detach from tty.  For daemon mode.
40787c478bd9Sstevel@tonic-gate  */
40797c478bd9Sstevel@tonic-gate void
40807c478bd9Sstevel@tonic-gate detachfromtty()
40817c478bd9Sstevel@tonic-gate {
40827c478bd9Sstevel@tonic-gate 	(void) setsid();
40837c478bd9Sstevel@tonic-gate 	if (DEVFSADM_DEBUG_ON == TRUE) {
40847c478bd9Sstevel@tonic-gate 		return;
40857c478bd9Sstevel@tonic-gate 	}
40867c478bd9Sstevel@tonic-gate 
40877c478bd9Sstevel@tonic-gate 	(void) close(0);
40887c478bd9Sstevel@tonic-gate 	(void) close(1);
40897c478bd9Sstevel@tonic-gate 	(void) close(2);
40907c478bd9Sstevel@tonic-gate 	(void) open("/dev/null", O_RDWR, 0);
40917c478bd9Sstevel@tonic-gate 	(void) dup(0);
40927c478bd9Sstevel@tonic-gate 	(void) dup(0);
40937c478bd9Sstevel@tonic-gate 	openlog(DEVFSADMD, LOG_PID, LOG_DAEMON);
40947c478bd9Sstevel@tonic-gate 	(void) setlogmask(LOG_UPTO(LOG_INFO));
40957c478bd9Sstevel@tonic-gate 	logflag = TRUE;
40967c478bd9Sstevel@tonic-gate }
40977c478bd9Sstevel@tonic-gate 
40987c478bd9Sstevel@tonic-gate /*
40997c478bd9Sstevel@tonic-gate  * Use an advisory lock to synchronize updates to /dev.  If the lock is
41007c478bd9Sstevel@tonic-gate  * held by another process, block in the fcntl() system call until that
41017c478bd9Sstevel@tonic-gate  * process drops the lock or exits.  The lock file itself is
41027c478bd9Sstevel@tonic-gate  * DEV_LOCK_FILE.  The process id of the current and last process owning
41037c478bd9Sstevel@tonic-gate  * the lock is kept in the lock file.  After acquiring the lock, read the
41047c478bd9Sstevel@tonic-gate  * process id and return it.  It is the process ID which last owned the
41057c478bd9Sstevel@tonic-gate  * lock, and will be used to determine if caches need to be flushed.
41067c478bd9Sstevel@tonic-gate  *
41077c478bd9Sstevel@tonic-gate  * NOTE: if the devlink database is held open by the caller, it may
41087c478bd9Sstevel@tonic-gate  * be closed by this routine. This is to enforce the following lock ordering:
41097c478bd9Sstevel@tonic-gate  *	1) /dev lock 2) database open
41107c478bd9Sstevel@tonic-gate  */
41117c478bd9Sstevel@tonic-gate pid_t
41127c478bd9Sstevel@tonic-gate enter_dev_lock()
41137c478bd9Sstevel@tonic-gate {
41147c478bd9Sstevel@tonic-gate 	struct flock lock;
41157c478bd9Sstevel@tonic-gate 	int n;
41167c478bd9Sstevel@tonic-gate 	pid_t pid;
41177c478bd9Sstevel@tonic-gate 	pid_t last_owner_pid;
41187c478bd9Sstevel@tonic-gate 
41197c478bd9Sstevel@tonic-gate 	if (file_mods == FALSE) {
41207c478bd9Sstevel@tonic-gate 		return (0);
41217c478bd9Sstevel@tonic-gate 	}
41227c478bd9Sstevel@tonic-gate 
41237c478bd9Sstevel@tonic-gate 	s_mkdirp(dev_dir, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
41247c478bd9Sstevel@tonic-gate 	(void) snprintf(dev_lockfile, sizeof (dev_lockfile),
41257c478bd9Sstevel@tonic-gate 	    "%s/%s", dev_dir, DEV_LOCK_FILE);
41267c478bd9Sstevel@tonic-gate 
41277c478bd9Sstevel@tonic-gate 	vprint(LOCK_MID, "enter_dev_lock: lock file %s\n", dev_lockfile);
41287c478bd9Sstevel@tonic-gate 
41297c478bd9Sstevel@tonic-gate 	dev_lock_fd = open(dev_lockfile, O_CREAT|O_RDWR, 0644);
41307c478bd9Sstevel@tonic-gate 	if (dev_lock_fd < 0) {
41317c478bd9Sstevel@tonic-gate 		err_print(OPEN_FAILED, dev_lockfile, strerror(errno));
41327c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
41337c478bd9Sstevel@tonic-gate 	}
41347c478bd9Sstevel@tonic-gate 
41357c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
41367c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
41377c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
41387c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
41397c478bd9Sstevel@tonic-gate 
41407c478bd9Sstevel@tonic-gate 	/* try for the lock, but don't wait */
41417c478bd9Sstevel@tonic-gate 	if (fcntl(dev_lock_fd, F_SETLK, &lock) == -1) {
41427c478bd9Sstevel@tonic-gate 		if ((errno == EACCES) || (errno == EAGAIN)) {
41437c478bd9Sstevel@tonic-gate 			pid = 0;
41447c478bd9Sstevel@tonic-gate 			n = read(dev_lock_fd, &pid, sizeof (pid_t));
41457c478bd9Sstevel@tonic-gate 			vprint(LOCK_MID, "waiting for PID %d to complete\n",
41467c478bd9Sstevel@tonic-gate 				(int)pid);
41477c478bd9Sstevel@tonic-gate 			if (lseek(dev_lock_fd, 0, SEEK_SET) == (off_t)-1) {
41487c478bd9Sstevel@tonic-gate 				err_print(LSEEK_FAILED, dev_lockfile,
41497c478bd9Sstevel@tonic-gate 						strerror(errno));
41507c478bd9Sstevel@tonic-gate 				devfsadm_exit(1);
41517c478bd9Sstevel@tonic-gate 			}
41527c478bd9Sstevel@tonic-gate 			/*
41537c478bd9Sstevel@tonic-gate 			 * wait for the dev lock. If we have the database open,
41547c478bd9Sstevel@tonic-gate 			 * close it first - the order of lock acquisition should
41557c478bd9Sstevel@tonic-gate 			 * always be:  1) dev_lock 2) database
41567c478bd9Sstevel@tonic-gate 			 * This is to prevent deadlocks with any locks the
41577c478bd9Sstevel@tonic-gate 			 * database code may hold.
41587c478bd9Sstevel@tonic-gate 			 */
41597c478bd9Sstevel@tonic-gate 			(void) di_devlink_close(&devlink_cache, 0);
41607c478bd9Sstevel@tonic-gate 			if (fcntl(dev_lock_fd, F_SETLKW, &lock) == -1) {
41617c478bd9Sstevel@tonic-gate 				err_print(LOCK_FAILED, dev_lockfile,
41627c478bd9Sstevel@tonic-gate 						strerror(errno));
41637c478bd9Sstevel@tonic-gate 				devfsadm_exit(1);
41647c478bd9Sstevel@tonic-gate 			}
41657c478bd9Sstevel@tonic-gate 		}
41667c478bd9Sstevel@tonic-gate 	}
41677c478bd9Sstevel@tonic-gate 
41687c478bd9Sstevel@tonic-gate 	hold_dev_lock = TRUE;
41697c478bd9Sstevel@tonic-gate 	pid = 0;
41707c478bd9Sstevel@tonic-gate 	n = read(dev_lock_fd, &pid, sizeof (pid_t));
41717c478bd9Sstevel@tonic-gate 	if (n == sizeof (pid_t) && pid == getpid()) {
41727c478bd9Sstevel@tonic-gate 		return (pid);
41737c478bd9Sstevel@tonic-gate 	}
41747c478bd9Sstevel@tonic-gate 
41757c478bd9Sstevel@tonic-gate 	last_owner_pid = pid;
41767c478bd9Sstevel@tonic-gate 
41777c478bd9Sstevel@tonic-gate 	if (lseek(dev_lock_fd, 0, SEEK_SET) == (off_t)-1) {
41787c478bd9Sstevel@tonic-gate 		err_print(LSEEK_FAILED, dev_lockfile, strerror(errno));
41797c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
41807c478bd9Sstevel@tonic-gate 	}
41817c478bd9Sstevel@tonic-gate 	pid = getpid();
41827c478bd9Sstevel@tonic-gate 	n = write(dev_lock_fd, &pid, sizeof (pid_t));
41837c478bd9Sstevel@tonic-gate 	if (n != sizeof (pid_t)) {
41847c478bd9Sstevel@tonic-gate 		err_print(WRITE_FAILED, dev_lockfile, strerror(errno));
41857c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
41867c478bd9Sstevel@tonic-gate 	}
41877c478bd9Sstevel@tonic-gate 
41887c478bd9Sstevel@tonic-gate 	return (last_owner_pid);
41897c478bd9Sstevel@tonic-gate }
41907c478bd9Sstevel@tonic-gate 
41917c478bd9Sstevel@tonic-gate /*
41927c478bd9Sstevel@tonic-gate  * Drop the advisory /dev lock, close lock file.  Close and re-open the
41937c478bd9Sstevel@tonic-gate  * file every time so to ensure a resync if for some reason the lock file
41947c478bd9Sstevel@tonic-gate  * gets removed.
41957c478bd9Sstevel@tonic-gate  */
41967c478bd9Sstevel@tonic-gate void
41977c478bd9Sstevel@tonic-gate exit_dev_lock()
41987c478bd9Sstevel@tonic-gate {
41997c478bd9Sstevel@tonic-gate 	struct flock unlock;
42007c478bd9Sstevel@tonic-gate 
42017c478bd9Sstevel@tonic-gate 	if (hold_dev_lock == FALSE) {
42027c478bd9Sstevel@tonic-gate 		return;
42037c478bd9Sstevel@tonic-gate 	}
42047c478bd9Sstevel@tonic-gate 
42057c478bd9Sstevel@tonic-gate 	vprint(LOCK_MID, "exit_dev_lock: lock file %s\n", dev_lockfile);
42067c478bd9Sstevel@tonic-gate 
42077c478bd9Sstevel@tonic-gate 	unlock.l_type = F_UNLCK;
42087c478bd9Sstevel@tonic-gate 	unlock.l_whence = SEEK_SET;
42097c478bd9Sstevel@tonic-gate 	unlock.l_start = 0;
42107c478bd9Sstevel@tonic-gate 	unlock.l_len = 0;
42117c478bd9Sstevel@tonic-gate 
42127c478bd9Sstevel@tonic-gate 	if (fcntl(dev_lock_fd, F_SETLK, &unlock) == -1) {
42137c478bd9Sstevel@tonic-gate 		err_print(UNLOCK_FAILED, dev_lockfile, strerror(errno));
42147c478bd9Sstevel@tonic-gate 	}
42157c478bd9Sstevel@tonic-gate 
42167c478bd9Sstevel@tonic-gate 	hold_dev_lock = FALSE;
42177c478bd9Sstevel@tonic-gate 
42187c478bd9Sstevel@tonic-gate 	if (close(dev_lock_fd) == -1) {
42197c478bd9Sstevel@tonic-gate 		err_print(CLOSE_FAILED, dev_lockfile, strerror(errno));
42207c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
42217c478bd9Sstevel@tonic-gate 	}
42227c478bd9Sstevel@tonic-gate }
42237c478bd9Sstevel@tonic-gate 
42247c478bd9Sstevel@tonic-gate /*
42257c478bd9Sstevel@tonic-gate  *
42267c478bd9Sstevel@tonic-gate  * Use an advisory lock to ensure that only one daemon process is active
42277c478bd9Sstevel@tonic-gate  * in the system at any point in time.	If the lock is held by another
42287c478bd9Sstevel@tonic-gate  * process, do not block but return the pid owner of the lock to the
42297c478bd9Sstevel@tonic-gate  * caller immediately.	The lock is cleared if the holding daemon process
42307c478bd9Sstevel@tonic-gate  * exits for any reason even if the lock file remains, so the daemon can
42317c478bd9Sstevel@tonic-gate  * be restarted if necessary.  The lock file is DAEMON_LOCK_FILE.
42327c478bd9Sstevel@tonic-gate  */
42337c478bd9Sstevel@tonic-gate pid_t
42347c478bd9Sstevel@tonic-gate enter_daemon_lock(void)
42357c478bd9Sstevel@tonic-gate {
42367c478bd9Sstevel@tonic-gate 	struct flock lock;
42377c478bd9Sstevel@tonic-gate 
42387c478bd9Sstevel@tonic-gate 	s_mkdirp(dev_dir, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
42397c478bd9Sstevel@tonic-gate 	(void) snprintf(daemon_lockfile, sizeof (daemon_lockfile),
42407c478bd9Sstevel@tonic-gate 	    "%s/%s", dev_dir, DAEMON_LOCK_FILE);
42417c478bd9Sstevel@tonic-gate 
42427c478bd9Sstevel@tonic-gate 	vprint(LOCK_MID, "enter_daemon_lock: lock file %s\n", daemon_lockfile);
42437c478bd9Sstevel@tonic-gate 
42447c478bd9Sstevel@tonic-gate 	daemon_lock_fd = open(daemon_lockfile, O_CREAT|O_RDWR, 0644);
42457c478bd9Sstevel@tonic-gate 	if (daemon_lock_fd < 0) {
42467c478bd9Sstevel@tonic-gate 		err_print(OPEN_FAILED, daemon_lockfile, strerror(errno));
42477c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
42487c478bd9Sstevel@tonic-gate 	}
42497c478bd9Sstevel@tonic-gate 
42507c478bd9Sstevel@tonic-gate 	lock.l_type = F_WRLCK;
42517c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
42527c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
42537c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
42547c478bd9Sstevel@tonic-gate 
42557c478bd9Sstevel@tonic-gate 	if (fcntl(daemon_lock_fd, F_SETLK, &lock) == -1) {
42567c478bd9Sstevel@tonic-gate 
42577c478bd9Sstevel@tonic-gate 		if (errno == EAGAIN || errno == EDEADLK) {
42587c478bd9Sstevel@tonic-gate 			if (fcntl(daemon_lock_fd, F_GETLK, &lock) == -1) {
42597c478bd9Sstevel@tonic-gate 				err_print(LOCK_FAILED, daemon_lockfile,
42607c478bd9Sstevel@tonic-gate 						strerror(errno));
42617c478bd9Sstevel@tonic-gate 				devfsadm_exit(1);
42627c478bd9Sstevel@tonic-gate 			}
42637c478bd9Sstevel@tonic-gate 			return (lock.l_pid);
42647c478bd9Sstevel@tonic-gate 		}
42657c478bd9Sstevel@tonic-gate 	}
42667c478bd9Sstevel@tonic-gate 	hold_daemon_lock = TRUE;
42677c478bd9Sstevel@tonic-gate 	return (getpid());
42687c478bd9Sstevel@tonic-gate }
42697c478bd9Sstevel@tonic-gate 
42707c478bd9Sstevel@tonic-gate /*
42717c478bd9Sstevel@tonic-gate  * Drop the advisory daemon lock, close lock file
42727c478bd9Sstevel@tonic-gate  */
42737c478bd9Sstevel@tonic-gate void
42747c478bd9Sstevel@tonic-gate exit_daemon_lock(void)
42757c478bd9Sstevel@tonic-gate {
42767c478bd9Sstevel@tonic-gate 	struct flock lock;
42777c478bd9Sstevel@tonic-gate 
42787c478bd9Sstevel@tonic-gate 	if (hold_daemon_lock == FALSE) {
42797c478bd9Sstevel@tonic-gate 		return;
42807c478bd9Sstevel@tonic-gate 	}
42817c478bd9Sstevel@tonic-gate 
42827c478bd9Sstevel@tonic-gate 	vprint(LOCK_MID, "exit_daemon_lock: lock file %s\n", daemon_lockfile);
42837c478bd9Sstevel@tonic-gate 
42847c478bd9Sstevel@tonic-gate 	lock.l_type = F_UNLCK;
42857c478bd9Sstevel@tonic-gate 	lock.l_whence = SEEK_SET;
42867c478bd9Sstevel@tonic-gate 	lock.l_start = 0;
42877c478bd9Sstevel@tonic-gate 	lock.l_len = 0;
42887c478bd9Sstevel@tonic-gate 
42897c478bd9Sstevel@tonic-gate 	if (fcntl(daemon_lock_fd, F_SETLK, &lock) == -1) {
42907c478bd9Sstevel@tonic-gate 		err_print(UNLOCK_FAILED, daemon_lockfile, strerror(errno));
42917c478bd9Sstevel@tonic-gate 	}
42927c478bd9Sstevel@tonic-gate 
42937c478bd9Sstevel@tonic-gate 	if (close(daemon_lock_fd) == -1) {
42947c478bd9Sstevel@tonic-gate 		err_print(CLOSE_FAILED, daemon_lockfile, strerror(errno));
42957c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
42967c478bd9Sstevel@tonic-gate 	}
42977c478bd9Sstevel@tonic-gate }
42987c478bd9Sstevel@tonic-gate 
42997c478bd9Sstevel@tonic-gate /*
43007c478bd9Sstevel@tonic-gate  * Called to removed danging nodes in two different modes: RM_PRE, RM_POST.
43017c478bd9Sstevel@tonic-gate  * RM_PRE mode is called before processing the entire devinfo tree, and RM_POST
43027c478bd9Sstevel@tonic-gate  * is called after processing the entire devinfo tree.
43037c478bd9Sstevel@tonic-gate  */
43047c478bd9Sstevel@tonic-gate static void
43057c478bd9Sstevel@tonic-gate pre_and_post_cleanup(int flags)
43067c478bd9Sstevel@tonic-gate {
43077c478bd9Sstevel@tonic-gate 	remove_list_t *rm;
43087c478bd9Sstevel@tonic-gate 	recurse_dev_t rd;
43097c478bd9Sstevel@tonic-gate 	cleanup_data_t cleanup_data;
43107c478bd9Sstevel@tonic-gate 	char *fcn = "pre_and_post_cleanup: ";
43117c478bd9Sstevel@tonic-gate 
43127c478bd9Sstevel@tonic-gate 	if (build_dev == FALSE)
43137c478bd9Sstevel@tonic-gate 		return;
43147c478bd9Sstevel@tonic-gate 
43157c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "attempting %s-cleanup\n",
43167c478bd9Sstevel@tonic-gate 	    flags == RM_PRE ? "pre" : "post");
43177c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%sflags = %d\n", fcn, flags);
43187c478bd9Sstevel@tonic-gate 
43197c478bd9Sstevel@tonic-gate 	/*
43207c478bd9Sstevel@tonic-gate 	 * the generic function recurse_dev_re is shared among different
43217c478bd9Sstevel@tonic-gate 	 * functions, so set the method and data that it should use for
43227c478bd9Sstevel@tonic-gate 	 * matches.
43237c478bd9Sstevel@tonic-gate 	 */
43247c478bd9Sstevel@tonic-gate 	rd.fcn = matching_dev;
43257c478bd9Sstevel@tonic-gate 	rd.data = (void *)&cleanup_data;
43267c478bd9Sstevel@tonic-gate 	cleanup_data.flags = flags;
43277c478bd9Sstevel@tonic-gate 
43287c478bd9Sstevel@tonic-gate 	for (rm = remove_head; rm != NULL; rm = rm->next) {
43297c478bd9Sstevel@tonic-gate 		if ((flags & rm->remove->flags) == flags) {
43307c478bd9Sstevel@tonic-gate 			cleanup_data.rm = rm;
43317c478bd9Sstevel@tonic-gate 			/*
43327c478bd9Sstevel@tonic-gate 			 * If reached this point, RM_PRE or RM_POST cleanup is
43337c478bd9Sstevel@tonic-gate 			 * desired.  clean_ok() decides whether to clean
43347c478bd9Sstevel@tonic-gate 			 * under the given circumstances.
43357c478bd9Sstevel@tonic-gate 			 */
43367c478bd9Sstevel@tonic-gate 			vprint(REMOVE_MID, "%scleanup: PRE or POST\n", fcn);
43377c478bd9Sstevel@tonic-gate 			if (clean_ok(rm->remove) == DEVFSADM_SUCCESS) {
43387c478bd9Sstevel@tonic-gate 				vprint(REMOVE_MID, "cleanup: cleanup OK\n");
43397c478bd9Sstevel@tonic-gate 				recurse_dev_re(dev_dir, rm->remove->
43407c478bd9Sstevel@tonic-gate 					dev_dirs_re, &rd);
43417c478bd9Sstevel@tonic-gate 			}
43427c478bd9Sstevel@tonic-gate 		}
43437c478bd9Sstevel@tonic-gate 	}
43447c478bd9Sstevel@tonic-gate }
43457c478bd9Sstevel@tonic-gate 
43467c478bd9Sstevel@tonic-gate /*
43477c478bd9Sstevel@tonic-gate  * clean_ok() determines whether cleanup should be done according
43487c478bd9Sstevel@tonic-gate  * to the following matrix:
43497c478bd9Sstevel@tonic-gate  *
43507c478bd9Sstevel@tonic-gate  * command line arguments RM_PRE    RM_POST	  RM_PRE &&    RM_POST &&
43517c478bd9Sstevel@tonic-gate  *						  RM_ALWAYS    RM_ALWAYS
43527c478bd9Sstevel@tonic-gate  * ---------------------- ------     -----	  ---------    ----------
43537c478bd9Sstevel@tonic-gate  *
43547c478bd9Sstevel@tonic-gate  * <neither -c nor -C>	  -	    -		  pre-clean    post-clean
43557c478bd9Sstevel@tonic-gate  *
43567c478bd9Sstevel@tonic-gate  * -C			  pre-clean  post-clean   pre-clean    post-clean
43577c478bd9Sstevel@tonic-gate  *
43587c478bd9Sstevel@tonic-gate  * -C -c class		  pre-clean  post-clean   pre-clean    post-clean
43597c478bd9Sstevel@tonic-gate  *			  if class  if class	  if class     if class
43607c478bd9Sstevel@tonic-gate  *			  matches   matches	  matches      matches
43617c478bd9Sstevel@tonic-gate  *
43627c478bd9Sstevel@tonic-gate  * -c class		   -	       -	  pre-clean    post-clean
43637c478bd9Sstevel@tonic-gate  *						  if class     if class
43647c478bd9Sstevel@tonic-gate  *						  matches      matches
43657c478bd9Sstevel@tonic-gate  *
43667c478bd9Sstevel@tonic-gate  */
43677c478bd9Sstevel@tonic-gate static int
43687c478bd9Sstevel@tonic-gate clean_ok(devfsadm_remove_t *remove)
43697c478bd9Sstevel@tonic-gate {
43707c478bd9Sstevel@tonic-gate 	int i;
43717c478bd9Sstevel@tonic-gate 
43727c478bd9Sstevel@tonic-gate 	if (single_drv == TRUE) {
43737c478bd9Sstevel@tonic-gate 		/* no cleanup at all when using -i option */
43747c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
43757c478bd9Sstevel@tonic-gate 	}
43767c478bd9Sstevel@tonic-gate 
43777c478bd9Sstevel@tonic-gate 	/*
43787c478bd9Sstevel@tonic-gate 	 * no cleanup if drivers are not loaded. We make an exception
43797c478bd9Sstevel@tonic-gate 	 * for the "disks" program however, since disks has a public
43807c478bd9Sstevel@tonic-gate 	 * cleanup flag (-C) and disk drivers are usually never
43817c478bd9Sstevel@tonic-gate 	 * unloaded.
43827c478bd9Sstevel@tonic-gate 	 */
43837c478bd9Sstevel@tonic-gate 	if (load_attach_drv == FALSE && strcmp(prog, DISKS) != 0) {
43847c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
43857c478bd9Sstevel@tonic-gate 	}
43867c478bd9Sstevel@tonic-gate 
43877c478bd9Sstevel@tonic-gate 	/* if the cleanup flag was not specified, return false */
43887c478bd9Sstevel@tonic-gate 	if ((cleanup == FALSE) && ((remove->flags & RM_ALWAYS) == 0)) {
43897c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
43907c478bd9Sstevel@tonic-gate 	}
43917c478bd9Sstevel@tonic-gate 
43927c478bd9Sstevel@tonic-gate 	if (num_classes == 0) {
43937c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
43947c478bd9Sstevel@tonic-gate 	}
43957c478bd9Sstevel@tonic-gate 
43967c478bd9Sstevel@tonic-gate 	/*
43977c478bd9Sstevel@tonic-gate 	 * if reached this point, check to see if the class in the given
43987c478bd9Sstevel@tonic-gate 	 * remove structure matches a class given on the command line
43997c478bd9Sstevel@tonic-gate 	 */
44007c478bd9Sstevel@tonic-gate 
44017c478bd9Sstevel@tonic-gate 	for (i = 0; i < num_classes; i++) {
44027c478bd9Sstevel@tonic-gate 		if (strcmp(remove->device_class, classes[i]) == 0) {
44037c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
44047c478bd9Sstevel@tonic-gate 		}
44057c478bd9Sstevel@tonic-gate 	}
44067c478bd9Sstevel@tonic-gate 
44077c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
44087c478bd9Sstevel@tonic-gate }
44097c478bd9Sstevel@tonic-gate 
44107c478bd9Sstevel@tonic-gate /*
44117c478bd9Sstevel@tonic-gate  * Called to remove dangling nodes after receiving a hotplug event
44127c478bd9Sstevel@tonic-gate  * containing the physical node pathname to be removed.
44137c478bd9Sstevel@tonic-gate  */
44147c478bd9Sstevel@tonic-gate void
44157c478bd9Sstevel@tonic-gate hot_cleanup(char *node_path, char *minor_name, char *ev_subclass,
44167c478bd9Sstevel@tonic-gate     char *driver_name, int instance)
44177c478bd9Sstevel@tonic-gate {
44187c478bd9Sstevel@tonic-gate 	link_t *link;
44197c478bd9Sstevel@tonic-gate 	linkhead_t *head;
44207c478bd9Sstevel@tonic-gate 	remove_list_t *rm;
44217c478bd9Sstevel@tonic-gate 	char *fcn = "hot_cleanup: ";
44227c478bd9Sstevel@tonic-gate 	char path[PATH_MAX + 1];
44237c478bd9Sstevel@tonic-gate 	int path_len;
44247c478bd9Sstevel@tonic-gate 	char rmlink[PATH_MAX + 1];
44257c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
4426fb9251a6Scth 	int skip;
44277c478bd9Sstevel@tonic-gate 
44287c478bd9Sstevel@tonic-gate 	/*
44297c478bd9Sstevel@tonic-gate 	 * dev links can go away as part of hot cleanup.
44307c478bd9Sstevel@tonic-gate 	 * So first build event attributes in order capture dev links.
44317c478bd9Sstevel@tonic-gate 	 */
44327c478bd9Sstevel@tonic-gate 	if (ev_subclass != NULL)
44337c478bd9Sstevel@tonic-gate 		nvl = build_event_attributes(EC_DEV_REMOVE, ev_subclass,
44347c478bd9Sstevel@tonic-gate 		    node_path, DI_NODE_NIL, driver_name, instance);
44357c478bd9Sstevel@tonic-gate 
44367c478bd9Sstevel@tonic-gate 	(void) strcpy(path, node_path);
44377c478bd9Sstevel@tonic-gate 	(void) strcat(path, ":");
44387c478bd9Sstevel@tonic-gate 	(void) strcat(path, minor_name == NULL ? "" : minor_name);
44397c478bd9Sstevel@tonic-gate 
44407c478bd9Sstevel@tonic-gate 	path_len = strlen(path);
44417c478bd9Sstevel@tonic-gate 
44427c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%spath=%s\n", fcn, path);
44437c478bd9Sstevel@tonic-gate 
44447c478bd9Sstevel@tonic-gate 	for (rm = remove_head; rm != NULL; rm = rm->next) {
44457c478bd9Sstevel@tonic-gate 		if ((RM_HOT & rm->remove->flags) == RM_HOT) {
44467c478bd9Sstevel@tonic-gate 			head = get_cached_links(rm->remove->dev_dirs_re);
44477c478bd9Sstevel@tonic-gate 			assert(head->nextlink == NULL);
44487c478bd9Sstevel@tonic-gate 			for (link = head->link;
44497c478bd9Sstevel@tonic-gate 			    link != NULL; link = head->nextlink) {
44507c478bd9Sstevel@tonic-gate 				/*
44517c478bd9Sstevel@tonic-gate 				 * The remove callback below may remove
44527c478bd9Sstevel@tonic-gate 				 * the current and/or any or all of the
44537c478bd9Sstevel@tonic-gate 				 * subsequent links in the list.
44547c478bd9Sstevel@tonic-gate 				 * Save the next link in the head. If
44557c478bd9Sstevel@tonic-gate 				 * the callback removes the next link
44567c478bd9Sstevel@tonic-gate 				 * the saved pointer in the head will be
44577c478bd9Sstevel@tonic-gate 				 * updated by the callback to point at
44587c478bd9Sstevel@tonic-gate 				 * the next valid link.
44597c478bd9Sstevel@tonic-gate 				 */
44607c478bd9Sstevel@tonic-gate 				head->nextlink = link->next;
4461fb9251a6Scth 				if (minor_name)
4462fb9251a6Scth 					skip = strcmp(link->contents, path);
4463fb9251a6Scth 				else
4464fb9251a6Scth 					skip = strncmp(link->contents, path,
4465fb9251a6Scth 					    path_len);
4466fb9251a6Scth 				if (skip ||
4467fb9251a6Scth 				    (call_minor_init(rm->modptr) ==
4468fb9251a6Scth 				    DEVFSADM_FAILURE))
4469fb9251a6Scth 					continue;
44707c478bd9Sstevel@tonic-gate 
4471fb9251a6Scth 				vprint(REMOVE_MID,
4472fb9251a6Scth 					"%sremoving %s -> %s\n", fcn,
4473fb9251a6Scth 					link->devlink, link->contents);
4474fb9251a6Scth 				/*
4475fb9251a6Scth 				 * Use a copy of the cached link name
4476fb9251a6Scth 				 * as the cache entry will go away
4477fb9251a6Scth 				 * during link removal
4478fb9251a6Scth 				 */
4479fb9251a6Scth 				(void) snprintf(rmlink, sizeof (rmlink),
4480fb9251a6Scth 				    "%s", link->devlink);
4481fb9251a6Scth 				(*(rm->remove->callback_fcn))(rmlink);
44827c478bd9Sstevel@tonic-gate 			}
44837c478bd9Sstevel@tonic-gate 		}
44847c478bd9Sstevel@tonic-gate 	}
44857c478bd9Sstevel@tonic-gate 
44867c478bd9Sstevel@tonic-gate 	/* now log an event */
44877c478bd9Sstevel@tonic-gate 	if (nvl) {
44887c478bd9Sstevel@tonic-gate 		log_event(EC_DEV_REMOVE, ev_subclass, nvl);
44897c478bd9Sstevel@tonic-gate 		free(nvl);
44907c478bd9Sstevel@tonic-gate 	}
44917c478bd9Sstevel@tonic-gate }
44927c478bd9Sstevel@tonic-gate 
44937c478bd9Sstevel@tonic-gate /*
44947c478bd9Sstevel@tonic-gate  * Open the dir current_dir.  For every file which matches the first dir
44957c478bd9Sstevel@tonic-gate  * component of path_re, recurse.  If there are no more *dir* path
44967c478bd9Sstevel@tonic-gate  * components left in path_re (ie no more /), then call function rd->fcn.
44977c478bd9Sstevel@tonic-gate  */
44987c478bd9Sstevel@tonic-gate static void
44997c478bd9Sstevel@tonic-gate recurse_dev_re(char *current_dir, char *path_re, recurse_dev_t *rd)
45007c478bd9Sstevel@tonic-gate {
45017c478bd9Sstevel@tonic-gate 	regex_t re1;
45027c478bd9Sstevel@tonic-gate 	char *slash;
45037c478bd9Sstevel@tonic-gate 	char new_path[PATH_MAX + 1];
45047c478bd9Sstevel@tonic-gate 	char *anchored_path_re;
45057c478bd9Sstevel@tonic-gate 	struct dirent *entp;
45067c478bd9Sstevel@tonic-gate 	DIR *dp;
45077c478bd9Sstevel@tonic-gate 	size_t len;
45087c478bd9Sstevel@tonic-gate 
45097c478bd9Sstevel@tonic-gate 	vprint(RECURSEDEV_MID, "recurse_dev_re: curr = %s path=%s\n",
45107c478bd9Sstevel@tonic-gate 		current_dir, path_re);
45117c478bd9Sstevel@tonic-gate 
45127c478bd9Sstevel@tonic-gate 	if ((dp = opendir(current_dir)) == NULL) {
45137c478bd9Sstevel@tonic-gate 		return;
45147c478bd9Sstevel@tonic-gate 	}
45157c478bd9Sstevel@tonic-gate 
45167c478bd9Sstevel@tonic-gate 	len = strlen(path_re);
45177c478bd9Sstevel@tonic-gate 	if ((slash = strchr(path_re, '/')) != NULL) {
45187c478bd9Sstevel@tonic-gate 		len = (slash - path_re);
45197c478bd9Sstevel@tonic-gate 	}
45207c478bd9Sstevel@tonic-gate 
45217c478bd9Sstevel@tonic-gate 	anchored_path_re = s_malloc(len + 3);
45227c478bd9Sstevel@tonic-gate 	(void) sprintf(anchored_path_re, "^%.*s$", len, path_re);
45237c478bd9Sstevel@tonic-gate 
45247c478bd9Sstevel@tonic-gate 	if (regcomp(&re1, anchored_path_re, REG_EXTENDED) != 0) {
45257c478bd9Sstevel@tonic-gate 		free(anchored_path_re);
45267c478bd9Sstevel@tonic-gate 		goto out;
45277c478bd9Sstevel@tonic-gate 	}
45287c478bd9Sstevel@tonic-gate 
45297c478bd9Sstevel@tonic-gate 	free(anchored_path_re);
45307c478bd9Sstevel@tonic-gate 
45314bc0a2efScasper 	while ((entp = readdir(dp)) != NULL) {
45327c478bd9Sstevel@tonic-gate 
45337c478bd9Sstevel@tonic-gate 		if (strcmp(entp->d_name, ".") == 0 ||
45347c478bd9Sstevel@tonic-gate 		    strcmp(entp->d_name, "..") == 0) {
45357c478bd9Sstevel@tonic-gate 			continue;
45367c478bd9Sstevel@tonic-gate 		}
45377c478bd9Sstevel@tonic-gate 
45387c478bd9Sstevel@tonic-gate 		if (regexec(&re1, entp->d_name, 0, NULL, 0) == 0) {
45397c478bd9Sstevel@tonic-gate 			/* match */
45407c478bd9Sstevel@tonic-gate 			(void) strcpy(new_path, current_dir);
45417c478bd9Sstevel@tonic-gate 			(void) strcat(new_path, "/");
45427c478bd9Sstevel@tonic-gate 			(void) strcat(new_path, entp->d_name);
45437c478bd9Sstevel@tonic-gate 
45447c478bd9Sstevel@tonic-gate 			vprint(RECURSEDEV_MID, "recurse_dev_re: match, new "
45457c478bd9Sstevel@tonic-gate 				"path = %s\n", new_path);
45467c478bd9Sstevel@tonic-gate 
45477c478bd9Sstevel@tonic-gate 			if (slash != NULL) {
45487c478bd9Sstevel@tonic-gate 				recurse_dev_re(new_path, slash + 1, rd);
45497c478bd9Sstevel@tonic-gate 			} else {
45507c478bd9Sstevel@tonic-gate 				/* reached the leaf component of path_re */
45517c478bd9Sstevel@tonic-gate 				vprint(RECURSEDEV_MID,
45527c478bd9Sstevel@tonic-gate 					"recurse_dev_re: calling fcn\n");
45537c478bd9Sstevel@tonic-gate 				(*(rd->fcn))(new_path, rd->data);
45547c478bd9Sstevel@tonic-gate 			}
45557c478bd9Sstevel@tonic-gate 		}
45567c478bd9Sstevel@tonic-gate 	}
45577c478bd9Sstevel@tonic-gate 
45587c478bd9Sstevel@tonic-gate 	regfree(&re1);
45597c478bd9Sstevel@tonic-gate 
45607c478bd9Sstevel@tonic-gate out:
45617c478bd9Sstevel@tonic-gate 	s_closedir(dp);
45627c478bd9Sstevel@tonic-gate }
45637c478bd9Sstevel@tonic-gate 
45647c478bd9Sstevel@tonic-gate /*
45657c478bd9Sstevel@tonic-gate  *  Found a devpath which matches a RE in the remove structure.
45667c478bd9Sstevel@tonic-gate  *  Now check to see if it is dangling.
45677c478bd9Sstevel@tonic-gate  */
45687c478bd9Sstevel@tonic-gate static void
45697c478bd9Sstevel@tonic-gate matching_dev(char *devpath, void *data)
45707c478bd9Sstevel@tonic-gate {
45717c478bd9Sstevel@tonic-gate 	cleanup_data_t *cleanup_data = data;
45727c478bd9Sstevel@tonic-gate 	char *fcn = "matching_dev: ";
45737c478bd9Sstevel@tonic-gate 
45747c478bd9Sstevel@tonic-gate 	vprint(RECURSEDEV_MID, "%sexamining devpath = '%s'\n", fcn,
45757c478bd9Sstevel@tonic-gate 			devpath);
45767c478bd9Sstevel@tonic-gate 
45777c478bd9Sstevel@tonic-gate 	if (resolve_link(devpath, NULL, NULL, NULL, 1) == TRUE) {
45787c478bd9Sstevel@tonic-gate 		if (call_minor_init(cleanup_data->rm->modptr) ==
45797c478bd9Sstevel@tonic-gate 				DEVFSADM_FAILURE) {
45807c478bd9Sstevel@tonic-gate 			return;
45817c478bd9Sstevel@tonic-gate 		}
45827c478bd9Sstevel@tonic-gate 
45837c478bd9Sstevel@tonic-gate 		devpath += strlen(dev_dir) + strlen("/");
45847c478bd9Sstevel@tonic-gate 
45857c478bd9Sstevel@tonic-gate 		vprint(RECURSEDEV_MID, "%scalling"
45867c478bd9Sstevel@tonic-gate 			" callback %s\n", fcn, devpath);
45877c478bd9Sstevel@tonic-gate 		(*(cleanup_data->rm->remove->callback_fcn))(devpath);
45887c478bd9Sstevel@tonic-gate 	}
45897c478bd9Sstevel@tonic-gate }
45907c478bd9Sstevel@tonic-gate 
45917c478bd9Sstevel@tonic-gate int
45927c478bd9Sstevel@tonic-gate devfsadm_read_link(char *link, char **devfs_path)
45937c478bd9Sstevel@tonic-gate {
45947c478bd9Sstevel@tonic-gate 	char devlink[PATH_MAX];
45957c478bd9Sstevel@tonic-gate 
45967c478bd9Sstevel@tonic-gate 	*devfs_path = NULL;
45977c478bd9Sstevel@tonic-gate 
45987c478bd9Sstevel@tonic-gate 	/* prepend link with dev_dir contents */
45997c478bd9Sstevel@tonic-gate 	(void) strcpy(devlink, dev_dir);
46007c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, "/");
46017c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, link);
46027c478bd9Sstevel@tonic-gate 
46037c478bd9Sstevel@tonic-gate 	/* We *don't* want a stat of the /devices node */
46047c478bd9Sstevel@tonic-gate 	(void) resolve_link(devlink, NULL, NULL, devfs_path, 0);
46057c478bd9Sstevel@tonic-gate 
46067c478bd9Sstevel@tonic-gate 	return (*devfs_path ? DEVFSADM_SUCCESS : DEVFSADM_FAILURE);
46077c478bd9Sstevel@tonic-gate }
46087c478bd9Sstevel@tonic-gate 
46097c478bd9Sstevel@tonic-gate int
46107c478bd9Sstevel@tonic-gate devfsadm_link_valid(char *link)
46117c478bd9Sstevel@tonic-gate {
46127c478bd9Sstevel@tonic-gate 	struct stat sb;
46137c478bd9Sstevel@tonic-gate 	char devlink[PATH_MAX + 1], *contents;
46147c478bd9Sstevel@tonic-gate 	int rv, type;
46157c478bd9Sstevel@tonic-gate 
46167c478bd9Sstevel@tonic-gate 	/* prepend link with dev_dir contents */
46177c478bd9Sstevel@tonic-gate 	(void) strcpy(devlink, dev_dir);
46187c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, "/");
46197c478bd9Sstevel@tonic-gate 	(void) strcat(devlink, link);
46207c478bd9Sstevel@tonic-gate 
46217c478bd9Sstevel@tonic-gate 	if (lstat(devlink, &sb) != 0) {
46227c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FALSE);
46237c478bd9Sstevel@tonic-gate 	}
46247c478bd9Sstevel@tonic-gate 
46257c478bd9Sstevel@tonic-gate 	contents = NULL;
46267c478bd9Sstevel@tonic-gate 	type = 0;
46277c478bd9Sstevel@tonic-gate 	if (resolve_link(devlink, &contents, &type, NULL, 1) == TRUE) {
46287c478bd9Sstevel@tonic-gate 		rv = DEVFSADM_FALSE;
46297c478bd9Sstevel@tonic-gate 	} else {
46307c478bd9Sstevel@tonic-gate 		rv = DEVFSADM_TRUE;
46317c478bd9Sstevel@tonic-gate 	}
46327c478bd9Sstevel@tonic-gate 
46337c478bd9Sstevel@tonic-gate 	/*
46347c478bd9Sstevel@tonic-gate 	 * The link exists. Add it to the database
46357c478bd9Sstevel@tonic-gate 	 */
46367c478bd9Sstevel@tonic-gate 	(void) di_devlink_add_link(devlink_cache, link, contents, type);
46377c478bd9Sstevel@tonic-gate 	free(contents);
46387c478bd9Sstevel@tonic-gate 
46397c478bd9Sstevel@tonic-gate 	return (rv);
46407c478bd9Sstevel@tonic-gate }
46417c478bd9Sstevel@tonic-gate 
46427c478bd9Sstevel@tonic-gate /*
46437c478bd9Sstevel@tonic-gate  * devpath: Absolute path to /dev link
46447c478bd9Sstevel@tonic-gate  * content_p: Returns malloced string (link content)
46457c478bd9Sstevel@tonic-gate  * type_p: Returns link type: primary or secondary
46467c478bd9Sstevel@tonic-gate  * devfs_path: Returns malloced string: /devices path w/out "/devices"
46477c478bd9Sstevel@tonic-gate  * dangle: if set, check if link is dangling
46487c478bd9Sstevel@tonic-gate  * Returns:
46497c478bd9Sstevel@tonic-gate  *	TRUE if dangling
46507c478bd9Sstevel@tonic-gate  *	FALSE if not or if caller doesn't care
46517c478bd9Sstevel@tonic-gate  * Caller is assumed to have initialized pointer contents to NULL
46527c478bd9Sstevel@tonic-gate  */
46537c478bd9Sstevel@tonic-gate static int
46547c478bd9Sstevel@tonic-gate resolve_link(char *devpath, char **content_p, int *type_p, char **devfs_path,
46557c478bd9Sstevel@tonic-gate     int dangle)
46567c478bd9Sstevel@tonic-gate {
46577c478bd9Sstevel@tonic-gate 	char contents[PATH_MAX + 1];
46587c478bd9Sstevel@tonic-gate 	char stage_link[PATH_MAX + 1];
46597c478bd9Sstevel@tonic-gate 	char *fcn = "resolve_link: ";
46607c478bd9Sstevel@tonic-gate 	char *ptr;
46617c478bd9Sstevel@tonic-gate 	int linksize;
46627c478bd9Sstevel@tonic-gate 	int rv = TRUE;
46637c478bd9Sstevel@tonic-gate 	struct stat sb;
46647c478bd9Sstevel@tonic-gate 
46657c478bd9Sstevel@tonic-gate 	linksize = readlink(devpath, contents, PATH_MAX);
46667c478bd9Sstevel@tonic-gate 
46677c478bd9Sstevel@tonic-gate 	if (linksize <= 0) {
46687c478bd9Sstevel@tonic-gate 		return (FALSE);
46697c478bd9Sstevel@tonic-gate 	} else {
46707c478bd9Sstevel@tonic-gate 		contents[linksize] = '\0';
46717c478bd9Sstevel@tonic-gate 	}
46727c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%s %s -> %s\n", fcn, devpath, contents);
46737c478bd9Sstevel@tonic-gate 
46747c478bd9Sstevel@tonic-gate 	if (content_p) {
46757c478bd9Sstevel@tonic-gate 		*content_p = s_strdup(contents);
46767c478bd9Sstevel@tonic-gate 	}
46777c478bd9Sstevel@tonic-gate 
46787c478bd9Sstevel@tonic-gate 	/*
46797c478bd9Sstevel@tonic-gate 	 * Check to see if this is a link pointing to another link in /dev.  The
46807c478bd9Sstevel@tonic-gate 	 * cheap way to do this is to look for a lack of ../devices/.
46817c478bd9Sstevel@tonic-gate 	 */
46827c478bd9Sstevel@tonic-gate 
46837c478bd9Sstevel@tonic-gate 	if (is_minor_node(contents, &ptr) == DEVFSADM_FALSE) {
46847c478bd9Sstevel@tonic-gate 
46857c478bd9Sstevel@tonic-gate 		if (type_p) {
46867c478bd9Sstevel@tonic-gate 			*type_p = DI_SECONDARY_LINK;
46877c478bd9Sstevel@tonic-gate 		}
46887c478bd9Sstevel@tonic-gate 
46897c478bd9Sstevel@tonic-gate 		/*
46907c478bd9Sstevel@tonic-gate 		 * assume that linkcontents is really a pointer to another
46917c478bd9Sstevel@tonic-gate 		 * link, and if so recurse and read its link contents.
46927c478bd9Sstevel@tonic-gate 		 */
46937c478bd9Sstevel@tonic-gate 		if (strncmp(contents, DEV "/", strlen(DEV) + 1) == 0)  {
46947c478bd9Sstevel@tonic-gate 			(void) strcpy(stage_link, dev_dir);
46957c478bd9Sstevel@tonic-gate 			(void) strcat(stage_link, "/");
46967c478bd9Sstevel@tonic-gate 			(void) strcpy(stage_link,
46977c478bd9Sstevel@tonic-gate 					&contents[strlen(DEV) + strlen("/")]);
46987c478bd9Sstevel@tonic-gate 		} else {
46997c478bd9Sstevel@tonic-gate 			if ((ptr = strrchr(devpath, '/')) == NULL) {
47007c478bd9Sstevel@tonic-gate 				vprint(REMOVE_MID, "%s%s -> %s invalid link. "
47017c478bd9Sstevel@tonic-gate 					"missing '/'\n", fcn, devpath,
47027c478bd9Sstevel@tonic-gate 					contents);
47037c478bd9Sstevel@tonic-gate 				return (TRUE);
47047c478bd9Sstevel@tonic-gate 			}
47057c478bd9Sstevel@tonic-gate 			*ptr = '\0';
47067c478bd9Sstevel@tonic-gate 			(void) strcpy(stage_link, devpath);
47077c478bd9Sstevel@tonic-gate 			*ptr = '/';
47087c478bd9Sstevel@tonic-gate 			(void) strcat(stage_link, "/");
47097c478bd9Sstevel@tonic-gate 			(void) strcat(stage_link, contents);
47107c478bd9Sstevel@tonic-gate 		}
47117c478bd9Sstevel@tonic-gate 		return (resolve_link(stage_link, NULL, NULL, devfs_path,
47127c478bd9Sstevel@tonic-gate 		    dangle));
47137c478bd9Sstevel@tonic-gate 	}
47147c478bd9Sstevel@tonic-gate 
47157c478bd9Sstevel@tonic-gate 	/* Current link points at a /devices minor node */
47167c478bd9Sstevel@tonic-gate 	if (type_p) {
47177c478bd9Sstevel@tonic-gate 		*type_p = DI_PRIMARY_LINK;
47187c478bd9Sstevel@tonic-gate 	}
47197c478bd9Sstevel@tonic-gate 
47207c478bd9Sstevel@tonic-gate 	if (devfs_path)
47217c478bd9Sstevel@tonic-gate 		*devfs_path = s_strdup(ptr);
47227c478bd9Sstevel@tonic-gate 
47237c478bd9Sstevel@tonic-gate 	rv = FALSE;
47247c478bd9Sstevel@tonic-gate 	if (dangle)
47257c478bd9Sstevel@tonic-gate 		rv = (stat(ptr - strlen(DEVICES), &sb) == -1);
47267c478bd9Sstevel@tonic-gate 
47277c478bd9Sstevel@tonic-gate 	vprint(REMOVE_MID, "%slink=%s, returning %s\n", fcn,
47287c478bd9Sstevel@tonic-gate 			devpath, ((rv == TRUE) ? "TRUE" : "FALSE"));
47297c478bd9Sstevel@tonic-gate 
47307c478bd9Sstevel@tonic-gate 	return (rv);
47317c478bd9Sstevel@tonic-gate }
47327c478bd9Sstevel@tonic-gate 
47337c478bd9Sstevel@tonic-gate /*
47347c478bd9Sstevel@tonic-gate  * Returns the substring of interest, given a path.
47357c478bd9Sstevel@tonic-gate  */
47367c478bd9Sstevel@tonic-gate static char *
47377c478bd9Sstevel@tonic-gate alloc_cmp_str(const char *path, devfsadm_enumerate_t *dep)
47387c478bd9Sstevel@tonic-gate {
47397c478bd9Sstevel@tonic-gate 	uint_t match;
47407c478bd9Sstevel@tonic-gate 	char *np, *ap, *mp;
47417c478bd9Sstevel@tonic-gate 	char *cmp_str = NULL;
47427c478bd9Sstevel@tonic-gate 	char at[] = "@";
47437c478bd9Sstevel@tonic-gate 	char *fcn = "alloc_cmp_str";
47447c478bd9Sstevel@tonic-gate 
47457c478bd9Sstevel@tonic-gate 	np = ap = mp = NULL;
47467c478bd9Sstevel@tonic-gate 
47477c478bd9Sstevel@tonic-gate 	/*
47487c478bd9Sstevel@tonic-gate 	 * extract match flags from the flags argument.
47497c478bd9Sstevel@tonic-gate 	 */
47507c478bd9Sstevel@tonic-gate 	match = (dep->flags & MATCH_MASK);
47517c478bd9Sstevel@tonic-gate 
47527c478bd9Sstevel@tonic-gate 	vprint(ENUM_MID, "%s: enumeration match type: 0x%x"
47537c478bd9Sstevel@tonic-gate 	    " path: %s\n", fcn, match, path);
47547c478bd9Sstevel@tonic-gate 
47557c478bd9Sstevel@tonic-gate 	/*
47567c478bd9Sstevel@tonic-gate 	 * MATCH_CALLBACK and MATCH_ALL are the only flags
47577c478bd9Sstevel@tonic-gate 	 * which may be used if "path" is a /dev path
47587c478bd9Sstevel@tonic-gate 	 */
47597c478bd9Sstevel@tonic-gate 	if (match == MATCH_CALLBACK) {
47607c478bd9Sstevel@tonic-gate 		if (dep->sel_fcn == NULL) {
47617c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid enumerate"
47627c478bd9Sstevel@tonic-gate 			    " callback: path: %s\n", fcn, path);
47637c478bd9Sstevel@tonic-gate 			return (NULL);
47647c478bd9Sstevel@tonic-gate 		}
47657c478bd9Sstevel@tonic-gate 		cmp_str = dep->sel_fcn(path, dep->cb_arg);
47667c478bd9Sstevel@tonic-gate 		return (cmp_str);
47677c478bd9Sstevel@tonic-gate 	}
47687c478bd9Sstevel@tonic-gate 
47697c478bd9Sstevel@tonic-gate 	cmp_str = s_strdup(path);
47707c478bd9Sstevel@tonic-gate 
47717c478bd9Sstevel@tonic-gate 	if (match == MATCH_ALL) {
47727c478bd9Sstevel@tonic-gate 		return (cmp_str);
47737c478bd9Sstevel@tonic-gate 	}
47747c478bd9Sstevel@tonic-gate 
47757c478bd9Sstevel@tonic-gate 	/*
47767c478bd9Sstevel@tonic-gate 	 * The remaining flags make sense only for /devices
47777c478bd9Sstevel@tonic-gate 	 * paths
47787c478bd9Sstevel@tonic-gate 	 */
47797c478bd9Sstevel@tonic-gate 	if ((mp = strrchr(cmp_str, ':')) == NULL) {
47807c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: invalid path: %s\n",
47817c478bd9Sstevel@tonic-gate 		    fcn, path);
47827c478bd9Sstevel@tonic-gate 		goto err;
47837c478bd9Sstevel@tonic-gate 	}
47847c478bd9Sstevel@tonic-gate 
47857c478bd9Sstevel@tonic-gate 	if (match == MATCH_MINOR) {
47867c478bd9Sstevel@tonic-gate 		/* A NULL "match_arg" values implies entire minor */
47877c478bd9Sstevel@tonic-gate 		if (get_component(mp + 1, dep->match_arg) == NULL) {
47887c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid minor component:"
47897c478bd9Sstevel@tonic-gate 			    " path: %s\n", fcn, path);
47907c478bd9Sstevel@tonic-gate 			goto err;
47917c478bd9Sstevel@tonic-gate 		}
47927c478bd9Sstevel@tonic-gate 		return (cmp_str);
47937c478bd9Sstevel@tonic-gate 	}
47947c478bd9Sstevel@tonic-gate 
47957c478bd9Sstevel@tonic-gate 	if ((np = strrchr(cmp_str, '/')) == NULL) {
47967c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: invalid path: %s\n", fcn, path);
47977c478bd9Sstevel@tonic-gate 		goto err;
47987c478bd9Sstevel@tonic-gate 	}
47997c478bd9Sstevel@tonic-gate 
48007c478bd9Sstevel@tonic-gate 	if (match == MATCH_PARENT) {
48017c478bd9Sstevel@tonic-gate 		if (strcmp(cmp_str, "/") == 0) {
48027c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid path: %s\n",
48037c478bd9Sstevel@tonic-gate 			    fcn, path);
48047c478bd9Sstevel@tonic-gate 			goto err;
48057c478bd9Sstevel@tonic-gate 		}
48067c478bd9Sstevel@tonic-gate 
48077c478bd9Sstevel@tonic-gate 		if (np == cmp_str) {
48087c478bd9Sstevel@tonic-gate 			*(np + 1) = '\0';
48097c478bd9Sstevel@tonic-gate 		} else {
48107c478bd9Sstevel@tonic-gate 			*np = '\0';
48117c478bd9Sstevel@tonic-gate 		}
48127c478bd9Sstevel@tonic-gate 		return (cmp_str);
48137c478bd9Sstevel@tonic-gate 	}
48147c478bd9Sstevel@tonic-gate 
48157c478bd9Sstevel@tonic-gate 	/* ap can be NULL - Leaf address may not exist or be empty string */
48167c478bd9Sstevel@tonic-gate 	ap = strchr(np+1, '@');
48177c478bd9Sstevel@tonic-gate 
48187c478bd9Sstevel@tonic-gate 	/* minor is no longer of interest */
48197c478bd9Sstevel@tonic-gate 	*mp = '\0';
48207c478bd9Sstevel@tonic-gate 
48217c478bd9Sstevel@tonic-gate 	if (match == MATCH_NODE) {
48227c478bd9Sstevel@tonic-gate 		if (ap)
48237c478bd9Sstevel@tonic-gate 			*ap = '\0';
48247c478bd9Sstevel@tonic-gate 		return (cmp_str);
48257c478bd9Sstevel@tonic-gate 	} else if (match == MATCH_ADDR) {
48267c478bd9Sstevel@tonic-gate 		/*
48277c478bd9Sstevel@tonic-gate 		 * The empty string is a valid address. The only MATCH_ADDR
48287c478bd9Sstevel@tonic-gate 		 * allowed in this case is against the whole address or
48297c478bd9Sstevel@tonic-gate 		 * the first component of the address (match_arg=NULL/"0"/"1")
48307c478bd9Sstevel@tonic-gate 		 * Note that in this case, the path won't have an "@"
48317c478bd9Sstevel@tonic-gate 		 * As a result ap will be NULL. We fake up an ap = @'\0'
48327c478bd9Sstevel@tonic-gate 		 * so that get_component() will work correctly.
48337c478bd9Sstevel@tonic-gate 		 */
48347c478bd9Sstevel@tonic-gate 		if (ap == NULL) {
48357c478bd9Sstevel@tonic-gate 			ap = at;
48367c478bd9Sstevel@tonic-gate 		}
48377c478bd9Sstevel@tonic-gate 
48387c478bd9Sstevel@tonic-gate 		if (get_component(ap + 1, dep->match_arg) == NULL) {
48397c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid leaf addr. component:"
48407c478bd9Sstevel@tonic-gate 			    " path: %s\n", fcn, path);
48417c478bd9Sstevel@tonic-gate 			goto err;
48427c478bd9Sstevel@tonic-gate 		}
48437c478bd9Sstevel@tonic-gate 		return (cmp_str);
48447c478bd9Sstevel@tonic-gate 	}
48457c478bd9Sstevel@tonic-gate 
48467c478bd9Sstevel@tonic-gate 	vprint(ENUM_MID, "%s: invalid enumeration flags: 0x%x"
48477c478bd9Sstevel@tonic-gate 		" path: %s\n", fcn, dep->flags, path);
48487c478bd9Sstevel@tonic-gate 
48497c478bd9Sstevel@tonic-gate 	/*FALLTHRU*/
48507c478bd9Sstevel@tonic-gate err:
48517c478bd9Sstevel@tonic-gate 	free(cmp_str);
48527c478bd9Sstevel@tonic-gate 	return (NULL);
48537c478bd9Sstevel@tonic-gate }
48547c478bd9Sstevel@tonic-gate 
48557c478bd9Sstevel@tonic-gate 
48567c478bd9Sstevel@tonic-gate /*
48577c478bd9Sstevel@tonic-gate  * "str" is expected to be a string with components separated by ','
48587c478bd9Sstevel@tonic-gate  * The terminating null char is considered a separator.
48597c478bd9Sstevel@tonic-gate  * get_component() will remove the portion of the string beyond
48607c478bd9Sstevel@tonic-gate  * the component indicated.
48617c478bd9Sstevel@tonic-gate  * If comp_str is NULL, the entire "str" is returned.
48627c478bd9Sstevel@tonic-gate  */
48637c478bd9Sstevel@tonic-gate static char *
48647c478bd9Sstevel@tonic-gate get_component(char *str, const char *comp_str)
48657c478bd9Sstevel@tonic-gate {
48667c478bd9Sstevel@tonic-gate 	long comp;
48677c478bd9Sstevel@tonic-gate 	char *cp;
48687c478bd9Sstevel@tonic-gate 
48697c478bd9Sstevel@tonic-gate 	if (str == NULL) {
48707c478bd9Sstevel@tonic-gate 		return (NULL);
48717c478bd9Sstevel@tonic-gate 	}
48727c478bd9Sstevel@tonic-gate 
48737c478bd9Sstevel@tonic-gate 	if (comp_str == NULL) {
48747c478bd9Sstevel@tonic-gate 		return (str);
48757c478bd9Sstevel@tonic-gate 	}
48767c478bd9Sstevel@tonic-gate 
48777c478bd9Sstevel@tonic-gate 	errno = 0;
48787c478bd9Sstevel@tonic-gate 	comp = strtol(comp_str, &cp, 10);
48797c478bd9Sstevel@tonic-gate 	if (errno != 0 || *cp != '\0' || comp < 0) {
48807c478bd9Sstevel@tonic-gate 		return (NULL);
48817c478bd9Sstevel@tonic-gate 	}
48827c478bd9Sstevel@tonic-gate 
48837c478bd9Sstevel@tonic-gate 	if (comp == 0)
48847c478bd9Sstevel@tonic-gate 		return (str);
48857c478bd9Sstevel@tonic-gate 
48867c478bd9Sstevel@tonic-gate 	for (cp = str; ; cp++) {
48877c478bd9Sstevel@tonic-gate 		if (*cp == ',' || *cp == '\0')
48887c478bd9Sstevel@tonic-gate 			comp--;
48897c478bd9Sstevel@tonic-gate 		if (*cp == '\0' || comp <= 0) {
48907c478bd9Sstevel@tonic-gate 			break;
48917c478bd9Sstevel@tonic-gate 		}
48927c478bd9Sstevel@tonic-gate 	}
48937c478bd9Sstevel@tonic-gate 
48947c478bd9Sstevel@tonic-gate 	if (comp == 0) {
48957c478bd9Sstevel@tonic-gate 		*cp = '\0';
48967c478bd9Sstevel@tonic-gate 	} else {
48977c478bd9Sstevel@tonic-gate 		str = NULL;
48987c478bd9Sstevel@tonic-gate 	}
48997c478bd9Sstevel@tonic-gate 
49007c478bd9Sstevel@tonic-gate 	return (str);
49017c478bd9Sstevel@tonic-gate }
49027c478bd9Sstevel@tonic-gate 
49037c478bd9Sstevel@tonic-gate 
49047c478bd9Sstevel@tonic-gate /*
49057c478bd9Sstevel@tonic-gate  * Enumerate serves as a generic counter as well as a means to determine
49067c478bd9Sstevel@tonic-gate  * logical unit/controller numbers for such items as disk and tape
49077c478bd9Sstevel@tonic-gate  * drives.
49087c478bd9Sstevel@tonic-gate  *
49097c478bd9Sstevel@tonic-gate  * rules[] is an array of  devfsadm_enumerate_t structures which defines
49107c478bd9Sstevel@tonic-gate  * the enumeration rules to be used for a specified set of links in /dev.
49117c478bd9Sstevel@tonic-gate  * The set of links is specified through regular expressions (of the flavor
49127c478bd9Sstevel@tonic-gate  * described in regex(5)). These regular expressions are used to determine
49137c478bd9Sstevel@tonic-gate  * the set of links in /dev to examine. The last path component in these
49147c478bd9Sstevel@tonic-gate  * regular expressions MUST contain a parenthesized subexpression surrounding
49157c478bd9Sstevel@tonic-gate  * the RE which is to be considered the enumerating component. The subexp
49167c478bd9Sstevel@tonic-gate  * member in a rule is the subexpression number of the enumerating
49177c478bd9Sstevel@tonic-gate  * component. Subexpressions in the last path component are numbered starting
49187c478bd9Sstevel@tonic-gate  * from 1.
49197c478bd9Sstevel@tonic-gate  *
49207c478bd9Sstevel@tonic-gate  * A cache of current id assignments is built up from existing symlinks and
49217c478bd9Sstevel@tonic-gate  * new assignments use the lowest unused id. Assignments are based on a
49227c478bd9Sstevel@tonic-gate  * match of a specified substring of a symlink's contents. If the specified
49237c478bd9Sstevel@tonic-gate  * component for the devfs_path argument matches the corresponding substring
49247c478bd9Sstevel@tonic-gate  * for a existing symlink's contents, the cached id is returned. Else, a new
49257c478bd9Sstevel@tonic-gate  * id is created and returned in *buf. *buf must be freed by the caller.
49267c478bd9Sstevel@tonic-gate  *
49277c478bd9Sstevel@tonic-gate  * An id assignment may be governed by a combination of rules, each rule
49287c478bd9Sstevel@tonic-gate  * applicable to a different subset of links in /dev. For example, controller
49297c478bd9Sstevel@tonic-gate  * numbers may be determined by a combination of disk symlinks in /dev/[r]dsk
49307c478bd9Sstevel@tonic-gate  * and controller symlinks in /dev/cfg, with the two sets requiring different
49317c478bd9Sstevel@tonic-gate  * rules to derive the "substring of interest". In such cases, the rules
49327c478bd9Sstevel@tonic-gate  * array will have more than one element.
49337c478bd9Sstevel@tonic-gate  */
49347c478bd9Sstevel@tonic-gate int
49357c478bd9Sstevel@tonic-gate devfsadm_enumerate_int(char *devfs_path, int index, char **buf,
49367c478bd9Sstevel@tonic-gate 			devfsadm_enumerate_t rules[], int nrules)
49377c478bd9Sstevel@tonic-gate {
49387c478bd9Sstevel@tonic-gate 	return (find_enum_id(rules, nrules,
49397c478bd9Sstevel@tonic-gate 	    devfs_path, index, "0", INTEGER, buf, 0));
49407c478bd9Sstevel@tonic-gate }
49417c478bd9Sstevel@tonic-gate 
49427c478bd9Sstevel@tonic-gate int
49437c478bd9Sstevel@tonic-gate disk_enumerate_int(char *devfs_path, int index, char **buf,
49447c478bd9Sstevel@tonic-gate     devfsadm_enumerate_t rules[], int nrules)
49457c478bd9Sstevel@tonic-gate {
49467c478bd9Sstevel@tonic-gate 	return (find_enum_id(rules, nrules,
49477c478bd9Sstevel@tonic-gate 	    devfs_path, index, "0", INTEGER, buf, 1));
49487c478bd9Sstevel@tonic-gate }
49497c478bd9Sstevel@tonic-gate 
49507c478bd9Sstevel@tonic-gate /*
49517c478bd9Sstevel@tonic-gate  * Same as above, but allows a starting value to be specified.
49527c478bd9Sstevel@tonic-gate  * Private to devfsadm.... used by devlinks.
49537c478bd9Sstevel@tonic-gate  */
49547c478bd9Sstevel@tonic-gate static int
49557c478bd9Sstevel@tonic-gate devfsadm_enumerate_int_start(char *devfs_path, int index, char **buf,
49567c478bd9Sstevel@tonic-gate 		devfsadm_enumerate_t rules[], int nrules, char *start)
49577c478bd9Sstevel@tonic-gate {
49587c478bd9Sstevel@tonic-gate 	return (find_enum_id(rules, nrules,
49597c478bd9Sstevel@tonic-gate 	    devfs_path, index, start, INTEGER, buf, 0));
49607c478bd9Sstevel@tonic-gate }
49617c478bd9Sstevel@tonic-gate 
49627c478bd9Sstevel@tonic-gate /*
49637c478bd9Sstevel@tonic-gate  *  devfsadm_enumerate_char serves as a generic counter returning
49647c478bd9Sstevel@tonic-gate  *  a single letter.
49657c478bd9Sstevel@tonic-gate  */
49667c478bd9Sstevel@tonic-gate int
49677c478bd9Sstevel@tonic-gate devfsadm_enumerate_char(char *devfs_path, int index, char **buf,
49687c478bd9Sstevel@tonic-gate 			devfsadm_enumerate_t rules[], int nrules)
49697c478bd9Sstevel@tonic-gate {
49707c478bd9Sstevel@tonic-gate 	return (find_enum_id(rules, nrules,
49717c478bd9Sstevel@tonic-gate 	    devfs_path, index, "a", LETTER, buf, 0));
49727c478bd9Sstevel@tonic-gate }
49737c478bd9Sstevel@tonic-gate 
49747c478bd9Sstevel@tonic-gate /*
49757c478bd9Sstevel@tonic-gate  * Same as above, but allows a starting char to be specified.
49767c478bd9Sstevel@tonic-gate  * Private to devfsadm - used by ports module (port_link.c)
49777c478bd9Sstevel@tonic-gate  */
49787c478bd9Sstevel@tonic-gate int
49797c478bd9Sstevel@tonic-gate devfsadm_enumerate_char_start(char *devfs_path, int index, char **buf,
49807c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[], int nrules, char *start)
49817c478bd9Sstevel@tonic-gate {
49827c478bd9Sstevel@tonic-gate 	return (find_enum_id(rules, nrules,
49837c478bd9Sstevel@tonic-gate 	    devfs_path, index, start, LETTER, buf, 0));
49847c478bd9Sstevel@tonic-gate }
49857c478bd9Sstevel@tonic-gate 
49867c478bd9Sstevel@tonic-gate 
49877c478bd9Sstevel@tonic-gate /*
49887c478bd9Sstevel@tonic-gate  * For a given numeral_set (see get_cached_set for desc of numeral_set),
49897c478bd9Sstevel@tonic-gate  * search all cached entries looking for matches on a specified substring
49907c478bd9Sstevel@tonic-gate  * of devfs_path. The substring is derived from devfs_path based on the
49917c478bd9Sstevel@tonic-gate  * rule specified by "index". If a match is found on a cached entry,
49927c478bd9Sstevel@tonic-gate  * return the enumerated id in buf. Otherwise, create a new id by calling
49937c478bd9Sstevel@tonic-gate  * new_id, then cache and return that entry.
49947c478bd9Sstevel@tonic-gate  */
49957c478bd9Sstevel@tonic-gate static int
49967c478bd9Sstevel@tonic-gate find_enum_id(devfsadm_enumerate_t rules[], int nrules,
49977c478bd9Sstevel@tonic-gate 	char *devfs_path, int index, char *min, int type, char **buf,
49987c478bd9Sstevel@tonic-gate 	int multiple)
49997c478bd9Sstevel@tonic-gate {
50007c478bd9Sstevel@tonic-gate 	numeral_t *matchnp;
50017c478bd9Sstevel@tonic-gate 	numeral_t *numeral;
50027c478bd9Sstevel@tonic-gate 	int matchcount = 0;
50037c478bd9Sstevel@tonic-gate 	char *cmp_str;
50047c478bd9Sstevel@tonic-gate 	char *fcn = "find_enum_id";
50057c478bd9Sstevel@tonic-gate 	numeral_set_t *set;
50067c478bd9Sstevel@tonic-gate 
50077c478bd9Sstevel@tonic-gate 	if (rules == NULL) {
50087c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: no rules. path: %s\n",
50097c478bd9Sstevel@tonic-gate 		    fcn, devfs_path ? devfs_path : "<NULL path>");
50107c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
50117c478bd9Sstevel@tonic-gate 	}
50127c478bd9Sstevel@tonic-gate 
50137c478bd9Sstevel@tonic-gate 	if (devfs_path == NULL) {
50147c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: NULL path\n", fcn);
50157c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
50167c478bd9Sstevel@tonic-gate 	}
50177c478bd9Sstevel@tonic-gate 
50187c478bd9Sstevel@tonic-gate 	if (nrules <= 0 || index < 0 || index >= nrules || buf == NULL) {
50197c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: invalid arguments. path: %s\n",
50207c478bd9Sstevel@tonic-gate 		    fcn, devfs_path);
50217c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
50227c478bd9Sstevel@tonic-gate 	}
50237c478bd9Sstevel@tonic-gate 
50247c478bd9Sstevel@tonic-gate 	*buf = NULL;
50257c478bd9Sstevel@tonic-gate 
50267c478bd9Sstevel@tonic-gate 
50277c478bd9Sstevel@tonic-gate 	cmp_str = alloc_cmp_str(devfs_path, &rules[index]);
50287c478bd9Sstevel@tonic-gate 	if (cmp_str == NULL) {
50297c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
50307c478bd9Sstevel@tonic-gate 	}
50317c478bd9Sstevel@tonic-gate 
50327c478bd9Sstevel@tonic-gate 	if ((set = get_enum_cache(rules, nrules)) == NULL) {
50337c478bd9Sstevel@tonic-gate 		free(cmp_str);
50347c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
50357c478bd9Sstevel@tonic-gate 	}
50367c478bd9Sstevel@tonic-gate 
50377c478bd9Sstevel@tonic-gate 	assert(nrules == set->re_count);
50387c478bd9Sstevel@tonic-gate 
50397c478bd9Sstevel@tonic-gate 	/*
50407c478bd9Sstevel@tonic-gate 	 * Check and see if a matching entry is already cached.
50417c478bd9Sstevel@tonic-gate 	 */
50427c478bd9Sstevel@tonic-gate 	matchcount = lookup_enum_cache(set, cmp_str, rules, index,
50437c478bd9Sstevel@tonic-gate 	    &matchnp);
50447c478bd9Sstevel@tonic-gate 
50457c478bd9Sstevel@tonic-gate 	if (matchcount < 0 || matchcount > 1) {
50467c478bd9Sstevel@tonic-gate 		free(cmp_str);
50477c478bd9Sstevel@tonic-gate 		if (multiple && matchcount > 1)
50487c478bd9Sstevel@tonic-gate 			return (DEVFSADM_MULTIPLE);
50497c478bd9Sstevel@tonic-gate 		else
50507c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
50517c478bd9Sstevel@tonic-gate 	}
50527c478bd9Sstevel@tonic-gate 
50537c478bd9Sstevel@tonic-gate 	/* if matching entry already cached, return it */
50547c478bd9Sstevel@tonic-gate 	if (matchcount == 1) {
50557c478bd9Sstevel@tonic-gate 		*buf = s_strdup(matchnp->id);
50567c478bd9Sstevel@tonic-gate 		free(cmp_str);
50577c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
50587c478bd9Sstevel@tonic-gate 	}
50597c478bd9Sstevel@tonic-gate 
50607c478bd9Sstevel@tonic-gate 	/*
50617c478bd9Sstevel@tonic-gate 	 * no cached entry, initialize a numeral struct
50627c478bd9Sstevel@tonic-gate 	 * by calling new_id() and cache onto the numeral_set
50637c478bd9Sstevel@tonic-gate 	 */
50647c478bd9Sstevel@tonic-gate 	numeral = s_malloc(sizeof (numeral_t));
50657c478bd9Sstevel@tonic-gate 	numeral->id = new_id(set->headnumeral, type, min);
50667c478bd9Sstevel@tonic-gate 	numeral->full_path = s_strdup(devfs_path);
50677c478bd9Sstevel@tonic-gate 	numeral->rule_index = index;
50687c478bd9Sstevel@tonic-gate 	numeral->cmp_str = cmp_str;
50697c478bd9Sstevel@tonic-gate 	cmp_str = NULL;
50707c478bd9Sstevel@tonic-gate 
50717c478bd9Sstevel@tonic-gate 	/* insert to head of list for fast lookups */
50727c478bd9Sstevel@tonic-gate 	numeral->next = set->headnumeral;
50737c478bd9Sstevel@tonic-gate 	set->headnumeral = numeral;
50747c478bd9Sstevel@tonic-gate 
50757c478bd9Sstevel@tonic-gate 	*buf = s_strdup(numeral->id);
50767c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
50777c478bd9Sstevel@tonic-gate }
50787c478bd9Sstevel@tonic-gate 
50797c478bd9Sstevel@tonic-gate 
50807c478bd9Sstevel@tonic-gate /*
50817c478bd9Sstevel@tonic-gate  * Looks up the specified cache for a match with a specified string
50827c478bd9Sstevel@tonic-gate  * Returns:
50837c478bd9Sstevel@tonic-gate  *	-1	: on error.
50847c478bd9Sstevel@tonic-gate  *	0/1/2	: Number of matches.
50857c478bd9Sstevel@tonic-gate  * Returns the matching element only if there is a single match.
50867c478bd9Sstevel@tonic-gate  * If the "uncached" flag is set, derives the "cmp_str" afresh
50877c478bd9Sstevel@tonic-gate  * for the match instead of using cached values.
50887c478bd9Sstevel@tonic-gate  */
50897c478bd9Sstevel@tonic-gate static int
50907c478bd9Sstevel@tonic-gate lookup_enum_cache(numeral_set_t *set, char *cmp_str,
50917c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[], int index, numeral_t **matchnpp)
50927c478bd9Sstevel@tonic-gate {
50937c478bd9Sstevel@tonic-gate 	int matchcount = 0, rv = -1;
50947c478bd9Sstevel@tonic-gate 	int uncached;
50957c478bd9Sstevel@tonic-gate 	numeral_t *np;
50967c478bd9Sstevel@tonic-gate 	char *fcn = "lookup_enum_cache";
50977c478bd9Sstevel@tonic-gate 	char *cp;
50987c478bd9Sstevel@tonic-gate 
50997c478bd9Sstevel@tonic-gate 	*matchnpp = NULL;
51007c478bd9Sstevel@tonic-gate 
51017c478bd9Sstevel@tonic-gate 	assert(index < set->re_count);
51027c478bd9Sstevel@tonic-gate 
51037c478bd9Sstevel@tonic-gate 	if (cmp_str == NULL) {
51047c478bd9Sstevel@tonic-gate 		return (-1);
51057c478bd9Sstevel@tonic-gate 	}
51067c478bd9Sstevel@tonic-gate 
51077c478bd9Sstevel@tonic-gate 	uncached = 0;
51087c478bd9Sstevel@tonic-gate 	if ((rules[index].flags & MATCH_UNCACHED) == MATCH_UNCACHED) {
51097c478bd9Sstevel@tonic-gate 		uncached = 1;
51107c478bd9Sstevel@tonic-gate 	}
51117c478bd9Sstevel@tonic-gate 
51127c478bd9Sstevel@tonic-gate 	/*
51137c478bd9Sstevel@tonic-gate 	 * Check and see if a matching entry is already cached.
51147c478bd9Sstevel@tonic-gate 	 */
51157c478bd9Sstevel@tonic-gate 	for (np = set->headnumeral; np != NULL; np = np->next) {
51167c478bd9Sstevel@tonic-gate 		if (np->cmp_str == NULL) {
51177c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid entry in enumerate"
51187c478bd9Sstevel@tonic-gate 			    " cache. path: %s\n", fcn, np->full_path);
51197c478bd9Sstevel@tonic-gate 			return (-1);
51207c478bd9Sstevel@tonic-gate 		}
51217c478bd9Sstevel@tonic-gate 
51227c478bd9Sstevel@tonic-gate 		if (uncached) {
51237c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "%s: bypassing enumerate cache."
51247c478bd9Sstevel@tonic-gate 			    " path: %s\n", fcn, cmp_str);
51257c478bd9Sstevel@tonic-gate 			cp = alloc_cmp_str(np->full_path,
51267c478bd9Sstevel@tonic-gate 			    &rules[np->rule_index]);
51277c478bd9Sstevel@tonic-gate 			if (cp == NULL)
51287c478bd9Sstevel@tonic-gate 				return (-1);
51297c478bd9Sstevel@tonic-gate 			rv = strcmp(cmp_str, cp);
51307c478bd9Sstevel@tonic-gate 			free(cp);
51317c478bd9Sstevel@tonic-gate 		} else {
51327c478bd9Sstevel@tonic-gate 			rv = strcmp(cmp_str, np->cmp_str);
51337c478bd9Sstevel@tonic-gate 		}
51347c478bd9Sstevel@tonic-gate 
51357c478bd9Sstevel@tonic-gate 		if (rv == 0) {
51367c478bd9Sstevel@tonic-gate 			if (matchcount++ != 0) {
51377c478bd9Sstevel@tonic-gate 				break; /* more than 1 match. */
51387c478bd9Sstevel@tonic-gate 			}
51397c478bd9Sstevel@tonic-gate 			*matchnpp = np;
51407c478bd9Sstevel@tonic-gate 		}
51417c478bd9Sstevel@tonic-gate 	}
51427c478bd9Sstevel@tonic-gate 
51437c478bd9Sstevel@tonic-gate 	return (matchcount);
51447c478bd9Sstevel@tonic-gate }
51457c478bd9Sstevel@tonic-gate 
51467c478bd9Sstevel@tonic-gate #ifdef	DEBUG
51477c478bd9Sstevel@tonic-gate static void
51487c478bd9Sstevel@tonic-gate dump_enum_cache(numeral_set_t *setp)
51497c478bd9Sstevel@tonic-gate {
51507c478bd9Sstevel@tonic-gate 	int i;
51517c478bd9Sstevel@tonic-gate 	numeral_t *np;
51527c478bd9Sstevel@tonic-gate 	char *fcn = "dump_enum_cache";
51537c478bd9Sstevel@tonic-gate 
51547c478bd9Sstevel@tonic-gate 	vprint(ENUM_MID, "%s: re_count = %d\n", fcn, setp->re_count);
51557c478bd9Sstevel@tonic-gate 	for (i = 0; i < setp->re_count; i++) {
51567c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: re[%d] = %s\n", fcn, i, setp->re[i]);
51577c478bd9Sstevel@tonic-gate 	}
51587c478bd9Sstevel@tonic-gate 
51597c478bd9Sstevel@tonic-gate 	for (np = setp->headnumeral; np != NULL; np = np->next) {
51607c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: id: %s\n", fcn, np->id);
51617c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: full_path: %s\n", fcn, np->full_path);
51627c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: rule_index: %d\n", fcn, np->rule_index);
51637c478bd9Sstevel@tonic-gate 		vprint(ENUM_MID, "%s: cmp_str: %s\n", fcn, np->cmp_str);
51647c478bd9Sstevel@tonic-gate 	}
51657c478bd9Sstevel@tonic-gate }
51667c478bd9Sstevel@tonic-gate #endif
51677c478bd9Sstevel@tonic-gate 
51687c478bd9Sstevel@tonic-gate /*
51697c478bd9Sstevel@tonic-gate  * For a given set of regular expressions in rules[], this function returns
51707c478bd9Sstevel@tonic-gate  * either a previously cached struct numeral_set or it will create and
51717c478bd9Sstevel@tonic-gate  * cache a new struct numeral_set.  There is only one struct numeral_set
51727c478bd9Sstevel@tonic-gate  * for the combination of REs present in rules[].  Each numeral_set contains
51737c478bd9Sstevel@tonic-gate  * the regular expressions in rules[] used for cache selection AND a linked
51747c478bd9Sstevel@tonic-gate  * list of struct numerals, ONE FOR EACH *UNIQUE* numeral or character ID
51757c478bd9Sstevel@tonic-gate  * selected by the grouping parenthesized subexpression found in the last
51767c478bd9Sstevel@tonic-gate  * path component of each rules[].re.  For example, the RE: "rmt/([0-9]+)"
51777c478bd9Sstevel@tonic-gate  * selects all the logical nodes of the correct form in dev/rmt/.
51787c478bd9Sstevel@tonic-gate  * Each rmt/X will store a *single* struct numeral... ie 0, 1, 2 each get a
51797c478bd9Sstevel@tonic-gate  * single struct numeral. There is no need to store more than a single logical
51807c478bd9Sstevel@tonic-gate  * node matching X since the information desired in the devfspath would be
51817c478bd9Sstevel@tonic-gate  * identical for the portion of the devfspath of interest. (the part up to,
51827c478bd9Sstevel@tonic-gate  * but not including the minor name in this example.)
51837c478bd9Sstevel@tonic-gate  *
51847c478bd9Sstevel@tonic-gate  * If the given numeral_set is not yet cached, call enumerate_recurse to
51857c478bd9Sstevel@tonic-gate  * create it.
51867c478bd9Sstevel@tonic-gate  */
51877c478bd9Sstevel@tonic-gate static numeral_set_t *
51887c478bd9Sstevel@tonic-gate get_enum_cache(devfsadm_enumerate_t rules[], int nrules)
51897c478bd9Sstevel@tonic-gate {
51907c478bd9Sstevel@tonic-gate 	/* linked list of numeral sets */
51917c478bd9Sstevel@tonic-gate 	numeral_set_t *setp;
51927c478bd9Sstevel@tonic-gate 	int i;
51937c478bd9Sstevel@tonic-gate 	char *path_left;
51947c478bd9Sstevel@tonic-gate 	char *fcn = "get_enum_cache";
51957c478bd9Sstevel@tonic-gate 
51967c478bd9Sstevel@tonic-gate 	/*
51977c478bd9Sstevel@tonic-gate 	 * See if we've already cached this numeral set.
51987c478bd9Sstevel@tonic-gate 	 */
51997c478bd9Sstevel@tonic-gate 	for (setp = head_numeral_set; setp != NULL; setp = setp->next) {
52007c478bd9Sstevel@tonic-gate 		/*
52017c478bd9Sstevel@tonic-gate 		 *  check all regexp's passed in function against
52027c478bd9Sstevel@tonic-gate 		 *  those in cached set.
52037c478bd9Sstevel@tonic-gate 		 */
52047c478bd9Sstevel@tonic-gate 		if (nrules != setp->re_count) {
52057c478bd9Sstevel@tonic-gate 			continue;
52067c478bd9Sstevel@tonic-gate 		}
52077c478bd9Sstevel@tonic-gate 
52087c478bd9Sstevel@tonic-gate 		for (i = 0; i < nrules; i++) {
52097c478bd9Sstevel@tonic-gate 			if (strcmp(setp->re[i], rules[i].re) != 0) {
52107c478bd9Sstevel@tonic-gate 				break;
52117c478bd9Sstevel@tonic-gate 			}
52127c478bd9Sstevel@tonic-gate 		}
52137c478bd9Sstevel@tonic-gate 
52147c478bd9Sstevel@tonic-gate 		if (i == nrules) {
52157c478bd9Sstevel@tonic-gate 			return (setp);
52167c478bd9Sstevel@tonic-gate 		}
52177c478bd9Sstevel@tonic-gate 	}
52187c478bd9Sstevel@tonic-gate 
52197c478bd9Sstevel@tonic-gate 	/*
52207c478bd9Sstevel@tonic-gate 	 * If the MATCH_UNCACHED flag is set, we should not  be here.
52217c478bd9Sstevel@tonic-gate 	 */
52227c478bd9Sstevel@tonic-gate 	for (i = 0; i < nrules; i++) {
52237c478bd9Sstevel@tonic-gate 		if ((rules[i].flags & MATCH_UNCACHED) == MATCH_UNCACHED) {
52247c478bd9Sstevel@tonic-gate 			vprint(ENUM_MID, "%s: invalid enumeration flags: "
52257c478bd9Sstevel@tonic-gate 			    "0x%x\n", fcn, rules[i].flags);
52267c478bd9Sstevel@tonic-gate 			return (NULL);
52277c478bd9Sstevel@tonic-gate 		}
52287c478bd9Sstevel@tonic-gate 	}
52297c478bd9Sstevel@tonic-gate 
52307c478bd9Sstevel@tonic-gate 	/*
52317c478bd9Sstevel@tonic-gate 	 *  Since we made it here, we have not yet cached the given set of
52327c478bd9Sstevel@tonic-gate 	 *  logical nodes matching the passed re.  Create a cached entry
52337c478bd9Sstevel@tonic-gate 	 *  struct numeral_set and populate it with a minimal set of
52347c478bd9Sstevel@tonic-gate 	 *  logical nodes from /dev.
52357c478bd9Sstevel@tonic-gate 	 */
52367c478bd9Sstevel@tonic-gate 
52377c478bd9Sstevel@tonic-gate 	setp = s_malloc(sizeof (numeral_set_t));
52387c478bd9Sstevel@tonic-gate 	setp->re = s_malloc(sizeof (char *) * nrules);
52397c478bd9Sstevel@tonic-gate 	for (i = 0; i < nrules; i++) {
52407c478bd9Sstevel@tonic-gate 		setp->re[i] = s_strdup(rules[i].re);
52417c478bd9Sstevel@tonic-gate 	}
52427c478bd9Sstevel@tonic-gate 	setp->re_count = nrules;
52437c478bd9Sstevel@tonic-gate 	setp->headnumeral = NULL;
52447c478bd9Sstevel@tonic-gate 
52457c478bd9Sstevel@tonic-gate 	/* put this new cached set on the cached set list */
52467c478bd9Sstevel@tonic-gate 	setp->next = head_numeral_set;
52477c478bd9Sstevel@tonic-gate 	head_numeral_set = setp;
52487c478bd9Sstevel@tonic-gate 
52497c478bd9Sstevel@tonic-gate 	/*
52507c478bd9Sstevel@tonic-gate 	 * For each RE, search disk and cache any matches on the
52517c478bd9Sstevel@tonic-gate 	 * numeral list.  We are careful to use global_dev_dir here since
52527c478bd9Sstevel@tonic-gate 	 * for zones, we want to use the global zone's enumeration as the
52537c478bd9Sstevel@tonic-gate 	 * source for enumeration within the zone.  Otherwise, for example,
52547c478bd9Sstevel@tonic-gate 	 * controller numbering would be wrong within the zone.
52557c478bd9Sstevel@tonic-gate 	 */
52567c478bd9Sstevel@tonic-gate 	for (i = 0; i < nrules; i++) {
52577c478bd9Sstevel@tonic-gate 		path_left = s_strdup(setp->re[i]);
52587c478bd9Sstevel@tonic-gate 		enumerate_recurse(global_dev_dir, path_left, setp, rules, i);
52597c478bd9Sstevel@tonic-gate 		free(path_left);
52607c478bd9Sstevel@tonic-gate 	}
52617c478bd9Sstevel@tonic-gate 
52627c478bd9Sstevel@tonic-gate #ifdef	DEBUG
52637c478bd9Sstevel@tonic-gate 	dump_enum_cache(setp);
52647c478bd9Sstevel@tonic-gate #endif
52657c478bd9Sstevel@tonic-gate 
52667c478bd9Sstevel@tonic-gate 	return (setp);
52677c478bd9Sstevel@tonic-gate }
52687c478bd9Sstevel@tonic-gate 
52697c478bd9Sstevel@tonic-gate 
52707c478bd9Sstevel@tonic-gate /*
52717c478bd9Sstevel@tonic-gate  * This function stats the pathname namebuf.  If this is a directory
52727c478bd9Sstevel@tonic-gate  * entry, we recurse down dname/fname until we find the first symbolic
52737c478bd9Sstevel@tonic-gate  * link, and then stat and return it.  This is valid for the same reason
52747c478bd9Sstevel@tonic-gate  * that we only need to read a single pathname for multiple matching
52757c478bd9Sstevel@tonic-gate  * logical ID's... ie, all the logical nodes should contain identical
52767c478bd9Sstevel@tonic-gate  * physical paths for the parts we are interested.
52777c478bd9Sstevel@tonic-gate  */
52787c478bd9Sstevel@tonic-gate int
52797c478bd9Sstevel@tonic-gate get_stat_info(char *namebuf, struct stat *sb)
52807c478bd9Sstevel@tonic-gate {
52817c478bd9Sstevel@tonic-gate 	struct dirent *entp;
52827c478bd9Sstevel@tonic-gate 	DIR *dp;
52837c478bd9Sstevel@tonic-gate 	char *cp;
52847c478bd9Sstevel@tonic-gate 
52857c478bd9Sstevel@tonic-gate 	if (lstat(namebuf, sb) < 0) {
52867c478bd9Sstevel@tonic-gate 		(void) err_print(LSTAT_FAILED, namebuf, strerror(errno));
52877c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
52887c478bd9Sstevel@tonic-gate 	}
52897c478bd9Sstevel@tonic-gate 
52907c478bd9Sstevel@tonic-gate 	if ((sb->st_mode & S_IFMT) == S_IFLNK) {
52917c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
52927c478bd9Sstevel@tonic-gate 	}
52937c478bd9Sstevel@tonic-gate 
52947c478bd9Sstevel@tonic-gate 	/*
52957c478bd9Sstevel@tonic-gate 	 * If it is a dir, recurse down until we find a link and
52967c478bd9Sstevel@tonic-gate 	 * then use the link.
52977c478bd9Sstevel@tonic-gate 	 */
52987c478bd9Sstevel@tonic-gate 	if ((sb->st_mode & S_IFMT) == S_IFDIR) {
52997c478bd9Sstevel@tonic-gate 
53007c478bd9Sstevel@tonic-gate 		if ((dp = opendir(namebuf)) == NULL) {
53017c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
53027c478bd9Sstevel@tonic-gate 		}
53037c478bd9Sstevel@tonic-gate 
53047c478bd9Sstevel@tonic-gate 		/*
53057c478bd9Sstevel@tonic-gate 		 *  Search each dir entry looking for a symlink.  Return
53067c478bd9Sstevel@tonic-gate 		 *  the first symlink found in namebuf.  Recurse dirs.
53077c478bd9Sstevel@tonic-gate 		 */
53084bc0a2efScasper 		while ((entp = readdir(dp)) != NULL) {
53097c478bd9Sstevel@tonic-gate 			if (strcmp(entp->d_name, ".") == 0 ||
53107c478bd9Sstevel@tonic-gate 			    strcmp(entp->d_name, "..") == 0) {
53117c478bd9Sstevel@tonic-gate 				continue;
53127c478bd9Sstevel@tonic-gate 			}
53137c478bd9Sstevel@tonic-gate 
53147c478bd9Sstevel@tonic-gate 			cp = namebuf + strlen(namebuf);
53157c478bd9Sstevel@tonic-gate 			(void) strcat(namebuf, "/");
53167c478bd9Sstevel@tonic-gate 			(void) strcat(namebuf, entp->d_name);
53177c478bd9Sstevel@tonic-gate 			if (get_stat_info(namebuf, sb) == DEVFSADM_SUCCESS) {
53187c478bd9Sstevel@tonic-gate 				s_closedir(dp);
53197c478bd9Sstevel@tonic-gate 				return (DEVFSADM_SUCCESS);
53207c478bd9Sstevel@tonic-gate 			}
53217c478bd9Sstevel@tonic-gate 			*cp = '\0';
53227c478bd9Sstevel@tonic-gate 		}
53237c478bd9Sstevel@tonic-gate 		s_closedir(dp);
53247c478bd9Sstevel@tonic-gate 	}
53257c478bd9Sstevel@tonic-gate 
53267c478bd9Sstevel@tonic-gate 	/* no symlink found, so return error */
53277c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
53287c478bd9Sstevel@tonic-gate }
53297c478bd9Sstevel@tonic-gate 
53307c478bd9Sstevel@tonic-gate /*
53317c478bd9Sstevel@tonic-gate  * An existing matching ID was not found, so this function is called to
53327c478bd9Sstevel@tonic-gate  * create the next lowest ID.  In the INTEGER case, return the next
53337c478bd9Sstevel@tonic-gate  * lowest unused integer.  In the case of LETTER, return the next lowest
53347c478bd9Sstevel@tonic-gate  * unused letter.  Return empty string if all 26 are used.
53357c478bd9Sstevel@tonic-gate  * Only IDs >= min will be returned.
53367c478bd9Sstevel@tonic-gate  */
53377c478bd9Sstevel@tonic-gate char *
53387c478bd9Sstevel@tonic-gate new_id(numeral_t *numeral, int type, char *min)
53397c478bd9Sstevel@tonic-gate {
53407c478bd9Sstevel@tonic-gate 	int imin;
53417c478bd9Sstevel@tonic-gate 	temp_t *temp;
53427c478bd9Sstevel@tonic-gate 	temp_t *ptr;
53437c478bd9Sstevel@tonic-gate 	temp_t **previous;
53447c478bd9Sstevel@tonic-gate 	temp_t *head = NULL;
53457c478bd9Sstevel@tonic-gate 	char *retval;
53467c478bd9Sstevel@tonic-gate 	static char tempbuff[8];
53477c478bd9Sstevel@tonic-gate 	numeral_t *np;
53487c478bd9Sstevel@tonic-gate 
53497c478bd9Sstevel@tonic-gate 	if (type == LETTER) {
53507c478bd9Sstevel@tonic-gate 
53517c478bd9Sstevel@tonic-gate 		char letter[26], i;
53527c478bd9Sstevel@tonic-gate 
53537c478bd9Sstevel@tonic-gate 		if (numeral == NULL) {
53547c478bd9Sstevel@tonic-gate 			return (s_strdup(min));
53557c478bd9Sstevel@tonic-gate 		}
53567c478bd9Sstevel@tonic-gate 
53577c478bd9Sstevel@tonic-gate 		for (i = 0; i < 26; i++) {
53587c478bd9Sstevel@tonic-gate 			letter[i] = 0;
53597c478bd9Sstevel@tonic-gate 		}
53607c478bd9Sstevel@tonic-gate 
53617c478bd9Sstevel@tonic-gate 		for (np = numeral; np != NULL; np = np->next) {
53627c478bd9Sstevel@tonic-gate 			letter[*np->id - 'a']++;
53637c478bd9Sstevel@tonic-gate 		}
53647c478bd9Sstevel@tonic-gate 
53657c478bd9Sstevel@tonic-gate 		imin = *min - 'a';
53667c478bd9Sstevel@tonic-gate 
53677c478bd9Sstevel@tonic-gate 		for (i = imin; i < 26; i++) {
53687c478bd9Sstevel@tonic-gate 			if (letter[i] == 0) {
53697c478bd9Sstevel@tonic-gate 				retval = s_malloc(2);
53707c478bd9Sstevel@tonic-gate 				retval[0] = 'a' + i;
53717c478bd9Sstevel@tonic-gate 				retval[1] = '\0';
53727c478bd9Sstevel@tonic-gate 				return (retval);
53737c478bd9Sstevel@tonic-gate 			}
53747c478bd9Sstevel@tonic-gate 		}
53757c478bd9Sstevel@tonic-gate 
53767c478bd9Sstevel@tonic-gate 		return (s_strdup(""));
53777c478bd9Sstevel@tonic-gate 	}
53787c478bd9Sstevel@tonic-gate 
53797c478bd9Sstevel@tonic-gate 	if (type == INTEGER) {
53807c478bd9Sstevel@tonic-gate 
53817c478bd9Sstevel@tonic-gate 		if (numeral == NULL) {
53827c478bd9Sstevel@tonic-gate 			return (s_strdup(min));
53837c478bd9Sstevel@tonic-gate 		}
53847c478bd9Sstevel@tonic-gate 
53857c478bd9Sstevel@tonic-gate 		imin = atoi(min);
53867c478bd9Sstevel@tonic-gate 
53877c478bd9Sstevel@tonic-gate 		/* sort list */
53887c478bd9Sstevel@tonic-gate 		for (np = numeral; np != NULL; np = np->next) {
53897c478bd9Sstevel@tonic-gate 			temp = s_malloc(sizeof (temp_t));
53907c478bd9Sstevel@tonic-gate 			temp->integer = atoi(np->id);
53917c478bd9Sstevel@tonic-gate 			temp->next = NULL;
53927c478bd9Sstevel@tonic-gate 
53937c478bd9Sstevel@tonic-gate 			previous = &head;
53947c478bd9Sstevel@tonic-gate 			for (ptr = head; ptr != NULL; ptr = ptr->next) {
53957c478bd9Sstevel@tonic-gate 				if (temp->integer < ptr->integer) {
53967c478bd9Sstevel@tonic-gate 					temp->next = ptr;
53977c478bd9Sstevel@tonic-gate 					*previous = temp;
53987c478bd9Sstevel@tonic-gate 					break;
53997c478bd9Sstevel@tonic-gate 				}
54007c478bd9Sstevel@tonic-gate 				previous = &(ptr->next);
54017c478bd9Sstevel@tonic-gate 			}
54027c478bd9Sstevel@tonic-gate 			if (ptr == NULL) {
54037c478bd9Sstevel@tonic-gate 				*previous = temp;
54047c478bd9Sstevel@tonic-gate 			}
54057c478bd9Sstevel@tonic-gate 		}
54067c478bd9Sstevel@tonic-gate 
54077c478bd9Sstevel@tonic-gate 		/* now search sorted list for first hole >= imin */
54087c478bd9Sstevel@tonic-gate 		for (ptr = head; ptr != NULL; ptr = ptr->next) {
54097c478bd9Sstevel@tonic-gate 			if (imin == ptr->integer) {
54107c478bd9Sstevel@tonic-gate 				imin++;
54117c478bd9Sstevel@tonic-gate 			} else {
54127c478bd9Sstevel@tonic-gate 				if (imin < ptr->integer) {
54137c478bd9Sstevel@tonic-gate 					break;
54147c478bd9Sstevel@tonic-gate 				}
54157c478bd9Sstevel@tonic-gate 			}
54167c478bd9Sstevel@tonic-gate 
54177c478bd9Sstevel@tonic-gate 		}
54187c478bd9Sstevel@tonic-gate 
54197c478bd9Sstevel@tonic-gate 		/* free temp list */
54207c478bd9Sstevel@tonic-gate 		for (ptr = head; ptr != NULL; ) {
54217c478bd9Sstevel@tonic-gate 			temp = ptr;
54227c478bd9Sstevel@tonic-gate 			ptr = ptr->next;
54237c478bd9Sstevel@tonic-gate 			free(temp);
54247c478bd9Sstevel@tonic-gate 		}
54257c478bd9Sstevel@tonic-gate 
54267c478bd9Sstevel@tonic-gate 		(void) sprintf(tempbuff, "%d", imin);
54277c478bd9Sstevel@tonic-gate 		return (s_strdup(tempbuff));
54287c478bd9Sstevel@tonic-gate 	}
54297c478bd9Sstevel@tonic-gate 
54307c478bd9Sstevel@tonic-gate 	return (s_strdup(""));
54317c478bd9Sstevel@tonic-gate }
54327c478bd9Sstevel@tonic-gate 
54337c478bd9Sstevel@tonic-gate /*
54347c478bd9Sstevel@tonic-gate  * Search current_dir for all files which match the first path component
54357c478bd9Sstevel@tonic-gate  * of path_left, which is an RE.  If a match is found, but there are more
54367c478bd9Sstevel@tonic-gate  * components of path_left, then recurse, otherwise, if we have reached
54377c478bd9Sstevel@tonic-gate  * the last component of path_left, call create_cached_numerals for each
54387c478bd9Sstevel@tonic-gate  * file.   At some point, recurse_dev_re() should be rewritten so that this
54397c478bd9Sstevel@tonic-gate  * function can be eliminated.
54407c478bd9Sstevel@tonic-gate  */
54417c478bd9Sstevel@tonic-gate static void
54427c478bd9Sstevel@tonic-gate enumerate_recurse(char *current_dir, char *path_left, numeral_set_t *setp,
54437c478bd9Sstevel@tonic-gate 	    devfsadm_enumerate_t rules[], int index)
54447c478bd9Sstevel@tonic-gate {
54457c478bd9Sstevel@tonic-gate 	char *slash;
54467c478bd9Sstevel@tonic-gate 	char *new_path;
54477c478bd9Sstevel@tonic-gate 	char *numeral_id;
54487c478bd9Sstevel@tonic-gate 	struct dirent *entp;
54497c478bd9Sstevel@tonic-gate 	DIR *dp;
54507c478bd9Sstevel@tonic-gate 
54517c478bd9Sstevel@tonic-gate 	if ((dp = opendir(current_dir)) == NULL) {
54527c478bd9Sstevel@tonic-gate 		return;
54537c478bd9Sstevel@tonic-gate 	}
54547c478bd9Sstevel@tonic-gate 
54557c478bd9Sstevel@tonic-gate 	/* get rid of any extra '/' */
54567c478bd9Sstevel@tonic-gate 	while (*path_left == '/') {
54577c478bd9Sstevel@tonic-gate 		path_left++;
54587c478bd9Sstevel@tonic-gate 	}
54597c478bd9Sstevel@tonic-gate 
54607c478bd9Sstevel@tonic-gate 	if (slash = strchr(path_left, '/')) {
54617c478bd9Sstevel@tonic-gate 		*slash = '\0';
54627c478bd9Sstevel@tonic-gate 	}
54637c478bd9Sstevel@tonic-gate 
54644bc0a2efScasper 	while ((entp = readdir(dp)) != NULL) {
54657c478bd9Sstevel@tonic-gate 
54667c478bd9Sstevel@tonic-gate 		if (strcmp(entp->d_name, ".") == 0 ||
54677c478bd9Sstevel@tonic-gate 		    strcmp(entp->d_name, "..") == 0) {
54687c478bd9Sstevel@tonic-gate 			continue;
54697c478bd9Sstevel@tonic-gate 		}
54707c478bd9Sstevel@tonic-gate 
54717c478bd9Sstevel@tonic-gate 		/*
54727c478bd9Sstevel@tonic-gate 		 *  Returns true if path_left matches entp->d_name
54737c478bd9Sstevel@tonic-gate 		 *  If it is the last path component, pass subexp
54747c478bd9Sstevel@tonic-gate 		 *  so that it will return the corresponding ID in
54757c478bd9Sstevel@tonic-gate 		 *  numeral_id.
54767c478bd9Sstevel@tonic-gate 		 */
54777c478bd9Sstevel@tonic-gate 		numeral_id = NULL;
54787c478bd9Sstevel@tonic-gate 		if (match_path_component(path_left, entp->d_name, &numeral_id,
54797c478bd9Sstevel@tonic-gate 				    slash ? 0 : rules[index].subexp)) {
54807c478bd9Sstevel@tonic-gate 
54817c478bd9Sstevel@tonic-gate 			new_path = s_malloc(strlen(current_dir) +
54827c478bd9Sstevel@tonic-gate 					    strlen(entp->d_name) + 2);
54837c478bd9Sstevel@tonic-gate 
54847c478bd9Sstevel@tonic-gate 			(void) strcpy(new_path, current_dir);
54857c478bd9Sstevel@tonic-gate 			(void) strcat(new_path, "/");
54867c478bd9Sstevel@tonic-gate 			(void) strcat(new_path, entp->d_name);
54877c478bd9Sstevel@tonic-gate 
54887c478bd9Sstevel@tonic-gate 			if (slash != NULL) {
54897c478bd9Sstevel@tonic-gate 				enumerate_recurse(new_path, slash + 1,
54907c478bd9Sstevel@tonic-gate 				    setp, rules, index);
54917c478bd9Sstevel@tonic-gate 			} else {
54927c478bd9Sstevel@tonic-gate 				create_cached_numeral(new_path, setp,
54937c478bd9Sstevel@tonic-gate 				    numeral_id, rules, index);
54947c478bd9Sstevel@tonic-gate 				if (numeral_id != NULL) {
54957c478bd9Sstevel@tonic-gate 					free(numeral_id);
54967c478bd9Sstevel@tonic-gate 				}
54977c478bd9Sstevel@tonic-gate 			}
54987c478bd9Sstevel@tonic-gate 			free(new_path);
54997c478bd9Sstevel@tonic-gate 		}
55007c478bd9Sstevel@tonic-gate 	}
55017c478bd9Sstevel@tonic-gate 
55027c478bd9Sstevel@tonic-gate 	if (slash != NULL) {
55037c478bd9Sstevel@tonic-gate 		*slash = '/';
55047c478bd9Sstevel@tonic-gate 	}
55057c478bd9Sstevel@tonic-gate 	s_closedir(dp);
55067c478bd9Sstevel@tonic-gate }
55077c478bd9Sstevel@tonic-gate 
55087c478bd9Sstevel@tonic-gate 
55097c478bd9Sstevel@tonic-gate /*
55107c478bd9Sstevel@tonic-gate  * Returns true if file matches file_re.  If subexp is non-zero, it means
55117c478bd9Sstevel@tonic-gate  * we are searching the last path component and need to return the
55127c478bd9Sstevel@tonic-gate  * parenthesized subexpression subexp in id.
55137c478bd9Sstevel@tonic-gate  *
55147c478bd9Sstevel@tonic-gate  */
55157c478bd9Sstevel@tonic-gate static int
55167c478bd9Sstevel@tonic-gate match_path_component(char *file_re,  char *file,  char **id, int subexp)
55177c478bd9Sstevel@tonic-gate {
55187c478bd9Sstevel@tonic-gate 	regex_t re1;
55197c478bd9Sstevel@tonic-gate 	int match = 0;
55207c478bd9Sstevel@tonic-gate 	int nelements;
55217c478bd9Sstevel@tonic-gate 	regmatch_t *pmatch;
55227c478bd9Sstevel@tonic-gate 
55237c478bd9Sstevel@tonic-gate 	if (subexp != 0) {
55247c478bd9Sstevel@tonic-gate 		nelements = subexp + 1;
55257c478bd9Sstevel@tonic-gate 		pmatch = (regmatch_t *)
55267c478bd9Sstevel@tonic-gate 			s_malloc(sizeof (regmatch_t) * nelements);
55277c478bd9Sstevel@tonic-gate 	} else {
55287c478bd9Sstevel@tonic-gate 		pmatch = NULL;
55297c478bd9Sstevel@tonic-gate 		nelements = 0;
55307c478bd9Sstevel@tonic-gate 	}
55317c478bd9Sstevel@tonic-gate 
55327c478bd9Sstevel@tonic-gate 	if (regcomp(&re1, file_re, REG_EXTENDED) != 0) {
55337c478bd9Sstevel@tonic-gate 		if (pmatch != NULL) {
55347c478bd9Sstevel@tonic-gate 			free(pmatch);
55357c478bd9Sstevel@tonic-gate 		}
55367c478bd9Sstevel@tonic-gate 		return (0);
55377c478bd9Sstevel@tonic-gate 	}
55387c478bd9Sstevel@tonic-gate 
55397c478bd9Sstevel@tonic-gate 	if (regexec(&re1, file, nelements, pmatch, 0) == 0) {
55407c478bd9Sstevel@tonic-gate 		match = 1;
55417c478bd9Sstevel@tonic-gate 	}
55427c478bd9Sstevel@tonic-gate 
55437c478bd9Sstevel@tonic-gate 	if ((match != 0) && (subexp != 0)) {
55447c478bd9Sstevel@tonic-gate 		int size = pmatch[subexp].rm_eo - pmatch[subexp].rm_so;
55457c478bd9Sstevel@tonic-gate 		*id = s_malloc(size + 1);
55467c478bd9Sstevel@tonic-gate 		(void) strncpy(*id, &file[pmatch[subexp].rm_so], size);
55477c478bd9Sstevel@tonic-gate 		(*id)[size] = '\0';
55487c478bd9Sstevel@tonic-gate 	}
55497c478bd9Sstevel@tonic-gate 
55507c478bd9Sstevel@tonic-gate 	if (pmatch != NULL) {
55517c478bd9Sstevel@tonic-gate 		free(pmatch);
55527c478bd9Sstevel@tonic-gate 	}
55537c478bd9Sstevel@tonic-gate 	regfree(&re1);
55547c478bd9Sstevel@tonic-gate 	return (match);
55557c478bd9Sstevel@tonic-gate }
55567c478bd9Sstevel@tonic-gate 
55577c478bd9Sstevel@tonic-gate /*
55587c478bd9Sstevel@tonic-gate  * This function is called for every file which matched the leaf
55597c478bd9Sstevel@tonic-gate  * component of the RE.  If the "numeral_id" is not already on the
55607c478bd9Sstevel@tonic-gate  * numeral set's numeral list, add it and its physical path.
55617c478bd9Sstevel@tonic-gate  */
55627c478bd9Sstevel@tonic-gate static void
55637c478bd9Sstevel@tonic-gate create_cached_numeral(char *path, numeral_set_t *setp, char *numeral_id,
55647c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[], int index)
55657c478bd9Sstevel@tonic-gate {
55667c478bd9Sstevel@tonic-gate 	char linkbuf[PATH_MAX + 1];
55677c478bd9Sstevel@tonic-gate 	char lpath[PATH_MAX + 1];
55687c478bd9Sstevel@tonic-gate 	char *linkptr, *cmp_str;
55697c478bd9Sstevel@tonic-gate 	numeral_t *np;
55707c478bd9Sstevel@tonic-gate 	int linksize;
55717c478bd9Sstevel@tonic-gate 	struct stat sb;
55727c478bd9Sstevel@tonic-gate 	const char *fcn = "create_cached_numeral";
55737c478bd9Sstevel@tonic-gate 
55747c478bd9Sstevel@tonic-gate 	assert(index >= 0 && index < setp->re_count);
55757c478bd9Sstevel@tonic-gate 	assert(strcmp(rules[index].re, setp->re[index]) == 0);
55767c478bd9Sstevel@tonic-gate 
55777c478bd9Sstevel@tonic-gate 	/*
55787c478bd9Sstevel@tonic-gate 	 *  We found a numeral_id from an entry in /dev which matched
55797c478bd9Sstevel@tonic-gate 	 *  the re passed in from devfsadm_enumerate.  We only need to make sure
55807c478bd9Sstevel@tonic-gate 	 *  ONE copy of numeral_id exists on the numeral list.  We only need
55817c478bd9Sstevel@tonic-gate 	 *  to store /dev/dsk/cNtod0s0 and no other entries hanging off
55827c478bd9Sstevel@tonic-gate 	 *  of controller N.
55837c478bd9Sstevel@tonic-gate 	 */
55847c478bd9Sstevel@tonic-gate 	for (np = setp->headnumeral; np != NULL; np = np->next) {
55857c478bd9Sstevel@tonic-gate 		if (strcmp(numeral_id, np->id) == 0) {
55867c478bd9Sstevel@tonic-gate 			return;
55877c478bd9Sstevel@tonic-gate 		}
55887c478bd9Sstevel@tonic-gate 	}
55897c478bd9Sstevel@tonic-gate 
55907c478bd9Sstevel@tonic-gate 	/* NOT on list, so add it */
55917c478bd9Sstevel@tonic-gate 
55927c478bd9Sstevel@tonic-gate 	(void) strcpy(lpath, path);
55937c478bd9Sstevel@tonic-gate 	/*
55947c478bd9Sstevel@tonic-gate 	 * If path is a dir, it is changed to the first symbolic link it find
55957c478bd9Sstevel@tonic-gate 	 * if it finds one.
55967c478bd9Sstevel@tonic-gate 	 */
55977c478bd9Sstevel@tonic-gate 	if (get_stat_info(lpath, &sb) == DEVFSADM_FAILURE) {
55987c478bd9Sstevel@tonic-gate 		return;
55997c478bd9Sstevel@tonic-gate 	}
56007c478bd9Sstevel@tonic-gate 
56017c478bd9Sstevel@tonic-gate 	/* If we get here, we found a symlink */
56027c478bd9Sstevel@tonic-gate 	linksize = readlink(lpath, linkbuf, PATH_MAX);
56037c478bd9Sstevel@tonic-gate 
56047c478bd9Sstevel@tonic-gate 	if (linksize <= 0) {
56057c478bd9Sstevel@tonic-gate 		err_print(READLINK_FAILED, fcn, lpath, strerror(errno));
56067c478bd9Sstevel@tonic-gate 		return;
56077c478bd9Sstevel@tonic-gate 	}
56087c478bd9Sstevel@tonic-gate 
56097c478bd9Sstevel@tonic-gate 	linkbuf[linksize] = '\0';
56107c478bd9Sstevel@tonic-gate 
56117c478bd9Sstevel@tonic-gate 	/*
56127c478bd9Sstevel@tonic-gate 	 * the following just points linkptr to the root of the /devices
56137c478bd9Sstevel@tonic-gate 	 * node if it is a minor node, otherwise, to the first char of
56147c478bd9Sstevel@tonic-gate 	 * linkbuf if it is a link.
56157c478bd9Sstevel@tonic-gate 	 */
56167c478bd9Sstevel@tonic-gate 	(void) is_minor_node(linkbuf, &linkptr);
56177c478bd9Sstevel@tonic-gate 
56187c478bd9Sstevel@tonic-gate 	cmp_str = alloc_cmp_str(linkptr, &rules[index]);
56197c478bd9Sstevel@tonic-gate 	if (cmp_str == NULL) {
56207c478bd9Sstevel@tonic-gate 		return;
56217c478bd9Sstevel@tonic-gate 	}
56227c478bd9Sstevel@tonic-gate 
56237c478bd9Sstevel@tonic-gate 	np = s_malloc(sizeof (numeral_t));
56247c478bd9Sstevel@tonic-gate 
56257c478bd9Sstevel@tonic-gate 	np->id = s_strdup(numeral_id);
56267c478bd9Sstevel@tonic-gate 	np->full_path = s_strdup(linkptr);
56277c478bd9Sstevel@tonic-gate 	np->rule_index = index;
56287c478bd9Sstevel@tonic-gate 	np->cmp_str = cmp_str;
56297c478bd9Sstevel@tonic-gate 
56307c478bd9Sstevel@tonic-gate 	np->next = setp->headnumeral;
56317c478bd9Sstevel@tonic-gate 	setp->headnumeral = np;
56327c478bd9Sstevel@tonic-gate }
56337c478bd9Sstevel@tonic-gate 
56347c478bd9Sstevel@tonic-gate 
56357c478bd9Sstevel@tonic-gate /*
56367c478bd9Sstevel@tonic-gate  * This should be called either before or after granting access to a
56377c478bd9Sstevel@tonic-gate  * command line version of devfsadm running, since it may have changed
56387c478bd9Sstevel@tonic-gate  * the state of /dev.  It forces future enumerate calls to re-build
56397c478bd9Sstevel@tonic-gate  * cached information from /dev.
56407c478bd9Sstevel@tonic-gate  */
56417c478bd9Sstevel@tonic-gate void
56427c478bd9Sstevel@tonic-gate invalidate_enumerate_cache(void)
56437c478bd9Sstevel@tonic-gate {
56447c478bd9Sstevel@tonic-gate 	numeral_set_t *setp;
56457c478bd9Sstevel@tonic-gate 	numeral_set_t *savedsetp;
56467c478bd9Sstevel@tonic-gate 	numeral_t *savednumset;
56477c478bd9Sstevel@tonic-gate 	numeral_t *numset;
56487c478bd9Sstevel@tonic-gate 	int i;
56497c478bd9Sstevel@tonic-gate 
56507c478bd9Sstevel@tonic-gate 	for (setp = head_numeral_set; setp != NULL; ) {
56517c478bd9Sstevel@tonic-gate 		/*
56527c478bd9Sstevel@tonic-gate 		 *  check all regexp's passed in function against
56537c478bd9Sstevel@tonic-gate 		 *  those in cached set.
56547c478bd9Sstevel@tonic-gate 		 */
56557c478bd9Sstevel@tonic-gate 
56567c478bd9Sstevel@tonic-gate 		savedsetp = setp;
56577c478bd9Sstevel@tonic-gate 		setp = setp->next;
56587c478bd9Sstevel@tonic-gate 
56597c478bd9Sstevel@tonic-gate 		for (i = 0; i < savedsetp->re_count; i++) {
56607c478bd9Sstevel@tonic-gate 			free(savedsetp->re[i]);
56617c478bd9Sstevel@tonic-gate 		}
56627c478bd9Sstevel@tonic-gate 		free(savedsetp->re);
56637c478bd9Sstevel@tonic-gate 
56647c478bd9Sstevel@tonic-gate 		for (numset = savedsetp->headnumeral; numset != NULL; ) {
56657c478bd9Sstevel@tonic-gate 			savednumset = numset;
56667c478bd9Sstevel@tonic-gate 			numset = numset->next;
56677c478bd9Sstevel@tonic-gate 			assert(savednumset->rule_index < savedsetp->re_count);
56687c478bd9Sstevel@tonic-gate 			free(savednumset->id);
56697c478bd9Sstevel@tonic-gate 			free(savednumset->full_path);
56707c478bd9Sstevel@tonic-gate 			free(savednumset->cmp_str);
56717c478bd9Sstevel@tonic-gate 			free(savednumset);
56727c478bd9Sstevel@tonic-gate 		}
56737c478bd9Sstevel@tonic-gate 		free(savedsetp);
56747c478bd9Sstevel@tonic-gate 	}
56757c478bd9Sstevel@tonic-gate 	head_numeral_set = NULL;
56767c478bd9Sstevel@tonic-gate }
56777c478bd9Sstevel@tonic-gate 
56787c478bd9Sstevel@tonic-gate /*
56797c478bd9Sstevel@tonic-gate  * Copies over links from /dev to <root>/dev and device special files in
56807c478bd9Sstevel@tonic-gate  * /devices to <root>/devices, preserving the existing file modes.  If
56817c478bd9Sstevel@tonic-gate  * the link or special file already exists on <root>, skip the copy.  (it
56827c478bd9Sstevel@tonic-gate  * would exist only if a package hard coded it there, so assume package
56837c478bd9Sstevel@tonic-gate  * knows best?).  Use /etc/name_to_major and <root>/etc/name_to_major to
56847c478bd9Sstevel@tonic-gate  * make translations for major numbers on device special files.	No need to
56857c478bd9Sstevel@tonic-gate  * make a translation on minor_perm since if the file was created in the
56867c478bd9Sstevel@tonic-gate  * miniroot then it would presumably have the same minor_perm entry in
56877c478bd9Sstevel@tonic-gate  *  <root>/etc/minor_perm.  To be used only by install.
56887c478bd9Sstevel@tonic-gate  */
56897c478bd9Sstevel@tonic-gate int
56907c478bd9Sstevel@tonic-gate devfsadm_copy(void)
56917c478bd9Sstevel@tonic-gate {
56927c478bd9Sstevel@tonic-gate 	char filename[PATH_MAX + 1];
56937c478bd9Sstevel@tonic-gate 
56947c478bd9Sstevel@tonic-gate 	/* load the installed root's name_to_major for translations */
56957c478bd9Sstevel@tonic-gate 	(void) snprintf(filename, sizeof (filename), "%s%s", root_dir,
56967c478bd9Sstevel@tonic-gate 	    NAME_TO_MAJOR);
56977c478bd9Sstevel@tonic-gate 	if (load_n2m_table(filename) == DEVFSADM_FAILURE) {
56987c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
56997c478bd9Sstevel@tonic-gate 	}
57007c478bd9Sstevel@tonic-gate 
57017c478bd9Sstevel@tonic-gate 	/* Copy /dev to target disk. No need to copy /devices with devfs */
57027c478bd9Sstevel@tonic-gate 	(void) nftw(DEV, devfsadm_copy_file, 20, FTW_PHYS);
57037c478bd9Sstevel@tonic-gate 
57047c478bd9Sstevel@tonic-gate 	/* Let install handle copying over path_to_inst */
57057c478bd9Sstevel@tonic-gate 
57067c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
57077c478bd9Sstevel@tonic-gate }
57087c478bd9Sstevel@tonic-gate 
57097c478bd9Sstevel@tonic-gate /*
57107c478bd9Sstevel@tonic-gate  * This function copies links, dirs, and device special files.
57117c478bd9Sstevel@tonic-gate  * Note that it always returns DEVFSADM_SUCCESS, so that nftw doesn't
57127c478bd9Sstevel@tonic-gate  * abort.
57137c478bd9Sstevel@tonic-gate  */
57147c478bd9Sstevel@tonic-gate /*ARGSUSED*/
57157c478bd9Sstevel@tonic-gate static int
57167c478bd9Sstevel@tonic-gate devfsadm_copy_file(const char *file, const struct stat *stat,
57177c478bd9Sstevel@tonic-gate 		    int flags, struct FTW *ftw)
57187c478bd9Sstevel@tonic-gate {
57197c478bd9Sstevel@tonic-gate 	struct stat sp;
57207c478bd9Sstevel@tonic-gate 	dev_t newdev;
57217c478bd9Sstevel@tonic-gate 	char newfile[PATH_MAX + 1];
57227c478bd9Sstevel@tonic-gate 	char linkcontents[PATH_MAX + 1];
57237c478bd9Sstevel@tonic-gate 	int bytes;
57247c478bd9Sstevel@tonic-gate 	const char *fcn = "devfsadm_copy_file";
57257c478bd9Sstevel@tonic-gate 
57267c478bd9Sstevel@tonic-gate 	(void) strcpy(newfile, root_dir);
57277c478bd9Sstevel@tonic-gate 	(void) strcat(newfile, "/");
57287c478bd9Sstevel@tonic-gate 	(void) strcat(newfile, file);
57297c478bd9Sstevel@tonic-gate 
57307c478bd9Sstevel@tonic-gate 	if (lstat(newfile, &sp) == 0) {
57317c478bd9Sstevel@tonic-gate 		/* newfile already exists, so no need to continue */
57327c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
57337c478bd9Sstevel@tonic-gate 	}
57347c478bd9Sstevel@tonic-gate 
57357c478bd9Sstevel@tonic-gate 	if (((stat->st_mode & S_IFMT) == S_IFBLK) ||
57367c478bd9Sstevel@tonic-gate 	    ((stat->st_mode & S_IFMT) == S_IFCHR)) {
57377c478bd9Sstevel@tonic-gate 		if (translate_major(stat->st_rdev, &newdev) ==
57387c478bd9Sstevel@tonic-gate 		    DEVFSADM_FAILURE) {
57397c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
57407c478bd9Sstevel@tonic-gate 		}
57417c478bd9Sstevel@tonic-gate 		if (mknod(newfile, stat->st_mode, newdev) == -1) {
57427c478bd9Sstevel@tonic-gate 			err_print(MKNOD_FAILED, newfile, strerror(errno));
57437c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
57447c478bd9Sstevel@tonic-gate 		}
57457c478bd9Sstevel@tonic-gate 	} else if ((stat->st_mode & S_IFMT) == S_IFDIR) {
57467c478bd9Sstevel@tonic-gate 		if (mknod(newfile, stat->st_mode, 0) == -1) {
57477c478bd9Sstevel@tonic-gate 			err_print(MKNOD_FAILED, newfile, strerror(errno));
57487c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
57497c478bd9Sstevel@tonic-gate 		}
57507c478bd9Sstevel@tonic-gate 	} else if ((stat->st_mode & S_IFMT) == S_IFLNK)  {
57517c478bd9Sstevel@tonic-gate 		if ((bytes = readlink(file, linkcontents, PATH_MAX)) == -1)  {
57527c478bd9Sstevel@tonic-gate 			err_print(READLINK_FAILED, fcn, file, strerror(errno));
57537c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
57547c478bd9Sstevel@tonic-gate 		}
57557c478bd9Sstevel@tonic-gate 		linkcontents[bytes] = '\0';
57567c478bd9Sstevel@tonic-gate 		if (symlink(linkcontents, newfile) == -1) {
57577c478bd9Sstevel@tonic-gate 			err_print(SYMLINK_FAILED, newfile, newfile,
57587c478bd9Sstevel@tonic-gate 					strerror(errno));
57597c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
57607c478bd9Sstevel@tonic-gate 		}
57617c478bd9Sstevel@tonic-gate 	}
57627c478bd9Sstevel@tonic-gate 
57637c478bd9Sstevel@tonic-gate 	(void) lchown(newfile, stat->st_uid, stat->st_gid);
57647c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
57657c478bd9Sstevel@tonic-gate }
57667c478bd9Sstevel@tonic-gate 
57677c478bd9Sstevel@tonic-gate /*
57687c478bd9Sstevel@tonic-gate  *  Given a dev_t from the running kernel, return the new_dev_t
57697c478bd9Sstevel@tonic-gate  *  by translating to the major number found on the installed
57707c478bd9Sstevel@tonic-gate  *  target's root name_to_major file.
57717c478bd9Sstevel@tonic-gate  */
57727c478bd9Sstevel@tonic-gate static int
57737c478bd9Sstevel@tonic-gate translate_major(dev_t old_dev, dev_t *new_dev)
57747c478bd9Sstevel@tonic-gate {
57757c478bd9Sstevel@tonic-gate 	major_t oldmajor;
57767c478bd9Sstevel@tonic-gate 	major_t newmajor;
57777c478bd9Sstevel@tonic-gate 	minor_t oldminor;
57787c478bd9Sstevel@tonic-gate 	minor_t newminor;
57797c478bd9Sstevel@tonic-gate 	char cdriver[FILENAME_MAX + 1];
57807c478bd9Sstevel@tonic-gate 	char driver[FILENAME_MAX + 1];
57817c478bd9Sstevel@tonic-gate 	char *fcn = "translate_major: ";
57827c478bd9Sstevel@tonic-gate 
57837c478bd9Sstevel@tonic-gate 	oldmajor = major(old_dev);
57847c478bd9Sstevel@tonic-gate 	if (modctl(MODGETNAME, driver, sizeof (driver),
57857c478bd9Sstevel@tonic-gate 			    &oldmajor) != 0) {
57867c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
57877c478bd9Sstevel@tonic-gate 	}
57887c478bd9Sstevel@tonic-gate 
57897c478bd9Sstevel@tonic-gate 	if (strcmp(driver, "clone") != 0) {
57907c478bd9Sstevel@tonic-gate 		/* non-clone case */
57917c478bd9Sstevel@tonic-gate 
57927c478bd9Sstevel@tonic-gate 		/* look up major number is target's name2major */
57937c478bd9Sstevel@tonic-gate 		if (get_major_no(driver, &newmajor) == DEVFSADM_FAILURE) {
57947c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
57957c478bd9Sstevel@tonic-gate 		}
57967c478bd9Sstevel@tonic-gate 
57977c478bd9Sstevel@tonic-gate 		*new_dev = makedev(newmajor, minor(old_dev));
57987c478bd9Sstevel@tonic-gate 		if (old_dev != *new_dev) {
57997c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "%sdriver: %s old: %lu,%lu "
58007c478bd9Sstevel@tonic-gate 				"new: %lu,%lu\n", fcn, driver, major(old_dev),
58017c478bd9Sstevel@tonic-gate 				minor(old_dev), major(*new_dev),
58027c478bd9Sstevel@tonic-gate 				minor(*new_dev));
58037c478bd9Sstevel@tonic-gate 		}
58047c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
58057c478bd9Sstevel@tonic-gate 	} else {
58067c478bd9Sstevel@tonic-gate 		/*
58077c478bd9Sstevel@tonic-gate 		 *  The clone is a special case.  Look at its minor
58087c478bd9Sstevel@tonic-gate 		 *  number since it is the major number of the real driver.
58097c478bd9Sstevel@tonic-gate 		 */
58107c478bd9Sstevel@tonic-gate 		if (get_major_no(driver, &newmajor) == DEVFSADM_FAILURE) {
58117c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
58127c478bd9Sstevel@tonic-gate 		}
58137c478bd9Sstevel@tonic-gate 
58147c478bd9Sstevel@tonic-gate 		oldminor = minor(old_dev);
58157c478bd9Sstevel@tonic-gate 		if (modctl(MODGETNAME, cdriver, sizeof (cdriver),
58167c478bd9Sstevel@tonic-gate 					&oldminor) != 0) {
58177c478bd9Sstevel@tonic-gate 			err_print(MODGETNAME_FAILED, oldminor);
58187c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
58197c478bd9Sstevel@tonic-gate 		}
58207c478bd9Sstevel@tonic-gate 
58217c478bd9Sstevel@tonic-gate 		if (get_major_no(cdriver, &newminor) == DEVFSADM_FAILURE) {
58227c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
58237c478bd9Sstevel@tonic-gate 		}
58247c478bd9Sstevel@tonic-gate 
58257c478bd9Sstevel@tonic-gate 		*new_dev = makedev(newmajor, newminor);
58267c478bd9Sstevel@tonic-gate 		if (old_dev != *new_dev) {
58277c478bd9Sstevel@tonic-gate 			vprint(CHATTY_MID, "%sdriver: %s old: "
58287c478bd9Sstevel@tonic-gate 				"%lu,%lu  new: %lu,%lu\n", fcn, driver,
58297c478bd9Sstevel@tonic-gate 				major(old_dev), minor(old_dev),
58307c478bd9Sstevel@tonic-gate 				major(*new_dev), minor(*new_dev));
58317c478bd9Sstevel@tonic-gate 		}
58327c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
58337c478bd9Sstevel@tonic-gate 	}
58347c478bd9Sstevel@tonic-gate }
58357c478bd9Sstevel@tonic-gate 
58367c478bd9Sstevel@tonic-gate /*
58377c478bd9Sstevel@tonic-gate  *
58387c478bd9Sstevel@tonic-gate  * Find the major number for driver, searching the n2m_list that was
58397c478bd9Sstevel@tonic-gate  * built in load_n2m_table().
58407c478bd9Sstevel@tonic-gate  */
58417c478bd9Sstevel@tonic-gate static int
58427c478bd9Sstevel@tonic-gate get_major_no(char *driver, major_t *major)
58437c478bd9Sstevel@tonic-gate {
58447c478bd9Sstevel@tonic-gate 	n2m_t *ptr;
58457c478bd9Sstevel@tonic-gate 
58467c478bd9Sstevel@tonic-gate 	for (ptr = n2m_list; ptr != NULL; ptr = ptr->next) {
58477c478bd9Sstevel@tonic-gate 		if (strcmp(ptr->driver, driver) == 0) {
58487c478bd9Sstevel@tonic-gate 			*major = ptr->major;
58497c478bd9Sstevel@tonic-gate 			return (DEVFSADM_SUCCESS);
58507c478bd9Sstevel@tonic-gate 		}
58517c478bd9Sstevel@tonic-gate 	}
58527c478bd9Sstevel@tonic-gate 	err_print(FIND_MAJOR_FAILED, driver);
58537c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
58547c478bd9Sstevel@tonic-gate }
58557c478bd9Sstevel@tonic-gate 
58567c478bd9Sstevel@tonic-gate /*
58577c478bd9Sstevel@tonic-gate  * Loads a name_to_major table into memory.  Used only for suninstall's
58587c478bd9Sstevel@tonic-gate  * private -R option to devfsadm, to translate major numbers from the
58597c478bd9Sstevel@tonic-gate  * running to the installed target disk.
58607c478bd9Sstevel@tonic-gate  */
58617c478bd9Sstevel@tonic-gate static int
58627c478bd9Sstevel@tonic-gate load_n2m_table(char *file)
58637c478bd9Sstevel@tonic-gate {
58647c478bd9Sstevel@tonic-gate 	FILE *fp;
58657c478bd9Sstevel@tonic-gate 	char line[1024];
58667c478bd9Sstevel@tonic-gate 	char driver[PATH_MAX + 1];
58677c478bd9Sstevel@tonic-gate 	major_t major;
58687c478bd9Sstevel@tonic-gate 	n2m_t *ptr;
58697c478bd9Sstevel@tonic-gate 	int ln = 0;
58707c478bd9Sstevel@tonic-gate 
58717c478bd9Sstevel@tonic-gate 	if ((fp = fopen(file, "r")) == NULL) {
58727c478bd9Sstevel@tonic-gate 		err_print(FOPEN_FAILED, file, strerror(errno));
58737c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
58747c478bd9Sstevel@tonic-gate 	}
58757c478bd9Sstevel@tonic-gate 
58767c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line), fp) != NULL) {
58777c478bd9Sstevel@tonic-gate 		ln++;
58787c478bd9Sstevel@tonic-gate 		if (line[0] == '#') {
58797c478bd9Sstevel@tonic-gate 			continue;
58807c478bd9Sstevel@tonic-gate 		}
58817c478bd9Sstevel@tonic-gate 		if (sscanf(line, "%1024s%lu", driver, &major) != 2) {
58827c478bd9Sstevel@tonic-gate 			err_print(IGNORING_LINE_IN, ln, file);
58837c478bd9Sstevel@tonic-gate 			continue;
58847c478bd9Sstevel@tonic-gate 		}
58857c478bd9Sstevel@tonic-gate 		ptr = (n2m_t *)s_malloc(sizeof (n2m_t));
58867c478bd9Sstevel@tonic-gate 		ptr->major = major;
58877c478bd9Sstevel@tonic-gate 		ptr->driver = s_strdup(driver);
58887c478bd9Sstevel@tonic-gate 		ptr->next = n2m_list;
58897c478bd9Sstevel@tonic-gate 		n2m_list = ptr;
58907c478bd9Sstevel@tonic-gate 	}
58917c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
58927c478bd9Sstevel@tonic-gate 		err_print(FCLOSE_FAILED, file, strerror(errno));
58937c478bd9Sstevel@tonic-gate 	}
58947c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
58957c478bd9Sstevel@tonic-gate }
58967c478bd9Sstevel@tonic-gate 
58977c478bd9Sstevel@tonic-gate /*
58987c478bd9Sstevel@tonic-gate  * Called at devfsadm startup to read in the devlink.tab file.	Creates
58997c478bd9Sstevel@tonic-gate  * a linked list of devlinktab_list structures which will be
59007c478bd9Sstevel@tonic-gate  * searched for every minor node.
59017c478bd9Sstevel@tonic-gate  */
59027c478bd9Sstevel@tonic-gate static void
59037c478bd9Sstevel@tonic-gate read_devlinktab_file(void)
59047c478bd9Sstevel@tonic-gate {
59057c478bd9Sstevel@tonic-gate 	devlinktab_list_t *headp = NULL;
59067c478bd9Sstevel@tonic-gate 	devlinktab_list_t *entryp;
59077c478bd9Sstevel@tonic-gate 	devlinktab_list_t **previous;
59087c478bd9Sstevel@tonic-gate 	devlinktab_list_t *save;
59097c478bd9Sstevel@tonic-gate 	char line[MAX_DEVLINK_LINE];
59107c478bd9Sstevel@tonic-gate 	char *selector;
59117c478bd9Sstevel@tonic-gate 	char *p_link;
59127c478bd9Sstevel@tonic-gate 	char *s_link;
59137c478bd9Sstevel@tonic-gate 	FILE *fp;
59147c478bd9Sstevel@tonic-gate 	int i;
59157c478bd9Sstevel@tonic-gate 	static struct stat cached_sb;
59167c478bd9Sstevel@tonic-gate 	struct stat current_sb;
59177c478bd9Sstevel@tonic-gate 	static int cached = FALSE;
59187c478bd9Sstevel@tonic-gate 
59197c478bd9Sstevel@tonic-gate 	if (devlinktab_file == NULL) {
59207c478bd9Sstevel@tonic-gate 		return;
59217c478bd9Sstevel@tonic-gate 	}
59227c478bd9Sstevel@tonic-gate 
59237c478bd9Sstevel@tonic-gate 	(void) stat(devlinktab_file, &current_sb);
59247c478bd9Sstevel@tonic-gate 
59257c478bd9Sstevel@tonic-gate 	/* if already cached, check to see if it is still valid */
59267c478bd9Sstevel@tonic-gate 	if (cached == TRUE) {
59277c478bd9Sstevel@tonic-gate 
59287c478bd9Sstevel@tonic-gate 		if (current_sb.st_mtime == cached_sb.st_mtime) {
59297c478bd9Sstevel@tonic-gate 			vprint(FILES_MID, "%s cache valid\n", devlinktab_file);
59307c478bd9Sstevel@tonic-gate 			return;
59317c478bd9Sstevel@tonic-gate 		}
59327c478bd9Sstevel@tonic-gate 
59337c478bd9Sstevel@tonic-gate 		vprint(FILES_MID, "invalidating %s cache\n", devlinktab_file);
59347c478bd9Sstevel@tonic-gate 
59357c478bd9Sstevel@tonic-gate 		while (devlinktab_list != NULL) {
59367c478bd9Sstevel@tonic-gate 			free_link_list(devlinktab_list->p_link);
59377c478bd9Sstevel@tonic-gate 			free_link_list(devlinktab_list->s_link);
59387c478bd9Sstevel@tonic-gate 			free_selector_list(devlinktab_list->selector);
59397c478bd9Sstevel@tonic-gate 			free(devlinktab_list->selector_pattern);
59407c478bd9Sstevel@tonic-gate 			free(devlinktab_list->p_link_pattern);
59417c478bd9Sstevel@tonic-gate 			if (devlinktab_list->s_link_pattern != NULL) {
59427c478bd9Sstevel@tonic-gate 				free(devlinktab_list->s_link_pattern);
59437c478bd9Sstevel@tonic-gate 			}
59447c478bd9Sstevel@tonic-gate 			save = devlinktab_list;
59457c478bd9Sstevel@tonic-gate 			devlinktab_list = devlinktab_list->next;
59467c478bd9Sstevel@tonic-gate 			free(save);
59477c478bd9Sstevel@tonic-gate 		}
59487c478bd9Sstevel@tonic-gate 	} else {
59497c478bd9Sstevel@tonic-gate 		cached = TRUE;
59507c478bd9Sstevel@tonic-gate 	}
59517c478bd9Sstevel@tonic-gate 
59527c478bd9Sstevel@tonic-gate 	(void) stat(devlinktab_file, &cached_sb);
59537c478bd9Sstevel@tonic-gate 
59547c478bd9Sstevel@tonic-gate 	if ((fp = fopen(devlinktab_file, "r")) == NULL) {
59557c478bd9Sstevel@tonic-gate 		err_print(FOPEN_FAILED, devlinktab_file, strerror(errno));
59567c478bd9Sstevel@tonic-gate 		return;
59577c478bd9Sstevel@tonic-gate 	}
59587c478bd9Sstevel@tonic-gate 
59597c478bd9Sstevel@tonic-gate 	previous = &headp;
59607c478bd9Sstevel@tonic-gate 
59617c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line), fp) != NULL) {
59627c478bd9Sstevel@tonic-gate 		devlinktab_line++;
59637c478bd9Sstevel@tonic-gate 		i = strlen(line);
59647c478bd9Sstevel@tonic-gate 		if (line[i-1] == NEWLINE) {
59657c478bd9Sstevel@tonic-gate 			line[i-1] = '\0';
59667c478bd9Sstevel@tonic-gate 		} else if (i == sizeof (line-1)) {
59677c478bd9Sstevel@tonic-gate 			err_print(LINE_TOO_LONG, devlinktab_line,
59687c478bd9Sstevel@tonic-gate 			    devlinktab_file, sizeof (line)-1);
59697c478bd9Sstevel@tonic-gate 			while (((i = getc(fp)) != '\n') && (i != EOF));
59707c478bd9Sstevel@tonic-gate 			continue;
59717c478bd9Sstevel@tonic-gate 		}
59727c478bd9Sstevel@tonic-gate 
59737c478bd9Sstevel@tonic-gate 		if ((line[0] == '#') || (line[0] == '\0')) {
59747c478bd9Sstevel@tonic-gate 			/* Ignore comments and blank lines */
59757c478bd9Sstevel@tonic-gate 			continue;
59767c478bd9Sstevel@tonic-gate 		}
59777c478bd9Sstevel@tonic-gate 
59787c478bd9Sstevel@tonic-gate 		vprint(DEVLINK_MID, "table: %s line %d: '%s'\n",
59797c478bd9Sstevel@tonic-gate 			devlinktab_file, devlinktab_line, line);
59807c478bd9Sstevel@tonic-gate 
59817c478bd9Sstevel@tonic-gate 		/* break each entry into fields.  s_link may be NULL */
59827c478bd9Sstevel@tonic-gate 		if (split_devlinktab_entry(line, &selector, &p_link,
59837c478bd9Sstevel@tonic-gate 		    &s_link) == DEVFSADM_FAILURE) {
59847c478bd9Sstevel@tonic-gate 			vprint(DEVLINK_MID, "split_entry returns failure\n");
59857c478bd9Sstevel@tonic-gate 			continue;
59867c478bd9Sstevel@tonic-gate 		} else {
59877c478bd9Sstevel@tonic-gate 			vprint(DEVLINK_MID, "split_entry selector='%s' "
59887c478bd9Sstevel@tonic-gate 				"p_link='%s' s_link='%s'\n\n", selector,
59897c478bd9Sstevel@tonic-gate 				p_link, (s_link == NULL) ? "" : s_link);
59907c478bd9Sstevel@tonic-gate 		}
59917c478bd9Sstevel@tonic-gate 
59927c478bd9Sstevel@tonic-gate 		entryp = (devlinktab_list_t *)
59937c478bd9Sstevel@tonic-gate 			s_malloc(sizeof (devlinktab_list_t));
59947c478bd9Sstevel@tonic-gate 
59957c478bd9Sstevel@tonic-gate 		entryp->line_number = devlinktab_line;
59967c478bd9Sstevel@tonic-gate 
59977c478bd9Sstevel@tonic-gate 		if ((entryp->selector =
59987c478bd9Sstevel@tonic-gate 			create_selector_list(selector)) == NULL) {
59997c478bd9Sstevel@tonic-gate 			free(entryp);
60007c478bd9Sstevel@tonic-gate 			continue;
60017c478bd9Sstevel@tonic-gate 		}
60027c478bd9Sstevel@tonic-gate 		entryp->selector_pattern = s_strdup(selector);
60037c478bd9Sstevel@tonic-gate 
60047c478bd9Sstevel@tonic-gate 		if ((entryp->p_link = create_link_list(p_link)) == NULL) {
60057c478bd9Sstevel@tonic-gate 			free_selector_list(entryp->selector);
60067c478bd9Sstevel@tonic-gate 			free(entryp->selector_pattern);
60077c478bd9Sstevel@tonic-gate 			free(entryp);
60087c478bd9Sstevel@tonic-gate 			continue;
60097c478bd9Sstevel@tonic-gate 		}
60107c478bd9Sstevel@tonic-gate 
60117c478bd9Sstevel@tonic-gate 		entryp->p_link_pattern = s_strdup(p_link);
60127c478bd9Sstevel@tonic-gate 
60137c478bd9Sstevel@tonic-gate 		if (s_link != NULL) {
60147c478bd9Sstevel@tonic-gate 			if ((entryp->s_link =
60157c478bd9Sstevel@tonic-gate 			    create_link_list(s_link)) == NULL) {
60167c478bd9Sstevel@tonic-gate 				free_selector_list(entryp->selector);
60177c478bd9Sstevel@tonic-gate 				free_link_list(entryp->p_link);
60187c478bd9Sstevel@tonic-gate 				free(entryp->selector_pattern);
60197c478bd9Sstevel@tonic-gate 				free(entryp->p_link_pattern);
60207c478bd9Sstevel@tonic-gate 				free(entryp);
60217c478bd9Sstevel@tonic-gate 				continue;
60227c478bd9Sstevel@tonic-gate 			}
60237c478bd9Sstevel@tonic-gate 			    entryp->s_link_pattern = s_strdup(s_link);
60247c478bd9Sstevel@tonic-gate 		} else {
60257c478bd9Sstevel@tonic-gate 			entryp->s_link = NULL;
60267c478bd9Sstevel@tonic-gate 			entryp->s_link_pattern = NULL;
60277c478bd9Sstevel@tonic-gate 
60287c478bd9Sstevel@tonic-gate 		}
60297c478bd9Sstevel@tonic-gate 
60307c478bd9Sstevel@tonic-gate 		/* append to end of list */
60317c478bd9Sstevel@tonic-gate 
60327c478bd9Sstevel@tonic-gate 		entryp->next = NULL;
60337c478bd9Sstevel@tonic-gate 		*previous = entryp;
60347c478bd9Sstevel@tonic-gate 		previous = &(entryp->next);
60357c478bd9Sstevel@tonic-gate 	}
60367c478bd9Sstevel@tonic-gate 	if (fclose(fp) == EOF) {
60377c478bd9Sstevel@tonic-gate 		err_print(FCLOSE_FAILED, devlinktab_file, strerror(errno));
60387c478bd9Sstevel@tonic-gate 	}
60397c478bd9Sstevel@tonic-gate 	devlinktab_list = headp;
60407c478bd9Sstevel@tonic-gate }
60417c478bd9Sstevel@tonic-gate 
60427c478bd9Sstevel@tonic-gate /*
60437c478bd9Sstevel@tonic-gate  *
60447c478bd9Sstevel@tonic-gate  * For a single line entry in devlink.tab, split the line into fields
60457c478bd9Sstevel@tonic-gate  * selector, p_link, and an optionally s_link.	If s_link field is not
60467c478bd9Sstevel@tonic-gate  * present, then return NULL in s_link (not NULL string).
60477c478bd9Sstevel@tonic-gate  */
60487c478bd9Sstevel@tonic-gate static int
60497c478bd9Sstevel@tonic-gate split_devlinktab_entry(char *entry, char **selector, char **p_link,
60507c478bd9Sstevel@tonic-gate 			char **s_link)
60517c478bd9Sstevel@tonic-gate {
60527c478bd9Sstevel@tonic-gate 	char *tab;
60537c478bd9Sstevel@tonic-gate 
60547c478bd9Sstevel@tonic-gate 	*selector = entry;
60557c478bd9Sstevel@tonic-gate 
60567c478bd9Sstevel@tonic-gate 	if ((tab = strchr(entry, TAB)) != NULL) {
60577c478bd9Sstevel@tonic-gate 		*tab = '\0';
60587c478bd9Sstevel@tonic-gate 		*p_link = ++tab;
60597c478bd9Sstevel@tonic-gate 	} else {
60607c478bd9Sstevel@tonic-gate 		err_print(MISSING_TAB, devlinktab_line, devlinktab_file);
60617c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
60627c478bd9Sstevel@tonic-gate 	}
60637c478bd9Sstevel@tonic-gate 
60647c478bd9Sstevel@tonic-gate 	if (*p_link == '\0') {
60657c478bd9Sstevel@tonic-gate 		err_print(MISSING_DEVNAME, devlinktab_line, devlinktab_file);
60667c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
60677c478bd9Sstevel@tonic-gate 	}
60687c478bd9Sstevel@tonic-gate 
60697c478bd9Sstevel@tonic-gate 	if ((tab = strchr(*p_link, TAB)) != NULL) {
60707c478bd9Sstevel@tonic-gate 		*tab = '\0';
60717c478bd9Sstevel@tonic-gate 		*s_link = ++tab;
60727c478bd9Sstevel@tonic-gate 		if (strchr(*s_link, TAB) != NULL) {
60737c478bd9Sstevel@tonic-gate 			err_print(TOO_MANY_FIELDS, devlinktab_line,
60747c478bd9Sstevel@tonic-gate 					devlinktab_file);
60757c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
60767c478bd9Sstevel@tonic-gate 		}
60777c478bd9Sstevel@tonic-gate 	} else {
60787c478bd9Sstevel@tonic-gate 		*s_link = NULL;
60797c478bd9Sstevel@tonic-gate 	}
60807c478bd9Sstevel@tonic-gate 
60817c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
60827c478bd9Sstevel@tonic-gate }
60837c478bd9Sstevel@tonic-gate 
60847c478bd9Sstevel@tonic-gate /*
60857c478bd9Sstevel@tonic-gate  * For a given devfs_spec field, for each element in the field, add it to
60867c478bd9Sstevel@tonic-gate  * a linked list of devfs_spec structures.  Return the linked list in
60877c478bd9Sstevel@tonic-gate  * devfs_spec_list.
60887c478bd9Sstevel@tonic-gate  */
60897c478bd9Sstevel@tonic-gate static selector_list_t *
60907c478bd9Sstevel@tonic-gate create_selector_list(char *selector)
60917c478bd9Sstevel@tonic-gate {
60927c478bd9Sstevel@tonic-gate 	    char *key;
60937c478bd9Sstevel@tonic-gate 	    char *val;
60947c478bd9Sstevel@tonic-gate 	    int error = FALSE;
60957c478bd9Sstevel@tonic-gate 	    selector_list_t *head_selector_list = NULL;
60967c478bd9Sstevel@tonic-gate 	    selector_list_t *selector_list;
60977c478bd9Sstevel@tonic-gate 
60987c478bd9Sstevel@tonic-gate 	    /* parse_devfs_spec splits the next field into keyword & value */
60997c478bd9Sstevel@tonic-gate 	    while ((*selector != NULL) && (error == FALSE)) {
61007c478bd9Sstevel@tonic-gate 		    if (parse_selector(&selector, &key,
61017c478bd9Sstevel@tonic-gate 				&val) == DEVFSADM_FAILURE) {
61027c478bd9Sstevel@tonic-gate 			    error = TRUE;
61037c478bd9Sstevel@tonic-gate 			    break;
61047c478bd9Sstevel@tonic-gate 		    } else {
61057c478bd9Sstevel@tonic-gate 			    selector_list = (selector_list_t *)
61067c478bd9Sstevel@tonic-gate 				    s_malloc(sizeof (selector_list_t));
61077c478bd9Sstevel@tonic-gate 			    if (strcmp(NAME_S, key) == 0) {
61087c478bd9Sstevel@tonic-gate 				    selector_list->key = NAME;
61097c478bd9Sstevel@tonic-gate 			    } else if (strcmp(TYPE_S, key) == 0) {
61107c478bd9Sstevel@tonic-gate 				    selector_list->key = TYPE;
61117c478bd9Sstevel@tonic-gate 			    } else if (strncmp(ADDR_S, key, ADDR_S_LEN) == 0) {
61127c478bd9Sstevel@tonic-gate 				    selector_list->key = ADDR;
61137c478bd9Sstevel@tonic-gate 				    if (key[ADDR_S_LEN] == '\0') {
61147c478bd9Sstevel@tonic-gate 					    selector_list->arg = 0;
61157c478bd9Sstevel@tonic-gate 				    } else if (isdigit(key[ADDR_S_LEN]) !=
61167c478bd9Sstevel@tonic-gate 						FALSE) {
61177c478bd9Sstevel@tonic-gate 					    selector_list->arg =
61187c478bd9Sstevel@tonic-gate 							atoi(&key[ADDR_S_LEN]);
61197c478bd9Sstevel@tonic-gate 				    } else {
61207c478bd9Sstevel@tonic-gate 					    error = TRUE;
61217c478bd9Sstevel@tonic-gate 					    free(selector_list);
61227c478bd9Sstevel@tonic-gate 					    err_print(BADKEYWORD, key,
61237c478bd9Sstevel@tonic-gate 						devlinktab_line,
61247c478bd9Sstevel@tonic-gate 						devlinktab_file);
61257c478bd9Sstevel@tonic-gate 					    break;
61267c478bd9Sstevel@tonic-gate 				    }
61277c478bd9Sstevel@tonic-gate 			    } else if (strncmp(MINOR_S, key,
61287c478bd9Sstevel@tonic-gate 						MINOR_S_LEN) == 0) {
61297c478bd9Sstevel@tonic-gate 				    selector_list->key = MINOR;
61307c478bd9Sstevel@tonic-gate 				    if (key[MINOR_S_LEN] == '\0') {
61317c478bd9Sstevel@tonic-gate 					    selector_list->arg = 0;
61327c478bd9Sstevel@tonic-gate 				    } else if (isdigit(key[MINOR_S_LEN]) !=
61337c478bd9Sstevel@tonic-gate 						FALSE) {
61347c478bd9Sstevel@tonic-gate 					    selector_list->arg =
61357c478bd9Sstevel@tonic-gate 						atoi(&key[MINOR_S_LEN]);
61367c478bd9Sstevel@tonic-gate 				    } else {
61377c478bd9Sstevel@tonic-gate 					    error = TRUE;
61387c478bd9Sstevel@tonic-gate 					    free(selector_list);
61397c478bd9Sstevel@tonic-gate 					    err_print(BADKEYWORD, key,
61407c478bd9Sstevel@tonic-gate 						devlinktab_line,
61417c478bd9Sstevel@tonic-gate 						devlinktab_file);
61427c478bd9Sstevel@tonic-gate 					    break;
61437c478bd9Sstevel@tonic-gate 				    }
61447c478bd9Sstevel@tonic-gate 				    vprint(DEVLINK_MID, "MINOR = %s\n", val);
61457c478bd9Sstevel@tonic-gate 			    } else {
61467c478bd9Sstevel@tonic-gate 				    err_print(UNRECOGNIZED_KEY, key,
61477c478bd9Sstevel@tonic-gate 					devlinktab_line, devlinktab_file);
61487c478bd9Sstevel@tonic-gate 				    error = TRUE;
61497c478bd9Sstevel@tonic-gate 				    free(selector_list);
61507c478bd9Sstevel@tonic-gate 				    break;
61517c478bd9Sstevel@tonic-gate 			    }
61527c478bd9Sstevel@tonic-gate 			    selector_list->val = s_strdup(val);
61537c478bd9Sstevel@tonic-gate 			    selector_list->next = head_selector_list;
61547c478bd9Sstevel@tonic-gate 			    head_selector_list = selector_list;
61557c478bd9Sstevel@tonic-gate 			    vprint(DEVLINK_MID, "key='%s' val='%s' arg=%d\n",
61567c478bd9Sstevel@tonic-gate 					key, val, selector_list->arg);
61577c478bd9Sstevel@tonic-gate 		    }
61587c478bd9Sstevel@tonic-gate 	    }
61597c478bd9Sstevel@tonic-gate 
61607c478bd9Sstevel@tonic-gate 	    if ((error == FALSE) && (head_selector_list != NULL)) {
61617c478bd9Sstevel@tonic-gate 		    return (head_selector_list);
61627c478bd9Sstevel@tonic-gate 	    } else {
61637c478bd9Sstevel@tonic-gate 		    /* parse failed.  Free any allocated structs */
61647c478bd9Sstevel@tonic-gate 		    free_selector_list(head_selector_list);
61657c478bd9Sstevel@tonic-gate 		    return (NULL);
61667c478bd9Sstevel@tonic-gate 	    }
61677c478bd9Sstevel@tonic-gate }
61687c478bd9Sstevel@tonic-gate 
61697c478bd9Sstevel@tonic-gate /*
61707c478bd9Sstevel@tonic-gate  * Takes a semicolon separated list of selector elements and breaks up
61717c478bd9Sstevel@tonic-gate  * into a keyword-value pair.	semicolon and equal characters are
61727c478bd9Sstevel@tonic-gate  * replaced with NULL's.  On success, selector is updated to point to the
61737c478bd9Sstevel@tonic-gate  * terminating NULL character terminating the keyword-value pair, and the
61747c478bd9Sstevel@tonic-gate  * function returns DEVFSADM_SUCCESS.	If there is a syntax error,
61757c478bd9Sstevel@tonic-gate  * devfs_spec is not modified and function returns DEVFSADM_FAILURE.
61767c478bd9Sstevel@tonic-gate  */
61777c478bd9Sstevel@tonic-gate static int
61787c478bd9Sstevel@tonic-gate parse_selector(char **selector, char **key, char **val)
61797c478bd9Sstevel@tonic-gate {
61807c478bd9Sstevel@tonic-gate 	char *equal;
61817c478bd9Sstevel@tonic-gate 	char *semi_colon;
61827c478bd9Sstevel@tonic-gate 
61837c478bd9Sstevel@tonic-gate 	*key = *selector;
61847c478bd9Sstevel@tonic-gate 
61857c478bd9Sstevel@tonic-gate 	if ((equal = strchr(*key, '=')) != NULL) {
61867c478bd9Sstevel@tonic-gate 		*equal = '\0';
61877c478bd9Sstevel@tonic-gate 	} else {
61887c478bd9Sstevel@tonic-gate 		err_print(MISSING_EQUAL, devlinktab_line, devlinktab_file);
61897c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
61907c478bd9Sstevel@tonic-gate 	}
61917c478bd9Sstevel@tonic-gate 
61927c478bd9Sstevel@tonic-gate 	*val = ++equal;
61937c478bd9Sstevel@tonic-gate 	if ((semi_colon = strchr(equal, ';')) != NULL) {
61947c478bd9Sstevel@tonic-gate 		*semi_colon = '\0';
61957c478bd9Sstevel@tonic-gate 		*selector = semi_colon + 1;
61967c478bd9Sstevel@tonic-gate 	} else {
61977c478bd9Sstevel@tonic-gate 		*selector = equal + strlen(equal);
61987c478bd9Sstevel@tonic-gate 	}
61997c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
62007c478bd9Sstevel@tonic-gate }
62017c478bd9Sstevel@tonic-gate 
62027c478bd9Sstevel@tonic-gate /*
62037c478bd9Sstevel@tonic-gate  * link is either the second or third field of devlink.tab.  Parse link
62047c478bd9Sstevel@tonic-gate  * into a linked list of devlink structures and return ptr to list.  Each
62057c478bd9Sstevel@tonic-gate  * list element is either a constant string, or one of the following
62067c478bd9Sstevel@tonic-gate  * escape sequences: \M, \A, \N, or \D.  The first three escape sequences
62077c478bd9Sstevel@tonic-gate  * take a numerical argument.
62087c478bd9Sstevel@tonic-gate  */
62097c478bd9Sstevel@tonic-gate static link_list_t *
62107c478bd9Sstevel@tonic-gate create_link_list(char *link)
62117c478bd9Sstevel@tonic-gate {
62127c478bd9Sstevel@tonic-gate 	int x = 0;
62137c478bd9Sstevel@tonic-gate 	int error = FALSE;
62147c478bd9Sstevel@tonic-gate 	int counter_found = FALSE;
62157c478bd9Sstevel@tonic-gate 	link_list_t *head = NULL;
62167c478bd9Sstevel@tonic-gate 	link_list_t **ptr;
62177c478bd9Sstevel@tonic-gate 	link_list_t *link_list;
62187c478bd9Sstevel@tonic-gate 	char constant[MAX_DEVLINK_LINE];
62197c478bd9Sstevel@tonic-gate 	char *error_str;
62207c478bd9Sstevel@tonic-gate 
62217c478bd9Sstevel@tonic-gate 	if (link == NULL) {
62227c478bd9Sstevel@tonic-gate 		return (NULL);
62237c478bd9Sstevel@tonic-gate 	}
62247c478bd9Sstevel@tonic-gate 
62257c478bd9Sstevel@tonic-gate 	while ((*link != '\0') && (error == FALSE)) {
62267c478bd9Sstevel@tonic-gate 		link_list = (link_list_t *)s_malloc(sizeof (link_list_t));
62277c478bd9Sstevel@tonic-gate 		link_list->next = NULL;
62287c478bd9Sstevel@tonic-gate 
62297c478bd9Sstevel@tonic-gate 		while ((*link != '\0') && (*link != '\\')) {
62307c478bd9Sstevel@tonic-gate 			/* a non-escaped string */
62317c478bd9Sstevel@tonic-gate 			constant[x++] = *(link++);
62327c478bd9Sstevel@tonic-gate 		}
62337c478bd9Sstevel@tonic-gate 		if (x != 0) {
62347c478bd9Sstevel@tonic-gate 			constant[x] = '\0';
62357c478bd9Sstevel@tonic-gate 			link_list->type = CONSTANT;
62367c478bd9Sstevel@tonic-gate 			link_list->constant = s_strdup(constant);
62377c478bd9Sstevel@tonic-gate 			x = 0;
62387c478bd9Sstevel@tonic-gate 			vprint(DEVLINK_MID, "CONSTANT FOUND %s\n", constant);
62397c478bd9Sstevel@tonic-gate 		} else {
62407c478bd9Sstevel@tonic-gate 			switch (*(++link)) {
62417c478bd9Sstevel@tonic-gate 			case 'M':
62427c478bd9Sstevel@tonic-gate 				link_list->type = MINOR;
62437c478bd9Sstevel@tonic-gate 				break;
62447c478bd9Sstevel@tonic-gate 			case 'A':
62457c478bd9Sstevel@tonic-gate 				link_list->type = ADDR;
62467c478bd9Sstevel@tonic-gate 				break;
62477c478bd9Sstevel@tonic-gate 			case 'N':
62487c478bd9Sstevel@tonic-gate 				if (counter_found == TRUE) {
62497c478bd9Sstevel@tonic-gate 					error = TRUE;
62507c478bd9Sstevel@tonic-gate 					error_str = "multiple counters "
62517c478bd9Sstevel@tonic-gate 						"not permitted";
62527c478bd9Sstevel@tonic-gate 					free(link_list);
62537c478bd9Sstevel@tonic-gate 				} else {
62547c478bd9Sstevel@tonic-gate 					counter_found = TRUE;
62557c478bd9Sstevel@tonic-gate 					link_list->type = COUNTER;
62567c478bd9Sstevel@tonic-gate 				}
62577c478bd9Sstevel@tonic-gate 				break;
62587c478bd9Sstevel@tonic-gate 			case 'D':
62597c478bd9Sstevel@tonic-gate 				link_list->type = NAME;
62607c478bd9Sstevel@tonic-gate 				break;
62617c478bd9Sstevel@tonic-gate 			default:
62627c478bd9Sstevel@tonic-gate 				error = TRUE;
62637c478bd9Sstevel@tonic-gate 				free(link_list);
62647c478bd9Sstevel@tonic-gate 				error_str = "unrecognized escape sequence";
62657c478bd9Sstevel@tonic-gate 				break;
62667c478bd9Sstevel@tonic-gate 			}
62677c478bd9Sstevel@tonic-gate 			if (*(link++) != 'D') {
62687c478bd9Sstevel@tonic-gate 				if (isdigit(*link) == FALSE) {
62697c478bd9Sstevel@tonic-gate 					error_str = "escape sequence must be "
62707c478bd9Sstevel@tonic-gate 						"followed by a digit\n";
62717c478bd9Sstevel@tonic-gate 					error = TRUE;
62727c478bd9Sstevel@tonic-gate 					free(link_list);
62737c478bd9Sstevel@tonic-gate 				} else {
62747c478bd9Sstevel@tonic-gate 					link_list->arg =
62757c478bd9Sstevel@tonic-gate 						(int)strtoul(link, &link, 10);
62767c478bd9Sstevel@tonic-gate 					vprint(DEVLINK_MID, "link_list->arg = "
62777c478bd9Sstevel@tonic-gate 						"%d\n", link_list->arg);
62787c478bd9Sstevel@tonic-gate 				}
62797c478bd9Sstevel@tonic-gate 			}
62807c478bd9Sstevel@tonic-gate 		}
62817c478bd9Sstevel@tonic-gate 		/* append link_list struct to end of list */
62827c478bd9Sstevel@tonic-gate 		if (error == FALSE) {
62837c478bd9Sstevel@tonic-gate 			for (ptr = &head; *ptr != NULL; ptr = &((*ptr)->next));
62847c478bd9Sstevel@tonic-gate 			*ptr = link_list;
62857c478bd9Sstevel@tonic-gate 		}
62867c478bd9Sstevel@tonic-gate 	}
62877c478bd9Sstevel@tonic-gate 
62887c478bd9Sstevel@tonic-gate 	if (error == FALSE) {
62897c478bd9Sstevel@tonic-gate 		return (head);
62907c478bd9Sstevel@tonic-gate 	} else {
62917c478bd9Sstevel@tonic-gate 		err_print(CONFIG_INCORRECT, devlinktab_line, devlinktab_file,
62927c478bd9Sstevel@tonic-gate 		    error_str);
62937c478bd9Sstevel@tonic-gate 		free_link_list(head);
62947c478bd9Sstevel@tonic-gate 		return (NULL);
62957c478bd9Sstevel@tonic-gate 	}
62967c478bd9Sstevel@tonic-gate }
62977c478bd9Sstevel@tonic-gate 
62987c478bd9Sstevel@tonic-gate /*
62997c478bd9Sstevel@tonic-gate  * Called for each minor node devfsadm processes; for each minor node,
63007c478bd9Sstevel@tonic-gate  * look for matches in the devlinktab_list list which was created on
63017c478bd9Sstevel@tonic-gate  * startup read_devlinktab_file().  If there is a match, call build_links()
63027c478bd9Sstevel@tonic-gate  * to build a logical devlink and a possible extra devlink.
63037c478bd9Sstevel@tonic-gate  */
63047c478bd9Sstevel@tonic-gate static int
63057c478bd9Sstevel@tonic-gate process_devlink_compat(di_minor_t minor, di_node_t node)
63067c478bd9Sstevel@tonic-gate {
63077c478bd9Sstevel@tonic-gate 	int link_built = FALSE;
63087c478bd9Sstevel@tonic-gate 	devlinktab_list_t *entry;
63097c478bd9Sstevel@tonic-gate 	char *nodetype;
63107c478bd9Sstevel@tonic-gate 	char *dev_path;
63117c478bd9Sstevel@tonic-gate 
63127c478bd9Sstevel@tonic-gate 	if (devlinks_debug == TRUE) {
63137c478bd9Sstevel@tonic-gate 		nodetype =  di_minor_nodetype(minor);
63147c478bd9Sstevel@tonic-gate 		assert(nodetype != NULL);
63157c478bd9Sstevel@tonic-gate 		if ((dev_path = di_devfs_path(node)) != NULL) {
63167c478bd9Sstevel@tonic-gate 			vprint(INFO_MID, "'%s' entry: %s:%s\n", nodetype,
63177c478bd9Sstevel@tonic-gate 				dev_path,
63187c478bd9Sstevel@tonic-gate 				di_minor_name(minor) ? di_minor_name(minor) :
63197c478bd9Sstevel@tonic-gate 				"");
63207c478bd9Sstevel@tonic-gate 			di_devfs_path_free(dev_path);
63217c478bd9Sstevel@tonic-gate 		}
63227c478bd9Sstevel@tonic-gate 
63237c478bd9Sstevel@tonic-gate 	}
63247c478bd9Sstevel@tonic-gate 
63257c478bd9Sstevel@tonic-gate 
63267c478bd9Sstevel@tonic-gate 	/* don't process devlink.tab if devfsadm invoked with -c <class> */
63277c478bd9Sstevel@tonic-gate 	if (num_classes > 0) {
63287c478bd9Sstevel@tonic-gate 		return (FALSE);
63297c478bd9Sstevel@tonic-gate 	}
63307c478bd9Sstevel@tonic-gate 
63317c478bd9Sstevel@tonic-gate 	for (entry = devlinktab_list; entry != NULL; entry = entry->next) {
63327c478bd9Sstevel@tonic-gate 		if (devlink_matches(entry, minor, node) == DEVFSADM_SUCCESS) {
63337c478bd9Sstevel@tonic-gate 			link_built = TRUE;
63347c478bd9Sstevel@tonic-gate 			(void) build_links(entry, minor, node);
63357c478bd9Sstevel@tonic-gate 		}
63367c478bd9Sstevel@tonic-gate 	}
63377c478bd9Sstevel@tonic-gate 	return (link_built);
63387c478bd9Sstevel@tonic-gate }
63397c478bd9Sstevel@tonic-gate 
63407c478bd9Sstevel@tonic-gate /*
63417c478bd9Sstevel@tonic-gate  * For a given devlink.tab devlinktab_list entry, see if the selector
63427c478bd9Sstevel@tonic-gate  * field matches this minor node.  If it does, return DEVFSADM_SUCCESS,
63437c478bd9Sstevel@tonic-gate  * otherwise DEVFSADM_FAILURE.
63447c478bd9Sstevel@tonic-gate  */
63457c478bd9Sstevel@tonic-gate static int
63467c478bd9Sstevel@tonic-gate devlink_matches(devlinktab_list_t *entry, di_minor_t minor, di_node_t node)
63477c478bd9Sstevel@tonic-gate {
63487c478bd9Sstevel@tonic-gate 	selector_list_t *selector = entry->selector;
63497c478bd9Sstevel@tonic-gate 	char *addr;
63507c478bd9Sstevel@tonic-gate 	char *minor_name;
63517c478bd9Sstevel@tonic-gate 	char *node_type;
63527c478bd9Sstevel@tonic-gate 
63537c478bd9Sstevel@tonic-gate 	for (; selector != NULL; selector = selector->next) {
63547c478bd9Sstevel@tonic-gate 		switch (selector->key) {
63557c478bd9Sstevel@tonic-gate 		case NAME:
63567c478bd9Sstevel@tonic-gate 			if (strcmp(di_node_name(node), selector->val) != 0) {
63577c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
63587c478bd9Sstevel@tonic-gate 			}
63597c478bd9Sstevel@tonic-gate 			break;
63607c478bd9Sstevel@tonic-gate 		case TYPE:
63617c478bd9Sstevel@tonic-gate 			node_type = di_minor_nodetype(minor);
63627c478bd9Sstevel@tonic-gate 			assert(node_type != NULL);
63637c478bd9Sstevel@tonic-gate 			if (strcmp(node_type, selector->val) != 0) {
63647c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
63657c478bd9Sstevel@tonic-gate 			}
63667c478bd9Sstevel@tonic-gate 			break;
63677c478bd9Sstevel@tonic-gate 		case ADDR:
63687c478bd9Sstevel@tonic-gate 			if ((addr = di_bus_addr(node)) == NULL) {
63697c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
63707c478bd9Sstevel@tonic-gate 			}
63717c478bd9Sstevel@tonic-gate 			if (selector->arg == 0) {
63727c478bd9Sstevel@tonic-gate 				if (strcmp(addr, selector->val) != 0) {
63737c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
63747c478bd9Sstevel@tonic-gate 				}
63757c478bd9Sstevel@tonic-gate 			} else {
63767c478bd9Sstevel@tonic-gate 				if (compare_field(addr, selector->val,
63777c478bd9Sstevel@tonic-gate 				    selector->arg) == DEVFSADM_FAILURE) {
63787c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
63797c478bd9Sstevel@tonic-gate 				}
63807c478bd9Sstevel@tonic-gate 			}
63817c478bd9Sstevel@tonic-gate 			break;
63827c478bd9Sstevel@tonic-gate 		case MINOR:
63837c478bd9Sstevel@tonic-gate 			if ((minor_name = di_minor_name(minor)) == NULL) {
63847c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
63857c478bd9Sstevel@tonic-gate 			}
63867c478bd9Sstevel@tonic-gate 			if (selector->arg == 0) {
63877c478bd9Sstevel@tonic-gate 				if (strcmp(minor_name, selector->val) != 0) {
63887c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
63897c478bd9Sstevel@tonic-gate 				}
63907c478bd9Sstevel@tonic-gate 			} else {
63917c478bd9Sstevel@tonic-gate 				if (compare_field(minor_name, selector->val,
63927c478bd9Sstevel@tonic-gate 					selector->arg) == DEVFSADM_FAILURE) {
63937c478bd9Sstevel@tonic-gate 					return (DEVFSADM_FAILURE);
63947c478bd9Sstevel@tonic-gate 				}
63957c478bd9Sstevel@tonic-gate 			}
63967c478bd9Sstevel@tonic-gate 			break;
63977c478bd9Sstevel@tonic-gate 		default:
63987c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
63997c478bd9Sstevel@tonic-gate 		}
64007c478bd9Sstevel@tonic-gate 	}
64017c478bd9Sstevel@tonic-gate 
64027c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
64037c478bd9Sstevel@tonic-gate }
64047c478bd9Sstevel@tonic-gate 
64057c478bd9Sstevel@tonic-gate /*
64067c478bd9Sstevel@tonic-gate  * For the given minor node and devlinktab_list entry from devlink.tab,
64077c478bd9Sstevel@tonic-gate  * build a logical dev link and a possible extra devlink.
64087c478bd9Sstevel@tonic-gate  * Return DEVFSADM_SUCCESS if link is created, otherwise DEVFSADM_FAILURE.
64097c478bd9Sstevel@tonic-gate  */
64107c478bd9Sstevel@tonic-gate static int
64117c478bd9Sstevel@tonic-gate build_links(devlinktab_list_t *entry, di_minor_t minor, di_node_t node)
64127c478bd9Sstevel@tonic-gate {
64137c478bd9Sstevel@tonic-gate 	char secondary_link[PATH_MAX + 1];
64147c478bd9Sstevel@tonic-gate 	char primary_link[PATH_MAX + 1];
64157c478bd9Sstevel@tonic-gate 	char contents[PATH_MAX + 1];
64167c478bd9Sstevel@tonic-gate 	char *dev_path;
64177c478bd9Sstevel@tonic-gate 
64187c478bd9Sstevel@tonic-gate 	if ((dev_path = di_devfs_path(node)) == NULL) {
64197c478bd9Sstevel@tonic-gate 		err_print(DI_DEVFS_PATH_FAILED, strerror(errno));
64207c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
64217c478bd9Sstevel@tonic-gate 	}
64227c478bd9Sstevel@tonic-gate 	(void) strcpy(contents, dev_path);
64237c478bd9Sstevel@tonic-gate 	di_devfs_path_free(dev_path);
64247c478bd9Sstevel@tonic-gate 
64257c478bd9Sstevel@tonic-gate 	(void) strcat(contents, ":");
64267c478bd9Sstevel@tonic-gate 	(void) strcat(contents, di_minor_name(minor));
64277c478bd9Sstevel@tonic-gate 
64287c478bd9Sstevel@tonic-gate 	if (construct_devlink(primary_link, entry->p_link, contents,
64297c478bd9Sstevel@tonic-gate 				minor, node,
64307c478bd9Sstevel@tonic-gate 			    entry->p_link_pattern) == DEVFSADM_FAILURE) {
64317c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
64327c478bd9Sstevel@tonic-gate 	}
64337c478bd9Sstevel@tonic-gate 	(void) devfsadm_mklink(primary_link, node, minor, 0);
64347c478bd9Sstevel@tonic-gate 
64357c478bd9Sstevel@tonic-gate 	if (entry->s_link == NULL) {
64367c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
64377c478bd9Sstevel@tonic-gate 	}
64387c478bd9Sstevel@tonic-gate 
64397c478bd9Sstevel@tonic-gate 	if (construct_devlink(secondary_link, entry->s_link,
64407c478bd9Sstevel@tonic-gate 			primary_link, minor, node,
64417c478bd9Sstevel@tonic-gate 				entry->s_link_pattern) == DEVFSADM_FAILURE) {
64427c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
64437c478bd9Sstevel@tonic-gate 	}
64447c478bd9Sstevel@tonic-gate 
64457c478bd9Sstevel@tonic-gate 	(void) devfsadm_secondary_link(secondary_link, primary_link, 0);
64467c478bd9Sstevel@tonic-gate 
64477c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
64487c478bd9Sstevel@tonic-gate }
64497c478bd9Sstevel@tonic-gate 
64507c478bd9Sstevel@tonic-gate /*
64517c478bd9Sstevel@tonic-gate  * The counter rule for devlink.tab entries is implemented via
64527c478bd9Sstevel@tonic-gate  * devfsadm_enumerate_int_start(). One of the arguments to this function
64537c478bd9Sstevel@tonic-gate  * is a path, where each path component is treated as a regular expression.
64547c478bd9Sstevel@tonic-gate  * For devlink.tab entries, this path regular expression is derived from
64557c478bd9Sstevel@tonic-gate  * the devlink spec. get_anchored_re() accepts path regular expressions derived
64567c478bd9Sstevel@tonic-gate  * from devlink.tab entries and inserts the anchors '^' and '$' at the beginning
64577c478bd9Sstevel@tonic-gate  * and end respectively of each path component. This is done to prevent
64587c478bd9Sstevel@tonic-gate  * false matches. For example, without anchors, "a/([0-9]+)" will match "ab/c9"
64597c478bd9Sstevel@tonic-gate  * and incorrect links will be generated.
64607c478bd9Sstevel@tonic-gate  */
64617c478bd9Sstevel@tonic-gate static int
64627c478bd9Sstevel@tonic-gate get_anchored_re(char *link, char *anchored_re, char *pattern)
64637c478bd9Sstevel@tonic-gate {
64647c478bd9Sstevel@tonic-gate 	if (*link == '/' || *link == '\0') {
64657c478bd9Sstevel@tonic-gate 		err_print(INVALID_DEVLINK_SPEC, pattern);
64667c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
64677c478bd9Sstevel@tonic-gate 	}
64687c478bd9Sstevel@tonic-gate 
64697c478bd9Sstevel@tonic-gate 	*anchored_re++ = '^';
64707c478bd9Sstevel@tonic-gate 	for (; *link != '\0'; ) {
64717c478bd9Sstevel@tonic-gate 		if (*link == '/') {
64727c478bd9Sstevel@tonic-gate 			while (*link == '/')
64737c478bd9Sstevel@tonic-gate 				link++;
64747c478bd9Sstevel@tonic-gate 			*anchored_re++ = '$';
64757c478bd9Sstevel@tonic-gate 			*anchored_re++ = '/';
64767c478bd9Sstevel@tonic-gate 			if (*link != '\0') {
64777c478bd9Sstevel@tonic-gate 				*anchored_re++ = '^';
64787c478bd9Sstevel@tonic-gate 			}
64797c478bd9Sstevel@tonic-gate 		} else {
64807c478bd9Sstevel@tonic-gate 			*anchored_re++ = *link++;
64817c478bd9Sstevel@tonic-gate 			if (*link == '\0') {
64827c478bd9Sstevel@tonic-gate 				*anchored_re++ = '$';
64837c478bd9Sstevel@tonic-gate 			}
64847c478bd9Sstevel@tonic-gate 		}
64857c478bd9Sstevel@tonic-gate 	}
64867c478bd9Sstevel@tonic-gate 	*anchored_re = '\0';
64877c478bd9Sstevel@tonic-gate 
64887c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
64897c478bd9Sstevel@tonic-gate }
64907c478bd9Sstevel@tonic-gate 
64917c478bd9Sstevel@tonic-gate static int
64927c478bd9Sstevel@tonic-gate construct_devlink(char *link, link_list_t *link_build, char *contents,
64937c478bd9Sstevel@tonic-gate 			di_minor_t minor, di_node_t node, char *pattern)
64947c478bd9Sstevel@tonic-gate {
64957c478bd9Sstevel@tonic-gate 	int counter_offset = -1;
64967c478bd9Sstevel@tonic-gate 	devfsadm_enumerate_t rules[1] = {NULL};
64977c478bd9Sstevel@tonic-gate 	char templink[PATH_MAX + 1];
64987c478bd9Sstevel@tonic-gate 	char *buff;
64997c478bd9Sstevel@tonic-gate 	char start[10];
65007c478bd9Sstevel@tonic-gate 	char *node_path;
65017c478bd9Sstevel@tonic-gate 	char anchored_re[PATH_MAX + 1];
65027c478bd9Sstevel@tonic-gate 
65037c478bd9Sstevel@tonic-gate 	link[0] = '\0';
65047c478bd9Sstevel@tonic-gate 
65057c478bd9Sstevel@tonic-gate 	for (; link_build != NULL; link_build = link_build->next) {
65067c478bd9Sstevel@tonic-gate 		switch (link_build->type) {
65077c478bd9Sstevel@tonic-gate 		case NAME:
65087c478bd9Sstevel@tonic-gate 			(void) strcat(link, di_node_name(node));
65097c478bd9Sstevel@tonic-gate 			break;
65107c478bd9Sstevel@tonic-gate 		case CONSTANT:
65117c478bd9Sstevel@tonic-gate 			(void) strcat(link, link_build->constant);
65127c478bd9Sstevel@tonic-gate 			break;
65137c478bd9Sstevel@tonic-gate 		case ADDR:
65147c478bd9Sstevel@tonic-gate 			if (component_cat(link, di_bus_addr(node),
65157c478bd9Sstevel@tonic-gate 				    link_build->arg) == DEVFSADM_FAILURE) {
65167c478bd9Sstevel@tonic-gate 				node_path = di_devfs_path(node);
65177c478bd9Sstevel@tonic-gate 				err_print(CANNOT_BE_USED, pattern, node_path,
65187c478bd9Sstevel@tonic-gate 					    di_minor_name(minor));
65197c478bd9Sstevel@tonic-gate 				di_devfs_path_free(node_path);
65207c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
65217c478bd9Sstevel@tonic-gate 			}
65227c478bd9Sstevel@tonic-gate 			break;
65237c478bd9Sstevel@tonic-gate 		case MINOR:
65247c478bd9Sstevel@tonic-gate 			if (component_cat(link, di_minor_name(minor),
65257c478bd9Sstevel@tonic-gate 				    link_build->arg) == DEVFSADM_FAILURE) {
65267c478bd9Sstevel@tonic-gate 				node_path = di_devfs_path(node);
65277c478bd9Sstevel@tonic-gate 				err_print(CANNOT_BE_USED, pattern, node_path,
65287c478bd9Sstevel@tonic-gate 					    di_minor_name(minor));
65297c478bd9Sstevel@tonic-gate 				di_devfs_path_free(node_path);
65307c478bd9Sstevel@tonic-gate 				return (DEVFSADM_FAILURE);
65317c478bd9Sstevel@tonic-gate 			}
65327c478bd9Sstevel@tonic-gate 			break;
65337c478bd9Sstevel@tonic-gate 		case COUNTER:
65347c478bd9Sstevel@tonic-gate 			counter_offset = strlen(link);
65357c478bd9Sstevel@tonic-gate 			(void) strcat(link, "([0-9]+)");
65367c478bd9Sstevel@tonic-gate 			(void) sprintf(start, "%d", link_build->arg);
65377c478bd9Sstevel@tonic-gate 			break;
65387c478bd9Sstevel@tonic-gate 		default:
65397c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
65407c478bd9Sstevel@tonic-gate 		}
65417c478bd9Sstevel@tonic-gate 	}
65427c478bd9Sstevel@tonic-gate 
65437c478bd9Sstevel@tonic-gate 	if (counter_offset != -1) {
65447c478bd9Sstevel@tonic-gate 		/*
65457c478bd9Sstevel@tonic-gate 		 * copy anything appended after "([0-9]+)" into
65467c478bd9Sstevel@tonic-gate 		 * templink
65477c478bd9Sstevel@tonic-gate 		 */
65487c478bd9Sstevel@tonic-gate 
65497c478bd9Sstevel@tonic-gate 		(void) strcpy(templink,
65507c478bd9Sstevel@tonic-gate 			    &link[counter_offset + strlen("([0-9]+)")]);
65517c478bd9Sstevel@tonic-gate 		if (get_anchored_re(link, anchored_re, pattern)
65527c478bd9Sstevel@tonic-gate 		    != DEVFSADM_SUCCESS) {
65537c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
65547c478bd9Sstevel@tonic-gate 		}
65557c478bd9Sstevel@tonic-gate 		rules[0].re = anchored_re;
65567c478bd9Sstevel@tonic-gate 		rules[0].subexp = 1;
65577c478bd9Sstevel@tonic-gate 		rules[0].flags = MATCH_ALL;
65587c478bd9Sstevel@tonic-gate 		if (devfsadm_enumerate_int_start(contents, 0, &buff,
65597c478bd9Sstevel@tonic-gate 		    rules, 1, start) == DEVFSADM_FAILURE) {
65607c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
65617c478bd9Sstevel@tonic-gate 		}
65627c478bd9Sstevel@tonic-gate 		(void) strcpy(&link[counter_offset], buff);
65637c478bd9Sstevel@tonic-gate 		free(buff);
65647c478bd9Sstevel@tonic-gate 		(void) strcat(link, templink);
65657c478bd9Sstevel@tonic-gate 		vprint(DEVLINK_MID, "COUNTER is	%s\n", link);
65667c478bd9Sstevel@tonic-gate 	}
65677c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
65687c478bd9Sstevel@tonic-gate }
65697c478bd9Sstevel@tonic-gate 
65707c478bd9Sstevel@tonic-gate /*
65717c478bd9Sstevel@tonic-gate  * Compares "field" number of the comma separated list "full_name" with
65727c478bd9Sstevel@tonic-gate  * field_item.	Returns DEVFSADM_SUCCESS for match,
65737c478bd9Sstevel@tonic-gate  * DEVFSADM_FAILURE for no match.
65747c478bd9Sstevel@tonic-gate  */
65757c478bd9Sstevel@tonic-gate static int
65767c478bd9Sstevel@tonic-gate compare_field(char *full_name, char *field_item, int field)
65777c478bd9Sstevel@tonic-gate {
65787c478bd9Sstevel@tonic-gate 	--field;
65797c478bd9Sstevel@tonic-gate 	while ((*full_name != '\0') && (field != 0)) {
65807c478bd9Sstevel@tonic-gate 		if (*(full_name++) == ',') {
65817c478bd9Sstevel@tonic-gate 			field--;
65827c478bd9Sstevel@tonic-gate 		}
65837c478bd9Sstevel@tonic-gate 	}
65847c478bd9Sstevel@tonic-gate 
65857c478bd9Sstevel@tonic-gate 	if (field != 0) {
65867c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
65877c478bd9Sstevel@tonic-gate 	}
65887c478bd9Sstevel@tonic-gate 
65897c478bd9Sstevel@tonic-gate 	while ((*full_name != '\0') && (*field_item != '\0') &&
65907c478bd9Sstevel@tonic-gate 			(*full_name != ',')) {
65917c478bd9Sstevel@tonic-gate 		if (*(full_name++) != *(field_item++)) {
65927c478bd9Sstevel@tonic-gate 			return (DEVFSADM_FAILURE);
65937c478bd9Sstevel@tonic-gate 		}
65947c478bd9Sstevel@tonic-gate 	}
65957c478bd9Sstevel@tonic-gate 
65967c478bd9Sstevel@tonic-gate 	if (*field_item != '\0') {
65977c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
65987c478bd9Sstevel@tonic-gate 	}
65997c478bd9Sstevel@tonic-gate 
66007c478bd9Sstevel@tonic-gate 	if ((*full_name == '\0') || (*full_name == ','))
66017c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
66027c478bd9Sstevel@tonic-gate 
66037c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FAILURE);
66047c478bd9Sstevel@tonic-gate }
66057c478bd9Sstevel@tonic-gate 
66067c478bd9Sstevel@tonic-gate /*
66077c478bd9Sstevel@tonic-gate  * strcat() field # "field" of comma separated list "name" to "link".
66087c478bd9Sstevel@tonic-gate  * Field 0 is the entire name.
66097c478bd9Sstevel@tonic-gate  * Return DEVFSADM_SUCCESS or DEVFSADM_FAILURE.
66107c478bd9Sstevel@tonic-gate  */
66117c478bd9Sstevel@tonic-gate static int
66127c478bd9Sstevel@tonic-gate component_cat(char *link, char *name, int field)
66137c478bd9Sstevel@tonic-gate {
66147c478bd9Sstevel@tonic-gate 
66157c478bd9Sstevel@tonic-gate 	if (name == NULL) {
66167c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
66177c478bd9Sstevel@tonic-gate 	}
66187c478bd9Sstevel@tonic-gate 
66197c478bd9Sstevel@tonic-gate 	if (field == 0) {
66207c478bd9Sstevel@tonic-gate 		(void) strcat(link, name);
66217c478bd9Sstevel@tonic-gate 		return (DEVFSADM_SUCCESS);
66227c478bd9Sstevel@tonic-gate 	}
66237c478bd9Sstevel@tonic-gate 
66247c478bd9Sstevel@tonic-gate 	while (*link != '\0') {
66257c478bd9Sstevel@tonic-gate 		link++;
66267c478bd9Sstevel@tonic-gate 	}
66277c478bd9Sstevel@tonic-gate 
66287c478bd9Sstevel@tonic-gate 	--field;
66297c478bd9Sstevel@tonic-gate 	while ((*name != '\0') && (field != 0)) {
66307c478bd9Sstevel@tonic-gate 		if (*(name++) == ',') {
66317c478bd9Sstevel@tonic-gate 			--field;
66327c478bd9Sstevel@tonic-gate 		}
66337c478bd9Sstevel@tonic-gate 	}
66347c478bd9Sstevel@tonic-gate 
66357c478bd9Sstevel@tonic-gate 	if (field != 0) {
66367c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
66377c478bd9Sstevel@tonic-gate 	}
66387c478bd9Sstevel@tonic-gate 
66397c478bd9Sstevel@tonic-gate 	while ((*name != '\0') && (*name != ',')) {
66407c478bd9Sstevel@tonic-gate 		*(link++) = *(name++);
66417c478bd9Sstevel@tonic-gate 	}
66427c478bd9Sstevel@tonic-gate 
66437c478bd9Sstevel@tonic-gate 	*link = '\0';
66447c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
66457c478bd9Sstevel@tonic-gate }
66467c478bd9Sstevel@tonic-gate 
66477c478bd9Sstevel@tonic-gate static void
66487c478bd9Sstevel@tonic-gate free_selector_list(selector_list_t *head)
66497c478bd9Sstevel@tonic-gate {
66507c478bd9Sstevel@tonic-gate 	selector_list_t *temp;
66517c478bd9Sstevel@tonic-gate 
66527c478bd9Sstevel@tonic-gate 	while (head != NULL) {
66537c478bd9Sstevel@tonic-gate 		temp = head;
66547c478bd9Sstevel@tonic-gate 		head = head->next;
66557c478bd9Sstevel@tonic-gate 		free(temp->val);
66567c478bd9Sstevel@tonic-gate 		free(temp);
66577c478bd9Sstevel@tonic-gate 	}
66587c478bd9Sstevel@tonic-gate }
66597c478bd9Sstevel@tonic-gate 
66607c478bd9Sstevel@tonic-gate static void
66617c478bd9Sstevel@tonic-gate free_link_list(link_list_t *head)
66627c478bd9Sstevel@tonic-gate {
66637c478bd9Sstevel@tonic-gate 	link_list_t *temp;
66647c478bd9Sstevel@tonic-gate 
66657c478bd9Sstevel@tonic-gate 	while (head != NULL) {
66667c478bd9Sstevel@tonic-gate 		temp = head;
66677c478bd9Sstevel@tonic-gate 		head = head->next;
66687c478bd9Sstevel@tonic-gate 		if (temp->type == CONSTANT) {
66697c478bd9Sstevel@tonic-gate 			free(temp->constant);
66707c478bd9Sstevel@tonic-gate 		}
66717c478bd9Sstevel@tonic-gate 		free(temp);
66727c478bd9Sstevel@tonic-gate 	}
66737c478bd9Sstevel@tonic-gate }
66747c478bd9Sstevel@tonic-gate 
66757c478bd9Sstevel@tonic-gate /*
66767c478bd9Sstevel@tonic-gate  * Prints only if level matches one of the debug levels
66777c478bd9Sstevel@tonic-gate  * given on command line.  INFO_MID is always printed.
66787c478bd9Sstevel@tonic-gate  *
66797c478bd9Sstevel@tonic-gate  * See devfsadm.h for a listing of globally defined levels and
66807c478bd9Sstevel@tonic-gate  * meanings.  Modules should prefix the level with their
66817c478bd9Sstevel@tonic-gate  * module name to prevent collisions.
66827c478bd9Sstevel@tonic-gate  */
66837c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/
66847c478bd9Sstevel@tonic-gate void
66857c478bd9Sstevel@tonic-gate devfsadm_print(char *msgid, char *message, ...)
66867c478bd9Sstevel@tonic-gate {
66877c478bd9Sstevel@tonic-gate 	va_list ap;
66887c478bd9Sstevel@tonic-gate 	static int newline = TRUE;
66897c478bd9Sstevel@tonic-gate 	int x;
66907c478bd9Sstevel@tonic-gate 
66917c478bd9Sstevel@tonic-gate 	if (msgid != NULL) {
66927c478bd9Sstevel@tonic-gate 		for (x = 0; x < num_verbose; x++) {
66937c478bd9Sstevel@tonic-gate 			if (strcmp(verbose[x], msgid) == 0) {
66947c478bd9Sstevel@tonic-gate 				break;
66957c478bd9Sstevel@tonic-gate 			}
66967c478bd9Sstevel@tonic-gate 			if (strcmp(verbose[x], ALL_MID) == 0) {
66977c478bd9Sstevel@tonic-gate 				break;
66987c478bd9Sstevel@tonic-gate 			}
66997c478bd9Sstevel@tonic-gate 		}
67007c478bd9Sstevel@tonic-gate 		if (x == num_verbose) {
67017c478bd9Sstevel@tonic-gate 			return;
67027c478bd9Sstevel@tonic-gate 		}
67037c478bd9Sstevel@tonic-gate 	}
67047c478bd9Sstevel@tonic-gate 
67057c478bd9Sstevel@tonic-gate 	va_start(ap, message);
67067c478bd9Sstevel@tonic-gate 
67077c478bd9Sstevel@tonic-gate 	if (msgid == NULL) {
67087c478bd9Sstevel@tonic-gate 		if (logflag == TRUE) {
67097c478bd9Sstevel@tonic-gate 			(void) vsyslog(LOG_NOTICE, message, ap);
67107c478bd9Sstevel@tonic-gate 		} else {
67117c478bd9Sstevel@tonic-gate 			(void) vfprintf(stdout, message, ap);
67127c478bd9Sstevel@tonic-gate 		}
67137c478bd9Sstevel@tonic-gate 
67147c478bd9Sstevel@tonic-gate 	} else {
67157c478bd9Sstevel@tonic-gate 		if (logflag == TRUE) {
67167c478bd9Sstevel@tonic-gate 			(void) syslog(LOG_DEBUG, "%s[%ld]: %s: ",
67177c478bd9Sstevel@tonic-gate 				    prog, getpid(), msgid);
67187c478bd9Sstevel@tonic-gate 			(void) vsyslog(LOG_DEBUG, message, ap);
67197c478bd9Sstevel@tonic-gate 		} else {
67207c478bd9Sstevel@tonic-gate 			if (newline == TRUE) {
67217c478bd9Sstevel@tonic-gate 				(void) fprintf(stdout, "%s[%ld]: %s: ",
67227c478bd9Sstevel@tonic-gate 					prog, getpid(), msgid);
67237c478bd9Sstevel@tonic-gate 			}
67247c478bd9Sstevel@tonic-gate 			(void) vfprintf(stdout, message, ap);
67257c478bd9Sstevel@tonic-gate 		}
67267c478bd9Sstevel@tonic-gate 	}
67277c478bd9Sstevel@tonic-gate 
67287c478bd9Sstevel@tonic-gate 	if (message[strlen(message) - 1] == '\n') {
67297c478bd9Sstevel@tonic-gate 		newline = TRUE;
67307c478bd9Sstevel@tonic-gate 	} else {
67317c478bd9Sstevel@tonic-gate 		newline = FALSE;
67327c478bd9Sstevel@tonic-gate 	}
67337c478bd9Sstevel@tonic-gate 	va_end(ap);
67347c478bd9Sstevel@tonic-gate }
67357c478bd9Sstevel@tonic-gate 
67367c478bd9Sstevel@tonic-gate /*
67377c478bd9Sstevel@tonic-gate  * print error messages to the terminal or to syslog
67387c478bd9Sstevel@tonic-gate  */
67397c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
67407c478bd9Sstevel@tonic-gate void
67417c478bd9Sstevel@tonic-gate devfsadm_errprint(char *message, ...)
67427c478bd9Sstevel@tonic-gate {
67437c478bd9Sstevel@tonic-gate 	va_list ap;
67447c478bd9Sstevel@tonic-gate 
67457c478bd9Sstevel@tonic-gate 	va_start(ap, message);
67467c478bd9Sstevel@tonic-gate 
67477c478bd9Sstevel@tonic-gate 	if (logflag == TRUE) {
67487c478bd9Sstevel@tonic-gate 		(void) vsyslog(LOG_ERR, message, ap);
67497c478bd9Sstevel@tonic-gate 	} else {
67507c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "%s: ", prog);
67517c478bd9Sstevel@tonic-gate 		(void) vfprintf(stderr, message, ap);
67527c478bd9Sstevel@tonic-gate 	}
67537c478bd9Sstevel@tonic-gate 	va_end(ap);
67547c478bd9Sstevel@tonic-gate }
67557c478bd9Sstevel@tonic-gate 
67567c478bd9Sstevel@tonic-gate /*
67577c478bd9Sstevel@tonic-gate  * return noupdate state (-s)
67587c478bd9Sstevel@tonic-gate  */
67597c478bd9Sstevel@tonic-gate int
67607c478bd9Sstevel@tonic-gate devfsadm_noupdate(void)
67617c478bd9Sstevel@tonic-gate {
67627c478bd9Sstevel@tonic-gate 	return (file_mods == TRUE ? DEVFSADM_TRUE : DEVFSADM_FALSE);
67637c478bd9Sstevel@tonic-gate }
67647c478bd9Sstevel@tonic-gate 
67657c478bd9Sstevel@tonic-gate /*
67667c478bd9Sstevel@tonic-gate  * return current root update path (-r)
67677c478bd9Sstevel@tonic-gate  */
67687c478bd9Sstevel@tonic-gate const char *
67697c478bd9Sstevel@tonic-gate devfsadm_root_path(void)
67707c478bd9Sstevel@tonic-gate {
67717c478bd9Sstevel@tonic-gate 	if (root_dir[0] == '\0') {
67727c478bd9Sstevel@tonic-gate 		return ("/");
67737c478bd9Sstevel@tonic-gate 	} else {
67747c478bd9Sstevel@tonic-gate 		return ((const char *)root_dir);
67757c478bd9Sstevel@tonic-gate 	}
67767c478bd9Sstevel@tonic-gate }
67777c478bd9Sstevel@tonic-gate 
67787c478bd9Sstevel@tonic-gate /* common exit function which ensures releasing locks */
67797c478bd9Sstevel@tonic-gate static void
67807c478bd9Sstevel@tonic-gate devfsadm_exit(int status)
67817c478bd9Sstevel@tonic-gate {
67827c478bd9Sstevel@tonic-gate 	if (DEVFSADM_DEBUG_ON) {
67837c478bd9Sstevel@tonic-gate 		vprint(INFO_MID, "exit status = %d\n", status);
67847c478bd9Sstevel@tonic-gate 	}
67857c478bd9Sstevel@tonic-gate 
67867c478bd9Sstevel@tonic-gate 	if (rcm_hdl) {
67877c478bd9Sstevel@tonic-gate 		if (thr_self() != process_rcm_events_tid) {
67887c478bd9Sstevel@tonic-gate 			(void) mutex_lock(&rcm_eventq_lock);
67897c478bd9Sstevel@tonic-gate 			need_to_exit_rcm_event_thread = 1;
67907c478bd9Sstevel@tonic-gate 			(void) cond_broadcast(&rcm_eventq_cv);
67917c478bd9Sstevel@tonic-gate 			(void) mutex_unlock(&rcm_eventq_lock);
67927c478bd9Sstevel@tonic-gate 
67937c478bd9Sstevel@tonic-gate 			/* wait until process_rcm_events() thread exits */
67947c478bd9Sstevel@tonic-gate 			(void) thr_join(process_rcm_events_tid, NULL, NULL);
67957c478bd9Sstevel@tonic-gate 		}
67967c478bd9Sstevel@tonic-gate 		librcm_free_handle(rcm_hdl);
67977c478bd9Sstevel@tonic-gate 		(void) dlclose(librcm_hdl);
67987c478bd9Sstevel@tonic-gate 	}
67997c478bd9Sstevel@tonic-gate 
68007c478bd9Sstevel@tonic-gate 	zlist_deleteall_unlocked();		/* dispose of all zones */
68017c478bd9Sstevel@tonic-gate 
68027c478bd9Sstevel@tonic-gate 	exit_dev_lock();
68037c478bd9Sstevel@tonic-gate 	exit_daemon_lock();
68047c478bd9Sstevel@tonic-gate 
68057c478bd9Sstevel@tonic-gate 	if (logflag == TRUE) {
68067c478bd9Sstevel@tonic-gate 		closelog();
68077c478bd9Sstevel@tonic-gate 	}
68087c478bd9Sstevel@tonic-gate 
68097c478bd9Sstevel@tonic-gate 	exit(status);
68107c478bd9Sstevel@tonic-gate }
68117c478bd9Sstevel@tonic-gate 
68127c478bd9Sstevel@tonic-gate /*
68137c478bd9Sstevel@tonic-gate  * set root_dir, devices_dir, dev_dir using optarg.  zone_mode determines
68147c478bd9Sstevel@tonic-gate  * whether we're operating on behalf of a zone; in this case, we need to
68157c478bd9Sstevel@tonic-gate  * reference some things from the global zone.  Note that zone mode and
68167c478bd9Sstevel@tonic-gate  * -R don't get along, but that should be OK since zone mode is not
68177c478bd9Sstevel@tonic-gate  * a public interface.
68187c478bd9Sstevel@tonic-gate  */
68197c478bd9Sstevel@tonic-gate static void
68207c478bd9Sstevel@tonic-gate set_root_devices_dev_dir(char *dir, int zone_mode)
68217c478bd9Sstevel@tonic-gate {
68227c478bd9Sstevel@tonic-gate 	size_t len;
68237c478bd9Sstevel@tonic-gate 
68247c478bd9Sstevel@tonic-gate 	root_dir = s_strdup(dir);
68257c478bd9Sstevel@tonic-gate 	len = strlen(dir) + strlen(DEVICES) + 1;
68267c478bd9Sstevel@tonic-gate 	devices_dir = s_malloc(len);
68277c478bd9Sstevel@tonic-gate 	(void) snprintf(devices_dir, len, "%s%s", root_dir, DEVICES);
68287c478bd9Sstevel@tonic-gate 	len = strlen(root_dir) + strlen(DEV) + 1;
68297c478bd9Sstevel@tonic-gate 	dev_dir = s_malloc(len);
68307c478bd9Sstevel@tonic-gate 	(void) snprintf(dev_dir, len, "%s%s", root_dir, DEV);
68317c478bd9Sstevel@tonic-gate 	if (zone_mode) {
68327c478bd9Sstevel@tonic-gate 		len = strlen(DEV) + 1;
68337c478bd9Sstevel@tonic-gate 		global_dev_dir = s_malloc(len);
68347c478bd9Sstevel@tonic-gate 		(void) snprintf(global_dev_dir, len, "%s", DEV);
68357c478bd9Sstevel@tonic-gate 	} else {
68367c478bd9Sstevel@tonic-gate 		global_dev_dir = s_malloc(len);
68377c478bd9Sstevel@tonic-gate 		(void) snprintf(global_dev_dir, len, "%s%s", root_dir, DEV);
68387c478bd9Sstevel@tonic-gate 	}
68397c478bd9Sstevel@tonic-gate }
68407c478bd9Sstevel@tonic-gate 
68417c478bd9Sstevel@tonic-gate /*
68427c478bd9Sstevel@tonic-gate  * Removes quotes.
68437c478bd9Sstevel@tonic-gate  */
68447c478bd9Sstevel@tonic-gate static char *
68457c478bd9Sstevel@tonic-gate dequote(char *src)
68467c478bd9Sstevel@tonic-gate {
68477c478bd9Sstevel@tonic-gate 	char	*dst;
68487c478bd9Sstevel@tonic-gate 	int	len;
68497c478bd9Sstevel@tonic-gate 
68507c478bd9Sstevel@tonic-gate 	len = strlen(src);
68517c478bd9Sstevel@tonic-gate 	dst = s_malloc(len + 1);
68527c478bd9Sstevel@tonic-gate 	if (src[0] == '\"' && src[len - 1] == '\"') {
68537c478bd9Sstevel@tonic-gate 		len -= 2;
68547c478bd9Sstevel@tonic-gate 		(void) strncpy(dst, &src[1], len);
68557c478bd9Sstevel@tonic-gate 		dst[len] = '\0';
68567c478bd9Sstevel@tonic-gate 	} else {
68577c478bd9Sstevel@tonic-gate 		(void) strcpy(dst, src);
68587c478bd9Sstevel@tonic-gate 	}
68597c478bd9Sstevel@tonic-gate 	return (dst);
68607c478bd9Sstevel@tonic-gate }
68617c478bd9Sstevel@tonic-gate 
68627c478bd9Sstevel@tonic-gate /*
68637c478bd9Sstevel@tonic-gate  * For a given physical device pathname and spectype, return the
68647c478bd9Sstevel@tonic-gate  * ownership and permissions attributes by looking in data from
68657c478bd9Sstevel@tonic-gate  * /etc/minor_perm.  If currently in installation mode, check for
68667c478bd9Sstevel@tonic-gate  * possible major number translations from the miniroot to the installed
68677c478bd9Sstevel@tonic-gate  * root's name_to_major table. Note that there can be multiple matches,
68687c478bd9Sstevel@tonic-gate  * but the last match takes effect.  pts seems to rely on this
68697c478bd9Sstevel@tonic-gate  * implementation behavior.
68707c478bd9Sstevel@tonic-gate  */
68717c478bd9Sstevel@tonic-gate static void
68727c478bd9Sstevel@tonic-gate getattr(char *phy_path, char *aminor, int spectype, dev_t dev, mode_t *mode,
68737c478bd9Sstevel@tonic-gate 	uid_t *uid, gid_t *gid)
68747c478bd9Sstevel@tonic-gate {
68757c478bd9Sstevel@tonic-gate 	char devname[PATH_MAX + 1];
68767c478bd9Sstevel@tonic-gate 	char *node_name;
68777c478bd9Sstevel@tonic-gate 	char *minor_name;
68787c478bd9Sstevel@tonic-gate 	int match = FALSE;
68797c478bd9Sstevel@tonic-gate 	int is_clone;
68807c478bd9Sstevel@tonic-gate 	int mp_drvname_matches_node_name;
68817c478bd9Sstevel@tonic-gate 	int mp_drvname_matches_minor_name;
68827c478bd9Sstevel@tonic-gate 	int mp_drvname_is_clone;
68837c478bd9Sstevel@tonic-gate 	int mp_drvname_matches_drvname;
68847c478bd9Sstevel@tonic-gate 	struct mperm *mp;
68857c478bd9Sstevel@tonic-gate 	major_t major_no;
68867c478bd9Sstevel@tonic-gate 	char driver[PATH_MAX + 1];
68877c478bd9Sstevel@tonic-gate 
68887c478bd9Sstevel@tonic-gate 	/*
68897c478bd9Sstevel@tonic-gate 	 * Get the driver name based on the major number since the name
68907c478bd9Sstevel@tonic-gate 	 * in /devices may be generic.  Could be running with more major
68917c478bd9Sstevel@tonic-gate 	 * numbers than are in /etc/name_to_major, so get it from the kernel
68927c478bd9Sstevel@tonic-gate 	 */
68937c478bd9Sstevel@tonic-gate 	major_no = major(dev);
68947c478bd9Sstevel@tonic-gate 
68957c478bd9Sstevel@tonic-gate 	if (modctl(MODGETNAME, driver, sizeof (driver), &major_no) != 0) {
68967c478bd9Sstevel@tonic-gate 		/* return default values */
68977c478bd9Sstevel@tonic-gate 		goto use_defaults;
68987c478bd9Sstevel@tonic-gate 	}
68997c478bd9Sstevel@tonic-gate 
69007c478bd9Sstevel@tonic-gate 	(void) strcpy(devname, phy_path);
69017c478bd9Sstevel@tonic-gate 
69027c478bd9Sstevel@tonic-gate 	node_name = strrchr(devname, '/'); /* node name is the last */
69037c478bd9Sstevel@tonic-gate 					/* component */
69047c478bd9Sstevel@tonic-gate 	if (node_name == NULL) {
69057c478bd9Sstevel@tonic-gate 		err_print(NO_NODE, devname);
69067c478bd9Sstevel@tonic-gate 		goto use_defaults;
69077c478bd9Sstevel@tonic-gate 	}
69087c478bd9Sstevel@tonic-gate 
69097c478bd9Sstevel@tonic-gate 	minor_name = strchr(++node_name, '@'); /* see if it has address part */
69107c478bd9Sstevel@tonic-gate 
69117c478bd9Sstevel@tonic-gate 	if (minor_name != NULL) {
69127c478bd9Sstevel@tonic-gate 		*minor_name++ = '\0';
69137c478bd9Sstevel@tonic-gate 	} else {
69147c478bd9Sstevel@tonic-gate 		minor_name = node_name;
69157c478bd9Sstevel@tonic-gate 	}
69167c478bd9Sstevel@tonic-gate 
69177c478bd9Sstevel@tonic-gate 	minor_name = strchr(minor_name, ':'); /* look for minor name */
69187c478bd9Sstevel@tonic-gate 
69197c478bd9Sstevel@tonic-gate 	if (minor_name == NULL) {
69207c478bd9Sstevel@tonic-gate 		err_print(NO_MINOR, devname);
69217c478bd9Sstevel@tonic-gate 		goto use_defaults;
69227c478bd9Sstevel@tonic-gate 	}
69237c478bd9Sstevel@tonic-gate 	*minor_name++ = '\0';
69247c478bd9Sstevel@tonic-gate 
69257c478bd9Sstevel@tonic-gate 	/*
69267c478bd9Sstevel@tonic-gate 	 * mp->mp_drvname = device name from minor_perm
69277c478bd9Sstevel@tonic-gate 	 * mp->mp_minorname = minor part of device name from
69287c478bd9Sstevel@tonic-gate 	 * minor_perm
69297c478bd9Sstevel@tonic-gate 	 * drvname = name of driver for this device
69307c478bd9Sstevel@tonic-gate 	 */
69317c478bd9Sstevel@tonic-gate 
69327c478bd9Sstevel@tonic-gate 	is_clone = (strcmp(node_name, "clone") == 0 ? TRUE : FALSE);
69337c478bd9Sstevel@tonic-gate 	for (mp = minor_perms; mp != NULL; mp = mp->mp_next) {
69347c478bd9Sstevel@tonic-gate 		mp_drvname_matches_node_name =
69357c478bd9Sstevel@tonic-gate 			(strcmp(mp->mp_drvname, node_name) == 0 ? TRUE : FALSE);
69367c478bd9Sstevel@tonic-gate 		mp_drvname_matches_minor_name =
69377c478bd9Sstevel@tonic-gate 			(strcmp(mp->mp_drvname, minor_name) == 0  ? TRUE:FALSE);
69387c478bd9Sstevel@tonic-gate 		mp_drvname_is_clone =
69397c478bd9Sstevel@tonic-gate 			(strcmp(mp->mp_drvname, "clone") == 0  ? TRUE : FALSE);
69407c478bd9Sstevel@tonic-gate 		mp_drvname_matches_drvname =
69417c478bd9Sstevel@tonic-gate 			(strcmp(mp->mp_drvname, driver) == 0  ? TRUE : FALSE);
69427c478bd9Sstevel@tonic-gate 
69437c478bd9Sstevel@tonic-gate 		/*
69447c478bd9Sstevel@tonic-gate 		 * If one of the following cases is true, then we try to change
69457c478bd9Sstevel@tonic-gate 		 * the permissions if a "shell global pattern match" of
69467c478bd9Sstevel@tonic-gate 		 * mp_>mp_minorname matches minor_name.
69477c478bd9Sstevel@tonic-gate 		 *
69487c478bd9Sstevel@tonic-gate 		 * 1.  mp->mp_drvname matches driver.
69497c478bd9Sstevel@tonic-gate 		 *
69507c478bd9Sstevel@tonic-gate 		 * OR
69517c478bd9Sstevel@tonic-gate 		 *
69527c478bd9Sstevel@tonic-gate 		 * 2.  mp->mp_drvname matches node_name and this
69537c478bd9Sstevel@tonic-gate 		 *	name is an alias of the driver name
69547c478bd9Sstevel@tonic-gate 		 *
69557c478bd9Sstevel@tonic-gate 		 * OR
69567c478bd9Sstevel@tonic-gate 		 *
69577c478bd9Sstevel@tonic-gate 		 * 3.  /devices entry is the clone device and either
69587c478bd9Sstevel@tonic-gate 		 *	minor_perm entry is the clone device or matches
69597c478bd9Sstevel@tonic-gate 		 *	the minor part of the clone device.
69607c478bd9Sstevel@tonic-gate 		 */
69617c478bd9Sstevel@tonic-gate 
69627c478bd9Sstevel@tonic-gate 		if ((mp_drvname_matches_drvname == TRUE)||
69637c478bd9Sstevel@tonic-gate 		    ((mp_drvname_matches_node_name == TRUE) &&
69647c478bd9Sstevel@tonic-gate 		    (alias(driver, node_name) == TRUE)) ||
69657c478bd9Sstevel@tonic-gate 		    ((is_clone == TRUE) &&
69667c478bd9Sstevel@tonic-gate 		    ((mp_drvname_is_clone == TRUE) ||
69677c478bd9Sstevel@tonic-gate 		    (mp_drvname_matches_minor_name == TRUE)))) {
69687c478bd9Sstevel@tonic-gate 			/*
69697c478bd9Sstevel@tonic-gate 			 * Check that the minor part of the
69707c478bd9Sstevel@tonic-gate 			 * device name from the minor_perm
69717c478bd9Sstevel@tonic-gate 			 * entry matches and if so, set the
69727c478bd9Sstevel@tonic-gate 			 * permissions.
69737c478bd9Sstevel@tonic-gate 			 *
69747c478bd9Sstevel@tonic-gate 			 * Under real devfs, clone minor name is changed
69757c478bd9Sstevel@tonic-gate 			 * to match the driver name, but minor_perm may
69767c478bd9Sstevel@tonic-gate 			 * not match. We reconcile it here.
69777c478bd9Sstevel@tonic-gate 			 */
69787c478bd9Sstevel@tonic-gate 			if (aminor != NULL)
69797c478bd9Sstevel@tonic-gate 				minor_name = aminor;
69807c478bd9Sstevel@tonic-gate 
69817c478bd9Sstevel@tonic-gate 			if (gmatch(minor_name, mp->mp_minorname) != 0) {
69827c478bd9Sstevel@tonic-gate 				*uid = mp->mp_uid;
69837c478bd9Sstevel@tonic-gate 				*gid = mp->mp_gid;
69847c478bd9Sstevel@tonic-gate 				*mode = spectype | mp->mp_mode;
69857c478bd9Sstevel@tonic-gate 				match = TRUE;
69867c478bd9Sstevel@tonic-gate 			}
69877c478bd9Sstevel@tonic-gate 		}
69887c478bd9Sstevel@tonic-gate 	}
69897c478bd9Sstevel@tonic-gate 
69907c478bd9Sstevel@tonic-gate 	if (match == TRUE) {
69917c478bd9Sstevel@tonic-gate 		return;
69927c478bd9Sstevel@tonic-gate 	}
69937c478bd9Sstevel@tonic-gate 
69947c478bd9Sstevel@tonic-gate 	use_defaults:
69957c478bd9Sstevel@tonic-gate 	/* not found in minor_perm, so just use default values */
69967c478bd9Sstevel@tonic-gate 	*uid = root_uid;
69977c478bd9Sstevel@tonic-gate 	*gid = sys_gid;
69987c478bd9Sstevel@tonic-gate 	*mode = (spectype | 0600);
69997c478bd9Sstevel@tonic-gate }
70007c478bd9Sstevel@tonic-gate 
70017c478bd9Sstevel@tonic-gate /*
70027c478bd9Sstevel@tonic-gate  * Called by devfs_read_minor_perm() to report errors
70037c478bd9Sstevel@tonic-gate  * key is:
70047c478bd9Sstevel@tonic-gate  *	line number: ignoring line number error
70057c478bd9Sstevel@tonic-gate  *	errno: open/close errors
70067c478bd9Sstevel@tonic-gate  *	size: alloc errors
70077c478bd9Sstevel@tonic-gate  */
70087c478bd9Sstevel@tonic-gate static void
70097c478bd9Sstevel@tonic-gate minorperm_err_cb(minorperm_err_t mp_err, int key)
70107c478bd9Sstevel@tonic-gate {
70117c478bd9Sstevel@tonic-gate 	switch (mp_err) {
70127c478bd9Sstevel@tonic-gate 	case MP_FOPEN_ERR:
70137c478bd9Sstevel@tonic-gate 		err_print(FOPEN_FAILED, MINOR_PERM_FILE, strerror(key));
70147c478bd9Sstevel@tonic-gate 		break;
70157c478bd9Sstevel@tonic-gate 	case MP_FCLOSE_ERR:
70167c478bd9Sstevel@tonic-gate 		err_print(FCLOSE_FAILED, MINOR_PERM_FILE, strerror(key));
70177c478bd9Sstevel@tonic-gate 		break;
70187c478bd9Sstevel@tonic-gate 	case MP_IGNORING_LINE_ERR:
70197c478bd9Sstevel@tonic-gate 		err_print(IGNORING_LINE_IN, key, MINOR_PERM_FILE);
70207c478bd9Sstevel@tonic-gate 		break;
70217c478bd9Sstevel@tonic-gate 	case MP_ALLOC_ERR:
70227c478bd9Sstevel@tonic-gate 		err_print(MALLOC_FAILED, key);
70237c478bd9Sstevel@tonic-gate 		break;
70247c478bd9Sstevel@tonic-gate 	case MP_NVLIST_ERR:
70257c478bd9Sstevel@tonic-gate 		err_print(NVLIST_ERROR, MINOR_PERM_FILE, strerror(key));
70267c478bd9Sstevel@tonic-gate 		break;
70277c478bd9Sstevel@tonic-gate 	case MP_CANT_FIND_USER_ERR:
70287c478bd9Sstevel@tonic-gate 		err_print(CANT_FIND_USER, DEFAULT_DEV_USER);
70297c478bd9Sstevel@tonic-gate 		break;
70307c478bd9Sstevel@tonic-gate 	case MP_CANT_FIND_GROUP_ERR:
70317c478bd9Sstevel@tonic-gate 		err_print(CANT_FIND_GROUP, DEFAULT_DEV_GROUP);
70327c478bd9Sstevel@tonic-gate 		break;
70337c478bd9Sstevel@tonic-gate 	}
70347c478bd9Sstevel@tonic-gate }
70357c478bd9Sstevel@tonic-gate 
70367c478bd9Sstevel@tonic-gate static void
70377c478bd9Sstevel@tonic-gate read_minor_perm_file(void)
70387c478bd9Sstevel@tonic-gate {
70397c478bd9Sstevel@tonic-gate 	static int cached = FALSE;
70407c478bd9Sstevel@tonic-gate 	static struct stat cached_sb;
70417c478bd9Sstevel@tonic-gate 	struct stat current_sb;
70427c478bd9Sstevel@tonic-gate 
70437c478bd9Sstevel@tonic-gate 	(void) stat(MINOR_PERM_FILE, &current_sb);
70447c478bd9Sstevel@tonic-gate 
70457c478bd9Sstevel@tonic-gate 	/* If already cached, check to see if it is still valid */
70467c478bd9Sstevel@tonic-gate 	if (cached == TRUE) {
70477c478bd9Sstevel@tonic-gate 
70487c478bd9Sstevel@tonic-gate 		if (current_sb.st_mtime == cached_sb.st_mtime) {
70497c478bd9Sstevel@tonic-gate 			vprint(FILES_MID, "%s cache valid\n", MINOR_PERM_FILE);
70507c478bd9Sstevel@tonic-gate 			return;
70517c478bd9Sstevel@tonic-gate 		}
70527c478bd9Sstevel@tonic-gate 		devfs_free_minor_perm(minor_perms);
70537c478bd9Sstevel@tonic-gate 		minor_perms = NULL;
70547c478bd9Sstevel@tonic-gate 	} else {
70557c478bd9Sstevel@tonic-gate 		cached = TRUE;
70567c478bd9Sstevel@tonic-gate 	}
70577c478bd9Sstevel@tonic-gate 
70587c478bd9Sstevel@tonic-gate 	(void) stat(MINOR_PERM_FILE, &cached_sb);
70597c478bd9Sstevel@tonic-gate 
70607c478bd9Sstevel@tonic-gate 	vprint(FILES_MID, "loading binding file: %s\n", MINOR_PERM_FILE);
70617c478bd9Sstevel@tonic-gate 
70627c478bd9Sstevel@tonic-gate 	minor_perms = devfs_read_minor_perm(minorperm_err_cb);
70637c478bd9Sstevel@tonic-gate }
70647c478bd9Sstevel@tonic-gate 
70657c478bd9Sstevel@tonic-gate static void
70667c478bd9Sstevel@tonic-gate load_minor_perm_file(void)
70677c478bd9Sstevel@tonic-gate {
70687c478bd9Sstevel@tonic-gate 	read_minor_perm_file();
70697c478bd9Sstevel@tonic-gate 	if (devfs_load_minor_perm(minor_perms, minorperm_err_cb) != 0)
70707c478bd9Sstevel@tonic-gate 		err_print(gettext("minor_perm load failed\n"));
70717c478bd9Sstevel@tonic-gate }
70727c478bd9Sstevel@tonic-gate 
70737c478bd9Sstevel@tonic-gate static char *
70747c478bd9Sstevel@tonic-gate convert_to_re(char *dev)
70757c478bd9Sstevel@tonic-gate {
70767c478bd9Sstevel@tonic-gate 	char *p, *l, *out;
70777c478bd9Sstevel@tonic-gate 	int i;
70787c478bd9Sstevel@tonic-gate 
70797c478bd9Sstevel@tonic-gate 	out = s_malloc(PATH_MAX);
70807c478bd9Sstevel@tonic-gate 
70817c478bd9Sstevel@tonic-gate 	for (l = p = dev, i = 0; (*p != '\0') && (i < (PATH_MAX - 1));
70827c478bd9Sstevel@tonic-gate 	    ++p, i++) {
70837c478bd9Sstevel@tonic-gate 		if ((*p == '*') && ((l != p) && (*l == '/'))) {
70847c478bd9Sstevel@tonic-gate 			out[i++] = '.';
70857c478bd9Sstevel@tonic-gate 			out[i] = '+';
70867c478bd9Sstevel@tonic-gate 		} else {
70877c478bd9Sstevel@tonic-gate 			out[i] = *p;
70887c478bd9Sstevel@tonic-gate 		}
70897c478bd9Sstevel@tonic-gate 		l = p;
70907c478bd9Sstevel@tonic-gate 	}
70917c478bd9Sstevel@tonic-gate 	out[i] = '\0';
70927c478bd9Sstevel@tonic-gate 	p = (char *)s_malloc(strlen(out) + 1);
70937c478bd9Sstevel@tonic-gate 	(void) strlcpy(p, out, strlen(out) + 1);
70947c478bd9Sstevel@tonic-gate 	free(out);
70957c478bd9Sstevel@tonic-gate 
70967c478bd9Sstevel@tonic-gate 	vprint(FILES_MID, "converted %s -> %s\n", dev, p);
70977c478bd9Sstevel@tonic-gate 
70987c478bd9Sstevel@tonic-gate 	return (p);
70997c478bd9Sstevel@tonic-gate }
71007c478bd9Sstevel@tonic-gate 
71017c478bd9Sstevel@tonic-gate static void
71027c478bd9Sstevel@tonic-gate read_logindevperm_file(void)
71037c478bd9Sstevel@tonic-gate {
71047c478bd9Sstevel@tonic-gate 	static int cached = FALSE;
71057c478bd9Sstevel@tonic-gate 	static struct stat cached_sb;
71067c478bd9Sstevel@tonic-gate 	struct stat current_sb;
71077c478bd9Sstevel@tonic-gate 	struct login_dev *ldev;
71087c478bd9Sstevel@tonic-gate 	FILE *fp;
71097c478bd9Sstevel@tonic-gate 	char line[MAX_LDEV_LINE];
71107c478bd9Sstevel@tonic-gate 	int ln, perm, rv;
71117c478bd9Sstevel@tonic-gate 	char *cp, *console, *devlist, *dev;
71127c478bd9Sstevel@tonic-gate 	char *lasts, *devlasts, *permstr, *drv;
71137c478bd9Sstevel@tonic-gate 	struct driver_list *list, *next;
71147c478bd9Sstevel@tonic-gate 
71157c478bd9Sstevel@tonic-gate 	/* Read logindevperm only when enabled */
71167c478bd9Sstevel@tonic-gate 	if (login_dev_enable != TRUE)
71177c478bd9Sstevel@tonic-gate 		return;
71187c478bd9Sstevel@tonic-gate 
71197c478bd9Sstevel@tonic-gate 	if (cached == TRUE) {
71207c478bd9Sstevel@tonic-gate 		if (stat(LDEV_FILE, &current_sb) == 0 &&
71217c478bd9Sstevel@tonic-gate 		    current_sb.st_mtime == cached_sb.st_mtime) {
71227c478bd9Sstevel@tonic-gate 			vprint(FILES_MID, "%s cache valid\n", LDEV_FILE);
71237c478bd9Sstevel@tonic-gate 			return;
71247c478bd9Sstevel@tonic-gate 		}
71257c478bd9Sstevel@tonic-gate 		vprint(FILES_MID, "invalidating %s cache\n", LDEV_FILE);
71267c478bd9Sstevel@tonic-gate 		while (login_dev_cache != NULL) {
71277c478bd9Sstevel@tonic-gate 
71287c478bd9Sstevel@tonic-gate 			ldev = login_dev_cache;
71297c478bd9Sstevel@tonic-gate 			login_dev_cache = ldev->ldev_next;
71307c478bd9Sstevel@tonic-gate 			free(ldev->ldev_console);
71317c478bd9Sstevel@tonic-gate 			free(ldev->ldev_device);
71327c478bd9Sstevel@tonic-gate 			regfree(&ldev->ldev_device_regex);
71337c478bd9Sstevel@tonic-gate 			list = ldev->ldev_driver_list;
71347c478bd9Sstevel@tonic-gate 			while (list) {
71357c478bd9Sstevel@tonic-gate 				next = list->next;
71367c478bd9Sstevel@tonic-gate 				free(list);
71377c478bd9Sstevel@tonic-gate 				list = next;
71387c478bd9Sstevel@tonic-gate 			}
71397c478bd9Sstevel@tonic-gate 			free(ldev);
71407c478bd9Sstevel@tonic-gate 		}
71417c478bd9Sstevel@tonic-gate 	} else {
71427c478bd9Sstevel@tonic-gate 		cached = TRUE;
71437c478bd9Sstevel@tonic-gate 	}
71447c478bd9Sstevel@tonic-gate 
71457c478bd9Sstevel@tonic-gate 	assert(login_dev_cache == NULL);
71467c478bd9Sstevel@tonic-gate 
71477c478bd9Sstevel@tonic-gate 	if (stat(LDEV_FILE, &cached_sb) != 0) {
71487c478bd9Sstevel@tonic-gate 		cached = FALSE;
71497c478bd9Sstevel@tonic-gate 		return;
71507c478bd9Sstevel@tonic-gate 	}
71517c478bd9Sstevel@tonic-gate 
71527c478bd9Sstevel@tonic-gate 	vprint(FILES_MID, "loading file: %s\n", LDEV_FILE);
71537c478bd9Sstevel@tonic-gate 
71547c478bd9Sstevel@tonic-gate 	if ((fp = fopen(LDEV_FILE, "r")) == NULL) {
71557c478bd9Sstevel@tonic-gate 		/* Not fatal to devfsadm */
71567c478bd9Sstevel@tonic-gate 		cached = FALSE;
71577c478bd9Sstevel@tonic-gate 		err_print(FOPEN_FAILED, LDEV_FILE, strerror(errno));
71587c478bd9Sstevel@tonic-gate 		return;
71597c478bd9Sstevel@tonic-gate 	}
71607c478bd9Sstevel@tonic-gate 
71617c478bd9Sstevel@tonic-gate 	ln = 0;
71627c478bd9Sstevel@tonic-gate 	while (fgets(line, MAX_LDEV_LINE, fp) != NULL) {
71637c478bd9Sstevel@tonic-gate 		ln++;
71647c478bd9Sstevel@tonic-gate 
71657c478bd9Sstevel@tonic-gate 		/* Remove comments */
71667c478bd9Sstevel@tonic-gate 		if ((cp = strchr(line, '#')) != NULL)
71677c478bd9Sstevel@tonic-gate 			*cp = '\0';
71687c478bd9Sstevel@tonic-gate 
71697c478bd9Sstevel@tonic-gate 		if ((console = strtok_r(line, LDEV_DELIMS, &lasts)) == NULL)
71707c478bd9Sstevel@tonic-gate 			continue;	/* Blank line */
71717c478bd9Sstevel@tonic-gate 
71727c478bd9Sstevel@tonic-gate 		if ((permstr =  strtok_r(NULL, LDEV_DELIMS, &lasts)) == NULL) {
71737c478bd9Sstevel@tonic-gate 			err_print(IGNORING_LINE_IN, ln, LDEV_FILE);
71747c478bd9Sstevel@tonic-gate 			continue;	/* Malformed line */
71757c478bd9Sstevel@tonic-gate 		}
71767c478bd9Sstevel@tonic-gate 
71777c478bd9Sstevel@tonic-gate 		/*
71787c478bd9Sstevel@tonic-gate 		 * permstr is string in octal format. Convert to int
71797c478bd9Sstevel@tonic-gate 		 */
71807c478bd9Sstevel@tonic-gate 		cp = NULL;
71817c478bd9Sstevel@tonic-gate 		errno = 0;
71827c478bd9Sstevel@tonic-gate 		perm = strtol(permstr, &cp, 8);
71837c478bd9Sstevel@tonic-gate 		if (errno || perm < 0 || perm > 0777 || *cp != '\0') {
71847c478bd9Sstevel@tonic-gate 			err_print(IGNORING_LINE_IN, ln, LDEV_FILE);
71857c478bd9Sstevel@tonic-gate 			continue;
71867c478bd9Sstevel@tonic-gate 		}
71877c478bd9Sstevel@tonic-gate 
71887c478bd9Sstevel@tonic-gate 		if ((devlist = strtok_r(NULL, LDEV_DELIMS, &lasts)) == NULL) {
71897c478bd9Sstevel@tonic-gate 			err_print(IGNORING_LINE_IN, ln, LDEV_FILE);
71907c478bd9Sstevel@tonic-gate 			continue;
71917c478bd9Sstevel@tonic-gate 		}
71927c478bd9Sstevel@tonic-gate 
71937c478bd9Sstevel@tonic-gate 		dev = strtok_r(devlist, LDEV_DEV_DELIM, &devlasts);
71947c478bd9Sstevel@tonic-gate 		while (dev) {
71957c478bd9Sstevel@tonic-gate 
71967c478bd9Sstevel@tonic-gate 			ldev = (struct login_dev *)s_zalloc(
71977c478bd9Sstevel@tonic-gate 			    sizeof (struct login_dev));
71987c478bd9Sstevel@tonic-gate 			ldev->ldev_console = s_strdup(console);
71997c478bd9Sstevel@tonic-gate 			ldev->ldev_perms = perm;
72007c478bd9Sstevel@tonic-gate 
72017c478bd9Sstevel@tonic-gate 			/*
72027c478bd9Sstevel@tonic-gate 			 * the logical device name may contain '*' which
72037c478bd9Sstevel@tonic-gate 			 * we convert to a regular expression
72047c478bd9Sstevel@tonic-gate 			 */
72057c478bd9Sstevel@tonic-gate 			ldev->ldev_device = convert_to_re(dev);
72067c478bd9Sstevel@tonic-gate 			if (ldev->ldev_device &&
72077c478bd9Sstevel@tonic-gate 			    (rv = regcomp(&ldev->ldev_device_regex,
72087c478bd9Sstevel@tonic-gate 			    ldev->ldev_device, REG_EXTENDED))) {
72097c478bd9Sstevel@tonic-gate 				bzero(&ldev->ldev_device_regex,
72107c478bd9Sstevel@tonic-gate 				    sizeof (ldev->ldev_device_regex));
72117c478bd9Sstevel@tonic-gate 				err_print(REGCOMP_FAILED,
72127c478bd9Sstevel@tonic-gate 				    ldev->ldev_device, rv);
72137c478bd9Sstevel@tonic-gate 			}
72147c478bd9Sstevel@tonic-gate 			ldev->ldev_next = login_dev_cache;
72157c478bd9Sstevel@tonic-gate 			login_dev_cache = ldev;
72167c478bd9Sstevel@tonic-gate 			dev = strtok_r(NULL, LDEV_DEV_DELIM, &devlasts);
72177c478bd9Sstevel@tonic-gate 		}
72187c478bd9Sstevel@tonic-gate 
72197c478bd9Sstevel@tonic-gate 		drv = strtok_r(NULL, LDEV_DRVLIST_DELIMS, &lasts);
72207c478bd9Sstevel@tonic-gate 		if (drv) {
72217c478bd9Sstevel@tonic-gate 			if (strcmp(drv, LDEV_DRVLIST_NAME) == 0) {
72227c478bd9Sstevel@tonic-gate 
72237c478bd9Sstevel@tonic-gate 				drv = strtok_r(NULL, LDEV_DRV_DELIMS,
72247c478bd9Sstevel@tonic-gate 					&lasts);
72257c478bd9Sstevel@tonic-gate 
72267c478bd9Sstevel@tonic-gate 				while (drv) {
72277c478bd9Sstevel@tonic-gate 					vprint(FILES_MID,
72287c478bd9Sstevel@tonic-gate 					    "logindevperm driver=%s\n",
72297c478bd9Sstevel@tonic-gate 					    drv);
72307c478bd9Sstevel@tonic-gate 
72317c478bd9Sstevel@tonic-gate 					/*
72327c478bd9Sstevel@tonic-gate 					 * create a linked list of driver
72337c478bd9Sstevel@tonic-gate 					 * names
72347c478bd9Sstevel@tonic-gate 					 */
72357c478bd9Sstevel@tonic-gate 					list = (struct driver_list *)
72367c478bd9Sstevel@tonic-gate 					    s_zalloc(
72377c478bd9Sstevel@tonic-gate 					    sizeof (struct driver_list));
72387c478bd9Sstevel@tonic-gate 					(void) strlcpy(list->driver_name, drv,
72397c478bd9Sstevel@tonic-gate 					    sizeof (list->driver_name));
72407c478bd9Sstevel@tonic-gate 					list->next = ldev->ldev_driver_list;
72417c478bd9Sstevel@tonic-gate 					ldev->ldev_driver_list = list;
72427c478bd9Sstevel@tonic-gate 					drv = strtok_r(NULL, LDEV_DRV_DELIMS,
72437c478bd9Sstevel@tonic-gate 					    &lasts);
72447c478bd9Sstevel@tonic-gate 				}
72457c478bd9Sstevel@tonic-gate 			}
72467c478bd9Sstevel@tonic-gate 		}
72477c478bd9Sstevel@tonic-gate 	}
72487c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
72497c478bd9Sstevel@tonic-gate }
72507c478bd9Sstevel@tonic-gate 
72517c478bd9Sstevel@tonic-gate /*
72527c478bd9Sstevel@tonic-gate  * Tokens are separated by ' ', '\t', ':', '=', '&', '|', ';', '\n', or '\0'
72537c478bd9Sstevel@tonic-gate  *
72547c478bd9Sstevel@tonic-gate  * Returns DEVFSADM_SUCCESS if token found, DEVFSADM_FAILURE otherwise.
72557c478bd9Sstevel@tonic-gate  */
72567c478bd9Sstevel@tonic-gate static int
72577c478bd9Sstevel@tonic-gate getnexttoken(char *next, char **nextp, char **tokenpp, char *tchar)
72587c478bd9Sstevel@tonic-gate {
72597c478bd9Sstevel@tonic-gate 	char *cp;
72607c478bd9Sstevel@tonic-gate 	char *cp1;
72617c478bd9Sstevel@tonic-gate 	char *tokenp;
72627c478bd9Sstevel@tonic-gate 
72637c478bd9Sstevel@tonic-gate 	cp = next;
72647c478bd9Sstevel@tonic-gate 	while (*cp == ' ' || *cp == '\t') {
72657c478bd9Sstevel@tonic-gate 		cp++;			/* skip leading spaces */
72667c478bd9Sstevel@tonic-gate 	}
72677c478bd9Sstevel@tonic-gate 	tokenp = cp;			/* start of token */
72687c478bd9Sstevel@tonic-gate 	while (*cp != '\0' && *cp != '\n' && *cp != ' ' && *cp != '\t' &&
72697c478bd9Sstevel@tonic-gate 		*cp != ':' && *cp != '=' && *cp != '&' &&
72707c478bd9Sstevel@tonic-gate 		*cp != '|' && *cp != ';') {
72717c478bd9Sstevel@tonic-gate 		cp++;			/* point to next character */
72727c478bd9Sstevel@tonic-gate 	}
72737c478bd9Sstevel@tonic-gate 	/*
72747c478bd9Sstevel@tonic-gate 	 * If terminating character is a space or tab, look ahead to see if
72757c478bd9Sstevel@tonic-gate 	 * there's another terminator that's not a space or a tab.
72767c478bd9Sstevel@tonic-gate 	 * (This code handles trailing spaces.)
72777c478bd9Sstevel@tonic-gate 	 */
72787c478bd9Sstevel@tonic-gate 	if (*cp == ' ' || *cp == '\t') {
72797c478bd9Sstevel@tonic-gate 		cp1 = cp;
72807c478bd9Sstevel@tonic-gate 		while (*++cp1 == ' ' || *cp1 == '\t')
72817c478bd9Sstevel@tonic-gate 			;
72827c478bd9Sstevel@tonic-gate 		if (*cp1 == '=' || *cp1 == ':' || *cp1 == '&' || *cp1 == '|' ||
72837c478bd9Sstevel@tonic-gate 			*cp1 == ';' || *cp1 == '\n' || *cp1 == '\0') {
72847c478bd9Sstevel@tonic-gate 			*cp = NULL;	/* terminate token */
72857c478bd9Sstevel@tonic-gate 			cp = cp1;
72867c478bd9Sstevel@tonic-gate 		}
72877c478bd9Sstevel@tonic-gate 	}
72887c478bd9Sstevel@tonic-gate 	if (tchar != NULL) {
72897c478bd9Sstevel@tonic-gate 		*tchar = *cp;		/* save terminating character */
72907c478bd9Sstevel@tonic-gate 		if (*tchar == '\0') {
72917c478bd9Sstevel@tonic-gate 			*tchar = '\n';
72927c478bd9Sstevel@tonic-gate 		}
72937c478bd9Sstevel@tonic-gate 	}
72947c478bd9Sstevel@tonic-gate 	*cp++ = '\0';			/* terminate token, point to next */
72957c478bd9Sstevel@tonic-gate 	*nextp = cp;			/* set pointer to next character */
72967c478bd9Sstevel@tonic-gate 	if (cp - tokenp - 1 == 0) {
72977c478bd9Sstevel@tonic-gate 		return (DEVFSADM_FAILURE);
72987c478bd9Sstevel@tonic-gate 	}
72997c478bd9Sstevel@tonic-gate 	*tokenpp = tokenp;
73007c478bd9Sstevel@tonic-gate 	return (DEVFSADM_SUCCESS);
73017c478bd9Sstevel@tonic-gate }
73027c478bd9Sstevel@tonic-gate 
73037c478bd9Sstevel@tonic-gate /*
73047c478bd9Sstevel@tonic-gate  * read or reread the driver aliases file
73057c478bd9Sstevel@tonic-gate  */
73067c478bd9Sstevel@tonic-gate static void
73077c478bd9Sstevel@tonic-gate read_driver_aliases_file(void)
73087c478bd9Sstevel@tonic-gate {
73097c478bd9Sstevel@tonic-gate 
73107c478bd9Sstevel@tonic-gate 	driver_alias_t *save;
73117c478bd9Sstevel@tonic-gate 	driver_alias_t *lst_tail;
73127c478bd9Sstevel@tonic-gate 	driver_alias_t *ap;
73137c478bd9Sstevel@tonic-gate 	static int cached = FALSE;
73147c478bd9Sstevel@tonic-gate 	FILE *afd;
73157c478bd9Sstevel@tonic-gate 	char line[256];
73167c478bd9Sstevel@tonic-gate 	char *cp;
73177c478bd9Sstevel@tonic-gate 	char *p;
73187c478bd9Sstevel@tonic-gate 	char t;
73197c478bd9Sstevel@tonic-gate 	int ln = 0;
73207c478bd9Sstevel@tonic-gate 	static struct stat cached_sb;
73217c478bd9Sstevel@tonic-gate 	struct stat current_sb;
73227c478bd9Sstevel@tonic-gate 
73237c478bd9Sstevel@tonic-gate 	(void) stat(ALIASFILE, &current_sb);
73247c478bd9Sstevel@tonic-gate 
73257c478bd9Sstevel@tonic-gate 	/* If already cached, check to see if it is still valid */
73267c478bd9Sstevel@tonic-gate 	if (cached == TRUE) {
73277c478bd9Sstevel@tonic-gate 
73287c478bd9Sstevel@tonic-gate 		if (current_sb.st_mtime == cached_sb.st_mtime) {
73297c478bd9Sstevel@tonic-gate 			vprint(FILES_MID, "%s cache valid\n", ALIASFILE);
73307c478bd9Sstevel@tonic-gate 			return;
73317c478bd9Sstevel@tonic-gate 		}
73327c478bd9Sstevel@tonic-gate 
73337c478bd9Sstevel@tonic-gate 		vprint(FILES_MID, "invalidating %s cache\n", ALIASFILE);
73347c478bd9Sstevel@tonic-gate 		while (driver_aliases != NULL) {
73357c478bd9Sstevel@tonic-gate 			free(driver_aliases->alias_name);
73367c478bd9Sstevel@tonic-gate 			free(driver_aliases->driver_name);
73377c478bd9Sstevel@tonic-gate 			save = driver_aliases;
73387c478bd9Sstevel@tonic-gate 			driver_aliases = driver_aliases->next;
73397c478bd9Sstevel@tonic-gate 			free(save);
73407c478bd9Sstevel@tonic-gate 		}
73417c478bd9Sstevel@tonic-gate 	} else {
73427c478bd9Sstevel@tonic-gate 		cached = TRUE;
73437c478bd9Sstevel@tonic-gate 	}
73447c478bd9Sstevel@tonic-gate 
73457c478bd9Sstevel@tonic-gate 	(void) stat(ALIASFILE, &cached_sb);
73467c478bd9Sstevel@tonic-gate 
73477c478bd9Sstevel@tonic-gate 	vprint(FILES_MID, "loading binding file: %s\n", ALIASFILE);
73487c478bd9Sstevel@tonic-gate 
73497c478bd9Sstevel@tonic-gate 	if ((afd = fopen(ALIASFILE, "r")) == NULL) {
73507c478bd9Sstevel@tonic-gate 		err_print(FOPEN_FAILED, ALIASFILE, strerror(errno));
73517c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
73527c478bd9Sstevel@tonic-gate 	}
73537c478bd9Sstevel@tonic-gate 
73547c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line) - 1, afd) != NULL) {
73557c478bd9Sstevel@tonic-gate 		ln++;
73567c478bd9Sstevel@tonic-gate 		cp = line;
73577c478bd9Sstevel@tonic-gate 		if (getnexttoken(cp, &cp, &p, &t) == DEVFSADM_FAILURE) {
73587c478bd9Sstevel@tonic-gate 			err_print(IGNORING_LINE_IN, ln, ALIASFILE);
73597c478bd9Sstevel@tonic-gate 			continue;
73607c478bd9Sstevel@tonic-gate 		}
73617c478bd9Sstevel@tonic-gate 		if (t == '\n' || t == '\0') {
73627c478bd9Sstevel@tonic-gate 			err_print(DRV_BUT_NO_ALIAS, ln, ALIASFILE);
73637c478bd9Sstevel@tonic-gate 			continue;
73647c478bd9Sstevel@tonic-gate 		}
73657c478bd9Sstevel@tonic-gate 		ap = (struct driver_alias *)
73667c478bd9Sstevel@tonic-gate 				s_zalloc(sizeof (struct driver_alias));
73677c478bd9Sstevel@tonic-gate 		ap->driver_name = s_strdup(p);
73687c478bd9Sstevel@tonic-gate 		if (getnexttoken(cp, &cp, &p, &t) == DEVFSADM_FAILURE) {
73697c478bd9Sstevel@tonic-gate 			err_print(DRV_BUT_NO_ALIAS, ln, ALIASFILE);
73707c478bd9Sstevel@tonic-gate 			free(ap->driver_name);
73717c478bd9Sstevel@tonic-gate 			free(ap);
73727c478bd9Sstevel@tonic-gate 			continue;
73737c478bd9Sstevel@tonic-gate 		}
73747c478bd9Sstevel@tonic-gate 		if (*p == '"') {
73757c478bd9Sstevel@tonic-gate 			if (p[strlen(p) - 1] == '"') {
73767c478bd9Sstevel@tonic-gate 				p[strlen(p) - 1] = '\0';
73777c478bd9Sstevel@tonic-gate 				p++;
73787c478bd9Sstevel@tonic-gate 			}
73797c478bd9Sstevel@tonic-gate 		}
73807c478bd9Sstevel@tonic-gate 		ap->alias_name = s_strdup(p);
73817c478bd9Sstevel@tonic-gate 		if (driver_aliases == NULL) {
73827c478bd9Sstevel@tonic-gate 			driver_aliases = ap;
73837c478bd9Sstevel@tonic-gate 			lst_tail = ap;
73847c478bd9Sstevel@tonic-gate 		} else {
73857c478bd9Sstevel@tonic-gate 			lst_tail->next = ap;
73867c478bd9Sstevel@tonic-gate 			lst_tail = ap;
73877c478bd9Sstevel@tonic-gate 		}
73887c478bd9Sstevel@tonic-gate 	}
73897c478bd9Sstevel@tonic-gate 	if (fclose(afd) == EOF) {
73907c478bd9Sstevel@tonic-gate 		err_print(FCLOSE_FAILED, ALIASFILE, strerror(errno));
73917c478bd9Sstevel@tonic-gate 	}
73927c478bd9Sstevel@tonic-gate }
73937c478bd9Sstevel@tonic-gate 
73947c478bd9Sstevel@tonic-gate /*
73957c478bd9Sstevel@tonic-gate  * return TRUE if alias_name is an alias for driver_name, otherwise
73967c478bd9Sstevel@tonic-gate  * return FALSE.
73977c478bd9Sstevel@tonic-gate  */
73987c478bd9Sstevel@tonic-gate static int
73997c478bd9Sstevel@tonic-gate alias(char *driver_name, char *alias_name)
74007c478bd9Sstevel@tonic-gate {
74017c478bd9Sstevel@tonic-gate 	driver_alias_t *alias;
74027c478bd9Sstevel@tonic-gate 
74037c478bd9Sstevel@tonic-gate 	/*
74047c478bd9Sstevel@tonic-gate 	 * check for a match
74057c478bd9Sstevel@tonic-gate 	 */
74067c478bd9Sstevel@tonic-gate 	for (alias = driver_aliases; alias != NULL; alias = alias->next) {
74077c478bd9Sstevel@tonic-gate 		if ((strcmp(alias->driver_name, driver_name) == 0) &&
74087c478bd9Sstevel@tonic-gate 		    (strcmp(alias->alias_name, alias_name) == 0)) {
74097c478bd9Sstevel@tonic-gate 			return (TRUE);
74107c478bd9Sstevel@tonic-gate 		}
74117c478bd9Sstevel@tonic-gate 	}
74127c478bd9Sstevel@tonic-gate 	return (FALSE);
74137c478bd9Sstevel@tonic-gate }
74147c478bd9Sstevel@tonic-gate 
74157c478bd9Sstevel@tonic-gate /*
74167c478bd9Sstevel@tonic-gate  * convenience functions
74177c478bd9Sstevel@tonic-gate  */
74187c478bd9Sstevel@tonic-gate static void *
74197c478bd9Sstevel@tonic-gate s_malloc(const size_t size)
74207c478bd9Sstevel@tonic-gate {
74217c478bd9Sstevel@tonic-gate 	void *rp;
74227c478bd9Sstevel@tonic-gate 
74237c478bd9Sstevel@tonic-gate 	rp = malloc(size);
74247c478bd9Sstevel@tonic-gate 	if (rp == NULL) {
74257c478bd9Sstevel@tonic-gate 		err_print(MALLOC_FAILED, size);
74267c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
74277c478bd9Sstevel@tonic-gate 	}
74287c478bd9Sstevel@tonic-gate 	return (rp);
74297c478bd9Sstevel@tonic-gate }
74307c478bd9Sstevel@tonic-gate 
74317c478bd9Sstevel@tonic-gate /*
74327c478bd9Sstevel@tonic-gate  * convenience functions
74337c478bd9Sstevel@tonic-gate  */
74347c478bd9Sstevel@tonic-gate static void *
74357c478bd9Sstevel@tonic-gate s_realloc(void *ptr, const size_t size)
74367c478bd9Sstevel@tonic-gate {
74377c478bd9Sstevel@tonic-gate 	ptr = realloc(ptr, size);
74387c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
74397c478bd9Sstevel@tonic-gate 		err_print(REALLOC_FAILED, size);
74407c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
74417c478bd9Sstevel@tonic-gate 	}
74427c478bd9Sstevel@tonic-gate 	return (ptr);
74437c478bd9Sstevel@tonic-gate }
74447c478bd9Sstevel@tonic-gate 
74457c478bd9Sstevel@tonic-gate static void *
74467c478bd9Sstevel@tonic-gate s_zalloc(const size_t size)
74477c478bd9Sstevel@tonic-gate {
74487c478bd9Sstevel@tonic-gate 	void *rp;
74497c478bd9Sstevel@tonic-gate 
74507c478bd9Sstevel@tonic-gate 	rp = calloc(1, size);
74517c478bd9Sstevel@tonic-gate 	if (rp == NULL) {
74527c478bd9Sstevel@tonic-gate 		err_print(CALLOC_FAILED, size);
74537c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
74547c478bd9Sstevel@tonic-gate 	}
74557c478bd9Sstevel@tonic-gate 	return (rp);
74567c478bd9Sstevel@tonic-gate }
74577c478bd9Sstevel@tonic-gate 
74587c478bd9Sstevel@tonic-gate char *
74597c478bd9Sstevel@tonic-gate s_strdup(const char *ptr)
74607c478bd9Sstevel@tonic-gate {
74617c478bd9Sstevel@tonic-gate 	void *rp;
74627c478bd9Sstevel@tonic-gate 
74637c478bd9Sstevel@tonic-gate 	rp = strdup(ptr);
74647c478bd9Sstevel@tonic-gate 	if (rp == NULL) {
74657c478bd9Sstevel@tonic-gate 		err_print(STRDUP_FAILED, ptr);
74667c478bd9Sstevel@tonic-gate 		devfsadm_exit(1);
74677c478bd9Sstevel@tonic-gate 	}
74687c478bd9Sstevel@tonic-gate 	return (rp);
74697c478bd9Sstevel@tonic-gate }
74707c478bd9Sstevel@tonic-gate 
74717c478bd9Sstevel@tonic-gate static void
74727c478bd9Sstevel@tonic-gate s_closedir(DIR *dirp)
74737c478bd9Sstevel@tonic-gate {
74747c478bd9Sstevel@tonic-gate retry:
74757c478bd9Sstevel@tonic-gate 	if (closedir(dirp) != 0) {
74767c478bd9Sstevel@tonic-gate 		if (errno == EINTR)
74777c478bd9Sstevel@tonic-gate 			goto retry;
74787c478bd9Sstevel@tonic-gate 		err_print(CLOSEDIR_FAILED, strerror(errno));
74797c478bd9Sstevel@tonic-gate 	}
74807c478bd9Sstevel@tonic-gate }
74817c478bd9Sstevel@tonic-gate 
74827c478bd9Sstevel@tonic-gate static void
74837c478bd9Sstevel@tonic-gate s_mkdirp(const char *path, const mode_t mode)
74847c478bd9Sstevel@tonic-gate {
74857c478bd9Sstevel@tonic-gate 	vprint(CHATTY_MID, "mkdirp(%s, 0x%lx)\n", path, mode);
74867c478bd9Sstevel@tonic-gate 	if (mkdirp(path, mode) == -1) {
74877c478bd9Sstevel@tonic-gate 		if (errno != EEXIST) {
74887c478bd9Sstevel@tonic-gate 			err_print(MKDIR_FAILED, path, mode, strerror(errno));
74897c478bd9Sstevel@tonic-gate 		}
74907c478bd9Sstevel@tonic-gate 	}
74917c478bd9Sstevel@tonic-gate }
74927c478bd9Sstevel@tonic-gate 
74937c478bd9Sstevel@tonic-gate static void
74947c478bd9Sstevel@tonic-gate s_unlink(const char *file)
74957c478bd9Sstevel@tonic-gate {
74967c478bd9Sstevel@tonic-gate retry:
74977c478bd9Sstevel@tonic-gate 	if (unlink(file) == -1) {
74987c478bd9Sstevel@tonic-gate 		if (errno == EINTR || errno == EAGAIN)
74997c478bd9Sstevel@tonic-gate 			goto retry;
75007c478bd9Sstevel@tonic-gate 		if (errno != ENOENT) {
75017c478bd9Sstevel@tonic-gate 			err_print(UNLINK_FAILED, file, strerror(errno));
75027c478bd9Sstevel@tonic-gate 		}
75037c478bd9Sstevel@tonic-gate 	}
75047c478bd9Sstevel@tonic-gate }
75057c478bd9Sstevel@tonic-gate 
75067c478bd9Sstevel@tonic-gate static void
75077c478bd9Sstevel@tonic-gate add_verbose_id(char *mid)
75087c478bd9Sstevel@tonic-gate {
75097c478bd9Sstevel@tonic-gate 	num_verbose++;
75107c478bd9Sstevel@tonic-gate 	verbose = s_realloc(verbose, num_verbose * sizeof (char *));
75117c478bd9Sstevel@tonic-gate 	verbose[num_verbose - 1] = mid;
75127c478bd9Sstevel@tonic-gate }
75137c478bd9Sstevel@tonic-gate 
75147c478bd9Sstevel@tonic-gate /*
75157c478bd9Sstevel@tonic-gate  * returns DEVFSADM_TRUE if contents is a minor node in /devices.
75167c478bd9Sstevel@tonic-gate  * If mn_root is not NULL, mn_root is set to:
75177c478bd9Sstevel@tonic-gate  *	if contents is a /dev node, mn_root = contents
75187c478bd9Sstevel@tonic-gate  * 			OR
75197c478bd9Sstevel@tonic-gate  *	if contents is a /devices node, mn_root set to the '/'
75207c478bd9Sstevel@tonic-gate  *	following /devices.
75217c478bd9Sstevel@tonic-gate  */
75227c478bd9Sstevel@tonic-gate static int
75237c478bd9Sstevel@tonic-gate is_minor_node(char *contents, char **mn_root)
75247c478bd9Sstevel@tonic-gate {
75257c478bd9Sstevel@tonic-gate 	char *ptr;
75267c478bd9Sstevel@tonic-gate 	char device_prefix[100];
75277c478bd9Sstevel@tonic-gate 
75287c478bd9Sstevel@tonic-gate 	(void) snprintf(device_prefix, sizeof (device_prefix), "../devices/");
75297c478bd9Sstevel@tonic-gate 
75307c478bd9Sstevel@tonic-gate 	if ((ptr = strstr(contents, device_prefix)) != NULL) {
75317c478bd9Sstevel@tonic-gate 		if (mn_root != NULL) {
75327c478bd9Sstevel@tonic-gate 			/* mn_root should point to the / following /devices */
75337c478bd9Sstevel@tonic-gate 			*mn_root = ptr += strlen(device_prefix) - 1;
75347c478bd9Sstevel@tonic-gate 		}
75357c478bd9Sstevel@tonic-gate 		return (DEVFSADM_TRUE);
75367c478bd9Sstevel@tonic-gate 	}
75377c478bd9Sstevel@tonic-gate 
75387c478bd9Sstevel@tonic-gate 	(void) snprintf(device_prefix, sizeof (device_prefix), "/devices/");
75397c478bd9Sstevel@tonic-gate 
75407c478bd9Sstevel@tonic-gate 	if (strncmp(contents, device_prefix, strlen(device_prefix)) == 0) {
75417c478bd9Sstevel@tonic-gate 		if (mn_root != NULL) {
75427c478bd9Sstevel@tonic-gate 			/* mn_root should point to the / following /devices */
75437c478bd9Sstevel@tonic-gate 			*mn_root = contents + strlen(device_prefix) - 1;
75447c478bd9Sstevel@tonic-gate 		}
75457c478bd9Sstevel@tonic-gate 		return (DEVFSADM_TRUE);
75467c478bd9Sstevel@tonic-gate 	}
75477c478bd9Sstevel@tonic-gate 
75487c478bd9Sstevel@tonic-gate 	if (mn_root != NULL) {
75497c478bd9Sstevel@tonic-gate 		*mn_root = contents;
75507c478bd9Sstevel@tonic-gate 	}
75517c478bd9Sstevel@tonic-gate 	return (DEVFSADM_FALSE);
75527c478bd9Sstevel@tonic-gate }
75537c478bd9Sstevel@tonic-gate 
75547c478bd9Sstevel@tonic-gate /*
75557c478bd9Sstevel@tonic-gate  * Lookup nvpair corresponding to the given name and type:
75567c478bd9Sstevel@tonic-gate  *
75577c478bd9Sstevel@tonic-gate  * The standard nvlist_lookup functions in libnvpair don't work as our
75587c478bd9Sstevel@tonic-gate  * nvlist is not allocated with NV_UNIQUE_NAME or NV_UNIQUE_NAME_TYPE.
75597c478bd9Sstevel@tonic-gate  */
75607c478bd9Sstevel@tonic-gate static nvpair_t *
75617c478bd9Sstevel@tonic-gate lookup_nvpair(nvlist_t *nvl, char *name, data_type_t type)
75627c478bd9Sstevel@tonic-gate {
75637c478bd9Sstevel@tonic-gate 	nvpair_t *nvp;
75647c478bd9Sstevel@tonic-gate 
75657c478bd9Sstevel@tonic-gate 	for (nvp = nvlist_next_nvpair(nvl, NULL); nvp != NULL;
75667c478bd9Sstevel@tonic-gate 	    nvp = nvlist_next_nvpair(nvl, nvp)) {
75677c478bd9Sstevel@tonic-gate 		if (strcmp(name, nvpair_name(nvp)) == 0 &&
75687c478bd9Sstevel@tonic-gate 		    nvpair_type(nvp) == type)
75697c478bd9Sstevel@tonic-gate 			return (nvp);
75707c478bd9Sstevel@tonic-gate 	}
75717c478bd9Sstevel@tonic-gate 
75727c478bd9Sstevel@tonic-gate 	return (NULL);
75737c478bd9Sstevel@tonic-gate }
75747c478bd9Sstevel@tonic-gate 
75757c478bd9Sstevel@tonic-gate /*ARGSUSED*/
75767c478bd9Sstevel@tonic-gate static void
75777c478bd9Sstevel@tonic-gate process_rcm_events(void *arg)
75787c478bd9Sstevel@tonic-gate {
75797c478bd9Sstevel@tonic-gate 	struct rcm_eventq *ev, *ev_next;
7580210db224Sericheng 	nvpair_t *nvp;
7581210db224Sericheng 	char *path, *driver;
7582210db224Sericheng 	int instance;
7583210db224Sericheng 	int err;
75847c478bd9Sstevel@tonic-gate 	int need_to_exit;
75857c478bd9Sstevel@tonic-gate 
75867c478bd9Sstevel@tonic-gate 	for (;;) {
75877c478bd9Sstevel@tonic-gate 		(void) mutex_lock(&rcm_eventq_lock);
75887c478bd9Sstevel@tonic-gate 		while (rcm_eventq_head == NULL &&
75897c478bd9Sstevel@tonic-gate 		    need_to_exit_rcm_event_thread == 0)
75907c478bd9Sstevel@tonic-gate 			(void) cond_wait(&rcm_eventq_cv, &rcm_eventq_lock);
75917c478bd9Sstevel@tonic-gate 
75927c478bd9Sstevel@tonic-gate 		need_to_exit = need_to_exit_rcm_event_thread;
75937c478bd9Sstevel@tonic-gate 		ev = rcm_eventq_head;
75947c478bd9Sstevel@tonic-gate 		rcm_eventq_head = rcm_eventq_tail = NULL;
75957c478bd9Sstevel@tonic-gate 		(void) mutex_unlock(&rcm_eventq_lock);
75967c478bd9Sstevel@tonic-gate 
75977c478bd9Sstevel@tonic-gate 		for (; ev != NULL; ev = ev_next) {
75987c478bd9Sstevel@tonic-gate 			/*
75997c478bd9Sstevel@tonic-gate 			 * Private notification interface to RCM:
76007c478bd9Sstevel@tonic-gate 			 * Do not retry the RCM notification on an error since
76017c478bd9Sstevel@tonic-gate 			 * we do not know whether the failure occurred in
76027c478bd9Sstevel@tonic-gate 			 * librcm, rcm_daemon or rcm modules or scripts.
76037c478bd9Sstevel@tonic-gate 			 */
7604210db224Sericheng 			if (librcm_notify_event(rcm_hdl,
7605210db224Sericheng 			    RCM_RESOURCE_NETWORK_NEW, 0, ev->nvl, NULL)
7606210db224Sericheng 			    != RCM_SUCCESS) {
7607210db224Sericheng 
7608210db224Sericheng 				err = errno;
7609210db224Sericheng 
7610210db224Sericheng 				if (((nvp = lookup_nvpair(ev->nvl,
7611210db224Sericheng 				    RCM_NV_DEVFS_PATH, DATA_TYPE_STRING))
7612210db224Sericheng 				    == NULL) ||
7613210db224Sericheng 				    (nvpair_value_string(nvp, &path) != 0))
7614210db224Sericheng 					    path = "unknown";
7615210db224Sericheng 
7616210db224Sericheng 				if (((nvp = lookup_nvpair(ev->nvl,
7617210db224Sericheng 				    RCM_NV_DRIVER_NAME, DATA_TYPE_STRING))
7618210db224Sericheng 				    == NULL) ||
7619210db224Sericheng 				    (nvpair_value_string(nvp, &driver) != 0))
7620210db224Sericheng 					    driver = "unknown";
7621210db224Sericheng 				if (((nvp = lookup_nvpair(ev->nvl,
7622210db224Sericheng 				    RCM_NV_INSTANCE, DATA_TYPE_INT32))
7623210db224Sericheng 				    == NULL) ||
7624210db224Sericheng 				    (nvpair_value_int32(nvp, &instance) != 0))
7625210db224Sericheng 					    instance = -1;
7626210db224Sericheng 
7627210db224Sericheng 				err_print(RCM_NOTIFY_FAILED, path, driver,
7628210db224Sericheng 				    instance, strerror(err));
7629210db224Sericheng 			}
76307c478bd9Sstevel@tonic-gate 
76317c478bd9Sstevel@tonic-gate 			ev_next = ev->next;
76327c478bd9Sstevel@tonic-gate 			nvlist_free(ev->nvl);
76337c478bd9Sstevel@tonic-gate 			free(ev);
76347c478bd9Sstevel@tonic-gate 		}
76357c478bd9Sstevel@tonic-gate 
76367c478bd9Sstevel@tonic-gate 		if (need_to_exit)
76377c478bd9Sstevel@tonic-gate 			return;
76387c478bd9Sstevel@tonic-gate 	}
76397c478bd9Sstevel@tonic-gate }
76407c478bd9Sstevel@tonic-gate 
76417c478bd9Sstevel@tonic-gate /*
76427c478bd9Sstevel@tonic-gate  * Initialize rcm related handles and function pointers.
76437c478bd9Sstevel@tonic-gate  * Since RCM need not present in miniroot, we dlopen librcm.
76447c478bd9Sstevel@tonic-gate  */
76457c478bd9Sstevel@tonic-gate static int
76467c478bd9Sstevel@tonic-gate rcm_init(void)
76477c478bd9Sstevel@tonic-gate {
76487c478bd9Sstevel@tonic-gate #define	LIBRCM_PATH	"/usr/lib/librcm.so"
76497c478bd9Sstevel@tonic-gate 	rcm_handle_t *hdl = NULL;
76507c478bd9Sstevel@tonic-gate 	int err;
76517c478bd9Sstevel@tonic-gate 
76527c478bd9Sstevel@tonic-gate 	if ((librcm_hdl = dlopen(LIBRCM_PATH, RTLD_LAZY)) == NULL) {
76537c478bd9Sstevel@tonic-gate 		/*
76547c478bd9Sstevel@tonic-gate 		 * don't log an error here, since librcm may not be present
76557c478bd9Sstevel@tonic-gate 		 * in miniroot.
76567c478bd9Sstevel@tonic-gate 		 */
76577c478bd9Sstevel@tonic-gate 		return (-1);
76587c478bd9Sstevel@tonic-gate 	}
76597c478bd9Sstevel@tonic-gate 
76607c478bd9Sstevel@tonic-gate 	librcm_alloc_handle = (int (*)())dlsym(librcm_hdl, "rcm_alloc_handle");
76617c478bd9Sstevel@tonic-gate 	librcm_free_handle = (void (*)())dlsym(librcm_hdl, "rcm_free_handle");
76627c478bd9Sstevel@tonic-gate 	librcm_notify_event = (int (*)())dlsym(librcm_hdl, "rcm_notify_event");
76637c478bd9Sstevel@tonic-gate 
76647c478bd9Sstevel@tonic-gate 	if (librcm_alloc_handle == NULL || librcm_notify_event == NULL ||
76657c478bd9Sstevel@tonic-gate 	    librcm_free_handle == NULL) {
76667c478bd9Sstevel@tonic-gate 		err_print(MISSING_SYMBOLS, LIBRCM_PATH);
76677c478bd9Sstevel@tonic-gate 		goto out;
76687c478bd9Sstevel@tonic-gate 	}
76697c478bd9Sstevel@tonic-gate 
76707c478bd9Sstevel@tonic-gate 	/* Initialize the rcm handle */
76717c478bd9Sstevel@tonic-gate 	if (librcm_alloc_handle(NULL, 0, NULL, &hdl) != RCM_SUCCESS) {
76727c478bd9Sstevel@tonic-gate 		err_print(RCM_ALLOC_HANDLE_ERROR);
76737c478bd9Sstevel@tonic-gate 		goto out;
76747c478bd9Sstevel@tonic-gate 	}
76757c478bd9Sstevel@tonic-gate 
76767c478bd9Sstevel@tonic-gate 	(void) cond_init(&rcm_eventq_cv, USYNC_THREAD, 0);
76777c478bd9Sstevel@tonic-gate 	(void) mutex_init(&rcm_eventq_lock, USYNC_THREAD, 0);
76787c478bd9Sstevel@tonic-gate 
76797c478bd9Sstevel@tonic-gate 	/* create a thread to notify RCM of events */
76807c478bd9Sstevel@tonic-gate 	if ((err = thr_create(NULL, 0, (void *(*)(void *))process_rcm_events,
76817c478bd9Sstevel@tonic-gate 	    NULL, 0, &process_rcm_events_tid)) != 0) {
76827c478bd9Sstevel@tonic-gate 		err_print(CANT_CREATE_THREAD, "process_rcm_events",
76837c478bd9Sstevel@tonic-gate 		    strerror(err));
76847c478bd9Sstevel@tonic-gate 		goto out;
76857c478bd9Sstevel@tonic-gate 	}
76867c478bd9Sstevel@tonic-gate 
76877c478bd9Sstevel@tonic-gate 	rcm_hdl = hdl;
76887c478bd9Sstevel@tonic-gate 	return (0);
76897c478bd9Sstevel@tonic-gate 
76907c478bd9Sstevel@tonic-gate out:
76917c478bd9Sstevel@tonic-gate 	if (hdl)
76927c478bd9Sstevel@tonic-gate 		librcm_free_handle(hdl);
76937c478bd9Sstevel@tonic-gate 	(void) dlclose(librcm_hdl);
76947c478bd9Sstevel@tonic-gate 	return (-1);
76957c478bd9Sstevel@tonic-gate }
76967c478bd9Sstevel@tonic-gate 
76977c478bd9Sstevel@tonic-gate /*
76987c478bd9Sstevel@tonic-gate  * Build an nvlist using the minor data. Pack it and add the packed nvlist
76997c478bd9Sstevel@tonic-gate  * as a byte array to nv_list parameter.
77007c478bd9Sstevel@tonic-gate  * Return 0 on success, errno on failure.
77017c478bd9Sstevel@tonic-gate  */
77027c478bd9Sstevel@tonic-gate static int
77037c478bd9Sstevel@tonic-gate add_minor_data_to_nvl(nvlist_t *nv_list, di_minor_t minor)
77047c478bd9Sstevel@tonic-gate {
77057c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
77067c478bd9Sstevel@tonic-gate 	int32_t minor_type;
77077c478bd9Sstevel@tonic-gate 	char *minor_name, *minor_node_type;
77087c478bd9Sstevel@tonic-gate 	int err;
77097c478bd9Sstevel@tonic-gate 	char *buf = NULL;
77107c478bd9Sstevel@tonic-gate 	size_t buflen = 0;
77117c478bd9Sstevel@tonic-gate 
77127c478bd9Sstevel@tonic-gate 	if ((err = nvlist_alloc(&nvl, 0, 0)) != 0)
77137c478bd9Sstevel@tonic-gate 		return (err);
77147c478bd9Sstevel@tonic-gate 
77157c478bd9Sstevel@tonic-gate 	minor_type = (int32_t)di_minor_type(minor);
77167c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_int32(nvl, RCM_NV_MINOR_TYPE, minor_type)) != 0)
77177c478bd9Sstevel@tonic-gate 		goto error;
77187c478bd9Sstevel@tonic-gate 
77197c478bd9Sstevel@tonic-gate 	minor_name = di_minor_name(minor);
77207c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_string(nvl, RCM_NV_MINOR_NAME, minor_name)) != 0)
77217c478bd9Sstevel@tonic-gate 		goto error;
77227c478bd9Sstevel@tonic-gate 
77237c478bd9Sstevel@tonic-gate 	if ((minor_node_type = di_minor_nodetype(minor)) == NULL)
77247c478bd9Sstevel@tonic-gate 		minor_node_type = "";
77257c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_string(nvl, RCM_NV_MINOR_NODE_TYPE,
77267c478bd9Sstevel@tonic-gate 	    minor_node_type)) != 0)
77277c478bd9Sstevel@tonic-gate 		goto error;
77287c478bd9Sstevel@tonic-gate 
77297c478bd9Sstevel@tonic-gate 	if ((err = nvlist_pack(nvl, &buf, &buflen, NV_ENCODE_NATIVE, 0)) != 0)
77307c478bd9Sstevel@tonic-gate 		goto error;
77317c478bd9Sstevel@tonic-gate 
77327c478bd9Sstevel@tonic-gate 	err = nvlist_add_byte_array(nv_list, RCM_NV_MINOR_DATA,
77337c478bd9Sstevel@tonic-gate 	    (uchar_t *)(buf), (uint_t)(buflen));
77347c478bd9Sstevel@tonic-gate 
77357c478bd9Sstevel@tonic-gate error:
77367c478bd9Sstevel@tonic-gate 	nvlist_free(nvl);
77377c478bd9Sstevel@tonic-gate 	if (buf)
77387c478bd9Sstevel@tonic-gate 		free(buf);
77397c478bd9Sstevel@tonic-gate 	return (err);
77407c478bd9Sstevel@tonic-gate }
77417c478bd9Sstevel@tonic-gate 
77427c478bd9Sstevel@tonic-gate static void
77437c478bd9Sstevel@tonic-gate enqueue_rcm_event(nvlist_t *nvl)
77447c478bd9Sstevel@tonic-gate {
77457c478bd9Sstevel@tonic-gate 	struct rcm_eventq *ev;
77467c478bd9Sstevel@tonic-gate 
77477c478bd9Sstevel@tonic-gate 	ev = (struct rcm_eventq *)s_zalloc(sizeof (struct rcm_eventq));
77487c478bd9Sstevel@tonic-gate 	ev->nvl = nvl;
77497c478bd9Sstevel@tonic-gate 
77507c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&rcm_eventq_lock);
77517c478bd9Sstevel@tonic-gate 	if (rcm_eventq_head == NULL)
77527c478bd9Sstevel@tonic-gate 		rcm_eventq_head = ev;
77537c478bd9Sstevel@tonic-gate 	else
77547c478bd9Sstevel@tonic-gate 		rcm_eventq_tail->next = ev;
77557c478bd9Sstevel@tonic-gate 	rcm_eventq_tail = ev;
77567c478bd9Sstevel@tonic-gate 	(void) cond_broadcast(&rcm_eventq_cv);
77577c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&rcm_eventq_lock);
77587c478bd9Sstevel@tonic-gate }
77597c478bd9Sstevel@tonic-gate 
77607c478bd9Sstevel@tonic-gate /*
77617c478bd9Sstevel@tonic-gate  * Generate an nvlist using the information given in node and minor_name.
77627c478bd9Sstevel@tonic-gate  * If minor_name is NULL the nvlist will contain information on
77637c478bd9Sstevel@tonic-gate  * all minor nodes. Otherwise the nvlist will contain information
77647c478bd9Sstevel@tonic-gate  * only on the given minor_name. Notify RCM passing the nvlist.
77657c478bd9Sstevel@tonic-gate  *
77667c478bd9Sstevel@tonic-gate  * Return 0 upon successfully notifying RCM, errno on failure.
77677c478bd9Sstevel@tonic-gate  */
77687c478bd9Sstevel@tonic-gate static int
77697c478bd9Sstevel@tonic-gate notify_rcm(di_node_t node, char *minor_name)
77707c478bd9Sstevel@tonic-gate {
77717c478bd9Sstevel@tonic-gate 	nvlist_t *nvl = NULL;
77727c478bd9Sstevel@tonic-gate 	char *path, *driver_name;
77737c478bd9Sstevel@tonic-gate 	char *node_name;
77747c478bd9Sstevel@tonic-gate 	int err;
77757c478bd9Sstevel@tonic-gate 	int32_t instance;
77767c478bd9Sstevel@tonic-gate 	di_minor_t minor;
77777c478bd9Sstevel@tonic-gate 
77787c478bd9Sstevel@tonic-gate 	if ((driver_name = di_driver_name(node)) == NULL)
77797c478bd9Sstevel@tonic-gate 		driver_name = "";
77807c478bd9Sstevel@tonic-gate 
77817c478bd9Sstevel@tonic-gate 	instance = (int32_t)di_instance(node);
77827c478bd9Sstevel@tonic-gate 
77837c478bd9Sstevel@tonic-gate 	if ((path = di_devfs_path(node)) == NULL) {
77847c478bd9Sstevel@tonic-gate 		err = errno;
77857c478bd9Sstevel@tonic-gate 		goto error;
77867c478bd9Sstevel@tonic-gate 	}
77877c478bd9Sstevel@tonic-gate 
77887c478bd9Sstevel@tonic-gate 	if ((err = nvlist_alloc(&nvl, 0, 0)) != 0)
77897c478bd9Sstevel@tonic-gate 		goto error;
77907c478bd9Sstevel@tonic-gate 
77917c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_string(nvl, RCM_NV_DRIVER_NAME, driver_name))
77927c478bd9Sstevel@tonic-gate 	    != 0)
77937c478bd9Sstevel@tonic-gate 		goto error;
77947c478bd9Sstevel@tonic-gate 
77957c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_int32(nvl, RCM_NV_INSTANCE, instance)) != 0)
77967c478bd9Sstevel@tonic-gate 		goto error;
77977c478bd9Sstevel@tonic-gate 
77987c478bd9Sstevel@tonic-gate 	if ((node_name = di_node_name(node)) == NULL)
77997c478bd9Sstevel@tonic-gate 		node_name = "";
78007c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_string(nvl, RCM_NV_NODE_NAME, node_name)) != 0)
78017c478bd9Sstevel@tonic-gate 		goto error;
78027c478bd9Sstevel@tonic-gate 
78037c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_string(nvl, RCM_NV_DEVFS_PATH, path)) != 0)
78047c478bd9Sstevel@tonic-gate 		goto error;
78057c478bd9Sstevel@tonic-gate 
78067c478bd9Sstevel@tonic-gate 	minor = di_minor_next(node, DI_MINOR_NIL);
78077c478bd9Sstevel@tonic-gate 	while (minor != DI_MINOR_NIL) {
78087c478bd9Sstevel@tonic-gate 		if ((minor_name == NULL) ||
78097c478bd9Sstevel@tonic-gate 		    (strcmp(minor_name, di_minor_name(minor)) == 0)) {
78107c478bd9Sstevel@tonic-gate 			if ((err = add_minor_data_to_nvl(nvl, minor)) != 0)
78117c478bd9Sstevel@tonic-gate 				goto error;
78127c478bd9Sstevel@tonic-gate 		}
78137c478bd9Sstevel@tonic-gate 		minor = di_minor_next(node, minor);
78147c478bd9Sstevel@tonic-gate 	}
78157c478bd9Sstevel@tonic-gate 
78167c478bd9Sstevel@tonic-gate 	enqueue_rcm_event(nvl);
78177c478bd9Sstevel@tonic-gate 	di_devfs_path_free(path);
78187c478bd9Sstevel@tonic-gate 	return (0);
78197c478bd9Sstevel@tonic-gate 
78207c478bd9Sstevel@tonic-gate error:
78217c478bd9Sstevel@tonic-gate 	err_print(RCM_NVLIST_BUILD_ERROR, ((path != NULL) ? path : "unknown"),
78227c478bd9Sstevel@tonic-gate 	    driver_name, instance, strerror(err));
78237c478bd9Sstevel@tonic-gate 
78247c478bd9Sstevel@tonic-gate 	if (path)
78257c478bd9Sstevel@tonic-gate 		di_devfs_path_free(path);
78267c478bd9Sstevel@tonic-gate 	if (nvl)
78277c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
78287c478bd9Sstevel@tonic-gate 	return (err);
78297c478bd9Sstevel@tonic-gate }
78307c478bd9Sstevel@tonic-gate 
78317c478bd9Sstevel@tonic-gate /*
78327c478bd9Sstevel@tonic-gate  * Add the specified property to nvl.
78337c478bd9Sstevel@tonic-gate  * Returns:
78347c478bd9Sstevel@tonic-gate  *   0	successfully added
78357c478bd9Sstevel@tonic-gate  *   -1	an error occurred
78367c478bd9Sstevel@tonic-gate  *   1	could not add the property for reasons not due to errors.
78377c478bd9Sstevel@tonic-gate  */
78387c478bd9Sstevel@tonic-gate static int
78397c478bd9Sstevel@tonic-gate add_property(nvlist_t *nvl, di_prop_t prop)
78407c478bd9Sstevel@tonic-gate {
78417c478bd9Sstevel@tonic-gate 	char *name;
78427c478bd9Sstevel@tonic-gate 	char *attr_name;
78437c478bd9Sstevel@tonic-gate 	int n, len;
78447c478bd9Sstevel@tonic-gate 	int32_t *int32p;
78457c478bd9Sstevel@tonic-gate 	int64_t *int64p;
78467c478bd9Sstevel@tonic-gate 	char *str;
78477c478bd9Sstevel@tonic-gate 	char **strarray;
78487c478bd9Sstevel@tonic-gate 	uchar_t *bytep;
78497c478bd9Sstevel@tonic-gate 	int rv = 0;
78507c478bd9Sstevel@tonic-gate 	int i;
78517c478bd9Sstevel@tonic-gate 
78527c478bd9Sstevel@tonic-gate 	if ((name = di_prop_name(prop)) == NULL)
78537c478bd9Sstevel@tonic-gate 		return (-1);
78547c478bd9Sstevel@tonic-gate 
78557c478bd9Sstevel@tonic-gate 	len = sizeof (DEV_PROP_PREFIX) + strlen(name);
78567c478bd9Sstevel@tonic-gate 	if ((attr_name = malloc(len)) == NULL)
78577c478bd9Sstevel@tonic-gate 		return (-1);
78587c478bd9Sstevel@tonic-gate 
78597c478bd9Sstevel@tonic-gate 	(void) strlcpy(attr_name, DEV_PROP_PREFIX, len);
78607c478bd9Sstevel@tonic-gate 	(void) strlcat(attr_name, name, len);
78617c478bd9Sstevel@tonic-gate 
78627c478bd9Sstevel@tonic-gate 	switch (di_prop_type(prop)) {
78637c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_BOOLEAN:
78647c478bd9Sstevel@tonic-gate 		if (nvlist_add_boolean(nvl, attr_name) != 0)
78657c478bd9Sstevel@tonic-gate 			goto out;
78667c478bd9Sstevel@tonic-gate 		break;
78677c478bd9Sstevel@tonic-gate 
78687c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_INT:
78697c478bd9Sstevel@tonic-gate 		if ((n = di_prop_ints(prop, &int32p)) < 1)
78707c478bd9Sstevel@tonic-gate 			goto out;
78717c478bd9Sstevel@tonic-gate 
78727c478bd9Sstevel@tonic-gate 		if (n <= (PROP_LEN_LIMIT / sizeof (int32_t))) {
78737c478bd9Sstevel@tonic-gate 			if (nvlist_add_int32_array(nvl, attr_name, int32p,
78747c478bd9Sstevel@tonic-gate 			    n) != 0)
78757c478bd9Sstevel@tonic-gate 				goto out;
78767c478bd9Sstevel@tonic-gate 		} else
78777c478bd9Sstevel@tonic-gate 			rv = 1;
78787c478bd9Sstevel@tonic-gate 		break;
78797c478bd9Sstevel@tonic-gate 
78807c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_INT64:
78817c478bd9Sstevel@tonic-gate 		if ((n = di_prop_int64(prop, &int64p)) < 1)
78827c478bd9Sstevel@tonic-gate 			goto out;
78837c478bd9Sstevel@tonic-gate 
78847c478bd9Sstevel@tonic-gate 		if (n <= (PROP_LEN_LIMIT / sizeof (int64_t))) {
78857c478bd9Sstevel@tonic-gate 			if (nvlist_add_int64_array(nvl, attr_name, int64p,
78867c478bd9Sstevel@tonic-gate 			    n) != 0)
78877c478bd9Sstevel@tonic-gate 				goto out;
78887c478bd9Sstevel@tonic-gate 		} else
78897c478bd9Sstevel@tonic-gate 			rv = 1;
78907c478bd9Sstevel@tonic-gate 		break;
78917c478bd9Sstevel@tonic-gate 
78927c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_BYTE:
78937c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_UNKNOWN:
78947c478bd9Sstevel@tonic-gate 		if ((n = di_prop_bytes(prop, &bytep)) < 1)
78957c478bd9Sstevel@tonic-gate 			goto out;
78967c478bd9Sstevel@tonic-gate 
78977c478bd9Sstevel@tonic-gate 		if (n <= PROP_LEN_LIMIT) {
78987c478bd9Sstevel@tonic-gate 			if (nvlist_add_byte_array(nvl, attr_name, bytep, n)
78997c478bd9Sstevel@tonic-gate 			    != 0)
79007c478bd9Sstevel@tonic-gate 				goto out;
79017c478bd9Sstevel@tonic-gate 		} else
79027c478bd9Sstevel@tonic-gate 			rv = 1;
79037c478bd9Sstevel@tonic-gate 		break;
79047c478bd9Sstevel@tonic-gate 
79057c478bd9Sstevel@tonic-gate 	case DI_PROP_TYPE_STRING:
79067c478bd9Sstevel@tonic-gate 		if ((n = di_prop_strings(prop, &str)) < 1)
79077c478bd9Sstevel@tonic-gate 			goto out;
79087c478bd9Sstevel@tonic-gate 
79097c478bd9Sstevel@tonic-gate 		if ((strarray = malloc(n * sizeof (char *))) == NULL)
79107c478bd9Sstevel@tonic-gate 			goto out;
79117c478bd9Sstevel@tonic-gate 
79127c478bd9Sstevel@tonic-gate 		len = 0;
79137c478bd9Sstevel@tonic-gate 		for (i = 0; i < n; i++) {
79147c478bd9Sstevel@tonic-gate 			strarray[i] = str + len;
79157c478bd9Sstevel@tonic-gate 			len += strlen(strarray[i]) + 1;
79167c478bd9Sstevel@tonic-gate 		}
79177c478bd9Sstevel@tonic-gate 
79187c478bd9Sstevel@tonic-gate 		if (len <= PROP_LEN_LIMIT) {
79197c478bd9Sstevel@tonic-gate 			if (nvlist_add_string_array(nvl, attr_name, strarray,
79207c478bd9Sstevel@tonic-gate 			    n) != 0) {
79217c478bd9Sstevel@tonic-gate 				free(strarray);
79227c478bd9Sstevel@tonic-gate 				goto out;
79237c478bd9Sstevel@tonic-gate 			}
79247c478bd9Sstevel@tonic-gate 		} else
79257c478bd9Sstevel@tonic-gate 			rv = 1;
79267c478bd9Sstevel@tonic-gate 		free(strarray);
79277c478bd9Sstevel@tonic-gate 		break;
79287c478bd9Sstevel@tonic-gate 
79297c478bd9Sstevel@tonic-gate 	default:
79307c478bd9Sstevel@tonic-gate 		rv = 1;
79317c478bd9Sstevel@tonic-gate 		break;
79327c478bd9Sstevel@tonic-gate 	}
79337c478bd9Sstevel@tonic-gate 
79347c478bd9Sstevel@tonic-gate 	free(attr_name);
79357c478bd9Sstevel@tonic-gate 	return (rv);
79367c478bd9Sstevel@tonic-gate 
79377c478bd9Sstevel@tonic-gate out:
79387c478bd9Sstevel@tonic-gate 	free(attr_name);
79397c478bd9Sstevel@tonic-gate 	return (-1);
79407c478bd9Sstevel@tonic-gate }
79417c478bd9Sstevel@tonic-gate 
79427c478bd9Sstevel@tonic-gate static void
79437c478bd9Sstevel@tonic-gate free_dev_names(struct devlink_cb_arg *x)
79447c478bd9Sstevel@tonic-gate {
79457c478bd9Sstevel@tonic-gate 	int i;
79467c478bd9Sstevel@tonic-gate 
79477c478bd9Sstevel@tonic-gate 	for (i = 0; i < x->count; i++) {
79487c478bd9Sstevel@tonic-gate 		free(x->dev_names[i]);
79497c478bd9Sstevel@tonic-gate 		free(x->link_contents[i]);
79507c478bd9Sstevel@tonic-gate 	}
79517c478bd9Sstevel@tonic-gate }
79527c478bd9Sstevel@tonic-gate 
79537c478bd9Sstevel@tonic-gate /* callback function for di_devlink_cache_walk */
79547c478bd9Sstevel@tonic-gate static int
79557c478bd9Sstevel@tonic-gate devlink_cb(di_devlink_t dl, void *arg)
79567c478bd9Sstevel@tonic-gate {
79577c478bd9Sstevel@tonic-gate 	struct devlink_cb_arg *x = (struct devlink_cb_arg *)arg;
79587c478bd9Sstevel@tonic-gate 	const char *path;
79597c478bd9Sstevel@tonic-gate 	const char *content;
79607c478bd9Sstevel@tonic-gate 
79617c478bd9Sstevel@tonic-gate 	if ((path = di_devlink_path(dl)) == NULL ||
79627c478bd9Sstevel@tonic-gate 	    (content = di_devlink_content(dl)) == NULL ||
79637c478bd9Sstevel@tonic-gate 	    (x->dev_names[x->count] = strdup(path)) == NULL)
79647c478bd9Sstevel@tonic-gate 		goto out;
79657c478bd9Sstevel@tonic-gate 
79667c478bd9Sstevel@tonic-gate 	if ((x->link_contents[x->count] = strdup(content)) == NULL) {
79677c478bd9Sstevel@tonic-gate 		free(x->dev_names[x->count]);
79687c478bd9Sstevel@tonic-gate 		goto out;
79697c478bd9Sstevel@tonic-gate 	}
79707c478bd9Sstevel@tonic-gate 
79717c478bd9Sstevel@tonic-gate 	x->count++;
79727c478bd9Sstevel@tonic-gate 	if (x->count >= MAX_DEV_NAME_COUNT)
79737c478bd9Sstevel@tonic-gate 		return (DI_WALK_TERMINATE);
79747c478bd9Sstevel@tonic-gate 
79757c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
79767c478bd9Sstevel@tonic-gate 
79777c478bd9Sstevel@tonic-gate out:
79787c478bd9Sstevel@tonic-gate 	x->rv = -1;
79797c478bd9Sstevel@tonic-gate 	free_dev_names(x);
79807c478bd9Sstevel@tonic-gate 	return (DI_WALK_TERMINATE);
79817c478bd9Sstevel@tonic-gate }
79827c478bd9Sstevel@tonic-gate 
79837c478bd9Sstevel@tonic-gate /*
79847c478bd9Sstevel@tonic-gate  * Lookup dev name corresponding to the phys_path.
79857c478bd9Sstevel@tonic-gate  * phys_path is path to a node or minor node.
79867c478bd9Sstevel@tonic-gate  * Returns:
79877c478bd9Sstevel@tonic-gate  *	0 with *dev_name set to the dev name
79887c478bd9Sstevel@tonic-gate  *		Lookup succeeded and dev_name found
79897c478bd9Sstevel@tonic-gate  *	0 with *dev_name set to NULL
79907c478bd9Sstevel@tonic-gate  *		Lookup encountered no errors but dev name not found
79917c478bd9Sstevel@tonic-gate  *	-1
79927c478bd9Sstevel@tonic-gate  *		Lookup failed
79937c478bd9Sstevel@tonic-gate  */
79947c478bd9Sstevel@tonic-gate static int
79957c478bd9Sstevel@tonic-gate lookup_dev_name(char *phys_path, char **dev_name)
79967c478bd9Sstevel@tonic-gate {
79977c478bd9Sstevel@tonic-gate 	struct devlink_cb_arg cb_arg;
79987c478bd9Sstevel@tonic-gate 
79997c478bd9Sstevel@tonic-gate 	*dev_name = NULL;
80007c478bd9Sstevel@tonic-gate 
80017c478bd9Sstevel@tonic-gate 	cb_arg.count = 0;
80027c478bd9Sstevel@tonic-gate 	cb_arg.rv = 0;
80037c478bd9Sstevel@tonic-gate 	(void) di_devlink_cache_walk(devlink_cache, NULL, phys_path,
80047c478bd9Sstevel@tonic-gate 	    DI_PRIMARY_LINK, &cb_arg, devlink_cb);
80057c478bd9Sstevel@tonic-gate 
80067c478bd9Sstevel@tonic-gate 	if (cb_arg.rv == -1)
80077c478bd9Sstevel@tonic-gate 		return (-1);
80087c478bd9Sstevel@tonic-gate 
80097c478bd9Sstevel@tonic-gate 	if (cb_arg.count > 0) {
80107c478bd9Sstevel@tonic-gate 		*dev_name = strdup(cb_arg.dev_names[0]);
80117c478bd9Sstevel@tonic-gate 		free_dev_names(&cb_arg);
80127c478bd9Sstevel@tonic-gate 		if (*dev_name == NULL)
80137c478bd9Sstevel@tonic-gate 			return (-1);
80147c478bd9Sstevel@tonic-gate 	}
80157c478bd9Sstevel@tonic-gate 
80167c478bd9Sstevel@tonic-gate 	return (0);
80177c478bd9Sstevel@tonic-gate }
80187c478bd9Sstevel@tonic-gate 
80197c478bd9Sstevel@tonic-gate static char *
80207c478bd9Sstevel@tonic-gate lookup_disk_dev_name(char *node_path)
80217c478bd9Sstevel@tonic-gate {
80227c478bd9Sstevel@tonic-gate 	struct devlink_cb_arg cb_arg;
80237c478bd9Sstevel@tonic-gate 	char *dev_name = NULL;
80247c478bd9Sstevel@tonic-gate 	int i;
80257c478bd9Sstevel@tonic-gate 	char *p;
80267c478bd9Sstevel@tonic-gate 	int len1, len2;
80277c478bd9Sstevel@tonic-gate 
80287c478bd9Sstevel@tonic-gate #define	DEV_RDSK	"/dev/rdsk/"
80297c478bd9Sstevel@tonic-gate #define	DISK_RAW_MINOR	",raw"
80307c478bd9Sstevel@tonic-gate 
80317c478bd9Sstevel@tonic-gate 	cb_arg.count = 0;
80327c478bd9Sstevel@tonic-gate 	cb_arg.rv = 0;
80337c478bd9Sstevel@tonic-gate 	(void) di_devlink_cache_walk(devlink_cache, NULL, node_path,
80347c478bd9Sstevel@tonic-gate 	    DI_PRIMARY_LINK, &cb_arg, devlink_cb);
80357c478bd9Sstevel@tonic-gate 
80367c478bd9Sstevel@tonic-gate 	if (cb_arg.rv == -1 || cb_arg.count == 0)
80377c478bd9Sstevel@tonic-gate 		return (NULL);
80387c478bd9Sstevel@tonic-gate 
80397c478bd9Sstevel@tonic-gate 	/* first try lookup based on /dev/rdsk name */
80407c478bd9Sstevel@tonic-gate 	for (i = 0; i < cb_arg.count; i++) {
80417c478bd9Sstevel@tonic-gate 		if (strncmp(cb_arg.dev_names[i], DEV_RDSK,
80427c478bd9Sstevel@tonic-gate 		    sizeof (DEV_RDSK) - 1) == 0) {
80437c478bd9Sstevel@tonic-gate 			dev_name = strdup(cb_arg.dev_names[i]);
80447c478bd9Sstevel@tonic-gate 			break;
80457c478bd9Sstevel@tonic-gate 		}
80467c478bd9Sstevel@tonic-gate 	}
80477c478bd9Sstevel@tonic-gate 
80487c478bd9Sstevel@tonic-gate 	if (dev_name == NULL) {
80497c478bd9Sstevel@tonic-gate 		/* now try lookup based on a minor name ending with ",raw" */
80507c478bd9Sstevel@tonic-gate 		len1 = sizeof (DISK_RAW_MINOR) - 1;
80517c478bd9Sstevel@tonic-gate 		for (i = 0; i < cb_arg.count; i++) {
80527c478bd9Sstevel@tonic-gate 			len2 = strlen(cb_arg.link_contents[i]);
80537c478bd9Sstevel@tonic-gate 			if (len2 >= len1 &&
80547c478bd9Sstevel@tonic-gate 			    strcmp(cb_arg.link_contents[i] + len2 - len1,
80557c478bd9Sstevel@tonic-gate 			    DISK_RAW_MINOR) == 0) {
80567c478bd9Sstevel@tonic-gate 				dev_name = strdup(cb_arg.dev_names[i]);
80577c478bd9Sstevel@tonic-gate 				break;
80587c478bd9Sstevel@tonic-gate 			}
80597c478bd9Sstevel@tonic-gate 		}
80607c478bd9Sstevel@tonic-gate 	}
80617c478bd9Sstevel@tonic-gate 
80627c478bd9Sstevel@tonic-gate 	free_dev_names(&cb_arg);
80637c478bd9Sstevel@tonic-gate 
80647c478bd9Sstevel@tonic-gate 	if (strlen(dev_name) == 0) {
80657c478bd9Sstevel@tonic-gate 		free(dev_name);
80667c478bd9Sstevel@tonic-gate 		return (NULL);
80677c478bd9Sstevel@tonic-gate 	}
80687c478bd9Sstevel@tonic-gate 
80697c478bd9Sstevel@tonic-gate 	/* if the name contains slice or partition number strip it */
80707c478bd9Sstevel@tonic-gate 	p = dev_name + strlen(dev_name) - 1;
80717c478bd9Sstevel@tonic-gate 	if (isdigit(*p)) {
80727c478bd9Sstevel@tonic-gate 		while (p != dev_name && isdigit(*p))
80737c478bd9Sstevel@tonic-gate 			p--;
80747c478bd9Sstevel@tonic-gate 		if (*p == 's' || *p == 'p')
80757c478bd9Sstevel@tonic-gate 			*p = '\0';
80767c478bd9Sstevel@tonic-gate 	}
80777c478bd9Sstevel@tonic-gate 
80787c478bd9Sstevel@tonic-gate 	return (dev_name);
80797c478bd9Sstevel@tonic-gate }
80807c478bd9Sstevel@tonic-gate 
80817c478bd9Sstevel@tonic-gate static char *
80827c478bd9Sstevel@tonic-gate lookup_network_dev_name(char *node_path, char *driver_name)
80837c478bd9Sstevel@tonic-gate {
80847c478bd9Sstevel@tonic-gate 	char *dev_name = NULL;
80857c478bd9Sstevel@tonic-gate 	char phys_path[MAXPATHLEN];
80867c478bd9Sstevel@tonic-gate 
80877c478bd9Sstevel@tonic-gate 	if (lookup_dev_name(node_path, &dev_name) == -1)
80887c478bd9Sstevel@tonic-gate 		return (NULL);
80897c478bd9Sstevel@tonic-gate 
80907c478bd9Sstevel@tonic-gate 	if (dev_name == NULL) {
80917c478bd9Sstevel@tonic-gate 		/* dlpi style-2 only interface */
80927c478bd9Sstevel@tonic-gate 		(void) snprintf(phys_path, sizeof (phys_path),
80937c478bd9Sstevel@tonic-gate 		    "/pseudo/clone@0:%s", driver_name);
80947c478bd9Sstevel@tonic-gate 		if (lookup_dev_name(phys_path, &dev_name) == -1 ||
80957c478bd9Sstevel@tonic-gate 		    dev_name == NULL)
80967c478bd9Sstevel@tonic-gate 			return (NULL);
80977c478bd9Sstevel@tonic-gate 	}
80987c478bd9Sstevel@tonic-gate 
80997c478bd9Sstevel@tonic-gate 	return (dev_name);
81007c478bd9Sstevel@tonic-gate }
81017c478bd9Sstevel@tonic-gate 
81027c478bd9Sstevel@tonic-gate /*
81037c478bd9Sstevel@tonic-gate  * Build an nvlist containing all attributes for devfs events.
81047c478bd9Sstevel@tonic-gate  * Returns nvlist pointer on success, NULL on failure.
81057c478bd9Sstevel@tonic-gate  */
81067c478bd9Sstevel@tonic-gate static nvlist_t *
81077c478bd9Sstevel@tonic-gate build_event_attributes(char *class, char *subclass, char *node_path,
81087c478bd9Sstevel@tonic-gate     di_node_t node, char *driver_name, int instance)
81097c478bd9Sstevel@tonic-gate {
81107c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
81117c478bd9Sstevel@tonic-gate 	int err = 0;
81127c478bd9Sstevel@tonic-gate 	di_prop_t prop;
81137c478bd9Sstevel@tonic-gate 	int count;
81147c478bd9Sstevel@tonic-gate 	char *prop_name;
81157c478bd9Sstevel@tonic-gate 	int x;
81167c478bd9Sstevel@tonic-gate 	char *dev_name = NULL;
81177c478bd9Sstevel@tonic-gate 	int dev_name_lookup_err = 0;
81187c478bd9Sstevel@tonic-gate 
81197c478bd9Sstevel@tonic-gate 	if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, 0)) != 0) {
81207c478bd9Sstevel@tonic-gate 		nvl = NULL;
81217c478bd9Sstevel@tonic-gate 		goto out;
81227c478bd9Sstevel@tonic-gate 	}
81237c478bd9Sstevel@tonic-gate 
81247c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_int32(nvl, EV_VERSION, EV_V1)) != 0)
81257c478bd9Sstevel@tonic-gate 		goto out;
81267c478bd9Sstevel@tonic-gate 
81277c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_string(nvl, DEV_PHYS_PATH, node_path)) != 0)
81287c478bd9Sstevel@tonic-gate 		goto out;
81297c478bd9Sstevel@tonic-gate 
81307c478bd9Sstevel@tonic-gate 	if (strcmp(class, EC_DEV_ADD) != 0 &&
81317c478bd9Sstevel@tonic-gate 	    strcmp(class, EC_DEV_REMOVE) != 0)
81327c478bd9Sstevel@tonic-gate 		return (nvl);
81337c478bd9Sstevel@tonic-gate 
81347c478bd9Sstevel@tonic-gate 	if (driver_name == NULL || instance == -1)
81357c478bd9Sstevel@tonic-gate 		goto out;
81367c478bd9Sstevel@tonic-gate 
81377c478bd9Sstevel@tonic-gate 	if (strcmp(subclass, ESC_DISK) == 0) {
81387c478bd9Sstevel@tonic-gate 		if ((dev_name = lookup_disk_dev_name(node_path)) == NULL) {
81397c478bd9Sstevel@tonic-gate 			dev_name_lookup_err = 1;
81407c478bd9Sstevel@tonic-gate 			goto out;
81417c478bd9Sstevel@tonic-gate 		}
81427c478bd9Sstevel@tonic-gate 	} else if (strcmp(subclass, ESC_NETWORK) == 0) {
81437c478bd9Sstevel@tonic-gate 		if ((dev_name = lookup_network_dev_name(node_path, driver_name))
81447c478bd9Sstevel@tonic-gate 		    == NULL) {
81457c478bd9Sstevel@tonic-gate 			dev_name_lookup_err = 1;
81467c478bd9Sstevel@tonic-gate 			goto out;
81477c478bd9Sstevel@tonic-gate 		}
81487c478bd9Sstevel@tonic-gate 	}
81497c478bd9Sstevel@tonic-gate 
81507c478bd9Sstevel@tonic-gate 	if (dev_name) {
81517c478bd9Sstevel@tonic-gate 		if ((err = nvlist_add_string(nvl, DEV_NAME, dev_name)) != 0)
81527c478bd9Sstevel@tonic-gate 			goto out;
81537c478bd9Sstevel@tonic-gate 		free(dev_name);
81547c478bd9Sstevel@tonic-gate 		dev_name = NULL;
81557c478bd9Sstevel@tonic-gate 	}
81567c478bd9Sstevel@tonic-gate 
81577c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_string(nvl, DEV_DRIVER_NAME, driver_name)) != 0)
81587c478bd9Sstevel@tonic-gate 		goto out;
81597c478bd9Sstevel@tonic-gate 
81607c478bd9Sstevel@tonic-gate 	if ((err = nvlist_add_int32(nvl, DEV_INSTANCE, instance)) != 0)
81617c478bd9Sstevel@tonic-gate 		goto out;
81627c478bd9Sstevel@tonic-gate 
81637c478bd9Sstevel@tonic-gate 	if (strcmp(class, EC_DEV_ADD) == 0) {
81647c478bd9Sstevel@tonic-gate 		/* add properties */
81657c478bd9Sstevel@tonic-gate 		count = 0;
81667c478bd9Sstevel@tonic-gate 		for (prop = di_prop_next(node, DI_PROP_NIL);
81677c478bd9Sstevel@tonic-gate 		    prop != DI_PROP_NIL && count < MAX_PROP_COUNT;
81687c478bd9Sstevel@tonic-gate 		    prop = di_prop_next(node, prop)) {
81697c478bd9Sstevel@tonic-gate 
81707c478bd9Sstevel@tonic-gate 			if (di_prop_devt(prop) != DDI_DEV_T_NONE)
81717c478bd9Sstevel@tonic-gate 				continue;
81727c478bd9Sstevel@tonic-gate 
81737c478bd9Sstevel@tonic-gate 			if ((x = add_property(nvl, prop)) == 0)
81747c478bd9Sstevel@tonic-gate 				count++;
81757c478bd9Sstevel@tonic-gate 			else if (x == -1) {
81767c478bd9Sstevel@tonic-gate 				if ((prop_name = di_prop_name(prop)) == NULL)
81777c478bd9Sstevel@tonic-gate 					prop_name = "";
81787c478bd9Sstevel@tonic-gate 				err_print(PROP_ADD_FAILED, prop_name);
81797c478bd9Sstevel@tonic-gate 				goto out;
81807c478bd9Sstevel@tonic-gate 			}
81817c478bd9Sstevel@tonic-gate 		}
81827c478bd9Sstevel@tonic-gate 	}
81837c478bd9Sstevel@tonic-gate 
81847c478bd9Sstevel@tonic-gate 	return (nvl);
81857c478bd9Sstevel@tonic-gate 
81867c478bd9Sstevel@tonic-gate out:
81877c478bd9Sstevel@tonic-gate 	if (nvl)
81887c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
81897c478bd9Sstevel@tonic-gate 
81907c478bd9Sstevel@tonic-gate 	if (dev_name)
81917c478bd9Sstevel@tonic-gate 		free(dev_name);
81927c478bd9Sstevel@tonic-gate 
81937c478bd9Sstevel@tonic-gate 	if (dev_name_lookup_err)
81947c478bd9Sstevel@tonic-gate 		err_print(DEV_NAME_LOOKUP_FAILED, node_path);
81957c478bd9Sstevel@tonic-gate 	else
81967c478bd9Sstevel@tonic-gate 		err_print(BUILD_EVENT_ATTR_FAILED, (err) ? strerror(err) : "");
81977c478bd9Sstevel@tonic-gate 	return (NULL);
81987c478bd9Sstevel@tonic-gate }
81997c478bd9Sstevel@tonic-gate 
82007c478bd9Sstevel@tonic-gate static void
82017c478bd9Sstevel@tonic-gate log_event(char *class, char *subclass, nvlist_t *nvl)
82027c478bd9Sstevel@tonic-gate {
82037c478bd9Sstevel@tonic-gate 	sysevent_id_t eid;
82047c478bd9Sstevel@tonic-gate 
82057c478bd9Sstevel@tonic-gate 	if (sysevent_post_event(class, subclass, "SUNW", DEVFSADMD,
82067c478bd9Sstevel@tonic-gate 	    nvl, &eid) != 0) {
82077c478bd9Sstevel@tonic-gate 		err_print(LOG_EVENT_FAILED, strerror(errno));
82087c478bd9Sstevel@tonic-gate 	}
82097c478bd9Sstevel@tonic-gate }
82107c478bd9Sstevel@tonic-gate 
82117c478bd9Sstevel@tonic-gate static void
82127c478bd9Sstevel@tonic-gate build_and_log_event(char *class, char *subclass, char *node_path,
82137c478bd9Sstevel@tonic-gate     di_node_t node)
82147c478bd9Sstevel@tonic-gate {
82157c478bd9Sstevel@tonic-gate 	nvlist_t *nvl;
82167c478bd9Sstevel@tonic-gate 
82177c478bd9Sstevel@tonic-gate 	if (node != DI_NODE_NIL)
82187c478bd9Sstevel@tonic-gate 		nvl = build_event_attributes(class, subclass, node_path, node,
82197c478bd9Sstevel@tonic-gate 		    di_driver_name(node), di_instance(node));
82207c478bd9Sstevel@tonic-gate 	else
82217c478bd9Sstevel@tonic-gate 		nvl = build_event_attributes(class, subclass, node_path, node,
82227c478bd9Sstevel@tonic-gate 		    NULL, -1);
82237c478bd9Sstevel@tonic-gate 
82247c478bd9Sstevel@tonic-gate 	if (nvl) {
82257c478bd9Sstevel@tonic-gate 		log_event(class, subclass, nvl);
82267c478bd9Sstevel@tonic-gate 		nvlist_free(nvl);
82277c478bd9Sstevel@tonic-gate 	}
82287c478bd9Sstevel@tonic-gate }
8229