xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getgroups/getgroups01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or modify it
5*49cdfc7eSAndroid Build Coastguard Worker  * under the terms of version 2 of the GNU General Public License as
6*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation.
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful, but
9*49cdfc7eSAndroid Build Coastguard Worker  * WITHOUT ANY WARRANTY; without even the implied warranty of
10*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Further, this software is distributed without any warranty that it is
13*49cdfc7eSAndroid Build Coastguard Worker  * free of the rightful claim of any third person regarding infringement
14*49cdfc7eSAndroid Build Coastguard Worker  * or the like.  Any license provided herein, whether implied or
15*49cdfc7eSAndroid Build Coastguard Worker  * otherwise, applies only to this software file.  Patent licenses, if
16*49cdfc7eSAndroid Build Coastguard Worker  * any, provided herein do not apply to combinations of this program with
17*49cdfc7eSAndroid Build Coastguard Worker  * other software, or any other product whatsoever.
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License along
20*49cdfc7eSAndroid Build Coastguard Worker  * with this program; if not, write the Free Software Foundation, Inc.,
21*49cdfc7eSAndroid Build Coastguard Worker  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24*49cdfc7eSAndroid Build Coastguard Worker  * Mountain View, CA  94043, or:
25*49cdfc7eSAndroid Build Coastguard Worker  *
26*49cdfc7eSAndroid Build Coastguard Worker  * http://www.sgi.com
27*49cdfc7eSAndroid Build Coastguard Worker  *
28*49cdfc7eSAndroid Build Coastguard Worker  * For further information regarding this notice, see:
29*49cdfc7eSAndroid Build Coastguard Worker  *
30*49cdfc7eSAndroid Build Coastguard Worker  * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31*49cdfc7eSAndroid Build Coastguard Worker  */
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker /*
34*49cdfc7eSAndroid Build Coastguard Worker   AUTHOR: Barrie Kletscher
35*49cdfc7eSAndroid Build Coastguard Worker   Rewrote :  11-92 by Richard Logan
36*49cdfc7eSAndroid Build Coastguard Worker   CO-PILOT: Dave Baumgartner
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker   TEST ITEMS:
39*49cdfc7eSAndroid Build Coastguard Worker    1. Check to see if getgroups(-1, gidset) fails and sets errno to EINVAL
40*49cdfc7eSAndroid Build Coastguard Worker    2. Check to see if getgroups(0, gidset) does not return -1 and gidset is
41*49cdfc7eSAndroid Build Coastguard Worker       not modified.
42*49cdfc7eSAndroid Build Coastguard Worker    3. Check to see if getgroups(x, gigset) fails and sets errno to EINVAL,
43*49cdfc7eSAndroid Build Coastguard Worker       where x is one less then what is returned by getgroups(0, gidset).
44*49cdfc7eSAndroid Build Coastguard Worker    4. Check to see if getgroups() succeeds and gidset contains
45*49cdfc7eSAndroid Build Coastguard Worker       group id returned from getgid().
46*49cdfc7eSAndroid Build Coastguard Worker */
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
49*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
50*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
51*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
52*49cdfc7eSAndroid Build Coastguard Worker #include <grp.h>
53*49cdfc7eSAndroid Build Coastguard Worker #include <sys/param.h>
54*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker /*
59*49cdfc7eSAndroid Build Coastguard Worker  * Don't forget to remove USE_LEGACY_COMPAT_16_H from Makefile after
60*49cdfc7eSAndroid Build Coastguard Worker  * rewriting all tests to the new API.
61*49cdfc7eSAndroid Build Coastguard Worker  */
62*49cdfc7eSAndroid Build Coastguard Worker #include "compat_16.h"
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
65*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker TCID_DEFINE(getgroups01);
68*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 4;
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker static GID_T gidset[NGROUPS];
71*49cdfc7eSAndroid Build Coastguard Worker static GID_T cmpset[NGROUPS];
72*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)73*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
74*49cdfc7eSAndroid Build Coastguard Worker {
75*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
76*49cdfc7eSAndroid Build Coastguard Worker 	GID_T group;
77*49cdfc7eSAndroid Build Coastguard Worker 	int i;
78*49cdfc7eSAndroid Build Coastguard Worker 	int entries;
79*49cdfc7eSAndroid Build Coastguard Worker 
80*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	setup();
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 		TEST(GETGROUPS(cleanup, -1, gidset));
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN == 0) {
91*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "getgroups succeeded unexpectedly");
92*49cdfc7eSAndroid Build Coastguard Worker 		} else {
93*49cdfc7eSAndroid Build Coastguard Worker 			if (errno == EINVAL)
94*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TPASS,
95*49cdfc7eSAndroid Build Coastguard Worker 					 "getgroups failed as expected with EINVAL");
96*49cdfc7eSAndroid Build Coastguard Worker 			else
97*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL | TTERRNO,
98*49cdfc7eSAndroid Build Coastguard Worker 					 "getgroups didn't fail as expected with EINVAL");
99*49cdfc7eSAndroid Build Coastguard Worker 		}
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 		/*
102*49cdfc7eSAndroid Build Coastguard Worker 		 * Check that if ngrps is zero that the number of groups is
103*49cdfc7eSAndroid Build Coastguard Worker 		 * return and the gidset array is not modified.
104*49cdfc7eSAndroid Build Coastguard Worker 		 * This is a POSIX special case.
105*49cdfc7eSAndroid Build Coastguard Worker 		 */
106*49cdfc7eSAndroid Build Coastguard Worker 		memset(gidset, 052, NGROUPS * sizeof(GID_T));
107*49cdfc7eSAndroid Build Coastguard Worker 		memset(cmpset, 052, NGROUPS * sizeof(GID_T));
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 		TEST(GETGROUPS(cleanup, 0, gidset));
110*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN == -1) {
111*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL | TTERRNO, "getgroups failed unexpectedly");
112*49cdfc7eSAndroid Build Coastguard Worker 		} else {
113*49cdfc7eSAndroid Build Coastguard Worker 			if (memcmp(cmpset, gidset, NGROUPS * sizeof(GID_T)) != 0)
114*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL,
115*49cdfc7eSAndroid Build Coastguard Worker 					 "getgroups modified the gidset array");
116*49cdfc7eSAndroid Build Coastguard Worker 			else
117*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TPASS,
118*49cdfc7eSAndroid Build Coastguard Worker 					 "getgroups did not modify the gidset "
119*49cdfc7eSAndroid Build Coastguard Worker 					 "array");
120*49cdfc7eSAndroid Build Coastguard Worker 		}
121*49cdfc7eSAndroid Build Coastguard Worker 
122*49cdfc7eSAndroid Build Coastguard Worker 		/*
123*49cdfc7eSAndroid Build Coastguard Worker 		 * Check to see that is -1 is returned and errno is set to
124*49cdfc7eSAndroid Build Coastguard Worker 		 * EINVAL when ngroups is not big enough to hold all groups.
125*49cdfc7eSAndroid Build Coastguard Worker 		 */
126*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN <= 1) {
127*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TCONF,
128*49cdfc7eSAndroid Build Coastguard Worker 				 "getgroups returned %ld; unable to test that using ngrps >=1 but less than number of grps",
129*49cdfc7eSAndroid Build Coastguard Worker 				 TEST_RETURN);
130*49cdfc7eSAndroid Build Coastguard Worker 		} else {
131*49cdfc7eSAndroid Build Coastguard Worker 			TEST(GETGROUPS(cleanup, TEST_RETURN - 1, gidset));
132*49cdfc7eSAndroid Build Coastguard Worker 			if (TEST_RETURN == -1) {
133*49cdfc7eSAndroid Build Coastguard Worker 				if (errno == EINVAL)
134*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TPASS,
135*49cdfc7eSAndroid Build Coastguard Worker 						 "getgroups failed as "
136*49cdfc7eSAndroid Build Coastguard Worker 						 "expected with EINVAL");
137*49cdfc7eSAndroid Build Coastguard Worker 				else
138*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TFAIL | TERRNO,
139*49cdfc7eSAndroid Build Coastguard Worker 						 "getgroups didn't fail "
140*49cdfc7eSAndroid Build Coastguard Worker 						 "with EINVAL");
141*49cdfc7eSAndroid Build Coastguard Worker 			} else {
142*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL,
143*49cdfc7eSAndroid Build Coastguard Worker 					 "getgroups succeeded unexpectedly with %ld",
144*49cdfc7eSAndroid Build Coastguard Worker 					 TEST_RETURN);
145*49cdfc7eSAndroid Build Coastguard Worker 			}
146*49cdfc7eSAndroid Build Coastguard Worker 		}
147*49cdfc7eSAndroid Build Coastguard Worker 
148*49cdfc7eSAndroid Build Coastguard Worker 		TEST(GETGROUPS(cleanup, NGROUPS, gidset));
149*49cdfc7eSAndroid Build Coastguard Worker 		if ((entries = TEST_RETURN) == -1) {
150*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL | TTERRNO,
151*49cdfc7eSAndroid Build Coastguard Worker 				 "getgroups failed unexpectedly");
152*49cdfc7eSAndroid Build Coastguard Worker 		} else {
153*49cdfc7eSAndroid Build Coastguard Worker 
154*49cdfc7eSAndroid Build Coastguard Worker 			group = getgid();
155*49cdfc7eSAndroid Build Coastguard Worker 
156*49cdfc7eSAndroid Build Coastguard Worker 			for (i = 0; i < entries; i++) {
157*49cdfc7eSAndroid Build Coastguard Worker 				if (gidset[i] == group) {
158*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TPASS,
159*49cdfc7eSAndroid Build Coastguard Worker 						 "getgroups(NGROUPS,gidset) "
160*49cdfc7eSAndroid Build Coastguard Worker 						 "returned %d contains gid %d "
161*49cdfc7eSAndroid Build Coastguard Worker 						 "(from getgid)",
162*49cdfc7eSAndroid Build Coastguard Worker 						 entries, group);
163*49cdfc7eSAndroid Build Coastguard Worker 					break;
164*49cdfc7eSAndroid Build Coastguard Worker 				}
165*49cdfc7eSAndroid Build Coastguard Worker 			}
166*49cdfc7eSAndroid Build Coastguard Worker 
167*49cdfc7eSAndroid Build Coastguard Worker 			if (i == entries) {
168*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL,
169*49cdfc7eSAndroid Build Coastguard Worker 					 "getgroups(NGROUPS,gidset) ret %d, does "
170*49cdfc7eSAndroid Build Coastguard Worker 					 "not contain gid %d (from getgid)",
171*49cdfc7eSAndroid Build Coastguard Worker 					 entries, group);
172*49cdfc7eSAndroid Build Coastguard Worker 			}
173*49cdfc7eSAndroid Build Coastguard Worker 		}
174*49cdfc7eSAndroid Build Coastguard Worker 
175*49cdfc7eSAndroid Build Coastguard Worker 	}
176*49cdfc7eSAndroid Build Coastguard Worker 
177*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
178*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
179*49cdfc7eSAndroid Build Coastguard Worker }
180*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)181*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
182*49cdfc7eSAndroid Build Coastguard Worker {
183*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(FORK, DEF_HANDLER, cleanup);
184*49cdfc7eSAndroid Build Coastguard Worker 
185*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
186*49cdfc7eSAndroid Build Coastguard Worker 
187*49cdfc7eSAndroid Build Coastguard Worker 	gid_t init_gidset[3] = {0, 1, 2};
188*49cdfc7eSAndroid Build Coastguard Worker 	setgroups(3, init_gidset);
189*49cdfc7eSAndroid Build Coastguard Worker }
190*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)191*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
192*49cdfc7eSAndroid Build Coastguard Worker {
193*49cdfc7eSAndroid Build Coastguard Worker }
194