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 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 20 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 21 | * Test Name: ftruncate01 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, ftruncate(2) succeeds to truncate a file to a specified |
| 25 | * length if the file indicated by file descriptor opened for writing. |
| 26 | * |
| 27 | * Expected Result: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 28 | * ftruncate(2) should return a value 0 and the length of the file after |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 29 | * truncation should be equal to the length it is truncated to. |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 30 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 31 | * Algorithm: |
| 32 | * Setup: |
| 33 | * Setup signal handling. |
| 34 | * Create temporary directory. |
| 35 | * Pause for SIGUSR1 if option specified. |
| 36 | * |
| 37 | * Test: |
| 38 | * Loop if the proper options are given. |
| 39 | * Execute system call |
| 40 | * Check return code, if system call failed (return=-1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 41 | * Log the errno and Issue a FAIL message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | * Otherwise, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 43 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | * if successful, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 45 | * Issue Functionality-Pass message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | * Otherwise, |
| 47 | * Issue Functionality-Fail message. |
| 48 | * Cleanup: |
| 49 | * Print errno log and/or timing stats if options given |
| 50 | * Delete the temporary directory created. |
| 51 | * |
| 52 | * Usage: <for command-line> |
| 53 | * ftruncate01 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 54 | * where, -c n : Run n copies concurrently. |
| 55 | * -f : Turn off functionality Testing. |
| 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. |
| 60 | * |
| 61 | * HISTORY |
| 62 | * 07/2001 Ported by Wayne Boyer |
| 63 | * |
| 64 | * RESTRICTIONS: |
| 65 | * This test should be run by 'non-super-user' only. |
| 66 | * |
| 67 | */ |
| 68 | |
| 69 | #include <sys/types.h> |
| 70 | #include <sys/stat.h> |
| 71 | #include <sys/fcntl.h> |
| 72 | #include <errno.h> |
| 73 | #include <string.h> |
| 74 | #include <signal.h> |
vapier | c20c04b | 2009-08-28 13:00:33 +0000 | [diff] [blame] | 75 | #include <inttypes.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | |
| 77 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | #define TESTFILE "testfile" /* file under test */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | #define BUF_SIZE 256 /* buffer size */ |
| 82 | #define FILE_SIZE 1024 /* test file size */ |
| 83 | #define TRUNC_LEN 256 /* truncation length */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 85 | TCID_DEFINE(ftruncate01); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 86 | int TST_TOTAL = 1; /* Total number of test conditions */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | int fildes; /* file descriptor for test file */ |
| 88 | |
| 89 | void setup(); /* setup function for the 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 | { |
| 94 | struct stat stat_buf; /* stat(2) struct contents */ |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 95 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 96 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | off_t file_length; /* test file length */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 99 | msg = parse_opts(ac, av, NULL, NULL); |
| 100 | if (msg != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 102 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | } |
| 104 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | setup(); |
| 106 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 108 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 109 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 111 | /* |
| 112 | * Call ftruncate(2) to truncate a test file to a |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | * specified length. |
| 114 | */ |
| 115 | TEST(ftruncate(fildes, TRUNC_LEN)); |
| 116 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 118 | tst_resm(TFAIL | TTERRNO, "ftruncate(%s) failed", |
| 119 | TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 120 | continue; |
| 121 | } |
| 122 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 123 | * Get the testfile information using |
| 124 | * fstat(2). |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 125 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 126 | if (fstat(fildes, &stat_buf) < 0) { |
| 127 | tst_brkm(TFAIL, cleanup, |
| 128 | "stat(2) of %s failed, error:%d", |
| 129 | TESTFILE, errno); |
| 130 | } |
| 131 | stat_buf.st_mode &= ~S_IFREG; |
| 132 | file_length = stat_buf.st_size; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 134 | /* |
| 135 | * Check for expected size of testfile after |
| 136 | * truncate(2) on it. |
| 137 | */ |
| 138 | if (file_length != TRUNC_LEN) { |
| 139 | tst_resm(TFAIL, |
| 140 | "%s: Incorrect file size %" PRId64 ", " |
| 141 | "Expected %d", TESTFILE, |
| 142 | (int64_t) file_length, TRUNC_LEN); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 144 | tst_resm(TPASS, "Functionality of ftruncate() " |
| 145 | "on %s successful", TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 147 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 150 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 151 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * void |
| 155 | * setup() - performs all ONE TIME setup for this test. |
| 156 | * Create a temporary directory and change directory to it. |
| 157 | * Create a test file under temporary directory and write some |
| 158 | * data into it. |
| 159 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 160 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 162 | int i; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 163 | int c, c_total = 0; /* bytes to be written to file */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | char tst_buff[BUF_SIZE]; /* buffer to hold data */ |
| 165 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 167 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | TEST_PAUSE; |
| 169 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | tst_tmpdir(); |
| 171 | |
| 172 | /* Fill the test buffer with the known data */ |
| 173 | for (i = 0; i < BUF_SIZE; i++) { |
| 174 | tst_buff[i] = 'a'; |
| 175 | } |
| 176 | |
| 177 | /* open a file for reading/writing */ |
vapier | c20c04b | 2009-08-28 13:00:33 +0000 | [diff] [blame] | 178 | fildes = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE); |
| 179 | if (fildes == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 180 | tst_brkm(TBROK | TERRNO, cleanup, |
vapier | c20c04b | 2009-08-28 13:00:33 +0000 | [diff] [blame] | 181 | "open(%s, O_RDWR|O_CREAT, %o) failed", |
| 182 | TESTFILE, FILE_MODE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | |
| 184 | /* Write to the file 1k data from the buffer */ |
| 185 | while (c_total < FILE_SIZE) { |
| 186 | if ((c = write(fildes, tst_buff, sizeof(tst_buff))) <= 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 187 | tst_brkm(TBROK | TERRNO, cleanup, "write(%s) failed", |
| 188 | TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | } else { |
| 190 | c_total += c; |
| 191 | } |
| 192 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 193 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 194 | |
| 195 | /* |
| 196 | * void |
| 197 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 198 | * completion or premature exit. |
| 199 | * Close the temporary file. |
| 200 | * Remove the test directory and testfile created in the setup. |
| 201 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 202 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 203 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | |
| 205 | /* Close the testfile after writing data into it */ |
vapier | c20c04b | 2009-08-28 13:00:33 +0000 | [diff] [blame] | 206 | if (close(fildes) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 207 | tst_brkm(TFAIL | TERRNO, NULL, "close(%s) failed", TESTFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 209 | tst_rmdir(); |
| 210 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 211 | } |