chiark / gitweb /
Import gnupg2_2.1.18-8~deb9u1.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       |  1 -
25  dirmngr/ks-engine-hkp.c | 73 ++++++++++++++++++++++++-------------------------
26  3 files changed, 36 insertions(+), 41 deletions(-)
27
28 diff --git a/dirmngr/dirmngr.c b/dirmngr/dirmngr.c
29 index 061cfc300..a8be56fa4 100644
30 --- a/dirmngr/dirmngr.c
31 +++ b/dirmngr/dirmngr.c
32 @@ -1771,12 +1771,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 @@ -1789,7 +1787,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 35bc000fb..acd4c636d 100644
55 --- a/dirmngr/dirmngr.h
56 +++ b/dirmngr/dirmngr.h
57 @@ -193,7 +193,6 @@ const char* dirmngr_get_current_socket_name (void);
58  
59  
60  /*-- Various housekeeping functions.  --*/
61 -void ks_hkp_housekeeping (time_t curtime);
62  void ks_hkp_reload (void);
63  
64  
65 diff --git a/dirmngr/ks-engine-hkp.c b/dirmngr/ks-engine-hkp.c
66 index 57e7529f3..2b90441e2 100644
67 --- a/dirmngr/ks-engine-hkp.c
68 +++ b/dirmngr/ks-engine-hkp.c
69 @@ -203,6 +203,25 @@ host_in_pool_p (int *pool, int tblidx)
70  }
71  
72  
73 +static int
74 +host_is_alive (hostinfo_t hi, time_t curtime)
75 +{
76 +  if (!hi)
77 +    return 0;
78 +  if (!hi->dead)
79 +    return 1;
80 +  if (!hi->died_at)
81 +    return 0; /* manually marked dead */
82 +  if (hi->died_at + RESURRECT_INTERVAL <= curtime
83 +      || hi->died_at > curtime)
84 +    {
85 +      hi->dead = 0;
86 +      log_info ("resurrected host '%s'", hi->name);
87 +      return 1;
88 +    }
89 +  return 0;
90 +}
91 +
92  /* Select a random host.  Consult TABLE which indices into the global
93     hosttable.  Returns index into TABLE or -1 if no host could be
94     selected.  */
95 @@ -212,11 +231,13 @@ select_random_host (int *table)
96    int *tbl = NULL;
97    size_t tblsize = 0;
98    int pidx, idx;
99 +  time_t curtime;
100  
101 +  curtime = gnupg_get_time ();
102    /* We create a new table so that we randomly select only from
103       currently alive hosts.  */
104    for (idx=0; (pidx = table[idx]) != -1; idx++)
105 -    if (hosttable[pidx] && !hosttable[pidx]->dead)
106 +    if (hosttable[pidx] && host_is_alive (hosttable[pidx], curtime))
107        {
108          tblsize++;
109          tbl = xtryrealloc(tbl, tblsize * sizeof *tbl);
110 @@ -395,6 +416,7 @@ map_host (ctrl_t ctrl, const char *name, const char *srvtag, int force_reselect,
111    gpg_error_t err = 0;
112    hostinfo_t hi;
113    int idx;
114 +  time_t curtime;
115  
116    *r_host = NULL;
117    if (r_httpflags)
118 @@ -531,6 +553,7 @@ map_host (ctrl_t ctrl, const char *name, const char *srvtag, int force_reselect,
119          xfree (reftbl);
120      }
121  
122 +  curtime = gnupg_get_time ();
123    hi = hosttable[idx];
124    if (hi->pool)
125      {
126 @@ -547,7 +570,7 @@ map_host (ctrl_t ctrl, const char *name, const char *srvtag, int force_reselect,
127        if (force_reselect)
128          hi->poolidx = -1;
129        else if (hi->poolidx >= 0 && hi->poolidx < hosttable_size
130 -               && hosttable[hi->poolidx] && hosttable[hi->poolidx]->dead)
131 +               && hosttable[hi->poolidx] && !host_is_alive (hosttable[hi->poolidx], curtime))
132          hi->poolidx = -1;
133  
134        /* Select a host if needed.  */
135 @@ -599,7 +622,7 @@ map_host (ctrl_t ctrl, const char *name, const char *srvtag, int force_reselect,
136        free_dns_addrinfo (aibuf);
137      }
138  
139 -  if (hi->dead)
140 +  if (!host_is_alive (hi, curtime))
141      {
142        log_error ("host '%s' marked as dead\n", hi->name);
143        if (r_httphost)
144 @@ -704,7 +727,8 @@ ks_hkp_mark_host (ctrl_t ctrl, const char *name, int alive)
145  {
146    gpg_error_t err = 0;
147    hostinfo_t hi, hi2;
148 -  int idx, idx2, idx3, n;
149 +  int idx, idx2, idx3, n, is_alive;
150 +  time_t curtime;
151  
152    if (!name || !*name || !strcmp (name, "localhost"))
153      return 0;
154 @@ -713,13 +737,15 @@ ks_hkp_mark_host (ctrl_t ctrl, const char *name, int alive)
155    if (idx == -1)
156      return gpg_error (GPG_ERR_NOT_FOUND);
157  
158 +  curtime = gnupg_get_time ();
159    hi = hosttable[idx];
160 -  if (alive && hi->dead)
161 +  is_alive = host_is_alive (hi, curtime);
162 +  if (alive && !is_alive)
163      {
164        hi->dead = 0;
165        err = ks_printf_help (ctrl, "marking '%s' as alive", name);
166      }
167 -  else if (!alive && !hi->dead)
168 +  else if (!alive && is_alive)
169      {
170        hi->dead = 1;
171        hi->died_at = 0; /* Manually set dead.  */
172 @@ -751,14 +777,15 @@ ks_hkp_mark_host (ctrl_t ctrl, const char *name, int alive)
173  
174            hi2 = hosttable[n];
175            if (!hi2)
176 -            ;
177 -          else if (alive && hi2->dead)
178 +            continue;
179 +          is_alive = host_is_alive (hi2, curtime);
180 +          if (alive && !is_alive)
181              {
182                hi2->dead = 0;
183                err = ks_printf_help (ctrl, "marking '%s' as alive",
184                                      hi2->name);
185              }
186 -          else if (!alive && !hi2->dead)
187 +          else if (!alive && is_alive)
188              {
189                hi2->dead = 1;
190                hi2->died_at = 0; /* Manually set dead. */
191 @@ -974,34 +1001,6 @@ ks_hkp_resolve (ctrl_t ctrl, parsed_uri_t uri)
192  }
193  
194  
195 -/* Housekeeping function called from the housekeeping thread.  It is
196 -   used to mark dead hosts alive so that they may be tried again after
197 -   some time.  */
198 -void
199 -ks_hkp_housekeeping (time_t curtime)
200 -{
201 -  int idx;
202 -  hostinfo_t hi;
203 -
204 -  for (idx=0; idx < hosttable_size; idx++)
205 -    {
206 -      hi = hosttable[idx];
207 -      if (!hi)
208 -        continue;
209 -      if (!hi->dead)
210 -        continue;
211 -      if (!hi->died_at)
212 -        continue; /* Do not resurrect manually shot hosts.  */
213 -      if (hi->died_at + RESURRECT_INTERVAL <= curtime
214 -          || hi->died_at > curtime)
215 -        {
216 -          hi->dead = 0;
217 -          log_info ("resurrected host '%s'", hi->name);
218 -        }
219 -    }
220 -}
221 -
222 -
223  /* Reload (SIGHUP) action for this module.  We mark all host alive
224   * even those which have been manually shot.  */
225  void