Skip to content

Commit

Permalink
Try harder to attach to an existing tmux session
Browse files Browse the repository at this point in the history
If in the course of an ssh/tmux session the tmux package is upgraded
and the ssh connection broken, the next ssh attempt will encounter an
error because of the tmux protocol version mismatch.

The old tmux can be used to reattach by searching for the tmux process
under /proc and using its reference to the old tmux executable. This
change attempts to do that automatically if a first "tmux attach"
attempt fails, but we can see that a previous session still exists.
  • Loading branch information
rmol committed Mar 7, 2019
1 parent 21fcf15 commit 830411c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
# If not running interactively, do not do anything
[[ $- != *i* ]] && return

# Auto-launch tmux on login
# https://wiki.archlinux.org/index.php/Tmux#Start_tmux_on_every_shell_login
if which tmux >/dev/null 2>&1; then
#if not inside a tmux session, and if no session is started, start a new session
test -z "$TMUX" && (tmux attach || tmux new-session)
fi
which tmux >/dev/null 2>&1 || return

function tmux_attach_via_proc() {
pid=$(pgrep --newest tmux)
if test -n "$pid"
then
/proc/$pid/exe attach
fi
return 1
}

if test -z "$TMUX"
then
(tmux attach || tmux_attach_via_proc || tmux new-session)
fi
23 changes: 14 additions & 9 deletions molecule/testinfra/staging/common/test_user_config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import hashlib
import os
import re


Expand Down Expand Up @@ -38,16 +40,19 @@ def test_sudoers_tmux_env(host):
the corresponding settings there.
"""

f = host.file('/etc/profile.d/securedrop_additions.sh')
non_interactive_str = re.escape('[[ $- != *i* ]] && return')
tmux_check = re.escape('test -z "$TMUX" && (tmux attach ||'
' tmux new-session)')
host_file = host.file('/etc/profile.d/securedrop_additions.sh')
source_file = os.path.abspath(
os.path.join(
os.path.dirname(__file__),
'../../../../install_files/ansible-base/roles/common/files/',
'bashrc_securedrop_additions'
)
)
expected_content = open(source_file).read()
h = hashlib.sha256()
h.update(expected_content)

assert f.contains("^{}$".format(non_interactive_str))
assert f.contains("^if which tmux >\/dev\/null 2>&1; then$")

assert 'test -z "$TMUX" && (tmux attach || tmux new-session)' in f.content
assert f.contains(tmux_check)
assert host_file.sha256sum == h.hexdigest()


def test_tmux_installed(host):
Expand Down

0 comments on commit 830411c

Please sign in to comment.