[PATCH 3/5] dimrngr: avoid need for hkp housekeeping

Daniel Kahn Gillmor dkg at fifthhorseman.net
Tue Nov 1 01:33:04 CET 2016


* dirmngr/ks-engine-hkp.c (host_is_alive): new function tests whether
  host is alive and resurrects it if it has been dead long enough;
  (select_random_host, map_host, ks_hkp_mark_host): use host_is_alive
  instead of testing hostinfo_t->dead directly; (ks_hkp_housekeeping):
  remove function, no longer needed.
* dirmngr/dirmngr.c (housekeeping_thread): remove call to
  ks_hkp_housekeeping.

--

Rather than resurrecting hosts upon scheduled resurrection times, test
whether hosts should be resurrected as they're inspected for being
dead.  This removes the need for explicit housekeeping, and makes host
resurrections happen "just in time", rather than being clustered on
HOUSEKEEPING_INTERVAL seconds.

Signed-off-by: Daniel Kahn Gillmor <dkg at fifthhorseman.net>
---
 dirmngr/dirmngr.c       |  4 ---
 dirmngr/dirmngr.h       |  4 ---
 dirmngr/ks-engine-hkp.c | 73 ++++++++++++++++++++++++-------------------------
 3 files changed, 36 insertions(+), 45 deletions(-)

diff --git a/dirmngr/dirmngr.c b/dirmngr/dirmngr.c
index 776cf95..d7b43df 100644
--- a/dirmngr/dirmngr.c
+++ b/dirmngr/dirmngr.c
@@ -1757,11 +1757,9 @@ static void *
 housekeeping_thread (void *arg)
 {
   static int sentinel;
-  time_t curtime;
 
   (void)arg;
 
-  curtime = gnupg_get_time ();
   if (sentinel)
     {
       log_info ("housekeeping is already going on\n");
@@ -1771,8 +1769,6 @@ housekeeping_thread (void *arg)
   if (opt.verbose > 1)
     log_info ("starting housekeeping\n");
 
-  ks_hkp_housekeeping (curtime);
-
   if (opt.verbose > 1)
     log_info ("ready with housekeeping\n");
   sentinel--;
diff --git a/dirmngr/dirmngr.h b/dirmngr/dirmngr.h
index 9e216cd..40f9416 100644
--- a/dirmngr/dirmngr.h
+++ b/dirmngr/dirmngr.h
@@ -187,10 +187,6 @@ void dirmngr_sighup_action (void);
 const char* dirmngr_get_current_socket_name (void);
 
 
-/*-- Various housekeeping functions.  --*/
-void ks_hkp_housekeeping (time_t curtime);
-
-
 /*-- server.c --*/
 ldap_server_t get_ldapservers_from_ctrl (ctrl_t ctrl);
 ksba_cert_t get_cert_local (ctrl_t ctrl, const char *issuer);
diff --git a/dirmngr/ks-engine-hkp.c b/dirmngr/ks-engine-hkp.c
index a9d566e..7a8d647 100644
--- a/dirmngr/ks-engine-hkp.c
+++ b/dirmngr/ks-engine-hkp.c
@@ -203,6 +203,25 @@ host_in_pool_p (int *pool, int tblidx)
 }
 
 
+static int
+host_is_alive (hostinfo_t hi, time_t curtime)
+{
+  if (!hi)
+    return 0;
+  if (!hi->dead)
+    return 1;
+  if (!hi->died_at)
+    return 0; /* manually marked dead */
+  if (hi->died_at + RESURRECT_INTERVAL <= curtime
+      || hi->died_at > curtime)
+    {
+      hi->dead = 0;
+      log_info ("resurrected host '%s'", hi->name);
+      return 1;
+    }
+  return 0;
+}
+
 /* Select a random host.  Consult TABLE which indices into the global
    hosttable.  Returns index into TABLE or -1 if no host could be
    selected.  */
@@ -212,11 +231,13 @@ select_random_host (int *table)
   int *tbl = NULL;
   size_t tblsize = 0;
   int pidx, idx;
+  time_t curtime;
 
+  curtime = gnupg_get_time ();
   /* We create a new table so that we randomly select only from
      currently alive hosts.  */
   for (idx=0; (pidx = table[idx]) != -1; idx++)
-    if (hosttable[pidx] && !hosttable[pidx]->dead)
+    if (hosttable[pidx] && host_is_alive (hosttable[pidx], curtime))
       {
         tblsize++;
         tbl = xtryrealloc(tbl, tblsize * sizeof *tbl);
@@ -392,6 +413,7 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
   gpg_error_t err = 0;
   hostinfo_t hi;
   int idx;
+  time_t curtime;
 
   *r_host = NULL;
   if (r_httpflags)
@@ -540,6 +562,7 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
         xfree (reftbl);
     }
 
+  curtime = gnupg_get_time ();
   hi = hosttable[idx];
   if (hi->pool)
     {
@@ -556,7 +579,7 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
       if (force_reselect)
         hi->poolidx = -1;
       else if (hi->poolidx >= 0 && hi->poolidx < hosttable_size
-               && hosttable[hi->poolidx] && hosttable[hi->poolidx]->dead)
+               && hosttable[hi->poolidx] && !host_is_alive (hosttable[hi->poolidx], curtime))
         hi->poolidx = -1;
 
       /* Select a host if needed.  */
@@ -580,7 +603,7 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
       assert (hi);
     }
 
-  if (hi->dead)
+  if (!host_is_alive (hi, curtime))
     {
       log_error ("host '%s' marked as dead\n", hi->name);
       if (r_poolname)
@@ -685,7 +708,8 @@ ks_hkp_mark_host (ctrl_t ctrl, const char *name, int alive)
 {
   gpg_error_t err = 0;
   hostinfo_t hi, hi2;
-  int idx, idx2, idx3, n;
+  int idx, idx2, idx3, n, is_alive;
+  time_t curtime;
 
   if (!name || !*name || !strcmp (name, "localhost"))
     return 0;
@@ -694,13 +718,15 @@ ks_hkp_mark_host (ctrl_t ctrl, const char *name, int alive)
   if (idx == -1)
     return gpg_error (GPG_ERR_NOT_FOUND);
 
+  curtime = gnupg_get_time ();
   hi = hosttable[idx];
-  if (alive && hi->dead)
+  is_alive = host_is_alive (hi, curtime);
+  if (alive && !is_alive)
     {
       hi->dead = 0;
       err = ks_printf_help (ctrl, "marking '%s' as alive", name);
     }
-  else if (!alive && !hi->dead)
+  else if (!alive && is_alive)
     {
       hi->dead = 1;
       hi->died_at = 0; /* Manually set dead.  */
@@ -732,14 +758,15 @@ ks_hkp_mark_host (ctrl_t ctrl, const char *name, int alive)
 
           hi2 = hosttable[n];
           if (!hi2)
-            ;
-          else if (alive && hi2->dead)
+            continue;
+          is_alive = host_is_alive (hi2, curtime);
+          if (alive && !is_alive)
             {
               hi2->dead = 0;
               err = ks_printf_help (ctrl, "marking '%s' as alive",
                                     hi2->name);
             }
-          else if (!alive && !hi2->dead)
+          else if (!alive && is_alive)
             {
               hi2->dead = 1;
               hi2->died_at = 0; /* Manually set dead. */
@@ -941,34 +968,6 @@ ks_hkp_resolve (ctrl_t ctrl, parsed_uri_t uri)
 }
 
 
-/* Housekeeping function called from the housekeeping thread.  It is
-   used to mark dead hosts alive so that they may be tried again after
-   some time.  */
-void
-ks_hkp_housekeeping (time_t curtime)
-{
-  int idx;
-  hostinfo_t hi;
-
-  for (idx=0; idx < hosttable_size; idx++)
-    {
-      hi = hosttable[idx];
-      if (!hi)
-        continue;
-      if (!hi->dead)
-        continue;
-      if (!hi->died_at)
-        continue; /* Do not resurrect manually shot hosts.  */
-      if (hi->died_at + RESURRECT_INTERVAL <= curtime
-          || hi->died_at > curtime)
-        {
-          hi->dead = 0;
-          log_info ("resurrected host '%s'", hi->name);
-        }
-    }
-}
-
-
 /* Send an HTTP request.  On success returns an estream object at
    R_FP.  HOSTPORTSTR is only used for diagnostics.  If HTTPHOST is
    not NULL it will be used as HTTP "Host" header.  If POST_CB is not
-- 
2.10.1




More information about the Gnupg-devel mailing list