use closefrom() where available

e.g. on Linux via libbsd - or on BSDs where the symbol exists
This commit is contained in:
Georg Sauthoff 2016-08-26 22:25:48 +02:00
parent 064e38eae9
commit 3c28143a75
3 changed files with 22 additions and 5 deletions

View file

@ -5,6 +5,17 @@ project(somake C CXX)
include(CheckFunctionExists) include(CheckFunctionExists)
check_function_exists(wcsdup HAVE_WCSDUP) 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) configure_file(config.h.in config.h)
add_library(mksh STATIC 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}/include
${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}
${BSD_INCLUDE_DIRS}
) )
target_link_libraries(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME}
mksh vroot pthread mksh vroot pthread ${BSD_LIBRARIES}
) )
# default install prefix is /usr/local # default install prefix is /usr/local

View file

@ -3,5 +3,6 @@
#define SOMAKE_CONFIG_H #define SOMAKE_CONFIG_H
#cmakedefine HAVE_WCSDUP 1 #cmakedefine HAVE_WCSDUP 1
#cmakedefine HAVE_CLOSEFROM 1
#endif #endif

View file

@ -51,9 +51,15 @@
#include <sys/wait.h> /* wait() */ #include <sys/wait.h> /* wait() */
#include <ulimit.h> /* ulimit() */ #include <ulimit.h> /* ulimit() */
#include <unistd.h> /* close(), dup2() */ #include <unistd.h> /* close(), dup2() */
// closefrom only available on Solaris/BSD
#include <stdlib.h> /* closefrom() */ #include <stdlib.h> /* closefrom() */
#ifdef __linux
#include <bsd/unistd.h>
#endif
#include <libintl.h> #include <libintl.h>
#include <config.h>
/* /*
* typedefs & structs * typedefs & structs
*/ */
@ -93,12 +99,10 @@ redirect_io(char *stdout_file, char *stderr_file)
{ {
int i; int i;
#ifdef __sun #ifdef HAVE_CLOSEFROM
(void) closefrom(3); (void) closefrom(3);
#else #else
// XXX not available on Linux #warning "Compiling without closefrom ... consider linking libbsd ..."
// if necessary, could provide a compatibility function that
// iterates over /proc/$(getpid())/fd ...
#endif #endif
if ((i = my_open(stdout_file, if ((i = my_open(stdout_file,
O_WRONLY | O_CREAT | O_TRUNC | O_DSYNC, O_WRONLY | O_CREAT | O_TRUNC | O_DSYNC,