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. # Remove the SSH_AUTH_SOCK from the agents file.
[[ ! -z "$SSH_AUTH_SOCK" ]] && { [[ ! -z "$SSH_AUTH_SOCK" ]] && {
if [[ "$_PLATFORM" == "Linux" ]]; then _lock_agents_file && {
# 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
} }
} }
if [[ "$_PLATFORM" == "Linux" ]]; then _unlock_agents_file
exec 9>&-
elif [[ "$_PLATFORM" == "Darwin" ]]; then
rm -f "/tmp/agents.lock.$$"
fi
} }
# Kill the ssh-agent. # Kill the ssh-agent.

View file

@ -28,7 +28,6 @@ elif [[ "$_PLATFORM" = "Darwin" ]]; then
else else
echo "${BASH_SOURCE##*/}: unsupported platform: $_PLATFORM" >&2 echo "${BASH_SOURCE##*/}: unsupported platform: $_PLATFORM" >&2
fi fi
unset _PLATFORM
# Add bin directories to PATH. # Add bin directories to PATH.
[[ -d "$HOME/files/bin" ]] && export PATH="$HOME/files/bin:$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 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.
if [[ "$_PLATFORM" == "Linux" ]]; then _lock_agents_file && {
# 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
} }
if [[ "$_PLATFORM" == "Linux" ]]; then _unlock_agents_file
exec 9>&-
elif [[ "$_PLATFORM" == "Darwin" ]]; then
rm -f "/tmp/agents.lock.$$"
fi
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)"
@ -96,39 +70,14 @@ 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.
if [[ "$_PLATFORM" == "Linux" ]]; then _lock_agents_file && {
# 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
} }
if [[ "$_PLATFORM" == "Linux" ]]; then _unlock_agents_file
exec 9>&-
elif [[ "$_PLATFORM" == "Darwin" ]]; then
rm -f "/tmp/agents.lock.$$"
fi
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)"
@ -136,7 +85,7 @@ hash ssh-add ssh-agent >/dev/null 2>&1 && {
fi fi
echo echo
} }
unset I _GOT_LOCK unset _GOT_LOCK _PLATFORM
# Screen. # Screen.
hash screen >/dev/null 2>&1 && { hash screen >/dev/null 2>&1 && {

38
.bashrc
View file

@ -66,6 +66,32 @@ _find_agent_sock() {
return 1 return 1
} }
_lock_agents_file() {
local I
# Lock the ~/.ssh/agents file.
if [[ "$_PLATFORM" == "Linux" ]]; then
# Linux has 'flock', thankfully.
if exec 9>~/.ssh/agents.lock && flock -E 10 -w 0.5 9; then
return 0
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
return 0
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
return 1
}
_pop_agent_sock() { _pop_agent_sock() {
local I IFS=$'\n' REPLY SOCKS=() local I IFS=$'\n' REPLY SOCKS=()
# Read the current list of auth sockets. # Read the current list of auth sockets.
@ -107,6 +133,15 @@ _push_agent_sock() {
return $? return $?
} }
_unlock_agents_file() {
# Unlock the ~/.ssh/agents file.
if [[ "$_PLATFORM" == "Linux" ]]; then
exec 9>&-
elif [[ "$_PLATFORM" == "Darwin" ]]; then
rm -f "/tmp/agents.lock.$$"
fi
}
# Make bash a little more pleasent - these are valid for all versions. # Make bash a little more pleasent - these are valid for all versions.
shopt -s cdspell checkhash checkwinsize cmdhist histappend no_empty_cmd_completion shopt -s cdspell checkhash checkwinsize cmdhist histappend no_empty_cmd_completion
@ -160,6 +195,9 @@ unset _COLOUR
# PS4="+(\\\$? = \$?) \${BASH_SOURCE##*/}\${FUNCNAME:+(\$FUNCNAME)}:\$LINENO: " # PS4="+(\\\$? = \$?) \${BASH_SOURCE##*/}\${FUNCNAME:+(\$FUNCNAME)}:\$LINENO: "
PS4="+(\[\e[33m\]\\\$? = \$?\[$(tput sgr0)\]) \[$(tput bold)$(tput setaf 4)\]\${BASH_SOURCE##*/}\[$(tput sgr0)\]\${FUNCNAME:+(\[$(tput bold)$(tput setaf 2)\]\$FUNCNAME\[$(tput sgr0)\])}:\[$(tput bold)$(tput setaf 1)\]\$LINENO\[$(tput sgr0)\]: " PS4="+(\[\e[33m\]\\\$? = \$?\[$(tput sgr0)\]) \[$(tput bold)$(tput setaf 4)\]\${BASH_SOURCE##*/}\[$(tput sgr0)\]\${FUNCNAME:+(\[$(tput bold)$(tput setaf 2)\]\$FUNCNAME\[$(tput sgr0)\])}:\[$(tput bold)$(tput setaf 1)\]\$LINENO\[$(tput sgr0)\]: "
# The commands to execute before the prompt is displayed.
PROMPT_COMMAND="_agent_prompt_command"
# Platform specific set up. # Platform specific set up.
_PLATFORM="$(uname -s)" _PLATFORM="$(uname -s)"
if [[ "$_PLATFORM" = "Linux" ]]; then if [[ "$_PLATFORM" = "Linux" ]]; then