Source-Changes-HG archive

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

[src/trunk]: src/tests/kernel PR/56831: Eric van Gyzen: race condition in tes...



details:   https://anonhg.NetBSD.org/src/rev/15569078cffe
branches:  trunk
changeset: 366098:15569078cffe
user:      christos <christos%NetBSD.org@localhost>
date:      Sat May 14 14:02:03 2022 +0000

description:
PR/56831: Eric van Gyzen: race condition in tests/kernel/t_sysv.c
https://cgit.freebsd.org/src/commit/?id=20917cac7bcf216225a7b66f7b3a56f3764c5acc

diffstat:

 tests/kernel/t_sysv.c |  275 ++++++++++++++++++-------------------------------
 1 files changed, 100 insertions(+), 175 deletions(-)

diffs (truncated from 543 to 300 lines):

diff -r 837535b1914a -r 15569078cffe tests/kernel/t_sysv.c
--- a/tests/kernel/t_sysv.c     Sat May 14 12:25:16 2022 +0000
+++ b/tests/kernel/t_sysv.c     Sat May 14 14:02:03 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: t_sysv.c,v 1.5 2018/02/03 02:57:15 pgoyette Exp $      */
+/*     $NetBSD: t_sysv.c,v 1.6 2022/05/14 14:02:03 christos Exp $      */
 
 /*-
  * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
@@ -53,11 +53,9 @@
 #include <sys/shm.h>
 #include <sys/wait.h>
 
-volatile int did_sigsys, did_sigchild;
-volatile int child_status, child_count;
+volatile int did_sigsys;
 
 void   sigsys_handler(int);
-void   sigchld_handler(int);
 
 key_t  get_ftok(int);
 
@@ -120,16 +118,14 @@
 static int
 read_int(const char *path)
 {
-       int input;
+       int input, value;
 
        input = open(path, O_RDONLY);
        if (input == -1)
                return -1;
-       else {
-               int value;
-               read(input, &value, sizeof(value));
-               return value;
-       }
+
+       read(input, &value, sizeof(value));
+       return value;
 }
 
 
@@ -140,23 +136,6 @@
        did_sigsys = 1;
 }
 
-void
-sigchld_handler(int signo)
-{
-       int c_status;
-
-       did_sigchild = 1;
-       /*
-        * Reap the child and return its status
-        */
-       if (wait(&c_status) == -1)
-               child_status = -errno;
-       else
-               child_status = c_status;
-
-       child_count--;
-}
-
 key_t get_ftok(int id)
 {
        int fd;
@@ -179,8 +158,9 @@
                rmdir(tmpdir);
                atf_tc_fail("open() of temp file failed: %d", errno);
                return (key_t)-1;
-       } else
-               close(fd);
+       }
+
+       close(fd);
 
        key = ftok(token_key, id);
        ATF_REQUIRE_MSG(key != (key_t)-1, "ftok() failed");
@@ -204,10 +184,10 @@
        struct sigaction sa;
        struct msqid_ds m_ds;
        struct testmsg m;
-       sigset_t sigmask;
        int sender_msqid;
        int loop;
        int c_status;
+       pid_t wait_result;
 
        /*
         * Install a SIGSYS handler so that we can exit gracefully if
@@ -220,18 +200,6 @@
        ATF_REQUIRE_MSG(sigaction(SIGSYS, &sa, NULL) != -1,
            "sigaction SIGSYS: %d", errno);
 
-       /*
-        * Install a SIGCHLD handler to deal with all possible exit
-        * conditions of the receiver.
-        */
-       did_sigchild = 0;
-       child_count = 0;
-       sa.sa_handler = sigchld_handler;
-       sigemptyset(&sa.sa_mask);
-       sa.sa_flags = 0;
-       ATF_REQUIRE_MSG(sigaction(SIGCHLD, &sa, NULL) != -1,
-           "sigaction SIGCHLD: %d", errno);
-
        msgkey = get_ftok(4160);
        ATF_REQUIRE_MSG(msgkey != (key_t)-1, "get_ftok failed");
 
@@ -264,13 +232,14 @@
 
        print_msqid_ds(&m_ds, 0600);
 
+       fflush(stdout);
+
        switch ((child_pid = fork())) {
        case -1:
                atf_tc_fail("fork: %d", errno);
                return;
 
        case 0:
-               child_count++;
                receiver();
                break;
 
@@ -288,7 +257,7 @@
                    0) != -1, "sender: msgsnd 1: %d", errno);
 
                ATF_REQUIRE_MSG(msgrcv(sender_msqid, &m, MESSAGE_TEXT_LEN,
-                                      MTYPE_1_ACK, 0) == MESSAGE_TEXT_LEN,
+                   MTYPE_1_ACK, 0) == MESSAGE_TEXT_LEN,
                    "sender: msgrcv 1 ack: %d", errno);
 
                print_msqid_ds(&m_ds, 0600);
@@ -298,40 +267,29 @@
                 */
                m.mtype = MTYPE_2;
                strcpy(m.mtext, m2_str);
-               ATF_REQUIRE_MSG(msgsnd(sender_msqid, &m, MESSAGE_TEXT_LEN, 0) != -1,
-                   "sender: msgsnd 2: %d", errno);
+               ATF_REQUIRE_MSG(msgsnd(sender_msqid, &m, MESSAGE_TEXT_LEN, 0)
+                   != -1, "sender: msgsnd 2: %d", errno);
 
                ATF_REQUIRE_MSG(msgrcv(sender_msqid, &m, MESSAGE_TEXT_LEN,
-                                      MTYPE_2_ACK, 0) == MESSAGE_TEXT_LEN,
+                   MTYPE_2_ACK, 0) == MESSAGE_TEXT_LEN,
                    "sender: msgrcv 2 ack: %d", errno);
        }
 
        /*
         * Wait for child to finish
         */
-       sigemptyset(&sigmask);
-       (void) sigsuspend(&sigmask);
+       wait_result = wait(&c_status);
+       ATF_REQUIRE_EQ_MSG(wait_result, child_pid, "wait returned %d (%s)",
+           wait_result, wait_result == -1 ? strerror(errno) : "");
+       ATF_REQUIRE_MSG(WIFEXITED(c_status), "child abnormal exit: %d (sig %d)",
+           c_status, WTERMSIG(c_status));
+       ATF_REQUIRE_EQ_MSG(WEXITSTATUS(c_status), 0, "child status: %d",
+           WEXITSTATUS(c_status));
 
-       /*
-        * ...and any other signal is an unexpected error.
-        */
-       if (did_sigchild) {
-               c_status = child_status;
-               if (c_status < 0)
-                       atf_tc_fail("waitpid: %d", -c_status);
-               else if (WIFEXITED(c_status) == 0)
-                       atf_tc_fail("child abnormal exit: %d", c_status);
-               else if (WEXITSTATUS(c_status) != 0)
-                       atf_tc_fail("c status: %d", WEXITSTATUS(c_status));
-               else {
-                       ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_STAT, &m_ds)
-                           != -1, "msgctl IPC_STAT: %d", errno);
+       ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_STAT, &m_ds) != -1,
+           "msgctl IPC_STAT: %d", errno);
 
-                       print_msqid_ds(&m_ds, 0600);
-                       atf_tc_pass();
-               }
-       } else
-               atf_tc_fail("sender: received unexpected signal");
+       print_msqid_ds(&m_ds, 0600);
 }
 
 ATF_TC_CLEANUP(msg, tc)
@@ -342,9 +300,10 @@
         * Remove the message queue if it exists.
         */
        sender_msqid = read_int("sender_msqid");
-       if (sender_msqid != -1)
-               if (msgctl(sender_msqid, IPC_RMID, NULL) == -1)
-                       err(1, "msgctl IPC_RMID");
+       if (sender_msqid == -1)
+               return;
+       if (msgctl(sender_msqid, IPC_RMID, NULL) == -1)
+               err(EXIT_FAILURE, "msgctl IPC_RMID");
 }
 
 void
@@ -358,8 +317,8 @@
            mp->msg_perm.cuid, mp->msg_perm.cgid,
            mp->msg_perm.mode & 0777);
 
-       printf("qnum %lu, qbytes %lu, lspid %d, lrpid %d\n",
-           mp->msg_qnum, (u_long)mp->msg_qbytes, mp->msg_lspid,
+       printf("qnum %lu, qbytes %ju, lspid %d, lrpid %d\n",
+           mp->msg_qnum, (uintmax_t)mp->msg_qbytes, mp->msg_lspid,
            mp->msg_lrpid);
 
        printf("stime: %s", ctime(&mp->msg_stime));
@@ -386,42 +345,46 @@
        int msqid, loop;
 
        if ((msqid = msgget(msgkey, 0)) == -1)
-               err(1, "receiver: msgget");
+               err(EXIT_FAILURE, "receiver: msgget");
 
        for (loop = 0; loop < maxloop; loop++) {
                /*
                 * Receive the first message, print it, and send an ACK.
                 */
-               if (msgrcv(msqid, &m, MESSAGE_TEXT_LEN, MTYPE_1, 0) != MESSAGE_TEXT_LEN)
-                       err(1, "receiver: msgrcv 1");
+               if (msgrcv(msqid, &m, MESSAGE_TEXT_LEN, MTYPE_1, 0)
+                   != MESSAGE_TEXT_LEN)
+                       err(EXIT_FAILURE, "receiver: msgrcv 1");
 
                printf("%s\n", m.mtext);
                if (strcmp(m.mtext, m1_str) != 0)
-                       err(1, "receiver: message 1 data isn't correct");
+                       errx(EXIT_FAILURE,
+                           "receiver: message 1 data isn't correct");
 
                m.mtype = MTYPE_1_ACK;
 
                if (msgsnd(msqid, &m, MESSAGE_TEXT_LEN, 0) == -1)
-                       err(1, "receiver: msgsnd ack 1");
+                       err(EXIT_FAILURE, "receiver: msgsnd ack 1");
 
                /*
                 * Receive the second message, print it, and send an ACK.
                 */
 
-               if (msgrcv(msqid, &m, MESSAGE_TEXT_LEN, MTYPE_2, 0) != MESSAGE_TEXT_LEN)
-                       err(1, "receiver: msgrcv 2");
+               if (msgrcv(msqid, &m, MESSAGE_TEXT_LEN, MTYPE_2, 0)
+                   != MESSAGE_TEXT_LEN)
+                       err(EXIT_FAILURE, "receiver: msgrcv 2");
 
                printf("%s\n", m.mtext);
                if (strcmp(m.mtext, m2_str) != 0)
-                       err(1, "receiver: message 2 data isn't correct");
+                       errx(EXIT_FAILURE,
+                           "receiver: message 2 data isn't correct");
 
                m.mtype = MTYPE_2_ACK;
 
                if (msgsnd(msqid, &m, MESSAGE_TEXT_LEN, 0) == -1)
-                       err(1, "receiver: msgsnd ack 2");
+                       err(EXIT_FAILURE, "receiver: msgsnd ack 2");
        }
 
-       exit(0);
+       exit(EXIT_SUCCESS);
 }
 
 /*
@@ -441,10 +404,11 @@
        struct sigaction sa;
        union semun sun;
        struct semid_ds s_ds;
-       sigset_t sigmask;
        int sender_semid;
        int i;
        int c_status;
+       int child_count;
+       pid_t wait_result;
 
        /*
         * Install a SIGSYS handler so that we can exit gracefully if
@@ -457,18 +421,6 @@
        ATF_REQUIRE_MSG(sigaction(SIGSYS, &sa, NULL) != -1,
            "sigaction SIGSYS: %d", errno);
 
-       /*
-        * Install a SIGCHLD handler to deal with all possible exit
-        * conditions of the receiver.
-        */
-       did_sigchild = 0;
-       child_count = 0;
-       sa.sa_handler = sigchld_handler;
-       sigemptyset(&sa.sa_mask);
-       sa.sa_flags = 0;
-       ATF_REQUIRE_MSG(sigaction(SIGCHLD, &sa, NULL) != -1,
-           "sigaction SIGCHLD: %d", errno);
-



Home | Main Index | Thread Index | Old Index