1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * Copyright (C) 2007 Monakhov Dmitriy 3f08c3bdfSopenharmony_ci * 4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or modify 5f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by 6f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or 7f08c3bdfSopenharmony_ci * (at your option) any later version. 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful, 10f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 11f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 12f08c3bdfSopenharmony_ci * the GNU General Public License for more details. 13f08c3bdfSopenharmony_ci * 14f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License 15f08c3bdfSopenharmony_ci * along with this program; if not, write to the Free Software 16f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17f08c3bdfSopenharmony_ci */ 18f08c3bdfSopenharmony_ci 19f08c3bdfSopenharmony_ci/* 20f08c3bdfSopenharmony_ci * NAME 21f08c3bdfSopenharmony_ci * writev06.c 22f08c3bdfSopenharmony_ci * 23f08c3bdfSopenharmony_ci * DESCRIPTION 24f08c3bdfSopenharmony_ci * These testcases are written to test fault in pages readable 25f08c3bdfSopenharmony_ci * feature in generic write path. Because before actual write 26f08c3bdfSopenharmony_ci * kernel may want check is buffer passed is realy readable. 27f08c3bdfSopenharmony_ci * 28f08c3bdfSopenharmony_ci * USAGE: <for command-line> 29f08c3bdfSopenharmony_ci * writev06 [-c n] [-e] [-i n] [-I x] [-P x] [-t] 30f08c3bdfSopenharmony_ci * where, -c n : Run n copies concurrently. 31f08c3bdfSopenharmony_ci * -e : Turn on errno logging. 32f08c3bdfSopenharmony_ci * -i n : Execute test n times. 33f08c3bdfSopenharmony_ci * -I x : Execute test for x seconds. 34f08c3bdfSopenharmony_ci * -P x : Pause for x seconds between iterations. 35f08c3bdfSopenharmony_ci * -t : Turn on syscall timing. 36f08c3bdfSopenharmony_ci * 37f08c3bdfSopenharmony_ci * History 38f08c3bdfSopenharmony_ci * 39f08c3bdfSopenharmony_ci * Restrictions 40f08c3bdfSopenharmony_ci * NONE 41f08c3bdfSopenharmony_ci */ 42f08c3bdfSopenharmony_ci 43f08c3bdfSopenharmony_ci#include <sys/types.h> 44f08c3bdfSopenharmony_ci#include <signal.h> 45f08c3bdfSopenharmony_ci#include <sys/uio.h> 46f08c3bdfSopenharmony_ci#include <fcntl.h> 47f08c3bdfSopenharmony_ci#include <memory.h> 48f08c3bdfSopenharmony_ci#include <errno.h> 49f08c3bdfSopenharmony_ci#include "test.h" 50f08c3bdfSopenharmony_ci#include <sys/mman.h> 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_ci#define K_1 1024 53f08c3bdfSopenharmony_ci#define NBUFS 2 54f08c3bdfSopenharmony_ci#define MAX_IOVEC 2 55f08c3bdfSopenharmony_ci#define DATA_FILE "writev_data_file" 56f08c3bdfSopenharmony_ci 57f08c3bdfSopenharmony_ciint page_size; 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_cichar *good_addr[2] = { NULL, NULL }; 60f08c3bdfSopenharmony_cichar *bad_addr[2] = { NULL, NULL }; 61f08c3bdfSopenharmony_ci 62f08c3bdfSopenharmony_cistruct iovec wr_iovec[MAX_IOVEC] = { 63f08c3bdfSopenharmony_ci {(caddr_t) - 1, 1}, 64f08c3bdfSopenharmony_ci {(caddr_t) - 1, 1} 65f08c3bdfSopenharmony_ci}; 66f08c3bdfSopenharmony_ci 67f08c3bdfSopenharmony_cichar name[K_1], f_name[K_1]; 68f08c3bdfSopenharmony_ciint fd[2], in_sighandler; 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_cichar *TCID = "writev06"; 71f08c3bdfSopenharmony_ciint TST_TOTAL = 1; 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_civoid sighandler(int); 74f08c3bdfSopenharmony_civoid setup(void); 75f08c3bdfSopenharmony_civoid cleanup(void); 76f08c3bdfSopenharmony_ci 77f08c3bdfSopenharmony_ciint main(int argc, char **argv) 78f08c3bdfSopenharmony_ci{ 79f08c3bdfSopenharmony_ci int lc; 80f08c3bdfSopenharmony_ci 81f08c3bdfSopenharmony_ci tst_parse_opts(argc, argv, NULL, NULL); 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_ci setup(); /* set "tstdir", and "testfile" vars */ 84f08c3bdfSopenharmony_ci 85f08c3bdfSopenharmony_ci /* The following loop checks looping state if -i option given */ 86f08c3bdfSopenharmony_ci for (lc = 0; TEST_LOOPING(lc); lc++) { 87f08c3bdfSopenharmony_ci 88f08c3bdfSopenharmony_ci /* reset tst_count in case we are looping */ 89f08c3bdfSopenharmony_ci tst_count = 0; 90f08c3bdfSopenharmony_ci 91f08c3bdfSopenharmony_ci fd[1] = -1; /* Invalid file descriptor */ 92f08c3bdfSopenharmony_ci 93f08c3bdfSopenharmony_ci if (signal(SIGTERM, sighandler) == SIG_ERR) { 94f08c3bdfSopenharmony_ci perror("signal"); 95f08c3bdfSopenharmony_ci tst_resm(TFAIL, "signal() SIGTERM FAILED"); 96f08c3bdfSopenharmony_ci cleanup(); 97f08c3bdfSopenharmony_ci 98f08c3bdfSopenharmony_ci } 99f08c3bdfSopenharmony_ci 100f08c3bdfSopenharmony_ci if (signal(SIGPIPE, sighandler) == SIG_ERR) { 101f08c3bdfSopenharmony_ci perror("signal"); 102f08c3bdfSopenharmony_ci tst_resm(TFAIL, "signal() SIGPIPE FAILED"); 103f08c3bdfSopenharmony_ci cleanup(); 104f08c3bdfSopenharmony_ci 105f08c3bdfSopenharmony_ci } 106f08c3bdfSopenharmony_ci 107f08c3bdfSopenharmony_ci if ((fd[0] = open(f_name, O_WRONLY | O_CREAT, 0666)) < 0) { 108f08c3bdfSopenharmony_ci tst_resm(TFAIL, "open(2) failed: fname = %s, " 109f08c3bdfSopenharmony_ci "errno = %d", f_name, errno); 110f08c3bdfSopenharmony_ci cleanup(); 111f08c3bdfSopenharmony_ci 112f08c3bdfSopenharmony_ci } 113f08c3bdfSopenharmony_ci 114f08c3bdfSopenharmony_ci /* 115f08c3bdfSopenharmony_ci * Iovecs passed to writev points to valid (readable) regions, 116f08c3bdfSopenharmony_ci * so all bytes must be successfully written. 117f08c3bdfSopenharmony_ci */ 118f08c3bdfSopenharmony_ci TEST(writev(fd[0], wr_iovec, 2)); 119f08c3bdfSopenharmony_ci if (TEST_RETURN >= 0) { 120f08c3bdfSopenharmony_ci if (TEST_RETURN == 2) { 121f08c3bdfSopenharmony_ci tst_resm(TPASS, 122f08c3bdfSopenharmony_ci "writev returned %d as expected", 2); 123f08c3bdfSopenharmony_ci } else { 124f08c3bdfSopenharmony_ci tst_resm(TFAIL, "Expected nbytes = %d, got " 125f08c3bdfSopenharmony_ci "%ld", 2, TEST_RETURN); 126f08c3bdfSopenharmony_ci } 127f08c3bdfSopenharmony_ci } else { 128f08c3bdfSopenharmony_ci tst_resm(TFAIL | TTERRNO, 129f08c3bdfSopenharmony_ci "Error writev return value = %ld", 130f08c3bdfSopenharmony_ci TEST_RETURN); 131f08c3bdfSopenharmony_ci } 132f08c3bdfSopenharmony_ci } 133f08c3bdfSopenharmony_ci cleanup(); 134f08c3bdfSopenharmony_ci tst_exit(); 135f08c3bdfSopenharmony_ci 136f08c3bdfSopenharmony_ci} 137f08c3bdfSopenharmony_ci 138f08c3bdfSopenharmony_ci/* 139f08c3bdfSopenharmony_ci * setup() 140f08c3bdfSopenharmony_ci * performs all ONE TIME setup for this test 141f08c3bdfSopenharmony_ci */ 142f08c3bdfSopenharmony_civoid setup(void) 143f08c3bdfSopenharmony_ci{ 144f08c3bdfSopenharmony_ci 145f08c3bdfSopenharmony_ci tst_sig(FORK, DEF_HANDLER, cleanup); 146f08c3bdfSopenharmony_ci 147f08c3bdfSopenharmony_ci /* Pause if that option was specified. 148f08c3bdfSopenharmony_ci * TEST_PAUSE contains the code to fork the test with the -i option. 149f08c3bdfSopenharmony_ci * You want to make sure you do this before you create your temporary 150f08c3bdfSopenharmony_ci * directory. 151f08c3bdfSopenharmony_ci */ 152f08c3bdfSopenharmony_ci TEST_PAUSE; 153f08c3bdfSopenharmony_ci 154f08c3bdfSopenharmony_ci /* Create a unique temporary directory and chdir() to it. */ 155f08c3bdfSopenharmony_ci tst_tmpdir(); 156f08c3bdfSopenharmony_ci 157f08c3bdfSopenharmony_ci strcpy(name, DATA_FILE); 158f08c3bdfSopenharmony_ci sprintf(f_name, "%s.%d", name, getpid()); 159f08c3bdfSopenharmony_ci 160f08c3bdfSopenharmony_ci page_size = getpagesize(); 161f08c3bdfSopenharmony_ci 162f08c3bdfSopenharmony_ci /* Crate two readable and writeble mappings with non reabable 163f08c3bdfSopenharmony_ci * mapping around */ 164f08c3bdfSopenharmony_ci bad_addr[0] = mmap(NULL, page_size * 3, PROT_NONE, 165f08c3bdfSopenharmony_ci MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); 166f08c3bdfSopenharmony_ci if (bad_addr[0] == MAP_FAILED) 167f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "mmap failed for bad_addr[0]"); 168f08c3bdfSopenharmony_ci 169f08c3bdfSopenharmony_ci good_addr[0] = mmap(NULL, page_size, PROT_READ | PROT_WRITE, 170f08c3bdfSopenharmony_ci MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); 171f08c3bdfSopenharmony_ci if (good_addr[0] == MAP_FAILED) 172f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "mmap failed for good_addr[0]"); 173f08c3bdfSopenharmony_ci 174f08c3bdfSopenharmony_ci bad_addr[1] = mmap(NULL, page_size * 3, PROT_NONE, 175f08c3bdfSopenharmony_ci MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); 176f08c3bdfSopenharmony_ci if (bad_addr[1] == MAP_FAILED) 177f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "mmap failed for bad_addr[1]"); 178f08c3bdfSopenharmony_ci 179f08c3bdfSopenharmony_ci good_addr[1] = mmap(NULL, page_size, PROT_READ | PROT_WRITE, 180f08c3bdfSopenharmony_ci MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, 0, 0); 181f08c3bdfSopenharmony_ci if (good_addr[1] == MAP_FAILED) 182f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "mmap failed for good_addr[1]"); 183f08c3bdfSopenharmony_ci 184f08c3bdfSopenharmony_ci /* force page fault for writable mappings */ 185f08c3bdfSopenharmony_ci memset(good_addr[0], 'a', page_size); 186f08c3bdfSopenharmony_ci memset(good_addr[1], 'b', page_size); 187f08c3bdfSopenharmony_ci 188f08c3bdfSopenharmony_ci wr_iovec[0].iov_base = good_addr[0] + page_size - 1; 189f08c3bdfSopenharmony_ci wr_iovec[1].iov_base = good_addr[1] + page_size - 1; 190f08c3bdfSopenharmony_ci 191f08c3bdfSopenharmony_ci} 192f08c3bdfSopenharmony_ci 193f08c3bdfSopenharmony_ci/* 194f08c3bdfSopenharmony_ci * cleanup() 195f08c3bdfSopenharmony_ci * performs all ONE TIME cleanup for this test at 196f08c3bdfSopenharmony_ci * completion or premature exit 197f08c3bdfSopenharmony_ci */ 198f08c3bdfSopenharmony_civoid cleanup(void) 199f08c3bdfSopenharmony_ci{ 200f08c3bdfSopenharmony_ci 201f08c3bdfSopenharmony_ci close(fd[0]); 202f08c3bdfSopenharmony_ci 203f08c3bdfSopenharmony_ci if (unlink(f_name) < 0) { 204f08c3bdfSopenharmony_ci tst_resm(TFAIL, "unlink Failed--file = %s, errno = %d", 205f08c3bdfSopenharmony_ci f_name, errno); 206f08c3bdfSopenharmony_ci } 207f08c3bdfSopenharmony_ci tst_rmdir(); 208f08c3bdfSopenharmony_ci 209f08c3bdfSopenharmony_ci} 210f08c3bdfSopenharmony_ci 211f08c3bdfSopenharmony_ci/* 212f08c3bdfSopenharmony_ci * sighandler() 213f08c3bdfSopenharmony_ci * Signal handler for SIGTERM and SIGPIPE 214f08c3bdfSopenharmony_ci */ 215f08c3bdfSopenharmony_civoid sighandler(int sig) 216f08c3bdfSopenharmony_ci{ 217f08c3bdfSopenharmony_ci switch (sig) { 218f08c3bdfSopenharmony_ci case SIGTERM: 219f08c3bdfSopenharmony_ci break; 220f08c3bdfSopenharmony_ci case SIGPIPE: 221f08c3bdfSopenharmony_ci ++in_sighandler; 222f08c3bdfSopenharmony_ci return; 223f08c3bdfSopenharmony_ci default: 224f08c3bdfSopenharmony_ci tst_resm(TFAIL, "sighandler() received invalid signal " 225f08c3bdfSopenharmony_ci ": %d", sig); 226f08c3bdfSopenharmony_ci break; 227f08c3bdfSopenharmony_ci } 228f08c3bdfSopenharmony_ci 229f08c3bdfSopenharmony_ci if ((unlink(f_name) < 0) && (errno != ENOENT)) { 230f08c3bdfSopenharmony_ci tst_resm(TFAIL, "unlink Failed--file = %s, errno = %d", 231f08c3bdfSopenharmony_ci f_name, errno); 232f08c3bdfSopenharmony_ci cleanup(); 233f08c3bdfSopenharmony_ci 234f08c3bdfSopenharmony_ci } 235f08c3bdfSopenharmony_ci exit(sig); 236f08c3bdfSopenharmony_ci} 237