File: detach-vfork.test

package info (click to toggle)
strace 6.13%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 71,488 kB
  • sloc: ansic: 176,497; sh: 9,675; makefile: 4,133; cpp: 885; awk: 353; perl: 267; exp: 62; sed: 9
file content (89 lines) | stat: -rwxr-xr-x 1,948 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/sh
#
# Check detaching from vfork'ed processes.
#
# Copyright (c) 2023 Dmitry V. Levin <ldv@strace.io>
# All rights reserved.
#
# SPDX-License-Identifier: GPL-2.0-or-later

. "${srcdir=.}/init.sh"

max_wait_attempts=100

check_prog grep
run_prog_skip_if_failed \
	kill -0 $$
run_prog ../$NAME 0 > /dev/null

cat >script <<EOF
#!/bin/sh -efu
# Do not wait forever, stop waiting after a few iterations.
attempt=1
while [ "\$attempt" -lt "$max_wait_attempts" ] && [ ! -s signalled ]; do
	$SLEEP_A_BIT
	attempt=\$((attempt + 1))
done
$SLEEP_A_BIT
"\$@"
kill \$\$
EOF
chmod +x script
rm -f signalled

> "$OUT"
(
	./script &
	exec $STRACE -o'|./script exit' -I2 -f --trace=none --quiet=personality \
		../$NAME $! > "$EXP" 2> "$OUT"
) &
pid=$!

# Do not wait for strace output forever,
# stop waiting after $max_wait_attempts iterations.
attempt=0
while [ "$attempt" -lt "$max_wait_attempts" ] && [ ! -s "$OUT" ]; do
	$SLEEP_A_BIT
	attempt=$((attempt + 1))
done
[ -s "$OUT" ] || {
	kill -9 $pid
	fail_ 'timeout waiting for strace output'
}

# Non-empty $OUT means the vfork'ed process has been attached
# and it's time to send SIGTERM to strace.
kill $pid

# Wake up ./script invocations.
echo > signalled

# Do not wait for strace termination forever,
# stop waiting after $max_wait_attempts iterations.
attempt=0
while [ "$attempt" -lt "$max_wait_attempts" ] && kill -0 "$pid" 2> /dev/null; do
	$SLEEP_A_BIT
	attempt=$((attempt + 1))
done
[ "$attempt" -lt "$max_wait_attempts" ] || {
	kill -9 $pid
	fail_ 'timeout waiting for strace to terminate'
}

rc=0
wait $pid ||
	rc=$?
[ "$rc" = 143 ] ||
	fail_ "expected exit status 143, got $rc"

# Do not wait for expected output forever,
# stop waiting after $max_wait_attempts iterations.
attempt=0
while [ "$attempt" -lt "$max_wait_attempts" ] && [ ! -s "$EXP" ]; do
	$SLEEP_A_BIT
	attempt=$((attempt + 1))
done
[ -s "$EXP" ] ||
	fail_ 'timeout waiting for expected output'

match_diff "$OUT" "$EXP"