Skip to content

Commit

Permalink
stdstream support (#168)
Browse files Browse the repository at this point in the history
* stdstream support

* stdstream test (using /dev/std{in,out,err}), and fds directly
  • Loading branch information
ezrizhu authored Sep 18, 2024
1 parent b670fcf commit 71cb569
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
51 changes: 51 additions & 0 deletions test/stdstream.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/sh

TRY_TOP="${TRY_TOP:-$(git rev-parse --show-toplevel --show-superproject-working-tree 2>/dev/null || echo "${0%/*}")}"
TRY="$TRY_TOP/try"

cmdfile="$(mktemp)"

cat > "$cmdfile" <<'EOF'
read x < /dev/stdin
echo $((x * 2)) > /dev/stdout
echo $((x * 3)) > /dev/stderr
EOF

chmod +x "$cmdfile"

try_stdout=$(mktemp)
try_stderr=$(mktemp)
sh_stdout=$(mktemp)
sh_stderr=$(mktemp)

# test stdout
echo 5 | "$TRY" "$cmdfile" >"$try_stdout" 2>"$try_stderr"
echo 5 | sh "$cmdfile" >"$sh_stdout" 2>"$sh_stderr"

diff "$try_stdout" "$sh_stdout" || exit 1

# using grep because there's try errors printed
grep -q 15 "$try_stderr"
grep -q 15 "$sh_stderr"

rm "$try_stdout" "$try_stderr" "$sh_stdout" "$sh_stderr"

cat > "$cmdfile" <<'EOF'
read x <&0
echo $((x * 2)) >&1
echo $((x * 3)) >&2
EOF

# test stdout
echo 5 | "$TRY" "$cmdfile" >"$try_stdout" 2>"$try_stderr"
echo 5 | sh "$cmdfile" >"$sh_stdout" 2>"$sh_stderr"

diff "$try_stdout" "$sh_stdout" || exit 1

# using grep because there's try errors printed
grep -q 15 "$try_stderr"
grep -q 15 "$sh_stderr"

rm "$try_stdout" "$try_stderr" "$sh_stdout" "$sh_stderr"
7 changes: 7 additions & 0 deletions try
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ unshare --root="$SANDBOX_DIR/temproot" /bin/sh "$chroot_executable"
exitcode="$?"
# unmount the devices
rm "$sandbox_dir/temproot/dev/stdin"
rm "$sandbox_dir/temproot/dev/stdout"
rm "$sandbox_dir/temproot/dev/stderr"
unmount_devices "$SANDBOX_DIR"
exit $exitcode
Expand All @@ -262,6 +266,9 @@ unset START_DIR SANDBOX_DIR UNION_HELPER DIRS_AND_MOUNTS TRY_EXIT_STATUS
unset script_to_execute chroot_executable try_mount_log
mount -t proc proc /proc &&
ln -s /proc/self/fd/0 /dev/stdin &&
ln -s /proc/self/fd/1 /dev/stdout &&
ln -s /proc/self/fd/2 /dev/stderr &&
cd "$START_DIR" &&
. "$script_to_execute"
EOF
Expand Down

0 comments on commit 71cb569

Please sign in to comment.