1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate #include <unistd.h>
29*0Sstevel@tonic-gate #include <stdlib.h>
30*0Sstevel@tonic-gate #include <errno.h>
31*0Sstevel@tonic-gate #include <ftw.h>
32*0Sstevel@tonic-gate #include <string.h>
33*0Sstevel@tonic-gate #include <thread.h>
34*0Sstevel@tonic-gate #include <synch.h>
35*0Sstevel@tonic-gate #include <sys/types.h>
36*0Sstevel@tonic-gate #include <sys/stat.h>
37*0Sstevel@tonic-gate #include <fcntl.h>
38*0Sstevel@tonic-gate #include <sys/modctl.h>
39*0Sstevel@tonic-gate #include <strings.h>
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate #include <libdevinfo.h>
42*0Sstevel@tonic-gate #include "libdevid.h"
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate /*
45*0Sstevel@tonic-gate * Get Device Id from an open file descriptor
46*0Sstevel@tonic-gate */
47*0Sstevel@tonic-gate int
devid_get(int fd,ddi_devid_t * devidp)48*0Sstevel@tonic-gate devid_get(int fd, ddi_devid_t *devidp)
49*0Sstevel@tonic-gate {
50*0Sstevel@tonic-gate int len = 0;
51*0Sstevel@tonic-gate dev_t dev;
52*0Sstevel@tonic-gate struct stat statb;
53*0Sstevel@tonic-gate ddi_devid_t mydevid;
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate if (fstat(fd, &statb) != 0)
56*0Sstevel@tonic-gate return (-1);
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate /* If not char or block device, then error */
59*0Sstevel@tonic-gate if (!S_ISCHR(statb.st_mode) && !S_ISBLK(statb.st_mode))
60*0Sstevel@tonic-gate return (-1);
61*0Sstevel@tonic-gate
62*0Sstevel@tonic-gate /* Get the device id size */
63*0Sstevel@tonic-gate dev = statb.st_rdev;
64*0Sstevel@tonic-gate if (modctl(MODSIZEOF_DEVID, dev, &len) != 0)
65*0Sstevel@tonic-gate return (-1);
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate /* Allocate space to return device id */
68*0Sstevel@tonic-gate if ((mydevid = (ddi_devid_t)malloc(len)) == NULL)
69*0Sstevel@tonic-gate return (-1);
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate /* Get the device id */
72*0Sstevel@tonic-gate if (modctl(MODGETDEVID, dev, len, mydevid) != 0) {
73*0Sstevel@tonic-gate free(mydevid);
74*0Sstevel@tonic-gate return (-1);
75*0Sstevel@tonic-gate }
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate /* Return the device id copy */
78*0Sstevel@tonic-gate *devidp = mydevid;
79*0Sstevel@tonic-gate return (0);
80*0Sstevel@tonic-gate }
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate /*
83*0Sstevel@tonic-gate * Get the minor name
84*0Sstevel@tonic-gate */
85*0Sstevel@tonic-gate int
devid_get_minor_name(int fd,char ** minor_namep)86*0Sstevel@tonic-gate devid_get_minor_name(int fd, char **minor_namep)
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate int len = 0;
89*0Sstevel@tonic-gate dev_t dev;
90*0Sstevel@tonic-gate int spectype;
91*0Sstevel@tonic-gate char *myminor_name;
92*0Sstevel@tonic-gate struct stat statb;
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gate if (fstat(fd, &statb) != 0)
95*0Sstevel@tonic-gate return (-1);
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate /* If not a char or block device, then return an error */
98*0Sstevel@tonic-gate if (!S_ISCHR(statb.st_mode) && !S_ISBLK(statb.st_mode))
99*0Sstevel@tonic-gate return (-1);
100*0Sstevel@tonic-gate
101*0Sstevel@tonic-gate spectype = statb.st_mode & S_IFMT;
102*0Sstevel@tonic-gate dev = statb.st_rdev;
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate /* Get the minor name size */
105*0Sstevel@tonic-gate if (modctl(MODSIZEOF_MINORNAME, dev, spectype, &len) != 0)
106*0Sstevel@tonic-gate return (-1);
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate /* Allocate space for the minor name */
109*0Sstevel@tonic-gate if ((myminor_name = (char *)malloc(len)) == NULL)
110*0Sstevel@tonic-gate return (-1);
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gate /* Get the minor name */
113*0Sstevel@tonic-gate if (modctl(MODGETMINORNAME, dev, spectype, len, myminor_name) != 0) {
114*0Sstevel@tonic-gate free(myminor_name);
115*0Sstevel@tonic-gate return (-1);
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate /* return the minor name copy */
119*0Sstevel@tonic-gate *minor_namep = myminor_name;
120*0Sstevel@tonic-gate return (0);
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate /* list element of devid_nmlist_t information */
124*0Sstevel@tonic-gate struct nmlist {
125*0Sstevel@tonic-gate struct nmlist *nl_next;
126*0Sstevel@tonic-gate char *nl_devname;
127*0Sstevel@tonic-gate dev_t nl_dev;
128*0Sstevel@tonic-gate };
129*0Sstevel@tonic-gate
130*0Sstevel@tonic-gate /* add list element to end of nmlist headed by *nlhp */
131*0Sstevel@tonic-gate struct nmlist *
nmlist_add(struct nmlist ** nlhp,char * path)132*0Sstevel@tonic-gate nmlist_add(struct nmlist **nlhp, char *path)
133*0Sstevel@tonic-gate {
134*0Sstevel@tonic-gate struct stat statb;
135*0Sstevel@tonic-gate dev_t dev;
136*0Sstevel@tonic-gate struct nmlist *nl;
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate /* stat and get the devt for char or block */
139*0Sstevel@tonic-gate if ((stat(path, &statb) == 0) &&
140*0Sstevel@tonic-gate (S_ISCHR(statb.st_mode) || S_ISBLK(statb.st_mode)))
141*0Sstevel@tonic-gate dev = statb.st_rdev;
142*0Sstevel@tonic-gate else
143*0Sstevel@tonic-gate dev = NODEV;
144*0Sstevel@tonic-gate
145*0Sstevel@tonic-gate /* find the end of the list */
146*0Sstevel@tonic-gate for (; (nl = *nlhp) != NULL; nlhp = &nl->nl_next)
147*0Sstevel@tonic-gate ;
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate /* allocate and initialize new entry */
150*0Sstevel@tonic-gate if ((nl = malloc(sizeof (*nl))) == NULL)
151*0Sstevel@tonic-gate return (NULL);
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate if ((nl->nl_devname = strdup(path)) == NULL) {
154*0Sstevel@tonic-gate free(nl);
155*0Sstevel@tonic-gate return (NULL);
156*0Sstevel@tonic-gate }
157*0Sstevel@tonic-gate nl->nl_next = NULL;
158*0Sstevel@tonic-gate nl->nl_dev = dev;
159*0Sstevel@tonic-gate
160*0Sstevel@tonic-gate /* link new entry at end */
161*0Sstevel@tonic-gate *nlhp = nl;
162*0Sstevel@tonic-gate return (nl);
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate
165*0Sstevel@tonic-gate /* information needed by devlink_callback to call nmlist_add */
166*0Sstevel@tonic-gate struct devlink_cbinfo {
167*0Sstevel@tonic-gate struct nmlist **cbi_nlhp;
168*0Sstevel@tonic-gate char *cbi_search_path;
169*0Sstevel@tonic-gate int cbi_error;
170*0Sstevel@tonic-gate };
171*0Sstevel@tonic-gate
172*0Sstevel@tonic-gate /* di_devlink callback to add a /dev entry to nmlist */
173*0Sstevel@tonic-gate static int
devlink_callback(di_devlink_t dl,void * arg)174*0Sstevel@tonic-gate devlink_callback(di_devlink_t dl, void *arg)
175*0Sstevel@tonic-gate {
176*0Sstevel@tonic-gate struct devlink_cbinfo *cbip = (struct devlink_cbinfo *)arg;
177*0Sstevel@tonic-gate char *devpath = (char *)di_devlink_path(dl);
178*0Sstevel@tonic-gate
179*0Sstevel@tonic-gate if (strncmp(devpath, cbip->cbi_search_path,
180*0Sstevel@tonic-gate strlen(cbip->cbi_search_path)) == 0) {
181*0Sstevel@tonic-gate if (nmlist_add(cbip->cbi_nlhp, devpath) == NULL) {
182*0Sstevel@tonic-gate cbip->cbi_error = 1;
183*0Sstevel@tonic-gate return (DI_WALK_TERMINATE);
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate return (DI_WALK_CONTINUE);
187*0Sstevel@tonic-gate }
188*0Sstevel@tonic-gate
189*0Sstevel@tonic-gate /*
190*0Sstevel@tonic-gate * Resolve /dev names to DI_PRIMARY_LINK, DI_SECONDARY_LINK, or both.
191*0Sstevel@tonic-gate * The default is to resolve to just the DI_PRIMARY_LINK.
192*0Sstevel@tonic-gate */
193*0Sstevel@tonic-gate int devid_deviceid_to_nmlist_link = DI_PRIMARY_LINK;
194*0Sstevel@tonic-gate
195*0Sstevel@tonic-gate /*
196*0Sstevel@tonic-gate * Options for the devid_deviceid_to_nmlist implementation:
197*0Sstevel@tonic-gate *
198*0Sstevel@tonic-gate * DEVICEID_NMLIST_SLINK - reduce overhead by reuse the previous
199*0Sstevel@tonic-gate * di_devlink_init.
200*0Sstevel@tonic-gate */
201*0Sstevel@tonic-gate #define DEVICEID_NMLIST_SLINK 1
202*0Sstevel@tonic-gate int devid_deviceid_to_nmlist_flg = 0;
203*0Sstevel@tonic-gate static di_devlink_handle_t devid_deviceid_to_nmlist_dlh = NULL; /* SLINK */
204*0Sstevel@tonic-gate
205*0Sstevel@tonic-gate #define DEVICEID_NMLIST_NRETRY 10
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gate /*
208*0Sstevel@tonic-gate * Convert the specified devid/minor_name into a devid_nmlist_t array
209*0Sstevel@tonic-gate * with names that resolve into /devices or /dev depending on search_path.
210*0Sstevel@tonic-gate *
211*0Sstevel@tonic-gate * The man page indicates that:
212*0Sstevel@tonic-gate *
213*0Sstevel@tonic-gate * This function traverses the file tree, starting at search_path.
214*0Sstevel@tonic-gate *
215*0Sstevel@tonic-gate * This is not true, we reverse engineer the paths relative to
216*0Sstevel@tonic-gate * the specified search path to avoid attaching all devices.
217*0Sstevel@tonic-gate */
218*0Sstevel@tonic-gate int
devid_deviceid_to_nmlist(char * search_path,ddi_devid_t devid,char * minor_name,devid_nmlist_t ** retlist)219*0Sstevel@tonic-gate devid_deviceid_to_nmlist(
220*0Sstevel@tonic-gate char *search_path,
221*0Sstevel@tonic-gate ddi_devid_t devid,
222*0Sstevel@tonic-gate char *minor_name,
223*0Sstevel@tonic-gate devid_nmlist_t **retlist)
224*0Sstevel@tonic-gate {
225*0Sstevel@tonic-gate char *cp;
226*0Sstevel@tonic-gate int dev;
227*0Sstevel@tonic-gate char *paths = NULL;
228*0Sstevel@tonic-gate char *path;
229*0Sstevel@tonic-gate int lens;
230*0Sstevel@tonic-gate di_devlink_handle_t dlh = NULL;
231*0Sstevel@tonic-gate int ret = -1;
232*0Sstevel@tonic-gate struct devlink_cbinfo cbi;
233*0Sstevel@tonic-gate struct nmlist *nlh = NULL;
234*0Sstevel@tonic-gate struct nmlist *nl;
235*0Sstevel@tonic-gate devid_nmlist_t *rl;
236*0Sstevel@tonic-gate int nret;
237*0Sstevel@tonic-gate int nagain = 0;
238*0Sstevel@tonic-gate int err = 0;
239*0Sstevel@tonic-gate
240*0Sstevel@tonic-gate *retlist = NULL;
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate /* verify valid search path starts with "/devices" or "/dev" */
243*0Sstevel@tonic-gate if ((strcmp(search_path, "/devices") == 0) ||
244*0Sstevel@tonic-gate (strncmp(search_path, "/devices/", 9) == 0))
245*0Sstevel@tonic-gate dev = 0;
246*0Sstevel@tonic-gate else if ((strcmp(search_path, "/dev") == 0) ||
247*0Sstevel@tonic-gate (strncmp(search_path, "/dev/", 5) == 0))
248*0Sstevel@tonic-gate dev = 1;
249*0Sstevel@tonic-gate else {
250*0Sstevel@tonic-gate errno = EINVAL;
251*0Sstevel@tonic-gate return (-1);
252*0Sstevel@tonic-gate }
253*0Sstevel@tonic-gate
254*0Sstevel@tonic-gate
255*0Sstevel@tonic-gate /* translate devid/minor_name to /devices paths */
256*0Sstevel@tonic-gate again: if (modctl(MODDEVID2PATHS, devid, minor_name, 0, &lens, NULL) != 0)
257*0Sstevel@tonic-gate goto out;
258*0Sstevel@tonic-gate if ((paths = (char *)malloc(lens)) == NULL)
259*0Sstevel@tonic-gate goto out;
260*0Sstevel@tonic-gate if (modctl(MODDEVID2PATHS, devid, minor_name, 0, &lens, paths) != 0) {
261*0Sstevel@tonic-gate if ((errno == EAGAIN) && (nagain++ < DEVICEID_NMLIST_NRETRY)) {
262*0Sstevel@tonic-gate free(paths);
263*0Sstevel@tonic-gate paths = NULL;
264*0Sstevel@tonic-gate goto again;
265*0Sstevel@tonic-gate }
266*0Sstevel@tonic-gate goto out;
267*0Sstevel@tonic-gate }
268*0Sstevel@tonic-gate
269*0Sstevel@tonic-gate /*
270*0Sstevel@tonic-gate * initialize for /devices path to /dev path translation. To reduce
271*0Sstevel@tonic-gate * overhead we reuse the last snapshot if DEVICEID_NMLIST_SLINK is set.
272*0Sstevel@tonic-gate */
273*0Sstevel@tonic-gate if (dev) {
274*0Sstevel@tonic-gate dlh = devid_deviceid_to_nmlist_dlh;
275*0Sstevel@tonic-gate if (dlh &&
276*0Sstevel@tonic-gate !(devid_deviceid_to_nmlist_flg & DEVICEID_NMLIST_SLINK)) {
277*0Sstevel@tonic-gate (void) di_devlink_fini(&dlh);
278*0Sstevel@tonic-gate dlh = devid_deviceid_to_nmlist_dlh = NULL;
279*0Sstevel@tonic-gate }
280*0Sstevel@tonic-gate if ((dlh == NULL) &&
281*0Sstevel@tonic-gate ((dlh = di_devlink_init(NULL, 0)) == NULL))
282*0Sstevel@tonic-gate goto out;
283*0Sstevel@tonic-gate }
284*0Sstevel@tonic-gate
285*0Sstevel@tonic-gate /*
286*0Sstevel@tonic-gate * iterate over all the devtspectype resolutions of the devid and
287*0Sstevel@tonic-gate * convert them into the appropriate path form and add items to return
288*0Sstevel@tonic-gate * to the nmlist list;
289*0Sstevel@tonic-gate */
290*0Sstevel@tonic-gate for (path = paths; *path; path += strlen(path) + 1) {
291*0Sstevel@tonic-gate if (dev) {
292*0Sstevel@tonic-gate /* add /dev entries */
293*0Sstevel@tonic-gate cbi.cbi_nlhp = &nlh;
294*0Sstevel@tonic-gate cbi.cbi_search_path = search_path;
295*0Sstevel@tonic-gate cbi.cbi_error = 0;
296*0Sstevel@tonic-gate
297*0Sstevel@tonic-gate (void) di_devlink_walk(dlh, NULL, path,
298*0Sstevel@tonic-gate devid_deviceid_to_nmlist_link,
299*0Sstevel@tonic-gate (void *)&cbi, devlink_callback);
300*0Sstevel@tonic-gate if (cbi.cbi_error)
301*0Sstevel@tonic-gate goto out;
302*0Sstevel@tonic-gate } else {
303*0Sstevel@tonic-gate /* add /devices entry */
304*0Sstevel@tonic-gate cp = malloc(strlen("/devices") + strlen(path) + 1);
305*0Sstevel@tonic-gate (void) strcpy(cp, "/devices");
306*0Sstevel@tonic-gate (void) strcat(cp, path);
307*0Sstevel@tonic-gate if (strncmp(cp, search_path,
308*0Sstevel@tonic-gate strlen(search_path)) == 0) {
309*0Sstevel@tonic-gate if (nmlist_add(&nlh, cp) == NULL) {
310*0Sstevel@tonic-gate free(cp);
311*0Sstevel@tonic-gate goto out;
312*0Sstevel@tonic-gate }
313*0Sstevel@tonic-gate }
314*0Sstevel@tonic-gate free(cp);
315*0Sstevel@tonic-gate }
316*0Sstevel@tonic-gate }
317*0Sstevel@tonic-gate
318*0Sstevel@tonic-gate /* convert from nmlist to retlist array */
319*0Sstevel@tonic-gate for (nl = nlh, nret = 0; nl; nl = nl->nl_next)
320*0Sstevel@tonic-gate nret++;
321*0Sstevel@tonic-gate if (nret == 0) {
322*0Sstevel@tonic-gate err = ENODEV;
323*0Sstevel@tonic-gate goto out;
324*0Sstevel@tonic-gate }
325*0Sstevel@tonic-gate if ((*retlist = calloc(nret + 1, sizeof (devid_nmlist_t))) == NULL) {
326*0Sstevel@tonic-gate err = ENOMEM;
327*0Sstevel@tonic-gate goto out;
328*0Sstevel@tonic-gate }
329*0Sstevel@tonic-gate for (nl = nlh, rl = *retlist; nl; nl = nl->nl_next, rl++) {
330*0Sstevel@tonic-gate rl->devname = nl->nl_devname;
331*0Sstevel@tonic-gate rl->dev = nl->nl_dev;
332*0Sstevel@tonic-gate }
333*0Sstevel@tonic-gate rl->devname = NULL;
334*0Sstevel@tonic-gate rl->dev = NODEV;
335*0Sstevel@tonic-gate
336*0Sstevel@tonic-gate ret = 0;
337*0Sstevel@tonic-gate
338*0Sstevel@tonic-gate out:
339*0Sstevel@tonic-gate while ((nl = nlh) != NULL) { /* free the nmlist */
340*0Sstevel@tonic-gate nlh = nl->nl_next;
341*0Sstevel@tonic-gate free(nl);
342*0Sstevel@tonic-gate }
343*0Sstevel@tonic-gate if (paths)
344*0Sstevel@tonic-gate free(paths);
345*0Sstevel@tonic-gate if (dlh) {
346*0Sstevel@tonic-gate if ((ret == 0) &&
347*0Sstevel@tonic-gate (devid_deviceid_to_nmlist_flg & DEVICEID_NMLIST_SLINK))
348*0Sstevel@tonic-gate devid_deviceid_to_nmlist_dlh = dlh;
349*0Sstevel@tonic-gate else
350*0Sstevel@tonic-gate (void) di_devlink_fini(&dlh);
351*0Sstevel@tonic-gate }
352*0Sstevel@tonic-gate if (ret && *retlist)
353*0Sstevel@tonic-gate free(*retlist);
354*0Sstevel@tonic-gate if (ret && err != 0)
355*0Sstevel@tonic-gate errno = err;
356*0Sstevel@tonic-gate return (ret);
357*0Sstevel@tonic-gate }
358*0Sstevel@tonic-gate
359*0Sstevel@tonic-gate /*
360*0Sstevel@tonic-gate * Free Device Id Name List
361*0Sstevel@tonic-gate */
362*0Sstevel@tonic-gate void
devid_free_nmlist(devid_nmlist_t * list)363*0Sstevel@tonic-gate devid_free_nmlist(devid_nmlist_t *list)
364*0Sstevel@tonic-gate {
365*0Sstevel@tonic-gate devid_nmlist_t *p = list;
366*0Sstevel@tonic-gate
367*0Sstevel@tonic-gate if (list == NULL)
368*0Sstevel@tonic-gate return;
369*0Sstevel@tonic-gate
370*0Sstevel@tonic-gate /* Free all the device names */
371*0Sstevel@tonic-gate while (p->devname != NULL) {
372*0Sstevel@tonic-gate free(p->devname);
373*0Sstevel@tonic-gate p++;
374*0Sstevel@tonic-gate }
375*0Sstevel@tonic-gate
376*0Sstevel@tonic-gate /* Free the array */
377*0Sstevel@tonic-gate free(list);
378*0Sstevel@tonic-gate }
379