vm: fix region reporting bug
[minix.git] / test / ipc / semop / semop05.c
blobf30db6bd92cb33b347dde3cb52f32c83e9628a4d
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 * semop05.c
24 * DESCRIPTION
25 * semop05 - test for EINTR and EIDRM errors
27 * ALGORITHM
28 * create a semaphore set with read and alter permissions
29 * loop if that option was specified
30 * set up the s_buf buffer
31 * initialize the primitive semaphores
32 * fork a child process
33 * child calls semop() and sleeps
34 * parent either removes the semaphore set or sends a signal to the child
35 * parent then exits
36 * child gets a return from the semop() call
37 * check the errno value
38 * issue a PASS message if we get EINTR or EIDRM
39 * otherwise, the tests fails
40 * issue a FAIL message
41 * call cleanup
43 * USAGE: <for command-line>
44 * semop05 [-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 * none
59 #include "ipcsem.h"
60 #include <sys/types.h>
61 #include <sys/wait.h>
63 void sighandler(int);
65 char *TCID = "semop05";
66 int TST_TOTAL = 4;
67 extern int Tst_count;
69 int exp_enos[] = {EINTR, EIDRM, 0}; /* 0 terminated list of expected errnos */
71 int sem_id_1 = -1;
73 struct sembuf s_buf;
75 struct test_case_t {
76 union semun semunptr;
77 short op;
78 short flg;
79 short num;
80 int error;
81 } TC[] = {
82 /* EIRDM sem_op = 0 */
83 {{1}, 0, 0, 2, EIDRM},
85 /* EIRDM sem_op = -1 */
86 {{0}, -1, 0, 3, EIDRM},
88 /* EINTR sem_op = 0 */
89 {{1}, 0, 0, 4, EINTR},
91 /* EINTR sem_op = -1 */
92 {{0}, -1, 0, 5, EINTR}
95 #ifdef UCLINUX
96 void do_child_uclinux();
97 static int i_uclinux;
98 #endif
100 int main(int ac, char **av)
102 int lc; /* loop counter */
103 char *msg; /* message returned from parse_opts */
104 int i;
105 pid_t pid;
106 void do_child(int);
108 /* parse standard options */
109 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
110 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
113 #ifdef UCLINUX
114 maybe_run_child(&do_child_uclinux, "dd", &i_uclinux, &sem_id_1);
115 #endif
117 setup(); /* global setup */
119 /* The following loop checks looping state if -i option given */
121 for (lc = 0; TEST_LOOPING(lc); lc++) {
122 /* reset Tst_count in case we are looping */
123 Tst_count = 0;
125 for (i=0; i<TST_TOTAL; i++) {
127 /* initialize the s_buf buffer */
128 s_buf.sem_op = TC[i].op;
129 s_buf.sem_flg = TC[i].flg;
130 s_buf.sem_num = TC[i].num;
132 /* initialize all of the primitive semaphores */
133 if (semctl(sem_id_1, TC[i].num, SETVAL, TC[i].semunptr)
134 == -1) {
135 tst_brkm(TBROK, cleanup, "semctl() failed");
138 if ((pid = fork()) == -1) {
139 tst_brkm(TBROK, cleanup, "could not fork");
142 if (pid == 0) { /* child */
143 #ifdef UCLINUX
144 if (self_exec(av[0], "dd", i, sem_id_1) < 0) {
145 tst_brkm(TBROK, cleanup,
146 "could not self_exec");
148 #else
149 do_child(i);
150 #endif
151 } else { /* parent */
152 usleep(250000);
155 * If we are testing for EIDRM then remove
156 * the semaphore, else send a signal that
157 * must be caught as we are testing for
158 * EINTR.
160 if (TC[i].error == EIDRM) {
161 /* remove the semaphore resource */
162 rm_sema(sem_id_1);
163 } else {
164 if (kill(pid, SIGHUP) == -1) {
165 tst_brkm(TBROK, cleanup,
166 "kill failed");
170 /* let the child carry on */
171 waitpid(pid,NULL,0);
175 * recreate the semaphore resource if needed
177 if (TC[i].error == EINTR) {
178 continue;
181 if ((sem_id_1 = semget(semkey, PSEMS, IPC_CREAT |
182 IPC_EXCL | SEM_RA)) == -1) {
183 tst_brkm(TBROK, cleanup, "couldn't recreate "
184 "semaphore");
189 cleanup();
191 /*NOTREACHED*/
192 return(0);
196 * do_child()
198 void
199 do_child(int i)
202 * make the call with the TEST macro
205 TEST(semop(sem_id_1, &s_buf, 1));
207 if (TEST_RETURN != -1) {
208 tst_resm(TFAIL, "call succeeded when error expected");
209 exit(-1);
212 TEST_ERROR_LOG(TEST_ERRNO);
214 if (TEST_ERRNO == TC[i].error) {
215 tst_resm(TPASS, "expected failure - errno = %d"
216 " : %s", TEST_ERRNO, strerror(TEST_ERRNO));
217 } else {
218 tst_resm(TFAIL, "unexpected error - "
219 "%d : %s", TEST_ERRNO, strerror(TEST_ERRNO));
222 exit(0);
225 #ifdef UCLINUX
227 * do_child_uclinux() - capture signals, re-initialize s_buf then call do_child
228 * with the appropriate argument
230 void
231 do_child_uclinux()
233 int i = i_uclinux;
235 /* capture signals */
236 tst_sig(FORK, sighandler, cleanup);
238 /* initialize the s_buf buffer */
239 s_buf.sem_op = TC[i].op;
240 s_buf.sem_flg = TC[i].flg;
241 s_buf.sem_num = TC[i].num;
243 do_child(i);
245 #endif
248 * sighandler() - handle signals
250 void
251 sighandler(int sig)
253 /* we don't need to do anything here */
257 * setup() - performs all the ONE TIME setup for this test.
259 void
260 setup(void)
262 /* capture signals */
263 tst_sig(FORK, sighandler, cleanup);
265 /* Set up the expected error numbers for -e option */
266 TEST_EXP_ENOS(exp_enos);
268 /* Pause if that option was specified */
269 TEST_PAUSE;
272 * Create a temporary directory and cd into it.
273 * This helps to ensure that a unique msgkey is created.
274 * See ../lib/libipc.c for more information.
276 tst_tmpdir();
278 /* get an IPC resource key */
279 semkey = getipckey();
281 /* create a semaphore set with read and alter permissions */
282 /* and PSEMS "primitive" semaphores */
283 if ((sem_id_1 =
284 semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) == -1) {
285 tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
290 * cleanup() - performs all the ONE TIME cleanup for this test at completion
291 * or premature exit.
293 void
294 cleanup(void)
296 /* if it exists, remove the semaphore resource */
297 rm_sema(sem_id_1);
299 /* Remove the temporary directory */
300 tst_rmdir();
303 * print timing stats if that option was specified.
304 * print errno log if that option was specified.
306 TEST_CLEANUP;
308 /* exit with return code appropriate for results */
310 tst_exit();