34292b1d |
1 | /* |
2 | * winhandl.c: Module to give Windows front ends the general |
3 | * ability to deal with consoles, pipes, serial ports, or any other |
4 | * type of data stream accessed through a Windows API HANDLE rather |
5 | * than a WinSock SOCKET. |
6 | * |
7 | * We do this by spawning a subthread to continuously try to read |
8 | * from the handle. Every time a read successfully returns some |
9 | * data, the subthread sets an event object which is picked up by |
10 | * the main thread, and the main thread then sets an event in |
11 | * return to instruct the subthread to resume reading. |
12 | * |
13 | * Output works precisely the other way round, in a second |
14 | * subthread. The output subthread should not be attempting to |
15 | * write all the time, because it hasn't always got data _to_ |
16 | * write; so the output thread waits for an event object notifying |
17 | * it to _attempt_ a write, and then it sets an event in return |
18 | * when one completes. |
8e32bfe0 |
19 | * |
20 | * (It's terribly annoying having to spawn a subthread for each |
21 | * direction of each handle. Technically it isn't necessary for |
22 | * serial ports, since we could use overlapped I/O within the main |
23 | * thread and wait directly on the event objects in the OVERLAPPED |
24 | * structures. However, we can't use this trick for some types of |
25 | * file handle at all - for some reason Windows restricts use of |
26 | * OVERLAPPED to files which were opened with the overlapped flag - |
27 | * and so we must use threads for those. This being the case, it's |
28 | * simplest just to use threads for everything rather than trying |
29 | * to keep track of multiple completely separate mechanisms.) |
34292b1d |
30 | */ |
31 | |
34292b1d |
32 | #include <assert.h> |
33 | |
34 | #include "putty.h" |
35 | |
36 | /* ---------------------------------------------------------------------- |
37 | * Generic definitions. |
38 | */ |
39 | |
40 | /* |
41 | * Maximum amount of backlog we will allow to build up on an input |
42 | * handle before we stop reading from it. |
43 | */ |
44 | #define MAX_BACKLOG 32768 |
45 | |
46 | struct handle_generic { |
47 | /* |
48 | * Initial fields common to both handle_input and handle_output |
49 | * structures. |
50 | * |
51 | * The three HANDLEs are set up at initialisation time and are |
52 | * thereafter read-only to both main thread and subthread. |
53 | * `moribund' is only used by the main thread; `done' is |
54 | * written by the main thread before signalling to the |
55 | * subthread. `defunct' and `busy' are used only by the main |
56 | * thread. |
57 | */ |
58 | HANDLE h; /* the handle itself */ |
59 | HANDLE ev_to_main; /* event used to signal main thread */ |
60 | HANDLE ev_from_main; /* event used to signal back to us */ |
61 | int moribund; /* are we going to kill this soon? */ |
62 | int done; /* request subthread to terminate */ |
63 | int defunct; /* has the subthread already gone? */ |
64 | int busy; /* operation currently in progress? */ |
0e03ceff |
65 | void *privdata; /* for client to remember who they are */ |
34292b1d |
66 | }; |
67 | |
68 | /* ---------------------------------------------------------------------- |
69 | * Input threads. |
70 | */ |
71 | |
72 | /* |
73 | * Data required by an input thread. |
74 | */ |
75 | struct handle_input { |
76 | /* |
77 | * Copy of the handle_generic structure. |
78 | */ |
79 | HANDLE h; /* the handle itself */ |
80 | HANDLE ev_to_main; /* event used to signal main thread */ |
81 | HANDLE ev_from_main; /* event used to signal back to us */ |
82 | int moribund; /* are we going to kill this soon? */ |
83 | int done; /* request subthread to terminate */ |
84 | int defunct; /* has the subthread already gone? */ |
85 | int busy; /* operation currently in progress? */ |
0e03ceff |
86 | void *privdata; /* for client to remember who they are */ |
34292b1d |
87 | |
88 | /* |
bdebd7e9 |
89 | * Data set at initialisation and then read-only. |
90 | */ |
91 | int flags; |
92 | |
93 | /* |
34292b1d |
94 | * Data set by the input thread before signalling ev_to_main, |
95 | * and read by the main thread after receiving that signal. |
96 | */ |
97 | char buffer[4096]; /* the data read from the handle */ |
98 | DWORD len; /* how much data that was */ |
99 | int readret; /* lets us know about read errors */ |
100 | |
101 | /* |
102 | * Callback function called by this module when data arrives on |
103 | * an input handle. |
104 | */ |
105 | handle_inputfn_t gotdata; |
106 | }; |
107 | |
108 | /* |
109 | * The actual thread procedure for an input thread. |
110 | */ |
111 | static DWORD WINAPI handle_input_threadfunc(void *param) |
112 | { |
113 | struct handle_input *ctx = (struct handle_input *) param; |
bdebd7e9 |
114 | OVERLAPPED ovl, *povl; |
758a1377 |
115 | HANDLE oev; |
c606abbc |
116 | int readlen; |
bdebd7e9 |
117 | |
758a1377 |
118 | if (ctx->flags & HANDLE_FLAG_OVERLAPPED) { |
bdebd7e9 |
119 | povl = &ovl; |
758a1377 |
120 | oev = CreateEvent(NULL, TRUE, FALSE, NULL); |
121 | } else { |
bdebd7e9 |
122 | povl = NULL; |
758a1377 |
123 | } |
34292b1d |
124 | |
c606abbc |
125 | if (ctx->flags & HANDLE_FLAG_UNITBUFFER) |
126 | readlen = 1; |
127 | else |
128 | readlen = sizeof(ctx->buffer); |
129 | |
34292b1d |
130 | while (1) { |
758a1377 |
131 | if (povl) { |
bdebd7e9 |
132 | memset(povl, 0, sizeof(OVERLAPPED)); |
758a1377 |
133 | povl->hEvent = oev; |
134 | } |
c606abbc |
135 | ctx->readret = ReadFile(ctx->h, ctx->buffer, readlen, |
bdebd7e9 |
136 | &ctx->len, povl); |
425f5af2 |
137 | if (!ctx->readret) |
138 | error = GetLastError(); |
139 | if (povl && !ctx->readret && error == ERROR_IO_PENDING) { |
758a1377 |
140 | WaitForSingleObject(povl->hEvent, INFINITE); |
141 | ctx->readret = GetOverlappedResult(ctx->h, povl, &ctx->len, FALSE); |
142 | } |
bdebd7e9 |
143 | |
425f5af2 |
144 | if (!ctx->readret) { |
145 | /* |
146 | * Windows apparently sends ERROR_BROKEN_PIPE when a |
147 | * pipe we're reading from is closed normally from the |
148 | * writing end. This is ludicrous; if that situation |
149 | * isn't a natural EOF, _nothing_ is. So if we get that |
150 | * particular error, we pretend it's EOF. |
151 | */ |
152 | if (error == ERROR_BROKEN_PIPE) |
153 | ctx->readret = 1; |
34292b1d |
154 | ctx->len = 0; |
425f5af2 |
155 | } |
34292b1d |
156 | |
bdebd7e9 |
157 | if (ctx->readret && ctx->len == 0 && |
158 | (ctx->flags & HANDLE_FLAG_IGNOREEOF)) |
159 | continue; |
160 | |
34292b1d |
161 | SetEvent(ctx->ev_to_main); |
162 | |
163 | if (!ctx->len) |
164 | break; |
165 | |
166 | WaitForSingleObject(ctx->ev_from_main, INFINITE); |
167 | if (ctx->done) |
168 | break; /* main thread told us to shut down */ |
169 | } |
170 | |
758a1377 |
171 | if (povl) |
172 | CloseHandle(oev); |
173 | |
34292b1d |
174 | return 0; |
175 | } |
176 | |
177 | /* |
178 | * This is called after a succcessful read, or from the |
179 | * `unthrottle' function. It decides whether or not to begin a new |
180 | * read operation. |
181 | */ |
182 | static void handle_throttle(struct handle_input *ctx, int backlog) |
183 | { |
50ab783a |
184 | if (ctx->defunct) |
185 | return; |
34292b1d |
186 | |
187 | /* |
188 | * If there's a read operation already in progress, do nothing: |
189 | * when that completes, we'll come back here and be in a |
190 | * position to make a better decision. |
191 | */ |
192 | if (ctx->busy) |
193 | return; |
194 | |
195 | /* |
196 | * Otherwise, we must decide whether to start a new read based |
197 | * on the size of the backlog. |
198 | */ |
199 | if (backlog < MAX_BACKLOG) { |
200 | SetEvent(ctx->ev_from_main); |
201 | ctx->busy = TRUE; |
202 | } |
203 | } |
204 | |
205 | /* ---------------------------------------------------------------------- |
206 | * Output threads. |
207 | */ |
208 | |
209 | /* |
210 | * Data required by an output thread. |
211 | */ |
212 | struct handle_output { |
213 | /* |
214 | * Copy of the handle_generic structure. |
215 | */ |
216 | HANDLE h; /* the handle itself */ |
217 | HANDLE ev_to_main; /* event used to signal main thread */ |
218 | HANDLE ev_from_main; /* event used to signal back to us */ |
219 | int moribund; /* are we going to kill this soon? */ |
220 | int done; /* request subthread to terminate */ |
221 | int defunct; /* has the subthread already gone? */ |
222 | int busy; /* operation currently in progress? */ |
0e03ceff |
223 | void *privdata; /* for client to remember who they are */ |
34292b1d |
224 | |
225 | /* |
bdebd7e9 |
226 | * Data set at initialisation and then read-only. |
227 | */ |
228 | int flags; |
229 | |
230 | /* |
34292b1d |
231 | * Data set by the main thread before signalling ev_from_main, |
232 | * and read by the input thread after receiving that signal. |
233 | */ |
234 | char *buffer; /* the data to write */ |
235 | DWORD len; /* how much data there is */ |
236 | |
237 | /* |
238 | * Data set by the input thread before signalling ev_to_main, |
239 | * and read by the main thread after receiving that signal. |
240 | */ |
241 | DWORD lenwritten; /* how much data we actually wrote */ |
242 | int writeret; /* return value from WriteFile */ |
243 | |
244 | /* |
245 | * Data only ever read or written by the main thread. |
246 | */ |
247 | bufchain queued_data; /* data still waiting to be written */ |
248 | |
249 | /* |
250 | * Callback function called when the backlog in the bufchain |
251 | * drops. |
252 | */ |
253 | handle_outputfn_t sentdata; |
254 | }; |
255 | |
256 | static DWORD WINAPI handle_output_threadfunc(void *param) |
257 | { |
258 | struct handle_output *ctx = (struct handle_output *) param; |
bdebd7e9 |
259 | OVERLAPPED ovl, *povl; |
260 | |
261 | if (ctx->flags & HANDLE_FLAG_OVERLAPPED) |
262 | povl = &ovl; |
263 | else |
264 | povl = NULL; |
34292b1d |
265 | |
266 | while (1) { |
267 | WaitForSingleObject(ctx->ev_from_main, INFINITE); |
268 | if (ctx->done) { |
269 | SetEvent(ctx->ev_to_main); |
270 | break; |
271 | } |
bdebd7e9 |
272 | if (povl) |
273 | memset(povl, 0, sizeof(OVERLAPPED)); |
34292b1d |
274 | ctx->writeret = WriteFile(ctx->h, ctx->buffer, ctx->len, |
bdebd7e9 |
275 | &ctx->lenwritten, povl); |
276 | if (povl && !ctx->writeret && GetLastError() == ERROR_IO_PENDING) |
277 | ctx->writeret = GetOverlappedResult(ctx->h, povl, |
278 | &ctx->lenwritten, TRUE); |
279 | |
34292b1d |
280 | SetEvent(ctx->ev_to_main); |
281 | if (!ctx->writeret) |
282 | break; |
283 | } |
284 | |
285 | return 0; |
286 | } |
287 | |
288 | static void handle_try_output(struct handle_output *ctx) |
289 | { |
290 | void *senddata; |
291 | int sendlen; |
292 | |
293 | if (!ctx->busy && bufchain_size(&ctx->queued_data)) { |
294 | bufchain_prefix(&ctx->queued_data, &senddata, &sendlen); |
295 | ctx->buffer = senddata; |
296 | ctx->len = sendlen; |
297 | SetEvent(ctx->ev_from_main); |
298 | ctx->busy = TRUE; |
299 | } |
300 | } |
301 | |
302 | /* ---------------------------------------------------------------------- |
303 | * Unified code handling both input and output threads. |
304 | */ |
305 | |
306 | struct handle { |
307 | int output; |
308 | union { |
309 | struct handle_generic g; |
310 | struct handle_input i; |
311 | struct handle_output o; |
312 | } u; |
313 | }; |
314 | |
315 | static tree234 *handles_by_evtomain; |
316 | |
317 | static int handle_cmp_evtomain(void *av, void *bv) |
318 | { |
319 | struct handle *a = (struct handle *)av; |
320 | struct handle *b = (struct handle *)bv; |
321 | |
322 | if ((unsigned)a->u.g.ev_to_main < (unsigned)b->u.g.ev_to_main) |
323 | return -1; |
324 | else if ((unsigned)a->u.g.ev_to_main > (unsigned)b->u.g.ev_to_main) |
325 | return +1; |
326 | else |
327 | return 0; |
328 | } |
329 | |
330 | static int handle_find_evtomain(void *av, void *bv) |
331 | { |
332 | HANDLE *a = (HANDLE *)av; |
333 | struct handle *b = (struct handle *)bv; |
334 | |
335 | if ((unsigned)*a < (unsigned)b->u.g.ev_to_main) |
336 | return -1; |
337 | else if ((unsigned)*a > (unsigned)b->u.g.ev_to_main) |
338 | return +1; |
339 | else |
340 | return 0; |
341 | } |
342 | |
0e03ceff |
343 | struct handle *handle_input_new(HANDLE handle, handle_inputfn_t gotdata, |
bdebd7e9 |
344 | void *privdata, int flags) |
34292b1d |
345 | { |
346 | struct handle *h = snew(struct handle); |
600f6499 |
347 | DWORD in_threadid; /* required for Win9x */ |
34292b1d |
348 | |
349 | h->output = FALSE; |
350 | h->u.i.h = handle; |
351 | h->u.i.ev_to_main = CreateEvent(NULL, FALSE, FALSE, NULL); |
352 | h->u.i.ev_from_main = CreateEvent(NULL, FALSE, FALSE, NULL); |
353 | h->u.i.gotdata = gotdata; |
34292b1d |
354 | h->u.i.defunct = FALSE; |
355 | h->u.i.moribund = FALSE; |
356 | h->u.i.done = FALSE; |
0e03ceff |
357 | h->u.i.privdata = privdata; |
bdebd7e9 |
358 | h->u.i.flags = flags; |
34292b1d |
359 | |
360 | if (!handles_by_evtomain) |
361 | handles_by_evtomain = newtree234(handle_cmp_evtomain); |
362 | add234(handles_by_evtomain, h); |
363 | |
364 | CreateThread(NULL, 0, handle_input_threadfunc, |
600f6499 |
365 | &h->u.i, 0, &in_threadid); |
2ceabd36 |
366 | h->u.i.busy = TRUE; |
34292b1d |
367 | |
368 | return h; |
369 | } |
370 | |
0e03ceff |
371 | struct handle *handle_output_new(HANDLE handle, handle_outputfn_t sentdata, |
bdebd7e9 |
372 | void *privdata, int flags) |
34292b1d |
373 | { |
374 | struct handle *h = snew(struct handle); |
600f6499 |
375 | DWORD out_threadid; /* required for Win9x */ |
34292b1d |
376 | |
377 | h->output = TRUE; |
378 | h->u.o.h = handle; |
379 | h->u.o.ev_to_main = CreateEvent(NULL, FALSE, FALSE, NULL); |
380 | h->u.o.ev_from_main = CreateEvent(NULL, FALSE, FALSE, NULL); |
381 | h->u.o.busy = FALSE; |
382 | h->u.o.defunct = FALSE; |
383 | h->u.o.moribund = FALSE; |
384 | h->u.o.done = FALSE; |
0e03ceff |
385 | h->u.o.privdata = privdata; |
34292b1d |
386 | bufchain_init(&h->u.o.queued_data); |
387 | h->u.o.sentdata = sentdata; |
bdebd7e9 |
388 | h->u.o.flags = flags; |
34292b1d |
389 | |
390 | if (!handles_by_evtomain) |
391 | handles_by_evtomain = newtree234(handle_cmp_evtomain); |
392 | add234(handles_by_evtomain, h); |
393 | |
394 | CreateThread(NULL, 0, handle_output_threadfunc, |
600f6499 |
395 | &h->u.i, 0, &out_threadid); |
34292b1d |
396 | |
397 | return h; |
398 | } |
399 | |
400 | int handle_write(struct handle *h, const void *data, int len) |
401 | { |
402 | assert(h->output); |
403 | bufchain_add(&h->u.o.queued_data, data, len); |
404 | handle_try_output(&h->u.o); |
405 | return bufchain_size(&h->u.o.queued_data); |
406 | } |
407 | |
408 | HANDLE *handle_get_events(int *nevents) |
409 | { |
410 | HANDLE *ret; |
411 | struct handle *h; |
412 | int i, n, size; |
413 | |
414 | /* |
415 | * Go through our tree counting the handle objects currently |
416 | * engaged in useful activity. |
417 | */ |
418 | ret = NULL; |
419 | n = size = 0; |
420 | if (handles_by_evtomain) { |
421 | for (i = 0; (h = index234(handles_by_evtomain, i)) != NULL; i++) { |
422 | if (h->u.g.busy) { |
423 | if (n >= size) { |
424 | size += 32; |
425 | ret = sresize(ret, size, HANDLE); |
426 | } |
427 | ret[n++] = h->u.g.ev_to_main; |
428 | } |
429 | } |
430 | } |
431 | |
432 | *nevents = n; |
433 | return ret; |
434 | } |
435 | |
436 | static void handle_destroy(struct handle *h) |
437 | { |
438 | if (h->output) |
439 | bufchain_clear(&h->u.o.queued_data); |
440 | CloseHandle(h->u.g.ev_from_main); |
441 | CloseHandle(h->u.g.ev_to_main); |
442 | del234(handles_by_evtomain, h); |
443 | sfree(h); |
444 | } |
445 | |
446 | void handle_free(struct handle *h) |
447 | { |
448 | /* |
449 | * If the handle is currently busy, we cannot immediately free |
450 | * it. Instead we must wait until it's finished its current |
451 | * operation, because otherwise the subthread will write to |
452 | * invalid memory after we free its context from under it. |
453 | */ |
454 | assert(h && !h->u.g.moribund); |
455 | if (h->u.g.busy) { |
456 | /* |
457 | * Just set the moribund flag, which will be noticed next |
458 | * time an operation completes. |
459 | */ |
460 | h->u.g.moribund = TRUE; |
461 | } else if (h->u.g.defunct) { |
462 | /* |
463 | * There isn't even a subthread; we can go straight to |
464 | * handle_destroy. |
465 | */ |
466 | handle_destroy(h); |
467 | } else { |
468 | /* |
469 | * The subthread is alive but not busy, so we now signal it |
470 | * to die. Set the moribund flag to indicate that it will |
471 | * want destroying after that. |
472 | */ |
473 | h->u.g.moribund = TRUE; |
474 | h->u.g.done = TRUE; |
c969e831 |
475 | h->u.g.busy = TRUE; |
34292b1d |
476 | SetEvent(h->u.g.ev_from_main); |
477 | } |
478 | } |
479 | |
480 | void handle_got_event(HANDLE event) |
481 | { |
482 | struct handle *h; |
483 | |
484 | assert(handles_by_evtomain); |
485 | h = find234(handles_by_evtomain, &event, handle_find_evtomain); |
486 | if (!h) { |
487 | /* |
488 | * This isn't an error condition. If two or more event |
489 | * objects were signalled during the same select operation, |
490 | * and processing of the first caused the second handle to |
491 | * be closed, then it will sometimes happen that we receive |
492 | * an event notification here for a handle which is already |
493 | * deceased. In that situation we simply do nothing. |
494 | */ |
495 | return; |
496 | } |
497 | |
498 | if (h->u.g.moribund) { |
499 | /* |
500 | * A moribund handle is already treated as dead from the |
501 | * external user's point of view, so do nothing with the |
502 | * actual event. Just signal the thread to die if |
503 | * necessary, or destroy the handle if not. |
504 | */ |
505 | if (h->u.g.done) { |
506 | handle_destroy(h); |
507 | } else { |
508 | h->u.g.done = TRUE; |
c969e831 |
509 | h->u.g.busy = TRUE; |
34292b1d |
510 | SetEvent(h->u.g.ev_from_main); |
511 | } |
512 | return; |
513 | } |
514 | |
515 | if (!h->output) { |
516 | int backlog; |
517 | |
518 | h->u.i.busy = FALSE; |
519 | |
520 | /* |
521 | * A signal on an input handle means data has arrived. |
522 | */ |
523 | if (h->u.i.len == 0) { |
524 | /* |
525 | * EOF, or (nearly equivalently) read error. |
526 | */ |
527 | h->u.i.gotdata(h, NULL, (h->u.i.readret ? 0 : -1)); |
528 | h->u.i.defunct = TRUE; |
529 | } else { |
530 | backlog = h->u.i.gotdata(h, h->u.i.buffer, h->u.i.len); |
531 | handle_throttle(&h->u.i, backlog); |
532 | } |
533 | } else { |
534 | h->u.o.busy = FALSE; |
535 | |
536 | /* |
537 | * A signal on an output handle means we have completed a |
538 | * write. Call the callback to indicate that the output |
539 | * buffer size has decreased, or to indicate an error. |
540 | */ |
541 | if (!h->u.o.writeret) { |
542 | /* |
543 | * Write error. Send a negative value to the callback, |
544 | * and mark the thread as defunct (because the output |
545 | * thread is terminating by now). |
546 | */ |
547 | h->u.o.sentdata(h, -1); |
548 | h->u.o.defunct = TRUE; |
549 | } else { |
550 | bufchain_consume(&h->u.o.queued_data, h->u.o.lenwritten); |
551 | h->u.o.sentdata(h, bufchain_size(&h->u.o.queued_data)); |
552 | handle_try_output(&h->u.o); |
553 | } |
554 | } |
555 | } |
556 | |
557 | void handle_unthrottle(struct handle *h, int backlog) |
558 | { |
559 | assert(!h->output); |
560 | handle_throttle(&h->u.i, backlog); |
561 | } |
562 | |
563 | int handle_backlog(struct handle *h) |
564 | { |
565 | assert(h->output); |
566 | return bufchain_size(&h->u.o.queued_data); |
567 | } |
0e03ceff |
568 | |
569 | void *handle_get_privdata(struct handle *h) |
570 | { |
571 | return h->u.g.privdata; |
572 | } |