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 * shmctl02 - check for EACCES, EFAULT and EINVAL errors
28 * create a shared memory segment without read or write permissions
29 * create a shared memory segment with read & write permissions
30 * loop if that option was specified
31 * call shmctl() using five different invalid cases
32 * check the errno value
33 * issue a PASS message if we get EACCES, EFAULT or EINVAL
34 * otherwise, the tests fails
35 * issue a FAIL message
38 * USAGE: <for command-line>
39 * shmctl02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
40 * where, -c n : Run n copies concurrently.
41 * -e : Turn on errno logging.
42 * -i n : Execute test n times.
43 * -I x : Execute test for x seconds.
44 * -P x : Pause for x seconds between iterations.
45 * -t : Turn on syscall timing.
48 * 03/2001 - Written by Wayne Boyer
57 char *TCID
= "shmctl02";
59 char nobody_uid
[] = "nobody";
60 struct passwd
*ltpuser
;
62 int exp_enos
[] = {EPERM
, EACCES
, EFAULT
, EINVAL
, 0}; /* 0 terminated list */
63 /* of expected errnos */
73 struct shmid_ds
*sbuf
;
76 /* EACCES - segment has no read or write permissions */
77 {&shm_id_1
, IPC_STAT
, &buf
, EACCES
},
79 /* EFAULT - IPC_SET & buf isn't valid */
80 {&shm_id_2
, IPC_SET
, (struct shmid_ds
*)-1, EFAULT
},
82 /* EFAULT - IPC_STAT & buf isn't valid */
83 {&shm_id_2
, IPC_STAT
, (struct shmid_ds
*)-1, EFAULT
},
85 /* EINVAL - the shmid is not valid */
86 {&shm_id_3
, IPC_STAT
, &buf
, EINVAL
},
88 /* EINVAL - the command is not valid */
89 {&shm_id_2
, -1, &buf
, EINVAL
},
93 int TST_TOTAL
= (sizeof(TC
) / sizeof(*TC
));
95 int main(int ac
, char **av
)
97 int lc
; /* loop counter */
98 char *msg
; /* message returned from parse_opts */
101 /* parse standard options */
102 if ((msg
= parse_opts(ac
, av
, (option_t
*)NULL
, NULL
)) != (char *)NULL
){
103 tst_brkm(TBROK
, cleanup
, "OPTION PARSING ERROR - %s", msg
);
106 setup(); /* global setup */
108 /* The following loop checks looping state if -i option given */
110 for (lc
= 0; TEST_LOOPING(lc
); lc
++) {
111 /* reset Tst_count in case we are looping */
114 /* loop through the test cases */
115 for (i
=0; i
<TST_TOTAL
; i
++) {
117 * use the TEST() macro to make the call
120 TEST(shmctl(*(TC
[i
].shmid
), TC
[i
].cmd
, TC
[i
].sbuf
));
122 if ((TEST_RETURN
!= -1)&&(i
< 5)) {
123 tst_resm(TFAIL
, "call succeeded unexpectedly");
127 TEST_ERROR_LOG(TEST_ERRNO
);
129 if (TEST_ERRNO
== TC
[i
].error
) {
130 tst_resm(TPASS
, "expected failure - errno = "
131 "%d : %s", TEST_ERRNO
,
132 strerror(TEST_ERRNO
));
135 tst_resm(TCONF
,"shmctl() did not fail for non-root user."
136 "This may be okay for your distribution.");
138 tst_resm(TFAIL
, "call failed with an "
139 "unexpected error - %d : %s",
140 TEST_ERRNO
, strerror(TEST_ERRNO
));
152 * setup() - performs all the ONE TIME setup for this test.
157 /* Switch to nobody user for correct error code collection */
158 if (geteuid() != 0) {
159 tst_brkm(TBROK
, tst_exit
, "Test must be run as root");
161 /* capture signals */
162 tst_sig(NOFORK
, DEF_HANDLER
, cleanup
);
164 /* Set up the expected error numbers for -e option */
165 TEST_EXP_ENOS(exp_enos
);
167 /* Pause if that option was specified */
171 * Create a temporary directory and cd into it.
172 * This helps to ensure that a unique msgkey is created.
173 * See ../lib/libipc.c for more information.
177 ltpuser
= getpwnam(nobody_uid
);
178 if (seteuid(ltpuser
->pw_uid
) == -1) {
179 tst_resm(TINFO
, "setuid failed to "
180 "to set the effective uid to %d",
186 /* get an IPC resource key */
187 shmkey
= getipckey();
189 /* create a shared memory segment without read or write permissions */
190 if ((shm_id_1
= shmget(shmkey
, SHM_SIZE
, IPC_CREAT
| IPC_EXCL
)) == -1) {
191 tst_brkm(TBROK
, cleanup
, "couldn't create shared memory "
192 "segment #1 in setup()");
195 /* create a shared memory segment with read and write permissions */
196 if ((shm_id_2
= shmget(shmkey
+ 1, SHM_SIZE
, IPC_CREAT
| IPC_EXCL
|
198 tst_brkm(TBROK
, cleanup
, "couldn't create shared memory "
199 "segment #2 in setup()");
204 * cleanup() - performs all the ONE TIME cleanup for this test at completion
210 /* if they exist, remove the shared memory resources */
214 if (seteuid(0) == -1) {
215 tst_resm(TINFO
, "setuid failed to "
216 "to set the effective uid to %d",
221 /* Remove the temporary directory */
225 * print timing stats if that option was specified.
226 * print errno log if that option was specified.
230 /* exit with return code appropriate for results */