From 4670837f7c9d027b8724c4061219ee951dc8cd6e Mon Sep 17 00:00:00 2001 From: Dan Nicholson Date: Thu, 10 Jan 2019 13:49:17 -0600 Subject: [PATCH] lib/gpg: Add helper to kill GPG agent With GnuPG 2, any time you do basically any operation, a gpg-agent will be spawned for the GPG home directory in use. The classic way to kill a gpg-agent is to use `gpg-connect-agent` and send the `killagent` command as is done in libtest.sh. Closes: #1799 Approved by: cgwalters --- src/libotutil/ot-gpg-utils.c | 26 ++++++++++++++++++++++++++ src/libotutil/ot-gpg-utils.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/src/libotutil/ot-gpg-utils.c b/src/libotutil/ot-gpg-utils.c index cc5b0ae4..cf5ce3ea 100644 --- a/src/libotutil/ot-gpg-utils.c +++ b/src/libotutil/ot-gpg-utils.c @@ -437,3 +437,29 @@ ot_gpgme_new_ctx (const char *homedir, return g_steal_pointer (&context); } + +void +ot_gpgme_kill_agent (const char *homedir) +{ + g_return_if_fail (homedir != NULL); + + /* Run gpg-connect-agent killagent /bye */ + g_autoptr(GError) local_error = NULL; + g_autoptr(GSubprocess) proc = g_subprocess_new(G_SUBPROCESS_FLAGS_STDOUT_SILENCE, + &local_error, + "gpg-connect-agent", + "--homedir", + homedir, + "killagent", + "/bye", + NULL); + if (proc == NULL) { + g_debug ("Spawning gpg-connect-agent failed: %s", local_error->message); + return; + } + if (!g_subprocess_wait_check (proc, NULL, &local_error)) { + g_debug ("Killing GPG agent with gpg-connect-agent failed: %s", + local_error->message); + return; + } +} diff --git a/src/libotutil/ot-gpg-utils.h b/src/libotutil/ot-gpg-utils.h index 65ae55e4..e8a240b5 100644 --- a/src/libotutil/ot-gpg-utils.h +++ b/src/libotutil/ot-gpg-utils.h @@ -46,4 +46,6 @@ gpgme_data_t ot_gpgme_data_output (GOutputStream *output_stream); gpgme_ctx_t ot_gpgme_new_ctx (const char *homedir, GError **error); +void ot_gpgme_kill_agent (const char *homedir); + G_END_DECLS