1/*
2 * Copyright (c) 2014 Fujitsu Ltd.
3 * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17/*
18 * Test Description:
19 *  Verify that,
20 *	File system GID is always set to the same value as the (possibly new)
21 *	effective GID.
22 */
23
24#define _GNU_SOURCE
25
26#include <errno.h>
27#include <unistd.h>
28#include <pwd.h>
29#include <sys/stat.h>
30#include "test.h"
31#include "safe_macros.h"
32#include "compat_16.h"
33
34TCID_DEFINE(setresgid04);
35int TST_TOTAL = 1;
36static struct passwd *ltpuser;
37static void setup(void);
38static void setresgid_verify(void);
39static void cleanup(void);
40
41int main(int argc, char **argv)
42{
43	int i, lc;
44
45	tst_parse_opts(argc, argv, NULL, NULL);
46
47	setup();
48
49	for (lc = 0; TEST_LOOPING(lc); lc++) {
50		tst_count = 0;
51		for (i = 0; i < TST_TOTAL; i++)
52			setresgid_verify();
53	}
54
55	cleanup();
56	tst_exit();
57}
58
59static void setup(void)
60{
61	tst_require_root();
62
63	tst_sig(NOFORK, DEF_HANDLER, cleanup);
64
65	TEST_PAUSE;
66
67	tst_tmpdir();
68
69	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
70
71	GID16_CHECK(ltpuser->pw_gid, "setresgid", cleanup)
72}
73
74static void setresgid_verify(void)
75{
76	struct stat buf;
77
78	TEST(SETRESGID(cleanup, -1, ltpuser->pw_gid, -1));
79
80	if (TEST_RETURN != 0) {
81		tst_resm(TFAIL | TTERRNO, "setresgid failed unexpectedly");
82		return;
83	}
84
85	SAFE_TOUCH(cleanup, "test_file", 0644, NULL);
86
87	SAFE_STAT(cleanup, "test_file", &buf);
88
89	if (ltpuser->pw_gid == buf.st_gid) {
90		tst_resm(TPASS, "setresgid succeeded as expected");
91	} else {
92		tst_resm(TFAIL,
93			 "setresgid failed unexpectedly; egid(%d) - st_gid(%d)",
94			 ltpuser->pw_gid, buf.st_gid);
95	}
96}
97
98static void cleanup(void)
99{
100	tst_rmdir();
101}
102