openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/util-linux/2.33.2-r0/util-linux-2.33.2/login-utils/su-common.c
static void write_eof_to_child(struct su_context* su)
{
    unsigned int tries = 0;
    struct pollfd fds[] = {{.fd = su->pty_slave, .events = POLLIN}};
    char c = DEF_EOF;

    DBG(PTY, ul_debug(" waiting for empty slave"));
    while (poll(fds, 1, 10) == 1 && tries < 8)
    {
        DBG(PTY, ul_debug("   slave is not empty"));
        xusleep(250000);
        tries++;
    }
    if (tries < 8)
        DBG(PTY, ul_debug("   slave is empty now"));

    DBG(PTY, ul_debug(" sending EOF to master"));
    write_to_child(su, &c, sizeof(char));
}
/*
 * The su(1) is usually faster than shell, so it's a good idea to wait until
 * the previous message has been already read by shell from slave before we
 * write to master. This is necessary especially for EOF situation when we can
 * send EOF to master before shell is fully initialized, to workaround this
 * problem we wait until slave is empty. For example:
 *
 *   echo "date" | su
 *
 * Unfortunately, the child (usually shell) can ignore stdin at all, so we
 * don't wait forever to avoid dead locks...
 *
 * Note that su --pty is primarily designed for interactive sessions as it
 * maintains master+slave tty stuff within the session. Use pipe to write to
 * su(1) and assume non-interactive (tee-like) behavior is NOT well
 * supported.
 */
The su(1) is usually faster than shell, so it's a good idea to wait until
the previous message has been already read by shell from slave before we
write to master. This is necessary especially for EOF situation when we can
send EOF to master before shell is fully initialized, to workaround this
problem we wait until slave is empty. For example.

echo "date" | su

Unfortunately, the child (usually shell) can ignore stdin at all, so we
don't wait forever to avoid dead locks

Note that su --pty is primarily designed for interactive sessions as it
maintains master+slave tty stuff within the session. Use pipe to write to
su(1) and assume non-interactive (tee-like) behavior is NOT well
supported.
The su ( 1 ) is usually faster than shell so it ' s a good idea to wait until the previous message has been already read by shell from slave before we write to master . This is necessary especially for EOF situation when we can send EOF to master before shell is fully initialized to workaround this problem we wait until slave is empty . For example . echo " date " | su Unfortunately the child ( usually shell ) can ignore stdin at all so we don ' t wait forever to avoid dead locks Note that su -- pty is primarily designed for interactive sessions as it maintains master + slave tty stuff within the session . Use pipe to write to su ( 1 ) and assume non - interactive ( tee - like ) behavior is NOT well supported .
static void write_eof_to_child(struct su_context* su)
{
    unsigned int tries = 0;
    struct pollfd fds[] = {{.fd = su->pty_slave, .events = POLLIN}};
    char c = DEF_EOF;
    DBG(PTY, ul_debug(" waiting for empty slave"));
    while (poll(fds, 1, 10) == 1 && tries < 8)
    {
        DBG(PTY, ul_debug("   slave is not empty"));
        xusleep(250000);
        tries++;
    }
    if (tries < 8)
        DBG(PTY, ul_debug("   slave is empty now"));
    DBG(PTY, ul_debug(" sending EOF to master"));
    write_to_child(su, &c, sizeof(char));
}
static void write_eof_to_child ( struct su_context * su ) { unsigned int tries = 0 ; struct pollfd fds [ ] = { { . fd = su -> pty_slave , . events = POLLIN } } ; char c = DEF_EOF ; DBG ( PTY , ul_debug ( " " ) ) ; while ( poll ( fds , 1 , 10 ) == 1 && tries < 8 ) { DBG ( PTY , ul_debug ( " " ) ) ; xusleep ( 250000 ) ; tries ++ ; } if ( tries < 8 ) DBG ( PTY , ul_debug ( " " ) ) ; DBG ( PTY , ul_debug ( " " ) ) ; write_to_child ( su , & c , sizeof ( char ) ) ; }
The su(1) is usually faster than shell, so it's a good idea to wait until
 the previous message has been already read by shell from slave before we
 write to master.
The su ( 1 ) is usually faster than shell so it ' s a good idea to wait until the previous message has been already read by shell from slave before we write to master .