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. |
19 | */ |
20 | |
21 | /* |
22 | * TODO: |
23 | * |
24 | * - could do with some sort of private-data field in each handle |
25 | * structure. |
26 | */ |
27 | |
28 | #include <assert.h> |
29 | |
30 | #include "putty.h" |
31 | |
32 | /* ---------------------------------------------------------------------- |
33 | * Generic definitions. |
34 | */ |
35 | |
36 | /* |
37 | * Maximum amount of backlog we will allow to build up on an input |
38 | * handle before we stop reading from it. |
39 | */ |
40 | #define MAX_BACKLOG 32768 |
41 | |
42 | struct handle_generic { |
43 | /* |
44 | * Initial fields common to both handle_input and handle_output |
45 | * structures. |
46 | * |
47 | * The three HANDLEs are set up at initialisation time and are |
48 | * thereafter read-only to both main thread and subthread. |
49 | * `moribund' is only used by the main thread; `done' is |
50 | * written by the main thread before signalling to the |
51 | * subthread. `defunct' and `busy' are used only by the main |
52 | * thread. |
53 | */ |
54 | HANDLE h; /* the handle itself */ |
55 | HANDLE ev_to_main; /* event used to signal main thread */ |
56 | HANDLE ev_from_main; /* event used to signal back to us */ |
57 | int moribund; /* are we going to kill this soon? */ |
58 | int done; /* request subthread to terminate */ |
59 | int defunct; /* has the subthread already gone? */ |
60 | int busy; /* operation currently in progress? */ |
61 | }; |
62 | |
63 | /* ---------------------------------------------------------------------- |
64 | * Input threads. |
65 | */ |
66 | |
67 | /* |
68 | * Data required by an input thread. |
69 | */ |
70 | struct handle_input { |
71 | /* |
72 | * Copy of the handle_generic structure. |
73 | */ |
74 | HANDLE h; /* the handle itself */ |
75 | HANDLE ev_to_main; /* event used to signal main thread */ |
76 | HANDLE ev_from_main; /* event used to signal back to us */ |
77 | int moribund; /* are we going to kill this soon? */ |
78 | int done; /* request subthread to terminate */ |
79 | int defunct; /* has the subthread already gone? */ |
80 | int busy; /* operation currently in progress? */ |
81 | |
82 | /* |
83 | * Data set by the input thread before signalling ev_to_main, |
84 | * and read by the main thread after receiving that signal. |
85 | */ |
86 | char buffer[4096]; /* the data read from the handle */ |
87 | DWORD len; /* how much data that was */ |
88 | int readret; /* lets us know about read errors */ |
89 | |
90 | /* |
91 | * Callback function called by this module when data arrives on |
92 | * an input handle. |
93 | */ |
94 | handle_inputfn_t gotdata; |
95 | }; |
96 | |
97 | /* |
98 | * The actual thread procedure for an input thread. |
99 | */ |
100 | static DWORD WINAPI handle_input_threadfunc(void *param) |
101 | { |
102 | struct handle_input *ctx = (struct handle_input *) param; |
103 | |
104 | while (1) { |
105 | ctx->readret = ReadFile(ctx->h, ctx->buffer, sizeof(ctx->buffer), |
106 | &ctx->len, NULL); |
107 | if (!ctx->readret) |
108 | ctx->len = 0; |
109 | |
110 | SetEvent(ctx->ev_to_main); |
111 | |
112 | if (!ctx->len) |
113 | break; |
114 | |
115 | WaitForSingleObject(ctx->ev_from_main, INFINITE); |
116 | if (ctx->done) |
117 | break; /* main thread told us to shut down */ |
118 | } |
119 | |
120 | return 0; |
121 | } |
122 | |
123 | /* |
124 | * This is called after a succcessful read, or from the |
125 | * `unthrottle' function. It decides whether or not to begin a new |
126 | * read operation. |
127 | */ |
128 | static void handle_throttle(struct handle_input *ctx, int backlog) |
129 | { |
130 | assert(!ctx->defunct); |
131 | |
132 | /* |
133 | * If there's a read operation already in progress, do nothing: |
134 | * when that completes, we'll come back here and be in a |
135 | * position to make a better decision. |
136 | */ |
137 | if (ctx->busy) |
138 | return; |
139 | |
140 | /* |
141 | * Otherwise, we must decide whether to start a new read based |
142 | * on the size of the backlog. |
143 | */ |
144 | if (backlog < MAX_BACKLOG) { |
145 | SetEvent(ctx->ev_from_main); |
146 | ctx->busy = TRUE; |
147 | } |
148 | } |
149 | |
150 | /* ---------------------------------------------------------------------- |
151 | * Output threads. |
152 | */ |
153 | |
154 | /* |
155 | * Data required by an output thread. |
156 | */ |
157 | struct handle_output { |
158 | /* |
159 | * Copy of the handle_generic structure. |
160 | */ |
161 | HANDLE h; /* the handle itself */ |
162 | HANDLE ev_to_main; /* event used to signal main thread */ |
163 | HANDLE ev_from_main; /* event used to signal back to us */ |
164 | int moribund; /* are we going to kill this soon? */ |
165 | int done; /* request subthread to terminate */ |
166 | int defunct; /* has the subthread already gone? */ |
167 | int busy; /* operation currently in progress? */ |
168 | |
169 | /* |
170 | * Data set by the main thread before signalling ev_from_main, |
171 | * and read by the input thread after receiving that signal. |
172 | */ |
173 | char *buffer; /* the data to write */ |
174 | DWORD len; /* how much data there is */ |
175 | |
176 | /* |
177 | * Data set by the input thread before signalling ev_to_main, |
178 | * and read by the main thread after receiving that signal. |
179 | */ |
180 | DWORD lenwritten; /* how much data we actually wrote */ |
181 | int writeret; /* return value from WriteFile */ |
182 | |
183 | /* |
184 | * Data only ever read or written by the main thread. |
185 | */ |
186 | bufchain queued_data; /* data still waiting to be written */ |
187 | |
188 | /* |
189 | * Callback function called when the backlog in the bufchain |
190 | * drops. |
191 | */ |
192 | handle_outputfn_t sentdata; |
193 | }; |
194 | |
195 | static DWORD WINAPI handle_output_threadfunc(void *param) |
196 | { |
197 | struct handle_output *ctx = (struct handle_output *) param; |
198 | |
199 | while (1) { |
200 | WaitForSingleObject(ctx->ev_from_main, INFINITE); |
201 | if (ctx->done) { |
202 | SetEvent(ctx->ev_to_main); |
203 | break; |
204 | } |
205 | ctx->writeret = WriteFile(ctx->h, ctx->buffer, ctx->len, |
206 | &ctx->lenwritten, NULL); |
207 | SetEvent(ctx->ev_to_main); |
208 | if (!ctx->writeret) |
209 | break; |
210 | } |
211 | |
212 | return 0; |
213 | } |
214 | |
215 | static void handle_try_output(struct handle_output *ctx) |
216 | { |
217 | void *senddata; |
218 | int sendlen; |
219 | |
220 | if (!ctx->busy && bufchain_size(&ctx->queued_data)) { |
221 | bufchain_prefix(&ctx->queued_data, &senddata, &sendlen); |
222 | ctx->buffer = senddata; |
223 | ctx->len = sendlen; |
224 | SetEvent(ctx->ev_from_main); |
225 | ctx->busy = TRUE; |
226 | } |
227 | } |
228 | |
229 | /* ---------------------------------------------------------------------- |
230 | * Unified code handling both input and output threads. |
231 | */ |
232 | |
233 | struct handle { |
234 | int output; |
235 | union { |
236 | struct handle_generic g; |
237 | struct handle_input i; |
238 | struct handle_output o; |
239 | } u; |
240 | }; |
241 | |
242 | static tree234 *handles_by_evtomain; |
243 | |
244 | static int handle_cmp_evtomain(void *av, void *bv) |
245 | { |
246 | struct handle *a = (struct handle *)av; |
247 | struct handle *b = (struct handle *)bv; |
248 | |
249 | if ((unsigned)a->u.g.ev_to_main < (unsigned)b->u.g.ev_to_main) |
250 | return -1; |
251 | else if ((unsigned)a->u.g.ev_to_main > (unsigned)b->u.g.ev_to_main) |
252 | return +1; |
253 | else |
254 | return 0; |
255 | } |
256 | |
257 | static int handle_find_evtomain(void *av, void *bv) |
258 | { |
259 | HANDLE *a = (HANDLE *)av; |
260 | struct handle *b = (struct handle *)bv; |
261 | |
262 | if ((unsigned)*a < (unsigned)b->u.g.ev_to_main) |
263 | return -1; |
264 | else if ((unsigned)*a > (unsigned)b->u.g.ev_to_main) |
265 | return +1; |
266 | else |
267 | return 0; |
268 | } |
269 | |
270 | struct handle *handle_input_new(HANDLE handle, handle_inputfn_t gotdata) |
271 | { |
272 | struct handle *h = snew(struct handle); |
273 | |
274 | h->output = FALSE; |
275 | h->u.i.h = handle; |
276 | h->u.i.ev_to_main = CreateEvent(NULL, FALSE, FALSE, NULL); |
277 | h->u.i.ev_from_main = CreateEvent(NULL, FALSE, FALSE, NULL); |
278 | h->u.i.gotdata = gotdata; |
279 | h->u.i.busy = FALSE; |
280 | h->u.i.defunct = FALSE; |
281 | h->u.i.moribund = FALSE; |
282 | h->u.i.done = FALSE; |
283 | |
284 | if (!handles_by_evtomain) |
285 | handles_by_evtomain = newtree234(handle_cmp_evtomain); |
286 | add234(handles_by_evtomain, h); |
287 | |
288 | CreateThread(NULL, 0, handle_input_threadfunc, |
289 | &h->u.i, 0, NULL); |
290 | |
291 | handle_throttle(&h->u.i, 0); /* start first read operation */ |
292 | |
293 | return h; |
294 | } |
295 | |
296 | struct handle *handle_output_new(HANDLE handle, handle_outputfn_t sentdata) |
297 | { |
298 | struct handle *h = snew(struct handle); |
299 | |
300 | h->output = TRUE; |
301 | h->u.o.h = handle; |
302 | h->u.o.ev_to_main = CreateEvent(NULL, FALSE, FALSE, NULL); |
303 | h->u.o.ev_from_main = CreateEvent(NULL, FALSE, FALSE, NULL); |
304 | h->u.o.busy = FALSE; |
305 | h->u.o.defunct = FALSE; |
306 | h->u.o.moribund = FALSE; |
307 | h->u.o.done = FALSE; |
308 | bufchain_init(&h->u.o.queued_data); |
309 | h->u.o.sentdata = sentdata; |
310 | |
311 | if (!handles_by_evtomain) |
312 | handles_by_evtomain = newtree234(handle_cmp_evtomain); |
313 | add234(handles_by_evtomain, h); |
314 | |
315 | CreateThread(NULL, 0, handle_output_threadfunc, |
316 | &h->u.i, 0, NULL); |
317 | |
318 | return h; |
319 | } |
320 | |
321 | int handle_write(struct handle *h, const void *data, int len) |
322 | { |
323 | assert(h->output); |
324 | bufchain_add(&h->u.o.queued_data, data, len); |
325 | handle_try_output(&h->u.o); |
326 | return bufchain_size(&h->u.o.queued_data); |
327 | } |
328 | |
329 | HANDLE *handle_get_events(int *nevents) |
330 | { |
331 | HANDLE *ret; |
332 | struct handle *h; |
333 | int i, n, size; |
334 | |
335 | /* |
336 | * Go through our tree counting the handle objects currently |
337 | * engaged in useful activity. |
338 | */ |
339 | ret = NULL; |
340 | n = size = 0; |
341 | if (handles_by_evtomain) { |
342 | for (i = 0; (h = index234(handles_by_evtomain, i)) != NULL; i++) { |
343 | if (h->u.g.busy) { |
344 | if (n >= size) { |
345 | size += 32; |
346 | ret = sresize(ret, size, HANDLE); |
347 | } |
348 | ret[n++] = h->u.g.ev_to_main; |
349 | } |
350 | } |
351 | } |
352 | |
353 | *nevents = n; |
354 | return ret; |
355 | } |
356 | |
357 | static void handle_destroy(struct handle *h) |
358 | { |
359 | if (h->output) |
360 | bufchain_clear(&h->u.o.queued_data); |
361 | CloseHandle(h->u.g.ev_from_main); |
362 | CloseHandle(h->u.g.ev_to_main); |
363 | del234(handles_by_evtomain, h); |
364 | sfree(h); |
365 | } |
366 | |
367 | void handle_free(struct handle *h) |
368 | { |
369 | /* |
370 | * If the handle is currently busy, we cannot immediately free |
371 | * it. Instead we must wait until it's finished its current |
372 | * operation, because otherwise the subthread will write to |
373 | * invalid memory after we free its context from under it. |
374 | */ |
375 | assert(h && !h->u.g.moribund); |
376 | if (h->u.g.busy) { |
377 | /* |
378 | * Just set the moribund flag, which will be noticed next |
379 | * time an operation completes. |
380 | */ |
381 | h->u.g.moribund = TRUE; |
382 | } else if (h->u.g.defunct) { |
383 | /* |
384 | * There isn't even a subthread; we can go straight to |
385 | * handle_destroy. |
386 | */ |
387 | handle_destroy(h); |
388 | } else { |
389 | /* |
390 | * The subthread is alive but not busy, so we now signal it |
391 | * to die. Set the moribund flag to indicate that it will |
392 | * want destroying after that. |
393 | */ |
394 | h->u.g.moribund = TRUE; |
395 | h->u.g.done = TRUE; |
396 | SetEvent(h->u.g.ev_from_main); |
397 | } |
398 | } |
399 | |
400 | void handle_got_event(HANDLE event) |
401 | { |
402 | struct handle *h; |
403 | |
404 | assert(handles_by_evtomain); |
405 | h = find234(handles_by_evtomain, &event, handle_find_evtomain); |
406 | if (!h) { |
407 | /* |
408 | * This isn't an error condition. If two or more event |
409 | * objects were signalled during the same select operation, |
410 | * and processing of the first caused the second handle to |
411 | * be closed, then it will sometimes happen that we receive |
412 | * an event notification here for a handle which is already |
413 | * deceased. In that situation we simply do nothing. |
414 | */ |
415 | return; |
416 | } |
417 | |
418 | if (h->u.g.moribund) { |
419 | /* |
420 | * A moribund handle is already treated as dead from the |
421 | * external user's point of view, so do nothing with the |
422 | * actual event. Just signal the thread to die if |
423 | * necessary, or destroy the handle if not. |
424 | */ |
425 | if (h->u.g.done) { |
426 | handle_destroy(h); |
427 | } else { |
428 | h->u.g.done = TRUE; |
429 | SetEvent(h->u.g.ev_from_main); |
430 | } |
431 | return; |
432 | } |
433 | |
434 | if (!h->output) { |
435 | int backlog; |
436 | |
437 | h->u.i.busy = FALSE; |
438 | |
439 | /* |
440 | * A signal on an input handle means data has arrived. |
441 | */ |
442 | if (h->u.i.len == 0) { |
443 | /* |
444 | * EOF, or (nearly equivalently) read error. |
445 | */ |
446 | h->u.i.gotdata(h, NULL, (h->u.i.readret ? 0 : -1)); |
447 | h->u.i.defunct = TRUE; |
448 | } else { |
449 | backlog = h->u.i.gotdata(h, h->u.i.buffer, h->u.i.len); |
450 | handle_throttle(&h->u.i, backlog); |
451 | } |
452 | } else { |
453 | h->u.o.busy = FALSE; |
454 | |
455 | /* |
456 | * A signal on an output handle means we have completed a |
457 | * write. Call the callback to indicate that the output |
458 | * buffer size has decreased, or to indicate an error. |
459 | */ |
460 | if (!h->u.o.writeret) { |
461 | /* |
462 | * Write error. Send a negative value to the callback, |
463 | * and mark the thread as defunct (because the output |
464 | * thread is terminating by now). |
465 | */ |
466 | h->u.o.sentdata(h, -1); |
467 | h->u.o.defunct = TRUE; |
468 | } else { |
469 | bufchain_consume(&h->u.o.queued_data, h->u.o.lenwritten); |
470 | h->u.o.sentdata(h, bufchain_size(&h->u.o.queued_data)); |
471 | handle_try_output(&h->u.o); |
472 | } |
473 | } |
474 | } |
475 | |
476 | void handle_unthrottle(struct handle *h, int backlog) |
477 | { |
478 | assert(!h->output); |
479 | handle_throttle(&h->u.i, backlog); |
480 | } |
481 | |
482 | int handle_backlog(struct handle *h) |
483 | { |
484 | assert(h->output); |
485 | return bufchain_size(&h->u.o.queued_data); |
486 | } |