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 | { |
50ab783a |
125 | if (ctx->defunct) |
126 | return; |
34292b1d |
127 | |
128 | /* |
129 | * If there's a read operation already in progress, do nothing: |
130 | * when that completes, we'll come back here and be in a |
131 | * position to make a better decision. |
132 | */ |
133 | if (ctx->busy) |
134 | return; |
135 | |
136 | /* |
137 | * Otherwise, we must decide whether to start a new read based |
138 | * on the size of the backlog. |
139 | */ |
140 | if (backlog < MAX_BACKLOG) { |
141 | SetEvent(ctx->ev_from_main); |
142 | ctx->busy = TRUE; |
143 | } |
144 | } |
145 | |
146 | /* ---------------------------------------------------------------------- |
147 | * Output threads. |
148 | */ |
149 | |
150 | /* |
151 | * Data required by an output thread. |
152 | */ |
153 | struct handle_output { |
154 | /* |
155 | * Copy of the handle_generic structure. |
156 | */ |
157 | HANDLE h; /* the handle itself */ |
158 | HANDLE ev_to_main; /* event used to signal main thread */ |
159 | HANDLE ev_from_main; /* event used to signal back to us */ |
160 | int moribund; /* are we going to kill this soon? */ |
161 | int done; /* request subthread to terminate */ |
162 | int defunct; /* has the subthread already gone? */ |
163 | int busy; /* operation currently in progress? */ |
0e03ceff |
164 | void *privdata; /* for client to remember who they are */ |
34292b1d |
165 | |
166 | /* |
167 | * Data set by the main thread before signalling ev_from_main, |
168 | * and read by the input thread after receiving that signal. |
169 | */ |
170 | char *buffer; /* the data to write */ |
171 | DWORD len; /* how much data there is */ |
172 | |
173 | /* |
174 | * Data set by the input thread before signalling ev_to_main, |
175 | * and read by the main thread after receiving that signal. |
176 | */ |
177 | DWORD lenwritten; /* how much data we actually wrote */ |
178 | int writeret; /* return value from WriteFile */ |
179 | |
180 | /* |
181 | * Data only ever read or written by the main thread. |
182 | */ |
183 | bufchain queued_data; /* data still waiting to be written */ |
184 | |
185 | /* |
186 | * Callback function called when the backlog in the bufchain |
187 | * drops. |
188 | */ |
189 | handle_outputfn_t sentdata; |
190 | }; |
191 | |
192 | static DWORD WINAPI handle_output_threadfunc(void *param) |
193 | { |
194 | struct handle_output *ctx = (struct handle_output *) param; |
195 | |
196 | while (1) { |
197 | WaitForSingleObject(ctx->ev_from_main, INFINITE); |
198 | if (ctx->done) { |
199 | SetEvent(ctx->ev_to_main); |
200 | break; |
201 | } |
202 | ctx->writeret = WriteFile(ctx->h, ctx->buffer, ctx->len, |
203 | &ctx->lenwritten, NULL); |
204 | SetEvent(ctx->ev_to_main); |
205 | if (!ctx->writeret) |
206 | break; |
207 | } |
208 | |
209 | return 0; |
210 | } |
211 | |
212 | static void handle_try_output(struct handle_output *ctx) |
213 | { |
214 | void *senddata; |
215 | int sendlen; |
216 | |
217 | if (!ctx->busy && bufchain_size(&ctx->queued_data)) { |
218 | bufchain_prefix(&ctx->queued_data, &senddata, &sendlen); |
219 | ctx->buffer = senddata; |
220 | ctx->len = sendlen; |
221 | SetEvent(ctx->ev_from_main); |
222 | ctx->busy = TRUE; |
223 | } |
224 | } |
225 | |
226 | /* ---------------------------------------------------------------------- |
227 | * Unified code handling both input and output threads. |
228 | */ |
229 | |
230 | struct handle { |
231 | int output; |
232 | union { |
233 | struct handle_generic g; |
234 | struct handle_input i; |
235 | struct handle_output o; |
236 | } u; |
237 | }; |
238 | |
239 | static tree234 *handles_by_evtomain; |
240 | |
241 | static int handle_cmp_evtomain(void *av, void *bv) |
242 | { |
243 | struct handle *a = (struct handle *)av; |
244 | struct handle *b = (struct handle *)bv; |
245 | |
246 | if ((unsigned)a->u.g.ev_to_main < (unsigned)b->u.g.ev_to_main) |
247 | return -1; |
248 | else if ((unsigned)a->u.g.ev_to_main > (unsigned)b->u.g.ev_to_main) |
249 | return +1; |
250 | else |
251 | return 0; |
252 | } |
253 | |
254 | static int handle_find_evtomain(void *av, void *bv) |
255 | { |
256 | HANDLE *a = (HANDLE *)av; |
257 | struct handle *b = (struct handle *)bv; |
258 | |
259 | if ((unsigned)*a < (unsigned)b->u.g.ev_to_main) |
260 | return -1; |
261 | else if ((unsigned)*a > (unsigned)b->u.g.ev_to_main) |
262 | return +1; |
263 | else |
264 | return 0; |
265 | } |
266 | |
0e03ceff |
267 | struct handle *handle_input_new(HANDLE handle, handle_inputfn_t gotdata, |
268 | void *privdata) |
34292b1d |
269 | { |
270 | struct handle *h = snew(struct handle); |
271 | |
272 | h->output = FALSE; |
273 | h->u.i.h = handle; |
274 | h->u.i.ev_to_main = CreateEvent(NULL, FALSE, FALSE, NULL); |
275 | h->u.i.ev_from_main = CreateEvent(NULL, FALSE, FALSE, NULL); |
276 | h->u.i.gotdata = gotdata; |
34292b1d |
277 | h->u.i.defunct = FALSE; |
278 | h->u.i.moribund = FALSE; |
279 | h->u.i.done = FALSE; |
0e03ceff |
280 | h->u.i.privdata = privdata; |
34292b1d |
281 | |
282 | if (!handles_by_evtomain) |
283 | handles_by_evtomain = newtree234(handle_cmp_evtomain); |
284 | add234(handles_by_evtomain, h); |
285 | |
286 | CreateThread(NULL, 0, handle_input_threadfunc, |
287 | &h->u.i, 0, NULL); |
2ceabd36 |
288 | h->u.i.busy = TRUE; |
34292b1d |
289 | |
290 | return h; |
291 | } |
292 | |
0e03ceff |
293 | struct handle *handle_output_new(HANDLE handle, handle_outputfn_t sentdata, |
294 | void *privdata) |
34292b1d |
295 | { |
296 | struct handle *h = snew(struct handle); |
297 | |
298 | h->output = TRUE; |
299 | h->u.o.h = handle; |
300 | h->u.o.ev_to_main = CreateEvent(NULL, FALSE, FALSE, NULL); |
301 | h->u.o.ev_from_main = CreateEvent(NULL, FALSE, FALSE, NULL); |
302 | h->u.o.busy = FALSE; |
303 | h->u.o.defunct = FALSE; |
304 | h->u.o.moribund = FALSE; |
305 | h->u.o.done = FALSE; |
0e03ceff |
306 | h->u.o.privdata = privdata; |
34292b1d |
307 | bufchain_init(&h->u.o.queued_data); |
308 | h->u.o.sentdata = sentdata; |
309 | |
310 | if (!handles_by_evtomain) |
311 | handles_by_evtomain = newtree234(handle_cmp_evtomain); |
312 | add234(handles_by_evtomain, h); |
313 | |
314 | CreateThread(NULL, 0, handle_output_threadfunc, |
315 | &h->u.i, 0, NULL); |
316 | |
317 | return h; |
318 | } |
319 | |
320 | int handle_write(struct handle *h, const void *data, int len) |
321 | { |
322 | assert(h->output); |
323 | bufchain_add(&h->u.o.queued_data, data, len); |
324 | handle_try_output(&h->u.o); |
325 | return bufchain_size(&h->u.o.queued_data); |
326 | } |
327 | |
328 | HANDLE *handle_get_events(int *nevents) |
329 | { |
330 | HANDLE *ret; |
331 | struct handle *h; |
332 | int i, n, size; |
333 | |
334 | /* |
335 | * Go through our tree counting the handle objects currently |
336 | * engaged in useful activity. |
337 | */ |
338 | ret = NULL; |
339 | n = size = 0; |
340 | if (handles_by_evtomain) { |
341 | for (i = 0; (h = index234(handles_by_evtomain, i)) != NULL; i++) { |
342 | if (h->u.g.busy) { |
343 | if (n >= size) { |
344 | size += 32; |
345 | ret = sresize(ret, size, HANDLE); |
346 | } |
347 | ret[n++] = h->u.g.ev_to_main; |
348 | } |
349 | } |
350 | } |
351 | |
352 | *nevents = n; |
353 | return ret; |
354 | } |
355 | |
356 | static void handle_destroy(struct handle *h) |
357 | { |
358 | if (h->output) |
359 | bufchain_clear(&h->u.o.queued_data); |
360 | CloseHandle(h->u.g.ev_from_main); |
361 | CloseHandle(h->u.g.ev_to_main); |
362 | del234(handles_by_evtomain, h); |
363 | sfree(h); |
364 | } |
365 | |
366 | void handle_free(struct handle *h) |
367 | { |
368 | /* |
369 | * If the handle is currently busy, we cannot immediately free |
370 | * it. Instead we must wait until it's finished its current |
371 | * operation, because otherwise the subthread will write to |
372 | * invalid memory after we free its context from under it. |
373 | */ |
374 | assert(h && !h->u.g.moribund); |
375 | if (h->u.g.busy) { |
376 | /* |
377 | * Just set the moribund flag, which will be noticed next |
378 | * time an operation completes. |
379 | */ |
380 | h->u.g.moribund = TRUE; |
381 | } else if (h->u.g.defunct) { |
382 | /* |
383 | * There isn't even a subthread; we can go straight to |
384 | * handle_destroy. |
385 | */ |
386 | handle_destroy(h); |
387 | } else { |
388 | /* |
389 | * The subthread is alive but not busy, so we now signal it |
390 | * to die. Set the moribund flag to indicate that it will |
391 | * want destroying after that. |
392 | */ |
393 | h->u.g.moribund = TRUE; |
394 | h->u.g.done = TRUE; |
c969e831 |
395 | h->u.g.busy = TRUE; |
34292b1d |
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; |
c969e831 |
429 | h->u.g.busy = TRUE; |
34292b1d |
430 | SetEvent(h->u.g.ev_from_main); |
431 | } |
432 | return; |
433 | } |
434 | |
435 | if (!h->output) { |
436 | int backlog; |
437 | |
438 | h->u.i.busy = FALSE; |
439 | |
440 | /* |
441 | * A signal on an input handle means data has arrived. |
442 | */ |
443 | if (h->u.i.len == 0) { |
444 | /* |
445 | * EOF, or (nearly equivalently) read error. |
446 | */ |
447 | h->u.i.gotdata(h, NULL, (h->u.i.readret ? 0 : -1)); |
448 | h->u.i.defunct = TRUE; |
449 | } else { |
450 | backlog = h->u.i.gotdata(h, h->u.i.buffer, h->u.i.len); |
451 | handle_throttle(&h->u.i, backlog); |
452 | } |
453 | } else { |
454 | h->u.o.busy = FALSE; |
455 | |
456 | /* |
457 | * A signal on an output handle means we have completed a |
458 | * write. Call the callback to indicate that the output |
459 | * buffer size has decreased, or to indicate an error. |
460 | */ |
461 | if (!h->u.o.writeret) { |
462 | /* |
463 | * Write error. Send a negative value to the callback, |
464 | * and mark the thread as defunct (because the output |
465 | * thread is terminating by now). |
466 | */ |
467 | h->u.o.sentdata(h, -1); |
468 | h->u.o.defunct = TRUE; |
469 | } else { |
470 | bufchain_consume(&h->u.o.queued_data, h->u.o.lenwritten); |
471 | h->u.o.sentdata(h, bufchain_size(&h->u.o.queued_data)); |
472 | handle_try_output(&h->u.o); |
473 | } |
474 | } |
475 | } |
476 | |
477 | void handle_unthrottle(struct handle *h, int backlog) |
478 | { |
479 | assert(!h->output); |
480 | handle_throttle(&h->u.i, backlog); |
481 | } |
482 | |
483 | int handle_backlog(struct handle *h) |
484 | { |
485 | assert(h->output); |
486 | return bufchain_size(&h->u.o.queued_data); |
487 | } |
0e03ceff |
488 | |
489 | void *handle_get_privdata(struct handle *h) |
490 | { |
491 | return h->u.g.privdata; |
492 | } |