]> granicus.if.org Git - strace/commitdiff
tests: ensure that strace can detach from sleeping and stopped processes
authorDmitry V. Levin <ldv@altlinux.org>
Tue, 18 Jun 2013 16:50:18 +0000 (16:50 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Tue, 18 Jun 2013 17:07:48 +0000 (17:07 +0000)
* tests/set_ptracer_any.c: New file.
* tests/detach-sleeping: New test.
* tests/detach-stopped: Likewise.
* tests/Makefile.am (check_PROGRAMS): Add set_ptracer_any.
(TESTS): Add detach-sleeping and detach-stopped.
* tests/.gitignore: Add set_ptracer_any.

tests/.gitignore
tests/Makefile.am
tests/detach-sleeping [new file with mode: 0755]
tests/detach-stopped [new file with mode: 0755]
tests/set_ptracer_any.c [new file with mode: 0644]

index 947455d2820b4c16c9174788540a369608b71920..a8b1cba1eae443be8fdd171f2460cbf30a634112 100644 (file)
@@ -1,4 +1,5 @@
 net-accept-connect
+set_ptracer_any
 *.log
 *.log.*
 *.o
index a437f4a786c6f3d51acddd983aac70bce45d8fdb..98fda59120ca21cce17ea0e9e0beb6d8b9d310cd 100644 (file)
@@ -2,9 +2,10 @@
 
 AM_CFLAGS = $(WARN_CFLAGS)
 
-check_PROGRAMS = net-accept-connect
+check_PROGRAMS = net-accept-connect set_ptracer_any
 
-TESTS = ptrace_setoptions strace-f qual_syscall stat net
+TESTS = ptrace_setoptions strace-f qual_syscall stat net \
+       detach-sleeping detach-stopped
 
 LOG_COMPILER = $(srcdir)/run.sh
 
diff --git a/tests/detach-sleeping b/tests/detach-sleeping
new file mode 100755 (executable)
index 0000000..8ea91e4
--- /dev/null
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+# Ensure that strace can detach from sleeping processes.
+
+. "${srcdir=.}/init.sh"
+
+check_prog sleep
+check_prog grep
+
+set -e
+
+./set_ptracer_any sleep $((2*$TIMEOUT_DURATION)) > set_ptracer_any.out &
+
+while ! [ -s set_ptracer_any.out ]; do
+       kill -0 $! 2> /dev/null ||
+               fail_ 'set_ptracer_any sleep failed'
+       sleep 1
+done
+rm -f set_ptracer_any.out
+
+tracee_pid=$!
+
+cleanup()
+{
+       set +e
+       kill $tracee_pid
+       kill -CONT $tracee_pid
+       wait $tracee_pid 2> /dev/null
+}
+
+$STRACE -p $tracee_pid 2> $LOG &
+
+while ! grep -F "Process $tracee_pid attached" $LOG > /dev/null; do
+       kill -0 $! 2> /dev/null ||
+               { cat $LOG; cleanup; fail_ 'strace -p does not work'; }
+       sleep 1
+done
+
+kill -INT $!
+wait $!
+
+grep -F "Process $tracee_pid detached" $LOG > /dev/null ||
+       { cat $LOG; cleanup; fail_ 'strace -p failed to detach'; }
+
+cleanup
+exit 0
diff --git a/tests/detach-stopped b/tests/detach-stopped
new file mode 100755 (executable)
index 0000000..f837b04
--- /dev/null
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+# Ensure that strace can detach from stopped processes.
+
+. "${srcdir=.}/init.sh"
+
+check_prog sleep
+check_prog grep
+
+set -e
+
+./set_ptracer_any sleep $((2*$TIMEOUT_DURATION)) > set_ptracer_any.out &
+
+while ! [ -s set_ptracer_any.out ]; do
+       kill -0 $! 2> /dev/null ||
+               fail_ 'set_ptracer_any sleep failed'
+       sleep 1
+done
+rm -f set_ptracer_any.out
+
+tracee_pid=$!
+kill -STOP $tracee_pid
+
+cleanup()
+{
+       set +e
+       kill $tracee_pid
+       kill -CONT $tracee_pid
+       wait $tracee_pid 2> /dev/null
+}
+
+$STRACE -p $tracee_pid 2> $LOG &
+
+while ! grep -F "Process $tracee_pid attached" $LOG > /dev/null; do
+       kill -0 $! 2> /dev/null ||
+               { cat $LOG; cleanup; fail_ 'strace -p does not work'; }
+       sleep 1
+done
+
+while ! grep -F -e '--- stopped by ' $LOG > /dev/null; do
+       kill -0 $! 2> /dev/null ||
+               { cat $LOG; cleanup; fail_ 'strace -p does not work'; }
+       sleep 1
+done
+
+kill -INT $!
+wait $!
+
+grep -F "Process $tracee_pid detached" $LOG > /dev/null ||
+       { cat $LOG; cleanup; fail_ 'strace -p failed to detach'; }
+
+cleanup
+exit 0
diff --git a/tests/set_ptracer_any.c b/tests/set_ptracer_any.c
new file mode 100644 (file)
index 0000000..7254a07
--- /dev/null
@@ -0,0 +1,24 @@
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+#include <stdio.h>
+#include <unistd.h>
+#ifdef HAVE_PRCTL
+# include <sys/prctl.h>
+#endif
+
+int main(int argc, char **argv)
+{
+       if (argc < 2)
+               return 99;
+#if defined HAVE_PRCTL && defined PR_SET_PTRACER && defined PR_SET_PTRACER_ANY
+       (void) prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY);
+#endif
+       if (write(1, "\n", 1) != 1) {
+               perror("write");
+               return 99;
+       }
+       (void) execvp(argv[1], argv + 1);
+       perror(argv[1]);
+       return 99;
+}