This variable needs to be protected with a mutex. We don't want anyone to access it directly. So introduce a helper function adjust_agent_active_connections to increment/decrement it, and replace all direct accesses.
Also replace a couple of direct read-only accesses with calls to the existing function get_agent_active_connection_count. Signed-off-by: Ian Jackson <ijack...@chiark.greenend.org.uk> --- agent/gpg-agent.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/agent/gpg-agent.c b/agent/gpg-agent.c index 0e53549..8ca6d92 100644 --- a/agent/gpg-agent.c +++ b/agent/gpg-agent.c @@ -1995,6 +1995,13 @@ get_agent_active_connection_count (void) return active_connections; } +/* Increment/decrement the number of active connections. */ +static void +adjust_agent_active_connections (int delta) +{ + active_connections += delta; +} + /* Under W32, this function returns the handle of the scdaemon notification event. Calling it the first time creates that @@ -2378,7 +2385,7 @@ handle_signal (int signo) log_info ("SIGTERM received - shutting down ...\n"); else log_info ("SIGTERM received - still %i open connections\n", - active_connections); + get_agent_active_connection_count()); shutdown_pending++; if (shutdown_pending > 2) { @@ -2613,7 +2620,7 @@ putty_message_thread (void *arg) static void * do_start_connection_thread (ctrl_t ctrl) { - active_connections++; + adjust_agent_active_connections(+1); agent_init_default_ctrl (ctrl); if (opt.verbose && !DBG_IPC) log_info (_("handler 0x%lx for fd %d started\n"), @@ -2626,7 +2633,7 @@ do_start_connection_thread (ctrl_t ctrl) agent_deinit_default_ctrl (ctrl); xfree (ctrl); - active_connections--; + adjust_agent_active_connections(-1); return NULL; } @@ -2693,7 +2700,7 @@ start_connection_thread_ssh (void *arg) if (check_nonce (ctrl, &socket_nonce_ssh)) return NULL; - active_connections++; + adjust_agent_active_connections(+1); agent_init_default_ctrl (ctrl); if (opt.verbose) log_info (_("ssh handler 0x%lx for fd %d started\n"), @@ -2706,7 +2713,7 @@ start_connection_thread_ssh (void *arg) agent_deinit_default_ctrl (ctrl); xfree (ctrl); - active_connections--; + adjust_agent_active_connections(-1); return NULL; } @@ -2869,7 +2876,7 @@ handle_connections (gnupg_fd_t listen_fd, /* Shutdown test. */ if (shutdown_pending) { - if (active_connections == 0) + if (get_agent_active_connection_count() == 0) break; /* ready */ /* Do not accept new connections but keep on running the -- 2.11.0