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 * shmat03 - 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 shmat() 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 * shmat03 [-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
61 char *TCID
= "shmat03";
65 int exp_enos
[] = {EACCES
, 0}; /* 0 terminated list of expected errnos */
69 void *addr
; /* for result of shmat-call */
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");
100 } else { /* parent */
101 /* wait for the child to return */
102 if (waitpid(pid
, NULL
, 0) == -1) {
103 tst_brkm(TBROK
, cleanup
, "waitpid failed");
106 /* if it exists, remove the shared memory resource */
109 /* Remove the temporary directory */
118 * do_child - make the TEST call as the child process
125 /* The following loop checks looping state if -i option given */
127 for (lc
= 0; TEST_LOOPING(lc
); lc
++) {
128 /* reset Tst_count in case we are looping */
132 * use TEST macro to make the call
135 addr
= shmat(shm_id_1
, (const void *)0, 0);
138 if (addr
!= (char *)-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
,
149 strerror(TEST_ERRNO
));
152 tst_resm(TFAIL
, "call failed with an "
153 "unexpected error - %d : %s",
154 TEST_ERRNO
, strerror(TEST_ERRNO
));
161 * setup() - performs all the ONE TIME setup for this test.
166 /* check for root as process owner */
169 /* capture signals */
170 tst_sig(FORK
, DEF_HANDLER
, cleanup
);
172 /* Set up the expected error numbers for -e option */
173 TEST_EXP_ENOS(exp_enos
);
175 /* Pause if that option was specified */
179 * Create a temporary directory and cd into it.
180 * This helps to ensure that a unique msgkey is created.
181 * See ../lib/libipc.c for more information.
185 /* get an IPC resource key */
186 shmkey
= getipckey();
188 /* create a shared memory segment with read and write permissions */
189 if ((shm_id_1
= shmget(shmkey
, SHM_SIZE
,
190 SHM_RW
| IPC_CREAT
| IPC_EXCL
)) == -1) {
191 tst_brkm(TBROK
, cleanup
, "Failed to create shared memory "
195 /* get the userid for a non root user */
196 ltp_uid
= getuserid(ltp_user
);
200 * cleanup() - performs all the ONE TIME cleanup for this test at completion
207 * print timing stats if that option was specified.
208 * print errno log if that option was specified.
212 /* exit with return code appropriate for results */