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 @@ #else #include #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 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); #define pledge(request, paths) 0 +#ifndef HAVE_PIPE2 +int pipe2(int fildes[2], int flags); +#endif + #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]) ]) 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], [ 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) # 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) 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 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 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 @@ +/* + * Public domain + */ + +#include +#include +#include +#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; +}