1254721Semaste//===-- ConnectionFileDescriptor.cpp ----------------------------*- C++ -*-===// 2254721Semaste// 3254721Semaste// The LLVM Compiler Infrastructure 4254721Semaste// 5254721Semaste// This file is distributed under the University of Illinois Open Source 6254721Semaste// License. See LICENSE.TXT for details. 7254721Semaste// 8254721Semaste//===----------------------------------------------------------------------===// 9254721Semaste 10254721Semaste#if defined(__APPLE__) 11254721Semaste// Enable this special support for Apple builds where we can have unlimited 12254721Semaste// select bounds. We tried switching to poll() and kqueue and we were panicing 13254721Semaste// the kernel, so we have to stick with select for now. 14254721Semaste#define _DARWIN_UNLIMITED_SELECT 15254721Semaste#endif 16254721Semaste 17254721Semaste#include "lldb/Core/ConnectionFileDescriptor.h" 18263363Semaste#include "lldb/Host/Config.h" 19263363Semaste#include "lldb/Host/SocketAddress.h" 20254721Semaste 21254721Semaste// C Includes 22254721Semaste#include <errno.h> 23254721Semaste#include <fcntl.h> 24263363Semaste#include <string.h> 25263363Semaste#include <stdlib.h> 26263363Semaste#include <sys/types.h> 27263363Semaste#ifndef LLDB_DISABLE_POSIX 28254721Semaste#include <arpa/inet.h> 29254721Semaste#include <netdb.h> 30254721Semaste#include <netinet/in.h> 31254721Semaste#include <netinet/tcp.h> 32254721Semaste#include <sys/socket.h> 33254721Semaste#include <sys/un.h> 34254721Semaste#include <termios.h> 35254721Semaste#include <unistd.h> 36263363Semaste#endif 37263363Semaste#ifdef _WIN32 38263363Semaste#include "lldb/Host/windows/windows.h" 39263363Semaste#include <winsock2.h> 40263363Semaste#include <WS2tcpip.h> 41263363Semaste#endif 42254721Semaste 43254721Semaste// C++ Includes 44254721Semaste// Other libraries and framework includes 45263363Semaste#include "llvm/Support/ErrorHandling.h" 46254721Semaste#if defined(__APPLE__) 47254721Semaste#include "llvm/ADT/SmallVector.h" 48254721Semaste#endif 49254721Semaste// Project includes 50254721Semaste#include "lldb/lldb-private-log.h" 51254721Semaste#include "lldb/Interpreter/Args.h" 52254721Semaste#include "lldb/Core/Communication.h" 53254721Semaste#include "lldb/Core/Log.h" 54254721Semaste#include "lldb/Core/RegularExpression.h" 55254721Semaste#include "lldb/Core/Timer.h" 56263367Semaste#include "lldb/Host/Host.h" 57254721Semaste 58263367Semaste 59254721Semasteusing namespace lldb; 60254721Semasteusing namespace lldb_private; 61254721Semaste 62254721Semastestatic bool 63254721SemasteDecodeHostAndPort (const char *host_and_port, 64254721Semaste std::string &host_str, 65254721Semaste std::string &port_str, 66254721Semaste int32_t& port, 67254721Semaste Error *error_ptr) 68254721Semaste{ 69254721Semaste static RegularExpression g_regex ("([^:]+):([0-9]+)"); 70254721Semaste RegularExpression::Match regex_match(2); 71254721Semaste if (g_regex.Execute (host_and_port, ®ex_match)) 72254721Semaste { 73254721Semaste if (regex_match.GetMatchAtIndex (host_and_port, 1, host_str) && 74254721Semaste regex_match.GetMatchAtIndex (host_and_port, 2, port_str)) 75254721Semaste { 76254721Semaste port = Args::StringToSInt32 (port_str.c_str(), INT32_MIN); 77254721Semaste if (port != INT32_MIN) 78254721Semaste { 79254721Semaste if (error_ptr) 80254721Semaste error_ptr->Clear(); 81254721Semaste return true; 82254721Semaste } 83254721Semaste } 84254721Semaste } 85254721Semaste host_str.clear(); 86254721Semaste port_str.clear(); 87254721Semaste port = INT32_MIN; 88254721Semaste if (error_ptr) 89254721Semaste error_ptr->SetErrorStringWithFormat("invalid host:port specification: '%s'", host_and_port); 90254721Semaste return false; 91254721Semaste} 92254721Semaste 93254721SemasteConnectionFileDescriptor::ConnectionFileDescriptor () : 94254721Semaste Connection(), 95254721Semaste m_fd_send (-1), 96254721Semaste m_fd_recv (-1), 97254721Semaste m_fd_send_type (eFDTypeFile), 98254721Semaste m_fd_recv_type (eFDTypeFile), 99263363Semaste m_udp_send_sockaddr (new SocketAddress()), 100254721Semaste m_socket_timeout_usec(0), 101254721Semaste m_pipe_read(-1), 102254721Semaste m_pipe_write(-1), 103254721Semaste m_mutex (Mutex::eMutexTypeRecursive), 104269024Semaste m_should_close_fd (false), 105254721Semaste m_shutting_down (false) 106254721Semaste{ 107254721Semaste Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION | LIBLLDB_LOG_OBJECT)); 108254721Semaste if (log) 109254721Semaste log->Printf ("%p ConnectionFileDescriptor::ConnectionFileDescriptor ()", this); 110254721Semaste} 111254721Semaste 112254721SemasteConnectionFileDescriptor::ConnectionFileDescriptor (int fd, bool owns_fd) : 113254721Semaste Connection(), 114254721Semaste m_fd_send (fd), 115254721Semaste m_fd_recv (fd), 116254721Semaste m_fd_send_type (eFDTypeFile), 117254721Semaste m_fd_recv_type (eFDTypeFile), 118263363Semaste m_udp_send_sockaddr (new SocketAddress()), 119254721Semaste m_socket_timeout_usec(0), 120254721Semaste m_pipe_read(-1), 121254721Semaste m_pipe_write(-1), 122254721Semaste m_mutex (Mutex::eMutexTypeRecursive), 123269024Semaste m_should_close_fd (owns_fd), 124254721Semaste m_shutting_down (false) 125254721Semaste{ 126254721Semaste Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION | LIBLLDB_LOG_OBJECT)); 127254721Semaste if (log) 128254721Semaste log->Printf ("%p ConnectionFileDescriptor::ConnectionFileDescriptor (fd = %i, owns_fd = %i)", this, fd, owns_fd); 129254721Semaste OpenCommandPipe (); 130254721Semaste} 131254721Semaste 132254721Semaste 133254721SemasteConnectionFileDescriptor::~ConnectionFileDescriptor () 134254721Semaste{ 135254721Semaste Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION | LIBLLDB_LOG_OBJECT)); 136254721Semaste if (log) 137254721Semaste log->Printf ("%p ConnectionFileDescriptor::~ConnectionFileDescriptor ()", this); 138254721Semaste Disconnect (NULL); 139254721Semaste CloseCommandPipe (); 140254721Semaste} 141254721Semaste 142254721Semastevoid 143254721SemasteConnectionFileDescriptor::OpenCommandPipe () 144254721Semaste{ 145254721Semaste CloseCommandPipe(); 146254721Semaste 147254721Semaste Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); 148254721Semaste // Make the command file descriptor here: 149254721Semaste int filedes[2]; 150263363Semaste#ifndef LLDB_DISABLE_POSIX 151254721Semaste int result = pipe (filedes); 152263363Semaste#else 153263363Semaste int result = -1; 154263363Semaste#endif 155254721Semaste if (result != 0) 156254721Semaste { 157254721Semaste if (log) 158263363Semaste log->Printf ("%p ConnectionFileDescriptor::OpenCommandPipe () - could not make pipe: %s", 159254721Semaste this, 160254721Semaste strerror(errno)); 161254721Semaste } 162254721Semaste else 163254721Semaste { 164254721Semaste m_pipe_read = filedes[0]; 165254721Semaste m_pipe_write = filedes[1]; 166263363Semaste if (log) 167263363Semaste log->Printf ("%p ConnectionFileDescriptor::OpenCommandPipe() - success readfd=%d writefd=%d", 168263363Semaste this, 169263363Semaste m_pipe_read, 170263363Semaste m_pipe_write); 171254721Semaste } 172254721Semaste} 173254721Semaste 174254721Semastevoid 175254721SemasteConnectionFileDescriptor::CloseCommandPipe () 176254721Semaste{ 177263363Semaste Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); 178263363Semaste if (log) 179263363Semaste log->Printf ("%p ConnectionFileDescriptor::CloseCommandPipe()", 180263363Semaste this); 181263363Semaste 182254721Semaste if (m_pipe_read != -1) 183254721Semaste { 184263363Semaste#ifdef _MSC_VER 185263363Semaste llvm_unreachable("pipe close unsupported in MSVC"); 186263363Semaste#else 187254721Semaste close (m_pipe_read); 188263363Semaste#endif 189254721Semaste m_pipe_read = -1; 190254721Semaste } 191254721Semaste 192254721Semaste if (m_pipe_write != -1) 193254721Semaste { 194263363Semaste#ifdef _MSC_VER 195263363Semaste llvm_unreachable("pipe close unsupported in MSVC"); 196263363Semaste#else 197254721Semaste close (m_pipe_write); 198263363Semaste#endif 199254721Semaste m_pipe_write = -1; 200254721Semaste } 201254721Semaste} 202254721Semaste 203254721Semastebool 204254721SemasteConnectionFileDescriptor::IsConnected () const 205254721Semaste{ 206254721Semaste return m_fd_send >= 0 || m_fd_recv >= 0; 207254721Semaste} 208254721Semaste 209254721SemasteConnectionStatus 210254721SemasteConnectionFileDescriptor::Connect (const char *s, Error *error_ptr) 211254721Semaste{ 212254721Semaste Mutex::Locker locker (m_mutex); 213254721Semaste Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); 214254721Semaste if (log) 215254721Semaste log->Printf ("%p ConnectionFileDescriptor::Connect (url = '%s')", this, s); 216254721Semaste 217254721Semaste OpenCommandPipe(); 218254721Semaste 219254721Semaste if (s && s[0]) 220254721Semaste { 221254721Semaste if (strstr(s, "listen://")) 222254721Semaste { 223254721Semaste // listen://HOST:PORT 224269024Semaste return SocketListen (s + strlen("listen://"), error_ptr); 225254721Semaste } 226269024Semaste else if (strstr(s, "accept://")) 227269024Semaste { 228269024Semaste // unix://SOCKNAME 229269024Semaste return NamedSocketAccept (s + strlen("accept://"), error_ptr); 230269024Semaste } 231254721Semaste else if (strstr(s, "unix-accept://")) 232254721Semaste { 233254721Semaste // unix://SOCKNAME 234254721Semaste return NamedSocketAccept (s + strlen("unix-accept://"), error_ptr); 235254721Semaste } 236254721Semaste else if (strstr(s, "connect://")) 237254721Semaste { 238254721Semaste return ConnectTCP (s + strlen("connect://"), error_ptr); 239254721Semaste } 240254721Semaste else if (strstr(s, "tcp-connect://")) 241254721Semaste { 242254721Semaste return ConnectTCP (s + strlen("tcp-connect://"), error_ptr); 243254721Semaste } 244254721Semaste else if (strstr(s, "udp://")) 245254721Semaste { 246254721Semaste return ConnectUDP (s + strlen("udp://"), error_ptr); 247254721Semaste } 248254721Semaste else if (strstr(s, "fd://")) 249254721Semaste { 250254721Semaste // Just passing a native file descriptor within this current process 251254721Semaste // that is already opened (possibly from a service or other source). 252254721Semaste s += strlen ("fd://"); 253254721Semaste bool success = false; 254254721Semaste m_fd_send = m_fd_recv = Args::StringToSInt32 (s, -1, 0, &success); 255254721Semaste 256254721Semaste if (success) 257254721Semaste { 258254721Semaste // We have what looks to be a valid file descriptor, but we 259254721Semaste // should make sure it is. We currently are doing this by trying to 260254721Semaste // get the flags from the file descriptor and making sure it 261254721Semaste // isn't a bad fd. 262254721Semaste errno = 0; 263263363Semaste#ifndef LLDB_DISABLE_POSIX 264254721Semaste int flags = ::fcntl (m_fd_send, F_GETFL, 0); 265263363Semaste#else 266263363Semaste int flags = -1; 267263363Semaste#endif 268254721Semaste if (flags == -1 || errno == EBADF) 269254721Semaste { 270254721Semaste if (error_ptr) 271254721Semaste error_ptr->SetErrorStringWithFormat ("stale file descriptor: %s", s); 272254721Semaste m_fd_send = m_fd_recv = -1; 273254721Semaste return eConnectionStatusError; 274254721Semaste } 275254721Semaste else 276254721Semaste { 277254721Semaste // Try and get a socket option from this file descriptor to 278254721Semaste // see if this is a socket and set m_is_socket accordingly. 279254721Semaste int resuse; 280254721Semaste bool is_socket = GetSocketOption (m_fd_send, SOL_SOCKET, SO_REUSEADDR, resuse) == 0; 281254721Semaste if (is_socket) 282254721Semaste m_fd_send_type = m_fd_recv_type = eFDTypeSocket; 283254721Semaste // Don't take ownership of a file descriptor that gets passed 284254721Semaste // to us since someone else opened the file descriptor and 285254721Semaste // handed it to us. 286254721Semaste // TODO: Since are using a URL to open connection we should 287254721Semaste // eventually parse options using the web standard where we 288254721Semaste // have "fd://123?opt1=value;opt2=value" and we can have an 289254721Semaste // option be "owns=1" or "owns=0" or something like this to 290254721Semaste // allow us to specify this. For now, we assume we must 291254721Semaste // assume we don't own it. 292254721Semaste m_should_close_fd = false; 293254721Semaste return eConnectionStatusSuccess; 294254721Semaste } 295254721Semaste } 296254721Semaste 297254721Semaste if (error_ptr) 298254721Semaste error_ptr->SetErrorStringWithFormat ("invalid file descriptor: \"fd://%s\"", s); 299254721Semaste m_fd_send = m_fd_recv = -1; 300254721Semaste return eConnectionStatusError; 301254721Semaste } 302254721Semaste else if (strstr(s, "file://")) 303254721Semaste { 304254721Semaste // file:///PATH 305254721Semaste const char *path = s + strlen("file://"); 306263363Semaste#ifndef LLDB_DISABLE_POSIX 307254721Semaste do 308254721Semaste { 309254721Semaste m_fd_send = m_fd_recv = ::open (path, O_RDWR); 310254721Semaste } while (m_fd_send == -1 && errno == EINTR); 311254721Semaste if (m_fd_send == -1) 312254721Semaste { 313254721Semaste if (error_ptr) 314254721Semaste error_ptr->SetErrorToErrno(); 315254721Semaste return eConnectionStatusError; 316254721Semaste } 317254721Semaste 318254721Semaste if (::isatty(m_fd_send)) 319254721Semaste { 320254721Semaste // Set up serial terminal emulation 321254721Semaste struct termios options; 322254721Semaste ::tcgetattr (m_fd_send, &options); 323254721Semaste 324254721Semaste // Set port speed to maximum 325254721Semaste ::cfsetospeed (&options, B115200); 326254721Semaste ::cfsetispeed (&options, B115200); 327254721Semaste 328254721Semaste // Raw input, disable echo and signals 329254721Semaste options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); 330254721Semaste 331254721Semaste // Make sure only one character is needed to return from a read 332254721Semaste options.c_cc[VMIN] = 1; 333254721Semaste options.c_cc[VTIME] = 0; 334254721Semaste 335254721Semaste ::tcsetattr (m_fd_send, TCSANOW, &options); 336254721Semaste } 337254721Semaste 338254721Semaste int flags = ::fcntl (m_fd_send, F_GETFL, 0); 339254721Semaste if (flags >= 0) 340254721Semaste { 341254721Semaste if ((flags & O_NONBLOCK) == 0) 342254721Semaste { 343254721Semaste flags |= O_NONBLOCK; 344254721Semaste ::fcntl (m_fd_send, F_SETFL, flags); 345254721Semaste } 346254721Semaste } 347254721Semaste m_should_close_fd = true; 348254721Semaste return eConnectionStatusSuccess; 349263363Semaste#else 350263363Semaste return eConnectionStatusError; 351263363Semaste#endif 352254721Semaste } 353254721Semaste if (error_ptr) 354254721Semaste error_ptr->SetErrorStringWithFormat ("unsupported connection URL: '%s'", s); 355254721Semaste return eConnectionStatusError; 356254721Semaste } 357254721Semaste if (error_ptr) 358254721Semaste error_ptr->SetErrorString("invalid connect arguments"); 359254721Semaste return eConnectionStatusError; 360254721Semaste} 361254721Semaste 362254721SemasteConnectionStatus 363254721SemasteConnectionFileDescriptor::Disconnect (Error *error_ptr) 364254721Semaste{ 365254721Semaste Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); 366254721Semaste if (log) 367254721Semaste log->Printf ("%p ConnectionFileDescriptor::Disconnect ()", this); 368254721Semaste 369269024Semaste // Reset the port predicate when disconnecting and don't broadcast 370269024Semaste m_port_predicate.SetValue(0, eBroadcastNever); 371269024Semaste 372254721Semaste ConnectionStatus status = eConnectionStatusSuccess; 373254721Semaste 374254721Semaste if (m_fd_send < 0 && m_fd_recv < 0) 375254721Semaste { 376254721Semaste if (log) 377254721Semaste log->Printf ("%p ConnectionFileDescriptor::Disconnect(): Nothing to disconnect", this); 378254721Semaste return eConnectionStatusSuccess; 379254721Semaste } 380254721Semaste 381254721Semaste // Try to get the ConnectionFileDescriptor's mutex. If we fail, that is quite likely 382254721Semaste // because somebody is doing a blocking read on our file descriptor. If that's the case, 383254721Semaste // then send the "q" char to the command file channel so the read will wake up and the connection 384254721Semaste // will then know to shut down. 385254721Semaste 386254721Semaste m_shutting_down = true; 387254721Semaste 388254721Semaste Mutex::Locker locker; 389254721Semaste bool got_lock= locker.TryLock (m_mutex); 390254721Semaste 391254721Semaste if (!got_lock) 392254721Semaste { 393254721Semaste if (m_pipe_write != -1 ) 394254721Semaste { 395263363Semaste int result; 396263363Semaste result = write (m_pipe_write, "q", 1); 397263363Semaste if (log) 398263363Semaste log->Printf ("%p ConnectionFileDescriptor::Disconnect(): Couldn't get the lock, sent 'q' to %d, result = %d.", this, m_pipe_write, result); 399254721Semaste } 400263363Semaste else if (log) 401263363Semaste log->Printf ("%p ConnectionFileDescriptor::Disconnect(): Couldn't get the lock, but no command pipe is available.", this); 402254721Semaste locker.Lock (m_mutex); 403254721Semaste } 404263363Semaste 405254721Semaste if (m_should_close_fd == true) 406254721Semaste { 407254721Semaste if (m_fd_send == m_fd_recv) 408254721Semaste { 409263363Semaste status = Close (m_fd_send, m_fd_send_type, error_ptr); 410254721Semaste } 411254721Semaste else 412254721Semaste { 413254721Semaste // File descriptors are the different, close both if needed 414254721Semaste if (m_fd_send >= 0) 415263363Semaste status = Close (m_fd_send, m_fd_send_type, error_ptr); 416254721Semaste if (m_fd_recv >= 0) 417254721Semaste { 418263363Semaste ConnectionStatus recv_status = Close (m_fd_recv, m_fd_recv_type, error_ptr); 419254721Semaste if (status == eConnectionStatusSuccess) 420254721Semaste status = recv_status; 421254721Semaste } 422254721Semaste } 423254721Semaste } 424254721Semaste 425254721Semaste // Now set all our descriptors to invalid values. 426254721Semaste 427254721Semaste m_fd_send = m_fd_recv = -1; 428254721Semaste 429254721Semaste if (status != eConnectionStatusSuccess) 430254721Semaste { 431254721Semaste 432254721Semaste return status; 433254721Semaste } 434254721Semaste 435254721Semaste m_shutting_down = false; 436254721Semaste return eConnectionStatusSuccess; 437254721Semaste} 438254721Semaste 439254721Semastesize_t 440254721SemasteConnectionFileDescriptor::Read (void *dst, 441254721Semaste size_t dst_len, 442254721Semaste uint32_t timeout_usec, 443254721Semaste ConnectionStatus &status, 444254721Semaste Error *error_ptr) 445254721Semaste{ 446254721Semaste Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); 447254721Semaste if (log) 448254721Semaste log->Printf ("%p ConnectionFileDescriptor::Read () ::read (fd = %i, dst = %p, dst_len = %" PRIu64 ")...", 449254721Semaste this, m_fd_recv, dst, (uint64_t)dst_len); 450254721Semaste 451254721Semaste Mutex::Locker locker; 452254721Semaste bool got_lock = locker.TryLock (m_mutex); 453254721Semaste if (!got_lock) 454254721Semaste { 455254721Semaste if (log) 456254721Semaste log->Printf ("%p ConnectionFileDescriptor::Read () failed to get the connection lock.", 457254721Semaste this); 458254721Semaste if (error_ptr) 459254721Semaste error_ptr->SetErrorString ("failed to get the connection lock for read."); 460254721Semaste 461254721Semaste status = eConnectionStatusTimedOut; 462254721Semaste return 0; 463254721Semaste } 464254721Semaste else if (m_shutting_down) 465254721Semaste return eConnectionStatusError; 466254721Semaste 467254721Semaste ssize_t bytes_read = 0; 468254721Semaste 469254721Semaste status = BytesAvailable (timeout_usec, error_ptr); 470254721Semaste if (status == eConnectionStatusSuccess) 471254721Semaste { 472254721Semaste do 473254721Semaste { 474263363Semaste#ifndef LLDB_DISABLE_POSIX 475254721Semaste bytes_read = ::read (m_fd_recv, dst, dst_len); 476263363Semaste#else 477263363Semaste switch (m_fd_send_type) { 478263363Semaste case eFDTypeSocket: 479263363Semaste case eFDTypeSocketUDP: 480263363Semaste bytes_read = ::recv (m_fd_recv, (char*)dst, dst_len, 0); 481263363Semaste break; 482263363Semaste default: 483263363Semaste bytes_read = -1; 484263363Semaste break; 485263363Semaste 486263363Semaste } 487263363Semaste 488263363Semaste#endif 489254721Semaste } while (bytes_read < 0 && errno == EINTR); 490254721Semaste } 491254721Semaste 492254721Semaste if (status != eConnectionStatusSuccess) 493254721Semaste return 0; 494254721Semaste 495254721Semaste Error error; 496254721Semaste if (bytes_read == 0) 497254721Semaste { 498254721Semaste error.Clear(); // End-of-file. Do not automatically close; pass along for the end-of-file handlers. 499254721Semaste status = eConnectionStatusEndOfFile; 500254721Semaste } 501254721Semaste else if (bytes_read < 0) 502254721Semaste { 503254721Semaste error.SetErrorToErrno(); 504254721Semaste } 505254721Semaste else 506254721Semaste { 507254721Semaste error.Clear(); 508254721Semaste } 509254721Semaste 510254721Semaste if (log) 511254721Semaste log->Printf ("%p ConnectionFileDescriptor::Read () ::read (fd = %i, dst = %p, dst_len = %" PRIu64 ") => %" PRIi64 ", error = %s", 512254721Semaste this, 513254721Semaste m_fd_recv, 514254721Semaste dst, 515254721Semaste (uint64_t)dst_len, 516254721Semaste (int64_t)bytes_read, 517254721Semaste error.AsCString()); 518254721Semaste 519254721Semaste if (error_ptr) 520254721Semaste *error_ptr = error; 521254721Semaste 522254721Semaste if (error.Fail()) 523254721Semaste { 524254721Semaste uint32_t error_value = error.GetError(); 525254721Semaste switch (error_value) 526254721Semaste { 527254721Semaste case EAGAIN: // The file was marked for non-blocking I/O, and no data were ready to be read. 528254721Semaste if (m_fd_recv_type == eFDTypeSocket || m_fd_recv_type == eFDTypeSocketUDP) 529254721Semaste status = eConnectionStatusTimedOut; 530254721Semaste else 531254721Semaste status = eConnectionStatusSuccess; 532254721Semaste return 0; 533254721Semaste 534254721Semaste case EFAULT: // Buf points outside the allocated address space. 535254721Semaste case EINTR: // A read from a slow device was interrupted before any data arrived by the delivery of a signal. 536254721Semaste case EINVAL: // The pointer associated with fildes was negative. 537254721Semaste case EIO: // An I/O error occurred while reading from the file system. 538254721Semaste // The process group is orphaned. 539254721Semaste // The file is a regular file, nbyte is greater than 0, 540254721Semaste // the starting position is before the end-of-file, and 541254721Semaste // the starting position is greater than or equal to the 542254721Semaste // offset maximum established for the open file 543254721Semaste // descriptor associated with fildes. 544254721Semaste case EISDIR: // An attempt is made to read a directory. 545254721Semaste case ENOBUFS: // An attempt to allocate a memory buffer fails. 546254721Semaste case ENOMEM: // Insufficient memory is available. 547254721Semaste status = eConnectionStatusError; 548254721Semaste break; // Break to close.... 549254721Semaste 550254721Semaste case ENOENT: // no such file or directory 551254721Semaste case EBADF: // fildes is not a valid file or socket descriptor open for reading. 552254721Semaste case ENXIO: // An action is requested of a device that does not exist.. 553254721Semaste // A requested action cannot be performed by the device. 554254721Semaste case ECONNRESET:// The connection is closed by the peer during a read attempt on a socket. 555254721Semaste case ENOTCONN: // A read is attempted on an unconnected socket. 556254721Semaste status = eConnectionStatusLostConnection; 557254721Semaste break; // Break to close.... 558254721Semaste 559254721Semaste case ETIMEDOUT: // A transmission timeout occurs during a read attempt on a socket. 560254721Semaste status = eConnectionStatusTimedOut; 561254721Semaste return 0; 562263363Semaste 563263363Semaste default: 564263363Semaste if (log) 565263363Semaste log->Printf("%p ConnectionFileDescriptor::Read (), unexpected error: %s", this, strerror(error_value)); 566263363Semaste status = eConnectionStatusError; 567263363Semaste break; // Break to close.... 568263363Semaste 569254721Semaste } 570254721Semaste 571254721Semaste return 0; 572254721Semaste } 573254721Semaste return bytes_read; 574254721Semaste} 575254721Semaste 576254721Semastesize_t 577254721SemasteConnectionFileDescriptor::Write (const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr) 578254721Semaste{ 579254721Semaste Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); 580254721Semaste if (log) 581254721Semaste log->Printf ("%p ConnectionFileDescriptor::Write (src = %p, src_len = %" PRIu64 ")", this, src, (uint64_t)src_len); 582254721Semaste 583254721Semaste if (!IsConnected ()) 584254721Semaste { 585254721Semaste if (error_ptr) 586254721Semaste error_ptr->SetErrorString("not connected"); 587254721Semaste status = eConnectionStatusNoConnection; 588254721Semaste return 0; 589254721Semaste } 590254721Semaste 591254721Semaste 592254721Semaste Error error; 593254721Semaste 594254721Semaste ssize_t bytes_sent = 0; 595254721Semaste 596254721Semaste switch (m_fd_send_type) 597254721Semaste { 598263363Semaste#ifndef LLDB_DISABLE_POSIX 599254721Semaste case eFDTypeFile: // Other FD requireing read/write 600254721Semaste do 601254721Semaste { 602254721Semaste bytes_sent = ::write (m_fd_send, src, src_len); 603254721Semaste } while (bytes_sent < 0 && errno == EINTR); 604254721Semaste break; 605263363Semaste#endif 606254721Semaste case eFDTypeSocket: // Socket requiring send/recv 607254721Semaste do 608254721Semaste { 609263363Semaste bytes_sent = ::send (m_fd_send, (char*)src, src_len, 0); 610254721Semaste } while (bytes_sent < 0 && errno == EINTR); 611254721Semaste break; 612254721Semaste 613254721Semaste case eFDTypeSocketUDP: // Unconnected UDP socket requiring sendto/recvfrom 614263363Semaste assert (m_udp_send_sockaddr->GetFamily() != 0); 615254721Semaste do 616254721Semaste { 617254721Semaste bytes_sent = ::sendto (m_fd_send, 618263363Semaste (char*)src, 619254721Semaste src_len, 620254721Semaste 0, 621263363Semaste *m_udp_send_sockaddr, 622263363Semaste m_udp_send_sockaddr->GetLength()); 623254721Semaste } while (bytes_sent < 0 && errno == EINTR); 624254721Semaste break; 625254721Semaste } 626254721Semaste 627254721Semaste if (bytes_sent < 0) 628254721Semaste error.SetErrorToErrno (); 629254721Semaste else 630254721Semaste error.Clear (); 631254721Semaste 632254721Semaste if (log) 633254721Semaste { 634254721Semaste switch (m_fd_send_type) 635254721Semaste { 636254721Semaste case eFDTypeFile: // Other FD requireing read/write 637254721Semaste log->Printf ("%p ConnectionFileDescriptor::Write() ::write (fd = %i, src = %p, src_len = %" PRIu64 ") => %" PRIi64 " (error = %s)", 638254721Semaste this, 639254721Semaste m_fd_send, 640254721Semaste src, 641254721Semaste (uint64_t)src_len, 642254721Semaste (int64_t)bytes_sent, 643254721Semaste error.AsCString()); 644254721Semaste break; 645254721Semaste 646254721Semaste case eFDTypeSocket: // Socket requiring send/recv 647254721Semaste log->Printf ("%p ConnectionFileDescriptor::Write() ::send (socket = %i, src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64 " (error = %s)", 648254721Semaste this, 649254721Semaste m_fd_send, 650254721Semaste src, 651254721Semaste (uint64_t)src_len, 652254721Semaste (int64_t)bytes_sent, 653254721Semaste error.AsCString()); 654254721Semaste break; 655254721Semaste 656254721Semaste case eFDTypeSocketUDP: // Unconnected UDP socket requiring sendto/recvfrom 657254721Semaste log->Printf ("%p ConnectionFileDescriptor::Write() ::sendto (socket = %i, src = %p, src_len = %" PRIu64 ", flags = 0) => %" PRIi64 " (error = %s)", 658254721Semaste this, 659254721Semaste m_fd_send, 660254721Semaste src, 661254721Semaste (uint64_t)src_len, 662254721Semaste (int64_t)bytes_sent, 663254721Semaste error.AsCString()); 664254721Semaste break; 665254721Semaste } 666254721Semaste } 667254721Semaste 668254721Semaste if (error_ptr) 669254721Semaste *error_ptr = error; 670254721Semaste 671254721Semaste if (error.Fail()) 672254721Semaste { 673254721Semaste switch (error.GetError()) 674254721Semaste { 675254721Semaste case EAGAIN: 676254721Semaste case EINTR: 677254721Semaste status = eConnectionStatusSuccess; 678254721Semaste return 0; 679254721Semaste 680254721Semaste case ECONNRESET:// The connection is closed by the peer during a read attempt on a socket. 681254721Semaste case ENOTCONN: // A read is attempted on an unconnected socket. 682254721Semaste status = eConnectionStatusLostConnection; 683254721Semaste break; // Break to close.... 684254721Semaste 685254721Semaste default: 686254721Semaste status = eConnectionStatusError; 687254721Semaste break; // Break to close.... 688254721Semaste } 689254721Semaste 690254721Semaste return 0; 691254721Semaste } 692254721Semaste 693254721Semaste status = eConnectionStatusSuccess; 694254721Semaste return bytes_sent; 695254721Semaste} 696254721Semaste 697254721Semaste 698254721Semaste 699254721Semaste#if defined(__APPLE__) 700254721Semaste 701254721Semaste// This ConnectionFileDescriptor::BytesAvailable() uses select(). 702254721Semaste// 703254721Semaste// PROS: 704254721Semaste// - select is consistent across most unix platforms 705254721Semaste// - this Apple specific version allows for unlimited fds in the fd_sets by 706254721Semaste// setting the _DARWIN_UNLIMITED_SELECT define prior to including the 707254721Semaste// required header files. 708254721Semaste 709254721Semaste// CONS: 710254721Semaste// - Darwin only 711254721Semaste 712254721SemasteConnectionStatus 713254721SemasteConnectionFileDescriptor::BytesAvailable (uint32_t timeout_usec, Error *error_ptr) 714254721Semaste{ 715254721Semaste // Don't need to take the mutex here separately since we are only called from Read. If we 716254721Semaste // ever get used more generally we will need to lock here as well. 717254721Semaste 718254721Semaste Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); 719254721Semaste if (log) 720254721Semaste log->Printf("%p ConnectionFileDescriptor::BytesAvailable (timeout_usec = %u)", this, timeout_usec); 721254721Semaste struct timeval *tv_ptr; 722254721Semaste struct timeval tv; 723254721Semaste if (timeout_usec == UINT32_MAX) 724254721Semaste { 725254721Semaste // Infinite wait... 726254721Semaste tv_ptr = NULL; 727254721Semaste } 728254721Semaste else 729254721Semaste { 730254721Semaste TimeValue time_value; 731254721Semaste time_value.OffsetWithMicroSeconds (timeout_usec); 732263363Semaste tv.tv_sec = time_value.seconds(); 733263363Semaste tv.tv_usec = time_value.microseconds(); 734254721Semaste tv_ptr = &tv; 735254721Semaste } 736254721Semaste 737254721Semaste // Make a copy of the file descriptors to make sure we don't 738254721Semaste // have another thread change these values out from under us 739254721Semaste // and cause problems in the loop below where like in FS_SET() 740254721Semaste const int data_fd = m_fd_recv; 741254721Semaste const int pipe_fd = m_pipe_read; 742254721Semaste 743254721Semaste if (data_fd >= 0) 744254721Semaste { 745254721Semaste const bool have_pipe_fd = pipe_fd >= 0; 746254721Semaste 747254721Semaste while (data_fd == m_fd_recv) 748254721Semaste { 749254721Semaste const int nfds = std::max<int>(data_fd, pipe_fd) + 1; 750254721Semaste llvm::SmallVector<fd_set, 1> read_fds; 751254721Semaste read_fds.resize((nfds/FD_SETSIZE) + 1); 752254721Semaste for (size_t i=0; i<read_fds.size(); ++i) 753254721Semaste FD_ZERO (&read_fds[i]); 754254721Semaste // FD_SET doesn't bounds check, it just happily walks off the end 755254721Semaste // but we have taken care of making the extra storage with our 756254721Semaste // SmallVector of fd_set objects 757254721Semaste FD_SET (data_fd, read_fds.data()); 758254721Semaste if (have_pipe_fd) 759254721Semaste FD_SET (pipe_fd, read_fds.data()); 760254721Semaste 761254721Semaste Error error; 762254721Semaste 763254721Semaste if (log) 764254721Semaste { 765254721Semaste if (have_pipe_fd) 766254721Semaste log->Printf("%p ConnectionFileDescriptor::BytesAvailable() ::select (nfds=%i, fds={%i, %i}, NULL, NULL, timeout=%p)...", 767254721Semaste this, nfds, data_fd, pipe_fd, tv_ptr); 768254721Semaste else 769254721Semaste log->Printf("%p ConnectionFileDescriptor::BytesAvailable() ::select (nfds=%i, fds={%i}, NULL, NULL, timeout=%p)...", 770254721Semaste this, nfds, data_fd, tv_ptr); 771254721Semaste } 772254721Semaste 773254721Semaste const int num_set_fds = ::select (nfds, read_fds.data(), NULL, NULL, tv_ptr); 774254721Semaste if (num_set_fds < 0) 775254721Semaste error.SetErrorToErrno(); 776254721Semaste else 777254721Semaste error.Clear(); 778254721Semaste 779254721Semaste if (log) 780254721Semaste { 781254721Semaste if (have_pipe_fd) 782254721Semaste log->Printf("%p ConnectionFileDescriptor::BytesAvailable() ::select (nfds=%i, fds={%i, %i}, NULL, NULL, timeout=%p) => %d, error = %s", 783254721Semaste this, nfds, data_fd, pipe_fd, tv_ptr, num_set_fds, error.AsCString()); 784254721Semaste else 785254721Semaste log->Printf("%p ConnectionFileDescriptor::BytesAvailable() ::select (nfds=%i, fds={%i}, NULL, NULL, timeout=%p) => %d, error = %s", 786254721Semaste this, nfds, data_fd, tv_ptr, num_set_fds, error.AsCString()); 787254721Semaste } 788254721Semaste 789254721Semaste if (error_ptr) 790254721Semaste *error_ptr = error; 791254721Semaste 792254721Semaste if (error.Fail()) 793254721Semaste { 794254721Semaste switch (error.GetError()) 795254721Semaste { 796254721Semaste case EBADF: // One of the descriptor sets specified an invalid descriptor. 797254721Semaste return eConnectionStatusLostConnection; 798254721Semaste 799254721Semaste case EINVAL: // The specified time limit is invalid. One of its components is negative or too large. 800254721Semaste default: // Other unknown error 801254721Semaste return eConnectionStatusError; 802254721Semaste 803254721Semaste case EAGAIN: // The kernel was (perhaps temporarily) unable to 804254721Semaste // allocate the requested number of file descriptors, 805254721Semaste // or we have non-blocking IO 806254721Semaste case EINTR: // A signal was delivered before the time limit 807254721Semaste // expired and before any of the selected events 808254721Semaste // occurred. 809254721Semaste break; // Lets keep reading to until we timeout 810254721Semaste } 811254721Semaste } 812254721Semaste else if (num_set_fds == 0) 813254721Semaste { 814254721Semaste return eConnectionStatusTimedOut; 815254721Semaste } 816254721Semaste else if (num_set_fds > 0) 817254721Semaste { 818254721Semaste // FD_ISSET is happy to deal with a something larger than 819254721Semaste // a single fd_set. 820254721Semaste if (FD_ISSET(data_fd, read_fds.data())) 821254721Semaste return eConnectionStatusSuccess; 822254721Semaste if (have_pipe_fd && FD_ISSET(pipe_fd, read_fds.data())) 823254721Semaste { 824254721Semaste // We got a command to exit. Read the data from that pipe: 825254721Semaste char buffer[16]; 826254721Semaste ssize_t bytes_read; 827254721Semaste 828254721Semaste do 829254721Semaste { 830254721Semaste bytes_read = ::read (pipe_fd, buffer, sizeof(buffer)); 831254721Semaste } while (bytes_read < 0 && errno == EINTR); 832254721Semaste assert (bytes_read == 1 && buffer[0] == 'q'); 833254721Semaste 834254721Semaste if (log) 835254721Semaste log->Printf("%p ConnectionFileDescriptor::BytesAvailable() got data: %*s from the command channel.", 836254721Semaste this, (int) bytes_read, buffer); 837254721Semaste 838254721Semaste return eConnectionStatusEndOfFile; 839254721Semaste } 840254721Semaste } 841254721Semaste } 842254721Semaste } 843254721Semaste 844254721Semaste if (error_ptr) 845254721Semaste error_ptr->SetErrorString("not connected"); 846254721Semaste return eConnectionStatusLostConnection; 847254721Semaste} 848254721Semaste 849254721Semaste#else 850254721Semaste 851254721Semaste// This ConnectionFileDescriptor::BytesAvailable() uses select(). 852254721Semaste// 853254721Semaste// PROS: 854254721Semaste// - select is consistent across most unix platforms 855254721Semaste// CONS: 856254721Semaste// - only supports file descriptors up to FD_SETSIZE. This implementation 857254721Semaste// will assert if it runs into that hard limit to let users know that 858254721Semaste// another ConnectionFileDescriptor::BytesAvailable() should be used 859254721Semaste// or a new version of ConnectionFileDescriptor::BytesAvailable() should 860254721Semaste// be written for the system that is running into the limitations. MacOSX 861254721Semaste// uses kqueues, and there is a poll() based implementation below. 862254721Semaste 863254721SemasteConnectionStatus 864254721SemasteConnectionFileDescriptor::BytesAvailable (uint32_t timeout_usec, Error *error_ptr) 865254721Semaste{ 866254721Semaste // Don't need to take the mutex here separately since we are only called from Read. If we 867254721Semaste // ever get used more generally we will need to lock here as well. 868254721Semaste 869254721Semaste Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); 870254721Semaste if (log) 871254721Semaste log->Printf("%p ConnectionFileDescriptor::BytesAvailable (timeout_usec = %u)", this, timeout_usec); 872254721Semaste struct timeval *tv_ptr; 873254721Semaste struct timeval tv; 874254721Semaste if (timeout_usec == UINT32_MAX) 875254721Semaste { 876254721Semaste // Infinite wait... 877254721Semaste tv_ptr = NULL; 878254721Semaste } 879254721Semaste else 880254721Semaste { 881254721Semaste TimeValue time_value; 882254721Semaste time_value.OffsetWithMicroSeconds (timeout_usec); 883263363Semaste tv.tv_sec = time_value.seconds(); 884263363Semaste tv.tv_usec = time_value.microseconds(); 885254721Semaste tv_ptr = &tv; 886254721Semaste } 887254721Semaste 888254721Semaste // Make a copy of the file descriptors to make sure we don't 889254721Semaste // have another thread change these values out from under us 890254721Semaste // and cause problems in the loop below where like in FS_SET() 891254721Semaste const int data_fd = m_fd_recv; 892254721Semaste const int pipe_fd = m_pipe_read; 893254721Semaste 894254721Semaste if (data_fd >= 0) 895254721Semaste { 896254721Semaste // If this assert fires off on MacOSX, we will need to switch to using 897254721Semaste // libdispatch to read from file descriptors because poll() is causing 898254721Semaste // kernel panics and if we exceed FD_SETSIZE we will have no choice... 899263363Semaste#ifndef _MSC_VER 900254721Semaste assert (data_fd < FD_SETSIZE); 901263363Semaste#endif 902254721Semaste 903254721Semaste const bool have_pipe_fd = pipe_fd >= 0; 904254721Semaste 905254721Semaste if (have_pipe_fd) 906254721Semaste { 907254721Semaste assert (pipe_fd < FD_SETSIZE); 908254721Semaste } 909254721Semaste 910254721Semaste while (data_fd == m_fd_recv) 911254721Semaste { 912254721Semaste fd_set read_fds; 913254721Semaste FD_ZERO (&read_fds); 914254721Semaste FD_SET (data_fd, &read_fds); 915254721Semaste if (have_pipe_fd) 916254721Semaste FD_SET (pipe_fd, &read_fds); 917254721Semaste 918254721Semaste const int nfds = std::max<int>(data_fd, pipe_fd) + 1; 919254721Semaste 920254721Semaste Error error; 921254721Semaste 922254721Semaste if (log) 923254721Semaste { 924254721Semaste if (have_pipe_fd) 925254721Semaste log->Printf("%p ConnectionFileDescriptor::BytesAvailable() ::select (nfds=%i, fds={%i, %i}, NULL, NULL, timeout=%p)...", 926254721Semaste this, nfds, data_fd, pipe_fd, tv_ptr); 927254721Semaste else 928254721Semaste log->Printf("%p ConnectionFileDescriptor::BytesAvailable() ::select (nfds=%i, fds={%i}, NULL, NULL, timeout=%p)...", 929254721Semaste this, nfds, data_fd, tv_ptr); 930254721Semaste } 931254721Semaste 932254721Semaste const int num_set_fds = ::select (nfds, &read_fds, NULL, NULL, tv_ptr); 933254721Semaste if (num_set_fds < 0) 934254721Semaste error.SetErrorToErrno(); 935254721Semaste else 936254721Semaste error.Clear(); 937254721Semaste 938254721Semaste if (log) 939254721Semaste { 940254721Semaste if (have_pipe_fd) 941254721Semaste log->Printf("%p ConnectionFileDescriptor::BytesAvailable() ::select (nfds=%i, fds={%i, %i}, NULL, NULL, timeout=%p) => %d, error = %s", 942254721Semaste this, nfds, data_fd, pipe_fd, tv_ptr, num_set_fds, error.AsCString()); 943254721Semaste else 944254721Semaste log->Printf("%p ConnectionFileDescriptor::BytesAvailable() ::select (nfds=%i, fds={%i}, NULL, NULL, timeout=%p) => %d, error = %s", 945254721Semaste this, nfds, data_fd, tv_ptr, num_set_fds, error.AsCString()); 946254721Semaste } 947254721Semaste 948254721Semaste if (error_ptr) 949254721Semaste *error_ptr = error; 950254721Semaste 951254721Semaste if (error.Fail()) 952254721Semaste { 953254721Semaste switch (error.GetError()) 954254721Semaste { 955254721Semaste case EBADF: // One of the descriptor sets specified an invalid descriptor. 956254721Semaste return eConnectionStatusLostConnection; 957254721Semaste 958254721Semaste case EINVAL: // The specified time limit is invalid. One of its components is negative or too large. 959254721Semaste default: // Other unknown error 960254721Semaste return eConnectionStatusError; 961254721Semaste 962254721Semaste case EAGAIN: // The kernel was (perhaps temporarily) unable to 963254721Semaste // allocate the requested number of file descriptors, 964254721Semaste // or we have non-blocking IO 965254721Semaste case EINTR: // A signal was delivered before the time limit 966254721Semaste // expired and before any of the selected events 967254721Semaste // occurred. 968254721Semaste break; // Lets keep reading to until we timeout 969254721Semaste } 970254721Semaste } 971254721Semaste else if (num_set_fds == 0) 972254721Semaste { 973254721Semaste return eConnectionStatusTimedOut; 974254721Semaste } 975254721Semaste else if (num_set_fds > 0) 976254721Semaste { 977254721Semaste if (FD_ISSET(data_fd, &read_fds)) 978254721Semaste return eConnectionStatusSuccess; 979254721Semaste if (have_pipe_fd && FD_ISSET(pipe_fd, &read_fds)) 980254721Semaste { 981254721Semaste // We got a command to exit. Read the data from that pipe: 982254721Semaste char buffer[16]; 983254721Semaste ssize_t bytes_read; 984254721Semaste 985254721Semaste do 986254721Semaste { 987254721Semaste bytes_read = ::read (pipe_fd, buffer, sizeof(buffer)); 988254721Semaste } while (bytes_read < 0 && errno == EINTR); 989254721Semaste assert (bytes_read == 1 && buffer[0] == 'q'); 990254721Semaste 991254721Semaste if (log) 992254721Semaste log->Printf("%p ConnectionFileDescriptor::BytesAvailable() got data: %*s from the command channel.", 993254721Semaste this, (int) bytes_read, buffer); 994254721Semaste 995254721Semaste return eConnectionStatusEndOfFile; 996254721Semaste } 997254721Semaste } 998254721Semaste } 999254721Semaste } 1000254721Semaste 1001254721Semaste if (error_ptr) 1002254721Semaste error_ptr->SetErrorString("not connected"); 1003254721Semaste return eConnectionStatusLostConnection; 1004254721Semaste} 1005254721Semaste 1006254721Semaste#endif 1007254721Semaste 1008254721Semaste#if 0 1009254721Semaste#include <poll.h> 1010254721Semaste 1011254721Semaste// This ConnectionFileDescriptor::BytesAvailable() uses poll(). poll() should NOT 1012254721Semaste// be used on MacOSX as it has all sorts of restrictions on the types of file descriptors 1013254721Semaste// that it doesn't support. 1014254721Semaste// 1015254721Semaste// There may be some systems that properly support poll() that could use this 1016254721Semaste// implementation. I will let each system opt into this on their own. 1017254721Semaste// 1018254721Semaste// PROS: 1019254721Semaste// - no restrictions on the fd value that is used 1020254721Semaste// CONS: 1021254721Semaste// - varies wildly from platform to platform in its implementation restrictions 1022254721Semaste 1023254721SemasteConnectionStatus 1024254721SemasteConnectionFileDescriptor::BytesAvailable (uint32_t timeout_usec, Error *error_ptr) 1025254721Semaste{ 1026254721Semaste // Don't need to take the mutex here separately since we are only called from Read. If we 1027254721Semaste // ever get used more generally we will need to lock here as well. 1028254721Semaste 1029254721Semaste Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); 1030254721Semaste if (log) 1031254721Semaste log->Printf("%p ConnectionFileDescriptor::BytesAvailable (timeout_usec = %u)", this, timeout_usec); 1032254721Semaste int timeout_msec = 0; 1033254721Semaste if (timeout_usec == UINT32_MAX) 1034254721Semaste { 1035254721Semaste // Infinite wait... 1036254721Semaste timeout_msec = -1; 1037254721Semaste } 1038254721Semaste else if (timeout_usec == 0) 1039254721Semaste { 1040254721Semaste // Return immediately, don't wait 1041254721Semaste timeout_msec = 0; 1042254721Semaste } 1043254721Semaste else 1044254721Semaste { 1045254721Semaste // Convert usec to msec 1046254721Semaste timeout_msec = (timeout_usec + 999) / 1000; 1047254721Semaste } 1048254721Semaste 1049254721Semaste // Make a copy of the file descriptors to make sure we don't 1050254721Semaste // have another thread change these values out from under us 1051254721Semaste // and cause problems in the loop below where like in FS_SET() 1052254721Semaste const int data_fd = m_fd_recv; 1053254721Semaste const int pipe_fd = m_pipe_read; 1054254721Semaste 1055254721Semaste // Make sure the file descriptor can be used with select as it 1056254721Semaste // must be in range 1057254721Semaste if (data_fd >= 0) 1058254721Semaste { 1059254721Semaste const bool have_pipe_fd = pipe_fd >= 0; 1060254721Semaste struct pollfd fds[2] = 1061254721Semaste { 1062254721Semaste { data_fd, POLLIN, 0 }, 1063254721Semaste { pipe_fd, POLLIN, 0 } 1064254721Semaste }; 1065254721Semaste const int nfds = have_pipe_fd ? 2 : 1; 1066254721Semaste Error error; 1067254721Semaste while (data_fd == m_fd_recv) 1068254721Semaste { 1069254721Semaste const int num_set_fds = ::poll (fds, nfds, timeout_msec); 1070254721Semaste 1071254721Semaste if (num_set_fds < 0) 1072254721Semaste error.SetErrorToErrno(); 1073254721Semaste else 1074254721Semaste error.Clear(); 1075254721Semaste 1076254721Semaste if (error_ptr) 1077254721Semaste *error_ptr = error; 1078254721Semaste 1079254721Semaste if (log) 1080254721Semaste { 1081254721Semaste if (have_pipe_fd) 1082254721Semaste log->Printf("%p ConnectionFileDescriptor::BytesAvailable() ::poll (fds={{%i,POLLIN},{%i,POLLIN}}, nfds=%i, timeout_ms=%i) => %d, error = %s\n", 1083254721Semaste this, 1084254721Semaste data_fd, 1085254721Semaste pipe_fd, 1086254721Semaste nfds, 1087254721Semaste timeout_msec, 1088254721Semaste num_set_fds, 1089254721Semaste error.AsCString()); 1090254721Semaste else 1091254721Semaste log->Printf("%p ConnectionFileDescriptor::BytesAvailable() ::poll (fds={{%i,POLLIN}}, nfds=%i, timeout_ms=%i) => %d, error = %s\n", 1092254721Semaste this, 1093254721Semaste data_fd, 1094254721Semaste nfds, 1095254721Semaste timeout_msec, 1096254721Semaste num_set_fds, 1097254721Semaste error.AsCString()); 1098254721Semaste } 1099254721Semaste 1100254721Semaste if (error.Fail()) 1101254721Semaste { 1102254721Semaste switch (error.GetError()) 1103254721Semaste { 1104254721Semaste case EBADF: // One of the descriptor sets specified an invalid descriptor. 1105254721Semaste return eConnectionStatusLostConnection; 1106254721Semaste 1107254721Semaste case EINVAL: // The specified time limit is invalid. One of its components is negative or too large. 1108254721Semaste default: // Other unknown error 1109254721Semaste return eConnectionStatusError; 1110254721Semaste 1111254721Semaste case EAGAIN: // The kernel was (perhaps temporarily) unable to 1112254721Semaste // allocate the requested number of file descriptors, 1113254721Semaste // or we have non-blocking IO 1114254721Semaste case EINTR: // A signal was delivered before the time limit 1115254721Semaste // expired and before any of the selected events 1116254721Semaste // occurred. 1117254721Semaste break; // Lets keep reading to until we timeout 1118254721Semaste } 1119254721Semaste } 1120254721Semaste else if (num_set_fds == 0) 1121254721Semaste { 1122254721Semaste return eConnectionStatusTimedOut; 1123254721Semaste } 1124254721Semaste else if (num_set_fds > 0) 1125254721Semaste { 1126254721Semaste if (fds[0].revents & POLLIN) 1127254721Semaste return eConnectionStatusSuccess; 1128254721Semaste if (fds[1].revents & POLLIN) 1129254721Semaste { 1130254721Semaste // We got a command to exit. Read the data from that pipe: 1131254721Semaste char buffer[16]; 1132254721Semaste ssize_t bytes_read; 1133254721Semaste 1134254721Semaste do 1135254721Semaste { 1136254721Semaste bytes_read = ::read (pipe_fd, buffer, sizeof(buffer)); 1137254721Semaste } while (bytes_read < 0 && errno == EINTR); 1138254721Semaste assert (bytes_read == 1 && buffer[0] == 'q'); 1139254721Semaste 1140254721Semaste if (log) 1141254721Semaste log->Printf("%p ConnectionFileDescriptor::BytesAvailable() got data: %*s from the command channel.", 1142254721Semaste this, (int) bytes_read, buffer); 1143254721Semaste 1144254721Semaste return eConnectionStatusEndOfFile; 1145254721Semaste } 1146254721Semaste } 1147254721Semaste } 1148254721Semaste } 1149254721Semaste if (error_ptr) 1150254721Semaste error_ptr->SetErrorString("not connected"); 1151254721Semaste return eConnectionStatusLostConnection; 1152254721Semaste} 1153254721Semaste 1154254721Semaste#endif 1155254721Semaste 1156254721SemasteConnectionStatus 1157263363SemasteConnectionFileDescriptor::Close (int& fd, FDType type, Error *error_ptr) 1158254721Semaste{ 1159254721Semaste if (error_ptr) 1160254721Semaste error_ptr->Clear(); 1161254721Semaste bool success = true; 1162254721Semaste // Avoid taking a lock if we can 1163254721Semaste if (fd >= 0) 1164254721Semaste { 1165254721Semaste Mutex::Locker locker (m_mutex); 1166254721Semaste // Check the FD after the lock is taken to ensure only one thread 1167254721Semaste // can get into the close scope below 1168254721Semaste if (fd >= 0) 1169254721Semaste { 1170254721Semaste Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); 1171254721Semaste if (log) 1172254721Semaste log->Printf ("%p ConnectionFileDescriptor::Close (fd = %i)", this,fd); 1173263363Semaste#if _WIN32 1174263363Semaste if (type != eFDTypeFile) 1175263363Semaste success = closesocket(fd) == 0; 1176263363Semaste else 1177263363Semaste#endif 1178254721Semaste success = ::close (fd) == 0; 1179254721Semaste // A reference to a FD was passed in, set it to an invalid value 1180254721Semaste fd = -1; 1181254721Semaste if (!success && error_ptr) 1182254721Semaste { 1183254721Semaste // Only set the error if we have been asked to since something else 1184254721Semaste // might have caused us to try and shut down the connection and may 1185254721Semaste // have already set the error. 1186254721Semaste error_ptr->SetErrorToErrno(); 1187254721Semaste } 1188254721Semaste } 1189254721Semaste } 1190254721Semaste if (success) 1191254721Semaste return eConnectionStatusSuccess; 1192254721Semaste else 1193254721Semaste return eConnectionStatusError; 1194254721Semaste} 1195254721Semaste 1196254721SemasteConnectionStatus 1197254721SemasteConnectionFileDescriptor::NamedSocketAccept (const char *socket_name, Error *error_ptr) 1198254721Semaste{ 1199263363Semaste#ifndef LLDB_DISABLE_POSIX 1200254721Semaste ConnectionStatus result = eConnectionStatusError; 1201254721Semaste struct sockaddr_un saddr_un; 1202254721Semaste 1203254721Semaste m_fd_send_type = m_fd_recv_type = eFDTypeSocket; 1204254721Semaste 1205254721Semaste int listen_socket = ::socket (AF_UNIX, SOCK_STREAM, 0); 1206254721Semaste if (listen_socket == -1) 1207254721Semaste { 1208254721Semaste if (error_ptr) 1209254721Semaste error_ptr->SetErrorToErrno(); 1210254721Semaste return eConnectionStatusError; 1211254721Semaste } 1212254721Semaste 1213254721Semaste saddr_un.sun_family = AF_UNIX; 1214254721Semaste ::strncpy(saddr_un.sun_path, socket_name, sizeof(saddr_un.sun_path) - 1); 1215254721Semaste saddr_un.sun_path[sizeof(saddr_un.sun_path) - 1] = '\0'; 1216263363Semaste#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) 1217254721Semaste saddr_un.sun_len = SUN_LEN (&saddr_un); 1218254721Semaste#endif 1219254721Semaste 1220263367Semaste Host::Unlink (socket_name); 1221254721Semaste if (::bind (listen_socket, (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) == 0) 1222254721Semaste { 1223254721Semaste if (::listen (listen_socket, 5) == 0) 1224254721Semaste { 1225254721Semaste m_fd_send = m_fd_recv = ::accept (listen_socket, NULL, 0); 1226254721Semaste if (m_fd_send > 0) 1227254721Semaste { 1228254721Semaste m_should_close_fd = true; 1229254721Semaste 1230254721Semaste if (error_ptr) 1231254721Semaste error_ptr->Clear(); 1232254721Semaste result = eConnectionStatusSuccess; 1233254721Semaste } 1234254721Semaste } 1235254721Semaste } 1236254721Semaste 1237254721Semaste if (result != eConnectionStatusSuccess) 1238254721Semaste { 1239254721Semaste if (error_ptr) 1240254721Semaste error_ptr->SetErrorToErrno(); 1241254721Semaste } 1242254721Semaste // We are done with the listen port 1243263363Semaste Close (listen_socket, eFDTypeSocket, NULL); 1244254721Semaste return result; 1245263363Semaste#else 1246263363Semaste return eConnectionStatusError; 1247263363Semaste#endif 1248254721Semaste} 1249254721Semaste 1250254721SemasteConnectionStatus 1251254721SemasteConnectionFileDescriptor::NamedSocketConnect (const char *socket_name, Error *error_ptr) 1252254721Semaste{ 1253263363Semaste#ifndef LLDB_DISABLE_POSIX 1254254721Semaste Disconnect (NULL); 1255254721Semaste m_fd_send_type = m_fd_recv_type = eFDTypeSocket; 1256254721Semaste 1257254721Semaste // Open the socket that was passed in as an option 1258254721Semaste struct sockaddr_un saddr_un; 1259254721Semaste m_fd_send = m_fd_recv = ::socket (AF_UNIX, SOCK_STREAM, 0); 1260254721Semaste if (m_fd_send == -1) 1261254721Semaste { 1262254721Semaste if (error_ptr) 1263254721Semaste error_ptr->SetErrorToErrno(); 1264254721Semaste return eConnectionStatusError; 1265254721Semaste } 1266254721Semaste 1267254721Semaste saddr_un.sun_family = AF_UNIX; 1268254721Semaste ::strncpy(saddr_un.sun_path, socket_name, sizeof(saddr_un.sun_path) - 1); 1269254721Semaste saddr_un.sun_path[sizeof(saddr_un.sun_path) - 1] = '\0'; 1270263363Semaste#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) 1271254721Semaste saddr_un.sun_len = SUN_LEN (&saddr_un); 1272254721Semaste#endif 1273254721Semaste 1274254721Semaste if (::connect (m_fd_send, (struct sockaddr *)&saddr_un, SUN_LEN (&saddr_un)) < 0) 1275254721Semaste { 1276254721Semaste if (error_ptr) 1277254721Semaste error_ptr->SetErrorToErrno(); 1278254721Semaste Disconnect (NULL); 1279254721Semaste return eConnectionStatusError; 1280254721Semaste } 1281254721Semaste if (error_ptr) 1282254721Semaste error_ptr->Clear(); 1283254721Semaste return eConnectionStatusSuccess; 1284263363Semaste#else 1285263363Semaste return eConnectionStatusError; 1286263363Semaste#endif 1287254721Semaste} 1288254721Semaste 1289254721SemasteConnectionStatus 1290269024SemasteConnectionFileDescriptor::SocketListen (const char *host_and_port, Error *error_ptr) 1291254721Semaste{ 1292254721Semaste Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); 1293254721Semaste if (log) 1294269024Semaste log->Printf ("%p ConnectionFileDescriptor::SocketListen (%s)", this, host_and_port); 1295254721Semaste 1296254721Semaste Disconnect (NULL); 1297254721Semaste m_fd_send_type = m_fd_recv_type = eFDTypeSocket; 1298269024Semaste std::string host_str; 1299269024Semaste std::string port_str; 1300269024Semaste int32_t port = INT32_MIN; 1301269024Semaste if (!DecodeHostAndPort (host_and_port, host_str, port_str, port, error_ptr)) 1302254721Semaste { 1303269024Semaste // Might be just a port number 1304269024Semaste port = Args::StringToSInt32(host_and_port, -1); 1305269024Semaste if (port == -1) 1306269024Semaste return eConnectionStatusError; 1307269024Semaste else 1308269024Semaste host_str.clear(); 1309269024Semaste } 1310269024Semaste const sa_family_t family = AF_INET; 1311269024Semaste const int socktype = SOCK_STREAM; 1312269024Semaste const int protocol = IPPROTO_TCP; 1313269024Semaste int listen_fd = ::socket (family, socktype, protocol); 1314269024Semaste if (listen_fd == -1) 1315269024Semaste { 1316254721Semaste if (error_ptr) 1317254721Semaste error_ptr->SetErrorToErrno(); 1318254721Semaste return eConnectionStatusError; 1319254721Semaste } 1320254721Semaste 1321254721Semaste // enable local address reuse 1322269024Semaste SetSocketOption (listen_fd, SOL_SOCKET, SO_REUSEADDR, 1); 1323254721Semaste 1324269024Semaste SocketAddress listen_addr; 1325269024Semaste if (host_str.empty()) 1326269024Semaste listen_addr.SetToLocalhost(family, port); 1327269024Semaste else if (host_str.compare("*") == 0) 1328269024Semaste listen_addr.SetToAnyAddress(family, port); 1329269024Semaste else 1330254721Semaste { 1331269024Semaste if (!listen_addr.getaddrinfo(host_str.c_str(), port_str.c_str(), family, socktype, protocol)) 1332269024Semaste { 1333269024Semaste if (error_ptr) 1334269024Semaste error_ptr->SetErrorStringWithFormat("unable to resolve hostname '%s'", host_str.c_str()); 1335269024Semaste Close (listen_fd, eFDTypeSocket, NULL); 1336269024Semaste return eConnectionStatusError; 1337269024Semaste } 1338269024Semaste } 1339269024Semaste 1340269024Semaste SocketAddress anyaddr; 1341269024Semaste if (anyaddr.SetToAnyAddress (family, port)) 1342269024Semaste { 1343269024Semaste int err = ::bind (listen_fd, anyaddr, anyaddr.GetLength()); 1344254721Semaste if (err == -1) 1345254721Semaste { 1346254721Semaste if (error_ptr) 1347254721Semaste error_ptr->SetErrorToErrno(); 1348269024Semaste Close (listen_fd, eFDTypeSocket, NULL); 1349254721Semaste return eConnectionStatusError; 1350254721Semaste } 1351254721Semaste 1352269024Semaste err = ::listen (listen_fd, 1); 1353254721Semaste if (err == -1) 1354254721Semaste { 1355254721Semaste if (error_ptr) 1356254721Semaste error_ptr->SetErrorToErrno(); 1357269024Semaste Close (listen_fd, eFDTypeSocket, NULL); 1358254721Semaste return eConnectionStatusError; 1359254721Semaste } 1360254721Semaste 1361269024Semaste // We were asked to listen on port zero which means we 1362269024Semaste // must now read the actual port that was given to us 1363269024Semaste // as port zero is a special code for "find an open port 1364269024Semaste // for me". 1365269024Semaste if (port == 0) 1366269024Semaste port = GetSocketPort(listen_fd); 1367269024Semaste 1368269024Semaste // Set the port predicate since when doing a listen://<host>:<port> 1369269024Semaste // it often needs to accept the incoming connection which is a blocking 1370269024Semaste // system call. Allowing access to the bound port using a predicate allows 1371269024Semaste // us to wait for the port predicate to be set to a non-zero value from 1372269024Semaste // another thread in an efficient manor. 1373269024Semaste m_port_predicate.SetValue(port, eBroadcastAlways); 1374269024Semaste 1375269024Semaste 1376269024Semaste bool accept_connection = false; 1377269024Semaste 1378269024Semaste // Loop until we are happy with our connection 1379269024Semaste while (!accept_connection) 1380269024Semaste { 1381269024Semaste struct sockaddr_in accept_addr; 1382269024Semaste ::memset (&accept_addr, 0, sizeof accept_addr); 1383269024Semaste#if !(defined (__linux__) || defined(_MSC_VER)) 1384269024Semaste accept_addr.sin_len = sizeof accept_addr; 1385269024Semaste#endif 1386269024Semaste socklen_t accept_addr_len = sizeof accept_addr; 1387269024Semaste 1388269024Semaste int fd = ::accept (listen_fd, (struct sockaddr *)&accept_addr, &accept_addr_len); 1389269024Semaste 1390269024Semaste if (fd == -1) 1391269024Semaste { 1392269024Semaste if (error_ptr) 1393269024Semaste error_ptr->SetErrorToErrno(); 1394269024Semaste break; 1395269024Semaste } 1396269024Semaste 1397269024Semaste if (listen_addr.sockaddr_in().sin_addr.s_addr == INADDR_ANY) 1398269024Semaste { 1399269024Semaste accept_connection = true; 1400269024Semaste m_fd_send = m_fd_recv = fd; 1401269024Semaste } 1402269024Semaste else 1403269024Semaste { 1404269024Semaste if ( 1405269024Semaste#if !(defined(__linux__) || (defined(_MSC_VER))) 1406269024Semaste accept_addr_len == listen_addr.sockaddr_in().sin_len && 1407269024Semaste#endif 1408269024Semaste accept_addr.sin_addr.s_addr == listen_addr.sockaddr_in().sin_addr.s_addr) 1409269024Semaste { 1410269024Semaste accept_connection = true; 1411269024Semaste m_fd_send = m_fd_recv = fd; 1412269024Semaste } 1413269024Semaste else 1414269024Semaste { 1415269024Semaste ::close (fd); 1416269024Semaste m_fd_send = m_fd_recv = -1; 1417269024Semaste const uint8_t *accept_ip = (const uint8_t *)&accept_addr.sin_addr.s_addr; 1418269024Semaste const uint8_t *listen_ip = (const uint8_t *)&listen_addr.sockaddr_in().sin_addr.s_addr; 1419269024Semaste ::fprintf (stderr, "error: rejecting incoming connection from %u.%u.%u.%u (expecting %u.%u.%u.%u)\n", 1420269024Semaste accept_ip[0], accept_ip[1], accept_ip[2], accept_ip[3], 1421269024Semaste listen_ip[0], listen_ip[1], listen_ip[2], listen_ip[3]); 1422269024Semaste } 1423269024Semaste } 1424269024Semaste } 1425269024Semaste 1426254721Semaste if (m_fd_send == -1) 1427254721Semaste { 1428269024Semaste Close (listen_fd, eFDTypeSocket, NULL); 1429254721Semaste return eConnectionStatusError; 1430254721Semaste } 1431254721Semaste } 1432254721Semaste 1433254721Semaste // We are done with the listen port 1434269024Semaste Close (listen_fd, eFDTypeSocket, NULL); 1435254721Semaste 1436254721Semaste m_should_close_fd = true; 1437254721Semaste 1438254721Semaste // Keep our TCP packets coming without any delays. 1439254721Semaste SetSocketOption (m_fd_send, IPPROTO_TCP, TCP_NODELAY, 1); 1440254721Semaste if (error_ptr) 1441254721Semaste error_ptr->Clear(); 1442254721Semaste return eConnectionStatusSuccess; 1443254721Semaste} 1444254721Semaste 1445254721SemasteConnectionStatus 1446254721SemasteConnectionFileDescriptor::ConnectTCP (const char *host_and_port, Error *error_ptr) 1447254721Semaste{ 1448254721Semaste Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); 1449254721Semaste if (log) 1450254721Semaste log->Printf ("%p ConnectionFileDescriptor::ConnectTCP (host/port = %s)", this, host_and_port); 1451254721Semaste Disconnect (NULL); 1452254721Semaste 1453254721Semaste m_fd_send_type = m_fd_recv_type = eFDTypeSocket; 1454254721Semaste std::string host_str; 1455254721Semaste std::string port_str; 1456254721Semaste int32_t port = INT32_MIN; 1457254721Semaste if (!DecodeHostAndPort (host_and_port, host_str, port_str, port, error_ptr)) 1458254721Semaste return eConnectionStatusError; 1459254721Semaste 1460254721Semaste // Create the socket 1461254721Semaste m_fd_send = m_fd_recv = ::socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); 1462254721Semaste if (m_fd_send == -1) 1463254721Semaste { 1464254721Semaste if (error_ptr) 1465254721Semaste error_ptr->SetErrorToErrno(); 1466254721Semaste return eConnectionStatusError; 1467254721Semaste } 1468254721Semaste 1469254721Semaste m_should_close_fd = true; 1470254721Semaste 1471254721Semaste // Enable local address reuse 1472254721Semaste SetSocketOption (m_fd_send, SOL_SOCKET, SO_REUSEADDR, 1); 1473254721Semaste 1474254721Semaste struct sockaddr_in sa; 1475254721Semaste ::memset (&sa, 0, sizeof (sa)); 1476254721Semaste sa.sin_family = AF_INET; 1477254721Semaste sa.sin_port = htons (port); 1478254721Semaste 1479254721Semaste int inet_pton_result = ::inet_pton (AF_INET, host_str.c_str(), &sa.sin_addr); 1480254721Semaste 1481254721Semaste if (inet_pton_result <= 0) 1482254721Semaste { 1483254721Semaste struct hostent *host_entry = gethostbyname (host_str.c_str()); 1484254721Semaste if (host_entry) 1485254721Semaste host_str = ::inet_ntoa (*(struct in_addr *)*host_entry->h_addr_list); 1486254721Semaste inet_pton_result = ::inet_pton (AF_INET, host_str.c_str(), &sa.sin_addr); 1487254721Semaste if (inet_pton_result <= 0) 1488254721Semaste { 1489254721Semaste 1490254721Semaste if (error_ptr) 1491254721Semaste { 1492254721Semaste if (inet_pton_result == -1) 1493254721Semaste error_ptr->SetErrorToErrno(); 1494254721Semaste else 1495254721Semaste error_ptr->SetErrorStringWithFormat("invalid host string: '%s'", host_str.c_str()); 1496254721Semaste } 1497254721Semaste Disconnect (NULL); 1498254721Semaste 1499254721Semaste return eConnectionStatusError; 1500254721Semaste } 1501254721Semaste } 1502254721Semaste 1503254721Semaste if (-1 == ::connect (m_fd_send, (const struct sockaddr *)&sa, sizeof(sa))) 1504254721Semaste { 1505254721Semaste if (error_ptr) 1506254721Semaste error_ptr->SetErrorToErrno(); 1507254721Semaste Disconnect (NULL); 1508254721Semaste 1509254721Semaste return eConnectionStatusError; 1510254721Semaste } 1511254721Semaste 1512254721Semaste // Keep our TCP packets coming without any delays. 1513254721Semaste SetSocketOption (m_fd_send, IPPROTO_TCP, TCP_NODELAY, 1); 1514254721Semaste if (error_ptr) 1515254721Semaste error_ptr->Clear(); 1516254721Semaste return eConnectionStatusSuccess; 1517254721Semaste} 1518254721Semaste 1519254721SemasteConnectionStatus 1520254721SemasteConnectionFileDescriptor::ConnectUDP (const char *host_and_port, Error *error_ptr) 1521254721Semaste{ 1522254721Semaste Log *log(lldb_private::GetLogIfAnyCategoriesSet (LIBLLDB_LOG_CONNECTION)); 1523254721Semaste if (log) 1524254721Semaste log->Printf ("%p ConnectionFileDescriptor::ConnectUDP (host/port = %s)", this, host_and_port); 1525254721Semaste Disconnect (NULL); 1526254721Semaste 1527254721Semaste m_fd_send_type = m_fd_recv_type = eFDTypeSocketUDP; 1528254721Semaste 1529254721Semaste std::string host_str; 1530254721Semaste std::string port_str; 1531254721Semaste int32_t port = INT32_MIN; 1532254721Semaste if (!DecodeHostAndPort (host_and_port, host_str, port_str, port, error_ptr)) 1533254721Semaste return eConnectionStatusError; 1534254721Semaste 1535254721Semaste // Setup the receiving end of the UDP connection on this localhost 1536254721Semaste // on port zero. After we bind to port zero we can read the port. 1537254721Semaste m_fd_recv = ::socket (AF_INET, SOCK_DGRAM, 0); 1538254721Semaste if (m_fd_recv == -1) 1539254721Semaste { 1540254721Semaste // Socket creation failed... 1541254721Semaste if (error_ptr) 1542254721Semaste error_ptr->SetErrorToErrno(); 1543254721Semaste } 1544254721Semaste else 1545254721Semaste { 1546254721Semaste // Socket was created, now lets bind to the requested port 1547254721Semaste SocketAddress addr; 1548269024Semaste addr.SetToAnyAddress (AF_INET, 0); 1549254721Semaste 1550254721Semaste if (::bind (m_fd_recv, addr, addr.GetLength()) == -1) 1551254721Semaste { 1552254721Semaste // Bind failed... 1553254721Semaste if (error_ptr) 1554254721Semaste error_ptr->SetErrorToErrno(); 1555254721Semaste Disconnect (NULL); 1556254721Semaste } 1557254721Semaste } 1558254721Semaste 1559254721Semaste if (m_fd_recv == -1) 1560254721Semaste return eConnectionStatusError; 1561254721Semaste 1562254721Semaste // At this point we have setup the recieve port, now we need to 1563254721Semaste // setup the UDP send socket 1564254721Semaste 1565254721Semaste struct addrinfo hints; 1566254721Semaste struct addrinfo *service_info_list = NULL; 1567254721Semaste 1568254721Semaste ::memset (&hints, 0, sizeof(hints)); 1569254721Semaste hints.ai_family = AF_INET; 1570254721Semaste hints.ai_socktype = SOCK_DGRAM; 1571254721Semaste int err = ::getaddrinfo (host_str.c_str(), port_str.c_str(), &hints, &service_info_list); 1572254721Semaste if (err != 0) 1573254721Semaste { 1574254721Semaste if (error_ptr) 1575254721Semaste error_ptr->SetErrorStringWithFormat("getaddrinfo(%s, %s, &hints, &info) returned error %i (%s)", 1576254721Semaste host_str.c_str(), 1577254721Semaste port_str.c_str(), 1578254721Semaste err, 1579254721Semaste gai_strerror(err)); 1580254721Semaste Disconnect (NULL); 1581254721Semaste return eConnectionStatusError; 1582254721Semaste } 1583254721Semaste 1584254721Semaste for (struct addrinfo *service_info_ptr = service_info_list; 1585254721Semaste service_info_ptr != NULL; 1586254721Semaste service_info_ptr = service_info_ptr->ai_next) 1587254721Semaste { 1588254721Semaste m_fd_send = ::socket (service_info_ptr->ai_family, 1589254721Semaste service_info_ptr->ai_socktype, 1590254721Semaste service_info_ptr->ai_protocol); 1591254721Semaste 1592254721Semaste if (m_fd_send != -1) 1593254721Semaste { 1594263363Semaste *m_udp_send_sockaddr = service_info_ptr; 1595254721Semaste break; 1596254721Semaste } 1597254721Semaste else 1598254721Semaste continue; 1599254721Semaste } 1600254721Semaste 1601254721Semaste :: freeaddrinfo (service_info_list); 1602254721Semaste 1603254721Semaste if (m_fd_send == -1) 1604254721Semaste { 1605254721Semaste Disconnect (NULL); 1606254721Semaste return eConnectionStatusError; 1607254721Semaste } 1608254721Semaste 1609254721Semaste if (error_ptr) 1610254721Semaste error_ptr->Clear(); 1611254721Semaste 1612254721Semaste m_should_close_fd = true; 1613254721Semaste return eConnectionStatusSuccess; 1614254721Semaste} 1615254721Semaste 1616263363Semaste#if defined(_WIN32) 1617254721Semastetypedef const char * set_socket_option_arg_type; 1618254721Semastetypedef char * get_socket_option_arg_type; 1619263363Semaste#else // #if defined(_WIN32) 1620254721Semastetypedef const void * set_socket_option_arg_type; 1621254721Semastetypedef void * get_socket_option_arg_type; 1622263363Semaste#endif // #if defined(_WIN32) 1623254721Semaste 1624254721Semasteint 1625254721SemasteConnectionFileDescriptor::GetSocketOption(int fd, int level, int option_name, int &option_value) 1626254721Semaste{ 1627263363Semaste get_socket_option_arg_type option_value_p = reinterpret_cast<get_socket_option_arg_type>(&option_value); 1628254721Semaste socklen_t option_value_size = sizeof(int); 1629254721Semaste return ::getsockopt(fd, level, option_name, option_value_p, &option_value_size); 1630254721Semaste} 1631254721Semaste 1632254721Semasteint 1633254721SemasteConnectionFileDescriptor::SetSocketOption(int fd, int level, int option_name, int option_value) 1634254721Semaste{ 1635263363Semaste set_socket_option_arg_type option_value_p = reinterpret_cast<get_socket_option_arg_type>(&option_value); 1636254721Semaste return ::setsockopt(fd, level, option_name, option_value_p, sizeof(option_value)); 1637254721Semaste} 1638254721Semaste 1639254721Semastebool 1640254721SemasteConnectionFileDescriptor::SetSocketReceiveTimeout (uint32_t timeout_usec) 1641254721Semaste{ 1642254721Semaste switch (m_fd_recv_type) 1643254721Semaste { 1644254721Semaste case eFDTypeFile: // Other FD requireing read/write 1645254721Semaste break; 1646254721Semaste 1647254721Semaste case eFDTypeSocket: // Socket requiring send/recv 1648254721Semaste case eFDTypeSocketUDP: // Unconnected UDP socket requiring sendto/recvfrom 1649254721Semaste { 1650254721Semaste // Check in case timeout for m_fd has already been set to this value 1651254721Semaste if (timeout_usec == m_socket_timeout_usec) 1652254721Semaste return true; 1653254721Semaste //printf ("ConnectionFileDescriptor::SetSocketReceiveTimeout (timeout_usec = %u)\n", timeout_usec); 1654254721Semaste 1655254721Semaste struct timeval timeout; 1656254721Semaste if (timeout_usec == UINT32_MAX) 1657254721Semaste { 1658254721Semaste timeout.tv_sec = 0; 1659254721Semaste timeout.tv_usec = 0; 1660254721Semaste } 1661254721Semaste else if (timeout_usec == 0) 1662254721Semaste { 1663254721Semaste // Sending in zero does an infinite timeout, so set this as low 1664254721Semaste // as we can go to get an effective zero timeout... 1665254721Semaste timeout.tv_sec = 0; 1666254721Semaste timeout.tv_usec = 1; 1667254721Semaste } 1668254721Semaste else 1669254721Semaste { 1670254721Semaste timeout.tv_sec = timeout_usec / TimeValue::MicroSecPerSec; 1671254721Semaste timeout.tv_usec = timeout_usec % TimeValue::MicroSecPerSec; 1672254721Semaste } 1673263363Semaste if (::setsockopt (m_fd_recv, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<get_socket_option_arg_type>(&timeout), sizeof(timeout)) == 0) 1674254721Semaste { 1675254721Semaste m_socket_timeout_usec = timeout_usec; 1676254721Semaste return true; 1677254721Semaste } 1678254721Semaste } 1679254721Semaste } 1680254721Semaste return false; 1681254721Semaste} 1682254721Semaste 1683269024Semasteuint16_t 1684254721SemasteConnectionFileDescriptor::GetSocketPort (int fd) 1685254721Semaste{ 1686254721Semaste // We bound to port zero, so we need to figure out which port we actually bound to 1687269024Semaste if (fd >= 0) 1688269024Semaste { 1689269024Semaste SocketAddress sock_addr; 1690269024Semaste socklen_t sock_addr_len = sock_addr.GetMaxLength (); 1691269024Semaste if (::getsockname (fd, sock_addr, &sock_addr_len) == 0) 1692269024Semaste return sock_addr.GetPort (); 1693269024Semaste } 1694254721Semaste return 0; 1695254721Semaste} 1696254721Semaste 1697254721Semaste// If the read file descriptor is a socket, then return 1698254721Semaste// the port number that is being used by the socket. 1699269024Semasteuint16_t 1700254721SemasteConnectionFileDescriptor::GetReadPort () const 1701254721Semaste{ 1702254721Semaste return ConnectionFileDescriptor::GetSocketPort (m_fd_recv); 1703254721Semaste} 1704254721Semaste 1705254721Semaste// If the write file descriptor is a socket, then return 1706254721Semaste// the port number that is being used by the socket. 1707269024Semasteuint16_t 1708254721SemasteConnectionFileDescriptor::GetWritePort () const 1709254721Semaste{ 1710254721Semaste return ConnectionFileDescriptor::GetSocketPort (m_fd_send); 1711254721Semaste} 1712254721Semaste 1713269024Semasteuint16_t 1714269024SemasteConnectionFileDescriptor::GetBoundPort (uint32_t timeout_sec) 1715269024Semaste{ 1716269024Semaste uint16_t bound_port = 0; 1717269024Semaste if (timeout_sec == UINT32_MAX) 1718269024Semaste m_port_predicate.WaitForValueNotEqualTo (0, bound_port); 1719269024Semaste else 1720269024Semaste { 1721269024Semaste TimeValue timeout = TimeValue::Now(); 1722269024Semaste timeout.OffsetWithSeconds(timeout_sec); 1723269024Semaste m_port_predicate.WaitForValueNotEqualTo (0, bound_port, &timeout); 1724269024Semaste } 1725269024Semaste return bound_port; 1726269024Semaste} 1727