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 | |
| parent | fa20dae3297fd547341021c7bda9b7a567f6a881 (diff) | |
| download | portable-dfb6b11e5a5ba21c6658d87b4df83c2a05d2deab.tar.gz portable-dfb6b11e5a5ba21c6658d87b4df83c2a05d2deab.tar.bz2 portable-dfb6b11e5a5ba21c6658d87b4df83c2a05d2deab.zip | |
include pipe2/socketpair compat for macOS
| -rw-r--r-- | include/compat/sys/socket.h | 7 | ||||
| -rw-r--r-- | include/compat/unistd.h | 4 | ||||
| -rw-r--r-- | m4/check-libc.m4 | 4 | ||||
| -rw-r--r-- | tests/CMakeLists.txt | 11 | ||||
| -rw-r--r-- | tests/Makefile.am | 3 | ||||
| -rw-r--r-- | tests/compat/pipe2.c | 56 |
6 files changed, 83 insertions, 2 deletions
diff --git a/include/compat/sys/socket.h b/include/compat/sys/socket.h index 17e84f1..10eb05f 100644 --- a/include/compat/sys/socket.h +++ b/include/compat/sys/socket.h | |||
| @@ -8,3 +8,10 @@ | |||
| 8 | #else | 8 | #else |
| 9 | #include <win32netcompat.h> | 9 | #include <win32netcompat.h> |
| 10 | #endif | 10 | #endif |
| 11 | |||
| 12 | #if !defined(SOCK_NONBLOCK) || !defined(SOCK_CLOEXEC) | ||
| 13 | #define SOCK_CLOEXEC 0x8000 /* set FD_CLOEXEC */ | ||
| 14 | #define SOCK_NONBLOCK 0x4000 /* set O_NONBLOCK */ | ||
| 15 | int bsd_socketpair(int domain, int type, int protocol, int socket_vector[2]); | ||
| 16 | #define socketpair(d,t,p,sv) bsd_socketpair(d,t,p,sv) | ||
| 17 | #endif | ||
diff --git a/include/compat/unistd.h b/include/compat/unistd.h index 52255bb..14825eb 100644 --- a/include/compat/unistd.h +++ b/include/compat/unistd.h | |||
| @@ -40,4 +40,8 @@ int getentropy(void *buf, size_t buflen); | |||
| 40 | 40 | ||
| 41 | #define pledge(request, paths) 0 | 41 | #define pledge(request, paths) 0 |
| 42 | 42 | ||
| 43 | #ifndef HAVE_PIPE2 | ||
| 44 | int pipe2(int fildes[2], int flags); | ||
| 45 | #endif | ||
| 46 | |||
| 43 | #endif | 47 | #endif |
diff --git a/m4/check-libc.m4 b/m4/check-libc.m4 index b58f0b1..54efc37 100644 --- a/m4/check-libc.m4 +++ b/m4/check-libc.m4 | |||
| @@ -20,10 +20,12 @@ AM_CONDITIONAL([HAVE_TIMEGM], [test "x$ac_cv_func_timegm" = xyes]) | |||
| 20 | ]) | 20 | ]) |
| 21 | 21 | ||
| 22 | AC_DEFUN([CHECK_SYSCALL_COMPAT], [ | 22 | AC_DEFUN([CHECK_SYSCALL_COMPAT], [ |
| 23 | AC_CHECK_FUNCS([accept4 pledge poll]) | 23 | AC_CHECK_FUNCS([accept4 pipe2 pledge poll socketpair]) |
| 24 | AM_CONDITIONAL([HAVE_ACCEPT4], [test "x$ac_cv_func_accept4" = xyes]) | 24 | AM_CONDITIONAL([HAVE_ACCEPT4], [test "x$ac_cv_func_accept4" = xyes]) |
| 25 | AM_CONDITIONAL([HAVE_PIPE2], [test "x$ac_cv_func_pipe2" = xyes]) | ||
| 25 | AM_CONDITIONAL([HAVE_PLEDGE], [test "x$ac_cv_func_pledge" = xyes]) | 26 | AM_CONDITIONAL([HAVE_PLEDGE], [test "x$ac_cv_func_pledge" = xyes]) |
| 26 | AM_CONDITIONAL([HAVE_POLL], [test "x$ac_cv_func_poll" = xyes]) | 27 | AM_CONDITIONAL([HAVE_POLL], [test "x$ac_cv_func_poll" = xyes]) |
| 28 | AM_CONDITIONAL([HAVE_SOCKETPAIR], [test "x$ac_cv_func_socketpair" = xyes]) | ||
| 27 | ]) | 29 | ]) |
| 28 | 30 | ||
| 29 | AC_DEFUN([CHECK_B64_NTOP], [ | 31 | AC_DEFUN([CHECK_B64_NTOP], [ |
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f4277bd..0b2f7fa 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt | |||
| @@ -346,7 +346,16 @@ add_test(timingsafe timingsafe) | |||
| 346 | 346 | ||
| 347 | # tlstest | 347 | # tlstest |
| 348 | if(NOT CMAKE_HOST_WIN32 AND NOT CMAKE_SYSTEM_NAME MATCHES "MINGW") | 348 | if(NOT CMAKE_HOST_WIN32 AND NOT CMAKE_SYSTEM_NAME MATCHES "MINGW") |
| 349 | add_executable(tlstest tlstest.c) | 349 | |
| 350 | set(TLSTEST_SRC tlstest.c) | ||
| 351 | check_function_exists(pipe2 HAVE_PIPE2) | ||
| 352 | if(HAVE_PIPE2) | ||
| 353 | add_definitions(-DHAVE_PIPE2) | ||
| 354 | else() | ||
| 355 | set(TLSTEST_SRC ${TLSTEST_SRC} compat/pipe2.c) | ||
| 356 | endif() | ||
| 357 | |||
| 358 | add_executable(tlstest ${TLSTEST_SRC}) | ||
| 350 | target_link_libraries(tlstest ${TESTS_LIBS}) | 359 | target_link_libraries(tlstest ${TESTS_LIBS}) |
| 351 | if(NOT MSVC) | 360 | if(NOT MSVC) |
| 352 | add_test(tlstest ${CMAKE_CURRENT_SOURCE_DIR}/tlstest.sh) | 361 | add_test(tlstest ${CMAKE_CURRENT_SOURCE_DIR}/tlstest.sh) |
diff --git a/tests/Makefile.am b/tests/Makefile.am index ab1f2f1..57347d6 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am | |||
| @@ -337,6 +337,9 @@ if !HOST_WIN | |||
| 337 | TESTS += tlstest.sh | 337 | TESTS += tlstest.sh |
| 338 | check_PROGRAMS += tlstest | 338 | check_PROGRAMS += tlstest |
| 339 | tlstest_SOURCES = tlstest.c | 339 | tlstest_SOURCES = tlstest.c |
| 340 | if !HAVE_PIPE2 | ||
| 341 | tlstest_SOURCES += compat/pipe2.c | ||
| 342 | endif | ||
| 340 | EXTRA_DIST += tlstest.sh tlstest.bat | 343 | EXTRA_DIST += tlstest.sh tlstest.bat |
| 341 | endif | 344 | endif |
| 342 | 345 | ||
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 | } | ||
