LLDB mainline
PseudoTerminal.h
Go to the documentation of this file.
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#ifndef LLDB_HOST_PSEUDOTERMINAL_H
10#define LLDB_HOST_PSEUDOTERMINAL_H
11
12#include "lldb/lldb-defines.h"
13#include "llvm/Support/Error.h"
14#include <fcntl.h>
15#include <string>
16
17namespace lldb_private {
18
19/// \class PseudoTerminal PseudoTerminal.h "lldb/Host/PseudoTerminal.h"
20/// A pseudo terminal helper class.
21///
22/// The pseudo terminal class abstracts the use of pseudo terminals on the
23/// host system.
25public:
26 enum {
27 invalid_fd = -1 ///< Invalid file descriptor value
28 };
29
30 /// Constructs this object with invalid primary and secondary file
31 /// descriptors.
33
34 /// The destructor will close the primary and secondary file
35 /// descriptor/HANDLEs if they are valid and ownership has not been released.
36 ///
37 /// \see PseudoTerminal::ReleasePrimaryFileDescriptor()
38 /// \see PseudoTerminal::ReleaseSecondaryFileDescriptor()
40
41 /// Close the primary file descriptor if it is valid.
43
44 /// Close the secondary file descriptor if it is valid.
46
47 /// Close both file descriptors and clear cached state. Used to recycle a
48 /// PseudoTerminal across multiple launches without invalidating any
49 /// outstanding shared_ptr references.
50 void Reset();
51
52 /// Fork a child process that uses pseudo terminals for its stdio.
53 ///
54 /// In the parent process, a call to this function results in a pid being
55 /// returned. If the pid is valid, the primary file descriptor can be used
56 /// for read/write access to stdio of the child process.
57 ///
58 /// In the child process the stdin/stdout/stderr will already be routed to
59 /// the secondary pseudo terminal and the primary file descriptor will be
60 /// closed as it is no longer needed by the child process.
61 ///
62 /// This class will close the file descriptors for the primary/secondary when
63 /// the destructor is called. The file handles can be released using one of:
64 /// @li PseudoTerminal::ReleasePrimaryFileDescriptor()
65 /// @li PseudoTerminal::ReleaseSecondaryFileDescriptor()
66 ///
67 /// \return
68 /// \b Parent process: a child process ID that is greater
69 /// than zero, or an error if the fork fails.
70 /// \b Child process: zero.
71 ///
72 /// \see PseudoTerminal::ReleasePrimaryFileDescriptor()
73 /// \see PseudoTerminal::ReleaseSecondaryFileDescriptor()
74 llvm::Expected<lldb::pid_t> Fork();
75
76 /// The primary file descriptor accessor.
77 ///
78 /// This object retains ownership of the primary file descriptor when this
79 /// accessor is used. Users can call the member function
80 /// PseudoTerminal::ReleasePrimaryFileDescriptor() if this object should
81 /// release ownership of the secondary file descriptor.
82 ///
83 /// \return
84 /// The primary file descriptor, or PseudoTerminal::invalid_fd
85 /// if the primary file descriptor is not currently valid.
86 ///
87 /// \see PseudoTerminal::ReleasePrimaryFileDescriptor()
88 int GetPrimaryFileDescriptor() const;
89
90 /// The secondary file descriptor accessor.
91 ///
92 /// This object retains ownership of the secondary file descriptor when this
93 /// accessor is used. Users can call the member function
94 /// PseudoTerminal::ReleaseSecondaryFileDescriptor() if this object should
95 /// release ownership of the secondary file descriptor.
96 ///
97 /// \return
98 /// The secondary file descriptor, or PseudoTerminal::invalid_fd
99 /// if the secondary file descriptor is not currently valid.
100 ///
101 /// \see PseudoTerminal::ReleaseSecondaryFileDescriptor()
102 int GetSecondaryFileDescriptor() const;
103
104 /// Get the name of the secondary pseudo terminal.
105 ///
106 /// A primary pseudo terminal should already be valid prior to
107 /// calling this function.
108 ///
109 /// \return
110 /// The name of the secondary pseudo terminal.
111 ///
112 /// \see PseudoTerminal::OpenFirstAvailablePrimary()
113 std::string GetSecondaryName() const;
114
115 /// Open the first available pseudo terminal.
116 ///
117 /// Opens the first available pseudo terminal with \a oflag as the
118 /// permissions. The opened primary file descriptor is stored in this object
119 /// and can be accessed by calling the
120 /// PseudoTerminal::GetPrimaryFileDescriptor() accessor. Clients can call the
121 /// PseudoTerminal::ReleasePrimaryFileDescriptor() accessor function if they
122 /// wish to use the primary file descriptor beyond the lifespan of this
123 /// object.
124 ///
125 /// If this object still has a valid primary file descriptor when its
126 /// destructor is called, it will close it.
127 ///
128 /// \param[in] oflag
129 /// Flags to use when calling \c posix_openpt(\a oflag).
130 /// A value of "O_RDWR|O_NOCTTY" is suggested.
131 ///
132 /// \see PseudoTerminal::GetPrimaryFileDescriptor() @see
133 /// PseudoTerminal::ReleasePrimaryFileDescriptor()
134 llvm::Error OpenFirstAvailablePrimary(int oflag);
135
136 /// Open the secondary for the current primary pseudo terminal.
137 ///
138 /// A primary pseudo terminal should already be valid prior to
139 /// calling this function. The opened secondary file descriptor is stored in
140 /// this object and can be accessed by calling the
141 /// PseudoTerminal::GetSecondaryFileDescriptor() accessor. Clients can call
142 /// the PseudoTerminal::ReleaseSecondaryFileDescriptor() accessor function if
143 /// they wish to use the secondary file descriptor beyond the lifespan of this
144 /// object.
145 ///
146 /// If this object still has a valid secondary file descriptor when its
147 /// destructor is called, it will close it.
148 ///
149 /// \param[in] oflag
150 /// Flags to use when calling \c open(\a oflag).
151 ///
152 /// \see PseudoTerminal::OpenFirstAvailablePrimary() @see
153 /// PseudoTerminal::GetSecondaryFileDescriptor() @see
154 /// PseudoTerminal::ReleaseSecondaryFileDescriptor()
155 llvm::Error OpenSecondary(int oflag);
156
157 /// Release the primary file descriptor.
158 ///
159 /// Releases ownership of the primary pseudo terminal file descriptor without
160 /// closing it. The destructor for this class will close the primary file
161 /// descriptor if the ownership isn't released using this call and the
162 /// primary file descriptor has been opened.
163 ///
164 /// \return
165 /// The primary file descriptor, or PseudoTerminal::invalid_fd
166 /// if the mast file descriptor is not currently valid.
168
169 /// Release the secondary file descriptor.
170 ///
171 /// Release ownership of the secondary pseudo terminal file descriptor without
172 /// closing it. The destructor for this class will close the secondary file
173 /// descriptor if the ownership isn't released using this call and the
174 /// secondary file descriptor has been opened.
175 ///
176 /// \return
177 /// The secondary file descriptor, or PseudoTerminal::invalid_fd
178 /// if the secondary file descriptor is not currently valid.
180
181protected:
182 // Member variables
183 int m_primary_fd = invalid_fd; ///< The file descriptor for the primary.
184 int m_secondary_fd = invalid_fd; ///< The file descriptor for the secondary.
185
186private:
188 const PseudoTerminal &operator=(const PseudoTerminal &) = delete;
189};
190
191} // namespace lldb_private
192
193#endif // LLDB_HOST_PSEUDOTERMINAL_H
PseudoTerminal()
Constructs this object with invalid primary and secondary file descriptors.
const PseudoTerminal & operator=(const PseudoTerminal &)=delete
int m_secondary_fd
The file descriptor for the secondary.
int ReleaseSecondaryFileDescriptor()
Release the secondary file descriptor.
llvm::Error OpenFirstAvailablePrimary(int oflag)
Open the first available pseudo terminal.
PseudoTerminal(const PseudoTerminal &)=delete
llvm::Error OpenSecondary(int oflag)
Open the secondary for the current primary pseudo terminal.
void ClosePrimaryFileDescriptor()
Close the primary file descriptor if it is valid.
int m_primary_fd
The file descriptor for the primary.
llvm::Expected< lldb::pid_t > Fork()
Fork a child process that uses pseudo terminals for its stdio.
void Reset()
Close both file descriptors and clear cached state.
@ invalid_fd
Invalid file descriptor value.
int GetPrimaryFileDescriptor() const
The primary file descriptor accessor.
int ReleasePrimaryFileDescriptor()
Release the primary file descriptor.
std::string GetSecondaryName() const
Get the name of the secondary pseudo terminal.
void CloseSecondaryFileDescriptor()
Close the secondary file descriptor if it is valid.
int GetSecondaryFileDescriptor() const
The secondary file descriptor accessor.
~PseudoTerminal()
The destructor will close the primary and secondary file descriptor/HANDLEs if they are valid and own...
A class that represents a running process on the host machine.