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 * writev05.c 23f08c3bdfSopenharmony_ci * 24f08c3bdfSopenharmony_ci * DESCRIPTION 25f08c3bdfSopenharmony_ci * These testcases are written to test writev() on sparse files. This 26f08c3bdfSopenharmony_ci * is same as writev02.c. But the initial write() with valid data is 27f08c3bdfSopenharmony_ci * done at the beginning of the file. 28f08c3bdfSopenharmony_ci * 29f08c3bdfSopenharmony_ci * USAGE: <for command-line> 30f08c3bdfSopenharmony_ci * writev05 [-c n] [-e] [-i n] [-I x] [-P x] [-t] 31f08c3bdfSopenharmony_ci * where, -c n : Run n copies concurrently. 32f08c3bdfSopenharmony_ci * -e : Turn on errno logging. 33f08c3bdfSopenharmony_ci * -i n : Execute test n times. 34f08c3bdfSopenharmony_ci * -I x : Execute test for x seconds. 35f08c3bdfSopenharmony_ci * -P x : Pause for x seconds between iterations. 36f08c3bdfSopenharmony_ci * -t : Turn on syscall timing. 37f08c3bdfSopenharmony_ci * 38f08c3bdfSopenharmony_ci * History 39f08c3bdfSopenharmony_ci * 07/2001 John George 40f08c3bdfSopenharmony_ci * -Ported 41f08c3bdfSopenharmony_ci * 04/2002 wjhuie sigset cleanups 42f08c3bdfSopenharmony_ci * 43f08c3bdfSopenharmony_ci * Restrictions 44f08c3bdfSopenharmony_ci * NONE 45f08c3bdfSopenharmony_ci */ 46f08c3bdfSopenharmony_ci 47f08c3bdfSopenharmony_ci#include <sys/types.h> 48f08c3bdfSopenharmony_ci#include <signal.h> 49f08c3bdfSopenharmony_ci#include <sys/uio.h> 50f08c3bdfSopenharmony_ci#include <fcntl.h> 51f08c3bdfSopenharmony_ci#include <memory.h> 52f08c3bdfSopenharmony_ci#include <errno.h> 53f08c3bdfSopenharmony_ci#include "test.h" 54f08c3bdfSopenharmony_ci#include <sys/mman.h> 55f08c3bdfSopenharmony_ci 56f08c3bdfSopenharmony_ci#define K_1 8192 57f08c3bdfSopenharmony_ci 58f08c3bdfSopenharmony_ci#define NBUFS 2 59f08c3bdfSopenharmony_ci#define CHUNK K_1 /* single chunk */ 60f08c3bdfSopenharmony_ci#define MAX_IOVEC 2 61f08c3bdfSopenharmony_ci#define DATA_FILE "writev_data_file" 62f08c3bdfSopenharmony_ci 63f08c3bdfSopenharmony_cichar buf1[K_1]; 64f08c3bdfSopenharmony_cichar buf2[K_1]; 65f08c3bdfSopenharmony_cichar buf3[K_1]; 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_cichar *bad_addr = 0; 68f08c3bdfSopenharmony_ci 69f08c3bdfSopenharmony_cistruct iovec wr_iovec[MAX_IOVEC] = { 70f08c3bdfSopenharmony_ci {(caddr_t) - 1, CHUNK}, 71f08c3bdfSopenharmony_ci {NULL, 0} 72f08c3bdfSopenharmony_ci}; 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_cichar name[K_1], f_name[K_1]; 75f08c3bdfSopenharmony_ciint fd[2], in_sighandler; 76f08c3bdfSopenharmony_cichar *buf_list[NBUFS]; 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_cichar *TCID = "writev05"; 79f08c3bdfSopenharmony_ciint TST_TOTAL = 1; 80f08c3bdfSopenharmony_ci 81f08c3bdfSopenharmony_civoid sighandler(int); 82f08c3bdfSopenharmony_cilong l_seek(int, long, int); 83f08c3bdfSopenharmony_civoid setup(void); 84f08c3bdfSopenharmony_civoid cleanup(void); 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_ci#if !defined(UCLINUX) 87f08c3bdfSopenharmony_ci 88f08c3bdfSopenharmony_ciint main(int argc, char **argv) 89f08c3bdfSopenharmony_ci{ 90f08c3bdfSopenharmony_ci int lc; 91f08c3bdfSopenharmony_ci 92f08c3bdfSopenharmony_ci int nbytes; 93f08c3bdfSopenharmony_ci 94f08c3bdfSopenharmony_ci tst_parse_opts(argc, argv, NULL, NULL); 95f08c3bdfSopenharmony_ci 96f08c3bdfSopenharmony_ci setup(); /* set "tstdir", and "testfile" vars */ 97f08c3bdfSopenharmony_ci 98f08c3bdfSopenharmony_ci /* The following loop checks looping state if -i option given */ 99f08c3bdfSopenharmony_ci for (lc = 0; TEST_LOOPING(lc); lc++) { 100f08c3bdfSopenharmony_ci 101f08c3bdfSopenharmony_ci /* reset tst_count in case we are looping */ 102f08c3bdfSopenharmony_ci tst_count = 0; 103f08c3bdfSopenharmony_ci 104f08c3bdfSopenharmony_ci buf_list[0] = buf1; 105f08c3bdfSopenharmony_ci buf_list[1] = buf2; 106f08c3bdfSopenharmony_ci 107f08c3bdfSopenharmony_ci fd[1] = -1; /* Invalid file descriptor */ 108f08c3bdfSopenharmony_ci 109f08c3bdfSopenharmony_ci if (signal(SIGTERM, sighandler) == SIG_ERR) { 110f08c3bdfSopenharmony_ci perror("signal"); 111f08c3bdfSopenharmony_ci tst_resm(TFAIL, "signal() SIGTERM FAILED"); 112f08c3bdfSopenharmony_ci cleanup(); 113f08c3bdfSopenharmony_ci } 114f08c3bdfSopenharmony_ci 115f08c3bdfSopenharmony_ci if (signal(SIGPIPE, sighandler) == SIG_ERR) { 116f08c3bdfSopenharmony_ci perror("signal"); 117f08c3bdfSopenharmony_ci tst_resm(TFAIL, "signal() SIGPIPE FAILED"); 118f08c3bdfSopenharmony_ci cleanup(); 119f08c3bdfSopenharmony_ci } 120f08c3bdfSopenharmony_ci 121f08c3bdfSopenharmony_ci /* Fill the buf_list[0] and buf_list[1] with 0 zeros */ 122f08c3bdfSopenharmony_ci memset(buf_list[0], 0, K_1); 123f08c3bdfSopenharmony_ci memset(buf_list[1], 0, K_1); 124f08c3bdfSopenharmony_ci 125f08c3bdfSopenharmony_ci if ((fd[0] = open(f_name, O_WRONLY | O_CREAT, 0666)) < 0) { 126f08c3bdfSopenharmony_ci tst_resm(TFAIL, "open(2) failed: fname = %s, " 127f08c3bdfSopenharmony_ci "errno = %d", f_name, errno); 128f08c3bdfSopenharmony_ci cleanup(); 129f08c3bdfSopenharmony_ci } else { 130f08c3bdfSopenharmony_ci if ((nbytes = write(fd[0], buf_list[1], K_1)) != K_1) { 131f08c3bdfSopenharmony_ci tst_resm(TFAIL, "write(2) failed: nbytes " 132f08c3bdfSopenharmony_ci "= %d, errno = %d", nbytes, errno); 133f08c3bdfSopenharmony_ci cleanup(); 134f08c3bdfSopenharmony_ci } 135f08c3bdfSopenharmony_ci } 136f08c3bdfSopenharmony_ci 137f08c3bdfSopenharmony_ci if (close(fd[0]) < 0) { 138f08c3bdfSopenharmony_ci tst_resm(TFAIL, "close failed: errno = %d", errno); 139f08c3bdfSopenharmony_ci cleanup(); 140f08c3bdfSopenharmony_ci } 141f08c3bdfSopenharmony_ci 142f08c3bdfSopenharmony_ci if ((fd[0] = open(f_name, O_RDWR, 0666)) < 0) { 143f08c3bdfSopenharmony_ci tst_resm(TFAIL, "open failed: fname = %s, errno = %d", 144f08c3bdfSopenharmony_ci f_name, errno); 145f08c3bdfSopenharmony_ci cleanup(); 146f08c3bdfSopenharmony_ci } 147f08c3bdfSopenharmony_ci 148f08c3bdfSopenharmony_ci /* 149f08c3bdfSopenharmony_ci * In this block we are trying to call writev() with invalid 150f08c3bdfSopenharmony_ci * vector to be written in a sparse file. This will return 151f08c3bdfSopenharmony_ci * EFAULT. At the same time, check should be made whether 152f08c3bdfSopenharmony_ci * the scheduled write() with valid data is done correctly 153f08c3bdfSopenharmony_ci * or not. 154f08c3bdfSopenharmony_ci */ 155f08c3bdfSopenharmony_ci l_seek(fd[0], 0, 0); 156f08c3bdfSopenharmony_ci TEST(writev(fd[0], wr_iovec, 2)); 157f08c3bdfSopenharmony_ci if (TEST_RETURN < 0) { 158f08c3bdfSopenharmony_ci if (TEST_ERRNO == EFAULT) { 159f08c3bdfSopenharmony_ci tst_resm(TPASS, "Received EFAULT as expected"); 160f08c3bdfSopenharmony_ci } else { 161f08c3bdfSopenharmony_ci tst_resm(TFAIL, "Expected EFAULT, got %d", 162f08c3bdfSopenharmony_ci TEST_ERRNO); 163f08c3bdfSopenharmony_ci } 164f08c3bdfSopenharmony_ci l_seek(fd[0], K_1, 0); 165f08c3bdfSopenharmony_ci if ((nbytes = read(fd[0], buf_list[0], CHUNK)) != 0) { 166f08c3bdfSopenharmony_ci tst_resm(TFAIL, "Expected nbytes = 0, got " 167f08c3bdfSopenharmony_ci "%d", nbytes); 168f08c3bdfSopenharmony_ci } 169f08c3bdfSopenharmony_ci } else { 170f08c3bdfSopenharmony_ci tst_resm(TFAIL, "Error writev returned a positive " 171f08c3bdfSopenharmony_ci "value"); 172f08c3bdfSopenharmony_ci } 173f08c3bdfSopenharmony_ci } 174f08c3bdfSopenharmony_ci close(fd[0]); 175f08c3bdfSopenharmony_ci close(fd[1]); 176f08c3bdfSopenharmony_ci cleanup(); 177f08c3bdfSopenharmony_ci tst_exit(); 178f08c3bdfSopenharmony_ci 179f08c3bdfSopenharmony_ci} 180f08c3bdfSopenharmony_ci 181f08c3bdfSopenharmony_ci#else 182f08c3bdfSopenharmony_ci 183f08c3bdfSopenharmony_ciint main(void) 184f08c3bdfSopenharmony_ci{ 185f08c3bdfSopenharmony_ci tst_resm(TINFO, "test is not available on uClinux"); 186f08c3bdfSopenharmony_ci tst_exit(); 187f08c3bdfSopenharmony_ci} 188f08c3bdfSopenharmony_ci 189f08c3bdfSopenharmony_ci#endif /* if !defined(UCLINUX) */ 190f08c3bdfSopenharmony_ci 191f08c3bdfSopenharmony_ci/* 192f08c3bdfSopenharmony_ci * setup() 193f08c3bdfSopenharmony_ci * performs all ONE TIME setup for this test 194f08c3bdfSopenharmony_ci */ 195f08c3bdfSopenharmony_civoid setup(void) 196f08c3bdfSopenharmony_ci{ 197f08c3bdfSopenharmony_ci 198f08c3bdfSopenharmony_ci tst_sig(FORK, DEF_HANDLER, cleanup); 199f08c3bdfSopenharmony_ci 200f08c3bdfSopenharmony_ci /* Pause if that option was specified. 201f08c3bdfSopenharmony_ci * TEST_PAUSE contains the code to fork the test with the -i option. 202f08c3bdfSopenharmony_ci * You want to make sure you do this before you create your temporary 203f08c3bdfSopenharmony_ci * directory. 204f08c3bdfSopenharmony_ci */ 205f08c3bdfSopenharmony_ci TEST_PAUSE; 206f08c3bdfSopenharmony_ci 207f08c3bdfSopenharmony_ci /* Create a unique temporary directory and chdir() to it. */ 208f08c3bdfSopenharmony_ci tst_tmpdir(); 209f08c3bdfSopenharmony_ci 210f08c3bdfSopenharmony_ci strcpy(name, DATA_FILE); 211f08c3bdfSopenharmony_ci sprintf(f_name, "%s.%d", name, getpid()); 212f08c3bdfSopenharmony_ci 213f08c3bdfSopenharmony_ci bad_addr = mmap(0, 1, PROT_NONE, 214f08c3bdfSopenharmony_ci MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); 215f08c3bdfSopenharmony_ci if (bad_addr == MAP_FAILED) { 216f08c3bdfSopenharmony_ci printf("mmap failed\n"); 217f08c3bdfSopenharmony_ci } 218f08c3bdfSopenharmony_ci wr_iovec[0].iov_base = bad_addr; 219f08c3bdfSopenharmony_ci 220f08c3bdfSopenharmony_ci} 221f08c3bdfSopenharmony_ci 222f08c3bdfSopenharmony_ci/* 223f08c3bdfSopenharmony_ci * cleanup() 224f08c3bdfSopenharmony_ci * performs all ONE TIME cleanup for this test at 225f08c3bdfSopenharmony_ci * completion or premature exit 226f08c3bdfSopenharmony_ci */ 227f08c3bdfSopenharmony_civoid cleanup(void) 228f08c3bdfSopenharmony_ci{ 229f08c3bdfSopenharmony_ci 230f08c3bdfSopenharmony_ci if (unlink(f_name) < 0) { 231f08c3bdfSopenharmony_ci tst_resm(TFAIL, "unlink Failed--file = %s, errno = %d", 232f08c3bdfSopenharmony_ci f_name, errno); 233f08c3bdfSopenharmony_ci } 234f08c3bdfSopenharmony_ci tst_rmdir(); 235f08c3bdfSopenharmony_ci 236f08c3bdfSopenharmony_ci} 237f08c3bdfSopenharmony_ci 238f08c3bdfSopenharmony_ci/* 239f08c3bdfSopenharmony_ci * sighandler() 240f08c3bdfSopenharmony_ci * Signal handler for SIGTERM and SIGPIPE 241f08c3bdfSopenharmony_ci */ 242f08c3bdfSopenharmony_civoid sighandler(int sig) 243f08c3bdfSopenharmony_ci{ 244f08c3bdfSopenharmony_ci switch (sig) { 245f08c3bdfSopenharmony_ci case SIGTERM: 246f08c3bdfSopenharmony_ci break; 247f08c3bdfSopenharmony_ci case SIGPIPE: 248f08c3bdfSopenharmony_ci ++in_sighandler; 249f08c3bdfSopenharmony_ci return; 250f08c3bdfSopenharmony_ci default: 251f08c3bdfSopenharmony_ci tst_resm(TFAIL, "sighandler() received invalid signal " 252f08c3bdfSopenharmony_ci ": %d", sig); 253f08c3bdfSopenharmony_ci break; 254f08c3bdfSopenharmony_ci } 255f08c3bdfSopenharmony_ci 256f08c3bdfSopenharmony_ci if ((unlink(f_name) < 0) && (errno != ENOENT)) { 257f08c3bdfSopenharmony_ci tst_resm(TFAIL, "unlink Failed--file = %s, errno = %d", 258f08c3bdfSopenharmony_ci f_name, errno); 259f08c3bdfSopenharmony_ci cleanup(); 260f08c3bdfSopenharmony_ci } 261f08c3bdfSopenharmony_ci exit(sig); 262f08c3bdfSopenharmony_ci} 263f08c3bdfSopenharmony_ci 264f08c3bdfSopenharmony_ci/* 265f08c3bdfSopenharmony_ci * l_seek() 266f08c3bdfSopenharmony_ci * Wrap around for regular lseek() to give error message on failure 267f08c3bdfSopenharmony_ci */ 268f08c3bdfSopenharmony_cilong l_seek(int fdesc, long offset, int whence) 269f08c3bdfSopenharmony_ci{ 270f08c3bdfSopenharmony_ci if (lseek(fdesc, offset, whence) < 0) { 271f08c3bdfSopenharmony_ci tst_resm(TFAIL, "lseek Failed : errno = %d", errno); 272f08c3bdfSopenharmony_ci } 273f08c3bdfSopenharmony_ci return 0; 274f08c3bdfSopenharmony_ci} 275