• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * Test Name: sync02
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)
41  *	Log the errno and Issue a FAIL message.
42  *   Otherwise,
43  *	Verify the Functionality of system call
44  *      if successful,
45  *		Issue Functionality-Pass message.
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  *  sync02 [-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 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 
80 #define TEMP_FILE	"temp_file"
81 #define FILE_MODE       S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
82 
83 char *TCID = "sync02";
84 int TST_TOTAL = 1;
85 char write_buffer[BUFSIZ];	/* buffer used to write data to file */
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 
main(int ac,char ** av)91 int main(int ac, char **av)
92 {
93 	int lc;
94 	char read_buffer[BUFSIZ];	/* buffer used to read data from file */
95 
96 	tst_parse_opts(ac, av, NULL, NULL);
97 
98 	setup();
99 
100 	for (lc = 0; TEST_LOOPING(lc); lc++) {
101 
102 		tst_count = 0;
103 
104 		/*
105 		 * Call sync(2) to commit buffer data to disk.
106 		 */
107 		TEST_VOID(sync());
108 
109 		if (TEST_RETURN == -1) {
110 			tst_resm(TFAIL, "%s, Failed, errno=%d : %s",
111 				 TCID, TEST_ERRNO, strerror(TEST_ERRNO));
112 		} else {
113 			/* Set the file ptr to b'nning of file */
114 			if (lseek(fildes, 0, SEEK_SET) < 0) {
115 				tst_brkm(TFAIL, cleanup, "lseek() "
116 					 "failed on %s, error=%d",
117 					 TEMP_FILE, errno);
118 			}
119 
120 			/* Read the contents of file */
121 			if (read(fildes, read_buffer,
122 				 sizeof(read_buffer)) > 0) {
123 				if (strcmp(read_buffer, write_buffer)) {
124 					tst_resm(TFAIL, "Data read "
125 						 "from %s doesn't match "
126 						 "with witten data",
127 						 TEMP_FILE);
128 				} else {
129 					tst_resm(TPASS, "Functionality "
130 						 "of sync() successful");
131 				}
132 			} else {
133 				tst_brkm(TFAIL, cleanup,
134 					 "read() Fails on %s, error=%d",
135 					 TEMP_FILE, errno);
136 			}
137 		}
138 		tst_count++;	/* incr. TEST_LOOP counter */
139 	}
140 
141 	cleanup();
142 	tst_exit();
143 }
144 
145 /*
146  * void
147  * setup() - performs all ONE TIME setup for this test.
148  *  Create a temporary directory and change directory to it.
149  *  Create a test file under temporary directory and write some
150  *  data into it.
151  */
setup(void)152 void setup(void)
153 {
154 
155 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
156 
157 	/* Pause if that option was specified
158 	 * TEST_PAUSE contains the code to fork the test with the -i option.
159 	 * You want to make sure you do this before you create your temporary
160 	 * directory.
161 	 */
162 	TEST_PAUSE;
163 
164 	tst_tmpdir();
165 
166 	/* Copy some data into data buffer */
167 	strcpy(write_buffer, "abcdefghijklmnopqrstuvwxyz");
168 
169 	/* Creat a temporary file under above directory */
170 	if ((fildes = open(TEMP_FILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
171 		tst_brkm(TBROK, cleanup,
172 			 "open(%s, O_RDWR | O_CREAT, %#o) Failed, errno=%d :%s",
173 			 TEMP_FILE, FILE_MODE, errno, strerror(errno));
174 	}
175 
176 	/* Write the buffer data into file */
177 	if (write(fildes, write_buffer, strlen(write_buffer) + 1) !=
178 	    strlen(write_buffer) + 1) {
179 		tst_brkm(TBROK, cleanup,
180 			 "write() failed to write buffer data to %s",
181 			 TEMP_FILE);
182 	}
183 
184 }
185 
186 /*
187  * void
188  * cleanup() - performs all ONE TIME cleanup for this test at
189  *             completion or premature exit.
190  *  Remove the test directory and testfile created in the setup.
191  */
cleanup(void)192 void cleanup(void)
193 {
194 
195 	/* Close the temporary file */
196 	if (close(fildes) == -1) {
197 		tst_brkm(TFAIL, NULL,
198 			 "close(%s) Failed, errno=%d : %s",
199 			 TEMP_FILE, errno, strerror(errno));
200 	}
201 
202 	tst_rmdir();
203 
204 }
205