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