VM: simplify slab allocator
[minix.git] / test / ipc / shmget / shmget05.c
blob051208fe2395a7436ef94b5d545d3678a640a672
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 * shmget05.c
24 * DESCRIPTION
25 * shmget05 - 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 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
38 * call cleanup
39 * if parent
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.
52 * HISTORY
53 * 03/2001 - Written by Wayne Boyer
55 * RESTRICTIONS
56 * test must be run at root
59 #include "ipcshm.h"
60 #include <sys/types.h>
61 #include <sys/wait.h>
63 char *TCID = "shmget05";
64 int TST_TOTAL = 1;
65 extern int Tst_count;
67 int exp_enos[] = {EACCES, 0}; /* 0 terminated list of expected errnos */
69 int shm_id_1 = -1;
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 cleanup();
102 /*NOTREACHED*/
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 */
110 rm_shm(shm_id_1);
112 /* Remove the temporary directory */
113 tst_rmdir();
115 return(0);
119 * do_child - make the TEST call as the child process
121 void
122 do_child(void)
124 int lc;
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 */
130 Tst_count = 0;
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");
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, strerror(TEST_ERRNO));
149 break;
150 default:
151 tst_resm(TFAIL, "call failed with an "
152 "unexpected error - %d : %s",
153 TEST_ERRNO, strerror(TEST_ERRNO));
154 break;
160 * setup() - performs all the ONE TIME setup for this test.
162 void
163 setup(void)
165 /* check for root as process owner */
166 check_root();
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 */
175 TEST_PAUSE;
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.
182 tst_tmpdir();
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 "
191 "segment in setup");
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
200 * or premature exit.
202 void
203 cleanup(void)
206 * print timing stats if that option was specified.
207 * print errno log if that option was specified.
209 TEST_CLEANUP;
211 /* exit with return code appropriate for results */
212 tst_exit();