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
25 * shmat01 - test that shmat() works correctly
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.
34 * if doing functionality testing
35 * check for the correct conditions after the call
37 * issue a PASS message
39 * issue a FAIL message
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.
52 * 03/2001 - Written by Wayne Boyer
60 char *TCID
= "shmat01";
64 #define CASE0 10 /* values to write into the shared */
65 #define CASE1 20 /* memory location. */
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
73 #define UNALIGNED 0x28ffeeee
75 #define UNALIGNED 0x5fffeeee
80 void *addr
; /* for result of shmat-call */
87 /* a straight forward read/write attach */
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 */
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 */
117 /* loop through the test cases */
118 for (i
=0; i
<TST_TOTAL
; i
++) {
121 * Use TEST macro to make the call
124 addr
= shmat(*(TC
[i
].shmid
), (void *)(TC
[i
].addr
),
128 if (addr
== (void *)-1) {
129 tst_brkm(TFAIL
, cleanup
, "%s call failed - "
130 "errno = %d : %s", TCID
, TEST_ERRNO
,
131 strerror(TEST_ERRNO
));
133 if (STD_FUNCTIONAL_TEST
) {
134 check_functionality(i
);
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");
158 * check_functionality - check various conditions to make sure they
162 check_functionality(int i
)
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");
182 /* check the size of the segment */
183 if (buf
.shm_segsz
!= INT_SIZE
) {
184 tst_resm(TFAIL
, "segment size is incorrect");
188 /* check for specific conditions depending on the type of attach */
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
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.
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 "
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");
231 tst_resm(TPASS
, "conditions and functionality are correct");
236 * setup() - performs all the ONE TIME setup for this test.
241 /* capture signals */
242 tst_sig(NOFORK
, DEF_HANDLER
, cleanup
);
244 /* Pause if that option was specified */
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.
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
|
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
272 /* if it exists, remove the shared memory resource */
275 /* Remove the temporary directory */
279 * print timing stats if that option was specified.
280 * print errno log if that option was specified.
284 /* exit with return code appropriate for results */