Improve the clean-up of outdated IAuth instances on rehash (SF bug #2789656).
[ircu2.10.12-pk.git] / ircd / engine_kqueue.c
index 892c595daa231df0625106febf548bbedc030553..577cac01d57cd4785dbbe70a26f00b61fda9daa8 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 FreeBSD kqueue()/kevent() event engine.
+ * @version $Id$
  */
 #include "config.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 <signal.h>
+#include <sys/types.h>
 #include <sys/event.h>
 #include <sys/socket.h>
 #include <sys/time.h>
-#include <sys/types.h>
 #include <time.h>
 #include <unistd.h>
 
-#define KQUEUE_ERROR_THRESHOLD 20      /* after 20 kqueue errors, restart */
-#define ERROR_EXPIRE_TIME      3600    /* expire errors after an hour */
+#define KQUEUE_ERROR_THRESHOLD 20      /**< after 20 kqueue errors, restart */
+#define ERROR_EXPIRE_TIME      3600    /**< expire errors after an hour */
 
+/** Array of active Socket structures, indexed by file descriptor. */
 static struct Socket** sockList;
+/** Maximum file descriptor supported, plus one. */
 static int kqueue_max;
+/** File descriptor for kqueue pseudo-file. */
 static int kqueue_id;
 
+/** Number of recent errors from kqueue. */
 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)
 {
@@ -56,7 +65,10 @@ error_clear(struct Event* ev)
     timer_del(ev_timer(ev));
 }
 
-/* initialize the kqueue engine */
+/** Initialize the kqueue 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)
 {
@@ -80,7 +92,9 @@ engine_init(int max_sockets)
   return 1; /* success! */
 }
 
-/* add a signel to be watched for */
+/** Add a signal to the event engine.
+ * @param[in] sig Signal to add to engine.
+ */
 static void
 engine_signal(struct Signal* sig)
 {
@@ -111,7 +125,11 @@ engine_signal(struct Signal* sig)
   sigaction(sig_signal(sig), &act, 0);
 }
 
-/* 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)
 {
@@ -134,7 +152,11 @@ state_to_events(enum SocketState state, unsigned int events)
   return 0;
 }
 
-/* Activate kqueue filters as appropriate */
+/** Activate kqueue filters as appropriate.
+ * @param[in] sock Socket structure to operate on.
+ * @param[in] clear Set of interest events to clear from socket.
+ * @param[in] set Set of interest events to set on socket.
+ */
 static void
 set_or_clear(struct Socket* sock, unsigned int clear, unsigned int set)
 {
@@ -180,7 +202,10 @@ set_or_clear(struct Socket* sock, unsigned int clear, unsigned int set)
     event_generate(ET_ERROR, sock, errno); /* report error */
 }
 
-/* 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)
 {
@@ -206,7 +231,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)
 {
@@ -223,7 +251,10 @@ engine_state(struct Socket* sock, enum SocketState new_state)
 
 }
 
-/* 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)
 {
@@ -239,7 +270,9 @@ engine_events(struct Socket* sock, unsigned int new_events)
               state_to_events(s_state(sock), new_events)); /* 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)
 {
@@ -265,15 +298,12 @@ engine_delete(struct Socket* sock)
   dellist[1].data = 0;
   dellist[1].udata = 0;
 
-  /* make it all go away */
-  if (kevent(kqueue_id, dellist, 2, 0, 0, 0) < 0)
-    log_write(LS_SOCKET, L_WARNING, 0,
-             "Unable to delete kevent items for socket %d", s_fd(sock));
-
   sockList[s_fd(sock)] = 0;
 }
 
-/* engine event loop */
+/** Run engine event loop.
+ * @param[in] gen Lists of generators of various types.
+ */
 static void
 engine_loop(struct Generators* gen)
 {
@@ -402,8 +432,6 @@ engine_loop(struct Generators* gen)
        break;
       }
 
-      assert(s_fd(sock) == events[i].ident);
-
       gen_ref_dec(sock); /* we're done with it */
     }
 
@@ -411,6 +439,7 @@ engine_loop(struct Generators* gen)
   }
 }
 
+/** Descriptor for kqueue() event engine. */
 struct Engine engine_kqueue = {
   "kqueue()",          /* Engine name */
   engine_init,         /* Engine initialization function */