bin/mdw-sbuild-server: Keep track of the target CPU architectures.
[profile] / bin / start-ssh-agent
index 30c0bc7..bfb2608 100755 (executable)
@@ -29,26 +29,45 @@ case $chosen,$style in
     ;;
 esac
 
-### Should I start a new agent?
-case "$force,$SSH_AUTH_SOCK" in
-  t,* | nil,)
-    foundp=nil
-    ;;
-  *)
-    foundp=t
-    set +e; ssh-add -l >/dev/null 2>&1; rc=$?; set -e
-    [ $rc -ge 2 ] && foundp=nil
+## Some useful variables.
+hostname=${HOST-$(hostname)}
+user=${USER-${LOGNAME-$(id -un)}} uid=${UID-$(id -u)}
+dir=$TMPDIR/.ssh-agent.$hostname.$user
+socket=$dir/sock; pid=$dir/pid
+export SSH_AUTH_SOCK
+
+## Should I start a new agent?
+foundp=nil
+case $force in
+  nil)
+    case ${SSH_AUTH_SOCK+t} in
+      t)
+       set +e; ssh-add -l >/dev/null 2>&1; rc=$?; set -e
+       if [ $rc -lt 2 ]; then foundp=t; fi
+       ;;
+    esac
+    case $foundp in
+      t) ;;
+      *)
+       for i in \
+         "$HOME/.cache/keyring-"*"/ssh" \
+         "/run/user/$uid/keyring/ssh" \
+         "$socket"
+       do
+         SSH_AUTH_SOCK=$i
+         set +e; ssh-add -l >/dev/null 2>&1; rc=$?; set -e
+         if [ $rc -lt 2 ]; then foundp=t; break; fi
+       done
+       ;;
+    esac
     ;;
 esac
 
 ### If so, do that
 case $foundp in
   nil)
-    hostname=${HOST-$(hostname)}
-    user=${USER-${LOGNAME-$(whoami)}}
-    dir=$TMPDIR/.ssh-agent.$hostname.$user; socket=$dir/sock; pid=$dir/pid
     mkdir -p -m700 "$dir"
-    SSH_AUTH_SOCK=$socket; export SSH_AUTH_SOCK
+    SSH_AUTH_SOCK=$socket
     set +e; ssh-add -l >/dev/null 2>&1; rc=$?; set -e
     if [ $rc -ge 2 ]; then
       if [ -f "$pid" ]; then
@@ -57,7 +76,7 @@ case $foundp in
       rm -f "$socket" "$pid"
       (cd /; exec ssh-agent -d -a "$socket" >/dev/null 2>&1)&
       echo $! >"$pid"
-      SSH_AUTH_SOCK=$socket; export SSH_AUTH_SOCK
+      SSH_AUTH_SOCK=$socket
     fi
     ;;
 esac