1
0

zsh: ssh-agent persistence
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Konstantin Demin 2024-03-04 16:05:08 +03:00
parent 98deefe039
commit 5a9f0b08c0
Signed by: krd
GPG Key ID: 1F33CB0BA4731BC6

View File

@ -1,11 +1,15 @@
#!/bin/zsh #!/bin/zsh
z-ssh-agent() { z-ssh-agent() {
while [ -n "${SSH_AGENT_PID}" ] ; do while : ; do
[ -n "${SSH_AGENT_PID}" ] || break
z-proc-exists "${SSH_AGENT_PID}" || break z-proc-exists "${SSH_AGENT_PID}" || break
[ -n "${SSH_AUTH_SOCK}" ] || break
[ -S "${SSH_AUTH_SOCK}" ] || break
## don't bother with ssh agent socket if it already set ## don't bother with ssh agent socket if it already set
[ -z "${SSH_AUTH_SOCK}" ] || return 0 return 0
break break
done done
@ -25,7 +29,34 @@ z-ssh-agent() {
SSH_AUTH_SOCK="${sock_dir}/ssh-agent.sock" SSH_AUTH_SOCK="${sock_dir}/ssh-agent.sock"
fi fi
local pid_file
pid_file="${SSH_AUTH_SOCK:h}/ssh-agent.pid"
while : ; do
[ -s "${pid_file}" ] || break
SSH_AGENT_PID=$(cat "${pid_file}")
z-proc-exists "${SSH_AGENT_PID}" || break
[ -S "${SSH_AUTH_SOCK}" ] || break
## don't bother with ssh agent socket if it already set
export SSH_AGENT_PID SSH_AUTH_SOCK
return 0
done
unset SSH_AGENT_PID
{ {
eval "$(ssh-agent -s -a "${SSH_AUTH_SOCK}")" eval "$(ssh-agent -s -a "${SSH_AUTH_SOCK}")"
} >/dev/null } >/dev/null
while : ; do
[ -n "${SSH_AGENT_PID}" ] || break
[ -n "${SSH_AUTH_SOCK}" ] || break
[ -S "${SSH_AUTH_SOCK}" ] || break
echo "${SSH_AGENT_PID}" > "${pid_file}"
export SSH_AGENT_PID SSH_AUTH_SOCK
return 0
done
unset SSH_AGENT_PID SSH_AUTH_SOCK
return 1
} }