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 | /* |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 21 | * Test Name: lseek10 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, |
| 25 | * 1. lseek() returns -1 and sets errno to ESPIPE, if the file handle of |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 26 | * the specified file is associated with a pipe, socket, or FIFO. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | * 2. lseek() returns -1 and sets errno to EINVAL, if the 'Whence' argument |
| 28 | * is not a proper value. |
| 29 | * 3. lseek() returns -1 and sets errno to EBADF, if the file handle of |
| 30 | * the specified file is not valid. |
| 31 | * |
| 32 | * Expected Result: |
| 33 | * lseek() should fail with return value -1 and set expected errno. |
| 34 | * |
| 35 | * Algorithm: |
| 36 | * Setup: |
| 37 | * Setup signal handling. |
| 38 | * Create temporary directory. |
| 39 | * Pause for SIGUSR1 if option specified. |
| 40 | * |
| 41 | * Test: |
| 42 | * Loop if the proper options are given. |
| 43 | * Execute system call |
| 44 | * Check return code, if system call failed (return=-1) |
| 45 | * if errno set == expected errno |
| 46 | * Issue sys call fails with expected return value and errno. |
| 47 | * Otherwise, |
| 48 | * Issue sys call fails with unexpected errno. |
| 49 | * Otherwise, |
| 50 | * Issue sys call returns unexpected value. |
| 51 | * |
| 52 | * Cleanup: |
| 53 | * Print errno log and/or timing stats if options given |
| 54 | * Delete the temporary directory(s)/file(s) created. |
| 55 | * |
| 56 | * Usage: <for command-line> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 57 | * lseek10 [-c n] [-e] [-i n] [-I x] [-p x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | * where, -c n : Run n copies concurrently. |
| 59 | * -e : Turn on errno logging. |
| 60 | * -i n : Execute test n times. |
| 61 | * -I x : Execute test for x seconds. |
| 62 | * -P x : Pause for x seconds between iterations. |
| 63 | * -t : Turn on syscall timing. |
| 64 | * |
| 65 | * HISTORY |
| 66 | * 07/2001 Ported by Wayne Boyer |
| 67 | * |
| 68 | * RESTRICTIONS: |
| 69 | * None. |
| 70 | */ |
| 71 | |
| 72 | #include <stdio.h> |
| 73 | #include <unistd.h> |
| 74 | #include <sys/types.h> |
| 75 | #include <errno.h> |
| 76 | #include <unistd.h> |
| 77 | #include <fcntl.h> |
| 78 | #include <utime.h> |
| 79 | #include <string.h> |
| 80 | #include <sys/stat.h> |
| 81 | #include <signal.h> |
| 82 | |
| 83 | #include "test.h" |
| 84 | #include "usctest.h" |
| 85 | |
| 86 | #define TEMP_FILE1 "tmp_file1" |
| 87 | #define TEMP_FILE2 "tmp_file2" |
| 88 | #define TEMP_FILE3 "tmp_file3" |
| 89 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 90 | #define PIPE_MODE S_IFIFO | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 91 | #define SEEK_TOP 10 |
| 92 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | char *TCID = "lseek10"; /* Test program identifier. */ |
| 94 | int TST_TOTAL = 3; /* Total number of test cases. */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | int exp_enos[] = { ESPIPE, EINVAL, EBADF, 0 }; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | |
| 97 | int no_setup(); |
| 98 | int setup1(); /* setup function to test lseek() for ESPIPE */ |
| 99 | int setup2(); /* setup function to test lseek() for EINVAL */ |
| 100 | int setup3(); /* setup function to test lseek() for EBADF */ |
| 101 | |
| 102 | int fd1; /* file handle for testfile1 */ |
| 103 | int fd2; /* file handle for testfile2 */ |
| 104 | int fd3; /* file handle for testfile3 */ |
| 105 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 106 | struct test_case_t { /* test case struct. to hold ref. test cond's */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | int fd; |
| 108 | int Whence; |
| 109 | char *desc; |
| 110 | int exp_errno; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 111 | int (*setupfunc) (); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | } Test_cases[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | { |
| 114 | 1, SEEK_SET, "'fd' associated with a pipe/fifo", ESPIPE, setup1}, { |
| 115 | 2, SEEK_TOP, "'whence' argument is not valid", EINVAL, setup2}, { |
| 116 | 3, SEEK_SET, "'fd' is not an open file descriptor", EBADF, setup3}, |
| 117 | { |
| 118 | 0, 0, NULL, 0, no_setup} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | void setup(); /* Main setup function of test */ |
| 122 | void cleanup(); /* cleanup function for the test */ |
| 123 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 124 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 126 | int lc; |
| 127 | char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 128 | int fildes; /* file handle for testfile */ |
| 129 | int whence; /* position of file handle in the file */ |
| 130 | char *test_desc; /* test specific error message */ |
| 131 | int ind; /* counter to test different test conditions */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | /* Parse standard options given to run the test. */ |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 134 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 136 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | setup(); |
| 138 | |
| 139 | /* set the expected errnos... */ |
| 140 | TEST_EXP_ENOS(exp_enos); |
| 141 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 143 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 144 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | |
| 146 | for (ind = 0; Test_cases[ind].desc != NULL; ind++) { |
| 147 | fildes = Test_cases[ind].fd; |
| 148 | test_desc = Test_cases[ind].desc; |
| 149 | whence = Test_cases[ind].Whence; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 150 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | /* Assign the 'fd' values appropriatly */ |
| 152 | if (fildes == 1) { |
| 153 | fildes = fd1; |
| 154 | } else if (fildes == 2) { |
| 155 | fildes = fd2; |
| 156 | } else { |
| 157 | fildes = fd3; |
| 158 | } |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 159 | /* |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 160 | * Invoke lseek(2) to test different test conditions. |
| 161 | * Verify that it fails with -1 return value and |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | * sets appropriate errno. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 163 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | TEST(lseek(fildes, 0, whence)); |
| 165 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 166 | if (TEST_RETURN != (off_t) - 1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 167 | tst_resm(TFAIL, |
| 168 | "lseek() returned %ld, expected " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | "-1, errno:%d", TEST_RETURN, |
| 170 | Test_cases[ind].exp_errno); |
| 171 | continue; |
| 172 | } |
| 173 | TEST_ERROR_LOG(TEST_ERRNO); |
| 174 | if (TEST_ERRNO == Test_cases[ind].exp_errno) { |
| 175 | tst_resm(TPASS, "lseek() fails, %s, errno:%d", |
| 176 | test_desc, TEST_ERRNO); |
| 177 | } else { |
| 178 | tst_resm(TFAIL, "lseek() fails, %s, errno:%d, " |
| 179 | "expected errno:%d", test_desc, |
| 180 | TEST_ERRNO, Test_cases[ind].exp_errno); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 186 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 188 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | |
| 190 | /* |
| 191 | * setup() - performs all ONE TIME setup for this test. |
| 192 | * Create a temporary directory and change directory to it. |
| 193 | * Invoke individual test setup functions according to the order |
| 194 | * set in test struct. definition. |
| 195 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 196 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 197 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 198 | int ind; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 199 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 201 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | TEST_PAUSE; |
| 203 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | tst_tmpdir(); |
| 205 | |
| 206 | /* call individual setup functions */ |
| 207 | for (ind = 0; Test_cases[ind].desc != NULL; ind++) { |
| 208 | Test_cases[ind].setupfunc(); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * no_setup() - This is a dummy function which simply returns 0. |
| 214 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 215 | int no_setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 216 | { |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 221 | * setup1() - setup function for a test condition for which lseek(2) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 222 | * returns -1 and sets errno to ESPIPE. |
| 223 | * Creat a named pipe/fifo using mknod() and open it for |
| 224 | * reading/writing. |
| 225 | * This function returns 0 on success. |
| 226 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 227 | int setup1() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 228 | { |
| 229 | /* Creat a named pipe/fifo using mknod() */ |
| 230 | if (mknod(TEMP_FILE1, PIPE_MODE, 0) < 0) { |
| 231 | tst_brkm(TBROK, cleanup, |
| 232 | "mknod(%s, %#o, 0) Failed, errno=%d :%s", |
| 233 | TEMP_FILE1, FILE_MODE, errno, strerror(errno)); |
| 234 | } |
| 235 | |
| 236 | /* Open the named pipe/fifo for reading/writing */ |
| 237 | if ((fd1 = open(TEMP_FILE1, O_RDWR)) < 0) { |
| 238 | tst_brkm(TBROK, cleanup, |
| 239 | "open(%s, O_RDWR) Failed, errno=%d, :%s", |
| 240 | TEMP_FILE1, errno, strerror(errno)); |
| 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * setup2() - setup function for a test condition for which lseek(2) |
| 248 | * returns -1 and sets errno to EINVAL. |
| 249 | * Creat a temporary file for reading/writing and write some data |
| 250 | * into it. |
| 251 | * This function returns 0 on success. |
| 252 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 253 | int setup2() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 254 | { |
| 255 | char write_buff[BUFSIZ]; /* buffer to hold data */ |
| 256 | |
| 257 | /* Get the data to be written to temporary file */ |
| 258 | strcpy(write_buff, "abcdefg"); |
| 259 | |
| 260 | /* Creat/open a temporary file under above directory */ |
| 261 | if ((fd2 = open(TEMP_FILE2, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
| 262 | tst_brkm(TBROK, cleanup, |
| 263 | "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s", |
| 264 | TEMP_FILE2, FILE_MODE, errno, strerror(errno)); |
| 265 | } |
| 266 | |
| 267 | /* Write data into temporary file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 268 | if (write(fd2, write_buff, sizeof(write_buff)) <= 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 269 | tst_brkm(TBROK, cleanup, |
| 270 | "write(2) on %s Failed, errno=%d : %s", |
| 271 | TEMP_FILE2, errno, strerror(errno)); |
| 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * setup3() - setup function for a test condition for which lseek(2) |
| 279 | * returns -1 and sets errno to EBADF. |
| 280 | * Creat a temporary file for reading/writing and close it. |
| 281 | * This function returns 0 on success. |
| 282 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 283 | int setup3() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 284 | { |
| 285 | /* Creat/open a temporary file under above directory */ |
| 286 | if ((fd3 = open(TEMP_FILE3, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
| 287 | tst_brkm(TBROK, cleanup, |
| 288 | "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s", |
| 289 | TEMP_FILE3, FILE_MODE, errno, strerror(errno)); |
| 290 | } |
| 291 | |
| 292 | /* Close the temporary file created above */ |
| 293 | if (close(fd3) < 0) { |
| 294 | tst_brkm(TBROK, cleanup, |
| 295 | "close(%s) Failed, errno=%d : %s:", |
| 296 | TEMP_FILE3, errno, strerror(errno)); |
| 297 | } |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 304 | * completion or premature exit. |
| 305 | * Remove the test directory and testfile(s) created in the setup. |
| 306 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 307 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 308 | { |
| 309 | /* |
| 310 | * print timing stats if that option was specified. |
| 311 | * print errno log if that option was specified. |
| 312 | */ |
| 313 | TEST_CLEANUP; |
| 314 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 315 | /* Close the temporary file(s) created in setup1/setup2 */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 316 | if (close(fd1) < 0) { |
| 317 | tst_brkm(TFAIL, NULL, |
| 318 | "close(%s) Failed, errno=%d : %s:", |
| 319 | TEMP_FILE1, errno, strerror(errno)); |
| 320 | } |
| 321 | if (close(fd2) < 0) { |
| 322 | tst_brkm(TFAIL, NULL, |
| 323 | "close(%s) Failed, errno=%d : %s:", |
| 324 | TEMP_FILE2, errno, strerror(errno)); |
| 325 | } |
| 326 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 327 | tst_rmdir(); |
| 328 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 329 | } |