blob: 293a15a9909dd8dce6a9112bfe3ce454fb33cc52 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubis7dc42c92013-09-12 14:55:55 +02002 * Copyright (c) International Business Machines Corp., 2001
3 * Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +00004 *
Cyril Hrubis7dc42c92013-09-12 14:55:55 +02005 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
plars865695b2001-08-27 22:15:12 +00009 *
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020010 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
plars865695b2001-08-27 22:15:12 +000014 *
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
plars865695b2001-08-27 22:15:12 +000021 * ALGORITHM
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020022 * As root sets the current group id to ltpuser1, verify the results
plars865695b2001-08-27 22:15:12 +000023 */
24#include <pwd.h>
25#include <errno.h>
Stanislav Kholmanskikh1c442442013-09-11 13:25:43 +040026
plars865695b2001-08-27 22:15:12 +000027#include "test.h"
Stanislav Kholmanskikh1c442442013-09-11 13:25:43 +040028#include <compat_16.h>
plars865695b2001-08-27 22:15:12 +000029
subrata_modak585950c2008-08-20 10:55:19 +000030TCID_DEFINE(setgid03);
plars865695b2001-08-27 22:15:12 +000031int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000032
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020033static char ltpuser1[] = "nobody";
34static char root[] = "root";
Vinson Lee3a7395c2013-09-12 13:24:39 -070035static struct passwd *ltpuser1pwent, *rootpwent;
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020036static gid_t mygid;
plars865695b2001-08-27 22:15:12 +000037
Vinson Lee3a7395c2013-09-12 13:24:39 -070038static void setup(void);
39static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000040
robbiew8541eeb2003-03-27 19:59:32 +000041int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000042{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020043 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020044 const char *msg;
plars865695b2001-08-27 22:15:12 +000045
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020046 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080047 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modakbdbaec52009-02-26 12:14:51 +000048
plars865695b2001-08-27 22:15:12 +000049 setup();
50
plars865695b2001-08-27 22:15:12 +000051 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080052 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000053
Stanislav Kholmanskikh1c442442013-09-11 13:25:43 +040054 TEST(SETGID(cleanup, ltpuser1pwent->pw_gid));
plars865695b2001-08-27 22:15:12 +000055
56 if (TEST_RETURN == -1) {
57 tst_resm(TFAIL, "call failed unexpectedly");
58 continue;
59 }
60
plars865695b2001-08-27 22:15:12 +000061 if (getgid() != ltpuser1pwent->pw_gid) {
62 tst_resm(TFAIL, "setgid failed to set gid to "
63 "ltpuser1's gid");
64 } else {
65 tst_resm(TPASS, "functionality of getgid() is correct");
66 }
67 }
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020068
plars865695b2001-08-27 22:15:12 +000069 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -080070 tst_exit();
plars865695b2001-08-27 22:15:12 +000071}
72
Cyril Hrubis7dc42c92013-09-12 14:55:55 +020073static void setup(void)
plars865695b2001-08-27 22:15:12 +000074{
Stanislav Kholmanskikh1c442442013-09-11 13:25:43 +040075 tst_require_root(NULL);
plars865695b2001-08-27 22:15:12 +000076
plars865695b2001-08-27 22:15:12 +000077 tst_sig(NOFORK, DEF_HANDLER, cleanup);
78
plars865695b2001-08-27 22:15:12 +000079 TEST_PAUSE;
80
81 if ((rootpwent = getpwnam(root)) == NULL) {
82 tst_brkm(TBROK, cleanup, "getpwnam failed for "
83 "user id %s", root);
84 }
85
86 mygid = getgid();
87
88 if (mygid != rootpwent->pw_gid) {
89 tst_brkm(TBROK, cleanup, "real group id is not root");
90 }
91
92 if ((ltpuser1pwent = getpwnam(ltpuser1)) == NULL) {
93 tst_brkm(TBROK, cleanup, "getpwnam failed for user "
94 "id %s", ltpuser1);
95 }
subrata_modakda53b082008-08-04 12:06:21 +000096
Stanislav Kholmanskikh1c442442013-09-11 13:25:43 +040097 GID16_CHECK(ltpuser1pwent->pw_gid, setgid, cleanup);
plars865695b2001-08-27 22:15:12 +000098}
99
Cyril Hrubis7dc42c92013-09-12 14:55:55 +0200100static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000101{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700102}