From 5a9f0b08c08929dc3253df843571ab90df418286 Mon Sep 17 00:00:00 2001 From: Konstantin Demin Date: Mon, 4 Mar 2024 16:05:08 +0300 Subject: [PATCH] zsh: ssh-agent persistence --- .config/zsh/rc/ssh-agent.zsh | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/.config/zsh/rc/ssh-agent.zsh b/.config/zsh/rc/ssh-agent.zsh index fa0f8d1..0fc6383 100644 --- a/.config/zsh/rc/ssh-agent.zsh +++ b/.config/zsh/rc/ssh-agent.zsh @@ -1,11 +1,15 @@ #!/bin/zsh z-ssh-agent() { - while [ -n "${SSH_AGENT_PID}" ] ; do + while : ; do + [ -n "${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 - [ -z "${SSH_AUTH_SOCK}" ] || return 0 + return 0 break done @@ -25,7 +29,34 @@ z-ssh-agent() { SSH_AUTH_SOCK="${sock_dir}/ssh-agent.sock" 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}")" } >/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 }