source: branches/3.2/mindi-busybox/networking/telnetd.ctrlSQ.patch @ 3232

Last change on this file since 3232 was 2725, checked in by bruno, 8 years ago
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File size: 6.5 KB
  • busybox-1.13.2/networking/telnetd.c

    From: "Doug Graham" <dgraham@nortel.com>
    Date: 2009-01-22 07:20
    
    Hello,
    
    Busybox's telnetd does not disable local (client-side) flow control
    properly.  It does not put the pty into packet mode and then notify the
    client whenever flow control is disabled by an application running under
    its control.  The result is that ^S/^Q are not passed through to the
    application, which is painful when the application is an emacs variant.
    
    I suppose that support for this might be considered bloat, but the
    included patch only adds about 200 bytes of text to x86 busybox and 300
    bytes to mipsel busybox.  Please consider applying.
    
    =============================
    
    NB: the patch doesn't work as-is because we now have iac_safe_write()
    which quotes IACs on output.
    
    =============================
    Docs:
    
    The following ioctl(2) calls apply only to pseudo terminals:
    
    TIOCSTOP Stops output to a terminal (e.g. like typing ^S). Takes no parameter.
    
    TIOCSTART Restarts output (stopped by TIOCSTOP or by typing ^S). Takes no parameter.
    
    TIOCPKT         Enable/disable packet mode. When applied to the master side of a pseudo terminal, each
    subsequent read(2) from the terminal will return data written on the slave part of the pseudo terminal preceded by a
    zero byte (symbolically defined as TIOCPKT_DATA), or a single byte reflecting control status information.
    In the latter case, the byte is an inclusive-or of zero or more of the bits:
    
    TIOCPKT_FLUSHREAD     whenever the read queue for the terminal is flushed.
    TIOCPKT_FLUSHWRITE    whenever the write queue for the terminal is flushed.
    TIOCPKT_STOP    whenever output to the terminal is stopped a la ^S.
    TIOCPKT_START   whenever output to the terminal is restarted.
    TIOCPKT_DOSTOP  whenever t_stopc is ^S and t_startc is ^Q.
    TIOCPKT_NOSTOP  whenever the start and stop characters are not ^S/^Q.
    
    While this mode is in use, the presence of control status information to be read from the master side may be detected
    by a select(2) for exceptional conditions.
    
    This mode is used by rlogin(1) and rlogind(8) to implement a remote-echoed, locally ^S/^Q flow-controlled remote login
    with proper back-flushing of output; it can be used by other similar programs.
    
    TIOCUCNTL       Enable/disable a mode that allows a small number of simple user ioctl(2) commands to be passed through
    the pseudo-terminal, using a protocol similar to that of TIOCPKT. The TIOCUCNTL and TIOCPKT modes are mutually
    exclusive. This mode is enabled from the master side of a pseudo terminal. Each subsequent read(2) from the master side
    will return data written on the slave part of the pseudo terminal preceded by a zero byte, or a single byte reflecting a
    user control operation on the slave side. A user control command consists of a special ioctl(2) operation with no data;
    the command is given as UIOCCMD (n), where n is a number in the range 1-255. The operation value n will be received as
    a single byte on the next read(2) from the master side. The ioctl(2) UIOCCMD (0) is a no-op that may be used to probe
    for the existence of this facility. As with TIOCPKT mode, command operations may be detected with a select(2) for
    exceptional conditions.
    
     
    3838    int sockfd_read, sockfd_write, ptyfd;
    3939    int shell_pid;
    4040
     41#ifdef TIOCPKT
     42    int flowstate;
     43#endif
    4144    /* two circular buffers */
    4245    /*char *buf1, *buf2;*/
    4346/*#define TS_BUF1 ts->buf1*/
     
    170173    int fd, pid;
    171174    char tty_name[GETPTY_BUFSIZE];
    172175    struct tsession *ts = xzalloc(sizeof(struct tsession) + BUFSIZE * 2);
     176#ifdef TIOCPKT
     177    int on = 1;
     178#endif
    173179
    174180    /*ts->buf1 = (char *)(ts + 1);*/
    175181    /*ts->buf2 = ts->buf1 + BUFSIZE;*/
     
    180186        maxfd = fd;
    181187    ts->ptyfd = fd;
    182188    ndelay_on(fd);
     189#ifdef TIOCPKT
     190    ioctl(fd, TIOCPKT, &on);
     191    ts->flowstate = TIOCPKT_DOSTOP;
     192#endif
    183193#if ENABLE_FEATURE_TELNETD_STANDALONE
    184194    ts->sockfd_read = sock;
    185195    /* SO_KEEPALIVE by popular demand */
     
    385395        portnbr = 23,
    386396    };
    387397#endif
     398#ifdef TIOCPKT
     399    int control;
     400    static const char lflow_on[] =
     401        {IAC, SB, TELOPT_LFLOW, LFLOW_ON, IAC, SE};
     402    static const char lflow_off[] =
     403        {IAC, SB, TELOPT_LFLOW, LFLOW_OFF, IAC, SE};
     404# define RESERVED sizeof(lflow_on)
     405#else
     406# define RESERVED 0
     407#endif
    388408    /* Even if !STANDALONE, we accept (and ignore) -i, thus people
    389409     * don't need to guess whether it's ok to pass -i to us */
    390410    opt = getopt32(argv, "f:l:Ki" IF_FEATURE_TELNETD_STANDALONE("p:b:F"),
     
    475495                FD_SET(ts->sockfd_read, &rdfdset);
    476496            if (ts->size2 > 0)       /* can write to socket */
    477497                FD_SET(ts->sockfd_write, &wrfdset);
    478             if (ts->size2 < BUFSIZE) /* can read from pty */
     498            if (ts->size2 < (BUFSIZE - RESERVED)) /* can read from pty */
    479499                FD_SET(ts->ptyfd, &rdfdset);
    480500        }
    481501        ts = next;
     
    593613                    goto skip4;
    594614                goto kill_session;
    595615            }
     616#ifdef TIOCPKT
     617            control = TS_BUF2[ts->rdidx2];
     618            if (--count > 0 && control == TIOCPKT_DATA) {
     619                /*
     620                 * If we are in packet mode, and we have
     621                 * just read a chunk of actual data from
     622                 * the pty, then there is the TIOCPKT_DATA
     623                 * byte (zero) that we have got to remove
     624                 * somehow.  If there were no chars in
     625                 * TS_BUF2 before we did this read, then
     626                 * we can optimize by just advancing wridx2.
     627                 * Otherwise we have to copy the new data down
     628                 * to close the gap (Could use readv() instead).
     629                 */
     630                if (ts->size2 == 0)
     631                    ts->wridx2++;
     632                else {
     633                    memmove(TS_BUF2 + ts->rdidx2,
     634                        TS_BUF2 + ts->rdidx2 + 1, count);
     635                }
     636            }
     637
     638            /*
     639             * If the flow control state changed, notify
     640             * the client.  If "control" is not TIOCPKT_DATA,
     641             * then there are no data bytes to worry about.
     642             */
     643            if ((control & (TIOCPKT_DOSTOP|TIOCPKT_NOSTOP)) != 0
     644             && ts->flowstate != (control & TIOCPKT_DOSTOP)) {
     645                const char *p = ts->flowstate ? lflow_off : lflow_on;
     646
     647                /*
     648                 * We know we have enough free slots available
     649                 * (see RESERVED) but they are not necessarily
     650                 * contiguous; we may have to wrap.
     651                 */
     652                for (count = sizeof(lflow_on); count > 0; count--) {
     653                    TS_BUF2[ts->rdidx2++] = *p++;
     654                    if (ts->rdidx2 >= BUFSIZE)
     655                        ts->rdidx2 = 0;
     656                    ts->size2++;
     657                }
     658
     659                ts->flowstate = control & TIOCPKT_DOSTOP;
     660            }
     661#endif /* TIOCPKT */
    596662            ts->size2 += count;
    597663            ts->rdidx2 += count;
    598664            if (ts->rdidx2 >= BUFSIZE) /* actually == BUFSIZE */
Note: See TracBrowser for help on using the repository browser.