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