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 * semop01 - test that semop() basic functionality is correct
28 * create a semaphore set and initialize some values
29 * loop if that option was specified
30 * call semop() to set values for the primitive semaphores
31 * check the return code
32 * if failure, issue a FAIL message.
34 * if doing functionality testing
35 * get the semaphore values and compare with expected values
37 * issue a PASS message
39 * issue a FAIL message
40 * else issue a PASS message
43 * USAGE: <for command-line>
44 * semop01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
45 * where, -c n : Run n copies concurrently.
46 * -f : Turn off functionality Testing.
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.
53 * 03/2001 - Written by Wayne Boyer
54 * 17/01/02 - Modified. Manoj Iyer, IBM Austin. TX. manjo@austin.ibm.com
55 * 4th argument to semctl() system call was modified according
57 * In my opinion The test should not even have compiled but
58 * it was working due to some mysterious reason.
66 #define NSEMS 4 /* the number of primitive semaphores to test */
68 char *TCID
= "semop01";
72 int sem_id_1
= -1; /* a semaphore set with read & alter permissions */
75 struct sembuf sops
[PSEMS
]; /* an array of sembuf structures */
78 int main(int ac
, char **av
)
81 int lc
; /* loop counter */
82 char *msg
; /* message returned from parse_opts */
86 get_arr
.array
= malloc(sizeof(unsigned short int) * PSEMS
);
87 /* parse standard options */
88 if ((msg
= parse_opts(ac
, av
, (option_t
*)NULL
, NULL
)) != (char *)NULL
){
89 tst_brkm(TBROK
, cleanup
, "OPTION PARSING ERROR - %s", msg
);
92 setup(); /* global setup */
94 /* The following loop checks looping state if -i option given */
96 for (lc
= 0; TEST_LOOPING(lc
); lc
++) {
97 /* reset Tst_count in case we are looping */
101 * Use TEST macro to make the call
104 TEST(semop(sem_id_1
, sops
, NSEMS
));
106 if (TEST_RETURN
== -1) {
107 tst_resm(TFAIL
, "%s call failed - errno = %d "
108 ": %s", TCID
, TEST_ERRNO
,
109 strerror(TEST_ERRNO
));
111 if (STD_FUNCTIONAL_TEST
) {
113 /* get the values and make sure they */
114 /* are the same as what was set */
115 if (semctl(sem_id_1
, 0, GETALL
, get_arr
) ==
117 tst_brkm(TBROK
, cleanup
, "semctl() "
118 "failed in functional test");
121 for (i
=0; i
<NSEMS
; i
++) {
122 if (get_arr
.array
[i
] != i
*i
) {
127 tst_resm(TFAIL
, "semaphore values"
128 " are not expected");
130 tst_resm(TPASS
, "semaphore values"
135 tst_resm(TPASS
, "call succeeded");
141 * clean up things in case we are looping
144 for (i
=0; i
<NSEMS
; i
++) {
145 if(semctl(sem_id_1
, i
, SETVAL
, get_arr
) == -1) {
146 tst_brkm(TBROK
, cleanup
, "semctl failed");
158 * setup() - performs all the ONE TIME setup for this test.
165 /* capture signals */
166 tst_sig(NOFORK
, DEF_HANDLER
, cleanup
);
168 /* Pause if that option was specified */
172 * Create a temporary directory and cd into it.
173 * This helps to ensure that a unique msgkey is created.
174 * See ../lib/libipc.c for more information.
178 /* get an IPC resource key */
179 semkey
= getipckey();
181 /* create a semaphore set with read and alter permissions */
183 semget(semkey
, PSEMS
, IPC_CREAT
| IPC_EXCL
| SEM_RA
)) == -1) {
184 tst_brkm(TBROK
, cleanup
, "couldn't create semaphore in setup");
187 /* set up some values for the first four primitive semaphores */
188 for (i
=0; i
<NSEMS
; i
++){
190 sops
[i
].sem_op
= i
*i
; /* 0, 1, 4, 9, */
191 sops
[i
].sem_flg
= SEM_UNDO
;
196 * cleanup() - performs all the ONE TIME cleanup for this test at completion
202 /* if it exists, remove the semaphore resouce */
205 /* Remove the temporary directory */
209 * print timing stats if that option was specified.
210 * print errno log if that option was specified.
214 /* exit with return code appropriate for results */