telnetd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Simple telnet server
  4. * Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  7. *
  8. * ---------------------------------------------------------------------------
  9. * (C) Copyright 2000, Axis Communications AB, LUND, SWEDEN
  10. ****************************************************************************
  11. *
  12. * The telnetd manpage says it all:
  13. *
  14. * Telnetd operates by allocating a pseudo-terminal device (see pty(4)) for
  15. * a client, then creating a login process which has the slave side of the
  16. * pseudo-terminal as stdin, stdout, and stderr. Telnetd manipulates the
  17. * master side of the pseudo-terminal, implementing the telnet protocol and
  18. * passing characters between the remote client and the login process.
  19. *
  20. * Vladimir Oleynik <dzo@simtreas.ru> 2001
  21. * Set process group corrections, initial busybox port
  22. */
  23. #define DEBUG 0
  24. #include "libbb.h"
  25. #if DEBUG
  26. #define TELCMDS
  27. #define TELOPTS
  28. #endif
  29. #include <arpa/telnet.h>
  30. #include <sys/syslog.h>
  31. /* Structure that describes a session */
  32. struct tsession {
  33. struct tsession *next;
  34. int sockfd_read, sockfd_write, ptyfd;
  35. int shell_pid;
  36. /* two circular buffers */
  37. /*char *buf1, *buf2;*/
  38. /*#define TS_BUF1 ts->buf1*/
  39. /*#define TS_BUF2 TS_BUF2*/
  40. #define TS_BUF1 ((unsigned char*)(ts + 1))
  41. #define TS_BUF2 (((unsigned char*)(ts + 1)) + BUFSIZE)
  42. int rdidx1, wridx1, size1;
  43. int rdidx2, wridx2, size2;
  44. };
  45. /* Two buffers are directly after tsession in malloced memory.
  46. * Make whole thing fit in 4k */
  47. enum { BUFSIZE = (4 * 1024 - sizeof(struct tsession)) / 2 };
  48. /* Globals */
  49. static int maxfd;
  50. static struct tsession *sessions;
  51. #if ENABLE_LOGIN
  52. static const char *loginpath = "/bin/login";
  53. #else
  54. static const char *loginpath = DEFAULT_SHELL;
  55. #endif
  56. static const char *issuefile = "/etc/issue.net";
  57. /*
  58. Remove all IAC's from buf1 (received IACs are ignored and must be removed
  59. so as to not be interpreted by the terminal). Make an uninterrupted
  60. string of characters fit for the terminal. Do this by packing
  61. all characters meant for the terminal sequentially towards the end of buf.
  62. Return a pointer to the beginning of the characters meant for the terminal.
  63. and make *num_totty the number of characters that should be sent to
  64. the terminal.
  65. Note - If an IAC (3 byte quantity) starts before (bf + len) but extends
  66. past (bf + len) then that IAC will be left unprocessed and *processed
  67. will be less than len.
  68. FIXME - if we mean to send 0xFF to the terminal then it will be escaped,
  69. what is the escape character? We aren't handling that situation here.
  70. CR-LF ->'s CR mapping is also done here, for convenience.
  71. NB: may fail to remove iacs which wrap around buffer!
  72. */
  73. static unsigned char *
  74. remove_iacs(struct tsession *ts, int *pnum_totty)
  75. {
  76. unsigned char *ptr0 = TS_BUF1 + ts->wridx1;
  77. unsigned char *ptr = ptr0;
  78. unsigned char *totty = ptr;
  79. unsigned char *end = ptr + MIN(BUFSIZE - ts->wridx1, ts->size1);
  80. int num_totty;
  81. while (ptr < end) {
  82. if (*ptr != IAC) {
  83. char c = *ptr;
  84. *totty++ = c;
  85. ptr++;
  86. /* We now map \r\n ==> \r for pragmatic reasons.
  87. * Many client implementations send \r\n when
  88. * the user hits the CarriageReturn key.
  89. */
  90. if (c == '\r' && ptr < end && (*ptr == '\n' || *ptr == '\0'))
  91. ptr++;
  92. } else {
  93. /*
  94. * TELOPT_NAWS support!
  95. */
  96. if ((ptr+2) >= end) {
  97. /* only the beginning of the IAC is in the
  98. buffer we were asked to process, we can't
  99. process this char. */
  100. break;
  101. }
  102. /*
  103. * IAC -> SB -> TELOPT_NAWS -> 4-byte -> IAC -> SE
  104. */
  105. else if (ptr[1] == SB && ptr[2] == TELOPT_NAWS) {
  106. struct winsize ws;
  107. if ((ptr+8) >= end)
  108. break; /* incomplete, can't process */
  109. ws.ws_col = (ptr[3] << 8) | ptr[4];
  110. ws.ws_row = (ptr[5] << 8) | ptr[6];
  111. ioctl(ts->ptyfd, TIOCSWINSZ, (char *)&ws);
  112. ptr += 9;
  113. } else {
  114. /* skip 3-byte IAC non-SB cmd */
  115. #if DEBUG
  116. fprintf(stderr, "Ignoring IAC %s,%s\n",
  117. TELCMD(ptr[1]), TELOPT(ptr[2]));
  118. #endif
  119. ptr += 3;
  120. }
  121. }
  122. }
  123. num_totty = totty - ptr0;
  124. *pnum_totty = num_totty;
  125. /* the difference between ptr and totty is number of iacs
  126. we removed from the stream. Adjust buf1 accordingly. */
  127. if ((ptr - totty) == 0) /* 99.999% of cases */
  128. return ptr0;
  129. ts->wridx1 += ptr - totty;
  130. ts->size1 -= ptr - totty;
  131. /* move chars meant for the terminal towards the end of the buffer */
  132. return memmove(ptr - num_totty, ptr0, num_totty);
  133. }
  134. static int
  135. getpty(char *line, int size)
  136. {
  137. int p;
  138. #if ENABLE_FEATURE_DEVPTS
  139. p = open("/dev/ptmx", O_RDWR);
  140. if (p > 0) {
  141. const char *name;
  142. grantpt(p);
  143. unlockpt(p);
  144. name = ptsname(p);
  145. if (!name) {
  146. bb_perror_msg("ptsname error (is /dev/pts mounted?)");
  147. return -1;
  148. }
  149. safe_strncpy(line, name, size);
  150. return p;
  151. }
  152. #else
  153. struct stat stb;
  154. int i;
  155. int j;
  156. strcpy(line, "/dev/ptyXX");
  157. for (i = 0; i < 16; i++) {
  158. line[8] = "pqrstuvwxyzabcde"[i];
  159. line[9] = '0';
  160. if (stat(line, &stb) < 0) {
  161. continue;
  162. }
  163. for (j = 0; j < 16; j++) {
  164. line[9] = j < 10 ? j + '0' : j - 10 + 'a';
  165. if (DEBUG)
  166. fprintf(stderr, "Trying to open device: %s\n", line);
  167. p = open(line, O_RDWR | O_NOCTTY);
  168. if (p >= 0) {
  169. line[5] = 't';
  170. return p;
  171. }
  172. }
  173. }
  174. #endif /* FEATURE_DEVPTS */
  175. return -1;
  176. }
  177. static struct tsession *
  178. make_new_session(
  179. USE_FEATURE_TELNETD_STANDALONE(int sock)
  180. SKIP_FEATURE_TELNETD_STANDALONE(void)
  181. ) {
  182. const char *login_argv[2];
  183. struct termios termbuf;
  184. int fd, pid;
  185. char tty_name[32];
  186. struct tsession *ts = xzalloc(sizeof(struct tsession) + BUFSIZE * 2);
  187. /*ts->buf1 = (char *)(ts + 1);*/
  188. /*ts->buf2 = ts->buf1 + BUFSIZE;*/
  189. /* Got a new connection, set up a tty. */
  190. fd = getpty(tty_name, 32);
  191. if (fd < 0) {
  192. bb_error_msg("can't create pty");
  193. return NULL;
  194. }
  195. if (fd > maxfd)
  196. maxfd = fd;
  197. ts->ptyfd = fd;
  198. ndelay_on(fd);
  199. #if ENABLE_FEATURE_TELNETD_STANDALONE
  200. ts->sockfd_read = sock;
  201. ndelay_on(sock);
  202. if (!sock) { /* We are called with fd 0 - we are in inetd mode */
  203. sock++; /* so use fd 1 for output */
  204. ndelay_on(sock);
  205. }
  206. ts->sockfd_write = sock;
  207. if (sock > maxfd)
  208. maxfd = sock;
  209. #else
  210. /* ts->sockfd_read = 0; - done by xzalloc */
  211. ts->sockfd_write = 1;
  212. ndelay_on(0);
  213. ndelay_on(1);
  214. #endif
  215. /* Make the telnet client understand we will echo characters so it
  216. * should not do it locally. We don't tell the client to run linemode,
  217. * because we want to handle line editing and tab completion and other
  218. * stuff that requires char-by-char support. */
  219. {
  220. static const char iacs_to_send[] ALIGN1 = {
  221. IAC, DO, TELOPT_ECHO,
  222. IAC, DO, TELOPT_NAWS,
  223. IAC, DO, TELOPT_LFLOW,
  224. IAC, WILL, TELOPT_ECHO,
  225. IAC, WILL, TELOPT_SGA
  226. };
  227. memcpy(TS_BUF2, iacs_to_send, sizeof(iacs_to_send));
  228. ts->rdidx2 = sizeof(iacs_to_send);
  229. ts->size2 = sizeof(iacs_to_send);
  230. }
  231. fflush(NULL); /* flush all streams */
  232. pid = vfork(); /* NOMMU-friendly */
  233. if (pid < 0) {
  234. free(ts);
  235. close(fd);
  236. /* sock will be closed by caller */
  237. bb_perror_msg("vfork");
  238. return NULL;
  239. }
  240. if (pid > 0) {
  241. /* Parent */
  242. ts->shell_pid = pid;
  243. return ts;
  244. }
  245. /* Child */
  246. /* Careful - we are after vfork! */
  247. /* make new session and process group */
  248. setsid();
  249. /* Restore default signal handling */
  250. signal(SIGCHLD, SIG_DFL);
  251. signal(SIGPIPE, SIG_DFL);
  252. /* open the child's side of the tty. */
  253. /* NB: setsid() disconnects from any previous ctty's. Therefore
  254. * we must open child's side of the tty AFTER setsid! */
  255. fd = xopen(tty_name, O_RDWR); /* becomes our ctty */
  256. dup2(fd, 0);
  257. dup2(fd, 1);
  258. dup2(fd, 2);
  259. while (fd > 2) close(fd--);
  260. tcsetpgrp(0, getpid()); /* switch this tty's process group to us */
  261. /* The pseudo-terminal allocated to the client is configured to operate in
  262. * cooked mode, and with XTABS CRMOD enabled (see tty(4)). */
  263. tcgetattr(0, &termbuf);
  264. termbuf.c_lflag |= ECHO; /* if we use readline we dont want this */
  265. termbuf.c_oflag |= ONLCR | XTABS;
  266. termbuf.c_iflag |= ICRNL;
  267. termbuf.c_iflag &= ~IXOFF;
  268. /*termbuf.c_lflag &= ~ICANON;*/
  269. tcsetattr(0, TCSANOW, &termbuf);
  270. /* Uses FILE-based I/O to stdout, but does fflush(stdout),
  271. * so should be safe with vfork.
  272. * I fear, though, that some users will have ridiculously big
  273. * issue files, and they may block writing to fd 1,
  274. * (parent is supposed to read it, but parent waits
  275. * for vforked child to exec!) */
  276. print_login_issue(issuefile, NULL);
  277. /* Exec shell / login / whatever */
  278. login_argv[0] = loginpath;
  279. login_argv[1] = NULL;
  280. /* exec busybox applet (if PREFER_APPLETS=y), if that fails,
  281. * exec external program */
  282. BB_EXECVP(loginpath, (char **)login_argv);
  283. /* _exit is safer with vfork, and we shouldn't send message
  284. * to remote clients anyway */
  285. _exit(1); /*bb_perror_msg_and_die("execv %s", loginpath);*/
  286. }
  287. /* Must match getopt32 string */
  288. enum {
  289. OPT_WATCHCHILD = (1 << 2), /* -K */
  290. OPT_INETD = (1 << 3) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -i */
  291. OPT_PORT = (1 << 4) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -p */
  292. OPT_FOREGROUND = (1 << 6) * ENABLE_FEATURE_TELNETD_STANDALONE, /* -F */
  293. };
  294. #if ENABLE_FEATURE_TELNETD_STANDALONE
  295. static void
  296. free_session(struct tsession *ts)
  297. {
  298. struct tsession *t = sessions;
  299. if (option_mask32 & OPT_INETD)
  300. exit(0);
  301. /* Unlink this telnet session from the session list */
  302. if (t == ts)
  303. sessions = ts->next;
  304. else {
  305. while (t->next != ts)
  306. t = t->next;
  307. t->next = ts->next;
  308. }
  309. #if 0
  310. /* It was said that "normal" telnetd just closes ptyfd,
  311. * doesn't send SIGKILL. When we close ptyfd,
  312. * kernel sends SIGHUP to processes having slave side opened. */
  313. kill(ts->shell_pid, SIGKILL);
  314. wait4(ts->shell_pid, NULL, 0, NULL);
  315. #endif
  316. close(ts->ptyfd);
  317. close(ts->sockfd_read);
  318. /* We do not need to close(ts->sockfd_write), it's the same
  319. * as sockfd_read unless we are in inetd mode. But in inetd mode
  320. * we do not reach this */
  321. free(ts);
  322. /* Scan all sessions and find new maxfd */
  323. maxfd = 0;
  324. ts = sessions;
  325. while (ts) {
  326. if (maxfd < ts->ptyfd)
  327. maxfd = ts->ptyfd;
  328. if (maxfd < ts->sockfd_read)
  329. maxfd = ts->sockfd_read;
  330. #if 0
  331. /* Again, sockfd_write == sockfd_read here */
  332. if (maxfd < ts->sockfd_write)
  333. maxfd = ts->sockfd_write;
  334. #endif
  335. ts = ts->next;
  336. }
  337. }
  338. #else /* !FEATURE_TELNETD_STANDALONE */
  339. /* Used in main() only, thus "return 0" actually is exit(0). */
  340. #define free_session(ts) return 0
  341. #endif
  342. static void handle_sigchld(int sig)
  343. {
  344. pid_t pid;
  345. struct tsession *ts;
  346. /* Looping: more than one child may have exited */
  347. while (1) {
  348. pid = waitpid(-1, NULL, WNOHANG);
  349. if (pid <= 0)
  350. break;
  351. ts = sessions;
  352. while (ts) {
  353. if (ts->shell_pid == pid) {
  354. ts->shell_pid = -1;
  355. break;
  356. }
  357. ts = ts->next;
  358. }
  359. }
  360. }
  361. int telnetd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  362. int telnetd_main(int argc, char **argv)
  363. {
  364. fd_set rdfdset, wrfdset;
  365. unsigned opt;
  366. int count;
  367. struct tsession *ts;
  368. #if ENABLE_FEATURE_TELNETD_STANDALONE
  369. #define IS_INETD (opt & OPT_INETD)
  370. int master_fd = master_fd; /* be happy, gcc */
  371. unsigned portnbr = 23;
  372. char *opt_bindaddr = NULL;
  373. char *opt_portnbr;
  374. #else
  375. enum {
  376. IS_INETD = 1,
  377. master_fd = -1,
  378. portnbr = 23,
  379. };
  380. #endif
  381. /* Even if !STANDALONE, we accept (and ignore) -i, thus people
  382. * don't need to guess whether it's ok to pass -i to us */
  383. opt = getopt32(argv, "f:l:Ki" USE_FEATURE_TELNETD_STANDALONE("p:b:F"),
  384. &issuefile, &loginpath
  385. USE_FEATURE_TELNETD_STANDALONE(, &opt_portnbr, &opt_bindaddr));
  386. if (!IS_INETD /*&& !re_execed*/) {
  387. /* inform that we start in standalone mode?
  388. * May be useful when people forget to give -i */
  389. /*bb_error_msg("listening for connections");*/
  390. if (!(opt & OPT_FOREGROUND)) {
  391. /* DAEMON_CHDIR_ROOT was giving inconsistent
  392. * behavior with/without -F, -i */
  393. bb_daemonize_or_rexec(0 /*was DAEMON_CHDIR_ROOT*/, argv);
  394. }
  395. }
  396. /* Redirect log to syslog early, if needed */
  397. if (IS_INETD || !(opt & OPT_FOREGROUND)) {
  398. openlog(applet_name, 0, LOG_USER);
  399. logmode = LOGMODE_SYSLOG;
  400. }
  401. USE_FEATURE_TELNETD_STANDALONE(
  402. if (opt & OPT_PORT)
  403. portnbr = xatou16(opt_portnbr);
  404. );
  405. /* Used to check access(loginpath, X_OK) here. Pointless.
  406. * exec will do this for us for free later. */
  407. #if ENABLE_FEATURE_TELNETD_STANDALONE
  408. if (IS_INETD) {
  409. sessions = make_new_session(0);
  410. if (!sessions) /* pty opening or vfork problem, exit */
  411. return 1; /* make_new_session prints error message */
  412. } else {
  413. master_fd = create_and_bind_stream_or_die(opt_bindaddr, portnbr);
  414. xlisten(master_fd, 1);
  415. }
  416. #else
  417. sessions = make_new_session();
  418. if (!sessions) /* pty opening or vfork problem, exit */
  419. return 1; /* make_new_session prints error message */
  420. #endif
  421. /* We don't want to die if just one session is broken */
  422. signal(SIGPIPE, SIG_IGN);
  423. if (opt & OPT_WATCHCHILD)
  424. signal(SIGCHLD, handle_sigchld);
  425. else /* prevent dead children from becoming zombies */
  426. signal(SIGCHLD, SIG_IGN);
  427. /*
  428. This is how the buffers are used. The arrows indicate the movement
  429. of data.
  430. +-------+ wridx1++ +------+ rdidx1++ +----------+
  431. | | <-------------- | buf1 | <-------------- | |
  432. | | size1-- +------+ size1++ | |
  433. | pty | | socket |
  434. | | rdidx2++ +------+ wridx2++ | |
  435. | | --------------> | buf2 | --------------> | |
  436. +-------+ size2++ +------+ size2-- +----------+
  437. size1: "how many bytes are buffered for pty between rdidx1 and wridx1?"
  438. size2: "how many bytes are buffered for socket between rdidx2 and wridx2?"
  439. Each session has got two buffers. Buffers are circular. If sizeN == 0,
  440. buffer is empty. If sizeN == BUFSIZE, buffer is full. In both these cases
  441. rdidxN == wridxN.
  442. */
  443. again:
  444. FD_ZERO(&rdfdset);
  445. FD_ZERO(&wrfdset);
  446. /* Select on the master socket, all telnet sockets and their
  447. * ptys if there is room in their session buffers.
  448. * NB: scalability problem: we recalculate entire bitmap
  449. * before each select. Can be a problem with 500+ connections. */
  450. ts = sessions;
  451. while (ts) {
  452. struct tsession *next = ts->next; /* in case we free ts. */
  453. if (ts->shell_pid == -1) {
  454. /* Child died and we detected that */
  455. free_session(ts);
  456. } else {
  457. if (ts->size1 > 0) /* can write to pty */
  458. FD_SET(ts->ptyfd, &wrfdset);
  459. if (ts->size1 < BUFSIZE) /* can read from socket */
  460. FD_SET(ts->sockfd_read, &rdfdset);
  461. if (ts->size2 > 0) /* can write to socket */
  462. FD_SET(ts->sockfd_write, &wrfdset);
  463. if (ts->size2 < BUFSIZE) /* can read from pty */
  464. FD_SET(ts->ptyfd, &rdfdset);
  465. }
  466. ts = next;
  467. }
  468. if (!IS_INETD) {
  469. FD_SET(master_fd, &rdfdset);
  470. /* This is needed because free_session() does not
  471. * take master_fd into account when it finds new
  472. * maxfd among remaining fd's */
  473. if (master_fd > maxfd)
  474. maxfd = master_fd;
  475. }
  476. count = select(maxfd + 1, &rdfdset, &wrfdset, NULL, NULL);
  477. if (count < 0)
  478. goto again; /* EINTR or ENOMEM */
  479. #if ENABLE_FEATURE_TELNETD_STANDALONE
  480. /* First check for and accept new sessions. */
  481. if (!IS_INETD && FD_ISSET(master_fd, &rdfdset)) {
  482. int fd;
  483. struct tsession *new_ts;
  484. fd = accept(master_fd, NULL, NULL);
  485. if (fd < 0)
  486. goto again;
  487. /* Create a new session and link it into our active list */
  488. new_ts = make_new_session(fd);
  489. if (new_ts) {
  490. new_ts->next = sessions;
  491. sessions = new_ts;
  492. } else {
  493. close(fd);
  494. }
  495. }
  496. #endif
  497. /* Then check for data tunneling. */
  498. ts = sessions;
  499. while (ts) { /* For all sessions... */
  500. struct tsession *next = ts->next; /* in case we free ts. */
  501. if (/*ts->size1 &&*/ FD_ISSET(ts->ptyfd, &wrfdset)) {
  502. int num_totty;
  503. unsigned char *ptr;
  504. /* Write to pty from buffer 1. */
  505. ptr = remove_iacs(ts, &num_totty);
  506. count = safe_write(ts->ptyfd, ptr, num_totty);
  507. if (count < 0) {
  508. if (errno == EAGAIN)
  509. goto skip1;
  510. goto kill_session;
  511. }
  512. ts->size1 -= count;
  513. ts->wridx1 += count;
  514. if (ts->wridx1 >= BUFSIZE) /* actually == BUFSIZE */
  515. ts->wridx1 = 0;
  516. }
  517. skip1:
  518. if (/*ts->size2 &&*/ FD_ISSET(ts->sockfd_write, &wrfdset)) {
  519. /* Write to socket from buffer 2. */
  520. count = MIN(BUFSIZE - ts->wridx2, ts->size2);
  521. count = safe_write(ts->sockfd_write, TS_BUF2 + ts->wridx2, count);
  522. if (count < 0) {
  523. if (errno == EAGAIN)
  524. goto skip2;
  525. goto kill_session;
  526. }
  527. ts->size2 -= count;
  528. ts->wridx2 += count;
  529. if (ts->wridx2 >= BUFSIZE) /* actually == BUFSIZE */
  530. ts->wridx2 = 0;
  531. }
  532. skip2:
  533. /* Should not be needed, but... remove_iacs is actually buggy
  534. * (it cannot process iacs which wrap around buffer's end)!
  535. * Since properly fixing it requires writing bigger code,
  536. * we rely instead on this code making it virtually impossible
  537. * to have wrapped iac (people don't type at 2k/second).
  538. * It also allows for bigger reads in common case. */
  539. if (ts->size1 == 0) {
  540. ts->rdidx1 = 0;
  541. ts->wridx1 = 0;
  542. }
  543. if (ts->size2 == 0) {
  544. ts->rdidx2 = 0;
  545. ts->wridx2 = 0;
  546. }
  547. if (/*ts->size1 < BUFSIZE &&*/ FD_ISSET(ts->sockfd_read, &rdfdset)) {
  548. /* Read from socket to buffer 1. */
  549. count = MIN(BUFSIZE - ts->rdidx1, BUFSIZE - ts->size1);
  550. count = safe_read(ts->sockfd_read, TS_BUF1 + ts->rdidx1, count);
  551. if (count <= 0) {
  552. if (count < 0 && errno == EAGAIN)
  553. goto skip3;
  554. goto kill_session;
  555. }
  556. /* Ignore trailing NUL if it is there */
  557. if (!TS_BUF1[ts->rdidx1 + count - 1]) {
  558. --count;
  559. }
  560. ts->size1 += count;
  561. ts->rdidx1 += count;
  562. if (ts->rdidx1 >= BUFSIZE) /* actually == BUFSIZE */
  563. ts->rdidx1 = 0;
  564. }
  565. skip3:
  566. if (/*ts->size2 < BUFSIZE &&*/ FD_ISSET(ts->ptyfd, &rdfdset)) {
  567. /* Read from pty to buffer 2. */
  568. count = MIN(BUFSIZE - ts->rdidx2, BUFSIZE - ts->size2);
  569. count = safe_read(ts->ptyfd, TS_BUF2 + ts->rdidx2, count);
  570. if (count <= 0) {
  571. if (count < 0 && errno == EAGAIN)
  572. goto skip4;
  573. goto kill_session;
  574. }
  575. ts->size2 += count;
  576. ts->rdidx2 += count;
  577. if (ts->rdidx2 >= BUFSIZE) /* actually == BUFSIZE */
  578. ts->rdidx2 = 0;
  579. }
  580. skip4:
  581. ts = next;
  582. continue;
  583. kill_session:
  584. free_session(ts);
  585. ts = next;
  586. }
  587. goto again;
  588. }