From 97c85088a286831aec3ecb41942946d52dc006d4 Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Tue, 18 Jun 2013 16:50:18 +0000 Subject: [PATCH] tests: ensure that strace can detach from sleeping and stopped processes * 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 | 1 + tests/Makefile.am | 5 ++-- tests/detach-sleeping | 46 +++++++++++++++++++++++++++++++++++ tests/detach-stopped | 53 +++++++++++++++++++++++++++++++++++++++++ tests/set_ptracer_any.c | 24 +++++++++++++++++++ 5 files changed, 127 insertions(+), 2 deletions(-) create mode 100755 tests/detach-sleeping create mode 100755 tests/detach-stopped create mode 100644 tests/set_ptracer_any.c diff --git a/tests/.gitignore b/tests/.gitignore index 947455d2..a8b1cba1 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1,4 +1,5 @@ net-accept-connect +set_ptracer_any *.log *.log.* *.o diff --git a/tests/Makefile.am b/tests/Makefile.am index a437f4a7..98fda591 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 00000000..8ea91e48 --- /dev/null +++ b/tests/detach-sleeping @@ -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 index 00000000..f837b046 --- /dev/null +++ b/tests/detach-stopped @@ -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 index 00000000..7254a07f --- /dev/null +++ b/tests/set_ptracer_any.c @@ -0,0 +1,24 @@ +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif +#include +#include +#ifdef HAVE_PRCTL +# include +#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; +} -- 2.40.0