unstack, sort: cleanup and improvement
[minix.git] / test / ipc / semctl / semctl01.c
blob111ea45f699518abc622b7d2810f33b9eacb14c7
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 * semctl01.c
24 * DESCRIPTION
25 * semctl01 - test the 10 possible semctl() commands
27 * ALGORITHM
28 * create a semaphore set with read and alter permissions
29 * loop if that option was specified
30 * loop through the test cases
31 * do any setup required for the test case
32 * make the semctl() call using the TEST() macro
33 * check the return code
34 * if failure, issue a FAIL message.
35 * otherwise,
36 * if doing functionality testing
37 * call the appropriate test function
38 * if correct,
39 * issue a PASS message
40 * otherwise
41 * issue a FAIL message
42 * call cleanup
44 * USAGE: <for command-line>
45 * semctl01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
46 * where, -c n : Run n copies concurrently.
47 * -f : Turn off functionality Testing.
48 * -i n : Execute test n times.
49 * -I x : Execute test for x seconds.
50 * -P x : Pause for x seconds between iterations.
51 * -t : Turn on syscall timing.
53 * HISTORY
54 * 03/2001 - Written by Wayne Boyer
56 * RESTRICTIONS
57 * none
60 #include "ipcsem.h"
62 char *TCID = "semctl01";
63 int TST_TOTAL = 10;
64 extern int Tst_count;
66 int sem_id_1 = -1; /* a semaphore set with read and alter permissions */
69 * These are the various setup and check functions for the 10 different
70 * commands that are available for the semctl() call.
72 void func_stat(int);
73 void set_setup(int), func_set(int);
74 void func_gall(int);
75 void cnt_setup(int), func_cnt(int);
76 void pid_setup(int), func_pid(int);
77 void gval_setup(int), func_gval(int);
78 void sall_setup(int), func_sall(int);
79 void func_sval(int);
80 void func_rmid(int);
81 void child_cnt(void);
82 void child_pid(void);
84 struct semid_ds buf;
85 unsigned short array[PSEMS];
86 struct sembuf sops;
88 #define INCVAL 2 /* a semaphore increment value */
89 #define NEWMODE 066
90 #define NCHILD 5
91 #define SEM2 2 /* semaphore to use for GETPID and GETVAL */
92 #define SEM4 4 /* semaphore to use for GETNCNT and GETZCNT */
93 #define ONE 1
95 #define SEMUN_CAST (union semun)
97 int pid_arr[NCHILD];
99 #ifdef UCLINUX
100 static char *argv0;
101 #endif
103 struct test_case_t {
104 int semnum; /* the primitive semaphore to use */
105 int cmd; /* the command to test */
106 void (*func_test)(int); /* the test function */
107 union semun arg;
108 void (*func_setup)(int); /* the setup function if necessary */
109 } TC[10];
111 void setup_test_cases(void)
113 int i;
115 i = -1;
117 /* {0, IPC_STAT, func_stat, &buf, NULL}, */
118 i++;
119 TC[i].semnum = 0;
120 TC[i].cmd = IPC_STAT;
121 TC[i].func_test = func_stat;
122 TC[i].arg.buf = &buf;
123 TC[i].func_setup = NULL;
125 /* {0, IPC_SET, func_set, &buf, set_setup}, */
126 i++;
127 TC[i].semnum = 0;
128 TC[i].cmd = IPC_SET;
129 TC[i].func_test = func_set;
130 TC[i].arg.buf = &buf;
131 TC[i].func_setup = set_setup;
133 /* {0, GETALL, func_gall, array, NULL}, */
134 i++;
135 TC[i].semnum = 0;
136 TC[i].cmd = GETALL;
137 TC[i].func_test = func_gall;
138 TC[i].arg.array = array;
139 TC[i].func_setup = NULL;
141 /* {SEM4, GETNCNT, func_cnt, SEMUN_CAST &buf, cnt_setup}, */
142 i++;
143 TC[i].semnum = SEM4;
144 TC[i].cmd = GETNCNT;
145 TC[i].func_test = func_cnt;
146 TC[i].arg.buf = &buf;
147 TC[i].func_setup = cnt_setup;
149 /* {SEM2, GETPID, func_pid, SEMUN_CAST &buf, pid_setup}, */
150 i++;
151 TC[i].semnum = SEM2;
152 TC[i].cmd = GETPID;
153 TC[i].func_test = func_pid;
154 TC[i].arg.buf = &buf;
155 TC[i].func_setup = pid_setup;
157 /* {SEM2, GETVAL, func_gval, SEMUN_CAST &buf, NULL}, */
158 i++;
159 TC[i].semnum = SEM2;
160 TC[i].cmd = GETVAL;
161 TC[i].func_test = func_gval;
162 TC[i].arg.buf = &buf;
163 TC[i].func_setup = NULL;
165 /* {SEM4, GETZCNT, func_cnt, SEMUN_CAST &buf, cnt_setup}, */
166 i++;
167 TC[i].semnum = SEM4;
168 TC[i].cmd = GETZCNT;
169 TC[i].func_test = func_cnt;
170 TC[i].arg.buf = &buf;
171 TC[i].func_setup = cnt_setup;
173 /* {0, SETALL, func_sall, SEMUN_CAST array, sall_setup}, */
174 i++;
175 TC[i].semnum = 0;
176 TC[i].cmd = SETALL;
177 TC[i].func_test = func_sall;
178 TC[i].arg.array = array;
179 TC[i].func_setup = sall_setup;
181 /* {SEM4, SETVAL, func_sval, SEMUN_CAST INCVAL, NULL}, */
182 i++;
183 TC[i].semnum = SEM4;
184 TC[i].cmd = SETVAL;
185 TC[i].func_test = func_sval;
186 TC[i].arg.val = INCVAL;
187 TC[i].func_setup = NULL;
189 /* {0, IPC_RMID, func_rmid, SEMUN_CAST &buf, NULL} */
190 i++;
191 TC[i].semnum = 0;
192 TC[i].cmd = IPC_RMID;
193 TC[i].func_test = func_rmid;
194 TC[i].arg.buf = &buf;
195 TC[i].func_setup = NULL;
198 int main(int ac, char **av)
200 int lc; /* loop counter */
201 char *msg; /* message returned from parse_opts */
202 int i, j;
204 /* parse standard options */
205 if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
206 tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
209 #ifdef UCLINUX
210 argv0 = av[0];
211 maybe_run_child(&child_pid, "nd", 1, &sem_id_1);
212 maybe_run_child(&child_cnt, "ndd", 2, &sem_id_1, &sops.sem_op);
213 #endif
215 setup(); /* global setup */
217 /* The following loop checks looping state if -i option given */
219 for (lc = 0; TEST_LOOPING(lc); lc++) {
220 /* reset Tst_count in case we are looping */
221 Tst_count = 0;
223 /* loop through the test cases */
224 for (i=0; i<TST_TOTAL; i++) {
227 * Set up any conditions if needed
230 if (TC[i].func_setup != NULL) {
231 /* call the setup function */
232 switch (TC[i].cmd) {
233 case GETNCNT:
234 (*TC[i].func_setup)(-ONE);
235 break;
236 case GETZCNT:
237 (*TC[i].func_setup)(0);
238 break;
239 default:
240 (*TC[i].func_setup)(ONE);
241 break;
246 * Use TEST macro to make the call
249 TEST(semctl(sem_id_1, TC[i].semnum, TC[i].cmd,
250 TC[i].arg));
252 if (TEST_RETURN == -1) {
253 tst_resm(TFAIL, "%s call failed - errno = %d "
254 ": %s", TCID, TEST_ERRNO,
255 strerror(TEST_ERRNO));
256 } else {
257 if (STD_FUNCTIONAL_TEST) {
259 * call the appropriate test function
260 * and pass the return value where it
261 * is needed to perform certain tests.
263 switch (TC[i].cmd) {
264 case GETNCNT:
265 /*FALLTHROUGH*/
266 case GETZCNT:
267 /*FALLTHROUGH*/
268 case GETPID:
269 /*FALLTHROUGH*/
270 case GETVAL:
271 (*TC[i].func_test)(TEST_RETURN);
272 break;
273 default:
274 (*TC[i].func_test)(0);
275 break;
277 } else {
278 tst_resm(TPASS, "call succeeded");
283 * If testing GETNCNT or GETZCNT, clean up the children.
285 switch (TC[i].cmd) {
286 case GETNCNT:
287 /*FALLTHROUGH*/
288 case GETZCNT:
289 for (j=0; j<NCHILD; j++) {
290 if (kill(pid_arr[j], SIGKILL) == -1) {
291 tst_brkm(TBROK, cleanup,
292 "child kill failed");
295 break;
299 * recreate the semaphore resource if looping
301 if (TEST_LOOPING(lc)) {
302 if ((sem_id_1 = semget(semkey, PSEMS,
303 IPC_CREAT | IPC_EXCL | SEM_RA)) == -1 ) {
304 tst_brkm(TBROK, cleanup, "couldn't recreate "
305 "semaphore");
310 cleanup();
312 /*NOTREACHED*/
313 return(0);
318 * func_stat() - check the functionality of the IPC_STAT command with semctl()
320 void
321 func_stat(int n)
323 /* check the number of semaphores and the ipc_perm.mode value */
324 if (buf.sem_nsems == PSEMS && buf.sem_perm.mode == (SEM_RA)) {
325 tst_resm(TPASS, "buf.sem_nsems and buf.sem_perm.mode"
326 " are correct");
327 } else {
328 tst_resm(TFAIL, "semaphore STAT info is incorrect");
333 * set_setup() - set up for the IPC_SET command with semctl()
335 void
336 set_setup(int n)
338 /* set up a new mode for the semaphore set */
339 buf.sem_perm.mode = SEM_RA | NEWMODE;
343 * func_set() - check the functionality of the IPC_SET command with semctl()
345 void
346 func_set(int n)
348 /* first stat the semaphore to get the new data */
349 union semun arg;
351 arg.buf = &buf;
352 if (semctl(sem_id_1, 0, IPC_STAT, arg) == -1) {
353 tst_resm(TBROK, "stat failed in func_set()");
354 return;
357 /* check that the new mode is what we set */
358 if (buf.sem_perm.mode == (SEM_RA | NEWMODE)) {
359 tst_resm(TPASS, "buf.sem_perm.mode is correct");
360 } else {
361 tst_resm(TFAIL, "semaphore mode info is incorrect");
366 * func_gall() - check the functionality of the GETALL command with semctl()
368 void
369 func_gall(int n)
371 int i;
373 /* the initial value of the primitive semaphores should be zero */
374 for (i=0 ; i<PSEMS; i++) {
375 if (array[i] != 0) {
376 tst_resm(TFAIL, "semaphore %d has unexpected value", i);
377 return;
380 tst_resm(TPASS, "semaphores have expected values");
384 * cnt_setup() - set up for the GETNCNT and GETZCNT commands with semctl()
386 void
387 cnt_setup(int opval)
389 int pid, i;
391 sops.sem_num = SEM4;
392 sops.sem_flg = 0;
395 * if seting up for GETZCNT, the semaphore value needs to be positive
397 if (opval == 0) {
398 /* initialize the semaphore value to ONE */
399 sops.sem_op = ONE;
400 if (semop(sem_id_1, &sops, 1) == -1) {
401 tst_brkm(TBROK, cleanup, "semop #1 failed - cnt_setup");
405 sops.sem_op = opval; /* set the correct operation */
407 for (i=0; i<NCHILD; i++) {
408 /* fork five children to wait */
409 if ((pid = FORK_OR_VFORK()) == -1) {
410 tst_brkm(TBROK, cleanup, "fork failed in cnt_setup");
413 if (pid == 0) { /* child */
414 #ifdef UCLINUX
415 if (self_exec(argv0, "ndd", 2, sem_id_1,
416 sops.sem_op) < 0) {
417 tst_brkm(TBROK, cleanup, "self_exec failed "
418 "in cnt_setup");
420 #else
421 child_cnt();
422 #endif
423 } else { /* parent */
424 /* take a quick nap so that commands execute orderly */
425 usleep(50000);
427 /* save the pid so we can kill it later */
428 pid_arr[i] = pid;
433 void
434 child_cnt(void)
436 sops.sem_num = SEM4;
437 sops.sem_flg = 0;
440 * Do a semop that will cause the child to sleep.
441 * The child process will be killed in the func_ncnt
442 * routine which should cause an error to be return
443 * by the semop() call.
445 if (semop(sem_id_1, &sops, 1) != -1) {
446 tst_resm(TBROK, "semop succeeded - cnt_setup");
448 exit(0);
452 * func_cnt() - check the functionality of the GETNCNT and GETZCNT commands
453 * with semctl()
455 void
456 func_cnt(int rval)
459 if (rval == NCHILD) {
460 tst_resm(TPASS, "number of sleeping processes is correct");
461 } else {
462 tst_resm(TFAIL, "number of sleeping processes is not correct");
467 * pid_setup() - set up for the GETPID command with semctl()
469 void
470 pid_setup(int n)
472 int pid;
475 * Fork a child to do a semop that will pass.
477 if ((pid = FORK_OR_VFORK()) == -1) {
478 tst_brkm(TBROK, cleanup, "fork failed in pid_setup()");
481 if (pid == 0) { /* child */
482 #ifdef UCLINUX
483 if (self_exec(argv0, "nd", 1, sem_id_1) < 0) {
484 tst_brkm(TBROK, cleanup, "self_exec failed "
485 "in pid_setup()");
487 #else
488 child_pid();
489 #endif
490 } else { /* parent */
491 /* take a quick nap so that commands execute orderly */
492 usleep(50000);
494 pid_arr[SEM2] = pid;
498 void
499 child_pid(void)
501 sops.sem_num = SEM2; /* semaphore to change */
502 sops.sem_op = ONE; /* operation is to increment semaphore */
503 sops.sem_flg = 0;
506 * Do a semop that will increment the semaphore.
508 if (semop(sem_id_1, &sops, 1) == -1) {
509 tst_resm(TBROK, "semop failed - pid_setup");
511 exit(0);
515 * func_pid() - check the functionality of the GETPID command with semctl()
517 void
518 func_pid(int rval)
520 /* compare the rval (pid) to the saved pid from the setup */
521 if (rval == pid_arr[SEM2]) {
522 tst_resm(TPASS, "last pid value is correct");
523 } else {
524 tst_resm(TFAIL, "last pid value is not correct");
529 * func_gval() - check the functionality of the GETVAL command with semctl()
531 void
532 func_gval(int rval)
535 * This is a simple test. The semaphore value should be equal
536 * to ONE as it was set in the last test (GETPID).
538 if (rval == 1) {
539 tst_resm(TPASS, "semaphore value is correct");
540 } else {
541 tst_resm(TFAIL, "semaphore value is not correct");
547 * all_setup() - set up for the SETALL command with semctl()
549 void
550 sall_setup(int n)
552 int i;
554 for (i=0; i<PSEMS; i++) {
555 /* initialize the array values to 3 */
556 array[i] = 3;
561 * func_sall() - check the functionality of the SETALL command with semctl()
563 void
564 func_sall(int n)
566 int i;
567 unsigned short rarray[PSEMS];
568 union semun arg;
571 * do a GETALL and compare the values to those set above
574 arg.array = rarray;
575 if (semctl(sem_id_1, 0, GETALL, arg) == -1) {
576 tst_brkm(TBROK, cleanup, "semctl failed in func_sall");
579 for (i=0; i<PSEMS; i++) {
580 if (array[i] != rarray[i]) {
581 tst_resm(TFAIL, "semaphore values are not correct");
582 return;
586 tst_resm(TPASS, "semaphore values are correct");
590 * func_sval() - check the functionality of the SETVAL command with semctl()
592 void
593 func_sval(int n)
595 int semv;
596 union semun arr;
599 * do a GETVAL and compare it to the value set above
602 if ((semv = semctl(sem_id_1, SEM4, GETVAL, arr)) == -1) {
603 tst_brkm(TBROK, cleanup, "semctl failed in func_sval");
606 if (semv != INCVAL) {
607 tst_resm(TFAIL, "semaphore value is not what was set");
608 } else {
609 tst_resm(TPASS, "semaphore value is correct");
614 * func_rmid() - check the functionality of the IPC_RMID command with semctl()
616 void
617 func_rmid(int n)
621 * do a semop() - we should get EINVAL
623 if (semop(sem_id_1, &sops, 1) != -1) {
624 tst_resm(TFAIL, "semop succeeded on expected fail");
627 if (errno != EINVAL) {
628 tst_resm(TFAIL, "returned errno - %d - is not expected", errno);
629 } else {
630 tst_resm(TPASS, "semaphore appears to be removed");
633 sem_id_1 = -1;
637 * setup() - performs all the ONE TIME setup for this test.
639 void
640 setup(void)
642 setup_test_cases();
644 /* capture signals */
645 tst_sig(FORK, DEF_HANDLER, cleanup);
647 /* Pause if that option was specified */
648 TEST_PAUSE;
651 * Create a temporary directory and cd into it.
652 * This helps to ensure that a unique msgkey is created.
653 * See ../lib/libipc.c for more information.
655 tst_tmpdir();
657 /* get an IPC resource key */
658 semkey = getipckey();
660 /* create a semaphore set with read and alter permissions */
661 if ((sem_id_1 =
662 semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) == -1 ) {
663 tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
668 * cleanup() - performs all the ONE TIME cleanup for this test at completion
669 * or premature exit.
671 void
672 cleanup(void)
674 /* if it exists, remove the semaphore resource */
675 rm_sema(sem_id_1);
677 /* Remove the temporary directory */
678 tst_rmdir();
681 * print timing stats if that option was specified.
682 * print errno log if that option was specified.
684 TEST_CLEANUP;
686 /* exit with return code appropriate for results */
687 tst_exit();