xref: /illumos-gate/usr/src/lib/libdevid/deviceid.c (revision 46d46cd4fa12218e54fa3d73a9be825ed504cabb)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*46d46cd4SYuri Pankov 
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
26*46d46cd4SYuri Pankov  * Copyright 2017 Nexenta Systems, Inc.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <errno.h>
327c478bd9Sstevel@tonic-gate #include <ftw.h>
337c478bd9Sstevel@tonic-gate #include <string.h>
347c478bd9Sstevel@tonic-gate #include <thread.h>
357c478bd9Sstevel@tonic-gate #include <synch.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <fcntl.h>
397c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
407c478bd9Sstevel@tonic-gate #include <strings.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
437c478bd9Sstevel@tonic-gate #include "libdevid.h"
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * Get Device Id from an open file descriptor
477c478bd9Sstevel@tonic-gate  */
487c478bd9Sstevel@tonic-gate int
devid_get(int fd,ddi_devid_t * devidp)497c478bd9Sstevel@tonic-gate devid_get(int fd, ddi_devid_t *devidp)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	int		len = 0;
527c478bd9Sstevel@tonic-gate 	dev_t		dev;
537c478bd9Sstevel@tonic-gate 	struct stat	statb;
547c478bd9Sstevel@tonic-gate 	ddi_devid_t	mydevid;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	if (fstat(fd, &statb) != 0)
577c478bd9Sstevel@tonic-gate 		return (-1);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	/* If not char or block device, then error */
607c478bd9Sstevel@tonic-gate 	if (!S_ISCHR(statb.st_mode) && !S_ISBLK(statb.st_mode))
617c478bd9Sstevel@tonic-gate 		return (-1);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	/* Get the device id size */
647c478bd9Sstevel@tonic-gate 	dev = statb.st_rdev;
657c478bd9Sstevel@tonic-gate 	if (modctl(MODSIZEOF_DEVID, dev, &len) != 0)
667c478bd9Sstevel@tonic-gate 		return (-1);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	/* Allocate space to return device id */
697c478bd9Sstevel@tonic-gate 	if ((mydevid = (ddi_devid_t)malloc(len)) == NULL)
707c478bd9Sstevel@tonic-gate 		return (-1);
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	/* Get the device id */
737c478bd9Sstevel@tonic-gate 	if (modctl(MODGETDEVID, dev, len, mydevid) != 0) {
747c478bd9Sstevel@tonic-gate 		free(mydevid);
757c478bd9Sstevel@tonic-gate 		return (-1);
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	/* Return the device id copy */
797c478bd9Sstevel@tonic-gate 	*devidp = mydevid;
807c478bd9Sstevel@tonic-gate 	return (0);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /*
847c478bd9Sstevel@tonic-gate  * Get the minor name
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate int
devid_get_minor_name(int fd,char ** minor_namep)877c478bd9Sstevel@tonic-gate devid_get_minor_name(int fd, char **minor_namep)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	int		len = 0;
907c478bd9Sstevel@tonic-gate 	dev_t		dev;
917c478bd9Sstevel@tonic-gate 	int		spectype;
927c478bd9Sstevel@tonic-gate 	char		*myminor_name;
937c478bd9Sstevel@tonic-gate 	struct stat	statb;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	if (fstat(fd, &statb) != 0)
967c478bd9Sstevel@tonic-gate 		return (-1);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	/* If not a char or block device, then return an error */
997c478bd9Sstevel@tonic-gate 	if (!S_ISCHR(statb.st_mode) && !S_ISBLK(statb.st_mode))
1007c478bd9Sstevel@tonic-gate 		return (-1);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	spectype = statb.st_mode & S_IFMT;
1037c478bd9Sstevel@tonic-gate 	dev = statb.st_rdev;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	/* Get the minor name size */
1067c478bd9Sstevel@tonic-gate 	if (modctl(MODSIZEOF_MINORNAME, dev, spectype, &len) != 0)
1077c478bd9Sstevel@tonic-gate 		return (-1);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	/* Allocate space for the minor name */
1107c478bd9Sstevel@tonic-gate 	if ((myminor_name = (char *)malloc(len)) == NULL)
1117c478bd9Sstevel@tonic-gate 		return (-1);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	/* Get the minor name */
1147c478bd9Sstevel@tonic-gate 	if (modctl(MODGETMINORNAME, dev, spectype, len, myminor_name) != 0) {
1157c478bd9Sstevel@tonic-gate 		free(myminor_name);
1167c478bd9Sstevel@tonic-gate 		return (-1);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	/* return the minor name copy */
1207c478bd9Sstevel@tonic-gate 	*minor_namep = myminor_name;
1217c478bd9Sstevel@tonic-gate 	return (0);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
124*46d46cd4SYuri Pankov char *
devid_str_from_path(const char * path)125*46d46cd4SYuri Pankov devid_str_from_path(const char *path)
126*46d46cd4SYuri Pankov {
127*46d46cd4SYuri Pankov 	int		fd;
128*46d46cd4SYuri Pankov 	ddi_devid_t	devid;
129*46d46cd4SYuri Pankov 	char		*minor, *ret = NULL;
130*46d46cd4SYuri Pankov 
131*46d46cd4SYuri Pankov 	if ((fd = open(path, O_RDONLY)) < 0)
132*46d46cd4SYuri Pankov 		return (NULL);
133*46d46cd4SYuri Pankov 
134*46d46cd4SYuri Pankov 	if (devid_get(fd, &devid) == 0) {
135*46d46cd4SYuri Pankov 		if (devid_get_minor_name(fd, &minor) != 0)
136*46d46cd4SYuri Pankov 			minor = NULL;
137*46d46cd4SYuri Pankov 		ret = devid_str_encode(devid, minor);
138*46d46cd4SYuri Pankov 		if (minor != NULL)
139*46d46cd4SYuri Pankov 			devid_str_free(minor);
140*46d46cd4SYuri Pankov 		devid_free(devid);
141*46d46cd4SYuri Pankov 	}
142*46d46cd4SYuri Pankov 	(void) close(fd);
143*46d46cd4SYuri Pankov 
144*46d46cd4SYuri Pankov 	return (ret);
145*46d46cd4SYuri Pankov }
146*46d46cd4SYuri Pankov 
1477c478bd9Sstevel@tonic-gate /* list element of devid_nmlist_t information */
1487c478bd9Sstevel@tonic-gate struct nmlist {
1497c478bd9Sstevel@tonic-gate 	struct nmlist	*nl_next;
1507c478bd9Sstevel@tonic-gate 	char		*nl_devname;
1517c478bd9Sstevel@tonic-gate 	dev_t		nl_dev;
1527c478bd9Sstevel@tonic-gate };
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /* add list element to end of nmlist headed by *nlhp */
1557c478bd9Sstevel@tonic-gate struct nmlist *
nmlist_add(struct nmlist ** nlhp,char * path)1567c478bd9Sstevel@tonic-gate nmlist_add(struct nmlist **nlhp, char *path)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	struct stat	statb;
1597c478bd9Sstevel@tonic-gate 	dev_t		dev;
1607c478bd9Sstevel@tonic-gate 	struct nmlist	*nl;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	/* stat and get the devt for char or block */
1637c478bd9Sstevel@tonic-gate 	if ((stat(path, &statb) == 0) &&
1647c478bd9Sstevel@tonic-gate 	    (S_ISCHR(statb.st_mode) || S_ISBLK(statb.st_mode)))
1657c478bd9Sstevel@tonic-gate 		dev = statb.st_rdev;
1667c478bd9Sstevel@tonic-gate 	else
1677c478bd9Sstevel@tonic-gate 		dev = NODEV;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/* find the end of the list */
1707c478bd9Sstevel@tonic-gate 	for (; (nl = *nlhp) != NULL; nlhp = &nl->nl_next)
1717c478bd9Sstevel@tonic-gate 		;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/* allocate and initialize new entry */
1747c478bd9Sstevel@tonic-gate 	if ((nl = malloc(sizeof (*nl))) == NULL)
1757c478bd9Sstevel@tonic-gate 		return (NULL);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if ((nl->nl_devname = strdup(path)) == NULL) {
1787c478bd9Sstevel@tonic-gate 		free(nl);
1797c478bd9Sstevel@tonic-gate 		return (NULL);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 	nl->nl_next = NULL;
1827c478bd9Sstevel@tonic-gate 	nl->nl_dev = dev;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/* link new entry at end */
1857c478bd9Sstevel@tonic-gate 	*nlhp = nl;
1867c478bd9Sstevel@tonic-gate 	return (nl);
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate /* information needed by devlink_callback to call nmlist_add */
1907c478bd9Sstevel@tonic-gate struct devlink_cbinfo {
1917c478bd9Sstevel@tonic-gate 	struct nmlist	**cbi_nlhp;
1927c478bd9Sstevel@tonic-gate 	char		*cbi_search_path;
1937c478bd9Sstevel@tonic-gate 	int		cbi_error;
1947c478bd9Sstevel@tonic-gate };
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /* di_devlink callback to add a /dev entry to nmlist */
1977c478bd9Sstevel@tonic-gate static int
devlink_callback(di_devlink_t dl,void * arg)1987c478bd9Sstevel@tonic-gate devlink_callback(di_devlink_t dl, void *arg)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	struct devlink_cbinfo	*cbip = (struct devlink_cbinfo *)arg;
2017c478bd9Sstevel@tonic-gate 	char			*devpath = (char *)di_devlink_path(dl);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	if (strncmp(devpath, cbip->cbi_search_path,
2047c478bd9Sstevel@tonic-gate 	    strlen(cbip->cbi_search_path)) == 0) {
2057c478bd9Sstevel@tonic-gate 		if (nmlist_add(cbip->cbi_nlhp, devpath) == NULL) {
2067c478bd9Sstevel@tonic-gate 			cbip->cbi_error = 1;
2077c478bd9Sstevel@tonic-gate 			return (DI_WALK_TERMINATE);
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 	return (DI_WALK_CONTINUE);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate  * Resolve /dev names to DI_PRIMARY_LINK, DI_SECONDARY_LINK, or both.
2157c478bd9Sstevel@tonic-gate  * The default is to resolve to just the DI_PRIMARY_LINK.
2167c478bd9Sstevel@tonic-gate  */
2177c478bd9Sstevel@tonic-gate int			devid_deviceid_to_nmlist_link = DI_PRIMARY_LINK;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate  * Options for the devid_deviceid_to_nmlist implementation:
2217c478bd9Sstevel@tonic-gate  *
2227c478bd9Sstevel@tonic-gate  *   DEVICEID_NMLIST_SLINK -	reduce overhead by reuse the previous
2237c478bd9Sstevel@tonic-gate  *				di_devlink_init.
2247c478bd9Sstevel@tonic-gate  */
2257c478bd9Sstevel@tonic-gate #define	DEVICEID_NMLIST_SLINK	1
2267c478bd9Sstevel@tonic-gate int			devid_deviceid_to_nmlist_flg = 0;
2277c478bd9Sstevel@tonic-gate static di_devlink_handle_t devid_deviceid_to_nmlist_dlh = NULL;	/* SLINK */
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate #define	DEVICEID_NMLIST_NRETRY	10
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * Convert the specified devid/minor_name into a devid_nmlist_t array
2337c478bd9Sstevel@tonic-gate  * with names that resolve into /devices or /dev depending on search_path.
2347c478bd9Sstevel@tonic-gate  *
2357c478bd9Sstevel@tonic-gate  * The man page indicates that:
2367c478bd9Sstevel@tonic-gate  *
2377c478bd9Sstevel@tonic-gate  *     This function traverses the file tree, starting at search_path.
2387c478bd9Sstevel@tonic-gate  *
2397c478bd9Sstevel@tonic-gate  * This is not true, we reverse engineer the paths relative to
2407c478bd9Sstevel@tonic-gate  * the specified search path to avoid attaching all devices.
2417c478bd9Sstevel@tonic-gate  */
2427c478bd9Sstevel@tonic-gate int
devid_deviceid_to_nmlist(char * search_path,ddi_devid_t devid,char * minor_name,devid_nmlist_t ** retlist)243*46d46cd4SYuri Pankov devid_deviceid_to_nmlist(char *search_path, ddi_devid_t devid, char *minor_name,
2447c478bd9Sstevel@tonic-gate     devid_nmlist_t **retlist)
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	char			*cp;
2477c478bd9Sstevel@tonic-gate 	int			dev;
2487c478bd9Sstevel@tonic-gate 	char			*paths = NULL;
2497c478bd9Sstevel@tonic-gate 	char			*path;
2507c478bd9Sstevel@tonic-gate 	int			lens;
2517c478bd9Sstevel@tonic-gate 	di_devlink_handle_t	dlh = NULL;
2527c478bd9Sstevel@tonic-gate 	int			ret = -1;
2537c478bd9Sstevel@tonic-gate 	struct devlink_cbinfo	cbi;
2547c478bd9Sstevel@tonic-gate 	struct nmlist		*nlh = NULL;
2557c478bd9Sstevel@tonic-gate 	struct nmlist		*nl;
2567c478bd9Sstevel@tonic-gate 	devid_nmlist_t		*rl;
2577c478bd9Sstevel@tonic-gate 	int			nret;
2587c478bd9Sstevel@tonic-gate 	int			nagain = 0;
2597c478bd9Sstevel@tonic-gate 	int			err = 0;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	*retlist = NULL;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/* verify valid search path starts with "/devices" or "/dev" */
2647c478bd9Sstevel@tonic-gate 	if ((strcmp(search_path, "/devices") == 0) ||
2657c478bd9Sstevel@tonic-gate 	    (strncmp(search_path, "/devices/", 9) == 0))
2667c478bd9Sstevel@tonic-gate 		dev = 0;
2677c478bd9Sstevel@tonic-gate 	else if ((strcmp(search_path, "/dev") == 0) ||
2687c478bd9Sstevel@tonic-gate 	    (strncmp(search_path, "/dev/", 5) == 0))
2697c478bd9Sstevel@tonic-gate 		dev = 1;
2707c478bd9Sstevel@tonic-gate 	else {
2717c478bd9Sstevel@tonic-gate 		errno = EINVAL;
2727c478bd9Sstevel@tonic-gate 		return (-1);
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	/* translate devid/minor_name to /devices paths */
2777c478bd9Sstevel@tonic-gate again:	if (modctl(MODDEVID2PATHS, devid, minor_name, 0, &lens, NULL) != 0)
2787c478bd9Sstevel@tonic-gate 		goto out;
2797c478bd9Sstevel@tonic-gate 	if ((paths = (char *)malloc(lens)) == NULL)
2807c478bd9Sstevel@tonic-gate 		goto out;
2817c478bd9Sstevel@tonic-gate 	if (modctl(MODDEVID2PATHS, devid, minor_name, 0, &lens, paths) != 0) {
2827c478bd9Sstevel@tonic-gate 		if ((errno == EAGAIN) && (nagain++ < DEVICEID_NMLIST_NRETRY)) {
2837c478bd9Sstevel@tonic-gate 			free(paths);
2847c478bd9Sstevel@tonic-gate 			paths = NULL;
2857c478bd9Sstevel@tonic-gate 			goto again;
2867c478bd9Sstevel@tonic-gate 		}
2877c478bd9Sstevel@tonic-gate 		goto out;
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/*
2917c478bd9Sstevel@tonic-gate 	 * initialize for /devices path to /dev path translation. To reduce
2927c478bd9Sstevel@tonic-gate 	 * overhead we reuse the last snapshot if DEVICEID_NMLIST_SLINK is set.
2937c478bd9Sstevel@tonic-gate 	 */
2947c478bd9Sstevel@tonic-gate 	if (dev) {
2957c478bd9Sstevel@tonic-gate 		dlh = devid_deviceid_to_nmlist_dlh;
2967c478bd9Sstevel@tonic-gate 		if (dlh &&
2977c478bd9Sstevel@tonic-gate 		    !(devid_deviceid_to_nmlist_flg & DEVICEID_NMLIST_SLINK)) {
2987c478bd9Sstevel@tonic-gate 			(void) di_devlink_fini(&dlh);
2997c478bd9Sstevel@tonic-gate 			dlh = devid_deviceid_to_nmlist_dlh = NULL;
3007c478bd9Sstevel@tonic-gate 		}
3017c478bd9Sstevel@tonic-gate 		if ((dlh == NULL) &&
3027c478bd9Sstevel@tonic-gate 		    ((dlh = di_devlink_init(NULL, 0)) == NULL))
3037c478bd9Sstevel@tonic-gate 				goto out;
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * iterate over all the devtspectype resolutions of the devid and
3087c478bd9Sstevel@tonic-gate 	 * convert them into the appropriate path form and add items to return
3097c478bd9Sstevel@tonic-gate 	 * to the nmlist list;
3107c478bd9Sstevel@tonic-gate 	 */
3117c478bd9Sstevel@tonic-gate 	for (path = paths; *path; path += strlen(path) + 1) {
3127c478bd9Sstevel@tonic-gate 		if (dev) {
3137c478bd9Sstevel@tonic-gate 			/* add /dev entries */
3147c478bd9Sstevel@tonic-gate 			cbi.cbi_nlhp = &nlh;
3157c478bd9Sstevel@tonic-gate 			cbi.cbi_search_path = search_path;
3167c478bd9Sstevel@tonic-gate 			cbi.cbi_error = 0;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 			(void) di_devlink_walk(dlh, NULL, path,
3197c478bd9Sstevel@tonic-gate 			    devid_deviceid_to_nmlist_link,
3207c478bd9Sstevel@tonic-gate 			    (void *)&cbi, devlink_callback);
3217c478bd9Sstevel@tonic-gate 			if (cbi.cbi_error)
3227c478bd9Sstevel@tonic-gate 				goto out;
3237c478bd9Sstevel@tonic-gate 		} else {
3247c478bd9Sstevel@tonic-gate 			/* add /devices entry */
3257c478bd9Sstevel@tonic-gate 			cp = malloc(strlen("/devices") + strlen(path) + 1);
3267c478bd9Sstevel@tonic-gate 			(void) strcpy(cp, "/devices");
3277c478bd9Sstevel@tonic-gate 			(void) strcat(cp, path);
3287c478bd9Sstevel@tonic-gate 			if (strncmp(cp, search_path,
3297c478bd9Sstevel@tonic-gate 			    strlen(search_path)) == 0) {
3307c478bd9Sstevel@tonic-gate 				if (nmlist_add(&nlh, cp) == NULL) {
3317c478bd9Sstevel@tonic-gate 					free(cp);
3327c478bd9Sstevel@tonic-gate 					goto out;
3337c478bd9Sstevel@tonic-gate 				}
3347c478bd9Sstevel@tonic-gate 			}
3357c478bd9Sstevel@tonic-gate 			free(cp);
3367c478bd9Sstevel@tonic-gate 		}
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	/* convert from nmlist to retlist array */
3407c478bd9Sstevel@tonic-gate 	for (nl = nlh, nret = 0; nl; nl = nl->nl_next)
3417c478bd9Sstevel@tonic-gate 		nret++;
3427c478bd9Sstevel@tonic-gate 	if (nret == 0) {
3437c478bd9Sstevel@tonic-gate 		err = ENODEV;
3447c478bd9Sstevel@tonic-gate 		goto out;
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 	if ((*retlist = calloc(nret + 1, sizeof (devid_nmlist_t))) == NULL) {
3477c478bd9Sstevel@tonic-gate 		err = ENOMEM;
3487c478bd9Sstevel@tonic-gate 		goto out;
3497c478bd9Sstevel@tonic-gate 	}
3507c478bd9Sstevel@tonic-gate 	for (nl = nlh, rl = *retlist; nl; nl = nl->nl_next, rl++) {
3517c478bd9Sstevel@tonic-gate 		rl->devname = nl->nl_devname;
3527c478bd9Sstevel@tonic-gate 		rl->dev = nl->nl_dev;
3537c478bd9Sstevel@tonic-gate 	}
3547c478bd9Sstevel@tonic-gate 	rl->devname = NULL;
3557c478bd9Sstevel@tonic-gate 	rl->dev = NODEV;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	ret = 0;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate out:
3607c478bd9Sstevel@tonic-gate 	while ((nl = nlh) != NULL) {	/* free the nmlist */
3617c478bd9Sstevel@tonic-gate 		nlh = nl->nl_next;
3627c478bd9Sstevel@tonic-gate 		free(nl);
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 	if (paths)
3657c478bd9Sstevel@tonic-gate 		free(paths);
3667c478bd9Sstevel@tonic-gate 	if (dlh) {
3677c478bd9Sstevel@tonic-gate 		if ((ret == 0) &&
3687c478bd9Sstevel@tonic-gate 		    (devid_deviceid_to_nmlist_flg & DEVICEID_NMLIST_SLINK))
3697c478bd9Sstevel@tonic-gate 			devid_deviceid_to_nmlist_dlh = dlh;
3707c478bd9Sstevel@tonic-gate 		else
3717c478bd9Sstevel@tonic-gate 			(void) di_devlink_fini(&dlh);
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 	if (ret && *retlist)
3747c478bd9Sstevel@tonic-gate 		free(*retlist);
3757c478bd9Sstevel@tonic-gate 	if (ret && err != 0)
3767c478bd9Sstevel@tonic-gate 		errno = err;
3777c478bd9Sstevel@tonic-gate 	return (ret);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate /*
3817c478bd9Sstevel@tonic-gate  * Free Device Id Name List
3827c478bd9Sstevel@tonic-gate  */
3837c478bd9Sstevel@tonic-gate void
devid_free_nmlist(devid_nmlist_t * list)3847c478bd9Sstevel@tonic-gate devid_free_nmlist(devid_nmlist_t *list)
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate 	devid_nmlist_t *p = list;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	if (list == NULL)
3897c478bd9Sstevel@tonic-gate 		return;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	/* Free all the device names */
3927c478bd9Sstevel@tonic-gate 	while (p->devname != NULL) {
3937c478bd9Sstevel@tonic-gate 		free(p->devname);
3947c478bd9Sstevel@tonic-gate 		p++;
3957c478bd9Sstevel@tonic-gate 	}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	/* Free the array */
3987c478bd9Sstevel@tonic-gate 	free(list);
3997c478bd9Sstevel@tonic-gate }
400