1 //===-- PseudoTerminal.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Utility/PseudoTerminal.h"
11 #include "lldb/Host/Config.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_utility;
29 //----------------------------------------------------------------------
30 // PseudoTerminal constructor
31 //----------------------------------------------------------------------
32 PseudoTerminal::PseudoTerminal()
33 : m_master_fd(invalid_fd), m_slave_fd(invalid_fd) {}
35 //----------------------------------------------------------------------
38 // The destructor will close the master and slave file descriptors
39 // if they are valid and ownership has not been released using the
40 // ReleaseMasterFileDescriptor() or the ReleaseSaveFileDescriptor()
42 //----------------------------------------------------------------------
43 PseudoTerminal::~PseudoTerminal() {
44 CloseMasterFileDescriptor();
45 CloseSlaveFileDescriptor();
48 //----------------------------------------------------------------------
49 // Close the master file descriptor if it is valid.
50 //----------------------------------------------------------------------
51 void PseudoTerminal::CloseMasterFileDescriptor() {
52 if (m_master_fd >= 0) {
54 m_master_fd = invalid_fd;
58 //----------------------------------------------------------------------
59 // Close the slave file descriptor if it is valid.
60 //----------------------------------------------------------------------
61 void PseudoTerminal::CloseSlaveFileDescriptor() {
62 if (m_slave_fd >= 0) {
64 m_slave_fd = invalid_fd;
68 //----------------------------------------------------------------------
69 // Open the first available pseudo terminal with OFLAG as the
70 // permissions. The file descriptor is stored in this object and can
71 // be accessed with the MasterFileDescriptor() accessor. The
72 // ownership of the master file descriptor can be released using
73 // the ReleaseMasterFileDescriptor() accessor. If this object has
74 // a valid master files descriptor when its destructor is called, it
75 // will close the master file descriptor, therefore clients must
76 // call ReleaseMasterFileDescriptor() if they wish to use the master
77 // file descriptor after this object is out of scope or destroyed.
80 // True when successful, false indicating an error occurred.
81 //----------------------------------------------------------------------
82 bool PseudoTerminal::OpenFirstAvailableMaster(int oflag, char *error_str,
87 #if !defined(LLDB_DISABLE_POSIX)
88 // Open the master side of a pseudo terminal
89 m_master_fd = ::posix_openpt(oflag);
90 if (m_master_fd < 0) {
92 ::strerror_r(errno, error_str, error_len);
96 // Grant access to the slave pseudo terminal
97 if (::grantpt(m_master_fd) < 0) {
99 ::strerror_r(errno, error_str, error_len);
100 CloseMasterFileDescriptor();
104 // Clear the lock flag on the slave pseudo terminal
105 if (::unlockpt(m_master_fd) < 0) {
107 ::strerror_r(errno, error_str, error_len);
108 CloseMasterFileDescriptor();
115 ::snprintf(error_str, error_len, "%s",
116 "pseudo terminal not supported");
121 //----------------------------------------------------------------------
122 // Open the slave pseudo terminal for the current master pseudo
123 // terminal. A master pseudo terminal should already be valid prior to
124 // calling this function (see OpenFirstAvailableMaster()).
125 // The file descriptor is stored this object's member variables and can
126 // be accessed via the GetSlaveFileDescriptor(), or released using the
127 // ReleaseSlaveFileDescriptor() member function.
130 // True when successful, false indicating an error occurred.
131 //----------------------------------------------------------------------
132 bool PseudoTerminal::OpenSlave(int oflag, char *error_str, size_t error_len) {
136 CloseSlaveFileDescriptor();
138 // Open the master side of a pseudo terminal
139 const char *slave_name = GetSlaveName(error_str, error_len);
141 if (slave_name == nullptr)
144 m_slave_fd = ::open(slave_name, oflag);
146 if (m_slave_fd < 0) {
148 ::strerror_r(errno, error_str, error_len);
155 //----------------------------------------------------------------------
156 // Get the name of the slave pseudo terminal. A master pseudo terminal
157 // should already be valid prior to calling this function (see
158 // OpenFirstAvailableMaster()).
161 // NULL if no valid master pseudo terminal or if ptsname() fails.
162 // The name of the slave pseudo terminal as a NULL terminated C string
163 // that comes from static memory, so a copy of the string should be
164 // made as subsequent calls can change this value.
165 //----------------------------------------------------------------------
166 const char *PseudoTerminal::GetSlaveName(char *error_str,
167 size_t error_len) const {
171 if (m_master_fd < 0) {
173 ::snprintf(error_str, error_len, "%s",
174 "master file descriptor is invalid");
177 const char *slave_name = ::ptsname(m_master_fd);
179 if (error_str && slave_name == nullptr)
180 ::strerror_r(errno, error_str, error_len);
185 //----------------------------------------------------------------------
186 // Fork a child process and have its stdio routed to a pseudo terminal.
188 // In the parent process when a valid pid is returned, the master file
189 // descriptor can be used as a read/write access to stdio of the
192 // In the child process the stdin/stdout/stderr will already be routed
193 // to the slave pseudo terminal and the master file descriptor will be
194 // closed as it is no longer needed by the child process.
196 // This class will close the file descriptors for the master/slave
197 // when the destructor is called, so be sure to call
198 // ReleaseMasterFileDescriptor() or ReleaseSlaveFileDescriptor() if any
199 // file descriptors are going to be used past the lifespan of this
203 // in the parent process: the pid of the child, or -1 if fork fails
204 // in the child process: zero
205 //----------------------------------------------------------------------
206 lldb::pid_t PseudoTerminal::Fork(char *error_str, size_t error_len) {
209 pid_t pid = LLDB_INVALID_PROCESS_ID;
210 #if !defined(LLDB_DISABLE_POSIX)
213 if (OpenFirstAvailableMaster(flags, error_str, error_len)) {
214 // Successfully opened our master pseudo terminal
220 ::strerror_r(errno, error_str, error_len);
221 } else if (pid == 0) {
225 if (OpenSlave(O_RDWR, error_str, error_len)) {
226 // Successfully opened slave
228 // Master FD should have O_CLOEXEC set, but let's close it just in
230 CloseMasterFileDescriptor();
232 #if defined(TIOCSCTTY)
233 // Acquire the controlling terminal
234 if (::ioctl(m_slave_fd, TIOCSCTTY, (char *)0) < 0) {
236 ::strerror_r(errno, error_str, error_len);
239 // Duplicate all stdio file descriptors to the slave pseudo terminal
240 if (::dup2(m_slave_fd, STDIN_FILENO) != STDIN_FILENO) {
241 if (error_str && !error_str[0])
242 ::strerror_r(errno, error_str, error_len);
245 if (::dup2(m_slave_fd, STDOUT_FILENO) != STDOUT_FILENO) {
246 if (error_str && !error_str[0])
247 ::strerror_r(errno, error_str, error_len);
250 if (::dup2(m_slave_fd, STDERR_FILENO) != STDERR_FILENO) {
251 if (error_str && !error_str[0])
252 ::strerror_r(errno, error_str, error_len);
257 // Do nothing and let the pid get returned!
264 //----------------------------------------------------------------------
265 // The master file descriptor accessor. This object retains ownership
266 // of the master file descriptor when this accessor is used. Use
267 // ReleaseMasterFileDescriptor() if you wish this object to release
268 // ownership of the master file descriptor.
270 // Returns the master file descriptor, or -1 if the master file
271 // descriptor is not currently valid.
272 //----------------------------------------------------------------------
273 int PseudoTerminal::GetMasterFileDescriptor() const { return m_master_fd; }
275 //----------------------------------------------------------------------
276 // The slave file descriptor accessor.
278 // Returns the slave file descriptor, or -1 if the slave file
279 // descriptor is not currently valid.
280 //----------------------------------------------------------------------
281 int PseudoTerminal::GetSlaveFileDescriptor() const { return m_slave_fd; }
283 //----------------------------------------------------------------------
284 // Release ownership of the master pseudo terminal file descriptor
285 // without closing it. The destructor for this class will close the
286 // master file descriptor if the ownership isn't released using this
287 // call and the master file descriptor has been opened.
288 //----------------------------------------------------------------------
289 int PseudoTerminal::ReleaseMasterFileDescriptor() {
290 // Release ownership of the master pseudo terminal file
291 // descriptor without closing it. (the destructor for this
292 // class will close it otherwise!)
293 int fd = m_master_fd;
294 m_master_fd = invalid_fd;
298 //----------------------------------------------------------------------
299 // Release ownership of the slave pseudo terminal file descriptor
300 // without closing it. The destructor for this class will close the
301 // slave file descriptor if the ownership isn't released using this
302 // call and the slave file descriptor has been opened.
303 //----------------------------------------------------------------------
304 int PseudoTerminal::ReleaseSlaveFileDescriptor() {
305 // Release ownership of the slave pseudo terminal file
306 // descriptor without closing it (the destructor for this
307 // class will close it otherwise!)
309 m_slave_fd = invalid_fd;