blob: 2c371d359da6f3217fcbb03e5bc5b0d0f2e5bfc2 [file] [log] [blame]
Zeng Linggangb9297492014-05-15 14:48:57 +08001/*
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"
Zeng Linggangb9297492014-05-15 14:48:57 +080031#include "safe_macros.h"
Han Pingtian75fb0572014-11-28 16:33:41 +080032#include "compat_16.h"
Zeng Linggangb9297492014-05-15 14:48:57 +080033
Han Pingtian75fb0572014-11-28 16:33:41 +080034TCID_DEFINE(setresgid04);
Zeng Linggangb9297492014-05-15 14:48:57 +080035int 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;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020044 const char *msg;
Zeng Linggangb9297492014-05-15 14:48:57 +080045
46 msg = parse_opts(argc, argv, NULL, NULL);
47 if (msg != NULL)
48 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
49
50 setup();
51
52 for (lc = 0; TEST_LOOPING(lc); lc++) {
53 tst_count = 0;
54 for (i = 0; i < TST_TOTAL; i++)
55 setresgid_verify();
56 }
57
58 cleanup();
59 tst_exit();
60}
61
62static void setup(void)
63{
64 tst_require_root(NULL);
65
66 tst_sig(NOFORK, DEF_HANDLER, cleanup);
67
68 TEST_PAUSE;
69
70 tst_tmpdir();
71
72 ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
Han Pingtian75fb0572014-11-28 16:33:41 +080073
74 GID16_CHECK(ltpuser->pw_gid, "setresgid", cleanup)
Zeng Linggangb9297492014-05-15 14:48:57 +080075}
76
77static void setresgid_verify(void)
78{
79 struct stat buf;
80
Han Pingtian75fb0572014-11-28 16:33:41 +080081 TEST(SETRESGID(cleanup, -1, ltpuser->pw_gid, -1));
Zeng Linggangb9297492014-05-15 14:48:57 +080082
83 if (TEST_RETURN != 0) {
84 tst_resm(TFAIL | TTERRNO, "setresgid failed unexpectedly");
85 return;
86 }
87
88 SAFE_TOUCH(cleanup, "test_file", 0644, NULL);
89
90 SAFE_STAT(cleanup, "test_file", &buf);
91
92 if (ltpuser->pw_gid == buf.st_gid) {
93 tst_resm(TPASS, "setresgid succeeded as expected");
94 } else {
95 tst_resm(TFAIL,
96 "setresgid failed unexpectedly; egid(%d) - st_gid(%d)",
97 ltpuser->pw_gid, buf.st_gid);
98 }
99}
100
101static void cleanup(void)
102{
Zeng Linggangb9297492014-05-15 14:48:57 +0800103 tst_rmdir();
104}