plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * NAME |
| 3 | * rename07 |
| 4 | * |
| 5 | * DESCRIPTION |
| 6 | * This test will verify that rename(2) failed in ENOTDIR |
| 7 | * |
| 8 | * CALLS |
| 9 | * stat,open,rename,mkdir,close |
| 10 | * |
| 11 | * ALGORITHM |
| 12 | * Setup: |
| 13 | * Setup signal handling. |
| 14 | * Create temporary directory. |
| 15 | * Pause for SIGUSR1 if option specified. |
| 16 | * create the "old" directory and the "new" file |
| 17 | * rename the "old" directory to the "new" file |
| 18 | * |
| 19 | * Test: |
| 20 | * Loop if the proper options are given. |
| 21 | * verify rename() failed and returned ENOTDIR |
| 22 | * |
| 23 | * Cleanup: |
| 24 | * Print errno log and/or timing stats if options given |
| 25 | * Delete the temporary directory created.* |
| 26 | * USAGE |
| 27 | * rename07 [-c n] [-e] [-i n] [-I x] [-p x] [-t] |
| 28 | * where, -c n : Run n copies concurrently. |
| 29 | * -e : Turn on errno logging. |
| 30 | * -i n : Execute test n times. |
| 31 | * -I x : Execute test for x seconds. |
| 32 | * -P x : Pause for x seconds between iterations. |
| 33 | * -t : Turn on syscall timing. |
| 34 | * |
| 35 | * HISTORY |
| 36 | * 07/2001 Ported by Wayne Boyer |
| 37 | * |
| 38 | * RESTRICTIONS |
| 39 | * None. |
| 40 | */ |
| 41 | #include <sys/types.h> |
| 42 | #include <sys/fcntl.h> |
| 43 | #include <sys/stat.h> |
| 44 | #include <unistd.h> |
| 45 | #include <errno.h> |
| 46 | |
| 47 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | |
| 49 | void setup(); |
| 50 | void cleanup(); |
| 51 | extern void do_file_setup(char *); |
| 52 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 53 | char *TCID = "rename07"; |
| 54 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | int fd; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 57 | char mname[255], fdir[255]; |
| 58 | struct stat buf1, buf2; |
| 59 | dev_t olddev, olddev1; |
| 60 | ino_t oldino, oldino1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 62 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 64 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 65 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * parse standard options |
| 69 | */ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 70 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 71 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * perform global setup for test |
| 75 | */ |
| 76 | setup(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 77 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | /* |
| 79 | * check looping state if -i option given |
| 80 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 82 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 83 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
| 85 | /* rename a directory to a file */ |
| 86 | /* Call rename(2) */ |
| 87 | TEST(rename(fdir, mname)); |
| 88 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | if (TEST_RETURN != -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | tst_resm(TFAIL, "rename(%s, %s) succeeded unexpectedly", |
| 91 | fdir, mname); |
| 92 | continue; |
| 93 | } |
| 94 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | if (TEST_ERRNO != ENOTDIR) { |
| 96 | tst_resm(TFAIL, "Expected ENOTDIR got %d", TEST_ERRNO); |
| 97 | } else { |
| 98 | tst_resm(TPASS, "rename() returned ENOTDIR"); |
| 99 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 100 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 101 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 103 | tst_exit(); |
robbiew | c2cbe20 | 2003-03-27 18:32:02 +0000 | [diff] [blame] | 104 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* |
| 108 | * setup() - performs all ONE TIME setup for this test. |
| 109 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 110 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 112 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 114 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 115 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | |
| 117 | /* Create a temporary directory and make it current. */ |
| 118 | tst_tmpdir(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 119 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 120 | sprintf(fdir, "./rndir_%d", getpid()); |
| 121 | sprintf(mname, "./tfile_%d", getpid()); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | |
| 123 | /* create "old" directory */ |
| 124 | if (stat(fdir, &buf1) != -1) { |
| 125 | tst_brkm(TBROK, cleanup, "tmp directory %s found!", fdir); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 126 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | |
| 128 | if (mkdir(fdir, 00770) == -1) { |
| 129 | tst_brkm(TBROK, cleanup, "Could not create directory %s", fdir); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 130 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | |
| 132 | if (stat(fdir, &buf1) == -1) { |
| 133 | tst_brkm(TBROK, cleanup, "failed to stat directory %s " |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 134 | "in rename()", fdir); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 135 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* save "old"'s dev and ino */ |
| 139 | olddev = buf1.st_dev; |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 140 | oldino = buf1.st_ino; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 142 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | * create "new" file |
| 144 | */ |
| 145 | do_file_setup(mname); |
| 146 | |
| 147 | if (stat(mname, &buf2) == -1) { |
| 148 | tst_brkm(TBROK, cleanup, "failed to stat file %s in rename()", |
| 149 | mname); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 150 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /* save "new"'s dev and ino */ |
| 154 | olddev1 = buf2.st_dev; |
| 155 | oldino1 = buf2.st_ino; |
| 156 | } |
| 157 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | /* |
| 159 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 160 | * completion or premature exit. |
| 161 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 162 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | |
| 165 | /* |
| 166 | * Remove the temporary directory. |
| 167 | */ |
| 168 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 169 | } |