blob: 6e3762376c37a58ccf3d4407684484487f5f0f70 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars865695b2001-08-27 22:15:12 +000022 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 *
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040032 * Author: William Roske
33 * Co-pilot: Dave Fenner
plars865695b2001-08-27 22:15:12 +000034 */
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040035
36/*
37 * Testcase to test the basic functionality of setregid(2) systemm call.
38 */
plars865695b2001-08-27 22:15:12 +000039
40#include <errno.h>
41#include <string.h>
42#include <signal.h>
plars865695b2001-08-27 22:15:12 +000043#include <sys/types.h>
44
45#include "test.h"
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040046#include "compat_16.h"
plars865695b2001-08-27 22:15:12 +000047
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040048static void setup(void);
49static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000050
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040051TCID_DEFINE(setregid01);
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020052int TST_TOTAL = 5;
plars865695b2001-08-27 22:15:12 +000053
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040054static gid_t gid, egid; /* current real and effective group id */
plars865695b2001-08-27 22:15:12 +000055
subrata_modak56207ce2009-03-23 13:35:39 +000056int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000057{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020058 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020059 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000060
Garrett Cooper45e285d2010-11-22 12:19:25 -080061 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080062 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000063
subrata_modak56207ce2009-03-23 13:35:39 +000064 setup();
plars865695b2001-08-27 22:15:12 +000065
subrata_modak56207ce2009-03-23 13:35:39 +000066 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars865695b2001-08-27 22:15:12 +000067
Caspar Zhangd59a6592013-03-07 14:59:12 +080068 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000069
subrata_modak56207ce2009-03-23 13:35:39 +000070 /*
71 * TEST CASE:
72 * Dont change either real or effective gid
73 */
74 gid = getgid();
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040075 GID16_CHECK(gid, setregid, cleanup);
76
subrata_modak56207ce2009-03-23 13:35:39 +000077 egid = getegid();
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040078 GID16_CHECK(egid, setregid, cleanup);
plars865695b2001-08-27 22:15:12 +000079
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040080 TEST(SETREGID(cleanup, -1, -1));
plars865695b2001-08-27 22:15:12 +000081
subrata_modak56207ce2009-03-23 13:35:39 +000082 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +000083 tst_resm(TFAIL,
84 "setregid - Dont change either real or effective gid failed, errno=%d : %s",
85 TEST_ERRNO, strerror(TEST_ERRNO));
86 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +020087 tst_resm(TPASS,
88 "setregid - Dont change either real or effective gid returned %ld",
89 TEST_RETURN);
subrata_modak56207ce2009-03-23 13:35:39 +000090 }
subrata_modakbdbaec52009-02-26 12:14:51 +000091
subrata_modak56207ce2009-03-23 13:35:39 +000092 /*
93 * TEST CASE:
94 * change effective to effective gid
95 */
subrata_modakbdbaec52009-02-26 12:14:51 +000096
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +040097 TEST(SETREGID(cleanup, -1, egid));
subrata_modakbdbaec52009-02-26 12:14:51 +000098
subrata_modak56207ce2009-03-23 13:35:39 +000099 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000100 tst_resm(TFAIL,
101 "setregid - change effective to effective gid failed, errno=%d : %s",
102 TEST_ERRNO, strerror(TEST_ERRNO));
103 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200104 tst_resm(TPASS,
105 "setregid - change effective to effective gid returned %ld",
106 TEST_RETURN);
subrata_modak56207ce2009-03-23 13:35:39 +0000107 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000108
subrata_modak56207ce2009-03-23 13:35:39 +0000109 /*
110 * TEST CASE:
111 * change real to real gid
112 */
subrata_modakbdbaec52009-02-26 12:14:51 +0000113
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400114 TEST(SETREGID(cleanup, gid, -1));
subrata_modakbdbaec52009-02-26 12:14:51 +0000115
subrata_modak56207ce2009-03-23 13:35:39 +0000116 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000117 tst_resm(TFAIL,
118 "setregid - change real to real gid failed, errno=%d : %s",
119 TEST_ERRNO, strerror(TEST_ERRNO));
120 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200121 tst_resm(TPASS,
122 "setregid - change real to real gid returned %ld",
123 TEST_RETURN);
subrata_modak56207ce2009-03-23 13:35:39 +0000124 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000125
subrata_modak56207ce2009-03-23 13:35:39 +0000126 /*
127 * TEST CASE:
128 * change effective to real gid
129 */
subrata_modakbdbaec52009-02-26 12:14:51 +0000130
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400131 TEST(SETREGID(cleanup, -1, gid));
subrata_modakbdbaec52009-02-26 12:14:51 +0000132
subrata_modak56207ce2009-03-23 13:35:39 +0000133 if (TEST_RETURN == -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000134 tst_resm(TFAIL,
135 "setregid - change effective to real gid failed, errno=%d : %s",
136 TEST_ERRNO, strerror(TEST_ERRNO));
137 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200138 tst_resm(TPASS,
139 "setregid - change effective to real gid returned %ld",
140 TEST_RETURN);
subrata_modak56207ce2009-03-23 13:35:39 +0000141 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000142
subrata_modak56207ce2009-03-23 13:35:39 +0000143 /*
144 * TEST CASE:
145 * try to change real to current real
146 */
subrata_modakbdbaec52009-02-26 12:14:51 +0000147
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400148 TEST(SETREGID(cleanup, gid, gid));
subrata_modakbdbaec52009-02-26 12:14:51 +0000149
subrata_modak56207ce2009-03-23 13:35:39 +0000150 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151 tst_resm(TFAIL | TTERRNO, "setregid failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000152 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200153 tst_resm(TPASS, "setregid return %ld",
154 TEST_RETURN);
subrata_modak56207ce2009-03-23 13:35:39 +0000155 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000156
Garrett Cooper2c282152010-12-16 00:55:50 -0800157 }
plars865695b2001-08-27 22:15:12 +0000158
subrata_modak56207ce2009-03-23 13:35:39 +0000159 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800160 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800161}
plars865695b2001-08-27 22:15:12 +0000162
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400163static void setup(void)
plars865695b2001-08-27 22:15:12 +0000164{
subrata_modak56207ce2009-03-23 13:35:39 +0000165 tst_sig(NOFORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000166
subrata_modak56207ce2009-03-23 13:35:39 +0000167 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000168
subrata_modak56207ce2009-03-23 13:35:39 +0000169 tst_tmpdir();
Garrett Cooper2c282152010-12-16 00:55:50 -0800170}
plars865695b2001-08-27 22:15:12 +0000171
Stanislav Kholmanskikhde8b8db2013-10-21 11:25:16 +0400172static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000173{
subrata_modak56207ce2009-03-23 13:35:39 +0000174 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700175}