]> granicus.if.org Git - strace/blob - tests/detach-stopped.test
Update copyright headers
[strace] / tests / detach-stopped.test
1 #!/bin/sh
2 #
3 # Ensure that strace can detach from stopped processes.
4 #
5 # Copyright (c) 2013-2015 Dmitry V. Levin <ldv@altlinux.org>
6 # Copyright (c) 2014-2018 The strace developers.
7 # All rights reserved.
8 #
9 # SPDX-License-Identifier: GPL-2.0-or-later
10
11 . "${srcdir=.}/init.sh"
12
13 run_prog_skip_if_failed \
14         kill -0 $$
15
16 $STRACE -d -enone / > /dev/null 2> "$LOG"
17 if grep -x "[^:]*strace: PTRACE_SEIZE doesn't work" "$LOG" > /dev/null; then
18         skip_ "PTRACE_SEIZE doesn't work"
19 fi
20
21 check_prog sleep
22
23 trap - TERM
24 sleep $TIMEOUT_DURATION &
25 kill -TERM $!
26 wait $!
27 expected_rc=$?
28
29 set -e
30
31 > "$LOG"
32 ../set_ptracer_any sleep $((2*$TIMEOUT_DURATION)) > "$LOG" &
33
34 while ! [ -s "$LOG" ]; do
35         kill -0 $! 2> /dev/null ||
36                 fail_ 'set_ptracer_any sleep failed'
37         $SLEEP_A_BIT
38 done
39
40 tracee_pid=$!
41 kill -STOP $tracee_pid
42
43 cleanup()
44 {
45         set +e
46         kill $tracee_pid
47         kill -CONT $tracee_pid
48         wait $tracee_pid 2> /dev/null
49         return 0
50 }
51
52 > "$LOG"
53 $STRACE -p $tracee_pid 2> "$LOG" &
54
55 while ! grep -F "Process $tracee_pid attached" "$LOG" > /dev/null; do
56         kill -0 $! 2> /dev/null || {
57                 cleanup
58                 dump_log_and_fail_with "$STRACE -p failed to attach"
59         }
60         $SLEEP_A_BIT
61 done
62
63 while ! grep -F -e '--- stopped by ' "$LOG" > /dev/null; do
64         kill -0 $! 2> /dev/null || {
65                 cleanup
66                 dump_log_and_fail_with "$STRACE -p missed stop notifications"
67         }
68         $SLEEP_A_BIT
69 done
70
71 kill -TERM $!
72 wait $! && rc=0 || rc=$?
73
74 grep -F "Process $tracee_pid detached" "$LOG" > /dev/null || {
75         cleanup
76         dump_log_and_fail_with "$STRACE -p failed to detach"
77 }
78
79 [ "$rc" = "$expected_rc" ] || {
80         cleanup
81         dump_log_and_fail_with "$STRACE -p failed to terminate itself"
82 }
83
84 if [ -f /proc/self/status ]; then
85         $SLEEP_A_BIT
86         test -d /proc/$tracee_pid || {
87                 cleanup
88                 dump_log_and_fail_with 'tracee died after detach'
89         }
90         grep_pid_status "$tracee_pid" '^State:.*T (stopped)' > /dev/null || {
91                 grep_pid_status "$tracee_pid" '^State:'
92                 cleanup
93                 dump_log_and_fail_with 'tracee is not group-stopped after detach'
94         }
95 fi
96
97 cleanup
98 exit 0