aboutsummaryrefslogtreecommitdiff
path: root/networking/telnetd.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-10-16 21:35:17 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-10-16 21:35:17 +0000
commitf472b237a1590bd4a20cb89ac6edc5f8a19ddbf3 (patch)
treed9d30cd77f6ef94c3021e9e050937a13e4e6c347 /networking/telnetd.c
parent686b0ef7d83f11741c21aae292558671e5298799 (diff)
downloadbusybox-w32-f472b237a1590bd4a20cb89ac6edc5f8a19ddbf3.tar.gz
busybox-w32-f472b237a1590bd4a20cb89ac6edc5f8a19ddbf3.tar.bz2
busybox-w32-f472b237a1590bd4a20cb89ac6edc5f8a19ddbf3.zip
telnetd: code shrink suggested by Ralf Friedl <Ralf.Friedl@online.de>
Diffstat (limited to 'networking/telnetd.c')
-rw-r--r--networking/telnetd.c67
1 files changed, 36 insertions, 31 deletions
diff --git a/networking/telnetd.c b/networking/telnetd.c
index eb3701133..f06bff8b9 100644
--- a/networking/telnetd.c
+++ b/networking/telnetd.c
@@ -203,7 +203,7 @@ getpty(char *line, int size)
203 203
204static struct tsession * 204static struct tsession *
205make_new_session( 205make_new_session(
206 USE_FEATURE_TELNETD_STANDALONE(int sock_r, int sock_w) 206 USE_FEATURE_TELNETD_STANDALONE(int sock)
207 SKIP_FEATURE_TELNETD_STANDALONE(void) 207 SKIP_FEATURE_TELNETD_STANDALONE(void)
208) { 208) {
209 const char *login_argv[2]; 209 const char *login_argv[2];
@@ -221,19 +221,22 @@ make_new_session(
221 bb_error_msg("can't create pty"); 221 bb_error_msg("can't create pty");
222 return NULL; 222 return NULL;
223 } 223 }
224 if (fd > maxfd) maxfd = fd; 224 if (fd > maxfd)
225 maxfd = fd;
225 ts->ptyfd = fd; 226 ts->ptyfd = fd;
226 ndelay_on(fd); 227 ndelay_on(fd);
227#if ENABLE_FEATURE_TELNETD_STANDALONE 228#if ENABLE_FEATURE_TELNETD_STANDALONE
228 if (sock_w > maxfd) maxfd = sock_w; 229 ts->sockfd_read = sock;
229 ts->sockfd_write = sock_w; 230 ndelay_on(sock);
230 ndelay_on(sock_w); 231 if (!sock) /* We are called with fd 0 - we are in inetd mode */
231 if (sock_r > maxfd) maxfd = sock_r; 232 sock++;
232 ts->sockfd_read = sock_r; 233 ts->sockfd_write = sock;
233 ndelay_on(sock_r); 234 ndelay_on(sock);
235 if (sock > maxfd)
236 maxfd = sock;
234#else 237#else
235 ts->sockfd_write = 1;
236 /* ts->sockfd_read = 0; - done by xzalloc */ 238 /* ts->sockfd_read = 0; - done by xzalloc */
239 ts->sockfd_write = 1;
237 ndelay_on(0); 240 ndelay_on(0);
238 ndelay_on(1); 241 ndelay_on(1);
239#endif 242#endif
@@ -326,27 +329,33 @@ free_session(struct tsession *ts)
326 t->next = ts->next; 329 t->next = ts->next;
327 } 330 }
328 331
332#if 0
329 /* It was said that "normal" telnetd just closes ptyfd, 333 /* It was said that "normal" telnetd just closes ptyfd,
330 * doesn't send SIGKILL. When we close ptyfd, 334 * doesn't send SIGKILL. When we close ptyfd,
331 * kernel sends SIGHUP to processes having slave side opened. */ 335 * kernel sends SIGHUP to processes having slave side opened. */
332 /*kill(ts->shell_pid, SIGKILL); 336 kill(ts->shell_pid, SIGKILL);
333 wait4(ts->shell_pid, NULL, 0, NULL);*/ 337 wait4(ts->shell_pid, NULL, 0, NULL);
338#endif
334 close(ts->ptyfd); 339 close(ts->ptyfd);
335 close(ts->sockfd_read); 340 close(ts->sockfd_read);
336 /* error if ts->sockfd_read == ts->sockfd_write. So what? ;) */ 341 /* We do not need to close(ts->sockfd_write), it's the same
337 close(ts->sockfd_write); 342 * as sockfd_read unless we are in inetd mode. But in inetd mode
343 * we do not free_session(), ever */
338 free(ts); 344 free(ts);
339 345
340 /* Scan all sessions and find new maxfd */ 346 /* Scan all sessions and find new maxfd */
341 ts = sessions;
342 maxfd = 0; 347 maxfd = 0;
348 ts = sessions;
343 while (ts) { 349 while (ts) {
344 if (maxfd < ts->ptyfd) 350 if (maxfd < ts->ptyfd)
345 maxfd = ts->ptyfd; 351 maxfd = ts->ptyfd;
346 if (maxfd < ts->sockfd_read) 352 if (maxfd < ts->sockfd_read)
347 maxfd = ts->sockfd_read; 353 maxfd = ts->sockfd_read;
354#if 0
355 /* Again, sockfd_write == sockfd_read here */
348 if (maxfd < ts->sockfd_write) 356 if (maxfd < ts->sockfd_write)
349 maxfd = ts->sockfd_write; 357 maxfd = ts->sockfd_write;
358#endif
350 ts = ts->next; 359 ts = ts->next;
351 } 360 }
352} 361}
@@ -434,7 +443,7 @@ int telnetd_main(int argc, char **argv)
434 443
435#if ENABLE_FEATURE_TELNETD_STANDALONE 444#if ENABLE_FEATURE_TELNETD_STANDALONE
436 if (IS_INETD) { 445 if (IS_INETD) {
437 sessions = make_new_session(0, 1); 446 sessions = make_new_session(0);
438 if (!sessions) /* pty opening or vfork problem, exit */ 447 if (!sessions) /* pty opening or vfork problem, exit */
439 return 1; /* make_new_session prints error message */ 448 return 1; /* make_new_session prints error message */
440 } else { 449 } else {
@@ -519,7 +528,7 @@ int telnetd_main(int argc, char **argv)
519 if (fd < 0) 528 if (fd < 0)
520 goto again; 529 goto again;
521 /* Create a new session and link it into our active list */ 530 /* Create a new session and link it into our active list */
522 new_ts = make_new_session(fd, fd); 531 new_ts = make_new_session(fd);
523 if (new_ts) { 532 if (new_ts) {
524 new_ts->next = sessions; 533 new_ts->next = sessions;
525 sessions = new_ts; 534 sessions = new_ts;
@@ -545,9 +554,7 @@ int telnetd_main(int argc, char **argv)
545 goto skip1; 554 goto skip1;
546 if (IS_INETD) 555 if (IS_INETD)
547 return 0; 556 return 0;
548 free_session(ts); 557 goto kill_session;
549 ts = next;
550 continue;
551 } 558 }
552 ts->size1 -= count; 559 ts->size1 -= count;
553 ts->wridx1 += count; 560 ts->wridx1 += count;
@@ -564,9 +571,7 @@ int telnetd_main(int argc, char **argv)
564 goto skip2; 571 goto skip2;
565 if (IS_INETD) 572 if (IS_INETD)
566 return 0; 573 return 0;
567 free_session(ts); 574 goto kill_session;
568 ts = next;
569 continue;
570 } 575 }
571 ts->size2 -= count; 576 ts->size2 -= count;
572 ts->wridx2 += count; 577 ts->wridx2 += count;
@@ -577,7 +582,7 @@ int telnetd_main(int argc, char **argv)
577 /* Should not be needed, but... remove_iacs is actually buggy 582 /* Should not be needed, but... remove_iacs is actually buggy
578 * (it cannot process iacs which wrap around buffer's end)! 583 * (it cannot process iacs which wrap around buffer's end)!
579 * Since properly fixing it requires writing bigger code, 584 * Since properly fixing it requires writing bigger code,
580 * we will rely instead on this code making it virtually impossible 585 * we rely instead on this code making it virtually impossible
581 * to have wrapped iac (people don't type at 2k/second). 586 * to have wrapped iac (people don't type at 2k/second).
582 * It also allows for bigger reads in common case. */ 587 * It also allows for bigger reads in common case. */
583 if (ts->size1 == 0) { 588 if (ts->size1 == 0) {
@@ -598,14 +603,11 @@ int telnetd_main(int argc, char **argv)
598 goto skip3; 603 goto skip3;
599 if (IS_INETD) 604 if (IS_INETD)
600 return 0; 605 return 0;
601 free_session(ts); 606 goto kill_session;
602 ts = next;
603 continue;
604 } 607 }
605 /* Ignore trailing NUL if it is there */ 608 /* Ignore trailing NUL if it is there */
606 if (!TS_BUF1[ts->rdidx1 + count - 1]) { 609 if (!TS_BUF1[ts->rdidx1 + count - 1]) {
607 if (!--count) 610 --count;
608 goto skip3;
609 } 611 }
610 ts->size1 += count; 612 ts->size1 += count;
611 ts->rdidx1 += count; 613 ts->rdidx1 += count;
@@ -622,9 +624,7 @@ int telnetd_main(int argc, char **argv)
622 goto skip4; 624 goto skip4;
623 if (IS_INETD) 625 if (IS_INETD)
624 return 0; 626 return 0;
625 free_session(ts); 627 goto kill_session;
626 ts = next;
627 continue;
628 } 628 }
629 ts->size2 += count; 629 ts->size2 += count;
630 ts->rdidx2 += count; 630 ts->rdidx2 += count;
@@ -633,6 +633,11 @@ int telnetd_main(int argc, char **argv)
633 } 633 }
634 skip4: 634 skip4:
635 ts = next; 635 ts = next;
636 continue;
637 kill_session:
638 free_session(ts);
639 ts = next;
636 } 640 }
641
637 goto again; 642 goto again;
638} 643}