711e96b1a277b1d5b9771c2eec19fa34f796839b

Author
Olaf Conradi <olaf@conradi.org>
Committer
Olaf Conradi <olaf@conradi.org>
Date

Message

Prevent starting multiple gpg-agents

Don't just overwrite the environment. First check for a running agent (an
x-session might have one running). If no agent is found, source the
environment and check again using those settings. If again no agent is
found, start a new instance.

Diff

 1diff --git a/plugins/gpg-agent/gpg-agent.plugin.zsh b/plugins/gpg-agent/gpg-agent.plugin.zsh
 2index 109af44c8d5d6be7d4ec47fdc6fe1e40cd8aeb58..b8271187287be1e8bb9e13fb6cacab9c67b7d408 100644
 3--- a/plugins/gpg-agent/gpg-agent.plugin.zsh
 4+++ b/plugins/gpg-agent/gpg-agent.plugin.zsh
 5@@ -14,16 +14,24 @@ function start_agent_withssh {
 6     export SSH_AGENT_PID
 7 }
 8 
 9-# source settings of old agent, if applicable
10-if [ -f "${GPG_ENV}" ]; then
11-  . ${GPG_ENV} > /dev/null
12-fi
13+# check if another agent is running
14+if ! gpg-connect-agent --quiet /bye > /dev/null 2> /dev/null; then
15+    # source settings of old agent, if applicable
16+    if [ -f "${GPG_ENV}" ]; then
17+        . ${GPG_ENV} > /dev/null
18+    fi
19 
20-# check for existing ssh-agent
21-if ssh-add -l > /dev/null 2> /dev/null; then
22-    start_agent_nossh;
23-else
24-    start_agent_withssh;
25+    # check again if another agent is running using the newly sources settings
26+    if ! gpg-connect-agent --quiet /bye > /dev/null 2> /dev/null; then
27+        # check for existing ssh-agent
28+        if ssh-add -l > /dev/null 2> /dev/null; then
29+            # ssh-agent running, start gpg-agent without ssh support
30+            start_agent_nossh;
31+        else
32+            # otherwise start gpg-agent with ssh support
33+            start_agent_withssh;
34+        fi
35+    fi
36 fi
37 
38 GPG_TTY=$(tty)