vm: fix region reporting bug
[minix.git] / test / ipc / shmat / shmat03.c
blobd0f6a0e9a471d4821da889e8ce7c580b12992f3b
1 /*
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
21 * NAME
22 * shmat03.c
24 * DESCRIPTION
25 * shmat03 - test for EACCES error
27 * ALGORITHM
28 * create a shared memory segment with root only read & write permissions
29 * fork a child process
30 * if child
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
38 * call cleanup
39 * if parent
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.
52 * HISTORY
53 * 03/2001 - Written by Wayne Boyer
55 * RESTRICTIONS
56 * test must be run at root
59 #include "ipcshm.h"
61 char *TCID = "shmat03";
62 int TST_TOTAL = 1;
63 extern int Tst_count;
65 int exp_enos[] = {EACCES, 0}; /* 0 terminated list of expected errnos */
67 int shm_id_1 = -1;
69 void *addr; /* for result of shmat-call */
71 uid_t ltp_uid;
72 char *ltp_user = "nobody";
74 int main(int ac, char **av)
76 char *msg; /* message returned from parse_opts */
77 int pid;
78 void do_child(void);
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");
95 exit(1);
98 do_child();
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 */
107 rm_shm(shm_id_1);
109 /* Remove the temporary directory */
110 tst_rmdir();
113 cleanup();
114 return(0);
118 * do_child - make the TEST call as the child process
120 void
121 do_child(void)
123 int lc;
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 */
129 Tst_count = 0;
132 * use TEST macro to make the call
134 errno = 0;
135 addr = shmat(shm_id_1, (const void *)0, 0);
136 TEST_ERRNO = errno;
138 if (addr != (char *)-1) {
139 tst_resm(TFAIL, "call succeeded when error expected");
140 continue;
143 TEST_ERROR_LOG(TEST_ERRNO);
145 switch(TEST_ERRNO) {
146 case EACCES:
147 tst_resm(TPASS, "expected failure - errno = "
148 "%d : %s", TEST_ERRNO,
149 strerror(TEST_ERRNO));
150 break;
151 default:
152 tst_resm(TFAIL, "call failed with an "
153 "unexpected error - %d : %s",
154 TEST_ERRNO, strerror(TEST_ERRNO));
155 break;
161 * setup() - performs all the ONE TIME setup for this test.
163 void
164 setup(void)
166 /* check for root as process owner */
167 check_root();
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 */
176 TEST_PAUSE;
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.
183 tst_tmpdir();
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 "
192 "segment in setup");
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
201 * or premature exit.
203 void
204 cleanup(void)
207 * print timing stats if that option was specified.
208 * print errno log if that option was specified.
210 TEST_CLEANUP;
212 /* exit with return code appropriate for results */
213 tst_exit();