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: 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
| 80 | #define TEMP_FILE "temp_file" |
| 81 | #define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH |
| 82 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 83 | char *TCID = "sync02"; |
| 84 | int TST_TOTAL = 1; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 85 | char write_buffer[BUFSIZ]; /* buffer used to write data to file */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | int fildes; /* file descriptor for temporary file */ |
| 87 | |
| 88 | void setup(); /* Main setup function of test */ |
| 89 | void cleanup(); /* cleanup function for the test */ |
| 90 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 91 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 93 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 94 | const char *msg; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | char read_buffer[BUFSIZ]; /* buffer used to read data from file */ |
| 96 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 97 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 98 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | setup(); |
| 101 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 103 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 104 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 106 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | * Call sync(2) to commit buffer data to disk. |
| 108 | */ |
| 109 | TEST_VOID(sync()); |
| 110 | |
| 111 | if (TEST_RETURN == -1) { |
| 112 | tst_resm(TFAIL, "%s, Failed, errno=%d : %s", |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 113 | TCID, TEST_ERRNO, strerror(TEST_ERRNO)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 115 | /* Set the file ptr to b'nning of file */ |
| 116 | if (lseek(fildes, 0, SEEK_SET) < 0) { |
| 117 | tst_brkm(TFAIL, cleanup, "lseek() " |
| 118 | "failed on %s, error=%d", |
| 119 | TEMP_FILE, errno); |
| 120 | } |
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 | /* Read the contents of file */ |
| 123 | if (read(fildes, read_buffer, |
| 124 | sizeof(read_buffer)) > 0) { |
| 125 | if (strcmp(read_buffer, write_buffer)) { |
| 126 | tst_resm(TFAIL, "Data read " |
| 127 | "from %s doesn't match " |
| 128 | "with witten data", |
| 129 | TEMP_FILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 131 | tst_resm(TPASS, "Functionality " |
| 132 | "of sync() successful"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 133 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 135 | tst_brkm(TFAIL, cleanup, |
| 136 | "read() Fails on %s, error=%d", |
| 137 | TEMP_FILE, errno); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 140 | tst_count++; /* incr. TEST_LOOP counter */ |
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 | 7d0a4a5 | 2010-12-16 10:05:08 -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 | * void |
| 149 | * setup() - performs all ONE TIME setup for this test. |
| 150 | * Create a temporary directory and change directory to it. |
| 151 | * Create a test file under temporary directory and write some |
| 152 | * data into it. |
| 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 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 156 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 158 | |
| 159 | /* Pause if that option was specified |
| 160 | * TEST_PAUSE contains the code to fork the test with the -i option. |
| 161 | * You want to make sure you do this before you create your temporary |
| 162 | * directory. |
| 163 | */ |
| 164 | TEST_PAUSE; |
| 165 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | tst_tmpdir(); |
| 167 | |
| 168 | /* Copy some data into data buffer */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 169 | strcpy(write_buffer, "abcdefghijklmnopqrstuvwxyz"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | |
| 171 | /* Creat a temporary file under above directory */ |
| 172 | if ((fildes = open(TEMP_FILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) { |
| 173 | tst_brkm(TBROK, cleanup, |
| 174 | "open(%s, O_RDWR | O_CREAT, %#o) Failed, errno=%d :%s", |
| 175 | TEMP_FILE, FILE_MODE, errno, strerror(errno)); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 176 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | |
| 178 | /* Write the buffer data into file */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 179 | if (write(fildes, write_buffer, strlen(write_buffer) + 1) != |
| 180 | strlen(write_buffer) + 1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | tst_brkm(TBROK, cleanup, |
| 182 | "write() failed to write buffer data to %s", |
| 183 | TEMP_FILE); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 184 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 185 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 186 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | |
| 188 | /* |
| 189 | * void |
| 190 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 191 | * completion or premature exit. |
| 192 | * Remove the test directory and testfile created in the setup. |
| 193 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 194 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | |
| 197 | /* Close the temporary file */ |
| 198 | if (close(fildes) == -1) { |
| 199 | tst_brkm(TFAIL, NULL, |
| 200 | "close(%s) Failed, errno=%d : %s", |
| 201 | TEMP_FILE, errno, strerror(errno)); |
| 202 | } |
| 203 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | tst_rmdir(); |
| 205 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 206 | } |