Line 0
Link Here
|
|
|
1 |
--- src/missing.cc.orig 2020-09-15 17:46:41.000000000 +0300 |
2 |
+++ src/missing.cc 2020-09-25 19:02:27.887117000 +0300 |
3 |
@@ -40,6 +40,13 @@ |
4 |
|
5 |
#ifndef HAVE_FDWALK |
6 |
|
7 |
+#ifdef __FreeBSD__ |
8 |
+#include <sys/param.h> |
9 |
+#include <sys/sysctl.h> |
10 |
+#include <sys/user.h> |
11 |
+#include <sys/file.h> |
12 |
+#endif |
13 |
+ |
14 |
#ifdef __linux__ |
15 |
|
16 |
struct linux_dirent64 |
17 |
@@ -128,6 +135,48 @@ |
18 |
return RLIM_INFINITY; |
19 |
} |
20 |
|
21 |
+#ifdef __FreeBSD__ |
22 |
+static int |
23 |
+fdwalk2(int (*func)(void *, int), void *udata, int *ret) { |
24 |
+ size_t i, bufsz = 0; |
25 |
+ struct xfile *xfbuf, *xf; |
26 |
+ int uret = 0, pid_found = 0; |
27 |
+ int mib[2] = { CTL_KERN, KERN_FILE }; |
28 |
+ pid_t pid; |
29 |
+ |
30 |
+ if (NULL == func) |
31 |
+ return EINVAL; |
32 |
+ |
33 |
+ if (sysctl(mib, nitems(mib), NULL, &bufsz, NULL, 0) == -1) |
34 |
+ return (errno); |
35 |
+ bufsz += 65536; |
36 |
+ xfbuf = (struct xfile*)alloca(bufsz); |
37 |
+ if (xfbuf == NULL) |
38 |
+ return errno; |
39 |
+ if (sysctl(mib, nitems(mib), xfbuf, &bufsz, NULL, 0) == -1) |
40 |
+ return errno; |
41 |
+ bufsz /= sizeof(struct xfile); |
42 |
+ |
43 |
+ pid = getpid(); |
44 |
+ for (i = 0; i < bufsz; i++) { |
45 |
+ xf = &xfbuf[i]; |
46 |
+ if (pid != xf->xf_pid) { |
47 |
+ if (pid_found) |
48 |
+ return 0; |
49 |
+ continue; |
50 |
+ } |
51 |
+ pid_found = 1; |
52 |
+ if (0 > xf->xf_fd) |
53 |
+ continue; |
54 |
+ uret = func(udata, xf->xf_fd); |
55 |
+ if (uret != 0) |
56 |
+ break; |
57 |
+ } |
58 |
+ |
59 |
+ return 0; |
60 |
+} |
61 |
+#endif |
62 |
+ |
63 |
/* This function is called between fork and execve/_exit and so must be |
64 |
* async-signal-safe; see man:signal-safety(7). |
65 |
*/ |
66 |
@@ -141,6 +190,12 @@ |
67 |
*/ |
68 |
int fd; |
69 |
int res = 0; |
70 |
+ |
71 |
+#ifdef __FreeBSD__ |
72 |
+ if (fdwalk2(cb, data, &res) == 0) |
73 |
+ return res; |
74 |
+ /* If any sysctl/malloc call fails continue with the fall back method */ |
75 |
+#endif |
76 |
|
77 |
#ifdef __linux__ |
78 |
/* Avoid use of opendir/closedir since these are not async-signal-safe. */ |