1 //===-- PipePosix.cpp -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Host/posix/PipePosix.h"
11 
12 #include <unistd.h>
13 
14 using namespace lldb_private;
15 
16 int Pipe::kInvalidDescriptor = -1;
17 
18 enum PIPES { READ, WRITE }; // Constants 0 and 1 for READ and WRITE
19 
20 Pipe::Pipe()
21 {
22     m_fds[READ] = Pipe::kInvalidDescriptor;
23     m_fds[WRITE] = Pipe::kInvalidDescriptor;
24 }
25 
26 Pipe::~Pipe()
27 {
28     Close();
29 }
30 
31 bool
32 Pipe::Open()
33 {
34     if (IsValid())
35         return true;
36 
37     if (::pipe(m_fds) == 0)
38         return true;
39 
40     m_fds[READ] = Pipe::kInvalidDescriptor;
41     m_fds[WRITE] = Pipe::kInvalidDescriptor;
42     return false;
43 }
44 
45 int
46 Pipe::GetReadFileDescriptor() const
47 {
48     return m_fds[READ];
49 }
50 
51 int
52 Pipe::GetWriteFileDescriptor() const
53 {
54     return m_fds[WRITE];
55 }
56 
57 int
58 Pipe::ReleaseReadFileDescriptor()
59 {
60     const int fd = m_fds[READ];
61     m_fds[READ] = Pipe::kInvalidDescriptor;
62     return fd;
63 }
64 
65 int
66 Pipe::ReleaseWriteFileDescriptor()
67 {
68     const int fd = m_fds[WRITE];
69     m_fds[WRITE] = Pipe::kInvalidDescriptor;
70     return fd;
71 }
72 
73 void
74 Pipe::Close()
75 {
76     CloseReadFileDescriptor();
77     CloseWriteFileDescriptor();
78 }
79 
80 bool
81 Pipe::ReadDescriptorIsValid() const
82 {
83     return m_fds[READ] != Pipe::kInvalidDescriptor;
84 }
85 
86 bool
87 Pipe::WriteDescriptorIsValid() const
88 {
89     return m_fds[WRITE] != Pipe::kInvalidDescriptor;
90 }
91 
92 bool
93 Pipe::IsValid() const
94 {
95     return ReadDescriptorIsValid() && WriteDescriptorIsValid();
96 }
97 
98 bool
99 Pipe::CloseReadFileDescriptor()
100 {
101     if (ReadDescriptorIsValid())
102     {
103         int err;
104         err = close(m_fds[READ]);
105         m_fds[READ] = Pipe::kInvalidDescriptor;
106         return err == 0;
107     }
108     return true;
109 }
110 
111 bool
112 Pipe::CloseWriteFileDescriptor()
113 {
114     if (WriteDescriptorIsValid())
115     {
116         int err;
117         err = close(m_fds[WRITE]);
118         m_fds[WRITE] = Pipe::kInvalidDescriptor;
119         return err == 0;
120     }
121     return true;
122 }
123 
124 
125 size_t
126 Pipe::Read (void *buf, size_t num_bytes)
127 {
128     if (ReadDescriptorIsValid())
129     {
130         const int fd = GetReadFileDescriptor();
131         return read (fd, buf, num_bytes);
132     }
133     return 0; // Return 0 since errno won't be set if we didn't call read
134 }
135 
136 size_t
137 Pipe::Write (const void *buf, size_t num_bytes)
138 {
139     if (WriteDescriptorIsValid())
140     {
141         const int fd = GetWriteFileDescriptor();
142         return write (fd, buf, num_bytes);
143     }
144     return 0; // Return 0 since errno won't be set if we didn't call write
145 }
146