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 * semop05 - test for EINTR and EIDRM errors
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
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
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.
53 * 03/2001 - Written by Wayne Boyer
60 #include <sys/types.h>
65 char *TCID
= "semop05";
69 int exp_enos
[] = {EINTR
, EIDRM
, 0}; /* 0 terminated list of expected errnos */
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
}
96 void do_child_uclinux();
100 int main(int ac
, char **av
)
102 int lc
; /* loop counter */
103 char *msg
; /* message returned from parse_opts */
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
);
114 maybe_run_child(&do_child_uclinux
, "dd", &i_uclinux
, &sem_id_1
);
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 */
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
)
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 */
144 if (self_exec(av
[0], "dd", i
, sem_id_1
) < 0) {
145 tst_brkm(TBROK
, cleanup
,
146 "could not self_exec");
151 } else { /* parent */
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
160 if (TC
[i
].error
== EIDRM
) {
161 /* remove the semaphore resource */
164 if (kill(pid
, SIGHUP
) == -1) {
165 tst_brkm(TBROK
, cleanup
,
170 /* let the child carry on */
175 * recreate the semaphore resource if needed
177 if (TC
[i
].error
== EINTR
) {
181 if ((sem_id_1
= semget(semkey
, PSEMS
, IPC_CREAT
|
182 IPC_EXCL
| SEM_RA
)) == -1) {
183 tst_brkm(TBROK
, cleanup
, "couldn't recreate "
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");
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
));
218 tst_resm(TFAIL
, "unexpected error - "
219 "%d : %s", TEST_ERRNO
, strerror(TEST_ERRNO
));
227 * do_child_uclinux() - capture signals, re-initialize s_buf then call do_child
228 * with the appropriate argument
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
;
248 * sighandler() - handle signals
253 /* we don't need to do anything here */
257 * setup() - performs all the ONE TIME setup for this test.
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 */
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.
278 /* get an IPC resource key */
279 semkey
= getipckey();
281 /* create a semaphore set with read and alter permissions */
282 /* and PSEMS "primitive" semaphores */
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
296 /* if it exists, remove the semaphore resource */
299 /* Remove the temporary directory */
303 * print timing stats if that option was specified.
304 * print errno log if that option was specified.
308 /* exit with return code appropriate for results */