plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Test Name: pread02 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, |
| 25 | * 1) pread() fails when attempted to read from an unnamed pipe. |
| 26 | * 2) pread() fails if the specified offset position was invalid. |
| 27 | * |
| 28 | * Expected Result: |
| 29 | * 1) pread() should return -1 and set errno to ESPIPE. |
| 30 | * 2) pread() should return -1 and set errno to EINVAL. |
| 31 | * |
| 32 | * Algorithm: |
| 33 | * Setup: |
| 34 | * Setup signal handling. |
| 35 | * Create a temporary directory. |
| 36 | * Pause for SIGUSR1 if option specified. |
| 37 | * |
| 38 | * Test: |
| 39 | * Loop if the proper options are given. |
| 40 | * Execute system call |
| 41 | * Check return code, if system call failed (return=-1) |
| 42 | * if errno set == expected errno |
| 43 | * Issue sys call fails with expected return value and errno. |
| 44 | * Otherwise, |
| 45 | * Issue sys call fails with unexpected errno. |
| 46 | * Otherwise, |
| 47 | * Issue sys call returns unexpected value. |
| 48 | * |
| 49 | * Cleanup: |
| 50 | * Print errno log and/or timing stats if options given |
| 51 | * Delete the temporary directory(s)/file(s) created. |
| 52 | * |
| 53 | * Usage: <for command-line> |
| 54 | * pread02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 55 | * where, -c n : Run n copies concurrently. |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 56 | * -i n : Execute test n times. |
| 57 | * -I x : Execute test for x seconds. |
| 58 | * -P x : Pause for x seconds between iterations. |
| 59 | * -t : Turn on syscall timing. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | * |
| 61 | * HISTORY |
| 62 | * 07/2001 Ported by Wayne Boyer |
| 63 | * |
| 64 | * RESTRICTIONS: |
| 65 | * None. |
| 66 | */ |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 67 | |
| 68 | #define _XOPEN_SOURCE 500 |
| 69 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | #include <errno.h> |
| 71 | #include <unistd.h> |
| 72 | #include <fcntl.h> |
| 73 | |
| 74 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
| 76 | #define TEMPFILE "pread_file" |
| 77 | #define K1 1024 |
| 78 | #define NBUFS 4 |
| 79 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 80 | char *TCID = "pread02"; |
| 81 | int TST_TOTAL = 2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | |
| 83 | char *write_buf[NBUFS]; /* buffer to hold data to be written */ |
| 84 | char *read_buf[NBUFS]; /* buffer to hold data read from file */ |
| 85 | int pfd[2]; /* pair of file descriptors */ |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 86 | int fd1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | |
| 88 | void setup(); /* Main setup function of test */ |
| 89 | void cleanup(); /* cleanup function for the test */ |
| 90 | int setup1(); /* setup function for test #1 */ |
| 91 | int setup2(); /* setup function for test #2 */ |
| 92 | int no_setup(); |
| 93 | void init_buffers(); /* function to initialize/allocate buffers */ |
| 94 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | struct test_case_t { /* test case struct. to hold ref. test cond's */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | int fd; |
| 97 | size_t nb; |
| 98 | off_t offst; |
| 99 | char *desc; |
| 100 | int exp_errno; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | int (*setupfunc) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | } Test_cases[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | { |
| 104 | 1, K1, 0, "file descriptor is a PIPE or FIFO", ESPIPE, setup1}, { |
| 105 | 2, K1, -1, "specified offset is -ve or invalid", EINVAL, setup2}, { |
| 106 | 0, 0, 0, NULL, 0, no_setup} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 109 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 111 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 112 | const char *msg; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 113 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | int fildes; /* file descriptor of test file */ |
| 115 | size_t nbytes; /* no. of bytes to be written */ |
| 116 | off_t offset; /* offset position in the specified file */ |
| 117 | char *test_desc; /* test specific error message */ |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 118 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 119 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 120 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | setup(); |
| 123 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 125 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 126 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | |
| 128 | /* loop through the test cases */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | for (i = 0; Test_cases[i].desc != NULL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | fildes = Test_cases[i].fd; |
| 131 | test_desc = Test_cases[i].desc; |
| 132 | nbytes = Test_cases[i].nb; |
| 133 | offset = Test_cases[i].offst; |
| 134 | |
| 135 | if (fildes == 1) { |
| 136 | fildes = pfd[0]; |
| 137 | } else if (fildes == 2) { |
| 138 | fildes = fd1; |
| 139 | } |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 140 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | /* |
| 142 | * Call pread() with the specified file descriptor, |
| 143 | * no. of bytes to be read from specified offset. |
| 144 | * and verify that call should fail with appropriate |
| 145 | * errno set. |
| 146 | */ |
| 147 | TEST(pread(fildes, read_buf[0], nbytes, offset)); |
| 148 | |
| 149 | /* Check for the return code of pread() */ |
| 150 | if (TEST_RETURN != -1) { |
plars | 24acd6e | 2002-02-20 23:45:31 +0000 | [diff] [blame] | 151 | tst_brkm(TFAIL, cleanup, "pread() returned " |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 152 | "%ld, expected -1, errno:%d", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 153 | TEST_RETURN, Test_cases[i].exp_errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | } |
| 155 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | /* |
| 157 | * Verify whether expected errno is set. |
| 158 | */ |
| 159 | if (TEST_ERRNO == Test_cases[i].exp_errno) { |
| 160 | tst_resm(TPASS, "pread() fails, %s, errno:%d", |
| 161 | test_desc, TEST_ERRNO); |
| 162 | } else { |
| 163 | tst_resm(TFAIL, "pread() fails, %s, unexpected " |
| 164 | "errno:%d, expected:%d", test_desc, |
| 165 | TEST_ERRNO, Test_cases[i].exp_errno); |
| 166 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 167 | } |
| 168 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | cleanup(); |
| 171 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 172 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* |
| 176 | * setup() - performs all ONE TIME setup for this test. |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 177 | * Initialize/allocate write buffer. |
| 178 | * Call individual setup function. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 180 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 182 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 185 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 186 | TEST_PAUSE; |
| 187 | |
| 188 | /* Allocate/Initialize the read/write buffer with known data */ |
| 189 | init_buffers(); |
| 190 | |
| 191 | /* Call individual setup functions */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 192 | for (i = 0; Test_cases[i].desc != NULL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 193 | Test_cases[i].setupfunc(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * no_setup() - This function simply returns. |
| 199 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 200 | int no_setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 201 | { |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * setup1() - setup function for a test condition for which pread() |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 207 | * returns -ve value and sets errno to ESPIPE. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | * |
| 209 | * Create an unnamed pipe using pipe(). |
| 210 | * Write some known data to the write end of the pipe. |
| 211 | * return 0. |
| 212 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 213 | int setup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | { |
| 215 | /* Create a pair of unnamed pipe */ |
| 216 | if (pipe(pfd) < 0) { |
| 217 | tst_brkm(TBROK, cleanup, "pipe() failed to create pair of " |
| 218 | "pipe, error:%d", errno); |
| 219 | } |
| 220 | |
| 221 | /* Write known data (0's) of K1 bytes */ |
| 222 | if (write(pfd[1], write_buf[0], K1) != K1) { |
| 223 | tst_brkm(TBROK, cleanup, "write to pipe failed: errno=%d : %s", |
| 224 | errno, strerror(errno)); |
| 225 | } |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * setup2 - setup function for a test condition for which pread() |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 232 | * returns -ve value and sets errno to EINVAL. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 233 | * |
| 234 | * Create a temporary directory and a file under it. |
| 235 | * return 0. |
| 236 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 237 | int setup2(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 238 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 239 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 240 | tst_tmpdir(); |
| 241 | |
| 242 | /* Creat a temporary file used for mapping */ |
| 243 | if ((fd1 = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) { |
| 244 | tst_brkm(TBROK, cleanup, "open() on %s Failed, errno=%d : %s", |
| 245 | TEMPFILE, errno, strerror(errno)); |
| 246 | } |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * init_buffers() - allocate/Initialize write_buf array. |
| 253 | * |
| 254 | * Allocate read/write buffer. |
| 255 | * Fill the write buffer with the following data like, |
| 256 | * write_buf[0] has 0's, write_buf[1] has 1's, write_buf[2] has 2's |
| 257 | * write_buf[3] has 3's. |
| 258 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 259 | void init_buffers(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 260 | { |
| 261 | int count; /* counter variable for loop */ |
| 262 | |
| 263 | /* Allocate and Initialize write buffer with known data */ |
| 264 | for (count = 0; count < NBUFS; count++) { |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 265 | write_buf[count] = malloc(K1); |
| 266 | read_buf[count] = malloc(K1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 267 | |
| 268 | if ((write_buf[count] == NULL) || (read_buf[count] == NULL)) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 269 | tst_brkm(TBROK, NULL, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 270 | "malloc() failed on read/write buffers"); |
| 271 | } |
| 272 | memset(write_buf[count], count, K1); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 278 | * completion or premature exit. |
| 279 | * |
| 280 | * Deallocate the memory allocated to read/write buffers. |
| 281 | * Close the temporary file. |
| 282 | * Remove the temporary directory created. |
| 283 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 284 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 285 | { |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 286 | int count; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 287 | |
| 288 | /* Free the memory allocated for the read/write buffer */ |
| 289 | for (count = 0; count < NBUFS; count++) { |
| 290 | free(write_buf[count]); |
| 291 | free(read_buf[count]); |
| 292 | } |
| 293 | |
| 294 | /* Close the temporary file created in setup2 */ |
| 295 | if (close(fd1) < 0) { |
| 296 | tst_brkm(TBROK, NULL, "close() on %s Failed, errno=%d : %s", |
| 297 | TEMPFILE, errno, strerror(errno)); |
| 298 | } |
| 299 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 300 | tst_rmdir(); |
plars | 24acd6e | 2002-02-20 23:45:31 +0000 | [diff] [blame] | 301 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 302 | } |