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" |
| 75 | #include "usctest.h" |
| 76 | |
| 77 | #define TEMPFILE "pread_file" |
| 78 | #define K1 1024 |
| 79 | #define NBUFS 4 |
| 80 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 81 | char *TCID = "pread02"; |
| 82 | int TST_TOTAL = 2; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
| 84 | char *write_buf[NBUFS]; /* buffer to hold data to be written */ |
| 85 | char *read_buf[NBUFS]; /* buffer to hold data read from file */ |
| 86 | int pfd[2]; /* pair of file descriptors */ |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 87 | int fd1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
| 89 | void setup(); /* Main setup function of test */ |
| 90 | void cleanup(); /* cleanup function for the test */ |
| 91 | int setup1(); /* setup function for test #1 */ |
| 92 | int setup2(); /* setup function for test #2 */ |
| 93 | int no_setup(); |
| 94 | void init_buffers(); /* function to initialize/allocate buffers */ |
| 95 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 96 | struct test_case_t { /* test case struct. to hold ref. test cond's */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | int fd; |
| 98 | size_t nb; |
| 99 | off_t offst; |
| 100 | char *desc; |
| 101 | int exp_errno; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 102 | int (*setupfunc) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | } Test_cases[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 104 | { |
| 105 | 1, K1, 0, "file descriptor is a PIPE or FIFO", ESPIPE, setup1}, { |
| 106 | 2, K1, -1, "specified offset is -ve or invalid", EINVAL, setup2}, { |
| 107 | 0, 0, 0, NULL, 0, no_setup} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 110 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 112 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 113 | const char *msg; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 114 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | int fildes; /* file descriptor of test file */ |
| 116 | size_t nbytes; /* no. of bytes to be written */ |
| 117 | off_t offset; /* offset position in the specified file */ |
| 118 | char *test_desc; /* test specific error message */ |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 119 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 120 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 121 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | setup(); |
| 124 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 126 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 127 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | |
| 129 | /* loop through the test cases */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 130 | for (i = 0; Test_cases[i].desc != NULL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | fildes = Test_cases[i].fd; |
| 132 | test_desc = Test_cases[i].desc; |
| 133 | nbytes = Test_cases[i].nb; |
| 134 | offset = Test_cases[i].offst; |
| 135 | |
| 136 | if (fildes == 1) { |
| 137 | fildes = pfd[0]; |
| 138 | } else if (fildes == 2) { |
| 139 | fildes = fd1; |
| 140 | } |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 141 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | /* |
| 143 | * Call pread() with the specified file descriptor, |
| 144 | * no. of bytes to be read from specified offset. |
| 145 | * and verify that call should fail with appropriate |
| 146 | * errno set. |
| 147 | */ |
| 148 | TEST(pread(fildes, read_buf[0], nbytes, offset)); |
| 149 | |
| 150 | /* Check for the return code of pread() */ |
| 151 | if (TEST_RETURN != -1) { |
plars | 24acd6e | 2002-02-20 23:45:31 +0000 | [diff] [blame] | 152 | tst_brkm(TFAIL, cleanup, "pread() returned " |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 153 | "%ld, expected -1, errno:%d", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 154 | TEST_RETURN, Test_cases[i].exp_errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | } |
| 156 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | /* |
| 158 | * Verify whether expected errno is set. |
| 159 | */ |
| 160 | if (TEST_ERRNO == Test_cases[i].exp_errno) { |
| 161 | tst_resm(TPASS, "pread() fails, %s, errno:%d", |
| 162 | test_desc, TEST_ERRNO); |
| 163 | } else { |
| 164 | tst_resm(TFAIL, "pread() fails, %s, unexpected " |
| 165 | "errno:%d, expected:%d", test_desc, |
| 166 | TEST_ERRNO, Test_cases[i].exp_errno); |
| 167 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 171 | cleanup(); |
| 172 | |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 173 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* |
| 177 | * setup() - performs all ONE TIME setup for this test. |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 178 | * Initialize/allocate write buffer. |
| 179 | * Call individual setup function. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 180 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 181 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 183 | int i; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 186 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | TEST_PAUSE; |
| 188 | |
| 189 | /* Allocate/Initialize the read/write buffer with known data */ |
| 190 | init_buffers(); |
| 191 | |
| 192 | /* Call individual setup functions */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 193 | for (i = 0; Test_cases[i].desc != NULL; i++) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 194 | Test_cases[i].setupfunc(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * no_setup() - This function simply returns. |
| 200 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 201 | int no_setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | { |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * setup1() - setup function for a test condition for which pread() |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 208 | * returns -ve value and sets errno to ESPIPE. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 209 | * |
| 210 | * Create an unnamed pipe using pipe(). |
| 211 | * Write some known data to the write end of the pipe. |
| 212 | * return 0. |
| 213 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 214 | int setup1(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 215 | { |
| 216 | /* Create a pair of unnamed pipe */ |
| 217 | if (pipe(pfd) < 0) { |
| 218 | tst_brkm(TBROK, cleanup, "pipe() failed to create pair of " |
| 219 | "pipe, error:%d", errno); |
| 220 | } |
| 221 | |
| 222 | /* Write known data (0's) of K1 bytes */ |
| 223 | if (write(pfd[1], write_buf[0], K1) != K1) { |
| 224 | tst_brkm(TBROK, cleanup, "write to pipe failed: errno=%d : %s", |
| 225 | errno, strerror(errno)); |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * setup2 - setup function for a test condition for which pread() |
vapier | 22cf204 | 2006-04-10 23:53:29 +0000 | [diff] [blame] | 233 | * returns -ve value and sets errno to EINVAL. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 234 | * |
| 235 | * Create a temporary directory and a file under it. |
| 236 | * return 0. |
| 237 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 238 | int setup2(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 239 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 240 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 241 | tst_tmpdir(); |
| 242 | |
| 243 | /* Creat a temporary file used for mapping */ |
| 244 | if ((fd1 = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) { |
| 245 | tst_brkm(TBROK, cleanup, "open() on %s Failed, errno=%d : %s", |
| 246 | TEMPFILE, errno, strerror(errno)); |
| 247 | } |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * init_buffers() - allocate/Initialize write_buf array. |
| 254 | * |
| 255 | * Allocate read/write buffer. |
| 256 | * Fill the write buffer with the following data like, |
| 257 | * write_buf[0] has 0's, write_buf[1] has 1's, write_buf[2] has 2's |
| 258 | * write_buf[3] has 3's. |
| 259 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 260 | void init_buffers(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 261 | { |
| 262 | int count; /* counter variable for loop */ |
| 263 | |
| 264 | /* Allocate and Initialize write buffer with known data */ |
| 265 | for (count = 0; count < NBUFS; count++) { |
Cyril Hrubis | d218f34 | 2014-09-23 13:14:56 +0200 | [diff] [blame] | 266 | write_buf[count] = malloc(K1); |
| 267 | read_buf[count] = malloc(K1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 268 | |
| 269 | if ((write_buf[count] == NULL) || (read_buf[count] == NULL)) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 270 | tst_brkm(TBROK, NULL, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 271 | "malloc() failed on read/write buffers"); |
| 272 | } |
| 273 | memset(write_buf[count], count, K1); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 279 | * completion or premature exit. |
| 280 | * |
| 281 | * Deallocate the memory allocated to read/write buffers. |
| 282 | * Close the temporary file. |
| 283 | * Remove the temporary directory created. |
| 284 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 285 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 286 | { |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 287 | int count; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 288 | |
| 289 | /* Free the memory allocated for the read/write buffer */ |
| 290 | for (count = 0; count < NBUFS; count++) { |
| 291 | free(write_buf[count]); |
| 292 | free(read_buf[count]); |
| 293 | } |
| 294 | |
| 295 | /* Close the temporary file created in setup2 */ |
| 296 | if (close(fd1) < 0) { |
| 297 | tst_brkm(TBROK, NULL, "close() on %s Failed, errno=%d : %s", |
| 298 | TEMPFILE, errno, strerror(errno)); |
| 299 | } |
| 300 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 301 | tst_rmdir(); |
plars | 24acd6e | 2002-02-20 23:45:31 +0000 | [diff] [blame] | 302 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 303 | } |