1 //===-- PseudoTerminal.cpp --------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Host/PseudoTerminal.h"
10 #include "lldb/Host/Config.h"
12 #include "llvm/Support/Errno.h"
17 #if defined(TIOCSCTTY)
18 #include <sys/ioctl.h>
21 #include "lldb/Host/PosixApi.h"
23 #if defined(__ANDROID__)
24 int posix_openpt(int flags);
27 using namespace lldb_private;
29 // Write string describing error number
30 static void ErrnoToStr(char *error_str, size_t error_len) {
31 std::string strerror = llvm::sys::StrError();
32 ::snprintf(error_str, error_len, "%s", strerror.c_str());
35 // PseudoTerminal constructor
36 PseudoTerminal::PseudoTerminal()
37 : m_master_fd(invalid_fd), m_slave_fd(invalid_fd) {}
41 // The destructor will close the master and slave file descriptors if they are
42 // valid and ownership has not been released using the
43 // ReleaseMasterFileDescriptor() or the ReleaseSaveFileDescriptor() member
45 PseudoTerminal::~PseudoTerminal() {
46 CloseMasterFileDescriptor();
47 CloseSlaveFileDescriptor();
50 // Close the master file descriptor if it is valid.
51 void PseudoTerminal::CloseMasterFileDescriptor() {
52 if (m_master_fd >= 0) {
54 m_master_fd = invalid_fd;
58 // Close the slave file descriptor if it is valid.
59 void PseudoTerminal::CloseSlaveFileDescriptor() {
60 if (m_slave_fd >= 0) {
62 m_slave_fd = invalid_fd;
66 // Open the first available pseudo terminal with OFLAG as the permissions. The
67 // file descriptor is stored in this object and can be accessed with the
68 // MasterFileDescriptor() accessor. The ownership of the master file descriptor
69 // can be released using the ReleaseMasterFileDescriptor() accessor. If this
70 // object has a valid master files descriptor when its destructor is called, it
71 // will close the master file descriptor, therefore clients must call
72 // ReleaseMasterFileDescriptor() if they wish to use the master file descriptor
73 // after this object is out of scope or destroyed.
76 // True when successful, false indicating an error occurred.
77 bool PseudoTerminal::OpenFirstAvailableMaster(int oflag, char *error_str,
82 #if !defined(LLDB_DISABLE_POSIX)
83 // Open the master side of a pseudo terminal
84 m_master_fd = ::posix_openpt(oflag);
85 if (m_master_fd < 0) {
87 ErrnoToStr(error_str, error_len);
91 // Grant access to the slave pseudo terminal
92 if (::grantpt(m_master_fd) < 0) {
94 ErrnoToStr(error_str, error_len);
95 CloseMasterFileDescriptor();
99 // Clear the lock flag on the slave pseudo terminal
100 if (::unlockpt(m_master_fd) < 0) {
102 ErrnoToStr(error_str, error_len);
103 CloseMasterFileDescriptor();
110 ::snprintf(error_str, error_len, "%s", "pseudo terminal not supported");
115 // Open the slave pseudo terminal for the current master pseudo terminal. A
116 // master pseudo terminal should already be valid prior to calling this
117 // function (see OpenFirstAvailableMaster()). The file descriptor is stored
118 // this object's member variables and can be accessed via the
119 // GetSlaveFileDescriptor(), or released using the ReleaseSlaveFileDescriptor()
123 // True when successful, false indicating an error occurred.
124 bool PseudoTerminal::OpenSlave(int oflag, char *error_str, size_t error_len) {
128 CloseSlaveFileDescriptor();
130 // Open the master side of a pseudo terminal
131 const char *slave_name = GetSlaveName(error_str, error_len);
133 if (slave_name == nullptr)
136 m_slave_fd = llvm::sys::RetryAfterSignal(-1, ::open, slave_name, oflag);
138 if (m_slave_fd < 0) {
140 ErrnoToStr(error_str, error_len);
147 // Get the name of the slave pseudo terminal. A master pseudo terminal should
148 // already be valid prior to calling this function (see
149 // OpenFirstAvailableMaster()).
152 // NULL if no valid master pseudo terminal or if ptsname() fails.
153 // The name of the slave pseudo terminal as a NULL terminated C string
154 // that comes from static memory, so a copy of the string should be
155 // made as subsequent calls can change this value.
156 const char *PseudoTerminal::GetSlaveName(char *error_str,
157 size_t error_len) const {
161 if (m_master_fd < 0) {
163 ::snprintf(error_str, error_len, "%s",
164 "master file descriptor is invalid");
167 const char *slave_name = ::ptsname(m_master_fd);
169 if (error_str && slave_name == nullptr)
170 ErrnoToStr(error_str, error_len);
175 // Fork a child process and have its stdio routed to a pseudo terminal.
177 // In the parent process when a valid pid is returned, the master file
178 // descriptor can be used as a read/write access to stdio of the child process.
180 // In the child process the stdin/stdout/stderr will already be routed to the
181 // slave pseudo terminal and the master file descriptor will be closed as it is
182 // no longer needed by the child process.
184 // This class will close the file descriptors for the master/slave when the
185 // destructor is called, so be sure to call ReleaseMasterFileDescriptor() or
186 // ReleaseSlaveFileDescriptor() if any file descriptors are going to be used
187 // past the lifespan of this object.
190 // in the parent process: the pid of the child, or -1 if fork fails
191 // in the child process: zero
192 lldb::pid_t PseudoTerminal::Fork(char *error_str, size_t error_len) {
195 pid_t pid = LLDB_INVALID_PROCESS_ID;
196 #if !defined(LLDB_DISABLE_POSIX)
199 if (OpenFirstAvailableMaster(flags, error_str, error_len)) {
200 // Successfully opened our master pseudo terminal
206 ErrnoToStr(error_str, error_len);
207 } else if (pid == 0) {
211 if (OpenSlave(O_RDWR, error_str, error_len)) {
212 // Successfully opened slave
214 // Master FD should have O_CLOEXEC set, but let's close it just in
216 CloseMasterFileDescriptor();
218 #if defined(TIOCSCTTY)
219 // Acquire the controlling terminal
220 if (::ioctl(m_slave_fd, TIOCSCTTY, (char *)0) < 0) {
222 ErrnoToStr(error_str, error_len);
225 // Duplicate all stdio file descriptors to the slave pseudo terminal
226 if (::dup2(m_slave_fd, STDIN_FILENO) != STDIN_FILENO) {
227 if (error_str && !error_str[0])
228 ErrnoToStr(error_str, error_len);
231 if (::dup2(m_slave_fd, STDOUT_FILENO) != STDOUT_FILENO) {
232 if (error_str && !error_str[0])
233 ErrnoToStr(error_str, error_len);
236 if (::dup2(m_slave_fd, STDERR_FILENO) != STDERR_FILENO) {
237 if (error_str && !error_str[0])
238 ErrnoToStr(error_str, error_len);
243 // Do nothing and let the pid get returned!
250 // The master file descriptor accessor. This object retains ownership of the
251 // master file descriptor when this accessor is used. Use
252 // ReleaseMasterFileDescriptor() if you wish this object to release ownership
253 // of the master file descriptor.
255 // Returns the master file descriptor, or -1 if the master file descriptor is
256 // not currently valid.
257 int PseudoTerminal::GetMasterFileDescriptor() const { return m_master_fd; }
259 // The slave file descriptor accessor.
261 // Returns the slave file descriptor, or -1 if the slave file descriptor is not
263 int PseudoTerminal::GetSlaveFileDescriptor() const { return m_slave_fd; }
265 // Release ownership of the master pseudo terminal file descriptor without
266 // closing it. The destructor for this class will close the master file
267 // descriptor if the ownership isn't released using this call and the master
268 // file descriptor has been opened.
269 int PseudoTerminal::ReleaseMasterFileDescriptor() {
270 // Release ownership of the master pseudo terminal file descriptor without
271 // closing it. (the destructor for this class will close it otherwise!)
272 int fd = m_master_fd;
273 m_master_fd = invalid_fd;
277 // Release ownership of the slave pseudo terminal file descriptor without
278 // closing it. The destructor for this class will close the slave file
279 // descriptor if the ownership isn't released using this call and the slave
280 // file descriptor has been opened.
281 int PseudoTerminal::ReleaseSlaveFileDescriptor() {
282 // Release ownership of the slave pseudo terminal file descriptor without
283 // closing it (the destructor for this class will close it otherwise!)
285 m_slave_fd = invalid_fd;