xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/setgroups/setgroups04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /*
2  *   Copyright (C) Bull S.A. 2001
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: setgroups04
22  *
23  * Test Description:
24  *  Verify that, setgroups() fails with -1 and sets errno to EFAULT if the list has an invalid address.
25  *
26  * Expected Result:
27  *  setgroups() should fail with return value -1 and set expected errno.
28  *
29  * Algorithm:
30  *  Setup:
31  *   Setup signal handling.
32  *   Pause for SIGUSR1 if option specified.
33  *
34  *  Test:
35  *   Loop if the proper options are given.
36  *   Execute system call
37  *   Check return code, if system call failed (return=-1)
38  *   	if errno set == expected errno
39  *   		Issue sys call fails with expected return value and errno.
40  *   	Otherwise,
41  *		Issue sys call fails with unexpected errno.
42  *   Otherwise,
43  *	Issue sys call returns unexpected value.
44  *
45  *  Cleanup:
46  *   Print errno log and/or timing stats if options given
47  *
48  * Usage:  <for command-line>
49  *  setgroups04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
50  *     where,  -c n : Run n copies concurrently.
51  *             -f   : Turn off functionality Testing.
52  *	       -i n : Execute test n times.
53  *	       -I x : Execute test for x seconds.
54  *	       -P x : Pause for x seconds between iterations.
55  *	       -t   : Turn on syscall timing.
56  *
57  * HISTORY
58  *	05/2002 Ported by Andr� Merlier
59  *
60  * RESTRICTIONS:
61  *  none.
62  *
63  */
64 #include <sys/types.h>
65 #include <sys/param.h>
66 #include <unistd.h>
67 #include <errno.h>
68 #include <pwd.h>
69 #include <grp.h>
70 
71 #include "test.h"
72 
73 /*
74  * Don't forget to remove USE_LEGACY_COMPAT_16_H from Makefile after
75  * rewriting this test to the new API.
76  */
77 #include "compat_16.h"
78 
79 TCID_DEFINE(setgroups04);
80 int TST_TOTAL = 1;
81 
82 GID_T groups_list[NGROUPS];
83 
84 void setup();			/* setup function for the test */
85 void cleanup();			/* cleanup function for the test */
86 
main(int ac,char ** av)87 int main(int ac, char **av)
88 {
89 	int lc;
90 	int gidsetsize;		/* total no. of groups */
91 	char *test_desc;	/* test specific error message */
92 
93 	tst_parse_opts(ac, av, NULL, NULL);
94 
95 	/* Perform setup for test */
96 	setup();
97 
98 	for (lc = 0; TEST_LOOPING(lc); lc++) {
99 
100 		tst_count = 0;
101 
102 		gidsetsize = NGROUPS;
103 		test_desc = "EFAULT";
104 
105 		/*
106 		 * Call setgroups() to test condition
107 		 * verify that it fails with -1 return value and
108 		 * sets appropriate errno.
109 		 */
110 		TEST(SETGROUPS(cleanup, gidsetsize, sbrk(0)));
111 
112 		if (TEST_RETURN != -1) {
113 			tst_resm(TFAIL, "setgroups() returned %ld, "
114 				 "expected -1, errno=%d", TEST_RETURN,
115 				 EFAULT);
116 		} else {
117 
118 			if (TEST_ERRNO == EFAULT) {
119 				tst_resm(TPASS,
120 					 "setgroups() fails with expected "
121 					 "error EFAULT errno:%d", TEST_ERRNO);
122 			} else {
123 				tst_resm(TFAIL, "setgroups() fails, %s, "
124 					 "errno=%d, expected errno=%d",
125 					 test_desc, TEST_ERRNO, EFAULT);
126 			}
127 		}
128 
129 	}
130 
131 	cleanup();
132 	tst_exit();
133 
134 }
135 
136 /*
137  * setup()
138  */
setup(void)139 void setup(void)
140 {
141 	tst_require_root();
142 
143 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
144 
145 	TEST_PAUSE;
146 
147 }
148 
149 /*
150  * cleanup()
151  */
cleanup(void)152 void cleanup(void)
153 {
154 
155 }
156