Prevent gdm(8) lockups on non-interactive consoles.
[nethome.git] / bin / ssh-agent-spawn
index 88fa34c..bc01a99 100644 (file)
@@ -13,10 +13,24 @@ if lockfile -1 -r 3 -l 10 -s 1 $agenteval.lock;then
                source $agenteval >/dev/null
        fi
        rm -f $agenteval.lock
-       for key in $HOME/.ssh/id_dsa;do
-               (ssh-add -l|grep -qw "$key") ||
-                               # "2>/dev/null" to prevent: Identity added: .ssh/id_dsa (.ssh/id_dsa)
-                               ssh-add "$key" 2>/dev/null
-       done
 fi
+for key in $HOME/.ssh/id_dsa;do
+       # Prevent gdm(8) lockups on non-interactive consoles.
+       tty -s || continue
+       (ssh-add -l|grep -qw "$key") && continue
+       if lockfile -! -1 -r 0 $agenteval.input.lock 2>/dev/null;then
+               echo >&2 -n "Locking: $agenteval.input.lock ... "
+               lockfile -1 -r -1 $agenteval.input.lock
+               echo >&2 "locked"
+       fi
+       # Recheck identity existence after the lock acquire
+       if ssh-add -l|grep -qw "$key";then
+               echo >&2 "Identity $key already appended in the meantime."
+       else
+               # "2>/dev/null" to prevent: Identity added: .ssh/id_dsa (.ssh/id_dsa)
+               ssh-add "$key" 2>/dev/null
+       fi
+       rm -f $agenteval.input.lock
+       echo >&2 "$agenteval.input.lock unlocked."
+done
 unset agenteval