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 *	semget03.c
23 *
24 * DESCRIPTION
25 *	semget03 - test for ENOENT error
26 *
27 * ALGORITHM
28 *	loop if that option was specified
29 *	call semget() with a valid key but with no associated semaphore set
30 *	   and IPC_CREAT is not asserted
31 *	check the errno value
32 *	  issue a PASS message if we get ENOENT
33 *	otherwise, the tests fails
34 *	  issue a FAIL message
35 *	call cleanup
36 *
37 * USAGE:  <for command-line>
38 *  semget03 [-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 "../lib/ipcsem.h"
54
55char *TCID = "semget03";
56int TST_TOTAL = 1;
57
58int sem_id_1 = -1;
59
60int main(int ac, char **av)
61{
62	int lc;
63
64	tst_parse_opts(ac, av, NULL, NULL);
65
66	setup();		/* global setup */
67
68	/* The following loop checks looping state if -i option given */
69
70	for (lc = 0; TEST_LOOPING(lc); lc++) {
71		/* reset tst_count in case we are looping */
72		tst_count = 0;
73
74		/* use the TEST macro to make the call */
75
76		TEST(semget(semkey, PSEMS, SEM_RA));
77
78		if (TEST_RETURN != -1) {
79			sem_id_1 = TEST_RETURN;
80			tst_resm(TFAIL, "call succeeded when error expected");
81			continue;
82		}
83
84		switch (TEST_ERRNO) {
85		case ENOENT:
86			tst_resm(TPASS, "expected failure - errno "
87				 "= %d : %s", TEST_ERRNO, strerror(TEST_ERRNO));
88			break;
89		default:
90			tst_resm(TFAIL, "unexpected error - %d : %s",
91				 TEST_ERRNO, strerror(TEST_ERRNO));
92			break;
93		}
94	}
95
96	cleanup();
97
98	tst_exit();
99}
100
101/*
102 * setup() - performs all the ONE TIME setup for this test.
103 */
104void setup(void)
105{
106
107	tst_sig(NOFORK, DEF_HANDLER, cleanup);
108
109	TEST_PAUSE;
110
111	/*
112	 * Create a temporary directory and cd into it.
113	 * This helps to ensure that a unique msgkey is created.
114	 * See ../lib/libipc.c for more information.
115	 */
116	tst_tmpdir();
117
118	/* get an IPC resource key */
119	semkey = getipckey();
120}
121
122/*
123 * cleanup() - performs all the ONE TIME cleanup for this test at completion
124 * 	       or premature exit.
125 */
126void cleanup(void)
127{
128	/* if it exists, remove the semaphore resource */
129	rm_sema(sem_id_1);
130
131	tst_rmdir();
132
133}
134