summaryrefslogtreecommitdiff
path: root/usr/src/lib/libdevid/deviceid.c
blob: 67f185fe69bf8981905c3358ddd064e9a2e8ada0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright 2017 Nexenta Systems, Inc.
 */

#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <ftw.h>
#include <string.h>
#include <thread.h>
#include <synch.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/modctl.h>
#include <strings.h>

#include <libdevinfo.h>
#include "libdevid.h"

/*
 * Get Device Id from an open file descriptor
 */
int
devid_get(int fd, ddi_devid_t *devidp)
{
	int		len = 0;
	dev_t		dev;
	struct stat	statb;
	ddi_devid_t	mydevid;

	if (fstat(fd, &statb) != 0)
		return (-1);

	/* If not char or block device, then error */
	if (!S_ISCHR(statb.st_mode) && !S_ISBLK(statb.st_mode))
		return (-1);

	/* Get the device id size */
	dev = statb.st_rdev;
	if (modctl(MODSIZEOF_DEVID, dev, &len) != 0)
		return (-1);

	/* Allocate space to return device id */
	if ((mydevid = (ddi_devid_t)malloc(len)) == NULL)
		return (-1);

	/* Get the device id */
	if (modctl(MODGETDEVID, dev, len, mydevid) != 0) {
		free(mydevid);
		return (-1);
	}

	/* Return the device id copy */
	*devidp = mydevid;
	return (0);
}

/*
 * Get the minor name
 */
int
devid_get_minor_name(int fd, char **minor_namep)
{
	int		len = 0;
	dev_t		dev;
	int		spectype;
	char		*myminor_name;
	struct stat	statb;

	if (fstat(fd, &statb) != 0)
		return (-1);

	/* If not a char or block device, then return an error */
	if (!S_ISCHR(statb.st_mode) && !S_ISBLK(statb.st_mode))
		return (-1);

	spectype = statb.st_mode & S_IFMT;
	dev = statb.st_rdev;

	/* Get the minor name size */
	if (modctl(MODSIZEOF_MINORNAME, dev, spectype, &len) != 0)
		return (-1);

	/* Allocate space for the minor name */
	if ((myminor_name = (char *)malloc(len)) == NULL)
		return (-1);

	/* Get the minor name */
	if (modctl(MODGETMINORNAME, dev, spectype, len, myminor_name) != 0) {
		free(myminor_name);
		return (-1);
	}

	/* return the minor name copy */
	*minor_namep = myminor_name;
	return (0);
}

char *
devid_str_from_path(const char *path)
{
	int		fd;
	ddi_devid_t	devid;
	char		*minor, *ret = NULL;

	if ((fd = open(path, O_RDONLY)) < 0)
		return (NULL);

	if (devid_get(fd, &devid) == 0) {
		if (devid_get_minor_name(fd, &minor) != 0)
			minor = NULL;
		ret = devid_str_encode(devid, minor);
		if (minor != NULL)
			devid_str_free(minor);
		devid_free(devid);
	}
	(void) close(fd);

	return (ret);
}

/* list element of devid_nmlist_t information */
struct nmlist {
	struct nmlist	*nl_next;
	char		*nl_devname;
	dev_t		nl_dev;
};

/* add list element to end of nmlist headed by *nlhp */
struct nmlist *
nmlist_add(struct nmlist **nlhp, char *path)
{
	struct stat	statb;
	dev_t		dev;
	struct nmlist	*nl;

	/* stat and get the devt for char or block */
	if ((stat(path, &statb) == 0) &&
	    (S_ISCHR(statb.st_mode) || S_ISBLK(statb.st_mode)))
		dev = statb.st_rdev;
	else
		dev = NODEV;

	/* find the end of the list */
	for (; (nl = *nlhp) != NULL; nlhp = &nl->nl_next)
		;

	/* allocate and initialize new entry */
	if ((nl = malloc(sizeof (*nl))) == NULL)
		return (NULL);

	if ((nl->nl_devname = strdup(path)) == NULL) {
		free(nl);
		return (NULL);
	}
	nl->nl_next = NULL;
	nl->nl_dev = dev;

	/* link new entry at end */
	*nlhp = nl;
	return (nl);
}

/* information needed by devlink_callback to call nmlist_add */
struct devlink_cbinfo {
	struct nmlist	**cbi_nlhp;
	char		*cbi_search_path;
	int		cbi_error;
};

/* di_devlink callback to add a /dev entry to nmlist */
static int
devlink_callback(di_devlink_t dl, void *arg)
{
	struct devlink_cbinfo	*cbip = (struct devlink_cbinfo *)arg;
	char			*devpath = (char *)di_devlink_path(dl);

	if (strncmp(devpath, cbip->cbi_search_path,
	    strlen(cbip->cbi_search_path)) == 0) {
		if (nmlist_add(cbip->cbi_nlhp, devpath) == NULL) {
			cbip->cbi_error = 1;
			return (DI_WALK_TERMINATE);
		}
	}
	return (DI_WALK_CONTINUE);
}

/*
 * Resolve /dev names to DI_PRIMARY_LINK, DI_SECONDARY_LINK, or both.
 * The default is to resolve to just the DI_PRIMARY_LINK.
 */
int			devid_deviceid_to_nmlist_link = DI_PRIMARY_LINK;

/*
 * Options for the devid_deviceid_to_nmlist implementation:
 *
 *   DEVICEID_NMLIST_SLINK -	reduce overhead by reuse the previous
 *				di_devlink_init.
 */
#define	DEVICEID_NMLIST_SLINK	1
int			devid_deviceid_to_nmlist_flg = 0;
static di_devlink_handle_t devid_deviceid_to_nmlist_dlh = NULL;	/* SLINK */

#define	DEVICEID_NMLIST_NRETRY	10

/*
 * Convert the specified devid/minor_name into a devid_nmlist_t array
 * with names that resolve into /devices or /dev depending on search_path.
 *
 * The man page indicates that:
 *
 *     This function traverses the file tree, starting at search_path.
 *
 * This is not true, we reverse engineer the paths relative to
 * the specified search path to avoid attaching all devices.
 */
int
devid_deviceid_to_nmlist(char *search_path, ddi_devid_t devid, char *minor_name,
    devid_nmlist_t **retlist)
{
	char			*cp;
	int			dev;
	char			*paths = NULL;
	char			*path;
	int			lens;
	di_devlink_handle_t	dlh = NULL;
	int			ret = -1;
	struct devlink_cbinfo	cbi;
	struct nmlist		*nlh = NULL;
	struct nmlist		*nl;
	devid_nmlist_t		*rl;
	int			nret;
	int			nagain = 0;
	int			err = 0;

	*retlist = NULL;

	/* verify valid search path starts with "/devices" or "/dev" */
	if ((strcmp(search_path, "/devices") == 0) ||
	    (strncmp(search_path, "/devices/", 9) == 0))
		dev = 0;
	else if ((strcmp(search_path, "/dev") == 0) ||
	    (strncmp(search_path, "/dev/", 5) == 0))
		dev = 1;
	else {
		errno = EINVAL;
		return (-1);
	}


	/* translate devid/minor_name to /devices paths */
again:	if (modctl(MODDEVID2PATHS, devid, minor_name, 0, &lens, NULL) != 0)
		goto out;
	if ((paths = (char *)malloc(lens)) == NULL)
		goto out;
	if (modctl(MODDEVID2PATHS, devid, minor_name, 0, &lens, paths) != 0) {
		if ((errno == EAGAIN) && (nagain++ < DEVICEID_NMLIST_NRETRY)) {
			free(paths);
			paths = NULL;
			goto again;
		}
		goto out;
	}

	/*
	 * initialize for /devices path to /dev path translation. To reduce
	 * overhead we reuse the last snapshot if DEVICEID_NMLIST_SLINK is set.
	 */
	if (dev) {
		dlh = devid_deviceid_to_nmlist_dlh;
		if (dlh &&
		    !(devid_deviceid_to_nmlist_flg & DEVICEID_NMLIST_SLINK)) {
			(void) di_devlink_fini(&dlh);
			dlh = devid_deviceid_to_nmlist_dlh = NULL;
		}
		if ((dlh == NULL) &&
		    ((dlh = di_devlink_init(NULL, 0)) == NULL))
				goto out;
	}

	/*
	 * iterate over all the devtspectype resolutions of the devid and
	 * convert them into the appropriate path form and add items to return
	 * to the nmlist list;
	 */
	for (path = paths; *path; path += strlen(path) + 1) {
		if (dev) {
			/* add /dev entries */
			cbi.cbi_nlhp = &nlh;
			cbi.cbi_search_path = search_path;
			cbi.cbi_error = 0;

			(void) di_devlink_walk(dlh, NULL, path,
			    devid_deviceid_to_nmlist_link,
			    (void *)&cbi, devlink_callback);
			if (cbi.cbi_error)
				goto out;
		} else {
			/* add /devices entry */
			cp = malloc(strlen("/devices") + strlen(path) + 1);
			(void) strcpy(cp, "/devices");
			(void) strcat(cp, path);
			if (strncmp(cp, search_path,
			    strlen(search_path)) == 0) {
				if (nmlist_add(&nlh, cp) == NULL) {
					free(cp);
					goto out;
				}
			}
			free(cp);
		}
	}

	/* convert from nmlist to retlist array */
	for (nl = nlh, nret = 0; nl; nl = nl->nl_next)
		nret++;
	if (nret == 0) {
		err = ENODEV;
		goto out;
	}
	if ((*retlist = calloc(nret + 1, sizeof (devid_nmlist_t))) == NULL) {
		err = ENOMEM;
		goto out;
	}
	for (nl = nlh, rl = *retlist; nl; nl = nl->nl_next, rl++) {
		rl->devname = nl->nl_devname;
		rl->dev = nl->nl_dev;
	}
	rl->devname = NULL;
	rl->dev = NODEV;

	ret = 0;

out:
	while ((nl = nlh) != NULL) {	/* free the nmlist */
		nlh = nl->nl_next;
		free(nl);
	}
	if (paths)
		free(paths);
	if (dlh) {
		if ((ret == 0) &&
		    (devid_deviceid_to_nmlist_flg & DEVICEID_NMLIST_SLINK))
			devid_deviceid_to_nmlist_dlh = dlh;
		else
			(void) di_devlink_fini(&dlh);
	}
	if (ret && *retlist)
		free(*retlist);
	if (ret && err != 0)
		errno = err;
	return (ret);
}

/*
 * Free Device Id Name List
 */
void
devid_free_nmlist(devid_nmlist_t *list)
{
	devid_nmlist_t *p = list;

	if (list == NULL)
		return;

	/* Free all the device names */
	while (p->devname != NULL) {
		free(p->devname);
		p++;
	}

	/* Free the array */
	free(list);
}