plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 4 | * |
| 5 | * 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. |
| 9 | * |
| 10 | * 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. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 20 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 21 | * Test Name: fchmod02 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, fchmod(2) will succeed to change the mode of a file/directory |
| 25 | * set the sticky bit on it if invoked by root (uid = 0) process with |
| 26 | * the following constraints, |
| 27 | * - the process is not the owner of the file/directory. |
| 28 | * - the effective group ID or one of the supplementary group ID's of the |
| 29 | * process is equal to the group ID of the file/directory. |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 30 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 31 | * Expected Result: |
| 32 | * fchmod() should return value 0 on success and succeeds to set sticky bit |
| 33 | * on the specified file. |
| 34 | * |
| 35 | * Algorithm: |
| 36 | * Setup: |
| 37 | * Setup signal handling. |
| 38 | * Create temporary directory. |
| 39 | * Pause for SIGUSR1 if option specified. |
| 40 | * |
| 41 | * Test: |
| 42 | * Loop if the proper options are given. |
| 43 | * Execute system call |
| 44 | * Check return code, if system call failed (return=-1) |
| 45 | * Log the errno and Issue a FAIL message. |
| 46 | * Otherwise, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 47 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | * if successful, |
| 49 | * Issue Functionality-Pass message. |
| 50 | * Otherwise, |
| 51 | * Issue Functionality-Fail message. |
| 52 | * Cleanup: |
| 53 | * Print errno log and/or timing stats if options given |
| 54 | * Delete the temporary directory created. |
| 55 | * |
| 56 | * Usage: <for command-line> |
| 57 | * fchmod02 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 58 | * where, -c n : Run n copies concurrently. |
| 59 | * -f : Turn off functionality Testing. |
| 60 | * -i n : Execute test n times. |
| 61 | * -I x : Execute test for x seconds. |
| 62 | * -P x : Pause for x seconds between iterations. |
| 63 | * -t : Turn on syscall timing. |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 64 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | * HISTORY |
| 66 | * 07/2001 Ported by Wayne Boyer |
| 67 | * |
| 68 | * RESTRICTIONS: |
| 69 | * This test should be run by 'super-user' (root) only. |
| 70 | * |
| 71 | */ |
| 72 | |
| 73 | #include <stdio.h> |
| 74 | #include <sys/types.h> |
| 75 | #include <sys/stat.h> |
| 76 | #include <sys/fcntl.h> |
| 77 | #include <errno.h> |
| 78 | #include <string.h> |
| 79 | #include <signal.h> |
| 80 | #include <grp.h> |
| 81 | #include <pwd.h> |
| 82 | |
| 83 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
| 85 | #define LTPUSER "nobody" |
robbiew | 2dbfc40 | 2001-08-31 16:22:34 +0000 | [diff] [blame] | 86 | #define LTPGRP "users" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 88 | #define PERMS 01777 /* |
| 89 | * Mode permissions of test file with sticky |
| 90 | * bit set. |
| 91 | */ |
| 92 | #define TESTFILE "testfile" |
| 93 | |
| 94 | int fd; /* file descriptor variable */ |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 95 | char *TCID = "fchmod02"; |
| 96 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | |
| 98 | void setup(); /* Main setup function for the test */ |
| 99 | void cleanup(); /* Main cleanup function for the test */ |
| 100 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | { |
| 103 | struct stat stat_buf; /* stat(2) struct contents */ |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 104 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 105 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 107 | msg = parse_opts(ac, av, NULL, NULL); |
| 108 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 110 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | } |
| 112 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | setup(); |
| 114 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 116 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 117 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 119 | /* |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 120 | * Call fchmod(2) with specified mode argument |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | * (sticky-bit set) on testfile. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 122 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | TEST(fchmod(fd, PERMS)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 124 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | if (TEST_RETURN == -1) { |
| 126 | tst_resm(TFAIL, "fchmod(%d, %#o) Failed, errno=%d : %s", |
| 127 | fd, PERMS, TEST_ERRNO, strerror(TEST_ERRNO)); |
| 128 | continue; |
| 129 | } |
| 130 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 131 | * Get the testfile information using |
| 132 | * fstat(2). |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 134 | if (fstat(fd, &stat_buf) < 0) { |
| 135 | tst_brkm(TFAIL, cleanup, "fstat(2) of %s " |
| 136 | "failed, errno:%d", TESTFILE, |
| 137 | TEST_ERRNO); |
| 138 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 140 | /* Check for expected mode permissions */ |
| 141 | if ((stat_buf.st_mode & PERMS) == PERMS) { |
| 142 | tst_resm(TPASS, "Functionality of fchmod(%d, " |
| 143 | "%#o) Successful", fd, PERMS); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 145 | tst_resm(TFAIL, "%s: Incorrect modes 0%03o, " |
| 146 | "Expected 0%03o", TESTFILE, |
| 147 | stat_buf.st_mode, PERMS); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 149 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 152 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 153 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | |
| 155 | /* |
| 156 | * void |
| 157 | * setup() - performs all ONE TIME setup for this test. |
| 158 | * Create a temporary directory and change directory to it. |
| 159 | * Create a test file under temporary directory. |
| 160 | * Change the ownership of test file to that of "ltpuser1" user. |
| 161 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 162 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 164 | struct passwd *ltpuser; /* password struct for ltpuser1 */ |
| 165 | struct group *ltpgroup; /* group struct for ltpuser1 */ |
| 166 | gid_t group1_gid; /* user and process group id's */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | uid_t user1_uid; |
| 168 | |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 169 | tst_require_root(NULL); |
| 170 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 172 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | TEST_PAUSE; |
| 174 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | tst_tmpdir(); |
| 176 | |
| 177 | /* Get the uid of guest user - ltpuser1 */ |
| 178 | if ((ltpuser = getpwnam(LTPUSER)) == NULL) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 179 | tst_brkm(TBROK, cleanup, "%s not in /etc/passwd", LTPUSER); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | } |
| 181 | user1_uid = ltpuser->pw_uid; |
| 182 | |
| 183 | /* Get the group id of guest user - ltpuser1 */ |
| 184 | if ((ltpgroup = getgrnam(LTPGRP)) == NULL) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 185 | tst_brkm(TBROK, cleanup, "%s not in /etc/group", LTPGRP); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 186 | } |
| 187 | group1_gid = ltpgroup->gr_gid; |
| 188 | |
| 189 | /* |
| 190 | * Create a test file under temporary directory with specified |
| 191 | * mode permissios and set the ownership of the test file to the |
| 192 | * uid/gid of guest user user1. |
| 193 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 194 | if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | tst_brkm(TBROK, cleanup, |
| 196 | "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d : %s", |
| 197 | TESTFILE, FILE_MODE, errno, strerror(errno)); |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 198 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 199 | |
| 200 | if (chown(TESTFILE, user1_uid, group1_gid) < 0) { |
| 201 | tst_brkm(TBROK, cleanup, "chown(2) of %s failed", TESTFILE); |
| 202 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 203 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | /* Set the effective gid of the process to that of user */ |
| 205 | if (setgid(group1_gid) < 0) { |
| 206 | tst_brkm(TBROK, cleanup, "setgid(2) to %d failed", group1_gid); |
| 207 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 208 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * void |
| 212 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 213 | * completion or premature exit. |
| 214 | * Close the testfile created in the setup. |
| 215 | * Remove the test directory and testfile created in the setup. |
| 216 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 217 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 218 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 219 | |
| 220 | /* Close the testfile created in the setup() */ |
| 221 | if (close(fd) == -1) { |
| 222 | tst_brkm(TBROK, NULL, "close(%s) Failed, errno=%d : %s", |
| 223 | TESTFILE, errno, strerror(errno)); |
| 224 | } |
| 225 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 226 | tst_rmdir(); |
| 227 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 228 | } |