blob: 99a431265a2fcf1552378cb7ac4c62e0423c3751 [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 * 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 Coopere8530df2010-12-21 11:37:57 -080054#include "test.h"
robbiew37d19bd2003-11-14 16:57:49 +000055#include <sys/mman.h>
plars865695b2001-08-27 22:15:12 +000056
plars865695b2001-08-27 22:15:12 +000057char *TCID = "write03";
58int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000059
subrata_modak56207ce2009-03-23 13:35:39 +000060char *bad_addr = 0;
robbiew37d19bd2003-11-14 16:57:49 +000061
plars865695b2001-08-27 22:15:12 +000062void setup(void);
63void cleanup(void);
64
65char filename[100];
66
vapier6925ca32006-02-27 04:25:25 +000067#if !defined(UCLINUX)
68
robbiew6ee509b2003-04-01 20:28:41 +000069int main(int argc, char **argv)
plars865695b2001-08-27 22:15:12 +000070{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020071 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020072 const char *msg;
plars865695b2001-08-27 22:15:12 +000073
74 char wbuf[BUFSIZ], rbuf[BUFSIZ];
75 int fd;
76
Wanlong Gao354ebb42012-12-07 10:10:04 +080077 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080078 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +080079 }
plars865695b2001-08-27 22:15:12 +000080
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 Zhangd59a6592013-03-07 14:59:12 +080087 /* reset tst_count in case we are looping */
88 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000089
robbiew6ee509b2003-04-01 20:28:41 +000090//block1:
plars865695b2001-08-27 22:15:12 +000091 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 Gao354ebb42012-12-07 10:10:04 +080098 }
plars865695b2001-08-27 22:15:12 +000099
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 Gao354ebb42012-12-07 10:10:04 +0800105 }
plars865695b2001-08-27 22:15:12 +0000106
robbiew37d19bd2003-11-14 16:57:49 +0000107 if (write(fd, bad_addr, 100) != -1) {
plars865695b2001-08-27 22:15:12 +0000108 tst_resm(TFAIL, "write(2) failed to fail");
109 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800110 }
plars865695b2001-08-27 22:15:12 +0000111 close(fd);
112
113 if ((fd = open(filename, O_RDONLY)) == -1) {
114 tst_resm(TBROK, "open(2) failed, errno: %d", errno);
115 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800116 }
plars865695b2001-08-27 22:15:12 +0000117
118 if (read(fd, rbuf, 100) == -1) {
119 tst_resm(TBROK, "read(2) failed, errno: %d", errno);
120 cleanup();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800121 }
plars865695b2001-08-27 22:15:12 +0000122
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_modakdad6e1a2007-10-30 10:46:58 +0000131 close(fd);
plars865695b2001-08-27 22:15:12 +0000132 }
133 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800134 tst_exit();
plars865695b2001-08-27 22:15:12 +0000135}
136
vapier6925ca32006-02-27 04:25:25 +0000137#else
138
Mike Frysingerc57fba52014-04-09 18:56:30 -0400139int main(void)
vapier6925ca32006-02-27 04:25:25 +0000140{
141 tst_resm(TINFO, "test is not available on uClinux");
Garrett Cooper2c282152010-12-16 00:55:50 -0800142 tst_exit();
vapier6925ca32006-02-27 04:25:25 +0000143}
144
145#endif /* if !defined(UCLINUX) */
146
plars865695b2001-08-27 22:15:12 +0000147/*
148 * setup() - performs all ONE TIME setup for this test
149 */
subrata_modak56207ce2009-03-23 13:35:39 +0000150void setup(void)
plars865695b2001-08-27 22:15:12 +0000151{
Garrett Cooper2c282152010-12-16 00:55:50 -0800152
plars865695b2001-08-27 22:15:12 +0000153 tst_sig(FORK, DEF_HANDLER, cleanup);
154
plars865695b2001-08-27 22:15:12 +0000155 /* 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());
robbiew37d19bd2003-11-14 16:57:49 +0000166
subrata_modak56207ce2009-03-23 13:35:39 +0000167 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 }
robbiew37d19bd2003-11-14 16:57:49 +0000172
plars865695b2001-08-27 22:15:12 +0000173}
174
175/*
176 * cleanup() - performs all ONE TIME cleanup for this test at
subrata_modak56207ce2009-03-23 13:35:39 +0000177 * completion or premature exit
plars865695b2001-08-27 22:15:12 +0000178 */
subrata_modak56207ce2009-03-23 13:35:39 +0000179void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000180{
plars865695b2001-08-27 22:15:12 +0000181
182 unlink(filename);
183 tst_rmdir();
184
Wanlong Gao354ebb42012-12-07 10:10:04 +0800185}