summaryrefslogtreecommitdiffstats
path: root/lldb/source/Host/posix/PipePosix.cpp
blob: 414fcc68edbb26bc850b80f5763f9a5ae271bd0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//===-- PipePosix.cpp -------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Host/posix/PipePosix.h"

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>

using namespace lldb;
using namespace lldb_private;

int PipePosix::kInvalidDescriptor = -1;

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

// pipe2 is supported by Linux, FreeBSD v10 and higher.
// TODO: Add more platforms that support pipe2.
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD__ >= 10)
#define PIPE2_SUPPORTED 1
#else
#define PIPE2_SUPPORTED 0
#endif

namespace
{

#if defined(FD_CLOEXEC) && !PIPE2_SUPPORTED
bool SetCloexecFlag(int fd)
{
    int flags = ::fcntl(fd, F_GETFD);
    if (flags == -1)
        return false;
    return (::fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == 0);
}
#endif

}

PipePosix::PipePosix()
{
    m_fds[READ] = PipePosix::kInvalidDescriptor;
    m_fds[WRITE] = PipePosix::kInvalidDescriptor;
}

PipePosix::~PipePosix()
{
    Close();
}

Error
PipePosix::CreateNew(bool child_processes_inherit)
{
    Error error;
    if (CanRead() || CanWrite())
    {
        error.SetError(EINVAL, eErrorTypePOSIX);
        return error;
    }

#if PIPE2_SUPPORTED
    if (::pipe2(m_fds, (child_processes_inherit) ? 0 : O_CLOEXEC) == 0)
        return error;
#else
    if (::pipe(m_fds) == 0)
    {
#ifdef FD_CLOEXEC
        if (!child_processes_inherit)
        {
            if (!SetCloexecFlag(m_fds[0]) || !SetCloexecFlag(m_fds[1]))
            {
                error.SetErrorToErrno();
                Close();
                return error;
            }
        }
#endif
        return error;
    }
#endif

    m_fds[READ] = PipePosix::kInvalidDescriptor;
    m_fds[WRITE] = PipePosix::kInvalidDescriptor;
    error.SetErrorToErrno();
    return error;
}

Error
PipePosix::CreateNew(llvm::StringRef name, bool child_process_inherit)
{
    Error error;
    if (CanRead() || CanWrite())
        error.SetErrorString("Pipe is already opened");
    else if (name.empty())
        error.SetErrorString("Cannot create named pipe with empty name.");
    else
        error.SetErrorString("Not implemented");
    return error;
}

Error
PipePosix::OpenAsReader(llvm::StringRef name, bool child_process_inherit)
{
    Error error;
    if (CanRead() || CanWrite())
        error.SetErrorString("Pipe is already opened");
    else if (name.empty())
        error.SetErrorString("Cannot open named pipe with empty name.");
    else
        error.SetErrorString("Not implemented");
    return error;
}

Error
PipePosix::OpenAsWriter(llvm::StringRef name, bool child_process_inherit)
{
    Error error;
    if (CanRead() || CanWrite())
        error.SetErrorString("Pipe is already opened");
    else if (name.empty())
        error.SetErrorString("Cannot create named pipe with empty name.");
    else
        error.SetErrorString("Not implemented");
    return error;
}

int
PipePosix::GetReadFileDescriptor() const
{
    return m_fds[READ];
}

int
PipePosix::GetWriteFileDescriptor() const
{
    return m_fds[WRITE];
}

int
PipePosix::ReleaseReadFileDescriptor()
{
    const int fd = m_fds[READ];
    m_fds[READ] = PipePosix::kInvalidDescriptor;
    return fd;
}

int
PipePosix::ReleaseWriteFileDescriptor()
{
    const int fd = m_fds[WRITE];
    m_fds[WRITE] = PipePosix::kInvalidDescriptor;
    return fd;
}

void
PipePosix::Close()
{
    CloseReadFileDescriptor();
    CloseWriteFileDescriptor();
}

bool
PipePosix::CanRead() const
{
    return m_fds[READ] != PipePosix::kInvalidDescriptor;
}

bool
PipePosix::CanWrite() const
{
    return m_fds[WRITE] != PipePosix::kInvalidDescriptor;
}

void
PipePosix::CloseReadFileDescriptor()
{
    if (CanRead())
    {
        int err;
        err = close(m_fds[READ]);
        m_fds[READ] = PipePosix::kInvalidDescriptor;
    }
}

void
PipePosix::CloseWriteFileDescriptor()
{
    if (CanWrite())
    {
        int err;
        err = close(m_fds[WRITE]);
        m_fds[WRITE] = PipePosix::kInvalidDescriptor;
    }
}

Error
PipePosix::Read(void *buf, size_t num_bytes, size_t &bytes_read)
{
    bytes_read = 0;
    Error error;

    if (CanRead())
    {
        const int fd = GetReadFileDescriptor();
        int result = read(fd, buf, num_bytes);
        if (result >= 0)
            bytes_read = result;
        else
            error.SetErrorToErrno();
    }
    else
        error.SetError(EINVAL, eErrorTypePOSIX);

    return error;
}

Error
PipePosix::ReadWithTimeout(void *buf, size_t num_bytes, const std::chrono::milliseconds &duration, size_t &bytes_read)
{
    bytes_read = 0;
    Error error;
    error.SetErrorString("Not implemented");
    return error;
}

Error
PipePosix::Write(const void *buf, size_t num_bytes, size_t &bytes_written)
{
    bytes_written = 0;
    Error error;

    if (CanWrite())
    {
        const int fd = GetWriteFileDescriptor();
        int result = write(fd, buf, num_bytes);
        if (result >= 0)
            bytes_written = result;
        else
            error.SetErrorToErrno();
    }
    else
        error.SetError(EINVAL, eErrorTypePOSIX);

    return error;
}
OpenPOWER on IntegriCloud