forked from openssh/openssh-portable
-
Notifications
You must be signed in to change notification settings - Fork 325
/
termio.c
292 lines (261 loc) · 9.73 KB
/
termio.c
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
/*
* Author: Manoj Ampalam <[email protected]>
* read() and write() on tty using worker threads to handle
* synchronous Windows Console IO
*
* Author: Ray Hayes <[email protected]>
* TTY/PTY support added by capturing all terminal input events
*
* Author: Balu <[email protected]>
* Misc fixes and code cleanup
*
* Author: Manoj Ampalam <[email protected]>
* Extended support to other Windows IO that does not support
* overlapped IO. Ex. pipe handles returned by CreatePipe()
*
* Copyright (c) 2017 Microsoft Corp.
* All rights reserved
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <Windows.h>
#include <process.h>
#include "w32fd.h"
#include "tncon.h"
#include "inc\utf.h"
#include "debug.h"
#include "tnnet.h"
#include "misc_internal.h"
#define TERM_IO_BUF_SIZE 2048
extern int in_raw_mode;
BOOL isFirstTime = TRUE;
/* APC that gets queued on main thread when a sync Read completes on worker thread */
static VOID CALLBACK
ReadAPCProc(_In_ ULONG_PTR dwParam)
{
struct w32_io* pio = (struct w32_io*)dwParam;
debug5("TermRead CB - io:%p, bytes: %d, pending: %d, error: %d", pio, pio->read_details.completed,
pio->read_details.pending, pio->sync_read_status.error);
pio->read_details.error = pio->sync_read_status.error;
pio->read_details.remaining = pio->sync_read_status.transferred;
pio->read_details.completed = 0;
pio->read_details.pending = FALSE;
WaitForSingleObject(pio->read_overlapped.hEvent, INFINITE);
CloseHandle(pio->read_overlapped.hEvent);
pio->read_overlapped.hEvent = 0;
}
/* Read worker thread */
static unsigned __stdcall
ReadThread(_In_ LPVOID lpParameter)
{
int nBytesReturned = 0;
struct w32_io* pio = (struct w32_io*)lpParameter;
debug5("TermRead thread, io:%p", pio);
memset(&pio->sync_read_status, 0, sizeof(pio->sync_read_status));
if (FILETYPE(pio) == FILE_TYPE_CHAR) {
if (in_raw_mode) {
while (nBytesReturned == 0) {
nBytesReturned = ReadConsoleForTermEmul(WINHANDLE(pio),
pio->read_details.buf, pio->read_details.buf_size);
}
pio->sync_read_status.transferred = nBytesReturned;
} else {
if (isFirstTime) {
isFirstTime = false;
DWORD dwAttributes;
/* open(dev/null) is showing up as FILE_TYPE_CHAR but is not a valid console handle */
if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwAttributes)) {
dwAttributes |= (ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
if (!SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwAttributes))
debug2("SetConsoleMode on STD_INPUT_HANDLE failed with %d", GetLastError());
} else if (GetLastError() != ERROR_INVALID_HANDLE)
debug2("GetConsoleMode on STD_INPUT_HANDLE failed with %d", GetLastError());
}
if (!ReadFile(WINHANDLE(pio), pio->read_details.buf,
pio->read_details.buf_size, &(pio->sync_read_status.transferred), NULL)) {
debug4("ReadThread - ReadFile failed, error:%d, io:%p", GetLastError(), pio);
pio->sync_read_status.error = GetLastError();
goto done;
}
if (pio->sync_read_status.transferred) {
char *p = NULL;
if (p = strstr(pio->read_details.buf, "\r\n"))
*p++ = '\n';
else if (p = strstr(pio->read_details.buf, "\r"))
*p++ = '\n';
if (p) {
*p = '\0';
pio->sync_read_status.transferred = (DWORD)strlen(pio->read_details.buf);
}
}
}
} else {
if (!ReadFile(WINHANDLE(pio), pio->read_details.buf,
pio->read_details.buf_size, &(pio->sync_read_status.transferred), NULL)) {
debug4("ReadThread - ReadFile failed, error:%d, io:%p", GetLastError(), pio);
pio->sync_read_status.error = GetLastError();
goto done;
}
/* If there is no data to be read then set the error to ERROR_HANDLE_EOF */
if (!pio->sync_read_status.transferred) {
pio->sync_read_status.error = ERROR_HANDLE_EOF;
}
}
done:
if (0 == QueueUserAPC(ReadAPCProc, main_thread, (ULONG_PTR)pio)) {
pio->read_details.pending = FALSE;
pio->read_details.error = GetLastError();
debug_assert_internal();
}
return 0;
}
/* Initiates read on tty */
int
syncio_initiate_read(struct w32_io* pio)
{
HANDLE read_thread;
debug5("syncio_initiate_read io:%p", pio);
if (pio->read_details.buf_size == 0) {
pio->read_details.buf = malloc(TERM_IO_BUF_SIZE);
if (pio->read_details.buf == NULL) {
errno = ENOMEM;
return -1;
}
pio->read_details.buf_size = TERM_IO_BUF_SIZE;
}
read_thread = (HANDLE) _beginthreadex(NULL, 0, ReadThread, pio, 0, NULL);
if (read_thread == NULL) {
errno = errno_from_Win32LastError();
debug3("TermRead initiate - ERROR _beginthreadex %d, io:%p", GetLastError(), pio);
return -1;
}
pio->read_overlapped.hEvent = read_thread;
pio->read_details.pending = TRUE;
return 0;
}
/* APC that gets queued on main thread when a sync Write completes on worker thread */
static VOID CALLBACK
WriteAPCProc(_In_ ULONG_PTR dwParam)
{
struct w32_io* pio = (struct w32_io*)dwParam;
debug5("TermWrite CB - io:%p, bytes: %d, pending: %d, error: %d", pio, pio->write_details.completed,
pio->write_details.pending, pio->sync_write_status.error);
pio->write_details.error = pio->sync_write_status.error;
pio->write_details.remaining -= pio->sync_write_status.transferred;
/* TODO- assert that reamining is 0 by now */
pio->write_details.completed = 0;
pio->write_details.pending = FALSE;
WaitForSingleObject(pio->write_overlapped.hEvent, INFINITE);
CloseHandle(pio->write_overlapped.hEvent);
pio->write_overlapped.hEvent = 0;
}
/* Write worker thread */
static unsigned __stdcall
WriteThread(_In_ LPVOID lpParameter)
{
struct w32_io* pio = (struct w32_io*)lpParameter;
char *respbuf = NULL;
size_t resplen = 0;
debug5("WriteThread thread, io:%p", pio);
if (FILETYPE(pio) == FILE_TYPE_CHAR) {
pio->write_details.buf[pio->sync_write_status.to_transfer] = '\0';
if (0 == in_raw_mode) {
wchar_t* t = utf8_to_utf16(pio->write_details.buf);
WriteConsoleW(WINHANDLE(pio), t, (DWORD)wcslen(t), 0, 0);
free(t);
} else {
processBuffer(WINHANDLE(pio), pio->write_details.buf, pio->sync_write_status.to_transfer, &respbuf, &resplen);
/* TODO - respbuf is not null in some cases, this needs to be returned back via read stream */
}
pio->sync_write_status.transferred = pio->sync_write_status.to_transfer;
} else {
if (!WriteFile(WINHANDLE(pio), pio->write_details.buf, pio->sync_write_status.to_transfer,
&(pio->sync_write_status.transferred), NULL)) {
pio->sync_write_status.error = GetLastError();
debug4("WriteThread - WriteFile %d, io:%p", GetLastError(), pio);
}
}
if (0 == QueueUserAPC(WriteAPCProc, main_thread, (ULONG_PTR)pio)) {
error("WriteThread thread - ERROR QueueUserAPC failed %d, io:%p", GetLastError(), pio);
pio->write_details.pending = FALSE;
pio->write_details.error = GetLastError();
debug_assert_internal();
}
return 0;
}
/* Initiates write on tty */
int
syncio_initiate_write(struct w32_io* pio, DWORD num_bytes)
{
HANDLE write_thread;
debug5("syncio_initiate_write initiate io:%p", pio);
memset(&(pio->sync_write_status), 0, sizeof(pio->sync_write_status));
pio->sync_write_status.to_transfer = num_bytes;
write_thread = (HANDLE)_beginthreadex(NULL, 0, WriteThread, pio, 0, NULL);
if (write_thread == NULL) {
errno = errno_from_Win32LastError();
debug3("syncio_initiate_write initiate - ERROR _beginthreadex %d, io:%p", GetLastError(), pio);
return -1;
}
pio->write_overlapped.hEvent = write_thread;
pio->write_details.pending = TRUE;
return 0;
}
static VOID CALLBACK
InterruptThread(_In_ ULONG_PTR dwParam)
{
_endthreadex(0);
}
/* close */
int
syncio_close(struct w32_io* pio)
{
debug4("syncio_close - pio:%p", pio);
CancelIoEx(WINHANDLE(pio), NULL);
/* If io is pending, let worker threads exit. */
if (pio->read_details.pending) {
/*
Terminate the read thread at the below situations:
1. For console - the read thread is blocked by the while loop on raw mode
2. Function ReadFile on Win7 machine dees not return when no content to read in non-interactive mode.
*/
if (FILETYPE(pio) == FILE_TYPE_CHAR && (IsWin7OrLess() || in_raw_mode)) {
QueueUserAPC(InterruptThread, pio->read_overlapped.hEvent, (ULONG_PTR)NULL);
CancelSynchronousIo(pio->read_overlapped.hEvent);
}
WaitForSingleObject(pio->read_overlapped.hEvent, INFINITE);
}
if (pio->write_details.pending)
WaitForSingleObject(pio->write_overlapped.hEvent, INFINITE);
/* drain queued APCs */
SleepEx(0, TRUE);
/* TODO - fix this, closing Console handles is interfering with TTY/PTY rendering */
if (FILETYPE(pio) != FILE_TYPE_CHAR)
CloseHandle(WINHANDLE(pio));
if (pio->read_details.buf)
free(pio->read_details.buf);
if (pio->write_details.buf)
free(pio->write_details.buf);
free(pio);
return 0;
}