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: lseek06 |
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 less than or equal to the file size, when a file is opened for |
| 26 | * read or write. |
| 27 | * |
| 28 | * Expected Result: |
| 29 | * lseek() should return the offset from the beginning of the file measured |
| 30 | * in bytes. Also check if able to read valid data from this location. |
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 | * Pause for SIGUSR1 if option specified. |
| 36 | * Create temporary directory. |
| 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 | * lseek06 [-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 OFFSET 4 |
| 82 | #define TEMP_FILE "tmp_file" |
| 83 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 84 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 85 | char *TCID = "lseek06"; |
| 86 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | int fildes; /* file handle for temp 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 by OFFSET bytes. |
| 110 | */ |
| 111 | TEST(lseek(fildes, OFFSET, SEEK_SET)); |
| 112 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | if (TEST_RETURN == (off_t) - 1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | tst_resm(TFAIL, "lseek on (%s) Failed, errno=%d : %s", |
| 115 | TEMP_FILE, TEST_ERRNO, strerror(TEST_ERRNO)); |
| 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 specified OFFSET value. |
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 != OFFSET) { |
| 123 | tst_resm(TFAIL, "lseek() returned incorrect " |
| 124 | "value %ld, expected %d", |
| 125 | TEST_RETURN, OFFSET); |
| 126 | continue; |
| 127 | } |
| 128 | /* |
| 129 | * The return value is good, now check data. |
| 130 | * Read the data byte from this location. |
| 131 | */ |
| 132 | if (read(fildes, &read_buf, sizeof(read_buf)) < 0) { |
| 133 | tst_brkm(TFAIL, cleanup, "read() failed " |
| 134 | "on %s, error=%d", TEMP_FILE, errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 136 | /* |
| 137 | * Check if read byte is the expected character. |
| 138 | * For pos 4 ---> 'e' |
| 139 | */ |
| 140 | if (read_buf[0] != 'e') { |
| 141 | tst_resm(TFAIL, "Incorrect data read " |
| 142 | "from file %s", TEMP_FILE); |
| 143 | } else { |
| 144 | tst_resm(TPASS, "Functionality " |
| 145 | "of lseek() on %s " |
| 146 | "successful", TEMP_FILE); |
| 147 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 149 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 152 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 153 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | |
| 155 | /* |
| 156 | * setup() - performs all ONE TIME setup for this test. |
| 157 | * Create a temporary directory and change directory to it. |
| 158 | * Create a test file under temporary directory and write some |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 159 | * data into it. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 161 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 163 | char write_buf[BUFSIZ]; /* buffer to hold data */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 166 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | TEST_PAUSE; |
| 168 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | tst_tmpdir(); |
| 170 | |
| 171 | /* Get the data to be written to temporary file */ |
| 172 | strcpy(write_buf, "abcdefg"); |
| 173 | |
| 174 | /* Creat/open a temporary file under above directory */ |
| 175 | if ((fildes = open(TEMP_FILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
| 176 | tst_brkm(TBROK, cleanup, |
| 177 | "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d :%s", |
| 178 | TEMP_FILE, FILE_MODE, errno, strerror(errno)); |
| 179 | } |
| 180 | |
| 181 | /* Write data into temporary file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 182 | if (write(fildes, write_buf, strlen(write_buf)) != strlen(write_buf)) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | tst_brkm(TBROK, cleanup, "write(2) on %s Failed, errno=%d : %s", |
| 184 | TEMP_FILE, errno, strerror(errno)); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 190 | * completion or premature exit. |
| 191 | * Remove the test directory and testfile created in the setup. |
| 192 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 193 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 194 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | |
| 196 | /* Close the temporary file created */ |
| 197 | if (close(fildes) < 0) { |
| 198 | tst_brkm(TFAIL, NULL, "close(%s) Failed, errno=%d : %s:", |
| 199 | TEMP_FILE, errno, strerror(errno)); |
| 200 | } |
| 201 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | tst_rmdir(); |
| 203 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 204 | } |