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"
16 #if defined(TIOCSCTTY)
17 #include <sys/ioctl.h>
21 #include "lldb/Host/windows/win32.h"
23 int posix_openpt(int flag) { return 0; }
25 int strerror_r(int errnum, char *buf, size_t buflen) { return 0; }
27 int unlockpt(int fd) { return 0; }
28 int grantpt(int fd) { return 0; }
29 char *ptsname(int fd) { return 0; }
31 pid_t fork(void) { return 0; }
32 pid_t setsid(void) { return 0; }
35 using namespace lldb_utility;
37 //----------------------------------------------------------------------
38 // PseudoTerminal constructor
39 //----------------------------------------------------------------------
40 PseudoTerminal::PseudoTerminal () :
41 m_master_fd(invalid_fd),
42 m_slave_fd(invalid_fd)
46 //----------------------------------------------------------------------
49 // The destructor will close the master and slave file descriptors
50 // if they are valid and ownwership has not been released using the
51 // ReleaseMasterFileDescriptor() or the ReleaseSaveFileDescriptor()
53 //----------------------------------------------------------------------
54 PseudoTerminal::~PseudoTerminal ()
56 CloseMasterFileDescriptor();
57 CloseSlaveFileDescriptor();
60 //----------------------------------------------------------------------
61 // Close the master file descriptor if it is valid.
62 //----------------------------------------------------------------------
64 PseudoTerminal::CloseMasterFileDescriptor ()
68 ::close (m_master_fd);
69 m_master_fd = invalid_fd;
73 //----------------------------------------------------------------------
74 // Close the slave file descriptor if it is valid.
75 //----------------------------------------------------------------------
77 PseudoTerminal::CloseSlaveFileDescriptor ()
82 m_slave_fd = invalid_fd;
86 //----------------------------------------------------------------------
87 // Open the first available pseudo terminal with OFLAG as the
88 // permissions. The file descriptor is stored in this object and can
89 // be accessed with the MasterFileDescriptor() accessor. The
90 // ownership of the master file descriptor can be released using
91 // the ReleaseMasterFileDescriptor() accessor. If this object has
92 // a valid master files descriptor when its destructor is called, it
93 // will close the master file descriptor, therefore clients must
94 // call ReleaseMasterFileDescriptor() if they wish to use the master
95 // file descriptor after this object is out of scope or destroyed.
98 // Zero when successful, non-zero indicating an error occurred.
99 //----------------------------------------------------------------------
101 PseudoTerminal::OpenFirstAvailableMaster (int oflag, char *error_str, size_t error_len)
106 // Open the master side of a pseudo terminal
107 m_master_fd = ::posix_openpt (oflag);
111 ::strerror_r (errno, error_str, error_len);
115 // Grant access to the slave pseudo terminal
116 if (::grantpt (m_master_fd) < 0)
119 ::strerror_r (errno, error_str, error_len);
120 CloseMasterFileDescriptor ();
124 // Clear the lock flag on the slave pseudo terminal
125 if (::unlockpt (m_master_fd) < 0)
128 ::strerror_r (errno, error_str, error_len);
129 CloseMasterFileDescriptor ();
136 //----------------------------------------------------------------------
137 // Open the slave pseudo terminal for the current master pseudo
138 // terminal. A master pseudo terminal should already be valid prior to
139 // calling this function (see OpenFirstAvailableMaster()).
140 // The file descriptor is stored this object's member variables and can
141 // be accessed via the GetSlaveFileDescriptor(), or released using the
142 // ReleaseSlaveFileDescriptor() member function.
145 // Zero when successful, non-zero indicating an error occurred.
146 //----------------------------------------------------------------------
148 PseudoTerminal::OpenSlave (int oflag, char *error_str, size_t error_len)
153 CloseSlaveFileDescriptor();
155 // Open the master side of a pseudo terminal
156 const char *slave_name = GetSlaveName (error_str, error_len);
158 if (slave_name == NULL)
161 m_slave_fd = ::open (slave_name, oflag);
166 ::strerror_r (errno, error_str, error_len);
175 //----------------------------------------------------------------------
176 // Get the name of the slave pseudo terminal. A master pseudo terminal
177 // should already be valid prior to calling this function (see
178 // OpenFirstAvailableMaster()).
181 // NULL if no valid master pseudo terminal or if ptsname() fails.
182 // The name of the slave pseudo terminal as a NULL terminated C string
183 // that comes from static memory, so a copy of the string should be
184 // made as subsequent calls can change this value.
185 //----------------------------------------------------------------------
187 PseudoTerminal::GetSlaveName (char *error_str, size_t error_len) const
195 ::snprintf (error_str, error_len, "%s", "master file descriptor is invalid");
198 const char *slave_name = ::ptsname (m_master_fd);
200 if (error_str && slave_name == NULL)
201 ::strerror_r (errno, error_str, error_len);
207 //----------------------------------------------------------------------
208 // Fork a child process and have its stdio routed to a pseudo terminal.
210 // In the parent process when a valid pid is returned, the master file
211 // descriptor can be used as a read/write access to stdio of the
214 // In the child process the stdin/stdout/stderr will already be routed
215 // to the slave pseudo terminal and the master file descriptor will be
216 // closed as it is no longer needed by the child process.
218 // This class will close the file descriptors for the master/slave
219 // when the destructor is called, so be sure to call
220 // ReleaseMasterFileDescriptor() or ReleaseSlaveFileDescriptor() if any
221 // file descriptors are going to be used past the lifespan of this
225 // in the parent process: the pid of the child, or -1 if fork fails
226 // in the child process: zero
227 //----------------------------------------------------------------------
229 PseudoTerminal::Fork (char *error_str, size_t error_len)
234 pid_t pid = LLDB_INVALID_PROCESS_ID;
235 if (OpenFirstAvailableMaster (O_RDWR, error_str, error_len))
237 // Successfully opened our master pseudo terminal
244 ::strerror_r (errno, error_str, error_len);
251 if (OpenSlave (O_RDWR, error_str, error_len))
253 // Successfully opened slave
254 // We are done with the master in the child process so lets close it
255 CloseMasterFileDescriptor ();
257 #if defined(TIOCSCTTY)
258 // Acquire the controlling terminal
259 if (::ioctl (m_slave_fd, TIOCSCTTY, (char *)0) < 0)
262 ::strerror_r (errno, error_str, error_len);
265 // Duplicate all stdio file descriptors to the slave pseudo terminal
266 if (::dup2 (m_slave_fd, STDIN_FILENO) != STDIN_FILENO)
268 if (error_str && !error_str[0])
269 ::strerror_r (errno, error_str, error_len);
272 if (::dup2 (m_slave_fd, STDOUT_FILENO) != STDOUT_FILENO)
274 if (error_str && !error_str[0])
275 ::strerror_r (errno, error_str, error_len);
278 if (::dup2 (m_slave_fd, STDERR_FILENO) != STDERR_FILENO)
280 if (error_str && !error_str[0])
281 ::strerror_r (errno, error_str, error_len);
288 // Do nothing and let the pid get returned!
294 //----------------------------------------------------------------------
295 // The master file descriptor accessor. This object retains ownership
296 // of the master file descriptor when this accessor is used. Use
297 // ReleaseMasterFileDescriptor() if you wish this object to release
298 // ownership of the master file descriptor.
300 // Returns the master file descriptor, or -1 if the master file
301 // descriptor is not currently valid.
302 //----------------------------------------------------------------------
304 PseudoTerminal::GetMasterFileDescriptor () const
309 //----------------------------------------------------------------------
310 // The slave file descriptor accessor.
312 // Returns the slave file descriptor, or -1 if the slave file
313 // descriptor is not currently valid.
314 //----------------------------------------------------------------------
316 PseudoTerminal::GetSlaveFileDescriptor () const
321 //----------------------------------------------------------------------
322 // Release ownership of the master pseudo terminal file descriptor
323 // without closing it. The destructor for this class will close the
324 // master file descriptor if the ownership isn't released using this
325 // call and the master file descriptor has been opened.
326 //----------------------------------------------------------------------
328 PseudoTerminal::ReleaseMasterFileDescriptor ()
330 // Release ownership of the master pseudo terminal file
331 // descriptor without closing it. (the destructor for this
332 // class will close it otherwise!)
333 int fd = m_master_fd;
334 m_master_fd = invalid_fd;
338 //----------------------------------------------------------------------
339 // Release ownership of the slave pseudo terminal file descriptor
340 // without closing it. The destructor for this class will close the
341 // slave file descriptor if the ownership isn't released using this
342 // call and the slave file descriptor has been opened.
343 //----------------------------------------------------------------------
345 PseudoTerminal::ReleaseSlaveFileDescriptor ()
347 // Release ownership of the slave pseudo terminal file
348 // descriptor without closing it (the destructor for this
349 // class will close it otherwise!)
351 m_slave_fd = invalid_fd;