1 //===-- ConnectionFileDescriptorPosix.cpp ---------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #if defined(__APPLE__)
10 // Enable this special support for Apple builds where we can have unlimited
11 // select bounds. We tried switching to poll() and kqueue and we were panicing
12 // the kernel, so we have to stick with select for now.
13 #define _DARWIN_UNLIMITED_SELECT
14 #endif
15
16 #include "lldb/Host/posix/ConnectionFileDescriptorPosix.h"
17 #include "lldb/Host/Config.h"
18 #include "lldb/Host/Socket.h"
19 #include "lldb/Host/SocketAddress.h"
20 #include "lldb/Utility/SelectHelper.h"
21 #include "lldb/Utility/Timeout.h"
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28
29 #if LLDB_ENABLE_POSIX
30 #include <termios.h>
31 #include <unistd.h>
32 #endif
33
34 #include <memory>
35 #include <sstream>
36
37 #include "llvm/Support/Errno.h"
38 #include "llvm/Support/ErrorHandling.h"
39 #if defined(__APPLE__)
40 #include "llvm/ADT/SmallVector.h"
41 #endif
42 #include "lldb/Host/Host.h"
43 #include "lldb/Host/Socket.h"
44 #include "lldb/Host/common/TCPSocket.h"
45 #include "lldb/Host/common/UDPSocket.h"
46 #include "lldb/Utility/Log.h"
47 #include "lldb/Utility/StreamString.h"
48 #include "lldb/Utility/Timer.h"
49
50 using namespace lldb;
51 using namespace lldb_private;
52
53 const char *ConnectionFileDescriptor::LISTEN_SCHEME = "listen";
54 const char *ConnectionFileDescriptor::ACCEPT_SCHEME = "accept";
55 const char *ConnectionFileDescriptor::UNIX_ACCEPT_SCHEME = "unix-accept";
56 const char *ConnectionFileDescriptor::CONNECT_SCHEME = "connect";
57 const char *ConnectionFileDescriptor::TCP_CONNECT_SCHEME = "tcp-connect";
58 const char *ConnectionFileDescriptor::UDP_SCHEME = "udp";
59 const char *ConnectionFileDescriptor::UNIX_CONNECT_SCHEME = "unix-connect";
60 const char *ConnectionFileDescriptor::UNIX_ABSTRACT_CONNECT_SCHEME =
61 "unix-abstract-connect";
62 const char *ConnectionFileDescriptor::FD_SCHEME = "fd";
63 const char *ConnectionFileDescriptor::FILE_SCHEME = "file";
64
65 namespace {
66
GetURLAddress(llvm::StringRef url,llvm::StringRef scheme)67 llvm::Optional<llvm::StringRef> GetURLAddress(llvm::StringRef url,
68 llvm::StringRef scheme) {
69 if (!url.consume_front(scheme))
70 return llvm::None;
71 if (!url.consume_front("://"))
72 return llvm::None;
73 return url;
74 }
75 }
76
ConnectionFileDescriptor(bool child_processes_inherit)77 ConnectionFileDescriptor::ConnectionFileDescriptor(bool child_processes_inherit)
78 : Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
79 m_waiting_for_accept(false),
80 m_child_processes_inherit(child_processes_inherit) {
81 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION |
82 LIBLLDB_LOG_OBJECT));
83 LLDB_LOGF(log, "%p ConnectionFileDescriptor::ConnectionFileDescriptor ()",
84 static_cast<void *>(this));
85 }
86
ConnectionFileDescriptor(int fd,bool owns_fd)87 ConnectionFileDescriptor::ConnectionFileDescriptor(int fd, bool owns_fd)
88 : Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
89 m_waiting_for_accept(false), m_child_processes_inherit(false) {
90 m_write_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionWrite, owns_fd);
91 m_read_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionRead, false);
92
93 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION |
94 LIBLLDB_LOG_OBJECT));
95 LLDB_LOGF(log,
96 "%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = "
97 "%i, owns_fd = %i)",
98 static_cast<void *>(this), fd, owns_fd);
99 OpenCommandPipe();
100 }
101
ConnectionFileDescriptor(Socket * socket)102 ConnectionFileDescriptor::ConnectionFileDescriptor(Socket *socket)
103 : Connection(), m_pipe(), m_mutex(), m_shutting_down(false),
104 m_waiting_for_accept(false), m_child_processes_inherit(false) {
105 InitializeSocket(socket);
106 }
107
~ConnectionFileDescriptor()108 ConnectionFileDescriptor::~ConnectionFileDescriptor() {
109 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION |
110 LIBLLDB_LOG_OBJECT));
111 LLDB_LOGF(log, "%p ConnectionFileDescriptor::~ConnectionFileDescriptor ()",
112 static_cast<void *>(this));
113 Disconnect(nullptr);
114 CloseCommandPipe();
115 }
116
OpenCommandPipe()117 void ConnectionFileDescriptor::OpenCommandPipe() {
118 CloseCommandPipe();
119
120 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
121 // Make the command file descriptor here:
122 Status result = m_pipe.CreateNew(m_child_processes_inherit);
123 if (!result.Success()) {
124 LLDB_LOGF(log,
125 "%p ConnectionFileDescriptor::OpenCommandPipe () - could not "
126 "make pipe: %s",
127 static_cast<void *>(this), result.AsCString());
128 } else {
129 LLDB_LOGF(log,
130 "%p ConnectionFileDescriptor::OpenCommandPipe() - success "
131 "readfd=%d writefd=%d",
132 static_cast<void *>(this), m_pipe.GetReadFileDescriptor(),
133 m_pipe.GetWriteFileDescriptor());
134 }
135 }
136
CloseCommandPipe()137 void ConnectionFileDescriptor::CloseCommandPipe() {
138 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
139 LLDB_LOGF(log, "%p ConnectionFileDescriptor::CloseCommandPipe()",
140 static_cast<void *>(this));
141
142 m_pipe.Close();
143 }
144
IsConnected() const145 bool ConnectionFileDescriptor::IsConnected() const {
146 return (m_read_sp && m_read_sp->IsValid()) ||
147 (m_write_sp && m_write_sp->IsValid());
148 }
149
Connect(llvm::StringRef path,Status * error_ptr)150 ConnectionStatus ConnectionFileDescriptor::Connect(llvm::StringRef path,
151 Status *error_ptr) {
152 std::lock_guard<std::recursive_mutex> guard(m_mutex);
153 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
154 LLDB_LOGF(log, "%p ConnectionFileDescriptor::Connect (url = '%s')",
155 static_cast<void *>(this), path.str().c_str());
156
157 OpenCommandPipe();
158
159 if (!path.empty()) {
160 llvm::Optional<llvm::StringRef> addr;
161 if ((addr = GetURLAddress(path, LISTEN_SCHEME))) {
162 // listen://HOST:PORT
163 return SocketListenAndAccept(*addr, error_ptr);
164 } else if ((addr = GetURLAddress(path, ACCEPT_SCHEME))) {
165 // unix://SOCKNAME
166 return NamedSocketAccept(*addr, error_ptr);
167 } else if ((addr = GetURLAddress(path, UNIX_ACCEPT_SCHEME))) {
168 // unix://SOCKNAME
169 return NamedSocketAccept(*addr, error_ptr);
170 } else if ((addr = GetURLAddress(path, CONNECT_SCHEME))) {
171 return ConnectTCP(*addr, error_ptr);
172 } else if ((addr = GetURLAddress(path, TCP_CONNECT_SCHEME))) {
173 return ConnectTCP(*addr, error_ptr);
174 } else if ((addr = GetURLAddress(path, UDP_SCHEME))) {
175 return ConnectUDP(*addr, error_ptr);
176 } else if ((addr = GetURLAddress(path, UNIX_CONNECT_SCHEME))) {
177 // unix-connect://SOCKNAME
178 return NamedSocketConnect(*addr, error_ptr);
179 } else if ((addr = GetURLAddress(path, UNIX_ABSTRACT_CONNECT_SCHEME))) {
180 // unix-abstract-connect://SOCKNAME
181 return UnixAbstractSocketConnect(*addr, error_ptr);
182 }
183 #if LLDB_ENABLE_POSIX
184 else if ((addr = GetURLAddress(path, FD_SCHEME))) {
185 // Just passing a native file descriptor within this current process that
186 // is already opened (possibly from a service or other source).
187 int fd = -1;
188
189 if (!addr->getAsInteger(0, fd)) {
190 // We have what looks to be a valid file descriptor, but we should make
191 // sure it is. We currently are doing this by trying to get the flags
192 // from the file descriptor and making sure it isn't a bad fd.
193 errno = 0;
194 int flags = ::fcntl(fd, F_GETFL, 0);
195 if (flags == -1 || errno == EBADF) {
196 if (error_ptr)
197 error_ptr->SetErrorStringWithFormat("stale file descriptor: %s",
198 path.str().c_str());
199 m_read_sp.reset();
200 m_write_sp.reset();
201 return eConnectionStatusError;
202 } else {
203 // Don't take ownership of a file descriptor that gets passed to us
204 // since someone else opened the file descriptor and handed it to us.
205 // TODO: Since are using a URL to open connection we should
206 // eventually parse options using the web standard where we have
207 // "fd://123?opt1=value;opt2=value" and we can have an option be
208 // "owns=1" or "owns=0" or something like this to allow us to specify
209 // this. For now, we assume we must assume we don't own it.
210
211 std::unique_ptr<TCPSocket> tcp_socket;
212 tcp_socket = std::make_unique<TCPSocket>(fd, false, false);
213 // Try and get a socket option from this file descriptor to see if
214 // this is a socket and set m_is_socket accordingly.
215 int resuse;
216 bool is_socket =
217 !!tcp_socket->GetOption(SOL_SOCKET, SO_REUSEADDR, resuse);
218 if (is_socket) {
219 m_read_sp = std::move(tcp_socket);
220 m_write_sp = m_read_sp;
221 } else {
222 m_read_sp =
223 std::make_shared<NativeFile>(fd, File::eOpenOptionRead, false);
224 m_write_sp =
225 std::make_shared<NativeFile>(fd, File::eOpenOptionWrite, false);
226 }
227 m_uri = std::string(*addr);
228 return eConnectionStatusSuccess;
229 }
230 }
231
232 if (error_ptr)
233 error_ptr->SetErrorStringWithFormat("invalid file descriptor: \"%s\"",
234 path.str().c_str());
235 m_read_sp.reset();
236 m_write_sp.reset();
237 return eConnectionStatusError;
238 } else if ((addr = GetURLAddress(path, FILE_SCHEME))) {
239 std::string addr_str = addr->str();
240 // file:///PATH
241 int fd = llvm::sys::RetryAfterSignal(-1, ::open, addr_str.c_str(), O_RDWR);
242 if (fd == -1) {
243 if (error_ptr)
244 error_ptr->SetErrorToErrno();
245 return eConnectionStatusError;
246 }
247
248 if (::isatty(fd)) {
249 // Set up serial terminal emulation
250 struct termios options;
251 ::tcgetattr(fd, &options);
252
253 // Set port speed to maximum
254 ::cfsetospeed(&options, B115200);
255 ::cfsetispeed(&options, B115200);
256
257 // Raw input, disable echo and signals
258 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
259
260 // Make sure only one character is needed to return from a read
261 options.c_cc[VMIN] = 1;
262 options.c_cc[VTIME] = 0;
263
264 llvm::sys::RetryAfterSignal(-1, ::tcsetattr, fd, TCSANOW, &options);
265 }
266
267 int flags = ::fcntl(fd, F_GETFL, 0);
268 if (flags >= 0) {
269 if ((flags & O_NONBLOCK) == 0) {
270 flags |= O_NONBLOCK;
271 ::fcntl(fd, F_SETFL, flags);
272 }
273 }
274 m_read_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionRead, true);
275 m_write_sp = std::make_shared<NativeFile>(fd, File::eOpenOptionWrite, false);
276 return eConnectionStatusSuccess;
277 }
278 #endif
279 if (error_ptr)
280 error_ptr->SetErrorStringWithFormat("unsupported connection URL: '%s'",
281 path.str().c_str());
282 return eConnectionStatusError;
283 }
284 if (error_ptr)
285 error_ptr->SetErrorString("invalid connect arguments");
286 return eConnectionStatusError;
287 }
288
InterruptRead()289 bool ConnectionFileDescriptor::InterruptRead() {
290 size_t bytes_written = 0;
291 Status result = m_pipe.Write("i", 1, bytes_written);
292 return result.Success();
293 }
294
Disconnect(Status * error_ptr)295 ConnectionStatus ConnectionFileDescriptor::Disconnect(Status *error_ptr) {
296 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
297 LLDB_LOGF(log, "%p ConnectionFileDescriptor::Disconnect ()",
298 static_cast<void *>(this));
299
300 ConnectionStatus status = eConnectionStatusSuccess;
301
302 if (!IsConnected()) {
303 LLDB_LOGF(
304 log, "%p ConnectionFileDescriptor::Disconnect(): Nothing to disconnect",
305 static_cast<void *>(this));
306 return eConnectionStatusSuccess;
307 }
308
309 if (m_read_sp && m_read_sp->IsValid() &&
310 m_read_sp->GetFdType() == IOObject::eFDTypeSocket)
311 static_cast<Socket &>(*m_read_sp).PreDisconnect();
312
313 // Try to get the ConnectionFileDescriptor's mutex. If we fail, that is
314 // quite likely because somebody is doing a blocking read on our file
315 // descriptor. If that's the case, then send the "q" char to the command
316 // file channel so the read will wake up and the connection will then know to
317 // shut down.
318 std::unique_lock<std::recursive_mutex> locker(m_mutex, std::defer_lock);
319 if (!locker.try_lock()) {
320 if (m_pipe.CanWrite()) {
321 size_t bytes_written = 0;
322 Status result = m_pipe.Write("q", 1, bytes_written);
323 LLDB_LOGF(log,
324 "%p ConnectionFileDescriptor::Disconnect(): Couldn't get "
325 "the lock, sent 'q' to %d, error = '%s'.",
326 static_cast<void *>(this), m_pipe.GetWriteFileDescriptor(),
327 result.AsCString());
328 } else if (log) {
329 LLDB_LOGF(log,
330 "%p ConnectionFileDescriptor::Disconnect(): Couldn't get the "
331 "lock, but no command pipe is available.",
332 static_cast<void *>(this));
333 }
334 locker.lock();
335 }
336
337 // Prevents reads and writes during shutdown.
338 m_shutting_down = true;
339
340 Status error = m_read_sp->Close();
341 Status error2 = m_write_sp->Close();
342 if (error.Fail() || error2.Fail())
343 status = eConnectionStatusError;
344 if (error_ptr)
345 *error_ptr = error.Fail() ? error : error2;
346
347 // Close any pipes we were using for async interrupts
348 m_pipe.Close();
349
350 m_uri.clear();
351 m_shutting_down = false;
352 return status;
353 }
354
Read(void * dst,size_t dst_len,const Timeout<std::micro> & timeout,ConnectionStatus & status,Status * error_ptr)355 size_t ConnectionFileDescriptor::Read(void *dst, size_t dst_len,
356 const Timeout<std::micro> &timeout,
357 ConnectionStatus &status,
358 Status *error_ptr) {
359 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
360
361 std::unique_lock<std::recursive_mutex> locker(m_mutex, std::defer_lock);
362 if (!locker.try_lock()) {
363 LLDB_LOGF(log,
364 "%p ConnectionFileDescriptor::Read () failed to get the "
365 "connection lock.",
366 static_cast<void *>(this));
367 if (error_ptr)
368 error_ptr->SetErrorString("failed to get the connection lock for read.");
369
370 status = eConnectionStatusTimedOut;
371 return 0;
372 }
373
374 if (m_shutting_down) {
375 if (error_ptr)
376 error_ptr->SetErrorString("shutting down");
377 status = eConnectionStatusError;
378 return 0;
379 }
380
381 status = BytesAvailable(timeout, error_ptr);
382 if (status != eConnectionStatusSuccess)
383 return 0;
384
385 Status error;
386 size_t bytes_read = dst_len;
387 error = m_read_sp->Read(dst, bytes_read);
388
389 if (log) {
390 LLDB_LOGF(log,
391 "%p ConnectionFileDescriptor::Read() fd = %" PRIu64
392 ", dst = %p, dst_len = %" PRIu64 ") => %" PRIu64 ", error = %s",
393 static_cast<void *>(this),
394 static_cast<uint64_t>(m_read_sp->GetWaitableHandle()),
395 static_cast<void *>(dst), static_cast<uint64_t>(dst_len),
396 static_cast<uint64_t>(bytes_read), error.AsCString());
397 }
398
399 if (bytes_read == 0) {
400 error.Clear(); // End-of-file. Do not automatically close; pass along for
401 // the end-of-file handlers.
402 status = eConnectionStatusEndOfFile;
403 }
404
405 if (error_ptr)
406 *error_ptr = error;
407
408 if (error.Fail()) {
409 uint32_t error_value = error.GetError();
410 switch (error_value) {
411 case EAGAIN: // The file was marked for non-blocking I/O, and no data were
412 // ready to be read.
413 if (m_read_sp->GetFdType() == IOObject::eFDTypeSocket)
414 status = eConnectionStatusTimedOut;
415 else
416 status = eConnectionStatusSuccess;
417 return 0;
418
419 case EFAULT: // Buf points outside the allocated address space.
420 case EINTR: // A read from a slow device was interrupted before any data
421 // arrived by the delivery of a signal.
422 case EINVAL: // The pointer associated with fildes was negative.
423 case EIO: // An I/O error occurred while reading from the file system.
424 // The process group is orphaned.
425 // The file is a regular file, nbyte is greater than 0, the
426 // starting position is before the end-of-file, and the
427 // starting position is greater than or equal to the offset
428 // maximum established for the open file descriptor
429 // associated with fildes.
430 case EISDIR: // An attempt is made to read a directory.
431 case ENOBUFS: // An attempt to allocate a memory buffer fails.
432 case ENOMEM: // Insufficient memory is available.
433 status = eConnectionStatusError;
434 break; // Break to close....
435
436 case ENOENT: // no such file or directory
437 case EBADF: // fildes is not a valid file or socket descriptor open for
438 // reading.
439 case ENXIO: // An action is requested of a device that does not exist..
440 // A requested action cannot be performed by the device.
441 case ECONNRESET: // The connection is closed by the peer during a read
442 // attempt on a socket.
443 case ENOTCONN: // A read is attempted on an unconnected socket.
444 status = eConnectionStatusLostConnection;
445 break; // Break to close....
446
447 case ETIMEDOUT: // A transmission timeout occurs during a read attempt on a
448 // socket.
449 status = eConnectionStatusTimedOut;
450 return 0;
451
452 default:
453 LLDB_LOG(log, "this = {0}, unexpected error: {1}", this,
454 llvm::sys::StrError(error_value));
455 status = eConnectionStatusError;
456 break; // Break to close....
457 }
458
459 return 0;
460 }
461 return bytes_read;
462 }
463
Write(const void * src,size_t src_len,ConnectionStatus & status,Status * error_ptr)464 size_t ConnectionFileDescriptor::Write(const void *src, size_t src_len,
465 ConnectionStatus &status,
466 Status *error_ptr) {
467 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION));
468 LLDB_LOGF(log,
469 "%p ConnectionFileDescriptor::Write (src = %p, src_len = %" PRIu64
470 ")",
471 static_cast<void *>(this), static_cast<const void *>(src),
472 static_cast<uint64_t>(src_len));
473
474 if (!IsConnected()) {
475 if (error_ptr)
476 error_ptr->SetErrorString("not connected");
477 status = eConnectionStatusNoConnection;
478 return 0;
479 }
480
481 if (m_shutting_down) {
482 if (error_ptr)
483 error_ptr->SetErrorString("shutting down");
484 status = eConnectionStatusError;
485 return 0;
486 }
487
488 Status error;
489
490 size_t bytes_sent = src_len;
491 error = m_write_sp->Write(src, bytes_sent);
492
493 if (log) {
494 LLDB_LOGF(log,
495 "%p ConnectionFileDescriptor::Write(fd = %" PRIu64
496 ", src = %p, src_len = %" PRIu64 ") => %" PRIu64 " (error = %s)",
497 static_cast<void *>(this),
498 static_cast<uint64_t>(m_write_sp->GetWaitableHandle()),
499 static_cast<const void *>(src), static_cast<uint64_t>(src_len),
500 static_cast<uint64_t>(bytes_sent), error.AsCString());
501 }
502
503 if (error_ptr)
504 *error_ptr = error;
505
506 if (error.Fail()) {
507 switch (error.GetError()) {
508 case EAGAIN:
509 case EINTR:
510 status = eConnectionStatusSuccess;
511 return 0;
512
513 case ECONNRESET: // The connection is closed by the peer during a read
514 // attempt on a socket.
515 case ENOTCONN: // A read is attempted on an unconnected socket.
516 status = eConnectionStatusLostConnection;
517 break; // Break to close....
518
519 default:
520 status = eConnectionStatusError;
521 break; // Break to close....
522 }
523
524 return 0;
525 }
526
527 status = eConnectionStatusSuccess;
528 return bytes_sent;
529 }
530
GetURI()531 std::string ConnectionFileDescriptor::GetURI() { return m_uri; }
532
533 // This ConnectionFileDescriptor::BytesAvailable() uses select() via
534 // SelectHelper
535 //
536 // PROS:
537 // - select is consistent across most unix platforms
538 // - The Apple specific version allows for unlimited fds in the fd_sets by
539 // setting the _DARWIN_UNLIMITED_SELECT define prior to including the
540 // required header files.
541 // CONS:
542 // - on non-Apple platforms, only supports file descriptors up to FD_SETSIZE.
543 // This implementation will assert if it runs into that hard limit to let
544 // users know that another ConnectionFileDescriptor::BytesAvailable() should
545 // be used or a new version of ConnectionFileDescriptor::BytesAvailable()
546 // should be written for the system that is running into the limitations.
547
548 ConnectionStatus
BytesAvailable(const Timeout<std::micro> & timeout,Status * error_ptr)549 ConnectionFileDescriptor::BytesAvailable(const Timeout<std::micro> &timeout,
550 Status *error_ptr) {
551 // Don't need to take the mutex here separately since we are only called from
552 // Read. If we ever get used more generally we will need to lock here as
553 // well.
554
555 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_CONNECTION));
556 LLDB_LOG(log, "this = {0}, timeout = {1}", this, timeout);
557
558 // Make a copy of the file descriptors to make sure we don't have another
559 // thread change these values out from under us and cause problems in the
560 // loop below where like in FS_SET()
561 const IOObject::WaitableHandle handle = m_read_sp->GetWaitableHandle();
562 const int pipe_fd = m_pipe.GetReadFileDescriptor();
563
564 if (handle != IOObject::kInvalidHandleValue) {
565 SelectHelper select_helper;
566 if (timeout)
567 select_helper.SetTimeout(*timeout);
568
569 select_helper.FDSetRead(handle);
570 #if defined(_WIN32)
571 // select() won't accept pipes on Windows. The entire Windows codepath
572 // needs to be converted over to using WaitForMultipleObjects and event
573 // HANDLEs, but for now at least this will allow ::select() to not return
574 // an error.
575 const bool have_pipe_fd = false;
576 #else
577 const bool have_pipe_fd = pipe_fd >= 0;
578 #endif
579 if (have_pipe_fd)
580 select_helper.FDSetRead(pipe_fd);
581
582 while (handle == m_read_sp->GetWaitableHandle()) {
583
584 Status error = select_helper.Select();
585
586 if (error_ptr)
587 *error_ptr = error;
588
589 if (error.Fail()) {
590 switch (error.GetError()) {
591 case EBADF: // One of the descriptor sets specified an invalid
592 // descriptor.
593 return eConnectionStatusLostConnection;
594
595 case EINVAL: // The specified time limit is invalid. One of its
596 // components is negative or too large.
597 default: // Other unknown error
598 return eConnectionStatusError;
599
600 case ETIMEDOUT:
601 return eConnectionStatusTimedOut;
602
603 case EAGAIN: // The kernel was (perhaps temporarily) unable to
604 // allocate the requested number of file descriptors, or
605 // we have non-blocking IO
606 case EINTR: // A signal was delivered before the time limit
607 // expired and before any of the selected events occurred.
608 break; // Lets keep reading to until we timeout
609 }
610 } else {
611 if (select_helper.FDIsSetRead(handle))
612 return eConnectionStatusSuccess;
613
614 if (select_helper.FDIsSetRead(pipe_fd)) {
615 // There is an interrupt or exit command in the command pipe Read the
616 // data from that pipe:
617 char c;
618
619 ssize_t bytes_read = llvm::sys::RetryAfterSignal(-1, ::read, pipe_fd, &c, 1);
620 assert(bytes_read == 1);
621 (void)bytes_read;
622 switch (c) {
623 case 'q':
624 LLDB_LOGF(log,
625 "%p ConnectionFileDescriptor::BytesAvailable() "
626 "got data: %c from the command channel.",
627 static_cast<void *>(this), c);
628 return eConnectionStatusEndOfFile;
629 case 'i':
630 // Interrupt the current read
631 return eConnectionStatusInterrupted;
632 }
633 }
634 }
635 }
636 }
637
638 if (error_ptr)
639 error_ptr->SetErrorString("not connected");
640 return eConnectionStatusLostConnection;
641 }
642
643 ConnectionStatus
NamedSocketAccept(llvm::StringRef socket_name,Status * error_ptr)644 ConnectionFileDescriptor::NamedSocketAccept(llvm::StringRef socket_name,
645 Status *error_ptr) {
646 Socket *socket = nullptr;
647 Status error =
648 Socket::UnixDomainAccept(socket_name, m_child_processes_inherit, socket);
649 if (error_ptr)
650 *error_ptr = error;
651 m_write_sp.reset(socket);
652 m_read_sp = m_write_sp;
653 if (error.Fail()) {
654 return eConnectionStatusError;
655 }
656 m_uri.assign(std::string(socket_name));
657 return eConnectionStatusSuccess;
658 }
659
660 ConnectionStatus
NamedSocketConnect(llvm::StringRef socket_name,Status * error_ptr)661 ConnectionFileDescriptor::NamedSocketConnect(llvm::StringRef socket_name,
662 Status *error_ptr) {
663 Socket *socket = nullptr;
664 Status error =
665 Socket::UnixDomainConnect(socket_name, m_child_processes_inherit, socket);
666 if (error_ptr)
667 *error_ptr = error;
668 m_write_sp.reset(socket);
669 m_read_sp = m_write_sp;
670 if (error.Fail()) {
671 return eConnectionStatusError;
672 }
673 m_uri.assign(std::string(socket_name));
674 return eConnectionStatusSuccess;
675 }
676
677 lldb::ConnectionStatus
UnixAbstractSocketConnect(llvm::StringRef socket_name,Status * error_ptr)678 ConnectionFileDescriptor::UnixAbstractSocketConnect(llvm::StringRef socket_name,
679 Status *error_ptr) {
680 Socket *socket = nullptr;
681 Status error = Socket::UnixAbstractConnect(socket_name,
682 m_child_processes_inherit, socket);
683 if (error_ptr)
684 *error_ptr = error;
685 m_write_sp.reset(socket);
686 m_read_sp = m_write_sp;
687 if (error.Fail()) {
688 return eConnectionStatusError;
689 }
690 m_uri.assign(std::string(socket_name));
691 return eConnectionStatusSuccess;
692 }
693
694 ConnectionStatus
SocketListenAndAccept(llvm::StringRef s,Status * error_ptr)695 ConnectionFileDescriptor::SocketListenAndAccept(llvm::StringRef s,
696 Status *error_ptr) {
697 if (error_ptr)
698 *error_ptr = Status();
699 m_port_predicate.SetValue(0, eBroadcastNever);
700
701 m_waiting_for_accept = true;
702 llvm::Expected<std::unique_ptr<TCPSocket>> listening_socket =
703 Socket::TcpListen(s, m_child_processes_inherit, &m_port_predicate);
704 if (!listening_socket) {
705 if (error_ptr)
706 *error_ptr = listening_socket.takeError();
707 else
708 LLDB_LOG_ERROR(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION),
709 listening_socket.takeError(), "tcp listen failed: {0}");
710 return eConnectionStatusError;
711 }
712
713
714 Socket *accepted_socket;
715 Status error = listening_socket.get()->Accept(accepted_socket);
716 if (error_ptr)
717 *error_ptr = error;
718 if (error.Fail())
719 return eConnectionStatusError;
720
721 InitializeSocket(accepted_socket);
722 return eConnectionStatusSuccess;
723 }
724
ConnectTCP(llvm::StringRef s,Status * error_ptr)725 ConnectionStatus ConnectionFileDescriptor::ConnectTCP(llvm::StringRef s,
726 Status *error_ptr) {
727 if (error_ptr)
728 *error_ptr = Status();
729
730 llvm::Expected<std::unique_ptr<Socket>> socket =
731 Socket::TcpConnect(s, m_child_processes_inherit);
732 if (!socket) {
733 if (error_ptr)
734 *error_ptr = socket.takeError();
735 else
736 LLDB_LOG_ERROR(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION),
737 socket.takeError(), "tcp connect failed: {0}");
738 return eConnectionStatusError;
739 }
740 m_write_sp = std::move(*socket);
741 m_read_sp = m_write_sp;
742 m_uri.assign(std::string(s));
743 return eConnectionStatusSuccess;
744 }
745
ConnectUDP(llvm::StringRef s,Status * error_ptr)746 ConnectionStatus ConnectionFileDescriptor::ConnectUDP(llvm::StringRef s,
747 Status *error_ptr) {
748 if (error_ptr)
749 *error_ptr = Status();
750 llvm::Expected<std::unique_ptr<UDPSocket>> socket =
751 Socket::UdpConnect(s, m_child_processes_inherit);
752 if (!socket) {
753 if (error_ptr)
754 *error_ptr = socket.takeError();
755 else
756 LLDB_LOG_ERROR(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_CONNECTION),
757 socket.takeError(), "tcp connect failed: {0}");
758 return eConnectionStatusError;
759 }
760 m_write_sp = std::move(*socket);
761 m_read_sp = m_write_sp;
762 m_uri.assign(std::string(s));
763 return eConnectionStatusSuccess;
764 }
765
766 uint16_t
GetListeningPort(const Timeout<std::micro> & timeout)767 ConnectionFileDescriptor::GetListeningPort(const Timeout<std::micro> &timeout) {
768 auto Result = m_port_predicate.WaitForValueNotEqualTo(0, timeout);
769 return Result ? *Result : 0;
770 }
771
GetChildProcessesInherit() const772 bool ConnectionFileDescriptor::GetChildProcessesInherit() const {
773 return m_child_processes_inherit;
774 }
775
SetChildProcessesInherit(bool child_processes_inherit)776 void ConnectionFileDescriptor::SetChildProcessesInherit(
777 bool child_processes_inherit) {
778 m_child_processes_inherit = child_processes_inherit;
779 }
780
InitializeSocket(Socket * socket)781 void ConnectionFileDescriptor::InitializeSocket(Socket *socket) {
782 m_write_sp.reset(socket);
783 m_read_sp = m_write_sp;
784 m_uri = socket->GetRemoteConnectionURI();
785 }
786