Periodically call tids_sweep_procs() during trust router operation
authorJennifer Richards <jennifer@painless-security.com>
Thu, 19 Apr 2018 02:39:44 +0000 (22:39 -0400)
committerJennifer Richards <jennifer@painless-security.com>
Thu, 19 Apr 2018 02:39:44 +0000 (22:39 -0400)
include/tr_tid.h
tid/tids.c
tr/tr_main.c
tr/tr_tid.c

index 3b06e4b..d8285b0 100644 (file)
@@ -43,7 +43,7 @@
 #define TR_TID_MAX_AAA_SERVERS 10
 
 int tr_tids_event_init(struct event_base *base, TIDS_INSTANCE *tids, TR_CFG_MGR *cfg_mgr, TRPS_INSTANCE *trps,
-                       struct tr_socket_event *tids_ev);
+                       struct tr_socket_event *tids_ev, struct event **sweep_ev);
 
 /* tr_tid_mons.c */
 void tr_tid_register_mons_handlers(TIDS_INSTANCE *tids, MONS_INSTANCE *mons);
index a11f48b..6a5b172 100644 (file)
@@ -442,6 +442,17 @@ int tids_accept(TIDS_INSTANCE *tids, int listen)
   return 0;
 }
 
+/**
+ * Clean up any finished TID request processes
+ *
+ * This is called by the main process after forking each TID request. If you want to be
+ * sure finished processes are cleaned up promptly even during a lull in TID requests,
+ * this can be called from the main thread of the main process. It is not thread-safe,
+ * so should not be used from sub-threads. It should not be called by child processes -
+ * this would probably be harmless but ineffective.
+ *
+ * @param tids
+ */
 void tids_sweep_procs(TIDS_INSTANCE *tids)
 {
   guint ii;
index 91d225b..68f682e 100644 (file)
@@ -167,6 +167,7 @@ int main(int argc, char *argv[])
   struct cmdline_args opts;
   struct event_base *ev_base;
   struct tr_socket_event tids_ev = {0};
+  struct event *tids_sweep_ev;
   struct tr_socket_event mon_ev = {0};
   struct event *cfgwatch_ev;
 
@@ -271,11 +272,7 @@ int main(int argc, char *argv[])
 
   /* install TID server events */
   tr_debug("Initializing TID server events.");
-  if (0 != tr_tids_event_init(ev_base,
-                              tr->tids,
-                              tr->cfg_mgr,
-                              tr->trps,
-                             &tids_ev)) {
+  if (0 != tr_tids_event_init(ev_base, tr->tids, tr->cfg_mgr, tr->trps, &tids_ev, &tids_sweep_ev)) {
     tr_crit("Error initializing Trust Path Query Server instance.");
     return 1;
   }
index 1bec328..fdf40d6 100644 (file)
@@ -673,7 +673,7 @@ static int tr_tids_gss_handler(gss_name_t client_name, TR_NAME *gss_name,
 /* called when a connection to the TIDS port is received */
 static void tr_tids_event_cb(int listener, short event, void *arg)
 {
-  TIDS_INSTANCE *tids = (TIDS_INSTANCE *)arg;
+  TIDS_INSTANCE *tids = talloc_get_type_abort(arg, TIDS_INSTANCE);
 
   if (0==(event & EV_READ))
     tr_debug("tr_tids_event_cb: unexpected event on TIDS socket (event=0x%X)", event);
@@ -681,17 +681,26 @@ static void tr_tids_event_cb(int listener, short event, void *arg)
     tids_accept(tids, listener);
 }
 
-/* Configure the tids instance and set up its event handler.
+/* called when it's time to sweep for completed TID child processes */
+static void tr_tids_sweep_cb(int listener, short event, void *arg)
+{
+  TIDS_INSTANCE *tids = talloc_get_type_abort(arg, TIDS_INSTANCE);
+
+  if (0==(event & EV_TIMEOUT))
+    tr_debug("tr_tids_event_cb: unexpected event on TID process sweep timer (event=0x%X)", event);
+  else
+    tids_sweep_procs(tids);
+}
+
+/* Configure the tids instance and set up its event handlers.
  * Returns 0 on success, nonzero on failure. Fills in
  * *tids_event (which should be allocated by caller). */
-int tr_tids_event_init(struct event_base *base,
-                       TIDS_INSTANCE *tids,
-                       TR_CFG_MGR *cfg_mgr,
-                       TRPS_INSTANCE *trps,
-                       struct tr_socket_event *tids_ev)
+int tr_tids_event_init(struct event_base *base, TIDS_INSTANCE *tids, TR_CFG_MGR *cfg_mgr, TRPS_INSTANCE *trps,
+                       struct tr_socket_event *tids_ev, struct event **sweep_ev)
 {
   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
   struct tr_tids_event_cookie *cookie=NULL;
+  struct timeval sweep_interval;
   int retval=0;
   int ii=0;
 
@@ -701,6 +710,12 @@ int tr_tids_event_init(struct event_base *base,
     goto cleanup;
   }
 
+  if (sweep_ev == NULL) {
+    tr_debug("tr_tids_event_init: Null sweep_ev.");
+    retval = 1;
+    goto cleanup;
+  }
+
   /* Create the cookie for callbacks. We'll put it in the tids context, so it will
    * be cleaned up when tids is freed by talloc_free. */
   cookie=talloc(tmp_ctx, struct tr_tids_event_cookie);
@@ -729,7 +744,7 @@ int tr_tids_event_init(struct event_base *base,
     goto cleanup;
   }
 
-  /* Set up events */
+  /* Set up listener events */
   for (ii=0; ii<tids_ev->n_sock_fd; ii++) {
     tids_ev->ev[ii]=event_new(base,
                               tids_ev->sock_fd[ii],
@@ -739,6 +754,12 @@ int tr_tids_event_init(struct event_base *base,
     event_add(tids_ev->ev[ii], NULL);
   }
 
+  /* Set up a periodic check for completed TID handler processes */
+  *sweep_ev = event_new(base, -1, EV_TIMEOUT|EV_PERSIST, tr_tids_sweep_cb, tids);
+  sweep_interval.tv_sec = 10;
+  sweep_interval.tv_usec = 0;
+  event_add(*sweep_ev, &sweep_interval);
+
 cleanup:
   talloc_free(tmp_ctx);
   return retval;