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 | /* |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 21 | * Test Name: fchmod07 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, fchmod(2) succeeds when used to change the mode permissions |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 25 | * of a file specified by file descriptor. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | * |
| 27 | * Expected Result: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 28 | * fchmod(2) should return 0 and the mode permissions set on file should match |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | * the specified mode. |
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 | * Algorithm: |
| 32 | * Setup: |
| 33 | * Setup signal handling. |
| 34 | * Create temporary directory. |
| 35 | * Pause for SIGUSR1 if option specified. |
| 36 | * |
| 37 | * Test: |
| 38 | * Loop if the proper options are given. |
| 39 | * Execute system call |
| 40 | * Check return code, if system call failed (return=-1) |
| 41 | * Log the errno and Issue a FAIL message. |
| 42 | * Otherwise, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 43 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | * if successful, |
| 45 | * Issue Functionality-Pass message. |
| 46 | * Otherwise, |
| 47 | * Issue Functionality-Fail message. |
| 48 | * Cleanup: |
| 49 | * Print errno log and/or timing stats if options given |
| 50 | * Delete the temporary directory created. |
| 51 | * |
| 52 | * Usage: <for command-line> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 53 | * fchmod07 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | * where, -c n : Run n copies concurrently. |
| 55 | * -f : Turn off functionality Testing. |
| 56 | * -i n : Execute test n times. |
| 57 | * -I x : Execute test for x seconds. |
| 58 | * -P x : Pause for x seconds between iterations. |
| 59 | * -t : Turn on syscall timing. |
| 60 | * |
| 61 | * HISTORY |
| 62 | * 07/2001 Ported by Wayne Boyer |
| 63 | * |
| 64 | * RESTRICTIONS: |
| 65 | * None. |
| 66 | */ |
| 67 | |
| 68 | #include <stdio.h> |
| 69 | #include <sys/types.h> |
| 70 | #include <sys/stat.h> |
| 71 | #include <sys/fcntl.h> |
| 72 | #include <errno.h> |
| 73 | #include <string.h> |
| 74 | #include <signal.h> |
| 75 | |
| 76 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
| 78 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 79 | #define TESTFILE "testfile" |
| 80 | |
| 81 | int fd; /* file descriptor for testfile */ |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 82 | char *TCID = "fchmod07"; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | int TST_TOTAL = 8; /* Total number of test conditions */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | int Modes[] = { 0, 07, 070, 0700, 0777, 02777, 04777, 06777 }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | |
| 87 | void setup(); /* setup function for the test */ |
| 88 | void cleanup(); /* cleanup function for the test */ |
| 89 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 90 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | { |
| 92 | struct stat stat_buf; /* stat(2) struct contents */ |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 93 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 94 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | int ind; /* counter variable for chmod(2) tests */ |
| 96 | int mode; /* file mode permission */ |
| 97 | |
| 98 | TST_TOTAL = sizeof(Modes) / sizeof(int); |
| 99 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 100 | msg = parse_opts(ac, av, NULL, NULL); |
| 101 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 103 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | } |
| 105 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | setup(); |
| 107 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 109 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 110 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | |
| 112 | for (ind = 0; ind < TST_TOTAL; ind++) { |
| 113 | mode = Modes[ind]; |
| 114 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 115 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | * Call fchmod(2) with different mode permission |
| 117 | * bits to set it for "testfile". |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 118 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | TEST(fchmod(fd, mode)); |
| 120 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | if (TEST_RETURN == -1) { |
| 122 | tst_resm(TFAIL, "fchmod(%d, %#o) Failed, " |
| 123 | "errno=%d : %s", fd, mode, TEST_ERRNO, |
| 124 | strerror(TEST_ERRNO)); |
| 125 | continue; |
| 126 | } |
| 127 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 128 | * Get the testfile information using |
| 129 | * fstat(2). |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 131 | if (fstat(fd, &stat_buf) < 0) { |
| 132 | tst_brkm(TFAIL, cleanup, |
| 133 | "fstat(2) of " |
| 134 | "%s failed, errno:%d", |
| 135 | TESTFILE, TEST_ERRNO); |
| 136 | } |
| 137 | stat_buf.st_mode &= ~S_IFREG; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 139 | /* |
| 140 | * Check for expected mode permissions |
| 141 | * on testfile. |
| 142 | */ |
| 143 | if (stat_buf.st_mode == mode) { |
| 144 | tst_resm(TPASS, |
| 145 | "Functionality of " |
| 146 | "fchmod(%d, %#o) successful", |
| 147 | fd, mode); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 149 | tst_resm(TFAIL, "%s: Incorrect modes " |
| 150 | "0%03o, Expected 0%03o", |
| 151 | TESTFILE, stat_buf.st_mode, |
| 152 | mode); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 155 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 158 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 159 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | |
| 161 | /* |
| 162 | * void |
| 163 | * setup() - performs all ONE TIME setup for this test. |
| 164 | * Create a temporary directory and change directory to it. |
| 165 | * Create a test file under temporary directory. |
| 166 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 167 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 169 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 171 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | TEST_PAUSE; |
| 173 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | tst_tmpdir(); |
| 175 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 176 | if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | tst_brkm(TBROK, cleanup, |
| 178 | "open(%s, O_RDWR|O_CREAT, %o) Failed, errno=%d : %s", |
| 179 | TESTFILE, FILE_MODE, errno, strerror(errno)); |
| 180 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 181 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | |
| 183 | /* |
| 184 | * void |
| 185 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 186 | * completion or premature exit. |
| 187 | * Close the testfile created in the setup. |
| 188 | * Remove the test directory and testfile created in the setup. |
| 189 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 190 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | |
| 193 | /* Close the TESTFILE opened in the setup() */ |
| 194 | if (close(fd) == -1) { |
| 195 | tst_brkm(TBROK, NULL, |
| 196 | "close(%s) Failed, errno=%d : %s", |
| 197 | TESTFILE, errno, strerror(errno)); |
| 198 | } |
| 199 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | tst_rmdir(); |
| 201 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 202 | } |