-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.c
417 lines (355 loc) · 8.4 KB
/
utils.c
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* Copyright (c) 2015, 2021 Vladimir Kondratyev <vladimir@kondratyev.su>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "config.h"
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_LIBPROCSTAT_H
#include <sys/sysctl.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <kvm.h>
#include <libprocstat.h>
#endif
#ifdef HAVE_DEVINFO_H
#include <devinfo.h>
#include <pthread.h>
static pthread_mutex_t devinfo_mtx = PTHREAD_MUTEX_INITIALIZER;
#endif
#ifndef HAVE_PIPE2
#include <fcntl.h>
#endif
#include "utils.h"
/*
* locates the occurrence of last component of the pathname
* pointed to by path
*/
char *
strbase(const char *path)
{
char *base;
base = strrchr(path, '/');
if (base != NULL)
base++;
return (base);
}
char *
get_kern_prop_value(const char *buf, const char *prop, size_t *len)
{
char *prop_pos, *ret;
size_t prop_len;
prop_len = strlen(prop);
prop_pos = strstr(buf, prop);
if (prop_pos == NULL ||
(prop_pos != buf && prop_pos[-1] != ' ') ||
prop_pos[prop_len] != '=')
return (NULL);
ret = prop_pos + prop_len + 1;
if (len != NULL)
*len = strchrnul(ret, ' ') - ret;
return (ret);
}
int
match_kern_prop_value(const char *buf, const char *prop,
const char *match_value)
{
const char *value;
size_t len;
value = get_kern_prop_value(buf, prop, &len);
if (value != NULL &&
len == strlen(match_value) &&
strncmp(value, match_value, len) == 0)
return (1);
return (0);
}
int
path_to_fd(const char *path)
{
int fd = -1;
#ifdef HAVE_LIBPROCSTAT_H
struct procstat *procstat;
struct kinfo_proc *kip;
struct filestat_list *head = NULL;
struct filestat *fst;
unsigned int count;
#else
struct stat st, fst;
#define MAX_FD 128
#endif
#ifdef HAVE_LIBPROCSTAT_H
procstat = procstat_open_sysctl();
if (procstat == NULL)
return (-1);
count = 0;
kip = procstat_getprocs(procstat, KERN_PROC_PID, getpid(), &count);
if (kip == NULL || count != 1)
goto out;
head = procstat_getfiles(procstat, kip, 0);
if (head == NULL)
goto out;
STAILQ_FOREACH(fst, head, next) {
if (fst->fs_uflags == 0 &&
fst->fs_type == PS_FST_TYPE_VNODE &&
fst->fs_path != NULL &&
strcmp(fst->fs_path, path) == 0) {
fd = fst->fs_fd;
break;
}
}
out:
if (head != NULL)
procstat_freefiles(procstat, head);
if (kip != NULL)
procstat_freeprocs(procstat, kip);
procstat_close(procstat);
#else
if (stat(path, &st) != 0)
return (-1);
for (fd = 0; fd < MAX_FD; ++fd) {
if (fstat(fd, &fst) != 0) {
if (errno != EBADF) {
return -1;
} else {
continue;
}
}
if (fst.st_rdev == st.st_rdev)
break;
}
if (fd == MAX_FD)
return (-1);
#endif
return (fd);
}
static int
scandir_sub(char *path, int off, int rem, struct scandir_ctx *ctx)
{
DIR *dir;
struct dirent *ent;
int ret = 0;
dir = opendir(path);
if (dir == NULL)
return (errno == ENOMEM ? -1 : 0);
while (ret >= 0 && (ent = readdir(dir)) != NULL) {
if (strcmp(ent->d_name, ".") == 0 ||
strcmp(ent->d_name, "..") == 0)
continue;
int len = strlen(ent->d_name);
if (len > rem)
continue;
strcpy(path + off, ent->d_name);
off += len;
rem -= len;
if ((ctx->recursive) && (ent->d_type == DT_DIR)) {
if (rem < 1)
break;
path[off] = '/';
path[off+1] = '\0';
off++;
rem--;
/* recurse */
ret = scandir_sub(path, off, rem, ctx);
off--;
rem++;
} else {
ret = (ctx->cb)(path, DTTOIF(ent->d_type), ctx->args);
}
off -= len;
rem += len;
}
closedir(dir);
return (ret);
}
int
scandir_recursive(char *path, size_t len, struct scandir_ctx *ctx)
{
size_t root_len = strlen(path);
return (scandir_sub(path, root_len, len - root_len - 1, ctx));
}
#ifdef HAVE_DEVINFO_H
static int
scandev_sub(struct devinfo_dev *dev, void *args)
{
struct scandev_ctx *ctx = args;
if ((ctx->cb)(dev, ctx->args) < 0)
return (-1);
/* recurse */
return (devinfo_foreach_device_child(dev, scandev_sub, args));
}
int
scandev_recursive (struct scandev_ctx *ctx)
{
struct devinfo_dev *root;
int ret;
pthread_mutex_lock(&devinfo_mtx);
if (devinfo_init()) {
pthread_mutex_unlock(&devinfo_mtx);
ERR("devinfo_init failed");
return (-1);
}
if ((root = devinfo_handle_to_device(DEVINFO_ROOT_DEVICE)) == NULL) {
ERR("faled to init devinfo root device");
ret = -1;
} else {
ret = devinfo_foreach_device_child(root, scandev_sub, ctx);
if (ret < 0)
ERR("devinfo_foreach_device_child failed");
}
devinfo_free();
pthread_mutex_unlock(&devinfo_mtx);
return (ret);
}
#endif /* HAVE_DEVINFO_H */
#ifndef HAVE_DEVNAME_R
struct devname_scan_args {
dev_t dev;
mode_t type;
char *buf;
int len;
};
static int
devname_cb(const char *path, mode_t type, void *args)
{
struct devname_scan_args *sa = args;
struct stat st;
if (sa->type == type &&
lstat(path, &st) == 0 &&
st.ST_RDEV == sa->dev) {
strlcpy(sa->buf, path + 5, sa->len);
return (-1);
}
return (0);
}
char *
devname_r(dev_t dev, mode_t type, char *buf, int len)
{
char path[PATH_MAX] = "/dev/";
struct devname_scan_args args = {
.dev = dev,
.type = type,
.buf = buf,
.len = len,
};
struct scandir_ctx ctx = {
.recursive = true,
.cb = devname_cb,
.args = &args,
};
if (dev == NODEV || !(S_ISCHR(type) || S_ISBLK(dev))) {
strlcpy(buf, "#NODEV", len);
return (buf);
}
if (scandir_recursive(path, sizeof(path), &ctx) == 0)
/* Finally just format it */
snprintf(buf, len, "#%c:%#jx",
S_ISCHR(type) ? 'C' : 'B', (uintmax_t)dev);
return (buf);
}
#endif /* !HAVE_DEVNAME_R */
#ifndef HAVE_PIPE2
int
pipe2(int fildes[2], int flags)
{
int ret;
ret = pipe(fildes);
if (ret != -1) {
if (flags & O_CLOEXEC) {
fcntl(fildes[0], F_SETFD, FD_CLOEXEC);
fcntl(fildes[1], F_SETFD, FD_CLOEXEC);
}
if (flags & O_NONBLOCK) {
fcntl(fildes[0], F_SETFL, O_NONBLOCK);
fcntl(fildes[1], F_SETFL, O_NONBLOCK);
}
}
return (ret);
}
#endif /* !HAVE_PIPE2 */
#ifndef HAVE_STRCHRNUL
char *
strchrnul(const char *p, int ch)
{
char c;
c = ch;
for (;; ++p) {
if (*p == c || *p == '\0')
return ((char *)p);
}
/* NOTREACHED */
}
#endif /* !HAVE_STRCHRNUL */
#ifndef HAVE_STRLCPY
size_t strlcpy(char *dst, const char *src, size_t dsize)
{
const char *osrc = src;
size_t nleft = dsize;
/* Copy as many bytes as will fit. */
if (nleft != 0) {
while (--nleft != 0) {
if ((*dst++ = *src++) == '\0')
break;
}
}
/* Not enough room in dst, add NUL and traverse rest of src. */
if (nleft == 0) {
if (dsize != 0)
*dst = '\0'; /* NUL-terminate dst */
while (*src++)
;
}
return(src - osrc - 1); /* count does not include NUL */
}
#endif /* !HAVE_STRLCPY */
#ifndef HAVE_STRLCAT
size_t
strlcat(char *dst, const char *src, size_t dsize)
{
const char *odst = dst;
const char *osrc = src;
size_t n = dsize;
size_t dlen;
/* Find the end of dst and adjust bytes left but don't go past end. */
while (n-- != 0 && *dst != '\0')
dst++;
dlen = dst - odst;
n = dsize - dlen;
if (n-- == 0)
return(dlen + strlen(src));
while (*src != '\0') {
if (n != 0) {
*dst++ = *src;
n--;
}
src++;
}
*dst = '\0';
return(dlen + (src - osrc)); /* count does not include NUL */
}
#endif /* !HAVE_STRLCAT */