Zeng Linggang | b929749 | 2014-05-15 14:48:57 +0800 | [diff] [blame] | 1 | /* |
| 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 Linggang | b929749 | 2014-05-15 14:48:57 +0800 | [diff] [blame] | 31 | #include "safe_macros.h" |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 32 | #include "compat_16.h" |
Zeng Linggang | b929749 | 2014-05-15 14:48:57 +0800 | [diff] [blame] | 33 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 34 | TCID_DEFINE(setresgid04); |
Zeng Linggang | b929749 | 2014-05-15 14:48:57 +0800 | [diff] [blame] | 35 | int TST_TOTAL = 1; |
| 36 | static struct passwd *ltpuser; |
| 37 | static void setup(void); |
| 38 | static void setresgid_verify(void); |
| 39 | static void cleanup(void); |
| 40 | |
| 41 | int main(int argc, char **argv) |
| 42 | { |
| 43 | int i, lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 44 | const char *msg; |
Zeng Linggang | b929749 | 2014-05-15 14:48:57 +0800 | [diff] [blame] | 45 | |
| 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 | |
| 62 | static 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 Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 73 | |
| 74 | GID16_CHECK(ltpuser->pw_gid, "setresgid", cleanup) |
Zeng Linggang | b929749 | 2014-05-15 14:48:57 +0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static void setresgid_verify(void) |
| 78 | { |
| 79 | struct stat buf; |
| 80 | |
Han Pingtian | 75fb057 | 2014-11-28 16:33:41 +0800 | [diff] [blame] | 81 | TEST(SETRESGID(cleanup, -1, ltpuser->pw_gid, -1)); |
Zeng Linggang | b929749 | 2014-05-15 14:48:57 +0800 | [diff] [blame] | 82 | |
| 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 | |
| 101 | static void cleanup(void) |
| 102 | { |
Zeng Linggang | b929749 | 2014-05-15 14:48:57 +0800 | [diff] [blame] | 103 | tst_rmdir(); |
| 104 | } |