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
25 * semctl01 - test the 10 possible semctl() commands
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.
36 * if doing functionality testing
37 * call the appropriate test function
39 * issue a PASS message
41 * issue a FAIL message
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.
54 * 03/2001 - Written by Wayne Boyer
62 char *TCID
= "semctl01";
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.
73 void set_setup(int), func_set(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);
85 unsigned short array
[PSEMS
];
88 #define INCVAL 2 /* a semaphore increment value */
91 #define SEM2 2 /* semaphore to use for GETPID and GETVAL */
92 #define SEM4 4 /* semaphore to use for GETNCNT and GETZCNT */
95 #define SEMUN_CAST (union semun)
104 int semnum
; /* the primitive semaphore to use */
105 int cmd
; /* the command to test */
106 void (*func_test
)(int); /* the test function */
108 void (*func_setup
)(int); /* the setup function if necessary */
111 void setup_test_cases(void)
117 /* {0, IPC_STAT, func_stat, &buf, NULL}, */
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}, */
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}, */
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}, */
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}, */
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}, */
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}, */
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}, */
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}, */
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} */
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 */
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
);
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
);
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 */
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 */
234 (*TC
[i
].func_setup
)(-ONE
);
237 (*TC
[i
].func_setup
)(0);
240 (*TC
[i
].func_setup
)(ONE
);
246 * Use TEST macro to make the call
249 TEST(semctl(sem_id_1
, TC
[i
].semnum
, TC
[i
].cmd
,
252 if (TEST_RETURN
== -1) {
253 tst_resm(TFAIL
, "%s call failed - errno = %d "
254 ": %s", TCID
, TEST_ERRNO
,
255 strerror(TEST_ERRNO
));
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.
271 (*TC
[i
].func_test
)(TEST_RETURN
);
274 (*TC
[i
].func_test
)(0);
278 tst_resm(TPASS
, "call succeeded");
283 * If testing GETNCNT or GETZCNT, clean up the children.
289 for (j
=0; j
<NCHILD
; j
++) {
290 if (kill(pid_arr
[j
], SIGKILL
) == -1) {
291 tst_brkm(TBROK
, cleanup
,
292 "child kill failed");
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 "
318 * func_stat() - check the functionality of the IPC_STAT command with semctl()
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"
328 tst_resm(TFAIL
, "semaphore STAT info is incorrect");
333 * set_setup() - set up for the IPC_SET command with semctl()
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()
348 /* first stat the semaphore to get the new data */
352 if (semctl(sem_id_1
, 0, IPC_STAT
, arg
) == -1) {
353 tst_resm(TBROK
, "stat failed in func_set()");
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");
361 tst_resm(TFAIL
, "semaphore mode info is incorrect");
366 * func_gall() - check the functionality of the GETALL command with semctl()
373 /* the initial value of the primitive semaphores should be zero */
374 for (i
=0 ; i
<PSEMS
; i
++) {
376 tst_resm(TFAIL
, "semaphore %d has unexpected value", i
);
380 tst_resm(TPASS
, "semaphores have expected values");
384 * cnt_setup() - set up for the GETNCNT and GETZCNT commands with semctl()
395 * if seting up for GETZCNT, the semaphore value needs to be positive
398 /* initialize the semaphore value to 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 */
415 if (self_exec(argv0
, "ndd", 2, sem_id_1
,
417 tst_brkm(TBROK
, cleanup
, "self_exec failed "
423 } else { /* parent */
424 /* take a quick nap so that commands execute orderly */
427 /* save the pid so we can kill it later */
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");
452 * func_cnt() - check the functionality of the GETNCNT and GETZCNT commands
459 if (rval
== NCHILD
) {
460 tst_resm(TPASS
, "number of sleeping processes is correct");
462 tst_resm(TFAIL
, "number of sleeping processes is not correct");
467 * pid_setup() - set up for the GETPID command with semctl()
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 */
483 if (self_exec(argv0
, "nd", 1, sem_id_1
) < 0) {
484 tst_brkm(TBROK
, cleanup
, "self_exec failed "
490 } else { /* parent */
491 /* take a quick nap so that commands execute orderly */
501 sops
.sem_num
= SEM2
; /* semaphore to change */
502 sops
.sem_op
= ONE
; /* operation is to increment semaphore */
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");
515 * func_pid() - check the functionality of the GETPID command with semctl()
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");
524 tst_resm(TFAIL
, "last pid value is not correct");
529 * func_gval() - check the functionality of the GETVAL command with semctl()
535 * This is a simple test. The semaphore value should be equal
536 * to ONE as it was set in the last test (GETPID).
539 tst_resm(TPASS
, "semaphore value is correct");
541 tst_resm(TFAIL
, "semaphore value is not correct");
547 * all_setup() - set up for the SETALL command with semctl()
554 for (i
=0; i
<PSEMS
; i
++) {
555 /* initialize the array values to 3 */
561 * func_sall() - check the functionality of the SETALL command with semctl()
567 unsigned short rarray
[PSEMS
];
571 * do a GETALL and compare the values to those set above
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");
586 tst_resm(TPASS
, "semaphore values are correct");
590 * func_sval() - check the functionality of the SETVAL command with semctl()
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");
609 tst_resm(TPASS
, "semaphore value is correct");
614 * func_rmid() - check the functionality of the IPC_RMID command with semctl()
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
);
630 tst_resm(TPASS
, "semaphore appears to be removed");
637 * setup() - performs all the ONE TIME setup for this test.
644 /* capture signals */
645 tst_sig(FORK
, DEF_HANDLER
, cleanup
);
647 /* Pause if that option was specified */
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.
657 /* get an IPC resource key */
658 semkey
= getipckey();
660 /* create a semaphore set with read and alter permissions */
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
674 /* if it exists, remove the semaphore resource */
677 /* Remove the temporary directory */
681 * print timing stats if that option was specified.
682 * print errno log if that option was specified.
686 /* exit with return code appropriate for results */