POLLNVAL
means that the file descriptor value is invalid. It usually indicates an error in your program, but you can rely on poll
returning POLLNVAL
if you've closed a file descriptor and you haven't opened any file since then that might have reused the descriptor.
POLLHUP
basically means that what's at the other end of the connection has closed its end of the connection. POSIX describes it as
The device has been disconnected. This event and POLLOUT are mutually-exclusive; a stream can never be writable if a hangup has occurred.
This is clear enough for a terminal: the terminal has gone away (same event that generates a SIGHUP: the modem session has been terminated, the terminal emulator window has been closed, etc.). POLLHUP
is never sent for a regular file. For pipes and sockets, it depends. Linux sets POLLHUP
when the program on the writing end of a pipe closes the pipe, and sets POLLIN|POLLHUP
when the other end of a socket closed the socket, but POLLIN
only for a socket shutdown. Recent *BSD set POLLIN|POLLUP
when the writing end of a pipe closes the pipe, and the behavior for sockets is more variable.
To observe POLLHUP
, have your program read from a terminal and close the terminal. Or, on Linux, have your program read from a pipe and close the writing end (e.g. sleep 1 | yourprogram
).
man
page says:POLLHUP The device or socket has been disconnected. This flag is output only, and ignored if present in the input events bitmask. Note that POLLHUP and POLLOUT are mutually exclusive and should never be present in the revents bitmask at the same time.
– Gerik