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 |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 22 | * rename13 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Verify rename() return successfully and performs no other action |
| 26 | * when "old" file and "new" file link to the same file. |
| 27 | * |
| 28 | * ALGORITHM |
| 29 | * Setup: |
| 30 | * Setup signal handling. |
| 31 | * Create temporary directory. |
| 32 | * Pause for SIGUSR1 if option specified. |
| 33 | * |
| 34 | * Test: |
| 35 | * Loop if the proper options are given. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 36 | * create the "old" file |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 37 | * link the "new" file to the "old" file |
| 38 | * rename the "old" to the "new" file |
| 39 | * verify the "new" file points to the original file |
| 40 | * verify the "old" file exists and points to |
| 41 | * the original file |
| 42 | * Cleanup: |
| 43 | * Print errno log and/or timing stats if options given |
| 44 | * Delete the temporary directory created. |
| 45 | * |
| 46 | * USAGE |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 47 | * rename13 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | * where, -c n : Run n copies concurrently. |
| 49 | * -f : Turn off functionality Testing. |
| 50 | * -i n : Execute test n times. |
| 51 | * -I x : Execute test for x seconds. |
| 52 | * -P x : Pause for x seconds between iterations. |
| 53 | * -t : Turn on syscall timing. |
| 54 | * |
| 55 | * HISTORY |
| 56 | * 07/2001 Ported by Wayne Boyer |
| 57 | * |
| 58 | * RESTRICTIONS |
| 59 | * None. |
| 60 | */ |
| 61 | #include <sys/types.h> |
| 62 | #include <sys/fcntl.h> |
| 63 | #include <sys/stat.h> |
| 64 | #include <unistd.h> |
| 65 | #include <errno.h> |
| 66 | |
| 67 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
| 69 | void setup(); |
| 70 | void cleanup(); |
| 71 | extern void do_file_setup(char *); |
| 72 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 73 | char *TCID = "rename13"; |
| 74 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
| 76 | int fd; |
| 77 | char fname[255], mname[255]; |
| 78 | struct stat buf1, buf2; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | dev_t olddev; |
| 80 | ino_t oldino; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 82 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 84 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 85 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * parse standard options |
| 89 | */ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 90 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 91 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * perform global setup for test |
| 95 | */ |
| 96 | setup(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 97 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | /* |
| 99 | * check looping state if -i option given |
| 100 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 102 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 103 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | * TEST rename()works when |
| 107 | * both old file and new file link to the same file |
| 108 | */ |
| 109 | |
| 110 | /* Call rename(2) */ |
| 111 | TEST(rename(fname, mname)); |
| 112 | |
| 113 | if (TEST_RETURN == -1) { |
| 114 | tst_resm(TFAIL, "rename(%s, %s) failed", fname, mname); |
| 115 | continue; |
| 116 | } |
| 117 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 118 | /* check the existence of "new", and get the status */ |
| 119 | if (stat(mname, &buf2) == -1) { |
| 120 | tst_brkm(TBROK, cleanup, "failed to stat file " |
| 121 | "%s in rename()", mname); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 122 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 124 | |
| 125 | /* check the existence of "old", and get the status */ |
| 126 | if (stat(fname, &buf1) == -1) { |
| 127 | tst_brkm(TBROK, cleanup, "failed to stat file " |
| 128 | "%s in rename()", fname); |
| 129 | |
| 130 | } |
| 131 | |
| 132 | /* verify the new file is the same as the original */ |
| 133 | if (buf2.st_dev != olddev || buf2.st_ino != oldino) { |
| 134 | tst_resm(TFAIL, |
| 135 | "rename() failed: new file does " |
| 136 | "not point to the same file as old " |
| 137 | "file"); |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | /* verify the old file is unchanged */ |
| 142 | if (buf1.st_dev != olddev || buf1.st_ino != oldino) { |
| 143 | tst_resm(TFAIL, |
| 144 | "rename() failed: old file does " |
| 145 | "not point to the original file"); |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | tst_resm(TPASS, "functionality of rename() is correct"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 150 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 151 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 153 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 154 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | |
| 156 | /* |
| 157 | * setup() - performs all ONE TIME setup for this test. |
| 158 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 159 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 161 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 163 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 164 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | |
| 166 | /* Create a temporary directory and make it current. */ |
| 167 | tst_tmpdir(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 168 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 169 | sprintf(fname, "./tfile_%d", getpid()); |
| 170 | sprintf(mname, "./rnfile_%d", getpid()); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | |
| 172 | /* create the "old" file */ |
| 173 | do_file_setup(fname); |
| 174 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 175 | if (stat(fname, &buf1) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | tst_brkm(TBROK, cleanup, "failed to stat file %s" |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 177 | "in rename()", fname); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 178 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* save the dev and inode */ |
| 182 | olddev = buf1.st_dev; |
| 183 | oldino = buf1.st_ino; |
| 184 | |
| 185 | /* link the "new" file to the "old" file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 186 | if (link(fname, mname) == -1) { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 187 | tst_brkm(TBROK, cleanup, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | "link from %s to %s failed!", fname, mname); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 189 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* |
| 193 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 194 | * completion or premature exit. |
| 195 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 196 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 197 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | |
| 199 | /* |
| 200 | * Remove the temporary directory. |
| 201 | */ |
| 202 | tst_rmdir(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 203 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 204 | } |