1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-27 12:42:41 +00:00

Emulate O_CLOEXEC on systems that don't support it

This commit is contained in:
Pavel Labath 2011-10-01 22:18:25 +02:00
parent 031e79e77e
commit 0493ba853c
4 changed files with 43 additions and 1 deletions

View File

@ -34,6 +34,9 @@ check_include_files(dirent.h HAVE_DIRENT_H)
# Check for some functions # Check for some functions
check_function_exists(strndup HAVE_STRNDUP) check_function_exists(strndup HAVE_STRNDUP)
check_symbol_exists(pipe2 "unistd.h" HAVE_PIPE2)
check_symbol_exists(O_CLOEXEC "fcntl.h" HAVE_O_CLOEXEC)
AC_SEARCH_LIBS(clock_gettime "time.h" CLOCK_GETTIME_LIB "rt") AC_SEARCH_LIBS(clock_gettime "time.h" CLOCK_GETTIME_LIB "rt")
if(NOT CLOCK_GETTIME_LIB) if(NOT CLOCK_GETTIME_LIB)
message(FATAL_ERROR "clock_gettime not found.") message(FATAL_ERROR "clock_gettime not found.")

View File

@ -33,6 +33,9 @@
#cmakedefine HAVE_FOPENCOOKIE 1 #cmakedefine HAVE_FOPENCOOKIE 1
#cmakedefine HAVE_FUNOPEN 1 #cmakedefine HAVE_FUNOPEN 1
#cmakedefine HAVE_PIPE2 1
#cmakedefine HAVE_O_CLOEXEC 1
#cmakedefine BUILD_X11 1 #cmakedefine BUILD_X11 1
#cmakedefine OWN_WINDOW 1 #cmakedefine OWN_WINDOW 1

View File

@ -27,6 +27,38 @@
#include <string.h> #include <string.h>
#if !defined(HAVE_PIPE2) || !defined(HAVE_O_CLOEXEC)
#include <fcntl.h>
namespace {
int pipe2_emulate(int pipefd[2], int flags)
{
if(pipe(pipefd) == -1)
return -1;
if(flags & O_CLOEXEC) {
// we emulate O_CLOEXEC if the system does not have it
// not very thread-safe, but at least it works
for(int i = 0; i < 2; ++i) {
int r = fcntl(pipefd[i], F_GETFD);
if(r == -1)
return -1;
if(fcntl(pipefd[i], F_SETFD, r | FD_CLOEXEC) == -1)
return -1;
}
}
return 0;
}
int (* const pipe2_ptr)(int[2], int) = &pipe2_emulate;
}
#else
int (* const pipe2_ptr)(int[2], int) = &pipe2;
#endif
std::string strerror_r(int errnum) std::string strerror_r(int errnum)
{ {
char buf[100]; char buf[100];
@ -36,7 +68,7 @@ std::string strerror_r(int errnum)
std::pair<int, int> pipe2(int flags) std::pair<int, int> pipe2(int flags)
{ {
int fd[2]; int fd[2];
if(pipe2(fd, flags) == -1) if(pipe2_ptr(fd, flags) == -1)
throw errno_error("pipe2"); throw errno_error("pipe2");
else else
return std::pair<int, int>(fd[0], fd[1]); return std::pair<int, int>(fd[0], fd[1]);

View File

@ -24,7 +24,11 @@
#ifndef CPPWRAP_HH #ifndef CPPWRAP_HH
#define CPPWRAP_HH #define CPPWRAP_HH
#ifdef HAVE_O_CLOEXEC
#include <fcntl.h> #include <fcntl.h>
#else
enum { O_CLOEXEC = 02000000 };
#endif
#include <cerrno> #include <cerrno>
#include <stdexcept> #include <stdexcept>