KPty 5.109.0
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
KPty

Provides primitives for opening & closing a pseudo TTY pair, assigning the controlling TTY, utmp registration and setting various terminal attributes. More...

#include <kpty.h>

Public Member Functions

 KPty ()
 Constructor.
 
 KPty (const KPty &)=delete
 
 ~KPty ()
 Destructor:
 
void close ()
 Close the pty master/slave pair.
 
void closeSlave ()
 Close the pty slave descriptor.
 
void login (const char *user=nullptr, const char *remotehost=nullptr)
 Creates an utmp entry for the tty.
 
void logout ()
 Removes the utmp entry for this tty.
 
int masterFd () const
 
bool open ()
 Create a pty master/slave pair.
 
bool open (int fd)
 Open using an existing pty master.
 
bool openSlave ()
 Open the pty slave descriptor.
 
KPtyoperator= (const KPty &)=delete
 
void setCTty ()
 Creates a new session and process group and makes this pty the controlling tty.
 
void setCTtyEnabled (bool enable)
 Whether this will be a controlling terminal.
 
bool setEcho (bool echo)
 Set whether the pty should echo input.
 
bool setWinSize (int lines, int columns)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Change the logical (screen) size of the pty.
 
bool setWinSize (int lines, int columns, int height, int width)
 Change the logical (screen) size of the pty.
 
int slaveFd () const
 
bool tcGetAttr (struct ::termios *ttmode) const
 Wrapper around tcgetattr(3).
 
bool tcSetAttr (struct ::termios *ttmode)
 Wrapper around tcsetattr(3) with mode TCSANOW.
 
const char * ttyName () const
 

Protected Member Functions

 __attribute__ ((visibility("hidden"))) explicit KPty(KPtyPrivate *d)
 

Protected Attributes

std::unique_ptr< KPtyPrivate > const d_ptr
 

Detailed Description

Provides primitives for opening & closing a pseudo TTY pair, assigning the controlling TTY, utmp registration and setting various terminal attributes.

Constructor & Destructor Documentation

◆ KPty()

KPty::KPty ( )

Constructor.

◆ ~KPty()

KPty::~KPty ( )

Destructor:

If the pty is still open, it will be closed. Note, however, that an utmp registration is not undone.

Member Function Documentation

◆ close()

void KPty::close ( )

Close the pty master/slave pair.

◆ closeSlave()

void KPty::closeSlave ( )

Close the pty slave descriptor.

When creating the pty, KPty also opens the slave and keeps it open. Consequently the master will never receive an EOF notification. Usually this is the desired behavior, as a closed pty slave can be reopened any time - unlike a pipe or socket. However, in some cases pipe-alike behavior might be desired.

After this function was called, slaveFd() and setCTty() cannot be used.

◆ login()

void KPty::login ( const char *  user = nullptr,
const char *  remotehost = nullptr 
)

Creates an utmp entry for the tty.

This function must be called after calling setCTty and making this pty the stdin.

Parameters
userthe user to be logged on
remotehostthe host from which the login is coming. This is not the local host. For remote logins it should be the hostname of the client. For local logins from inside an X session it should be the name of the X display. Otherwise it should be empty.

◆ logout()

void KPty::logout ( )

Removes the utmp entry for this tty.

◆ masterFd()

int KPty::masterFd ( ) const
Returns
the file descriptor of the master pty

This function should be called only while the pty is open.

◆ open() [1/2]

bool KPty::open ( )

Create a pty master/slave pair.

Returns
true if a pty pair was successfully opened

◆ open() [2/2]

bool KPty::open ( int  fd)

Open using an existing pty master.

Parameters
fdan open pty master file descriptor. The ownership of the fd remains with the caller; it will not be automatically closed at any point.
Returns
true if a pty pair was successfully opened

◆ openSlave()

bool KPty::openSlave ( )

Open the pty slave descriptor.

This undoes the effect of closeSlave().

Returns
true if the pty slave was successfully opened

◆ setCTty()

void KPty::setCTty ( )

Creates a new session and process group and makes this pty the controlling tty.

◆ setCTtyEnabled()

void KPty::setCTtyEnabled ( bool  enable)

Whether this will be a controlling terminal.

This is on by default. Disabling the controllig aspect only makes sense if another process will take over control or there is nothing to control or for technical reasons control cannot be set (this notably is the case with flatpak-spawn when used inside a sandbox).

Parameters
enablewhether to enable ctty set up

◆ setEcho()

bool KPty::setEcho ( bool  echo)

Set whether the pty should echo input.

Echo is on by default. If the output of automatically fed (non-interactive) PTY clients needs to be parsed, disabling echo often makes it much simpler.

This function can be used only while the PTY is open.

Parameters
echotrue if input should be echoed.
Returns
true on success, false otherwise

◆ setWinSize() [1/2]

bool KPty::setWinSize ( int  lines,
int  columns 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Change the logical (screen) size of the pty.

The pixel size is set to zero.

◆ setWinSize() [2/2]

bool KPty::setWinSize ( int  lines,
int  columns,
int  height,
int  width 
)

Change the logical (screen) size of the pty.

The default is 24 lines by 80 columns in characters, and zero pixels.

This function can be used only while the PTY is open.

Parameters
linesthe number of character rows
columnsthe number of character columns
heightthe view height in pixels
widththe view width in pixels
Returns
true on success, false otherwise
Since
5.93

◆ slaveFd()

int KPty::slaveFd ( ) const
Returns
the file descriptor of the slave pty

This function should be called only while the pty slave is open.

◆ tcGetAttr()

bool KPty::tcGetAttr ( struct ::termios *  ttmode) const

Wrapper around tcgetattr(3).

This function can be used only while the PTY is open. You will need an #include <termios.h> to do anything useful with it.

Parameters
ttmodea pointer to a termios structure. Note: when declaring ttmode, struct ::termios must be used - without the '::' some version of HP-UX thinks, this declares the struct in your class, in your method.
Returns
true on success, false otherwise

◆ tcSetAttr()

bool KPty::tcSetAttr ( struct ::termios *  ttmode)

Wrapper around tcsetattr(3) with mode TCSANOW.

This function can be used only while the PTY is open.

Parameters
ttmodea pointer to a termios structure.
Returns
true on success, false otherwise. Note that success means that at least one attribute could be set.

◆ ttyName()

const char * KPty::ttyName ( ) const
Returns
the name of the slave pty device.

This function should be called only while the pty is open.