Line 0
Link Here
|
|
|
1 |
--- glib/gspawn.c 2018-09-21 12:29:23.000000000 +0300 |
2 |
+++ glib/gspawn.c 2019-07-20 18:05:15.486558000 +0300 |
3 |
@@ -39,6 +39,13 @@ |
4 |
#include <sys/resource.h> |
5 |
#endif /* HAVE_SYS_RESOURCE_H */ |
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 |
#include "gspawn.h" |
15 |
#include "gthread.h" |
16 |
#include "glib/gstdio.h" |
17 |
@@ -1077,7 +1084,53 @@ set_cloexec (void *data, gint fd) |
18 |
} |
19 |
|
20 |
#ifndef HAVE_FDWALK |
21 |
+ |
22 |
+#ifdef __FreeBSD__ |
23 |
static int |
24 |
+fdwalk2(int (*func)(void *, int), void *udata, int *ret) { |
25 |
+ size_t i, bufsz = 0; |
26 |
+ struct xfile *xfbuf, *xf; |
27 |
+ int uret = 0, pid_found = 0; |
28 |
+ int mib[2] = { CTL_KERN, KERN_FILE }; |
29 |
+ pid_t pid; |
30 |
+ |
31 |
+ if (NULL == func) |
32 |
+ return EINVAL; |
33 |
+ |
34 |
+ if (sysctl (mib, nitems(mib), NULL, &bufsz, NULL, 0) == -1) |
35 |
+ return (errno); |
36 |
+ bufsz += 65536; |
37 |
+ xfbuf = alloca (bufsz); |
38 |
+ if (xfbuf == NULL) |
39 |
+ return errno; |
40 |
+ if (sysctl (mib, 2, xfbuf, &bufsz, NULL, 0) == -1) |
41 |
+ return errno; |
42 |
+ bufsz /= sizeof(struct xfile); |
43 |
+ |
44 |
+ pid = getpid(); |
45 |
+ for (i = 0; i < bufsz; i++) { |
46 |
+ xf = &xfbuf[i]; |
47 |
+ if (pid != xf->xf_pid) { |
48 |
+ if (pid_found) { |
49 |
+ return 0; |
50 |
+ } else { |
51 |
+ continue; |
52 |
+ } |
53 |
+ } |
54 |
+ pid_found = 1; |
55 |
+ if (0 > xf->xf_fd) |
56 |
+ continue; |
57 |
+ uret = func (udata, xf->xf_fd); |
58 |
+ if (uret != 0) |
59 |
+ break; |
60 |
+ |
61 |
+ } |
62 |
+ |
63 |
+ return 0; |
64 |
+} |
65 |
+#endif |
66 |
+ |
67 |
+static int |
68 |
fdwalk (int (*cb)(void *data, int fd), void *data) |
69 |
{ |
70 |
gint open_max; |
71 |
@@ -1086,6 +1139,12 @@ fdwalk (int (*cb)(void *data, int fd), void *data) |
72 |
|
73 |
#ifdef HAVE_SYS_RESOURCE_H |
74 |
struct rlimit rl; |
75 |
+#endif |
76 |
+ |
77 |
+#ifdef __FreeBSD__ |
78 |
+ if (fdwalk2(cb, data, &res) == 0) |
79 |
+ return res; |
80 |
+ /* If any sysctl/malloc call fails continue with the fall back method */ |
81 |
#endif |
82 |
|
83 |
#ifdef __linux__ |