1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci *
5f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
6f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
7f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
8f08c3bdfSopenharmony_ci *   (at your option) any later version.
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
11f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
14f08c3bdfSopenharmony_ci *
15f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
16f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
17f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18f08c3bdfSopenharmony_ci */
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci/*
21f08c3bdfSopenharmony_ci * NAME
22f08c3bdfSopenharmony_ci *	semget03.c
23f08c3bdfSopenharmony_ci *
24f08c3bdfSopenharmony_ci * DESCRIPTION
25f08c3bdfSopenharmony_ci *	semget03 - test for ENOENT error
26f08c3bdfSopenharmony_ci *
27f08c3bdfSopenharmony_ci * ALGORITHM
28f08c3bdfSopenharmony_ci *	loop if that option was specified
29f08c3bdfSopenharmony_ci *	call semget() with a valid key but with no associated semaphore set
30f08c3bdfSopenharmony_ci *	   and IPC_CREAT is not asserted
31f08c3bdfSopenharmony_ci *	check the errno value
32f08c3bdfSopenharmony_ci *	  issue a PASS message if we get ENOENT
33f08c3bdfSopenharmony_ci *	otherwise, the tests fails
34f08c3bdfSopenharmony_ci *	  issue a FAIL message
35f08c3bdfSopenharmony_ci *	call cleanup
36f08c3bdfSopenharmony_ci *
37f08c3bdfSopenharmony_ci * USAGE:  <for command-line>
38f08c3bdfSopenharmony_ci *  semget03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
39f08c3bdfSopenharmony_ci *     where,  -c n : Run n copies concurrently.
40f08c3bdfSopenharmony_ci *             -e   : Turn on errno logging.
41f08c3bdfSopenharmony_ci *	       -i n : Execute test n times.
42f08c3bdfSopenharmony_ci *	       -I x : Execute test for x seconds.
43f08c3bdfSopenharmony_ci *	       -P x : Pause for x seconds between iterations.
44f08c3bdfSopenharmony_ci *	       -t   : Turn on syscall timing.
45f08c3bdfSopenharmony_ci *
46f08c3bdfSopenharmony_ci * HISTORY
47f08c3bdfSopenharmony_ci *	03/2001 - Written by Wayne Boyer
48f08c3bdfSopenharmony_ci *
49f08c3bdfSopenharmony_ci * RESTRICTIONS
50f08c3bdfSopenharmony_ci *	none
51f08c3bdfSopenharmony_ci */
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci#include "ipcsem.h"
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_cichar *TCID = "semget03";
56f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ciint sem_id_1 = -1;
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ciint main(int ac, char **av)
61f08c3bdfSopenharmony_ci{
62f08c3bdfSopenharmony_ci	int lc;
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	setup();		/* global setup */
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci	/* The following loop checks looping state if -i option given */
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
71f08c3bdfSopenharmony_ci		/* reset tst_count in case we are looping */
72f08c3bdfSopenharmony_ci		tst_count = 0;
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci		/* use the TEST macro to make the call */
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_ci		TEST(semget(semkey, PSEMS, SEM_RA));
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci		if (TEST_RETURN != -1) {
79f08c3bdfSopenharmony_ci			sem_id_1 = TEST_RETURN;
80f08c3bdfSopenharmony_ci			tst_resm(TFAIL, "call succeeded when error expected");
81f08c3bdfSopenharmony_ci			continue;
82f08c3bdfSopenharmony_ci		}
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ci		switch (TEST_ERRNO) {
85f08c3bdfSopenharmony_ci		case ENOENT:
86f08c3bdfSopenharmony_ci			tst_resm(TPASS, "expected failure - errno "
87f08c3bdfSopenharmony_ci				 "= %d : %s", TEST_ERRNO, strerror(TEST_ERRNO));
88f08c3bdfSopenharmony_ci			break;
89f08c3bdfSopenharmony_ci		default:
90f08c3bdfSopenharmony_ci			tst_resm(TFAIL, "unexpected error - %d : %s",
91f08c3bdfSopenharmony_ci				 TEST_ERRNO, strerror(TEST_ERRNO));
92f08c3bdfSopenharmony_ci			break;
93f08c3bdfSopenharmony_ci		}
94f08c3bdfSopenharmony_ci	}
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	cleanup();
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci	tst_exit();
99f08c3bdfSopenharmony_ci}
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci/*
102f08c3bdfSopenharmony_ci * setup() - performs all the ONE TIME setup for this test.
103f08c3bdfSopenharmony_ci */
104f08c3bdfSopenharmony_civoid setup(void)
105f08c3bdfSopenharmony_ci{
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_ci	tst_sig(NOFORK, DEF_HANDLER, cleanup);
108f08c3bdfSopenharmony_ci
109f08c3bdfSopenharmony_ci	TEST_PAUSE;
110f08c3bdfSopenharmony_ci
111f08c3bdfSopenharmony_ci	/*
112f08c3bdfSopenharmony_ci	 * Create a temporary directory and cd into it.
113f08c3bdfSopenharmony_ci	 * This helps to ensure that a unique msgkey is created.
114f08c3bdfSopenharmony_ci	 * See libs/libltpipc/libipc.c for more information.
115f08c3bdfSopenharmony_ci	 */
116f08c3bdfSopenharmony_ci	tst_tmpdir();
117f08c3bdfSopenharmony_ci
118f08c3bdfSopenharmony_ci	/* get an IPC resource key */
119f08c3bdfSopenharmony_ci	semkey = getipckey();
120f08c3bdfSopenharmony_ci}
121f08c3bdfSopenharmony_ci
122f08c3bdfSopenharmony_ci/*
123f08c3bdfSopenharmony_ci * cleanup() - performs all the ONE TIME cleanup for this test at completion
124f08c3bdfSopenharmony_ci * 	       or premature exit.
125f08c3bdfSopenharmony_ci */
126f08c3bdfSopenharmony_civoid cleanup(void)
127f08c3bdfSopenharmony_ci{
128f08c3bdfSopenharmony_ci	/* if it exists, remove the semaphore resource */
129f08c3bdfSopenharmony_ci	rm_sema(sem_id_1);
130f08c3bdfSopenharmony_ci
131f08c3bdfSopenharmony_ci	tst_rmdir();
132f08c3bdfSopenharmony_ci
133f08c3bdfSopenharmony_ci}
134