plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame] | 20 | * with this program; if not, write the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * |
| 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 Kholmanskikh | de8b8db | 2013-10-21 11:25:16 +0400 | [diff] [blame] | 32 | * Author: William Roske |
| 33 | * Co-pilot: Dave Fenner |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | */ |
Stanislav Kholmanskikh | de8b8db | 2013-10-21 11:25:16 +0400 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * Testcase to test the basic functionality of setregid(2) systemm call. |
| 38 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | |
| 40 | #include <errno.h> |
| 41 | #include <string.h> |
| 42 | #include <signal.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | #include <sys/types.h> |
| 44 | |
| 45 | #include "test.h" |
Stanislav Kholmanskikh | de8b8db | 2013-10-21 11:25:16 +0400 | [diff] [blame] | 46 | #include "compat_16.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | |
Stanislav Kholmanskikh | de8b8db | 2013-10-21 11:25:16 +0400 | [diff] [blame] | 48 | static void setup(void); |
| 49 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | |
Stanislav Kholmanskikh | de8b8db | 2013-10-21 11:25:16 +0400 | [diff] [blame] | 51 | TCID_DEFINE(setregid01); |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 52 | int TST_TOTAL = 5; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
Stanislav Kholmanskikh | de8b8db | 2013-10-21 11:25:16 +0400 | [diff] [blame] | 54 | static gid_t gid, egid; /* current real and effective group id */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 56 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 58 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 59 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 60 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 61 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 62 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 64 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 66 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 68 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 70 | /* |
| 71 | * TEST CASE: |
| 72 | * Dont change either real or effective gid |
| 73 | */ |
| 74 | gid = getgid(); |
Stanislav Kholmanskikh | de8b8db | 2013-10-21 11:25:16 +0400 | [diff] [blame] | 75 | GID16_CHECK(gid, setregid, cleanup); |
| 76 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 77 | egid = getegid(); |
Stanislav Kholmanskikh | de8b8db | 2013-10-21 11:25:16 +0400 | [diff] [blame] | 78 | GID16_CHECK(egid, setregid, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
Stanislav Kholmanskikh | de8b8db | 2013-10-21 11:25:16 +0400 | [diff] [blame] | 80 | TEST(SETREGID(cleanup, -1, -1)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 82 | if (TEST_RETURN == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | 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 Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 87 | tst_resm(TPASS, |
| 88 | "setregid - Dont change either real or effective gid returned %ld", |
| 89 | TEST_RETURN); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 91 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 92 | /* |
| 93 | * TEST CASE: |
| 94 | * change effective to effective gid |
| 95 | */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 96 | |
Stanislav Kholmanskikh | de8b8db | 2013-10-21 11:25:16 +0400 | [diff] [blame] | 97 | TEST(SETREGID(cleanup, -1, egid)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 98 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | if (TEST_RETURN == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 100 | tst_resm(TFAIL, |
| 101 | "setregid - change effective to effective gid failed, errno=%d : %s", |
| 102 | TEST_ERRNO, strerror(TEST_ERRNO)); |
| 103 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 104 | tst_resm(TPASS, |
| 105 | "setregid - change effective to effective gid returned %ld", |
| 106 | TEST_RETURN); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 108 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | /* |
| 110 | * TEST CASE: |
| 111 | * change real to real gid |
| 112 | */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 113 | |
Stanislav Kholmanskikh | de8b8db | 2013-10-21 11:25:16 +0400 | [diff] [blame] | 114 | TEST(SETREGID(cleanup, gid, -1)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 115 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | if (TEST_RETURN == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | tst_resm(TFAIL, |
| 118 | "setregid - change real to real gid failed, errno=%d : %s", |
| 119 | TEST_ERRNO, strerror(TEST_ERRNO)); |
| 120 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 121 | tst_resm(TPASS, |
| 122 | "setregid - change real to real gid returned %ld", |
| 123 | TEST_RETURN); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 124 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 125 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 126 | /* |
| 127 | * TEST CASE: |
| 128 | * change effective to real gid |
| 129 | */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 130 | |
Stanislav Kholmanskikh | de8b8db | 2013-10-21 11:25:16 +0400 | [diff] [blame] | 131 | TEST(SETREGID(cleanup, -1, gid)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 132 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 133 | if (TEST_RETURN == -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 134 | tst_resm(TFAIL, |
| 135 | "setregid - change effective to real gid failed, errno=%d : %s", |
| 136 | TEST_ERRNO, strerror(TEST_ERRNO)); |
| 137 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 138 | tst_resm(TPASS, |
| 139 | "setregid - change effective to real gid returned %ld", |
| 140 | TEST_RETURN); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 141 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 142 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | /* |
| 144 | * TEST CASE: |
| 145 | * try to change real to current real |
| 146 | */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 147 | |
Stanislav Kholmanskikh | de8b8db | 2013-10-21 11:25:16 +0400 | [diff] [blame] | 148 | TEST(SETREGID(cleanup, gid, gid)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 149 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 150 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 151 | tst_resm(TFAIL | TTERRNO, "setregid failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 152 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 153 | tst_resm(TPASS, "setregid return %ld", |
| 154 | TEST_RETURN); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 155 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 156 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 157 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 160 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 161 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | |
Stanislav Kholmanskikh | de8b8db | 2013-10-21 11:25:16 +0400 | [diff] [blame] | 163 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 165 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 169 | tst_tmpdir(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 170 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | |
Stanislav Kholmanskikh | de8b8db | 2013-10-21 11:25:16 +0400 | [diff] [blame] | 172 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 174 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 175 | } |