• 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  *	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 "ipcsem.h"
54 #include <pwd.h>
55 
56 char *TCID = "semctl02";
57 int TST_TOTAL = 1;
58 
59 char nobody_uid[] = "nobody";
60 struct passwd *ltpuser;
61 
62 int sem_id_1 = -1;
63 
main(int ac,char ** av)64 int main(int ac, char **av)
65 {
66 	int lc;
67 
68 	struct semid_ds sem_ds;
69 	union semun un_arg;
70 
71 	tst_parse_opts(ac, av, NULL, NULL);
72 
73 	setup();		/* global setup */
74 
75 	/* The following loop checks looping state if -i option given */
76 
77 	for (lc = 0; TEST_LOOPING(lc); lc++) {
78 		/* reset tst_count in case we are looping */
79 		tst_count = 0;
80 
81 		un_arg.buf = &sem_ds;
82 
83 		/*
84 		 * use the TEST macro to make the call
85 		 */
86 
87 		TEST(semctl(sem_id_1, 0, IPC_STAT, un_arg));
88 
89 		if (TEST_RETURN != -1) {
90 			tst_resm(TFAIL, "call succeeded when error expected");
91 			continue;
92 		}
93 
94 		switch (TEST_ERRNO) {
95 		case EACCES:
96 			tst_resm(TPASS, "expected failure - errno = %d : %s",
97 				 TEST_ERRNO, strerror(TEST_ERRNO));
98 			break;
99 		default:
100 			tst_resm(TFAIL, "unexpected error - %d : %s",
101 				 TEST_ERRNO, strerror(TEST_ERRNO));
102 			break;
103 		}
104 	}
105 
106 	cleanup();
107 
108 	tst_exit();
109 }
110 
111 /*
112  * setup() - performs all the ONE TIME setup for this test.
113  */
setup(void)114 void setup(void)
115 {
116 	tst_require_root();
117 
118 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
119 
120 	TEST_PAUSE;
121 
122 	/* Switch to nobody user for correct error code collection */
123 	ltpuser = getpwnam(nobody_uid);
124 	if (setuid(ltpuser->pw_uid) == -1) {
125 		tst_resm(TINFO, "setuid failed to "
126 			 "to set the effective uid to %d", ltpuser->pw_uid);
127 		perror("setuid");
128 	}
129 
130 	/*
131 	 * Create a temporary directory and cd into it.
132 	 * This helps to ensure that a unique msgkey is created.
133 	 * See ../lib/libipc.c for more information.
134 	 */
135 	tst_tmpdir();
136 
137 	/* get an IPC resource key */
138 	semkey = getipckey();
139 
140 	/* create a semaphore set without read or alter permissions */
141 	if ((sem_id_1 = semget(semkey, PSEMS, IPC_CREAT | IPC_EXCL)) == -1) {
142 		tst_brkm(TBROK, cleanup, "couldn't create semaphore in setup");
143 	}
144 }
145 
146 /*
147  * cleanup() - performs all the ONE TIME cleanup for this test at completion
148  * 	       or premature exit.
149  */
cleanup(void)150 void cleanup(void)
151 {
152 	/* if it exists, remove the semaphore resouce */
153 	rm_sema(sem_id_1);
154 
155 	tst_rmdir();
156 
157 }
158