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#include <config.h>
34
35#include <infiniband/endian.h>
36#include <inttypes.h>
37#include <string.h>
38#include <errno.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <unistd.h>
42#include <sys/types.h>
43#include <sys/stat.h>
44#include <sys/sysctl.h>
45#include <fcntl.h>
46#include <dirent.h>
47#include "sysfs.h"
48
49static int ret_code(void)
50{
51	int e = errno;
52
53	if (e > 0)
54		return -e;
55	return e;
56}
57
58int sys_read_string(const char *dir_name, const char *file_name, char *str, int max_len)
59{
60	char path[256], *s;
61	size_t len;
62
63	snprintf(path, sizeof(path), "%s/%s", dir_name, file_name);
64
65	len = max_len;
66	if (sysctlbyname(PATH_TO_SYS(path), str, &len, NULL, 0) == -1)
67		return ret_code();
68
69	str[(len < max_len) ? len : max_len - 1] = 0;
70
71	if ((s = strrchr(str, '\n')))
72		*s = 0;
73
74	return 0;
75}
76
77int sys_read_guid(const char *dir_name, const char *file_name, __be64 *net_guid)
78{
79	char buf[32], *str, *s;
80	uint64_t guid;
81	int r, i;
82
83	if ((r = sys_read_string(dir_name, file_name, buf, sizeof(buf))) < 0)
84		return r;
85
86	guid = 0;
87
88	for (s = buf, i = 0; i < 4; i++) {
89		if (!(str = strsep(&s, ": \t\n")))
90			return -EINVAL;
91		guid = (guid << 16) | (strtoul(str, NULL, 16) & 0xffff);
92	}
93
94	*net_guid = htobe64(guid);
95
96	return 0;
97}
98
99int sys_read_gid(const char *dir_name, const char *file_name,
100		 union umad_gid *gid)
101{
102	char buf[64], *str, *s;
103	__be16 *ugid = (__be16 *) gid;
104	int r, i;
105
106	if ((r = sys_read_string(dir_name, file_name, buf, sizeof(buf))) < 0)
107		return r;
108
109	for (s = buf, i = 0; i < 8; i++) {
110		if (!(str = strsep(&s, ": \t\n")))
111			return -EINVAL;
112		ugid[i] = htobe16(strtoul(str, NULL, 16) & 0xffff);
113	}
114
115	return 0;
116}
117
118int sys_read_uint64(const char *dir_name, const char *file_name, uint64_t * u)
119{
120	char buf[32];
121	int r;
122
123	if ((r = sys_read_string(dir_name, file_name, buf, sizeof(buf))) < 0)
124		return r;
125
126	*u = strtoull(buf, NULL, 0);
127
128	return 0;
129}
130
131int sys_read_uint(const char *dir_name, const char *file_name, unsigned *u)
132{
133	char buf[32];
134	int r;
135
136	if ((r = sys_read_string(dir_name, file_name, buf, sizeof(buf))) < 0)
137		return r;
138
139	*u = strtoul(buf, NULL, 0);
140
141	return 0;
142}
143
144#define	DIRECTSIZ(namlen)						\
145	(((uintptr_t)&((struct dirent *)0)->d_name +			\
146	((namlen)+1)*sizeof(((struct dirent *)0)->d_name[0]) + 3) & ~3)
147
148int
149sys_scandir(const char *dirname, struct dirent ***namelist,
150    int (*select)(const struct dirent *),
151    int (*compar)(const struct dirent **, const struct dirent **))
152{
153	struct dirent **names;
154	struct dirent **names2;
155	struct dirent *dp;
156	char name[1024];
157	int lsname[22];
158	int chname[22];
159	int name2[22];
160	int oid[22];
161	char *s;
162	size_t n1, n2;
163	size_t len, oidlen, namlen;
164	int cnt, max;
165	int err;
166	int i;
167
168	*namelist = NULL;
169	if (strlcpy(name, PATH_TO_SYS(dirname), sizeof(name)) >= sizeof(name))
170		return (-EINVAL);
171	/*
172	 * Resolve the path.
173	 */
174	len = sizeof(oid) / sizeof(int);
175	namlen = strlen(name) + 1;
176	if (sysctlnametomib(name, oid, &len) != 0)
177		return (-errno);
178	lsname[0] = 0;	/* Root */
179	lsname[1] = 2;	/* Get next */
180	memcpy(lsname+2, oid, len * sizeof(int));
181	n1 = 2 + len;
182	oidlen = len;
183	/*
184	 * Setup the return list of dirents.
185	 */
186	cnt = 0;
187	max = 64;
188	names = malloc(max * sizeof(void *));
189	if (names == NULL)
190		return (-ENOMEM);
191
192	for (;;) {
193		n2 = sizeof(name2);
194		if (sysctl(lsname, n1, name2, &n2, 0, 0) < 0) {
195			if (errno == ENOENT)
196				break;
197			goto errout;
198		}
199		n2 /= sizeof(int);
200		if (n2 < oidlen)
201			break;
202		for (i = 0; i < oidlen; i++)
203			if (name2[i] != oid[i])
204				goto out;
205		chname[0] = 0;	/* root */
206		chname[1] = 1;	/* oid name */
207		memcpy(chname + 2, name2, n2 * sizeof(int));
208		memcpy(lsname + 2, name2, n2 * sizeof(int));
209		n1 = 2 + n2;
210		/*
211		 * scandir() is not supposed to go deeper than the requested
212		 * directory but sysctl also doesn't return a node for
213		 * 'subdirectories' so we have to find a file in the subdir
214		 * and then truncate the name to report it.
215	 	 */
216		if (n2 > oidlen + 1) {
217			/* Skip to the next name after this one. */
218			n1 = 2 + oidlen + 1;
219			lsname[n1 - 1]++;
220		}
221		len = sizeof(name);
222		if (sysctl(chname, n2 + 2, name, &len, 0, 0) < 0)
223			goto errout;
224		if (len <= 0 || len < namlen)
225			goto out;
226		s = name + namlen;
227		/* Just keep the first level name. */
228		if (strchr(s, '.'))
229			*strchr(s, '.') = '\0';
230		len = strlen(s) + 1;
231		dp = malloc(DIRECTSIZ(len));
232		dp->d_reclen = DIRECTSIZ(len);
233		dp->d_namlen = len;
234		memcpy(&dp->d_name, s, len);
235		if (select && !select(dp)) {
236			free(dp);
237			continue;
238		}
239		if (cnt == max) {
240			max *= 2;
241			names2 = realloc(names, max * sizeof(void *));
242			if (names2 == NULL) {
243				errno = ENOMEM;
244				free(dp);
245				goto errout;
246			}
247			names = names2;
248		}
249		names[cnt++] = dp;
250	}
251out:
252	if (cnt && compar)
253		qsort(names, cnt, sizeof(struct dirent *),
254		    (int (*)(const void *, const void *))compar);
255
256	*namelist = names;
257
258	return (cnt);
259
260errout:
261	err = errno;
262	for (i = 0; i < cnt; i++)
263		free(names[i]);
264	free(names);
265	return (-err);
266}
267