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