]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/winplink.c
Only run one toplevel callback per event loop iteration.
[PuTTY.git] / windows / winplink.c
1 /*
2  * PLink - a Windows command-line (stdin/stdout) variant of PuTTY.
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <assert.h>
8 #include <stdarg.h>
9
10 #define PUTTY_DO_GLOBALS               /* actually _define_ globals */
11 #include "putty.h"
12 #include "storage.h"
13 #include "tree234.h"
14
15 #define WM_AGENT_CALLBACK (WM_APP + 4)
16
17 struct agent_callback {
18     void (*callback)(void *, void *, int);
19     void *callback_ctx;
20     void *data;
21     int len;
22 };
23
24 void fatalbox(char *p, ...)
25 {
26     va_list ap;
27     fprintf(stderr, "FATAL ERROR: ");
28     va_start(ap, p);
29     vfprintf(stderr, p, ap);
30     va_end(ap);
31     fputc('\n', stderr);
32     if (logctx) {
33         log_free(logctx);
34         logctx = NULL;
35     }
36     cleanup_exit(1);
37 }
38 void modalfatalbox(char *p, ...)
39 {
40     va_list ap;
41     fprintf(stderr, "FATAL ERROR: ");
42     va_start(ap, p);
43     vfprintf(stderr, p, ap);
44     va_end(ap);
45     fputc('\n', stderr);
46     if (logctx) {
47         log_free(logctx);
48         logctx = NULL;
49     }
50     cleanup_exit(1);
51 }
52 void nonfatal(char *p, ...)
53 {
54     va_list ap;
55     fprintf(stderr, "ERROR: ");
56     va_start(ap, p);
57     vfprintf(stderr, p, ap);
58     va_end(ap);
59     fputc('\n', stderr);
60     if (logctx) {
61         log_free(logctx);
62         logctx = NULL;
63     }
64 }
65 void connection_fatal(void *frontend, char *p, ...)
66 {
67     va_list ap;
68     fprintf(stderr, "FATAL ERROR: ");
69     va_start(ap, p);
70     vfprintf(stderr, p, ap);
71     va_end(ap);
72     fputc('\n', stderr);
73     if (logctx) {
74         log_free(logctx);
75         logctx = NULL;
76     }
77     cleanup_exit(1);
78 }
79 void cmdline_error(char *p, ...)
80 {
81     va_list ap;
82     fprintf(stderr, "plink: ");
83     va_start(ap, p);
84     vfprintf(stderr, p, ap);
85     va_end(ap);
86     fputc('\n', stderr);
87     exit(1);
88 }
89
90 HANDLE inhandle, outhandle, errhandle;
91 struct handle *stdin_handle, *stdout_handle, *stderr_handle;
92 DWORD orig_console_mode;
93 int connopen;
94
95 WSAEVENT netevent;
96
97 static Backend *back;
98 static void *backhandle;
99 static Conf *conf;
100
101 int term_ldisc(Terminal *term, int mode)
102 {
103     return FALSE;
104 }
105 void ldisc_update(void *frontend, int echo, int edit)
106 {
107     /* Update stdin read mode to reflect changes in line discipline. */
108     DWORD mode;
109
110     mode = ENABLE_PROCESSED_INPUT;
111     if (echo)
112         mode = mode | ENABLE_ECHO_INPUT;
113     else
114         mode = mode & ~ENABLE_ECHO_INPUT;
115     if (edit)
116         mode = mode | ENABLE_LINE_INPUT;
117     else
118         mode = mode & ~ENABLE_LINE_INPUT;
119     SetConsoleMode(inhandle, mode);
120 }
121
122 char *get_ttymode(void *frontend, const char *mode) { return NULL; }
123
124 int from_backend(void *frontend_handle, int is_stderr,
125                  const char *data, int len)
126 {
127     if (is_stderr) {
128         handle_write(stderr_handle, data, len);
129     } else {
130         handle_write(stdout_handle, data, len);
131     }
132
133     return handle_backlog(stdout_handle) + handle_backlog(stderr_handle);
134 }
135
136 int from_backend_untrusted(void *frontend_handle, const char *data, int len)
137 {
138     /*
139      * No "untrusted" output should get here (the way the code is
140      * currently, it's all diverted by FLAG_STDERR).
141      */
142     assert(!"Unexpected call to from_backend_untrusted()");
143     return 0; /* not reached */
144 }
145
146 int from_backend_eof(void *frontend_handle)
147 {
148     handle_write_eof(stdout_handle);
149     return FALSE;   /* do not respond to incoming EOF with outgoing */
150 }
151
152 int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
153 {
154     int ret;
155     ret = cmdline_get_passwd_input(p, in, inlen);
156     if (ret == -1)
157         ret = console_get_userpass_input(p, in, inlen);
158     return ret;
159 }
160
161 static DWORD main_thread_id;
162
163 void agent_schedule_callback(void (*callback)(void *, void *, int),
164                              void *callback_ctx, void *data, int len)
165 {
166     struct agent_callback *c = snew(struct agent_callback);
167     c->callback = callback;
168     c->callback_ctx = callback_ctx;
169     c->data = data;
170     c->len = len;
171     PostThreadMessage(main_thread_id, WM_AGENT_CALLBACK, 0, (LPARAM)c);
172 }
173
174 /*
175  *  Short description of parameters.
176  */
177 static void usage(void)
178 {
179     printf("PuTTY Link: command-line connection utility\n");
180     printf("%s\n", ver);
181     printf("Usage: plink [options] [user@]host [command]\n");
182     printf("       (\"host\" can also be a PuTTY saved session name)\n");
183     printf("Options:\n");
184     printf("  -V        print version information and exit\n");
185     printf("  -pgpfp    print PGP key fingerprints and exit\n");
186     printf("  -v        show verbose messages\n");
187     printf("  -load sessname  Load settings from saved session\n");
188     printf("  -ssh -telnet -rlogin -raw -serial\n");
189     printf("            force use of a particular protocol\n");
190     printf("  -P port   connect to specified port\n");
191     printf("  -l user   connect with specified username\n");
192     printf("  -batch    disable all interactive prompts\n");
193     printf("The following options only apply to SSH connections:\n");
194     printf("  -pw passw login with specified password\n");
195     printf("  -D [listen-IP:]listen-port\n");
196     printf("            Dynamic SOCKS-based port forwarding\n");
197     printf("  -L [listen-IP:]listen-port:host:port\n");
198     printf("            Forward local port to remote address\n");
199     printf("  -R [listen-IP:]listen-port:host:port\n");
200     printf("            Forward remote port to local address\n");
201     printf("  -X -x     enable / disable X11 forwarding\n");
202     printf("  -A -a     enable / disable agent forwarding\n");
203     printf("  -t -T     enable / disable pty allocation\n");
204     printf("  -1 -2     force use of particular protocol version\n");
205     printf("  -4 -6     force use of IPv4 or IPv6\n");
206     printf("  -C        enable compression\n");
207     printf("  -i key    private key file for authentication\n");
208     printf("  -noagent  disable use of Pageant\n");
209     printf("  -agent    enable use of Pageant\n");
210     printf("  -m file   read remote command(s) from file\n");
211     printf("  -s        remote command is an SSH subsystem (SSH-2 only)\n");
212     printf("  -N        don't start a shell/command (SSH-2 only)\n");
213     printf("  -nc host:port\n");
214     printf("            open tunnel in place of session (SSH-2 only)\n");
215     printf("  -sercfg configuration-string (e.g. 19200,8,n,1,X)\n");
216     printf("            Specify the serial configuration (serial only)\n");
217     exit(1);
218 }
219
220 static void version(void)
221 {
222     printf("plink: %s\n", ver);
223     exit(1);
224 }
225
226 char *do_select(SOCKET skt, int startup)
227 {
228     int events;
229     if (startup) {
230         events = (FD_CONNECT | FD_READ | FD_WRITE |
231                   FD_OOB | FD_CLOSE | FD_ACCEPT);
232     } else {
233         events = 0;
234     }
235     if (p_WSAEventSelect(skt, netevent, events) == SOCKET_ERROR) {
236         switch (p_WSAGetLastError()) {
237           case WSAENETDOWN:
238             return "Network is down";
239           default:
240             return "WSAEventSelect(): unknown error";
241         }
242     }
243     return NULL;
244 }
245
246 int stdin_gotdata(struct handle *h, void *data, int len)
247 {
248     if (len < 0) {
249         /*
250          * Special case: report read error.
251          */
252         char buf[4096];
253         FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, -len, 0,
254                       buf, lenof(buf), NULL);
255         buf[lenof(buf)-1] = '\0';
256         if (buf[strlen(buf)-1] == '\n')
257             buf[strlen(buf)-1] = '\0';
258         fprintf(stderr, "Unable to read from standard input: %s\n", buf);
259         cleanup_exit(0);
260     }
261     noise_ultralight(len);
262     if (connopen && back->connected(backhandle)) {
263         if (len > 0) {
264             return back->send(backhandle, data, len);
265         } else {
266             back->special(backhandle, TS_EOF);
267             return 0;
268         }
269     } else
270         return 0;
271 }
272
273 void stdouterr_sent(struct handle *h, int new_backlog)
274 {
275     if (new_backlog < 0) {
276         /*
277          * Special case: report write error.
278          */
279         char buf[4096];
280         FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, -new_backlog, 0,
281                       buf, lenof(buf), NULL);
282         buf[lenof(buf)-1] = '\0';
283         if (buf[strlen(buf)-1] == '\n')
284             buf[strlen(buf)-1] = '\0';
285         fprintf(stderr, "Unable to write to standard %s: %s\n",
286                 (h == stdout_handle ? "output" : "error"), buf);
287         cleanup_exit(0);
288     }
289     if (connopen && back->connected(backhandle)) {
290         back->unthrottle(backhandle, (handle_backlog(stdout_handle) +
291                                       handle_backlog(stderr_handle)));
292     }
293 }
294
295 int main(int argc, char **argv)
296 {
297     int sending;
298     int portnumber = -1;
299     SOCKET *sklist;
300     int skcount, sksize;
301     int exitcode;
302     int errors;
303     int got_host = FALSE;
304     int use_subsystem = 0;
305     unsigned long now, next, then;
306
307     sklist = NULL;
308     skcount = sksize = 0;
309     /*
310      * Initialise port and protocol to sensible defaults. (These
311      * will be overridden by more or less anything.)
312      */
313     default_protocol = PROT_SSH;
314     default_port = 22;
315
316     flags = FLAG_STDERR;
317     /*
318      * Process the command line.
319      */
320     conf = conf_new();
321     do_defaults(NULL, conf);
322     loaded_session = FALSE;
323     default_protocol = conf_get_int(conf, CONF_protocol);
324     default_port = conf_get_int(conf, CONF_port);
325     errors = 0;
326     {
327         /*
328          * Override the default protocol if PLINK_PROTOCOL is set.
329          */
330         char *p = getenv("PLINK_PROTOCOL");
331         if (p) {
332             const Backend *b = backend_from_name(p);
333             if (b) {
334                 default_protocol = b->protocol;
335                 default_port = b->default_port;
336                 conf_set_int(conf, CONF_protocol, default_protocol);
337                 conf_set_int(conf, CONF_port, default_port);
338             }
339         }
340     }
341     while (--argc) {
342         char *p = *++argv;
343         if (*p == '-') {
344             int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
345                                             1, conf);
346             if (ret == -2) {
347                 fprintf(stderr,
348                         "plink: option \"%s\" requires an argument\n", p);
349                 errors = 1;
350             } else if (ret == 2) {
351                 --argc, ++argv;
352             } else if (ret == 1) {
353                 continue;
354             } else if (!strcmp(p, "-batch")) {
355                 console_batch_mode = 1;
356             } else if (!strcmp(p, "-s")) {
357                 /* Save status to write to conf later. */
358                 use_subsystem = 1;
359             } else if (!strcmp(p, "-V") || !strcmp(p, "--version")) {
360                 version();
361             } else if (!strcmp(p, "--help")) {
362                 usage();
363             } else if (!strcmp(p, "-pgpfp")) {
364                 pgp_fingerprints();
365                 exit(1);
366             } else {
367                 fprintf(stderr, "plink: unknown option \"%s\"\n", p);
368                 errors = 1;
369             }
370         } else if (*p) {
371             if (!conf_launchable(conf) || !(got_host || loaded_session)) {
372                 char *q = p;
373                 /*
374                  * If the hostname starts with "telnet:", set the
375                  * protocol to Telnet and process the string as a
376                  * Telnet URL.
377                  */
378                 if (!strncmp(q, "telnet:", 7)) {
379                     char c;
380
381                     q += 7;
382                     if (q[0] == '/' && q[1] == '/')
383                         q += 2;
384                     conf_set_int(conf, CONF_protocol, PROT_TELNET);
385                     p = q;
386                     while (*p && *p != ':' && *p != '/')
387                         p++;
388                     c = *p;
389                     if (*p)
390                         *p++ = '\0';
391                     if (c == ':')
392                         conf_set_int(conf, CONF_port, atoi(p));
393                     else
394                         conf_set_int(conf, CONF_port, -1);
395                     conf_set_str(conf, CONF_host, q);
396                     got_host = TRUE;
397                 } else {
398                     char *r, *user, *host;
399                     /*
400                      * Before we process the [user@]host string, we
401                      * first check for the presence of a protocol
402                      * prefix (a protocol name followed by ",").
403                      */
404                     r = strchr(p, ',');
405                     if (r) {
406                         const Backend *b;
407                         *r = '\0';
408                         b = backend_from_name(p);
409                         if (b) {
410                             default_protocol = b->protocol;
411                             conf_set_int(conf, CONF_protocol,
412                                          default_protocol);
413                             portnumber = b->default_port;
414                         }
415                         p = r + 1;
416                     }
417
418                     /*
419                      * A nonzero length string followed by an @ is treated
420                      * as a username. (We discount an _initial_ @.) The
421                      * rest of the string (or the whole string if no @)
422                      * is treated as a session name and/or hostname.
423                      */
424                     r = strrchr(p, '@');
425                     if (r == p)
426                         p++, r = NULL; /* discount initial @ */
427                     if (r) {
428                         *r++ = '\0';
429                         user = p, host = r;
430                     } else {
431                         user = NULL, host = p;
432                     }
433
434                     /*
435                      * Now attempt to load a saved session with the
436                      * same name as the hostname.
437                      */
438                     {
439                         Conf *conf2 = conf_new();
440                         do_defaults(host, conf2);
441                         if (loaded_session || !conf_launchable(conf2)) {
442                             /* No settings for this host; use defaults */
443                             /* (or session was already loaded with -load) */
444                             conf_set_str(conf, CONF_host, host);
445                             conf_set_int(conf, CONF_port, default_port);
446                             got_host = TRUE;
447                         } else {
448                             conf_copy_into(conf, conf2);
449                             loaded_session = TRUE;
450                         }
451                         conf_free(conf2);
452                     }
453
454                     if (user) {
455                         /* Patch in specified username. */
456                         conf_set_str(conf, CONF_username, user);
457                     }
458
459                 }
460             } else {
461                 char *command;
462                 int cmdlen, cmdsize;
463                 cmdlen = cmdsize = 0;
464                 command = NULL;
465
466                 while (argc) {
467                     while (*p) {
468                         if (cmdlen >= cmdsize) {
469                             cmdsize = cmdlen + 512;
470                             command = sresize(command, cmdsize, char);
471                         }
472                         command[cmdlen++]=*p++;
473                     }
474                     if (cmdlen >= cmdsize) {
475                         cmdsize = cmdlen + 512;
476                         command = sresize(command, cmdsize, char);
477                     }
478                     command[cmdlen++]=' '; /* always add trailing space */
479                     if (--argc) p = *++argv;
480                 }
481                 if (cmdlen) command[--cmdlen]='\0';
482                                        /* change trailing blank to NUL */
483                 conf_set_str(conf, CONF_remote_cmd, command);
484                 conf_set_str(conf, CONF_remote_cmd2, "");
485                 conf_set_int(conf, CONF_nopty, TRUE);  /* command => no tty */
486
487                 break;                 /* done with cmdline */
488             }
489         }
490     }
491
492     if (errors)
493         return 1;
494
495     if (!conf_launchable(conf) || !(got_host || loaded_session)) {
496         usage();
497     }
498
499     /*
500      * Muck about with the hostname in various ways.
501      */
502     {
503         char *hostbuf = dupstr(conf_get_str(conf, CONF_host));
504         char *host = hostbuf;
505         char *p, *q;
506
507         /*
508          * Trim leading whitespace.
509          */
510         host += strspn(host, " \t");
511
512         /*
513          * See if host is of the form user@host, and separate out
514          * the username if so.
515          */
516         if (host[0] != '\0') {
517             char *atsign = strrchr(host, '@');
518             if (atsign) {
519                 *atsign = '\0';
520                 conf_set_str(conf, CONF_username, host);
521                 host = atsign + 1;
522             }
523         }
524
525         /*
526          * Trim off a colon suffix if it's there.
527          */
528         host[strcspn(host, ":")] = '\0';
529
530         /*
531          * Remove any remaining whitespace.
532          */
533         p = hostbuf;
534         q = host;
535         while (*q) {
536             if (*q != ' ' && *q != '\t')
537                 *p++ = *q;
538             q++;
539         }
540         *p = '\0';
541
542         conf_set_str(conf, CONF_host, hostbuf);
543         sfree(hostbuf);
544     }
545
546     /*
547      * Perform command-line overrides on session configuration.
548      */
549     cmdline_run_saved(conf);
550
551     /*
552      * Apply subsystem status.
553      */
554     if (use_subsystem)
555         conf_set_int(conf, CONF_ssh_subsys, TRUE);
556
557     if (!*conf_get_str(conf, CONF_remote_cmd) &&
558         !*conf_get_str(conf, CONF_remote_cmd2) &&
559         !*conf_get_str(conf, CONF_ssh_nc_host))
560         flags |= FLAG_INTERACTIVE;
561
562     /*
563      * Select protocol. This is farmed out into a table in a
564      * separate file to enable an ssh-free variant.
565      */
566     back = backend_from_proto(conf_get_int(conf, CONF_protocol));
567     if (back == NULL) {
568         fprintf(stderr,
569                 "Internal fault: Unsupported protocol found\n");
570         return 1;
571     }
572
573     /*
574      * Select port.
575      */
576     if (portnumber != -1)
577         conf_set_int(conf, CONF_port, portnumber);
578
579     sk_init();
580     if (p_WSAEventSelect == NULL) {
581         fprintf(stderr, "Plink requires WinSock 2\n");
582         return 1;
583     }
584
585     logctx = log_init(NULL, conf);
586     console_provide_logctx(logctx);
587
588     /*
589      * Start up the connection.
590      */
591     netevent = CreateEvent(NULL, FALSE, FALSE, NULL);
592     {
593         const char *error;
594         char *realhost;
595         /* nodelay is only useful if stdin is a character device (console) */
596         int nodelay = conf_get_int(conf, CONF_tcp_nodelay) &&
597             (GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_CHAR);
598
599         error = back->init(NULL, &backhandle, conf,
600                            conf_get_str(conf, CONF_host),
601                            conf_get_int(conf, CONF_port),
602                            &realhost, nodelay,
603                            conf_get_int(conf, CONF_tcp_keepalives));
604         if (error) {
605             fprintf(stderr, "Unable to open connection:\n%s", error);
606             return 1;
607         }
608         back->provide_logctx(backhandle, logctx);
609         sfree(realhost);
610     }
611     connopen = 1;
612
613     inhandle = GetStdHandle(STD_INPUT_HANDLE);
614     outhandle = GetStdHandle(STD_OUTPUT_HANDLE);
615     errhandle = GetStdHandle(STD_ERROR_HANDLE);
616
617     /*
618      * Turn off ECHO and LINE input modes. We don't care if this
619      * call fails, because we know we aren't necessarily running in
620      * a console.
621      */
622     GetConsoleMode(inhandle, &orig_console_mode);
623     SetConsoleMode(inhandle, ENABLE_PROCESSED_INPUT);
624
625     /*
626      * Pass the output handles to the handle-handling subsystem.
627      * (The input one we leave until we're through the
628      * authentication process.)
629      */
630     stdout_handle = handle_output_new(outhandle, stdouterr_sent, NULL, 0);
631     stderr_handle = handle_output_new(errhandle, stdouterr_sent, NULL, 0);
632
633     main_thread_id = GetCurrentThreadId();
634
635     sending = FALSE;
636
637     now = GETTICKCOUNT();
638
639     while (1) {
640         int nhandles;
641         HANDLE *handles;        
642         int n;
643         DWORD ticks;
644
645         if (!sending && back->sendok(backhandle)) {
646             stdin_handle = handle_input_new(inhandle, stdin_gotdata, NULL,
647                                             0);
648             sending = TRUE;
649         }
650
651         if (toplevel_callback_pending()) {
652             ticks = 0;
653         } else if (run_timers(now, &next)) {
654             then = now;
655             now = GETTICKCOUNT();
656             if (now - then > next - then)
657                 ticks = 0;
658             else
659                 ticks = next - now;
660         } else {
661             ticks = INFINITE;
662         }
663
664         handles = handle_get_events(&nhandles);
665         handles = sresize(handles, nhandles+1, HANDLE);
666         handles[nhandles] = netevent;
667         n = MsgWaitForMultipleObjects(nhandles+1, handles, FALSE, ticks,
668                                       QS_POSTMESSAGE);
669         if ((unsigned)(n - WAIT_OBJECT_0) < (unsigned)nhandles) {
670             handle_got_event(handles[n - WAIT_OBJECT_0]);
671         } else if (n == WAIT_OBJECT_0 + nhandles) {
672             WSANETWORKEVENTS things;
673             SOCKET socket;
674             extern SOCKET first_socket(int *), next_socket(int *);
675             extern int select_result(WPARAM, LPARAM);
676             int i, socketstate;
677
678             /*
679              * We must not call select_result() for any socket
680              * until we have finished enumerating within the tree.
681              * This is because select_result() may close the socket
682              * and modify the tree.
683              */
684             /* Count the active sockets. */
685             i = 0;
686             for (socket = first_socket(&socketstate);
687                  socket != INVALID_SOCKET;
688                  socket = next_socket(&socketstate)) i++;
689
690             /* Expand the buffer if necessary. */
691             if (i > sksize) {
692                 sksize = i + 16;
693                 sklist = sresize(sklist, sksize, SOCKET);
694             }
695
696             /* Retrieve the sockets into sklist. */
697             skcount = 0;
698             for (socket = first_socket(&socketstate);
699                  socket != INVALID_SOCKET;
700                  socket = next_socket(&socketstate)) {
701                 sklist[skcount++] = socket;
702             }
703
704             /* Now we're done enumerating; go through the list. */
705             for (i = 0; i < skcount; i++) {
706                 WPARAM wp;
707                 socket = sklist[i];
708                 wp = (WPARAM) socket;
709                 if (!p_WSAEnumNetworkEvents(socket, NULL, &things)) {
710                     static const struct { int bit, mask; } eventtypes[] = {
711                         {FD_CONNECT_BIT, FD_CONNECT},
712                         {FD_READ_BIT, FD_READ},
713                         {FD_CLOSE_BIT, FD_CLOSE},
714                         {FD_OOB_BIT, FD_OOB},
715                         {FD_WRITE_BIT, FD_WRITE},
716                         {FD_ACCEPT_BIT, FD_ACCEPT},
717                     };
718                     int e;
719
720                     noise_ultralight(socket);
721                     noise_ultralight(things.lNetworkEvents);
722
723                     for (e = 0; e < lenof(eventtypes); e++)
724                         if (things.lNetworkEvents & eventtypes[e].mask) {
725                             LPARAM lp;
726                             int err = things.iErrorCode[eventtypes[e].bit];
727                             lp = WSAMAKESELECTREPLY(eventtypes[e].mask, err);
728                             connopen &= select_result(wp, lp);
729                         }
730                 }
731             }
732         } else if (n == WAIT_OBJECT_0 + nhandles + 1) {
733             MSG msg;
734             while (PeekMessage(&msg, INVALID_HANDLE_VALUE,
735                                WM_AGENT_CALLBACK, WM_AGENT_CALLBACK,
736                                PM_REMOVE)) {
737                 struct agent_callback *c = (struct agent_callback *)msg.lParam;
738                 c->callback(c->callback_ctx, c->data, c->len);
739                 sfree(c);
740             }
741         }
742
743         run_toplevel_callbacks();
744
745         if (n == WAIT_TIMEOUT) {
746             now = next;
747         } else {
748             now = GETTICKCOUNT();
749         }
750
751         sfree(handles);
752
753         if (sending)
754             handle_unthrottle(stdin_handle, back->sendbuffer(backhandle));
755
756         if ((!connopen || !back->connected(backhandle)) &&
757             handle_backlog(stdout_handle) + handle_backlog(stderr_handle) == 0)
758             break;                     /* we closed the connection */
759     }
760     exitcode = back->exitcode(backhandle);
761     if (exitcode < 0) {
762         fprintf(stderr, "Remote process exit code unavailable\n");
763         exitcode = 1;                  /* this is an error condition */
764     }
765     cleanup_exit(exitcode);
766     return 0;                          /* placate compiler warning */
767 }