summaryrefslogtreecommitdiff
path: root/plugins/ssh-agent/ssh-agent.plugin.zsh
diff options
context:
space:
mode:
authorNuno Goncalves <nunojpg@gmail.com>2021-06-12 04:03:25 +0200
committerGitHub <noreply@github.com>2021-06-11 19:03:25 -0700
commita206271460ce49e842b1b410c0424b8c9a0a3d14 (patch)
tree294a3c55b1fdfb479d67dd247d4103b3bf2704ed /plugins/ssh-agent/ssh-agent.plugin.zsh
parent02ce2c4a2f563c4e092410fe3ddc8b29b6de7fdc (diff)
downloadzsh-a206271460ce49e842b1b410c0424b8c9a0a3d14.tar.gz
zsh-a206271460ce49e842b1b410c0424b8c9a0a3d14.tar.bz2
zsh-a206271460ce49e842b1b410c0424b8c9a0a3d14.zip
ssh-agent: improvements (#6309)
* ssh-agent: lock this script with a mkdir style mutex This script is a kind of singleton pattern and is not reentrant. If several shells are oppened in a fast sequence, then several independent ssh-agents would be created, which is not acceptable. A mutex is required. Signed-off-by: Nuno Goncalves <nunojpg@gmail.com> * ssh-agent: only start agent if .ssh dir exists To use the same profile system-wide, it might happen that the .ssh directory does not exist (typically $HOME/.ssh/). This would trigger a error. Creating the directory would be a option, but it usually will not make sense to do so because it means the user doesn't have ssh keys or config. Signed-off-by: Nuno Goncalves <nunojpg@gmail.com> * ssh-agent: adds lazy option to disable key loading on start Option is documented on updated README.md Signed-off-by: Nuno Goncalves <nunojpg@gmail.com> * ssh-agent: simplify agent-forwarding checking Signed-off-by: Nuno Goncalves <nunojpg@gmail.com> Co-authored-by: Robby Russell <robby@planetargon.com>
Diffstat (limited to 'plugins/ssh-agent/ssh-agent.plugin.zsh')
-rw-r--r--plugins/ssh-agent/ssh-agent.plugin.zsh29
1 files changed, 21 insertions, 8 deletions
diff --git a/plugins/ssh-agent/ssh-agent.plugin.zsh b/plugins/ssh-agent/ssh-agent.plugin.zsh
index d45406f63..494cf1393 100644
--- a/plugins/ssh-agent/ssh-agent.plugin.zsh
+++ b/plugins/ssh-agent/ssh-agent.plugin.zsh
@@ -1,4 +1,16 @@
-typeset _agent_forwarding _ssh_env_cache
+lockdir=/tmp/oh-my-zsh-ssh-agent.lock
+
+while true; do
+ if mkdir "$lockdir" 2>/dev/null
+ then # directory did not exist, but was created successfully
+ trap 'rm -rf "$lockdir"' 0 # remove directory when script finishes
+ break # continue with script
+ else
+ sleep 0.1 # sleep for 0.2 and try again
+ fi
+done
+
+typeset _ssh_env_cache
function _start_agent() {
local lifetime
@@ -56,10 +68,7 @@ function _add_identities() {
# Get the filename to store/lookup the environment from
_ssh_env_cache="$HOME/.ssh/environment-$SHORT_HOST"
-# test if agent-forwarding is enabled
-zstyle -b :omz:plugins:ssh-agent agent-forwarding _agent_forwarding
-
-if [[ $_agent_forwarding == "yes" && -n "$SSH_AUTH_SOCK" ]]; then
+if zstyle -t :omz:plugins:ssh-agent agent-forwarding && [[ -n "$SSH_AUTH_SOCK" ]]; then
# Add a nifty symlink for screen/tmux if agent forwarding
[[ -L $SSH_AUTH_SOCK ]] || ln -sf "$SSH_AUTH_SOCK" /tmp/ssh-agent-$USERNAME-screen
elif [[ -f "$_ssh_env_cache" ]]; then
@@ -73,12 +82,16 @@ elif [[ -f "$_ssh_env_cache" ]]; then
ps $FILTER | grep ssh-agent | grep -q $SSH_AGENT_PID || {
_start_agent
}
-else
+elif [[ -d $HOME/.ssh ]]; then
_start_agent
fi
-_add_identities
+if ! zstyle -t :omz:plugins:ssh-agent lazy; then
+ _add_identities
+fi
# tidy up after ourselves
-unset _agent_forwarding _ssh_env_cache
+unset _ssh_env_cache
unfunction _start_agent _add_identities
+
+rm -rf "$lockdir"