blob: 28547019b4fdd6b688b2d0ce05aedd9b77b42e0b [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
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>
plars1ad84512002-07-23 13:11:18 +000069#include <sys/mman.h>
plars865695b2001-08-27 22:15:12 +000070#include <unistd.h>
71#include <errno.h>
72
73#include "test.h"
plars865695b2001-08-27 22:15:12 +000074
75void setup();
76void cleanup();
77extern void do_file_setup(char *);
78
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020079char *TCID = "rename08";
plars865695b2001-08-27 22:15:12 +000080
subrata_modak56207ce2009-03-23 13:35:39 +000081char *bad_addr = 0;
plars1ad84512002-07-23 13:11:18 +000082
plars865695b2001-08-27 22:15:12 +000083int fd;
84char fname[255];
85
86struct test_case_t {
87 char *fd;
88 char *fd2;
89 int error;
90} TC[] = {
vapierb5ed1f62006-08-24 04:16:32 +000091#if !defined(UCLINUX)
plars865695b2001-08-27 22:15:12 +000092 /* "new" file is invalid - EFAULT */
subrata_modak56207ce2009-03-23 13:35:39 +000093 {
94 fname, (char *)-1, EFAULT},
95 /* "old" file is invalid - EFAULT */
96 {
97 (char *)-1, fname, EFAULT},
vapierb5ed1f62006-08-24 04:16:32 +000098#endif
subrata_modak56207ce2009-03-23 13:35:39 +000099 /* both files are NULL - EFAULT */
100 {
101 NULL, NULL, EFAULT}
plars865695b2001-08-27 22:15:12 +0000102};
Wanlong Gao354ebb42012-12-07 10:10:04 +0800103
Cyril Hrubisb863a0b2014-09-24 13:15:29 +0200104int TST_TOTAL = ARRAY_SIZE(TC);
plars865695b2001-08-27 22:15:12 +0000105
subrata_modak56207ce2009-03-23 13:35:39 +0000106int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000107{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200108 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200109 const char *msg;
plars865695b2001-08-27 22:15:12 +0000110 int i;
111
112 /*
113 * parse standard options
114 */
Garrett Cooper53740502010-12-16 00:04:01 -0800115 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800116 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000117
plars865695b2001-08-27 22:15:12 +0000118 setup();
subrata_modakbdbaec52009-02-26 12:14:51 +0000119
plars865695b2001-08-27 22:15:12 +0000120 /*
121 * check looping state if -i option given
122 */
subrata_modak56207ce2009-03-23 13:35:39 +0000123 for (lc = 0; TEST_LOOPING(lc); lc++) {
124
Caspar Zhangd59a6592013-03-07 14:59:12 +0800125 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000126
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
plars865695b2001-08-27 22:15:12 +0000137 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 Cooper2c282152010-12-16 00:55:50 -0800147 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000148
plars865695b2001-08-27 22:15:12 +0000149 cleanup();
Garrett Cooper53740502010-12-16 00:04:01 -0800150 tst_exit();
robbiewc2cbe202003-03-27 18:32:02 +0000151
plars865695b2001-08-27 22:15:12 +0000152}
153
154/*
155 * setup() - performs all ONE TIME setup for this test.
156 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400157void setup(void)
plars865695b2001-08-27 22:15:12 +0000158{
Garrett Cooper2c282152010-12-16 00:55:50 -0800159
plars865695b2001-08-27 22:15:12 +0000160 tst_sig(NOFORK, DEF_HANDLER, cleanup);
161
subrata_modak4bb656a2009-02-26 12:02:09 +0000162 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000163
164 /* Create a temporary directory and make it current. */
165 tst_tmpdir();
subrata_modakbdbaec52009-02-26 12:14:51 +0000166
subrata_modak56207ce2009-03-23 13:35:39 +0000167 sprintf(fname, "./tfile_%d", getpid());
plars865695b2001-08-27 22:15:12 +0000168
169 do_file_setup(fname);
plars1ad84512002-07-23 13:11:18 +0000170
vapier62b16cf2007-02-09 20:48:23 +0000171#if !defined(UCLINUX)
robbiewd34d5812005-07-11 22:28:09 +0000172 bad_addr = mmap(0, 1, PROT_NONE,
subrata_modak56207ce2009-03-23 13:35:39 +0000173 MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0);
vapier0722a2b2006-05-12 15:44:11 +0000174 if (bad_addr == MAP_FAILED) {
plars1ad84512002-07-23 13:11:18 +0000175 tst_brkm(TBROK, cleanup, "mmap failed");
176 }
177 TC[0].fd2 = bad_addr;
178 TC[1].fd = bad_addr;
vapierb5ed1f62006-08-24 04:16:32 +0000179#endif
plars865695b2001-08-27 22:15:12 +0000180}
181
182/*
183 * cleanup() - performs all ONE TIME cleanup for this test at
184 * completion or premature exit.
185 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400186void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000187{
plars865695b2001-08-27 22:15:12 +0000188
189 /*
190 * Remove the temporary directory.
191 */
192 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700193}