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