From f91c9fe814916c2865ea76fda3714f39760183cc Mon Sep 17 00:00:00 2001 From: "Dmitry V. Levin" Date: Tue, 6 Dec 2016 00:32:06 +0000 Subject: [PATCH] tests: robustify attach-p-cmd.test against race conditions Introduce a synchronization mechanism between attach-p-cmd-p and attach-p-cmd-cmd processes. * tests/attach-p-cmd-cmd.c: Include and . (main): Wait for the lock directory creation by attach-p-cmd-p. * tests/attach-p-cmd-p.c: Include and . (main): Create a lock directory and wait for its removal by attach-p-cmd-cmd. * tests/attach-p-cmd.test: Assume that test programs work. --- tests/attach-p-cmd-cmd.c | 13 +++++++++++-- tests/attach-p-cmd-p.c | 21 +++++++++++++++++++-- tests/attach-p-cmd.test | 5 ++--- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/tests/attach-p-cmd-cmd.c b/tests/attach-p-cmd-cmd.c index edfff206..d0f824e9 100644 --- a/tests/attach-p-cmd-cmd.c +++ b/tests/attach-p-cmd-cmd.c @@ -28,19 +28,28 @@ */ #include "tests.h" +#include #include +#include #include int main(void) { + static const char lockdir[] = "attach-p-cmd.test-lock"; + /* wait for the lock directory to be created by peer */ + while (rmdir(lockdir)) { + if (ENOENT != errno) + perror_msg_and_fail("rmdir: %s", lockdir); + } + static const char dir[] = "attach-p-cmd.test cmd"; pid_t pid = getpid(); int rc = chdir(dir); - printf("%-5d chdir(\"%s\") = %d %s (%m)\n" + printf("%-5d chdir(\"%s\") = %s\n" "%-5d +++ exited with 0 +++\n", - pid, dir, rc, errno2name(), pid); + pid, dir, sprintrc(rc), pid); return 0; } diff --git a/tests/attach-p-cmd-p.c b/tests/attach-p-cmd-p.c index d63371d4..254d19ae 100644 --- a/tests/attach-p-cmd-p.c +++ b/tests/attach-p-cmd-p.c @@ -28,9 +28,11 @@ */ #include "tests.h" +#include #include #include #include +#include #include static void @@ -43,12 +45,27 @@ main(void) { const struct sigaction act = { .sa_handler = handler }; if (sigaction(SIGALRM, &act, NULL)) - perror_msg_and_skip("sigaction"); + perror_msg_and_fail("sigaction"); sigset_t mask = {}; sigaddset(&mask, SIGALRM); if (sigprocmask(SIG_UNBLOCK, &mask, NULL)) - perror_msg_and_skip("sigprocmask"); + perror_msg_and_fail("sigprocmask"); + + static const char lockdir[] = "attach-p-cmd.test-lock"; + /* create a lock directory */ + if (mkdir(lockdir, 0700)) + perror_msg_and_fail("mkdir: %s", lockdir); + + /* wait for the lock directory to be removed by peer */ + while (mkdir(lockdir, 0700)) { + if (EEXIST != errno) + perror_msg_and_fail("mkdir: %s", lockdir); + } + + /* remove the lock directory */ + if (rmdir(lockdir)) + perror_msg_and_fail("rmdir: %s", lockdir); alarm(1); pause(); diff --git a/tests/attach-p-cmd.test b/tests/attach-p-cmd.test index 27797196..2068cf1d 100755 --- a/tests/attach-p-cmd.test +++ b/tests/attach-p-cmd.test @@ -31,16 +31,15 @@ run_prog_skip_if_failed \ kill -0 $$ -run_prog ./attach-p-cmd-cmd > /dev/null -run_prog ./attach-p-cmd-p > /dev/null +rm -rf attach-p-cmd.test-lock rm -f "$OUT" ./set_ptracer_any ./attach-p-cmd-p >> "$OUT" & tracee_pid=$! while ! [ -s "$OUT" ]; do kill -0 $tracee_pid 2> /dev/null || - fail_ 'set_ptracer_any sleep failed' + fail_ 'set_ptracer_any ./attach-p-cmd-p failed' done run_strace -a30 -echdir -p $tracee_pid ./attach-p-cmd-cmd > "$OUT" -- 2.40.0