Repository navigation
Expand file tree
/
Copy pathptyprocess.cpp
More file actions
198 lines (177 loc) · 4.56 KB
/
Copy pathptyprocess.cpp
File metadata and controls
198 lines (177 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "ptyprocess.h"
#include <QSocketNotifier>
#include <QVector>
#ifdef Q_OS_UNIX
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <termios.h>
#include <unistd.h>
#if defined(Q_OS_LINUX) || defined(Q_OS_ANDROID)
#include <pty.h>
#elif defined(Q_OS_MACOS) || defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD) || \
defined(Q_OS_OPENBSD)
#include <util.h>
#else
#include <pty.h>
#endif
#else
#include <stdlib.h>
#endif
PtyProcess::PtyProcess(QObject *parent) : QObject(parent) {}
PtyProcess::~PtyProcess() {
terminate();
closeMasterFd();
}
bool PtyProcess::start(const QString &program, const QStringList &arguments,
int columns, int rows, const QString &termType,
const QString &workingDirectory) {
#ifdef Q_OS_UNIX
if (isRunning()) {
return false;
}
QByteArray programBytes = program.toLocal8Bit();
QByteArray termBytes = termType.toLocal8Bit();
QByteArray workingDirectoryBytes = workingDirectory.toLocal8Bit();
QVector<QByteArray> argumentBytes;
argumentBytes.append(programBytes);
for (const QString &argument : arguments) {
argumentBytes.append(argument.toLocal8Bit());
}
QVector<char *> argv;
for (QByteArray &argument : argumentBytes) {
argv.append(argument.data());
}
argv.append(nullptr);
struct winsize windowSize;
memset(&windowSize, 0, sizeof(windowSize));
windowSize.ws_col = static_cast<unsigned short>(qMax(1, columns));
windowSize.ws_row = static_cast<unsigned short>(qMax(1, rows));
int masterFd = -1;
const pid_t pid = ::forkpty(&masterFd, nullptr, nullptr, &windowSize);
if (pid < 0) {
return false;
}
if (pid == 0) {
// Child: only async-signal-safe work until execvp().
::setenv("TERM", termBytes.constData(), 1);
::setenv("COLORTERM", "truecolor", 1);
if (!workingDirectoryBytes.isEmpty() &&
::chdir(workingDirectoryBytes.constData()) != 0) {
// Keep the inherited working directory if the home path is unavailable.
}
::execvp(programBytes.constData(), argv.data());
::_exit(127);
}
masterFd_ = masterFd;
childPid_ = pid;
const int flags = ::fcntl(masterFd_, F_GETFL);
if (flags >= 0) {
::fcntl(masterFd_, F_SETFL, flags | O_NONBLOCK);
}
notifier_ = new QSocketNotifier(masterFd_, QSocketNotifier::Read, this);
connect(notifier_, &QSocketNotifier::activated, this, &PtyProcess::onReadable);
return true;
#else
Q_UNUSED(program);
Q_UNUSED(arguments);
Q_UNUSED(columns);
Q_UNUSED(rows);
Q_UNUSED(termType);
Q_UNUSED(workingDirectory);
return false;
#endif
}
void PtyProcess::write(const QByteArray &bytes) {
#ifdef Q_OS_UNIX
if (masterFd_ < 0 || bytes.isEmpty()) {
return;
}
qint64 offset = 0;
while (offset < bytes.size()) {
const ssize_t written =
::write(masterFd_, bytes.constData() + offset, size_t(bytes.size() - offset));
if (written > 0) {
offset += written;
continue;
}
if (written < 0 && (errno == EINTR)) {
continue;
}
break; // EAGAIN: the child is not reading; drop the rest for now
}
#else
Q_UNUSED(bytes);
#endif
}
void PtyProcess::resize(int columns, int rows) {
#ifdef Q_OS_UNIX
if (masterFd_ < 0) {
return;
}
struct winsize windowSize;
memset(&windowSize, 0, sizeof(windowSize));
windowSize.ws_col = static_cast<unsigned short>(qMax(1, columns));
windowSize.ws_row = static_cast<unsigned short>(qMax(1, rows));
::ioctl(masterFd_, TIOCSWINSZ, &windowSize);
#else
Q_UNUSED(columns);
Q_UNUSED(rows);
#endif
}
void PtyProcess::terminate() {
#ifdef Q_OS_UNIX
if (childPid_ > 0) {
::kill(pid_t(childPid_), SIGHUP);
int status = 0;
::waitpid(pid_t(childPid_), &status, WNOHANG);
childPid_ = 0;
}
#endif
}
void PtyProcess::onReadable() {
#ifdef Q_OS_UNIX
if (masterFd_ < 0) {
return;
}
char buffer[8192];
while (true) {
const ssize_t count = ::read(masterFd_, buffer, sizeof(buffer));
if (count > 0) {
emit readyRead(QByteArray(buffer, int(count)));
continue;
}
if (count == 0) {
closeMasterFd();
emit exited(0);
return;
}
if (errno == EINTR) {
continue;
}
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return;
}
closeMasterFd();
emit exited(-1);
return;
}
#endif
}
void PtyProcess::closeMasterFd() {
#ifdef Q_OS_UNIX
if (notifier_) {
notifier_->setEnabled(false);
notifier_->deleteLater();
notifier_ = nullptr;
}
if (masterFd_ >= 0) {
::close(masterFd_);
masterFd_ = -1;
}
#endif
}