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