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