| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# SPDX-License-Identifier: LGPL-2.1+ |
| 4 |
|
| 5 |
# lxc: linux Container library |
| 6 |
|
| 7 |
set -e |
| 8 |
|
| 9 |
# NOTE: |
| 10 |
# lxc-attach allocates a pty on the host and attaches any standard file |
| 11 |
# descriptors that refer to a pty to it. Standard file descriptors which do not |
| 12 |
# refer to a pty are not attached. In order to determine whether lxc-attach |
| 13 |
# works correctly we test various methods of redirection on the host. E.g.: |
| 14 |
# |
| 15 |
# lxc-attach -n busy -- hostname < /dev/null |
| 16 |
# |
| 17 |
# This is done to check whether the file descriptor that gets redirected to |
| 18 |
# /dev/null is (a) left alone and (b) that lxc-attach does not fail. When |
| 19 |
# lxc-attach fails we know that it's behavior has been altered, e.g. by trying |
| 20 |
# to attach a standard file descriptor that does not refer to a pty. |
| 21 |
# The small table preceeding each test case show which standard file descriptors |
| 22 |
# we expect to be attached to a pty and which we expect to be redirected. E.g. |
| 23 |
# |
| 24 |
# stdin --> attached to pty |
| 25 |
# stdout --> attached to pty |
| 26 |
# stderr --> attached to pty |
| 27 |
|
| 28 |
allocate_pty="nopty" |
| 29 |
|
| 30 |
ATTACH_LOG=$(mktemp --dry-run) |
| 31 |
|
| 32 |
FAIL() { |
| 33 |
echo -n "Failed " >&2 |
| 34 |
echo "$*" >&2 |
| 35 |
cat "${ATTACH_LOG}" |
| 36 |
rm -f "${ATTACH_LOG}" || true |
| 37 |
lxc-destroy -n busy -f |
| 38 |
exit 1 |
| 39 |
} |
| 40 |
|
| 41 |
# Create a container, start it and wait for it to be in running state. |
| 42 |
lxc-create -t busybox -n busy -l trace -o "${ATTACH_LOG}" || FAIL "creating busybox container" |
| 43 |
lxc-start -n busy -d -l trace -o "${ATTACH_LOG}" || FAIL "starting busybox container" |
| 44 |
lxc-wait -n busy -s RUNNING -l trace -o "${ATTACH_LOG}" || FAIL "waiting for busybox container to run" |
| 45 |
|
| 46 |
if [ -t 0 ] && [ -t 1 ] && [ -t 2 ]; then |
| 47 |
allocate_pty="pty" |
| 48 |
echo "All standard file descriptors refer to a pty." |
| 49 |
echo "Tests for lxc-attach pty allocation and I/O redirection" |
| 50 |
echo "will be performed correctly." |
| 51 |
fi |
| 52 |
|
| 53 |
# stdin --> attached to pty |
| 54 |
# stdout --> attached to pty |
| 55 |
# stderr --> attached to pty |
| 56 |
for i in $(seq 1 100); do |
| 57 |
attach=$(lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- hostname || FAIL "to allocate or setup pty") |
| 58 |
if [ "$attach" != "busy" ]; then |
| 59 |
FAIL "lxc-attach -n busy -- hostname" |
| 60 |
fi |
| 61 |
rm -f "${ATTACH_LOG}" |
| 62 |
done |
| 63 |
|
| 64 |
# stdin --> /dev/null |
| 65 |
# stdout --> attached to pty |
| 66 |
# stderr --> attached to pty |
| 67 |
attach=$(lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- hostname < /dev/null || FAIL "to allocate or setup pty") |
| 68 |
if [ "$attach" != "busy" ]; then |
| 69 |
FAIL "lxc-attach -n busy -- hostname < /dev/null" |
| 70 |
fi |
| 71 |
rm -f "${ATTACH_LOG}" |
| 72 |
|
| 73 |
# stdin --> attached to pty |
| 74 |
# stdout --> /dev/null |
| 75 |
# stderr --> attached to pty |
| 76 |
attach=$(lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- hostname > /dev/null || FAIL "to allocate or setup pty") |
| 77 |
if [ -n "$attach" ]; then |
| 78 |
FAIL "lxc-attach -n busy -- hostname > /dev/null" |
| 79 |
fi |
| 80 |
rm -f "${ATTACH_LOG}" |
| 81 |
|
| 82 |
# stdin --> attached to pty |
| 83 |
# stdout --> attached to pty |
| 84 |
# stderr --> /dev/null |
| 85 |
attach=$(lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- hostname 2> /dev/null || FAIL "to allocate or setup pty") |
| 86 |
if [ "$attach" != "busy" ]; then |
| 87 |
FAIL "lxc-attach -n busy -- hostname 2> /dev/null < /dev/null" |
| 88 |
fi |
| 89 |
rm -f "${ATTACH_LOG}" |
| 90 |
|
| 91 |
# stdin --> /dev/null |
| 92 |
# stdout --> attached to pty |
| 93 |
# stderr --> /dev/null |
| 94 |
attach=$(lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- hostname 2> /dev/null < /dev/null || FAIL "to allocate or setup pty") |
| 95 |
if [ "$attach" != "busy" ]; then |
| 96 |
FAIL "lxc-attach -n busy -- hostname 2> /dev/null < /dev/null" |
| 97 |
fi |
| 98 |
rm -f "${ATTACH_LOG}" |
| 99 |
|
| 100 |
# Use a synthetic reproducer in container to produce output on stderr. stdout on |
| 101 |
# the host gets redirect to /dev/null. We should still be able to receive |
| 102 |
# containers output on stderr on the host. (The command is run in a subshell. |
| 103 |
# This allows us to redirect stderr to stdout for the subshell and capture the |
| 104 |
# output in the attach variable.) |
| 105 |
# stdin --> attached to pty |
| 106 |
# stdout --> /dev/null |
| 107 |
# stderr --> attached to pty |
| 108 |
attach=$( ( lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- sh -c 'hostname >&2' > /dev/null ) 2>&1 || FAIL "to allocate or setup pty") |
| 109 |
if [ "$attach" != "busy" ]; then |
| 110 |
FAIL "lxc-attach -n busy -- sh -c 'hostname >&2' > /dev/null" |
| 111 |
fi |
| 112 |
rm -f "${ATTACH_LOG}" |
| 113 |
|
| 114 |
# Use a synthetic reproducer in container to produce output on stderr. stderr on |
| 115 |
# the host gets redirect to /dev/null. We should not receive output on stderr on |
| 116 |
# the host. (The command is run in a subshell. This allows us to redirect stderr |
| 117 |
# to stdout for the subshell and capture the output in the attach variable.) |
| 118 |
# stdin --> attached to pty |
| 119 |
# stdout --> attach to pty |
| 120 |
# stderr --> /dev/null |
| 121 |
attach=$( ( lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- sh -c 'hostname >&2' 2> /dev/null ) 2>&1 || FAIL "to allocate or setup pty") |
| 122 |
if [ -n "$attach" ]; then |
| 123 |
FAIL "lxc-attach -n busy -- sh -c 'hostname >&2' 2> /dev/null" |
| 124 |
fi |
| 125 |
rm -f "${ATTACH_LOG}" |
| 126 |
|
| 127 |
# stdin --> attached to pty |
| 128 |
# stdout --> /dev/null |
| 129 |
# stderr --> attached to pty |
| 130 |
# (As we expect the exit code of the command to be 1 we ignore it.) |
| 131 |
attach=$(lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- sh -c 'rm 2>&1' > /dev/null || true) |
| 132 |
if [ -n "$attach" ]; then |
| 133 |
FAIL "lxc-attach -n busy -- sh -c 'rm 2>&1' > /dev/null" |
| 134 |
fi |
| 135 |
rm -f "${ATTACH_LOG}" |
| 136 |
|
| 137 |
# - stdin --> attached to pty |
| 138 |
# - stdout --> attached to pty |
| 139 |
# - stderr --> /dev/null |
| 140 |
# (As we expect the exit code of the command to be 1 we ignore it.) |
| 141 |
attach=$(lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- sh -c 'rm 2>&1' 2> /dev/null || true) |
| 142 |
if [ -z "$attach" ]; then |
| 143 |
FAIL "lxc-attach -n busy -- sh -c 'rm 2>&1' 2> /dev/null" |
| 144 |
fi |
| 145 |
rm -f "${ATTACH_LOG}" |
| 146 |
|
| 147 |
# stdin --> $in |
| 148 |
# stdout --> attached to pty |
| 149 |
# stderr --> attached to pty |
| 150 |
attach=$(echo hostname | lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- || FAIL "to allocate or setup pty") |
| 151 |
if [ "$attach" != "busy" ]; then |
| 152 |
FAIL "echo hostname | lxc-attach -n busy --" |
| 153 |
fi |
| 154 |
rm -f "${ATTACH_LOG}" |
| 155 |
|
| 156 |
# stdin --> attached to pty |
| 157 |
# stdout --> $out |
| 158 |
# stderr --> $err |
| 159 |
out=$(mktemp /tmp/out_XXXX) |
| 160 |
err=$(mktemp /tmp/err_XXXX) |
| 161 |
trap "rm -f $out $err" EXIT INT QUIT PIPE |
| 162 |
lxc-attach -n busy -l trace -o "${ATTACH_LOG}" -- sh -c 'echo OUT; echo ERR >&2' > $out 2> $err || FAIL "to allocate or setup pty" |
| 163 |
outcontent=$(cat $out) |
| 164 |
errcontent=$(cat $err) |
| 165 |
if [ "$outcontent" != "OUT" ] || [ "$errcontent" != "ERR" ]; then |
| 166 |
FAIL "lxc-attach -n busy -- sh -c 'echo OUT; echo ERR >&2' > $out 2> $err" |
| 167 |
fi |
| 168 |
|
| 169 |
rm -f $out $err "${ATTACH_LOG}" |
| 170 |
|
| 171 |
# stdin --> $in |
| 172 |
# stdout --> $out |
| 173 |
# stderr --> $err |
| 174 |
# (As we expect the exit code of the command to be 1 we ignore it.) |
| 175 |
out=$(mktemp /tmp/out_XXXX) |
| 176 |
err=$(mktemp /tmp/err_XXXX) |
| 177 |
trap "rm -f $out $err" EXIT INT QUIT PIPE |
| 178 |
echo "hostname; rm" | lxc-attach -n busy -l trace -o "${ATTACH_LOG}" > $out 2> $err || true |
| 179 |
outcontent=$(cat $out) |
| 180 |
errcontent=$(cat $err) |
| 181 |
if [ "$outcontent" != "busy" ] || [ -z "$errcontent" ]; then |
| 182 |
FAIL "echo 'hostname; rm' | lxc-attach -n busy > $out 2> $err" |
| 183 |
fi |
| 184 |
|
| 185 |
rm -f $out $err "${ATTACH_LOG}" |
| 186 |
|
| 187 |
# |
| 188 |
# This testcase covers cases like: |
| 189 |
# https://github.com/lxc/lxc/issues/4546 |
| 190 |
# https://discuss.linuxcontainers.org/t/lxc-attach-long-output-stops-suddenly-possible-bug/22031 |
| 191 |
# https://discuss.linuxcontainers.org/t/fixing-forgejo-runners-lxc-logging/25918 |
| 192 |
# |
| 193 |
# Idea is simple, we simulate a heavy IO and write relatively large amount of data to overfill |
| 194 |
# pts device buffers, then ensure data integrity. |
| 195 |
# |
| 196 |
# We need to use "script" tool to allocate TTYs properly, otherwise we don't go into a |
| 197 |
# problematic LXC code-path we want to cover. |
| 198 |
# |
| 199 |
# Also, I had to introduce two synthetic sleeps: one before issuing commands to busybox shell inside |
| 200 |
# a container and another one after. |
| 201 |
# |
| 202 |
# First one is needed, because LXC looses some pieces of terminal device input during |
| 203 |
# lxc-attach command initialization because of tcsetattr(fd, TCSAFLUSH, &newtios) call |
| 204 |
# (see https://github.com/lxc/lxc/blob/5d9839bc1316fa185d8c29b90982684b32e3dfa7/src/lxc/terminal.c#L523) |
| 205 |
# I would replace TCSAFLUSH with TCSANOW to avoid TTY buffer flush (and I tested that it helps), |
| 206 |
# but taking into account that this code is here since 2010 |
| 207 |
# (see https://github.com/lxc/lxc/commit/e0dc0de76ed1ad9e284a37bd01268227d4eae8c9) |
| 208 |
# I decided to keep it like it is for now (FIXME?). |
| 209 |
# |
| 210 |
# Second sleep is needed because of a bug in busybox, unfortunately, without this sleep, |
| 211 |
# busybox fails to react on the host pipe write-end closure (after full command submission) |
| 212 |
# and continues to poll infinitely. This sleep makes pipe closure even to be separated from |
| 213 |
# a heavy IO and avoids this bug. |
| 214 |
# |
| 215 |
|
| 216 |
# Check test dependencies |
| 217 |
command -v script >/dev/null 2>&1 || { echo "'script' command is missing" >&2; exit 1; } |
| 218 |
busybox dd --help >/dev/null 2>&1 || FAIL "missing busybox's dd applet" |
| 219 |
busybox hexdump --help >/dev/null 2>&1 || FAIL "missing busybox's hexdump applet" |
| 220 |
busybox tee --help >/dev/null 2>&1 || FAIL "missing busybox's tee applet" |
| 221 |
|
| 222 |
out=$(mktemp /tmp/out_XXXX) |
| 223 |
BS=1000000 |
| 224 |
( sleep 3; echo "echo DATASTART ; dd if=/dev/urandom bs=$BS count=1 status=none | hexdump | tee /root/large-data.txt ; echo DATAEND" ; sleep 3 ) | \ |
| 225 |
script -q -e -c "lxc-attach -n busy -l trace -o \"${ATTACH_LOG}\"" | \ |
| 226 |
sed -n '/DATASTART/,/DATAEND/{/DATASTART/d;/DATAEND/d;s/[\r\n]*$//;p}' > $out |
| 227 |
|
| 228 |
[ $(stat -c%s $out) -gt $BS ] || FAIL "generated file size is too small" |
| 229 |
cmp -s /var/lib/lxc/busy/rootfs/root/large-data.txt $out || FAIL "data corruption detected" |
| 230 |
|
| 231 |
md5sum /var/lib/lxc/busy/rootfs/root/large-data.txt $out |
| 232 |
ls -lah /var/lib/lxc/busy/rootfs/root/large-data.txt |
| 233 |
rm -f /var/lib/lxc/busy/rootfs/root/large-data.txt $out "${ATTACH_LOG}" |
| 234 |
|
| 235 |
# Cleanup stage |
| 236 |
lxc-destroy -n busy -f |
| 237 |
rm -f "${ATTACH_LOG}" || true |
| 238 |
|
| 239 |
exit 0 |