blob: 595a505be9e7cc13fbfb76bdc6b3f8d5ce7c723a [file] [log] [blame]
Dmitry V. Levin78460f82013-06-19 10:22:18 +00001#!/bin/sh
2
3# Ensure that strace can detach from running processes.
4
5. "${srcdir=.}/init.sh"
6
Dmitry V. Levin8f546642015-03-17 17:07:57 +00007run_prog_skip_if_failed \
8 kill -0 $$
Dmitry V. Levin74697bd2015-01-14 08:08:56 +00009
Dmitry V. Levin3ec5c042014-09-23 01:51:05 +000010check_prog sleep
Dmitry V. Levin78460f82013-06-19 10:22:18 +000011
12set -e
13
14./set_ptracer_any sh -c "echo > $LOG; while :; do :; done" > /dev/null &
15
Dmitry V. Levin8f546642015-03-17 17:07:57 +000016while ! [ -s "$LOG" ]; do
Dmitry V. Levin78460f82013-06-19 10:22:18 +000017 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
Mike Frysinger2b5bfeb2014-08-11 01:31:23 -040029 return 0
Dmitry V. Levin78460f82013-06-19 10:22:18 +000030}
31
Dmitry V. Levin8f546642015-03-17 17:07:57 +000032rm -f "$LOG"
33$STRACE -p $tracee_pid 2> "$LOG" &
Dmitry V. Levin78460f82013-06-19 10:22:18 +000034
Dmitry V. Levin8f546642015-03-17 17:07:57 +000035while ! 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 }
Dmitry V. Levin78460f82013-06-19 10:22:18 +000040 $SLEEP_A_BIT
41done
42
43kill -INT $!
44wait $!
45
Dmitry V. Levin8f546642015-03-17 17:07:57 +000046grep -F "Process $tracee_pid detached" "$LOG" > /dev/null || {
47 cleanup
48 dump_log_and_fail_with "$STRACE -p failed to detach"
49 }
Dmitry V. Levin78460f82013-06-19 10:22:18 +000050
Dmitry V. Levin0d7c3652013-06-19 14:57:05 +000051if [ -f /proc/self/status ]; then
52 $SLEEP_A_BIT
Dmitry V. Levin8f546642015-03-17 17:07:57 +000053 test -d /proc/$tracee_pid || {
54 cleanup
55 dump_log_and_fail_with 'tracee died after detach'
56 }
Dmitry V. Levin0d7c3652013-06-19 14:57:05 +000057 grep '^State:.*R (running)' < /proc/$tracee_pid/status > /dev/null || {
Dmitry V. Levin0d7c3652013-06-19 14:57:05 +000058 grep '^State:' < /proc/$tracee_pid/status
59 cleanup
Dmitry V. Levin8f546642015-03-17 17:07:57 +000060 dump_log_and_fail_with 'tracee is not running after detach'
Denys Vlasenkoa8151852013-06-19 16:37:24 +020061 }
Dmitry V. Levin0d7c3652013-06-19 14:57:05 +000062fi
Denys Vlasenkoa8151852013-06-19 16:37:24 +020063
Dmitry V. Levin78460f82013-06-19 10:22:18 +000064cleanup
65exit 0