vm: use arch_map2str to print pagefault info, to properly display code addrs
[minix.git] / test / ipc / shmget / shmget02.c
blob7792051c0c6e86a3b0932e96613e9c983abcb49a
1 /*
3 * Copyright (c) International Business Machines Corp., 2001
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.
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.
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
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * NAME
22 * shmget02.c
24 * DESCRIPTION
25 * shmget02 - check for ENOENT, EEXIST and EINVAL errors
27 * ALGORITHM
28 * create a shared memory segment with read & write permissions
29 * loop if that option was specified
30 * call shmget() using five different invalid cases
31 * check the errno value
32 * issue a PASS message if we get ENOENT, EEXIST or EINVAL
33 * otherwise, the tests fails
34 * issue a FAIL message
35 * call cleanup
37 * USAGE: <for command-line>
38 * shmget02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
39 * where, -c n : Run n copies concurrently.
40 * -e : Turn on errno logging.
41 * -i n : Execute test n times.
42 * -I x : Execute test for x seconds.
43 * -P x : Pause for x seconds between iterations.
44 * -t : Turn on syscall timing.
46 * HISTORY
47 * 03/2001 - Written by Wayne Boyer
49 * RESTRICTIONS
50 * none
53 #include "ipcshm.h"
55 char *TCID = "shmget02";
56 int TST_TOTAL = 5;
57 extern int Tst_count;
59 int exp_enos[] = {ENOENT, EEXIST, EINVAL, 0}; /* 0 terminated list of */
60 /* expected errnos */
62 int shm_id_1 = -1;
63 int shm_nonexisting_key = -1;
64 int shmkey2;
66 struct test_case_t {
67 int *skey;
68 int size;
69 int flags;
70 int error;
71 } TC[] = {
72 /* EINVAL - size is 0 */
73 {&shmkey2, 0, IPC_CREAT | IPC_EXCL | SHM_RW, EINVAL},
75 /* EINVAL - size is negative */
76 {&shmkey2, -1, IPC_CREAT | IPC_EXCL | SHM_RW, EINVAL},
78 /* EINVAL - size is larger than created segment */
79 {(int *)&shmkey, SHM_SIZE * 2, SHM_RW, EINVAL},
81 /* EEXIST - the segment exists and IPC_CREAT | IPC_EXCL is given */
82 {(int *)&shmkey, SHM_SIZE, IPC_CREAT | IPC_EXCL | SHM_RW, EEXIST},
84 /* ENOENT - no segment exists for the key and IPC_CREAT is not given */
85 /* use shm_id_2 (-1) as the key */
86 {&shm_nonexisting_key, SHM_SIZE, SHM_RW, ENOENT}
89 int main(int ac, char **av)
91 int lc; /* loop counter */
92 char *msg; /* message returned from parse_opts */
93 int i;
95 /* parse standard options */
96 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
97 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
100 setup(); /* global setup */
102 /* The following loop checks looping state if -i option given */
104 for (lc = 0; TEST_LOOPING(lc); lc++) {
105 /* reset Tst_count in case we are looping */
106 Tst_count = 0;
108 /* loop through the test cases */
109 for (i=0; i<TST_TOTAL; i++) {
111 * Look for a failure ...
114 TEST(shmget(*(TC[i].skey), TC[i].size, TC[i].flags));
116 if (TEST_RETURN != -1) {
117 tst_resm(TFAIL, "call succeeded unexpectedly");
118 continue;
121 TEST_ERROR_LOG(TEST_ERRNO);
123 if (TEST_ERRNO == TC[i].error) {
124 tst_resm(TPASS, "expected failure - errno = "
125 "%d : %s", TEST_ERRNO,
126 strerror(TEST_ERRNO));
127 } else {
128 tst_resm(TFAIL, "call failed with an "
129 "unexpected error - %d : %s",
130 TEST_ERRNO, strerror(TEST_ERRNO));
135 cleanup();
137 /*NOTREACHED*/
139 return(0);
143 * setup() - performs all the ONE TIME setup for this test.
145 void
146 setup(void)
148 /* capture signals */
149 tst_sig(NOFORK, DEF_HANDLER, cleanup);
151 /* Set up the expected error numbers for -e option */
152 TEST_EXP_ENOS(exp_enos);
154 /* Pause if that option was specified */
155 TEST_PAUSE;
158 * Create a temporary directory and cd into it.
159 * This helps to ensure that a unique msgkey is created.
160 * See ../lib/libipc.c for more information.
162 tst_tmpdir();
164 /* get an IPC resource key */
165 shmkey = getipckey();
167 shmkey2 = shmkey + 1;
169 if ((shm_id_1 = shmget(shmkey, SHM_SIZE, IPC_CREAT | IPC_EXCL |
170 SHM_RW)) == -1) {
171 tst_brkm(TBROK, cleanup, "couldn't create shared memory "
172 "segment in setup()");
175 /* Make sure shm_nonexisting_key is a nonexisting key */
176 while(1) {
177 while(-1 != shmget(shm_nonexisting_key,1,SHM_RD)) {
178 shm_nonexisting_key--;
180 if(errno == ENOENT)
181 break;
186 * cleanup() - performs all the ONE TIME cleanup for this test at completion
187 * or premature exit.
189 void
190 cleanup(void)
192 /* if it exists, remove the shared memory resource */
193 rm_shm(shm_id_1);
195 /* Remove the temporary directory */
196 tst_rmdir();
199 * print timing stats if that option was specified.
200 * print errno log if that option was specified.
202 TEST_CLEANUP;
204 /* exit with return code appropriate for results */
205 tst_exit();