xref: /openbsd-src/gnu/llvm/lldb/tools/debugserver/source/PseudoTerminal.h (revision 1a8dbaac879b9f3335ad7fb25429ce63ac1d6bac)
1 //===-- PseudoTerminal.h ----------------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  Created by Greg Clayton on 1/8/08.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef __PseudoTerminal_h__
14 #define __PseudoTerminal_h__
15 
16 #include <fcntl.h>
17 #include <string>
18 #include <termios.h>
19 
20 class PseudoTerminal {
21 public:
22   enum { invalid_fd = -1, invalid_pid = -1 };
23 
24   enum Status {
25     success = 0,
26     err_posix_openpt_failed = -2,
27     err_grantpt_failed = -3,
28     err_unlockpt_failed = -4,
29     err_ptsname_failed = -5,
30     err_open_slave_failed = -6,
31     err_fork_failed = -7,
32     err_setsid_failed = -8,
33     err_failed_to_acquire_controlling_terminal = -9,
34     err_dup2_failed_on_stdin = -10,
35     err_dup2_failed_on_stdout = -11,
36     err_dup2_failed_on_stderr = -12
37   };
38   // Constructors and Destructors
39   PseudoTerminal();
40   ~PseudoTerminal();
41 
42   void CloseMaster();
43   void CloseSlave();
44   Status OpenFirstAvailableMaster(int oflag);
45   Status OpenSlave(int oflag);
46   int MasterFD() const { return m_master_fd; }
47   int SlaveFD() const { return m_slave_fd; }
48   int ReleaseMasterFD() {
49     // Release ownership of the master pseudo terminal file
50     // descriptor without closing it. (the destructor for this
51     // class will close it otherwise!)
52     int fd = m_master_fd;
53     m_master_fd = invalid_fd;
54     return fd;
55   }
56   int ReleaseSlaveFD() {
57     // Release ownership of the slave pseudo terminal file
58     // descriptor without closing it (the destructor for this
59     // class will close it otherwise!)
60     int fd = m_slave_fd;
61     m_slave_fd = invalid_fd;
62     return fd;
63   }
64 
65   const char *SlaveName() const;
66 
67   pid_t Fork(Status &error);
68 
69 protected:
70   // Classes that inherit from PseudoTerminal can see and modify these
71   int m_master_fd;
72   int m_slave_fd;
73 
74 private:
75   PseudoTerminal(const PseudoTerminal &rhs) = delete;
76   PseudoTerminal &operator=(const PseudoTerminal &rhs) = delete;
77 };
78 
79 #endif // #ifndef __PseudoTerminal_h__
80