summaryrefslogtreecommitdiff
path: root/tools/debugserver/source/PseudoTerminal.h
blob: f6b71f8909a6c2595012159331d329e7904dfa03 (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
//===-- PseudoTerminal.h ----------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  Created by Greg Clayton on 1/8/08.
//
//===----------------------------------------------------------------------===//

#ifndef __PseudoTerminal_h__
#define __PseudoTerminal_h__

#include <fcntl.h>
#include <string>
#include <termios.h>

class PseudoTerminal {
public:
  enum { invalid_fd = -1, invalid_pid = -1 };

  enum Status {
    success = 0,
    err_posix_openpt_failed = -2,
    err_grantpt_failed = -3,
    err_unlockpt_failed = -4,
    err_ptsname_failed = -5,
    err_open_slave_failed = -6,
    err_fork_failed = -7,
    err_setsid_failed = -8,
    err_failed_to_acquire_controlling_terminal = -9,
    err_dup2_failed_on_stdin = -10,
    err_dup2_failed_on_stdout = -11,
    err_dup2_failed_on_stderr = -12
  };
  //------------------------------------------------------------------
  // Constructors and Destructors
  //------------------------------------------------------------------
  PseudoTerminal();
  ~PseudoTerminal();

  void CloseMaster();
  void CloseSlave();
  Status OpenFirstAvailableMaster(int oflag);
  Status OpenSlave(int oflag);
  int MasterFD() const { return m_master_fd; }
  int SlaveFD() const { return m_slave_fd; }
  int ReleaseMasterFD() {
    // 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;
  }
  int ReleaseSlaveFD() {
    // 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;
  }

  const char *SlaveName() const;

  pid_t Fork(Status &error);

protected:
  //------------------------------------------------------------------
  // Classes that inherit from PseudoTerminal can see and modify these
  //------------------------------------------------------------------
  int m_master_fd;
  int m_slave_fd;

private:
  //------------------------------------------------------------------
  // Outlaw copy and assignment constructors
  //------------------------------------------------------------------
  PseudoTerminal(const PseudoTerminal &rhs);
  PseudoTerminal &operator=(const PseudoTerminal &rhs);
};

#endif // #ifndef __PseudoTerminal_h__