Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:49

0001 //===-- PseudoTerminal.h ----------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLDB_HOST_PSEUDOTERMINAL_H
0010 #define LLDB_HOST_PSEUDOTERMINAL_H
0011 
0012 #include "lldb/lldb-defines.h"
0013 #include "llvm/Support/Error.h"
0014 #include <fcntl.h>
0015 #include <string>
0016 
0017 namespace lldb_private {
0018 
0019 /// \class PseudoTerminal PseudoTerminal.h "lldb/Host/PseudoTerminal.h"
0020 /// A pseudo terminal helper class.
0021 ///
0022 /// The pseudo terminal class abstracts the use of pseudo terminals on the
0023 /// host system.
0024 class PseudoTerminal {
0025 public:
0026   enum {
0027     invalid_fd = -1 ///< Invalid file descriptor value
0028   };
0029 
0030   /// Default constructor
0031   ///
0032   /// Constructs this object with invalid primary and secondary file
0033   /// descriptors.
0034   PseudoTerminal();
0035 
0036   /// Destructor
0037   ///
0038   /// The destructor will close the primary and secondary file descriptors if
0039   /// they are valid and ownership has not been released using one of: @li
0040   /// PseudoTerminal::ReleasePrimaryFileDescriptor() @li
0041   /// PseudoTerminal::ReleaseSaveFileDescriptor()
0042   ~PseudoTerminal();
0043 
0044   /// Close the primary file descriptor if it is valid.
0045   void ClosePrimaryFileDescriptor();
0046 
0047   /// Close the secondary file descriptor if it is valid.
0048   void CloseSecondaryFileDescriptor();
0049 
0050   /// Fork a child process that uses pseudo terminals for its stdio.
0051   ///
0052   /// In the parent process, a call to this function results in a pid being
0053   /// returned. If the pid is valid, the primary file descriptor can be used
0054   /// for read/write access to stdio of the child process.
0055   ///
0056   /// In the child process the stdin/stdout/stderr will already be routed to
0057   /// the secondary pseudo terminal and the primary file descriptor will be
0058   /// closed as it is no longer needed by the child process.
0059   ///
0060   /// This class will close the file descriptors for the primary/secondary when
0061   /// the destructor is called. The file handles can be released using either:
0062   /// @li PseudoTerminal::ReleasePrimaryFileDescriptor() @li
0063   /// PseudoTerminal::ReleaseSaveFileDescriptor()
0064   ///
0065   /// \return
0066   ///     \b Parent process: a child process ID that is greater
0067   ///         than zero, or an error if the fork fails.
0068   ///     \b Child process: zero.
0069   llvm::Expected<lldb::pid_t> Fork();
0070 
0071   /// The primary file descriptor accessor.
0072   ///
0073   /// This object retains ownership of the primary file descriptor when this
0074   /// accessor is used. Users can call the member function
0075   /// PseudoTerminal::ReleasePrimaryFileDescriptor() if this object should
0076   /// release ownership of the secondary file descriptor.
0077   ///
0078   /// \return
0079   ///     The primary file descriptor, or PseudoTerminal::invalid_fd
0080   ///     if the primary file  descriptor is not currently valid.
0081   ///
0082   /// \see PseudoTerminal::ReleasePrimaryFileDescriptor()
0083   int GetPrimaryFileDescriptor() const;
0084 
0085   /// The secondary file descriptor accessor.
0086   ///
0087   /// This object retains ownership of the secondary file descriptor when this
0088   /// accessor is used. Users can call the member function
0089   /// PseudoTerminal::ReleaseSecondaryFileDescriptor() if this object should
0090   /// release ownership of the secondary file descriptor.
0091   ///
0092   /// \return
0093   ///     The secondary file descriptor, or PseudoTerminal::invalid_fd
0094   ///     if the secondary file descriptor is not currently valid.
0095   ///
0096   /// \see PseudoTerminal::ReleaseSecondaryFileDescriptor()
0097   int GetSecondaryFileDescriptor() const;
0098 
0099   /// Get the name of the secondary pseudo terminal.
0100   ///
0101   /// A primary pseudo terminal should already be valid prior to
0102   /// calling this function.
0103   ///
0104   /// \return
0105   ///     The name of the secondary pseudo terminal.
0106   ///
0107   /// \see PseudoTerminal::OpenFirstAvailablePrimary()
0108   std::string GetSecondaryName() const;
0109 
0110   /// Open the first available pseudo terminal.
0111   ///
0112   /// Opens the first available pseudo terminal with \a oflag as the
0113   /// permissions. The opened primary file descriptor is stored in this object
0114   /// and can be accessed by calling the
0115   /// PseudoTerminal::GetPrimaryFileDescriptor() accessor. Clients can call the
0116   /// PseudoTerminal::ReleasePrimaryFileDescriptor() accessor function if they
0117   /// wish to use the primary file descriptor beyond the lifespan of this
0118   /// object.
0119   ///
0120   /// If this object still has a valid primary file descriptor when its
0121   /// destructor is called, it will close it.
0122   ///
0123   /// \param[in] oflag
0124   ///     Flags to use when calling \c posix_openpt(\a oflag).
0125   ///     A value of "O_RDWR|O_NOCTTY" is suggested.
0126   ///
0127   /// \see PseudoTerminal::GetPrimaryFileDescriptor() @see
0128   /// PseudoTerminal::ReleasePrimaryFileDescriptor()
0129   llvm::Error OpenFirstAvailablePrimary(int oflag);
0130 
0131   /// Open the secondary for the current primary pseudo terminal.
0132   ///
0133   /// A primary pseudo terminal should already be valid prior to
0134   /// calling this function. The opened secondary file descriptor is stored in
0135   /// this object and can be accessed by calling the
0136   /// PseudoTerminal::GetSecondaryFileDescriptor() accessor. Clients can call
0137   /// the PseudoTerminal::ReleaseSecondaryFileDescriptor() accessor function if
0138   /// they wish to use the secondary file descriptor beyond the lifespan of this
0139   /// object.
0140   ///
0141   /// If this object still has a valid secondary file descriptor when its
0142   /// destructor is called, it will close it.
0143   ///
0144   /// \param[in] oflag
0145   ///     Flags to use when calling \c open(\a oflag).
0146   ///
0147   /// \see PseudoTerminal::OpenFirstAvailablePrimary() @see
0148   /// PseudoTerminal::GetSecondaryFileDescriptor() @see
0149   /// PseudoTerminal::ReleaseSecondaryFileDescriptor()
0150   llvm::Error OpenSecondary(int oflag);
0151 
0152   /// Release the primary file descriptor.
0153   ///
0154   /// Releases ownership of the primary pseudo terminal file descriptor without
0155   /// closing it. The destructor for this class will close the primary file
0156   /// descriptor if the ownership isn't released using this call and the
0157   /// primary file descriptor has been opened.
0158   ///
0159   /// \return
0160   ///     The primary file descriptor, or PseudoTerminal::invalid_fd
0161   ///     if the mast file descriptor is not currently valid.
0162   int ReleasePrimaryFileDescriptor();
0163 
0164   /// Release the secondary file descriptor.
0165   ///
0166   /// Release ownership of the secondary pseudo terminal file descriptor without
0167   /// closing it. The destructor for this class will close the secondary file
0168   /// descriptor if the ownership isn't released using this call and the
0169   /// secondary file descriptor has been opened.
0170   ///
0171   /// \return
0172   ///     The secondary file descriptor, or PseudoTerminal::invalid_fd
0173   ///     if the secondary file descriptor is not currently valid.
0174   int ReleaseSecondaryFileDescriptor();
0175 
0176 protected:
0177   // Member variables
0178   int m_primary_fd = invalid_fd;   ///< The file descriptor for the primary.
0179   int m_secondary_fd = invalid_fd; ///< The file descriptor for the secondary.
0180 
0181 private:
0182   PseudoTerminal(const PseudoTerminal &) = delete;
0183   const PseudoTerminal &operator=(const PseudoTerminal &) = delete;
0184 };
0185 
0186 } // namespace lldb_private
0187 
0188 #endif // LLDB_HOST_PSEUDOTERMINAL_H