xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setgroups/setgroups04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (C) Bull S.A. 2001
3*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker /*
21*49cdfc7eSAndroid Build Coastguard Worker  * Test Name: setgroups04
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  * Test Description:
24*49cdfc7eSAndroid Build Coastguard Worker  *  Verify that, setgroups() fails with -1 and sets errno to EFAULT if the list has an invalid address.
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  * Expected Result:
27*49cdfc7eSAndroid Build Coastguard Worker  *  setgroups() should fail with return value -1 and set expected errno.
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  * Algorithm:
30*49cdfc7eSAndroid Build Coastguard Worker  *  Setup:
31*49cdfc7eSAndroid Build Coastguard Worker  *   Setup signal handling.
32*49cdfc7eSAndroid Build Coastguard Worker  *   Pause for SIGUSR1 if option specified.
33*49cdfc7eSAndroid Build Coastguard Worker  *
34*49cdfc7eSAndroid Build Coastguard Worker  *  Test:
35*49cdfc7eSAndroid Build Coastguard Worker  *   Loop if the proper options are given.
36*49cdfc7eSAndroid Build Coastguard Worker  *   Execute system call
37*49cdfc7eSAndroid Build Coastguard Worker  *   Check return code, if system call failed (return=-1)
38*49cdfc7eSAndroid Build Coastguard Worker  *   	if errno set == expected errno
39*49cdfc7eSAndroid Build Coastguard Worker  *   		Issue sys call fails with expected return value and errno.
40*49cdfc7eSAndroid Build Coastguard Worker  *   	Otherwise,
41*49cdfc7eSAndroid Build Coastguard Worker  *		Issue sys call fails with unexpected errno.
42*49cdfc7eSAndroid Build Coastguard Worker  *   Otherwise,
43*49cdfc7eSAndroid Build Coastguard Worker  *	Issue sys call returns unexpected value.
44*49cdfc7eSAndroid Build Coastguard Worker  *
45*49cdfc7eSAndroid Build Coastguard Worker  *  Cleanup:
46*49cdfc7eSAndroid Build Coastguard Worker  *   Print errno log and/or timing stats if options given
47*49cdfc7eSAndroid Build Coastguard Worker  *
48*49cdfc7eSAndroid Build Coastguard Worker  * Usage:  <for command-line>
49*49cdfc7eSAndroid Build Coastguard Worker  *  setgroups04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
50*49cdfc7eSAndroid Build Coastguard Worker  *     where,  -c n : Run n copies concurrently.
51*49cdfc7eSAndroid Build Coastguard Worker  *             -f   : Turn off functionality Testing.
52*49cdfc7eSAndroid Build Coastguard Worker  *	       -i n : Execute test n times.
53*49cdfc7eSAndroid Build Coastguard Worker  *	       -I x : Execute test for x seconds.
54*49cdfc7eSAndroid Build Coastguard Worker  *	       -P x : Pause for x seconds between iterations.
55*49cdfc7eSAndroid Build Coastguard Worker  *	       -t   : Turn on syscall timing.
56*49cdfc7eSAndroid Build Coastguard Worker  *
57*49cdfc7eSAndroid Build Coastguard Worker  * HISTORY
58*49cdfc7eSAndroid Build Coastguard Worker  *	05/2002 Ported by Andr� Merlier
59*49cdfc7eSAndroid Build Coastguard Worker  *
60*49cdfc7eSAndroid Build Coastguard Worker  * RESTRICTIONS:
61*49cdfc7eSAndroid Build Coastguard Worker  *  none.
62*49cdfc7eSAndroid Build Coastguard Worker  *
63*49cdfc7eSAndroid Build Coastguard Worker  */
64*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
65*49cdfc7eSAndroid Build Coastguard Worker #include <sys/param.h>
66*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
67*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
68*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
69*49cdfc7eSAndroid Build Coastguard Worker #include <grp.h>
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker /*
74*49cdfc7eSAndroid Build Coastguard Worker  * Don't forget to remove USE_LEGACY_COMPAT_16_H from Makefile after
75*49cdfc7eSAndroid Build Coastguard Worker  * rewriting this test to the new API.
76*49cdfc7eSAndroid Build Coastguard Worker  */
77*49cdfc7eSAndroid Build Coastguard Worker #include "compat_16.h"
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker TCID_DEFINE(setgroups04);
80*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker GID_T groups_list[NGROUPS];
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker void setup();			/* setup function for the test */
85*49cdfc7eSAndroid Build Coastguard Worker void cleanup();			/* cleanup function for the test */
86*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)87*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
88*49cdfc7eSAndroid Build Coastguard Worker {
89*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
90*49cdfc7eSAndroid Build Coastguard Worker 	int gidsetsize;		/* total no. of groups */
91*49cdfc7eSAndroid Build Coastguard Worker 	char *test_desc;	/* test specific error message */
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	/* Perform setup for test */
96*49cdfc7eSAndroid Build Coastguard Worker 	setup();
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 		gidsetsize = NGROUPS;
103*49cdfc7eSAndroid Build Coastguard Worker 		test_desc = "EFAULT";
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 		/*
106*49cdfc7eSAndroid Build Coastguard Worker 		 * Call setgroups() to test condition
107*49cdfc7eSAndroid Build Coastguard Worker 		 * verify that it fails with -1 return value and
108*49cdfc7eSAndroid Build Coastguard Worker 		 * sets appropriate errno.
109*49cdfc7eSAndroid Build Coastguard Worker 		 */
110*49cdfc7eSAndroid Build Coastguard Worker 		TEST(SETGROUPS(cleanup, gidsetsize, sbrk(0)));
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN != -1) {
113*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "setgroups() returned %ld, "
114*49cdfc7eSAndroid Build Coastguard Worker 				 "expected -1, errno=%d", TEST_RETURN,
115*49cdfc7eSAndroid Build Coastguard Worker 				 EFAULT);
116*49cdfc7eSAndroid Build Coastguard Worker 		} else {
117*49cdfc7eSAndroid Build Coastguard Worker 
118*49cdfc7eSAndroid Build Coastguard Worker 			if (TEST_ERRNO == EFAULT) {
119*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TPASS,
120*49cdfc7eSAndroid Build Coastguard Worker 					 "setgroups() fails with expected "
121*49cdfc7eSAndroid Build Coastguard Worker 					 "error EFAULT errno:%d", TEST_ERRNO);
122*49cdfc7eSAndroid Build Coastguard Worker 			} else {
123*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "setgroups() fails, %s, "
124*49cdfc7eSAndroid Build Coastguard Worker 					 "errno=%d, expected errno=%d",
125*49cdfc7eSAndroid Build Coastguard Worker 					 test_desc, TEST_ERRNO, EFAULT);
126*49cdfc7eSAndroid Build Coastguard Worker 			}
127*49cdfc7eSAndroid Build Coastguard Worker 		}
128*49cdfc7eSAndroid Build Coastguard Worker 
129*49cdfc7eSAndroid Build Coastguard Worker 	}
130*49cdfc7eSAndroid Build Coastguard Worker 
131*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
132*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
133*49cdfc7eSAndroid Build Coastguard Worker 
134*49cdfc7eSAndroid Build Coastguard Worker }
135*49cdfc7eSAndroid Build Coastguard Worker 
136*49cdfc7eSAndroid Build Coastguard Worker /*
137*49cdfc7eSAndroid Build Coastguard Worker  * setup()
138*49cdfc7eSAndroid Build Coastguard Worker  */
setup(void)139*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
140*49cdfc7eSAndroid Build Coastguard Worker {
141*49cdfc7eSAndroid Build Coastguard Worker 	tst_require_root();
142*49cdfc7eSAndroid Build Coastguard Worker 
143*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
144*49cdfc7eSAndroid Build Coastguard Worker 
145*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
146*49cdfc7eSAndroid Build Coastguard Worker 
147*49cdfc7eSAndroid Build Coastguard Worker }
148*49cdfc7eSAndroid Build Coastguard Worker 
149*49cdfc7eSAndroid Build Coastguard Worker /*
150*49cdfc7eSAndroid Build Coastguard Worker  * cleanup()
151*49cdfc7eSAndroid Build Coastguard Worker  */
cleanup(void)152*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
153*49cdfc7eSAndroid Build Coastguard Worker {
154*49cdfc7eSAndroid Build Coastguard Worker 
155*49cdfc7eSAndroid Build Coastguard Worker }
156