From 3c28143a75340567eef9fe23f978782f710d2e90 Mon Sep 17 00:00:00 2001 From: Georg Sauthoff Date: Fri, 26 Aug 2016 22:25:48 +0200 Subject: [PATCH] use closefrom() where available e.g. on Linux via libbsd - or on BSDs where the symbol exists --- CMakeLists.txt | 14 +++++++++++++- config.h.in | 1 + lib/mksh/dosys.cc | 12 ++++++++---- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3ff0469..9d75bf1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,17 @@ project(somake C CXX) include(CheckFunctionExists) check_function_exists(wcsdup HAVE_WCSDUP) + +if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + find_package(PkgConfig) + pkg_search_module(BSD libbsd) + if (BSD_LIBRARIES) + set(HAVE_CLOSEFROM 1) + endif() +else() + check_function_exists(closefrom HAVE_CLOSEFROM) +endif() + configure_file(config.h.in config.h) add_library(mksh STATIC @@ -37,10 +48,11 @@ set_property(TARGET ${PROJECT_NAME} mksh vroot PROPERTY INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} + ${BSD_INCLUDE_DIRS} ) target_link_libraries(${PROJECT_NAME} - mksh vroot pthread + mksh vroot pthread ${BSD_LIBRARIES} ) # default install prefix is /usr/local diff --git a/config.h.in b/config.h.in index 07c409d..a7d1b35 100644 --- a/config.h.in +++ b/config.h.in @@ -3,5 +3,6 @@ #define SOMAKE_CONFIG_H #cmakedefine HAVE_WCSDUP 1 +#cmakedefine HAVE_CLOSEFROM 1 #endif diff --git a/lib/mksh/dosys.cc b/lib/mksh/dosys.cc index 25cd550..68c9ca2 100644 --- a/lib/mksh/dosys.cc +++ b/lib/mksh/dosys.cc @@ -51,9 +51,15 @@ #include /* wait() */ #include /* ulimit() */ #include /* close(), dup2() */ +// closefrom only available on Solaris/BSD #include /* closefrom() */ +#ifdef __linux + #include +#endif #include +#include + /* * typedefs & structs */ @@ -93,12 +99,10 @@ redirect_io(char *stdout_file, char *stderr_file) { int i; -#ifdef __sun +#ifdef HAVE_CLOSEFROM (void) closefrom(3); #else - // XXX not available on Linux - // if necessary, could provide a compatibility function that - // iterates over /proc/$(getpid())/fd ... + #warning "Compiling without closefrom ... consider linking libbsd ..." #endif if ((i = my_open(stdout_file, O_WRONLY | O_CREAT | O_TRUNC | O_DSYNC,