How to get errno when epoll_wait returns EPOLLERR?
Asked Answered
L

4

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?

Larrisa answered 31/10, 2012 at 2:20 Comment(0)
K
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));
}
Ketose answered 21/3, 2013 at 7:30 Comment(2)
Sorry for the lateness of the reply - this worked perfectly - thanks! :)Larrisa
but what if fd is a file/pipe and not a socket ?Rely
I
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.

Index answered 26/6, 2013 at 19:10 Comment(1)
This should have been a comment not an answerTiliaceous
S
-2

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

Sect answered 31/10, 2012 at 5:14 Comment(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 onLarrisa
E
-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 : ");
    }
Enplane answered 31/10, 2012 at 8:41 Comment(4)
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 onLarrisa
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?Enplane
For socket, I'm using this flag to check for closed peers though: EPOLLRDHUP.Enplane
I'm not looking for epoll_wait errors, I'm looking for information on the errors returned for file descriptors being watched by epollLarrisa

© 2022 - 2024 — McMap. All rights reserved.