Skip to content
Snippets Groups Projects
  • Nadav Har'El's avatar
    b53d39ac
    Our select() function is emulated using poll(), which is a sensible thing · b53d39ac
    Nadav Har'El authored
    to do. However, it did several things wrong that this patch fixes. Thanks
    to Paolo Bonzini for finding these problems (see issue #35).
    
    1. When poll() returned a bare POLLHUP, without POLLIN, our select() didn't
    return a read event. But nothing in the manpages guarantees that POLLHUP
    is accompanied by POLLIN, and some special file implementations might
    forget it. As an example, in Linux POLLHUP without POLLIN is common.
    But POLLHUP on its own already means that there's nothing more to read,
    so a read() will return immediately without blocking - and therefore
    select() needs to turn on the readable bit for this fd.
    
    2. Similarly, a bare POLLRDHUP should turn on the writable bit: The
    reader on this file hug up, so a write will fail immediately.
    
    3. Our poll() and select() confused what POLLERR means. POLLERR does not
    mean poll() found a bad file descriptor - there is POLLNVAL for that.
    So this patch fixes poll() to set POLLNVAL, not POLLERR, and select()
    to return with errno=EBADF when it sees POLLNVAL, not POLLERR.
    
    4. Rather, POLLERR means the file descriptor is in an error state, so every
    read() or write() will return immediately (with an error). So when we see
    it, we need to turn on both read and write bits in this case.
    
    5. The meaning of "exceptfds" isn't clear in any manual page, and it
    seems there're a lot of opinions on what it might mean. In this patch I
    did what Paolo suggested, which is to set the except bit when POLLPRI.
    (I don't set exceptfds on POLLERR, or any other case).
    
    Signed-off-by: default avatarNadav Har'El <nyh@cloudius-systems.com>
    b53d39ac
    History
    Our select() function is emulated using poll(), which is a sensible thing
    Nadav Har'El authored
    to do. However, it did several things wrong that this patch fixes. Thanks
    to Paolo Bonzini for finding these problems (see issue #35).
    
    1. When poll() returned a bare POLLHUP, without POLLIN, our select() didn't
    return a read event. But nothing in the manpages guarantees that POLLHUP
    is accompanied by POLLIN, and some special file implementations might
    forget it. As an example, in Linux POLLHUP without POLLIN is common.
    But POLLHUP on its own already means that there's nothing more to read,
    so a read() will return immediately without blocking - and therefore
    select() needs to turn on the readable bit for this fd.
    
    2. Similarly, a bare POLLRDHUP should turn on the writable bit: The
    reader on this file hug up, so a write will fail immediately.
    
    3. Our poll() and select() confused what POLLERR means. POLLERR does not
    mean poll() found a bad file descriptor - there is POLLNVAL for that.
    So this patch fixes poll() to set POLLNVAL, not POLLERR, and select()
    to return with errno=EBADF when it sees POLLNVAL, not POLLERR.
    
    4. Rather, POLLERR means the file descriptor is in an error state, so every
    read() or write() will return immediately (with an error). So when we see
    it, we need to turn on both read and write bits in this case.
    
    5. The meaning of "exceptfds" isn't clear in any manual page, and it
    seems there're a lot of opinions on what it might mean. In this patch I
    did what Paolo suggested, which is to set the except bit when POLLPRI.
    (I don't set exceptfds on POLLERR, or any other case).
    
    Signed-off-by: default avatarNadav Har'El <nyh@cloudius-systems.com>