1 From: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
2 Date: Mon, 31 Oct 2016 21:27:36 -0400
3 Subject: agent: Create framework of scheduled timers.
5 agent/gpg-agent.c (handle_tick): Remove intermittent call to
7 (tv_is_set): Add inline helper function for readability.
8 (handle_connections) Create general table of pending scheduled
13 handle_tick() does fine-grained, rapid activity. check_own_socket()
14 is supposed to happen at a different interval.
16 Mixing the two of them makes it a requirement that one interval be a
17 multiple of the other, which isn't ideal if there are different delay
18 strategies that we might want in the future.
20 Creating an extensible regular timer framework in handle_connections
21 should make it possible to have any number of cadenced timers fire
22 regularly, without requiring that they happen in cadences related to
25 It should also make it possible to dynamically change the cadence of
26 any regularly-scheduled timeout.
28 Signed-off-by: Daniel Kahn Gillmor <dkg@fifthhorseman.net>
30 agent/gpg-agent.c | 87 ++++++++++++++++++++++++++++++++++++-------------------
31 1 file changed, 58 insertions(+), 29 deletions(-)
33 diff --git a/agent/gpg-agent.c b/agent/gpg-agent.c
34 index f5ecea544..82c8ae062 100644
35 --- a/agent/gpg-agent.c
36 +++ b/agent/gpg-agent.c
37 @@ -2270,11 +2270,6 @@ create_directories (void)
41 - static time_t last_minute;
44 - last_minute = time (NULL);
46 /* Check whether the scdaemon has died and cleanup in this case. */
47 agent_scd_check_aliveness ();
49 @@ -2293,16 +2288,6 @@ handle_tick (void)
52 #endif /*HAVE_W32_SYSTEM*/
54 - /* Code to be run from time to time. */
55 -#if CHECK_OWN_SOCKET_INTERVAL > 0
56 - if (last_minute + CHECK_OWN_SOCKET_INTERVAL <= time (NULL))
58 - check_own_socket ();
59 - last_minute = time (NULL);
66 @@ -2699,6 +2684,15 @@ start_connection_thread_ssh (void *arg)
70 +/* helper function for readability: test whether a given struct
71 + timespec is set to all-zeros */
73 +tv_is_set (struct timespec tv)
75 + return tv.tv_sec || tv.tv_nsec;
79 /* Connection handler loop. Wait for connection requests and spawn a
80 thread after accepting a connection. */
82 @@ -2716,9 +2710,11 @@ handle_connections (gnupg_fd_t listen_fd,
87 struct timespec abstime;
88 struct timespec curtime;
89 struct timespec timeout;
90 + struct timespec *select_timeout;
91 #ifdef HAVE_W32_SYSTEM
93 unsigned int events_set;
94 @@ -2734,6 +2730,14 @@ handle_connections (gnupg_fd_t listen_fd,
95 { "browser", start_connection_thread_browser },
96 { "ssh", start_connection_thread_ssh }
99 + struct timespec interval;
100 + void (*func) (void);
101 + struct timespec next;
103 + { { TIMERTICK_INTERVAL, 0 }, handle_tick },
104 + { { CHECK_OWN_SOCKET_INTERVAL, 0 }, check_own_socket }
108 ret = npth_attr_init(&tattr);
109 @@ -2823,9 +2827,6 @@ handle_connections (gnupg_fd_t listen_fd,
110 listentbl[2].l_fd = listen_fd_browser;
111 listentbl[3].l_fd = listen_fd_ssh;
113 - npth_clock_gettime (&abstime);
114 - abstime.tv_sec += TIMERTICK_INTERVAL;
119 @@ -2854,18 +2855,47 @@ handle_connections (gnupg_fd_t listen_fd,
120 thus a simple assignment is fine to copy the entire set. */
123 + /* loop through all timers, fire any registered functions, and
124 + plan next timer to trigger */
125 npth_clock_gettime (&curtime);
126 - if (!(npth_timercmp (&curtime, &abstime, <)))
130 - npth_clock_gettime (&abstime);
131 - abstime.tv_sec += TIMERTICK_INTERVAL;
133 - npth_timersub (&abstime, &curtime, &timeout);
134 + abstime.tv_sec = abstime.tv_nsec = 0;
135 + for (idx=0; idx < DIM(timertbl); idx++)
137 + /* schedule any unscheduled timers */
138 + if ((!tv_is_set (timertbl[idx].next)) && tv_is_set (timertbl[idx].interval))
139 + npth_timeradd (&timertbl[idx].interval, &curtime, &timertbl[idx].next);
140 + /* if a timer is due, fire it ... */
141 + if (tv_is_set (timertbl[idx].next))
143 + if (!(npth_timercmp (&curtime, &timertbl[idx].next, <)))
145 + timertbl[idx].func ();
146 + npth_clock_gettime (&curtime);
147 + /* ...and reschedule it, if desired: */
148 + if (tv_is_set (timertbl[idx].interval))
149 + npth_timeradd (&timertbl[idx].interval, &curtime, &timertbl[idx].next);
151 + timertbl[idx].next.tv_sec = timertbl[idx].next.tv_nsec = 0;
154 + /* accumulate next timer to come due in abstime: */
155 + if (tv_is_set (timertbl[idx].next) &&
156 + ((!tv_is_set (abstime)) ||
157 + (npth_timercmp (&abstime, &timertbl[idx].next, >))))
158 + abstime = timertbl[idx].next;
160 + /* choose a timeout for the select loop: */
161 + if (tv_is_set (abstime))
163 + npth_timersub (&abstime, &curtime, &timeout);
164 + select_timeout = &timeout;
167 + select_timeout = NULL;
170 #ifndef HAVE_W32_SYSTEM
171 - ret = npth_pselect (nfd+1, &read_fdset, NULL, NULL, &timeout,
172 + ret = npth_pselect (nfd+1, &read_fdset, NULL, NULL, select_timeout,
173 npth_sigev_sigmask ());
176 @@ -2875,7 +2905,7 @@ handle_connections (gnupg_fd_t listen_fd,
177 handle_signal (signo);
180 - ret = npth_eselect (nfd+1, &read_fdset, NULL, NULL, &timeout,
181 + ret = npth_eselect (nfd+1, &read_fdset, NULL, NULL, select_timeout,
182 events, &events_set);
185 @@ -2898,7 +2928,6 @@ handle_connections (gnupg_fd_t listen_fd,
187 if (!shutdown_pending)