include pipe2/socketpair compat for macOS

This commit is contained in:
Brent Cook 2017-01-15 04:30:41 -06:00 committed by Brent Cook
parent fa20dae329
commit dfb6b11e5a
6 changed files with 83 additions and 2 deletions

View File

@ -8,3 +8,10 @@
#else
#include <win32netcompat.h>
#endif
#if !defined(SOCK_NONBLOCK) || !defined(SOCK_CLOEXEC)
#define SOCK_CLOEXEC 0x8000 /* set FD_CLOEXEC */
#define SOCK_NONBLOCK 0x4000 /* set O_NONBLOCK */
int bsd_socketpair(int domain, int type, int protocol, int socket_vector[2]);
#define socketpair(d,t,p,sv) bsd_socketpair(d,t,p,sv)
#endif

View File

@ -40,4 +40,8 @@ int getentropy(void *buf, size_t buflen);
#define pledge(request, paths) 0
#ifndef HAVE_PIPE2
int pipe2(int fildes[2], int flags);
#endif
#endif

View File

@ -20,10 +20,12 @@ AM_CONDITIONAL([HAVE_TIMEGM], [test "x$ac_cv_func_timegm" = xyes])
])
AC_DEFUN([CHECK_SYSCALL_COMPAT], [
AC_CHECK_FUNCS([accept4 pledge poll])
AC_CHECK_FUNCS([accept4 pipe2 pledge poll socketpair])
AM_CONDITIONAL([HAVE_ACCEPT4], [test "x$ac_cv_func_accept4" = xyes])
AM_CONDITIONAL([HAVE_PIPE2], [test "x$ac_cv_func_pipe2" = xyes])
AM_CONDITIONAL([HAVE_PLEDGE], [test "x$ac_cv_func_pledge" = xyes])
AM_CONDITIONAL([HAVE_POLL], [test "x$ac_cv_func_poll" = xyes])
AM_CONDITIONAL([HAVE_SOCKETPAIR], [test "x$ac_cv_func_socketpair" = xyes])
])
AC_DEFUN([CHECK_B64_NTOP], [

View File

@ -346,7 +346,16 @@ add_test(timingsafe timingsafe)
# tlstest
if(NOT CMAKE_HOST_WIN32 AND NOT CMAKE_SYSTEM_NAME MATCHES "MINGW")
add_executable(tlstest tlstest.c)
set(TLSTEST_SRC tlstest.c)
check_function_exists(pipe2 HAVE_PIPE2)
if(HAVE_PIPE2)
add_definitions(-DHAVE_PIPE2)
else()
set(TLSTEST_SRC ${TLSTEST_SRC} compat/pipe2.c)
endif()
add_executable(tlstest ${TLSTEST_SRC})
target_link_libraries(tlstest ${TESTS_LIBS})
if(NOT MSVC)
add_test(tlstest ${CMAKE_CURRENT_SOURCE_DIR}/tlstest.sh)

View File

@ -337,6 +337,9 @@ if !HOST_WIN
TESTS += tlstest.sh
check_PROGRAMS += tlstest
tlstest_SOURCES = tlstest.c
if !HAVE_PIPE2
tlstest_SOURCES += compat/pipe2.c
endif
EXTRA_DIST += tlstest.sh tlstest.bat
endif

56
tests/compat/pipe2.c Normal file
View File

@ -0,0 +1,56 @@
/*
* Public domain
*/
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#undef socketpair
static int setfd(int fd, int flag)
{
int flags = fcntl(fd, F_GETFD);
flags |= flag;
return fcntl(fd, F_SETFD, flags);
}
static int setfl(int fd, int flag)
{
int flags = fcntl(fd, F_GETFL);
flags |= flag;
return fcntl(fd, F_SETFL, flags);
}
int pipe2(int fildes[2], int flags)
{
int rc = pipe(fildes);
if (rc == 0) {
if (flags & O_NONBLOCK) {
setfl(fildes[0], O_NONBLOCK);
setfl(fildes[1], O_NONBLOCK);
}
if (flags & O_CLOEXEC) {
setfd(fildes[0], FD_CLOEXEC);
setfd(fildes[1], FD_CLOEXEC);
}
}
return rc;
}
int bsd_socketpair(int domain, int type, int protocol, int socket_vector[2])
{
int flags = type & ~0xf;
type &= 0xf;
int rc = socketpair(domain, type, protocol, socket_vector);
if (rc == 0) {
if (flags & SOCK_NONBLOCK) {
setfl(socket_vector[0], O_NONBLOCK);
setfl(socket_vector[1], O_NONBLOCK);
}
if (flags & SOCK_CLOEXEC) {
setfd(socket_vector[0], FD_CLOEXEC);
setfd(socket_vector[1], FD_CLOEXEC);
}
}
return rc;
}