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: lseek08 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * |
| 23 | * Test Description: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 24 | * Verify that, lseek() call succeeds to set the file pointer position |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 25 | * to the end of the file when 'whence' value set to SEEK_END and any |
| 26 | * attempts to read from that position should fail. |
| 27 | * |
| 28 | * Expected Result: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 29 | * lseek() should return the offset which is set to the file size measured |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | * in bytes. read() attempt should fail with -1 return value. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 31 | * $ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | * Algorithm: |
| 33 | * Setup: |
| 34 | * Setup signal handling. |
| 35 | * Create 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 | * Log the errno and Issue a FAIL message. |
| 43 | * Otherwise, |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 44 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | * if successful, |
| 46 | * Issue Functionality-Pass message. |
| 47 | * Otherwise, |
| 48 | * Issue Functionality-Fail message. |
| 49 | * Cleanup: |
| 50 | * Print errno log and/or timing stats if options given |
| 51 | * Delete the temporary directory created. |
| 52 | * |
| 53 | * Usage: <for command-line> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 54 | * lseek08 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | * where, -c n : Run n copies concurrently. |
| 56 | * -f : Turn off functionality Testing. |
| 57 | * -i n : Execute test n times. |
| 58 | * -I x : Execute test for x seconds. |
| 59 | * -P x : Pause for x seconds between iterations. |
| 60 | * -t : Turn on syscall timing. |
| 61 | * |
| 62 | * HISTORY |
| 63 | * 07/2001 Ported by Wayne Boyer |
| 64 | * |
| 65 | * RESTRICTIONS: |
| 66 | * None. |
| 67 | */ |
| 68 | |
| 69 | #include <stdio.h> |
| 70 | #include <unistd.h> |
| 71 | #include <sys/types.h> |
| 72 | #include <errno.h> |
| 73 | #include <fcntl.h> |
| 74 | #include <utime.h> |
| 75 | #include <string.h> |
| 76 | #include <sys/stat.h> |
| 77 | #include <signal.h> |
| 78 | |
| 79 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
| 81 | #define TEMP_FILE "tmp_file" |
| 82 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 83 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 84 | char *TCID = "lseek08"; |
| 85 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | int fildes; /* file handle for temp file */ |
| 87 | size_t file_size; /* size of the temporary file */ |
| 88 | |
| 89 | void setup(); /* Main setup function of test */ |
| 90 | void cleanup(); /* cleanup function for the test */ |
| 91 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 92 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 94 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 95 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | char read_buf[1]; /* data read from temp. file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 97 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 98 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 100 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | setup(); |
| 102 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 104 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 105 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 107 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | * Invoke lseek(2) to move the read/write file |
| 109 | * pointer/handle to the END of the file. |
| 110 | */ |
| 111 | TEST(lseek(fildes, 0, SEEK_END)); |
| 112 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 113 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 114 | tst_resm(TFAIL | TTERRNO, |
| 115 | "lseek of %s failed", TEMP_FILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | continue; |
| 117 | } |
| 118 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 119 | * Check if the return value from lseek(2) |
| 120 | * is equal to the file_size. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 122 | if (TEST_RETURN != file_size) { |
| 123 | tst_resm(TFAIL, "lseek() returned incorrect " |
| 124 | "value %ld, expected %zu", |
| 125 | TEST_RETURN, file_size); |
| 126 | continue; |
| 127 | } |
| 128 | /* |
| 129 | * The return value is okay, now attempt to read data |
| 130 | * from the file. This should fail as the file pointer |
| 131 | * should be pointing to END OF FILE. |
| 132 | */ |
| 133 | read_buf[0] = '\0'; |
| 134 | if (read(fildes, &read_buf, sizeof(read_buf)) > 0) { |
| 135 | tst_resm(TFAIL, "read() successful on %s", |
| 136 | TEMP_FILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 138 | tst_resm(TPASS, "Functionality of lseek() on " |
| 139 | "%s successful", TEMP_FILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 141 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 144 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 145 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | |
| 147 | /* |
| 148 | * setup() - performs all ONE TIME setup for this test. |
| 149 | * Create a temporary directory and change directory to it. |
| 150 | * Create a test file under temporary directory and write some |
| 151 | * data into it. |
| 152 | * Get the size of the file using fstat(). |
| 153 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 154 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 156 | struct stat stat_buf; /* struct. buffer for stat(2) */ |
| 157 | char write_buf[BUFSIZ]; /* buffer to hold data */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 160 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | TEST_PAUSE; |
| 162 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | tst_tmpdir(); |
| 164 | |
| 165 | /* Get the data to be written to temporary file */ |
| 166 | strcpy(write_buf, "abcdefg\n"); |
| 167 | |
| 168 | /* Creat/open a temporary file under above directory */ |
| 169 | if ((fildes = open(TEMP_FILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 170 | tst_brkm(TBROK | TERRNO, cleanup, |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 171 | "open(%s, O_RDWR|O_CREAT, %#o) failed", |
| 172 | TEMP_FILE, FILE_MODE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* Write data into temporary file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 176 | if (write(fildes, write_buf, strlen(write_buf)) <= 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 177 | tst_brkm(TBROK | TERRNO, cleanup, |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 178 | "writing to %s failed", TEMP_FILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* Get the size of the file using fstat */ |
| 182 | if (fstat(fildes, &stat_buf) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 183 | tst_brkm(TBROK | TERRNO, cleanup, |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 184 | "fstat of %s failed", TEMP_FILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | file_size = stat_buf.st_size; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 192 | * completion or premature exit. |
| 193 | * Remove the test directory and testfile created in the setup. |
| 194 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 195 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 197 | |
| 198 | /* Close the temporary file created */ |
| 199 | if (close(fildes) < 0) { |
| 200 | tst_brkm(TFAIL, NULL, |
| 201 | "close(%s) Failed, errno=%d : %s:", |
| 202 | TEMP_FILE, errno, strerror(errno)); |
| 203 | } |
| 204 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 205 | tst_rmdir(); |
| 206 | |
Markos Chandras | f4539c6 | 2012-01-03 09:41:10 +0000 | [diff] [blame] | 207 | } |