KDESu 5.99.0
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
KDESu::PtyProcess

Synchronous communication with tty programs. More...

#include <KDESu/PtyProcess>

Public Types

enum  checkPidStatus { Error = -1 , NotExited = -2 , Killed = -3 }
 Error return values for checkPidExited() More...
 

Public Member Functions

int enableLocalEcho (bool enable=true)
 Enables/disables local echo on the pseudo tty. More...
 
int exec (const QByteArray &command, const QList< QByteArray > &args)
 Forks off and execute a command. More...
 
int fd () const
 Returns the filedescriptor of the process. More...
 
int pid () const
 Returns the pid of the process. More...
 
QByteArray readAll (bool block=true)
 Read all available output from the program's standard out. More...
 
QByteArray readLine (bool block=true)
 Reads a line from the program's standard out. More...
 
void setEnvironment (const QList< QByteArray > &env)
 Set additinal environment variables. More...
 
void setErase (bool erase)
 Overwrites the password as soon as it is used. More...
 
void setExitString (const QByteArray &exit)
 Sets the exit string. More...
 
void setTerminal (bool terminal)
 Enables/disables terminal output. More...
 
void unreadLine (const QByteArray &line, bool addNewline=true)
 Puts back a line of input. More...
 
int waitForChild ()
 Waits for the child to exit. More...
 
int waitSlave ()
 Waits until the pty has cleared the ECHO flag. More...
 
int WaitSlave ()
 
void writeLine (const QByteArray &line, bool addNewline=true)
 Writes a line of text to the program's standard in. More...
 

Static Public Member Functions

static bool checkPid (pid_t pid)
 Basic check for the existence of pid. More...
 
static int checkPidExited (pid_t pid)
 Check process exit status for process pid. More...
 
static int waitMS (int fd, int ms)
 Wait ms milliseconds (ie. More...
 

Protected Member Functions

 PtyProcess (PtyProcessPrivate &dd)
 
QList< QByteArray > environment () const
 
virtual void virtual_hook (int id, void *data)
 Standard hack to add virtual methods in a BC way. More...
 

Protected Attributes

QByteArray m_command
 Unused. More...
 
bool m_erase
 
QByteArray m_exitString
 String to scan for in output that indicates child has exited. More...
 
int m_pid
 PID of child process. More...
 
bool m_terminal
 Indicates running in a terminal, causes additional newlines to be printed after output. More...
 

Detailed Description

Synchronous communication with tty programs.

PtyProcess provides synchronous communication with tty based programs. The communications channel used is a pseudo tty (as opposed to a pipe) This means that programs which require a terminal will work.

Member Enumeration Documentation

◆ checkPidStatus

Error return values for checkPidExited()

Enumerator
Error 

No child.

NotExited 

Child hasn't exited.

Killed 

Child terminated by signal.

Member Function Documentation

◆ checkPid()

static bool KDESu::PtyProcess::checkPid ( pid_t  pid)
static

Basic check for the existence of pid.

Returns true iff pid is an extant process, (one you could kill - see man kill(2) for signal 0).

◆ checkPidExited()

static int KDESu::PtyProcess::checkPidExited ( pid_t  pid)
static

Check process exit status for process pid.

If child pid has exited, return its exit status, (which may be zero). On error (no child, no exit), return -1. If child has not exited, return -2.

◆ enableLocalEcho()

int KDESu::PtyProcess::enableLocalEcho ( bool  enable = true)

Enables/disables local echo on the pseudo tty.

◆ exec()

int KDESu::PtyProcess::exec ( const QByteArray &  command,
const QList< QByteArray > &  args 
)

Forks off and execute a command.

The command's standard in and output are connected to the pseudo tty. They are accessible with readLine and writeLine.

Parameters
commandThe command to execute.
argsThe arguments to the command.
Returns
0 on success, -1 on error. errno might give more information then.

◆ fd()

int KDESu::PtyProcess::fd ( ) const

Returns the filedescriptor of the process.

◆ pid()

int KDESu::PtyProcess::pid ( ) const

Returns the pid of the process.

◆ readAll()

QByteArray KDESu::PtyProcess::readAll ( bool  block = true)

Read all available output from the program's standard out.

Parameters
blockIf no output is in the buffer, should the function block (else it will return an empty QByteArray)?
Returns
The output.

◆ readLine()

QByteArray KDESu::PtyProcess::readLine ( bool  block = true)

Reads a line from the program's standard out.

Depending on the block parameter, this call blocks until something was read. Note that in some situations this function will return less than a full line of output, but never more. Newline characters are stripped.

Parameters
blockBlock until a full line is read?
Returns
The output string.

◆ setEnvironment()

void KDESu::PtyProcess::setEnvironment ( const QList< QByteArray > &  env)

Set additinal environment variables.

◆ setErase()

void KDESu::PtyProcess::setErase ( bool  erase)

Overwrites the password as soon as it is used.

Relevant only to some subclasses.

◆ setExitString()

void KDESu::PtyProcess::setExitString ( const QByteArray &  exit)

Sets the exit string.

If a line of program output matches this, waitForChild() will terminate the program and return.

◆ setTerminal()

void KDESu::PtyProcess::setTerminal ( bool  terminal)

Enables/disables terminal output.

Relevant only to some subclasses.

◆ unreadLine()

void KDESu::PtyProcess::unreadLine ( const QByteArray &  line,
bool  addNewline = true 
)

Puts back a line of input.

Parameters
lineThe line to put back.
addNewlineAdds a '
' to the line.

◆ virtual_hook()

virtual void KDESu::PtyProcess::virtual_hook ( int  id,
void *  data 
)
protectedvirtual

Standard hack to add virtual methods in a BC way.

Unused.

Reimplemented in KDESu::SshProcess, KDESu::StubProcess, and KDESu::SuProcess.

◆ waitForChild()

int KDESu::PtyProcess::waitForChild ( )

Waits for the child to exit.

See also setExitString.

◆ waitMS()

static int KDESu::PtyProcess::waitMS ( int  fd,
int  ms 
)
static

Wait ms milliseconds (ie.

1/10th of a second is 100ms), using fd as a filedescriptor to wait on. Returns select(2)'s result, which is -1 on error, 0 on timeout, or positive if there is data on one of the selected fd's.

ms must be in the range 0..999 (i.e. the maximum wait duration is 999ms, almost one second).

◆ waitSlave()

int KDESu::PtyProcess::waitSlave ( )

Waits until the pty has cleared the ECHO flag.

This is useful when programs write a password prompt before they disable ECHO. Disabling it might flush any input that was written.

◆ WaitSlave()

int KDESu::PtyProcess::WaitSlave ( )
inline
Deprecated:
since 5.0, use waitSlave()

◆ writeLine()

void KDESu::PtyProcess::writeLine ( const QByteArray &  line,
bool  addNewline = true 
)

Writes a line of text to the program's standard in.

Parameters
lineThe text to write.
addNewlineAdds a '
' to the line.

Member Data Documentation

◆ m_command

QByteArray KDESu::PtyProcess::m_command
protected

Unused.

◆ m_erase

bool KDESu::PtyProcess::m_erase
protected
See also
setErase()

◆ m_exitString

QByteArray KDESu::PtyProcess::m_exitString
protected

String to scan for in output that indicates child has exited.

◆ m_pid

int KDESu::PtyProcess::m_pid
protected

PID of child process.

◆ m_terminal

bool KDESu::PtyProcess::m_terminal
protected

Indicates running in a terminal, causes additional newlines to be printed after output.

Set to false in constructor.

See also
setTerminal()