Added support for locking on Darwin.

This commit is contained in:
Darren 'Tadgy' Austin 2019-07-01 20:17:23 +01:00
commit 31d09b8bb5
2 changed files with 90 additions and 16 deletions

View file

@ -14,19 +14,48 @@ if (( $SHLVL == 10 )); then
fi fi
hash ssh-add ssh-agent >/dev/null 2>&1 && { hash ssh-add ssh-agent >/dev/null 2>&1 && {
# Determine the platform we're on.
_PLATFORM="$(uname -s)"
# Remove the SSH_AUTH_SOCK from the agents file. # Remove the SSH_AUTH_SOCK from the agents file.
[[ ! -z "$SSH_AUTH_SOCK" ]] && { [[ ! -z "$SSH_AUTH_SOCK" ]] && {
exec 9>~/.ssh/agents.lock if [[ "$_PLATFORM" == "Linux" ]]; then
if flock -E 10 -w 0.5 9; then # Linux has 'flock', thankfully.
if exec 9>~/.ssh/agents.lock && flock -E 10 -w 0.5 9; then
_GOT_LOCK=1
else
echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)"
_SLEEP=3
fi
elif [[ "$_PLATFORM" == "Darwin" ]]; then
# Do locking the sucky way on OSX.
for ((I=0; I < 5; I++)); do
if shlock -f "/tmp/agents.lock.$$" -p "$$"; then
_GOT_LOCK=1
break
else
sleep 0.1
fi
done
[[ "${_GOT_LOCK:-0}" == "0" ]] && {
echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)"
_SLEEP=3
}
else
echo "$(tput setaf 1 || tput AF 1)File locking unsupported - skipping clean up of agents file!$(tput op)"
_SLEEP=3
fi
[[ "${_GOT_LOCK:-0}" == "1" ]] && {
_pop_agent_sock || { _pop_agent_sock || {
echo -e "$(tput setaf 1 || tput AF 1)Failed to clean up agents file!$(tput op)" echo -e "$(tput setaf 1 || tput AF 1)Failed to clean up agents file!$(tput op)"
_SLEEP=3 _SLEEP=3
} }
else }
echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)" if [[ "$_PLATFORM" == "Linux" ]]; then
_SLEEP=3 exec 9>&-
elif [[ "$_PLATFORM" == "Darwin" ]]; then
rm -f "/tmp/agents.lock.$$"
fi fi
exec 9>&-
} }
# Kill the ssh-agent. # Kill the ssh-agent.

View file

@ -51,17 +51,39 @@ hash ssh-add ssh-agent >/dev/null 2>&1 && {
ssh-add -l >/dev/null 2>&1 ssh-add -l >/dev/null 2>&1
if (( $? < 2 )); then if (( $? < 2 )); then
# Agent started - add new socket to the agent sockets file. # Agent started - add new socket to the agent sockets file.
exec 9>~/.ssh/agents.lock if [[ "$_PLATFORM" == "Linux" ]]; then
if flock -E 10 -w 0.5 9; then # Linux has 'flock', thankfully.
if exec 9>~/.ssh/agents.lock && flock -E 10 -w 0.5 9; then
_GOT_LOCK=1
else
echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)"
fi
elif [[ "$_PLATFORM" == "Darwin" ]]; then
# Do locking the sucky way on OSX.
for ((I=0; I < 5; I++)); do
if shlock -f "/tmp/agents.lock.$$" -p "$$"; then
_GOT_LOCK=1
break
else
sleep 0.1
fi
done
[[ "${_GOT_LOCK:-0}" == "0" ]] && echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)"
else
echo "$(tput setaf 1 || tput AF 1)File locking unsupported - skipping clean up of agents file!$(tput op)"
fi
[[ "$_GOT_LOCK" == "1" ]] && {
if _push_agent_sock; then if _push_agent_sock; then
echo "Started new ssh-agent." echo "Started new ssh-agent."
else else
echo "$(tput setaf 1 || tput AF 1)Started new ssh-agent, but failed to register socket!$(tput op)" echo "$(tput setaf 1 || tput AF 1)Started new ssh-agent, but failed to register socket!$(tput op)"
fi fi
else }
echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)" if [[ "$_PLATFORM" == "Linux" ]]; then
exec 9>&-
elif [[ "$_PLATFORM" == "Darwin" ]]; then
rm -f "/tmp/agents.lock.$$"
fi fi
exec 9>&-
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh' alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
else else
echo "$(tput setaf 1 || tput AF 1)Failed to start new ssh-agent!$(tput op)" echo "$(tput setaf 1 || tput AF 1)Failed to start new ssh-agent!$(tput op)"
@ -74,17 +96,39 @@ hash ssh-add ssh-agent >/dev/null 2>&1 && {
ssh-add -l >/dev/null 2>&1 ssh-add -l >/dev/null 2>&1
if (( $? < 2 )); then if (( $? < 2 )); then
# Agent is connected - add new socket to the agent sockets file. # Agent is connected - add new socket to the agent sockets file.
exec 9>~/.ssh/agents.lock if [[ "$_PLATFORM" == "Linux" ]]; then
if flock -E 10 -w 0.5 9; then # Linux has 'flock', thankfully.
if exec 9>~/.ssh/agents.lock && flock -E 10 -w 0.5 9; then
_GOT_LOCK=1
else
echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)"
fi
elif [[ "$_PLATFORM" == "Darwin" ]]; then
# Do locking the sucky way on OSX.
for ((I=0; I < 5; I++)); do
if shlock -f "/tmp/agents.lock.$$" -p "$$"; then
_GOT_LOCK=1
break
else
sleep 0.1
fi
done
[[ "${_GOT_LOCK:-0}" == "0" ]] && echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)"
else
echo "$(tput setaf 1 || tput AF 1)File locking unsupported - skipping update of agents file!$(tput op)"
fi
[[ "${_GOT_LOCK:-0}" == "1" ]] && {
if _push_agent_sock; then if _push_agent_sock; then
echo "Connected to forwarded ssh-agent." echo "Connected to forwarded ssh-agent."
else else
echo -e "$(tput setaf 1 || tput AF 1)Failed to register forwarded ssh-agent socket!$(tput op)" echo -e "$(tput setaf 1 || tput AF 1)Failed to register forwarded ssh-agent socket!$(tput op)"
fi fi
else }
echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)" if [[ "$_PLATFORM" == "Linux" ]]; then
exec 9>&-
elif [[ "$_PLATFORM" == "Darwin" ]]; then
rm -f "/tmp/agents.lock.$$"
fi fi
exec 9>&-
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh' alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
else else
echo -e "$(tput setaf 1 || tput AF 1)Problem connecting to forwarded ssh-agent!$(tput op)" echo -e "$(tput setaf 1 || tput AF 1)Problem connecting to forwarded ssh-agent!$(tput op)"
@ -92,6 +136,7 @@ hash ssh-add ssh-agent >/dev/null 2>&1 && {
fi fi
echo echo
} }
unset I _GOT_LOCK
# Screen. # Screen.
hash screen >/dev/null 2>&1 && { hash screen >/dev/null 2>&1 && {