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