blob: 24e15109f51c2e1d3c01b3f7b8863f79babf1dea [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
nstrazfa31d552002-05-14 16:50:06 +000021 * Test Name: sync02
plars865695b2001-08-27 22:15:12 +000022 *
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_modak56207ce2009-03-23 13:35:39 +000041 * Log the errno and Issue a FAIL message.
plars865695b2001-08-27 22:15:12 +000042 * Otherwise,
subrata_modak56207ce2009-03-23 13:35:39 +000043 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000044 * if successful,
subrata_modak56207ce2009-03-23 13:35:39 +000045 * Issue Functionality-Pass message.
plars865695b2001-08-27 22:15:12 +000046 * 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>
nstrazfa31d552002-05-14 16:50:06 +000053 * sync02 [-c n] [-f] [-i n] [-I x] [-p x] [-t]
plars865695b2001-08-27 22:15:12 +000054 * 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"
plars865695b2001-08-27 22:15:12 +000079
80#define TEMP_FILE "temp_file"
81#define FILE_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
82
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020083char *TCID = "sync02";
84int TST_TOTAL = 1;
subrata_modak56207ce2009-03-23 13:35:39 +000085char write_buffer[BUFSIZ]; /* buffer used to write data to file */
plars865695b2001-08-27 22:15:12 +000086int fildes; /* file descriptor for temporary file */
87
88void setup(); /* Main setup function of test */
89void cleanup(); /* cleanup function for the test */
90
subrata_modak56207ce2009-03-23 13:35:39 +000091int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000092{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020093 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020094 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +000095 char read_buffer[BUFSIZ]; /* buffer used to read data from file */
96
Garrett Cooper7d0a4a52010-12-16 10:05:08 -080097 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080098 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000099
plars865695b2001-08-27 22:15:12 +0000100 setup();
101
plars865695b2001-08-27 22:15:12 +0000102 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -0800103
Caspar Zhangd59a6592013-03-07 14:59:12 +0800104 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000105
subrata_modak4bb656a2009-02-26 12:02:09 +0000106 /*
plars865695b2001-08-27 22:15:12 +0000107 * 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_modak56207ce2009-03-23 13:35:39 +0000113 TCID, TEST_ERRNO, strerror(TEST_ERRNO));
plars865695b2001-08-27 22:15:12 +0000114 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200115 /* 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 }
plars865695b2001-08-27 22:15:12 +0000121
Cyril Hrubise38b9612014-06-02 17:20:57 +0200122 /* 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);
plars865695b2001-08-27 22:15:12 +0000130 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200131 tst_resm(TPASS, "Functionality "
132 "of sync() successful");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 }
plars865695b2001-08-27 22:15:12 +0000134 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200135 tst_brkm(TFAIL, cleanup,
136 "read() Fails on %s, error=%d",
137 TEMP_FILE, errno);
plars865695b2001-08-27 22:15:12 +0000138 }
139 }
Caspar Zhangd59a6592013-03-07 14:59:12 +0800140 tst_count++; /* incr. TEST_LOOP counter */
Garrett Cooper2c282152010-12-16 00:55:50 -0800141 }
plars865695b2001-08-27 22:15:12 +0000142
plars865695b2001-08-27 22:15:12 +0000143 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800144 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800145}
plars865695b2001-08-27 22:15:12 +0000146
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 Frysingerc57fba52014-04-09 18:56:30 -0400154void setup(void)
plars865695b2001-08-27 22:15:12 +0000155{
Garrett Cooper2c282152010-12-16 00:55:50 -0800156
plars865695b2001-08-27 22:15:12 +0000157 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
plars865695b2001-08-27 22:15:12 +0000166 tst_tmpdir();
167
168 /* Copy some data into data buffer */
subrata_modak56207ce2009-03-23 13:35:39 +0000169 strcpy(write_buffer, "abcdefghijklmnopqrstuvwxyz");
plars865695b2001-08-27 22:15:12 +0000170
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 Gao354ebb42012-12-07 10:10:04 +0800176 }
plars865695b2001-08-27 22:15:12 +0000177
178 /* Write the buffer data into file */
subrata_modak56207ce2009-03-23 13:35:39 +0000179 if (write(fildes, write_buffer, strlen(write_buffer) + 1) !=
180 strlen(write_buffer) + 1) {
plars865695b2001-08-27 22:15:12 +0000181 tst_brkm(TBROK, cleanup,
182 "write() failed to write buffer data to %s",
183 TEMP_FILE);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800184 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000185
Garrett Cooper2c282152010-12-16 00:55:50 -0800186}
plars865695b2001-08-27 22:15:12 +0000187
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 Frysingerc57fba52014-04-09 18:56:30 -0400194void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000195{
plars865695b2001-08-27 22:15:12 +0000196
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
plars865695b2001-08-27 22:15:12 +0000204 tst_rmdir();
205
Chris Dearmanec6edca2012-10-17 19:54:01 -0700206}