From 70abe8c58fef74435a6729dbd4ba1fb799c66a20 Mon Sep 17 00:00:00 2001 From: Darren 'Tadgy' Austin Date: Mon, 1 Jul 2019 21:14:42 +0100 Subject: [PATCH] Moved locking/unlocking into a function. Added PROMPT_COMMAND that went missing. --- .bash_logout | 34 ++-------------------------- .bash_profile | 61 +++++---------------------------------------------- .bashrc | 38 ++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 88 deletions(-) diff --git a/.bash_logout b/.bash_logout index 5dbdd1c..c801b0d 100644 --- a/.bash_logout +++ b/.bash_logout @@ -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. diff --git a/.bash_profile b/.bash_profile index 8e9916b..abc72ec 100644 --- a/.bash_profile +++ b/.bash_profile @@ -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 && { diff --git a/.bashrc b/.bashrc index 25d2023..ac3a573 100644 --- a/.bashrc +++ b/.bashrc @@ -66,6 +66,32 @@ _find_agent_sock() { 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() { local I IFS=$'\n' REPLY SOCKS=() # Read the current list of auth sockets. @@ -107,6 +133,15 @@ _push_agent_sock() { 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. shopt -s cdspell checkhash checkwinsize cmdhist histappend no_empty_cmd_completion @@ -160,6 +195,9 @@ unset _COLOUR # 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)\]: " +# The commands to execute before the prompt is displayed. +PROMPT_COMMAND="_agent_prompt_command" + # Platform specific set up. _PLATFORM="$(uname -s)" if [[ "$_PLATFORM" = "Linux" ]]; then