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