1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Copyright (c) International Business Machines Corp., 2001 4 * 07/2001 Ported by John George 5 * Copyright (c) 2017 Fujitsu Ltd. 6 * 04/2017 Modified by Jinhui Huang 7 */ 8 9/* 10 * DESCRIPTION 11 * Testcase to check that write(2) doesn't corrupt a file when it fails 12 * 13 * ALGORITHM 14 * Create a file for writing, write 100 bytes to it. Then make write(2) 15 * fail with some erroneous parameter, close the fd. Then reopen the 16 * file in RDONLY mode, and read the contents of the file. Compare the 17 * buffers, to see whether they are same. 18 */ 19 20#include <stdio.h> 21#include <errno.h> 22#include "tst_test.h" 23 24static char *bad_addr; 25static char wbuf[BUFSIZ], rbuf[BUFSIZ]; 26static int fd; 27 28static void verify_write(void) 29{ 30 fd = SAFE_CREAT("testfile", 0644); 31 32 SAFE_WRITE(SAFE_WRITE_ALL, fd, wbuf, 100); 33 34 if (write(fd, bad_addr, 100) != -1) { 35 tst_res(TFAIL, "write() failed to fail"); 36 SAFE_CLOSE(fd); 37 return; 38 } 39 40 SAFE_CLOSE(fd); 41 42 fd = SAFE_OPEN("testfile", O_RDONLY); 43 44 memset(rbuf, 0, BUFSIZ); 45 46 SAFE_READ(0, fd, rbuf, 100); 47 48 if (memcmp(wbuf, rbuf, 100) == 0) 49 tst_res(TPASS, "failure of write() did not corrupt the file"); 50 else 51 tst_res(TFAIL, "failure of write() corrupted the file"); 52 53 SAFE_CLOSE(fd); 54} 55 56static void setup(void) 57{ 58 bad_addr = SAFE_MMAP(0, 1, PROT_NONE, 59 MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); 60 61 memset(wbuf, '0', 100); 62} 63 64static void cleanup(void) 65{ 66 if (fd > 0) 67 SAFE_CLOSE(fd); 68} 69 70static struct tst_test test = { 71 .test_all = verify_write, 72 .setup = setup, 73 .cleanup = cleanup, 74 .needs_tmpdir = 1, 75}; 76