semctl02.c revision 865695bbc89088b9526ea9045410e5afb70a985c
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * NAME
22 *	semctl02.c
23 *
24 * DESCRIPTION
25 *	semctl02 - test for EACCES error
26 *
27 * ALGORITHM
28 *	create a semaphore set without read/alter permissions
29 *	loop if that option was specified
30 *	call semctl() attempting an IPC_STAT command
31 *	check the errno value
32 *	  issue a PASS message if we get EACCES
33 *	otherwise, the tests fails
34 *	  issue a FAIL message
35 *	call cleanup
36 *
37 * USAGE:  <for command-line>
38 *  semctl02 [-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 = "semctl02";
56int TST_TOTAL = 1;
57extern int Tst_count;
58
59int exp_enos[] = {EACCES, 0};	/* 0 terminated list of expected errnos */
60
61int sem_id_1 = -1;
62
63main(int ac, char **av)
64{
65	int lc;				/* loop counter */
66	char *msg;			/* message returned from parse_opts */
67
68	struct semid_ds sem_ds;
69	struct ipc_perm *ipcp = &sem_ds.sem_perm;
70	union semun un_arg;
71
72	/* parse standard options */
73	if ((msg = parse_opts(ac, av, (option_t *)NULL, NULL)) != (char *)NULL){
74		tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg);
75	}
76
77	setup();			/* global setup */
78
79	/* The following loop checks looping state if -i option given */
80
81	for (lc = 0; TEST_LOOPING(lc); lc++) {
82		/* reset Tst_count in case we are looping */
83		Tst_count = 0;
84
85		un_arg.buf = &sem_ds;
86
87		/*
88		 * use the TEST macro to make the call
89		 */
90
91		TEST(semctl(sem_id_1, 0, IPC_STAT, un_arg));
92
93		if (TEST_RETURN != -1) {
94			tst_resm(TFAIL, "call succeeded when error expected");
95			continue;
96		}
97
98		TEST_ERROR_LOG(TEST_ERRNO);
99
100		switch(TEST_ERRNO) {
101		case EACCES:
102			tst_resm(TPASS, "expected failure - errno = %d : %s",
103				 TEST_ERRNO, strerror(TEST_ERRNO));
104			break;
105		default:
106			tst_resm(TFAIL, "unexpected error - %d : %s",
107				 TEST_ERRNO, strerror(TEST_ERRNO));
108			break;
109		}
110	}
111
112	cleanup();
113
114	/*NOTREACHED*/
115}
116
117/*
118 * setup() - performs all the ONE TIME setup for this test.
119 */
120void
121setup(void)
122{
123	/* capture signals */
124	tst_sig(NOFORK, DEF_HANDLER, cleanup);
125
126	/* Set up the expected error numbers for -e option */
127	TEST_EXP_ENOS(exp_enos);
128
129	/* Pause if that option was specified */
130	TEST_PAUSE;
131
132	/*
133	 * Create a temporary directory and cd into it.
134	 * This helps to ensure that a unique msgkey is created.
135	 * See ../lib/libipc.c for more information.
136	 */
137	tst_tmpdir();
138
139	/* get an IPC resource key */
140	semkey = getipckey();
141
142	/* create a semaphore set without read or alter permissions */
143	if ((sem_id_1 = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL)) == -1) {
144		tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
145	}
146}
147
148/*
149 * cleanup() - performs all the ONE TIME cleanup for this test at completion
150 * 	       or premature exit.
151 */
152void
153cleanup(void)
154{
155	/* if it exists, remove the semaphore resouce */
156	rm_sema(sem_id_1);
157
158	/* Remove the temporary directory */
159	tst_rmdir();
160
161	/*
162	 * print timing stats if that option was specified.
163	 * print errno log if that option was specified.
164	 */
165	TEST_CLEANUP;
166
167	/* exit with return code appropriate for results */
168	tst_exit();
169}
170
171