ipc/msgget0*: cleanup && convert to new API
The msgget04 mas merged into msgget02
Signed-off-by: Xiao Yang <yangx.jy@cn.fujitsu.com>
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
diff --git a/runtest/ltplite b/runtest/ltplite
index 1c5ffd0..83141d1 100644
--- a/runtest/ltplite
+++ b/runtest/ltplite
@@ -502,7 +502,6 @@
msgget01 msgget01
msgget02 msgget02
msgget03 msgget03
-msgget04 msgget04
msgrcv01 msgrcv01
msgrcv02 msgrcv02
diff --git a/runtest/stress.part3 b/runtest/stress.part3
index 7e43124..fcf27d0 100644
--- a/runtest/stress.part3
+++ b/runtest/stress.part3
@@ -418,7 +418,6 @@
msgget01 msgget01
msgget02 msgget02
msgget03 msgget03
-msgget04 msgget04
msgrcv01 msgrcv01
msgrcv02 msgrcv02
diff --git a/runtest/syscalls b/runtest/syscalls
index 9446f7d..884ab80 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -681,7 +681,6 @@
msgget01 msgget01
msgget02 msgget02
msgget03 msgget03
-msgget04 msgget04
msgrcv01 msgrcv01
msgrcv02 msgrcv02
diff --git a/runtest/syscalls-ipc b/runtest/syscalls-ipc
index 5592a00..8212222 100644
--- a/runtest/syscalls-ipc
+++ b/runtest/syscalls-ipc
@@ -15,7 +15,6 @@
msgget01 msgget01
msgget02 msgget02
msgget03 msgget03
-msgget04 msgget04
msgrcv01 msgrcv01
msgrcv02 msgrcv02
diff --git a/testcases/kernel/syscalls/.gitignore b/testcases/kernel/syscalls/.gitignore
index 533edd2..3201fa9 100644
--- a/testcases/kernel/syscalls/.gitignore
+++ b/testcases/kernel/syscalls/.gitignore
@@ -404,7 +404,6 @@
/ipc/msgget/msgget01
/ipc/msgget/msgget02
/ipc/msgget/msgget03
-/ipc/msgget/msgget04
/ipc/msgrcv/msgrcv01
/ipc/msgrcv/msgrcv02
/ipc/msgrcv/msgrcv03
diff --git a/testcases/kernel/syscalls/ipc/msgget/Makefile b/testcases/kernel/syscalls/ipc/msgget/Makefile
index f467389..f11974c 100644
--- a/testcases/kernel/syscalls/ipc/msgget/Makefile
+++ b/testcases/kernel/syscalls/ipc/msgget/Makefile
@@ -1,23 +1,22 @@
#
-# Copyright (c) International Business Machines Corp., 2001
+# Copyright (c) International Business Machines Corp., 2001
#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
-# the GNU General Public License for more details.
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+# the GNU General Public License for more details.
#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+# You should have received a copy of the GNU General Public License
+# along with this program.
#
top_srcdir ?= ../../../../..
include $(top_srcdir)/include/mk/testcases.mk
-include $(abs_srcdir)/../Makefile.inc
+include $(abs_srcdir)/../Makefile2.inc
include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/ipc/msgget/msgget01.c b/testcases/kernel/syscalls/ipc/msgget/msgget01.c
index e8208b7..8d2db4d 100644
--- a/testcases/kernel/syscalls/ipc/msgget/msgget01.c
+++ b/testcases/kernel/syscalls/ipc/msgget/msgget01.c
@@ -1,186 +1,82 @@
/*
+ * Copyright (c) International Business Machines Corp., 2001
*
- * Copyright (c) International Business Machines Corp., 2001
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+ * the GNU General Public License for more details.
*
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * You should have received a copy of the GNU General Public License
+ * along with this program.
*/
/*
- * NAME
- * msgget01.c
- *
* DESCRIPTION
- * msgget01 - create a message queue, write a message to it and
- * read it back.
- *
- * ALGORITHM
- * loop if that option was specified
- * create a message queue
- * check the return code
- * if failure, issue a FAIL message.
- * otherwise,
- * if doing functionality testing by writting a message to the queue,
- * reading it back and comparing the two.
- * if the messages are the same,
- * issue a PASS message
- * otherwise
- * issue a FAIL message
- * call cleanup
- *
- * USAGE: <for command-line>
- * msgget01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
- * where, -c n : Run n copies concurrently.
- * -f : Turn off functionality Testing.
- * -i n : Execute test n times.
- * -I x : Execute test for x seconds.
- * -P x : Pause for x seconds between iterations.
- * -t : Turn on syscall timing.
- *
- * HISTORY
- * 03/2001 - Written by Wayne Boyer
- *
- * RESTRICTIONS
- * none
+ * create a message queue, write a message to it and
+ * read it back.
*/
-#include "ipcmsg.h"
-
+#include <errno.h>
#include <string.h>
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/msg.h>
-char *TCID = "msgget01";
-int TST_TOTAL = 1;
+#include "tst_test.h"
+#include "libnewipc.h"
-int msg_q_1 = -1; /* to hold the message queue ID */
+static int queue_id = -1;
+static key_t msgkey;
-int main(int ac, char **av)
+static struct buf {
+ long type;
+ char text[MSGSIZE];
+} rcv_buf, snd_buf = {MSGTYPE, "hello, world"};
+
+static void verify_msgget(void)
{
- int lc;
- void check_functionality(void);
-
- tst_parse_opts(ac, av, NULL, NULL);
-
- setup(); /* global setup */
-
- /* The following loop checks looping state if -i option given */
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- /* reset tst_count in case we are looping */
- tst_count = 0;
-
- /*
- * Use TEST macro to make the call to create the message queue
- */
-
- TEST(msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RD | MSG_WR));
-
- if (TEST_RETURN == -1) {
- tst_resm(TFAIL, "%s call failed - errno = %d : %s",
- TCID, TEST_ERRNO, strerror(TEST_ERRNO));
- } else {
- msg_q_1 = TEST_RETURN;
- /*
- * write a message to the queue.
- * read back the message.
- * PASS the test if they are the same.
- */
- check_functionality();
- }
-
- /*
- * remove the message queue that was created and mark the ID
- * as invalid.
- */
- if (msg_q_1 != -1) {
- rm_queue(msg_q_1);
- msg_q_1 = -1;
- }
+ TEST(msgget(msgkey, IPC_CREAT | MSG_RW));
+ if (TEST_RETURN == -1) {
+ tst_res(TFAIL | TTERRNO, "msgget() failed");
+ return;
}
- cleanup();
- tst_exit();
+ queue_id = TEST_RETURN;
+
+ if (msgsnd(queue_id, &snd_buf, MSGSIZE, 0) == -1)
+ tst_brk(TBROK | TERRNO, "msgsnd() failed");
+
+ if (msgrcv(queue_id, &rcv_buf, MSGSIZE, MSGTYPE, IPC_NOWAIT) == -1)
+ tst_brk(TBROK | TERRNO, "msgrcv() failed");
+
+ if (strcmp(snd_buf.text, rcv_buf.text) == 0)
+ tst_res(TPASS, "message received = message sent");
+ else
+ tst_res(TFAIL, "message received != message sent");
}
-/*
- * check_functionality() - check the functionality of the tested system call.
- */
-void check_functionality(void)
+static void setup(void)
{
- int i = 0;
- MSGBUF snd_buf, rcv_buf;
+ msgkey = GETIPCKEY();
+}
- /* EAGLE: Houston, Tranquility Base here. The Eagle has landed! */
- char *queue_msg =
- "Qston, check_functionality here. The message has queued!";
-
- /*
- * copy our message into the buffer and then set the type.
- */
- do {
- snd_buf.mtext[i++] = *queue_msg;
- } while (*queue_msg++ != '\0');
-
- snd_buf.mtype = MSGTYPE;
-
- /* send the message */
- if (msgsnd(msg_q_1, &snd_buf, MSGSIZE, 0) == -1) {
- tst_brkm(TBROK, cleanup, "Could not send a message in the "
- "check_functionality() routine.");
- }
-
- /* receive the message */
- if (msgrcv(msg_q_1, &rcv_buf, MSGSIZE, MSGTYPE, IPC_NOWAIT) == -1) {
- tst_brkm(TBROK, cleanup, "Could not read a messages in the "
- "check_functionality() routine.");
- }
-
- if (strcmp(snd_buf.mtext, rcv_buf.mtext) == 0) {
- tst_resm(TPASS, "message received = message sent");
- } else {
- tst_resm(TFAIL, "message received != message sent");
+static void cleanup(void)
+{
+ if (queue_id != -1 && msgctl(queue_id, IPC_RMID, NULL)) {
+ tst_res(TWARN | TERRNO, "failed to delete message queue %i",
+ queue_id);
}
}
-/*
- * setup() - performs all the ONE TIME setup for this test.
- */
-void setup(void)
-{
-
- tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
- TEST_PAUSE;
-
- /*
- * Create a temporary directory and cd into it.
- * This helps to ensure that a unique msgkey is created.
- * See ../lib/libipc.c for more information.
- */
- tst_tmpdir();
-
- msgkey = getipckey();
-}
-
-/*
- * cleanup() - performs all the ONE TIME cleanup for this test at completion
- * or premature exit.
- */
-void cleanup(void)
-{
- /* if it exists, remove the message queue that was created */
- rm_queue(msg_q_1);
-
- tst_rmdir();
-
-}
+static struct tst_test test = {
+ .tid = "msgget01",
+ .setup = setup,
+ .cleanup = cleanup,
+ .test_all = verify_msgget,
+ .needs_tmpdir = 1
+};
diff --git a/testcases/kernel/syscalls/ipc/msgget/msgget02.c b/testcases/kernel/syscalls/ipc/msgget/msgget02.c
index be9bc61..a3fcb38 100644
--- a/testcases/kernel/syscalls/ipc/msgget/msgget02.c
+++ b/testcases/kernel/syscalls/ipc/msgget/msgget02.c
@@ -1,176 +1,123 @@
/*
+ * Copyright (c) International Business Machines Corp., 2001
*
- * Copyright (c) International Business Machines Corp., 2001
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+ * the GNU General Public License for more details.
*
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * You should have received a copy of the GNU General Public License
+ * along with this program.
*/
/*
- * NAME
- * msgget02.c
- *
* DESCRIPTION
- * msgget02 - test for EEXIST and ENOENT errors
+ * 1) msgget(2) fails if a message queue exists for key and msgflg
+ * specified both IPC_CREAT and IPC_EXCL.
+ * 2) msgget(2) fails if no message queue exists for key and msgflg
+ * did not specify IPC_CREAT.
+ * 3) msgget(2) fails if a message queue exists for key, but the
+ * calling process does not have permission to access the queue,
+ * and does not have the CAP_IPC_OWNER capability.
*
- * ALGORITHM
- * create a message queue
- * loop if that option was specified
- * try to recreate the same queue - test #1
- * try to access a queue that doesn't exist - tests #2 & #3
- * check the errno value
- * issue a PASS message if we get EEXIST or ENOENT depening on test
- * otherwise, the tests fails
- * issue a FAIL message
- * break any remaining tests
- * call cleanup
- *
- * USAGE: <for command-line>
- * msgget02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- * where, -c n : Run n copies concurrently.
- * -e : Turn on errno logging.
- * -i n : Execute test n times.
- * -I x : Execute test for x seconds.
- * -P x : Pause for x seconds between iterations.
- * -t : Turn on syscall timing.
- *
- * HISTORY
- * 03/2001 - Written by Wayne Boyer
- *
- * 28/03/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
- * - Fix concurrency issue. The second key used for this test was
- * sometime conflicting with the key from another task.
- * Generate a valid second key through getipckey to avoid conflicts.
- *
- * RESTRICTIONS
- * none
*/
+#include <errno.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/msg.h>
+#include <pwd.h>
-#include "test.h"
+#include "tst_test.h"
+#include "libnewipc.h"
-#include "ipcmsg.h"
+static key_t msgkey, msgkey1;
+static int queue_id = -1;
+static struct passwd *pw;
-char *TCID = "msgget02";
-int TST_TOTAL = 3;
-
-struct test_case_t {
- int error;
- int msgkey;
+static struct tcase {
+ int *key;
int flags;
-} TC[] = {
- {
- EEXIST, 0, IPC_CREAT | IPC_EXCL}, {
- ENOENT, 1, IPC_PRIVATE}, {
- ENOENT, 1, IPC_EXCL}
+ int exp_err;
+ /*1: nobody expected 0: root expected */
+ int exp_user;
+} tcases[] = {
+ {&msgkey, IPC_CREAT | IPC_EXCL, EEXIST, 0},
+ {&msgkey1, IPC_PRIVATE, ENOENT, 0},
+ {&msgkey1, IPC_EXCL, ENOENT, 0},
+ {&msgkey, MSG_RD, EACCES, 1},
+ {&msgkey, MSG_WR, EACCES, 1},
+ {&msgkey, MSG_RW, EACCES, 1}
};
-key_t msgkey1;
-int msg_q_1 = -1; /* The message queue id created in setup */
-
-int main(int ac, char **av)
+static void verify_msgget(struct tcase *tc)
{
- int lc;
- int i;
- key_t key;
+ TEST(msgget(*tc->key, tc->flags));
- tst_parse_opts(ac, av, NULL, NULL);
+ if (TEST_RETURN != -1) {
+ tst_res(TFAIL, "msgget() succeeded unexpectedly");
+ return;
+ }
- setup(); /* global setup */
+ if (TEST_ERRNO == tc->exp_err) {
+ tst_res(TPASS | TTERRNO, "msgget() failed as expected");
+ } else {
+ tst_res(TFAIL | TTERRNO, "msgget() failed unexpectedly,"
+ " expected %s", tst_strerrno(tc->exp_err));
+ }
+}
- /* The following loop checks looping state if -i option given */
+static void do_test(unsigned int n)
+{
+ pid_t pid;
+ struct tcase *tc = &tcases[n];
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- /* reset tst_count in case we are looping */
- tst_count = 0;
-
- /* loop through the test cases */
-
- for (i = 0; i < TST_TOTAL; i++) {
-
- if (TC[i].msgkey == 0)
- key = msgkey;
- else
- key = msgkey1;
-
- TEST(msgget(key, TC[i].flags));
-
- if (TEST_RETURN != -1) {
- tst_resm(TFAIL, "msgget() call succeeded "
- "on expected fail");
- continue;
- }
-
- switch (TEST_ERRNO) {
- case ENOENT:
- /*FALLTHROUGH*/ case EEXIST:
- if (TEST_ERRNO == TC[i].error) {
- tst_resm(TPASS, "expected failure - "
- "errno = %d : %s", TEST_ERRNO,
- strerror(TEST_ERRNO));
- break;
- }
- /*FALLTHROUGH*/ default:
- tst_resm(TFAIL, "call failed with an "
- "unexpected error - %d : %s",
- TEST_ERRNO, strerror(TEST_ERRNO));
- break;
- }
+ if (tc->exp_user == 0) {
+ verify_msgget(tc);
+ } else {
+ pid = SAFE_FORK();
+ if (pid) {
+ tst_reap_children();
+ } else {
+ SAFE_SETUID(pw->pw_uid);
+ verify_msgget(tc);
+ exit(0);
}
}
-
- cleanup();
-
- tst_exit();
}
-/*
- * setup() - performs all the ONE TIME setup for this test.
- */
-void setup(void)
+static void setup(void)
{
+ msgkey = GETIPCKEY();
+ msgkey1 = GETIPCKEY();
- tst_sig(NOFORK, DEF_HANDLER, cleanup);
+ queue_id = msgget(msgkey, IPC_CREAT | IPC_EXCL);
+ if (queue_id == -1)
+ tst_brk(TBROK | TERRNO, "can't create message queue");
- TEST_PAUSE;
+ pw = SAFE_GETPWNAM("nobody");
+}
- /*
- * Create a temporary directory and cd into it.
- * This helps to ensure that a unique msgkey is created.
- * See ../lib/libipc.c for more information.
- */
- tst_tmpdir();
-
- msgkey = getipckey();
- msgkey1 = getipckey();
-
- /* now we have a key, so let's create a message queue */
- if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL)) == -1) {
- system("ipcs > /tmp/toto");
- system("ps -aux >> /tmp/toto");
- tst_brkm(TBROK, cleanup, "Can't create message queue");
+static void cleanup(void)
+{
+ if (queue_id != -1 && msgctl(queue_id, IPC_RMID, NULL)) {
+ tst_res(TWARN | TERRNO, "failed to delete message queue %i",
+ queue_id);
}
}
-/*
- * cleanup() - performs all the ONE TIME cleanup for this test at completion
- * or premature exit.
- */
-void cleanup(void)
-{
- /* if it exists, remove the message queue that was created. */
- rm_queue(msg_q_1);
-
- tst_rmdir();
-
-}
+static struct tst_test test = {
+ .tid = "msgget02",
+ .needs_tmpdir = 1,
+ .needs_root = 1,
+ .forks_child = 1,
+ .tcnt = ARRAY_SIZE(tcases),
+ .setup = setup,
+ .cleanup = cleanup,
+ .test = do_test
+};
diff --git a/testcases/kernel/syscalls/ipc/msgget/msgget03.c b/testcases/kernel/syscalls/ipc/msgget/msgget03.c
index 2eda252..0b7c66a 100644
--- a/testcases/kernel/syscalls/ipc/msgget/msgget03.c
+++ b/testcases/kernel/syscalls/ipc/msgget/msgget03.c
@@ -1,168 +1,98 @@
/*
+ * Copyright (c) International Business Machines Corp., 2001
*
- * Copyright (c) International Business Machines Corp., 2001
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+ * the GNU General Public License for more details.
*
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * You should have received a copy of the GNU General Public License
+ * along with this program.
*/
/*
- * NAME
- * msgget03.c
- *
* DESCRIPTION
- * msgget03 - test for an ENOSPC error by using up all available
- * message queues.
+ * test for an ENOSPC error by using up all available
+ * message queues.
*
- * ALGORITHM
- * Get all the message queues that can be allocated
- * loop if that option was specified
- * Try to get one more message queue
- * check the errno value
- * issue a PASS message if we get ENOSPC
- * otherwise, the tests fails
- * issue a FAIL message
- * break any remaining tests
- * call cleanup
- *
- * USAGE: <for command-line>
- * msgget03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- * where, -c n : Run n copies concurrently.
- * -e : Turn on errno logging.
- * -i n : Execute test n times.
- * -I x : Execute test for x seconds.
- * -P x : Pause for x seconds between iterations.
- * -t : Turn on syscall timing.
- *
- * HISTORY
- * 03/2001 - Written by Wayne Boyer
- *
- * RESTRICTIONS
- * none
*/
-#include "test.h"
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/msg.h>
+#include <stdlib.h>
-#include "ipcmsg.h"
+#include "tst_test.h"
+#include "libnewipc.h"
-char *TCID = "msgget03";
-int TST_TOTAL = 1;
+static int maxmsgs;
+static int *queues;
+static key_t msgkey;
-int maxmsgs = 0;
-
-int *msg_q_arr = NULL; /* hold the id's that we create */
-int num_queue = 0; /* count the queues created */
-
-int main(int ac, char **av)
+static void verify_msgget(void)
{
- int lc;
- int msg_q;
+ TEST(msgget(msgkey + maxmsgs, IPC_CREAT | IPC_EXCL));
+ if (TEST_RETURN != -1)
+ tst_res(TFAIL, "msgget() succeeded unexpectedly");
- tst_parse_opts(ac, av, NULL, NULL);
-
- setup(); /* global setup */
-
- /* The following loop checks looping state if -i option given */
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- /* reset tst_count in case we are looping */
- tst_count = 0;
-
- /*
- * Use a while loop to create the maximum number of queues.
- * When we get an error, check for ENOSPC.
- */
- while ((msg_q =
- msgget(msgkey + num_queue,
- IPC_CREAT | IPC_EXCL)) != -1) {
- msg_q_arr[num_queue] = msg_q;
- if (num_queue == maxmsgs) {
- tst_resm(TINFO, "The maximum number of message"
- " queues (%d) has been reached",
- maxmsgs);
- break;
- }
- num_queue++;
- }
-
- switch (errno) {
- case ENOSPC:
- tst_resm(TPASS, "expected failure - errno = %d : %s",
- TEST_ERRNO, strerror(TEST_ERRNO));
- break;
- default:
- tst_resm(TFAIL, "call failed with an "
- "unexpected error - %d : %s",
- TEST_ERRNO, strerror(TEST_ERRNO));
- break;
- }
- }
-
- cleanup();
-
- tst_exit();
-}
-
-/*
- * setup() - performs all the ONE TIME setup for this test.
- */
-void setup(void)
-{
-
- tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
- TEST_PAUSE;
-
- /*
- * Create a temporary directory and cd into it.
- * This helps to ensure that a unique msgkey is created.
- * See ../lib/libipc.c for more information.
- */
- tst_tmpdir();
-
- msgkey = getipckey();
-
- maxmsgs = get_max_msgqueues();
- if (maxmsgs < 0)
- tst_brkm(TBROK, cleanup, "get_max_msgqueues failed");
-
- msg_q_arr = (int *)calloc(maxmsgs, sizeof(int));
- if (msg_q_arr == NULL) {
- tst_brkm(TBROK, cleanup, "Couldn't allocate memory "
- "for msg_q_arr: calloc() failed");
+ if (TEST_ERRNO == ENOSPC) {
+ tst_res(TPASS | TTERRNO, "msgget() failed as expected");
+ } else {
+ tst_res(TFAIL | TTERRNO, "msgget() failed unexpectedly,"
+ " expected ENOSPC");
}
}
-/*
- * cleanup() - performs all the ONE TIME cleanup for this test at completion
- * or premature exit.
- */
-void cleanup(void)
+static void setup(void)
{
- int i;
+ int res, num;
- /*
- * remove the message queues if they were created
- */
+ msgkey = GETIPCKEY();
- if (msg_q_arr != NULL) {
- for (i = 0; i < num_queue; i++) {
- rm_queue(msg_q_arr[i]);
- }
- (void)free(msg_q_arr);
+ SAFE_FILE_SCANF("/proc/sys/kernel/msgmni", "%i", &maxmsgs);
+
+ queues = SAFE_MALLOC(maxmsgs * sizeof(int));
+
+ for (num = 0; num < maxmsgs; num++) {
+ queues[num] = -1;
+
+ res = msgget(msgkey + num, IPC_CREAT | IPC_EXCL);
+ if (res != -1)
+ queues[num] = res;
}
- tst_rmdir();
-
+ tst_res(TINFO, "The maximum number of message queues (%d) reached",
+ maxmsgs);
}
+
+static void cleanup(void)
+{
+ int num;
+
+ if (!queues)
+ return;
+
+ for (num = 0; num < maxmsgs; num++) {
+ if (queues[num] != -1 && msgctl(queues[num], IPC_RMID, NULL)) {
+ tst_res(TWARN | TERRNO,
+ "failed to delete message queue %i",
+ queues[num]);
+ }
+ }
+
+ free(queues);
+}
+
+static struct tst_test test = {
+ .tid = "msgget03",
+ .needs_tmpdir = 1,
+ .setup = setup,
+ .cleanup = cleanup,
+ .test_all = verify_msgget
+};
diff --git a/testcases/kernel/syscalls/ipc/msgget/msgget04.c b/testcases/kernel/syscalls/ipc/msgget/msgget04.c
deleted file mode 100644
index 22f8b38..0000000
--- a/testcases/kernel/syscalls/ipc/msgget/msgget04.c
+++ /dev/null
@@ -1,169 +0,0 @@
-/*
- *
- * Copyright (c) International Business Machines Corp., 2001
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * NAME
- * msgget04.c
- *
- * DESCRIPTION
- * msgget04 - test for an EACCES error by creating a message queue
- * with no read or write permission and then attempting
- * to access it with various permissions.
- *
- * ALGORITHM
- * Create a message queue with no read or write permission
- * loop if that option was specified
- * Try to access the message queue with various permissions
- * check the errno value
- * issue a PASS message if we get EACCES
- * otherwise, the tests fails
- * issue a FAIL message
- * break any remaining tests
- * call cleanup
- *
- * USAGE: <for command-line>
- * msgget04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
- * where, -c n : Run n copies concurrently.
- * -e : Turn on errno logging.
- * -i n : Execute test n times.
- * -I x : Execute test for x seconds.
- * -P x : Pause for x seconds between iterations.
- * -t : Turn on syscall timing.
- *
- * HISTORY
- * 03/2001 - Written by Wayne Boyer
- *
- * RESTRICTIONS
- * none
- */
-#include <pwd.h>
-#include "test.h"
-
-#include "ipcmsg.h"
-
-char *TCID = "msgget04";
-int TST_TOTAL = 3;
-
-char nobody_uid[] = "nobody";
-struct passwd *ltpuser;
-
-int msg_q_1 = -1; /* to hold the message queue id */
-
-int test_flags[] = { MSG_RD, MSG_WR, MSG_RD | MSG_WR };
-
-int main(int ac, char **av)
-{
- int lc;
- int i;
-
- tst_parse_opts(ac, av, NULL, NULL);
-
- setup(); /* global setup */
-
- /* The following loop checks looping state if -i option given */
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- /* reset tst_count in case we are looping */
- tst_count = 0;
-
- /* loop through the test cases */
-
- for (i = 0; i < TST_TOTAL; i++) {
- /*
- * Try to access the message queue with specified
- * permissions.
- */
-
- TEST(msgget(msgkey, test_flags[i]));
-
- if (TEST_RETURN != -1) {
- tst_resm(TFAIL, "call succeeded "
- "when EACCES error expected");
- continue;
- }
-
- switch (TEST_ERRNO) {
- case EACCES:
- tst_resm(TPASS, "expected failure - errno = "
- "%d : %s", TEST_ERRNO,
- strerror(TEST_ERRNO));
- break;
- default:
- tst_resm(TFAIL, "call failed with an "
- "unexpected error - %d : %s",
- TEST_ERRNO, strerror(TEST_ERRNO));
- break;
- }
- }
- }
-
- cleanup();
-
- tst_exit();
-}
-
-/*
- * setup() - performs all the ONE TIME setup for this test.
- */
-void setup(void)
-{
- tst_require_root();
-
- tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
- TEST_PAUSE;
-
- /* Switch to nobody user for correct error code collection */
- ltpuser = getpwnam(nobody_uid);
- if (setuid(ltpuser->pw_uid) == -1) {
- tst_resm(TINFO, "setuid failed to "
- "to set the effective uid to %d", ltpuser->pw_uid);
- perror("setuid");
- }
-
- /*
- * Create a temporary directory and cd into it.
- * This helps to ensure that a unique msgkey is created.
- * See ../lib/libipc.c for more information.
- */
- tst_tmpdir();
-
- msgkey = getipckey();
-
- /*
- * Create the message queue without specifying permissions.
- */
- if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL)) == -1) {
- tst_brkm(TBROK, cleanup, "Could not create message queue"
- " - errno = %d : %s", errno, strerror(errno));
- }
-}
-
-/*
- * cleanup() - performs all the ONE TIME cleanup for this test at completion
- * or premature exit.
- */
-void cleanup(void)
-{
- /* if it exists, remove the message queue */
- rm_queue(msg_q_1);
-
- tst_rmdir();
-
-}