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 | * write03.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to check that write(2) doesn't corrupt a file when it fails |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * Create a file for writing, write 100 bytes to it. Then make write(2) |
| 29 | * fail with some erroneous parameter, close the fd. Then reopen the |
| 30 | * file in RDONLY mode, and read the contents of the file. Compare the |
| 31 | * buffers, to see whether they are same. |
| 32 | * |
| 33 | * USAGE: <for command-line> |
| 34 | * write03 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 35 | * where, -c n : Run n copies concurrently. |
| 36 | * -e : Turn on errno logging. |
| 37 | * -i n : Execute test n times. |
| 38 | * -I x : Execute test for x seconds. |
| 39 | * -P x : Pause for x seconds between iterations. |
| 40 | * -t : Turn on syscall timing. |
| 41 | * |
| 42 | * History |
| 43 | * 07/2001 John George |
| 44 | * -Ported |
| 45 | * |
| 46 | * Restrictions |
| 47 | * NONE |
| 48 | */ |
| 49 | |
| 50 | #include <unistd.h> |
| 51 | #include <string.h> |
| 52 | #include <fcntl.h> |
| 53 | #include <errno.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 54 | #include "test.h" |
robbiew | 37d19bd | 2003-11-14 16:57:49 +0000 | [diff] [blame] | 55 | #include <sys/mman.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | char *TCID = "write03"; |
| 58 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 60 | char *bad_addr = 0; |
robbiew | 37d19bd | 2003-11-14 16:57:49 +0000 | [diff] [blame] | 61 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | void setup(void); |
| 63 | void cleanup(void); |
| 64 | |
| 65 | char filename[100]; |
| 66 | |
vapier | 6925ca3 | 2006-02-27 04:25:25 +0000 | [diff] [blame] | 67 | #if !defined(UCLINUX) |
| 68 | |
robbiew | 6ee509b | 2003-04-01 20:28:41 +0000 | [diff] [blame] | 69 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 71 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 72 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
| 74 | char wbuf[BUFSIZ], rbuf[BUFSIZ]; |
| 75 | int fd; |
| 76 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 77 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 78 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 79 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
| 81 | /* global setup */ |
| 82 | setup(); |
| 83 | |
| 84 | /* The following loop checks looping state if -i option given */ |
| 85 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 86 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 87 | /* reset tst_count in case we are looping */ |
| 88 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
robbiew | 6ee509b | 2003-04-01 20:28:41 +0000 | [diff] [blame] | 90 | //block1: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | tst_resm(TINFO, "Enter Block 1: test to check if write " |
| 92 | "corrupts the file when write fails"); |
| 93 | |
| 94 | fd = creat(filename, 0644); |
| 95 | if (fd < 0) { |
| 96 | tst_resm(TBROK, "creating a new file failed"); |
| 97 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 98 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | |
| 100 | (void)memset(wbuf, '0', 100); |
| 101 | |
| 102 | if (write(fd, wbuf, 100) == -1) { |
| 103 | tst_resm(TFAIL, "failed to write to %s", filename); |
| 104 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 105 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
robbiew | 37d19bd | 2003-11-14 16:57:49 +0000 | [diff] [blame] | 107 | if (write(fd, bad_addr, 100) != -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | tst_resm(TFAIL, "write(2) failed to fail"); |
| 109 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 110 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | close(fd); |
| 112 | |
| 113 | if ((fd = open(filename, O_RDONLY)) == -1) { |
| 114 | tst_resm(TBROK, "open(2) failed, errno: %d", errno); |
| 115 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 116 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | |
| 118 | if (read(fd, rbuf, 100) == -1) { |
| 119 | tst_resm(TBROK, "read(2) failed, errno: %d", errno); |
| 120 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | |
| 123 | if (memcmp(wbuf, rbuf, 100) == 0) { |
| 124 | tst_resm(TPASS, "failure of write(2) didnot corrupt " |
| 125 | "the file"); |
| 126 | } else { |
| 127 | tst_resm(TFAIL, "failure of write(2) corrupted the " |
| 128 | "file"); |
| 129 | } |
| 130 | tst_resm(TINFO, "Exit block 1"); |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 131 | close(fd); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | } |
| 133 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 134 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | } |
| 136 | |
vapier | 6925ca3 | 2006-02-27 04:25:25 +0000 | [diff] [blame] | 137 | #else |
| 138 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 139 | int main(void) |
vapier | 6925ca3 | 2006-02-27 04:25:25 +0000 | [diff] [blame] | 140 | { |
| 141 | tst_resm(TINFO, "test is not available on uClinux"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 142 | tst_exit(); |
vapier | 6925ca3 | 2006-02-27 04:25:25 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | #endif /* if !defined(UCLINUX) */ |
| 146 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | /* |
| 148 | * setup() - performs all ONE TIME setup for this test |
| 149 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 150 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 152 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 154 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | /* Pause if that option was specified |
| 156 | * TEST_PAUSE contains the code to fork the test with the -i option. |
| 157 | * You want to make sure you do this before you create your temporary |
| 158 | * directory. |
| 159 | */ |
| 160 | TEST_PAUSE; |
| 161 | |
| 162 | /* Create a unique temporary directory and chdir() to it. */ |
| 163 | tst_tmpdir(); |
| 164 | |
| 165 | sprintf(filename, "./write03.%d", getpid()); |
robbiew | 37d19bd | 2003-11-14 16:57:49 +0000 | [diff] [blame] | 166 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 167 | bad_addr = mmap(0, 1, PROT_NONE, |
| 168 | MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); |
| 169 | if (bad_addr == MAP_FAILED) { |
| 170 | printf("mmap failed\n"); |
| 171 | } |
robbiew | 37d19bd | 2003-11-14 16:57:49 +0000 | [diff] [blame] | 172 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* |
| 176 | * cleanup() - performs all ONE TIME cleanup for this test at |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 177 | * completion or premature exit |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 178 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 179 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | |
| 182 | unlink(filename); |
| 183 | tst_rmdir(); |
| 184 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 185 | } |