16

Is there a way to find out the errno when epoll_wait returns EPOLLERR for a particular fd?

Is there any further information about the nature of the error?

Edit:

Adding more information to prevent ambiguity

epoll_wait waits on a number of file descriptors. When you call epoll_wait you pass it an array of epoll_event structures:

struct epoll_event {
           uint32_t     events;    /* Epoll events */
           epoll_data_t data;      /* User data variable */
       };

The epoll_data_t structure has the same details as the one you used with epoll_ctl to add a file descriptor to epoll:

typedef union epoll_data {
           void    *ptr;
           int      fd;
           uint32_t u32;
           uint64_t u64;
       } epoll_data_t;

What I'm looking for is what happens when there is an error on one of the file descriptors that epoll is waiting on.

ie: (epoll_event.events & EPOLLERR) == 1 - is there a way to find out more details of the error on the file descriptor?

Steve Lorimer
  • 27,059
  • 17
  • 118
  • 213

4 Answers4

23

Use getsockopt and SO_ERROR to get the pending error on the socket

int       error = 0;
socklen_t errlen = sizeof(error);
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen) == 0)
{
    printf("error = %s\n", strerror(error));
}
Vestman
  • 386
  • 4
  • 7
0

Just a minor point: Your test won't work correctly, for two reasons. If EPOLLERR is defined as, say, 0x8, then your test will be comparing 8 with one (since == has higher precedence than &), giving you a zero, then anding that with the event mask.

What you want is: (epoll_event.events & EPOLLERR) != 0 to test for the EPOLLERR bit being set.

-2

epoll_wait returns -1 when an error occurs and sets errno appropriately. See "man 2 epoll_wait" for more info.

Danil Onishchenko
  • 2,030
  • 11
  • 19
  • 1
    Thanks for the response, but as you will see from my comments on the answer by @HongZhou, this is not what I'm looking for. I am aware that if epoll_wait returns -1 for the epoll file descriptor I can use errno to get the error. What I'm looking for is when epoll_event.events & EPOLLERR == 1 for one of the file descriptors epoll is waiting on – Steve Lorimer Oct 31 '12 at 20:31
-4

Include errno.h and use perror to see the error message. Basically error is from the epfd or interupt, it will not arise from the file descriptor in your set.

include "errno.h"

if(epoll_wait() == -1)
    {
      perror("Epoll error : ");
    }
Community
  • 1
  • 1
Hong Zhou
  • 659
  • 1
  • 9
  • 20
  • Thanks for the response, but this is not what I'm looking for. I am aware that if epoll_wait returns -1 for the epoll file descriptor I can use errno to get the error. What I'm looking for is when epoll_event.events & EPOLLERR == 1 for one of the file descriptors epoll is waiting on – Steve Lorimer Oct 31 '12 at 20:30
  • 1.EBADF - epfd is not a valid file descriptor. 2.EFAULT - The memory area pointed to by events is not accessible with write permissions. 3.EINTR - The call was interrupted by a signal handler before any of the requested events occurred or the timeout expired; see signal(7). 4.EINVAL - epfd is not an epoll file descriptor, or maxevents is less than or equal to zero. Above are the only 4 errors in the man. Under what circumstances will EPOLLERR == 1? – Hong Zhou Nov 01 '12 at 06:59
  • For socket, I'm using this flag to check for closed peers though: EPOLLRDHUP. – Hong Zhou Nov 01 '12 at 07:20
  • I'm not looking for `epoll_wait` errors, I'm looking for information on the errors returned for file descriptors being **watched** by epoll – Steve Lorimer Nov 01 '12 at 11:09