blob: 7c1527d89ad34be3a0b104eb6f01874da55c4395 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
22 * msgget03.c
23 *
24 * DESCRIPTION
25 * msgget03 - test for an ENOSPC error by using up all available
26 * message queues.
27 *
28 * ALGORITHM
29 * Get all the message queues that can be allocated
30 * loop if that option was specified
31 * Try to get one more message queue
32 * check the errno value
33 * issue a PASS message if we get ENOSPC
34 * otherwise, the tests fails
35 * issue a FAIL message
36 * break any remaining tests
37 * call cleanup
38 *
39 * USAGE: <for command-line>
40 * msgget03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
41 * where, -c n : Run n copies concurrently.
42 * -e : Turn on errno logging.
43 * -i n : Execute test n times.
44 * -I x : Execute test for x seconds.
45 * -P x : Pause for x seconds between iterations.
46 * -t : Turn on syscall timing.
47 *
48 * HISTORY
49 * 03/2001 - Written by Wayne Boyer
50 *
51 * RESTRICTIONS
52 * none
53 */
54
55#include "test.h"
plars865695b2001-08-27 22:15:12 +000056
robbiew23499f02002-11-18 19:54:58 +000057#include "ipcmsg.h"
plars865695b2001-08-27 22:15:12 +000058
59char *TCID = "msgget03";
60int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000061
robbiew7805c0e2005-08-02 19:38:41 +000062int maxmsgs = 0;
plars865695b2001-08-27 22:15:12 +000063
robbiew7805c0e2005-08-02 19:38:41 +000064int *msg_q_arr = NULL; /* hold the id's that we create */
plars865695b2001-08-27 22:15:12 +000065int num_queue = 0; /* count the queues created */
66
robbiew23499f02002-11-18 19:54:58 +000067int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000068{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020069 int lc;
Cyril Hrubis74225622014-06-02 17:54:38 +020070 const char *msg;
subrata_modak19bace22008-04-06 11:16:42 +000071 int msg_q;
plars865695b2001-08-27 22:15:12 +000072
Cyril Hrubis74225622014-06-02 17:54:38 +020073 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080074 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000075
subrata_modak56207ce2009-03-23 13:35:39 +000076 setup(); /* global setup */
plars865695b2001-08-27 22:15:12 +000077
78 /* The following loop checks looping state if -i option given */
79
80 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080081 /* reset tst_count in case we are looping */
82 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +000083
plars865695b2001-08-27 22:15:12 +000084 /*
subrata_modak19bace22008-04-06 11:16:42 +000085 * Use a while loop to create the maximum number of queues.
86 * When we get an error, check for ENOSPC.
plars865695b2001-08-27 22:15:12 +000087 */
subrata_modak56207ce2009-03-23 13:35:39 +000088 while ((msg_q =
89 msgget(msgkey + num_queue,
90 IPC_CREAT | IPC_EXCL)) != -1) {
subrata_modak19bace22008-04-06 11:16:42 +000091 msg_q_arr[num_queue] = msg_q;
92 if (num_queue == maxmsgs) {
93 tst_resm(TINFO, "The maximum number of message"
94 " queues (%d) has been reached",
95 maxmsgs);
96 break;
97 }
98 num_queue++;
plars865695b2001-08-27 22:15:12 +000099 }
plars865695b2001-08-27 22:15:12 +0000100
subrata_modak56207ce2009-03-23 13:35:39 +0000101 switch (errno) {
plars865695b2001-08-27 22:15:12 +0000102 case ENOSPC:
103 tst_resm(TPASS, "expected failure - errno = %d : %s",
104 TEST_ERRNO, strerror(TEST_ERRNO));
105 break;
106 default:
107 tst_resm(TFAIL, "call failed with an "
108 "unexpected error - %d : %s",
109 TEST_ERRNO, strerror(TEST_ERRNO));
subrata_modak56207ce2009-03-23 13:35:39 +0000110 break;
plars865695b2001-08-27 22:15:12 +0000111 }
112 }
113
114 cleanup();
115
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800116 tst_exit();
plars865695b2001-08-27 22:15:12 +0000117}
118
119/*
120 * setup() - performs all the ONE TIME setup for this test.
121 */
subrata_modak56207ce2009-03-23 13:35:39 +0000122void setup(void)
plars865695b2001-08-27 22:15:12 +0000123{
Garrett Cooper2c282152010-12-16 00:55:50 -0800124
plars865695b2001-08-27 22:15:12 +0000125 tst_sig(NOFORK, DEF_HANDLER, cleanup);
126
plars865695b2001-08-27 22:15:12 +0000127 TEST_PAUSE;
128
129 /*
130 * Create a temporary directory and cd into it.
131 * This helps to ensure that a unique msgkey is created.
132 * See ../lib/libipc.c for more information.
133 */
134 tst_tmpdir();
135
136 msgkey = getipckey();
137
robbiew7805c0e2005-08-02 19:38:41 +0000138 maxmsgs = get_max_msgqueues();
subrata_modak5b693742008-02-22 08:33:53 +0000139 if (maxmsgs < 0)
Garrett Cooper460095d2010-12-19 10:18:06 -0800140 tst_brkm(TBROK, cleanup, "get_max_msgqueues failed");
robbiew7805c0e2005-08-02 19:38:41 +0000141
subrata_modak56207ce2009-03-23 13:35:39 +0000142 msg_q_arr = (int *)calloc(maxmsgs, sizeof(int));
robbiew7805c0e2005-08-02 19:38:41 +0000143 if (msg_q_arr == NULL) {
144 tst_brkm(TBROK, cleanup, "Couldn't allocate memory "
subrata_modak19bace22008-04-06 11:16:42 +0000145 "for msg_q_arr: calloc() failed");
plars865695b2001-08-27 22:15:12 +0000146 }
147}
148
149/*
150 * cleanup() - performs all the ONE TIME cleanup for this test at completion
151 * or premature exit.
152 */
subrata_modak56207ce2009-03-23 13:35:39 +0000153void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000154{
155 int i;
156
157 /*
158 * remove the message queues if they were created
159 */
160
robbiew7805c0e2005-08-02 19:38:41 +0000161 if (msg_q_arr != NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000162 for (i = 0; i < num_queue; i++) {
robbiew7805c0e2005-08-02 19:38:41 +0000163 rm_queue(msg_q_arr[i]);
164 }
subrata_modak56207ce2009-03-23 13:35:39 +0000165 (void)free(msg_q_arr);
plars865695b2001-08-27 22:15:12 +0000166 }
167
plars865695b2001-08-27 22:15:12 +0000168 tst_rmdir();
169
Garrett Cooper460095d2010-12-19 10:18:06 -0800170}