diff options
author | Brent Cook <bcook@rapid7.com> | 2017-01-15 04:30:41 -0600 |
---|---|---|
committer | Brent Cook <busterb@gmail.com> | 2017-01-15 16:00:16 -0600 |
commit | dfb6b11e5a5ba21c6658d87b4df83c2a05d2deab (patch) | |
tree | 8ecf72c600ad63d8eba282b0c64cb0169451620d /tests/compat/pipe2.c | |
parent | fa20dae3297fd547341021c7bda9b7a567f6a881 (diff) | |
download | portable-dfb6b11e5a5ba21c6658d87b4df83c2a05d2deab.tar.gz portable-dfb6b11e5a5ba21c6658d87b4df83c2a05d2deab.tar.bz2 portable-dfb6b11e5a5ba21c6658d87b4df83c2a05d2deab.zip |
include pipe2/socketpair compat for macOS
Diffstat (limited to '')
-rw-r--r-- | tests/compat/pipe2.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/tests/compat/pipe2.c b/tests/compat/pipe2.c new file mode 100644 index 0000000..e2aaa28 --- /dev/null +++ b/tests/compat/pipe2.c | |||
@@ -0,0 +1,56 @@ | |||
1 | /* | ||
2 | * Public domain | ||
3 | */ | ||
4 | |||
5 | #include <fcntl.h> | ||
6 | #include <unistd.h> | ||
7 | #include <sys/socket.h> | ||
8 | #undef socketpair | ||
9 | |||
10 | static int setfd(int fd, int flag) | ||
11 | { | ||
12 | int flags = fcntl(fd, F_GETFD); | ||
13 | flags |= flag; | ||
14 | return fcntl(fd, F_SETFD, flags); | ||
15 | } | ||
16 | |||
17 | static int setfl(int fd, int flag) | ||
18 | { | ||
19 | int flags = fcntl(fd, F_GETFL); | ||
20 | flags |= flag; | ||
21 | return fcntl(fd, F_SETFL, flags); | ||
22 | } | ||
23 | |||
24 | int pipe2(int fildes[2], int flags) | ||
25 | { | ||
26 | int rc = pipe(fildes); | ||
27 | if (rc == 0) { | ||
28 | if (flags & O_NONBLOCK) { | ||
29 | setfl(fildes[0], O_NONBLOCK); | ||
30 | setfl(fildes[1], O_NONBLOCK); | ||
31 | } | ||
32 | if (flags & O_CLOEXEC) { | ||
33 | setfd(fildes[0], FD_CLOEXEC); | ||
34 | setfd(fildes[1], FD_CLOEXEC); | ||
35 | } | ||
36 | } | ||
37 | return rc; | ||
38 | } | ||
39 | |||
40 | int bsd_socketpair(int domain, int type, int protocol, int socket_vector[2]) | ||
41 | { | ||
42 | int flags = type & ~0xf; | ||
43 | type &= 0xf; | ||
44 | int rc = socketpair(domain, type, protocol, socket_vector); | ||
45 | if (rc == 0) { | ||
46 | if (flags & SOCK_NONBLOCK) { | ||
47 | setfl(socket_vector[0], O_NONBLOCK); | ||
48 | setfl(socket_vector[1], O_NONBLOCK); | ||
49 | } | ||
50 | if (flags & SOCK_CLOEXEC) { | ||
51 | setfd(socket_vector[0], FD_CLOEXEC); | ||
52 | setfd(socket_vector[1], FD_CLOEXEC); | ||
53 | } | ||
54 | } | ||
55 | return rc; | ||
56 | } | ||