Improve the clean-up of outdated IAuth instances on rehash (SF bug #2789656).
[ircu2.10.12-pk.git] / ircd / engine_devpoll.c
index 8bd9c75511708b0c454d96e0846d22b35b6ace4d..9ef4dee0b36a77817243469d58d25dfc20defd8b 100644 (file)
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * $Id$
+ */
+/** @file
+ * @brief Solaris /dev/poll event engine.
+ * @version $Id$
  */
 #include "config.h"
 
 
 #include "ircd.h"
 #include "ircd_alloc.h"
+#include "ircd_features.h"
 #include "ircd_log.h"
 #include "s_debug.h"
 
-#include <assert.h>
+/* #include <assert.h> -- Now using assert in ircd_log.h */
 #include <errno.h>
 #include <fcntl.h>
 #include <sys/devpoll.h>
 #include <sys/types.h>
 #include <unistd.h>
 
-#define DEVPOLL_ERROR_THRESHOLD        20      /* after 20 devpoll errors, restart */
-#define ERROR_EXPIRE_TIME      3600    /* expire errors after an hour */
-
-#define POLLS_PER_DEVPOLL      20      /* get 20 pollfd's per turn */
+#define DEVPOLL_ERROR_THRESHOLD        20      /**< after 20 devpoll errors, restart */
+#define ERROR_EXPIRE_TIME      3600    /**< expire errors after an hour */
 
 /* Figure out what bits to set for read */
 #if defined(POLLMSG) && defined(POLLIN) && defined(POLLRDNORM)
 #  define POLLWRITEFLAGS POLLWRNORM
 #endif
 
-/* Figure out what bits indicate errors */
-#ifdef POLLHUP
-#  define POLLERRORS (POLLHUP|POLLERR)
-#else
-#  define POLLERRORS POLLERR
-#endif
-
+/** Array of active Socket structures, indexed by file descriptor. */
 static struct Socket** sockList;
+/** Maximum file descriptor supported, plus one. */
 static int devpoll_max;
+/** File descriptor for /dev/poll device. */
 static int devpoll_fd;
 
+/** Number of recent errors from /dev/poll. */
 static int errors = 0;
+/** Periodic timer to forget errors. */
 static struct Timer clear_error;
 
-/* decrements the error count once per hour */
+/** Decrement the error count (once per hour).
+ * @param[in] ev Expired timer event (ignored).
+ */
 static void
 error_clear(struct Event* ev)
 {
@@ -84,7 +85,10 @@ error_clear(struct Event* ev)
     timer_del(ev_timer(ev));
 }
 
-/* initialize the devpoll engine */
+/** Initialize the /dev/poll engine.
+ * @param[in] max_sockets Maximum number of file descriptors to support.
+ * @return Non-zero on success, or zero on failure.
+ */
 static int
 engine_init(int max_sockets)
 {
@@ -108,7 +112,11 @@ engine_init(int max_sockets)
   return 1;
 }
 
-/* Figure out what events go with a given state */
+/** Figure out what events go with a given state.
+ * @param[in] state %Socket state to consider.
+ * @param[in] events User-specified preferred event set.
+ * @return Actual set of preferred events.
+ */
 static unsigned int
 state_to_events(enum SocketState state, unsigned int events)
 {
@@ -131,7 +139,10 @@ state_to_events(enum SocketState state, unsigned int events)
   return 0;
 }
 
-/* Reset the desired events */
+/** Set the desired events for a socket.
+ * @param[in,out] sock Socket to operate on.
+ * @param[in] events User-specified preferred event set.
+ */
 static void
 set_events(struct Socket* sock, unsigned int events)
 {
@@ -174,7 +185,10 @@ set_events(struct Socket* sock, unsigned int events)
   s_ed_int(sock) = 1; /* mark that we've added a pollfd */
 }
 
-/* add a socket to be listened on */
+/** Add a socket to the event engine.
+ * @param[in] sock Socket to add to engine.
+ * @return Non-zero on success, or zero on error.
+ */
 static int
 engine_add(struct Socket* sock)
 {
@@ -200,7 +214,10 @@ engine_add(struct Socket* sock)
   return 1; /* success */
 }
 
-/* socket switching to new state */
+/** Handle state transition for a socket.
+ * @param[in] sock Socket changing state.
+ * @param[in] new_state New state for socket.
+ */
 static void
 engine_state(struct Socket* sock, enum SocketState new_state)
 {
@@ -214,7 +231,10 @@ engine_state(struct Socket* sock, enum SocketState new_state)
   set_events(sock, state_to_events(new_state, s_events(sock)));
 }
 
-/* socket events changing */
+/** Handle change to preferred socket events.
+ * @param[in] sock Socket getting new interest list.
+ * @param[in] new_events New set of interesting events for socket.
+ */
 static void
 engine_events(struct Socket* sock, unsigned int new_events)
 {
@@ -228,7 +248,9 @@ engine_events(struct Socket* sock, unsigned int new_events)
   set_events(sock, state_to_events(s_state(sock), new_events));
 }
 
-/* socket going away */
+/** Remove a socket from the event engine.
+ * @param[in] sock Socket being destroyed.
+ */
 static void
 engine_delete(struct Socket* sock)
 {
@@ -243,21 +265,33 @@ engine_delete(struct Socket* sock)
   sockList[s_fd(sock)] = 0; /* zero the socket list entry */
 }
 
-/* engine event loop */
+/** Run engine event loop.
+ * @param[in] gen Lists of generators of various types.
+ */
 static void
 engine_loop(struct Generators* gen)
 {
   struct dvpoll dopoll;
-  struct pollfd polls[POLLS_PER_DEVPOLL];
+  struct pollfd *polls;
+  int polls_count;
   struct Socket* sock;
   int nfds;
   int i;
   int errcode;
   size_t codesize;
 
+  if ((polls_count = feature_int(FEAT_POLLS_PER_LOOP)) < 20)
+    polls_count = 20;
+  polls = (struct pollfd *)MyMalloc(sizeof(struct pollfd) * polls_count);
+
   while (running) {
+    if ((i = feature_int(FEAT_POLLS_PER_LOOP)) >= 20 && i != polls_count) {
+      polls = (struct pollfd *)MyRealloc(polls, sizeof(struct pollfd) * i);
+      polls_count = i;
+    }
+
     dopoll.dp_fds = polls; /* set up the struct dvpoll */
-    dopoll.dp_nfds = POLLS_PER_DEVPOLL;
+    dopoll.dp_nfds = polls_count;
 
     /* calculate the proper timeout */
     dopoll.dp_timeout = timer_next(gen) ?
@@ -276,7 +310,7 @@ engine_loop(struct Generators* gen)
        /* Log the poll error */
        log_write(LS_SOCKET, L_ERROR, 0, "ioctl(DP_POLL) error: %m");
        if (!errors++)
-         timer_add(&clear_error, error_clear, 0, TT_PERIODIC,
+         timer_add(timer_init(&clear_error), error_clear, 0, TT_PERIODIC,
                    ERROR_EXPIRE_TIME);
        else if (errors > DEVPOLL_ERROR_THRESHOLD) /* too many errors... */
          server_restart("too many /dev/poll errors");
@@ -289,13 +323,13 @@ engine_loop(struct Generators* gen)
 
     for (i = 0; i < nfds; i++) {
       assert(-1 < polls[i].fd);
-      assert(0 != sockList[polls[i].fd]);
-      assert(s_fd(sockList[polls[i].fd]) == polls[i].fd);
 
       sock = sockList[polls[i].fd];
       if (!sock) /* slots may become empty while processing events */
        continue;
 
+      assert(s_fd(sock) == polls[i].fd);
+
       gen_ref_inc(sock); /* can't have it going away on us */
 
       Debug((DEBUG_ENGINE, "devpoll: Checking socket %p (fd %d) state %s, "
@@ -318,6 +352,17 @@ engine_loop(struct Generators* gen)
        }
       }
 
+      assert(!(polls[i].revents & POLLERR));
+
+#ifdef POLLHUP
+      if (polls[i].revents & POLLHUP) { /* hang-up on socket */
+       Debug((DEBUG_ENGINE, "devpoll: EOF from client (POLLHUP)"));
+       event_generate(ET_EOF, sock, 0);
+       nfds--;
+       continue;
+      }
+#endif /* POLLHUP */
+
       switch (s_state(sock)) {
       case SS_CONNECTING:
        if (polls[i].revents & POLLWRITEFLAGS) { /* connection completed */
@@ -385,8 +430,6 @@ engine_loop(struct Generators* gen)
        break;
       }
 
-      assert(s_fd(sock) == polls[i].fd);
-
       gen_ref_dec(sock); /* we're done with it */
     }
 
@@ -394,6 +437,7 @@ engine_loop(struct Generators* gen)
   }
 }
 
+/** Descriptor for /dev/poll event engine. */
 struct Engine engine_devpoll = {
   "/dev/poll",         /* Engine name */
   engine_init,         /* Engine initialization function */