poll returns 0 in bash but works as expected in tcsh

39 Views Asked by At

I'm using sshfs and I face a strange behaviour regarding the poll system call.

    while (1) {
    struct pollfd fds[2];

    fds[0].fd = sshfs.rfd;
    fds[0].events = POLLIN;
    fds[1].fd = sshfs.ptyfd;
    fds[1].events = POLLIN;
    res = poll(fds, 2, timeout);
    if (res == -1) {
        perror("poll");
        return -1;
    }
    if (res == 0) {
        fprintf(stderr, "Timeout waiting for prompt\n");
        return -1;
    }
    if (fds[0].revents) {
        /*
         * Something happened on stdout of ssh, this
         * either means, that we are connected, or
         * that we are disconnected.  In any case the
         * password doesn't matter any more.
         */
        break;
    }

    res = read(sshfs.ptyfd, &c, 1);
    if (res == -1) {
        perror("read");
        return -1;
    }
    if (res == 0) {
        fprintf(stderr, "EOF while waiting for prompt\n");
        return -1;
    }
    buf[len] = c;
    len++;
    if (len == passwd_len) {
        if (memcmp(buf, passwd_str, passwd_len) == 0) {
            write(sshfs.ptyfd, sshfs.password,
                  strlen(sshfs.password));
        }
        memmove(buf, buf + 1, passwd_len - 1);
        len--;
    }
}

while in tcsh the code actually accepts input and able to process further, in bash, no input is accepted, and then after 60 seconds, it timeouts (the timeout that is assigned for this syscall). Why those shells behave differently, and how I can make bash accept the password without reprogramming the code?

I tried to run the code both in bash and tcsh, but only in tcsh it works as expected.

Edit: It seems that in a nested scenario. (tcsh on bash on tcsh for example), it doesn't work.

0

There are 0 best solutions below