vm: use arch_map2str to print pagefault info, to properly display code addrs
[minix.git] / test / ipc / semget / semget02.c
blobfd1a2eb68b24cd3491840ba7addcd0bc7fcd2473
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 * semget02.c
24 * DESCRIPTION
25 * semget02 - test for EACCES and EEXIST errors
27 * ALGORITHM
28 * create a semaphore set without read or alter permissions
29 * loop if that option was specified
30 * call semget() using two different invalid cases
31 * check the errno value
32 * issue a PASS message if we get EACCES or EEXIST
33 * otherwise, the tests fails
34 * issue a FAIL message
35 * call cleanup
37 * USAGE: <for command-line>
38 * semget02 [-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>
54 #include "../lib/ipcsem.h"
56 char *TCID = "semget02";
57 int TST_TOTAL = 2;
58 extern int Tst_count;
60 int exp_enos[] = {EACCES, EEXIST, 0};
62 char nobody_uid[] = "nobody";
63 struct passwd *ltpuser;
66 int sem_id_1 = -1;
68 struct test_case_t {
69 int flags;
70 int error;
71 } TC [] = {
72 /* EACCES - the semaphore has no read or alter permissions */
73 {SEM_RA, EACCES},
75 /* EEXIST - the semaphore id exists and semget() was called with */
76 /* IPC_CREAT and IPC_EXCL */
77 {IPC_CREAT | IPC_EXCL, EEXIST}
80 int main(int ac, char **av)
82 int lc; /* loop counter */
83 char *msg; /* message returned from parse_opts */
84 int i;
86 /* parse standard options */
87 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
88 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
91 setup(); /* global setup */
93 /* The following loop checks looping state if -i option given */
95 for (lc = 0; TEST_LOOPING(lc); lc++) {
96 /* reset Tst_count in case we are looping */
97 Tst_count = 0;
99 for (i=0; i<TST_TOTAL; i++) {
100 /* use the TEST macro to make the call */
102 TEST(semget(semkey, PSEMS, TC[i].flags));
104 if (TEST_RETURN != -1) {
105 sem_id_1 = TEST_RETURN;
106 tst_resm(TFAIL, "call succeeded");
107 continue;
110 TEST_ERROR_LOG(TEST_ERRNO);
112 if (TEST_ERRNO == TC[i].error) {
113 tst_resm(TPASS, "expected failure - errno "
114 "= %d : %s", TEST_ERRNO,
115 strerror(TEST_ERRNO));
116 } else {
117 tst_resm(TFAIL, "unexpected error - %d : %s",
118 TEST_ERRNO, strerror(TEST_ERRNO));
123 cleanup();
125 /*NOTREACHED*/
126 return(0);
130 * setup() - performs all the ONE TIME setup for this test.
132 void
133 setup(void)
135 /* Switch to nobody user for correct error code collection */
136 if (geteuid() != 0) {
137 tst_brkm(TBROK, tst_exit, "Test must be run as root");
140 /* capture signals */
141 tst_sig(NOFORK, DEF_HANDLER, cleanup);
143 /* Set up the expected error numbers for -e option */
144 TEST_EXP_ENOS(exp_enos);
146 /* Pause if that option was specified */
147 TEST_PAUSE;
150 * Create a temporary directory and cd into it.
151 * This helps to ensure that a unique msgkey is created.
152 * See ../lib/libipc.c for more information.
154 tst_tmpdir();
156 ltpuser = getpwnam(nobody_uid);
157 if (seteuid(ltpuser->pw_uid) == -1) {
158 tst_resm(TINFO, "setreuid failed to "
159 "to set the effective uid to %d",
160 ltpuser->pw_uid);
161 perror("setreuid");
165 /* get an IPC resource key */
166 semkey = getipckey();
168 /* create a semaphore set without read or alter permissions */
169 if ((sem_id_1 = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL)) == -1) {
170 tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
175 * cleanup() - performs all the ONE TIME cleanup for this test at completion
176 * or premature exit.
178 void
179 cleanup(void)
181 /* if it exists, remove the semaphore resource */
182 rm_sema(sem_id_1);
184 /* Remove the temporary directory */
185 seteuid(getuid());
186 tst_rmdir();
189 * print timing stats if that option was specified.
190 * print errno log if that option was specified.
192 TEST_CLEANUP;
194 /* exit with return code appropriate for results */
195 tst_exit();