summaryrefslogtreecommitdiff
path: root/source/Host/common/PseudoTerminal.cpp
blob: 08d4fa218968f8733c4eb63a2258b63c67b6196b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//===-- PseudoTerminal.cpp --------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Host/PseudoTerminal.h"
#include "lldb/Host/Config.h"

#include "llvm/Support/Errno.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(TIOCSCTTY)
#include <sys/ioctl.h>
#endif

#include "lldb/Host/PosixApi.h"

#if defined(__ANDROID__)
int posix_openpt(int flags);
#endif

using namespace lldb_private;

//----------------------------------------------------------------------
// Write string describing error number
//----------------------------------------------------------------------
static void ErrnoToStr(char *error_str, size_t error_len) {
  std::string strerror = llvm::sys::StrError();
  ::snprintf(error_str, error_len, "%s", strerror.c_str());
}

//----------------------------------------------------------------------
// PseudoTerminal constructor
//----------------------------------------------------------------------
PseudoTerminal::PseudoTerminal()
    : m_master_fd(invalid_fd), m_slave_fd(invalid_fd) {}

//----------------------------------------------------------------------
// Destructor
//
// The destructor will close the master and slave file descriptors if they are
// valid and ownership has not been released using the
// ReleaseMasterFileDescriptor() or the ReleaseSaveFileDescriptor() member
// functions.
//----------------------------------------------------------------------
PseudoTerminal::~PseudoTerminal() {
  CloseMasterFileDescriptor();
  CloseSlaveFileDescriptor();
}

//----------------------------------------------------------------------
// Close the master file descriptor if it is valid.
//----------------------------------------------------------------------
void PseudoTerminal::CloseMasterFileDescriptor() {
  if (m_master_fd >= 0) {
    ::close(m_master_fd);
    m_master_fd = invalid_fd;
  }
}

//----------------------------------------------------------------------
// Close the slave file descriptor if it is valid.
//----------------------------------------------------------------------
void PseudoTerminal::CloseSlaveFileDescriptor() {
  if (m_slave_fd >= 0) {
    ::close(m_slave_fd);
    m_slave_fd = invalid_fd;
  }
}

//----------------------------------------------------------------------
// Open the first available pseudo terminal with OFLAG as the permissions. The
// file descriptor is stored in this object and can be accessed with the
// MasterFileDescriptor() accessor. The ownership of the master file descriptor
// can be released using the ReleaseMasterFileDescriptor() accessor. If this
// object has a valid master files descriptor when its destructor is called, it
// will close the master file descriptor, therefore clients must call
// ReleaseMasterFileDescriptor() if they wish to use the master file descriptor
// after this object is out of scope or destroyed.
//
// RETURNS:
//  True when successful, false indicating an error occurred.
//----------------------------------------------------------------------
bool PseudoTerminal::OpenFirstAvailableMaster(int oflag, char *error_str,
                                              size_t error_len) {
  if (error_str)
    error_str[0] = '\0';

#if !defined(LLDB_DISABLE_POSIX)
  // Open the master side of a pseudo terminal
  m_master_fd = ::posix_openpt(oflag);
  if (m_master_fd < 0) {
    if (error_str)
      ErrnoToStr(error_str, error_len);
    return false;
  }

  // Grant access to the slave pseudo terminal
  if (::grantpt(m_master_fd) < 0) {
    if (error_str)
      ErrnoToStr(error_str, error_len);
    CloseMasterFileDescriptor();
    return false;
  }

  // Clear the lock flag on the slave pseudo terminal
  if (::unlockpt(m_master_fd) < 0) {
    if (error_str)
      ErrnoToStr(error_str, error_len);
    CloseMasterFileDescriptor();
    return false;
  }

  return true;
#else
  if (error_str)
    ::snprintf(error_str, error_len, "%s", "pseudo terminal not supported");
  return false;
#endif
}

//----------------------------------------------------------------------
// Open the slave pseudo terminal for the current master pseudo terminal. A
// master pseudo terminal should already be valid prior to calling this
// function (see OpenFirstAvailableMaster()). The file descriptor is stored
// this object's member variables and can be accessed via the
// GetSlaveFileDescriptor(), or released using the ReleaseSlaveFileDescriptor()
// member function.
//
// RETURNS:
//  True when successful, false indicating an error occurred.
//----------------------------------------------------------------------
bool PseudoTerminal::OpenSlave(int oflag, char *error_str, size_t error_len) {
  if (error_str)
    error_str[0] = '\0';

  CloseSlaveFileDescriptor();

  // Open the master side of a pseudo terminal
  const char *slave_name = GetSlaveName(error_str, error_len);

  if (slave_name == nullptr)
    return false;

  m_slave_fd = ::open(slave_name, oflag);

  if (m_slave_fd < 0) {
    if (error_str)
      ErrnoToStr(error_str, error_len);
    return false;
  }

  return true;
}

//----------------------------------------------------------------------
// Get the name of the slave pseudo terminal. A master pseudo terminal should
// already be valid prior to calling this function (see
// OpenFirstAvailableMaster()).
//
// RETURNS:
//  NULL if no valid master pseudo terminal or if ptsname() fails.
//  The name of the slave pseudo terminal as a NULL terminated C string
//  that comes from static memory, so a copy of the string should be
//  made as subsequent calls can change this value.
//----------------------------------------------------------------------
const char *PseudoTerminal::GetSlaveName(char *error_str,
                                         size_t error_len) const {
  if (error_str)
    error_str[0] = '\0';

  if (m_master_fd < 0) {
    if (error_str)
      ::snprintf(error_str, error_len, "%s",
                 "master file descriptor is invalid");
    return nullptr;
  }
  const char *slave_name = ::ptsname(m_master_fd);

  if (error_str && slave_name == nullptr)
    ErrnoToStr(error_str, error_len);

  return slave_name;
}

//----------------------------------------------------------------------
// Fork a child process and have its stdio routed to a pseudo terminal.
//
// In the parent process when a valid pid is returned, the master file
// descriptor can be used as a read/write access to stdio of the child process.
//
// In the child process the stdin/stdout/stderr will already be routed to the
// slave pseudo terminal and the master file descriptor will be closed as it is
// no longer needed by the child process.
//
// This class will close the file descriptors for the master/slave when the
// destructor is called, so be sure to call ReleaseMasterFileDescriptor() or
// ReleaseSlaveFileDescriptor() if any file descriptors are going to be used
// past the lifespan of this object.
//
// RETURNS:
//  in the parent process: the pid of the child, or -1 if fork fails
//  in the child process: zero
//----------------------------------------------------------------------
lldb::pid_t PseudoTerminal::Fork(char *error_str, size_t error_len) {
  if (error_str)
    error_str[0] = '\0';
  pid_t pid = LLDB_INVALID_PROCESS_ID;
#if !defined(LLDB_DISABLE_POSIX)
  int flags = O_RDWR;
  flags |= O_CLOEXEC;
  if (OpenFirstAvailableMaster(flags, error_str, error_len)) {
    // Successfully opened our master pseudo terminal

    pid = ::fork();
    if (pid < 0) {
      // Fork failed
      if (error_str)
        ErrnoToStr(error_str, error_len);
    } else if (pid == 0) {
      // Child Process
      ::setsid();

      if (OpenSlave(O_RDWR, error_str, error_len)) {
        // Successfully opened slave

        // Master FD should have O_CLOEXEC set, but let's close it just in
        // case...
        CloseMasterFileDescriptor();

#if defined(TIOCSCTTY)
        // Acquire the controlling terminal
        if (::ioctl(m_slave_fd, TIOCSCTTY, (char *)0) < 0) {
          if (error_str)
            ErrnoToStr(error_str, error_len);
        }
#endif
        // Duplicate all stdio file descriptors to the slave pseudo terminal
        if (::dup2(m_slave_fd, STDIN_FILENO) != STDIN_FILENO) {
          if (error_str && !error_str[0])
            ErrnoToStr(error_str, error_len);
        }

        if (::dup2(m_slave_fd, STDOUT_FILENO) != STDOUT_FILENO) {
          if (error_str && !error_str[0])
            ErrnoToStr(error_str, error_len);
        }

        if (::dup2(m_slave_fd, STDERR_FILENO) != STDERR_FILENO) {
          if (error_str && !error_str[0])
            ErrnoToStr(error_str, error_len);
        }
      }
    } else {
      // Parent Process
      // Do nothing and let the pid get returned!
    }
  }
#endif
  return pid;
}

//----------------------------------------------------------------------
// The master file descriptor accessor. This object retains ownership of the
// master file descriptor when this accessor is used. Use
// ReleaseMasterFileDescriptor() if you wish this object to release ownership
// of the master file descriptor.
//
// Returns the master file descriptor, or -1 if the master file descriptor is
// not currently valid.
//----------------------------------------------------------------------
int PseudoTerminal::GetMasterFileDescriptor() const { return m_master_fd; }

//----------------------------------------------------------------------
// The slave file descriptor accessor.
//
// Returns the slave file descriptor, or -1 if the slave file descriptor is not
// currently valid.
//----------------------------------------------------------------------
int PseudoTerminal::GetSlaveFileDescriptor() const { return m_slave_fd; }

//----------------------------------------------------------------------
// Release ownership of the master pseudo terminal file descriptor without
// closing it. The destructor for this class will close the master file
// descriptor if the ownership isn't released using this call and the master
// file descriptor has been opened.
//----------------------------------------------------------------------
int PseudoTerminal::ReleaseMasterFileDescriptor() {
  // Release ownership of the master pseudo terminal file descriptor without
  // closing it. (the destructor for this class will close it otherwise!)
  int fd = m_master_fd;
  m_master_fd = invalid_fd;
  return fd;
}

//----------------------------------------------------------------------
// Release ownership of the slave pseudo terminal file descriptor without
// closing it. The destructor for this class will close the slave file
// descriptor if the ownership isn't released using this call and the slave
// file descriptor has been opened.
//----------------------------------------------------------------------
int PseudoTerminal::ReleaseSlaveFileDescriptor() {
  // Release ownership of the slave pseudo terminal file descriptor without
  // closing it (the destructor for this class will close it otherwise!)
  int fd = m_slave_fd;
  m_slave_fd = invalid_fd;
  return fd;
}