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