-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathconpty_common.cpp
303 lines (241 loc) · 8.75 KB
/
conpty_common.cpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "conpty_common.h"
#include <string>
/**
Native ConPTY calls.
See: https://docs.microsoft.com/en-us/windows/console/creating-a-pseudoconsole-session
**/
#if ENABLE_CONPTY
HRESULT SetUpPseudoConsole(HPCON* hPC, COORD size, HANDLE* inputReadSide, HANDLE* outputWriteSide,
HANDLE* outputReadSide, HANDLE* inputWriteSide) {
HRESULT hr = S_OK;
if (!CreatePipe(inputReadSide, inputWriteSide, NULL, 0)) {
return HRESULT_FROM_WIN32(GetLastError());
}
if (!CreatePipe(outputReadSide, outputWriteSide, NULL, 0)) {
return HRESULT_FROM_WIN32(GetLastError());
}
hr = CreatePseudoConsole(size, *inputReadSide, *outputWriteSide, 0, hPC);
return hr;
}
// Initializes the specified startup info struct with the required properties and
// updates its thread attribute list with the specified ConPTY handle
HRESULT PrepareStartupInformation(HPCON hpc, STARTUPINFOEX* psi)
{
// Prepare Startup Information structure
STARTUPINFOEX si;
ZeroMemory(&si, sizeof(si));
si.StartupInfo.cb = sizeof(STARTUPINFOEX);
// Discover the size required for the list
size_t bytesRequired;
InitializeProcThreadAttributeList(NULL, 1, 0, &bytesRequired);
// Allocate memory to represent the list
si.lpAttributeList = (PPROC_THREAD_ATTRIBUTE_LIST)HeapAlloc(GetProcessHeap(), 0, bytesRequired);
if (!si.lpAttributeList)
{
return E_OUTOFMEMORY;
}
// Initialize the list memory location
if (!InitializeProcThreadAttributeList(si.lpAttributeList, 1, 0, &bytesRequired))
{
HeapFree(GetProcessHeap(), 0, si.lpAttributeList);
return HRESULT_FROM_WIN32(GetLastError());
}
// Set the pseudoconsole information into the list
if (!UpdateProcThreadAttribute(si.lpAttributeList,
0,
PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE,
hpc,
sizeof(hpc),
NULL,
NULL))
{
HeapFree(GetProcessHeap(), 0, si.lpAttributeList);
return HRESULT_FROM_WIN32(GetLastError());
}
*psi = si;
return S_OK;
}
ConPTY::ConPTY(int cols, int rows) {
pty_started = false;
pty_created = false;
using_pipes = true;
pid = 0;
if (cols <= 0 || rows <= 0) {
std::string prefix = "PTY cols and rows must be positive and non-zero. Got: ";
std::string size = "(" + std::to_string(cols) + "," + std::to_string(rows) + ")";
std::string error = prefix + size;
throw std::runtime_error(error.c_str());
}
HRESULT hr{ E_UNEXPECTED };
// Create a console window in case ConPTY is running in a GUI application
AllocConsole();
ShowWindow(GetConsoleWindow(), SW_HIDE);
// Recreate the standard stream inputs in case the parent process
// has redirected them
HANDLE hConsole = CreateFile(
L"CONOUT$",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hConsole == INVALID_HANDLE_VALUE) {
hr = GetLastError();
throw_runtime_error(hr);
}
HANDLE hIn = CreateFile(
L"CONIN$",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if (hIn == INVALID_HANDLE_VALUE) {
hr = GetLastError();
throw_runtime_error(hr);
}
// Enable Console VT Processing
DWORD consoleMode{};
hr = GetConsoleMode(hConsole, &consoleMode)
? S_OK
: GetLastError();
if (hr != S_OK) {
throw_runtime_error(hr);
}
// Enable stream to accept VT100 input sequences
hr = SetConsoleMode(hConsole, consoleMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
? S_OK
: GetLastError();
if (hr != S_OK) {
throw_runtime_error(hr);
}
// Set new streams
SetStdHandle(STD_OUTPUT_HANDLE, hConsole);
SetStdHandle(STD_ERROR_HANDLE, hConsole);
SetStdHandle(STD_INPUT_HANDLE, hIn);
// Create communication channels
// - Close these after CreateProcess of child application with pseudoconsole object.
HANDLE inputReadSide{ INVALID_HANDLE_VALUE };
HANDLE outputWriteSide{ INVALID_HANDLE_VALUE };
// - Hold onto these and use them for communication with the child through the pseudoconsole.
HANDLE outputReadSide{ INVALID_HANDLE_VALUE };
HANDLE inputWriteSide{ INVALID_HANDLE_VALUE };
// Setup PTY size
COORD size = {};
size.X = cols;
size.Y = rows;
hr = SetUpPseudoConsole(&pty_handle, size, &inputReadSide, &outputWriteSide,
&outputReadSide, &inputWriteSide);
if (hr != S_OK) {
throw_runtime_error(hr);
}
this->inputReadSide = inputReadSide;
this->outputWriteSide = outputWriteSide;
this->outputReadSide = outputReadSide;
this->inputWriteSide = inputWriteSide;
pty_created = true;
}
ConPTY::~ConPTY() {
if (pty_started) {
// Close process
CloseHandle(process_info.hThread);
CloseHandle(process_info.hProcess);
// Cleanup attribute list
DeleteProcThreadAttributeList(startup_info.lpAttributeList);
}
if (pty_created) {
// Close ConPTY - this will terminate client process if running
ClosePseudoConsole(pty_handle);
FreeConsole();
// Clean-up the pipes
if (INVALID_HANDLE_VALUE != outputReadSide) CloseHandle(outputReadSide);
if (INVALID_HANDLE_VALUE != inputWriteSide) CloseHandle(inputWriteSide);
}
}
bool ConPTY::spawn(std::wstring appname, std::wstring cmdline, std::wstring cwd, std::wstring env) {
if (pty_started && is_alive()) {
throw std::runtime_error("A process was already spawned and is running currently");
}
HRESULT hr{ E_UNEXPECTED };
STARTUPINFOEX siEx;
hr = PrepareStartupInformation(pty_handle, &siEx);
if (hr != S_OK) {
throw_runtime_error(hr);
}
PCWSTR childApplication = L"";
if(cmdline.length() > 0) {
childApplication = cmdline.c_str();
}
LPVOID environment = NULL;
if (env.length() > 0) {
environment = (void*)env.c_str();
}
LPCWSTR working_dir = NULL;
if (cwd.length() > 0) {
working_dir = cwd.c_str();
}
// Create mutable text string for CreateProcessW command line string.
const size_t charsRequired = wcslen(childApplication) + 1; // +1 null terminator
PWSTR cmdLineMutable = (PWSTR)HeapAlloc(GetProcessHeap(), 0, sizeof(wchar_t) * charsRequired);
if (!cmdLineMutable) {
hr = E_OUTOFMEMORY;
}
if (hr != S_OK) {
throw_runtime_error(hr);
}
wcscpy_s(cmdLineMutable, charsRequired, childApplication);
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
// Call CreateProcess
hr = CreateProcessW(
appname.c_str(), // Application name
cmdLineMutable, // Command line arguments
NULL, // Process attributes (unused)
NULL, // Thread attributes (unused)
FALSE, // Inherit pipes (false)
EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT, // Process creation flags
environment, // Environment variables
working_dir, // Current working directory
&siEx.StartupInfo, // Startup info
&pi // Process information
) ? S_OK : GetLastError();
if (hr != S_OK) {
HeapFree(GetProcessHeap(), 0, cmdLineMutable);
throw_runtime_error(hr);
}
CloseHandle(inputReadSide);
CloseHandle(outputWriteSide);
conout = outputReadSide;
conin = inputWriteSide;
pid = pi.dwProcessId;
process = pi.hProcess;
pty_started = true;
process_info = pi;
return true;
}
void ConPTY::set_size(int cols, int rows) {
if (cols <= 0 || rows <= 0) {
std::string prefix = "PTY cols and rows must be positive and non-zero. Got: ";
std::string size = "(" + std::to_string(cols) + "," + std::to_string(rows) + ")";
std::string error = prefix + size;
throw std::runtime_error(error.c_str());
}
COORD consoleSize{};
consoleSize.X = cols;
consoleSize.Y = rows;
HRESULT hr = ResizePseudoConsole(pty_handle, consoleSize);
if (hr != S_OK) {
throw_runtime_error(hr);
}
}
#else
ConPTY::ConPTY(int cols, int rows) {
throw std::runtime_error("pywinpty was compiled without ConPTY support");
}
ConPTY::~ConPTY() {
}
bool ConPTY::spawn(std::wstring appname, std::wstring cmdline, std::wstring cwd, std::wstring env) {
throw std::runtime_error("pywinpty was compiled without ConPTY support");
}
void ConPTY::set_size(int cols, int rows) {
throw std::runtime_error("pywinpty was compiled without ConPTY support");
}
#endif // ENABLE_CONPTY
uint32_t ConPTY::read_stderr(char* buf, uint64_t length, bool blocking) {
throw std::runtime_error("ConPTY stderr reading is disabled");
}