xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getresgid/getresgid03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *
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: getresgid03
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  * Test Description:
24*49cdfc7eSAndroid Build Coastguard Worker  *  Verify that getresgid() will be successful to get the real, effective
25*49cdfc7eSAndroid Build Coastguard Worker  *  and saved user ids after calling process invokes setresgid() to change
26*49cdfc7eSAndroid Build Coastguard Worker  *  the effective gid to that of specified user.
27*49cdfc7eSAndroid Build Coastguard Worker  *
28*49cdfc7eSAndroid Build Coastguard Worker  * Expected Result:
29*49cdfc7eSAndroid Build Coastguard Worker  *  getresgid() should return with 0 value and the effective user id
30*49cdfc7eSAndroid Build Coastguard Worker  *  should match the egid of specified user, real/saved user ids should
31*49cdfc7eSAndroid Build Coastguard Worker  *  remain unchanged.
32*49cdfc7eSAndroid Build Coastguard Worker  *
33*49cdfc7eSAndroid Build Coastguard Worker  * Algorithm:
34*49cdfc7eSAndroid Build Coastguard Worker  *  Setup:
35*49cdfc7eSAndroid Build Coastguard Worker  *   Setup signal handling.
36*49cdfc7eSAndroid Build Coastguard Worker  *   Pause for SIGUSR1 if option specified.
37*49cdfc7eSAndroid Build Coastguard Worker  *
38*49cdfc7eSAndroid Build Coastguard Worker  *  Test:
39*49cdfc7eSAndroid Build Coastguard Worker  *   Loop if the proper options are given.
40*49cdfc7eSAndroid Build Coastguard Worker  *   Execute system call
41*49cdfc7eSAndroid Build Coastguard Worker  *   Check return code, if system call failed (return=-1)
42*49cdfc7eSAndroid Build Coastguard Worker  *	Log the errno and Issue a FAIL message.
43*49cdfc7eSAndroid Build Coastguard Worker  *   Otherwise,
44*49cdfc7eSAndroid Build Coastguard Worker  *	Verify the Functionality of system call
45*49cdfc7eSAndroid Build Coastguard Worker  *      if successful,
46*49cdfc7eSAndroid Build Coastguard Worker  *		Issue Functionality-Pass message.
47*49cdfc7eSAndroid Build Coastguard Worker  *      Otherwise,
48*49cdfc7eSAndroid Build Coastguard Worker  *		Issue Functionality-Fail message.
49*49cdfc7eSAndroid Build Coastguard Worker  *  Cleanup:
50*49cdfc7eSAndroid Build Coastguard Worker  *   Print errno log and/or timing stats if options given
51*49cdfc7eSAndroid Build Coastguard Worker  *
52*49cdfc7eSAndroid Build Coastguard Worker  * Usage:  <for command-line>
53*49cdfc7eSAndroid Build Coastguard Worker  *  getresgid03 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
54*49cdfc7eSAndroid Build Coastguard Worker  *     where,  -c n : Run n copies concurrently.
55*49cdfc7eSAndroid Build Coastguard Worker  *             -f   : Turn off functionality Testing.
56*49cdfc7eSAndroid Build Coastguard Worker  *	       -i n : Execute test n times.
57*49cdfc7eSAndroid Build Coastguard Worker  *	       -I x : Execute test for x seconds.
58*49cdfc7eSAndroid Build Coastguard Worker  *	       -P x : Pause for x seconds between iterations.
59*49cdfc7eSAndroid Build Coastguard Worker  *	       -t   : Turn on syscall timing.
60*49cdfc7eSAndroid Build Coastguard Worker  *
61*49cdfc7eSAndroid Build Coastguard Worker  * HISTORY
62*49cdfc7eSAndroid Build Coastguard Worker  *	07/2001 Ported by Wayne Boyer
63*49cdfc7eSAndroid Build Coastguard Worker  *
64*49cdfc7eSAndroid Build Coastguard Worker  * RESTRICTIONS:
65*49cdfc7eSAndroid Build Coastguard Worker  *  This test should be run by 'super-user' (root) only.
66*49cdfc7eSAndroid Build Coastguard Worker  *
67*49cdfc7eSAndroid Build Coastguard Worker  */
68*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE 1
69*49cdfc7eSAndroid Build Coastguard Worker 
70*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
71*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
72*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
73*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
74*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
75*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
76*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
77*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker /*
82*49cdfc7eSAndroid Build Coastguard Worker  * Don't forget to remove USE_LEGACY_COMPAT_16_H from Makefile after
83*49cdfc7eSAndroid Build Coastguard Worker  * rewriting all tests to the new API.
84*49cdfc7eSAndroid Build Coastguard Worker  */
85*49cdfc7eSAndroid Build Coastguard Worker #include "compat_16.h"
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "getresgid03";
88*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
89*49cdfc7eSAndroid Build Coastguard Worker GID_T pr_gid, pe_gid, ps_gid;	/* calling process real/effective/saved gid */
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker void setup();			/* Main setup function of test */
92*49cdfc7eSAndroid Build Coastguard Worker void cleanup();			/* cleanup function for the test */
93*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)94*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
95*49cdfc7eSAndroid Build Coastguard Worker {
96*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
97*49cdfc7eSAndroid Build Coastguard Worker 	GID_T real_gid,		/* real/eff./saved user id from getresgid() */
98*49cdfc7eSAndroid Build Coastguard Worker 	 eff_gid, sav_gid;
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 	setup();
103*49cdfc7eSAndroid Build Coastguard Worker 
104*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
107*49cdfc7eSAndroid Build Coastguard Worker 
108*49cdfc7eSAndroid Build Coastguard Worker 		/*
109*49cdfc7eSAndroid Build Coastguard Worker 		 * Call getresgid() to get the real/effective/saved
110*49cdfc7eSAndroid Build Coastguard Worker 		 * user id's of the calling process after
111*49cdfc7eSAndroid Build Coastguard Worker 		 * setregid() in setup.
112*49cdfc7eSAndroid Build Coastguard Worker 		 */
113*49cdfc7eSAndroid Build Coastguard Worker 		TEST(GETRESGID(cleanup, &real_gid, &eff_gid, &sav_gid));
114*49cdfc7eSAndroid Build Coastguard Worker 
115*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN == -1) {
116*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "getresgid() Failed, errno=%d : %s",
117*49cdfc7eSAndroid Build Coastguard Worker 				 TEST_ERRNO, strerror(TEST_ERRNO));
118*49cdfc7eSAndroid Build Coastguard Worker 			continue;
119*49cdfc7eSAndroid Build Coastguard Worker 		}
120*49cdfc7eSAndroid Build Coastguard Worker 		/*
121*49cdfc7eSAndroid Build Coastguard Worker 		 * Verify the real/effective/saved gid
122*49cdfc7eSAndroid Build Coastguard Worker 		 * values returned by getresgid with the
123*49cdfc7eSAndroid Build Coastguard Worker 		 * expected values.
124*49cdfc7eSAndroid Build Coastguard Worker 		 */
125*49cdfc7eSAndroid Build Coastguard Worker 		if ((real_gid != pr_gid) || (eff_gid != pe_gid) ||
126*49cdfc7eSAndroid Build Coastguard Worker 		    (sav_gid != ps_gid)) {
127*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "real:%d, effective:%d, "
128*49cdfc7eSAndroid Build Coastguard Worker 				 "saved-user:%d ids differ",
129*49cdfc7eSAndroid Build Coastguard Worker 				 real_gid, eff_gid, sav_gid);
130*49cdfc7eSAndroid Build Coastguard Worker 		} else {
131*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS, "Functionality of getresgid() "
132*49cdfc7eSAndroid Build Coastguard Worker 				 "successful");
133*49cdfc7eSAndroid Build Coastguard Worker 		}
134*49cdfc7eSAndroid Build Coastguard Worker 	}
135*49cdfc7eSAndroid Build Coastguard Worker 
136*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
137*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
138*49cdfc7eSAndroid Build Coastguard Worker }
139*49cdfc7eSAndroid Build Coastguard Worker 
140*49cdfc7eSAndroid Build Coastguard Worker /*
141*49cdfc7eSAndroid Build Coastguard Worker  * setup() - performs all ONE TIME setup for this test.
142*49cdfc7eSAndroid Build Coastguard Worker  *	     Make sure test process gid is root.
143*49cdfc7eSAndroid Build Coastguard Worker  *	     Get the real/effective/saved user id of the calling process.
144*49cdfc7eSAndroid Build Coastguard Worker  *	     Get the user info. of test user "ltpuser1" from /etc/passwd file.
145*49cdfc7eSAndroid Build Coastguard Worker  *	     Set the eff. user id of test process to that of "ltpuser1" user.
146*49cdfc7eSAndroid Build Coastguard Worker  */
setup(void)147*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
148*49cdfc7eSAndroid Build Coastguard Worker {
149*49cdfc7eSAndroid Build Coastguard Worker 	struct passwd *user_id;	/* passwd struct for test user */
150*49cdfc7eSAndroid Build Coastguard Worker 
151*49cdfc7eSAndroid Build Coastguard Worker 	tst_require_root();
152*49cdfc7eSAndroid Build Coastguard Worker 
153*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
154*49cdfc7eSAndroid Build Coastguard Worker 
155*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
156*49cdfc7eSAndroid Build Coastguard Worker 
157*49cdfc7eSAndroid Build Coastguard Worker 	/* Real user-id of the calling process */
158*49cdfc7eSAndroid Build Coastguard Worker 	pr_gid = getgid();
159*49cdfc7eSAndroid Build Coastguard Worker 
160*49cdfc7eSAndroid Build Coastguard Worker 	/* Saved user-id of the calling process. */
161*49cdfc7eSAndroid Build Coastguard Worker 	ps_gid = getegid();
162*49cdfc7eSAndroid Build Coastguard Worker 
163*49cdfc7eSAndroid Build Coastguard Worker 	/* Get effective gid of "ltpuser1" user from passwd file */
164*49cdfc7eSAndroid Build Coastguard Worker 	if ((user_id = getpwnam("nobody")) == NULL) {
165*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, cleanup,
166*49cdfc7eSAndroid Build Coastguard Worker 			 "getpwnam(nobody) Failed, errno=%d", errno);
167*49cdfc7eSAndroid Build Coastguard Worker 	}
168*49cdfc7eSAndroid Build Coastguard Worker 
169*49cdfc7eSAndroid Build Coastguard Worker 	/* Effective user-id of the test-user "ltpuser1" */
170*49cdfc7eSAndroid Build Coastguard Worker 	pe_gid = user_id->pw_gid;
171*49cdfc7eSAndroid Build Coastguard Worker 
172*49cdfc7eSAndroid Build Coastguard Worker 	/*
173*49cdfc7eSAndroid Build Coastguard Worker 	 * Set the effective user-id of the process to that of
174*49cdfc7eSAndroid Build Coastguard Worker 	 * test user "ltpuser1".
175*49cdfc7eSAndroid Build Coastguard Worker 	 * The real/saved  user id remains same as of caller.
176*49cdfc7eSAndroid Build Coastguard Worker 	 */
177*49cdfc7eSAndroid Build Coastguard Worker 	if (setresgid(-1, pe_gid, -1) < 0) {
178*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, cleanup,
179*49cdfc7eSAndroid Build Coastguard Worker 			 "setresgid(-1, %d, -1) Fails, errno:%d : %s",
180*49cdfc7eSAndroid Build Coastguard Worker 			 ps_gid, errno, strerror(errno));
181*49cdfc7eSAndroid Build Coastguard Worker 	}
182*49cdfc7eSAndroid Build Coastguard Worker }
183*49cdfc7eSAndroid Build Coastguard Worker 
184*49cdfc7eSAndroid Build Coastguard Worker /*
185*49cdfc7eSAndroid Build Coastguard Worker  * cleanup() - performs all ONE TIME cleanup for this test at
186*49cdfc7eSAndroid Build Coastguard Worker  *             completion or premature exit.
187*49cdfc7eSAndroid Build Coastguard Worker  */
cleanup(void)188*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
189*49cdfc7eSAndroid Build Coastguard Worker {
190*49cdfc7eSAndroid Build Coastguard Worker 
191*49cdfc7eSAndroid Build Coastguard Worker 	/* Reset the effective/saved gid of the calling process */
192*49cdfc7eSAndroid Build Coastguard Worker 	if (setregid(-1, pr_gid) < 0) {
193*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, NULL, "resetting process effective gid failed");
194*49cdfc7eSAndroid Build Coastguard Worker 	}
195*49cdfc7eSAndroid Build Coastguard Worker 
196*49cdfc7eSAndroid Build Coastguard Worker }
197