chiark / gitweb /
Import gnupg2_2.1.17-3.debian.tar.bz2
[gnupg2.git] / patches / dirmngr-idling / 0002-dimrngr-Avoid-need-for-hkp-housekeeping.patch
1 From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
2 Date: Sat, 29 Oct 2016 02:00:50 -0400
3 Subject: dimrngr: Avoid need for hkp housekeeping.
4
5 * dirmngr/ks-engine-hkp.c (host_is_alive): New function.  Test whether
6 host is alive and resurrects it if it has been dead long enough.
7 (select_random_host, map_host, ks_hkp_mark_host): Use host_is_alive
8 instead of testing hostinfo_t->dead directly.
9 (ks_hkp_housekeeping): Remove function, no longer needed.
10 * dirmngr/dirmngr.c (housekeeping_thread): Remove call to
11 ks_hkp_housekeeping.
12
13 --
14
15 Rather than resurrecting hosts upon scheduled resurrection times, test
16 whether hosts should be resurrected as they're inspected for being
17 dead.  This removes the need for explicit housekeeping, and makes host
18 resurrections happen "just in time", rather than being clustered on
19 HOUSEKEEPING_INTERVAL seconds.
20
21 Signed-off-by: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
22 ---
23  dirmngr/dirmngr.c       |  3 --
24  dirmngr/dirmngr.h       |  4 ---
25  dirmngr/ks-engine-hkp.c | 73 ++++++++++++++++++++++++-------------------------
26  3 files changed, 36 insertions(+), 44 deletions(-)
27
28 diff --git a/dirmngr/dirmngr.c b/dirmngr/dirmngr.c
29 index 5ee589e93..130f74bc7 100644
30 --- a/dirmngr/dirmngr.c
31 +++ b/dirmngr/dirmngr.c
32 @@ -1773,12 +1773,10 @@ static void *
33  housekeeping_thread (void *arg)
34  {
35    static int sentinel;
36 -  time_t curtime;
37    struct server_control_s ctrlbuf;
38  
39    (void)arg;
40  
41 -  curtime = gnupg_get_time ();
42    if (sentinel)
43      {
44        log_info ("housekeeping is already going on\n");
45 @@ -1791,7 +1789,6 @@ housekeeping_thread (void *arg)
46    memset (&ctrlbuf, 0, sizeof ctrlbuf);
47    dirmngr_init_default_ctrl (&ctrlbuf);
48  
49 -  ks_hkp_housekeeping (curtime);
50    if (network_activity_seen)
51      {
52        network_activity_seen = 0;
53 diff --git a/dirmngr/dirmngr.h b/dirmngr/dirmngr.h
54 index 9a8787842..0469d55bc 100644
55 --- a/dirmngr/dirmngr.h
56 +++ b/dirmngr/dirmngr.h
57 @@ -192,10 +192,6 @@ void dirmngr_sighup_action (void);
58  const char* dirmngr_get_current_socket_name (void);
59  
60  
61 -/*-- Various housekeeping functions.  --*/
62 -void ks_hkp_housekeeping (time_t curtime);
63 -
64 -
65  /*-- server.c --*/
66  ldap_server_t get_ldapservers_from_ctrl (ctrl_t ctrl);
67  ksba_cert_t get_cert_local (ctrl_t ctrl, const char *issuer);
68 diff --git a/dirmngr/ks-engine-hkp.c b/dirmngr/ks-engine-hkp.c
69 index 2d1240bb3..6f1c2e8e0 100644
70 --- a/dirmngr/ks-engine-hkp.c
71 +++ b/dirmngr/ks-engine-hkp.c
72 @@ -203,6 +203,25 @@ host_in_pool_p (int *pool, int tblidx)
73  }
74  
75  
76 +static int
77 +host_is_alive (hostinfo_t hi, time_t curtime)
78 +{
79 +  if (!hi)
80 +    return 0;
81 +  if (!hi->dead)
82 +    return 1;
83 +  if (!hi->died_at)
84 +    return 0; /* manually marked dead */
85 +  if (hi->died_at + RESURRECT_INTERVAL <= curtime
86 +      || hi->died_at > curtime)
87 +    {
88 +      hi->dead = 0;
89 +      log_info ("resurrected host '%s'", hi->name);
90 +      return 1;
91 +    }
92 +  return 0;
93 +}
94 +
95  /* Select a random host.  Consult TABLE which indices into the global
96     hosttable.  Returns index into TABLE or -1 if no host could be
97     selected.  */
98 @@ -212,11 +231,13 @@ select_random_host (int *table)
99    int *tbl = NULL;
100    size_t tblsize = 0;
101    int pidx, idx;
102 +  time_t curtime;
103  
104 +  curtime = gnupg_get_time ();
105    /* We create a new table so that we randomly select only from
106       currently alive hosts.  */
107    for (idx=0; (pidx = table[idx]) != -1; idx++)
108 -    if (hosttable[pidx] && !hosttable[pidx]->dead)
109 +    if (hosttable[pidx] && host_is_alive (hosttable[pidx], curtime))
110        {
111          tblsize++;
112          tbl = xtryrealloc(tbl, tblsize * sizeof *tbl);
113 @@ -392,6 +413,7 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
114    gpg_error_t err = 0;
115    hostinfo_t hi;
116    int idx;
117 +  time_t curtime;
118  
119    *r_host = NULL;
120    if (r_httpflags)
121 @@ -538,6 +560,7 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
122          xfree (reftbl);
123      }
124  
125 +  curtime = gnupg_get_time ();
126    hi = hosttable[idx];
127    if (hi->pool)
128      {
129 @@ -554,7 +577,7 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
130        if (force_reselect)
131          hi->poolidx = -1;
132        else if (hi->poolidx >= 0 && hi->poolidx < hosttable_size
133 -               && hosttable[hi->poolidx] && hosttable[hi->poolidx]->dead)
134 +               && hosttable[hi->poolidx] && !host_is_alive (hosttable[hi->poolidx], curtime))
135          hi->poolidx = -1;
136  
137        /* Select a host if needed.  */
138 @@ -578,7 +601,7 @@ map_host (ctrl_t ctrl, const char *name, int force_reselect,
139        assert (hi);
140      }
141  
142 -  if (hi->dead)
143 +  if (!host_is_alive (hi, curtime))
144      {
145        log_error ("host '%s' marked as dead\n", hi->name);
146        if (r_poolname)
147 @@ -683,7 +706,8 @@ ks_hkp_mark_host (ctrl_t ctrl, const char *name, int alive)
148  {
149    gpg_error_t err = 0;
150    hostinfo_t hi, hi2;
151 -  int idx, idx2, idx3, n;
152 +  int idx, idx2, idx3, n, is_alive;
153 +  time_t curtime;
154  
155    if (!name || !*name || !strcmp (name, "localhost"))
156      return 0;
157 @@ -692,13 +716,15 @@ ks_hkp_mark_host (ctrl_t ctrl, const char *name, int alive)
158    if (idx == -1)
159      return gpg_error (GPG_ERR_NOT_FOUND);
160  
161 +  curtime = gnupg_get_time ();
162    hi = hosttable[idx];
163 -  if (alive && hi->dead)
164 +  is_alive = host_is_alive (hi, curtime);
165 +  if (alive && !is_alive)
166      {
167        hi->dead = 0;
168        err = ks_printf_help (ctrl, "marking '%s' as alive", name);
169      }
170 -  else if (!alive && !hi->dead)
171 +  else if (!alive && is_alive)
172      {
173        hi->dead = 1;
174        hi->died_at = 0; /* Manually set dead.  */
175 @@ -730,14 +756,15 @@ ks_hkp_mark_host (ctrl_t ctrl, const char *name, int alive)
176  
177            hi2 = hosttable[n];
178            if (!hi2)
179 -            ;
180 -          else if (alive && hi2->dead)
181 +            continue;
182 +          is_alive = host_is_alive (hi2, curtime);
183 +          if (alive && !is_alive)
184              {
185                hi2->dead = 0;
186                err = ks_printf_help (ctrl, "marking '%s' as alive",
187                                      hi2->name);
188              }
189 -          else if (!alive && !hi2->dead)
190 +          else if (!alive && is_alive)
191              {
192                hi2->dead = 1;
193                hi2->died_at = 0; /* Manually set dead. */
194 @@ -939,34 +966,6 @@ ks_hkp_resolve (ctrl_t ctrl, parsed_uri_t uri)
195  }
196  
197  
198 -/* Housekeeping function called from the housekeeping thread.  It is
199 -   used to mark dead hosts alive so that they may be tried again after
200 -   some time.  */
201 -void
202 -ks_hkp_housekeeping (time_t curtime)
203 -{
204 -  int idx;
205 -  hostinfo_t hi;
206 -
207 -  for (idx=0; idx < hosttable_size; idx++)
208 -    {
209 -      hi = hosttable[idx];
210 -      if (!hi)
211 -        continue;
212 -      if (!hi->dead)
213 -        continue;
214 -      if (!hi->died_at)
215 -        continue; /* Do not resurrect manually shot hosts.  */
216 -      if (hi->died_at + RESURRECT_INTERVAL <= curtime
217 -          || hi->died_at > curtime)
218 -        {
219 -          hi->dead = 0;
220 -          log_info ("resurrected host '%s'", hi->name);
221 -        }
222 -    }
223 -}
224 -
225 -
226  /* Send an HTTP request.  On success returns an estream object at
227     R_FP.  HOSTPORTSTR is only used for diagnostics.  If HTTPHOST is
228     not NULL it will be used as HTTP "Host" header.  If POST_CB is not