Moved locking/unlocking into a function. Added PROMPT_COMMAND that went missing.

This commit is contained in:
Darren 'Tadgy' Austin 2019-07-01 21:14:42 +01:00
commit 70abe8c58f
3 changed files with 45 additions and 88 deletions

View file

@ -19,43 +19,13 @@ hash ssh-add ssh-agent >/dev/null 2>&1 && {
# Remove the SSH_AUTH_SOCK from the agents file.
[[ ! -z "$SSH_AUTH_SOCK" ]] && {
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" ]] && {
_lock_agents_file && {
_pop_agent_sock || {
echo -e "$(tput setaf 1 || tput AF 1)Failed to clean up agents file!$(tput op)"
_SLEEP=3
}
}
if [[ "$_PLATFORM" == "Linux" ]]; then
exec 9>&-
elif [[ "$_PLATFORM" == "Darwin" ]]; then
rm -f "/tmp/agents.lock.$$"
fi
_unlock_agents_file
}
# Kill the ssh-agent.