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 LLDB_TOOLS_DEBUGSERVER_SOURCE_PSEUDOTERMINAL_H 14 #define LLDB_TOOLS_DEBUGSERVER_SOURCE_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_secondary_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 ClosePrimary(); 43 void CloseSecondary(); 44 Status OpenFirstAvailablePrimary(int oflag); 45 Status OpenSecondary(int oflag); PrimaryFD()46 int PrimaryFD() const { return m_primary_fd; } SecondaryFD()47 int SecondaryFD() const { return m_secondary_fd; } ReleasePrimaryFD()48 int ReleasePrimaryFD() { 49 // Release ownership of the primary pseudo terminal file 50 // descriptor without closing it. (the destructor for this 51 // class will close it otherwise!) 52 int fd = m_primary_fd; 53 m_primary_fd = invalid_fd; 54 return fd; 55 } ReleaseSecondaryFD()56 int ReleaseSecondaryFD() { 57 // Release ownership of the secondary pseudo terminal file 58 // descriptor without closing it (the destructor for this 59 // class will close it otherwise!) 60 int fd = m_secondary_fd; 61 m_secondary_fd = invalid_fd; 62 return fd; 63 } 64 65 const char *SecondaryName() 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_primary_fd; 72 int m_secondary_fd; 73 74 private: 75 PseudoTerminal(const PseudoTerminal &rhs) = delete; 76 PseudoTerminal &operator=(const PseudoTerminal &rhs) = delete; 77 }; 78 79 #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_PSEUDOTERMINAL_H 80