]> granicus.if.org Git - strace/commitdiff
tests: robustify attach-p-cmd.test against race conditions
authorDmitry V. Levin <ldv@altlinux.org>
Tue, 6 Dec 2016 00:32:06 +0000 (00:32 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Tue, 6 Dec 2016 09:08:36 +0000 (09:08 +0000)
Introduce a synchronization mechanism between attach-p-cmd-p
and attach-p-cmd-cmd processes.

* tests/attach-p-cmd-cmd.c: Include <errno.h> and <sys/stat.h>.
(main): Wait for the lock directory creation by attach-p-cmd-p.
* tests/attach-p-cmd-p.c: Include <errno.h> and <sys/stat.h>.
(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
tests/attach-p-cmd-p.c
tests/attach-p-cmd.test

index edfff20632f9de4289abd96941031d36fad8c851..d0f824e95780cace3e046f1374b356ca947cf906 100644 (file)
  */
 
 #include "tests.h"
+#include <errno.h>
 #include <stdio.h>
+#include <sys/stat.h>
 #include <unistd.h>
 
 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;
 }
index d63371d445a6eabcdee5d80472474edae12788f5..254d19ae04b0916dff45dc937b3ef09fe3432fe9 100644 (file)
  */
 
 #include "tests.h"
+#include <errno.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/stat.h>
 #include <unistd.h>
 
 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();
index 27797196bd816b46a9409a8626abad0acf7953db..2068cf1d2ed7a089bdbace3d7ac7fa889c46d644 100755 (executable)
 
 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"