Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/kernel - abstract the pipe calls into routines.



details:   https://anonhg.NetBSD.org/src/rev/6d682eb43b35
branches:  trunk
changeset: 349306:6d682eb43b35
user:      christos <christos%NetBSD.org@localhost>
date:      Mon Dec 05 20:10:10 2016 +0000

description:
- abstract the pipe calls into routines.
- some of the tests that worked (really failed silently before) now fail.

diffstat:

 tests/kernel/msg.h           |  136 +++++++++++
 tests/kernel/t_ptrace_wait.c |  505 ++++++++++--------------------------------
 tests/kernel/t_ptrace_wait.h |   28 +-
 3 files changed, 280 insertions(+), 389 deletions(-)

diffs (truncated from 1175 to 300 lines):

diff -r eecd80a61dd7 -r 6d682eb43b35 tests/kernel/msg.h
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/kernel/msg.h        Mon Dec 05 20:10:10 2016 +0000
@@ -0,0 +1,136 @@
+/*     $NetBSD: msg.h,v 1.1 2016/12/05 20:10:10 christos Exp $ */
+
+/*-
+ * Copyright (c) 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+struct msg_fds {
+       int pfd[2];
+       int cfd[2];
+};
+
+#define CLOSEFD(fd) do { \
+       if (fd != -1) { \
+               close(fd); \
+               fd = -1; \
+       } \
+} while (/*CONSTCOND*/ 0)
+
+static int
+msg_open(struct msg_fds *fds)
+{
+       if (pipe(fds->pfd) == -1)
+               return -1;
+       if (pipe(fds->cfd) == -1) {
+               close(fds->pfd[0]);
+               close(fds->pfd[1]);
+               return -1;
+       }
+       return 0;
+}
+
+static void
+msg_close(struct msg_fds *fds)
+{
+       CLOSEFD(fds->pfd[0]);
+       CLOSEFD(fds->pfd[1]);
+       CLOSEFD(fds->cfd[0]);
+       CLOSEFD(fds->cfd[1]);
+}
+
+static int
+msg_write_child(const char *info, struct msg_fds *fds, void *msg, size_t len)
+{
+       ssize_t rv;
+       CLOSEFD(fds->cfd[1]);
+       CLOSEFD(fds->pfd[0]);
+
+       printf("Send %s\n", info);
+       rv = write(fds->pfd[1], msg, len);
+       if (rv != (ssize_t)len)
+               return 1;
+//     printf("Wait %s\n", info);
+       rv = read(fds->cfd[0], msg, len);
+       if (rv != (ssize_t)len)
+               return 1;
+       return 0;
+}
+
+static int
+msg_write_parent(const char *info, struct msg_fds *fds, void *msg, size_t len)
+{
+       ssize_t rv;
+       CLOSEFD(fds->pfd[1]);
+       CLOSEFD(fds->cfd[0]);
+
+       printf("Send %s\n", info);
+       rv = write(fds->cfd[1], msg, len);
+       if (rv != (ssize_t)len)
+               return 1;
+//     printf("Wait %s\n", info);
+       rv = read(fds->pfd[0], msg, len);
+       if (rv != (ssize_t)len)
+               return 1;
+       return 0;
+}
+
+static int
+msg_read_parent(const char *info, struct msg_fds *fds, void *msg, size_t len)
+{
+       ssize_t rv;
+       CLOSEFD(fds->pfd[1]);
+       CLOSEFD(fds->cfd[0]);
+
+       printf("Wait %s\n", info);
+       rv = read(fds->pfd[0], msg, len);
+       if (rv != (ssize_t)len)
+               return 1;
+//     printf("Send %s\n", info);
+       rv = write(fds->cfd[1], msg, len);
+       if (rv != (ssize_t)len)
+               return 1;
+       return 0;
+}
+
+static int
+msg_read_child(const char *info, struct msg_fds *fds, void *msg, size_t len)
+{
+       ssize_t rv;
+       CLOSEFD(fds->cfd[1]);
+       CLOSEFD(fds->pfd[0]);
+
+       printf("Wait %s\n", info);
+       rv = read(fds->cfd[0], msg, len);
+       if (rv != (ssize_t)len)
+               return 1;
+//     printf("Send %s\n", info);
+       rv = write(fds->pfd[1], msg, len);
+       if (rv != (ssize_t)len)
+               return 1;
+       return 0;
+}
diff -r eecd80a61dd7 -r 6d682eb43b35 tests/kernel/t_ptrace_wait.c
--- a/tests/kernel/t_ptrace_wait.c      Mon Dec 05 15:31:01 2016 +0000
+++ b/tests/kernel/t_ptrace_wait.c      Mon Dec 05 20:10:10 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: t_ptrace_wait.c,v 1.40 2016/12/05 07:18:10 kamil Exp $ */
+/*     $NetBSD: t_ptrace_wait.c,v 1.41 2016/12/05 20:10:10 christos Exp $      */
 
 /*-
  * Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: t_ptrace_wait.c,v 1.40 2016/12/05 07:18:10 kamil Exp $");
+__RCSID("$NetBSD: t_ptrace_wait.c,v 1.41 2016/12/05 20:10:10 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/types.h>
@@ -51,6 +51,20 @@
 #include "../h_macros.h"
 
 #include "t_ptrace_wait.h"
+#include "msg.h"
+// #define atf_utils_fork() fork()
+
+#define PARENT_TO_CHILD(info, fds, msg) \
+    ATF_REQUIRE(msg_write_child(info " to child " # fds, &fds, &msg, sizeof(msg)) == 0)
+
+#define CHILD_FROM_PARENT(info, fds, msg) \
+    FORKEE_ASSERT(msg_read_parent(info " from parent " # fds, &fds, &msg, sizeof(msg)) == 0)
+
+#define CHILD_TO_PARENT(info, fds, msg) \
+    FORKEE_ASSERT(msg_write_parent(info " to parent " # fds, &fds, &msg, sizeof(msg)) == 0)
+
+#define PARENT_FROM_CHILD(info, fds, msg) \
+    ATF_REQUIRE(msg_read_child(info " from parent " # fds, &fds, &msg, sizeof(msg)) == 0)
 
 ATF_TC(traceme1);
 ATF_TC_HEAD(traceme1, tc)
@@ -285,8 +299,7 @@
 
 ATF_TC_BODY(attach1, tc)
 {
-       int fds_totracee[2], fds_totracer[2], fds_fromtracer[2];
-       int rv;
+       struct msg_fds parent_tracee, parent_tracer;
        const int exitval_tracee = 5;
        const int exitval_tracer = 10;
        pid_t tracee, tracer, wpid;
@@ -296,30 +309,18 @@
 #endif
 
        printf("Spawn tracee\n");
-       ATF_REQUIRE(pipe(fds_totracee) == 0);
+       ATF_REQUIRE(msg_open(&parent_tracee) == 0);
        tracee = atf_utils_fork();
        if (tracee == 0) {
-               FORKEE_ASSERT(close(fds_totracee[1]) == 0);
-
-               /* Wait for message from the parent */
-               rv = read(fds_totracee[0], &msg, sizeof(msg));
-               FORKEE_ASSERT(rv == sizeof(msg));
-
+               CHILD_FROM_PARENT("message 1", parent_tracee, msg);
+               msg_close(&parent_tracee);
                _exit(exitval_tracee);
        }
-       ATF_REQUIRE(close(fds_totracee[0]) == 0);
 
        printf("Spawn debugger\n");
-       ATF_REQUIRE(pipe(fds_totracer) == 0);
-       ATF_REQUIRE(pipe(fds_fromtracer) == 0);
+       ATF_REQUIRE(msg_open(&parent_tracer) == 0);
        tracer = atf_utils_fork();
        if (tracer == 0) {
-               /* No IPC to communicate with the child */
-               FORKEE_ASSERT(close(fds_totracee[1]) == 0);
-
-               FORKEE_ASSERT(close(fds_totracer[1]) == 0);
-               FORKEE_ASSERT(close(fds_fromtracer[0]) == 0);
-
                printf("Before calling PT_ATTACH from tracee %d\n", getpid());
                FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
 
@@ -333,47 +334,46 @@
                FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
 
                /* Inform parent that tracer has attached to tracee */
-               rv = write(fds_fromtracer[1], &msg, sizeof(msg));
-               FORKEE_ASSERT(rv == sizeof(msg));
-
-               /* Wait for parent */
-               rv = read(fds_totracer[0], &msg, sizeof(msg));
-               FORKEE_ASSERT(rv == sizeof(msg));
+               CHILD_TO_PARENT("Message 1", parent_tracer, msg);
+
+               CHILD_FROM_PARENT("Message 2", parent_tracer, msg);
 
                /* Wait for tracee and assert that it exited */
                FORKEE_REQUIRE_SUCCESS(
                    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
 
                forkee_status_exited(status, exitval_tracee);
+               printf("Tracee %d exited with %d\n", tracee, exitval_tracee);
+
+               CHILD_TO_PARENT("Message 3", parent_tracer, msg);
 
                printf("Before exiting of the tracer process\n");
+               msg_close(&parent_tracer);
                _exit(exitval_tracer);
        }
-       ATF_REQUIRE(close(fds_totracer[0]) == 0);
-       ATF_REQUIRE(close(fds_fromtracer[1]) == 0);
 
        printf("Wait for the tracer to attach to the tracee\n");
-       rv = read(fds_fromtracer[0], &msg, sizeof(msg));
-       ATF_REQUIRE(rv == sizeof(msg));
+       PARENT_FROM_CHILD("Message 1", parent_tracer, msg);
 
        printf("Resume the tracee and let it exit\n");
-       rv = write(fds_totracee[1], &msg, sizeof(msg));
-       ATF_REQUIRE(rv == sizeof(msg));
-
-       printf("fds_totracee is no longer needed - close it\n");
-       ATF_REQUIRE(close(fds_totracee[1]) == 0);
+       PARENT_TO_CHILD("Message 1", parent_tracee,  msg);
 
        printf("Detect that tracee is zombie\n");
        await_zombie(tracee);
 
-       printf("Assert that there is no status about tracee - "
-           "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
+       printf("Tell the tracer child should have exited\n");
+       PARENT_TO_CHILD("Message 2", parent_tracer,  msg);
+
+       printf("Wait from tracer child to complete waiting for tracee\n");
+       PARENT_FROM_CHILD("Message 3", parent_tracer, msg);
+       printf("Assert that there is no status about tracee %d - "
+           "Tracer must detect zombie first - calling %s()\n", tracee,
+           TWAIT_FNAME);
        TWAIT_REQUIRE_SUCCESS(
            wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
 
        printf("Resume the tracer and let it detect exited tracee\n");
-       rv = write(fds_totracer[1], &msg, sizeof(msg));
-       ATF_REQUIRE(rv == sizeof(msg));
+       PARENT_TO_CHILD("Message 2", parent_tracer, msg);
 
        printf("Wait for tracer to finish its job and exit - calling %s()\n",
            TWAIT_FNAME);
@@ -385,15 +385,12 @@
        printf("Wait for tracee to finish its job and exit - calling %s()\n",
            TWAIT_FNAME);
        TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
-           tracee);
+           0);
 
        validate_status_exited(status, exitval_tracee);
 
-       printf("fds_fromtracer is no longer needed - close it\n");



Home | Main Index | Thread Index | Old Index