Source-Changes-HG archive

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

[src/tls-maxphys]: src/tests/kernel Replace the atexit() routines with ATF_TC...



details:   https://anonhg.NetBSD.org/src/rev/138d2e38f533
branches:  tls-maxphys
changeset: 852947:138d2e38f533
user:      pgoyette <pgoyette%NetBSD.org@localhost>
date:      Tue Nov 06 18:31:54 2012 +0000

description:
Replace the atexit() routines with ATF_TC_CLEANUP()

Factor out the generation of token_keys to a separate routine, called
from each test case.  And make sure we remove the mkdtemp()-created
directory after we're finished with it.

diffstat:

 tests/kernel/t_sysv.c |  816 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 816 insertions(+), 0 deletions(-)

diffs (truncated from 820 to 300 lines):

diff -r d52b61521e85 -r 138d2e38f533 tests/kernel/t_sysv.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/kernel/t_sysv.c     Tue Nov 06 18:31:54 2012 +0000
@@ -0,0 +1,816 @@
+/*     $NetBSD: t_sysv.c,v 1.2.2.2 2012/11/06 18:31:54 pgoyette Exp $  */
+
+/*-
+ * Copyright (c) 1999, 2007 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
+ * NASA Ames Research Center, and by Andrew Doran.
+ *
+ * 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.
+ */
+
+/*
+ * Test the SVID-compatible Message Queue facility.
+ */
+
+#include <atf-c.h>
+
+#include <err.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <sys/ipc.h>
+#include <sys/msg.h>
+#include <sys/param.h>
+#include <sys/sem.h>
+#include <sys/shm.h>
+#include <sys/wait.h>
+
+volatile int did_sigsys, did_sigchild;
+volatile int child_status, child_count;
+
+void   sigsys_handler(int);
+void   sigchld_handler(int);
+
+key_t  get_ftok(int);
+
+void   print_msqid_ds(struct msqid_ds *, mode_t);
+void   receiver(void);
+
+void   print_semid_ds(struct semid_ds *, mode_t);
+void   waiter(void);
+
+void   print_shmid_ds(struct shmid_ds *, mode_t);
+void   sharer(void);
+
+#define        MESSAGE_TEXT_LEN        256
+
+struct mymsg {
+       long    mtype;
+       char    mtext[MESSAGE_TEXT_LEN];
+};
+
+const char *m1_str = "California is overrated.";
+const char *m2_str = "The quick brown fox jumped over the lazy dog.";
+
+size_t pgsize;
+
+#define        MTYPE_1         1
+#define        MTYPE_1_ACK     2
+
+#define        MTYPE_2         3
+#define        MTYPE_2_ACK     4
+
+int    sender_msqid = -1;
+int    sender_semid = -1;
+int    sender_shmid = -1;
+pid_t  child_pid;
+
+key_t  msgkey, semkey, shmkey;
+
+int    maxloop = 1;
+
+union semun {
+       int     val;            /* value for SETVAL */
+       struct  semid_ds *buf;  /* buffer for IPC_{STAT,SET} */
+       u_short *array;         /* array for GETALL & SETALL */
+};
+
+
+void
+sigsys_handler(int signo)
+{
+
+       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;
+       char token_key[64], token_dir[64];
+       char *tmpdir;
+       key_t key;
+
+       strlcpy(token_key, "/tmp/t_sysv.XXXXXX", sizeof(token_key));
+       tmpdir = mkdtemp(token_key);
+       ATF_REQUIRE_MSG(tmpdir != NULL, "mkdtemp() failed: %d", errno);
+
+       strlcpy(token_dir, tmpdir, sizeof(token_dir));
+       strlcpy(token_key, tmpdir, sizeof(token_key));
+       strlcat(token_key, "/token_key", sizeof(token_key));
+
+       /* Create the file, since ftok() requires it to exist! */
+
+       fd = open(token_key, O_RDWR | O_CREAT | O_EXCL);
+       if (fd == -1) {
+               rmdir(tmpdir);
+               atf_tc_fail("open() of temp file failed: %d", errno);
+               return (key_t)-1;
+       } else
+               close(fd);
+
+       key = ftok(token_key, id);
+
+       ATF_REQUIRE_MSG(unlink(token_key) != -1, "unlink() failed: %d", errno);
+       ATF_REQUIRE_MSG(rmdir(token_dir) != -1, "rmdir() failed: %d", errno);
+
+       return key;
+}
+
+ATF_TC_WITH_CLEANUP(msg);
+ATF_TC_HEAD(msg, tc)
+{  
+
+       atf_tc_set_md_var(tc, "timeout", "3");
+       atf_tc_set_md_var(tc, "descr", "Checks sysvmsg passing");
+}
+
+ATF_TC_BODY(msg, tc)
+{
+       struct sigaction sa;
+       struct msqid_ds m_ds;
+       struct mymsg m;
+       sigset_t sigmask;
+       int loop;
+       int c_status;
+
+       /*
+        * Install a SIGSYS handler so that we can exit gracefully if
+        * System V Message Queue support isn't in the kernel.
+        */
+       did_sigsys = 0;
+       sa.sa_handler = sigsys_handler;
+       sigemptyset(&sa.sa_mask);
+       sa.sa_flags = 0;
+       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");
+
+       sender_msqid = msgget(msgkey, IPC_CREAT | 0640);
+       ATF_REQUIRE_MSG(sender_msqid != -1, "msgget: %d", errno);
+
+       if (did_sigsys) {
+               atf_tc_skip("SYSV Message Queue not supported");
+               return;
+       }
+
+       ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_STAT, &m_ds) != -1,
+       "msgctl IPC_STAT 1: %d", errno);
+
+       print_msqid_ds(&m_ds, 0640);
+
+       m_ds.msg_perm.mode = (m_ds.msg_perm.mode & ~0777) | 0600;
+
+       ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_SET, &m_ds) != -1,
+           "msgctl IPC_SET: %d", errno);
+
+       memset(&m_ds, 0, sizeof(m_ds));
+
+       ATF_REQUIRE_MSG(msgctl(sender_msqid, IPC_STAT, &m_ds) != -1,
+           "msgctl IPC_STAT 2: %d", errno);
+
+       ATF_REQUIRE_MSG((m_ds.msg_perm.mode & 0777) == 0600,
+           "IPC_SET of mode didn't hold");
+
+       print_msqid_ds(&m_ds, 0600);
+
+       switch ((child_pid = fork())) {
+       case -1:
+               atf_tc_fail("fork: %d", errno);
+               return;
+
+       case 0:
+               child_count++;
+               receiver();
+               break;
+
+       default:
+               break;
+       }
+
+       for (loop = 0; loop < maxloop; loop++) {
+               /*
+                * Send the first message to the receiver and wait for the ACK.
+                */
+               m.mtype = MTYPE_1;
+               strcpy(m.mtext, m1_str);
+               ATF_REQUIRE_MSG(msgsnd(sender_msqid, &m, sizeof(m), 0) != -1,
+                   "sender: msgsnd 1: %d", errno);
+
+               ATF_REQUIRE_MSG(msgrcv(sender_msqid, &m, sizeof(m),
+                                      MTYPE_1_ACK, 0) == sizeof(m),
+                   "sender: msgrcv 1 ack: %d", errno);
+
+               print_msqid_ds(&m_ds, 0600);
+
+               /*
+                * Send the second message to the receiver and wait for the ACK.
+                */
+               m.mtype = MTYPE_2;
+               strcpy(m.mtext, m2_str);
+               ATF_REQUIRE_MSG(msgsnd(sender_msqid, &m, sizeof(m), 0) != -1,
+                   "sender: msgsnd 2: %d", errno);
+
+               ATF_REQUIRE_MSG(msgrcv(sender_msqid, &m, sizeof(m),
+                                      MTYPE_2_ACK, 0) == sizeof(m),
+                   "sender: msgrcv 2 ack: %d", errno);
+       }
+
+       /*
+        * Wait for child to finish
+        */
+       sigemptyset(&sigmask);
+       (void) sigsuspend(&sigmask);
+
+       /*
+        * ...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);



Home | Main Index | Thread Index | Old Index