chiark / gitweb /
Import gnupg2_2.1.17-3.debian.tar.bz2
[gnupg2.git] / debian / patches / gpg-agent-idling / 0003-agent-Avoid-tight-timer-tick-when-possible.patch
1 From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
2 Date: Tue, 1 Nov 2016 00:14:10 -0400
3 Subject: agent: Avoid tight timer tick when possible.
4
5 * agent/gpg-agent.c (need_tick): Evaluate whether the short-phase
6 handle_tick() is needed.
7 (handle_connections): On each cycle of the select loop, adjust whether
8 we should call handle_tick() or not.
9 * agent/call-scd.c (start_scd): Call interrupt_main_thread_loop() once
10 the scdaemon thread context has started up.
11
12 --
13
14 With this change, an idle gpg-agent that has no scdaemon running only
15 wakes up once a minute (to check_own_socket).
16
17 Signed-off-by: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
18 ---
19  agent/call-scd.c  |  4 +++-
20  agent/gpg-agent.c | 25 ++++++++++++++++++++++++-
21  2 files changed, 27 insertions(+), 2 deletions(-)
22
23 diff --git a/agent/call-scd.c b/agent/call-scd.c
24 index ba59c1825..1ac0f6ba5 100644
25 --- a/agent/call-scd.c
26 +++ b/agent/call-scd.c
27 @@ -407,7 +407,9 @@ start_scd (ctrl_t ctrl)
28  
29    primary_scd_ctx = ctx;
30    primary_scd_ctx_reusable = 0;
31 -
32 +  /* notify the main loop that something has changed */
33 +  interrupt_main_thread_loop ();
34 +  
35   leave:
36    xfree (abs_homedir);
37    if (err)
38 diff --git a/agent/gpg-agent.c b/agent/gpg-agent.c
39 index 04a775c9b..1bfe0f3ae 100644
40 --- a/agent/gpg-agent.c
41 +++ b/agent/gpg-agent.c
42 @@ -2267,6 +2267,26 @@ create_directories (void)
43  }
44  
45  
46 +static int
47 +need_tick (void)
48 +{
49 +#ifdef HAVE_W32_SYSTEM
50 +  /* We do not know how to interrupt the select loop on Windows, so we
51 +     always need a short tick there. */
52 +  return 1;
53 +#else
54 +  /* if we were invoked like "gpg-agent cmd arg1 arg2" then we need to
55 +     watch our parent. */
56 +  if (parent_pid != (pid_t)(-1))
57 +    return 1;
58 +  /* if scdaemon is running, we need to check that it's alive */
59 +  if (agent_scd_check_running ())
60 +    return 1;
61 +  /* otherwise, nothing fine-grained to do. */
62 +  return 0;
63 +#endif /*HAVE_W32_SYSTEM*/
64 +}
65 +
66  
67  /* This is the worker for the ticker.  It is called every few seconds
68     and may only do fast operations. */
69 @@ -2325,7 +2345,7 @@ agent_sigusr2_action (void)
70  
71  #ifndef HAVE_W32_SYSTEM
72  /* The signal handler for this program.  It is expected to be run in
73 -   its own trhead and not in the context of a signal handler.  */
74 +   its own thread and not in the context of a signal handler.  */
75  static void
76  handle_signal (int signo)
77  {
78 @@ -2872,6 +2892,9 @@ handle_connections (gnupg_fd_t listen_fd,
79           thus a simple assignment is fine to copy the entire set.  */
80        read_fdset = fdset;
81  
82 +      /* avoid a fine-grained timer if we don't need one: */
83 +      timertbl[0].interval.tv_sec = need_tick () ? TIMERTICK_INTERVAL : 0;
84 +
85        /* loop through all timers, fire any registered functions, and
86           plan next timer to trigger */
87        npth_clock_gettime (&curtime);