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 * shmget05 - test for EACCES error
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 "nobody"
32 * loop if that option was specified
33 * call shmget() using the TEST() macro
34 * check the errno value
35 * issue a PASS message if we get EACCES
36 * otherwise, the tests fails
37 * issue a FAIL message
40 * wait for child to exit
41 * remove the shared memory segment
43 * USAGE: <for command-line>
44 * shmget05 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
45 * where, -c n : Run n copies concurrently.
46 * -e : Turn on errno logging.
47 * -i n : Execute test n times.
48 * -I x : Execute test for x seconds.
49 * -P x : Pause for x seconds between iterations.
50 * -t : Turn on syscall timing.
53 * 03/2001 - Written by Wayne Boyer
56 * test must be run at root
60 #include <sys/types.h>
63 char *TCID
= "shmget05";
67 int exp_enos
[] = {EACCES
, 0}; /* 0 terminated list of expected errnos */
72 char *ltp_user
= "nobody";
74 int main(int ac
, char **av
)
76 char *msg
; /* message returned from parse_opts */
80 /* parse standard options */
81 if ((msg
= parse_opts(ac
, av
, (option_t
*)NULL
, NULL
)) != (char *)NULL
){
82 tst_brkm(TBROK
, cleanup
, "OPTION PARSING ERROR - %s", msg
);
85 setup(); /* global setup */
87 if ((pid
= FORK_OR_VFORK()) == -1) {
88 tst_brkm(TBROK
, cleanup
, "could not fork");
91 if (pid
== 0) { /* child */
92 /* set the user ID of the child to the non root user */
93 if (setuid(ltp_uid
) == -1) {
94 tst_resm(TBROK
, "setuid() failed");
103 } else { /* parent */
104 /* wait for the child to return */
105 if (waitpid(pid
, NULL
, 0) == -1) {
106 tst_brkm(TBROK
, cleanup
, "waitpid failed");
109 /* if it exists, remove the shared memory resource */
112 /* Remove the temporary directory */
119 * do_child - make the TEST call as the child process
126 /* The following loop checks looping state if -i option given */
128 for (lc
= 0; TEST_LOOPING(lc
); lc
++) {
129 /* reset Tst_count in case we are looping */
133 * Look for a failure ...
136 TEST(shmget(shmkey
, SHM_SIZE
, SHM_RW
));
138 if (TEST_RETURN
!= -1) {
139 tst_resm(TFAIL
, "call succeeded when error expected");
143 TEST_ERROR_LOG(TEST_ERRNO
);
147 tst_resm(TPASS
, "expected failure - errno = "
148 "%d : %s", TEST_ERRNO
, strerror(TEST_ERRNO
));
151 tst_resm(TFAIL
, "call failed with an "
152 "unexpected error - %d : %s",
153 TEST_ERRNO
, strerror(TEST_ERRNO
));
160 * setup() - performs all the ONE TIME setup for this test.
165 /* check for root as process owner */
168 /* capture signals */
169 tst_sig(FORK
, DEF_HANDLER
, cleanup
);
171 /* Set up the expected error numbers for -e option */
172 TEST_EXP_ENOS(exp_enos
);
174 /* Pause if that option was specified */
178 * Create a temporary directory and cd into it.
179 * This helps to ensure that a unique msgkey is created.
180 * See ../lib/libipc.c for more information.
184 /* get an IPC resource key */
185 shmkey
= getipckey();
187 /* create a shared memory segment with read and write permissions */
188 if ((shm_id_1
= shmget(shmkey
, SHM_SIZE
,
189 SHM_RW
| IPC_CREAT
| IPC_EXCL
)) == -1) {
190 tst_brkm(TBROK
, cleanup
, "Failed to create shared memory "
194 /* get the userid for a non root user */
195 ltp_uid
= getuserid(ltp_user
);
199 * cleanup() - performs all the ONE TIME cleanup for this test at completion
206 * print timing stats if that option was specified.
207 * print errno log if that option was specified.
211 /* exit with return code appropriate for results */