How to get errno when epoll_wait returns EPOLLERR?

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

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

Edit:

Adding more information to prevent ambiguity

epoll_wait expects several 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; 

I'm looking for what happens when an error occurs in one of the file descriptors that epoll expects.

i.e.: (epoll_event.events & EPOLLERR) == 1 - is there a way to find out more information about the error in the file descriptor?

+11
linux epoll
source share
4 answers

Use getsockopt and SO_ERROR to get a pending socket error

 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)); } 
+18
source share

A small point: your test will not work correctly for two reasons. If EPOLLERR is defined as, say, 0x8, then your test will compare 8 with one (since == takes precedence over &), giving you zero and then using the event mask.

You want: (epoll_event.events and EPOLLERR)! = 0 to check if the EPOLLERR bit is set.

0
source share

epoll_wait returns -1 when an error occurs and sets errno correctly. See "Man 2 epoll_wait" for more information.

-2
source share

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

include "errno.h"

 if(epoll_wait() == -1) { perror("Epoll error : "); } 
-4
source share

All Articles