vm: fix region reporting bug
[minix.git] / test / ipc / shmctl / shmctl02.c
blob0801a9320e6911e43db5062c5c2460c6b09fdfaf
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 * shmctl02.c
24 * DESCRIPTION
25 * shmctl02 - check for EACCES, EFAULT and EINVAL errors
27 * ALGORITHM
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
36 * call cleanup
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.
47 * HISTORY
48 * 03/2001 - Written by Wayne Boyer
50 * RESTRICTIONS
51 * none
54 #include "ipcshm.h"
55 #include <pwd.h>
57 char *TCID = "shmctl02";
58 extern int Tst_count;
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 */
64 int shm_id_1 = -1;
65 int shm_id_2 = -1;
66 int shm_id_3 = -1;
68 struct shmid_ds buf;
70 struct test_case_t {
71 int *shmid;
72 int cmd;
73 struct shmid_ds *sbuf;
74 int error;
75 } TC[] = {
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 */
99 int i;
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 */
112 Tst_count = 0;
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");
124 continue;
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));
133 } else {
134 if (i >= 5)
135 tst_resm(TCONF,"shmctl() did not fail for non-root user."
136 "This may be okay for your distribution.");
137 else
138 tst_resm(TFAIL, "call failed with an "
139 "unexpected error - %d : %s",
140 TEST_ERRNO, strerror(TEST_ERRNO));
145 cleanup();
147 /*NOTREACHED*/
148 return(0);
152 * setup() - performs all the ONE TIME setup for this test.
154 void
155 setup(void)
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 */
168 TEST_PAUSE;
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.
175 tst_tmpdir();
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",
181 ltpuser->pw_uid);
182 perror("seteuid");
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 |
197 SHM_RW)) == -1) {
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
205 * or premature exit.
207 void
208 cleanup(void)
210 /* if they exist, remove the shared memory resources */
211 rm_shm(shm_id_1);
212 rm_shm(shm_id_2);
214 if (seteuid(0) == -1) {
215 tst_resm(TINFO, "setuid failed to "
216 "to set the effective uid to %d",
217 ltpuser->pw_uid);
218 perror("seteuid");
221 /* Remove the temporary directory */
222 tst_rmdir();
225 * print timing stats if that option was specified.
226 * print errno log if that option was specified.
228 TEST_CLEANUP;
230 /* exit with return code appropriate for results */
231 tst_exit();