• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *	semget02.c
23  *
24  * DESCRIPTION
25  *	semget02 - test for EACCES and EEXIST errors
26  *
27  * ALGORITHM
28  *	create a semaphore set without read or alter permissions
29  *	loop if that option was specified
30  *	call semget() using two different invalid cases
31  *	check the errno value
32  *	  issue a PASS message if we get EACCES or EEXIST
33  *	otherwise, the tests fails
34  *	  issue a FAIL message
35  *	call cleanup
36  *
37  * USAGE:  <for command-line>
38  *  semget02 [-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 #include <pwd.h>
53 
54 #include "ipcsem.h"
55 
56 char *TCID = "semget02";
57 int TST_TOTAL = 2;
58 
59 char nobody_uid[] = "nobody";
60 struct passwd *ltpuser;
61 
62 int sem_id_1 = -1;
63 
64 struct test_case_t {
65 	int flags;
66 	int error;
67 } TC[] = {
68 	/* EACCES - the semaphore has no read or alter permissions */
69 	{
70 	SEM_RA, EACCES},
71 	    /* EEXIST - the semaphore id exists and semget() was called with  */
72 	    /* IPC_CREAT and IPC_EXCL                                         */
73 	{
74 	IPC_CREAT | IPC_EXCL, EEXIST}
75 };
76 
main(int ac,char ** av)77 int main(int ac, char **av)
78 {
79 	int lc;
80 	int i;
81 
82 	tst_parse_opts(ac, av, NULL, NULL);
83 
84 	setup();		/* global setup */
85 
86 	/* The following loop checks looping state if -i option given */
87 
88 	for (lc = 0; TEST_LOOPING(lc); lc++) {
89 		/* reset tst_count in case we are looping */
90 		tst_count = 0;
91 
92 		for (i = 0; i < TST_TOTAL; i++) {
93 			/* use the TEST macro to make the call */
94 
95 			TEST(semget(semkey, PSEMS, TC[i].flags));
96 
97 			if (TEST_RETURN != -1) {
98 				sem_id_1 = TEST_RETURN;
99 				tst_resm(TFAIL, "call succeeded");
100 				continue;
101 			}
102 
103 			if (TEST_ERRNO == TC[i].error) {
104 				tst_resm(TPASS, "expected failure - errno "
105 					 "= %d : %s", TEST_ERRNO,
106 					 strerror(TEST_ERRNO));
107 			} else {
108 				tst_resm(TFAIL, "unexpected error - %d : %s",
109 					 TEST_ERRNO, strerror(TEST_ERRNO));
110 			}
111 		}
112 	}
113 
114 	cleanup();
115 
116 	tst_exit();
117 }
118 
119 /*
120  * setup() - performs all the ONE TIME setup for this test.
121  */
setup(void)122 void setup(void)
123 {
124 	tst_require_root();
125 
126 	/* Switch to nobody user for correct error code collection */
127 	ltpuser = getpwnam(nobody_uid);
128 	if (seteuid(ltpuser->pw_uid) == -1) {
129 		tst_resm(TINFO, "setreuid failed to "
130 			 "to set the effective uid to %d", ltpuser->pw_uid);
131 		perror("setreuid");
132 	}
133 
134 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
135 
136 	TEST_PAUSE;
137 
138 	/*
139 	 * Create a temporary directory and cd into it.
140 	 * This helps to ensure that a unique msgkey is created.
141 	 * See libs/libltpipc/libipc.c for more information.
142 	 */
143 	tst_tmpdir();
144 
145 	/* get an IPC resource key */
146 	semkey = getipckey();
147 
148 	/* create a semaphore set without read or alter permissions */
149 	if ((sem_id_1 = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL)) == -1) {
150 		tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
151 	}
152 }
153 
154 /*
155  * cleanup() - performs all the ONE TIME cleanup for this test at completion
156  * 	       or premature exit.
157  */
cleanup(void)158 void cleanup(void)
159 {
160 	/* if it exists, remove the semaphore resource */
161 	rm_sema(sem_id_1);
162 
163 	tst_rmdir();
164 
165 }
166