1#!/bin/sh
2
3# Ensure that strace can detach from running processes.
4
5. "${srcdir=.}/init.sh"
6
7run_prog_skip_if_failed \
8	kill -0 $$
9
10check_prog sleep
11
12set -e
13
14./set_ptracer_any sh -c "echo > $LOG; while :; do :; done" > /dev/null &
15
16while ! [ -s "$LOG" ]; do
17	kill -0 $! 2> /dev/null ||
18		fail_ 'set_ptracer_any sh failed'
19	$SLEEP_A_BIT
20done
21
22tracee_pid=$!
23
24cleanup()
25{
26	set +e
27	kill $tracee_pid
28	wait $tracee_pid 2> /dev/null
29	return 0
30}
31
32rm -f "$LOG"
33$STRACE -p $tracee_pid 2> "$LOG" &
34
35while ! grep -F "Process $tracee_pid attached" "$LOG" > /dev/null; do
36	kill -0 $! 2> /dev/null || {
37		cleanup
38		dump_log_and_fail_with "$STRACE -p failed to attach"
39	}
40	$SLEEP_A_BIT
41done
42
43kill -INT $!
44wait $!
45
46grep -F "Process $tracee_pid detached" "$LOG" > /dev/null || {
47		cleanup
48		dump_log_and_fail_with "$STRACE -p failed to detach"
49	}
50
51if [ -f /proc/self/status ]; then
52	$SLEEP_A_BIT
53	test -d /proc/$tracee_pid || {
54		cleanup
55		dump_log_and_fail_with 'tracee died after detach'
56	}
57	grep '^State:.*R (running)' < /proc/$tracee_pid/status > /dev/null || {
58		grep '^State:' < /proc/$tracee_pid/status
59		cleanup
60		dump_log_and_fail_with 'tracee is not running after detach'
61	}
62fi
63
64cleanup
65exit 0
66