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
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.
[[ ! -z "$SSH_AUTH_SOCK" ]] && {
exec 9>~/.ssh/agents.lock
if flock -E 10 -w 0.5 9; then
if [[ "$_PLATFORM" == "Linux" ]]; 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 || {
echo -e "$(tput setaf 1 || tput AF 1)Failed to clean up agents file!$(tput op)"
_SLEEP=3
}
else
echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)"
_SLEEP=3
}
if [[ "$_PLATFORM" == "Linux" ]]; then
exec 9>&-
elif [[ "$_PLATFORM" == "Darwin" ]]; then
rm -f "/tmp/agents.lock.$$"
fi
exec 9>&-
}
# Kill the ssh-agent.