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