summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2023-08-26 09:09:09 -0400
committerAndrew Cady <d@jerkface.net>2023-08-26 09:09:19 -0400
commit03eb0cad29e677f0b1e49f7eba8e581ecaaa2018 (patch)
treea2535422d530fed8aa91758e7b65aad1ee1dab7e
parent1ed2f3f601b8a720b8c9cfd285bfc56a43670f2c (diff)
it works except the terminal is echoing
when ssh is called with '-tt' it echoes back its stdin to the terminal, which dumps the source code of the rpc there it echoes before the remote bash code even runs, like how the terminal sometimes buffers input in the display when there is nothing on the other end listening for it. the solution is probably running 'ssh' from within 'socat' as documented in socat(1)
-rw-r--r--src/rpc.bash63
1 files changed, 38 insertions, 25 deletions
diff --git a/src/rpc.bash b/src/rpc.bash
index eb41538..44f7020 100644
--- a/src/rpc.bash
+++ b/src/rpc.bash
@@ -50,22 +50,28 @@ recursive_dependencies()
50 50
51# Input: $BASH_RPC_REMOTE_DEST - hostname passed to ssh (uses ssh host aliases) 51# Input: $BASH_RPC_REMOTE_DEST - hostname passed to ssh (uses ssh host aliases)
52# Input: $BASH_RPC_SSH_OPTIONS - option arguments passed to ssh (they precede '--') 52# Input: $BASH_RPC_SSH_OPTIONS - option arguments passed to ssh (they precede '--')
53# Input: $1 - bash shell script source code to run remotely 53# Input; $BASH_RPC_STDIN_IS_TTY - controls whether to allocate ssh pty. Doesn't work.
54# Input: $2, $3, ... - command line arguments passed to the remote bash shell 54# Input: $1 - shell script source code to run in the remote bash
55# Input: stdin, stdout, stderr: passed to ssh 55# Input: $2, $3, ... - command line arguments passed to the remote bash
56# Input: stdin, stdout, stderr: passed to the remote bash over ssh
57# Input: "$0" - copied to remote bash's $0
58# Input: "$SHELL" - remote shell to launch (better be bash)
56bash_rpc_remote_run_script() 59bash_rpc_remote_run_script()
57{ 60{
58 ( 61 local script="$1"
59 local script="$1" 62 shift
60 shift 63 exec ssh \
61 set -- "$SHELL" -c "${script@Q}" "$0" "${@@Q}" 64 "${BASH_RPC_SSH_OPTIONS[@]}" -- \
62 set -x 65 "${BASH_RPC_REMOTE_DEST:-localhost}" \
63 exec ssh \ 66 "$SHELL" -c "${script@Q}" "$0" "${@@Q}"
64 "${BASH_RPC_SSH_OPTIONS[@]}" \ 67}
65 -- \ 68
66 "${BASH_RPC_REMOTE_DEST:-localhost}" \ 69with_ssh_option()
67 "$@" 70{
68 ) 71 local BASH_RPC_SSH_OPTIONS
72 BASH_RPC_SSH_OPTIONS+=("$1")
73 shift
74 "$@"
69} 75}
70 76
71# Preserves the rest of stdin for the command. 77# Preserves the rest of stdin for the command.
@@ -73,14 +79,20 @@ bash_rpc_eval_stdin()
73{ 79{
74 [ "$1" -gt 0 ] || return 80 [ "$1" -gt 0 ] || return
75 read -N"$1" -r || return 81 read -N"$1" -r || return
76 shift # prepare "$@" for script execution context 82
77 source <(printf '%s' "$REPLY" | tr -d '\015') 83 # Prepare "$@" for script execution context.
84 shift
85
86 # Even unset REPLY to leave pristine environment.
87 eval "unset REPLY; $(cat <<< "$REPLY")"
78} 88}
79 89
80remote_run_function_simple() 90remote_run_function_simple()
81{ 91{
82 script_source=$(declare -f "$1" && printf '"$@";\n') 92 script_source=$(declare -f "$1" && printf '"$@";\n')
83 bash_rpc_remote_run_script "$script_source" "$@" 93 bash_rpc_remote_run_script \
94 "$script_source" \
95 "$@"
84} 96}
85 97
86remote_run_function() 98remote_run_function()
@@ -91,15 +103,16 @@ remote_run_function()
91 declare -f $funcs 103 declare -f $funcs
92 printf '"$@"\n' "$main" 104 printf '"$@"\n' "$main"
93 ) 105 )
94 BASH_RPC_SSH_OPTIONS=(-tt) 106
107 local BASH_RPC_STDIN_IS_TTY=$([ ! -t 0 ] || echo y)
95 { 108 {
96 printf '%s' "$stage2_source" 109 printf '%s' "$stage2_source"
97 if [ -t 0 ] 110 cat
98 then 111 } |
99 socat stdio PTY 112 ${BASH_RPC_STDIN_IS_TTY:+ with_ssh_option '-tt'} \
100 else 113 remote_run_function_simple \
101 cat 114 bash_rpc_eval_stdin \
102 fi 115 "${#stage2_source}" \
103 } | remote_run_function_simple bash_rpc_eval_stdin "${#stage2_source}" "$@" 116 "$@"
104} 117}
105 118