1 //===-- PseudoTerminal.h ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLDB_HOST_PSEUDOTERMINAL_H
11 #define LLDB_HOST_PSEUDOTERMINAL_H
16 #include "lldb/lldb-defines.h"
18 namespace lldb_private {
20 //----------------------------------------------------------------------
21 /// @class PseudoTerminal PseudoTerminal.h "lldb/Host/PseudoTerminal.h"
22 /// @brief A pseudo terminal helper class.
24 /// The pseudo terminal class abstracts the use of pseudo terminals on
26 //----------------------------------------------------------------------
27 class PseudoTerminal {
30 invalid_fd = -1 ///< Invalid file descriptor value
33 //------------------------------------------------------------------
34 /// Default constructor
36 /// Constructs this object with invalid master and slave file
38 //------------------------------------------------------------------
41 //------------------------------------------------------------------
44 /// The destructor will close the master and slave file descriptors
45 /// if they are valid and ownership has not been released using
47 /// @li PseudoTerminal::ReleaseMasterFileDescriptor()
48 /// @li PseudoTerminal::ReleaseSaveFileDescriptor()
49 //------------------------------------------------------------------
52 //------------------------------------------------------------------
53 /// Close the master file descriptor if it is valid.
54 //------------------------------------------------------------------
55 void CloseMasterFileDescriptor();
57 //------------------------------------------------------------------
58 /// Close the slave file descriptor if it is valid.
59 //------------------------------------------------------------------
60 void CloseSlaveFileDescriptor();
62 //------------------------------------------------------------------
63 /// Fork a child process that uses pseudo terminals for its stdio.
65 /// In the parent process, a call to this function results in a pid
66 /// being returned. If the pid is valid, the master file descriptor
67 /// can be used for read/write access to stdio of the child process.
69 /// In the child process the stdin/stdout/stderr will already be
70 /// routed to the slave pseudo terminal and the master file
71 /// descriptor will be closed as it is no longer needed by the child
74 /// This class will close the file descriptors for the master/slave
75 /// when the destructor is called. The file handles can be released
77 /// @li PseudoTerminal::ReleaseMasterFileDescriptor()
78 /// @li PseudoTerminal::ReleaseSaveFileDescriptor()
81 /// An pointer to an error that can describe any errors that
82 /// occur. This can be NULL if no error status is desired.
85 /// @li \b Parent process: a child process ID that is greater
86 /// than zero, or -1 if the fork fails.
87 /// @li \b Child process: zero.
88 //------------------------------------------------------------------
89 lldb::pid_t Fork(char *error_str, size_t error_len);
91 //------------------------------------------------------------------
92 /// The master file descriptor accessor.
94 /// This object retains ownership of the master file descriptor when
95 /// this accessor is used. Users can call the member function
96 /// PseudoTerminal::ReleaseMasterFileDescriptor() if this
97 /// object should release ownership of the slave file descriptor.
100 /// The master file descriptor, or PseudoTerminal::invalid_fd
101 /// if the master file descriptor is not currently valid.
103 /// @see PseudoTerminal::ReleaseMasterFileDescriptor()
104 //------------------------------------------------------------------
105 int GetMasterFileDescriptor() const;
107 //------------------------------------------------------------------
108 /// The slave file descriptor accessor.
110 /// This object retains ownership of the slave file descriptor when
111 /// this accessor is used. Users can call the member function
112 /// PseudoTerminal::ReleaseSlaveFileDescriptor() if this
113 /// object should release ownership of the slave file descriptor.
116 /// The slave file descriptor, or PseudoTerminal::invalid_fd
117 /// if the slave file descriptor is not currently valid.
119 /// @see PseudoTerminal::ReleaseSlaveFileDescriptor()
120 //------------------------------------------------------------------
121 int GetSlaveFileDescriptor() const;
123 //------------------------------------------------------------------
124 /// Get the name of the slave pseudo terminal.
126 /// A master pseudo terminal should already be valid prior to
127 /// calling this function.
129 /// @param[out] error
130 /// An pointer to an error that can describe any errors that
131 /// occur. This can be NULL if no error status is desired.
134 /// The name of the slave pseudo terminal as a NULL terminated
135 /// C. This string that comes from static memory, so a copy of
136 /// the string should be made as subsequent calls can change
137 /// this value. NULL is returned if this object doesn't have
138 /// a valid master pseudo terminal opened or if the call to
139 /// \c ptsname() fails.
141 /// @see PseudoTerminal::OpenFirstAvailableMaster()
142 //------------------------------------------------------------------
143 const char *GetSlaveName(char *error_str, size_t error_len) const;
145 //------------------------------------------------------------------
146 /// Open the first available pseudo terminal.
148 /// Opens the first available pseudo terminal with \a oflag as the
149 /// permissions. The opened master file descriptor is stored in this
150 /// object and can be accessed by calling the
151 /// PseudoTerminal::GetMasterFileDescriptor() accessor. Clients
152 /// can call the PseudoTerminal::ReleaseMasterFileDescriptor()
153 /// accessor function if they wish to use the master file descriptor
154 /// beyond the lifespan of this object.
156 /// If this object still has a valid master file descriptor when its
157 /// destructor is called, it will close it.
160 /// Flags to use when calling \c posix_openpt(\a oflag).
161 /// A value of "O_RDWR|O_NOCTTY" is suggested.
163 /// @param[out] error
164 /// An pointer to an error that can describe any errors that
165 /// occur. This can be NULL if no error status is desired.
168 /// @li \b true when the master files descriptor is
169 /// successfully opened.
170 /// @li \b false if anything goes wrong.
172 /// @see PseudoTerminal::GetMasterFileDescriptor()
173 /// @see PseudoTerminal::ReleaseMasterFileDescriptor()
174 //------------------------------------------------------------------
175 bool OpenFirstAvailableMaster(int oflag, char *error_str, size_t error_len);
177 //------------------------------------------------------------------
178 /// Open the slave for the current master pseudo terminal.
180 /// A master pseudo terminal should already be valid prior to
181 /// calling this function. The opened slave file descriptor is
182 /// stored in this object and can be accessed by calling the
183 /// PseudoTerminal::GetSlaveFileDescriptor() accessor. Clients
184 /// can call the PseudoTerminal::ReleaseSlaveFileDescriptor()
185 /// accessor function if they wish to use the slave file descriptor
186 /// beyond the lifespan of this object.
188 /// If this object still has a valid slave file descriptor when its
189 /// destructor is called, it will close it.
192 /// Flags to use when calling \c open(\a oflag).
194 /// @param[out] error
195 /// An pointer to an error that can describe any errors that
196 /// occur. This can be NULL if no error status is desired.
199 /// @li \b true when the master files descriptor is
200 /// successfully opened.
201 /// @li \b false if anything goes wrong.
203 /// @see PseudoTerminal::OpenFirstAvailableMaster()
204 /// @see PseudoTerminal::GetSlaveFileDescriptor()
205 /// @see PseudoTerminal::ReleaseSlaveFileDescriptor()
206 //------------------------------------------------------------------
207 bool OpenSlave(int oflag, char *error_str, size_t error_len);
209 //------------------------------------------------------------------
210 /// Release the master file descriptor.
212 /// Releases ownership of the master pseudo terminal file descriptor
213 /// without closing it. The destructor for this class will close the
214 /// master file descriptor if the ownership isn't released using this
215 /// call and the master file descriptor has been opened.
218 /// The master file descriptor, or PseudoTerminal::invalid_fd
219 /// if the mast file descriptor is not currently valid.
220 //------------------------------------------------------------------
221 int ReleaseMasterFileDescriptor();
223 //------------------------------------------------------------------
224 /// Release the slave file descriptor.
226 /// Release ownership of the slave pseudo terminal file descriptor
227 /// without closing it. The destructor for this class will close the
228 /// slave file descriptor if the ownership isn't released using this
229 /// call and the slave file descriptor has been opened.
232 /// The slave file descriptor, or PseudoTerminal::invalid_fd
233 /// if the slave file descriptor is not currently valid.
234 //------------------------------------------------------------------
235 int ReleaseSlaveFileDescriptor();
238 //------------------------------------------------------------------
240 //------------------------------------------------------------------
241 int m_master_fd; ///< The file descriptor for the master.
242 int m_slave_fd; ///< The file descriptor for the slave.
245 DISALLOW_COPY_AND_ASSIGN(PseudoTerminal);
248 } // namespace lldb_private
250 #endif // #ifndef liblldb_PseudoTerminal_h_