1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * 3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001 4f08c3bdfSopenharmony_ci * 5f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify 6f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by 7f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or 8f08c3bdfSopenharmony_ci * (at your option) any later version. 9f08c3bdfSopenharmony_ci * 10f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 11f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 12f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13f08c3bdfSopenharmony_ci * the GNU General Public License for more details. 14f08c3bdfSopenharmony_ci * 15f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License 16f08c3bdfSopenharmony_ci * along with this program; if not, write to the Free Software 17f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18f08c3bdfSopenharmony_ci */ 19f08c3bdfSopenharmony_ci 20f08c3bdfSopenharmony_ci/* 21f08c3bdfSopenharmony_ci * NAME 22f08c3bdfSopenharmony_ci * writev02.c 23f08c3bdfSopenharmony_ci * 24f08c3bdfSopenharmony_ci * DESCRIPTION 25f08c3bdfSopenharmony_ci * In these testcases, writev() is called with partially valid data 26f08c3bdfSopenharmony_ci * to be written in a sparse file. 27f08c3bdfSopenharmony_ci * 28f08c3bdfSopenharmony_ci * ALGORITHM 29f08c3bdfSopenharmony_ci * The file is created and write() is called with valid buffer to write 30f08c3bdfSopenharmony_ci * at some 8k th offset. After that writev() will be called with invalid 31f08c3bdfSopenharmony_ci * vector. This should return EFAULT. And at the same time, try looking at 32f08c3bdfSopenharmony_ci * the 8kth offset whether the file is intact or not. 33f08c3bdfSopenharmony_ci * 34f08c3bdfSopenharmony_ci * USAGE: <for command-line> 35f08c3bdfSopenharmony_ci * writev02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] 36f08c3bdfSopenharmony_ci * where, -c n : Run n copies concurrently. 37f08c3bdfSopenharmony_ci * -e : Turn on errno logging. 38f08c3bdfSopenharmony_ci * -i n : Execute test n times. 39f08c3bdfSopenharmony_ci * -I x : Execute test for x seconds. 40f08c3bdfSopenharmony_ci * -P x : Pause for x seconds between iterations. 41f08c3bdfSopenharmony_ci * -t : Turn on syscall timing. 42f08c3bdfSopenharmony_ci * 43f08c3bdfSopenharmony_ci * History 44f08c3bdfSopenharmony_ci * 07/2001 John George 45f08c3bdfSopenharmony_ci * -Ported 46f08c3bdfSopenharmony_ci * 04/2002 wjhuie sigset cleanups 47f08c3bdfSopenharmony_ci * 48f08c3bdfSopenharmony_ci * Restrictions 49f08c3bdfSopenharmony_ci * None 50f08c3bdfSopenharmony_ci */ 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_ci#include <sys/types.h> 53f08c3bdfSopenharmony_ci#include <signal.h> 54f08c3bdfSopenharmony_ci#include <sys/uio.h> 55f08c3bdfSopenharmony_ci#include <fcntl.h> 56f08c3bdfSopenharmony_ci#include <memory.h> 57f08c3bdfSopenharmony_ci#include <errno.h> 58f08c3bdfSopenharmony_ci#include "test.h" 59f08c3bdfSopenharmony_ci#include <sys/mman.h> 60f08c3bdfSopenharmony_ci 61f08c3bdfSopenharmony_ci#define K_1 8192 62f08c3bdfSopenharmony_ci 63f08c3bdfSopenharmony_ci#define NBUFS 2 64f08c3bdfSopenharmony_ci#define CHUNK K_1 /* single chunk */ 65f08c3bdfSopenharmony_ci#define MAX_IOVEC 2 66f08c3bdfSopenharmony_ci#define DATA_FILE "writev_data_file" 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_cichar buf1[K_1]; 69f08c3bdfSopenharmony_cichar buf2[K_1]; 70f08c3bdfSopenharmony_ci 71f08c3bdfSopenharmony_cichar *bad_addr = 0; 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_cistruct iovec wr_iovec[MAX_IOVEC] = { 74f08c3bdfSopenharmony_ci {(caddr_t) - 1, CHUNK}, 75f08c3bdfSopenharmony_ci {NULL, 0}, 76f08c3bdfSopenharmony_ci}; 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_cichar name[K_1], f_name[K_1]; 79f08c3bdfSopenharmony_ci 80f08c3bdfSopenharmony_ciint fd[2], in_sighandler; 81f08c3bdfSopenharmony_cichar *buf_list[NBUFS]; 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_cichar *TCID = "writev02"; 84f08c3bdfSopenharmony_ciint TST_TOTAL = 1; 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_civoid sighandler(int); 87f08c3bdfSopenharmony_civoid l_seek(int, off_t, int); 88f08c3bdfSopenharmony_civoid setup(void); 89f08c3bdfSopenharmony_civoid cleanup(void); 90f08c3bdfSopenharmony_ci 91f08c3bdfSopenharmony_ciint main(int argc, char **argv) 92f08c3bdfSopenharmony_ci{ 93f08c3bdfSopenharmony_ci int lc; 94f08c3bdfSopenharmony_ci 95f08c3bdfSopenharmony_ci int nbytes; 96f08c3bdfSopenharmony_ci 97f08c3bdfSopenharmony_ci tst_parse_opts(argc, argv, NULL, NULL); 98f08c3bdfSopenharmony_ci 99f08c3bdfSopenharmony_ci setup(); 100f08c3bdfSopenharmony_ci 101f08c3bdfSopenharmony_ci for (lc = 0; TEST_LOOPING(lc); lc++) { 102f08c3bdfSopenharmony_ci 103f08c3bdfSopenharmony_ci tst_count = 0; 104f08c3bdfSopenharmony_ci 105f08c3bdfSopenharmony_ci buf_list[0] = buf1; 106f08c3bdfSopenharmony_ci buf_list[1] = buf2; 107f08c3bdfSopenharmony_ci 108f08c3bdfSopenharmony_ci memset(buf_list[0], 0, K_1); 109f08c3bdfSopenharmony_ci memset(buf_list[1], 0, K_1); 110f08c3bdfSopenharmony_ci 111f08c3bdfSopenharmony_ci fd[1] = -1; /* Invalid file descriptor */ 112f08c3bdfSopenharmony_ci 113f08c3bdfSopenharmony_ci if (signal(SIGTERM, sighandler) == SIG_ERR) 114f08c3bdfSopenharmony_ci tst_brkm(TFAIL | TERRNO, cleanup, 115f08c3bdfSopenharmony_ci "signal(SIGTERM, ..)"); 116f08c3bdfSopenharmony_ci 117f08c3bdfSopenharmony_ci if (signal(SIGPIPE, sighandler) == SIG_ERR) 118f08c3bdfSopenharmony_ci tst_brkm(TFAIL | TERRNO, cleanup, 119f08c3bdfSopenharmony_ci "signal(SIGPIPE, ..)"); 120f08c3bdfSopenharmony_ci 121f08c3bdfSopenharmony_ci if ((fd[0] = open(f_name, O_WRONLY | O_CREAT, 0666)) < 0) 122f08c3bdfSopenharmony_ci tst_brkm(TFAIL | TERRNO, cleanup, 123f08c3bdfSopenharmony_ci "open(.., O_WRONLY|O_CREAT, ..) failed"); 124f08c3bdfSopenharmony_ci else { 125f08c3bdfSopenharmony_ci l_seek(fd[0], K_1, 0); 126f08c3bdfSopenharmony_ci if ((nbytes = write(fd[0], buf_list[1], K_1)) != K_1) 127f08c3bdfSopenharmony_ci tst_brkm(TFAIL | TERRNO, cleanup, 128f08c3bdfSopenharmony_ci "write failed"); 129f08c3bdfSopenharmony_ci } 130f08c3bdfSopenharmony_ci 131f08c3bdfSopenharmony_ci if (close(fd[0]) == -1) 132f08c3bdfSopenharmony_ci tst_brkm(TFAIL | TERRNO, cleanup, "close failed"); 133f08c3bdfSopenharmony_ci 134f08c3bdfSopenharmony_ci if ((fd[0] = open(f_name, O_RDWR, 0666)) < 0) 135f08c3bdfSopenharmony_ci tst_brkm(TFAIL | TERRNO, cleanup, 136f08c3bdfSopenharmony_ci "open(.., O_RDWR, ..) failed"); 137f08c3bdfSopenharmony_ci /* 138f08c3bdfSopenharmony_ci * In this block we are trying to call writev() with invalid 139f08c3bdfSopenharmony_ci * vector to be written in a sparse file. This will return 140f08c3bdfSopenharmony_ci * EFAULT. At the same time, check should be made whether 141f08c3bdfSopenharmony_ci * the scheduled write() with valid data at 8k th offset is 142f08c3bdfSopenharmony_ci * done correctly or not. 143f08c3bdfSopenharmony_ci */ 144f08c3bdfSopenharmony_ci l_seek(fd[0], 0, 0); 145f08c3bdfSopenharmony_ci TEST(writev(fd[0], wr_iovec, 2)); 146f08c3bdfSopenharmony_ci if (TEST_RETURN < 0) { 147f08c3bdfSopenharmony_ci if (TEST_ERRNO == EFAULT) { 148f08c3bdfSopenharmony_ci tst_resm(TPASS, "Received EFAULT as expected"); 149f08c3bdfSopenharmony_ci } else if (TEST_ERRNO != EFAULT) { 150f08c3bdfSopenharmony_ci tst_resm(TFAIL, "Expected EFAULT, got %d", 151f08c3bdfSopenharmony_ci TEST_ERRNO); 152f08c3bdfSopenharmony_ci } 153f08c3bdfSopenharmony_ci l_seek(fd[0], K_1, 0); 154f08c3bdfSopenharmony_ci if ((nbytes = read(fd[0], buf_list[0], CHUNK)) != CHUNK) { 155f08c3bdfSopenharmony_ci tst_resm(TFAIL, "Expected nbytes = 64, got " 156f08c3bdfSopenharmony_ci "%d", nbytes); 157f08c3bdfSopenharmony_ci } else { 158f08c3bdfSopenharmony_ci if (memcmp(buf_list[0], buf_list[1], CHUNK) 159f08c3bdfSopenharmony_ci != 0) 160f08c3bdfSopenharmony_ci tst_resm(TFAIL, "Error: writev() " 161f08c3bdfSopenharmony_ci "over wrote %s", f_name); 162f08c3bdfSopenharmony_ci } 163f08c3bdfSopenharmony_ci } else 164f08c3bdfSopenharmony_ci tst_resm(TFAIL, "Error writev returned a positive " 165f08c3bdfSopenharmony_ci "value"); 166f08c3bdfSopenharmony_ci } 167f08c3bdfSopenharmony_ci cleanup(); 168f08c3bdfSopenharmony_ci tst_exit(); 169f08c3bdfSopenharmony_ci} 170f08c3bdfSopenharmony_ci 171f08c3bdfSopenharmony_civoid setup(void) 172f08c3bdfSopenharmony_ci{ 173f08c3bdfSopenharmony_ci tst_sig(FORK, sighandler, cleanup); 174f08c3bdfSopenharmony_ci 175f08c3bdfSopenharmony_ci TEST_PAUSE; 176f08c3bdfSopenharmony_ci 177f08c3bdfSopenharmony_ci tst_tmpdir(); 178f08c3bdfSopenharmony_ci 179f08c3bdfSopenharmony_ci strcpy(name, DATA_FILE); 180f08c3bdfSopenharmony_ci sprintf(f_name, "%s.%d", name, getpid()); 181f08c3bdfSopenharmony_ci 182f08c3bdfSopenharmony_ci bad_addr = mmap(0, 1, PROT_NONE, 183f08c3bdfSopenharmony_ci MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); 184f08c3bdfSopenharmony_ci if (bad_addr == MAP_FAILED) 185f08c3bdfSopenharmony_ci tst_brkm(TBROK | TERRNO, cleanup, "mmap failed"); 186f08c3bdfSopenharmony_ci wr_iovec[0].iov_base = bad_addr; 187f08c3bdfSopenharmony_ci 188f08c3bdfSopenharmony_ci} 189f08c3bdfSopenharmony_ci 190f08c3bdfSopenharmony_civoid cleanup(void) 191f08c3bdfSopenharmony_ci{ 192f08c3bdfSopenharmony_ci close(fd[0]); 193f08c3bdfSopenharmony_ci close(fd[1]); 194f08c3bdfSopenharmony_ci 195f08c3bdfSopenharmony_ci if (munmap(bad_addr, 1) == -1) 196f08c3bdfSopenharmony_ci tst_resm(TWARN | TERRNO, "unmap failed"); 197f08c3bdfSopenharmony_ci if (unlink(f_name) == -1) 198f08c3bdfSopenharmony_ci tst_resm(TWARN | TERRNO, "unlink failed"); 199f08c3bdfSopenharmony_ci 200f08c3bdfSopenharmony_ci tst_rmdir(); 201f08c3bdfSopenharmony_ci 202f08c3bdfSopenharmony_ci} 203f08c3bdfSopenharmony_ci 204f08c3bdfSopenharmony_civoid sighandler(int sig) 205f08c3bdfSopenharmony_ci{ 206f08c3bdfSopenharmony_ci switch (sig) { 207f08c3bdfSopenharmony_ci case SIGTERM: 208f08c3bdfSopenharmony_ci break; 209f08c3bdfSopenharmony_ci case SIGPIPE: 210f08c3bdfSopenharmony_ci ++in_sighandler; 211f08c3bdfSopenharmony_ci return; 212f08c3bdfSopenharmony_ci default: 213f08c3bdfSopenharmony_ci tst_resm(TBROK, "sighandler received invalid signal : %d", sig); 214f08c3bdfSopenharmony_ci break; 215f08c3bdfSopenharmony_ci } 216f08c3bdfSopenharmony_ci 217f08c3bdfSopenharmony_ci if (unlink(f_name) == -1 && errno != ENOENT) 218f08c3bdfSopenharmony_ci tst_resm(TFAIL | TERRNO, "unlink failed"); 219f08c3bdfSopenharmony_ci} 220f08c3bdfSopenharmony_ci 221f08c3bdfSopenharmony_ci/* 222f08c3bdfSopenharmony_ci * l_seek() 223f08c3bdfSopenharmony_ci * Wrap around for regular lseek function for giving error message 224f08c3bdfSopenharmony_ci */ 225f08c3bdfSopenharmony_civoid l_seek(int fdesc, off_t offset, int whence) 226f08c3bdfSopenharmony_ci{ 227f08c3bdfSopenharmony_ci if (lseek(fdesc, offset, whence) == -1) 228f08c3bdfSopenharmony_ci tst_resm(TBROK | TERRNO, "lseek failed"); 229f08c3bdfSopenharmony_ci} 230