-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathpty.cc
370 lines (301 loc) · 8.59 KB
/
pty.cc
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/**
* pty.js
* Copyright (c) 2013-2015, Christopher Jeffrey, Peter Sunde (MIT License)
*
* pty.cc:
* This file is responsible for starting processes
* with pseudo-terminal file descriptors.
*/
#include "nan.h"
#include <string.h>
#include <stdlib.h>
#include <winpty.h>
#include <Shlwapi.h> // PathCombine
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
using namespace v8;
using namespace std;
using namespace node;
/**
* Misc
*/
extern "C" void init(Handle<Object>);
#define WINPTY_DBG_VARIABLE TEXT("WINPTYDBG")
#define MAX_ENV 65536
/**
* winpty
*/
static std::vector<winpty_t *> ptyHandles;
static volatile LONG ptyCounter;
struct winpty_s {
winpty_s();
HANDLE controlPipe;
HANDLE dataPipe;
};
winpty_s::winpty_s() :
controlPipe(nullptr),
dataPipe(nullptr)
{
}
/**
* Helpers
*/
const wchar_t* to_wstring(const String::Utf8Value& str)
{
const char *bytes = *str;
unsigned int sizeOfStr = MultiByteToWideChar(CP_ACP, 0, bytes, -1, NULL, 0);
wchar_t *output = new wchar_t[sizeOfStr];
MultiByteToWideChar(CP_ACP, 0, bytes, -1, output, sizeOfStr);
return output;
}
static winpty_t *get_pipe_handle(int handle) {
for(size_t i = 0; i < ptyHandles.size(); ++i) {
winpty_t *ptyHandle = ptyHandles[i];
int current = (int)ptyHandle->controlPipe;
if(current == handle) {
return ptyHandle;
}
}
return nullptr;
}
static bool remove_pipe_handle(int handle) {
for(size_t i = 0; i < ptyHandles.size(); ++i) {
winpty_t *ptyHandle = ptyHandles[i];
if((int)ptyHandle->controlPipe == handle) {
delete ptyHandle;
ptyHandle = nullptr;
return true;
}
}
return false;
}
static bool file_exists(std::wstring filename) {
DWORD attr = ::GetFileAttributesW(filename.c_str());
if(attr == INVALID_FILE_ATTRIBUTES || (attr & FILE_ATTRIBUTE_DIRECTORY)) {
return false;
}
return true;
}
// cmd.exe -> C:\Windows\system32\cmd.exe
static std::wstring get_shell_path(std::wstring filename) {
std::wstring shellpath;
if(file_exists(filename)) {
return shellpath;
}
wchar_t buffer_[MAX_ENV];
int read = ::GetEnvironmentVariableW(L"Path", buffer_, MAX_ENV);
if(!read) {
return shellpath;
}
std::wstring delimiter = L";";
size_t pos = 0;
vector<wstring> paths;
std::wstring buffer(buffer_);
while ((pos = buffer.find(delimiter)) != std::wstring::npos) {
paths.push_back(buffer.substr(0, pos));
buffer.erase(0, pos + delimiter.length());
}
const wchar_t *filename_ = filename.c_str();
for (int i = 0; i < paths.size(); ++i) {
wstring path = paths[i];
wchar_t searchPath[MAX_PATH];
::PathCombineW(searchPath, const_cast<wchar_t*>(path.c_str()), filename_);
if(searchPath == NULL) {
continue;
}
if(file_exists(searchPath)) {
shellpath = searchPath;
break;
}
}
return shellpath;
}
/*
* PtyOpen
* pty.open(dataPipe, cols, rows)
*
* If you need to debug winpty-agent.exe do the following:
* ======================================================
*
* 1) Install python 2.7
* 2) Install win32pipe
x86) http://sourceforge.net/projects/pywin32/files/pywin32/Build%20218/pywin32-218.win32-py2.7.exe/download
x64) http://sourceforge.net/projects/pywin32/files/pywin32/Build%20218/pywin32-218.win-amd64-py2.7.exe/download
* 3) Start deps/winpty/misc/DebugServer.py (Before you start node)
*
* Then you'll see output from winpty-agent.exe.
*
* Important part:
* ===============
* CreateProcess: success 8896 0 (Windows error code)
*
* Create test.js:
* ===============
*
* var pty = require('./');
*
* var term = pty.fork('cmd.exe', [], {
* name: 'Windows Shell',
* cols: 80,
* rows: 30,
* cwd: process.env.HOME,
* env: process.env,
* debug: true
* });
*
* term.on('data', function(data) {
* console.log(data);
* });
*
*/
static NAN_METHOD(PtyOpen) {
Nan::HandleScope scope;
if (info.Length() != 4
|| !info[0]->IsString() // dataPipe
|| !info[1]->IsNumber() // cols
|| !info[2]->IsNumber() // rows
|| !info[3]->IsBoolean()) // debug
{
return Nan::ThrowError("Usage: pty.open(dataPipe, cols, rows, debug)");
}
std::wstring pipeName = to_wstring(String::Utf8Value(info[0]->ToString()));
int cols = info[1]->Int32Value();
int rows = info[2]->Int32Value();
bool debug = info[3]->ToBoolean()->IsTrue();
// Enable/disable debugging
SetEnvironmentVariable(WINPTY_DBG_VARIABLE, debug ? "1" : NULL); // NULL = deletes variable
// Open a new pty session.
winpty_t *pc = winpty_open_use_own_datapipe(pipeName.c_str(), cols, rows);
// Error occured during startup of agent process.
assert(pc != nullptr);
// Save pty struct fpr later use.
ptyHandles.insert(ptyHandles.end(), pc);
// Pty object values.
Local<Object> marshal = Nan::New<Object>();
marshal->Set(Nan::New<String>("pid").ToLocalChecked(), Nan::New<Number>((int)pc->controlPipe));
marshal->Set(Nan::New<String>("pty").ToLocalChecked(), Nan::New<Number>(InterlockedIncrement(&ptyCounter)));
marshal->Set(Nan::New<String>("fd").ToLocalChecked(), Nan::New<Number>(-1));
return info.GetReturnValue().Set(marshal);
}
/*
* PtyStartProcess
* pty.startProcess(pid, file, env, cwd);
*/
static NAN_METHOD(PtyStartProcess) {
Nan::HandleScope scope;
if (info.Length() != 5
|| !info[0]->IsNumber() // pid
|| !info[1]->IsString() // file
|| !info[2]->IsString() // cmdline
|| !info[3]->IsArray() // env
|| !info[4]->IsString()) // cwd
{
return Nan::ThrowError(
"Usage: pty.startProcess(pid, file, cmdline, env, cwd)");
}
std::stringstream why;
// Get winpty_t by control pipe handle
int pid = info[0]->Int32Value();
winpty_t *pc = get_pipe_handle(pid);
assert(pc != nullptr);
const wchar_t *filename = to_wstring(String::Utf8Value(info[1]->ToString()));
const wchar_t *cmdline = to_wstring(String::Utf8Value(info[2]->ToString()));
const wchar_t *cwd = to_wstring(String::Utf8Value(info[4]->ToString()));
// create environment block
wchar_t *env = NULL;
const Handle<Array> envValues = Handle<Array>::Cast(info[3]);
if(!envValues.IsEmpty()) {
std::wstringstream envBlock;
for(uint32_t i = 0; i < envValues->Length(); i++) {
std::wstring envValue(to_wstring(String::Utf8Value(envValues->Get(i)->ToString())));
envBlock << envValue << L' ';
}
std::wstring output = envBlock.str();
size_t count = output.size();
env = new wchar_t[count + 2];
wcsncpy(env, output.c_str(), count);
wcscat(env, L"\0");
}
// use environment 'Path' variable to determine location of
// the relative path that we have recieved (e.g cmd.exe)
std::wstring shellpath;
if(::PathIsRelativeW(filename)) {
shellpath = get_shell_path(filename);
} else {
shellpath = filename;
}
std::string shellpath_(shellpath.begin(), shellpath.end());
if(shellpath.empty() || !file_exists(shellpath)) {
goto invalid_filename;
}
goto open;
open:
int result = winpty_start_process(pc, shellpath.c_str(), cmdline, cwd, env);
if(result != 0) {
why << "Unable to start terminal process. Win32 error code: " << result;
Nan::ThrowError(why.str().c_str());
}
goto cleanup;
invalid_filename:
why << "File not found: " << shellpath_;
Nan::ThrowError(why.str().c_str());
goto cleanup;
cleanup:
delete filename;
delete cmdline;
delete cwd;
delete env;
return info.GetReturnValue().SetUndefined();
}
/*
* PtyResize
* pty.resize(pid, cols, rows);
*/
static NAN_METHOD(PtyResize) {
Nan::HandleScope scope;
if (info.Length() != 3
|| !info[0]->IsNumber() // pid
|| !info[1]->IsNumber() // cols
|| !info[2]->IsNumber()) // rows
{
return Nan::ThrowError("Usage: pty.resize(pid, cols, rows)");
}
int handle = info[0]->Int32Value();
int cols = info[1]->Int32Value();
int rows = info[2]->Int32Value();
winpty_t *pc = get_pipe_handle(handle);
assert(pc != nullptr);
assert(0 == winpty_set_size(pc, cols, rows));
return info.GetReturnValue().SetUndefined();
}
/*
* PtyKill
* pty.kill(pid);
*/
static NAN_METHOD(PtyKill) {
Nan::HandleScope scope;
if (info.Length() != 1
|| !info[0]->IsNumber()) // pid
{
return Nan::ThrowError("Usage: pty.kill(pid)");
}
int handle = info[0]->Int32Value();
winpty_t *pc = get_pipe_handle(handle);
assert(pc != nullptr);
winpty_exit(pc);
assert(true == remove_pipe_handle(handle));
return info.GetReturnValue().SetUndefined();
}
/**
* Init
*/
extern "C" void init(Handle<Object> target) {
Nan::HandleScope scope;
Nan::SetMethod(target, "open", PtyOpen);
Nan::SetMethod(target, "startProcess", PtyStartProcess);
Nan::SetMethod(target, "resize", PtyResize);
Nan::SetMethod(target, "kill", PtyKill);
};
NODE_MODULE(pty, init);