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 |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | |
| 20 | /* |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 21 | * Test Name: sync02 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * |
| 23 | * Test Description: |
| 24 | * Open a file for write; modify the file, then do a sync(). |
| 25 | * Verify that the data has been written to disk by re-opening the file. |
| 26 | * |
| 27 | * Expected Result: |
| 28 | * sync() alawys returns 0 in Linux. The data written to the file should |
| 29 | * be updated to the disk. |
| 30 | * |
| 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> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 53 | * sync02 [-c n] [-f] [-i n] [-I x] [-p x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 John George |
| 63 | * -Ported |
| 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 <string.h> |
| 75 | #include <signal.h> |
| 76 | #include <sys/stat.h> |
| 77 | |
| 78 | #include "test.h" |
| 79 | #include "usctest.h" |
| 80 | |
| 81 | #define TEMP_FILE "temp_file" |
| 82 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 83 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 84 | char *TCID = "sync02"; /* Test program identifier. */ |
| 85 | int TST_TOTAL = 1; /* Total number of test cases. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | extern int Tst_count; /* Test Case counter for tst_* routines */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 87 | char write_buffer[BUFSIZ]; /* buffer used to write data to file */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | int fildes; /* file descriptor for temporary file */ |
| 89 | |
| 90 | void setup(); /* Main setup function of test */ |
| 91 | void cleanup(); /* cleanup function for the test */ |
| 92 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | int lc; /* loop counter */ |
| 96 | char *msg; /* message returned from parse_opts */ |
| 97 | char read_buffer[BUFSIZ]; /* buffer used to read data from file */ |
| 98 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | /* Parse standard options given to run the test. */ |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame^] | 100 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 101 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | setup(); |
| 104 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 106 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 107 | Tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 109 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | * Call sync(2) to commit buffer data to disk. |
| 111 | */ |
| 112 | TEST_VOID(sync()); |
| 113 | |
| 114 | if (TEST_RETURN == -1) { |
| 115 | tst_resm(TFAIL, "%s, Failed, errno=%d : %s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | TCID, TEST_ERRNO, strerror(TEST_ERRNO)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | } else { |
| 118 | /* |
| 119 | * Perform functional verification if test |
| 120 | * executed without (-f) option. |
| 121 | */ |
| 122 | if (STD_FUNCTIONAL_TEST) { |
| 123 | /* Set the file ptr to b'nning of file */ |
| 124 | if (lseek(fildes, 0, SEEK_SET) < 0) { |
| 125 | tst_brkm(TFAIL, cleanup, "lseek() " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 126 | "failed on %s, error=%d", |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | TEMP_FILE, errno); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 128 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
| 130 | /* Read the contents of file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 131 | if (read(fildes, read_buffer, |
| 132 | sizeof(read_buffer)) > 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | if (strcmp(read_buffer, write_buffer)) { |
| 134 | tst_resm(TFAIL, "Data read " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 135 | "from %s doesn't match " |
| 136 | "with witten data", |
| 137 | TEMP_FILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | } else { |
| 139 | tst_resm(TPASS, "Functionality " |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | "of sync() successful"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | } |
| 142 | } else { |
| 143 | tst_brkm(TFAIL, cleanup, |
| 144 | "read() Fails on %s, error=%d", |
| 145 | TEMP_FILE, errno); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 146 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | } else { |
| 148 | tst_resm(TPASS, "call succeeded"); |
| 149 | } |
| 150 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 151 | Tst_count++; /* incr. TEST_LOOP counter */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 152 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame^] | 155 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 157 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | |
| 159 | /* |
| 160 | * void |
| 161 | * setup() - performs all ONE TIME setup for this test. |
| 162 | * Create a temporary directory and change directory to it. |
| 163 | * Create a test file under temporary directory and write some |
| 164 | * data into it. |
| 165 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 166 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 168 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 170 | |
| 171 | /* Pause if that option was specified |
| 172 | * TEST_PAUSE contains the code to fork the test with the -i option. |
| 173 | * You want to make sure you do this before you create your temporary |
| 174 | * directory. |
| 175 | */ |
| 176 | TEST_PAUSE; |
| 177 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 178 | tst_tmpdir(); |
| 179 | |
| 180 | /* Copy some data into data buffer */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 181 | strcpy(write_buffer, "abcdefghijklmnopqrstuvwxyz"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | |
| 183 | /* Creat a temporary file under above directory */ |
| 184 | if ((fildes = open(TEMP_FILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
| 185 | tst_brkm(TBROK, cleanup, |
| 186 | "open(%s, O_RDWR | O_CREAT, %#o) Failed, errno=%d :%s", |
| 187 | TEMP_FILE, FILE_MODE, errno, strerror(errno)); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 188 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 189 | |
| 190 | /* Write the buffer data into file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 191 | if (write(fildes, write_buffer, strlen(write_buffer) + 1) != |
| 192 | strlen(write_buffer) + 1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 193 | tst_brkm(TBROK, cleanup, |
| 194 | "write() failed to write buffer data to %s", |
| 195 | TEMP_FILE); |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 196 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 197 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 198 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * void |
| 202 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 203 | * completion or premature exit. |
| 204 | * Remove the test directory and testfile created in the setup. |
| 205 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 206 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 207 | { |
| 208 | /* |
| 209 | * print timing stats if that option was specified. |
| 210 | * print errno log if that option was specified. |
| 211 | */ |
| 212 | TEST_CLEANUP; |
| 213 | |
| 214 | /* Close the temporary file */ |
| 215 | if (close(fildes) == -1) { |
| 216 | tst_brkm(TFAIL, NULL, |
| 217 | "close(%s) Failed, errno=%d : %s", |
| 218 | TEMP_FILE, errno, strerror(errno)); |
| 219 | } |
| 220 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 221 | tst_rmdir(); |
| 222 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame^] | 223 | } |