KDESu 5.99.0
ptyprocess.h
1/*
2 This file is part of the KDE project, module kdesu.
3 SPDX-FileCopyrightText: 1999, 2000 Geert Jansen <jansen@kde.org>
4
5 SPDX-License-Identifier: GPL-2.0-only
6*/
7
8#ifndef KDESUPTYPROCESS_H
9#define KDESUPTYPROCESS_H
10
11#include <memory>
12#include <sys/types.h>
13
14#include <QByteArray>
15#include <QList>
16#include <QString>
17#include <QStringList>
18
19#include <kdesu/kdesu_export.h>
20
21#include <KPty>
22
23namespace KDESu
24{
25class PtyProcessPrivate;
26
35class KDESU_EXPORT PtyProcess
36{
37public:
40 Error = -1,
41 NotExited = -2,
42 Killed = -3,
43 };
44
45 PtyProcess();
46 virtual ~PtyProcess();
47
56 int exec(const QByteArray &command, const QList<QByteArray> &args);
57
66 QByteArray readLine(bool block = true);
67
74 QByteArray readAll(bool block = true);
75
81 void writeLine(const QByteArray &line, bool addNewline = true);
82
88 void unreadLine(const QByteArray &line, bool addNewline = true);
89
94 void setExitString(const QByteArray &exit);
95
100
107
108#if KDESU_ENABLE_DEPRECATED_SINCE(5, 0)
112 KDESU_DEPRECATED_VERSION(5, 0, "Use PtyProcess::waitSlave()")
113 int WaitSlave()
114 {
115 return waitSlave();
116 }
117#endif
118
122 int enableLocalEcho(bool enable = true);
123
127 void setTerminal(bool terminal);
128
133 void setErase(bool erase);
134
138 void setEnvironment(const QList<QByteArray> &env);
139
143 int fd() const;
144
148 int pid() const;
149
150 /*
151 ** This is a collection of static functions that can be
152 ** used for process control inside kdesu. I'd suggest
153 ** against using this publicly. There are probably
154 ** nicer Qt based ways to do what you want.
155 */
156
166 static int waitMS(int fd, int ms);
167
173 static bool checkPid(pid_t pid);
174
182 static int checkPidExited(pid_t pid);
183
184protected:
185 explicit PtyProcess(PtyProcessPrivate &dd);
186
188 virtual void virtual_hook(int id, void *data);
189 QList<QByteArray> environment() const;
190
191 // KF6 TODO: move to PtyProcessPrivate
192 bool m_erase;
196 int m_pid;
197 QByteArray m_command;
198 QByteArray m_exitString;
200private:
201 int init();
202 int setupTTY();
203
204private:
205 friend class StubProcess;
206 friend class SshProcess;
207 friend class SuProcess;
208 std::unique_ptr<PtyProcessPrivate> const d;
209 // KF6 TODO: change private d to protected d_ptr, use normal Q_DECLARE_PRIVATE, remove friend
210};
211
212}
213
214#endif // KDESUPTYPROCESS_H
Synchronous communication with tty programs.
Definition: ptyprocess.h:36
QByteArray m_command
Unused.
Definition: ptyprocess.h:197
static int waitMS(int fd, int ms)
Wait ms milliseconds (ie.
int waitSlave()
Waits until the pty has cleared the ECHO flag.
void setExitString(const QByteArray &exit)
Sets the exit string.
int m_pid
PID of child process.
Definition: ptyprocess.h:196
bool m_erase
Definition: ptyprocess.h:192
int pid() const
Returns the pid of the process.
QByteArray readLine(bool block=true)
Reads a line from the program's standard out.
checkPidStatus
Error return values for checkPidExited()
Definition: ptyprocess.h:39
static bool checkPid(pid_t pid)
Basic check for the existence of pid.
virtual void virtual_hook(int id, void *data)
Standard hack to add virtual methods in a BC way.
QByteArray m_exitString
String to scan for in output that indicates child has exited.
Definition: ptyprocess.h:198
QByteArray readAll(bool block=true)
Read all available output from the program's standard out.
int exec(const QByteArray &command, const QList< QByteArray > &args)
Forks off and execute a command.
static int checkPidExited(pid_t pid)
Check process exit status for process pid.
bool m_terminal
Indicates running in a terminal, causes additional newlines to be printed after output.
Definition: ptyprocess.h:193
void setEnvironment(const QList< QByteArray > &env)
Set additinal environment variables.
void setErase(bool erase)
Overwrites the password as soon as it is used.
int fd() const
Returns the filedescriptor of the process.
void writeLine(const QByteArray &line, bool addNewline=true)
Writes a line of text to the program's standard in.
int waitForChild()
Waits for the child to exit.
int enableLocalEcho(bool enable=true)
Enables/disables local echo on the pseudo tty.
void unreadLine(const QByteArray &line, bool addNewline=true)
Puts back a line of input.
void setTerminal(bool terminal)
Enables/disables terminal output.
Executes a remote command, using ssh.
Definition: sshprocess.h:24
Chat with kdesu_stub.
Definition: stubprocess.h:33
Executes a command under elevated privileges, using su.
Definition: suprocess.h:24