agent: Better interaction between main loop and cache expiration.

* agent/agent.h (agent_cache_housekeeping): Remove.
(agent_cache_expiration): New.
* agent/cache.c (agent_cache_housekeeping): Remove.
(agent_cache_expiration): New.
* agent/gpg-agent.c (TIMERTICK_INTERVAL): Remove.
(handle_tick): Remove.
(handle_connections): Call agent_cache_expiration and use the timeout
value determined by the call.

--

GnuPG-bug-id: 6681
Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>
This commit is contained in:
NIIBE Yutaka 2023-08-30 14:48:56 +09:00
parent 7025375e8b
commit 76a2f18028
No known key found for this signature in database
GPG Key ID: 640114AF89DE6054
3 changed files with 44 additions and 47 deletions

View File

@ -515,7 +515,7 @@ int agent_clear_passphrase (ctrl_t ctrl,
/*-- cache.c --*/ /*-- cache.c --*/
void initialize_module_cache (void); void initialize_module_cache (void);
void deinitialize_module_cache (void); void deinitialize_module_cache (void);
void agent_cache_housekeeping (void); struct timespec *agent_cache_expiration (void);
void agent_flush_cache (int pincache_only); void agent_flush_cache (int pincache_only);
int agent_put_cache (ctrl_t ctrl, const char *key, cache_mode_t cache_mode, int agent_put_cache (ctrl_t ctrl, const char *key, cache_mode_t cache_mode,
const char *data, int ttl); const char *data, int ttl);

View File

@ -270,23 +270,46 @@ housekeeping (void)
} }
void #define TIMERTICK_INTERVAL (4)
agent_cache_housekeeping (void) struct timespec *
agent_cache_expiration (void)
{ {
static struct timespec abstime;
static struct timespec timeout;
static int initialized = 0;
struct timespec curtime;
int res; int res;
if (DBG_CACHE) if (!initialized)
log_debug ("agent_cache_housekeeping\n"); {
initialized = 1;
npth_clock_gettime (&abstime);
abstime.tv_sec += TIMERTICK_INTERVAL;
}
res = npth_mutex_lock (&cache_lock); npth_clock_gettime (&curtime);
if (res) if (!(npth_timercmp (&curtime, &abstime, <)))
log_fatal ("failed to acquire cache mutex: %s\n", strerror (res)); {
/* Timeout. */
npth_clock_gettime (&abstime);
abstime.tv_sec += TIMERTICK_INTERVAL;
housekeeping (); if (DBG_CACHE)
log_debug ("agent_cache_housekeeping\n");
res = npth_mutex_unlock (&cache_lock); res = npth_mutex_lock (&cache_lock);
if (res) if (res)
log_fatal ("failed to release cache mutex: %s\n", strerror (res)); log_fatal ("failed to acquire cache mutex: %s\n", strerror (res));
housekeeping ();
res = npth_mutex_unlock (&cache_lock);
if (res)
log_fatal ("failed to release cache mutex: %s\n", strerror (res));
}
npth_timersub (&abstime, &curtime, &timeout);
return &timeout;
} }

View File

@ -341,14 +341,12 @@ static struct debug_flags_s debug_flags [] =
#define MIN_PASSPHRASE_NONALPHA (1) #define MIN_PASSPHRASE_NONALPHA (1)
#define MAX_PASSPHRASE_DAYS (0) #define MAX_PASSPHRASE_DAYS (0)
/* The timer tick used for housekeeping stuff. Note that on Windows /* CHECK_OWN_SOCKET_INTERVAL defines how often we check our own socket
* we use a SetWaitableTimer seems to signal earlier than about 2 * in standard socket mode. If that value is 0 we don't check at all.
* seconds. Thus we use 4 seconds on all platforms. * Values is in seconds. */
* CHECK_OWN_SOCKET_INTERVAL defines how often we check
* our own socket in standard socket mode. If that value is 0 we
* don't check at all. All values are in seconds. */
#define TIMERTICK_INTERVAL (4)
#define CHECK_OWN_SOCKET_INTERVAL (60) #define CHECK_OWN_SOCKET_INTERVAL (60)
/* CHECK_PROBLEMS_INTERFAL defines how often we check the existence of
* parent process and homedir. Value is in seconds. */
#define CHECK_PROBLEMS_INTERVAL (4) #define CHECK_PROBLEMS_INTERVAL (4)
/* Flag indicating that the ssh-agent subsystem has been enabled. */ /* Flag indicating that the ssh-agent subsystem has been enabled. */
@ -2446,17 +2444,6 @@ create_directories (void)
} }
/* This is the worker for the ticker. It is called every few seconds
and may only do fast operations. */
static void
handle_tick (void)
{
/* Need to check for expired cache entries. */
agent_cache_housekeeping ();
}
/* A global function which allows us to call the reload stuff from /* A global function which allows us to call the reload stuff from
other places too. This is only used when build for W32. */ other places too. This is only used when build for W32. */
void void
@ -2990,9 +2977,7 @@ handle_connections (gnupg_fd_t listen_fd,
gnupg_fd_t fd; gnupg_fd_t fd;
int nfd; int nfd;
int saved_errno; int saved_errno;
struct timespec abstime; struct timespec *tp;
struct timespec curtime;
struct timespec timeout;
#ifdef HAVE_W32_SYSTEM #ifdef HAVE_W32_SYSTEM
HANDLE events[3]; HANDLE events[3];
unsigned int events_set; unsigned int events_set;
@ -3156,9 +3141,6 @@ handle_connections (gnupg_fd_t listen_fd,
listentbl[2].l_fd = listen_fd_browser; listentbl[2].l_fd = listen_fd_browser;
listentbl[3].l_fd = listen_fd_ssh; listentbl[3].l_fd = listen_fd_ssh;
npth_clock_gettime (&abstime);
abstime.tv_sec += TIMERTICK_INTERVAL;
for (;;) for (;;)
{ {
/* Shutdown test. */ /* Shutdown test. */
@ -3199,25 +3181,17 @@ handle_connections (gnupg_fd_t listen_fd,
nfd = pipe_fd[0]; nfd = pipe_fd[0];
#endif #endif
npth_clock_gettime (&curtime); tp = agent_cache_expiration ();
if (!(npth_timercmp (&curtime, &abstime, <)))
{
/* Timeout. */
handle_tick ();
npth_clock_gettime (&abstime);
abstime.tv_sec += TIMERTICK_INTERVAL;
}
npth_timersub (&abstime, &curtime, &timeout);
#ifndef HAVE_W32_SYSTEM #ifndef HAVE_W32_SYSTEM
ret = npth_pselect (nfd+1, &read_fdset, NULL, NULL, &timeout, ret = npth_pselect (nfd+1, &read_fdset, NULL, NULL, tp,
npth_sigev_sigmask ()); npth_sigev_sigmask ());
saved_errno = errno; saved_errno = errno;
while (npth_sigev_get_pending (&signo)) while (npth_sigev_get_pending (&signo))
handle_signal (signo); handle_signal (signo);
#else #else
ret = npth_eselect (nfd+1, &read_fdset, NULL, NULL, &timeout, ret = npth_eselect (nfd+1, &read_fdset, NULL, NULL, tp,
events, &events_set); events, &events_set);
saved_errno = errno; saved_errno = errno;