xref: /titanic_41/usr/src/cmd/devfsadm/devfsadm.c (revision 74b5a35d491a5d9569dd85fa6d4037ba66246430)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*
27  * Devfsadm replaces drvconfig, audlinks, disks, tapes, ports, devlinks
28  * as a general purpose device administrative utility.	It creates
29  * devices special files in /devices and logical links in /dev, and
30  * coordinates updates to /etc/path_to_instance with the kernel.  It
31  * operates in both command line mode to handle user or script invoked
32  * reconfiguration updates, and operates in daemon mode to handle dynamic
33  * reconfiguration for hotplugging support.
34  */
35 
36 #include <string.h>
37 #include <deflt.h>
38 #include <tsol/label.h>
39 #include <bsm/devices.h>
40 #include <bsm/devalloc.h>
41 #include <utime.h>
42 #include <sys/param.h>
43 #include <bsm/libbsm.h>
44 #include <zone.h>
45 #include "devfsadm_impl.h"
46 
47 /* externs from devalloc.c */
48 extern void  _reset_devalloc(int);
49 extern void _update_devalloc_db(devlist_t *, int, int, char *, char *);
50 extern int _da_check_for_usb(char *, char *);
51 
52 /* create or remove nodes or links. unset with -n */
53 static int file_mods = TRUE;
54 
55 /* cleanup mode.  Set with -C */
56 static int cleanup = FALSE;
57 
58 /* devlinks -d compatibility */
59 static int devlinks_debug = FALSE;
60 
61 /* flag to check if system is labeled */
62 int system_labeled = FALSE;
63 
64 /* flag to enable/disable device allocation with -e/-d */
65 static int devalloc_flag = 0;
66 
67 /* flag that indicates if device allocation is on or not */
68 static int devalloc_is_on = 0;
69 
70 /* flag to update device allocation database for this device type */
71 static int update_devdb = 0;
72 
73 /*
74  * devices to be deallocated with -d :
75  *	audio, floppy, cd, floppy, tape, rmdisk.
76  */
77 static char *devalloc_list[10] = {DDI_NT_AUDIO, DDI_NT_CD, DDI_NT_CD_CHAN,
78 				    DDI_NT_FD, DDI_NT_TAPE, DDI_NT_BLOCK_CHAN,
79 				    DDI_NT_UGEN, DDI_NT_USB_ATTACHMENT_POINT,
80 				    DDI_NT_SCSI_NEXUS, NULL};
81 
82 /* list of allocatable devices */
83 static devlist_t devlist;
84 
85 /* load a single driver only.  set with -i */
86 static int single_drv = FALSE;
87 static char *driver = NULL;
88 
89 /* attempt to load drivers or defer attach nodes */
90 static int load_attach_drv = TRUE;
91 
92 /* reload all driver.conf files */
93 static int update_all_drivers = FALSE;
94 
95 /* set if invoked via /usr/lib/devfsadm/devfsadmd */
96 static int daemon_mode = FALSE;
97 
98 /* set if event_handler triggered */
99 int event_driven = FALSE;
100 
101 /* output directed to syslog during daemon mode if set */
102 static int logflag = FALSE;
103 
104 /* build links in /dev.  -x to turn off */
105 static int build_dev = TRUE;
106 
107 /* build nodes in /devices.  -y to turn off */
108 static int build_devices = TRUE;
109 
110 /* -z to turn off */
111 static int flush_path_to_inst_enable = TRUE;
112 
113 /* variables used for path_to_inst flushing */
114 static int inst_count = 0;
115 static mutex_t count_lock;
116 static cond_t cv;
117 
118 /* variables for minor_fini thread */
119 static mutex_t minor_fini_mutex;
120 static int minor_fini_canceled = TRUE;
121 static int minor_fini_delayed = FALSE;
122 static cond_t minor_fini_cv;
123 static int minor_fini_timeout = MINOR_FINI_TIMEOUT_DEFAULT;
124 
125 /* single-threads /dev modification */
126 static sema_t dev_sema;
127 
128 /* the program we were invoked as; ie argv[0] */
129 static char *prog;
130 
131 /* pointers to create/remove link lists */
132 static create_list_t *create_head = NULL;
133 static remove_list_t *remove_head = NULL;
134 
135 /*  supports the class -c option */
136 static char **classes = NULL;
137 static int num_classes = 0;
138 
139 /* used with verbose option -v or -V */
140 static int num_verbose = 0;
141 static char **verbose = NULL;
142 
143 static struct mperm *minor_perms = NULL;
144 static driver_alias_t *driver_aliases = NULL;
145 
146 /* set if -r alternate root given */
147 static char *root_dir = "";
148 
149 /* /devices or <rootdir>/devices */
150 static char *devices_dir  = DEVICES;
151 
152 /* /dev or <rootdir>/dev */
153 static char *dev_dir = DEV;
154 
155 /* /etc/dev or <rootdir>/etc/dev */
156 static char *etc_dev_dir = ETCDEV;
157 
158 /*
159  * writable root (for lock files and doors during install).
160  * This is also root dir for /dev attr dir during install.
161  */
162 static char *attr_root = NULL;
163 
164 /* /etc/path_to_inst unless -p used */
165 static char *inst_file = INSTANCE_FILE;
166 
167 /* /usr/lib/devfsadm/linkmods unless -l used */
168 static char *module_dirs = MODULE_DIRS;
169 
170 /* default uid/gid used if /etc/minor_perm entry not found */
171 static uid_t root_uid;
172 static gid_t sys_gid;
173 
174 /* /etc/devlink.tab unless devlinks -t used */
175 static char *devlinktab_file = NULL;
176 
177 /* File and data structure to reserve enumerate IDs */
178 static char *enumerate_file = ENUMERATE_RESERVED;
179 static enumerate_file_t *enumerate_reserved = NULL;
180 
181 /* set if /dev link is new. speeds up rm_stale_links */
182 static int linknew = TRUE;
183 
184 /* variables for devlink.tab compat processing */
185 static devlinktab_list_t *devlinktab_list = NULL;
186 static unsigned int devlinktab_line = 0;
187 
188 /* cache head for devfsadm_enumerate*() functions */
189 static numeral_set_t *head_numeral_set = NULL;
190 
191 /* list list of devfsadm modules */
192 static module_t *module_head = NULL;
193 
194 /* name_to_major list used in utility function */
195 static n2m_t *n2m_list = NULL;
196 
197 /* cache of some links used for performance */
198 static linkhead_t *headlinkhead = NULL;
199 
200 /* locking variables to prevent multiples writes to /dev */
201 static int hold_dev_lock = FALSE;
202 static int hold_daemon_lock = FALSE;
203 static int dev_lock_fd;
204 static int daemon_lock_fd;
205 static char dev_lockfile[PATH_MAX + 1];
206 static char daemon_lockfile[PATH_MAX + 1];
207 
208 /* last devinfo node/minor processed. used for performance */
209 static di_node_t lnode;
210 static di_minor_t lminor;
211 static char lphy_path[PATH_MAX + 1] = {""};
212 
213 /* Globals used by the link database */
214 static di_devlink_handle_t devlink_cache;
215 static int update_database = FALSE;
216 
217 /* Globals used to set logindev perms */
218 static struct login_dev *login_dev_cache = NULL;
219 static int login_dev_enable = FALSE;
220 
221 /* Global to use devinfo snapshot cache */
222 static int use_snapshot_cache = FALSE;
223 
224 /* Global for no-further-processing hash */
225 static item_t **nfp_hash;
226 static mutex_t  nfp_mutex = DEFAULTMUTEX;
227 
228 /*
229  * Packaged directories - not removed even when empty.
230  * The dirs must be listed in canonical form
231  * i.e. without leading "/dev/"
232  */
233 static char *packaged_dirs[] =
234 	{"dsk", "rdsk", "term", NULL};
235 
236 /* Devname globals */
237 static int lookup_door_fd = -1;
238 static char *lookup_door_path;
239 
240 static void load_dev_acl(void);
241 static void update_drvconf(major_t, int);
242 static void check_reconfig_state(void);
243 static int s_stat(const char *, struct stat *);
244 
245 static int is_blank(char *);
246 
247 /* sysevent queue related globals */
248 static mutex_t  syseventq_mutex = DEFAULTMUTEX;
249 static syseventq_t *syseventq_front;
250 static syseventq_t *syseventq_back;
251 static void process_syseventq();
252 
253 static di_node_t devi_root_node = DI_NODE_NIL;
254 
255 int
256 main(int argc, char *argv[])
257 {
258 	struct passwd *pw;
259 	struct group *gp;
260 	pid_t pid;
261 
262 	(void) setlocale(LC_ALL, "");
263 	(void) textdomain(TEXT_DOMAIN);
264 
265 	if ((prog = strrchr(argv[0], '/')) == NULL) {
266 		prog = argv[0];
267 	} else {
268 		prog++;
269 	}
270 
271 	if (getuid() != 0) {
272 		err_print(MUST_BE_ROOT);
273 		devfsadm_exit(1);
274 		/*NOTREACHED*/
275 	}
276 
277 	if (getzoneid() != GLOBAL_ZONEID) {
278 		err_print(MUST_BE_GLOBAL_ZONE);
279 		devfsadm_exit(1);
280 	}
281 
282 	/*
283 	 * Close all files except stdin/stdout/stderr
284 	 */
285 	closefrom(3);
286 
287 	if ((pw = getpwnam(DEFAULT_DEV_USER)) != NULL) {
288 		root_uid = pw->pw_uid;
289 	} else {
290 		err_print(CANT_FIND_USER, DEFAULT_DEV_USER);
291 		root_uid = (uid_t)0;	/* assume 0 is root */
292 	}
293 
294 	/* the default group is sys */
295 
296 	if ((gp = getgrnam(DEFAULT_DEV_GROUP)) != NULL) {
297 		sys_gid = gp->gr_gid;
298 	} else {
299 		err_print(CANT_FIND_GROUP, DEFAULT_DEV_GROUP);
300 		sys_gid = (gid_t)3;	/* assume 3 is sys */
301 	}
302 
303 	(void) umask(0);
304 
305 	system_labeled = is_system_labeled();
306 	if (system_labeled == FALSE) {
307 		/*
308 		 * is_system_labeled() will return false in case we are
309 		 * starting before the first reboot after Trusted Extensions
310 		 * is enabled.  Check the setting in /etc/system to see if
311 		 * TX is enabled (even if not yet booted).
312 		 */
313 		if (defopen("/etc/system") == 0) {
314 			if (defread("set sys_labeling=1") != NULL)
315 				system_labeled = TRUE;
316 
317 			/* close defaults file */
318 			(void) defopen(NULL);
319 		}
320 	}
321 	/*
322 	 * Check if device allocation is enabled.
323 	 */
324 	devalloc_is_on = (da_is_on() == 1) ? 1 : 0;
325 
326 #ifdef DEBUG
327 	if (system_labeled == FALSE) {
328 		struct stat tx_stat;
329 
330 		/* test hook: see also mkdevalloc.c and allocate.c */
331 		system_labeled = is_system_labeled_debug(&tx_stat);
332 	}
333 #endif
334 
335 	parse_args(argc, argv);
336 
337 	(void) sema_init(&dev_sema, 1, USYNC_THREAD, NULL);
338 
339 	/* Initialize device allocation list */
340 	devlist.audio = devlist.cd = devlist.floppy = devlist.tape =
341 	    devlist.rmdisk = NULL;
342 
343 	if (daemon_mode == TRUE) {
344 		/*
345 		 * Build /dev and /devices before daemonizing if
346 		 * reconfig booting and daemon invoked with alternate
347 		 * root. This is to support install.
348 		 */
349 		if (getenv(RECONFIG_BOOT) != NULL && root_dir[0] != '\0') {
350 			vprint(INFO_MID, CONFIGURING);
351 			load_dev_acl();
352 			update_drvconf((major_t)-1, 0);
353 			process_devinfo_tree();
354 			(void) modctl(MODSETMINIROOT);
355 		}
356 
357 		/*
358 		 * fork before detaching from tty in order to print error
359 		 * message if unable to acquire file lock.  locks not preserved
360 		 * across forks.  Even under debug we want to fork so that
361 		 * when executed at boot we don't hang.
362 		 */
363 		if (fork() != 0) {
364 			devfsadm_exit(0);
365 			/*NOTREACHED*/
366 		}
367 
368 		/* set directory to / so it coredumps there */
369 		if (chdir("/") == -1) {
370 			err_print(CHROOT_FAILED, strerror(errno));
371 		}
372 
373 		/* only one daemon can run at a time */
374 		if ((pid = enter_daemon_lock()) == getpid()) {
375 			detachfromtty();
376 			(void) cond_init(&cv, USYNC_THREAD, 0);
377 			(void) mutex_init(&count_lock, USYNC_THREAD, 0);
378 			if (thr_create(NULL, NULL,
379 			    (void *(*)(void *))instance_flush_thread,
380 			    NULL, THR_DETACHED, NULL) != 0) {
381 				err_print(CANT_CREATE_THREAD, "daemon",
382 				    strerror(errno));
383 				devfsadm_exit(1);
384 				/*NOTREACHED*/
385 			}
386 
387 			/* start the minor_fini_thread */
388 			(void) mutex_init(&minor_fini_mutex, USYNC_THREAD, 0);
389 			(void) cond_init(&minor_fini_cv, USYNC_THREAD, 0);
390 			if (thr_create(NULL, NULL,
391 			    (void *(*)(void *))minor_fini_thread,
392 			    NULL, THR_DETACHED, NULL)) {
393 				err_print(CANT_CREATE_THREAD, "minor_fini",
394 				    strerror(errno));
395 				devfsadm_exit(1);
396 				/*NOTREACHED*/
397 			}
398 
399 
400 			/*
401 			 * logindevperms need only be set
402 			 * in daemon mode and when root dir is "/".
403 			 */
404 			if (root_dir[0] == '\0')
405 				login_dev_enable = TRUE;
406 			daemon_update();
407 			devfsadm_exit(0);
408 			/*NOTREACHED*/
409 		} else {
410 			err_print(DAEMON_RUNNING, pid);
411 			devfsadm_exit(1);
412 			/*NOTREACHED*/
413 		}
414 	} else {
415 		/* not a daemon, so just build /dev and /devices */
416 
417 		/*
418 		 * If turning off device allocation, load the
419 		 * minor_perm file because process_devinfo_tree() will
420 		 * need this in order to reset the permissions of the
421 		 * device files.
422 		 */
423 		if (devalloc_flag == DA_OFF) {
424 			read_minor_perm_file();
425 		}
426 
427 		process_devinfo_tree();
428 		if (devalloc_flag != 0)
429 			/* Enable/disable device allocation */
430 			_reset_devalloc(devalloc_flag);
431 	}
432 	return (0);
433 }
434 
435 static void
436 update_drvconf(major_t major, int flags)
437 {
438 	if (modctl(MODLOADDRVCONF, major, flags) != 0)
439 		err_print(gettext("update_drvconf failed for major %d\n"),
440 		    major);
441 }
442 
443 static void
444 load_dev_acl()
445 {
446 	if (load_devpolicy() != 0)
447 		err_print(gettext("device policy load failed\n"));
448 	load_minor_perm_file();
449 }
450 
451 /*
452  * As devfsadm is run early in boot to provide the kernel with
453  * minor_perm info, we might as well check for reconfig at the
454  * same time to avoid running devfsadm twice.  This gets invoked
455  * earlier than the env variable RECONFIG_BOOT is set up.
456  */
457 static void
458 check_reconfig_state()
459 {
460 	struct stat sb;
461 
462 	if (s_stat("/reconfigure", &sb) == 0) {
463 		(void) modctl(MODDEVNAME, MODDEVNAME_RECONFIG, 0);
464 	}
465 }
466 
467 static void
468 modctl_sysavail()
469 {
470 	/*
471 	 * Inform /dev that system is available, that
472 	 * implicit reconfig can now be performed.
473 	 */
474 	(void) modctl(MODDEVNAME, MODDEVNAME_SYSAVAIL, 0);
475 }
476 
477 static void
478 set_lock_root(void)
479 {
480 	struct stat sb;
481 	char *lock_root;
482 	size_t len;
483 
484 	lock_root = attr_root ? attr_root : root_dir;
485 
486 	len = strlen(lock_root) + strlen(ETCDEV) + 1;
487 	etc_dev_dir = s_malloc(len);
488 	(void) snprintf(etc_dev_dir, len, "%s%s", lock_root, ETCDEV);
489 
490 	if (s_stat(etc_dev_dir, &sb) != 0) {
491 		s_mkdirp(etc_dev_dir, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
492 	} else if (!S_ISDIR(sb.st_mode)) {
493 		err_print(NOT_DIR, etc_dev_dir);
494 		devfsadm_exit(1);
495 		/*NOTREACHED*/
496 	}
497 }
498 
499 
500 /*
501  * Parse arguments for all 6 programs handled from devfsadm.
502  */
503 static void
504 parse_args(int argc, char *argv[])
505 {
506 	char opt;
507 	char get_linkcompat_opts = FALSE;
508 	char *compat_class;
509 	int num_aliases = 0;
510 	int len;
511 	int retval;
512 	int config = TRUE;
513 	int bind = FALSE;
514 	int force_flag = FALSE;
515 	struct aliases *ap = NULL;
516 	struct aliases *a_head = NULL;
517 	struct aliases *a_tail = NULL;
518 	struct modconfig mc;
519 
520 	(void) bzero(&mc, sizeof (mc));
521 
522 	if (strcmp(prog, DISKS) == 0) {
523 		compat_class = "disk";
524 		get_linkcompat_opts = TRUE;
525 
526 	} else if (strcmp(prog, TAPES) == 0) {
527 		compat_class = "tape";
528 		get_linkcompat_opts = TRUE;
529 
530 	} else if (strcmp(prog, PORTS) == 0) {
531 		compat_class = "port";
532 		get_linkcompat_opts = TRUE;
533 
534 	} else if (strcmp(prog, AUDLINKS) == 0) {
535 		compat_class = "audio";
536 		get_linkcompat_opts = TRUE;
537 
538 	} else if (strcmp(prog, DEVLINKS) == 0) {
539 		devlinktab_file = DEVLINKTAB_FILE;
540 
541 		build_devices = FALSE;
542 		load_attach_drv = FALSE;
543 
544 		while ((opt = getopt(argc, argv, "dnr:st:vV:")) != EOF) {
545 			switch (opt) {
546 			case 'd':
547 				file_mods = FALSE;
548 				flush_path_to_inst_enable = FALSE;
549 				devlinks_debug = TRUE;
550 				break;
551 			case 'n':
552 				/* prevent driver loading and deferred attach */
553 				load_attach_drv = FALSE;
554 				break;
555 			case 'r':
556 				set_root_devices_dev_dir(optarg);
557 				if (zone_pathcheck(root_dir) !=
558 				    DEVFSADM_SUCCESS)
559 					devfsadm_exit(1);
560 					/*NOTREACHED*/
561 				break;
562 			case 's':
563 				/*
564 				 * suppress.  don't create/remove links/nodes
565 				 * useful with -v or -V
566 				 */
567 				file_mods = FALSE;
568 				flush_path_to_inst_enable = FALSE;
569 				break;
570 			case 't':
571 				/* supply a non-default table file */
572 				devlinktab_file = optarg;
573 				break;
574 			case 'v':
575 				/* documented verbose flag */
576 				add_verbose_id(VERBOSE_MID);
577 				break;
578 			case 'V':
579 				/* undocumented for extra verbose levels */
580 				add_verbose_id(optarg);
581 				break;
582 			default:
583 				usage();
584 				break;
585 			}
586 		}
587 
588 		if (optind < argc) {
589 			usage();
590 		}
591 
592 	} else if (strcmp(prog, DRVCONFIG) == 0) {
593 		int update_only = 0;
594 		build_dev = FALSE;
595 
596 		while ((opt =
597 		    getopt(argc, argv, "a:bc:dfi:m:np:R:r:suvV:x")) != EOF) {
598 			switch (opt) {
599 			case 'a':
600 				ap = calloc(sizeof (struct aliases), 1);
601 				ap->a_name = dequote(optarg);
602 				len = strlen(ap->a_name) + 1;
603 				if (len > MAXMODCONFNAME) {
604 					err_print(ALIAS_TOO_LONG,
605 					    MAXMODCONFNAME, ap->a_name);
606 					devfsadm_exit(1);
607 					/*NOTREACHED*/
608 				}
609 				ap->a_len = len;
610 				if (a_tail == NULL) {
611 					a_head = ap;
612 				} else {
613 					a_tail->a_next = ap;
614 				}
615 				a_tail = ap;
616 				num_aliases++;
617 				bind = TRUE;
618 				break;
619 			case 'b':
620 				bind = TRUE;
621 				break;
622 			case 'c':
623 				(void) strcpy(mc.drvclass, optarg);
624 				break;
625 			case 'd':
626 				/*
627 				 * need to keep for compatibility, but
628 				 * do nothing.
629 				 */
630 				break;
631 			case 'f':
632 				force_flag = TRUE;
633 				break;
634 			case 'i':
635 				single_drv = TRUE;
636 				(void) strcpy(mc.drvname, optarg);
637 				driver = s_strdup(optarg);
638 				break;
639 			case 'm':
640 				mc.major = atoi(optarg);
641 				break;
642 			case 'n':
643 				/* prevent driver loading and deferred attach */
644 				load_attach_drv = FALSE;
645 				break;
646 			case 'p':
647 				/* specify alternate path_to_inst file */
648 				inst_file = s_strdup(optarg);
649 				break;
650 			case 'R':
651 				/*
652 				 * Private flag for suninstall to populate
653 				 * device information on the installed root.
654 				 */
655 				root_dir = s_strdup(optarg);
656 				if (zone_pathcheck(root_dir) !=
657 				    DEVFSADM_SUCCESS)
658 				devfsadm_exit(devfsadm_copy());
659 				/*NOTREACHED*/
660 				break;
661 			case 'r':
662 				devices_dir = s_strdup(optarg);
663 				if (zone_pathcheck(devices_dir) !=
664 				    DEVFSADM_SUCCESS)
665 					devfsadm_exit(1);
666 					/*NOTREACHED*/
667 				break;
668 			case 's':
669 				/*
670 				 * suppress.  don't create nodes
671 				 * useful with -v or -V
672 				 */
673 				file_mods = FALSE;
674 				flush_path_to_inst_enable = FALSE;
675 				break;
676 			case 'u':
677 				/*
678 				 * Invoked via update_drv(1m) to update
679 				 * the kernel's driver/alias binding
680 				 * when removing one or more aliases.
681 				 */
682 				config = FALSE;
683 				break;
684 			case 'v':
685 				/* documented verbose flag */
686 				add_verbose_id(VERBOSE_MID);
687 				break;
688 			case 'V':
689 				/* undocumented for extra verbose levels */
690 				add_verbose_id(optarg);
691 				break;
692 			case 'x':
693 				update_only = 1;
694 				break;
695 			default:
696 				usage();
697 			}
698 		}
699 
700 		if (optind < argc) {
701 			usage();
702 		}
703 
704 		if (bind == TRUE) {
705 			if ((mc.major == -1) || (mc.drvname[0] == NULL)) {
706 				err_print(MAJOR_AND_B_FLAG);
707 				devfsadm_exit(1);
708 				/*NOTREACHED*/
709 			}
710 			mc.flags = 0;
711 			if (force_flag)
712 				mc.flags |= MOD_UNBIND_OVERRIDE;
713 			if (update_only)
714 				mc.flags |= MOD_ADDMAJBIND_UPDATE;
715 			mc.num_aliases = num_aliases;
716 			mc.ap = a_head;
717 			retval =  modctl((config == TRUE) ? MODADDMAJBIND :
718 			    MODREMDRVALIAS, NULL, (caddr_t)&mc);
719 			if (retval < 0) {
720 				err_print((config == TRUE) ? MODCTL_ADDMAJBIND :
721 				    MODCTL_REMMAJBIND);
722 			}
723 			devfsadm_exit(retval);
724 			/*NOTREACHED*/
725 		}
726 
727 	} else if ((strcmp(prog, DEVFSADM) == 0) ||
728 	    (strcmp(prog, DEVFSADMD) == 0)) {
729 		char *zonename = NULL;
730 		int init_drvconf = 0;
731 		int init_perm = 0;
732 		int public_mode = 0;
733 		int init_sysavail = 0;
734 
735 		if (strcmp(prog, DEVFSADMD) == 0) {
736 			daemon_mode = TRUE;
737 		}
738 
739 		devlinktab_file = DEVLINKTAB_FILE;
740 
741 		while ((opt = getopt(argc, argv,
742 		    "a:Cc:deIi:l:np:PR:r:sSt:uvV:x:")) != EOF) {
743 			if (opt == 'I' || opt == 'P' || opt == 'S') {
744 				if (public_mode)
745 					usage();
746 			} else {
747 				if (init_perm || init_drvconf || init_sysavail)
748 					usage();
749 				public_mode = 1;
750 			}
751 			switch (opt) {
752 			case 'a':
753 				attr_root = s_strdup(optarg);
754 				break;
755 			case 'C':
756 				cleanup = TRUE;
757 				break;
758 			case 'c':
759 				num_classes++;
760 				classes = s_realloc(classes,
761 				    num_classes * sizeof (char *));
762 				classes[num_classes - 1] = optarg;
763 				break;
764 			case 'd':
765 				if (daemon_mode == FALSE) {
766 					/*
767 					 * Device allocation to be disabled.
768 					 */
769 					devalloc_flag = DA_OFF;
770 					build_dev = FALSE;
771 				}
772 				break;
773 			case 'e':
774 				if (daemon_mode == FALSE) {
775 					/*
776 					 * Device allocation to be enabled.
777 					 */
778 					devalloc_flag = DA_ON;
779 					build_dev = FALSE;
780 				}
781 				break;
782 			case 'I':	/* update kernel driver.conf cache */
783 				if (daemon_mode == TRUE)
784 					usage();
785 				init_drvconf = 1;
786 				break;
787 			case 'i':
788 				single_drv = TRUE;
789 				driver = s_strdup(optarg);
790 				break;
791 			case 'l':
792 				/* specify an alternate module load path */
793 				module_dirs = s_strdup(optarg);
794 				break;
795 			case 'n':
796 				/* prevent driver loading and deferred attach */
797 				load_attach_drv = FALSE;
798 				break;
799 			case 'p':
800 				/* specify alternate path_to_inst file */
801 				inst_file = s_strdup(optarg);
802 				break;
803 			case 'P':
804 				if (daemon_mode == TRUE)
805 					usage();
806 				/* load minor_perm and device_policy */
807 				init_perm = 1;
808 				break;
809 			case 'R':
810 				/*
811 				 * Private flag for suninstall to populate
812 				 * device information on the installed root.
813 				 */
814 				root_dir = s_strdup(optarg);
815 				devfsadm_exit(devfsadm_copy());
816 				/*NOTREACHED*/
817 				break;
818 			case 'r':
819 				set_root_devices_dev_dir(optarg);
820 				break;
821 			case 's':
822 				/*
823 				 * suppress. don't create/remove links/nodes
824 				 * useful with -v or -V
825 				 */
826 				file_mods = FALSE;
827 				flush_path_to_inst_enable = FALSE;
828 				break;
829 			case 'S':
830 				if (daemon_mode == TRUE)
831 					usage();
832 				init_sysavail = 1;
833 				break;
834 			case 't':
835 				devlinktab_file = optarg;
836 				break;
837 			case 'u':	/* complete configuration after */
838 					/* adding a driver update-only */
839 				if (daemon_mode == TRUE)
840 					usage();
841 				update_all_drivers = TRUE;
842 				break;
843 			case 'v':
844 				/* documented verbose flag */
845 				add_verbose_id(VERBOSE_MID);
846 				break;
847 			case 'V':
848 				/* undocumented: specify verbose lvl */
849 				add_verbose_id(optarg);
850 				break;
851 			case 'x':
852 				/*
853 				 * x is the "private switch" option.  The
854 				 * goal is to not suck up all the other
855 				 * option letters.
856 				 */
857 				if (strcmp(optarg, "update_devlinksdb") == 0) {
858 					update_database = TRUE;
859 				} else if (strcmp(optarg, "no_dev") == 0) {
860 					/* don't build /dev */
861 					build_dev = FALSE;
862 				} else if (strcmp(optarg, "no_devices") == 0) {
863 					/* don't build /devices */
864 					build_devices = FALSE;
865 				} else if (strcmp(optarg, "no_p2i") == 0) {
866 					/* don't flush path_to_inst */
867 					flush_path_to_inst_enable = FALSE;
868 				} else if (strcmp(optarg, "use_dicache") == 0) {
869 					use_snapshot_cache = TRUE;
870 				} else {
871 					usage();
872 				}
873 				break;
874 			default:
875 				usage();
876 				break;
877 			}
878 		}
879 		if (optind < argc) {
880 			usage();
881 		}
882 
883 		/*
884 		 * We're not in zone mode; Check to see if the rootpath
885 		 * collides with any zonepaths.
886 		 */
887 		if (zonename == NULL) {
888 			if (zone_pathcheck(root_dir) != DEVFSADM_SUCCESS)
889 				devfsadm_exit(1);
890 				/*NOTREACHED*/
891 		}
892 
893 		if (init_drvconf || init_perm || init_sysavail) {
894 			/*
895 			 * Load minor perm before force-loading drivers
896 			 * so the correct permissions are picked up.
897 			 */
898 			if (init_perm) {
899 				check_reconfig_state();
900 				load_dev_acl();
901 			}
902 			if (init_drvconf)
903 				update_drvconf((major_t)-1, 0);
904 			if (init_sysavail)
905 				modctl_sysavail();
906 			devfsadm_exit(0);
907 			/*NOTREACHED*/
908 		}
909 	}
910 
911 
912 	if (get_linkcompat_opts == TRUE) {
913 
914 		build_devices = FALSE;
915 		load_attach_drv = FALSE;
916 		num_classes++;
917 		classes = s_realloc(classes, num_classes *
918 		    sizeof (char *));
919 		classes[num_classes - 1] = compat_class;
920 
921 		while ((opt = getopt(argc, argv, "Cnr:svV:")) != EOF) {
922 			switch (opt) {
923 			case 'C':
924 				cleanup = TRUE;
925 				break;
926 			case 'n':
927 				/* prevent driver loading or deferred attach */
928 				load_attach_drv = FALSE;
929 				break;
930 			case 'r':
931 				set_root_devices_dev_dir(optarg);
932 				if (zone_pathcheck(root_dir) !=
933 				    DEVFSADM_SUCCESS)
934 					devfsadm_exit(1);
935 					/*NOTREACHED*/
936 				break;
937 			case 's':
938 				/* suppress.  don't create/remove links/nodes */
939 				/* useful with -v or -V */
940 				file_mods = FALSE;
941 				flush_path_to_inst_enable = FALSE;
942 				break;
943 			case 'v':
944 				/* documented verbose flag */
945 				add_verbose_id(VERBOSE_MID);
946 				break;
947 			case 'V':
948 				/* undocumented for extra verbose levels */
949 				add_verbose_id(optarg);
950 				break;
951 			default:
952 				usage();
953 			}
954 		}
955 		if (optind < argc) {
956 			usage();
957 		}
958 	}
959 	set_lock_root();
960 }
961 
962 void
963 usage(void)
964 {
965 	if (strcmp(prog, DEVLINKS) == 0) {
966 		err_print(DEVLINKS_USAGE);
967 	} else if (strcmp(prog, DRVCONFIG) == 0) {
968 		err_print(DRVCONFIG_USAGE);
969 	} else if ((strcmp(prog, DEVFSADM) == 0) ||
970 	    (strcmp(prog, DEVFSADMD) == 0)) {
971 		err_print(DEVFSADM_USAGE);
972 	} else {
973 		err_print(COMPAT_LINK_USAGE);
974 	}
975 
976 	devfsadm_exit(1);
977 	/*NOTREACHED*/
978 }
979 
980 static void
981 devi_tree_walk(struct dca_impl *dcip, int flags, char *ev_subclass)
982 {
983 	char *msg, *name;
984 	struct mlist	mlist = {0};
985 	di_node_t	node;
986 
987 	vprint(CHATTY_MID, "devi_tree_walk: root=%s, minor=%s, driver=%s,"
988 	    " error=%d, flags=%u\n", dcip->dci_root,
989 	    dcip->dci_minor ? dcip->dci_minor : "<NULL>",
990 	    dcip->dci_driver ? dcip->dci_driver : "<NULL>", dcip->dci_error,
991 	    dcip->dci_flags);
992 
993 	assert(dcip->dci_root);
994 
995 	if (dcip->dci_flags & DCA_LOAD_DRV) {
996 		node = di_init_driver(dcip->dci_driver, flags);
997 		msg = DRIVER_FAILURE;
998 		name = dcip->dci_driver;
999 	} else {
1000 		node = di_init(dcip->dci_root, flags);
1001 		msg = DI_INIT_FAILED;
1002 		name = dcip->dci_root;
1003 	}
1004 
1005 	if (node == DI_NODE_NIL) {
1006 		dcip->dci_error = errno;
1007 		/*
1008 		 * Rapid hotplugging (commonly seen during USB testing),
1009 		 * may remove a device before the create event for it
1010 		 * has been processed. To prevent alarming users with
1011 		 * a superfluous message, we suppress error messages
1012 		 * for ENXIO and hotplug.
1013 		 */
1014 		if (!(errno == ENXIO && (dcip->dci_flags & DCA_HOT_PLUG)))
1015 			err_print(msg, name, strerror(dcip->dci_error));
1016 		return;
1017 	}
1018 
1019 	if (dcip->dci_flags & DCA_FLUSH_PATHINST)
1020 		flush_path_to_inst();
1021 
1022 	dcip->dci_arg = &mlist;
1023 	devi_root_node = node;	/* protected by lock_dev() */
1024 
1025 	vprint(CHATTY_MID, "walking device tree\n");
1026 
1027 	(void) di_walk_minor(node, NULL, DI_CHECK_ALIAS, dcip,
1028 	    check_minor_type);
1029 
1030 	process_deferred_links(dcip, DCA_CREATE_LINK);
1031 
1032 	dcip->dci_arg = NULL;
1033 
1034 	/*
1035 	 * Finished creating devfs files and dev links.
1036 	 * Log sysevent.
1037 	 */
1038 	if (ev_subclass)
1039 		build_and_enq_event(EC_DEV_ADD, ev_subclass, dcip->dci_root,
1040 		    node, dcip->dci_minor);
1041 
1042 	/* Add new device to device allocation database */
1043 	if (system_labeled && update_devdb) {
1044 		_update_devalloc_db(&devlist, 0, DA_ADD, NULL, root_dir);
1045 		update_devdb = 0;
1046 	}
1047 
1048 	devi_root_node = DI_NODE_NIL;	/* protected by lock_dev() */
1049 	di_fini(node);
1050 }
1051 
1052 static void
1053 process_deferred_links(struct dca_impl *dcip, int flags)
1054 {
1055 	struct mlist	*dep;
1056 	struct minor	*mp, *smp;
1057 
1058 	vprint(CHATTY_MID, "processing deferred links\n");
1059 
1060 	dep = dcip->dci_arg;
1061 
1062 	/*
1063 	 * The list head is not used during the deferred create phase
1064 	 */
1065 	dcip->dci_arg = NULL;
1066 
1067 	assert(dep);
1068 	assert((dep->head == NULL) ^ (dep->tail != NULL));
1069 	assert(flags == DCA_FREE_LIST || flags == DCA_CREATE_LINK);
1070 
1071 	for (smp = NULL, mp = dep->head; mp; mp = mp->next) {
1072 		if (flags == DCA_CREATE_LINK)
1073 			(void) check_minor_type(mp->node, mp->minor, dcip);
1074 		free(smp);
1075 		smp = mp;
1076 	}
1077 
1078 	free(smp);
1079 }
1080 
1081 /*
1082  * Called in non-daemon mode to take a snap shot of the devinfo tree.
1083  * Then it calls the appropriate functions to build /devices and /dev.
1084  * It also flushes path_to_inst.
1085  * Except in the devfsadm -i (single driver case), the flags used by devfsadm
1086  * needs to match DI_CACHE_SNAPSHOT_FLAGS. That will make DINFOCACHE snapshot
1087  * updated.
1088  */
1089 void
1090 process_devinfo_tree()
1091 {
1092 	uint_t		flags;
1093 	struct dca_impl	dci;
1094 	char		name[MAXNAMELEN];
1095 	char		*fcn = "process_devinfo_tree: ";
1096 
1097 	vprint(CHATTY_MID, "%senter\n", fcn);
1098 
1099 	dca_impl_init("/", NULL, &dci);
1100 
1101 	lock_dev();
1102 
1103 	/*
1104 	 * Update kernel driver.conf cache when devfsadm/drvconfig
1105 	 * is invoked to build /devices and /dev.
1106 	 */
1107 	if (update_all_drivers || load_attach_drv) {
1108 		update_drvconf((major_t)-1,
1109 		    update_all_drivers ? MOD_LOADDRVCONF_RECONF : 0);
1110 	}
1111 
1112 	if (single_drv == TRUE) {
1113 		/*
1114 		 * load a single driver, but walk the entire devinfo tree
1115 		 */
1116 		if (load_attach_drv == FALSE)
1117 			err_print(DRV_LOAD_REQD);
1118 
1119 		vprint(CHATTY_MID, "%sattaching driver (%s)\n", fcn, driver);
1120 
1121 		dci.dci_flags |= DCA_LOAD_DRV;
1122 		(void) snprintf(name, sizeof (name), "%s", driver);
1123 		dci.dci_driver = name;
1124 		flags = DINFOCPYALL | DINFOPATH;
1125 
1126 	} else if (load_attach_drv == TRUE) {
1127 		/*
1128 		 * Load and attach all drivers, then walk the entire tree.
1129 		 * If the cache flag is set, use DINFOCACHE to get cached
1130 		 * data.
1131 		 */
1132 		if (use_snapshot_cache == TRUE) {
1133 			flags = DINFOCACHE;
1134 			vprint(CHATTY_MID, "%susing snapshot cache\n", fcn);
1135 		} else {
1136 			vprint(CHATTY_MID, "%sattaching all drivers\n", fcn);
1137 			flags = DI_CACHE_SNAPSHOT_FLAGS;
1138 			if (cleanup) {
1139 				/*
1140 				 * remove dangling entries from /etc/devices
1141 				 * files.
1142 				 */
1143 				flags |= DINFOCLEANUP;
1144 			}
1145 		}
1146 	} else {
1147 		/*
1148 		 * For devlinks, disks, ports, tapes and devfsadm -n,
1149 		 * just need to take a snapshot with active devices.
1150 		 */
1151 		vprint(CHATTY_MID, "%staking snapshot of active devices\n",
1152 		    fcn);
1153 		flags = DINFOCPYALL;
1154 	}
1155 
1156 	if (((load_attach_drv == TRUE) || (single_drv == TRUE)) &&
1157 	    (build_devices == TRUE)) {
1158 		dci.dci_flags |= DCA_FLUSH_PATHINST;
1159 	}
1160 
1161 	/* handle pre-cleanup operations desired by the modules. */
1162 	pre_and_post_cleanup(RM_PRE);
1163 
1164 	devi_tree_walk(&dci, flags, NULL);
1165 
1166 	if (dci.dci_error) {
1167 		devfsadm_exit(1);
1168 		/*NOTREACHED*/
1169 	}
1170 
1171 	/* handle post-cleanup operations desired by the modules. */
1172 	pre_and_post_cleanup(RM_POST);
1173 
1174 	unlock_dev(SYNC_STATE);
1175 }
1176 
1177 /*ARGSUSED*/
1178 static void
1179 print_cache_signal(int signo)
1180 {
1181 	if (signal(SIGUSR1, print_cache_signal) == SIG_ERR) {
1182 		err_print("signal SIGUSR1 failed: %s\n", strerror(errno));
1183 		devfsadm_exit(1);
1184 		/*NOTREACHED*/
1185 	}
1186 }
1187 
1188 static void
1189 revoke_lookup_door(void)
1190 {
1191 	if (lookup_door_fd != -1) {
1192 		if (door_revoke(lookup_door_fd) == -1) {
1193 			err_print("door_revoke of %s failed - %s\n",
1194 			    lookup_door_path, strerror(errno));
1195 		}
1196 	}
1197 }
1198 
1199 /*ARGSUSED*/
1200 static void
1201 catch_exit(int signo)
1202 {
1203 	revoke_lookup_door();
1204 }
1205 
1206 /*
1207  * Register with eventd for messages. Create doors for synchronous
1208  * link creation.
1209  */
1210 static void
1211 daemon_update(void)
1212 {
1213 	int fd;
1214 	char *fcn = "daemon_update: ";
1215 	char door_file[MAXPATHLEN];
1216 	const char *subclass_list;
1217 	sysevent_handle_t *sysevent_hp;
1218 	vprint(CHATTY_MID, "%senter\n", fcn);
1219 
1220 	if (signal(SIGUSR1, print_cache_signal) == SIG_ERR) {
1221 		err_print("signal SIGUSR1 failed: %s\n", strerror(errno));
1222 		devfsadm_exit(1);
1223 		/*NOTREACHED*/
1224 	}
1225 	if (signal(SIGTERM, catch_exit) == SIG_ERR) {
1226 		err_print("signal SIGTERM failed: %s\n", strerror(errno));
1227 		devfsadm_exit(1);
1228 		/*NOTREACHED*/
1229 	}
1230 
1231 	if (snprintf(door_file, sizeof (door_file),
1232 	    "%s%s", attr_root ? attr_root : root_dir, DEVFSADM_SERVICE_DOOR)
1233 	    >= sizeof (door_file)) {
1234 		err_print("update_daemon failed to open sysevent service "
1235 		    "door\n");
1236 		devfsadm_exit(1);
1237 		/*NOTREACHED*/
1238 	}
1239 	if ((sysevent_hp = sysevent_open_channel_alt(
1240 	    door_file)) == NULL) {
1241 		err_print(CANT_CREATE_DOOR,
1242 		    door_file, strerror(errno));
1243 		devfsadm_exit(1);
1244 		/*NOTREACHED*/
1245 	}
1246 	if (sysevent_bind_subscriber(sysevent_hp, event_handler) != 0) {
1247 		err_print(CANT_CREATE_DOOR,
1248 		    door_file, strerror(errno));
1249 		(void) sysevent_close_channel(sysevent_hp);
1250 		devfsadm_exit(1);
1251 		/*NOTREACHED*/
1252 	}
1253 	subclass_list = EC_SUB_ALL;
1254 	if (sysevent_register_event(sysevent_hp, EC_ALL, &subclass_list, 1)
1255 	    != 0) {
1256 		err_print(CANT_CREATE_DOOR,
1257 		    door_file, strerror(errno));
1258 		(void) sysevent_unbind_subscriber(sysevent_hp);
1259 		(void) sysevent_close_channel(sysevent_hp);
1260 		devfsadm_exit(1);
1261 		/*NOTREACHED*/
1262 	}
1263 	if (snprintf(door_file, sizeof (door_file), "%s/%s",
1264 	    etc_dev_dir, DEVFSADM_SYNCH_DOOR) >= sizeof (door_file)) {
1265 		err_print(CANT_CREATE_DOOR, DEVFSADM_SYNCH_DOOR,
1266 		    strerror(ENAMETOOLONG));
1267 		devfsadm_exit(1);
1268 		/*NOTREACHED*/
1269 	}
1270 
1271 	(void) s_unlink(door_file);
1272 	if ((fd = open(door_file, O_RDWR | O_CREAT, SYNCH_DOOR_PERMS)) == -1) {
1273 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
1274 		devfsadm_exit(1);
1275 		/*NOTREACHED*/
1276 	}
1277 	(void) close(fd);
1278 
1279 	if ((fd = door_create(sync_handler, NULL,
1280 	    DOOR_REFUSE_DESC | DOOR_NO_CANCEL)) == -1) {
1281 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
1282 		(void) s_unlink(door_file);
1283 		devfsadm_exit(1);
1284 		/*NOTREACHED*/
1285 	}
1286 
1287 	if (fattach(fd, door_file) == -1) {
1288 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
1289 		(void) s_unlink(door_file);
1290 		devfsadm_exit(1);
1291 		/*NOTREACHED*/
1292 	}
1293 
1294 	/*
1295 	 * devname_lookup_door
1296 	 */
1297 	if (snprintf(door_file, sizeof (door_file), "%s/%s",
1298 	    etc_dev_dir, DEVNAME_LOOKUP_DOOR) >= sizeof (door_file)) {
1299 		err_print(CANT_CREATE_DOOR, DEVNAME_LOOKUP_DOOR,
1300 		    strerror(ENAMETOOLONG));
1301 		devfsadm_exit(1);
1302 		/*NOTREACHED*/
1303 	}
1304 
1305 	(void) s_unlink(door_file);
1306 	if ((fd = open(door_file, O_RDWR | O_CREAT, S_IRUSR|S_IWUSR)) == -1) {
1307 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
1308 		devfsadm_exit(1);
1309 		/*NOTREACHED*/
1310 	}
1311 	(void) close(fd);
1312 
1313 	if ((fd = door_create(devname_lookup_handler, NULL,
1314 	    DOOR_REFUSE_DESC)) == -1) {
1315 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
1316 		(void) s_unlink(door_file);
1317 		devfsadm_exit(1);
1318 		/*NOTREACHED*/
1319 	}
1320 
1321 	(void) fdetach(door_file);
1322 	lookup_door_path = s_strdup(door_file);
1323 retry:
1324 	if (fattach(fd, door_file) == -1) {
1325 		if (errno == EBUSY)
1326 			goto retry;
1327 		err_print(CANT_CREATE_DOOR, door_file, strerror(errno));
1328 		(void) s_unlink(door_file);
1329 		devfsadm_exit(1);
1330 		/*NOTREACHED*/
1331 	}
1332 	lookup_door_fd = fd;
1333 
1334 	/* pass down the door name to kernel for door_ki_open */
1335 	if (devname_kcall(MODDEVNAME_LOOKUPDOOR, (void *)door_file) != 0)
1336 		err_print(DEVNAME_CONTACT_FAILED, strerror(errno));
1337 
1338 	vprint(CHATTY_MID, "%spausing\n", fcn);
1339 	for (;;) {
1340 		(void) pause();
1341 	}
1342 }
1343 
1344 /*ARGSUSED*/
1345 static void
1346 sync_handler(void *cookie, char *ap, size_t asize,
1347     door_desc_t *dp, uint_t ndesc)
1348 {
1349 	door_cred_t	dcred;
1350 	struct dca_off	*dcp, rdca;
1351 	struct dca_impl dci;
1352 
1353 	/*
1354 	 * Must be root to make this call
1355 	 * If caller is not root, don't touch its data.
1356 	 */
1357 	if (door_cred(&dcred) != 0 || dcred.dc_euid != 0) {
1358 		dcp = &rdca;
1359 		dcp->dca_error = EPERM;
1360 		goto out;
1361 	}
1362 
1363 	assert(ap);
1364 	assert(asize == sizeof (*dcp));
1365 
1366 	dcp = (void *)ap;
1367 
1368 	/*
1369 	 * Root is always present and is the first component of "name" member
1370 	 */
1371 	assert(dcp->dca_root == 0);
1372 
1373 	/*
1374 	 * The structure passed in by the door_client uses offsets
1375 	 * instead of pointers to work across address space boundaries.
1376 	 * Now copy the data into a structure (dca_impl) which uses
1377 	 * pointers.
1378 	 */
1379 	dci.dci_root = &dcp->dca_name[dcp->dca_root];
1380 	dci.dci_minor = dcp->dca_minor ? &dcp->dca_name[dcp->dca_minor] : NULL;
1381 	dci.dci_driver =
1382 	    dcp->dca_driver ? &dcp->dca_name[dcp->dca_driver] : NULL;
1383 	dci.dci_error = 0;
1384 	dci.dci_flags = dcp->dca_flags | (dci.dci_driver ? DCA_LOAD_DRV : 0);
1385 	dci.dci_arg = NULL;
1386 
1387 	lock_dev();
1388 	devi_tree_walk(&dci, DINFOCPYALL, NULL);
1389 	dcp->dca_error = dci.dci_error;
1390 
1391 	if (dcp->dca_flags & DCA_DEVLINK_SYNC)
1392 		unlock_dev(SYNC_STATE);
1393 	else
1394 		unlock_dev(CACHE_STATE);
1395 
1396 out:	(void) door_return((char *)dcp, sizeof (*dcp), NULL, 0);
1397 }
1398 
1399 static void
1400 lock_dev(void)
1401 {
1402 	vprint(CHATTY_MID, "lock_dev(): entered\n");
1403 
1404 	if (build_dev == FALSE)
1405 		return;
1406 
1407 	/* lockout other threads from /dev */
1408 	while (sema_wait(&dev_sema) != 0)
1409 		;
1410 
1411 	/*
1412 	 * Lock out other devfsadm processes from /dev.
1413 	 * If this wasn't the last process to run,
1414 	 * clear caches
1415 	 */
1416 	if (enter_dev_lock() != getpid()) {
1417 		invalidate_enumerate_cache();
1418 		rm_all_links_from_cache();
1419 		(void) di_devlink_close(&devlink_cache, DI_LINK_ERROR);
1420 
1421 		/* send any sysevents that were queued up. */
1422 		process_syseventq();
1423 	}
1424 
1425 	/*
1426 	 * (re)load the  reverse links database if not
1427 	 * already cached.
1428 	 */
1429 	if (devlink_cache == NULL)
1430 		devlink_cache = di_devlink_open(root_dir, 0);
1431 
1432 	/*
1433 	 * If modules were unloaded, reload them.  Also use module status
1434 	 * as an indication that we should check to see if other binding
1435 	 * files need to be reloaded.
1436 	 */
1437 	if (module_head == NULL) {
1438 		load_modules();
1439 		read_minor_perm_file();
1440 		read_driver_aliases_file();
1441 		read_devlinktab_file();
1442 		read_logindevperm_file();
1443 		read_enumerate_file();
1444 	}
1445 
1446 	if (module_head != NULL)
1447 		return;
1448 
1449 	if (strcmp(prog, DEVLINKS) == 0) {
1450 		if (devlinktab_list == NULL) {
1451 			err_print(NO_LINKTAB, devlinktab_file);
1452 			err_print(NO_MODULES, module_dirs);
1453 			err_print(ABORTING);
1454 			devfsadm_exit(1);
1455 			/*NOTREACHED*/
1456 		}
1457 	} else {
1458 		err_print(NO_MODULES, module_dirs);
1459 		if (strcmp(prog, DEVFSADM) == 0) {
1460 			err_print(MODIFY_PATH);
1461 		}
1462 	}
1463 }
1464 
1465 /*
1466  * Unlock the device.  If we are processing a CACHE_STATE call, we signal a
1467  * minor_fini_thread delayed SYNC_STATE at the end of the call.  If we are
1468  * processing a SYNC_STATE call, we cancel any minor_fini_thread SYNC_STATE
1469  * at both the start and end of the call since we will be doing the SYNC_STATE.
1470  */
1471 static void
1472 unlock_dev(int flag)
1473 {
1474 	assert(flag == SYNC_STATE || flag == CACHE_STATE);
1475 
1476 	vprint(CHATTY_MID, "unlock_dev(): entered\n");
1477 
1478 	/* If we are starting a SYNC_STATE, cancel minor_fini_thread SYNC */
1479 	if (flag == SYNC_STATE) {
1480 		(void) mutex_lock(&minor_fini_mutex);
1481 		minor_fini_canceled = TRUE;
1482 		minor_fini_delayed = FALSE;
1483 		(void) mutex_unlock(&minor_fini_mutex);
1484 	}
1485 
1486 	if (build_dev == FALSE)
1487 		return;
1488 
1489 	if (devlink_cache == NULL) {
1490 		err_print(NO_DEVLINK_CACHE);
1491 	}
1492 	assert(devlink_cache);
1493 
1494 	if (flag == SYNC_STATE) {
1495 		unload_modules();
1496 		if (update_database)
1497 			(void) di_devlink_update(devlink_cache);
1498 		(void) di_devlink_close(&devlink_cache, 0);
1499 
1500 		/*
1501 		 * now that the devlinks db cache has been flushed, it is safe
1502 		 * to send any sysevents that were queued up.
1503 		 */
1504 		process_syseventq();
1505 	}
1506 
1507 	exit_dev_lock(0);
1508 
1509 	(void) mutex_lock(&minor_fini_mutex);
1510 	if (flag == SYNC_STATE) {
1511 		/* We did a SYNC_STATE, cancel minor_fini_thread SYNC */
1512 		minor_fini_canceled = TRUE;
1513 		minor_fini_delayed = FALSE;
1514 	} else {
1515 		/* We did a CACHE_STATE, start delayed minor_fini_thread SYNC */
1516 		minor_fini_canceled = FALSE;
1517 		minor_fini_delayed = TRUE;
1518 		(void) cond_signal(&minor_fini_cv);
1519 	}
1520 	(void) mutex_unlock(&minor_fini_mutex);
1521 
1522 	(void) sema_post(&dev_sema);
1523 }
1524 
1525 /*
1526  * Check that if -r is set, it is not any part of a zone--- that is, that
1527  * the zonepath is not a substring of the root path.
1528  */
1529 static int
1530 zone_pathcheck(char *checkpath)
1531 {
1532 	void		*dlhdl = NULL;
1533 	char		*name;
1534 	char		root[MAXPATHLEN]; /* resolved devfsadm root path */
1535 	char		zroot[MAXPATHLEN]; /* zone root path */
1536 	char		rzroot[MAXPATHLEN]; /* resolved zone root path */
1537 	char		tmp[MAXPATHLEN];
1538 	FILE		*cookie;
1539 	int		err = DEVFSADM_SUCCESS;
1540 
1541 	if (checkpath[0] == '\0')
1542 		return (DEVFSADM_SUCCESS);
1543 
1544 	/*
1545 	 * Check if zones is available on this system.
1546 	 */
1547 	if ((dlhdl = dlopen(LIBZONECFG_PATH, RTLD_LAZY)) == NULL) {
1548 		return (DEVFSADM_SUCCESS);
1549 	}
1550 
1551 	bzero(root, sizeof (root));
1552 	if (resolvepath(checkpath, root, sizeof (root) - 1) == -1) {
1553 		/*
1554 		 * In this case the user has done "devfsadm -r" on some path
1555 		 * which does not yet exist, or we got some other misc. error.
1556 		 * We punt and don't resolve the path in this case.
1557 		 */
1558 		(void) strlcpy(root, checkpath, sizeof (root));
1559 	}
1560 
1561 	if (strlen(root) > 0 && (root[strlen(root) - 1] != '/')) {
1562 		(void) snprintf(tmp, sizeof (tmp), "%s/", root);
1563 		(void) strlcpy(root, tmp, sizeof (root));
1564 	}
1565 
1566 	cookie = setzoneent();
1567 	while ((name = getzoneent(cookie)) != NULL) {
1568 		/* Skip the global zone */
1569 		if (strcmp(name, GLOBAL_ZONENAME) == 0) {
1570 			free(name);
1571 			continue;
1572 		}
1573 
1574 		if (zone_get_zonepath(name, zroot, sizeof (zroot)) != Z_OK) {
1575 			free(name);
1576 			continue;
1577 		}
1578 
1579 		bzero(rzroot, sizeof (rzroot));
1580 		if (resolvepath(zroot, rzroot, sizeof (rzroot) - 1) == -1) {
1581 			/*
1582 			 * Zone path doesn't exist, or other misc error,
1583 			 * so we try using the non-resolved pathname.
1584 			 */
1585 			(void) strlcpy(rzroot, zroot, sizeof (rzroot));
1586 		}
1587 		if (strlen(rzroot) > 0 && (rzroot[strlen(rzroot) - 1] != '/')) {
1588 			(void) snprintf(tmp, sizeof (tmp), "%s/", rzroot);
1589 			(void) strlcpy(rzroot, tmp, sizeof (rzroot));
1590 		}
1591 
1592 		/*
1593 		 * Finally, the comparison.  If the zone root path is a
1594 		 * leading substring of the root path, fail.
1595 		 */
1596 		if (strncmp(rzroot, root, strlen(rzroot)) == 0) {
1597 			err_print(ZONE_PATHCHECK, root, name);
1598 			err = DEVFSADM_FAILURE;
1599 			free(name);
1600 			break;
1601 		}
1602 		free(name);
1603 	}
1604 	endzoneent(cookie);
1605 	(void) dlclose(dlhdl);
1606 	return (err);
1607 }
1608 
1609 /*
1610  *  Called by the daemon when it receives an event from the devfsadm SLM
1611  *  to syseventd.
1612  *
1613  *  The devfsadm SLM uses a private event channel for communication to
1614  *  devfsadmd set-up via private libsysevent interfaces.  This handler is
1615  *  used to bind to the devfsadmd channel for event delivery.
1616  *  The devfsadmd SLM insures single calls to this routine as well as
1617  *  synchronized event delivery.
1618  *
1619  */
1620 static void
1621 event_handler(sysevent_t *ev)
1622 {
1623 	char *path;
1624 	char *minor;
1625 	char *subclass;
1626 	char *dev_ev_subclass;
1627 	char *driver_name;
1628 	nvlist_t *attr_list = NULL;
1629 	int err = 0;
1630 	int instance;
1631 	int branch_event = 0;
1632 
1633 	/*
1634 	 * If this is event-driven, then we cannot trust the static devlist
1635 	 * to be correct.
1636 	 */
1637 
1638 	event_driven = TRUE;
1639 	subclass = sysevent_get_subclass_name(ev);
1640 	vprint(EVENT_MID, "event_handler: %s id:0X%llx\n",
1641 	    subclass, sysevent_get_seq(ev));
1642 
1643 	if (strcmp(subclass, ESC_DEVFS_START) == 0) {
1644 		return;
1645 	}
1646 
1647 	/* Check if event is an instance modification */
1648 	if (strcmp(subclass, ESC_DEVFS_INSTANCE_MOD) == 0) {
1649 		devfs_instance_mod();
1650 		return;
1651 	}
1652 	if (sysevent_get_attr_list(ev, &attr_list) != 0) {
1653 		vprint(EVENT_MID, "event_handler: can not get attr list\n");
1654 		return;
1655 	}
1656 
1657 	if (strcmp(subclass, ESC_DEVFS_DEVI_ADD) == 0 ||
1658 	    strcmp(subclass, ESC_DEVFS_DEVI_REMOVE) == 0 ||
1659 	    strcmp(subclass, ESC_DEVFS_MINOR_CREATE) == 0 ||
1660 	    strcmp(subclass, ESC_DEVFS_MINOR_REMOVE) == 0) {
1661 		if ((err = nvlist_lookup_string(attr_list, DEVFS_PATHNAME,
1662 		    &path)) != 0)
1663 			goto out;
1664 
1665 		if (nvlist_lookup_string(attr_list, DEVFS_DEVI_CLASS,
1666 		    &dev_ev_subclass) != 0)
1667 			dev_ev_subclass = NULL;
1668 
1669 		if (nvlist_lookup_string(attr_list, DEVFS_DRIVER_NAME,
1670 		    &driver_name) != 0)
1671 			driver_name = NULL;
1672 
1673 		if (nvlist_lookup_int32(attr_list, DEVFS_INSTANCE,
1674 		    &instance) != 0)
1675 			instance = -1;
1676 
1677 		if (nvlist_lookup_int32(attr_list, DEVFS_BRANCH_EVENT,
1678 		    &branch_event) != 0)
1679 			branch_event = 0;
1680 
1681 		if (nvlist_lookup_string(attr_list, DEVFS_MINOR_NAME,
1682 		    &minor) != 0)
1683 			minor = NULL;
1684 
1685 		lock_dev();
1686 
1687 		if (strcmp(ESC_DEVFS_DEVI_ADD, subclass) == 0) {
1688 			add_minor_pathname(path, NULL, dev_ev_subclass);
1689 			if (branch_event) {
1690 				build_and_enq_event(EC_DEV_BRANCH,
1691 				    ESC_DEV_BRANCH_ADD, path, DI_NODE_NIL,
1692 				    NULL);
1693 			}
1694 
1695 		} else if (strcmp(ESC_DEVFS_MINOR_CREATE, subclass) == 0) {
1696 			add_minor_pathname(path, minor, dev_ev_subclass);
1697 
1698 		} else if (strcmp(ESC_DEVFS_MINOR_REMOVE, subclass) == 0) {
1699 			hot_cleanup(path, minor, dev_ev_subclass, driver_name,
1700 			    instance);
1701 
1702 		} else { /* ESC_DEVFS_DEVI_REMOVE */
1703 			hot_cleanup(path, NULL, dev_ev_subclass,
1704 			    driver_name, instance);
1705 			if (branch_event) {
1706 				build_and_enq_event(EC_DEV_BRANCH,
1707 				    ESC_DEV_BRANCH_REMOVE, path, DI_NODE_NIL,
1708 				    NULL);
1709 			}
1710 		}
1711 
1712 		unlock_dev(CACHE_STATE);
1713 
1714 	} else if (strcmp(subclass, ESC_DEVFS_BRANCH_ADD) == 0 ||
1715 	    strcmp(subclass, ESC_DEVFS_BRANCH_REMOVE) == 0) {
1716 		if ((err = nvlist_lookup_string(attr_list,
1717 		    DEVFS_PATHNAME, &path)) != 0)
1718 			goto out;
1719 
1720 		/* just log ESC_DEV_BRANCH... event */
1721 		if (strcmp(subclass, ESC_DEVFS_BRANCH_ADD) == 0)
1722 			dev_ev_subclass = ESC_DEV_BRANCH_ADD;
1723 		else
1724 			dev_ev_subclass = ESC_DEV_BRANCH_REMOVE;
1725 
1726 		lock_dev();
1727 		build_and_enq_event(EC_DEV_BRANCH, dev_ev_subclass, path,
1728 		    DI_NODE_NIL, NULL);
1729 		unlock_dev(CACHE_STATE);
1730 	} else
1731 		err_print(UNKNOWN_EVENT, subclass);
1732 
1733 out:
1734 	if (err)
1735 		err_print(EVENT_ATTR_LOOKUP_FAILED, strerror(err));
1736 	nvlist_free(attr_list);
1737 }
1738 
1739 static void
1740 dca_impl_init(char *root, char *minor, struct dca_impl *dcip)
1741 {
1742 	assert(root);
1743 
1744 	dcip->dci_root = root;
1745 	dcip->dci_minor = minor;
1746 	dcip->dci_driver = NULL;
1747 	dcip->dci_error = 0;
1748 	dcip->dci_flags = 0;
1749 	dcip->dci_arg = NULL;
1750 }
1751 
1752 /*
1753  *  Kernel logs a message when a devinfo node is attached.  Try to create
1754  *  /dev and /devices for each minor node.  minorname can be NULL.
1755  */
1756 void
1757 add_minor_pathname(char *node, char *minor, char *ev_subclass)
1758 {
1759 	struct dca_impl	dci;
1760 
1761 	vprint(CHATTY_MID, "add_minor_pathname: node_path=%s minor=%s\n",
1762 	    node, minor ? minor : "NULL");
1763 
1764 	dca_impl_init(node, minor, &dci);
1765 
1766 	/*
1767 	 * Restrict hotplug link creation if daemon
1768 	 * started  with -i option.
1769 	 */
1770 	if (single_drv == TRUE) {
1771 		dci.dci_driver = driver;
1772 	}
1773 
1774 	/*
1775 	 * We are being invoked in response to a hotplug event.
1776 	 */
1777 	dci.dci_flags = DCA_HOT_PLUG | DCA_CHECK_TYPE;
1778 
1779 	devi_tree_walk(&dci, DINFOPROP|DINFOMINOR, ev_subclass);
1780 }
1781 
1782 static di_node_t
1783 find_clone_node()
1784 {
1785 	static di_node_t clone_node = DI_NODE_NIL;
1786 
1787 	if (clone_node == DI_NODE_NIL)
1788 		clone_node = di_init("/pseudo/clone@0", DINFOPROP);
1789 	return (clone_node);
1790 }
1791 
1792 static int
1793 is_descendent_of(di_node_t node, char *driver)
1794 {
1795 	while (node != DI_NODE_NIL) {
1796 		char *drv = di_driver_name(node);
1797 		if (strcmp(drv, driver) == 0)
1798 			return (1);
1799 		node = di_parent_node(node);
1800 	}
1801 	return (0);
1802 }
1803 
1804 /*
1805  * Checks the minor type.  If it is an alias node, then lookup
1806  * the real node/minor first, then call minor_process() to
1807  * do the real work.
1808  */
1809 static int
1810 check_minor_type(di_node_t node, di_minor_t minor, void *arg)
1811 {
1812 	ddi_minor_type	minor_type;
1813 	di_node_t	clone_node;
1814 	char		*mn;
1815 	char		*nt;
1816 	struct mlist	*dep;
1817 	struct dca_impl	*dcip = arg;
1818 
1819 	assert(dcip);
1820 
1821 	dep = dcip->dci_arg;
1822 
1823 	mn = di_minor_name(minor);
1824 
1825 	/*
1826 	 * We match driver here instead of in minor_process
1827 	 * as we want the actual driver name. This check is
1828 	 * unnecessary during deferred processing.
1829 	 */
1830 	if (dep &&
1831 	    ((dcip->dci_driver && !is_descendent_of(node, dcip->dci_driver)) ||
1832 	    (dcip->dci_minor && strcmp(mn, dcip->dci_minor)))) {
1833 		return (DI_WALK_CONTINUE);
1834 	}
1835 
1836 	if ((dcip->dci_flags & DCA_CHECK_TYPE) &&
1837 	    (nt = di_minor_nodetype(minor)) &&
1838 	    (strcmp(nt, DDI_NT_NET) == 0)) {
1839 		dcip->dci_flags &= ~DCA_CHECK_TYPE;
1840 	}
1841 
1842 	minor_type = di_minor_type(minor);
1843 
1844 	if (minor_type == DDM_MINOR) {
1845 		minor_process(node, minor, dep);
1846 
1847 	} else if (minor_type == DDM_ALIAS) {
1848 		struct mlist *cdep, clone_del = {0};
1849 
1850 		clone_node = find_clone_node();
1851 		if (clone_node == DI_NODE_NIL) {
1852 			err_print(DI_INIT_FAILED, "clone", strerror(errno));
1853 			return (DI_WALK_CONTINUE);
1854 		}
1855 
1856 		cdep = dep ? &clone_del : NULL;
1857 
1858 		minor_process(clone_node, minor, cdep);
1859 
1860 		/*
1861 		 * cache "alias" minor node and free "clone" minor
1862 		 */
1863 		if (cdep != NULL && cdep->head != NULL) {
1864 			assert(cdep->tail != NULL);
1865 			cache_deferred_minor(dep, node, minor);
1866 			dcip->dci_arg = cdep;
1867 			process_deferred_links(dcip, DCA_FREE_LIST);
1868 			dcip->dci_arg = dep;
1869 		}
1870 	}
1871 
1872 	return (DI_WALK_CONTINUE);
1873 }
1874 
1875 
1876 /*
1877  *  This is the entry point for each minor node, whether walking
1878  *  the entire tree via di_walk_minor() or processing a hotplug event
1879  *  for a single devinfo node (via hotplug ndi_devi_online()).
1880  */
1881 /*ARGSUSED*/
1882 static void
1883 minor_process(di_node_t node, di_minor_t minor, struct mlist *dep)
1884 {
1885 	create_list_t	*create;
1886 	int		defer;
1887 
1888 	vprint(CHATTY_MID, "minor_process: node=%s, minor=%s\n",
1889 	    di_node_name(node), di_minor_name(minor));
1890 
1891 	if (dep != NULL) {
1892 
1893 		/*
1894 		 * Reset /devices node to minor_perm perm/ownership
1895 		 * if we are here to deactivate device allocation
1896 		 */
1897 		if (build_devices == TRUE) {
1898 			reset_node_permissions(node, minor);
1899 		}
1900 
1901 		if (build_dev == FALSE) {
1902 			return;
1903 		}
1904 
1905 		/*
1906 		 * This function will create any nodes for /etc/devlink.tab.
1907 		 * If devlink.tab handles link creation, we don't call any
1908 		 * devfsadm modules since that could cause duplicate caching
1909 		 * in the enumerate functions if different re strings are
1910 		 * passed that are logically identical.  I'm still not
1911 		 * convinced this would cause any harm, but better to be safe.
1912 		 *
1913 		 * Deferred processing is available only for devlinks
1914 		 * created through devfsadm modules.
1915 		 */
1916 		if (process_devlink_compat(minor, node) == TRUE) {
1917 			return;
1918 		}
1919 	} else {
1920 		vprint(CHATTY_MID, "minor_process: deferred processing\n");
1921 	}
1922 
1923 	/*
1924 	 * look for relevant link create rules in the modules, and
1925 	 * invoke the link create callback function to build a link
1926 	 * if there is a match.
1927 	 */
1928 	defer = 0;
1929 	for (create = create_head; create != NULL; create = create->next) {
1930 		if ((minor_matches_rule(node, minor, create) == TRUE) &&
1931 		    class_ok(create->create->device_class) ==
1932 		    DEVFSADM_SUCCESS) {
1933 			if (call_minor_init(create->modptr) ==
1934 			    DEVFSADM_FAILURE) {
1935 				continue;
1936 			}
1937 
1938 			/*
1939 			 * If NOT doing the deferred creates (i.e. 1st pass) and
1940 			 * rule requests deferred processing cache the minor
1941 			 * data.
1942 			 *
1943 			 * If deferred processing (2nd pass), create links
1944 			 * ONLY if rule requests deferred processing.
1945 			 */
1946 			if (dep && ((create->create->flags & CREATE_MASK) ==
1947 			    CREATE_DEFER)) {
1948 				defer = 1;
1949 				continue;
1950 			} else if (dep == NULL &&
1951 			    ((create->create->flags & CREATE_MASK) !=
1952 			    CREATE_DEFER)) {
1953 				continue;
1954 			}
1955 
1956 			if ((*(create->create->callback_fcn))
1957 			    (minor, node) == DEVFSADM_TERMINATE) {
1958 				break;
1959 			}
1960 		}
1961 	}
1962 
1963 	if (defer)
1964 		cache_deferred_minor(dep, node, minor);
1965 }
1966 
1967 
1968 /*
1969  * Cache node and minor in defer list.
1970  */
1971 static void
1972 cache_deferred_minor(
1973 	struct mlist *dep,
1974 	di_node_t node,
1975 	di_minor_t minor)
1976 {
1977 	struct minor	*mp;
1978 	const char	*fcn = "cache_deferred_minor";
1979 
1980 	vprint(CHATTY_MID, "%s node=%s, minor=%s\n", fcn,
1981 	    di_node_name(node), di_minor_name(minor));
1982 
1983 	if (dep == NULL) {
1984 		vprint(CHATTY_MID, "%s: cannot cache during "
1985 		    "deferred processing. Ignoring minor\n", fcn);
1986 		return;
1987 	}
1988 
1989 	mp = (struct minor *)s_zalloc(sizeof (struct minor));
1990 	mp->node = node;
1991 	mp->minor = minor;
1992 	mp->next = NULL;
1993 
1994 	assert(dep->head == NULL || dep->tail != NULL);
1995 	if (dep->head == NULL) {
1996 		dep->head = mp;
1997 	} else {
1998 		dep->tail->next = mp;
1999 	}
2000 	dep->tail = mp;
2001 }
2002 
2003 /*
2004  *  Check to see if "create" link creation rule matches this node/minor.
2005  *  If it does, return TRUE.
2006  */
2007 static int
2008 minor_matches_rule(di_node_t node, di_minor_t minor, create_list_t *create)
2009 {
2010 	char *m_nodetype, *m_drvname;
2011 
2012 	if (create->create->node_type != NULL) {
2013 
2014 		m_nodetype = di_minor_nodetype(minor);
2015 		assert(m_nodetype != NULL);
2016 
2017 		switch (create->create->flags & TYPE_MASK) {
2018 		case TYPE_EXACT:
2019 			if (strcmp(create->create->node_type, m_nodetype) !=
2020 			    0) {
2021 				return (FALSE);
2022 			}
2023 			break;
2024 		case TYPE_PARTIAL:
2025 			if (strncmp(create->create->node_type, m_nodetype,
2026 			    strlen(create->create->node_type)) != 0) {
2027 				return (FALSE);
2028 			}
2029 			break;
2030 		case TYPE_RE:
2031 			if (regexec(&(create->node_type_comp), m_nodetype,
2032 			    0, NULL, 0) != 0) {
2033 				return (FALSE);
2034 			}
2035 			break;
2036 		}
2037 	}
2038 
2039 	if (create->create->drv_name != NULL) {
2040 		m_drvname = di_driver_name(node);
2041 		switch (create->create->flags & DRV_MASK) {
2042 		case DRV_EXACT:
2043 			if (strcmp(create->create->drv_name, m_drvname) != 0) {
2044 				return (FALSE);
2045 			}
2046 			break;
2047 		case DRV_RE:
2048 			if (regexec(&(create->drv_name_comp), m_drvname,
2049 			    0, NULL, 0) != 0) {
2050 				return (FALSE);
2051 			}
2052 			break;
2053 		}
2054 	}
2055 
2056 	return (TRUE);
2057 }
2058 
2059 /*
2060  * If no classes were given on the command line, then return DEVFSADM_SUCCESS.
2061  * Otherwise, return DEVFSADM_SUCCESS if the device "class" from the module
2062  * matches one of the device classes given on the command line,
2063  * otherwise, return DEVFSADM_FAILURE.
2064  */
2065 static int
2066 class_ok(char *class)
2067 {
2068 	int i;
2069 
2070 	if (num_classes == 0) {
2071 		return (DEVFSADM_SUCCESS);
2072 	}
2073 
2074 	for (i = 0; i < num_classes; i++) {
2075 		if (strcmp(class, classes[i]) == 0) {
2076 			return (DEVFSADM_SUCCESS);
2077 		}
2078 	}
2079 	return (DEVFSADM_FAILURE);
2080 }
2081 
2082 /*
2083  * call minor_fini on active modules, then unload ALL modules
2084  */
2085 static void
2086 unload_modules(void)
2087 {
2088 	module_t *module_free;
2089 	create_list_t *create_free;
2090 	remove_list_t *remove_free;
2091 
2092 	while (create_head != NULL) {
2093 		create_free = create_head;
2094 		create_head = create_head->next;
2095 
2096 		if ((create_free->create->flags & TYPE_RE) == TYPE_RE) {
2097 			regfree(&(create_free->node_type_comp));
2098 		}
2099 		if ((create_free->create->flags & DRV_RE) == DRV_RE) {
2100 			regfree(&(create_free->drv_name_comp));
2101 		}
2102 		free(create_free);
2103 	}
2104 
2105 	while (remove_head != NULL) {
2106 		remove_free = remove_head;
2107 		remove_head = remove_head->next;
2108 		free(remove_free);
2109 	}
2110 
2111 	while (module_head != NULL) {
2112 
2113 		if ((module_head->minor_fini != NULL) &&
2114 		    ((module_head->flags & MODULE_ACTIVE) == MODULE_ACTIVE)) {
2115 			(void) (*(module_head->minor_fini))();
2116 		}
2117 
2118 		vprint(MODLOAD_MID, "unloading module %s\n", module_head->name);
2119 		free(module_head->name);
2120 		(void) dlclose(module_head->dlhandle);
2121 
2122 		module_free = module_head;
2123 		module_head = module_head->next;
2124 		free(module_free);
2125 	}
2126 }
2127 
2128 /*
2129  * Load devfsadm logical link processing modules.
2130  */
2131 static void
2132 load_modules(void)
2133 {
2134 	DIR *mod_dir;
2135 	struct dirent *entp;
2136 	char cdir[PATH_MAX + 1];
2137 	char *last;
2138 	char *mdir = module_dirs;
2139 	char *fcn = "load_modules: ";
2140 
2141 	while (*mdir != '\0') {
2142 
2143 		while (*mdir == ':') {
2144 			mdir++;
2145 		}
2146 
2147 		if (*mdir == '\0') {
2148 			continue;
2149 		}
2150 
2151 		last = strchr(mdir, ':');
2152 
2153 		if (last == NULL) {
2154 			last = mdir + strlen(mdir);
2155 		}
2156 
2157 		(void) strncpy(cdir, mdir, last - mdir);
2158 		cdir[last - mdir] = '\0';
2159 		mdir += strlen(cdir);
2160 
2161 		if ((mod_dir = opendir(cdir)) == NULL) {
2162 			vprint(MODLOAD_MID, "%sopendir(%s): %s\n",
2163 			    fcn, cdir, strerror(errno));
2164 			continue;
2165 		}
2166 
2167 		while ((entp = readdir(mod_dir)) != NULL) {
2168 
2169 			if ((strcmp(entp->d_name, ".") == 0) ||
2170 			    (strcmp(entp->d_name, "..") == 0)) {
2171 				continue;
2172 			}
2173 
2174 			load_module(entp->d_name, cdir);
2175 		}
2176 		s_closedir(mod_dir);
2177 	}
2178 }
2179 
2180 static void
2181 load_module(char *mname, char *cdir)
2182 {
2183 	_devfsadm_create_reg_t *create_reg;
2184 	_devfsadm_remove_reg_V1_t *remove_reg;
2185 	create_list_t *create_list_element;
2186 	create_list_t **create_list_next;
2187 	remove_list_t *remove_list_element;
2188 	remove_list_t **remove_list_next;
2189 	char epath[PATH_MAX + 1], *end;
2190 	char *fcn = "load_module: ";
2191 	char *dlerrstr;
2192 	void *dlhandle;
2193 	module_t *module;
2194 	int flags;
2195 	int n;
2196 	int i;
2197 
2198 	/* ignore any file which does not end in '.so' */
2199 	if ((end = strstr(mname, MODULE_SUFFIX)) != NULL) {
2200 		if (end[strlen(MODULE_SUFFIX)] != '\0') {
2201 			return;
2202 		}
2203 	} else {
2204 		return;
2205 	}
2206 
2207 	(void) snprintf(epath, sizeof (epath), "%s/%s", cdir, mname);
2208 
2209 	if ((dlhandle = dlopen(epath, RTLD_LAZY)) == NULL) {
2210 		dlerrstr = dlerror();
2211 		err_print(DLOPEN_FAILED, epath,
2212 		    dlerrstr ? dlerrstr : "unknown error");
2213 		return;
2214 	}
2215 
2216 	/* dlsym the _devfsadm_create_reg structure */
2217 	if (NULL == (create_reg = (_devfsadm_create_reg_t *)
2218 	    dlsym(dlhandle, _DEVFSADM_CREATE_REG))) {
2219 		vprint(MODLOAD_MID, "dlsym(%s, %s): symbol not found\n", epath,
2220 		    _DEVFSADM_CREATE_REG);
2221 	} else {
2222 		vprint(MODLOAD_MID, "%sdlsym(%s, %s) succeeded\n",
2223 		    fcn, epath, _DEVFSADM_CREATE_REG);
2224 	}
2225 
2226 	/* dlsym the _devfsadm_remove_reg structure */
2227 	if (NULL == (remove_reg = (_devfsadm_remove_reg_V1_t *)
2228 	    dlsym(dlhandle, _DEVFSADM_REMOVE_REG))) {
2229 		vprint(MODLOAD_MID, "dlsym(%s,\n\t%s): symbol not found\n",
2230 		    epath, _DEVFSADM_REMOVE_REG);
2231 	} else {
2232 		vprint(MODLOAD_MID, "dlsym(%s, %s): succeeded\n",
2233 		    epath, _DEVFSADM_REMOVE_REG);
2234 	}
2235 
2236 	vprint(MODLOAD_MID, "module %s loaded\n", epath);
2237 
2238 	module = (module_t *)s_malloc(sizeof (module_t));
2239 	module->name = s_strdup(epath);
2240 	module->dlhandle = dlhandle;
2241 
2242 	/* dlsym other module functions, to be called later */
2243 	module->minor_fini = (int (*)())dlsym(dlhandle, MINOR_FINI);
2244 	module->minor_init = (int (*)())dlsym(dlhandle, MINOR_INIT);
2245 	module->flags = 0;
2246 
2247 	/*
2248 	 *  put a ptr to each struct devfsadm_create on "create_head"
2249 	 *  list sorted in interpose_lvl.
2250 	 */
2251 	if (create_reg != NULL) {
2252 		for (i = 0; i < create_reg->count; i++) {
2253 			int flags = create_reg->tblp[i].flags;
2254 
2255 			create_list_element = (create_list_t *)
2256 			    s_malloc(sizeof (create_list_t));
2257 
2258 			create_list_element->create = &(create_reg->tblp[i]);
2259 			create_list_element->modptr = module;
2260 
2261 			if (((flags & CREATE_MASK) != 0) &&
2262 			    ((flags & CREATE_MASK) != CREATE_DEFER)) {
2263 				free(create_list_element);
2264 				err_print("illegal flag combination in "
2265 				    "module create\n");
2266 				err_print(IGNORING_ENTRY, i, epath);
2267 				continue;
2268 			}
2269 
2270 			if (((flags & TYPE_MASK) == 0) ^
2271 			    (create_reg->tblp[i].node_type == NULL)) {
2272 				free(create_list_element);
2273 				err_print("flags value incompatible with "
2274 				    "node_type value in module create\n");
2275 				err_print(IGNORING_ENTRY, i, epath);
2276 				continue;
2277 			}
2278 
2279 			if (((flags & TYPE_MASK) != 0) &&
2280 			    ((flags & TYPE_MASK) != TYPE_EXACT) &&
2281 			    ((flags & TYPE_MASK) != TYPE_RE) &&
2282 			    ((flags & TYPE_MASK) != TYPE_PARTIAL)) {
2283 				free(create_list_element);
2284 				err_print("illegal TYPE_* flag combination in "
2285 				    "module create\n");
2286 				err_print(IGNORING_ENTRY, i, epath);
2287 				continue;
2288 			}
2289 
2290 			/* precompile regular expression for efficiency */
2291 			if ((flags & TYPE_RE) == TYPE_RE) {
2292 				if ((n = regcomp(&(create_list_element->
2293 				    node_type_comp),
2294 				    create_reg->tblp[i].node_type,
2295 				    REG_EXTENDED)) != 0) {
2296 					free(create_list_element);
2297 					err_print(REGCOMP_FAILED,
2298 					    create_reg->tblp[i].node_type, n);
2299 					err_print(IGNORING_ENTRY, i, epath);
2300 					continue;
2301 				}
2302 			}
2303 
2304 			if (((flags & DRV_MASK) == 0) ^
2305 			    (create_reg->tblp[i].drv_name == NULL)) {
2306 				if ((flags & TYPE_RE) == TYPE_RE) {
2307 					regfree(&(create_list_element->
2308 					    node_type_comp));
2309 				}
2310 				free(create_list_element);
2311 				err_print("flags value incompatible with "
2312 				    "drv_name value in module create\n");
2313 				err_print(IGNORING_ENTRY, i, epath);
2314 				continue;
2315 			}
2316 
2317 			if (((flags & DRV_MASK) != 0) &&
2318 			    ((flags & DRV_MASK) != DRV_EXACT) &&
2319 			    ((flags & DRV_MASK) !=  DRV_RE)) {
2320 				if ((flags & TYPE_RE) == TYPE_RE) {
2321 					regfree(&(create_list_element->
2322 					    node_type_comp));
2323 				}
2324 				free(create_list_element);
2325 				err_print("illegal DRV_* flag combination in "
2326 				    "module create\n");
2327 				err_print(IGNORING_ENTRY, i, epath);
2328 				continue;
2329 			}
2330 
2331 			/* precompile regular expression for efficiency */
2332 			if ((create_reg->tblp[i].flags & DRV_RE) == DRV_RE) {
2333 				if ((n = regcomp(&(create_list_element->
2334 				    drv_name_comp),
2335 				    create_reg->tblp[i].drv_name,
2336 				    REG_EXTENDED)) != 0) {
2337 					if ((flags & TYPE_RE) == TYPE_RE) {
2338 						regfree(&(create_list_element->
2339 						    node_type_comp));
2340 					}
2341 					free(create_list_element);
2342 					err_print(REGCOMP_FAILED,
2343 					    create_reg->tblp[i].drv_name, n);
2344 					err_print(IGNORING_ENTRY, i, epath);
2345 					continue;
2346 				}
2347 			}
2348 
2349 
2350 			/* add to list sorted by interpose level */
2351 			for (create_list_next = &(create_head);
2352 			    (*create_list_next != NULL) &&
2353 			    (*create_list_next)->create->interpose_lvl >=
2354 			    create_list_element->create->interpose_lvl;
2355 			    create_list_next = &((*create_list_next)->next))
2356 				;
2357 			create_list_element->next = *create_list_next;
2358 			*create_list_next = create_list_element;
2359 		}
2360 	}
2361 
2362 	/*
2363 	 *  put a ptr to each struct devfsadm_remove on "remove_head"
2364 	 *  list sorted by interpose_lvl.
2365 	 */
2366 	flags = 0;
2367 	if (remove_reg != NULL) {
2368 		if (remove_reg->version < DEVFSADM_V1)
2369 			flags |= RM_NOINTERPOSE;
2370 		for (i = 0; i < remove_reg->count; i++) {
2371 
2372 			remove_list_element = (remove_list_t *)
2373 			    s_malloc(sizeof (remove_list_t));
2374 
2375 			remove_list_element->remove = &(remove_reg->tblp[i]);
2376 			remove_list_element->remove->flags |= flags;
2377 			remove_list_element->modptr = module;
2378 
2379 			for (remove_list_next = &(remove_head);
2380 			    (*remove_list_next != NULL) &&
2381 			    (*remove_list_next)->remove->interpose_lvl >=
2382 			    remove_list_element->remove->interpose_lvl;
2383 			    remove_list_next = &((*remove_list_next)->next))
2384 				;
2385 			remove_list_element->next = *remove_list_next;
2386 			*remove_list_next = remove_list_element;
2387 		}
2388 	}
2389 
2390 	module->next = module_head;
2391 	module_head = module;
2392 }
2393 
2394 /*
2395  * After we have completed a CACHE_STATE, if a SYNC_STATE does not occur
2396  * within 'timeout' secs the minor_fini_thread needs to do a SYNC_STATE
2397  * so that we still call the minor_fini routines.
2398  */
2399 /*ARGSUSED*/
2400 static void
2401 minor_fini_thread(void *arg)
2402 {
2403 	timestruc_t	abstime;
2404 
2405 	vprint(INITFINI_MID, "minor_fini_thread starting\n");
2406 
2407 	(void) mutex_lock(&minor_fini_mutex);
2408 	for (;;) {
2409 		/* wait the gather period, or until signaled */
2410 		abstime.tv_sec = time(NULL) + minor_fini_timeout;
2411 		abstime.tv_nsec = 0;
2412 		(void) cond_timedwait(&minor_fini_cv,
2413 		    &minor_fini_mutex, &abstime);
2414 
2415 		/* if minor_fini was canceled, go wait again */
2416 		if (minor_fini_canceled == TRUE)
2417 			continue;
2418 
2419 		/* if minor_fini was delayed, go wait again */
2420 		if (minor_fini_delayed == TRUE) {
2421 			minor_fini_delayed = FALSE;
2422 			continue;
2423 		}
2424 
2425 		/* done with cancellations and delays, do the SYNC_STATE */
2426 		(void) mutex_unlock(&minor_fini_mutex);
2427 
2428 		lock_dev();
2429 		unlock_dev(SYNC_STATE);
2430 		vprint(INITFINI_MID, "minor_fini sync done\n");
2431 
2432 		(void) mutex_lock(&minor_fini_mutex);
2433 	}
2434 }
2435 
2436 
2437 /*
2438  * Attempt to initialize module, if a minor_init routine exists.  Set
2439  * the active flag if the routine exists and succeeds.	If it doesn't
2440  * exist, just set the active flag.
2441  */
2442 static int
2443 call_minor_init(module_t *module)
2444 {
2445 	char *fcn = "call_minor_init: ";
2446 
2447 	if ((module->flags & MODULE_ACTIVE) == MODULE_ACTIVE) {
2448 		return (DEVFSADM_SUCCESS);
2449 	}
2450 
2451 	vprint(INITFINI_MID, "%smodule %s.  current state: inactive\n",
2452 	    fcn, module->name);
2453 
2454 	if (module->minor_init == NULL) {
2455 		module->flags |= MODULE_ACTIVE;
2456 		vprint(INITFINI_MID, "minor_init not defined\n");
2457 		return (DEVFSADM_SUCCESS);
2458 	}
2459 
2460 	if ((*(module->minor_init))() == DEVFSADM_FAILURE) {
2461 		err_print(FAILED_FOR_MODULE, MINOR_INIT, module->name);
2462 		return (DEVFSADM_FAILURE);
2463 	}
2464 
2465 	vprint(INITFINI_MID, "minor_init() returns DEVFSADM_SUCCESS. "
2466 	    "new state: active\n");
2467 
2468 	module->flags |= MODULE_ACTIVE;
2469 	return (DEVFSADM_SUCCESS);
2470 }
2471 
2472 /*
2473  * Creates a symlink 'link' to the physical path of node:minor.
2474  * Construct link contents, then call create_link_common().
2475  */
2476 /*ARGSUSED*/
2477 int
2478 devfsadm_mklink(char *link, di_node_t node, di_minor_t minor, int flags)
2479 {
2480 	char rcontents[PATH_MAX];
2481 	char devlink[PATH_MAX];
2482 	char phy_path[PATH_MAX];
2483 	char *acontents;
2484 	char *dev_path;
2485 	int numslashes;
2486 	int rv;
2487 	int i, link_exists;
2488 	int last_was_slash = FALSE;
2489 
2490 	/*
2491 	 * try to use devices path
2492 	 */
2493 	if ((node == lnode) && (minor == lminor)) {
2494 		acontents = lphy_path;
2495 	} else if (di_minor_type(minor) == DDM_ALIAS) {
2496 		/* use /pseudo/clone@0:<driver> as the phys path */
2497 		(void) snprintf(phy_path, sizeof (phy_path),
2498 		    "/pseudo/clone@0:%s",
2499 		    di_driver_name(di_minor_devinfo(minor)));
2500 		acontents = phy_path;
2501 	} else {
2502 		if ((dev_path = di_devfs_path(node)) == NULL) {
2503 			err_print(DI_DEVFS_PATH_FAILED, strerror(errno));
2504 			devfsadm_exit(1);
2505 			/*NOTREACHED*/
2506 		}
2507 		(void) snprintf(phy_path, sizeof (phy_path), "%s:%s",
2508 		    dev_path, di_minor_name(minor));
2509 		di_devfs_path_free(dev_path);
2510 		acontents = phy_path;
2511 	}
2512 
2513 	/* prepend link with dev_dir contents */
2514 	(void) strlcpy(devlink, dev_dir, sizeof (devlink));
2515 	(void) strlcat(devlink, "/", sizeof (devlink));
2516 	(void) strlcat(devlink, link, sizeof (devlink));
2517 
2518 	/*
2519 	 * Calculate # of ../ to add.  Account for double '//' in path.
2520 	 * Ignore all leading slashes.
2521 	 */
2522 	for (i = 0; link[i] == '/'; i++)
2523 		;
2524 	for (numslashes = 0; link[i] != '\0'; i++) {
2525 		if (link[i] == '/') {
2526 			if (last_was_slash == FALSE) {
2527 				numslashes++;
2528 				last_was_slash = TRUE;
2529 			}
2530 		} else {
2531 			last_was_slash = FALSE;
2532 		}
2533 	}
2534 	/* Don't count any trailing '/' */
2535 	if (link[i-1] == '/') {
2536 		numslashes--;
2537 	}
2538 
2539 	rcontents[0] = '\0';
2540 	do {
2541 		(void) strlcat(rcontents, "../", sizeof (rcontents));
2542 	} while (numslashes-- != 0);
2543 
2544 	(void) strlcat(rcontents, "devices", sizeof (rcontents));
2545 	(void) strlcat(rcontents, acontents, sizeof (rcontents));
2546 
2547 	if (devlinks_debug == TRUE) {
2548 		vprint(INFO_MID, "adding link %s ==> %s\n", devlink, rcontents);
2549 	}
2550 
2551 	if ((rv = create_link_common(devlink, rcontents, &link_exists))
2552 	    == DEVFSADM_SUCCESS) {
2553 		linknew = TRUE;
2554 		add_link_to_cache(link, acontents);
2555 	} else {
2556 		linknew = FALSE;
2557 	}
2558 
2559 	if (link_exists == TRUE) {
2560 		/* Link exists or was just created */
2561 		(void) di_devlink_add_link(devlink_cache, link, rcontents,
2562 		    DI_PRIMARY_LINK);
2563 
2564 		if (system_labeled && (flags & DA_ADD)) {
2565 			/*
2566 			 * Add this to the list of allocatable devices. If this
2567 			 * is a hotplugged, removable disk, add it as rmdisk.
2568 			 */
2569 			int instance = di_instance(node);
2570 
2571 			if ((flags & DA_CD) &&
2572 			    (_da_check_for_usb(devlink, root_dir) == 1)) {
2573 				(void) da_add_list(&devlist, devlink, instance,
2574 				    DA_ADD|DA_RMDISK);
2575 				update_devdb = DA_RMDISK;
2576 			} else if (linknew == TRUE) {
2577 				(void) da_add_list(&devlist, devlink, instance,
2578 				    flags);
2579 				update_devdb = flags;
2580 			}
2581 		}
2582 	}
2583 
2584 	return (rv);
2585 }
2586 
2587 /*
2588  * Creates a symlink link to primary_link.  Calculates relative
2589  * directory offsets, then calls link_common().
2590  */
2591 /*ARGSUSED*/
2592 int
2593 devfsadm_secondary_link(char *link, char *primary_link, int flags)
2594 {
2595 	char contents[PATH_MAX + 1];
2596 	char devlink[PATH_MAX + 1];
2597 	int rv, link_exists;
2598 	char *fpath;
2599 	char *tpath;
2600 	char *op;
2601 
2602 	/* prepend link with dev_dir contents */
2603 	(void) strcpy(devlink, dev_dir);
2604 	(void) strcat(devlink, "/");
2605 	(void) strcat(devlink, link);
2606 	/*
2607 	 * building extra link, so use first link as link contents, but first
2608 	 * make it relative.
2609 	 */
2610 	fpath = link;
2611 	tpath = primary_link;
2612 	op = contents;
2613 
2614 	while (*fpath == *tpath && *fpath != '\0') {
2615 		fpath++, tpath++;
2616 	}
2617 
2618 	/* Count directories to go up, if any, and add "../" */
2619 	while (*fpath != '\0') {
2620 		if (*fpath == '/') {
2621 			(void) strcpy(op, "../");
2622 			op += 3;
2623 		}
2624 		fpath++;
2625 	}
2626 
2627 	/*
2628 	 * Back up to the start of the current path component, in
2629 	 * case in the middle
2630 	 */
2631 	while (tpath != primary_link && *(tpath-1) != '/') {
2632 		tpath--;
2633 	}
2634 	(void) strcpy(op, tpath);
2635 
2636 	if (devlinks_debug == TRUE) {
2637 		vprint(INFO_MID, "adding extra link %s ==> %s\n",
2638 		    devlink, contents);
2639 	}
2640 
2641 	if ((rv = create_link_common(devlink, contents, &link_exists))
2642 	    == DEVFSADM_SUCCESS) {
2643 		/*
2644 		 * we need to save the ultimate /devices contents, and not the
2645 		 * secondary link, since hotcleanup only looks at /devices path.
2646 		 * Since we don't have devices path here, we can try to get it
2647 		 * by readlink'ing the secondary link.  This assumes the primary
2648 		 * link was created first.
2649 		 */
2650 		add_link_to_cache(link, lphy_path);
2651 		linknew = TRUE;
2652 		if (system_labeled &&
2653 		    ((flags & DA_AUDIO) && (flags & DA_ADD))) {
2654 			/*
2655 			 * Add this device to the list of allocatable devices.
2656 			 */
2657 			int	instance = 0;
2658 
2659 			op = strrchr(contents, '/');
2660 			op++;
2661 			(void) sscanf(op, "%d", &instance);
2662 			(void) da_add_list(&devlist, devlink, instance, flags);
2663 			update_devdb = flags;
2664 		}
2665 	} else {
2666 		linknew = FALSE;
2667 	}
2668 
2669 	/*
2670 	 * If link exists or was just created, add it to the database
2671 	 */
2672 	if (link_exists == TRUE) {
2673 		(void) di_devlink_add_link(devlink_cache, link, contents,
2674 		    DI_SECONDARY_LINK);
2675 	}
2676 
2677 	return (rv);
2678 }
2679 
2680 /* returns pointer to the devices directory */
2681 char *
2682 devfsadm_get_devices_dir()
2683 {
2684 	return (devices_dir);
2685 }
2686 
2687 /*
2688  * Does the actual link creation.  VERBOSE_MID only used if there is
2689  * a change.  CHATTY_MID used otherwise.
2690  */
2691 static int
2692 create_link_common(char *devlink, char *contents, int *exists)
2693 {
2694 	int try;
2695 	int linksize;
2696 	int max_tries = 0;
2697 	static int prev_link_existed = TRUE;
2698 	char checkcontents[PATH_MAX + 1];
2699 	char *hide;
2700 
2701 	*exists = FALSE;
2702 
2703 	/* Database is not updated when file_mods == FALSE */
2704 	if (file_mods == FALSE) {
2705 		/* we want *actual* link contents so no alias redirection */
2706 		linksize = readlink(devlink, checkcontents, PATH_MAX);
2707 		if (linksize > 0) {
2708 			checkcontents[linksize] = '\0';
2709 			if (strcmp(checkcontents, contents) != 0) {
2710 				vprint(CHATTY_MID, REMOVING_LINK,
2711 				    devlink, checkcontents);
2712 				return (DEVFSADM_SUCCESS);
2713 			} else {
2714 				vprint(CHATTY_MID, "link exists and is correct:"
2715 				    " %s -> %s\n", devlink, contents);
2716 				/* failure only in that the link existed */
2717 				return (DEVFSADM_FAILURE);
2718 			}
2719 		} else {
2720 			vprint(VERBOSE_MID, CREATING_LINK, devlink, contents);
2721 			return (DEVFSADM_SUCCESS);
2722 		}
2723 	}
2724 
2725 	/*
2726 	 * systems calls are expensive, so predict whether to readlink
2727 	 * or symlink first, based on previous attempt
2728 	 */
2729 	if (prev_link_existed == FALSE) {
2730 		try = CREATE_LINK;
2731 	} else {
2732 		try = READ_LINK;
2733 	}
2734 
2735 	while (++max_tries <= 3) {
2736 
2737 		switch (try) {
2738 		case  CREATE_LINK:
2739 
2740 			if (symlink(contents, devlink) == 0) {
2741 				vprint(VERBOSE_MID, CREATING_LINK, devlink,
2742 				    contents);
2743 				prev_link_existed = FALSE;
2744 				/* link successfully created */
2745 				*exists = TRUE;
2746 				set_logindev_perms(devlink);
2747 				return (DEVFSADM_SUCCESS);
2748 			} else {
2749 				switch (errno) {
2750 
2751 				case ENOENT:
2752 					/* dirpath to node doesn't exist */
2753 					hide = strrchr(devlink, '/');
2754 					*hide = '\0';
2755 					s_mkdirp(devlink, S_IRWXU|S_IRGRP|
2756 					    S_IXGRP|S_IROTH|S_IXOTH);
2757 					*hide = '/';
2758 					break;
2759 				case EEXIST:
2760 					try = READ_LINK;
2761 					break;
2762 				default:
2763 					err_print(SYMLINK_FAILED, devlink,
2764 					    contents, strerror(errno));
2765 					return (DEVFSADM_FAILURE);
2766 				}
2767 			}
2768 			break;
2769 
2770 		case READ_LINK:
2771 
2772 			/*
2773 			 * If there is redirection, new phys path
2774 			 * and old phys path will not match and the
2775 			 * link will be created with new phys path
2776 			 * which is what we want. So we want real
2777 			 * contents.
2778 			 */
2779 			linksize = readlink(devlink, checkcontents, PATH_MAX);
2780 			if (linksize >= 0) {
2781 				checkcontents[linksize] = '\0';
2782 				if (strcmp(checkcontents, contents) != 0) {
2783 					s_unlink(devlink);
2784 					vprint(VERBOSE_MID, REMOVING_LINK,
2785 					    devlink, checkcontents);
2786 					try = CREATE_LINK;
2787 				} else {
2788 					prev_link_existed = TRUE;
2789 					vprint(CHATTY_MID,
2790 					    "link exists and is correct:"
2791 					    " %s -> %s\n", devlink, contents);
2792 					*exists = TRUE;
2793 					/* failure in that the link existed */
2794 					return (DEVFSADM_FAILURE);
2795 				}
2796 			} else {
2797 				switch (errno) {
2798 				case EINVAL:
2799 					/* not a symlink, remove and create */
2800 					s_unlink(devlink);
2801 				default:
2802 					/* maybe it didn't exist at all */
2803 					try = CREATE_LINK;
2804 					break;
2805 				}
2806 			}
2807 			break;
2808 		}
2809 	}
2810 	err_print(MAX_ATTEMPTS, devlink, contents);
2811 	return (DEVFSADM_FAILURE);
2812 }
2813 
2814 static void
2815 set_logindev_perms(char *devlink)
2816 {
2817 	struct login_dev *newdev;
2818 	struct passwd pwd, *resp;
2819 	char pwd_buf[PATH_MAX];
2820 	int rv;
2821 	struct stat sb;
2822 	char *devfs_path = NULL;
2823 
2824 	/*
2825 	 * We only want logindev perms to be set when a device is
2826 	 * hotplugged or an application requests synchronous creates.
2827 	 * So we enable this only in daemon mode. In addition,
2828 	 * login(1) only fixes the std. /dev dir. So we don't
2829 	 * change perms if alternate root is set.
2830 	 * login_dev_enable is TRUE only in these cases.
2831 	 */
2832 	if (login_dev_enable != TRUE)
2833 		return;
2834 
2835 	/*
2836 	 * Normally, /etc/logindevperm has few (8 - 10 entries) which
2837 	 * may be regular expressions (globs were converted to RE).
2838 	 * So just do a linear search through the list.
2839 	 */
2840 	for (newdev = login_dev_cache; newdev; newdev = newdev->ldev_next) {
2841 		vprint(FILES_MID, "matching %s with %s\n", devlink,
2842 		    newdev->ldev_device);
2843 
2844 		if (regexec(&newdev->ldev_device_regex, devlink, 0,
2845 		    NULL, 0) == 0)  {
2846 			vprint(FILES_MID, "matched %s with %s\n", devlink,
2847 			    newdev->ldev_device);
2848 			break;
2849 		}
2850 	}
2851 
2852 	if (newdev == NULL)
2853 		return;
2854 
2855 	/*
2856 	 * we have a match, now find the driver associated with this
2857 	 * minor node using a snapshot on the physical path
2858 	 */
2859 	(void) resolve_link(devlink, NULL, NULL, &devfs_path, 0);
2860 	/*
2861 	 * We dont need redirection here - the actual link contents
2862 	 * whether "alias" or "current" are fine
2863 	 */
2864 	if (devfs_path) {
2865 		di_node_t node;
2866 		char *drv;
2867 		struct driver_list *list;
2868 		char *p;
2869 
2870 		/* truncate on : so we can take a snapshot */
2871 		(void) strcpy(pwd_buf, devfs_path);
2872 		p = strrchr(pwd_buf, ':');
2873 		if (p == NULL) {
2874 			free(devfs_path);
2875 			return;
2876 		}
2877 		*p = '\0';
2878 
2879 		vprint(FILES_MID, "link=%s->physpath=%s\n",
2880 		    devlink, pwd_buf);
2881 
2882 		node = di_init(pwd_buf, DINFOMINOR);
2883 
2884 		drv = NULL;
2885 		if (node) {
2886 			drv = di_driver_name(node);
2887 
2888 			if (drv) {
2889 				vprint(FILES_MID, "%s: driver is %s\n",
2890 				    devlink, drv);
2891 			}
2892 		}
2893 		/* search thru the driver list specified in logindevperm */
2894 		list = newdev->ldev_driver_list;
2895 		if ((drv != NULL) && (list != NULL)) {
2896 			while (list) {
2897 				if (strcmp(list->driver_name,
2898 				    drv) == 0) {
2899 					vprint(FILES_MID,
2900 					    "driver %s match!\n", drv);
2901 					break;
2902 				}
2903 				list = list->next;
2904 			}
2905 			if (list == NULL) {
2906 				vprint(FILES_MID, "no driver match!\n");
2907 				free(devfs_path);
2908 				return;
2909 			}
2910 		}
2911 		free(devfs_path);
2912 		di_fini(node);
2913 	} else {
2914 		return;
2915 	}
2916 
2917 	vprint(FILES_MID, "changing permissions of %s\n", devlink);
2918 
2919 	/*
2920 	 * We have a match. We now attempt to determine the
2921 	 * owner and group of the console user.
2922 	 *
2923 	 * stat() the console device newdev->ldev_console
2924 	 * which will always exist - it will have the right owner but
2925 	 * not the right group. Use getpwuid_r() to determine group for this
2926 	 * uid.
2927 	 * Note, it is safe to use name service here since if name services
2928 	 * are not available (during boot or in single-user mode), then
2929 	 * console owner will be root and its gid can be found in
2930 	 * local files.
2931 	 */
2932 	if (stat(newdev->ldev_console, &sb) == -1) {
2933 		vprint(VERBOSE_MID, STAT_FAILED, newdev->ldev_console,
2934 		    strerror(errno));
2935 		return;
2936 	}
2937 
2938 	resp = NULL;
2939 	rv = getpwuid_r(sb.st_uid, &pwd, pwd_buf, sizeof (pwd_buf), &resp);
2940 	if (rv || resp == NULL) {
2941 		rv = rv ? rv : EINVAL;
2942 		vprint(VERBOSE_MID, GID_FAILED, sb.st_uid,
2943 		    strerror(rv));
2944 		return;
2945 	}
2946 
2947 	assert(&pwd == resp);
2948 
2949 	sb.st_gid = resp->pw_gid;
2950 
2951 	if (chmod(devlink, newdev->ldev_perms) == -1) {
2952 		vprint(VERBOSE_MID, CHMOD_FAILED, devlink,
2953 		    strerror(errno));
2954 		return;
2955 	}
2956 
2957 	if (chown(devlink, sb.st_uid, sb.st_gid)  == -1) {
2958 		vprint(VERBOSE_MID, CHOWN_FAILED, devlink,
2959 		    strerror(errno));
2960 	}
2961 }
2962 
2963 /*
2964  * Reset /devices node with appropriate permissions and
2965  * ownership as specified in /etc/minor_perm.
2966  */
2967 static void
2968 reset_node_permissions(di_node_t node, di_minor_t minor)
2969 {
2970 	int spectype;
2971 	char phy_path[PATH_MAX + 1];
2972 	mode_t mode;
2973 	dev_t dev;
2974 	uid_t uid;
2975 	gid_t gid;
2976 	struct stat sb;
2977 	char *dev_path, *aminor = NULL;
2978 
2979 	/* lphy_path starts with / */
2980 	if ((dev_path = di_devfs_path(node)) == NULL) {
2981 		err_print(DI_DEVFS_PATH_FAILED, strerror(errno));
2982 		devfsadm_exit(1);
2983 		/*NOTREACHED*/
2984 	}
2985 	(void) strcpy(lphy_path, dev_path);
2986 	di_devfs_path_free(dev_path);
2987 
2988 	(void) strcat(lphy_path, ":");
2989 	if (di_minor_type(minor) == DDM_ALIAS) {
2990 		char *driver;
2991 		aminor = di_minor_name(minor);
2992 		driver = di_driver_name(di_minor_devinfo(minor));
2993 		(void) strcat(lphy_path, driver);
2994 	} else
2995 		(void) strcat(lphy_path, di_minor_name(minor));
2996 
2997 	(void) strcpy(phy_path, devices_dir);
2998 	(void) strcat(phy_path, lphy_path);
2999 
3000 	lnode = node;
3001 	lminor = minor;
3002 
3003 	vprint(CHATTY_MID, "reset_node_permissions: phy_path=%s lphy_path=%s\n",
3004 	    phy_path, lphy_path);
3005 
3006 	dev = di_minor_devt(minor);
3007 	spectype = di_minor_spectype(minor); /* block or char */
3008 
3009 	getattr(phy_path, aminor, spectype, dev, &mode, &uid, &gid);
3010 
3011 	/*
3012 	 * compare and set permissions and ownership
3013 	 *
3014 	 * Under devfs, a quick insertion and removal of USB devices
3015 	 * would cause stat of physical path to fail. In this case,
3016 	 * we emit a verbose message, but don't print errors.
3017 	 */
3018 	if ((stat(phy_path, &sb) == -1) || (sb.st_rdev != dev)) {
3019 		vprint(VERBOSE_MID, NO_DEVFS_NODE, phy_path);
3020 		return;
3021 	}
3022 
3023 	/*
3024 	 * If we are here for a new device
3025 	 *	If device allocation is on
3026 	 *	then
3027 	 *		set ownership to root:other and permissions to 0000
3028 	 *	else
3029 	 *		set ownership and permissions as specified in minor_perm
3030 	 * If we are here for an existing device
3031 	 *	If device allocation is to be turned on
3032 	 *	then
3033 	 *		reset ownership to root:other and permissions to 0000
3034 	 *	else if device allocation is to be turned off
3035 	 *		reset ownership and permissions to those specified in
3036 	 *		minor_perm
3037 	 *	else
3038 	 *		preserve existing/user-modified ownership and
3039 	 *		permissions
3040 	 *
3041 	 * devfs indicates a new device by faking access time to be zero.
3042 	 */
3043 	if (sb.st_atime != 0) {
3044 		int  i;
3045 		char *nt;
3046 
3047 		if ((devalloc_flag == 0) && (devalloc_is_on != 1))
3048 			/*
3049 			 * Leave existing devices as they are if we are not
3050 			 * turning device allocation on/off.
3051 			 */
3052 			return;
3053 
3054 		nt = di_minor_nodetype(minor);
3055 
3056 		if (nt == NULL)
3057 			return;
3058 
3059 		for (i = 0; devalloc_list[i]; i++) {
3060 			if (strcmp(nt, devalloc_list[i]) == 0)
3061 				/*
3062 				 * One of the types recognized by devalloc,
3063 				 * reset attrs.
3064 				 */
3065 				break;
3066 		}
3067 		if (devalloc_list[i] == NULL)
3068 			return;
3069 	}
3070 
3071 	if (file_mods == FALSE) {
3072 		/* Nothing more to do if simulating */
3073 		vprint(VERBOSE_MID, PERM_MSG, phy_path, uid, gid, mode);
3074 		return;
3075 	}
3076 
3077 	if ((devalloc_flag == DA_ON) ||
3078 	    ((devalloc_is_on == 1) && (devalloc_flag != DA_OFF))) {
3079 		/*
3080 		 * we are here either to turn device allocation on or
3081 		 * to add a new device while device allocation is on
3082 		 * (and we've confirmed that we're not turning it
3083 		 * off).
3084 		 */
3085 		mode = DEALLOC_MODE;
3086 		uid = DA_UID;
3087 		gid = DA_GID;
3088 	}
3089 
3090 	if ((devalloc_is_on == 1) || (devalloc_flag == DA_ON) ||
3091 	    (sb.st_mode != mode)) {
3092 		if (chmod(phy_path, mode) == -1)
3093 			vprint(VERBOSE_MID, CHMOD_FAILED,
3094 			    phy_path, strerror(errno));
3095 	}
3096 	if ((devalloc_is_on == 1) || (devalloc_flag == DA_ON) ||
3097 	    (sb.st_uid != uid || sb.st_gid != gid)) {
3098 		if (chown(phy_path, uid, gid) == -1)
3099 			vprint(VERBOSE_MID, CHOWN_FAILED,
3100 			    phy_path, strerror(errno));
3101 	}
3102 
3103 	/* Report that we actually did something */
3104 	vprint(VERBOSE_MID, PERM_MSG, phy_path, uid, gid, mode);
3105 }
3106 
3107 /*
3108  * Removes logical link and the minor node it refers to.  If file is a
3109  * link, we recurse and try to remove the minor node (or link if path is
3110  * a double link) that file's link contents refer to.
3111  */
3112 static void
3113 devfsadm_rm_work(char *file, int recurse, int file_type)
3114 {
3115 	char *fcn = "devfsadm_rm_work: ";
3116 	int linksize;
3117 	char contents[PATH_MAX + 1];
3118 	char nextfile[PATH_MAX + 1];
3119 	char newfile[PATH_MAX + 1];
3120 	char *ptr;
3121 
3122 	vprint(REMOVE_MID, "%s%s\n", fcn, file);
3123 
3124 	/*
3125 	 * Note: we don't remove /devices (non-links) entries because they are
3126 	 *	covered by devfs.
3127 	 */
3128 	if (file_type != TYPE_LINK) {
3129 		return;
3130 	}
3131 
3132 	/* split into multiple if's due to excessive indentations */
3133 	(void) strcpy(newfile, dev_dir);
3134 	(void) strcat(newfile, "/");
3135 	(void) strcat(newfile, file);
3136 
3137 	/*
3138 	 * we dont care about the content of the symlink, so
3139 	 * redirection is not needed.
3140 	 */
3141 	if ((recurse == TRUE) &&
3142 	    ((linksize = readlink(newfile, contents, PATH_MAX)) > 0)) {
3143 		contents[linksize] = '\0';
3144 
3145 		/*
3146 		 * recurse if link points to another link
3147 		 */
3148 		if (is_minor_node(contents, &ptr) != DEVFSADM_TRUE) {
3149 			if (strncmp(contents, DEV "/", strlen(DEV) + 1) == 0) {
3150 				devfsadm_rm_work(&contents[strlen(DEV) + 1],
3151 				    TRUE, TYPE_LINK);
3152 			} else {
3153 				if ((ptr = strrchr(file, '/')) != NULL) {
3154 					*ptr = '\0';
3155 					(void) strcpy(nextfile, file);
3156 					*ptr = '/';
3157 					(void) strcat(nextfile, "/");
3158 				} else {
3159 					(void) strcpy(nextfile, "");
3160 				}
3161 				(void) strcat(nextfile, contents);
3162 				devfsadm_rm_work(nextfile, TRUE, TYPE_LINK);
3163 			}
3164 		}
3165 	}
3166 
3167 	vprint(VERBOSE_MID, DEVFSADM_UNLINK, newfile);
3168 	if (file_mods == TRUE) {
3169 		rm_link_from_cache(file);
3170 		s_unlink(newfile);
3171 		rm_parent_dir_if_empty(newfile);
3172 		invalidate_enumerate_cache();
3173 		(void) di_devlink_rm_link(devlink_cache, file);
3174 	}
3175 }
3176 
3177 void
3178 devfsadm_rm_link(char *file)
3179 {
3180 	devfsadm_rm_work(file, FALSE, TYPE_LINK);
3181 }
3182 
3183 void
3184 devfsadm_rm_all(char *file)
3185 {
3186 	devfsadm_rm_work(file, TRUE, TYPE_LINK);
3187 }
3188 
3189 static int
3190 s_rmdir(char *path)
3191 {
3192 	int	i;
3193 	char	*rpath, *dir;
3194 	const char *fcn = "s_rmdir";
3195 
3196 	/*
3197 	 * Certain directories are created at install time by packages.
3198 	 * Some of them (listed in packaged_dirs[]) are required by apps
3199 	 * and need to be present even when empty.
3200 	 */
3201 	vprint(REMOVE_MID, "%s: checking if %s is packaged\n", fcn, path);
3202 
3203 	rpath = path + strlen(dev_dir) + 1;
3204 
3205 	for (i = 0; (dir = packaged_dirs[i]) != NULL; i++) {
3206 		if (*rpath == *dir) {
3207 			if (strcmp(rpath, dir) == 0) {
3208 				vprint(REMOVE_MID, "%s: skipping packaged dir: "
3209 				    "%s\n", fcn, path);
3210 				errno = EEXIST;
3211 				return (-1);
3212 			}
3213 		}
3214 	}
3215 
3216 	return (rmdir(path));
3217 }
3218 
3219 /*
3220  * Try to remove any empty directories up the tree.  It is assumed that
3221  * pathname is a file that was removed, so start with its parent, and
3222  * work up the tree.
3223  */
3224 static void
3225 rm_parent_dir_if_empty(char *pathname)
3226 {
3227 	char *ptr, path[PATH_MAX + 1];
3228 	char *fcn = "rm_parent_dir_if_empty: ";
3229 
3230 	vprint(REMOVE_MID, "%schecking %s if empty\n", fcn, pathname);
3231 
3232 	(void) strcpy(path, pathname);
3233 
3234 	/*
3235 	 * ascend up the dir tree, deleting all empty dirs.
3236 	 * Return immediately if a dir is not empty.
3237 	 */
3238 	for (;;) {
3239 
3240 		if ((ptr = strrchr(path, '/')) == NULL) {
3241 			return;
3242 		}
3243 
3244 		*ptr = '\0';
3245 
3246 		if (finddev_emptydir(path)) {
3247 			/* directory is empty */
3248 			if (s_rmdir(path) == 0) {
3249 				vprint(REMOVE_MID,
3250 				    "%sremoving empty dir %s\n", fcn, path);
3251 			} else if (errno == EEXIST) {
3252 				vprint(REMOVE_MID,
3253 				    "%sfailed to remove dir: %s\n", fcn, path);
3254 				return;
3255 			}
3256 		} else {
3257 			/* some other file is here, so return */
3258 			vprint(REMOVE_MID, "%sdir not empty: %s\n", fcn, path);
3259 			return;
3260 		}
3261 	}
3262 }
3263 
3264 /*
3265  * This function and all the functions it calls below were added to
3266  * handle the unique problem with world wide names (WWN).  The problem is
3267  * that if a WWN device is moved to another address on the same controller
3268  * its logical link will change, while the physical node remains the same.
3269  * The result is that two logical links will point to the same physical path
3270  * in /devices, the valid link and a stale link. This function will
3271  * find all the stale nodes, though at a significant performance cost.
3272  *
3273  * Caching is used to increase performance.
3274  * A cache will be built from disk if the cache tag doesn't already exist.
3275  * The cache tag is a regular expression "dir_re", which selects a
3276  * subset of disks to search from typically something like
3277  * "dev/cXt[0-9]+d[0-9]+s[0-9]+".  After the cache is built, consistency must
3278  * be maintained, so entries are added as new links are created, and removed
3279  * as old links are deleted.  The whole cache is flushed if we are a daemon,
3280  * and another devfsadm process ran in between.
3281  *
3282  * Once the cache is built, this function finds the cache which matches
3283  * dir_re, and then it searches all links in that cache looking for
3284  * any link whose contents match "valid_link_contents" with a corresponding link
3285  * which does not match "valid_link".  Any such matches are stale and removed.
3286  *
3287  * This happens outside the context of a "reparenting" so we dont need
3288  * redirection.
3289  */
3290 void
3291 devfsadm_rm_stale_links(char *dir_re, char *valid_link, di_node_t node,
3292 			di_minor_t minor)
3293 {
3294 	link_t *link;
3295 	linkhead_t *head;
3296 	char phy_path[PATH_MAX + 1];
3297 	char *valid_link_contents;
3298 	char *dev_path;
3299 	char rmlink[PATH_MAX + 1];
3300 
3301 	/*
3302 	 * try to use devices path
3303 	 */
3304 	if ((node == lnode) && (minor == lminor)) {
3305 		valid_link_contents = lphy_path;
3306 	} else {
3307 		if ((dev_path = di_devfs_path(node)) == NULL) {
3308 			err_print(DI_DEVFS_PATH_FAILED, strerror(errno));
3309 			devfsadm_exit(1);
3310 			/*NOTREACHED*/
3311 		}
3312 		(void) strcpy(phy_path, dev_path);
3313 		di_devfs_path_free(dev_path);
3314 
3315 		(void) strcat(phy_path, ":");
3316 		(void) strcat(phy_path, di_minor_name(minor));
3317 		valid_link_contents = phy_path;
3318 	}
3319 
3320 	/*
3321 	 * As an optimization, check to make sure the corresponding
3322 	 * devlink was just created before continuing.
3323 	 */
3324 
3325 	if (linknew == FALSE) {
3326 		return;
3327 	}
3328 
3329 	head = get_cached_links(dir_re);
3330 
3331 	assert(head->nextlink == NULL);
3332 
3333 	for (link = head->link; link != NULL; link = head->nextlink) {
3334 		/*
3335 		 * See hot_cleanup() for why we do this
3336 		 */
3337 		head->nextlink = link->next;
3338 		if ((strcmp(link->contents, valid_link_contents) == 0) &&
3339 		    (strcmp(link->devlink, valid_link) != 0)) {
3340 			vprint(CHATTY_MID, "removing %s -> %s\n"
3341 			    "valid link is: %s -> %s\n",
3342 			    link->devlink, link->contents,
3343 			    valid_link, valid_link_contents);
3344 			/*
3345 			 * Use a copy of the cached link name as the
3346 			 * cache entry will go away during link removal
3347 			 */
3348 			(void) snprintf(rmlink, sizeof (rmlink), "%s",
3349 			    link->devlink);
3350 			devfsadm_rm_link(rmlink);
3351 		}
3352 	}
3353 }
3354 
3355 /*
3356  * Return previously created cache, or create cache.
3357  */
3358 static linkhead_t *
3359 get_cached_links(char *dir_re)
3360 {
3361 	recurse_dev_t rd;
3362 	linkhead_t *linkhead;
3363 	int n;
3364 
3365 	vprint(BUILDCACHE_MID, "get_cached_links: %s\n", dir_re);
3366 
3367 	for (linkhead = headlinkhead; linkhead != NULL;
3368 	    linkhead = linkhead->nexthead) {
3369 		if (strcmp(linkhead->dir_re, dir_re) == 0) {
3370 			return (linkhead);
3371 		}
3372 	}
3373 
3374 	/*
3375 	 * This tag is not in cache, so add it, along with all its
3376 	 * matching /dev entries.  This is the only time we go to disk.
3377 	 */
3378 	linkhead = s_malloc(sizeof (linkhead_t));
3379 	linkhead->nexthead = headlinkhead;
3380 	headlinkhead = linkhead;
3381 	linkhead->dir_re = s_strdup(dir_re);
3382 
3383 	if ((n = regcomp(&(linkhead->dir_re_compiled), dir_re,
3384 	    REG_EXTENDED)) != 0) {
3385 		err_print(REGCOMP_FAILED,  dir_re, n);
3386 	}
3387 
3388 	linkhead->nextlink = NULL;
3389 	linkhead->link = NULL;
3390 
3391 	rd.fcn = build_devlink_list;
3392 	rd.data = (void *)linkhead;
3393 
3394 	vprint(BUILDCACHE_MID, "get_cached_links: calling recurse_dev_re\n");
3395 
3396 	/* call build_devlink_list for each directory in the dir_re RE */
3397 	if (dir_re[0] == '/') {
3398 		recurse_dev_re("/", &dir_re[1], &rd);
3399 	} else {
3400 		recurse_dev_re(dev_dir, dir_re, &rd);
3401 	}
3402 
3403 	return (linkhead);
3404 }
3405 
3406 static void
3407 build_devlink_list(char *devlink, void *data)
3408 {
3409 	char *fcn = "build_devlink_list: ";
3410 	char *ptr;
3411 	char *r_contents;
3412 	char *r_devlink;
3413 	char contents[PATH_MAX + 1];
3414 	char newlink[PATH_MAX + 1];
3415 	char stage_link[PATH_MAX + 1];
3416 	int linksize;
3417 	linkhead_t *linkhead = (linkhead_t *)data;
3418 	link_t *link;
3419 	int i = 0;
3420 
3421 	vprint(BUILDCACHE_MID, "%scheck_link: %s\n", fcn, devlink);
3422 
3423 	(void) strcpy(newlink, devlink);
3424 
3425 	do {
3426 		/*
3427 		 * None of the consumers of this function need redirection
3428 		 * so this readlink gets the "current" contents
3429 		 */
3430 		linksize = readlink(newlink, contents, PATH_MAX);
3431 		if (linksize <= 0) {
3432 			/*
3433 			 * The first pass through the do loop we may readlink()
3434 			 * non-symlink files(EINVAL) from false regexec matches.
3435 			 * Suppress error messages in those cases or if the link
3436 			 * content is the empty string.
3437 			 */
3438 			if (linksize < 0 && (i || errno != EINVAL))
3439 				err_print(READLINK_FAILED, "build_devlink_list",
3440 				    newlink, strerror(errno));
3441 			return;
3442 		}
3443 		contents[linksize] = '\0';
3444 		i = 1;
3445 
3446 		if (is_minor_node(contents, &r_contents) == DEVFSADM_FALSE) {
3447 			/*
3448 			 * assume that link contents is really a pointer to
3449 			 * another link, so recurse and read its link contents.
3450 			 *
3451 			 * some link contents are absolute:
3452 			 *	/dev/audio -> /dev/sound/0
3453 			 */
3454 			if (strncmp(contents, DEV "/",
3455 			    strlen(DEV) + strlen("/")) != 0) {
3456 
3457 				if ((ptr = strrchr(newlink, '/')) == NULL) {
3458 					vprint(REMOVE_MID, "%s%s -> %s invalid "
3459 					    "link. missing '/'\n", fcn,
3460 					    newlink, contents);
3461 					return;
3462 				}
3463 				*ptr = '\0';
3464 				(void) strcpy(stage_link, newlink);
3465 				*ptr = '/';
3466 				(void) strcat(stage_link, "/");
3467 				(void) strcat(stage_link, contents);
3468 				(void) strcpy(newlink, stage_link);
3469 			} else {
3470 				(void) strcpy(newlink, dev_dir);
3471 				(void) strcat(newlink, "/");
3472 				(void) strcat(newlink,
3473 				    &contents[strlen(DEV) + strlen("/")]);
3474 			}
3475 
3476 		} else {
3477 			newlink[0] = '\0';
3478 		}
3479 	} while (newlink[0] != '\0');
3480 
3481 	if (strncmp(devlink, dev_dir, strlen(dev_dir)) != 0) {
3482 		vprint(BUILDCACHE_MID, "%sinvalid link: %s\n", fcn, devlink);
3483 		return;
3484 	}
3485 
3486 	r_devlink = devlink + strlen(dev_dir);
3487 
3488 	if (r_devlink[0] != '/')
3489 		return;
3490 
3491 	link = s_malloc(sizeof (link_t));
3492 
3493 	/* don't store the '/' after rootdir/dev */
3494 	r_devlink += 1;
3495 
3496 	vprint(BUILDCACHE_MID, "%scaching link: %s\n", fcn, r_devlink);
3497 	link->devlink = s_strdup(r_devlink);
3498 
3499 	link->contents = s_strdup(r_contents);
3500 
3501 	link->next = linkhead->link;
3502 	linkhead->link = link;
3503 }
3504 
3505 /*
3506  * to be consistent, devlink must not begin with / and must be
3507  * relative to /dev/, whereas physpath must contain / and be
3508  * relative to /devices.
3509  */
3510 static void
3511 add_link_to_cache(char *devlink, char *physpath)
3512 {
3513 	linkhead_t *linkhead;
3514 	link_t *link;
3515 	int added = 0;
3516 
3517 	if (file_mods == FALSE) {
3518 		return;
3519 	}
3520 
3521 	vprint(CACHE_MID, "add_link_to_cache: %s -> %s ",
3522 	    devlink, physpath);
3523 
3524 	for (linkhead = headlinkhead; linkhead != NULL;
3525 	    linkhead = linkhead->nexthead) {
3526 		if (regexec(&(linkhead->dir_re_compiled), devlink, 0, NULL, 0)
3527 		    == 0) {
3528 			added++;
3529 			link = s_malloc(sizeof (link_t));
3530 			link->devlink = s_strdup(devlink);
3531 			link->contents = s_strdup(physpath);
3532 			link->next = linkhead->link;
3533 			linkhead->link = link;
3534 		}
3535 	}
3536 
3537 	vprint(CACHE_MID,
3538 	    " %d %s\n", added, added == 0 ? "NOT ADDED" : "ADDED");
3539 }
3540 
3541 /*
3542  * Remove devlink from cache.  Devlink must be relative to /dev/ and not start
3543  * with /.
3544  */
3545 static void
3546 rm_link_from_cache(char *devlink)
3547 {
3548 	linkhead_t *linkhead;
3549 	link_t **linkp;
3550 	link_t *save;
3551 
3552 	vprint(CACHE_MID, "rm_link_from_cache enter: %s\n", devlink);
3553 
3554 	for (linkhead = headlinkhead; linkhead != NULL;
3555 	    linkhead = linkhead->nexthead) {
3556 		if (regexec(&(linkhead->dir_re_compiled), devlink, 0, NULL, 0)
3557 		    == 0) {
3558 
3559 			for (linkp = &(linkhead->link); *linkp != NULL; ) {
3560 				if ((strcmp((*linkp)->devlink, devlink) == 0)) {
3561 					save = *linkp;
3562 					*linkp = (*linkp)->next;
3563 					/*
3564 					 * We are removing our caller's
3565 					 * "next" link. Update the nextlink
3566 					 * field in the head so that our
3567 					 * callers accesses the next valid
3568 					 * link
3569 					 */
3570 					if (linkhead->nextlink == save)
3571 						linkhead->nextlink = *linkp;
3572 					free(save->devlink);
3573 					free(save->contents);
3574 					free(save);
3575 					vprint(CACHE_MID, " %s FREED FROM "
3576 					    "CACHE\n", devlink);
3577 				} else {
3578 					linkp = &((*linkp)->next);
3579 				}
3580 			}
3581 		}
3582 	}
3583 }
3584 
3585 static void
3586 rm_all_links_from_cache()
3587 {
3588 	linkhead_t *linkhead;
3589 	linkhead_t *nextlinkhead;
3590 	link_t *link;
3591 	link_t *nextlink;
3592 
3593 	vprint(CACHE_MID, "rm_all_links_from_cache\n");
3594 
3595 	for (linkhead = headlinkhead; linkhead != NULL;
3596 	    linkhead = nextlinkhead) {
3597 
3598 		nextlinkhead = linkhead->nexthead;
3599 		assert(linkhead->nextlink == NULL);
3600 		for (link = linkhead->link; link != NULL; link = nextlink) {
3601 			nextlink = link->next;
3602 			free(link->devlink);
3603 			free(link->contents);
3604 			free(link);
3605 		}
3606 		regfree(&(linkhead->dir_re_compiled));
3607 		free(linkhead->dir_re);
3608 		free(linkhead);
3609 	}
3610 	headlinkhead = NULL;
3611 }
3612 
3613 /*
3614  * Called when the kernel has modified the incore path_to_inst data.  This
3615  * function will schedule a flush of the data to the filesystem.
3616  */
3617 static void
3618 devfs_instance_mod(void)
3619 {
3620 	char *fcn = "devfs_instance_mod: ";
3621 	vprint(PATH2INST_MID, "%senter\n", fcn);
3622 
3623 	/* signal instance thread */
3624 	(void) mutex_lock(&count_lock);
3625 	inst_count++;
3626 	(void) cond_signal(&cv);
3627 	(void) mutex_unlock(&count_lock);
3628 }
3629 
3630 static void
3631 instance_flush_thread(void)
3632 {
3633 	int i;
3634 	int idle;
3635 
3636 	for (;;) {
3637 
3638 		(void) mutex_lock(&count_lock);
3639 		while (inst_count == 0) {
3640 			(void) cond_wait(&cv, &count_lock);
3641 		}
3642 		inst_count = 0;
3643 
3644 		vprint(PATH2INST_MID, "signaled to flush path_to_inst."
3645 		    " Enter delay loop\n");
3646 		/*
3647 		 * Wait MAX_IDLE_DELAY seconds after getting the last flush
3648 		 * path_to_inst event before invoking a flush, but never wait
3649 		 * more than MAX_DELAY seconds after getting the first event.
3650 		 */
3651 		for (idle = 0, i = 0; i < MAX_DELAY; i++) {
3652 
3653 			(void) mutex_unlock(&count_lock);
3654 			(void) sleep(1);
3655 			(void) mutex_lock(&count_lock);
3656 
3657 			/* shorten the delay if we are idle */
3658 			if (inst_count == 0) {
3659 				idle++;
3660 				if (idle > MAX_IDLE_DELAY) {
3661 					break;
3662 				}
3663 			} else {
3664 				inst_count = idle = 0;
3665 			}
3666 		}
3667 
3668 		(void) mutex_unlock(&count_lock);
3669 
3670 		flush_path_to_inst();
3671 	}
3672 }
3673 
3674 /*
3675  * Helper function for flush_path_to_inst() below; this routine calls the
3676  * inst_sync syscall to flush the path_to_inst database to the given file.
3677  */
3678 static int
3679 do_inst_sync(char *filename, char *instfilename)
3680 {
3681 	void (*sigsaved)(int);
3682 	int err = 0, flags = INST_SYNC_IF_REQUIRED;
3683 	struct stat sb;
3684 
3685 	if (stat(instfilename, &sb) == -1 && errno == ENOENT)
3686 		flags = INST_SYNC_ALWAYS;
3687 
3688 	vprint(INSTSYNC_MID, "do_inst_sync: about to flush %s\n", filename);
3689 	sigsaved = sigset(SIGSYS, SIG_IGN);
3690 	if (inst_sync(filename, flags) == -1)
3691 		err = errno;
3692 	(void) sigset(SIGSYS, sigsaved);
3693 
3694 	switch (err) {
3695 	case 0:
3696 		return (DEVFSADM_SUCCESS);
3697 	case EALREADY:	/* no-op, path_to_inst already up to date */
3698 		return (EALREADY);
3699 	case ENOSYS:
3700 		err_print(CANT_LOAD_SYSCALL);
3701 		break;
3702 	case EPERM:
3703 		err_print(SUPER_TO_SYNC);
3704 		break;
3705 	default:
3706 		err_print(INSTSYNC_FAILED, filename, strerror(err));
3707 		break;
3708 	}
3709 	return (DEVFSADM_FAILURE);
3710 }
3711 
3712 /*
3713  * Flush the kernel's path_to_inst database to /etc/path_to_inst.  To do so
3714  * safely, the database is flushed to a temporary file, then moved into place.
3715  *
3716  * The following files are used during this process:
3717  * 	/etc/path_to_inst:	The path_to_inst file
3718  * 	/etc/path_to_inst.<pid>: Contains data flushed from the kernel
3719  * 	/etc/path_to_inst.old:  The backup file
3720  * 	/etc/path_to_inst.old.<pid>: Temp file for creating backup
3721  *
3722  */
3723 static void
3724 flush_path_to_inst(void)
3725 {
3726 	char *new_inst_file = NULL;
3727 	char *old_inst_file = NULL;
3728 	char *old_inst_file_npid = NULL;
3729 	FILE *inst_file_fp = NULL;
3730 	FILE *old_inst_file_fp = NULL;
3731 	struct stat sb;
3732 	int err = 0;
3733 	int c;
3734 	int inst_strlen;
3735 
3736 	vprint(PATH2INST_MID, "flush_path_to_inst: %s\n",
3737 	    (flush_path_to_inst_enable == TRUE) ? "ENABLED" : "DISABLED");
3738 
3739 	if (flush_path_to_inst_enable == FALSE) {
3740 		return;
3741 	}
3742 
3743 	inst_strlen = strlen(inst_file);
3744 	new_inst_file = s_malloc(inst_strlen + PID_STR_LEN + 2);
3745 	old_inst_file = s_malloc(inst_strlen + PID_STR_LEN + 6);
3746 	old_inst_file_npid = s_malloc(inst_strlen +
3747 	    sizeof (INSTANCE_FILE_SUFFIX));
3748 
3749 	(void) snprintf(new_inst_file, inst_strlen + PID_STR_LEN + 2,
3750 	    "%s.%ld", inst_file, getpid());
3751 
3752 	if (stat(new_inst_file, &sb) == 0) {
3753 		s_unlink(new_inst_file);
3754 	}
3755 
3756 	err = do_inst_sync(new_inst_file, inst_file);
3757 	if (err != DEVFSADM_SUCCESS) {
3758 		goto out;
3759 		/*NOTREACHED*/
3760 	}
3761 
3762 	/*
3763 	 * Now we deal with the somewhat tricky updating and renaming
3764 	 * of this critical piece of kernel state.
3765 	 */
3766 
3767 	/*
3768 	 * Copy the current instance file into a temporary file.
3769 	 * Then rename the temporary file into the backup (.old)
3770 	 * file and rename the newly flushed kernel data into
3771 	 * the instance file.
3772 	 * Of course if 'inst_file' doesn't exist, there's much
3773 	 * less for us to do .. tee hee.
3774 	 */
3775 	if ((inst_file_fp = fopen(inst_file, "r")) == NULL) {
3776 		/*
3777 		 * No such file.  Rename the new onto the old
3778 		 */
3779 		if ((err = rename(new_inst_file, inst_file)) != 0)
3780 			err_print(RENAME_FAILED, inst_file, strerror(errno));
3781 		goto out;
3782 		/*NOTREACHED*/
3783 	}
3784 
3785 	(void) snprintf(old_inst_file, inst_strlen + PID_STR_LEN + 6,
3786 	    "%s.old.%ld", inst_file, getpid());
3787 
3788 	if (stat(old_inst_file, &sb) == 0) {
3789 		s_unlink(old_inst_file);
3790 	}
3791 
3792 	if ((old_inst_file_fp = fopen(old_inst_file, "w")) == NULL) {
3793 		/*
3794 		 * Can't open the 'old_inst_file' file for writing.
3795 		 * This is somewhat strange given that the syscall
3796 		 * just succeeded to write a file out.. hmm.. maybe
3797 		 * the fs just filled up or something nasty.
3798 		 *
3799 		 * Anyway, abort what we've done so far.
3800 		 */
3801 		err_print(CANT_UPDATE, old_inst_file);
3802 		err = DEVFSADM_FAILURE;
3803 		goto out;
3804 		/*NOTREACHED*/
3805 	}
3806 
3807 	/*
3808 	 * Copy current instance file into the temporary file
3809 	 */
3810 	err = 0;
3811 	while ((c = getc(inst_file_fp)) != EOF) {
3812 		if ((err = putc(c, old_inst_file_fp)) == EOF) {
3813 			break;
3814 		}
3815 	}
3816 
3817 	if (fclose(old_inst_file_fp) == EOF || err == EOF) {
3818 		vprint(INFO_MID, CANT_UPDATE, old_inst_file);
3819 		err = DEVFSADM_FAILURE;
3820 		goto out;
3821 		/* NOTREACHED */
3822 	}
3823 
3824 	/*
3825 	 * Set permissions to be the same on the backup as
3826 	 * /etc/path_to_inst.
3827 	 */
3828 	(void) chmod(old_inst_file, 0444);
3829 
3830 	/*
3831 	 * So far, everything we've done is more or less reversible.
3832 	 * But now we're going to commit ourselves.
3833 	 */
3834 
3835 	(void) snprintf(old_inst_file_npid,
3836 	    inst_strlen + sizeof (INSTANCE_FILE_SUFFIX),
3837 	    "%s%s", inst_file, INSTANCE_FILE_SUFFIX);
3838 
3839 	if ((err = rename(old_inst_file, old_inst_file_npid)) != 0) {
3840 		err_print(RENAME_FAILED, old_inst_file_npid,
3841 		    strerror(errno));
3842 	} else if ((err = rename(new_inst_file, inst_file)) != 0) {
3843 		err_print(RENAME_FAILED, inst_file, strerror(errno));
3844 	}
3845 
3846 out:
3847 	if (inst_file_fp != NULL) {
3848 		if (fclose(inst_file_fp) == EOF) {
3849 			err_print(FCLOSE_FAILED, inst_file, strerror(errno));
3850 		}
3851 	}
3852 
3853 	if (stat(new_inst_file, &sb) == 0) {
3854 		s_unlink(new_inst_file);
3855 	}
3856 	free(new_inst_file);
3857 
3858 	if (stat(old_inst_file, &sb) == 0) {
3859 		s_unlink(old_inst_file);
3860 	}
3861 	free(old_inst_file);
3862 
3863 	free(old_inst_file_npid);
3864 
3865 	if (err != 0 && err != EALREADY) {
3866 		err_print(FAILED_TO_UPDATE, inst_file);
3867 	}
3868 }
3869 
3870 /*
3871  * detach from tty.  For daemon mode.
3872  */
3873 void
3874 detachfromtty()
3875 {
3876 	(void) setsid();
3877 	if (DEVFSADM_DEBUG_ON == TRUE) {
3878 		return;
3879 	}
3880 
3881 	(void) close(0);
3882 	(void) close(1);
3883 	(void) close(2);
3884 	(void) open("/dev/null", O_RDWR, 0);
3885 	(void) dup(0);
3886 	(void) dup(0);
3887 	openlog(DEVFSADMD, LOG_PID, LOG_DAEMON);
3888 	(void) setlogmask(LOG_UPTO(LOG_INFO));
3889 	logflag = TRUE;
3890 }
3891 
3892 /*
3893  * Use an advisory lock to synchronize updates to /dev.  If the lock is
3894  * held by another process, block in the fcntl() system call until that
3895  * process drops the lock or exits.  The lock file itself is
3896  * DEV_LOCK_FILE.  The process id of the current and last process owning
3897  * the lock is kept in the lock file.  After acquiring the lock, read the
3898  * process id and return it.  It is the process ID which last owned the
3899  * lock, and will be used to determine if caches need to be flushed.
3900  *
3901  * NOTE: if the devlink database is held open by the caller, it may
3902  * be closed by this routine. This is to enforce the following lock ordering:
3903  *	1) /dev lock 2) database open
3904  */
3905 pid_t
3906 enter_dev_lock()
3907 {
3908 	struct flock lock;
3909 	int n;
3910 	pid_t pid;
3911 	pid_t last_owner_pid;
3912 
3913 	if (file_mods == FALSE) {
3914 		return (0);
3915 	}
3916 
3917 	(void) snprintf(dev_lockfile, sizeof (dev_lockfile),
3918 	    "%s/%s", etc_dev_dir, DEV_LOCK_FILE);
3919 
3920 	vprint(LOCK_MID, "enter_dev_lock: lock file %s\n", dev_lockfile);
3921 
3922 	dev_lock_fd = open(dev_lockfile, O_CREAT|O_RDWR, 0644);
3923 	if (dev_lock_fd < 0) {
3924 		err_print(OPEN_FAILED, dev_lockfile, strerror(errno));
3925 		devfsadm_exit(1);
3926 		/*NOTREACHED*/
3927 	}
3928 
3929 	lock.l_type = F_WRLCK;
3930 	lock.l_whence = SEEK_SET;
3931 	lock.l_start = 0;
3932 	lock.l_len = 0;
3933 
3934 	/* try for the lock, but don't wait */
3935 	if (fcntl(dev_lock_fd, F_SETLK, &lock) == -1) {
3936 		if ((errno == EACCES) || (errno == EAGAIN)) {
3937 			pid = 0;
3938 			n = read(dev_lock_fd, &pid, sizeof (pid_t));
3939 			vprint(LOCK_MID, "waiting for PID %d to complete\n",
3940 			    (int)pid);
3941 			if (lseek(dev_lock_fd, 0, SEEK_SET) == (off_t)-1) {
3942 				err_print(LSEEK_FAILED, dev_lockfile,
3943 				    strerror(errno));
3944 				devfsadm_exit(1);
3945 				/*NOTREACHED*/
3946 			}
3947 			/*
3948 			 * wait for the dev lock. If we have the database open,
3949 			 * close it first - the order of lock acquisition should
3950 			 * always be:  1) dev_lock 2) database
3951 			 * This is to prevent deadlocks with any locks the
3952 			 * database code may hold.
3953 			 */
3954 			(void) di_devlink_close(&devlink_cache, 0);
3955 
3956 			/* send any sysevents that were queued up. */
3957 			process_syseventq();
3958 
3959 			if (fcntl(dev_lock_fd, F_SETLKW, &lock) == -1) {
3960 				err_print(LOCK_FAILED, dev_lockfile,
3961 				    strerror(errno));
3962 				devfsadm_exit(1);
3963 				/*NOTREACHED*/
3964 			}
3965 		}
3966 	}
3967 
3968 	hold_dev_lock = TRUE;
3969 	pid = 0;
3970 	n = read(dev_lock_fd, &pid, sizeof (pid_t));
3971 	if (n == sizeof (pid_t) && pid == getpid()) {
3972 		return (pid);
3973 	}
3974 
3975 	last_owner_pid = pid;
3976 
3977 	if (lseek(dev_lock_fd, 0, SEEK_SET) == (off_t)-1) {
3978 		err_print(LSEEK_FAILED, dev_lockfile, strerror(errno));
3979 		devfsadm_exit(1);
3980 		/*NOTREACHED*/
3981 	}
3982 	pid = getpid();
3983 	n = write(dev_lock_fd, &pid, sizeof (pid_t));
3984 	if (n != sizeof (pid_t)) {
3985 		err_print(WRITE_FAILED, dev_lockfile, strerror(errno));
3986 		devfsadm_exit(1);
3987 		/*NOTREACHED*/
3988 	}
3989 
3990 	return (last_owner_pid);
3991 }
3992 
3993 /*
3994  * Drop the advisory /dev lock, close lock file.  Close and re-open the
3995  * file every time so to ensure a resync if for some reason the lock file
3996  * gets removed.
3997  */
3998 void
3999 exit_dev_lock(int exiting)
4000 {
4001 	struct flock unlock;
4002 
4003 	if (hold_dev_lock == FALSE) {
4004 		return;
4005 	}
4006 
4007 	vprint(LOCK_MID, "exit_dev_lock: lock file %s, exiting = %d\n",
4008 	    dev_lockfile, exiting);
4009 
4010 	unlock.l_type = F_UNLCK;
4011 	unlock.l_whence = SEEK_SET;
4012 	unlock.l_start = 0;
4013 	unlock.l_len = 0;
4014 
4015 	if (fcntl(dev_lock_fd, F_SETLK, &unlock) == -1) {
4016 		err_print(UNLOCK_FAILED, dev_lockfile, strerror(errno));
4017 	}
4018 
4019 	hold_dev_lock = FALSE;
4020 
4021 	if (close(dev_lock_fd) == -1) {
4022 		err_print(CLOSE_FAILED, dev_lockfile, strerror(errno));
4023 		if (!exiting)
4024 			devfsadm_exit(1);
4025 			/*NOTREACHED*/
4026 	}
4027 }
4028 
4029 /*
4030  *
4031  * Use an advisory lock to ensure that only one daemon process is active
4032  * in the system at any point in time.	If the lock is held by another
4033  * process, do not block but return the pid owner of the lock to the
4034  * caller immediately.	The lock is cleared if the holding daemon process
4035  * exits for any reason even if the lock file remains, so the daemon can
4036  * be restarted if necessary.  The lock file is DAEMON_LOCK_FILE.
4037  */
4038 pid_t
4039 enter_daemon_lock(void)
4040 {
4041 	struct flock lock;
4042 
4043 	(void) snprintf(daemon_lockfile, sizeof (daemon_lockfile),
4044 	    "%s/%s", etc_dev_dir, DAEMON_LOCK_FILE);
4045 
4046 	vprint(LOCK_MID, "enter_daemon_lock: lock file %s\n", daemon_lockfile);
4047 
4048 	daemon_lock_fd = open(daemon_lockfile, O_CREAT|O_RDWR, 0644);
4049 	if (daemon_lock_fd < 0) {
4050 		err_print(OPEN_FAILED, daemon_lockfile, strerror(errno));
4051 		devfsadm_exit(1);
4052 		/*NOTREACHED*/
4053 	}
4054 
4055 	lock.l_type = F_WRLCK;
4056 	lock.l_whence = SEEK_SET;
4057 	lock.l_start = 0;
4058 	lock.l_len = 0;
4059 
4060 	if (fcntl(daemon_lock_fd, F_SETLK, &lock) == -1) {
4061 
4062 		if (errno == EAGAIN || errno == EDEADLK) {
4063 			if (fcntl(daemon_lock_fd, F_GETLK, &lock) == -1) {
4064 				err_print(LOCK_FAILED, daemon_lockfile,
4065 				    strerror(errno));
4066 				devfsadm_exit(1);
4067 				/*NOTREACHED*/
4068 			}
4069 			return (lock.l_pid);
4070 		}
4071 	}
4072 	hold_daemon_lock = TRUE;
4073 	return (getpid());
4074 }
4075 
4076 /*
4077  * Drop the advisory daemon lock, close lock file
4078  */
4079 void
4080 exit_daemon_lock(int exiting)
4081 {
4082 	struct flock lock;
4083 
4084 	if (hold_daemon_lock == FALSE) {
4085 		return;
4086 	}
4087 
4088 	vprint(LOCK_MID, "exit_daemon_lock: lock file %s, exiting = %d\n",
4089 	    daemon_lockfile, exiting);
4090 
4091 	lock.l_type = F_UNLCK;
4092 	lock.l_whence = SEEK_SET;
4093 	lock.l_start = 0;
4094 	lock.l_len = 0;
4095 
4096 	if (fcntl(daemon_lock_fd, F_SETLK, &lock) == -1) {
4097 		err_print(UNLOCK_FAILED, daemon_lockfile, strerror(errno));
4098 	}
4099 
4100 	if (close(daemon_lock_fd) == -1) {
4101 		err_print(CLOSE_FAILED, daemon_lockfile, strerror(errno));
4102 		if (!exiting)
4103 			devfsadm_exit(1);
4104 			/*NOTREACHED*/
4105 	}
4106 }
4107 
4108 /*
4109  * Called to removed danging nodes in two different modes: RM_PRE, RM_POST.
4110  * RM_PRE mode is called before processing the entire devinfo tree, and RM_POST
4111  * is called after processing the entire devinfo tree.
4112  */
4113 static void
4114 pre_and_post_cleanup(int flags)
4115 {
4116 	remove_list_t *rm;
4117 	recurse_dev_t rd;
4118 	cleanup_data_t cleanup_data;
4119 	char *fcn = "pre_and_post_cleanup: ";
4120 
4121 	if (build_dev == FALSE)
4122 		return;
4123 
4124 	vprint(CHATTY_MID, "attempting %s-cleanup\n",
4125 	    flags == RM_PRE ? "pre" : "post");
4126 	vprint(REMOVE_MID, "%sflags = %d\n", fcn, flags);
4127 
4128 	/*
4129 	 * the generic function recurse_dev_re is shared among different
4130 	 * functions, so set the method and data that it should use for
4131 	 * matches.
4132 	 */
4133 	rd.fcn = matching_dev;
4134 	rd.data = (void *)&cleanup_data;
4135 	cleanup_data.flags = flags;
4136 
4137 	(void) mutex_lock(&nfp_mutex);
4138 	nfphash_create();
4139 
4140 	for (rm = remove_head; rm != NULL; rm = rm->next) {
4141 		if ((flags & rm->remove->flags) == flags) {
4142 			cleanup_data.rm = rm;
4143 			/*
4144 			 * If reached this point, RM_PRE or RM_POST cleanup is
4145 			 * desired.  clean_ok() decides whether to clean
4146 			 * under the given circumstances.
4147 			 */
4148 			vprint(REMOVE_MID, "%scleanup: PRE or POST\n", fcn);
4149 			if (clean_ok(rm->remove) == DEVFSADM_SUCCESS) {
4150 				vprint(REMOVE_MID, "cleanup: cleanup OK\n");
4151 				recurse_dev_re(dev_dir,
4152 				    rm->remove->dev_dirs_re, &rd);
4153 			}
4154 		}
4155 	}
4156 	nfphash_destroy();
4157 	(void) mutex_unlock(&nfp_mutex);
4158 }
4159 
4160 /*
4161  * clean_ok() determines whether cleanup should be done according
4162  * to the following matrix:
4163  *
4164  * command line arguments RM_PRE    RM_POST	  RM_PRE &&    RM_POST &&
4165  *						  RM_ALWAYS    RM_ALWAYS
4166  * ---------------------- ------     -----	  ---------    ----------
4167  *
4168  * <neither -c nor -C>	  -	    -		  pre-clean    post-clean
4169  *
4170  * -C			  pre-clean  post-clean   pre-clean    post-clean
4171  *
4172  * -C -c class		  pre-clean  post-clean   pre-clean    post-clean
4173  *			  if class  if class	  if class     if class
4174  *			  matches   matches	  matches      matches
4175  *
4176  * -c class		   -	       -	  pre-clean    post-clean
4177  *						  if class     if class
4178  *						  matches      matches
4179  *
4180  */
4181 static int
4182 clean_ok(devfsadm_remove_V1_t *remove)
4183 {
4184 	int i;
4185 
4186 	if (single_drv == TRUE) {
4187 		/* no cleanup at all when using -i option */
4188 		return (DEVFSADM_FAILURE);
4189 	}
4190 
4191 	/*
4192 	 * no cleanup if drivers are not loaded. We make an exception
4193 	 * for the "disks" program however, since disks has a public
4194 	 * cleanup flag (-C) and disk drivers are usually never
4195 	 * unloaded.
4196 	 */
4197 	if (load_attach_drv == FALSE && strcmp(prog, DISKS) != 0) {
4198 		return (DEVFSADM_FAILURE);
4199 	}
4200 
4201 	/* if the cleanup flag was not specified, return false */
4202 	if ((cleanup == FALSE) && ((remove->flags & RM_ALWAYS) == 0)) {
4203 		return (DEVFSADM_FAILURE);
4204 	}
4205 
4206 	if (num_classes == 0) {
4207 		return (DEVFSADM_SUCCESS);
4208 	}
4209 
4210 	/*
4211 	 * if reached this point, check to see if the class in the given
4212 	 * remove structure matches a class given on the command line
4213 	 */
4214 
4215 	for (i = 0; i < num_classes; i++) {
4216 		if (strcmp(remove->device_class, classes[i]) == 0) {
4217 			return (DEVFSADM_SUCCESS);
4218 		}
4219 	}
4220 
4221 	return (DEVFSADM_FAILURE);
4222 }
4223 
4224 /*
4225  * Called to remove dangling nodes after receiving a hotplug event
4226  * containing the physical node pathname to be removed.
4227  */
4228 void
4229 hot_cleanup(char *node_path, char *minor_name, char *ev_subclass,
4230     char *driver_name, int instance)
4231 {
4232 	link_t *link;
4233 	linkhead_t *head;
4234 	remove_list_t *rm;
4235 	char *fcn = "hot_cleanup: ";
4236 	char path[PATH_MAX + 1];
4237 	int path_len;
4238 	char rmlink[PATH_MAX + 1];
4239 	nvlist_t *nvl = NULL;
4240 	int skip;
4241 	int ret;
4242 
4243 	/*
4244 	 * dev links can go away as part of hot cleanup.
4245 	 * So first build event attributes in order capture dev links.
4246 	 */
4247 	if (ev_subclass != NULL)
4248 		nvl = build_event_attributes(EC_DEV_REMOVE, ev_subclass,
4249 		    node_path, DI_NODE_NIL, driver_name, instance, minor_name);
4250 
4251 	(void) strcpy(path, node_path);
4252 	(void) strcat(path, ":");
4253 	(void) strcat(path, minor_name == NULL ? "" : minor_name);
4254 
4255 	path_len = strlen(path);
4256 
4257 	vprint(REMOVE_MID, "%spath=%s\n", fcn, path);
4258 
4259 	(void) mutex_lock(&nfp_mutex);
4260 	nfphash_create();
4261 
4262 	for (rm = remove_head; rm != NULL; rm = rm->next) {
4263 		if ((RM_HOT & rm->remove->flags) == RM_HOT) {
4264 			head = get_cached_links(rm->remove->dev_dirs_re);
4265 			assert(head->nextlink == NULL);
4266 			for (link = head->link;
4267 			    link != NULL; link = head->nextlink) {
4268 				/*
4269 				 * The remove callback below may remove
4270 				 * the current and/or any or all of the
4271 				 * subsequent links in the list.
4272 				 * Save the next link in the head. If
4273 				 * the callback removes the next link
4274 				 * the saved pointer in the head will be
4275 				 * updated by the callback to point at
4276 				 * the next valid link.
4277 				 */
4278 				head->nextlink = link->next;
4279 
4280 				/*
4281 				 * if devlink is in no-further-process hash,
4282 				 * skip its remove
4283 				 */
4284 				if (nfphash_lookup(link->devlink) != NULL)
4285 					continue;
4286 
4287 				if (minor_name)
4288 					skip = strcmp(link->contents, path);
4289 				else
4290 					skip = strncmp(link->contents, path,
4291 					    path_len);
4292 				if (skip ||
4293 				    (call_minor_init(rm->modptr) ==
4294 				    DEVFSADM_FAILURE))
4295 					continue;
4296 
4297 				vprint(REMOVE_MID,
4298 				    "%sremoving %s -> %s\n", fcn,
4299 				    link->devlink, link->contents);
4300 				/*
4301 				 * Use a copy of the cached link name
4302 				 * as the cache entry will go away
4303 				 * during link removal
4304 				 */
4305 				(void) snprintf(rmlink, sizeof (rmlink),
4306 				    "%s", link->devlink);
4307 				if (rm->remove->flags & RM_NOINTERPOSE) {
4308 					((void (*)(char *))
4309 					    (rm->remove->callback_fcn))(rmlink);
4310 				} else {
4311 					ret = ((int (*)(char *))
4312 					    (rm->remove->callback_fcn))(rmlink);
4313 					if (ret == DEVFSADM_TERMINATE)
4314 						nfphash_insert(rmlink);
4315 				}
4316 			}
4317 		}
4318 	}
4319 
4320 	nfphash_destroy();
4321 	(void) mutex_unlock(&nfp_mutex);
4322 
4323 	/* update device allocation database */
4324 	if (system_labeled) {
4325 		int	devtype = 0;
4326 
4327 		if (strstr(path, DA_SOUND_NAME))
4328 			devtype = DA_AUDIO;
4329 		else if (strstr(path, "storage"))
4330 			devtype = DA_RMDISK;
4331 		else if (strstr(path, "disk"))
4332 			devtype = DA_RMDISK;
4333 		else if (strstr(path, "floppy"))
4334 			/* TODO: detect usb cds and floppies at insert time */
4335 			devtype = DA_RMDISK;
4336 		else
4337 			goto out;
4338 
4339 		(void) _update_devalloc_db(&devlist, devtype, DA_REMOVE,
4340 		    node_path, root_dir);
4341 	}
4342 
4343 out:
4344 	/* now log an event */
4345 	if (nvl) {
4346 		log_event(EC_DEV_REMOVE, ev_subclass, nvl);
4347 		free(nvl);
4348 	}
4349 }
4350 
4351 /*
4352  * Open the dir current_dir.  For every file which matches the first dir
4353  * component of path_re, recurse.  If there are no more *dir* path
4354  * components left in path_re (ie no more /), then call function rd->fcn.
4355  */
4356 static void
4357 recurse_dev_re(char *current_dir, char *path_re, recurse_dev_t *rd)
4358 {
4359 	regex_t re1;
4360 	char *slash;
4361 	char new_path[PATH_MAX + 1];
4362 	char *anchored_path_re;
4363 	size_t len;
4364 	finddevhdl_t fhandle;
4365 	const char *fp;
4366 
4367 	vprint(RECURSEDEV_MID, "recurse_dev_re: curr = %s path=%s\n",
4368 	    current_dir, path_re);
4369 
4370 	if (finddev_readdir(current_dir, &fhandle) != 0)
4371 		return;
4372 
4373 	len = strlen(path_re);
4374 	if ((slash = strchr(path_re, '/')) != NULL) {
4375 		len = (slash - path_re);
4376 	}
4377 
4378 	anchored_path_re = s_malloc(len + 3);
4379 	(void) sprintf(anchored_path_re, "^%.*s$", len, path_re);
4380 
4381 	if (regcomp(&re1, anchored_path_re, REG_EXTENDED) != 0) {
4382 		free(anchored_path_re);
4383 		goto out;
4384 	}
4385 
4386 	free(anchored_path_re);
4387 
4388 	while ((fp = finddev_next(fhandle)) != NULL) {
4389 
4390 		if (regexec(&re1, fp, 0, NULL, 0) == 0) {
4391 			/* match */
4392 			(void) strcpy(new_path, current_dir);
4393 			(void) strcat(new_path, "/");
4394 			(void) strcat(new_path, fp);
4395 
4396 			vprint(RECURSEDEV_MID, "recurse_dev_re: match, new "
4397 			    "path = %s\n", new_path);
4398 
4399 			if (slash != NULL) {
4400 				recurse_dev_re(new_path, slash + 1, rd);
4401 			} else {
4402 				/* reached the leaf component of path_re */
4403 				vprint(RECURSEDEV_MID,
4404 				    "recurse_dev_re: calling fcn\n");
4405 				(*(rd->fcn))(new_path, rd->data);
4406 			}
4407 		}
4408 	}
4409 
4410 	regfree(&re1);
4411 
4412 out:
4413 	finddev_close(fhandle);
4414 }
4415 
4416 /*
4417  *  Found a devpath which matches a RE in the remove structure.
4418  *  Now check to see if it is dangling.
4419  */
4420 static void
4421 matching_dev(char *devpath, void *data)
4422 {
4423 	cleanup_data_t *cleanup_data = data;
4424 	int norm_len = strlen(dev_dir) + strlen("/");
4425 	int ret;
4426 	char *fcn = "matching_dev: ";
4427 
4428 	vprint(RECURSEDEV_MID, "%sexamining devpath = '%s'\n", fcn,
4429 	    devpath);
4430 
4431 	/*
4432 	 * If the link is in the no-further-process hash
4433 	 * don't do any remove operation on it.
4434 	 */
4435 	if (nfphash_lookup(devpath + norm_len) != NULL)
4436 		return;
4437 
4438 	/*
4439 	 * Dangling check will work whether "alias" or "current"
4440 	 * so no need to redirect.
4441 	 */
4442 	if (resolve_link(devpath, NULL, NULL, NULL, 1) == TRUE) {
4443 		if (call_minor_init(cleanup_data->rm->modptr) ==
4444 		    DEVFSADM_FAILURE) {
4445 			return;
4446 		}
4447 
4448 		devpath += norm_len;
4449 
4450 		vprint(RECURSEDEV_MID, "%scalling callback %s\n", fcn, devpath);
4451 		if (cleanup_data->rm->remove->flags & RM_NOINTERPOSE)
4452 			((void (*)(char *))
4453 			    (cleanup_data->rm->remove->callback_fcn))(devpath);
4454 		else {
4455 			ret = ((int (*)(char *))
4456 			    (cleanup_data->rm->remove->callback_fcn))(devpath);
4457 			if (ret == DEVFSADM_TERMINATE) {
4458 				/*
4459 				 * We want no further remove processing for
4460 				 * this link. Add it to the nfp_hash;
4461 				 */
4462 				nfphash_insert(devpath);
4463 			}
4464 		}
4465 	}
4466 }
4467 
4468 int
4469 devfsadm_read_link(di_node_t anynode, char *link, char **devfs_path)
4470 {
4471 	char devlink[PATH_MAX];
4472 	char *path;
4473 
4474 	*devfs_path = NULL;
4475 
4476 	/* prepend link with dev_dir contents */
4477 	(void) strcpy(devlink, dev_dir);
4478 	(void) strcat(devlink, "/");
4479 	(void) strcat(devlink, link);
4480 
4481 	/* We *don't* want a stat of the /devices node */
4482 	path = NULL;
4483 	(void) resolve_link(devlink, NULL, NULL, &path, 0);
4484 
4485 	/* redirect if alias to current */
4486 	*devfs_path = di_alias2curr(anynode, path);
4487 	free(path);
4488 	return (*devfs_path ? DEVFSADM_SUCCESS : DEVFSADM_FAILURE);
4489 }
4490 
4491 int
4492 devfsadm_link_valid(di_node_t anynode, char *link)
4493 {
4494 	struct stat sb;
4495 	char devlink[PATH_MAX + 1], *contents, *raw_contents;
4496 	int rv, type;
4497 	int instance = 0;
4498 
4499 	/* prepend link with dev_dir contents */
4500 	(void) strcpy(devlink, dev_dir);
4501 	(void) strcat(devlink, "/");
4502 	(void) strcat(devlink, link);
4503 
4504 	if (!device_exists(devlink) || lstat(devlink, &sb) != 0) {
4505 		return (DEVFSADM_FALSE);
4506 	}
4507 
4508 	raw_contents = NULL;
4509 	type = 0;
4510 	if (resolve_link(devlink, &raw_contents, &type, NULL, 1) == TRUE) {
4511 		rv = DEVFSADM_FALSE;
4512 	} else {
4513 		rv = DEVFSADM_TRUE;
4514 	}
4515 
4516 	/*
4517 	 * resolve alias paths for primary links
4518 	 */
4519 	contents = raw_contents;
4520 	if (type == DI_PRIMARY_LINK) {
4521 		contents = di_alias2curr(anynode, raw_contents);
4522 		free(raw_contents);
4523 	}
4524 
4525 	/*
4526 	 * The link exists. Add it to the database
4527 	 */
4528 	(void) di_devlink_add_link(devlink_cache, link, contents, type);
4529 	if (system_labeled && (rv == DEVFSADM_TRUE) &&
4530 	    strstr(devlink, DA_AUDIO_NAME) && contents) {
4531 		(void) sscanf(contents, "%*[a-z]%d", &instance);
4532 		(void) da_add_list(&devlist, devlink, instance,
4533 		    DA_ADD|DA_AUDIO);
4534 		_update_devalloc_db(&devlist, 0, DA_ADD, NULL, root_dir);
4535 	}
4536 	free(contents);
4537 
4538 	return (rv);
4539 }
4540 
4541 /*
4542  * devpath: Absolute path to /dev link
4543  * content_p: Returns malloced string (link content)
4544  * type_p: Returns link type: primary or secondary
4545  * devfs_path: Returns malloced string: /devices path w/out "/devices"
4546  * dangle: if set, check if link is dangling
4547  * Returns:
4548  *	TRUE if dangling
4549  *	FALSE if not or if caller doesn't care
4550  * Caller is assumed to have initialized pointer contents to NULL
4551  *
4552  */
4553 static int
4554 resolve_link(char *devpath, char **content_p, int *type_p, char **devfs_path,
4555     int dangle)
4556 {
4557 	char contents[PATH_MAX + 1];
4558 	char stage_link[PATH_MAX + 1];
4559 	char *fcn = "resolve_link: ";
4560 	char *ptr;
4561 	int linksize;
4562 	int rv = TRUE;
4563 	struct stat sb;
4564 
4565 	/*
4566 	 * This routine will return the "raw" contents. It is upto the
4567 	 * the caller to redirect "alias" to "current" (or vice versa)
4568 	 */
4569 	linksize = readlink(devpath, contents, PATH_MAX);
4570 
4571 	if (linksize <= 0) {
4572 		return (FALSE);
4573 	} else {
4574 		contents[linksize] = '\0';
4575 	}
4576 	vprint(REMOVE_MID, "%s %s -> %s\n", fcn, devpath, contents);
4577 
4578 	if (content_p) {
4579 		*content_p = s_strdup(contents);
4580 	}
4581 
4582 	/*
4583 	 * Check to see if this is a link pointing to another link in /dev.  The
4584 	 * cheap way to do this is to look for a lack of ../devices/.
4585 	 */
4586 
4587 	if (is_minor_node(contents, &ptr) == DEVFSADM_FALSE) {
4588 
4589 		if (type_p) {
4590 			*type_p = DI_SECONDARY_LINK;
4591 		}
4592 
4593 		/*
4594 		 * assume that linkcontents is really a pointer to another
4595 		 * link, and if so recurse and read its link contents.
4596 		 */
4597 		if (strncmp(contents, DEV "/", strlen(DEV) + 1) == 0)  {
4598 			(void) strcpy(stage_link, dev_dir);
4599 			(void) strcat(stage_link, "/");
4600 			(void) strcpy(stage_link,
4601 			    &contents[strlen(DEV) + strlen("/")]);
4602 		} else {
4603 			if ((ptr = strrchr(devpath, '/')) == NULL) {
4604 				vprint(REMOVE_MID, "%s%s -> %s invalid link. "
4605 				    "missing '/'\n", fcn, devpath, contents);
4606 				return (TRUE);
4607 			}
4608 			*ptr = '\0';
4609 			(void) strcpy(stage_link, devpath);
4610 			*ptr = '/';
4611 			(void) strcat(stage_link, "/");
4612 			(void) strcat(stage_link, contents);
4613 		}
4614 		return (resolve_link(stage_link, NULL, NULL, devfs_path,
4615 		    dangle));
4616 	}
4617 
4618 	/* Current link points at a /devices minor node */
4619 	if (type_p) {
4620 		*type_p = DI_PRIMARY_LINK;
4621 	}
4622 
4623 	if (devfs_path)
4624 		*devfs_path = s_strdup(ptr);
4625 
4626 	rv = FALSE;
4627 	if (dangle)
4628 		rv = (stat(ptr - strlen(DEVICES), &sb) == -1);
4629 
4630 	vprint(REMOVE_MID, "%slink=%s, returning %s\n", fcn,
4631 	    devpath, ((rv == TRUE) ? "TRUE" : "FALSE"));
4632 
4633 	return (rv);
4634 }
4635 
4636 /*
4637  * Returns the substring of interest, given a path.
4638  */
4639 static char *
4640 alloc_cmp_str(const char *path, devfsadm_enumerate_t *dep)
4641 {
4642 	uint_t match;
4643 	char *np, *ap, *mp;
4644 	char *cmp_str = NULL;
4645 	char at[] = "@";
4646 	char *fcn = "alloc_cmp_str";
4647 
4648 	np = ap = mp = NULL;
4649 
4650 	/*
4651 	 * extract match flags from the flags argument.
4652 	 */
4653 	match = (dep->flags & MATCH_MASK);
4654 
4655 	vprint(ENUM_MID, "%s: enumeration match type: 0x%x"
4656 	    " path: %s\n", fcn, match, path);
4657 
4658 	/*
4659 	 * MATCH_CALLBACK and MATCH_ALL are the only flags
4660 	 * which may be used if "path" is a /dev path
4661 	 */
4662 	if (match == MATCH_CALLBACK) {
4663 		if (dep->sel_fcn == NULL) {
4664 			vprint(ENUM_MID, "%s: invalid enumerate"
4665 			    " callback: path: %s\n", fcn, path);
4666 			return (NULL);
4667 		}
4668 		cmp_str = dep->sel_fcn(path, dep->cb_arg);
4669 		return (cmp_str);
4670 	}
4671 
4672 	cmp_str = s_strdup(path);
4673 
4674 	if (match == MATCH_ALL) {
4675 		return (cmp_str);
4676 	}
4677 
4678 	/*
4679 	 * The remaining flags make sense only for /devices
4680 	 * paths
4681 	 */
4682 	if ((mp = strrchr(cmp_str, ':')) == NULL) {
4683 		vprint(ENUM_MID, "%s: invalid path: %s\n",
4684 		    fcn, path);
4685 		goto err;
4686 	}
4687 
4688 	if (match == MATCH_MINOR) {
4689 		/* A NULL "match_arg" values implies entire minor */
4690 		if (get_component(mp + 1, dep->match_arg) == NULL) {
4691 			vprint(ENUM_MID, "%s: invalid minor component:"
4692 			    " path: %s\n", fcn, path);
4693 			goto err;
4694 		}
4695 		return (cmp_str);
4696 	}
4697 
4698 	if ((np = strrchr(cmp_str, '/')) == NULL) {
4699 		vprint(ENUM_MID, "%s: invalid path: %s\n", fcn, path);
4700 		goto err;
4701 	}
4702 
4703 	if (match == MATCH_PARENT) {
4704 		if (strcmp(cmp_str, "/") == 0) {
4705 			vprint(ENUM_MID, "%s: invalid path: %s\n",
4706 			    fcn, path);
4707 			goto err;
4708 		}
4709 
4710 		if (np == cmp_str) {
4711 			*(np + 1) = '\0';
4712 		} else {
4713 			*np = '\0';
4714 		}
4715 		return (cmp_str);
4716 	}
4717 
4718 	/* ap can be NULL - Leaf address may not exist or be empty string */
4719 	ap = strchr(np+1, '@');
4720 
4721 	/* minor is no longer of interest */
4722 	*mp = '\0';
4723 
4724 	if (match == MATCH_NODE) {
4725 		if (ap)
4726 			*ap = '\0';
4727 		return (cmp_str);
4728 	} else if (match == MATCH_ADDR) {
4729 		/*
4730 		 * The empty string is a valid address. The only MATCH_ADDR
4731 		 * allowed in this case is against the whole address or
4732 		 * the first component of the address (match_arg=NULL/"0"/"1")
4733 		 * Note that in this case, the path won't have an "@"
4734 		 * As a result ap will be NULL. We fake up an ap = @'\0'
4735 		 * so that get_component() will work correctly.
4736 		 */
4737 		if (ap == NULL) {
4738 			ap = at;
4739 		}
4740 
4741 		if (get_component(ap + 1, dep->match_arg) == NULL) {
4742 			vprint(ENUM_MID, "%s: invalid leaf addr. component:"
4743 			    " path: %s\n", fcn, path);
4744 			goto err;
4745 		}
4746 		return (cmp_str);
4747 	}
4748 
4749 	vprint(ENUM_MID, "%s: invalid enumeration flags: 0x%x"
4750 	    " path: %s\n", fcn, dep->flags, path);
4751 
4752 	/*FALLTHRU*/
4753 err:
4754 	free(cmp_str);
4755 	return (NULL);
4756 }
4757 
4758 
4759 /*
4760  * "str" is expected to be a string with components separated by ','
4761  * The terminating null char is considered a separator.
4762  * get_component() will remove the portion of the string beyond
4763  * the component indicated.
4764  * If comp_str is NULL, the entire "str" is returned.
4765  */
4766 static char *
4767 get_component(char *str, const char *comp_str)
4768 {
4769 	long comp;
4770 	char *cp;
4771 
4772 	if (str == NULL) {
4773 		return (NULL);
4774 	}
4775 
4776 	if (comp_str == NULL) {
4777 		return (str);
4778 	}
4779 
4780 	errno = 0;
4781 	comp = strtol(comp_str, &cp, 10);
4782 	if (errno != 0 || *cp != '\0' || comp < 0) {
4783 		return (NULL);
4784 	}
4785 
4786 	if (comp == 0)
4787 		return (str);
4788 
4789 	for (cp = str; ; cp++) {
4790 		if (*cp == ',' || *cp == '\0')
4791 			comp--;
4792 		if (*cp == '\0' || comp <= 0) {
4793 			break;
4794 		}
4795 	}
4796 
4797 	if (comp == 0) {
4798 		*cp = '\0';
4799 	} else {
4800 		str = NULL;
4801 	}
4802 
4803 	return (str);
4804 }
4805 
4806 
4807 /*
4808  * Enumerate serves as a generic counter as well as a means to determine
4809  * logical unit/controller numbers for such items as disk and tape
4810  * drives.
4811  *
4812  * rules[] is an array of  devfsadm_enumerate_t structures which defines
4813  * the enumeration rules to be used for a specified set of links in /dev.
4814  * The set of links is specified through regular expressions (of the flavor
4815  * described in regex(5)). These regular expressions are used to determine
4816  * the set of links in /dev to examine. The last path component in these
4817  * regular expressions MUST contain a parenthesized subexpression surrounding
4818  * the RE which is to be considered the enumerating component. The subexp
4819  * member in a rule is the subexpression number of the enumerating
4820  * component. Subexpressions in the last path component are numbered starting
4821  * from 1.
4822  *
4823  * A cache of current id assignments is built up from existing symlinks and
4824  * new assignments use the lowest unused id. Assignments are based on a
4825  * match of a specified substring of a symlink's contents. If the specified
4826  * component for the devfs_path argument matches the corresponding substring
4827  * for a existing symlink's contents, the cached id is returned. Else, a new
4828  * id is created and returned in *buf. *buf must be freed by the caller.
4829  *
4830  * An id assignment may be governed by a combination of rules, each rule
4831  * applicable to a different subset of links in /dev. For example, controller
4832  * numbers may be determined by a combination of disk symlinks in /dev/[r]dsk
4833  * and controller symlinks in /dev/cfg, with the two sets requiring different
4834  * rules to derive the "substring of interest". In such cases, the rules
4835  * array will have more than one element.
4836  */
4837 int
4838 devfsadm_enumerate_int(char *devfs_path, int index, char **buf,
4839 			devfsadm_enumerate_t rules[], int nrules)
4840 {
4841 	return (find_enum_id(rules, nrules,
4842 	    devfs_path, index, "0", INTEGER, buf, 0));
4843 }
4844 
4845 int
4846 disk_enumerate_int(char *devfs_path, int index, char **buf,
4847     devfsadm_enumerate_t rules[], int nrules)
4848 {
4849 	return (find_enum_id(rules, nrules,
4850 	    devfs_path, index, "0", INTEGER, buf, 1));
4851 }
4852 
4853 /*
4854  * Same as above, but allows a starting value to be specified.
4855  * Private to devfsadm.... used by devlinks.
4856  */
4857 static int
4858 devfsadm_enumerate_int_start(char *devfs_path, int index, char **buf,
4859 		devfsadm_enumerate_t rules[], int nrules, char *start)
4860 {
4861 	return (find_enum_id(rules, nrules,
4862 	    devfs_path, index, start, INTEGER, buf, 0));
4863 }
4864 
4865 /*
4866  *  devfsadm_enumerate_char serves as a generic counter returning
4867  *  a single letter.
4868  */
4869 int
4870 devfsadm_enumerate_char(char *devfs_path, int index, char **buf,
4871 			devfsadm_enumerate_t rules[], int nrules)
4872 {
4873 	return (find_enum_id(rules, nrules,
4874 	    devfs_path, index, "a", LETTER, buf, 0));
4875 }
4876 
4877 /*
4878  * Same as above, but allows a starting char to be specified.
4879  * Private to devfsadm - used by ports module (port_link.c)
4880  */
4881 int
4882 devfsadm_enumerate_char_start(char *devfs_path, int index, char **buf,
4883 	devfsadm_enumerate_t rules[], int nrules, char *start)
4884 {
4885 	return (find_enum_id(rules, nrules,
4886 	    devfs_path, index, start, LETTER, buf, 0));
4887 }
4888 
4889 
4890 /*
4891  * For a given numeral_set (see get_cached_set for desc of numeral_set),
4892  * search all cached entries looking for matches on a specified substring
4893  * of devfs_path. The substring is derived from devfs_path based on the
4894  * rule specified by "index". If a match is found on a cached entry,
4895  * return the enumerated id in buf. Otherwise, create a new id by calling
4896  * new_id, then cache and return that entry.
4897  */
4898 static int
4899 find_enum_id(devfsadm_enumerate_t rules[], int nrules,
4900 	char *devfs_path, int index, char *min, int type, char **buf,
4901 	int multiple)
4902 {
4903 	numeral_t *matchnp;
4904 	numeral_t *numeral;
4905 	int matchcount = 0;
4906 	char *cmp_str;
4907 	char *fcn = "find_enum_id";
4908 	numeral_set_t *set;
4909 
4910 	if (rules == NULL) {
4911 		vprint(ENUM_MID, "%s: no rules. path: %s\n",
4912 		    fcn, devfs_path ? devfs_path : "<NULL path>");
4913 		return (DEVFSADM_FAILURE);
4914 	}
4915 
4916 	if (devfs_path == NULL) {
4917 		vprint(ENUM_MID, "%s: NULL path\n", fcn);
4918 		return (DEVFSADM_FAILURE);
4919 	}
4920 
4921 	if (nrules <= 0 || index < 0 || index >= nrules || buf == NULL) {
4922 		vprint(ENUM_MID, "%s: invalid arguments. path: %s\n",
4923 		    fcn, devfs_path);
4924 		return (DEVFSADM_FAILURE);
4925 	}
4926 
4927 	*buf = NULL;
4928 
4929 
4930 	cmp_str = alloc_cmp_str(devfs_path, &rules[index]);
4931 	if (cmp_str == NULL) {
4932 		return (DEVFSADM_FAILURE);
4933 	}
4934 
4935 	if ((set = get_enum_cache(rules, nrules)) == NULL) {
4936 		free(cmp_str);
4937 		return (DEVFSADM_FAILURE);
4938 	}
4939 
4940 	assert(nrules == set->re_count);
4941 
4942 	/*
4943 	 * Check and see if a matching entry is already cached.
4944 	 */
4945 	matchcount = lookup_enum_cache(set, cmp_str, rules, index,
4946 	    &matchnp);
4947 
4948 	if (matchcount < 0 || matchcount > 1) {
4949 		free(cmp_str);
4950 		if (multiple && matchcount > 1)
4951 			return (DEVFSADM_MULTIPLE);
4952 		else
4953 			return (DEVFSADM_FAILURE);
4954 	}
4955 
4956 	/* if matching entry already cached, return it */
4957 	if (matchcount == 1) {
4958 		/* should never create a link with a reserved ID */
4959 		vprint(ENUM_MID, "%s: 1 match w/ ID: %s\n", fcn, matchnp->id);
4960 		assert(matchnp->flags == 0);
4961 		*buf = s_strdup(matchnp->id);
4962 		free(cmp_str);
4963 		return (DEVFSADM_SUCCESS);
4964 	}
4965 
4966 	/*
4967 	 * no cached entry, initialize a numeral struct
4968 	 * by calling new_id() and cache onto the numeral_set
4969 	 */
4970 	numeral = s_malloc(sizeof (numeral_t));
4971 	numeral->id = new_id(set->headnumeral, type, min);
4972 	numeral->full_path = s_strdup(devfs_path);
4973 	numeral->rule_index = index;
4974 	numeral->cmp_str = cmp_str;
4975 	cmp_str = NULL;
4976 	numeral->flags = 0;
4977 	vprint(RSRV_MID, "%s: alloc new_id: %s numeral flags = %d\n",
4978 	    fcn, numeral->id, numeral->flags);
4979 
4980 
4981 	/* insert to head of list for fast lookups */
4982 	numeral->next = set->headnumeral;
4983 	set->headnumeral = numeral;
4984 
4985 	*buf = s_strdup(numeral->id);
4986 	return (DEVFSADM_SUCCESS);
4987 }
4988 
4989 
4990 /*
4991  * Looks up the specified cache for a match with a specified string
4992  * Returns:
4993  *	-1	: on error.
4994  *	0/1/2	: Number of matches.
4995  * Returns the matching element only if there is a single match.
4996  * If the "uncached" flag is set, derives the "cmp_str" afresh
4997  * for the match instead of using cached values.
4998  */
4999 static int
5000 lookup_enum_cache(numeral_set_t *set, char *cmp_str,
5001 	devfsadm_enumerate_t rules[], int index, numeral_t **matchnpp)
5002 {
5003 	int matchcount = 0, rv = -1;
5004 	int uncached;
5005 	numeral_t *np;
5006 	char *fcn = "lookup_enum_cache";
5007 	char *cp;
5008 
5009 	*matchnpp = NULL;
5010 
5011 	assert(index < set->re_count);
5012 
5013 	if (cmp_str == NULL) {
5014 		return (-1);
5015 	}
5016 
5017 	uncached = 0;
5018 	if ((rules[index].flags & MATCH_UNCACHED) == MATCH_UNCACHED) {
5019 		uncached = 1;
5020 	}
5021 
5022 	/*
5023 	 * Check and see if a matching entry is already cached.
5024 	 */
5025 	for (np = set->headnumeral; np != NULL; np = np->next) {
5026 
5027 		/*
5028 		 * Skip reserved IDs
5029 		 */
5030 		if (np->flags & NUMERAL_RESERVED) {
5031 			vprint(RSRV_MID, "lookup_enum_cache: "
5032 			    "Cannot Match with reserved ID (%s), "
5033 			    "skipping\n", np->id);
5034 			assert(np->flags == NUMERAL_RESERVED);
5035 			continue;
5036 		} else {
5037 			vprint(RSRV_MID, "lookup_enum_cache: "
5038 			    "Attempting match with numeral ID: %s"
5039 			    " numeral flags = %d\n", np->id, np->flags);
5040 			assert(np->flags == 0);
5041 		}
5042 
5043 		if (np->cmp_str == NULL) {
5044 			vprint(ENUM_MID, "%s: invalid entry in enumerate"
5045 			    " cache. path: %s\n", fcn, np->full_path);
5046 			return (-1);
5047 		}
5048 
5049 		if (uncached) {
5050 			vprint(CHATTY_MID, "%s: bypassing enumerate cache."
5051 			    " path: %s\n", fcn, cmp_str);
5052 			cp = alloc_cmp_str(np->full_path,
5053 			    &rules[np->rule_index]);
5054 			if (cp == NULL)
5055 				return (-1);
5056 			rv = strcmp(cmp_str, cp);
5057 			free(cp);
5058 		} else {
5059 			rv = strcmp(cmp_str, np->cmp_str);
5060 		}
5061 
5062 		if (rv == 0) {
5063 			if (matchcount++ != 0) {
5064 				break; /* more than 1 match. */
5065 			}
5066 			*matchnpp = np;
5067 		}
5068 	}
5069 
5070 	return (matchcount);
5071 }
5072 
5073 #ifdef	DEBUG
5074 static void
5075 dump_enum_cache(numeral_set_t *setp)
5076 {
5077 	int i;
5078 	numeral_t *np;
5079 	char *fcn = "dump_enum_cache";
5080 
5081 	vprint(ENUM_MID, "%s: re_count = %d\n", fcn, setp->re_count);
5082 	for (i = 0; i < setp->re_count; i++) {
5083 		vprint(ENUM_MID, "%s: re[%d] = %s\n", fcn, i, setp->re[i]);
5084 	}
5085 
5086 	for (np = setp->headnumeral; np != NULL; np = np->next) {
5087 		vprint(ENUM_MID, "%s: id: %s\n", fcn, np->id);
5088 		vprint(ENUM_MID, "%s: full_path: %s\n", fcn, np->full_path);
5089 		vprint(ENUM_MID, "%s: rule_index: %d\n", fcn, np->rule_index);
5090 		vprint(ENUM_MID, "%s: cmp_str: %s\n", fcn, np->cmp_str);
5091 		vprint(ENUM_MID, "%s: flags: %d\n", fcn, np->flags);
5092 	}
5093 }
5094 #endif
5095 
5096 /*
5097  * For a given set of regular expressions in rules[], this function returns
5098  * either a previously cached struct numeral_set or it will create and
5099  * cache a new struct numeral_set.  There is only one struct numeral_set
5100  * for the combination of REs present in rules[].  Each numeral_set contains
5101  * the regular expressions in rules[] used for cache selection AND a linked
5102  * list of struct numerals, ONE FOR EACH *UNIQUE* numeral or character ID
5103  * selected by the grouping parenthesized subexpression found in the last
5104  * path component of each rules[].re.  For example, the RE: "rmt/([0-9]+)"
5105  * selects all the logical nodes of the correct form in dev/rmt/.
5106  * Each rmt/X will store a *single* struct numeral... ie 0, 1, 2 each get a
5107  * single struct numeral. There is no need to store more than a single logical
5108  * node matching X since the information desired in the devfspath would be
5109  * identical for the portion of the devfspath of interest. (the part up to,
5110  * but not including the minor name in this example.)
5111  *
5112  * If the given numeral_set is not yet cached, call enumerate_recurse to
5113  * create it.
5114  */
5115 static numeral_set_t *
5116 get_enum_cache(devfsadm_enumerate_t rules[], int nrules)
5117 {
5118 	/* linked list of numeral sets */
5119 	numeral_set_t *setp;
5120 	int i;
5121 	int ret;
5122 	char *path_left;
5123 	enumerate_file_t *entry;
5124 	char *fcn = "get_enum_cache";
5125 
5126 	/*
5127 	 * See if we've already cached this numeral set.
5128 	 */
5129 	for (setp = head_numeral_set; setp != NULL; setp = setp->next) {
5130 		/*
5131 		 *  check all regexp's passed in function against
5132 		 *  those in cached set.
5133 		 */
5134 		if (nrules != setp->re_count) {
5135 			continue;
5136 		}
5137 
5138 		for (i = 0; i < nrules; i++) {
5139 			if (strcmp(setp->re[i], rules[i].re) != 0) {
5140 				break;
5141 			}
5142 		}
5143 
5144 		if (i == nrules) {
5145 			return (setp);
5146 		}
5147 	}
5148 
5149 	/*
5150 	 * If the MATCH_UNCACHED flag is set, we should not  be here.
5151 	 */
5152 	for (i = 0; i < nrules; i++) {
5153 		if ((rules[i].flags & MATCH_UNCACHED) == MATCH_UNCACHED) {
5154 			vprint(ENUM_MID, "%s: invalid enumeration flags: "
5155 			    "0x%x\n", fcn, rules[i].flags);
5156 			return (NULL);
5157 		}
5158 	}
5159 
5160 	/*
5161 	 *  Since we made it here, we have not yet cached the given set of
5162 	 *  logical nodes matching the passed re.  Create a cached entry
5163 	 *  struct numeral_set and populate it with a minimal set of
5164 	 *  logical nodes from /dev.
5165 	 */
5166 
5167 	setp = s_malloc(sizeof (numeral_set_t));
5168 	setp->re = s_malloc(sizeof (char *) * nrules);
5169 	for (i = 0; i < nrules; i++) {
5170 		setp->re[i] = s_strdup(rules[i].re);
5171 	}
5172 	setp->re_count = nrules;
5173 	setp->headnumeral = NULL;
5174 
5175 	/* put this new cached set on the cached set list */
5176 	setp->next = head_numeral_set;
5177 	head_numeral_set = setp;
5178 
5179 	/*
5180 	 * For each RE, search the "reserved" list to create numeral IDs that
5181 	 * are reserved.
5182 	 */
5183 	for (entry = enumerate_reserved; entry; entry = entry->er_next) {
5184 
5185 		vprint(RSRV_MID, "parsing rstring: %s\n", entry->er_file);
5186 
5187 		for (i = 0; i < nrules; i++) {
5188 			path_left = s_strdup(setp->re[i]);
5189 			vprint(RSRV_MID, "parsing rule RE: %s\n", path_left);
5190 			ret = enumerate_parse(entry->er_file, path_left,
5191 			    setp, rules, i);
5192 			free(path_left);
5193 			if (ret == 1) {
5194 				/*
5195 				 * We found the reserved ID for this entry.
5196 				 * We still keep the entry since it is needed
5197 				 * by the new link bypass code in disks
5198 				 */
5199 				vprint(RSRV_MID, "found rsv ID: rstring: %s "
5200 				    "rule RE: %s\n", entry->er_file, path_left);
5201 				break;
5202 			}
5203 		}
5204 	}
5205 
5206 	/*
5207 	 * For each RE, search disk and cache any matches on the
5208 	 * numeral list.
5209 	 */
5210 	for (i = 0; i < nrules; i++) {
5211 		path_left = s_strdup(setp->re[i]);
5212 		enumerate_recurse(dev_dir, path_left, setp, rules, i);
5213 		free(path_left);
5214 	}
5215 
5216 #ifdef	DEBUG
5217 	dump_enum_cache(setp);
5218 #endif
5219 
5220 	return (setp);
5221 }
5222 
5223 
5224 /*
5225  * This function stats the pathname namebuf.  If this is a directory
5226  * entry, we recurse down dname/fname until we find the first symbolic
5227  * link, and then stat and return it.  This is valid for the same reason
5228  * that we only need to read a single pathname for multiple matching
5229  * logical ID's... ie, all the logical nodes should contain identical
5230  * physical paths for the parts we are interested.
5231  */
5232 int
5233 get_stat_info(char *namebuf, struct stat *sb)
5234 {
5235 	char *cp;
5236 	finddevhdl_t fhandle;
5237 	const char *fp;
5238 
5239 	if (lstat(namebuf, sb) < 0) {
5240 		(void) err_print(LSTAT_FAILED, namebuf, strerror(errno));
5241 		return (DEVFSADM_FAILURE);
5242 	}
5243 
5244 	if ((sb->st_mode & S_IFMT) == S_IFLNK) {
5245 		return (DEVFSADM_SUCCESS);
5246 	}
5247 
5248 	/*
5249 	 * If it is a dir, recurse down until we find a link and
5250 	 * then use the link.
5251 	 */
5252 	if ((sb->st_mode & S_IFMT) == S_IFDIR) {
5253 
5254 		if (finddev_readdir(namebuf, &fhandle) != 0) {
5255 			return (DEVFSADM_FAILURE);
5256 		}
5257 
5258 		/*
5259 		 *  Search each dir entry looking for a symlink.  Return
5260 		 *  the first symlink found in namebuf.  Recurse dirs.
5261 		 */
5262 		while ((fp = finddev_next(fhandle)) != NULL) {
5263 			cp = namebuf + strlen(namebuf);
5264 			if ((strlcat(namebuf, "/", PATH_MAX) >= PATH_MAX) ||
5265 			    (strlcat(namebuf, fp, PATH_MAX) >= PATH_MAX)) {
5266 				*cp = '\0';
5267 				finddev_close(fhandle);
5268 				return (DEVFSADM_FAILURE);
5269 			}
5270 			if (get_stat_info(namebuf, sb) == DEVFSADM_SUCCESS) {
5271 				finddev_close(fhandle);
5272 				return (DEVFSADM_SUCCESS);
5273 			}
5274 			*cp = '\0';
5275 		}
5276 		finddev_close(fhandle);
5277 	}
5278 
5279 	/* no symlink found, so return error */
5280 	return (DEVFSADM_FAILURE);
5281 }
5282 
5283 /*
5284  * An existing matching ID was not found, so this function is called to
5285  * create the next lowest ID.  In the INTEGER case, return the next
5286  * lowest unused integer.  In the case of LETTER, return the next lowest
5287  * unused letter.  Return empty string if all 26 are used.
5288  * Only IDs >= min will be returned.
5289  */
5290 char *
5291 new_id(numeral_t *numeral, int type, char *min)
5292 {
5293 	int imin;
5294 	temp_t *temp;
5295 	temp_t *ptr;
5296 	temp_t **previous;
5297 	temp_t *head = NULL;
5298 	char *retval;
5299 	static char tempbuff[8];
5300 	numeral_t *np;
5301 
5302 	if (type == LETTER) {
5303 
5304 		char letter[26], i;
5305 
5306 		if (numeral == NULL) {
5307 			return (s_strdup(min));
5308 		}
5309 
5310 		for (i = 0; i < 26; i++) {
5311 			letter[i] = 0;
5312 		}
5313 
5314 		for (np = numeral; np != NULL; np = np->next) {
5315 			assert(np->flags == 0 ||
5316 			    np->flags == NUMERAL_RESERVED);
5317 			letter[*np->id - 'a']++;
5318 		}
5319 
5320 		imin = *min - 'a';
5321 
5322 		for (i = imin; i < 26; i++) {
5323 			if (letter[i] == 0) {
5324 				retval = s_malloc(2);
5325 				retval[0] = 'a' + i;
5326 				retval[1] = '\0';
5327 				return (retval);
5328 			}
5329 		}
5330 
5331 		return (s_strdup(""));
5332 	}
5333 
5334 	if (type == INTEGER) {
5335 
5336 		if (numeral == NULL) {
5337 			return (s_strdup(min));
5338 		}
5339 
5340 		imin = atoi(min);
5341 
5342 		/* sort list */
5343 		for (np = numeral; np != NULL; np = np->next) {
5344 			assert(np->flags == 0 ||
5345 			    np->flags == NUMERAL_RESERVED);
5346 			temp = s_malloc(sizeof (temp_t));
5347 			temp->integer = atoi(np->id);
5348 			temp->next = NULL;
5349 
5350 			previous = &head;
5351 			for (ptr = head; ptr != NULL; ptr = ptr->next) {
5352 				if (temp->integer < ptr->integer) {
5353 					temp->next = ptr;
5354 					*previous = temp;
5355 					break;
5356 				}
5357 				previous = &(ptr->next);
5358 			}
5359 			if (ptr == NULL) {
5360 				*previous = temp;
5361 			}
5362 		}
5363 
5364 		/* now search sorted list for first hole >= imin */
5365 		for (ptr = head; ptr != NULL; ptr = ptr->next) {
5366 			if (imin == ptr->integer) {
5367 				imin++;
5368 			} else {
5369 				if (imin < ptr->integer) {
5370 					break;
5371 				}
5372 			}
5373 
5374 		}
5375 
5376 		/* free temp list */
5377 		for (ptr = head; ptr != NULL; ) {
5378 			temp = ptr;
5379 			ptr = ptr->next;
5380 			free(temp);
5381 		}
5382 
5383 		(void) sprintf(tempbuff, "%d", imin);
5384 		return (s_strdup(tempbuff));
5385 	}
5386 
5387 	return (s_strdup(""));
5388 }
5389 
5390 static int
5391 enumerate_parse(char *rsvstr, char *path_left, numeral_set_t *setp,
5392 	    devfsadm_enumerate_t rules[], int index)
5393 {
5394 	char	*slash1 = NULL;
5395 	char	*slash2 = NULL;
5396 	char	*numeral_id;
5397 	char	*path_left_save;
5398 	char	*rsvstr_save;
5399 	int	ret = 0;
5400 	static int warned = 0;
5401 
5402 	rsvstr_save = rsvstr;
5403 	path_left_save = path_left;
5404 
5405 	if (rsvstr == NULL || rsvstr[0] == '\0' || rsvstr[0] == '/') {
5406 		if (!warned) {
5407 			err_print("invalid reserved filepath: %s\n",
5408 			    rsvstr ? rsvstr : "<NULL>");
5409 			warned = 1;
5410 		}
5411 		return (0);
5412 	}
5413 
5414 	vprint(RSRV_MID, "processing rule: %s, rstring: %s\n",
5415 	    path_left, rsvstr);
5416 
5417 
5418 	for (;;) {
5419 		/* get rid of any extra '/' in the reserve string */
5420 		while (*rsvstr == '/') {
5421 			rsvstr++;
5422 		}
5423 
5424 		/* get rid of any extra '/' in the RE */
5425 		while (*path_left == '/') {
5426 			path_left++;
5427 		}
5428 
5429 		if (slash1 = strchr(path_left, '/')) {
5430 			*slash1 = '\0';
5431 		}
5432 		if (slash2 = strchr(rsvstr, '/')) {
5433 			*slash2 = '\0';
5434 		}
5435 
5436 		if ((slash1 != NULL) ^ (slash2 != NULL)) {
5437 			ret = 0;
5438 			vprint(RSRV_MID, "mismatch in # of path components\n");
5439 			goto out;
5440 		}
5441 
5442 		/*
5443 		 *  Returns true if path_left matches the list entry.
5444 		 *  If it is the last path component, pass subexp
5445 		 *  so that it will return the corresponding ID in
5446 		 *  numeral_id.
5447 		 */
5448 		numeral_id = NULL;
5449 		if (match_path_component(path_left, rsvstr, &numeral_id,
5450 		    slash1 ? 0 : rules[index].subexp)) {
5451 
5452 			/* We have a match. */
5453 			if (slash1 == NULL) {
5454 				/* Is last path component */
5455 				vprint(RSRV_MID, "match and last component\n");
5456 				create_reserved_numeral(setp, numeral_id);
5457 				if (numeral_id != NULL) {
5458 					free(numeral_id);
5459 				}
5460 				ret = 1;
5461 				goto out;
5462 			} else {
5463 				/* Not last path component. Continue parsing */
5464 				*slash1 = '/';
5465 				*slash2 = '/';
5466 				path_left = slash1 + 1;
5467 				rsvstr = slash2 + 1;
5468 				vprint(RSRV_MID,
5469 				    "match and NOT last component\n");
5470 				continue;
5471 			}
5472 		} else {
5473 			/* No match */
5474 			ret = 0;
5475 			vprint(RSRV_MID, "No match: rule RE = %s, "
5476 			    "rstring = %s\n", path_left, rsvstr);
5477 			goto out;
5478 		}
5479 	}
5480 
5481 out:
5482 	if (slash1)
5483 		*slash1 = '/';
5484 	if (slash2)
5485 		*slash2 = '/';
5486 
5487 	if (ret == 1) {
5488 		vprint(RSRV_MID, "match: rule RE: %s, rstring: %s\n",
5489 		    path_left_save, rsvstr_save);
5490 	} else {
5491 		vprint(RSRV_MID, "NO match: rule RE: %s, rstring: %s\n",
5492 		    path_left_save, rsvstr_save);
5493 	}
5494 
5495 	return (ret);
5496 }
5497 
5498 /*
5499  * Search current_dir for all files which match the first path component
5500  * of path_left, which is an RE.  If a match is found, but there are more
5501  * components of path_left, then recurse, otherwise, if we have reached
5502  * the last component of path_left, call create_cached_numerals for each
5503  * file.   At some point, recurse_dev_re() should be rewritten so that this
5504  * function can be eliminated.
5505  */
5506 static void
5507 enumerate_recurse(char *current_dir, char *path_left, numeral_set_t *setp,
5508 	    devfsadm_enumerate_t rules[], int index)
5509 {
5510 	char *slash;
5511 	char *new_path;
5512 	char *numeral_id;
5513 	finddevhdl_t fhandle;
5514 	const char *fp;
5515 
5516 	if (finddev_readdir(current_dir, &fhandle) != 0) {
5517 		return;
5518 	}
5519 
5520 	/* get rid of any extra '/' */
5521 	while (*path_left == '/') {
5522 		path_left++;
5523 	}
5524 
5525 	if (slash = strchr(path_left, '/')) {
5526 		*slash = '\0';
5527 	}
5528 
5529 	while ((fp = finddev_next(fhandle)) != NULL) {
5530 
5531 		/*
5532 		 *  Returns true if path_left matches the list entry.
5533 		 *  If it is the last path component, pass subexp
5534 		 *  so that it will return the corresponding ID in
5535 		 *  numeral_id.
5536 		 */
5537 		numeral_id = NULL;
5538 		if (match_path_component(path_left, (char *)fp, &numeral_id,
5539 		    slash ? 0 : rules[index].subexp)) {
5540 
5541 			new_path = s_malloc(strlen(current_dir) +
5542 			    strlen(fp) + 2);
5543 
5544 			(void) strcpy(new_path, current_dir);
5545 			(void) strcat(new_path, "/");
5546 			(void) strcat(new_path, fp);
5547 
5548 			if (slash != NULL) {
5549 				enumerate_recurse(new_path, slash + 1,
5550 				    setp, rules, index);
5551 			} else {
5552 				create_cached_numeral(new_path, setp,
5553 				    numeral_id, rules, index);
5554 				if (numeral_id != NULL) {
5555 					free(numeral_id);
5556 				}
5557 			}
5558 			free(new_path);
5559 		}
5560 	}
5561 
5562 	if (slash != NULL) {
5563 		*slash = '/';
5564 	}
5565 	finddev_close(fhandle);
5566 }
5567 
5568 
5569 /*
5570  * Returns true if file matches file_re.  If subexp is non-zero, it means
5571  * we are searching the last path component and need to return the
5572  * parenthesized subexpression subexp in id.
5573  *
5574  */
5575 static int
5576 match_path_component(char *file_re,  char *file,  char **id, int subexp)
5577 {
5578 	regex_t re1;
5579 	int match = 0;
5580 	int nelements;
5581 	regmatch_t *pmatch;
5582 
5583 	if (subexp != 0) {
5584 		nelements = subexp + 1;
5585 		pmatch =
5586 		    (regmatch_t *)s_malloc(sizeof (regmatch_t) * nelements);
5587 	} else {
5588 		pmatch = NULL;
5589 		nelements = 0;
5590 	}
5591 
5592 	if (regcomp(&re1, file_re, REG_EXTENDED) != 0) {
5593 		if (pmatch != NULL) {
5594 			free(pmatch);
5595 		}
5596 		return (0);
5597 	}
5598 
5599 	if (regexec(&re1, file, nelements, pmatch, 0) == 0) {
5600 		match = 1;
5601 	}
5602 
5603 	if ((match != 0) && (subexp != 0)) {
5604 		int size = pmatch[subexp].rm_eo - pmatch[subexp].rm_so;
5605 		*id = s_malloc(size + 1);
5606 		(void) strncpy(*id, &file[pmatch[subexp].rm_so], size);
5607 		(*id)[size] = '\0';
5608 	}
5609 
5610 	if (pmatch != NULL) {
5611 		free(pmatch);
5612 	}
5613 	regfree(&re1);
5614 	return (match);
5615 }
5616 
5617 static void
5618 create_reserved_numeral(numeral_set_t *setp, char *numeral_id)
5619 {
5620 	numeral_t *np;
5621 
5622 	vprint(RSRV_MID, "Attempting to create reserved numeral: %s\n",
5623 	    numeral_id);
5624 
5625 	/*
5626 	 * We found a numeral_id from an entry in the enumerate_reserved file
5627 	 * which matched the re passed in from devfsadm_enumerate.  We only
5628 	 * need to make sure ONE copy of numeral_id exists on the numeral list.
5629 	 * We only need to store /dev/dsk/cNtod0s0 and no other entries
5630 	 * hanging off of controller N.
5631 	 */
5632 	for (np = setp->headnumeral; np != NULL; np = np->next) {
5633 		if (strcmp(numeral_id, np->id) == 0) {
5634 			vprint(RSRV_MID, "ID: %s, already reserved\n", np->id);
5635 			assert(np->flags == NUMERAL_RESERVED);
5636 			return;
5637 		} else {
5638 			assert(np->flags == 0 ||
5639 			    np->flags == NUMERAL_RESERVED);
5640 		}
5641 	}
5642 
5643 	/* NOT on list, so add it */
5644 	np = s_malloc(sizeof (numeral_t));
5645 	np->id = s_strdup(numeral_id);
5646 	np->full_path = NULL;
5647 	np->rule_index = 0;
5648 	np->cmp_str = NULL;
5649 	np->flags = NUMERAL_RESERVED;
5650 	np->next = setp->headnumeral;
5651 	setp->headnumeral = np;
5652 
5653 	vprint(RSRV_MID, "Reserved numeral ID: %s\n", np->id);
5654 }
5655 
5656 /*
5657  * This function is called for every file which matched the leaf
5658  * component of the RE.  If the "numeral_id" is not already on the
5659  * numeral set's numeral list, add it and its physical path.
5660  */
5661 static void
5662 create_cached_numeral(char *path, numeral_set_t *setp, char *numeral_id,
5663 	devfsadm_enumerate_t rules[], int index)
5664 {
5665 	char linkbuf[PATH_MAX + 1];
5666 	char lpath[PATH_MAX + 1];
5667 	char *linkptr, *cmp_str;
5668 	numeral_t *np;
5669 	int linksize;
5670 	struct stat sb;
5671 	char *contents;
5672 	const char *fcn = "create_cached_numeral";
5673 
5674 	assert(index >= 0 && index < setp->re_count);
5675 	assert(strcmp(rules[index].re, setp->re[index]) == 0);
5676 
5677 	/*
5678 	 *  We found a numeral_id from an entry in /dev which matched
5679 	 *  the re passed in from devfsadm_enumerate.  We only need to make sure
5680 	 *  ONE copy of numeral_id exists on the numeral list.  We only need
5681 	 *  to store /dev/dsk/cNtod0s0 and no other entries hanging off
5682 	 *  of controller N.
5683 	 */
5684 	for (np = setp->headnumeral; np != NULL; np = np->next) {
5685 		assert(np->flags == 0 || np->flags == NUMERAL_RESERVED);
5686 		if (strcmp(numeral_id, np->id) == 0) {
5687 			/*
5688 			 * Note that we can't assert that the flags field
5689 			 * of the numeral is 0, since both reserved and
5690 			 * unreserved links in /dev come here
5691 			 */
5692 			if (np->flags == NUMERAL_RESERVED) {
5693 				vprint(RSRV_MID, "ID derived from /dev link is"
5694 				    " reserved: %s\n", np->id);
5695 			} else {
5696 				vprint(RSRV_MID, "ID derived from /dev link is"
5697 				    " NOT reserved: %s\n", np->id);
5698 			}
5699 			return;
5700 		}
5701 	}
5702 
5703 	/* NOT on list, so add it */
5704 
5705 	(void) strcpy(lpath, path);
5706 	/*
5707 	 * If path is a dir, it is changed to the first symbolic link it find
5708 	 * if it finds one.
5709 	 */
5710 	if (get_stat_info(lpath, &sb) == DEVFSADM_FAILURE) {
5711 		return;
5712 	}
5713 
5714 	/* If we get here, we found a symlink */
5715 	linksize = readlink(lpath, linkbuf, PATH_MAX);
5716 
5717 	if (linksize <= 0) {
5718 		err_print(READLINK_FAILED, fcn, lpath, strerror(errno));
5719 		return;
5720 	}
5721 
5722 	linkbuf[linksize] = '\0';
5723 
5724 	/*
5725 	 * redirect alias path to current path
5726 	 * devi_root_node is protected by lock_dev()
5727 	 */
5728 	contents = di_alias2curr(devi_root_node, linkbuf);
5729 
5730 	/*
5731 	 * the following just points linkptr to the root of the /devices
5732 	 * node if it is a minor node, otherwise, to the first char of
5733 	 * linkbuf if it is a link.
5734 	 */
5735 	(void) is_minor_node(contents, &linkptr);
5736 
5737 	cmp_str = alloc_cmp_str(linkptr, &rules[index]);
5738 	if (cmp_str == NULL) {
5739 		free(contents);
5740 		return;
5741 	}
5742 
5743 	np = s_malloc(sizeof (numeral_t));
5744 
5745 	np->id = s_strdup(numeral_id);
5746 	np->full_path = s_strdup(linkptr);
5747 	np->rule_index = index;
5748 	np->cmp_str = cmp_str;
5749 	np->flags = 0;
5750 
5751 	np->next = setp->headnumeral;
5752 	setp->headnumeral = np;
5753 
5754 	free(contents);
5755 }
5756 
5757 
5758 /*
5759  * This should be called either before or after granting access to a
5760  * command line version of devfsadm running, since it may have changed
5761  * the state of /dev.  It forces future enumerate calls to re-build
5762  * cached information from /dev.
5763  */
5764 void
5765 invalidate_enumerate_cache(void)
5766 {
5767 	numeral_set_t *setp;
5768 	numeral_set_t *savedsetp;
5769 	numeral_t *savednumset;
5770 	numeral_t *numset;
5771 	int i;
5772 
5773 	for (setp = head_numeral_set; setp != NULL; ) {
5774 		/*
5775 		 *  check all regexp's passed in function against
5776 		 *  those in cached set.
5777 		 */
5778 
5779 		savedsetp = setp;
5780 		setp = setp->next;
5781 
5782 		for (i = 0; i < savedsetp->re_count; i++) {
5783 			free(savedsetp->re[i]);
5784 		}
5785 		free(savedsetp->re);
5786 
5787 		for (numset = savedsetp->headnumeral; numset != NULL; ) {
5788 			savednumset = numset;
5789 			numset = numset->next;
5790 			assert(savednumset->rule_index < savedsetp->re_count);
5791 			free(savednumset->id);
5792 			free(savednumset->full_path);
5793 			free(savednumset->cmp_str);
5794 			free(savednumset);
5795 		}
5796 		free(savedsetp);
5797 	}
5798 	head_numeral_set = NULL;
5799 }
5800 
5801 /*
5802  * Copies over links from /dev to <root>/dev and device special files in
5803  * /devices to <root>/devices, preserving the existing file modes.  If
5804  * the link or special file already exists on <root>, skip the copy.  (it
5805  * would exist only if a package hard coded it there, so assume package
5806  * knows best?).  Use /etc/name_to_major and <root>/etc/name_to_major to
5807  * make translations for major numbers on device special files.	No need to
5808  * make a translation on minor_perm since if the file was created in the
5809  * miniroot then it would presumably have the same minor_perm entry in
5810  *  <root>/etc/minor_perm.  To be used only by install.
5811  */
5812 int
5813 devfsadm_copy(void)
5814 {
5815 	char filename[PATH_MAX + 1];
5816 
5817 	/* load the installed root's name_to_major for translations */
5818 	(void) snprintf(filename, sizeof (filename), "%s%s", root_dir,
5819 	    NAME_TO_MAJOR);
5820 	if (load_n2m_table(filename) == DEVFSADM_FAILURE) {
5821 		return (DEVFSADM_FAILURE);
5822 	}
5823 
5824 	/* Copy /dev to target disk. No need to copy /devices with devfs */
5825 	(void) nftw(DEV, devfsadm_copy_file, 20, FTW_PHYS);
5826 
5827 	/* Let install handle copying over path_to_inst */
5828 
5829 	return (DEVFSADM_SUCCESS);
5830 }
5831 
5832 /*
5833  * This function copies links, dirs, and device special files.
5834  * Note that it always returns DEVFSADM_SUCCESS, so that nftw doesn't
5835  * abort.
5836  */
5837 /*ARGSUSED*/
5838 static int
5839 devfsadm_copy_file(const char *file, const struct stat *stat,
5840 		    int flags, struct FTW *ftw)
5841 {
5842 	struct stat sp;
5843 	dev_t newdev;
5844 	char newfile[PATH_MAX + 1];
5845 	char linkcontents[PATH_MAX + 1];
5846 	int bytes;
5847 	const char *fcn = "devfsadm_copy_file";
5848 
5849 	(void) strcpy(newfile, root_dir);
5850 	(void) strcat(newfile, "/");
5851 	(void) strcat(newfile, file);
5852 
5853 	if (lstat(newfile, &sp) == 0) {
5854 		/* newfile already exists, so no need to continue */
5855 		return (DEVFSADM_SUCCESS);
5856 	}
5857 
5858 	if (((stat->st_mode & S_IFMT) == S_IFBLK) ||
5859 	    ((stat->st_mode & S_IFMT) == S_IFCHR)) {
5860 		if (translate_major(stat->st_rdev, &newdev) ==
5861 		    DEVFSADM_FAILURE) {
5862 			return (DEVFSADM_SUCCESS);
5863 		}
5864 		if (mknod(newfile, stat->st_mode, newdev) == -1) {
5865 			err_print(MKNOD_FAILED, newfile, strerror(errno));
5866 			return (DEVFSADM_SUCCESS);
5867 		}
5868 	} else if ((stat->st_mode & S_IFMT) == S_IFDIR) {
5869 		if (mknod(newfile, stat->st_mode, 0) == -1) {
5870 			err_print(MKNOD_FAILED, newfile, strerror(errno));
5871 			return (DEVFSADM_SUCCESS);
5872 		}
5873 	} else if ((stat->st_mode & S_IFMT) == S_IFLNK)  {
5874 		/*
5875 		 * No need to redirect alias paths. We want a
5876 		 * true copy. The system on first boot after install
5877 		 * will redirect paths
5878 		 */
5879 		if ((bytes = readlink(file, linkcontents, PATH_MAX)) == -1)  {
5880 			err_print(READLINK_FAILED, fcn, file, strerror(errno));
5881 			return (DEVFSADM_SUCCESS);
5882 		}
5883 		linkcontents[bytes] = '\0';
5884 		if (symlink(linkcontents, newfile) == -1) {
5885 			err_print(SYMLINK_FAILED, newfile, newfile,
5886 			    strerror(errno));
5887 			return (DEVFSADM_SUCCESS);
5888 		}
5889 	}
5890 
5891 	(void) lchown(newfile, stat->st_uid, stat->st_gid);
5892 	return (DEVFSADM_SUCCESS);
5893 }
5894 
5895 /*
5896  *  Given a dev_t from the running kernel, return the new_dev_t
5897  *  by translating to the major number found on the installed
5898  *  target's root name_to_major file.
5899  */
5900 static int
5901 translate_major(dev_t old_dev, dev_t *new_dev)
5902 {
5903 	major_t oldmajor;
5904 	major_t newmajor;
5905 	minor_t oldminor;
5906 	minor_t newminor;
5907 	char cdriver[FILENAME_MAX + 1];
5908 	char driver[FILENAME_MAX + 1];
5909 	char *fcn = "translate_major: ";
5910 
5911 	oldmajor = major(old_dev);
5912 	if (modctl(MODGETNAME, driver, sizeof (driver), &oldmajor) != 0) {
5913 		return (DEVFSADM_FAILURE);
5914 	}
5915 
5916 	if (strcmp(driver, "clone") != 0) {
5917 		/* non-clone case */
5918 
5919 		/* look up major number is target's name2major */
5920 		if (get_major_no(driver, &newmajor) == DEVFSADM_FAILURE) {
5921 			return (DEVFSADM_FAILURE);
5922 		}
5923 
5924 		*new_dev = makedev(newmajor, minor(old_dev));
5925 		if (old_dev != *new_dev) {
5926 			vprint(CHATTY_MID, "%sdriver: %s old: %lu,%lu "
5927 			    "new: %lu,%lu\n", fcn, driver, major(old_dev),
5928 			    minor(old_dev), major(*new_dev), minor(*new_dev));
5929 		}
5930 		return (DEVFSADM_SUCCESS);
5931 	} else {
5932 		/*
5933 		 *  The clone is a special case.  Look at its minor
5934 		 *  number since it is the major number of the real driver.
5935 		 */
5936 		if (get_major_no(driver, &newmajor) == DEVFSADM_FAILURE) {
5937 			return (DEVFSADM_FAILURE);
5938 		}
5939 
5940 		oldminor = minor(old_dev);
5941 		if (modctl(MODGETNAME, cdriver, sizeof (cdriver),
5942 		    &oldminor) != 0) {
5943 			err_print(MODGETNAME_FAILED, oldminor);
5944 			return (DEVFSADM_FAILURE);
5945 		}
5946 
5947 		if (get_major_no(cdriver, &newminor) == DEVFSADM_FAILURE) {
5948 			return (DEVFSADM_FAILURE);
5949 		}
5950 
5951 		*new_dev = makedev(newmajor, newminor);
5952 		if (old_dev != *new_dev) {
5953 			vprint(CHATTY_MID, "%sdriver: %s old: "
5954 			    "%lu,%lu  new: %lu,%lu\n", fcn, driver,
5955 			    major(old_dev), minor(old_dev),
5956 			    major(*new_dev), minor(*new_dev));
5957 		}
5958 		return (DEVFSADM_SUCCESS);
5959 	}
5960 }
5961 
5962 /*
5963  *
5964  * Find the major number for driver, searching the n2m_list that was
5965  * built in load_n2m_table().
5966  */
5967 static int
5968 get_major_no(char *driver, major_t *major)
5969 {
5970 	n2m_t *ptr;
5971 
5972 	for (ptr = n2m_list; ptr != NULL; ptr = ptr->next) {
5973 		if (strcmp(ptr->driver, driver) == 0) {
5974 			*major = ptr->major;
5975 			return (DEVFSADM_SUCCESS);
5976 		}
5977 	}
5978 	err_print(FIND_MAJOR_FAILED, driver);
5979 	return (DEVFSADM_FAILURE);
5980 }
5981 
5982 /*
5983  * Loads a name_to_major table into memory.  Used only for suninstall's
5984  * private -R option to devfsadm, to translate major numbers from the
5985  * running to the installed target disk.
5986  */
5987 static int
5988 load_n2m_table(char *file)
5989 {
5990 	FILE *fp;
5991 	char line[1024], *cp;
5992 	char driver[PATH_MAX + 1];
5993 	major_t major;
5994 	n2m_t *ptr;
5995 	int ln = 0;
5996 
5997 	if ((fp = fopen(file, "r")) == NULL) {
5998 		err_print(FOPEN_FAILED, file, strerror(errno));
5999 		return (DEVFSADM_FAILURE);
6000 	}
6001 
6002 	while (fgets(line, sizeof (line), fp) != NULL) {
6003 		ln++;
6004 		/* cut off comments starting with '#' */
6005 		if ((cp = strchr(line, '#')) != NULL)
6006 			*cp = '\0';
6007 		/* ignore comment or blank lines */
6008 		if (is_blank(line))
6009 			continue;
6010 		/* sanity-check */
6011 		if (sscanf(line, "%1024s%lu", driver, &major) != 2) {
6012 			err_print(IGNORING_LINE_IN, ln, file);
6013 			continue;
6014 		}
6015 		ptr = (n2m_t *)s_malloc(sizeof (n2m_t));
6016 		ptr->major = major;
6017 		ptr->driver = s_strdup(driver);
6018 		ptr->next = n2m_list;
6019 		n2m_list = ptr;
6020 	}
6021 	if (fclose(fp) == EOF) {
6022 		err_print(FCLOSE_FAILED, file, strerror(errno));
6023 	}
6024 	return (DEVFSADM_SUCCESS);
6025 }
6026 
6027 /*
6028  * Called at devfsadm startup to read the file /etc/dev/enumerate_reserved
6029  * Creates a linked list of devlinks from which reserved IDs can be derived
6030  */
6031 static void
6032 read_enumerate_file(void)
6033 {
6034 	FILE *fp;
6035 	int linenum;
6036 	char line[PATH_MAX+1];
6037 	enumerate_file_t *entry;
6038 	struct stat current_sb;
6039 	static struct stat cached_sb;
6040 	static int cached = FALSE;
6041 
6042 	assert(enumerate_file);
6043 
6044 	if (stat(enumerate_file, &current_sb) == -1) {
6045 		vprint(RSRV_MID, "No reserved file: %s\n", enumerate_file);
6046 		cached = FALSE;
6047 		if (enumerate_reserved != NULL) {
6048 			vprint(RSRV_MID, "invalidating %s cache\n",
6049 			    enumerate_file);
6050 		}
6051 		while (enumerate_reserved != NULL) {
6052 			entry = enumerate_reserved;
6053 			enumerate_reserved = entry->er_next;
6054 			free(entry->er_file);
6055 			free(entry->er_id);
6056 			free(entry);
6057 		}
6058 		return;
6059 	}
6060 
6061 	/* if already cached, check to see if it is still valid */
6062 	if (cached == TRUE) {
6063 
6064 		if (current_sb.st_mtime == cached_sb.st_mtime) {
6065 			vprint(RSRV_MID, "%s cache valid\n", enumerate_file);
6066 			vprint(FILES_MID, "%s cache valid\n", enumerate_file);
6067 			return;
6068 		}
6069 
6070 		vprint(RSRV_MID, "invalidating %s cache\n", enumerate_file);
6071 		vprint(FILES_MID, "invalidating %s cache\n", enumerate_file);
6072 
6073 		while (enumerate_reserved != NULL) {
6074 			entry = enumerate_reserved;
6075 			enumerate_reserved = entry->er_next;
6076 			free(entry->er_file);
6077 			free(entry->er_id);
6078 			free(entry);
6079 		}
6080 		vprint(RSRV_MID, "Recaching file: %s\n", enumerate_file);
6081 	} else {
6082 		vprint(RSRV_MID, "Caching file (first time): %s\n",
6083 		    enumerate_file);
6084 		cached = TRUE;
6085 	}
6086 
6087 	(void) stat(enumerate_file, &cached_sb);
6088 
6089 	if ((fp = fopen(enumerate_file, "r")) == NULL) {
6090 		err_print(FOPEN_FAILED, enumerate_file, strerror(errno));
6091 		return;
6092 	}
6093 
6094 	vprint(RSRV_MID, "Reading reserve file: %s\n", enumerate_file);
6095 	linenum = 0;
6096 	while (fgets(line, sizeof (line), fp) != NULL) {
6097 		char	*cp, *ncp;
6098 
6099 		linenum++;
6100 
6101 		/* remove newline */
6102 		cp = strchr(line, '\n');
6103 		if (cp)
6104 			*cp = '\0';
6105 
6106 		vprint(RSRV_MID, "Reserve file: line %d: %s\n", linenum, line);
6107 
6108 		/* skip over space and tab */
6109 		for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
6110 			;
6111 
6112 		if (*cp == '\0' || *cp == '#') {
6113 			vprint(RSRV_MID, "Skipping line: '%s'\n", line);
6114 			continue; /* blank line or comment line */
6115 		}
6116 
6117 		ncp = cp;
6118 
6119 		/* delete trailing blanks */
6120 		for (; *cp != ' ' && *cp != '\t' && *cp != '\0'; cp++)
6121 			;
6122 		*cp = '\0';
6123 
6124 		entry = s_zalloc(sizeof (enumerate_file_t));
6125 		entry->er_file = s_strdup(ncp);
6126 		entry->er_id = NULL;
6127 		entry->er_next = enumerate_reserved;
6128 		enumerate_reserved = entry;
6129 	}
6130 
6131 	if (fclose(fp) == EOF) {
6132 		err_print(FCLOSE_FAILED, enumerate_file, strerror(errno));
6133 	}
6134 }
6135 
6136 /*
6137  * Called at devfsadm startup to read in the devlink.tab file.	Creates
6138  * a linked list of devlinktab_list structures which will be
6139  * searched for every minor node.
6140  */
6141 static void
6142 read_devlinktab_file(void)
6143 {
6144 	devlinktab_list_t *headp = NULL;
6145 	devlinktab_list_t *entryp;
6146 	devlinktab_list_t **previous;
6147 	devlinktab_list_t *save;
6148 	char line[MAX_DEVLINK_LINE], *cp;
6149 	char *selector;
6150 	char *p_link;
6151 	char *s_link;
6152 	FILE *fp;
6153 	int i;
6154 	static struct stat cached_sb;
6155 	struct stat current_sb;
6156 	static int cached = FALSE;
6157 
6158 	if (devlinktab_file == NULL) {
6159 		return;
6160 	}
6161 
6162 	(void) stat(devlinktab_file, &current_sb);
6163 
6164 	/* if already cached, check to see if it is still valid */
6165 	if (cached == TRUE) {
6166 
6167 		if (current_sb.st_mtime == cached_sb.st_mtime) {
6168 			vprint(FILES_MID, "%s cache valid\n", devlinktab_file);
6169 			return;
6170 		}
6171 
6172 		vprint(FILES_MID, "invalidating %s cache\n", devlinktab_file);
6173 
6174 		while (devlinktab_list != NULL) {
6175 			free_link_list(devlinktab_list->p_link);
6176 			free_link_list(devlinktab_list->s_link);
6177 			free_selector_list(devlinktab_list->selector);
6178 			free(devlinktab_list->selector_pattern);
6179 			free(devlinktab_list->p_link_pattern);
6180 			if (devlinktab_list->s_link_pattern != NULL) {
6181 				free(devlinktab_list->s_link_pattern);
6182 			}
6183 			save = devlinktab_list;
6184 			devlinktab_list = devlinktab_list->next;
6185 			free(save);
6186 		}
6187 	} else {
6188 		cached = TRUE;
6189 	}
6190 
6191 	(void) stat(devlinktab_file, &cached_sb);
6192 
6193 	if ((fp = fopen(devlinktab_file, "r")) == NULL) {
6194 		err_print(FOPEN_FAILED, devlinktab_file, strerror(errno));
6195 		return;
6196 	}
6197 
6198 	previous = &headp;
6199 
6200 	while (fgets(line, sizeof (line), fp) != NULL) {
6201 		devlinktab_line++;
6202 		i = strlen(line);
6203 		if (line[i-1] == NEWLINE) {
6204 			line[i-1] = '\0';
6205 		} else if (i == sizeof (line-1)) {
6206 			err_print(LINE_TOO_LONG, devlinktab_line,
6207 			    devlinktab_file, sizeof (line)-1);
6208 			while (((i = getc(fp)) != '\n') && (i != EOF))
6209 				;
6210 			continue;
6211 		}
6212 
6213 		/* cut off comments starting with '#' */
6214 		if ((cp = strchr(line, '#')) != NULL)
6215 			*cp = '\0';
6216 		/* ignore comment or blank lines */
6217 		if (is_blank(line))
6218 			continue;
6219 
6220 		vprint(DEVLINK_MID, "table: %s line %d: '%s'\n",
6221 		    devlinktab_file, devlinktab_line, line);
6222 
6223 		/* break each entry into fields.  s_link may be NULL */
6224 		if (split_devlinktab_entry(line, &selector, &p_link,
6225 		    &s_link) == DEVFSADM_FAILURE) {
6226 			vprint(DEVLINK_MID, "split_entry returns failure\n");
6227 			continue;
6228 		} else {
6229 			vprint(DEVLINK_MID, "split_entry selector='%s' "
6230 			    "p_link='%s' s_link='%s'\n\n", selector,
6231 			    p_link, (s_link == NULL) ? "" : s_link);
6232 		}
6233 
6234 		entryp =
6235 		    (devlinktab_list_t *)s_malloc(sizeof (devlinktab_list_t));
6236 
6237 		entryp->line_number = devlinktab_line;
6238 
6239 		if ((entryp->selector = create_selector_list(selector))
6240 		    == NULL) {
6241 			free(entryp);
6242 			continue;
6243 		}
6244 		entryp->selector_pattern = s_strdup(selector);
6245 
6246 		if ((entryp->p_link = create_link_list(p_link)) == NULL) {
6247 			free_selector_list(entryp->selector);
6248 			free(entryp->selector_pattern);
6249 			free(entryp);
6250 			continue;
6251 		}
6252 
6253 		entryp->p_link_pattern = s_strdup(p_link);
6254 
6255 		if (s_link != NULL) {
6256 			if ((entryp->s_link =
6257 			    create_link_list(s_link)) == NULL) {
6258 				free_selector_list(entryp->selector);
6259 				free_link_list(entryp->p_link);
6260 				free(entryp->selector_pattern);
6261 				free(entryp->p_link_pattern);
6262 				free(entryp);
6263 				continue;
6264 			}
6265 			entryp->s_link_pattern = s_strdup(s_link);
6266 		} else {
6267 			entryp->s_link = NULL;
6268 			entryp->s_link_pattern = NULL;
6269 
6270 		}
6271 
6272 		/* append to end of list */
6273 
6274 		entryp->next = NULL;
6275 		*previous = entryp;
6276 		previous = &(entryp->next);
6277 	}
6278 	if (fclose(fp) == EOF) {
6279 		err_print(FCLOSE_FAILED, devlinktab_file, strerror(errno));
6280 	}
6281 	devlinktab_list = headp;
6282 }
6283 
6284 /*
6285  *
6286  * For a single line entry in devlink.tab, split the line into fields
6287  * selector, p_link, and an optionally s_link.	If s_link field is not
6288  * present, then return NULL in s_link (not NULL string).
6289  */
6290 static int
6291 split_devlinktab_entry(char *entry, char **selector, char **p_link,
6292 			char **s_link)
6293 {
6294 	char *tab;
6295 
6296 	*selector = entry;
6297 
6298 	if ((tab = strchr(entry, TAB)) != NULL) {
6299 		*tab = '\0';
6300 		*p_link = ++tab;
6301 	} else {
6302 		err_print(MISSING_TAB, devlinktab_line, devlinktab_file);
6303 		return (DEVFSADM_FAILURE);
6304 	}
6305 
6306 	if (*p_link == '\0') {
6307 		err_print(MISSING_DEVNAME, devlinktab_line, devlinktab_file);
6308 		return (DEVFSADM_FAILURE);
6309 	}
6310 
6311 	if ((tab = strchr(*p_link, TAB)) != NULL) {
6312 		*tab = '\0';
6313 		*s_link = ++tab;
6314 		if (strchr(*s_link, TAB) != NULL) {
6315 			err_print(TOO_MANY_FIELDS, devlinktab_line,
6316 			    devlinktab_file);
6317 			return (DEVFSADM_FAILURE);
6318 		}
6319 	} else {
6320 		*s_link = NULL;
6321 	}
6322 
6323 	return (DEVFSADM_SUCCESS);
6324 }
6325 
6326 /*
6327  * For a given devfs_spec field, for each element in the field, add it to
6328  * a linked list of devfs_spec structures.  Return the linked list in
6329  * devfs_spec_list.
6330  */
6331 static selector_list_t *
6332 create_selector_list(char *selector)
6333 {
6334 	char *key;
6335 	char *val;
6336 	int error = FALSE;
6337 	selector_list_t *head_selector_list = NULL;
6338 	selector_list_t *selector_list;
6339 
6340 	/* parse_devfs_spec splits the next field into keyword & value */
6341 	while ((*selector != NULL) && (error == FALSE)) {
6342 		if (parse_selector(&selector, &key, &val) == DEVFSADM_FAILURE) {
6343 			error = TRUE;
6344 			break;
6345 		} else {
6346 			selector_list = (selector_list_t *)
6347 			    s_malloc(sizeof (selector_list_t));
6348 			if (strcmp(NAME_S, key) == 0) {
6349 				selector_list->key = NAME;
6350 			} else if (strcmp(TYPE_S, key) == 0) {
6351 				selector_list->key = TYPE;
6352 			} else if (strncmp(ADDR_S, key, ADDR_S_LEN) == 0) {
6353 				selector_list->key = ADDR;
6354 				if (key[ADDR_S_LEN] == '\0') {
6355 					selector_list->arg = 0;
6356 				} else if (isdigit(key[ADDR_S_LEN]) != FALSE) {
6357 					selector_list->arg =
6358 					    atoi(&key[ADDR_S_LEN]);
6359 				} else {
6360 					error = TRUE;
6361 					free(selector_list);
6362 					err_print(BADKEYWORD, key,
6363 					    devlinktab_line, devlinktab_file);
6364 					break;
6365 				}
6366 			} else if (strncmp(MINOR_S, key, MINOR_S_LEN) == 0) {
6367 				selector_list->key = MINOR;
6368 				if (key[MINOR_S_LEN] == '\0') {
6369 					selector_list->arg = 0;
6370 				} else if (isdigit(key[MINOR_S_LEN]) != FALSE) {
6371 					selector_list->arg =
6372 					    atoi(&key[MINOR_S_LEN]);
6373 				} else {
6374 					error = TRUE;
6375 					free(selector_list);
6376 					err_print(BADKEYWORD, key,
6377 					    devlinktab_line, devlinktab_file);
6378 					break;
6379 				}
6380 				vprint(DEVLINK_MID, "MINOR = %s\n", val);
6381 			} else {
6382 				err_print(UNRECOGNIZED_KEY, key,
6383 				    devlinktab_line, devlinktab_file);
6384 				error = TRUE;
6385 				free(selector_list);
6386 				break;
6387 			}
6388 			selector_list->val = s_strdup(val);
6389 			selector_list->next = head_selector_list;
6390 			head_selector_list = selector_list;
6391 			vprint(DEVLINK_MID, "key='%s' val='%s' arg=%d\n",
6392 			    key, val, selector_list->arg);
6393 		}
6394 	}
6395 
6396 	if ((error == FALSE) && (head_selector_list != NULL)) {
6397 		return (head_selector_list);
6398 	} else {
6399 		/* parse failed.  Free any allocated structs */
6400 		free_selector_list(head_selector_list);
6401 		return (NULL);
6402 	}
6403 }
6404 
6405 /*
6406  * Takes a semicolon separated list of selector elements and breaks up
6407  * into a keyword-value pair.	semicolon and equal characters are
6408  * replaced with NULL's.  On success, selector is updated to point to the
6409  * terminating NULL character terminating the keyword-value pair, and the
6410  * function returns DEVFSADM_SUCCESS.	If there is a syntax error,
6411  * devfs_spec is not modified and function returns DEVFSADM_FAILURE.
6412  */
6413 static int
6414 parse_selector(char **selector, char **key, char **val)
6415 {
6416 	char *equal;
6417 	char *semi_colon;
6418 
6419 	*key = *selector;
6420 
6421 	if ((equal = strchr(*key, '=')) != NULL) {
6422 		*equal = '\0';
6423 	} else {
6424 		err_print(MISSING_EQUAL, devlinktab_line, devlinktab_file);
6425 		return (DEVFSADM_FAILURE);
6426 	}
6427 
6428 	*val = ++equal;
6429 	if ((semi_colon = strchr(equal, ';')) != NULL) {
6430 		*semi_colon = '\0';
6431 		*selector = semi_colon + 1;
6432 	} else {
6433 		*selector = equal + strlen(equal);
6434 	}
6435 	return (DEVFSADM_SUCCESS);
6436 }
6437 
6438 /*
6439  * link is either the second or third field of devlink.tab.  Parse link
6440  * into a linked list of devlink structures and return ptr to list.  Each
6441  * list element is either a constant string, or one of the following
6442  * escape sequences: \M, \A, \N, or \D.  The first three escape sequences
6443  * take a numerical argument.
6444  */
6445 static link_list_t *
6446 create_link_list(char *link)
6447 {
6448 	int x = 0;
6449 	int error = FALSE;
6450 	int counter_found = FALSE;
6451 	link_list_t *head = NULL;
6452 	link_list_t **ptr;
6453 	link_list_t *link_list;
6454 	char constant[MAX_DEVLINK_LINE];
6455 	char *error_str;
6456 
6457 	if (link == NULL) {
6458 		return (NULL);
6459 	}
6460 
6461 	while ((*link != '\0') && (error == FALSE)) {
6462 		link_list = (link_list_t *)s_malloc(sizeof (link_list_t));
6463 		link_list->next = NULL;
6464 
6465 		while ((*link != '\0') && (*link != '\\')) {
6466 			/* a non-escaped string */
6467 			constant[x++] = *(link++);
6468 		}
6469 		if (x != 0) {
6470 			constant[x] = '\0';
6471 			link_list->type = CONSTANT;
6472 			link_list->constant = s_strdup(constant);
6473 			x = 0;
6474 			vprint(DEVLINK_MID, "CONSTANT FOUND %s\n", constant);
6475 		} else {
6476 			switch (*(++link)) {
6477 			case 'M':
6478 				link_list->type = MINOR;
6479 				break;
6480 			case 'A':
6481 				link_list->type = ADDR;
6482 				break;
6483 			case 'N':
6484 				if (counter_found == TRUE) {
6485 					error = TRUE;
6486 					error_str =
6487 					    "multiple counters not permitted";
6488 					free(link_list);
6489 				} else {
6490 					counter_found = TRUE;
6491 					link_list->type = COUNTER;
6492 				}
6493 				break;
6494 			case 'D':
6495 				link_list->type = NAME;
6496 				break;
6497 			default:
6498 				error = TRUE;
6499 				free(link_list);
6500 				error_str = "unrecognized escape sequence";
6501 				break;
6502 			}
6503 			if (*(link++) != 'D') {
6504 				if (isdigit(*link) == FALSE) {
6505 					error_str = "escape sequence must be "
6506 					    "followed by a digit\n";
6507 					error = TRUE;
6508 					free(link_list);
6509 				} else {
6510 					link_list->arg =
6511 					    (int)strtoul(link, &link, 10);
6512 					vprint(DEVLINK_MID, "link_list->arg = "
6513 					    "%d\n", link_list->arg);
6514 				}
6515 			}
6516 		}
6517 		/* append link_list struct to end of list */
6518 		if (error == FALSE) {
6519 			for (ptr = &head; *ptr != NULL; ptr = &((*ptr)->next))
6520 				;
6521 			*ptr = link_list;
6522 		}
6523 	}
6524 
6525 	if (error == FALSE) {
6526 		return (head);
6527 	} else {
6528 		err_print(CONFIG_INCORRECT, devlinktab_line, devlinktab_file,
6529 		    error_str);
6530 		free_link_list(head);
6531 		return (NULL);
6532 	}
6533 }
6534 
6535 /*
6536  * Called for each minor node devfsadm processes; for each minor node,
6537  * look for matches in the devlinktab_list list which was created on
6538  * startup read_devlinktab_file().  If there is a match, call build_links()
6539  * to build a logical devlink and a possible extra devlink.
6540  */
6541 static int
6542 process_devlink_compat(di_minor_t minor, di_node_t node)
6543 {
6544 	int link_built = FALSE;
6545 	devlinktab_list_t *entry;
6546 	char *nodetype;
6547 	char *dev_path;
6548 
6549 	if (devlinks_debug == TRUE) {
6550 		nodetype =  di_minor_nodetype(minor);
6551 		assert(nodetype != NULL);
6552 		if ((dev_path = di_devfs_path(node)) != NULL) {
6553 			vprint(INFO_MID, "'%s' entry: %s:%s\n",
6554 			    nodetype, dev_path,
6555 			    di_minor_name(minor) ? di_minor_name(minor) : "");
6556 			di_devfs_path_free(dev_path);
6557 		}
6558 
6559 	}
6560 
6561 
6562 	/* don't process devlink.tab if devfsadm invoked with -c <class> */
6563 	if (num_classes > 0) {
6564 		return (FALSE);
6565 	}
6566 
6567 	for (entry = devlinktab_list; entry != NULL; entry = entry->next) {
6568 		if (devlink_matches(entry, minor, node) == DEVFSADM_SUCCESS) {
6569 			link_built = TRUE;
6570 			(void) build_links(entry, minor, node);
6571 		}
6572 	}
6573 	return (link_built);
6574 }
6575 
6576 /*
6577  * For a given devlink.tab devlinktab_list entry, see if the selector
6578  * field matches this minor node.  If it does, return DEVFSADM_SUCCESS,
6579  * otherwise DEVFSADM_FAILURE.
6580  */
6581 static int
6582 devlink_matches(devlinktab_list_t *entry, di_minor_t minor, di_node_t node)
6583 {
6584 	selector_list_t *selector = entry->selector;
6585 	char *addr;
6586 	char *minor_name;
6587 	char *node_type;
6588 
6589 	for (; selector != NULL; selector = selector->next) {
6590 		switch (selector->key) {
6591 		case NAME:
6592 			if (strcmp(di_node_name(node), selector->val) != 0) {
6593 				return (DEVFSADM_FAILURE);
6594 			}
6595 			break;
6596 		case TYPE:
6597 			node_type = di_minor_nodetype(minor);
6598 			assert(node_type != NULL);
6599 			if (strcmp(node_type, selector->val) != 0) {
6600 				return (DEVFSADM_FAILURE);
6601 			}
6602 			break;
6603 		case ADDR:
6604 			if ((addr = di_bus_addr(node)) == NULL) {
6605 				return (DEVFSADM_FAILURE);
6606 			}
6607 			if (selector->arg == 0) {
6608 				if (strcmp(addr, selector->val) != 0) {
6609 					return (DEVFSADM_FAILURE);
6610 				}
6611 			} else {
6612 				if (compare_field(addr, selector->val,
6613 				    selector->arg) == DEVFSADM_FAILURE) {
6614 					return (DEVFSADM_FAILURE);
6615 				}
6616 			}
6617 			break;
6618 		case MINOR:
6619 			if ((minor_name = di_minor_name(minor)) == NULL) {
6620 				return (DEVFSADM_FAILURE);
6621 			}
6622 			if (selector->arg == 0) {
6623 				if (strcmp(minor_name, selector->val) != 0) {
6624 					return (DEVFSADM_FAILURE);
6625 				}
6626 			} else {
6627 				if (compare_field(minor_name, selector->val,
6628 				    selector->arg) == DEVFSADM_FAILURE) {
6629 					return (DEVFSADM_FAILURE);
6630 				}
6631 			}
6632 			break;
6633 		default:
6634 			return (DEVFSADM_FAILURE);
6635 		}
6636 	}
6637 
6638 	return (DEVFSADM_SUCCESS);
6639 }
6640 
6641 /*
6642  * For the given minor node and devlinktab_list entry from devlink.tab,
6643  * build a logical dev link and a possible extra devlink.
6644  * Return DEVFSADM_SUCCESS if link is created, otherwise DEVFSADM_FAILURE.
6645  */
6646 static int
6647 build_links(devlinktab_list_t *entry, di_minor_t minor, di_node_t node)
6648 {
6649 	char secondary_link[PATH_MAX + 1];
6650 	char primary_link[PATH_MAX + 1];
6651 	char contents[PATH_MAX + 1];
6652 	char *dev_path;
6653 
6654 	if ((dev_path = di_devfs_path(node)) == NULL) {
6655 		err_print(DI_DEVFS_PATH_FAILED, strerror(errno));
6656 		devfsadm_exit(1);
6657 		/*NOTREACHED*/
6658 	}
6659 	(void) strcpy(contents, dev_path);
6660 	di_devfs_path_free(dev_path);
6661 
6662 	(void) strcat(contents, ":");
6663 	(void) strcat(contents, di_minor_name(minor));
6664 
6665 	if (construct_devlink(primary_link, entry->p_link, contents,
6666 	    minor, node, entry->p_link_pattern) == DEVFSADM_FAILURE) {
6667 		return (DEVFSADM_FAILURE);
6668 	}
6669 	(void) devfsadm_mklink(primary_link, node, minor, 0);
6670 
6671 	if (entry->s_link == NULL) {
6672 		return (DEVFSADM_SUCCESS);
6673 	}
6674 
6675 	if (construct_devlink(secondary_link, entry->s_link, primary_link,
6676 	    minor, node, entry->s_link_pattern) == DEVFSADM_FAILURE) {
6677 		return (DEVFSADM_FAILURE);
6678 	}
6679 
6680 	(void) devfsadm_secondary_link(secondary_link, primary_link, 0);
6681 
6682 	return (DEVFSADM_SUCCESS);
6683 }
6684 
6685 /*
6686  * The counter rule for devlink.tab entries is implemented via
6687  * devfsadm_enumerate_int_start(). One of the arguments to this function
6688  * is a path, where each path component is treated as a regular expression.
6689  * For devlink.tab entries, this path regular expression is derived from
6690  * the devlink spec. get_anchored_re() accepts path regular expressions derived
6691  * from devlink.tab entries and inserts the anchors '^' and '$' at the beginning
6692  * and end respectively of each path component. This is done to prevent
6693  * false matches. For example, without anchors, "a/([0-9]+)" will match "ab/c9"
6694  * and incorrect links will be generated.
6695  */
6696 static int
6697 get_anchored_re(char *link, char *anchored_re, char *pattern)
6698 {
6699 	if (*link == '/' || *link == '\0') {
6700 		err_print(INVALID_DEVLINK_SPEC, pattern);
6701 		return (DEVFSADM_FAILURE);
6702 	}
6703 
6704 	*anchored_re++ = '^';
6705 	for (; *link != '\0'; ) {
6706 		if (*link == '/') {
6707 			while (*link == '/')
6708 				link++;
6709 			*anchored_re++ = '$';
6710 			*anchored_re++ = '/';
6711 			if (*link != '\0') {
6712 				*anchored_re++ = '^';
6713 			}
6714 		} else {
6715 			*anchored_re++ = *link++;
6716 			if (*link == '\0') {
6717 				*anchored_re++ = '$';
6718 			}
6719 		}
6720 	}
6721 	*anchored_re = '\0';
6722 
6723 	return (DEVFSADM_SUCCESS);
6724 }
6725 
6726 static int
6727 construct_devlink(char *link, link_list_t *link_build, char *contents,
6728 			di_minor_t minor, di_node_t node, char *pattern)
6729 {
6730 	int counter_offset = -1;
6731 	devfsadm_enumerate_t rules[1] = {NULL};
6732 	char templink[PATH_MAX + 1];
6733 	char *buff;
6734 	char start[10];
6735 	char *node_path;
6736 	char anchored_re[PATH_MAX + 1];
6737 
6738 	link[0] = '\0';
6739 
6740 	for (; link_build != NULL; link_build = link_build->next) {
6741 		switch (link_build->type) {
6742 		case NAME:
6743 			(void) strcat(link, di_node_name(node));
6744 			break;
6745 		case CONSTANT:
6746 			(void) strcat(link, link_build->constant);
6747 			break;
6748 		case ADDR:
6749 			if (component_cat(link, di_bus_addr(node),
6750 			    link_build->arg) == DEVFSADM_FAILURE) {
6751 				node_path = di_devfs_path(node);
6752 				err_print(CANNOT_BE_USED, pattern, node_path,
6753 				    di_minor_name(minor));
6754 				di_devfs_path_free(node_path);
6755 				return (DEVFSADM_FAILURE);
6756 			}
6757 			break;
6758 		case MINOR:
6759 			if (component_cat(link, di_minor_name(minor),
6760 			    link_build->arg) == DEVFSADM_FAILURE) {
6761 				node_path = di_devfs_path(node);
6762 				err_print(CANNOT_BE_USED, pattern, node_path,
6763 				    di_minor_name(minor));
6764 				di_devfs_path_free(node_path);
6765 				return (DEVFSADM_FAILURE);
6766 			}
6767 			break;
6768 		case COUNTER:
6769 			counter_offset = strlen(link);
6770 			(void) strcat(link, "([0-9]+)");
6771 			(void) sprintf(start, "%d", link_build->arg);
6772 			break;
6773 		default:
6774 			return (DEVFSADM_FAILURE);
6775 		}
6776 	}
6777 
6778 	if (counter_offset != -1) {
6779 		/*
6780 		 * copy anything appended after "([0-9]+)" into
6781 		 * templink
6782 		 */
6783 
6784 		(void) strcpy(templink,
6785 		    &link[counter_offset + strlen("([0-9]+)")]);
6786 		if (get_anchored_re(link, anchored_re, pattern)
6787 		    != DEVFSADM_SUCCESS) {
6788 			return (DEVFSADM_FAILURE);
6789 		}
6790 		rules[0].re = anchored_re;
6791 		rules[0].subexp = 1;
6792 		rules[0].flags = MATCH_ALL;
6793 		if (devfsadm_enumerate_int_start(contents, 0, &buff,
6794 		    rules, 1, start) == DEVFSADM_FAILURE) {
6795 			return (DEVFSADM_FAILURE);
6796 		}
6797 		(void) strcpy(&link[counter_offset], buff);
6798 		free(buff);
6799 		(void) strcat(link, templink);
6800 		vprint(DEVLINK_MID, "COUNTER is	%s\n", link);
6801 	}
6802 	return (DEVFSADM_SUCCESS);
6803 }
6804 
6805 /*
6806  * Compares "field" number of the comma separated list "full_name" with
6807  * field_item.	Returns DEVFSADM_SUCCESS for match,
6808  * DEVFSADM_FAILURE for no match.
6809  */
6810 static int
6811 compare_field(char *full_name, char *field_item, int field)
6812 {
6813 	--field;
6814 	while ((*full_name != '\0') && (field != 0)) {
6815 		if (*(full_name++) == ',') {
6816 			field--;
6817 		}
6818 	}
6819 
6820 	if (field != 0) {
6821 		return (DEVFSADM_FAILURE);
6822 	}
6823 
6824 	while ((*full_name != '\0') && (*field_item != '\0') &&
6825 	    (*full_name != ',')) {
6826 		if (*(full_name++) != *(field_item++)) {
6827 			return (DEVFSADM_FAILURE);
6828 		}
6829 	}
6830 
6831 	if (*field_item != '\0') {
6832 		return (DEVFSADM_FAILURE);
6833 	}
6834 
6835 	if ((*full_name == '\0') || (*full_name == ','))
6836 		return (DEVFSADM_SUCCESS);
6837 
6838 	return (DEVFSADM_FAILURE);
6839 }
6840 
6841 /*
6842  * strcat() field # "field" of comma separated list "name" to "link".
6843  * Field 0 is the entire name.
6844  * Return DEVFSADM_SUCCESS or DEVFSADM_FAILURE.
6845  */
6846 static int
6847 component_cat(char *link, char *name, int field)
6848 {
6849 
6850 	if (name == NULL) {
6851 		return (DEVFSADM_FAILURE);
6852 	}
6853 
6854 	if (field == 0) {
6855 		(void) strcat(link, name);
6856 		return (DEVFSADM_SUCCESS);
6857 	}
6858 
6859 	while (*link != '\0') {
6860 		link++;
6861 	}
6862 
6863 	--field;
6864 	while ((*name != '\0') && (field != 0)) {
6865 		if (*(name++) == ',') {
6866 			--field;
6867 		}
6868 	}
6869 
6870 	if (field != 0) {
6871 		return (DEVFSADM_FAILURE);
6872 	}
6873 
6874 	while ((*name != '\0') && (*name != ',')) {
6875 		*(link++) = *(name++);
6876 	}
6877 
6878 	*link = '\0';
6879 	return (DEVFSADM_SUCCESS);
6880 }
6881 
6882 static void
6883 free_selector_list(selector_list_t *head)
6884 {
6885 	selector_list_t *temp;
6886 
6887 	while (head != NULL) {
6888 		temp = head;
6889 		head = head->next;
6890 		free(temp->val);
6891 		free(temp);
6892 	}
6893 }
6894 
6895 static void
6896 free_link_list(link_list_t *head)
6897 {
6898 	link_list_t *temp;
6899 
6900 	while (head != NULL) {
6901 		temp = head;
6902 		head = head->next;
6903 		if (temp->type == CONSTANT) {
6904 			free(temp->constant);
6905 		}
6906 		free(temp);
6907 	}
6908 }
6909 
6910 /*
6911  * Prints only if level matches one of the debug levels
6912  * given on command line.  INFO_MID is always printed.
6913  *
6914  * See devfsadm.h for a listing of globally defined levels and
6915  * meanings.  Modules should prefix the level with their
6916  * module name to prevent collisions.
6917  */
6918 /*PRINTFLIKE2*/
6919 void
6920 devfsadm_print(char *msgid, char *message, ...)
6921 {
6922 	va_list ap;
6923 	static int newline = TRUE;
6924 	int x;
6925 
6926 	if (msgid != NULL) {
6927 		for (x = 0; x < num_verbose; x++) {
6928 			if (strcmp(verbose[x], msgid) == 0) {
6929 				break;
6930 			}
6931 			if (strcmp(verbose[x], ALL_MID) == 0) {
6932 				break;
6933 			}
6934 		}
6935 		if (x == num_verbose) {
6936 			return;
6937 		}
6938 	}
6939 
6940 	va_start(ap, message);
6941 
6942 	if (msgid == NULL) {
6943 		if (logflag == TRUE) {
6944 			(void) vsyslog(LOG_NOTICE, message, ap);
6945 		} else {
6946 			(void) vfprintf(stdout, message, ap);
6947 		}
6948 
6949 	} else {
6950 		if (logflag == TRUE) {
6951 			(void) syslog(LOG_DEBUG, "%s[%ld]: %s: ",
6952 			    prog, getpid(), msgid);
6953 			(void) vsyslog(LOG_DEBUG, message, ap);
6954 		} else {
6955 			if (newline == TRUE) {
6956 				(void) fprintf(stdout, "%s[%ld]: %s: ",
6957 				    prog, getpid(), msgid);
6958 			}
6959 			(void) vfprintf(stdout, message, ap);
6960 		}
6961 	}
6962 
6963 	if (message[strlen(message) - 1] == '\n') {
6964 		newline = TRUE;
6965 	} else {
6966 		newline = FALSE;
6967 	}
6968 	va_end(ap);
6969 }
6970 
6971 /*
6972  * print error messages to the terminal or to syslog
6973  */
6974 /*PRINTFLIKE1*/
6975 void
6976 devfsadm_errprint(char *message, ...)
6977 {
6978 	va_list ap;
6979 
6980 	va_start(ap, message);
6981 
6982 	if (logflag == TRUE) {
6983 		(void) vsyslog(LOG_ERR, message, ap);
6984 	} else {
6985 		(void) fprintf(stderr, "%s: ", prog);
6986 		(void) vfprintf(stderr, message, ap);
6987 	}
6988 	va_end(ap);
6989 }
6990 
6991 /*
6992  * return noupdate state (-s)
6993  */
6994 int
6995 devfsadm_noupdate(void)
6996 {
6997 	return (file_mods == TRUE ? DEVFSADM_TRUE : DEVFSADM_FALSE);
6998 }
6999 
7000 /*
7001  * return current root update path (-r)
7002  */
7003 const char *
7004 devfsadm_root_path(void)
7005 {
7006 	if (root_dir[0] == '\0') {
7007 		return ("/");
7008 	} else {
7009 		return ((const char *)root_dir);
7010 	}
7011 }
7012 
7013 void
7014 devfsadm_free_dev_names(char **dev_names, int len)
7015 {
7016 	int i;
7017 
7018 	for (i = 0; i < len; i++)
7019 		free(dev_names[i]);
7020 	free(dev_names);
7021 }
7022 
7023 /*
7024  * Return all devlinks corresponding to phys_path as an array of strings.
7025  * The number of entries in the array is returned through lenp.
7026  * devfsadm_free_dev_names() is used to free the returned array.
7027  * NULL is returned on failure or when there are no matching devlinks.
7028  *
7029  * re is an extended regular expression in regex(5) format used to further
7030  * match devlinks pointing to phys_path; it may be NULL to match all
7031  */
7032 char **
7033 devfsadm_lookup_dev_names(char *phys_path, char *re, int *lenp)
7034 {
7035 	struct devlink_cb_arg cb_arg;
7036 	char **dev_names = NULL;
7037 	int i;
7038 
7039 	*lenp = 0;
7040 	cb_arg.count = 0;
7041 	cb_arg.rv = 0;
7042 	(void) di_devlink_cache_walk(devlink_cache, re, phys_path,
7043 	    DI_PRIMARY_LINK, &cb_arg, devlink_cb);
7044 
7045 	if (cb_arg.rv == -1 || cb_arg.count <= 0)
7046 		return (NULL);
7047 
7048 	dev_names = s_malloc(cb_arg.count * sizeof (char *));
7049 	if (dev_names == NULL)
7050 		goto out;
7051 
7052 	for (i = 0; i < cb_arg.count; i++) {
7053 		dev_names[i] = s_strdup(cb_arg.dev_names[i]);
7054 		if (dev_names[i] == NULL) {
7055 			devfsadm_free_dev_names(dev_names, i);
7056 			dev_names = NULL;
7057 			goto out;
7058 		}
7059 	}
7060 	*lenp = cb_arg.count;
7061 
7062 out:
7063 	free_dev_names(&cb_arg);
7064 	return (dev_names);
7065 }
7066 
7067 /* common exit function which ensures releasing locks */
7068 static void
7069 devfsadm_exit(int status)
7070 {
7071 	if (DEVFSADM_DEBUG_ON) {
7072 		vprint(INFO_MID, "exit status = %d\n", status);
7073 	}
7074 
7075 	exit_dev_lock(1);
7076 	exit_daemon_lock(1);
7077 
7078 	if (logflag == TRUE) {
7079 		closelog();
7080 	}
7081 
7082 	exit(status);
7083 	/*NOTREACHED*/
7084 }
7085 
7086 /*
7087  * set root_dir, devices_dir, dev_dir using optarg.
7088  */
7089 static void
7090 set_root_devices_dev_dir(char *dir)
7091 {
7092 	size_t len;
7093 
7094 	root_dir = s_strdup(dir);
7095 	len = strlen(dir) + strlen(DEVICES) + 1;
7096 	devices_dir = s_malloc(len);
7097 	(void) snprintf(devices_dir, len, "%s%s", root_dir, DEVICES);
7098 	len = strlen(root_dir) + strlen(DEV) + 1;
7099 	dev_dir = s_malloc(len);
7100 	(void) snprintf(dev_dir, len, "%s%s", root_dir, DEV);
7101 }
7102 
7103 /*
7104  * Removes quotes.
7105  */
7106 static char *
7107 dequote(char *src)
7108 {
7109 	char	*dst;
7110 	int	len;
7111 
7112 	len = strlen(src);
7113 	dst = s_malloc(len + 1);
7114 	if (src[0] == '\"' && src[len - 1] == '\"') {
7115 		len -= 2;
7116 		(void) strncpy(dst, &src[1], len);
7117 		dst[len] = '\0';
7118 	} else {
7119 		(void) strcpy(dst, src);
7120 	}
7121 	return (dst);
7122 }
7123 
7124 /*
7125  * For a given physical device pathname and spectype, return the
7126  * ownership and permissions attributes by looking in data from
7127  * /etc/minor_perm.  If currently in installation mode, check for
7128  * possible major number translations from the miniroot to the installed
7129  * root's name_to_major table. Note that there can be multiple matches,
7130  * but the last match takes effect.  pts seems to rely on this
7131  * implementation behavior.
7132  */
7133 static void
7134 getattr(char *phy_path, char *aminor, int spectype, dev_t dev, mode_t *mode,
7135 	uid_t *uid, gid_t *gid)
7136 {
7137 	char devname[PATH_MAX + 1];
7138 	char *node_name;
7139 	char *minor_name;
7140 	int match = FALSE;
7141 	int is_clone;
7142 	int mp_drvname_matches_node_name;
7143 	int mp_drvname_matches_minor_name;
7144 	int mp_drvname_is_clone;
7145 	int mp_drvname_matches_drvname;
7146 	struct mperm *mp;
7147 	major_t major_no;
7148 	char driver[PATH_MAX + 1];
7149 
7150 	/*
7151 	 * Get the driver name based on the major number since the name
7152 	 * in /devices may be generic.  Could be running with more major
7153 	 * numbers than are in /etc/name_to_major, so get it from the kernel
7154 	 */
7155 	major_no = major(dev);
7156 
7157 	if (modctl(MODGETNAME, driver, sizeof (driver), &major_no) != 0) {
7158 		/* return default values */
7159 		goto use_defaults;
7160 	}
7161 
7162 	(void) strcpy(devname, phy_path);
7163 
7164 	node_name = strrchr(devname, '/'); /* node name is the last */
7165 					/* component */
7166 	if (node_name == NULL) {
7167 		err_print(NO_NODE, devname);
7168 		goto use_defaults;
7169 	}
7170 
7171 	minor_name = strchr(++node_name, '@'); /* see if it has address part */
7172 
7173 	if (minor_name != NULL) {
7174 		*minor_name++ = '\0';
7175 	} else {
7176 		minor_name = node_name;
7177 	}
7178 
7179 	minor_name = strchr(minor_name, ':'); /* look for minor name */
7180 
7181 	if (minor_name == NULL) {
7182 		err_print(NO_MINOR, devname);
7183 		goto use_defaults;
7184 	}
7185 	*minor_name++ = '\0';
7186 
7187 	/*
7188 	 * mp->mp_drvname = device name from minor_perm
7189 	 * mp->mp_minorname = minor part of device name from
7190 	 * minor_perm
7191 	 * drvname = name of driver for this device
7192 	 */
7193 
7194 	is_clone = (strcmp(node_name, "clone") == 0 ? TRUE : FALSE);
7195 	for (mp = minor_perms; mp != NULL; mp = mp->mp_next) {
7196 		mp_drvname_matches_node_name =
7197 		    (strcmp(mp->mp_drvname, node_name) == 0 ? TRUE : FALSE);
7198 		mp_drvname_matches_minor_name =
7199 		    (strcmp(mp->mp_drvname, minor_name) == 0  ? TRUE:FALSE);
7200 		mp_drvname_is_clone =
7201 		    (strcmp(mp->mp_drvname, "clone") == 0  ? TRUE : FALSE);
7202 		mp_drvname_matches_drvname =
7203 		    (strcmp(mp->mp_drvname, driver) == 0  ? TRUE : FALSE);
7204 
7205 		/*
7206 		 * If one of the following cases is true, then we try to change
7207 		 * the permissions if a "shell global pattern match" of
7208 		 * mp_>mp_minorname matches minor_name.
7209 		 *
7210 		 * 1.  mp->mp_drvname matches driver.
7211 		 *
7212 		 * OR
7213 		 *
7214 		 * 2.  mp->mp_drvname matches node_name and this
7215 		 *	name is an alias of the driver name
7216 		 *
7217 		 * OR
7218 		 *
7219 		 * 3.  /devices entry is the clone device and either
7220 		 *	minor_perm entry is the clone device or matches
7221 		 *	the minor part of the clone device.
7222 		 */
7223 
7224 		if ((mp_drvname_matches_drvname == TRUE)||
7225 		    ((mp_drvname_matches_node_name == TRUE) &&
7226 		    (alias(driver, node_name) == TRUE)) ||
7227 		    ((is_clone == TRUE) &&
7228 		    ((mp_drvname_is_clone == TRUE) ||
7229 		    (mp_drvname_matches_minor_name == TRUE)))) {
7230 			/*
7231 			 * Check that the minor part of the
7232 			 * device name from the minor_perm
7233 			 * entry matches and if so, set the
7234 			 * permissions.
7235 			 *
7236 			 * Under real devfs, clone minor name is changed
7237 			 * to match the driver name, but minor_perm may
7238 			 * not match. We reconcile it here.
7239 			 */
7240 			if (aminor != NULL)
7241 				minor_name = aminor;
7242 
7243 			if (gmatch(minor_name, mp->mp_minorname) != 0) {
7244 				*uid = mp->mp_uid;
7245 				*gid = mp->mp_gid;
7246 				*mode = spectype | mp->mp_mode;
7247 				match = TRUE;
7248 			}
7249 		}
7250 	}
7251 
7252 	if (match == TRUE) {
7253 		return;
7254 	}
7255 
7256 	use_defaults:
7257 	/* not found in minor_perm, so just use default values */
7258 	*uid = root_uid;
7259 	*gid = sys_gid;
7260 	*mode = (spectype | 0600);
7261 }
7262 
7263 /*
7264  * Called by devfs_read_minor_perm() to report errors
7265  * key is:
7266  *	line number: ignoring line number error
7267  *	errno: open/close errors
7268  *	size: alloc errors
7269  */
7270 static void
7271 minorperm_err_cb(minorperm_err_t mp_err, int key)
7272 {
7273 	switch (mp_err) {
7274 	case MP_FOPEN_ERR:
7275 		err_print(FOPEN_FAILED, MINOR_PERM_FILE, strerror(key));
7276 		break;
7277 	case MP_FCLOSE_ERR:
7278 		err_print(FCLOSE_FAILED, MINOR_PERM_FILE, strerror(key));
7279 		break;
7280 	case MP_IGNORING_LINE_ERR:
7281 		err_print(IGNORING_LINE_IN, key, MINOR_PERM_FILE);
7282 		break;
7283 	case MP_ALLOC_ERR:
7284 		err_print(MALLOC_FAILED, key);
7285 		break;
7286 	case MP_NVLIST_ERR:
7287 		err_print(NVLIST_ERROR, MINOR_PERM_FILE, strerror(key));
7288 		break;
7289 	case MP_CANT_FIND_USER_ERR:
7290 		err_print(CANT_FIND_USER, DEFAULT_DEV_USER);
7291 		break;
7292 	case MP_CANT_FIND_GROUP_ERR:
7293 		err_print(CANT_FIND_GROUP, DEFAULT_DEV_GROUP);
7294 		break;
7295 	}
7296 }
7297 
7298 static void
7299 read_minor_perm_file(void)
7300 {
7301 	static int cached = FALSE;
7302 	static struct stat cached_sb;
7303 	struct stat current_sb;
7304 
7305 	(void) stat(MINOR_PERM_FILE, &current_sb);
7306 
7307 	/* If already cached, check to see if it is still valid */
7308 	if (cached == TRUE) {
7309 
7310 		if (current_sb.st_mtime == cached_sb.st_mtime) {
7311 			vprint(FILES_MID, "%s cache valid\n", MINOR_PERM_FILE);
7312 			return;
7313 		}
7314 		devfs_free_minor_perm(minor_perms);
7315 		minor_perms = NULL;
7316 	} else {
7317 		cached = TRUE;
7318 	}
7319 
7320 	(void) stat(MINOR_PERM_FILE, &cached_sb);
7321 
7322 	vprint(FILES_MID, "loading binding file: %s\n", MINOR_PERM_FILE);
7323 
7324 	minor_perms = devfs_read_minor_perm(minorperm_err_cb);
7325 }
7326 
7327 static void
7328 load_minor_perm_file(void)
7329 {
7330 	read_minor_perm_file();
7331 	if (devfs_load_minor_perm(minor_perms, minorperm_err_cb) != 0)
7332 		err_print(gettext("minor_perm load failed\n"));
7333 }
7334 
7335 static char *
7336 convert_to_re(char *dev)
7337 {
7338 	char *p, *l, *out;
7339 	int i;
7340 
7341 	out = s_malloc(PATH_MAX);
7342 
7343 	for (l = p = dev, i = 0; (*p != '\0') && (i < (PATH_MAX - 1));
7344 	    ++p, i++) {
7345 		if ((*p == '*') && ((l != p) && (*l == '/'))) {
7346 			out[i++] = '.';
7347 			out[i] = '+';
7348 		} else {
7349 			out[i] = *p;
7350 		}
7351 		l = p;
7352 	}
7353 	out[i] = '\0';
7354 	p = (char *)s_malloc(strlen(out) + 1);
7355 	(void) strlcpy(p, out, strlen(out) + 1);
7356 	free(out);
7357 
7358 	vprint(FILES_MID, "converted %s -> %s\n", dev, p);
7359 
7360 	return (p);
7361 }
7362 
7363 static void
7364 read_logindevperm_file(void)
7365 {
7366 	static int cached = FALSE;
7367 	static struct stat cached_sb;
7368 	struct stat current_sb;
7369 	struct login_dev *ldev;
7370 	FILE *fp;
7371 	char line[MAX_LDEV_LINE];
7372 	int ln, perm, rv;
7373 	char *cp, *console, *dlist, *dev;
7374 	char *lasts, *devlasts, *permstr, *drv;
7375 	struct driver_list *list, *next;
7376 
7377 	/* Read logindevperm only when enabled */
7378 	if (login_dev_enable != TRUE)
7379 		return;
7380 
7381 	if (cached == TRUE) {
7382 		if (stat(LDEV_FILE, &current_sb) == 0 &&
7383 		    current_sb.st_mtime == cached_sb.st_mtime) {
7384 			vprint(FILES_MID, "%s cache valid\n", LDEV_FILE);
7385 			return;
7386 		}
7387 		vprint(FILES_MID, "invalidating %s cache\n", LDEV_FILE);
7388 		while (login_dev_cache != NULL) {
7389 
7390 			ldev = login_dev_cache;
7391 			login_dev_cache = ldev->ldev_next;
7392 			free(ldev->ldev_console);
7393 			free(ldev->ldev_device);
7394 			regfree(&ldev->ldev_device_regex);
7395 			list = ldev->ldev_driver_list;
7396 			while (list) {
7397 				next = list->next;
7398 				free(list);
7399 				list = next;
7400 			}
7401 			free(ldev);
7402 		}
7403 	} else {
7404 		cached = TRUE;
7405 	}
7406 
7407 	assert(login_dev_cache == NULL);
7408 
7409 	if (stat(LDEV_FILE, &cached_sb) != 0) {
7410 		cached = FALSE;
7411 		return;
7412 	}
7413 
7414 	vprint(FILES_MID, "loading file: %s\n", LDEV_FILE);
7415 
7416 	if ((fp = fopen(LDEV_FILE, "r")) == NULL) {
7417 		/* Not fatal to devfsadm */
7418 		cached = FALSE;
7419 		err_print(FOPEN_FAILED, LDEV_FILE, strerror(errno));
7420 		return;
7421 	}
7422 
7423 	ln = 0;
7424 	while (fgets(line, MAX_LDEV_LINE, fp) != NULL) {
7425 		ln++;
7426 
7427 		/* Remove comments */
7428 		if ((cp = strchr(line, '#')) != NULL)
7429 			*cp = '\0';
7430 
7431 		if ((console = strtok_r(line, LDEV_DELIMS, &lasts)) == NULL)
7432 			continue;	/* Blank line */
7433 
7434 		if ((permstr =  strtok_r(NULL, LDEV_DELIMS, &lasts)) == NULL) {
7435 			err_print(IGNORING_LINE_IN, ln, LDEV_FILE);
7436 			continue;	/* Malformed line */
7437 		}
7438 
7439 		/*
7440 		 * permstr is string in octal format. Convert to int
7441 		 */
7442 		cp = NULL;
7443 		errno = 0;
7444 		perm = strtol(permstr, &cp, 8);
7445 		if (errno || perm < 0 || perm > 0777 || *cp != '\0') {
7446 			err_print(IGNORING_LINE_IN, ln, LDEV_FILE);
7447 			continue;
7448 		}
7449 
7450 		if ((dlist = strtok_r(NULL, LDEV_DELIMS, &lasts)) == NULL) {
7451 			err_print(IGNORING_LINE_IN, ln, LDEV_FILE);
7452 			continue;
7453 		}
7454 
7455 		dev = strtok_r(dlist, LDEV_DEV_DELIM, &devlasts);
7456 		while (dev) {
7457 
7458 			ldev = (struct login_dev *)s_zalloc(
7459 			    sizeof (struct login_dev));
7460 			ldev->ldev_console = s_strdup(console);
7461 			ldev->ldev_perms = perm;
7462 
7463 			/*
7464 			 * the logical device name may contain '*' which
7465 			 * we convert to a regular expression
7466 			 */
7467 			ldev->ldev_device = convert_to_re(dev);
7468 			if (ldev->ldev_device &&
7469 			    (rv = regcomp(&ldev->ldev_device_regex,
7470 			    ldev->ldev_device, REG_EXTENDED))) {
7471 				bzero(&ldev->ldev_device_regex,
7472 				    sizeof (ldev->ldev_device_regex));
7473 				err_print(REGCOMP_FAILED,
7474 				    ldev->ldev_device, rv);
7475 			}
7476 			ldev->ldev_next = login_dev_cache;
7477 			login_dev_cache = ldev;
7478 			dev = strtok_r(NULL, LDEV_DEV_DELIM, &devlasts);
7479 		}
7480 
7481 		drv = strtok_r(NULL, LDEV_DRVLIST_DELIMS, &lasts);
7482 		if (drv) {
7483 			if (strcmp(drv, LDEV_DRVLIST_NAME) == 0) {
7484 
7485 				drv = strtok_r(NULL, LDEV_DRV_DELIMS, &lasts);
7486 
7487 				while (drv) {
7488 					vprint(FILES_MID,
7489 					    "logindevperm driver=%s\n", drv);
7490 
7491 					/*
7492 					 * create a linked list of driver
7493 					 * names
7494 					 */
7495 					list = (struct driver_list *)
7496 					    s_zalloc(
7497 					    sizeof (struct driver_list));
7498 					(void) strlcpy(list->driver_name, drv,
7499 					    sizeof (list->driver_name));
7500 					list->next = ldev->ldev_driver_list;
7501 					ldev->ldev_driver_list = list;
7502 					drv = strtok_r(NULL, LDEV_DRV_DELIMS,
7503 					    &lasts);
7504 				}
7505 			}
7506 		}
7507 	}
7508 	(void) fclose(fp);
7509 }
7510 
7511 /*
7512  * Tokens are separated by ' ', '\t', ':', '=', '&', '|', ';', '\n', or '\0'
7513  *
7514  * Returns DEVFSADM_SUCCESS if token found, DEVFSADM_FAILURE otherwise.
7515  */
7516 static int
7517 getnexttoken(char *next, char **nextp, char **tokenpp, char *tchar)
7518 {
7519 	char *cp;
7520 	char *cp1;
7521 	char *tokenp;
7522 
7523 	cp = next;
7524 	while (*cp == ' ' || *cp == '\t') {
7525 		cp++;			/* skip leading spaces */
7526 	}
7527 	tokenp = cp;			/* start of token */
7528 	while (*cp != '\0' && *cp != '\n' && *cp != ' ' && *cp != '\t' &&
7529 	    *cp != ':' && *cp != '=' && *cp != '&' &&
7530 	    *cp != '|' && *cp != ';') {
7531 		cp++;			/* point to next character */
7532 	}
7533 	/*
7534 	 * If terminating character is a space or tab, look ahead to see if
7535 	 * there's another terminator that's not a space or a tab.
7536 	 * (This code handles trailing spaces.)
7537 	 */
7538 	if (*cp == ' ' || *cp == '\t') {
7539 		cp1 = cp;
7540 		while (*++cp1 == ' ' || *cp1 == '\t')
7541 			;
7542 		if (*cp1 == '=' || *cp1 == ':' || *cp1 == '&' || *cp1 == '|' ||
7543 		    *cp1 == ';' || *cp1 == '\n' || *cp1 == '\0') {
7544 			*cp = NULL;	/* terminate token */
7545 			cp = cp1;
7546 		}
7547 	}
7548 	if (tchar != NULL) {
7549 		*tchar = *cp;		/* save terminating character */
7550 		if (*tchar == '\0') {
7551 			*tchar = '\n';
7552 		}
7553 	}
7554 	*cp++ = '\0';			/* terminate token, point to next */
7555 	*nextp = cp;			/* set pointer to next character */
7556 	if (cp - tokenp - 1 == 0) {
7557 		return (DEVFSADM_FAILURE);
7558 	}
7559 	*tokenpp = tokenp;
7560 	return (DEVFSADM_SUCCESS);
7561 }
7562 
7563 /*
7564  * read or reread the driver aliases file
7565  */
7566 static void
7567 read_driver_aliases_file(void)
7568 {
7569 
7570 	driver_alias_t *save;
7571 	driver_alias_t *lst_tail;
7572 	driver_alias_t *ap;
7573 	static int cached = FALSE;
7574 	FILE *afd;
7575 	char line[256];
7576 	char *cp;
7577 	char *p;
7578 	char t;
7579 	int ln = 0;
7580 	static struct stat cached_sb;
7581 	struct stat current_sb;
7582 
7583 	(void) stat(ALIASFILE, &current_sb);
7584 
7585 	/* If already cached, check to see if it is still valid */
7586 	if (cached == TRUE) {
7587 
7588 		if (current_sb.st_mtime == cached_sb.st_mtime) {
7589 			vprint(FILES_MID, "%s cache valid\n", ALIASFILE);
7590 			return;
7591 		}
7592 
7593 		vprint(FILES_MID, "invalidating %s cache\n", ALIASFILE);
7594 		while (driver_aliases != NULL) {
7595 			free(driver_aliases->alias_name);
7596 			free(driver_aliases->driver_name);
7597 			save = driver_aliases;
7598 			driver_aliases = driver_aliases->next;
7599 			free(save);
7600 		}
7601 	} else {
7602 		cached = TRUE;
7603 	}
7604 
7605 	(void) stat(ALIASFILE, &cached_sb);
7606 
7607 	vprint(FILES_MID, "loading binding file: %s\n", ALIASFILE);
7608 
7609 	if ((afd = fopen(ALIASFILE, "r")) == NULL) {
7610 		err_print(FOPEN_FAILED, ALIASFILE, strerror(errno));
7611 		devfsadm_exit(1);
7612 		/*NOTREACHED*/
7613 	}
7614 
7615 	while (fgets(line, sizeof (line), afd) != NULL) {
7616 		ln++;
7617 		/* cut off comments starting with '#' */
7618 		if ((cp = strchr(line, '#')) != NULL)
7619 			*cp = '\0';
7620 		/* ignore comment or blank lines */
7621 		if (is_blank(line))
7622 			continue;
7623 		cp = line;
7624 		if (getnexttoken(cp, &cp, &p, &t) == DEVFSADM_FAILURE) {
7625 			err_print(IGNORING_LINE_IN, ln, ALIASFILE);
7626 			continue;
7627 		}
7628 		if (t == '\n' || t == '\0') {
7629 			err_print(DRV_BUT_NO_ALIAS, ln, ALIASFILE);
7630 			continue;
7631 		}
7632 		ap = (struct driver_alias *)
7633 		    s_zalloc(sizeof (struct driver_alias));
7634 		ap->driver_name = s_strdup(p);
7635 		if (getnexttoken(cp, &cp, &p, &t) == DEVFSADM_FAILURE) {
7636 			err_print(DRV_BUT_NO_ALIAS, ln, ALIASFILE);
7637 			free(ap->driver_name);
7638 			free(ap);
7639 			continue;
7640 		}
7641 		if (*p == '"') {
7642 			if (p[strlen(p) - 1] == '"') {
7643 				p[strlen(p) - 1] = '\0';
7644 				p++;
7645 			}
7646 		}
7647 		ap->alias_name = s_strdup(p);
7648 		if (driver_aliases == NULL) {
7649 			driver_aliases = ap;
7650 			lst_tail = ap;
7651 		} else {
7652 			lst_tail->next = ap;
7653 			lst_tail = ap;
7654 		}
7655 	}
7656 	if (fclose(afd) == EOF) {
7657 		err_print(FCLOSE_FAILED, ALIASFILE, strerror(errno));
7658 	}
7659 }
7660 
7661 /*
7662  * return TRUE if alias_name is an alias for driver_name, otherwise
7663  * return FALSE.
7664  */
7665 static int
7666 alias(char *driver_name, char *alias_name)
7667 {
7668 	driver_alias_t *alias;
7669 
7670 	/*
7671 	 * check for a match
7672 	 */
7673 	for (alias = driver_aliases; alias != NULL; alias = alias->next) {
7674 		if ((strcmp(alias->driver_name, driver_name) == 0) &&
7675 		    (strcmp(alias->alias_name, alias_name) == 0)) {
7676 			return (TRUE);
7677 		}
7678 	}
7679 	return (FALSE);
7680 }
7681 
7682 /*
7683  * convenience functions
7684  */
7685 static int
7686 s_stat(const char *path, struct stat *sbufp)
7687 {
7688 	int rv;
7689 retry:
7690 	if ((rv = stat(path, sbufp)) == -1) {
7691 		if (errno == EINTR)
7692 			goto retry;
7693 	}
7694 	return (rv);
7695 }
7696 
7697 static void *
7698 s_malloc(const size_t size)
7699 {
7700 	void *rp;
7701 
7702 	rp = malloc(size);
7703 	if (rp == NULL) {
7704 		err_print(MALLOC_FAILED, size);
7705 		devfsadm_exit(1);
7706 		/*NOTREACHED*/
7707 	}
7708 	return (rp);
7709 }
7710 
7711 /*
7712  * convenience functions
7713  */
7714 static void *
7715 s_realloc(void *ptr, const size_t size)
7716 {
7717 	ptr = realloc(ptr, size);
7718 	if (ptr == NULL) {
7719 		err_print(REALLOC_FAILED, size);
7720 		devfsadm_exit(1);
7721 		/*NOTREACHED*/
7722 	}
7723 	return (ptr);
7724 }
7725 
7726 static void *
7727 s_zalloc(const size_t size)
7728 {
7729 	void *rp;
7730 
7731 	rp = calloc(1, size);
7732 	if (rp == NULL) {
7733 		err_print(CALLOC_FAILED, size);
7734 		devfsadm_exit(1);
7735 		/*NOTREACHED*/
7736 	}
7737 	return (rp);
7738 }
7739 
7740 char *
7741 s_strdup(const char *ptr)
7742 {
7743 	void *rp;
7744 
7745 	rp = strdup(ptr);
7746 	if (rp == NULL) {
7747 		err_print(STRDUP_FAILED, ptr);
7748 		devfsadm_exit(1);
7749 		/*NOTREACHED*/
7750 	}
7751 	return (rp);
7752 }
7753 
7754 static void
7755 s_closedir(DIR *dirp)
7756 {
7757 retry:
7758 	if (closedir(dirp) != 0) {
7759 		if (errno == EINTR)
7760 			goto retry;
7761 		err_print(CLOSEDIR_FAILED, strerror(errno));
7762 	}
7763 }
7764 
7765 static void
7766 s_mkdirp(const char *path, const mode_t mode)
7767 {
7768 	vprint(CHATTY_MID, "mkdirp(%s, 0x%lx)\n", path, mode);
7769 	if (mkdirp(path, mode) == -1) {
7770 		if (errno != EEXIST) {
7771 			err_print(MKDIR_FAILED, path, mode, strerror(errno));
7772 		}
7773 	}
7774 }
7775 
7776 static void
7777 s_unlink(const char *file)
7778 {
7779 retry:
7780 	if (unlink(file) == -1) {
7781 		if (errno == EINTR || errno == EAGAIN)
7782 			goto retry;
7783 		if (errno != ENOENT) {
7784 			err_print(UNLINK_FAILED, file, strerror(errno));
7785 		}
7786 	}
7787 }
7788 
7789 static void
7790 add_verbose_id(char *mid)
7791 {
7792 	num_verbose++;
7793 	verbose = s_realloc(verbose, num_verbose * sizeof (char *));
7794 	verbose[num_verbose - 1] = mid;
7795 }
7796 
7797 /*
7798  * returns DEVFSADM_TRUE if contents is a minor node in /devices.
7799  * If mn_root is not NULL, mn_root is set to:
7800  *	if contents is a /dev node, mn_root = contents
7801  * 			OR
7802  *	if contents is a /devices node, mn_root set to the '/'
7803  *	following /devices.
7804  */
7805 static int
7806 is_minor_node(char *contents, char **mn_root)
7807 {
7808 	char *ptr;
7809 	char device_prefix[100];
7810 
7811 	(void) snprintf(device_prefix, sizeof (device_prefix), "../devices/");
7812 
7813 	if ((ptr = strstr(contents, device_prefix)) != NULL) {
7814 		if (mn_root != NULL) {
7815 			/* mn_root should point to the / following /devices */
7816 			*mn_root = ptr += strlen(device_prefix) - 1;
7817 		}
7818 		return (DEVFSADM_TRUE);
7819 	}
7820 
7821 	(void) snprintf(device_prefix, sizeof (device_prefix), "/devices/");
7822 
7823 	if (strncmp(contents, device_prefix, strlen(device_prefix)) == 0) {
7824 		if (mn_root != NULL) {
7825 			/* mn_root should point to the / following /devices */
7826 			*mn_root = contents + strlen(device_prefix) - 1;
7827 		}
7828 		return (DEVFSADM_TRUE);
7829 	}
7830 
7831 	if (mn_root != NULL) {
7832 		*mn_root = contents;
7833 	}
7834 	return (DEVFSADM_FALSE);
7835 }
7836 
7837 /*
7838  * Add the specified property to nvl.
7839  * Returns:
7840  *   0	successfully added
7841  *   -1	an error occurred
7842  *   1	could not add the property for reasons not due to errors.
7843  */
7844 static int
7845 add_property(nvlist_t *nvl, di_prop_t prop)
7846 {
7847 	char *name;
7848 	char *attr_name;
7849 	int n, len;
7850 	int32_t *int32p;
7851 	int64_t *int64p;
7852 	char *str;
7853 	char **strarray;
7854 	uchar_t *bytep;
7855 	int rv = 0;
7856 	int i;
7857 
7858 	if ((name = di_prop_name(prop)) == NULL)
7859 		return (-1);
7860 
7861 	len = sizeof (DEV_PROP_PREFIX) + strlen(name);
7862 	if ((attr_name = malloc(len)) == NULL)
7863 		return (-1);
7864 
7865 	(void) strlcpy(attr_name, DEV_PROP_PREFIX, len);
7866 	(void) strlcat(attr_name, name, len);
7867 
7868 	switch (di_prop_type(prop)) {
7869 	case DI_PROP_TYPE_BOOLEAN:
7870 		if (nvlist_add_boolean(nvl, attr_name) != 0)
7871 			goto out;
7872 		break;
7873 
7874 	case DI_PROP_TYPE_INT:
7875 		if ((n = di_prop_ints(prop, &int32p)) < 1)
7876 			goto out;
7877 
7878 		if (n <= (PROP_LEN_LIMIT / sizeof (int32_t))) {
7879 			if (nvlist_add_int32_array(nvl, attr_name, int32p,
7880 			    n) != 0)
7881 				goto out;
7882 		} else
7883 			rv = 1;
7884 		break;
7885 
7886 	case DI_PROP_TYPE_INT64:
7887 		if ((n = di_prop_int64(prop, &int64p)) < 1)
7888 			goto out;
7889 
7890 		if (n <= (PROP_LEN_LIMIT / sizeof (int64_t))) {
7891 			if (nvlist_add_int64_array(nvl, attr_name, int64p,
7892 			    n) != 0)
7893 				goto out;
7894 		} else
7895 			rv = 1;
7896 		break;
7897 
7898 	case DI_PROP_TYPE_BYTE:
7899 	case DI_PROP_TYPE_UNKNOWN:
7900 		if ((n = di_prop_bytes(prop, &bytep)) < 1)
7901 			goto out;
7902 
7903 		if (n <= PROP_LEN_LIMIT) {
7904 			if (nvlist_add_byte_array(nvl, attr_name, bytep, n)
7905 			    != 0)
7906 				goto out;
7907 		} else
7908 			rv = 1;
7909 		break;
7910 
7911 	case DI_PROP_TYPE_STRING:
7912 		if ((n = di_prop_strings(prop, &str)) < 1)
7913 			goto out;
7914 
7915 		if ((strarray = malloc(n * sizeof (char *))) == NULL)
7916 			goto out;
7917 
7918 		len = 0;
7919 		for (i = 0; i < n; i++) {
7920 			strarray[i] = str + len;
7921 			len += strlen(strarray[i]) + 1;
7922 		}
7923 
7924 		if (len <= PROP_LEN_LIMIT) {
7925 			if (nvlist_add_string_array(nvl, attr_name, strarray,
7926 			    n) != 0) {
7927 				free(strarray);
7928 				goto out;
7929 			}
7930 		} else
7931 			rv = 1;
7932 		free(strarray);
7933 		break;
7934 
7935 	default:
7936 		rv = 1;
7937 		break;
7938 	}
7939 
7940 	free(attr_name);
7941 	return (rv);
7942 
7943 out:
7944 	free(attr_name);
7945 	return (-1);
7946 }
7947 
7948 static void
7949 free_dev_names(struct devlink_cb_arg *x)
7950 {
7951 	int i;
7952 
7953 	for (i = 0; i < x->count; i++) {
7954 		free(x->dev_names[i]);
7955 		free(x->link_contents[i]);
7956 	}
7957 }
7958 
7959 /* callback function for di_devlink_cache_walk */
7960 static int
7961 devlink_cb(di_devlink_t dl, void *arg)
7962 {
7963 	struct devlink_cb_arg *x = (struct devlink_cb_arg *)arg;
7964 	const char *path;
7965 	const char *content;
7966 
7967 	if ((path = di_devlink_path(dl)) == NULL ||
7968 	    (content = di_devlink_content(dl)) == NULL ||
7969 	    (x->dev_names[x->count] = s_strdup(path)) == NULL)
7970 		goto out;
7971 
7972 	if ((x->link_contents[x->count] = s_strdup(content)) == NULL) {
7973 		free(x->dev_names[x->count]);
7974 		goto out;
7975 	}
7976 
7977 	x->count++;
7978 	if (x->count >= MAX_DEV_NAME_COUNT)
7979 		return (DI_WALK_TERMINATE);
7980 
7981 	return (DI_WALK_CONTINUE);
7982 
7983 out:
7984 	x->rv = -1;
7985 	free_dev_names(x);
7986 	return (DI_WALK_TERMINATE);
7987 }
7988 
7989 /*
7990  * Lookup dev name corresponding to the phys_path.
7991  * phys_path is path to a node or minor node.
7992  * Returns:
7993  *	0 with *dev_name set to the dev name
7994  *		Lookup succeeded and dev_name found
7995  *	0 with *dev_name set to NULL
7996  *		Lookup encountered no errors but dev name not found
7997  *	-1
7998  *		Lookup failed
7999  */
8000 static int
8001 lookup_dev_name(char *phys_path, char **dev_name)
8002 {
8003 	struct devlink_cb_arg cb_arg;
8004 
8005 	*dev_name = NULL;
8006 
8007 	cb_arg.count = 0;
8008 	cb_arg.rv = 0;
8009 	(void) di_devlink_cache_walk(devlink_cache, NULL, phys_path,
8010 	    DI_PRIMARY_LINK, &cb_arg, devlink_cb);
8011 
8012 	if (cb_arg.rv == -1)
8013 		return (-1);
8014 
8015 	if (cb_arg.count > 0) {
8016 		*dev_name = s_strdup(cb_arg.dev_names[0]);
8017 		free_dev_names(&cb_arg);
8018 		if (*dev_name == NULL)
8019 			return (-1);
8020 	}
8021 
8022 	return (0);
8023 }
8024 
8025 static char *
8026 lookup_disk_dev_name(char *node_path)
8027 {
8028 	struct devlink_cb_arg cb_arg;
8029 	char *dev_name = NULL;
8030 	int i;
8031 	char *p;
8032 	int len1, len2;
8033 
8034 #define	DEV_RDSK	"/dev/rdsk/"
8035 #define	DISK_RAW_MINOR	",raw"
8036 
8037 	cb_arg.count = 0;
8038 	cb_arg.rv = 0;
8039 	(void) di_devlink_cache_walk(devlink_cache, NULL, node_path,
8040 	    DI_PRIMARY_LINK, &cb_arg, devlink_cb);
8041 
8042 	if (cb_arg.rv == -1 || cb_arg.count == 0)
8043 		return (NULL);
8044 
8045 	/* first try lookup based on /dev/rdsk name */
8046 	for (i = 0; i < cb_arg.count; i++) {
8047 		if (strncmp(cb_arg.dev_names[i], DEV_RDSK,
8048 		    sizeof (DEV_RDSK) - 1) == 0) {
8049 			dev_name = s_strdup(cb_arg.dev_names[i]);
8050 			break;
8051 		}
8052 	}
8053 
8054 	if (dev_name == NULL) {
8055 		/* now try lookup based on a minor name ending with ",raw" */
8056 		len1 = sizeof (DISK_RAW_MINOR) - 1;
8057 		for (i = 0; i < cb_arg.count; i++) {
8058 			len2 = strlen(cb_arg.link_contents[i]);
8059 			if (len2 >= len1 &&
8060 			    strcmp(cb_arg.link_contents[i] + len2 - len1,
8061 			    DISK_RAW_MINOR) == 0) {
8062 				dev_name = s_strdup(cb_arg.dev_names[i]);
8063 				break;
8064 			}
8065 		}
8066 	}
8067 
8068 	free_dev_names(&cb_arg);
8069 
8070 	if (dev_name == NULL)
8071 		return (NULL);
8072 	if (strlen(dev_name) == 0) {
8073 		free(dev_name);
8074 		return (NULL);
8075 	}
8076 
8077 	/* if the name contains slice or partition number strip it */
8078 	p = dev_name + strlen(dev_name) - 1;
8079 	if (isdigit(*p)) {
8080 		while (p != dev_name && isdigit(*p))
8081 			p--;
8082 		if (*p == 's' || *p == 'p')
8083 			*p = '\0';
8084 	}
8085 
8086 	return (dev_name);
8087 }
8088 
8089 static char *
8090 lookup_lofi_dev_name(char *node_path, char *minor)
8091 {
8092 	struct devlink_cb_arg cb_arg;
8093 	char *dev_name = NULL;
8094 	int i;
8095 	int len1, len2;
8096 
8097 	cb_arg.count = 0;
8098 	cb_arg.rv = 0;
8099 	(void) di_devlink_cache_walk(devlink_cache, NULL, node_path,
8100 	    DI_PRIMARY_LINK, &cb_arg, devlink_cb);
8101 
8102 	if (cb_arg.rv == -1 || cb_arg.count == 0)
8103 		return (NULL);
8104 
8105 	/* lookup based on a minor name ending with ",raw" */
8106 	len1 = strlen(minor);
8107 	for (i = 0; i < cb_arg.count; i++) {
8108 		len2 = strlen(cb_arg.link_contents[i]);
8109 		if (len2 >= len1 &&
8110 		    strcmp(cb_arg.link_contents[i] + len2 - len1,
8111 		    minor) == 0) {
8112 			dev_name = s_strdup(cb_arg.dev_names[i]);
8113 			break;
8114 		}
8115 	}
8116 
8117 	free_dev_names(&cb_arg);
8118 
8119 	if (dev_name == NULL)
8120 		return (NULL);
8121 	if (strlen(dev_name) == 0) {
8122 		free(dev_name);
8123 		return (NULL);
8124 	}
8125 
8126 	return (dev_name);
8127 }
8128 
8129 static char *
8130 lookup_network_dev_name(char *node_path, char *driver_name)
8131 {
8132 	char *dev_name = NULL;
8133 	char phys_path[MAXPATHLEN];
8134 
8135 	if (lookup_dev_name(node_path, &dev_name) == -1)
8136 		return (NULL);
8137 
8138 	if (dev_name == NULL) {
8139 		/* dlpi style-2 only interface */
8140 		(void) snprintf(phys_path, sizeof (phys_path),
8141 		    "/pseudo/clone@0:%s", driver_name);
8142 		if (lookup_dev_name(phys_path, &dev_name) == -1 ||
8143 		    dev_name == NULL)
8144 			return (NULL);
8145 	}
8146 
8147 	return (dev_name);
8148 }
8149 
8150 static char *
8151 lookup_printer_dev_name(char *node_path)
8152 {
8153 	struct devlink_cb_arg cb_arg;
8154 	char *dev_name = NULL;
8155 	int i;
8156 
8157 #define	DEV_PRINTERS	"/dev/printers/"
8158 
8159 	cb_arg.count = 0;
8160 	cb_arg.rv = 0;
8161 	(void) di_devlink_cache_walk(devlink_cache, NULL, node_path,
8162 	    DI_PRIMARY_LINK, &cb_arg, devlink_cb);
8163 
8164 	if (cb_arg.rv == -1 || cb_arg.count == 0)
8165 		return (NULL);
8166 
8167 	/* first try lookup based on /dev/printers name */
8168 	for (i = 0; i < cb_arg.count; i++) {
8169 		if (strncmp(cb_arg.dev_names[i], DEV_PRINTERS,
8170 		    sizeof (DEV_PRINTERS) - 1) == 0) {
8171 			dev_name = s_strdup(cb_arg.dev_names[i]);
8172 			break;
8173 		}
8174 	}
8175 
8176 	/* fallback to the first name */
8177 	if ((dev_name == NULL) && (cb_arg.count > 0))
8178 		dev_name = s_strdup(cb_arg.dev_names[0]);
8179 
8180 	free_dev_names(&cb_arg);
8181 
8182 	return (dev_name);
8183 }
8184 
8185 /*
8186  * Build an nvlist containing all attributes for devfs events.
8187  * Returns nvlist pointer on success, NULL on failure.
8188  */
8189 static nvlist_t *
8190 build_event_attributes(char *class, char *subclass, char *node_path,
8191     di_node_t node, char *driver_name, int instance, char *minor)
8192 {
8193 	nvlist_t *nvl;
8194 	int err = 0;
8195 	di_prop_t prop;
8196 	int count;
8197 	char *prop_name;
8198 	int x;
8199 	char *dev_name = NULL;
8200 	int dev_name_lookup_err = 0;
8201 
8202 	if ((err = nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, 0)) != 0) {
8203 		nvl = NULL;
8204 		goto out;
8205 	}
8206 
8207 	if ((err = nvlist_add_int32(nvl, EV_VERSION, EV_V1)) != 0)
8208 		goto out;
8209 
8210 	if ((err = nvlist_add_string(nvl, DEV_PHYS_PATH, node_path)) != 0)
8211 		goto out;
8212 
8213 	if (strcmp(class, EC_DEV_ADD) != 0 &&
8214 	    strcmp(class, EC_DEV_REMOVE) != 0)
8215 		return (nvl);
8216 
8217 	if (driver_name == NULL || instance == -1)
8218 		goto out;
8219 
8220 	if (strcmp(subclass, ESC_DISK) == 0) {
8221 		if ((dev_name = lookup_disk_dev_name(node_path)) == NULL) {
8222 			dev_name_lookup_err = 1;
8223 			goto out;
8224 		}
8225 	} else if (strcmp(subclass, ESC_NETWORK) == 0) {
8226 		if ((dev_name = lookup_network_dev_name(node_path, driver_name))
8227 		    == NULL) {
8228 			dev_name_lookup_err = 1;
8229 			goto out;
8230 		}
8231 	} else if (strcmp(subclass, ESC_PRINTER) == 0) {
8232 		if ((dev_name = lookup_printer_dev_name(node_path)) == NULL) {
8233 			dev_name_lookup_err = 1;
8234 			goto out;
8235 		}
8236 	} else if (strcmp(subclass, ESC_LOFI) == 0) {
8237 		/*
8238 		 * The raw minor node is created or removed after the block
8239 		 * node.  Lofi devfs events are dependent on this behavior.
8240 		 * Generate the sysevent only for the raw minor node.
8241 		 */
8242 		if (strstr(minor, "raw") == NULL) {
8243 			if (nvl) {
8244 				nvlist_free(nvl);
8245 			}
8246 			return (NULL);
8247 		}
8248 		if ((dev_name = lookup_lofi_dev_name(node_path, minor)) ==
8249 		    NULL) {
8250 			dev_name_lookup_err = 1;
8251 			goto out;
8252 		}
8253 	}
8254 
8255 	if (dev_name) {
8256 		if ((err = nvlist_add_string(nvl, DEV_NAME, dev_name)) != 0)
8257 			goto out;
8258 		free(dev_name);
8259 		dev_name = NULL;
8260 	}
8261 
8262 	if ((err = nvlist_add_string(nvl, DEV_DRIVER_NAME, driver_name)) != 0)
8263 		goto out;
8264 
8265 	if ((err = nvlist_add_int32(nvl, DEV_INSTANCE, instance)) != 0)
8266 		goto out;
8267 
8268 	if (strcmp(class, EC_DEV_ADD) == 0) {
8269 		/* add properties */
8270 		count = 0;
8271 		for (prop = di_prop_next(node, DI_PROP_NIL);
8272 		    prop != DI_PROP_NIL && count < MAX_PROP_COUNT;
8273 		    prop = di_prop_next(node, prop)) {
8274 
8275 			if (di_prop_devt(prop) != DDI_DEV_T_NONE)
8276 				continue;
8277 
8278 			if ((x = add_property(nvl, prop)) == 0)
8279 				count++;
8280 			else if (x == -1) {
8281 				if ((prop_name = di_prop_name(prop)) == NULL)
8282 					prop_name = "";
8283 				err_print(PROP_ADD_FAILED, prop_name);
8284 				goto out;
8285 			}
8286 		}
8287 	}
8288 
8289 	return (nvl);
8290 
8291 out:
8292 	if (nvl)
8293 		nvlist_free(nvl);
8294 
8295 	if (dev_name)
8296 		free(dev_name);
8297 
8298 	if (dev_name_lookup_err) {
8299 		/*
8300 		 * If a lofi mount fails, the /devices node may well have
8301 		 * disappeared by the time we run, so let's not complain.
8302 		 */
8303 		if (strcmp(subclass, ESC_LOFI) != 0)
8304 			err_print(DEV_NAME_LOOKUP_FAILED, node_path);
8305 	} else {
8306 		err_print(BUILD_EVENT_ATTR_FAILED, (err) ? strerror(err) : "");
8307 	}
8308 	return (NULL);
8309 }
8310 
8311 static void
8312 log_event(char *class, char *subclass, nvlist_t *nvl)
8313 {
8314 	sysevent_id_t eid;
8315 
8316 	if (sysevent_post_event(class, subclass, "SUNW", DEVFSADMD,
8317 	    nvl, &eid) != 0) {
8318 		err_print(LOG_EVENT_FAILED, strerror(errno));
8319 	}
8320 }
8321 
8322 /*
8323  * When devfsadmd needs to generate sysevents, they are queued for later
8324  * delivery this allows them to be delivered after the devlinks db cache has
8325  * been flushed guaranteeing that applications consuming these events have
8326  * access to an accurate devlinks db.  The queue is a FIFO, sysevents to be
8327  * inserted in the front of the queue and consumed off the back.
8328  */
8329 static void
8330 enqueue_sysevent(char *class, char *subclass, nvlist_t *nvl)
8331 {
8332 	syseventq_t *tmp;
8333 
8334 	if ((tmp = s_zalloc(sizeof (*tmp))) == NULL)
8335 		return;
8336 
8337 	tmp->class = s_strdup(class);
8338 	tmp->subclass = s_strdup(subclass);
8339 	tmp->nvl = nvl;
8340 
8341 	(void) mutex_lock(&syseventq_mutex);
8342 	if (syseventq_front != NULL)
8343 		syseventq_front->next = tmp;
8344 	else
8345 		syseventq_back = tmp;
8346 	syseventq_front = tmp;
8347 	(void) mutex_unlock(&syseventq_mutex);
8348 }
8349 
8350 static void
8351 process_syseventq()
8352 {
8353 	(void) mutex_lock(&syseventq_mutex);
8354 	while (syseventq_back != NULL) {
8355 		syseventq_t *tmp = syseventq_back;
8356 
8357 		vprint(CHATTY_MID, "sending queued event: %s, %s\n",
8358 		    tmp->class, tmp->subclass);
8359 
8360 		log_event(tmp->class, tmp->subclass, tmp->nvl);
8361 
8362 		if (tmp->class != NULL)
8363 			free(tmp->class);
8364 		if (tmp->subclass != NULL)
8365 			free(tmp->subclass);
8366 		if (tmp->nvl != NULL)
8367 			nvlist_free(tmp->nvl);
8368 		syseventq_back = syseventq_back->next;
8369 		if (syseventq_back == NULL)
8370 			syseventq_front = NULL;
8371 		free(tmp);
8372 	}
8373 	(void) mutex_unlock(&syseventq_mutex);
8374 }
8375 
8376 static void
8377 build_and_enq_event(char *class, char *subclass, char *node_path,
8378 	di_node_t node, char *minor)
8379 {
8380 	nvlist_t *nvl;
8381 
8382 	vprint(CHATTY_MID, "build_and_enq_event(%s, %s, %s, 0x%8.8x)\n",
8383 	    class, subclass, node_path, (int)node);
8384 
8385 	if (node != DI_NODE_NIL)
8386 		nvl = build_event_attributes(class, subclass, node_path, node,
8387 		    di_driver_name(node), di_instance(node), minor);
8388 	else
8389 		nvl = build_event_attributes(class, subclass, node_path, node,
8390 		    NULL, -1, minor);
8391 
8392 	if (nvl) {
8393 		enqueue_sysevent(class, subclass, nvl);
8394 	}
8395 }
8396 
8397 /*
8398  * is_blank() returns 1 (true) if a line specified is composed of
8399  * whitespace characters only. otherwise, it returns 0 (false).
8400  *
8401  * Note. the argument (line) must be null-terminated.
8402  */
8403 static int
8404 is_blank(char *line)
8405 {
8406 	for (/* nothing */; *line != '\0'; line++)
8407 		if (!isspace(*line))
8408 			return (0);
8409 	return (1);
8410 }
8411 
8412 /*
8413  * Functions to deal with the no-further-processing hash
8414  */
8415 
8416 static void
8417 nfphash_create(void)
8418 {
8419 	assert(nfp_hash == NULL);
8420 	nfp_hash = s_zalloc(NFP_HASH_SZ * sizeof (item_t *));
8421 }
8422 
8423 static int
8424 nfphash_fcn(char *key)
8425 {
8426 	int i;
8427 	uint64_t sum = 0;
8428 
8429 	for (i = 0; key[i] != '\0'; i++) {
8430 		sum += (uchar_t)key[i];
8431 	}
8432 
8433 	return (sum % NFP_HASH_SZ);
8434 }
8435 
8436 static item_t *
8437 nfphash_lookup(char *key)
8438 {
8439 	int	index;
8440 	item_t  *ip;
8441 
8442 	index = nfphash_fcn(key);
8443 
8444 	assert(index >= 0);
8445 
8446 	for (ip = nfp_hash[index]; ip; ip = ip->i_next) {
8447 		if (strcmp(ip->i_key, key) == 0)
8448 			return (ip);
8449 	}
8450 
8451 	return (NULL);
8452 }
8453 
8454 static void
8455 nfphash_insert(char *key)
8456 {
8457 	item_t	*ip;
8458 	int	index;
8459 
8460 	index = nfphash_fcn(key);
8461 
8462 	assert(index >= 0);
8463 
8464 	ip = s_zalloc(sizeof (item_t));
8465 	ip->i_key = s_strdup(key);
8466 
8467 	ip->i_next = nfp_hash[index];
8468 	nfp_hash[index] = ip;
8469 }
8470 
8471 static void
8472 nfphash_destroy(void)
8473 {
8474 	int	i;
8475 	item_t	*ip;
8476 
8477 	for (i = 0; i < NFP_HASH_SZ; i++) {
8478 		/*LINTED*/
8479 		while (ip = nfp_hash[i]) {
8480 			nfp_hash[i] = ip->i_next;
8481 			free(ip->i_key);
8482 			free(ip);
8483 		}
8484 	}
8485 
8486 	free(nfp_hash);
8487 	nfp_hash = NULL;
8488 }
8489 
8490 static int
8491 devname_kcall(int subcmd, void *args)
8492 {
8493 	int error = 0;
8494 
8495 	switch (subcmd) {
8496 	case MODDEVNAME_LOOKUPDOOR:
8497 		error = modctl(MODDEVNAME, subcmd, (uintptr_t)args);
8498 		if (error) {
8499 			vprint(INFO_MID, "modctl(MODDEVNAME, "
8500 			    "MODDEVNAME_LOOKUPDOOR) failed - %s\n",
8501 			    strerror(errno));
8502 		}
8503 		break;
8504 	default:
8505 		error = EINVAL;
8506 		break;
8507 	}
8508 	return (error);
8509 }
8510 
8511 /* ARGSUSED */
8512 static void
8513 devname_lookup_handler(void *cookie, char *argp, size_t arg_size,
8514     door_desc_t *dp, uint_t n_desc)
8515 {
8516 	int32_t error = 0;
8517 	door_cred_t dcred;
8518 	struct dca_impl	dci;
8519 	uint8_t	cmd;
8520 	sdev_door_res_t res;
8521 	sdev_door_arg_t *args;
8522 
8523 	if (argp == NULL || arg_size == 0) {
8524 		vprint(DEVNAME_MID, "devname_lookup_handler: argp wrong\n");
8525 		error = DEVFSADM_RUN_INVALID;
8526 		goto done;
8527 	}
8528 	vprint(DEVNAME_MID, "devname_lookup_handler\n");
8529 
8530 	if (door_cred(&dcred) != 0 || dcred.dc_euid != 0) {
8531 		vprint(DEVNAME_MID, "devname_lookup_handler: cred wrong\n");
8532 		error = DEVFSADM_RUN_EPERM;
8533 		goto done;
8534 	}
8535 
8536 	args = (sdev_door_arg_t *)argp;
8537 	cmd = args->devfsadm_cmd;
8538 
8539 	vprint(DEVNAME_MID, "devname_lookup_handler: cmd %d\n", cmd);
8540 	switch (cmd) {
8541 	case DEVFSADMD_RUN_ALL:
8542 		/*
8543 		 * run "devfsadm"
8544 		 */
8545 		dci.dci_root = "/";
8546 		dci.dci_minor = NULL;
8547 		dci.dci_driver = NULL;
8548 		dci.dci_error = 0;
8549 		dci.dci_flags = 0;
8550 		dci.dci_arg = NULL;
8551 
8552 		lock_dev();
8553 		update_drvconf((major_t)-1, 0);
8554 		dci.dci_flags |= DCA_FLUSH_PATHINST;
8555 
8556 		pre_and_post_cleanup(RM_PRE);
8557 		devi_tree_walk(&dci, DI_CACHE_SNAPSHOT_FLAGS, NULL);
8558 		error = (int32_t)dci.dci_error;
8559 		if (!error) {
8560 			pre_and_post_cleanup(RM_POST);
8561 			update_database = TRUE;
8562 			unlock_dev(SYNC_STATE);
8563 			update_database = FALSE;
8564 		} else {
8565 			if (DEVFSADM_DEBUG_ON) {
8566 				vprint(INFO_MID, "devname_lookup_handler: "
8567 				    "DEVFSADMD_RUN_ALL failed\n");
8568 			}
8569 
8570 			unlock_dev(SYNC_STATE);
8571 		}
8572 		break;
8573 	default:
8574 		/* log an error here? */
8575 		error = DEVFSADM_RUN_NOTSUP;
8576 		break;
8577 	}
8578 
8579 done:
8580 	vprint(DEVNAME_MID, "devname_lookup_handler: error %d\n", error);
8581 	res.devfsadm_error = error;
8582 	(void) door_return((char *)&res, sizeof (struct sdev_door_res),
8583 	    NULL, 0);
8584 }
8585 
8586 
8587 di_devlink_handle_t
8588 devfsadm_devlink_cache(void)
8589 {
8590 	return (devlink_cache);
8591 }
8592 
8593 int
8594 devfsadm_reserve_id_cache(devlink_re_t re_array[], enumerate_file_t *head)
8595 {
8596 	enumerate_file_t *entry;
8597 	int nelem;
8598 	int i;
8599 	int subex;
8600 	char *re;
8601 	size_t size;
8602 	regmatch_t *pmch;
8603 
8604 	/*
8605 	 * Check the <RE, subexp> array passed in and compile it.
8606 	 */
8607 	for (i = 0; re_array[i].d_re; i++) {
8608 		if (re_array[i].d_subexp == 0) {
8609 			err_print("bad subexp value in RE: %s\n",
8610 			    re_array[i].d_re);
8611 			goto bad_re;
8612 		}
8613 
8614 		re = re_array[i].d_re;
8615 		if (regcomp(&re_array[i].d_rcomp, re, REG_EXTENDED) != 0) {
8616 			err_print("reg. exp. failed to compile: %s\n", re);
8617 			goto bad_re;
8618 		}
8619 		subex = re_array[i].d_subexp;
8620 		nelem = subex + 1;
8621 		re_array[i].d_pmatch = s_malloc(sizeof (regmatch_t) * nelem);
8622 	}
8623 
8624 	entry = head ? head : enumerate_reserved;
8625 	for (; entry; entry = entry->er_next) {
8626 		if (entry->er_id) {
8627 			vprint(RSBY_MID, "entry %s already has ID %s\n",
8628 			    entry->er_file, entry->er_id);
8629 			continue;
8630 		}
8631 		for (i = 0; re_array[i].d_re; i++) {
8632 			subex = re_array[i].d_subexp;
8633 			pmch = re_array[i].d_pmatch;
8634 			if (regexec(&re_array[i].d_rcomp, entry->er_file,
8635 			    subex + 1, pmch, 0) != 0) {
8636 				/* No match */
8637 				continue;
8638 			}
8639 			size = pmch[subex].rm_eo - pmch[subex].rm_so;
8640 			entry->er_id = s_malloc(size + 1);
8641 			(void) strncpy(entry->er_id,
8642 			    &entry->er_file[pmch[subex].rm_so], size);
8643 			entry->er_id[size] = '\0';
8644 			if (head) {
8645 				vprint(RSBY_MID, "devlink(%s) matches RE(%s). "
8646 				    "ID is %s\n", entry->er_file,
8647 				    re_array[i].d_re, entry->er_id);
8648 			} else {
8649 				vprint(RSBY_MID, "rsrv entry(%s) matches "
8650 				    "RE(%s) ID is %s\n", entry->er_file,
8651 				    re_array[i].d_re, entry->er_id);
8652 			}
8653 			break;
8654 		}
8655 	}
8656 
8657 	for (i = 0; re_array[i].d_re; i++) {
8658 		regfree(&re_array[i].d_rcomp);
8659 		assert(re_array[i].d_pmatch);
8660 		free(re_array[i].d_pmatch);
8661 	}
8662 
8663 	entry = head ? head : enumerate_reserved;
8664 	for (; entry; entry = entry->er_next) {
8665 		if (entry->er_id == NULL)
8666 			continue;
8667 		if (head) {
8668 			vprint(RSBY_MID, "devlink: %s\n", entry->er_file);
8669 			vprint(RSBY_MID, "ID: %s\n", entry->er_id);
8670 		} else {
8671 			vprint(RSBY_MID, "reserve file entry: %s\n",
8672 			    entry->er_file);
8673 			vprint(RSBY_MID, "reserve file id: %s\n",
8674 			    entry->er_id);
8675 		}
8676 	}
8677 
8678 	return (DEVFSADM_SUCCESS);
8679 
8680 bad_re:
8681 	for (i = i-1; i >= 0; i--) {
8682 		regfree(&re_array[i].d_rcomp);
8683 		assert(re_array[i].d_pmatch);
8684 		free(re_array[i].d_pmatch);
8685 	}
8686 	return (DEVFSADM_FAILURE);
8687 }
8688 
8689 /*
8690  * Return 1 if we have reserved links.
8691  */
8692 int
8693 devfsadm_have_reserved()
8694 {
8695 	return (enumerate_reserved ? 1 : 0);
8696 }
8697 
8698 /*
8699  * This functions errs on the side of caution. If there is any error
8700  * we assume that the devlink is  *not* reserved
8701  */
8702 int
8703 devfsadm_is_reserved(devlink_re_t re_array[], char *devlink)
8704 {
8705 	int match;
8706 	enumerate_file_t estruct = {NULL};
8707 	enumerate_file_t *entry;
8708 
8709 	match = 0;
8710 	estruct.er_file = devlink;
8711 	estruct.er_id = NULL;
8712 	estruct.er_next = NULL;
8713 
8714 	if (devfsadm_reserve_id_cache(re_array, &estruct) != DEVFSADM_SUCCESS) {
8715 		err_print("devfsadm_is_reserved: devlink (%s) does not "
8716 		    "match RE\n", devlink);
8717 		return (0);
8718 	}
8719 	if (estruct.er_id == NULL) {
8720 		err_print("devfsadm_is_reserved: ID derived from devlink %s "
8721 		    "is NULL\n", devlink);
8722 		return (0);
8723 	}
8724 
8725 	entry = enumerate_reserved;
8726 	for (; entry; entry = entry->er_next) {
8727 		if (entry->er_id == NULL)
8728 			continue;
8729 		if (strcmp(entry->er_id, estruct.er_id) != 0)
8730 			continue;
8731 		match = 1;
8732 		vprint(RSBY_MID, "reserve file entry (%s) and devlink (%s) "
8733 		    "match\n", entry->er_file, devlink);
8734 		break;
8735 	}
8736 
8737 	free(estruct.er_id);
8738 	return (match);
8739 }
8740