VM: simplify slab allocator
[minix.git] / test / ipc / shmget / shmget04.c
blobd42287c7717008140d8ed8852bb1e4d17887bed0
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 * shmget04.c
24 * DESCRIPTION
25 * shmget04 - test for EACCES error
27 * ALGORITHM
28 * create a shared memory segment without read or write permissions
29 * loop if that option was specified
30 * call shmget() with SHM_RW flag using TEST() macro
31 * check the errno value
32 * issue a PASS message if we get EACCES
33 * otherwise, the tests fails
34 * issue a FAIL message
35 * call cleanup
37 * USAGE: <for command-line>
38 * shmget04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
39 * where, -c n : Run n copies concurrently.
40 * -e : Turn on errno logging.
41 * -i n : Execute test n times.
42 * -I x : Execute test for x seconds.
43 * -P x : Pause for x seconds between iterations.
44 * -t : Turn on syscall timing.
46 * HISTORY
47 * 03/2001 - Written by Wayne Boyer
49 * RESTRICTIONS
50 * none
52 #include <pwd.h>
53 #include "ipcshm.h"
55 char *TCID = "shmget04";
56 int TST_TOTAL = 1;
57 extern int Tst_count;
59 int exp_enos[] = {EACCES, 0}; /* 0 terminated list of expected errnos */
61 char nobody_uid[] = "nobody";
62 struct passwd *ltpuser;
64 int shm_id_1 = -1;
66 int main(int ac, char **av)
68 int lc; /* loop counter */
69 char *msg; /* message returned from parse_opts */
71 /* parse standard options */
72 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
73 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
76 setup(); /* global setup */
78 /* The following loop checks looping state if -i option given */
80 for (lc = 0; TEST_LOOPING(lc); lc++) {
81 /* reset Tst_count in case we are looping */
82 Tst_count = 0;
85 * use the TEST() macro to make the call
88 TEST(shmget(shmkey, SHM_SIZE, SHM_RW));
90 if (TEST_RETURN != -1) {
91 tst_resm(TFAIL, "call succeeded when error expected");
92 continue;
95 TEST_ERROR_LOG(TEST_ERRNO);
97 switch(TEST_ERRNO) {
98 case EACCES:
99 tst_resm(TPASS, "expected failure - errno = "
100 "%d : %s", TEST_ERRNO, strerror(TEST_ERRNO));
101 break;
102 default:
103 tst_resm(TFAIL, "call failed with an "
104 "unexpected error - %d : %s",
105 TEST_ERRNO, strerror(TEST_ERRNO));
106 break;
110 cleanup();
112 /*NOTREACHED*/
113 return(0);
117 * setup() - performs all the ONE TIME setup for this test.
119 void
120 setup(void)
122 /* capture signals */
123 tst_sig(NOFORK, DEF_HANDLER, cleanup);
125 /* Set up the expected error numbers for -e option */
126 TEST_EXP_ENOS(exp_enos);
128 /* Pause if that option was specified */
129 TEST_PAUSE;
131 /* Switch to nobody user for correct error code collection */
132 if (geteuid() != 0) {
133 tst_brkm(TBROK, tst_exit, "Test must be run as root");
137 * Create a temporary directory and cd into it.
138 * This helps to ensure that a unique msgkey is created.
139 * See ../lib/libipc.c for more information.
141 tst_tmpdir();
143 ltpuser = getpwnam(nobody_uid);
144 if (seteuid(ltpuser->pw_uid) == -1) {
145 tst_resm(TINFO, "setuid failed to "
146 "to set the effective uid to %d",
147 ltpuser->pw_uid);
148 perror("seteuid");
152 /* get an IPC resource key */
153 shmkey = getipckey();
155 /* create a shared memory segment without read or access permissions */
156 if ((shm_id_1 = shmget(shmkey, SHM_SIZE, IPC_CREAT | IPC_EXCL)) == -1) {
157 tst_brkm(TBROK, cleanup, "Failed to create shared memory "
158 "segment in setup");
163 * cleanup() - performs all the ONE TIME cleanup for this test at completion
164 * or premature exit.
166 void
167 cleanup(void)
169 /* if it exists, remove the shared memory resource */
170 rm_shm(shm_id_1);
172 if (seteuid(0) == -1) {
173 tst_resm(TINFO, "setuid failed to "
174 "to set the effective uid to root");
175 perror("seteuid");
178 /* Remove the temporary directory */
179 tst_rmdir();
182 * print timing stats if that option was specified.
183 * print errno log if that option was specified.
185 TEST_CLEANUP;
187 /* exit with return code appropriate for results */
188 tst_exit();