Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle the Linux socket revents POLLERR, POLLHUP and POLLNVAL?

Tags:

I'm wondering what should be done when poll set these bits? Close socket, ignore it or what?

like image 548
user3790882 Avatar asked Jul 16 '14 22:07

user3790882


People also ask

What is Pollhup?

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.

What does poll do Linux?

The poll() feature allows programs to multiplex input and output through a series of file descriptors. In other words, the poll() system call is analogous to select() system call in working as it holds its fire for one of several file descriptors by becoming available for I/O.


2 Answers

A POLLHUP means the socket is no longer connected. In TCP, this means FIN has been received and sent.

A POLLERR means the socket got an asynchronous error. In TCP, this typically means a RST has been received or sent. If the file descriptor is not a socket, POLLERR might mean the device does not support polling.

For both of the conditions above, the socket file descriptor is still open, and has not yet been closed (but shutdown() may have already been called). A close() on the file descriptor will release resources that are still being reserved on behalf of the socket. In theory, it should be possible to reuse the socket immediately (e.g., with another connect() call).

A POLLNVAL means the socket file descriptor is not open. It would be an error to close() it.

like image 59
jxh Avatar answered Sep 18 '22 13:09

jxh


It depend on the exact error nature. Use getsockopt() to see the problem:

int error = 0; socklen_t errlen = sizeof(error); getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen); 

Values: http://www.xinotes.net/notes/note/1793/

The easiest way is to assume that the socket is no longer usable in any case and close it.

like image 24
hasan Avatar answered Sep 17 '22 13:09

hasan