title: Always print EAGAIN when calling accept after epoll_wait
body: <p>I'm using epoll to monitor the listen fd event, after the EPOLLIN event occur I call accept to process,
  but always EAGAIN error. Anyone can give me some suggestions? thanks!</p>
  
  <p><strong>[log]</strong> print the information below all the time</p>
  
  <p>INFO      Jan 01 00:02:08:924 [385] poll_loop: epoll has 1 event</p>
  
  <p>INFO      Jan 01 00:02:08:925 [385] poll_loop: event fd 0, event type:1</p>
  
  <p>INFO      Jan 01 00:02:08:925 [385] handle_connect: fd 0, </p>
  
  <p>ERROR     Jan 01 00:02:08:925 [385] handle_connect: accept returned an error 11 Resource temporarily unavailable ... retrying, listen fd:0.</p>
  
  <p><strong>[code]</strong></p>
  
  <pre><code>    ................
  
          listenfd = listen_sock (port, &amp;addrlen);
          socket_nonblocking(listenfd);
          g_epollFd = epoll_create(MAX_EVENTS);  
          register_read(listenfd);
  
      .................
  
          while (!config.quit)
          {
  
              int fds = epoll_wait(g_epollFd, g_Events, MAX_EVENTS, -1);  
  
              if (fds &lt; 0)
              {
                  log_message(LOG_CRIT, "epoll wait error %d %s, continue", errno, strerror(errno));
                  continue;
              }
  
              log_message(LOG_INFO, "epoll has %d event", fds);
              for (i = 0; i &lt; fds; ++i)
               {
  
                  log_message(LOG_INFO, "event fd %d, event type:%d", g_Events[i].data.fd, g_Events[i].events);
                   if ((g_Events[i].events &amp; EPOLLERR) 
                      || (g_Events[i].events &amp; EPOLLHUP)
                      /*|| (g_Events[i].events &amp; POLLNVAL) compile err*/)
                  {
                      log_message(LOG_INFO, "A disconnect occurs, [fd:%d]", g_Events[i].data.fd);
                      handle_disconnect(g_Events[i].data.fd);      
                      continue;
                  }
                  if (g_Events[i].events &amp; EPOLLIN)
                  {
                       if (listenfd == g_Events[i].data.fd)
                      {
                          handle_connect(listenfd, ptr);
                      }
                      else if (g_dnsfd == g_Events[i].data.fd)
                      {
                          dns_poll(g_dns);
                      }
                      else
                      {
                          handle_input(g_Events[i].data.fd);
                      }
                  }
      ...................
  
      void handle_connect(int fd, struct child_s *ptr)
      {
          int connfd;
          struct conn_s *connptr = 0;
          char peer_ipaddr[IP_LENGTH]; 
          socklen_t clilen = sizeof(struct sockaddr_in);
          struct sockaddr_in cliaddr ;
  
          log_message(LOG_INFO, "fd %d, ");
          connfd = accept (fd, (struct sockaddr*)(&amp;cliaddr), &amp;clilen);
  
          if (connfd &lt; 0) 
          {
              log_message (LOG_ERR, "accept returned an error %d %s ... retrying, listen fd:%d", errno, strerror (errno), fd);
              return;
          }
  
      ......................
  
  #define REGISTER_EPOLL_EVENT(_fd, evt, op) \
  do\
  {\
      struct epoll_event epv = {0, {0}};  \
      epv.data.fd = _fd;  \
      epv.events  = evt;  /*EPOLLLT default*/\
  \
      if(epoll_ctl(g_epollFd, op, _fd, &amp;epv) &lt; 0)  \
      {\
          /*if ((errno != EEXIST) &amp;&amp; (errno != ENOENT))*/\
          {\
              log_message(LOG_ERR, "epool ctl set failed, fd %d, errno %d: %s", _fd, errno, strerror(errno));  \
              return ;\
          }\
      }\
      log_message(LOG_INFO, "epool ctl set sucess, fd %d", _fd);\
  }while(0)
  
  
  void register_read(int fd, int* evt)
  {
      if (0 == evt)
      {
          REGISTER_EPOLL_EVENT(fd, EPOLLIN, EPOLL_CTL_ADD);
      }
      else if (0 == *evt)
      {
          *evt |= EPOLLIN;
  
          REGISTER_EPOLL_EVENT(fd, (*evt), EPOLL_CTL_ADD);
      }
      else if (!(*evt &amp; EPOLLIN))
      {
          *evt |= EPOLLIN;
          REGISTER_EPOLL_EVENT(fd, (*evt), EPOLL_CTL_MOD);
      }
      else
      {
          log_message("event %d for fd %d is already there", *evt, fd);
      }
  
  }
  </code></pre>