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 | |
| 20 | /* |
| 21 | * NAME |
| 22 | * rename08 |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * This test will verify that rename(2) syscall failed in EFAULT |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * Setup: |
| 29 | * Setup signal handling. |
| 30 | * Create temporary directory. |
| 31 | * Pause for SIGUSR1 if option specified. |
| 32 | * Create a valid file to use in the rename() call. |
| 33 | * |
| 34 | * Test: |
| 35 | * Loop if the proper options are given. |
| 36 | * 1. "old" is a valid file, newpath points to address |
| 37 | * outside allocated address space |
| 38 | * rename the "old" to the "new" file |
| 39 | * verify rename() failed with error EFAULT |
| 40 | * |
| 41 | * 2. "old" points to address outside allocated address space |
| 42 | * ,"new" is a valid file |
| 43 | * rename the "old" to the "new" |
| 44 | * verify rename() failed with error EFAULT |
| 45 | * |
| 46 | * 3. oldpath and newpath are all NULL |
| 47 | * try to rename NULL to NULL |
| 48 | * verify rename() failed with error EFAULT |
| 49 | * Cleanup: |
| 50 | * Print errno log and/or timing stats if options given |
| 51 | * Delete the temporary directory created.* |
| 52 | * USAGE |
| 53 | * rename08 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 54 | * where, -c n : Run n copies concurrently. |
| 55 | * -e : Turn on errno logging. |
| 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 | #include <sys/types.h> |
| 68 | #include <sys/fcntl.h> |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 69 | #include <sys/mman.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | #include <unistd.h> |
| 71 | #include <errno.h> |
| 72 | |
| 73 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | |
| 75 | void setup(); |
| 76 | void cleanup(); |
| 77 | extern void do_file_setup(char *); |
| 78 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 79 | char *TCID = "rename08"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | char *bad_addr = 0; |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 82 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | int fd; |
| 84 | char fname[255]; |
| 85 | |
| 86 | struct test_case_t { |
| 87 | char *fd; |
| 88 | char *fd2; |
| 89 | int error; |
| 90 | } TC[] = { |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 91 | #if !defined(UCLINUX) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | /* "new" file is invalid - EFAULT */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | { |
| 94 | fname, (char *)-1, EFAULT}, |
| 95 | /* "old" file is invalid - EFAULT */ |
| 96 | { |
| 97 | (char *)-1, fname, EFAULT}, |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 98 | #endif |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 99 | /* both files are NULL - EFAULT */ |
| 100 | { |
| 101 | NULL, NULL, EFAULT} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | }; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 103 | |
Cyril Hrubis | b863a0b | 2014-09-24 13:15:29 +0200 | [diff] [blame] | 104 | int TST_TOTAL = ARRAY_SIZE(TC); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 108 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 109 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | int i; |
| 111 | |
| 112 | /* |
| 113 | * parse standard options |
| 114 | */ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 115 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 116 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | setup(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 119 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | /* |
| 121 | * check looping state if -i option given |
| 122 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 123 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 124 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 125 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | |
| 127 | /* loop through the test cases */ |
| 128 | for (i = 0; i < TST_TOTAL; i++) { |
| 129 | |
| 130 | TEST(rename(TC[i].fd, TC[i].fd2)); |
| 131 | |
| 132 | if (TEST_RETURN != -1) { |
| 133 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 134 | continue; |
| 135 | } |
| 136 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | if (TEST_ERRNO == TC[i].error) { |
| 138 | tst_resm(TPASS, "expected failure - " |
| 139 | "errno = %d : %s", TEST_ERRNO, |
| 140 | strerror(TEST_ERRNO)); |
| 141 | } else { |
| 142 | tst_resm(TFAIL, "unexpected error - %d : %s - " |
| 143 | "expected %d", TEST_ERRNO, |
| 144 | strerror(TEST_ERRNO), TC[i].error); |
| 145 | } |
| 146 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 147 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 148 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | cleanup(); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 150 | tst_exit(); |
robbiew | c2cbe20 | 2003-03-27 18:32:02 +0000 | [diff] [blame] | 151 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /* |
| 155 | * setup() - performs all ONE TIME setup for this test. |
| 156 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 157 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | { |
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 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 161 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 162 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | |
| 164 | /* Create a temporary directory and make it current. */ |
| 165 | tst_tmpdir(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 166 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | sprintf(fname, "./tfile_%d", getpid()); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | |
| 169 | do_file_setup(fname); |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 170 | |
vapier | 62b16cf | 2007-02-09 20:48:23 +0000 | [diff] [blame] | 171 | #if !defined(UCLINUX) |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 172 | bad_addr = mmap(0, 1, PROT_NONE, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 173 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
vapier | 0722a2b | 2006-05-12 15:44:11 +0000 | [diff] [blame] | 174 | if (bad_addr == MAP_FAILED) { |
plars | 1ad8451 | 2002-07-23 13:11:18 +0000 | [diff] [blame] | 175 | tst_brkm(TBROK, cleanup, "mmap failed"); |
| 176 | } |
| 177 | TC[0].fd2 = bad_addr; |
| 178 | TC[1].fd = bad_addr; |
vapier | b5ed1f6 | 2006-08-24 04:16:32 +0000 | [diff] [blame] | 179 | #endif |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* |
| 183 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 184 | * completion or premature exit. |
| 185 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 186 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | |
| 189 | /* |
| 190 | * Remove the temporary directory. |
| 191 | */ |
| 192 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 193 | } |