Text of lldb.llvm.org › cpp_reference › PseudoTerminal_8cpp_source.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

LLDB: PseudoTerminal.cpp Source File

LLDB mainline llvm-project lldb source Host common PseudoTerminal.cpp Go to the documentation of this file. 1 //===-- PseudoTerminal.cpp ------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include " lldb/Host/PseudoTerminal.h " 10 #include "lldb/Host/Config.h" 11 #include " lldb/Host/FileSystem.h " 12 #include "llvm/Support/Errc.h" 13 #include "llvm/Support/Errno.h" 14 #include <cassert> 15 #include <climits> 16 #include <cstdio> 17 #include <cstdlib> 18 #include <cstring> 19 #include <mutex> 20 #if defined(TIOCSCTTY) 21 #include <sys/ioctl.h> 22 #endif 23 24 #include " lldb/Host/PosixApi.h " 25 26 #if defined(__APPLE__) 27 #include <Availability.h> 28 #endif 29 30 using namespace lldb_private ; 31 32 // PseudoTerminal constructor 33 PseudoTerminal::PseudoTerminal () = default ; 34 35 // Destructor 36 // 37 // The destructor will close the primary and secondary file descriptors if they 38 // are valid and ownership has not been released using the 39 // ReleasePrimaryFileDescriptor() or the ReleaseSaveFileDescriptor() member 40 // functions.

41 PseudoTerminal::~PseudoTerminal () { Reset (); } 42 43 // Close the primary file descriptor if it is valid. 44 void PseudoTerminal::ClosePrimaryFileDescriptor () { 45 if ( m_primary_fd >= 0) { 46 ::close( m_primary_fd ); 47 m_primary_fd = invalid_fd ; 48 } 49 } 50 51 // Close the secondary file descriptor if it is valid. 52 void PseudoTerminal::CloseSecondaryFileDescriptor () { 53 if ( m_secondary_fd >= 0) { 54 ::close( m_secondary_fd ); 55 m_secondary_fd = invalid_fd ; 56 } 57 } 58 59 void PseudoTerminal::Reset () { 60 ClosePrimaryFileDescriptor (); 61 CloseSecondaryFileDescriptor (); 62 } 63 64 llvm::Error PseudoTerminal::OpenFirstAvailablePrimary ( int oflag) { 65 Reset (); 66 #if LLDB_ENABLE_POSIX 67 #if defined(__OpenBSD__) 68 // OpenBSD's posix_openpt() fails with EINVAL if O_CLOEXEC is passed, so set 69 // FD_CLOEXEC after opening instead.

70 const bool cloexec = oflag & O_CLOEXEC; 71 oflag &= ~O_CLOEXEC; 72 #endif 73 74 // Open the primary side of a pseudo terminal 75 m_primary_fd = ::posix_openpt (oflag); 76 if ( m_primary_fd < 0) { 77 return llvm::errorCodeToError( 78 std::error_code(errno, std::generic_category())); 79 } 80 81 #if defined(__OpenBSD__) 82 if (cloexec && ::fcntl( m_primary_fd , F_SETFD, FD_CLOEXEC) == -1) { 83 std::error_code EC(errno, std::generic_category()); 84 ClosePrimaryFileDescriptor (); 85 return llvm::errorCodeToError(EC); 86 } 87 #endif 88 89 // Grant access to the secondary pseudo terminal 90 if ( ::grantpt ( m_primary_fd ) < 0) { 91 std::error_code EC(errno, std::generic_category()); 92 ClosePrimaryFileDescriptor (); 93 return llvm::errorCodeToError(EC); 94 } 95 96 // Clear the lock flag on the secondary pseudo terminal 97 if ( ::unlockpt ( m_primary_fd ) < 0) { 98 std::error_code EC(errno, std::generic_category()); 99 ClosePrimaryFileDescriptor (); 100 return llvm::errorCodeToError(EC); 101 } 102 103 return llvm::Error::success(); 104 #else 105 return llvm::errorCodeToError(llvm::errc::not_supported); 106 #endif 107 } 108 109 llvm::Error PseudoTerminal::OpenSecondary ( int oflag) { 110 CloseSecondaryFileDescriptor (); 111 112 std::string name = GetSecondaryName (); 113 m_secondary_fd = FileSystem::Instance ().

Open (name.c_str(), oflag); 114 if ( m_secondary_fd >= 0) 115 return llvm::Error::success(); 116 117 return llvm::errorCodeToError( 118 std::error_code(errno, std::generic_category())); 119 } 120 121 #if !HAVE_PTSNAME_R || defined(__APPLE__) 122 static std::string use_ptsname ( int fd) { 123 static std::mutex mutex; 124 std::lock_guard<std::mutex> guard(mutex); 125 const char *r = ptsname (fd); 126 assert(r != nullptr ); 127 return r; 128 } 129 #endif 130 131 std::string PseudoTerminal::GetSecondaryName () const { 132 assert( m_primary_fd >= 0); 133 #if HAVE_PTSNAME_R 134 #if defined(__APPLE__) 135 if (__builtin_available(macos 10.13.4, iOS 11.3, tvOS 11.3, watchOS 4.4, *)) { 136 #endif 137 char buf[ PATH_MAX ]; 138 buf[0] = '\0' ; 139 int r = ptsname_r( m_primary_fd , buf, sizeof (buf)); 140 UNUSED_IF_ASSERT_DISABLED (r); 141 assert(r == 0); 142 return buf; 143 #if defined(__APPLE__) 144 } else { 145 return use_ptsname ( m_primary_fd ); 146 } 147 #endif 148 #else 149 return use_ptsname ( m_primary_fd ); 150 #endif 151 } 152 153 llvm::Expected<lldb::pid_t> PseudoTerminal::Fork () { 154 #if LLDB_ENABLE_POSIX 155 if (llvm::Error Err = OpenFirstAvailablePrimary (O_RDWR | O_CLOEXEC)) 156 return std::move(Err); 157 158 pid_t pid = ::fork (); 159 if (pid < 0) { 160 return llvm::errorCodeToError( 161 std::error_code(errno, std::generic_category())); 162 } 163 if (pid > 0) { 164 // Parent process.

165 return pid; 166 } 167 168 // Child Process 169 ::setsid (); 170 171 if (llvm::Error Err = OpenSecondary (O_RDWR)) 172 return std::move(Err); 173 174 // Primary FD should have O_CLOEXEC set, but let's close it just in 175 // case... 176 ClosePrimaryFileDescriptor (); 177 178 #if defined(TIOCSCTTY) 179 // Acquire the controlling terminal 180 if (::ioctl( m_secondary_fd , TIOCSCTTY, ( char *)0) < 0) { 181 return llvm::errorCodeToError( 182 std::error_code(errno, std::generic_category())); 183 } 184 #endif 185 // Duplicate all stdio file descriptors to the secondary pseudo terminal 186 for ( int fd : {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO}) { 187 if (::dup2( m_secondary_fd , fd) != fd) { 188 return llvm::errorCodeToError( 189 std::error_code(errno, std::generic_category())); 190 } 191 } 192 #endif 193 return 0; 194 } 195 196 // The primary file descriptor accessor.

This object retains ownership of the 197 // primary file descriptor when this accessor is used. Use 198 // ReleasePrimaryFileDescriptor() if you wish this object to release ownership 199 // of the primary file descriptor. 200 // 201 // Returns the primary file descriptor, or -1 if the primary file descriptor is 202 // not currently valid. 203 int PseudoTerminal::GetPrimaryFileDescriptor () const { return m_primary_fd ; } 204 205 // The secondary file descriptor accessor.

206 // 207 // Returns the secondary file descriptor, or -1 if the secondary file descriptor 208 // is not currently valid. 209 int PseudoTerminal::GetSecondaryFileDescriptor () const { 210 return m_secondary_fd ; 211 } 212 213 // Release ownership of the primary pseudo terminal file descriptor without 214 // closing it. The destructor for this class will close the primary file 215 // descriptor if the ownership isn't released using this call and the primary 216 // file descriptor has been opened.

217 int PseudoTerminal::ReleasePrimaryFileDescriptor () { 218 // Release ownership of the primary pseudo terminal file descriptor without 219 // closing it. (the destructor for this class will close it otherwise!) 220 int fd = m_primary_fd ; 221 m_primary_fd = invalid_fd ; 222 return fd; 223 } 224 225 // Release ownership of the secondary pseudo terminal file descriptor without 226 // closing it. The destructor for this class will close the secondary file 227 // descriptor if the ownership isn't released using this call and the secondary 228 // file descriptor has been opened.

229 int PseudoTerminal::ReleaseSecondaryFileDescriptor () { 230 // Release ownership of the secondary pseudo terminal file descriptor without 231 // closing it (the destructor for this class will close it otherwise!) 232 int fd = m_secondary_fd ; 233 m_secondary_fd = invalid_fd ; 234 return fd; 235 } FileSystem.h PosixApi.h use_ptsname static std::string use_ptsname(int fd) Definition PseudoTerminal.cpp:122 PseudoTerminal.h lldb_private::FileSystem::Open int Open(const char *path, int flags, int mode=0600) Wraps open in a platform-independent way.

Definition FileSystemPosix.cpp:69

The rest of this page is on the live site.