remove extra mkfs.1
[minix.git] / test / ipc / shmat / shmat01.c
blob5163fe13a79964d3093742387b07df6380ba002b
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 * shmat01.c
24 * DESCRIPTION
25 * shmat01 - test that shmat() works correctly
27 * ALGORITHM
28 * create a shared memory resouce with read/write permissions
29 * loop if that option was specified
30 * call shmat() with the TEST() macro using three valid conditions
31 * check the return code
32 * if failure, issue a FAIL message.
33 * otherwise,
34 * if doing functionality testing
35 * check for the correct conditions after the call
36 * if correct,
37 * issue a PASS message
38 * otherwise
39 * issue a FAIL message
40 * call cleanup
42 * USAGE: <for command-line>
43 * shmat01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
44 * where, -c n : Run n copies concurrently.
45 * -f : Turn off functionality Testing.
46 * -i n : Execute test n times.
47 * -I x : Execute test for x seconds.
48 * -P x : Pause for x seconds between iterations.
49 * -t : Turn on syscall timing.
51 * HISTORY
52 * 03/2001 - Written by Wayne Boyer
54 * RESTRICTIONS
55 * none
58 #include "ipcshm.h"
60 char *TCID = "shmat01";
61 int TST_TOTAL = 3;
62 extern int Tst_count;
64 #define CASE0 10 /* values to write into the shared */
65 #define CASE1 20 /* memory location. */
67 #ifdef __ia64__
68 #define UNALIGNED 0x5ff00eee /* an address not evenly divisible by */
69 #elif defined __XTENSA__ /* SHMLBA which defaults to 0x8048e8b */
70 /* TASK_SIZE on Xtensa is only 0x40000000 */
71 #define UNALIGNED 0x28ffeeee
72 #elif defined __arm__
73 #define UNALIGNED 0x28ffeeee
74 #else
75 #define UNALIGNED 0x5fffeeee
76 #endif
78 int shm_id_1 = -1;
80 void *addr; /* for result of shmat-call */
82 struct test_case_t {
83 int *shmid;
84 void *addr;
85 int flags;
86 } TC[] = {
87 /* a straight forward read/write attach */
88 {&shm_id_1, 0, 0},
90 /* an attach using non aligned memory */
91 {&shm_id_1, (void *)UNALIGNED, SHM_RND},
93 /* a read only attach */
94 {&shm_id_1, 0, SHM_RDONLY}
97 int main(int ac, char **av)
99 int lc; /* loop counter */
100 char *msg; /* message returned from parse_opts */
101 int i;
102 void check_functionality(int);
104 /* parse standard options */
105 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
106 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
109 setup(); /* global setup */
111 /* The following loop checks looping state if -i option given */
113 for (lc = 0; TEST_LOOPING(lc); lc++) {
114 /* reset Tst_count in case we are looping */
115 Tst_count = 0;
117 /* loop through the test cases */
118 for (i=0; i<TST_TOTAL; i++) {
121 * Use TEST macro to make the call
123 errno = 0;
124 addr = shmat(*(TC[i].shmid), (void *)(TC[i].addr),
125 TC[i].flags);
126 TEST_ERRNO = errno;
128 if (addr == (void *)-1) {
129 tst_brkm(TFAIL, cleanup, "%s call failed - "
130 "errno = %d : %s", TCID, TEST_ERRNO,
131 strerror(TEST_ERRNO));
132 } else {
133 if (STD_FUNCTIONAL_TEST) {
134 check_functionality(i);
135 } else {
136 tst_resm(TPASS, "call succeeded");
141 * clean up things in case we are looping - in
142 * this case, detach the shared memory
144 if (shmdt((const void *)addr) == -1) {
145 tst_brkm(TBROK, cleanup,
146 "Couldn't detach shared memory");
151 cleanup();
153 /*NOTREACHED*/
154 return(0);
158 * check_functionality - check various conditions to make sure they
159 * are correct.
161 void
162 check_functionality(int i)
164 void *orig_add;
165 int *shared;
166 int fail = 0;
167 struct shmid_ds buf;
169 shared = (int *)addr;
171 /* stat the shared memory ID */
172 if (shmctl(shm_id_1, IPC_STAT, &buf) == -1) {
173 tst_brkm(TBROK, cleanup, "couldn't stat shared memory");
176 /* check the number of attaches */
177 if (buf.shm_nattch != 1) {
178 tst_resm(TFAIL, "# of attaches is incorrect");
179 return;
182 /* check the size of the segment */
183 if (buf.shm_segsz != INT_SIZE) {
184 tst_resm(TFAIL, "segment size is incorrect");
185 return;
188 /* check for specific conditions depending on the type of attach */
189 switch(i) {
190 case 0:
192 * Check the functionality of the first call by simply
193 * "writing" a value to the shared memory space.
194 * If this fails the program will get a SIGSEGV, dump
195 * core and exit.
198 *shared = CASE0;
199 break;
200 case 1:
202 * Check the functionality of the second call by writing
203 * a value to the shared memory space and then checking
204 * that the original address given was rounded down as
205 * specified in the man page.
208 *shared = CASE1;
209 orig_add = (void *)((unsigned long)addr + (((unsigned long)TC[i].addr)%SHMLBA));
210 if (orig_add != TC[i].addr) {
211 tst_resm(TFAIL, "shared memory address is not "
212 "correct");
213 fail = 1;
215 break;
216 case 2:
218 * This time the shared memory is read only. Read the value
219 * and check that it is equal to the value set in case #2,
220 * because shared memory is persistent.
223 if (*shared != CASE1) {
224 tst_resm(TFAIL, "shared memory value isn't correct");
225 fail = 1;
227 break;
230 if (!fail) {
231 tst_resm(TPASS, "conditions and functionality are correct");
236 * setup() - performs all the ONE TIME setup for this test.
238 void
239 setup(void)
241 /* capture signals */
242 tst_sig(NOFORK, DEF_HANDLER, cleanup);
244 /* Pause if that option was specified */
245 TEST_PAUSE;
248 * Create a temporary directory and cd into it.
249 * This helps to ensure that a unique msgkey is created.
250 * See ../lib/libipc.c for more information.
252 tst_tmpdir();
254 /* Get an IPC resouce key */
255 shmkey = getipckey();
257 /* create a shared memory resource with read and write permissions */
258 if ((shm_id_1 = shmget(shmkey++, INT_SIZE, SHM_RW | IPC_CREAT |
259 IPC_EXCL)) == -1) {
260 tst_brkm(TBROK, cleanup, "Failed to create shared memory "
261 "resource 1 in setup()");
266 * cleanup() - performs all the ONE TIME cleanup for this test at completion
267 * or premature exit.
269 void
270 cleanup(void)
272 /* if it exists, remove the shared memory resource */
273 rm_shm(shm_id_1);
275 /* Remove the temporary directory */
276 tst_rmdir();
279 * print timing stats if that option was specified.
280 * print errno log if that option was specified.
282 TEST_CLEANUP;
284 /* exit with return code appropriate for results */
285 tst_exit();