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 * shmctl03 - check for EACCES, and EPERM errors
28 * create a shared memory segment with root only read & write permissions
29 * fork a child process
31 * set the ID of the child process to that of "ltpuser1"
33 * loop if that option was specified
34 * call shmctl() using three different invalid cases
35 * check the errno value
36 * issue a PASS message if we get EACCES or EPERM
37 * otherwise, the tests fails
38 * issue a FAIL message
41 * wait for child to exit
42 * remove the shared memory segment
44 * USAGE: <for command-line>
45 * shmctl03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
46 * where, -c n : Run n copies concurrently.
47 * -e : Turn on errno logging.
48 * -i n : Execute test n times.
49 * -I x : Execute test for x seconds.
50 * -P x : Pause for x seconds between iterations.
51 * -t : Turn on syscall timing.
54 * 03/2001 - Written by Wayne Boyer
57 * test must be run as root
61 #include <sys/types.h>
64 char *TCID
= "shmctl03";
67 int exp_enos
[] = {EACCES
, EPERM
, 0}; /* 0 terminated list of */
72 char *ltp_user
= "nobody";
79 struct shmid_ds
*sbuf
;
82 /* EACCES - child has no read permission for segment */
83 {&shm_id_1
, IPC_STAT
, &buf
, EACCES
},
85 /* EPERM - IPC_SET - child doesn't have permission to change segment */
86 {&shm_id_1
, IPC_SET
, &buf
, EPERM
},
88 /* EPERM - IPC_RMID - child can not remove the segment */
89 {&shm_id_1
, IPC_RMID
, &buf
, EPERM
},
92 int TST_TOTAL
= (sizeof(TC
) / sizeof(*TC
));
94 int main(int ac
, char **av
)
96 char *msg
; /* message returned from parse_opts */
100 /* parse standard options */
101 if ((msg
= parse_opts(ac
, av
, (option_t
*)NULL
, NULL
)) != (char *)NULL
){
102 tst_brkm(TBROK
, cleanup
, "OPTION PARSING ERROR - %s", msg
);
105 setup(); /* global setup */
107 if ((pid
= FORK_OR_VFORK()) == -1) {
108 tst_brkm(TBROK
, cleanup
, "could not fork");
111 if (pid
== 0) { /* child */
112 /* set the user ID of the child to the non root user */
113 if (setuid(ltp_uid
) == -1) {
114 tst_resm(TBROK
, "setuid() failed");
120 /* wait for the child to return */
121 if (waitpid(pid
, NULL
, 0) == -1) {
122 tst_brkm(TBROK
, cleanup
, "waitpid failed");
125 /* if it exists, remove the shared memory resource */
128 /* Remove the temporary directory */
137 * do_child - make the call as the child process
144 /* The following loop checks looping state if -i option given */
146 for (lc
= 0; TEST_LOOPING(lc
); lc
++) {
147 /* reset Tst_count in case we are looping */
150 /* loop through the test cases */
151 for (i
=0; i
<TST_TOTAL
; i
++) {
153 * use the TEST() macro to make the call
156 TEST(shmctl(*(TC
[i
].shmid
), TC
[i
].cmd
, TC
[i
].sbuf
));
158 if (TEST_RETURN
!= -1) {
159 tst_resm(TFAIL
, "call succeeded unexpectedly");
163 TEST_ERROR_LOG(TEST_ERRNO
);
165 if (TEST_ERRNO
== TC
[i
].error
) {
166 tst_resm(TPASS
, "expected failure - errno = "
167 "%d : %s", TEST_ERRNO
,
168 strerror(TEST_ERRNO
));
170 tst_resm(TFAIL
, "call failed with an "
171 "unexpected error - %d : %s",
172 TEST_ERRNO
, strerror(TEST_ERRNO
));
179 * setup() - performs all the ONE TIME setup for this test.
184 /* check for root as process owner */
187 /* capture signals */
188 tst_sig(FORK
, DEF_HANDLER
, cleanup
);
190 /* Set up the expected error numbers for -e option */
191 TEST_EXP_ENOS(exp_enos
);
193 /* Pause if that option was specified */
197 * Create a temporary directory and cd into it.
198 * This helps to ensure that a unique msgkey is created.
199 * See ../lib/libipc.c for more information.
203 /* get an IPC resource key */
204 shmkey
= getipckey();
206 /* create a shared memory segment with read and write permissions */
207 if ((shm_id_1
= shmget(shmkey
, SHM_SIZE
, IPC_CREAT
| IPC_EXCL
|
209 tst_brkm(TBROK
, cleanup
, "couldn't create shared memory "
210 "segment in setup()");
213 /* get the userid for a non root user */
214 ltp_uid
= getuserid(ltp_user
);
218 * cleanup() - performs all the ONE TIME cleanup for this test at completion
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 */