plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 57 | #include "ipcmsg.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
| 59 | char *TCID = "msgget03"; |
| 60 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
robbiew | 7805c0e | 2005-08-02 19:38:41 +0000 | [diff] [blame] | 62 | int maxmsgs = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | |
robbiew | 7805c0e | 2005-08-02 19:38:41 +0000 | [diff] [blame] | 64 | int *msg_q_arr = NULL; /* hold the id's that we create */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | int num_queue = 0; /* count the queues created */ |
| 66 | |
robbiew | 23499f0 | 2002-11-18 19:54:58 +0000 | [diff] [blame] | 67 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 69 | int lc; |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 70 | const char *msg; |
subrata_modak | 19bace2 | 2008-04-06 11:16:42 +0000 | [diff] [blame] | 71 | int msg_q; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
Cyril Hrubis | 7422562 | 2014-06-02 17:54:38 +0200 | [diff] [blame] | 73 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 74 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 76 | setup(); /* global setup */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
| 78 | /* The following loop checks looping state if -i option given */ |
| 79 | |
| 80 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 81 | /* reset tst_count in case we are looping */ |
| 82 | tst_count = 0; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | /* |
subrata_modak | 19bace2 | 2008-04-06 11:16:42 +0000 | [diff] [blame] | 85 | * Use a while loop to create the maximum number of queues. |
| 86 | * When we get an error, check for ENOSPC. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | while ((msg_q = |
| 89 | msgget(msgkey + num_queue, |
| 90 | IPC_CREAT | IPC_EXCL)) != -1) { |
subrata_modak | 19bace2 | 2008-04-06 11:16:42 +0000 | [diff] [blame] | 91 | 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++; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | switch (errno) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 110 | break; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | cleanup(); |
| 115 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 116 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /* |
| 120 | * setup() - performs all the ONE TIME setup for this test. |
| 121 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 122 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 124 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 126 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | 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 | |
robbiew | 7805c0e | 2005-08-02 19:38:41 +0000 | [diff] [blame] | 138 | maxmsgs = get_max_msgqueues(); |
subrata_modak | 5b69374 | 2008-02-22 08:33:53 +0000 | [diff] [blame] | 139 | if (maxmsgs < 0) |
Garrett Cooper | 460095d | 2010-12-19 10:18:06 -0800 | [diff] [blame] | 140 | tst_brkm(TBROK, cleanup, "get_max_msgqueues failed"); |
robbiew | 7805c0e | 2005-08-02 19:38:41 +0000 | [diff] [blame] | 141 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 142 | msg_q_arr = (int *)calloc(maxmsgs, sizeof(int)); |
robbiew | 7805c0e | 2005-08-02 19:38:41 +0000 | [diff] [blame] | 143 | if (msg_q_arr == NULL) { |
| 144 | tst_brkm(TBROK, cleanup, "Couldn't allocate memory " |
subrata_modak | 19bace2 | 2008-04-06 11:16:42 +0000 | [diff] [blame] | 145 | "for msg_q_arr: calloc() failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * cleanup() - performs all the ONE TIME cleanup for this test at completion |
| 151 | * or premature exit. |
| 152 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 153 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | { |
| 155 | int i; |
| 156 | |
| 157 | /* |
| 158 | * remove the message queues if they were created |
| 159 | */ |
| 160 | |
robbiew | 7805c0e | 2005-08-02 19:38:41 +0000 | [diff] [blame] | 161 | if (msg_q_arr != NULL) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 162 | for (i = 0; i < num_queue; i++) { |
robbiew | 7805c0e | 2005-08-02 19:38:41 +0000 | [diff] [blame] | 163 | rm_queue(msg_q_arr[i]); |
| 164 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 165 | (void)free(msg_q_arr); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | } |
| 167 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | tst_rmdir(); |
| 169 | |
Garrett Cooper | 460095d | 2010-12-19 10:18:06 -0800 | [diff] [blame] | 170 | } |