summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Toub <stoub@microsoft.com>2017-05-31 23:05:06 -0400
committerGitHub <noreply@github.com>2017-05-31 23:05:06 -0400
commit05732e6e9d79b0f65490c3bac5698cf8fe8ac9a4 (patch)
tree4bff55b9edc1e5f7413e8ac2ec39593c1032b5e6
parentbdc3f84b47d4cd93b6cc6771a52f9c57249ceddb (diff)
parent90d253f65200f1ab46c42a290098b91bf9544535 (diff)
downloadcoreclr-05732e6e9d79b0f65490c3bac5698cf8fe8ac9a4.tar.gz
coreclr-05732e6e9d79b0f65490c3bac5698cf8fe8ac9a4.tar.bz2
coreclr-05732e6e9d79b0f65490c3bac5698cf8fe8ac9a4.zip
Merge pull request #11988 from stephentoub/fix_fds
Ensure several runtime file descriptors are CLOEXEC
-rw-r--r--src/pal/src/config.h.in1
-rw-r--r--src/pal/src/configure.cmake1
-rw-r--r--src/pal/src/file/file.cpp6
-rw-r--r--src/pal/src/map/map.cpp6
-rw-r--r--src/pal/src/synchmgr/synchmanager.cpp12
-rw-r--r--src/pal/src/thread/threadsusp.cpp12
6 files changed, 30 insertions, 8 deletions
diff --git a/src/pal/src/config.h.in b/src/pal/src/config.h.in
index 48677df750..03513a1264 100644
--- a/src/pal/src/config.h.in
+++ b/src/pal/src/config.h.in
@@ -61,6 +61,7 @@
#cmakedefine01 HAS_SYSV_SEMAPHORES
#cmakedefine01 HAS_PTHREAD_MUTEXES
#cmakedefine01 HAVE_TTRACE
+#cmakedefine01 HAVE_PIPE2
#cmakedefine01 HAVE_SCHED_GETAFFINITY
#cmakedefine HAVE_UNW_GET_SAVE_LOC
#cmakedefine HAVE_UNW_GET_ACCESSORS
diff --git a/src/pal/src/configure.cmake b/src/pal/src/configure.cmake
index 6cf77054e4..d305081f87 100644
--- a/src/pal/src/configure.cmake
+++ b/src/pal/src/configure.cmake
@@ -100,6 +100,7 @@ check_function_exists(directio HAVE_DIRECTIO)
check_function_exists(semget HAS_SYSV_SEMAPHORES)
check_function_exists(pthread_mutex_init HAS_PTHREAD_MUTEXES)
check_function_exists(ttrace HAVE_TTRACE)
+check_function_exists(pipe2 HAVE_PIPE2)
set(CMAKE_REQUIRED_LIBRARIES unwind unwind-generic)
check_cxx_source_compiles("
#include <libunwind.h>
diff --git a/src/pal/src/file/file.cpp b/src/pal/src/file/file.cpp
index a4ad20db32..feec65531c 100644
--- a/src/pal/src/file/file.cpp
+++ b/src/pal/src/file/file.cpp
@@ -4056,14 +4056,14 @@ CorUnix::InternalCreatePipe(
/* enable close-on-exec for both pipes; if one gets passed to CreateProcess
it will be "uncloseonexeced" in order to be inherited */
- if(-1 == fcntl(readWritePipeDes[0],F_SETFD,1))
+ if(-1 == fcntl(readWritePipeDes[0],F_SETFD,FD_CLOEXEC))
{
ASSERT("can't set close-on-exec flag; fcntl() failed. errno is %d "
"(%s)\n", errno, strerror(errno));
palError = ERROR_INTERNAL_ERROR;
goto InternalCreatePipeExit;
}
- if(-1 == fcntl(readWritePipeDes[1],F_SETFD,1))
+ if(-1 == fcntl(readWritePipeDes[1],F_SETFD,FD_CLOEXEC))
{
ASSERT("can't set close-on-exec flag; fcntl() failed. errno is %d "
"(%s)\n", errno, strerror(errno));
@@ -4564,7 +4564,7 @@ static HANDLE init_std_handle(HANDLE * pStd, FILE *stream)
/* duplicate the FILE *, so that we can fclose() in FILECloseHandle without
closing the original */
- new_fd = dup(fileno(stream));
+ new_fd = fcntl(fileno(stream), F_DUPFD_CLOEXEC, 0); // dup, but with CLOEXEC
if(-1 == new_fd)
{
ERROR("dup() failed; errno is %d (%s)\n", errno, strerror(errno));
diff --git a/src/pal/src/map/map.cpp b/src/pal/src/map/map.cpp
index f26293bea3..b8ffc84db4 100644
--- a/src/pal/src/map/map.cpp
+++ b/src/pal/src/map/map.cpp
@@ -246,7 +246,7 @@ FileMappingInitializationRoutine(
pProcessLocalData->UnixFd = InternalOpen(
pImmutableData->szFileName,
- MAPProtectionToFileOpenFlags(pImmutableData->flProtect)
+ MAPProtectionToFileOpenFlags(pImmutableData->flProtect) | O_CLOEXEC
);
if (-1 == pProcessLocalData->UnixFd)
@@ -510,7 +510,7 @@ CorUnix::InternalCreateFileMapping(
#if HAVE_MMAP_DEV_ZERO
- UnixFd = InternalOpen(pImmutableData->szFileName, O_RDWR);
+ UnixFd = InternalOpen(pImmutableData->szFileName, O_RDWR | O_CLOEXEC);
if ( -1 == UnixFd )
{
ERROR( "Unable to open the file.\n");
@@ -587,7 +587,7 @@ CorUnix::InternalCreateFileMapping(
// information, though...
//
- UnixFd = dup(pFileLocalData->unix_fd);
+ UnixFd = fcntl(pFileLocalData->unix_fd, F_DUPFD_CLOEXEC, 0); // dup, but with CLOEXEC
if (-1 == UnixFd)
{
ERROR( "Unable to duplicate the Unix file descriptor!\n" );
diff --git a/src/pal/src/synchmgr/synchmanager.cpp b/src/pal/src/synchmgr/synchmanager.cpp
index d836a177bb..73b5644dbd 100644
--- a/src/pal/src/synchmgr/synchmanager.cpp
+++ b/src/pal/src/synchmgr/synchmanager.cpp
@@ -3525,12 +3525,22 @@ namespace CorUnix
}
#else // !CORECLR
int rgiPipe[] = { -1, -1 };
- if (pipe(rgiPipe) == -1)
+ int pipeRv =
+#if HAVE_PIPE2
+ pipe2(rgiPipe, O_CLOEXEC);
+#else
+ pipe(rgiPipe);
+#endif // HAVE_PIPE2
+ if (pipeRv == -1)
{
ERROR("Unable to create the process pipe\n");
fRet = false;
goto CPP_exit;
}
+#if !HAVE_PIPE2
+ fcntl(rgiPipe[0], F_SETFD, FD_CLOEXEC); // make pipe non-inheritable, if possible
+ fcntl(rgiPipe[1], F_SETFD, FD_CLOEXEC);
+#endif // !HAVE_PIPE2
#endif // !CORECLR
#if HAVE_KQUEUE && !HAVE_BROKEN_FIFO_KEVENT
diff --git a/src/pal/src/thread/threadsusp.cpp b/src/pal/src/thread/threadsusp.cpp
index c7787bef68..f8a435c022 100644
--- a/src/pal/src/thread/threadsusp.cpp
+++ b/src/pal/src/thread/threadsusp.cpp
@@ -74,11 +74,21 @@ CThreadSuspensionInfo::InternalSuspendNewThreadFromData(
ReleaseSuspensionLock(pThread);
int pipe_descs[2];
- if (pipe(pipe_descs) == -1)
+ int pipeRv =
+#if HAVE_PIPE2
+ pipe2(pipe_descs, O_CLOEXEC);
+#else
+ pipe(pipe_descs);
+#endif // HAVE_PIPE2
+ if (pipeRv == -1)
{
ERROR("pipe() failed! error is %d (%s)\n", errno, strerror(errno));
return ERROR_NOT_ENOUGH_MEMORY;
}
+#if !HAVE_PIPE2
+ fcntl(pipe_descs[0], F_SETFD, FD_CLOEXEC); // make pipe non-inheritable, if possible
+ fcntl(pipe_descs[1], F_SETFD, FD_CLOEXEC);
+#endif // !HAVE_PIPE2
// [0] is the read end of the pipe, and [1] is the write end.
pThread->suspensionInfo.SetBlockingPipe(pipe_descs[1]);