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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 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: chmod04 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, chmod(2) will succeed to change the mode of a directory |
| 25 | * and set the sticky bit on it if invoked by non-root (uid != 0) process |
| 26 | * with the following constraints, |
| 27 | * - the process is the owner of the 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 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 | * chmod() should return value 0 on success and succeeds to set sticky bit |
| 33 | * on the specified directory. |
| 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 | * chmod04 [-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. |
| 64 | * |
| 65 | * HISTORY |
| 66 | * 07/2001 Ported by Wayne Boyer |
| 67 | * |
| 68 | * RESTRICTIONS: |
| 69 | * This test should be run by 'non-super-user' 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> |
robbiew | a7142d9 | 2001-08-28 18:21:45 +0000 | [diff] [blame] | 80 | #include <pwd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
| 82 | #include "test.h" |
| 83 | #include "usctest.h" |
| 84 | |
| 85 | #define DIR_MODE S_IRWXU | S_IRWXG | S_IRWXO |
| 86 | #define PERMS 01777 /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 87 | * Mode permissions of test directory with |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | * sticky bit set. |
| 89 | */ |
| 90 | #define TESTDIR "testdir_4" |
| 91 | |
| 92 | char *TCID="chmod04"; /* Test program identifier. */ |
| 93 | int TST_TOTAL=1; /* Total number of test cases. */ |
| 94 | extern int Tst_count; /* Test Case counter for tst_* routines */ |
robbiew | a7142d9 | 2001-08-28 18:21:45 +0000 | [diff] [blame] | 95 | char nobody_uid[] = "nobody"; |
| 96 | struct passwd *ltpuser; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | |
| 98 | void setup(); /* Setup function for the test */ |
| 99 | void cleanup(); /* Cleanup function for the test */ |
| 100 | |
| 101 | int |
| 102 | main(int ac, char **av) |
| 103 | { |
| 104 | struct stat stat_buf; /* stat struct. */ |
| 105 | int lc; /* loop counter */ |
| 106 | char *msg; /* message returned from parse_opts */ |
| 107 | mode_t dir_mode; /* mode permissions set on testdirectory */ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 108 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | /* Parse standard options given to run the test. */ |
| 110 | msg = parse_opts(ac, av, (option_t *) NULL, NULL); |
| 111 | if (msg != (char *) NULL) { |
| 112 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 113 | tst_exit(); |
| 114 | } |
| 115 | |
| 116 | /* Perform global setup for test */ |
| 117 | setup(); |
| 118 | |
| 119 | /* Check looping state if -i option given */ |
| 120 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 121 | /* Reset Tst_count in case we are looping. */ |
| 122 | Tst_count = 0; |
| 123 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 124 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | * Call chmod(2) with mode argument to |
| 126 | * set sticky bit on TESTDIR |
| 127 | */ |
| 128 | TEST(chmod(TESTDIR, PERMS)); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame^] | 129 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | /* check return code of chmod(2) */ |
| 131 | if (TEST_RETURN == -1) { |
| 132 | tst_resm(TFAIL, "chmod(%s, %#o) Failed, errno=%d : %s", |
| 133 | TESTDIR, PERMS, TEST_ERRNO, |
| 134 | strerror(TEST_ERRNO)); |
| 135 | continue; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Perform functional verification if test |
| 140 | * executed without (-f) option. |
| 141 | */ |
| 142 | if (STD_FUNCTIONAL_TEST) { |
| 143 | /* |
| 144 | * Get the file information using |
| 145 | * stat(2). |
| 146 | */ |
| 147 | if (stat(TESTDIR, &stat_buf) < 0) { |
| 148 | tst_brkm(TFAIL, cleanup, |
| 149 | "stat(2) of %s failed, errno:%d", |
| 150 | TESTDIR, TEST_ERRNO); |
| 151 | } |
| 152 | dir_mode = stat_buf.st_mode; |
| 153 | |
| 154 | /* Verify STICKY BIT SET on directory */ |
| 155 | if ((dir_mode & PERMS) == PERMS) { |
| 156 | tst_resm(TPASS, "Functionality of " |
| 157 | "chmod(%s, %#o) successful", |
| 158 | TESTDIR, PERMS); |
| 159 | } else { |
| 160 | tst_resm(TFAIL, "%s: Incorrect modes 0%03o, " |
| 161 | "Expected 0%03o", |
| 162 | TESTDIR, dir_mode, PERMS); |
| 163 | } |
| 164 | } else { |
| 165 | tst_resm(TPASS, "call succeeded"); |
| 166 | } |
| 167 | } /* End for TEST_LOOPING */ |
| 168 | |
| 169 | /* Call cleanup() to undo setup done for the test. */ |
| 170 | cleanup(); |
| 171 | |
robbiew | 2c94524 | 2002-11-11 19:01:25 +0000 | [diff] [blame] | 172 | return 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | /*NOTREACHED*/ |
| 174 | } /* End main */ |
| 175 | |
| 176 | /* |
| 177 | * void |
| 178 | * setup() - performs all ONE TIME setup for this test. |
| 179 | * Create a temporary directory and cd to it. |
| 180 | * Create another test directory under temporary directory. |
| 181 | */ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 182 | void |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | setup() |
| 184 | { |
| 185 | /* capture signals */ |
| 186 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 187 | |
robbiew | a7142d9 | 2001-08-28 18:21:45 +0000 | [diff] [blame] | 188 | /* Switch to nobody user for correct error code collection */ |
| 189 | if (geteuid() != 0) { |
| 190 | tst_brkm(TBROK, tst_exit, "Test must be run as root"); |
| 191 | } |
| 192 | ltpuser = getpwnam(nobody_uid); |
| 193 | if (setuid(ltpuser->pw_uid) == -1) { |
| 194 | tst_resm(TINFO, "setuid failed to " |
| 195 | "to set the effective uid to %d", |
| 196 | ltpuser->pw_uid); |
| 197 | perror("setuid"); |
| 198 | } |
| 199 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | |
| 201 | /* Pause if that option was specified */ |
| 202 | TEST_PAUSE; |
| 203 | |
| 204 | /* make a temp directory and cd to it */ |
| 205 | tst_tmpdir(); |
| 206 | |
| 207 | /* |
| 208 | * Create a test directory under temporary directory with specified |
| 209 | * mode permissios. |
| 210 | */ |
| 211 | if (mkdir(TESTDIR, DIR_MODE) < 0) { |
| 212 | tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", TESTDIR); |
| 213 | } |
| 214 | } /* End setup() */ |
| 215 | |
| 216 | /* |
| 217 | * void |
| 218 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 219 | * completion or premature exit. |
| 220 | * Remove the test directory and temporary directory created in setup(). |
| 221 | */ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 222 | void |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 223 | cleanup() |
| 224 | { |
| 225 | /* |
| 226 | * print timing stats if that option was specified. |
| 227 | */ |
| 228 | TEST_CLEANUP; |
| 229 | |
| 230 | /* Remove tmp dir and all files in it */ |
| 231 | tst_rmdir(); |
| 232 | |
| 233 | /* exit with return code appropriate for results */ |
| 234 | tst_exit(); |
| 235 | } /* End cleanup() */ |