diff --git a/.bash_logout b/.bash_logout index 7bc70d4..5dbdd1c 100644 --- a/.bash_logout +++ b/.bash_logout @@ -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. diff --git a/.bash_profile b/.bash_profile index 00ba2d4..8e9916b 100644 --- a/.bash_profile +++ b/.bash_profile @@ -51,17 +51,39 @@ 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. - 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)" + 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 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 - else - echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)" + } + if [[ "$_PLATFORM" == "Linux" ]]; then + exec 9>&- + elif [[ "$_PLATFORM" == "Darwin" ]]; then + rm -f "/tmp/agents.lock.$$" fi - exec 9>&- 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)" @@ -74,17 +96,39 @@ 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. - 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)" + 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 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 - else - echo "$(tput setaf 1 || tput AF 1)Failed to obtain lockfile!$(tput op)" + } + if [[ "$_PLATFORM" == "Linux" ]]; then + exec 9>&- + elif [[ "$_PLATFORM" == "Darwin" ]]; then + rm -f "/tmp/agents.lock.$$" fi - exec 9>&- 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)" @@ -92,6 +136,7 @@ hash ssh-add ssh-agent >/dev/null 2>&1 && { fi echo } +unset I _GOT_LOCK # Screen. hash screen >/dev/null 2>&1 && {