Skip to content

Commit ff9a072

Browse files
committedNov 21, 2014
Extend PipePosix with child_processes_inherit support - to control whether pipe handles should be inherited by a child process.
http://reviews.llvm.org/D6348 llvm-svn: 222541
1 parent ce5a26b commit ff9a072

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed
 

‎lldb/include/lldb/Host/posix/PipePosix.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Pipe
3636
~Pipe();
3737

3838
bool
39-
Open();
39+
Open(bool child_processes_inherit = false);
4040

4141
bool
4242
IsValid() const;

‎lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ ConnectionFileDescriptor::OpenCommandPipe()
9696

9797
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
9898
// Make the command file descriptor here:
99-
if (!m_pipe.Open())
99+
if (!m_pipe.Open(m_child_processes_inherit))
100100
{
101101
if (log)
102102
log->Printf("%p ConnectionFileDescriptor::OpenCommandPipe () - could not make pipe: %s", static_cast<void *>(this),

‎lldb/source/Host/posix/PipePosix.cpp

+38-1
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,33 @@
1010
#include "lldb/Host/posix/PipePosix.h"
1111

1212
#include <unistd.h>
13+
#include <fcntl.h>
1314

1415
using namespace lldb_private;
1516

1617
int Pipe::kInvalidDescriptor = -1;
1718

1819
enum PIPES { READ, WRITE }; // Constants 0 and 1 for READ and WRITE
1920

21+
// pipe2 is supported by Linux, FreeBSD v10 and higher.
22+
// TODO: Add more platforms that support pipe2.
23+
#define PIPE2_SUPPORTED defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD__ >= 10)
24+
25+
namespace
26+
{
27+
28+
#if defined(FD_CLOEXEC) && !PIPE2_SUPPORTED
29+
bool SetCloexecFlag(int fd)
30+
{
31+
int flags = ::fcntl(fd, F_GETFD);
32+
if (flags == -1)
33+
return false;
34+
return (::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == 0);
35+
}
36+
#endif
37+
38+
}
39+
2040
Pipe::Pipe()
2141
{
2242
m_fds[READ] = Pipe::kInvalidDescriptor;
@@ -29,13 +49,30 @@ Pipe::~Pipe()
2949
}
3050

3151
bool
32-
Pipe::Open()
52+
Pipe::Open(bool child_processes_inherit)
3353
{
3454
if (IsValid())
3555
return true;
3656

57+
#if PIPE2_SUPPORTED
58+
if (::pipe2(m_fds, (child_processes_inherit) ? 0 : O_CLOEXEC) == 0)
59+
return true;
60+
#else
3761
if (::pipe(m_fds) == 0)
62+
{
63+
#ifdef FD_CLOEXEC
64+
if (!child_processes_inherit)
65+
{
66+
if (!SetCloexecFlag(m_fds[0]) || !SetCloexecFlag(m_fds[1]))
67+
{
68+
Close();
69+
return false;
70+
}
71+
}
72+
#endif
3873
return true;
74+
}
75+
#endif
3976

4077
m_fds[READ] = Pipe::kInvalidDescriptor;
4178
m_fds[WRITE] = Pipe::kInvalidDescriptor;

0 commit comments

Comments
 (0)