summaryrefslogtreecommitdiff
path: root/usr/src/lib/libdevinfo/devinfo_finddev.c
blob: eea252358f92d480215b5ace82d838fd491fbdcc (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (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 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <thread.h>
#include <synch.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <dirent.h>
#include <regex.h>
#include <errno.h>
#include <stdarg.h>
#include <libdevinfo.h>
#include <zone.h>
#include <sys/modctl.h>
#include <syslog.h>
#include <sys/stat.h>
#include <assert.h>


struct finddevhdl {
	int	npaths;
	int	curpath;
	char	**paths;
};


#define	GLOBAL_DEV_PATH(devpath)			\
	((getzoneid() == GLOBAL_ZONEID) &&		\
	    ((strcmp(devpath, "/dev") == 0) ||		\
	    (strncmp(devpath, "/dev/", strlen("/dev/")) == 0)))

/*
 * Return true if a device exists
 * If the path refers into the /dev filesystem, use a
 * private interface to query if the device exists but
 * without triggering an implicit reconfig if it does not.
 * Note: can only function properly with absolute pathnames
 * and only functions for persisted global /dev names, ie
 * those managed by devfsadm.  For paths other than
 * /dev, stat(2) is sufficient.
 */
int
device_exists(const char *devname)
{
	int	rv;
	struct stat st;

	if (GLOBAL_DEV_PATH(devname)) {
		rv = modctl(MODDEVEXISTS, devname, strlen(devname));
		return ((rv == 0) ? 1 : 0);
	}
	if (stat(devname, &st) == 0)
		return (1);
	return (0);
}


/*
 * Use the standard library readdir to read the contents of
 * directories on alternate root mounted filesystems.
 * Return results as per dev_readdir_devfs().
 *
 * The directory is traversed twice.  First, to calculate
 * the size of the buffer required; second, to copy the
 * directory contents into the buffer.  If the directory
 * contents grow in between passes, which should almost
 * never happen, start over again.
 */
static int
finddev_readdir_alt(const char *path, finddevhdl_t *handlep)
{
	struct finddevhdl *handle;
	DIR *dir;
	struct dirent *dp;
	size_t n;

	*handlep = NULL;
	if ((dir = opendir(path)) == NULL)
		return (ENOENT);

restart:
	handle = calloc(1, sizeof (struct finddevhdl));
	if (handle == NULL) {
		(void) closedir(dir);
		return (ENOMEM);
	}

	handle->npaths = 0;
	handle->curpath = 0;
	handle->paths = NULL;

	n = 0;
	rewinddir(dir);
	while ((dp = readdir(dir)) != NULL) {
		if ((strcmp(dp->d_name, ".") == 0) ||
		    (strcmp(dp->d_name, "..") == 0))
			continue;
		n++;
	}

	handle->npaths = n;
	handle->paths = calloc(n, sizeof (char *));
	if (handle->paths == NULL) {
		free(handle);
		(void) closedir(dir);
		return (ENOMEM);
	}

	n = 0;
	rewinddir(dir);
	while ((dp = readdir(dir)) != NULL) {
		if ((strcmp(dp->d_name, ".") == 0) ||
		    (strcmp(dp->d_name, "..") == 0))
			continue;
		if (n == handle->npaths) {
			/*
			 * restart if directory contents have out-grown
			 * buffer allocated in the first pass.
			 */
			finddev_close((finddevhdl_t)handle);
			goto restart;
		}
		handle->paths[n] = strdup(dp->d_name);
		if (handle->paths[n] == NULL) {
			(void) closedir(dir);
			finddev_close((finddevhdl_t)handle);
			return (ENOMEM);
		}
		n++;
	}
	(void) closedir(dir);
	*handlep = (finddevhdl_t)handle;
	return (0);
}

/*
 * Use of the dev filesystem's private readdir does not trigger
 * the implicit device reconfiguration.
 *
 * Note: only useable with paths mounted on an instance of the
 * dev filesystem.
 *
 * Does not return the . and .. entries.
 * Empty directories are returned as an zero-length list.
 * ENOENT is returned as a NULL list pointer.
 */
static int
finddev_readdir_devfs(const char *path, finddevhdl_t *handlep)
{
	struct finddevhdl	*handle;
	int			n;
	int			rv;
	int64_t			bufsiz;
	char			*pathlist;
	char			*p;
	int			len;

	*handlep = NULL;
	handle = calloc(1, sizeof (struct finddevhdl));
	if (handle == NULL)
		return (ENOMEM);

	handle->npaths = 0;
	handle->curpath = 0;
	handle->paths = NULL;

	rv = modctl(MODDEVREADDIR, path, strlen(path), NULL, &bufsiz);
	if (rv != 0) {
		free(handle);
		return (rv);
	}

	for (;;) {
		assert(bufsiz != 0);
		if ((pathlist = malloc(bufsiz)) == NULL) {
			free(handle);
			return (ENOMEM);
		}

		rv = modctl(MODDEVREADDIR, path, strlen(path),
		    pathlist, &bufsiz);
		if (rv == 0) {
			for (n = 0, p = pathlist;
			    (len = strlen(p)) > 0; p += len+1) {
				n++;
			}
			handle->npaths = n;
			handle->paths = calloc(n, sizeof (char *));
			if (handle->paths == NULL) {
				free(handle);
				free(pathlist);
				return (ENOMEM);
			}
			for (n = 0, p = pathlist;
			    (len = strlen(p)) > 0; p += len+1, n++) {
				handle->paths[n] = strdup(p);
				if (handle->paths[n] == NULL) {
					finddev_close((finddevhdl_t)handle);
					free(pathlist);
					return (ENOMEM);
				}
			}
			*handlep = (finddevhdl_t)handle;
			free(pathlist);
			return (0);
		}
		free(pathlist);
		switch (errno) {
		case EAGAIN:
			break;
		case ENOENT:
		default:
			free(handle);
			return (errno);
		}
	}
	/*NOTREACHED*/
}

int
finddev_readdir(const char *path, finddevhdl_t *handlep)
{
	if (GLOBAL_DEV_PATH(path)) {
		return (finddev_readdir_devfs(path, handlep));
	}
	return (finddev_readdir_alt(path, handlep));
}

/*
 * Return true if a directory is empty
 * Use the standard library readdir to determine if a directory is
 * empty.
 */
static int
finddev_emptydir_alt(const char *path)
{
	DIR		*dir;
	struct dirent	*dp;

	if ((dir = opendir(path)) == NULL)
		return (ENOENT);

	while ((dp = readdir(dir)) != NULL) {
		if ((strcmp(dp->d_name, ".") == 0) ||
		    (strcmp(dp->d_name, "..") == 0))
			continue;
		(void) closedir(dir);
		return (0);		/* not empty */
	}
	(void) closedir(dir);
	return (1);			/* empty */
}

/*
 * Use of the dev filesystem's private readdir does (not trigger
 * the implicit device reconfiguration) to determine if a directory
 * is empty.
 *
 * Note: only useable with paths mounted on an instance of the
 * dev filesystem.
 *
 * Does not return the . and .. entries.
 * Empty directories are returned as an zero-length list.
 * ENOENT is returned as a NULL list pointer.
 */
static int
finddev_emptydir_devfs(const char *path)
{
	int	rv;
	int	empty;

	rv = modctl(MODDEVEMPTYDIR, path, strlen(path), &empty);
	if (rv == 0) {
		return (empty);
	}
	return (0);
}

int
finddev_emptydir(const char *path)
{
	if (GLOBAL_DEV_PATH(path)) {
		return (finddev_emptydir_devfs(path));
	}
	return (finddev_emptydir_alt(path));
}

void
finddev_close(finddevhdl_t arg)
{
	struct finddevhdl *handle = (struct finddevhdl *)arg;
	int i;

	for (i = 0; i < handle->npaths; i++) {
		if (handle->paths[i])
			free(handle->paths[i]);
	}
	free(handle->paths);
	free(handle);
}

const char *
finddev_next(finddevhdl_t arg)
{
	struct finddevhdl *handle = (struct finddevhdl *)arg;
	const char *path = NULL;

	if (handle->curpath < handle->npaths) {
		path = handle->paths[handle->curpath];
		handle->curpath++;
	}
	return (path);
}