X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=.profile.d%2Fssh.bashrc;h=5f3c1a5e27be10cd1885024326b163815197344c;hb=95120cbd748ebe21f1cf944911632f9afdad1958;hp=4674bfc34892ba83d808d63f8db5c52040c00d57;hpb=aeda6a0753ac462d2a9d7da89d58ff5f3b61c2eb;p=profile.git diff --git a/.profile.d/ssh.bashrc b/.profile.d/ssh.bashrc index 4674bfc..5f3c1a5 100644 --- a/.profile.d/ssh.bashrc +++ b/.profile.d/ssh.bashrc @@ -2,6 +2,10 @@ SOCKET=~/.ssh/agent +function get_agent_pid() { + ps waux | grep -- ^$USER\ .\*ssh-agent\ -a\ .\*\ -s | grep -v grep | awk '{ print $2 }' | head -n 1 +} + if [ ! -z "$SSH_CLIENT" ]; then # Set display if we aren't already forwarding X11 if [ "$DISPLAY" = "" ]; then @@ -12,13 +16,24 @@ fi # Try to hook up with already running ssh-agent. if [ $UID -gt 0 ]; then # Not all OSes support ``ps -wu''. - RUNNING_AGENT="$(ps waux | grep -- ^$USER\ .\*ssh-agent\ -a\ .\*\ -s | grep -v grep | awk '{ print $2 }' | head -n 1)" + RUNNING_AGENT="$(get_agent_pid)" + + # Has the socket gone away? + if [ ! -z "$RUNNING_AGENT" -a ! -S "$SOCKET" ]; then + if kill $RUNNING_AGENT; then + RUNNING_AGENT="$(get_agent_pid)" + else + echo >&2 "$SOCKET has gone away but agent is running as PID $RUNNING_AGENT." + fi + fi + if [ -z "$SSH_AGENT_PID" -o ! "$SSH_AGENT_PID" = "$RUNNING_AGENT" ]; then export SSH_AGENT_PID="$RUNNING_AGENT" if [ "$SSH_AGENT_PID" = "" ]; then unset SSH_AGENT_PID unset SSH_AUTH_SOCK # Start ssh-agent up then. + rm -f "$SOCKET" eval $(/usr/bin/ssh-agent -a "$SOCKET" -s) trap "kill $SSH_AGENT_PID" 0 ssh-add @@ -30,3 +45,4 @@ if [ $UID -gt 0 ]; then fi unset SOCKET +unset get_agent_pid