2 * Copyright (c) 2004-2008 Voltaire Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
38 #endif /* HAVE_CONFIG_H */
47 #include <sys/types.h>
50 #include <sys/ioctl.h>
57 #include <netinet/in.h>
60 #include <sys/types.h>
61 #include <sys/sysctl.h>
76 sys_read_string(char *dir_name, char *file_name, char *str, int max_len)
81 snprintf(path, sizeof(path), "%s/%s", dir_name, file_name);
83 for (s = &path[0]; *s != '\0'; s++)
88 if (sysctlbyname(&path[1], str, &len, NULL, 0) == -1)
91 str[(len < max_len) ? len : max_len - 1] = 0;
93 if ((s = strrchr(str, '\n')))
100 sys_read_guid(char *dir_name, char *file_name, uint64_t *net_guid)
102 char buf[32], *str, *s;
106 if ((r = sys_read_string(dir_name, file_name, buf, sizeof(buf))) < 0)
111 for (s = buf, i = 0 ; i < 4; i++) {
112 if (!(str = strsep(&s, ": \t\n")))
114 guid = (guid << 16) | (strtoul(str, 0, 16) & 0xffff);
117 *net_guid = htonll(guid);
123 sys_read_gid(char *dir_name, char *file_name, uint8_t *gid)
125 char buf[64], *str, *s;
126 uint16_t *ugid = (uint16_t *)gid;
129 if ((r = sys_read_string(dir_name, file_name, buf, sizeof(buf))) < 0)
132 for (s = buf, i = 0 ; i < 8; i++) {
133 if (!(str = strsep(&s, ": \t\n")))
135 ugid[i] = htons(strtoul(str, 0, 16) & 0xffff);
142 sys_read_uint64(char *dir_name, char *file_name, uint64_t *u)
147 if ((r = sys_read_string(dir_name, file_name, buf, sizeof(buf))) < 0)
150 *u = strtoull(buf, 0, 0);
156 sys_read_uint(char *dir_name, char *file_name, unsigned *u)
161 if ((r = sys_read_string(dir_name, file_name, buf, sizeof(buf))) < 0)
164 *u = strtoul(buf, 0, 0);
169 #define DIRECTSIZ(namlen) \
170 (((uintptr_t)&((struct dirent *)0)->d_name + \
171 ((namlen)+1)*sizeof(((struct dirent *)0)->d_name[0]) + 3) & ~3)
174 sys_scandir(const char *dirname, struct dirent ***namelist,
175 int (*select)(const struct dirent *),
176 int (*compar)(const struct dirent **, const struct dirent **))
178 struct dirent **names;
179 struct dirent **names2;
188 size_t len, oidlen, namlen;
194 /* Skip the leading / */
195 strncpy(name, &dirname[1], sizeof(name));
196 for (s = &name[0]; *s != '\0'; s++)
202 len = sizeof(oid) / sizeof(int);
203 namlen = strlen(name) + 1;
204 if (sysctlnametomib(name, oid, &len) != 0)
206 lsname[0] = 0; /* Root */
207 lsname[1] = 2; /* Get next */
208 memcpy(lsname+2, oid, len * sizeof(int));
212 * Setup the return list of dirents.
216 names = malloc(max * sizeof(void *));
222 if (sysctl(lsname, n1, name2, &n2, 0, 0) < 0) {
230 for (i = 0; i < oidlen; i++)
231 if (name2[i] != oid[i])
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));
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.
244 if (n2 > oidlen + 1) {
245 /* Skip to the next name after this one. */
250 if (sysctl(chname, n2 + 2, name, &len, 0, 0) < 0)
252 if (len <= 0 || len < namlen)
255 /* Just keep the first level name. */
257 *strchr(s, '.') = '\0';
259 dp = malloc(DIRECTSIZ(len));
260 dp->d_reclen = DIRECTSIZ(len);
262 memcpy(&dp->d_name, s, len);
263 if (select && !select(dp)) {
269 names2 = realloc(names, max * sizeof(void *));
270 if (names2 == NULL) {
281 qsort(names, cnt, sizeof(struct dirent *),
282 (int (*)(const void *, const void *))compar);
290 for (i = 0; i < cnt; i++)