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
17 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/*
21 * NAME
22 *	shmget02.c
23 *
24 * DESCRIPTION
25 *	shmget02 - check for ENOENT, EEXIST and EINVAL errors
26 *
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
36 *
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.
45 *
46 * HISTORY
47 *	03/2001 - Written by Wayne Boyer
48 *
49 *      06/03/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
50 *      - Fix concurrency issue. The second key used for this test could
51 *        conflict with the key from another task.
52 *
53 * RESTRICTIONS
54 *	none
55 */
56
57#include "ipcshm.h"
58
59char *TCID = "shmget02";
60int TST_TOTAL = 4;
61
62int shm_id_1 = -1;
63int shm_nonexisting_key = -1;
64key_t shmkey2;
65
66struct test_case_t {
67	int *skey;
68	int size;
69	int flags;
70	int error;
71} TC[] = {
72	/* EINVAL - size is 0 */
73	{
74	&shmkey2, 0, IPC_CREAT | IPC_EXCL | SHM_RW, EINVAL},
75	    /* EINVAL - size is negative */
76//      {&shmkey2, -1, IPC_CREAT | IPC_EXCL | SHM_RW, EINVAL},
77	    /* EINVAL - size is larger than created segment */
78	{
79	&shmkey, SHM_SIZE * 2, SHM_RW, EINVAL},
80	    /* EEXIST - the segment exists and IPC_CREAT | IPC_EXCL is given */
81	{
82	&shmkey, SHM_SIZE, IPC_CREAT | IPC_EXCL | SHM_RW, EEXIST},
83	    /* ENOENT - no segment exists for the key and IPC_CREAT is not given */
84	    /* use shm_id_2 (-1) as the key */
85	{
86	&shm_nonexisting_key, SHM_SIZE, SHM_RW, ENOENT}
87};
88
89int main(int ac, char **av)
90{
91	int lc;
92	int i;
93
94	tst_parse_opts(ac, av, NULL, NULL);
95
96	setup();		/* global setup */
97
98	/* The following loop checks looping state if -i option given */
99
100	for (lc = 0; TEST_LOOPING(lc); lc++) {
101		/* reset tst_count in case we are looping */
102		tst_count = 0;
103
104		/* loop through the test cases */
105		for (i = 0; i < TST_TOTAL; i++) {
106			/*
107			 * Look for a failure ...
108			 */
109
110			TEST(shmget(*(TC[i].skey), TC[i].size, TC[i].flags));
111
112			if (TEST_RETURN != -1) {
113				tst_resm(TFAIL, "call succeeded unexpectedly");
114				continue;
115			}
116
117			if (TEST_ERRNO == TC[i].error) {
118				tst_resm(TPASS, "expected failure - errno = "
119					 "%d : %s", TEST_ERRNO,
120					 strerror(TEST_ERRNO));
121			} else {
122				tst_resm(TFAIL, "call failed with an "
123					 "unexpected error - %d : %s",
124					 TEST_ERRNO, strerror(TEST_ERRNO));
125			}
126		}
127	}
128
129	cleanup();
130
131	tst_exit();
132}
133
134/*
135 * setup() - performs all the ONE TIME setup for this test.
136 */
137void setup(void)
138{
139
140	tst_sig(NOFORK, DEF_HANDLER, cleanup);
141
142	TEST_PAUSE;
143
144	/*
145	 * Create a temporary directory and cd into it.
146	 * This helps to ensure that a unique msgkey is created.
147	 * See ../lib/libipc.c for more information.
148	 */
149	tst_tmpdir();
150
151	/* get an IPC resource key */
152	shmkey = getipckey();
153
154	/* Get an new IPC resource key. */
155	shmkey2 = getipckey();
156
157	if ((shm_id_1 = shmget(shmkey, SHM_SIZE, IPC_CREAT | IPC_EXCL |
158			       SHM_RW)) == -1) {
159		tst_brkm(TBROK, cleanup, "couldn't create shared memory "
160			 "segment in setup()");
161	}
162
163	/* Make sure shm_nonexisting_key is a nonexisting key */
164	while (1) {
165		while (-1 != shmget(shm_nonexisting_key, 1, SHM_RD)) {
166			shm_nonexisting_key--;
167		}
168		if (errno == ENOENT)
169			break;
170	}
171}
172
173/*
174 * cleanup() - performs all the ONE TIME cleanup for this test at completion
175 * 	       or premature exit.
176 */
177void cleanup(void)
178{
179	/* if it exists, remove the shared memory resource */
180	rm_shm(shm_id_1);
181
182	tst_rmdir();
183
184}
185