1/*
2 * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses.  You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 *     Redistribution and use in source and binary forms, with or
11 *     without modification, are permitted provided that the following
12 *     conditions are met:
13 *
14 *      - Redistributions of source code must retain the above
15 *        copyright notice, this list of conditions and the following
16 *        disclaimer.
17 *
18 *      - Redistributions in binary form must reproduce the above
19 *        copyright notice, this list of conditions and the following
20 *        disclaimer in the documentation and/or other materials
21 *        provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33
34#define _GNU_SOURCE
35
36#if HAVE_CONFIG_H
37#  include <config.h>
38#endif /* HAVE_CONFIG_H */
39
40#include <inttypes.h>
41#include <string.h>
42#include <errno.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <unistd.h>
46#include <stdarg.h>
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <fcntl.h>
50#include <sys/ioctl.h>
51#include <unistd.h>
52#include <string.h>
53#include <endian.h>
54#include <byteswap.h>
55#include <sys/poll.h>
56#include <syslog.h>
57#include <netinet/in.h>
58#include <errno.h>
59
60#include <sys/types.h>
61#include <sys/sysctl.h>
62
63#include "common.h"
64
65static int
66ret_code(void)
67{
68	int e = errno;
69
70	if (e > 0)
71		return -e;
72	return e;
73}
74
75int
76sys_read_string(char *dir_name, char *file_name, char *str, int max_len)
77{
78	char path[256], *s;
79	size_t len;
80
81	snprintf(path, sizeof(path), "%s/%s", dir_name, file_name);
82
83	for (s = &path[0]; *s != '\0'; s++)
84		if (*s == '/')
85			*s = '.';
86
87	len = max_len;
88	if (sysctlbyname(&path[1], str, &len, NULL, 0) == -1)
89		return ret_code();
90
91	str[(len < max_len) ? len : max_len - 1] = 0;
92
93	if ((s = strrchr(str, '\n')))
94		*s = 0;
95
96	return 0;
97}
98
99int
100sys_read_guid(char *dir_name, char *file_name, uint64_t *net_guid)
101{
102	char buf[32], *str, *s;
103	uint64_t guid;
104	int r, i;
105
106	if ((r = sys_read_string(dir_name, file_name, buf, sizeof(buf))) < 0)
107		return r;
108
109	guid = 0;
110
111	for (s = buf, i = 0 ; i < 4; i++) {
112		if (!(str = strsep(&s, ": \t\n")))
113			return -EINVAL;
114		guid = (guid << 16) | (strtoul(str, 0, 16) & 0xffff);
115	}
116
117	*net_guid = htonll(guid);
118
119	return 0;
120}
121
122int
123sys_read_gid(char *dir_name, char *file_name, uint8_t *gid)
124{
125	char buf[64], *str, *s;
126	uint16_t *ugid = (uint16_t *)gid;
127	int r, i;
128
129	if ((r = sys_read_string(dir_name, file_name, buf, sizeof(buf))) < 0)
130		return r;
131
132	for (s = buf, i = 0 ; i < 8; i++) {
133		if (!(str = strsep(&s, ": \t\n")))
134			return -EINVAL;
135		ugid[i] = htons(strtoul(str, 0, 16) & 0xffff);
136	}
137
138	return 0;
139}
140
141int
142sys_read_uint64(char *dir_name, char *file_name, uint64_t *u)
143{
144	char buf[32];
145	int r;
146
147	if ((r = sys_read_string(dir_name, file_name, buf, sizeof(buf))) < 0)
148		return r;
149
150	*u = strtoull(buf, 0, 0);
151
152	return 0;
153}
154
155int
156sys_read_uint(char *dir_name, char *file_name, unsigned *u)
157{
158	char buf[32];
159	int r;
160
161	if ((r = sys_read_string(dir_name, file_name, buf, sizeof(buf))) < 0)
162		return r;
163
164	*u = strtoul(buf, 0, 0);
165
166	return 0;
167}
168
169#define	DIRECTSIZ(namlen)						\
170	(((uintptr_t)&((struct dirent *)0)->d_name +			\
171	((namlen)+1)*sizeof(((struct dirent *)0)->d_name[0]) + 3) & ~3)
172
173int
174sys_scandir(const char *dirname, struct dirent ***namelist,
175    int (*select)(const struct dirent *),
176    int (*compar)(const struct dirent **, const struct dirent **))
177{
178	struct dirent **names;
179	struct dirent **names2;
180	struct dirent *dp;
181	char name[1024];
182	int lsname[22];
183	int chname[22];
184	int name2[22];
185	int oid[22];
186	char *s;
187	size_t n1, n2;
188	size_t len, oidlen, namlen;
189	int cnt, max;
190	int err;
191	int i;
192
193	*namelist = NULL;
194	/* Skip the leading / */
195	strncpy(name, &dirname[1], sizeof(name));
196	for (s = &name[0]; *s != '\0'; s++)
197		if (*s == '/')
198			*s = '.';
199	/*
200	 * Resolve the path.
201	 */
202	len = sizeof(oid) / sizeof(int);
203	namlen = strlen(name) + 1;
204	if (sysctlnametomib(name, oid, &len) != 0)
205		return (-errno);
206	lsname[0] = 0;	/* Root */
207	lsname[1] = 2;	/* Get next */
208	memcpy(lsname+2, oid, len * sizeof(int));
209	n1 = 2 + len;
210	oidlen = len;
211	/*
212	 * Setup the return list of dirents.
213	 */
214	cnt = 0;
215	max = 64;
216	names = malloc(max * sizeof(void *));
217	if (names == NULL)
218		return (-ENOMEM);
219
220	for (;;) {
221		n2 = sizeof(name2);
222		if (sysctl(lsname, n1, name2, &n2, 0, 0) < 0) {
223			if (errno == ENOENT)
224				break;
225			goto errout;
226		}
227		n2 /= sizeof(int);
228		if (n2 < oidlen)
229			break;
230		for (i = 0; i < oidlen; i++)
231			if (name2[i] != oid[i])
232				goto out;
233		chname[0] = 0;	/* root */
234		chname[1] = 1;	/* oid name */
235		memcpy(chname + 2, name2, n2 * sizeof(int));
236		memcpy(lsname + 2, name2, n2 * sizeof(int));
237		n1 = 2 + n2;
238		/*
239		 * scandir() is not supposed to go deeper than the requested
240		 * directory but sysctl also doesn't return a node for
241		 * 'subdirectories' so we have to find a file in the subdir
242		 * and then truncate the name to report it.
243	 	 */
244		if (n2 > oidlen + 1) {
245			/* Skip to the next name after this one. */
246			n1 = 2 + oidlen + 1;
247			lsname[n1 - 1]++;
248		}
249		len = sizeof(name);
250		if (sysctl(chname, n2 + 2, name, &len, 0, 0) < 0)
251			goto errout;
252		if (len <= 0 || len < namlen)
253			goto out;
254		s = name + namlen;
255		/* Just keep the first level name. */
256		if (strchr(s, '.'))
257			*strchr(s, '.') = '\0';
258		len = strlen(s) + 1;
259		dp = malloc(DIRECTSIZ(len));
260		dp->d_reclen = DIRECTSIZ(len);
261		dp->d_namlen = len;
262		memcpy(&dp->d_name, s, len);
263		if (select && !select(dp)) {
264			free(dp);
265			continue;
266		}
267		if (cnt == max) {
268			max *= 2;
269			names2 = realloc(names, max * sizeof(void *));
270			if (names2 == NULL) {
271				errno = ENOMEM;
272				free(dp);
273				goto errout;
274			}
275			names = names2;
276		}
277		names[cnt++] = dp;
278	}
279out:
280	if (cnt && compar)
281		qsort(names, cnt, sizeof(struct dirent *),
282		    (int (*)(const void *, const void *))compar);
283
284	*namelist = names;
285
286	return (cnt);
287
288errout:
289	err = errno;
290	for (i = 0; i < cnt; i++)
291		free(names[i]);
292	free(names);
293	return (-err);
294}
295