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