Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POLLHUP vs POLLNVAL, or what is POLLHUP? [duplicate]

The manpages say for poll(2):

POLLHUP - Hang up (output only)

POLLNVAL - Invalid request: fd not open (output only)

What exactly is the difference? Writing a simple program shows that POLLNVAL will trigger if I close a file descriptor, then try reading from the closed fd. However, I can't figure out a way to return a POLLHUP.

like image 351
Lycus Avatar asked Aug 05 '14 19:08

Lycus


1 Answers

POLLNVAL is equivalent to EBADF: it means the file descriptor does not actually refer to any open file, i.e. it was closed or never open to begin with. This can never happen except as a result of a programming error or intentional attempt to query whether a file descriptor is invalid. External conditions, such as a peer closing its end of a network socket or pipe, can never close your file descriptor to your end of the socket or pipe. If it could, this would lead to massive vulnerabilities in basically any program using sockets/pipes/etc.

POLLHUP, on the other hand, indicates that your file descriptor is valid, but that it's in a state where:

A device has been disconnected, or a pipe or FIFO has been closed by the last process that had it open for writing. Once set, the hangup state of a FIFO shall persist until some process opens the FIFO for writing or until all read-only file descriptors for the FIFO are closed. This event and POLLOUT are mutually-exclusive; a stream can never be writable if a hangup has occurred. However, this event and POLLIN, POLLRDNORM, POLLRDBAND, or POLLPRI are not mutually-exclusive. This flag is only valid in the revents bitmask; it shall be ignored in the events member.

Source: http://pubs.opengroup.org/onlinepubs/9699919799/functions/poll.html

If you want to see POLLHUP, simply open a pipe, close the reading end, and query the writing end with poll.

like image 196
R.. GitHub STOP HELPING ICE Avatar answered Oct 15 '22 12:10

R.. GitHub STOP HELPING ICE