fchmod03.c revision 4548c6cf9bcdd96d8303caa4130ab638b61f8a30
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20/* 21 * Test Name: fchmod03 22 * 23 * Test Description: 24 * Verify that, fchmod(2) will succeed to change the mode of a file 25 * and set the sticky bit on it if invoked by non-root (uid != 0) 26 * process with the following constraints, 27 * - the process is the owner of the file. 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. 30 * 31 * Expected Result: 32 * fchmod() should return value 0 on success and succeeds to change 33 * the mode of specified file, sets sticky bit on it. 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, 47 * Verify the Functionality of system call 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 * fchmod03 [-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> 80#include <pwd.h> 81 82#include "test.h" 83#include "usctest.h" 84 85#define FILE_MODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) 86#define PERMS 01777 87#define TESTFILE "testfile" 88 89int fd; /* file descriptor for test file */ 90char *TCID = "fchmod03"; /* Test program identifier. */ 91int TST_TOTAL = 1; /* Total number of test cases. */ 92 93char nobody_uid[] = "nobody"; 94struct passwd *ltpuser; 95 96void setup(); /* Main setup function for the test */ 97void cleanup(); /* Main cleanup function for the test */ 98 99int main(int ac, char **av) 100{ 101 struct stat stat_buf; /* stat struct. */ 102 int lc; /* loop counter */ 103 char *msg; /* message returned from parse_opts */ 104 mode_t file_mode; /* mode permissions set on testfile */ 105 106 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) 107 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); 108 109 setup(); 110 111 for (lc = 0; TEST_LOOPING(lc); lc++) { 112 113 Tst_count = 0; 114 115 TEST(fchmod(fd, PERMS)); 116 117 if (TEST_RETURN == -1) { 118 tst_resm(TFAIL|TTERRNO, "fchmod failed"); 119 continue; 120 } 121 /* 122 * Perform functional verification if test 123 * executed without (-f) option. 124 */ 125 if (STD_FUNCTIONAL_TEST) { 126 /* 127 * Get the file information using 128 * fstat(2). 129 */ 130 if (fstat(fd, &stat_buf) == -1) 131 tst_brkm(TFAIL|TERRNO, cleanup, "fstat failed"); 132 file_mode = stat_buf.st_mode; 133 134 /* Verify STICKY BIT set on testfile */ 135 if ((file_mode & PERMS) != PERMS) 136 tst_resm(TFAIL, "%s: Incorrect modes 0%3o, " 137 "Expected 0777", TESTFILE, file_mode); 138 else 139 tst_resm(TPASS, "Functionality of fchmod(%d, " 140 "%#o) successful", fd, PERMS); 141 } else 142 tst_resm(TPASS, "call succeeded"); 143 } 144 145 cleanup(); 146 147 tst_exit(); 148} 149 150void setup() 151{ 152 153 tst_sig(NOFORK, DEF_HANDLER, cleanup); 154 155 tst_require_root(NULL); 156 157 ltpuser = getpwnam(nobody_uid); 158 if (ltpuser == NULL) 159 tst_brkm(TBROK|TERRNO, NULL, "getpwnam failed"); 160 if (seteuid(ltpuser->pw_uid) == -1) 161 tst_brkm(TBROK|TERRNO, NULL, "seteuid failed"); 162 163 TEST_PAUSE; 164 165 tst_tmpdir(); 166 167 /* 168 * Create a test file under temporary directory with specified 169 * mode permissios and set the ownership of the test file to the 170 * uid/gid of guest user. 171 */ 172 if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) 173 tst_brkm(TBROK|TERRNO, cleanup, "open failed"); 174} 175 176void cleanup() 177{ 178 TEST_CLEANUP; 179 180 if (close(fd) == -1) 181 tst_resm(TWARN|TERRNO, "close failed"); 182 183 tst_rmdir(); 184 185} 186