/*-
* 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.
*/
struct testmsg {
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
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 */
};
/* Writes an integer to a file. To be used from the body of the test
* cases below to pass any global identifiers to the cleanup routine. */
static void
write_int(const char *path, const int value)
{
int output;
/* Reads an integer from a file. To be used from the cleanup routines
* of the test cases below. */
static int
read_int(const char *path)
{
int input, value;
input = open(path, O_RDONLY);
if (input == -1)
return -1;
ATF_TC_BODY(msg, tc)
{
struct sigaction sa;
struct msqid_ds m_ds;
struct testmsg m;
int sender_msqid;
int loop;
int c_status;
pid_t wait_result;
/*
* 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);
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, MESSAGE_TEXT_LEN,
0) != -1, "sender: msgsnd 1: %d", errno);
/*
* 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, MESSAGE_TEXT_LEN, 0)
!= -1, "sender: msgsnd 2: %d", errno);
ATF_TC_BODY(sem, tc)
{
struct sigaction sa;
union semun sun;
struct semid_ds s_ds;
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
* System V Semaphore 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);
ATF_REQUIRE_MSG((s_ds.sem_perm.mode & 0777) == 0600,
"IPC_SET of mode didn't hold");
print_semid_ds(&s_ds, 0600);
fflush(stdout);
for (child_count = 0; child_count < 5; child_count++) {
switch ((child_pid = fork())) {
case -1:
atf_tc_fail("fork: %d", errno);
return;
case 0:
waiter();
break;
default:
break;
}
}
/*
* Wait for all of the waiters to be attempting to acquire the
* semaphore.
*/
for (;;) {
i = semctl(sender_semid, 0, GETNCNT);
if (i == -1)
atf_tc_fail("semctl GETNCNT: %d", i);
if (i == 5)
break;
}
/*
* Now set the thundering herd in motion by initializing the
* semaphore to the value 1.
*/
sun.val = 1;
ATF_REQUIRE_MSG(semctl(sender_semid, 0, SETVAL, sun) != -1,
"sender: semctl SETVAL to 1: %d", errno);
/*
* Wait for all children to finish
*/
while (child_count-- > 0) {
wait_result = wait(&c_status);
ATF_REQUIRE_MSG(wait_result != -1, "wait failed: %s",
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));
ATF_TC_BODY(shm, tc)
{
struct sigaction sa;
struct shmid_ds s_ds;
char *shm_buf;
int sender_shmid;
int c_status;
pid_t wait_result;
/*
* Install a SIGSYS handler so that we can exit gracefully if
* System V Shared Memory 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);
/*
* Remove the shared memory area if it exists.
*/
sender_shmid = read_int("sender_shmid");
if (sender_shmid == -1)
return;
if (shmctl(sender_shmid, IPC_RMID, NULL) == -1)
err(EXIT_FAILURE, "shmctl IPC_RMID");
}