summaryrefslogtreecommitdiff
path: root/serverloop.c
diff options
context:
space:
mode:
Diffstat (limited to 'serverloop.c')
-rw-r--r--serverloop.c114
1 files changed, 68 insertions, 46 deletions
diff --git a/serverloop.c b/serverloop.c
index fc959baef..683598ef8 100644
--- a/serverloop.c
+++ b/serverloop.c
@@ -33,8 +33,10 @@ static int connection_out; /* Connection to client (output). */
33static unsigned int buffer_high;/* "Soft" max buffer size. */ 33static unsigned int buffer_high;/* "Soft" max buffer size. */
34static int max_fd; /* Max file descriptor number for select(). */ 34static int max_fd; /* Max file descriptor number for select(). */
35 35
36/* This SIGCHLD kludge is used to detect when the child exits. The server 36/*
37 will exit after that, as soon as forwarded connections have terminated. */ 37 * This SIGCHLD kludge is used to detect when the child exits. The server
38 * will exit after that, as soon as forwarded connections have terminated.
39 */
38 40
39static int child_pid; /* Pid of the child. */ 41static int child_pid; /* Pid of the child. */
40static volatile int child_terminated; /* The child has terminated. */ 42static volatile int child_terminated; /* The child has terminated. */
@@ -87,9 +89,11 @@ process_buffered_input_packets()
87 break; 89 break;
88 90
89 case SSH_CMSG_EOF: 91 case SSH_CMSG_EOF:
90 /* Eof from the client. The stdin descriptor to 92 /*
91 the program will be closed when all buffered 93 * Eof from the client. The stdin descriptor to the
92 data has drained. */ 94 * program will be closed when all buffered data has
95 * drained.
96 */
93 debug("EOF received for stdin."); 97 debug("EOF received for stdin.");
94 packet_integrity_check(payload_len, 0, type); 98 packet_integrity_check(payload_len, 0, type);
95 stdin_eof = 1; 99 stdin_eof = 1;
@@ -140,13 +144,15 @@ process_buffered_input_packets()
140 break; 144 break;
141 145
142 default: 146 default:
143 /* In this phase, any unexpected messages cause a 147 /*
144 protocol error. This is to ease debugging; 148 * In this phase, any unexpected messages cause a
145 also, since no confirmations are sent messages, 149 * protocol error. This is to ease debugging; also,
146 unprocessed unknown messages could cause 150 * since no confirmations are sent messages,
147 strange problems. Any compatible protocol 151 * unprocessed unknown messages could cause strange
148 extensions must be negotiated before entering 152 * problems. Any compatible protocol extensions must
149 the interactive session. */ 153 * be negotiated before entering the interactive
154 * session.
155 */
150 packet_disconnect("Protocol error during session: type %d", 156 packet_disconnect("Protocol error during session: type %d",
151 type); 157 type);
152 } 158 }
@@ -230,14 +236,18 @@ retry_select:
230 /* Initialize select() masks. */ 236 /* Initialize select() masks. */
231 FD_ZERO(readset); 237 FD_ZERO(readset);
232 238
233 /* Read packets from the client unless we have too much buffered 239 /*
234 stdin or channel data. */ 240 * Read packets from the client unless we have too much buffered
241 * stdin or channel data.
242 */
235 if (buffer_len(&stdin_buffer) < 4096 && 243 if (buffer_len(&stdin_buffer) < 4096 &&
236 channel_not_very_much_buffered_data()) 244 channel_not_very_much_buffered_data())
237 FD_SET(connection_in, readset); 245 FD_SET(connection_in, readset);
238 246
239 /* If there is not too much data already buffered going to the 247 /*
240 client, try to get some more data from the program. */ 248 * If there is not too much data already buffered going to the
249 * client, try to get some more data from the program.
250 */
241 if (packet_not_very_much_data_to_write()) { 251 if (packet_not_very_much_data_to_write()) {
242 if (!fdout_eof) 252 if (!fdout_eof)
243 FD_SET(fdout, readset); 253 FD_SET(fdout, readset);
@@ -249,8 +259,10 @@ retry_select:
249 /* Set masks for channel descriptors. */ 259 /* Set masks for channel descriptors. */
250 channel_prepare_select(readset, writeset); 260 channel_prepare_select(readset, writeset);
251 261
252 /* If we have buffered packet data going to the client, mark that 262 /*
253 descriptor. */ 263 * If we have buffered packet data going to the client, mark that
264 * descriptor.
265 */
254 if (packet_have_data_to_write()) 266 if (packet_have_data_to_write())
255 FD_SET(connection_out, writeset); 267 FD_SET(connection_out, writeset);
256 268
@@ -263,8 +275,10 @@ retry_select:
263 if (channel_max_fd() > max_fd) 275 if (channel_max_fd() > max_fd)
264 max_fd = channel_max_fd(); 276 max_fd = channel_max_fd();
265 277
266 /* If child has terminated and there is enough buffer space to 278 /*
267 read from it, then read as much as is available and exit. */ 279 * If child has terminated and there is enough buffer space to read
280 * from it, then read as much as is available and exit.
281 */
268 if (child_terminated && packet_not_very_much_data_to_write()) 282 if (child_terminated && packet_not_very_much_data_to_write())
269 if (max_time_milliseconds == 0) 283 if (max_time_milliseconds == 0)
270 max_time_milliseconds = 100; 284 max_time_milliseconds = 100;
@@ -305,9 +319,10 @@ process_input(fd_set * readset)
305 verbose("Connection closed by remote host."); 319 verbose("Connection closed by remote host.");
306 fatal_cleanup(); 320 fatal_cleanup();
307 } 321 }
308 /* There is a kernel bug on Solaris that causes select to 322 /*
309 sometimes wake up even though there is no data 323 * There is a kernel bug on Solaris that causes select to
310 available. */ 324 * sometimes wake up even though there is no data available.
325 */
311 if (len < 0 && errno == EAGAIN) 326 if (len < 0 && errno == EAGAIN)
312 len = 0; 327 len = 0;
313 328
@@ -456,11 +471,12 @@ server_loop(int pid, int fdin_arg, int fdout_arg, int fderr_arg)
456 buffer_init(&stdout_buffer); 471 buffer_init(&stdout_buffer);
457 buffer_init(&stderr_buffer); 472 buffer_init(&stderr_buffer);
458 473
459 /* If we have no separate fderr (which is the case when we have a 474 /*
460 pty - there we cannot make difference between data sent to 475 * If we have no separate fderr (which is the case when we have a pty
461 stdout and stderr), indicate that we have seen an EOF from 476 * - there we cannot make difference between data sent to stdout and
462 stderr. This way we don\'t need to check the descriptor 477 * stderr), indicate that we have seen an EOF from stderr. This way
463 everywhere. */ 478 * we don\'t need to check the descriptor everywhere.
479 */
464 if (fderr == -1) 480 if (fderr == -1)
465 fderr_eof = 1; 481 fderr_eof = 1;
466 482
@@ -471,8 +487,10 @@ server_loop(int pid, int fdin_arg, int fdout_arg, int fderr_arg)
471 /* Process buffered packets from the client. */ 487 /* Process buffered packets from the client. */
472 process_buffered_input_packets(); 488 process_buffered_input_packets();
473 489
474 /* If we have received eof, and there is no more pending 490 /*
475 input data, cause a real eof by closing fdin. */ 491 * If we have received eof, and there is no more pending
492 * input data, cause a real eof by closing fdin.
493 */
476 if (stdin_eof && fdin != -1 && buffer_len(&stdin_buffer) == 0) { 494 if (stdin_eof && fdin != -1 && buffer_len(&stdin_buffer) == 0) {
477#ifdef USE_PIPES 495#ifdef USE_PIPES
478 close(fdin); 496 close(fdin);
@@ -484,16 +502,16 @@ server_loop(int pid, int fdin_arg, int fdout_arg, int fderr_arg)
484#endif 502#endif
485 fdin = -1; 503 fdin = -1;
486 } 504 }
487 /* Make packets from buffered stderr data to send to the 505 /* Make packets from buffered stderr data to send to the client. */
488 client. */
489 make_packets_from_stderr_data(); 506 make_packets_from_stderr_data();
490 507
491 /* Make packets from buffered stdout data to send to the 508 /*
492 client. If there is very little to send, this arranges 509 * Make packets from buffered stdout data to send to the
493 to not send them now, but to wait a short while to see 510 * client. If there is very little to send, this arranges to
494 if we are getting more data. This is necessary, as some 511 * not send them now, but to wait a short while to see if we
495 systems wake up readers from a pty after each separate 512 * are getting more data. This is necessary, as some systems
496 character. */ 513 * wake up readers from a pty after each separate character.
514 */
497 max_time_milliseconds = 0; 515 max_time_milliseconds = 0;
498 stdout_buffer_bytes = buffer_len(&stdout_buffer); 516 stdout_buffer_bytes = buffer_len(&stdout_buffer);
499 if (stdout_buffer_bytes != 0 && stdout_buffer_bytes < 256 && 517 if (stdout_buffer_bytes != 0 && stdout_buffer_bytes < 256 &&
@@ -510,9 +528,11 @@ server_loop(int pid, int fdin_arg, int fdout_arg, int fderr_arg)
510 if (packet_not_very_much_data_to_write()) 528 if (packet_not_very_much_data_to_write())
511 channel_output_poll(); 529 channel_output_poll();
512 530
513 /* Bail out of the loop if the program has closed its 531 /*
514 output descriptors, and we have no more data to send to 532 * Bail out of the loop if the program has closed its output
515 the client, and there is no pending buffered data. */ 533 * descriptors, and we have no more data to send to the
534 * client, and there is no pending buffered data.
535 */
516 if (fdout_eof && fderr_eof && !packet_have_data_to_write() && 536 if (fdout_eof && fderr_eof && !packet_have_data_to_write() &&
517 buffer_len(&stdout_buffer) == 0 && buffer_len(&stderr_buffer) == 0) { 537 buffer_len(&stdout_buffer) == 0 && buffer_len(&stderr_buffer) == 0) {
518 if (!channel_still_open()) 538 if (!channel_still_open())
@@ -604,11 +624,13 @@ quit:
604 packet_send(); 624 packet_send();
605 packet_write_wait(); 625 packet_write_wait();
606 626
607 /* Wait for exit confirmation. Note that there might be 627 /*
608 other packets coming before it; however, the program 628 * Wait for exit confirmation. Note that there might be
609 has already died so we just ignore them. The client is 629 * other packets coming before it; however, the program has
610 supposed to respond with the confirmation when it 630 * already died so we just ignore them. The client is
611 receives the exit status. */ 631 * supposed to respond with the confirmation when it receives
632 * the exit status.
633 */
612 do { 634 do {
613 int plen; 635 int plen;
614 type = packet_read(&plen); 636 type = packet_read(&plen);