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 * shmat02 - check for EINVAL and EACCES errors
28 * loop if that option was specified
29 * call shmat() using three invalid test cases
30 * check the errno value
31 * issue a PASS message if we get EINVAL or EACCES
32 * otherwise, the tests fails
33 * issue a FAIL message
36 * USAGE: <for command-line>
37 * shmat02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
38 * where, -c n : Run n copies concurrently.
39 * -e : Turn on errno logging.
40 * -i n : Execute test n times.
41 * -I x : Execute test for x seconds.
42 * -P x : Pause for x seconds between iterations.
43 * -t : Turn on syscall timing.
46 * 03/2001 - Written by Wayne Boyer
49 * Must be ran as non-root
55 char *TCID
= "shmat02";
58 char nobody_uid
[] = "nobody";
59 struct passwd
*ltpuser
;
62 int exp_enos
[] = {EINVAL
, EACCES
, 0}; /* 0 terminated list of */
69 void *addr
; /* for result of shmat-call */
71 #define NADDR 0x40FFFEE5 /* a non alligned address value */
77 /* EINVAL - the shared memory ID is not valid */
78 {&shm_id_1
, 0, EINVAL
},
80 /* EINVAL - the address is not page aligned and SHM_RND is not given */
81 {&shm_id_2
, (void *)NADDR
, EINVAL
},
83 /* EACCES - the shared memory resource has no read/write permission */
84 {&shm_id_3
, 0, EACCES
}
87 int main(int ac
, char **av
)
89 int lc
; /* loop counter */
90 char *msg
; /* message returned from parse_opts */
93 /* parse standard options */
94 if ((msg
= parse_opts(ac
, av
, (option_t
*)NULL
, NULL
)) != (char *)NULL
){
95 tst_brkm(TBROK
, cleanup
, "OPTION PARSING ERROR - %s", msg
);
98 setup(); /* global setup */
100 /* The following loop checks looping state if -i option given */
102 for (lc
= 0; TEST_LOOPING(lc
); lc
++) {
103 /* reset Tst_count in case we are looping */
106 /* loop through the test cases */
107 for (i
=0; i
<TST_TOTAL
; i
++) {
109 * make the call using the TEST() macro - attempt
110 * various invalid shared memory attaches
113 addr
= shmat(*(TC
[i
].shmid
), (const void *)TC
[i
].addr
, 0);
116 if (addr
!= (void *)-1) {
117 tst_resm(TFAIL
, "call succeeded unexpectedly");
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
));
128 tst_resm(TFAIL
, "call failed with an "
129 "unexpected error - %d : %s",
130 TEST_ERRNO
, strerror(TEST_ERRNO
));
143 * setup() - performs all the ONE TIME setup for this test.
148 /* Switch to nobody user for correct error code collection */
149 if (geteuid() != 0) {
150 tst_brkm(TBROK
, tst_exit
, "Test must be run as root");
153 /* capture signals */
154 tst_sig(NOFORK
, DEF_HANDLER
, cleanup
);
156 /* Set up the expected error numbers for -e option */
157 TEST_EXP_ENOS(exp_enos
);
159 /* Pause if that option was specified */
163 * Create a temporary directory and cd into it.
164 * This helps to ensure that a unique msgkey is created.
165 * See ../lib/libipc.c for more information.
169 ltpuser
= getpwnam(nobody_uid
);
170 if (seteuid(ltpuser
->pw_uid
) == -1) {
171 tst_resm(TINFO
, "setuid failed to "
172 "to set the effective uid to %d",
177 /* get an IPC resource key */
178 shmkey
= getipckey();
180 /* create a shared memory resource with read and write permissions */
181 /* also post increment the shmkey for the next shmget call */
182 if ((shm_id_2
= shmget(shmkey
++, INT_SIZE
, SHM_RW
| IPC_CREAT
|
184 tst_brkm(TBROK
, cleanup
, "Failed to create shared memory "
185 "resource #1 in setup()");
188 /* create a shared memory resource without read and write permissions */
189 if ((shm_id_3
= shmget(shmkey
, INT_SIZE
, IPC_CREAT
| IPC_EXCL
)) == -1) {
190 tst_brkm(TBROK
, cleanup
, "Failed to create shared memory "
191 "resource #2 in setup()");
196 * cleanup() - performs all the ONE TIME cleanup for this test at completion
202 /* if they exist, remove the shared memory resources */
206 if (seteuid(0) == -1) {
207 tst_resm(TINFO
, "setuid failed to "
208 "to set the effective uid to root");
212 /* Remove the temporary directory */
216 * print timing stats if that option was specified.
217 * print errno log if that option was specified.
221 /* exit with return code appropriate for results */