Moved locking/unlocking into a function. Added PROMPT_COMMAND that went missing.
This commit is contained in:
parent
31d09b8bb5
commit
70abe8c58f
3 changed files with 45 additions and 88 deletions
|
@ -28,7 +28,6 @@ elif [[ "$_PLATFORM" = "Darwin" ]]; then
|
|||
else
|
||||
echo "${BASH_SOURCE##*/}: unsupported platform: $_PLATFORM" >&2
|
||||
fi
|
||||
unset _PLATFORM
|
||||
|
||||
# Add bin directories to PATH.
|
||||
[[ -d "$HOME/files/bin" ]] && export PATH="$HOME/files/bin:$PATH"
|
||||
|
@ -51,39 +50,14 @@ hash ssh-add ssh-agent >/dev/null 2>&1 && {
|
|||
ssh-add -l >/dev/null 2>&1
|
||||
if (( $? < 2 )); then
|
||||
# Agent started - add new socket to the agent sockets file.
|
||||
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)"
|
||||
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" ]] && {
|
||||
_lock_agents_file && {
|
||||
if _push_agent_sock; then
|
||||
echo "Started new ssh-agent."
|
||||
else
|
||||
echo "$(tput setaf 1 || tput AF 1)Started new ssh-agent, but failed to register socket!$(tput op)"
|
||||
fi
|
||||
}
|
||||
if [[ "$_PLATFORM" == "Linux" ]]; then
|
||||
exec 9>&-
|
||||
elif [[ "$_PLATFORM" == "Darwin" ]]; then
|
||||
rm -f "/tmp/agents.lock.$$"
|
||||
fi
|
||||
_unlock_agents_file
|
||||
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
|
||||
else
|
||||
echo "$(tput setaf 1 || tput AF 1)Failed to start new ssh-agent!$(tput op)"
|
||||
|
@ -96,39 +70,14 @@ hash ssh-add ssh-agent >/dev/null 2>&1 && {
|
|||
ssh-add -l >/dev/null 2>&1
|
||||
if (( $? < 2 )); then
|
||||
# Agent is connected - add new socket to the agent sockets file.
|
||||
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)"
|
||||
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" ]] && {
|
||||
_lock_agents_file && {
|
||||
if _push_agent_sock; then
|
||||
echo "Connected to forwarded ssh-agent."
|
||||
else
|
||||
echo -e "$(tput setaf 1 || tput AF 1)Failed to register forwarded ssh-agent socket!$(tput op)"
|
||||
fi
|
||||
}
|
||||
if [[ "$_PLATFORM" == "Linux" ]]; then
|
||||
exec 9>&-
|
||||
elif [[ "$_PLATFORM" == "Darwin" ]]; then
|
||||
rm -f "/tmp/agents.lock.$$"
|
||||
fi
|
||||
_unlock_agents_file
|
||||
alias ssh='ssh-add -l >/dev/null 2>&1 && unalias ssh || { ssh-add && unalias ssh; }; ssh'
|
||||
else
|
||||
echo -e "$(tput setaf 1 || tput AF 1)Problem connecting to forwarded ssh-agent!$(tput op)"
|
||||
|
@ -136,7 +85,7 @@ hash ssh-add ssh-agent >/dev/null 2>&1 && {
|
|||
fi
|
||||
echo
|
||||
}
|
||||
unset I _GOT_LOCK
|
||||
unset _GOT_LOCK _PLATFORM
|
||||
|
||||
# Screen.
|
||||
hash screen >/dev/null 2>&1 && {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue