semop04.c revision d59a659cd639ca2780b00049d102acd2a783d585
1/*
2 *
3 *   Copyright (c) International Business Machines  Corp., 2001
4 *
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.
9 *
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.
14 *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20/*
21 * NAME
22 *	semop04.c
23 *
24 * DESCRIPTION
25 *	semop04 - test for EAGAIN error
26 *
27 * ALGORITHM
28 *	create a semaphore set with read and alter permissions
29 *	loop if that option was specified
30 *	call semop() with two different invalid cases
31 *	check the errno value
32 *	  issue a PASS message if we get EAGAIN
33 *	otherwise, the tests fails
34 *	  issue a FAIL message
35 *	call cleanup
36 *
37 * USAGE:  <for command-line>
38 *  semop04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
39 *     where,  -c n : Run n copies concurrently.
40 *             -e   : Turn on errno logging.
41 *	       -i n : Execute test n times.
42 *	       -I x : Execute test for x seconds.
43 *	       -P x : Pause for x seconds between iterations.
44 *	       -t   : Turn on syscall timing.
45 *
46 * HISTORY
47 *	03/2001 - Written by Wayne Boyer
48 *
49 * RESTRICTIONS
50 *	none
51 */
52
53#include "ipcsem.h"
54
55char *TCID = "semop04";
56int TST_TOTAL = 2;
57
58int exp_enos[] = { EAGAIN, 0 };	/* 0 terminated list of expected errnos */
59
60int sem_id_1 = -1;
61
62struct sembuf s_buf;
63
64struct test_case_t {
65	union semun get_arr;
66	short op;
67	short flg;
68	short num;
69	int error;
70} TC[] = {
71	/* EAGAIN sem_op = 0 */
72	{ {
73	1}, 0, IPC_NOWAIT, 2, EAGAIN},
74	    /* EAGAIN sem_op = -1 */
75	{ {
76	0}, -1, IPC_NOWAIT, 2, EAGAIN}
77};
78
79int main(int ac, char **av)
80{
81	int lc;
82	char *msg;
83	int val = 1;		/* value for SETVAL */
84
85	int i;
86
87	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
88		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
89	}
90
91	setup();		/* global setup */
92
93	/* The following loop checks looping state if -i option given */
94
95	for (lc = 0; TEST_LOOPING(lc); lc++) {
96		/* reset tst_count in case we are looping */
97		tst_count = 0;
98
99		for (i = 0; i < TST_TOTAL; i++) {
100
101			/* initialize the s_buf buffer */
102			s_buf.sem_op = TC[i].op;
103			s_buf.sem_flg = TC[i].flg;
104			s_buf.sem_num = TC[i].num;
105
106			/* initialize all the primitive semaphores */
107			TC[i].get_arr.val = val--;
108			if (semctl(sem_id_1, TC[i].num, SETVAL, TC[i].get_arr)
109			    == -1) {
110				tst_brkm(TBROK, cleanup, "semctl() failed");
111			}
112
113			/*
114			 * make the call with the TEST macro
115			 */
116
117			TEST(semop(sem_id_1, &s_buf, 1));
118
119			if (TEST_RETURN != -1) {
120				tst_resm(TFAIL, "call succeeded unexpectedly");
121				continue;
122			}
123
124			TEST_ERROR_LOG(TEST_ERRNO);
125
126			if (TEST_ERRNO == TC[i].error) {
127				tst_resm(TPASS,
128					 "expected failure - errno = %d"
129					 " : %s", TEST_ERRNO,
130					 strerror(TEST_ERRNO));
131			} else {
132				tst_resm(TFAIL, "unexpected error - "
133					 "%d : %s", TEST_ERRNO,
134					 strerror(TEST_ERRNO));
135			}
136		}
137	}
138
139	cleanup();
140
141	tst_exit();
142}
143
144/*
145 * setup() - performs all the ONE TIME setup for this test.
146 */
147void setup(void)
148{
149
150	tst_sig(NOFORK, DEF_HANDLER, cleanup);
151
152	/* Set up the expected error numbers for -e option */
153	TEST_EXP_ENOS(exp_enos);
154
155	TEST_PAUSE;
156
157	/*
158	 * Create a temporary directory and cd into it.
159	 * This helps to ensure that a unique msgkey is created.
160	 * See ../lib/libipc.c for more information.
161	 */
162	tst_tmpdir();
163
164	/* get an IPC resource key */
165	semkey = getipckey();
166
167	/* create a semaphore set with read and alter permissions */
168	/* and PSEMS "primitive" semaphores                       */
169	if ((sem_id_1 =
170	     semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL | SEM_RA)) == -1) {
171		tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
172	}
173}
174
175/*
176 * cleanup() - performs all the ONE TIME cleanup for this test at completion
177 * 	       or premature exit.
178 */
179void cleanup(void)
180{
181	/* if it exists, remove the semaphore resource */
182	rm_sema(sem_id_1);
183
184	tst_rmdir();
185
186	/*
187	 * print timing stats if that option was specified.
188	 * print errno log if that option was specified.
189	 */
190	TEST_CLEANUP;
191
192}
193