blob: e2905f9e8686b75a8b4ea022c80f069440009a53 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080020 * with this program; if not, write the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
plars865695b2001-08-27 22:15:12 +000022 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 *
32 */
vapier6659f412009-08-28 11:17:39 +000033/* $Id: asyncio02.c,v 1.6 2009/08/28 11:17:39 vapier Exp $ */
plars865695b2001-08-27 22:15:12 +000034/************************************************************
35 * OS Test - Silicon Graphics, Inc.
36 * Mendota Heights, Minnesota
subrata_modak4bb656a2009-02-26 12:02:09 +000037 *
plars865695b2001-08-27 22:15:12 +000038 * TEST IDENTIFIER: aiotcs02: write/close flushes data to the file
subrata_modak4bb656a2009-02-26 12:02:09 +000039 *
plars865695b2001-08-27 22:15:12 +000040 * PARENT DOCUMENT: aiotds01: kernel i/o
subrata_modak4bb656a2009-02-26 12:02:09 +000041 *
plars865695b2001-08-27 22:15:12 +000042 * AUTHOR: Barrie Kletscher
subrata_modak4bb656a2009-02-26 12:02:09 +000043 *
plars865695b2001-08-27 22:15:12 +000044 * CO-PILOT: Dave Baumgartner
subrata_modak4bb656a2009-02-26 12:02:09 +000045 *
plars865695b2001-08-27 22:15:12 +000046 * TEST ITEMS:
47 * for each open flags set used:
48 * 1. Multiple writes to a file work as specified for
49 * more than BUFSIZ bytes.
50 * 2. Multiple writes to a file work as specified for
51 * BUFSIZ bytes.
52 * 3. Multiple writes to a file work as specified for
53 * lower than BUFSIZ bytes.
subrata_modak4bb656a2009-02-26 12:02:09 +000054 *
plars865695b2001-08-27 22:15:12 +000055 * INPUT SPECIFICATIONS:
56 * Standard parse_opts supported options.
subrata_modak56207ce2009-03-23 13:35:39 +000057 *$
plars865695b2001-08-27 22:15:12 +000058 * OUTPUT SPECIFICATIONS
59 * Standard tst_res output format
subrata_modak4bb656a2009-02-26 12:02:09 +000060 *
plars865695b2001-08-27 22:15:12 +000061 * ENVIRONMENTAL NEEDS:
62 * This program uses the environment variable TMPDIR for the location
63 * of the temporary directory.
subrata_modak4bb656a2009-02-26 12:02:09 +000064 *
65 *
plars865695b2001-08-27 22:15:12 +000066 * SPECIAL PROCEDURAL REQUIREMENTS:
67 * The program must be linked with tst_*.o and parse_opts.o.
subrata_modak4bb656a2009-02-26 12:02:09 +000068 *
plars865695b2001-08-27 22:15:12 +000069 * INTERCASE DEPENDENCIES:
70 * NONE.
subrata_modak4bb656a2009-02-26 12:02:09 +000071 *
plars865695b2001-08-27 22:15:12 +000072 * DETAILED DESCRIPTION:
73 * Attempt to get some memory to work with.
74 * Call testrun writing (BUFSIZ + 1) bytes
75 * Call testrun writing BUFSIZ bytes
76 * Repeated call to testrun() with decreasing write sizes
77 * less than BUFSIZ
78 * End
subrata_modak4bb656a2009-02-26 12:02:09 +000079 *
plars865695b2001-08-27 22:15:12 +000080 * Start testrun()
81 * Attempt to open a temporary file.
82 * Write the memory to the file.
83 * Attempt to close the file which also flushes the buffers.
84 * Now check to see if the number of bytes written is the
85 * same as the number of bytes in the file.
86 * Cleanup
subrata_modak4bb656a2009-02-26 12:02:09 +000087 *
plars865695b2001-08-27 22:15:12 +000088 * BUGS:
89 * NONE.
subrata_modak4bb656a2009-02-26 12:02:09 +000090 *
plars865695b2001-08-27 22:15:12 +000091************************************************************/
92
93#include <fcntl.h>
94#include <sys/types.h>
95#include <sys/stat.h>
96#include <sys/signal.h>
97#include <errno.h>
98#include <stdlib.h>
99#include "test.h"
plars865695b2001-08-27 22:15:12 +0000100
101#define FLAG O_RDWR | O_CREAT | O_TRUNC /* Flags used when opening temp tile */
subrata_modak56207ce2009-03-23 13:35:39 +0000102#define MODE 0777 /* Mode to open file with */
103#define WRITES 10 /* Number of times buffer is written */
104#define DECR 1000 /* Number of bytes decremented between */
plars865695b2001-08-27 22:15:12 +0000105 /* Calls to testrun() */
subrata_modak56207ce2009-03-23 13:35:39 +0000106#define OK -1 /* Return value from testrun() */
plars865695b2001-08-27 22:15:12 +0000107
108#define FNAME1 "aio02.1"
109#define FNAME2 "aio02.2"
110#define FNAME3 "aio02.3"
111
112#define ERR_MSG1 "Bytes in file not equal to bytes written."
113#define ERR_MSG2 "Bytes in file (%d) not equal to bytes written (%d)."
114
subrata_modak56207ce2009-03-23 13:35:39 +0000115char *dp; /* pointer to area of memory */
plars865695b2001-08-27 22:15:12 +0000116
117void setup();
118void cleanup();
119int testrun(int flag, int bytes, int ti);
120
Cyril Hrubisfdce7d52013-04-04 18:35:48 +0200121char *TCID = "asyncio02";
122int TST_TOTAL = 6;
plars865695b2001-08-27 22:15:12 +0000123
plars865695b2001-08-27 22:15:12 +0000124char *filename; /* name of the temporary file */
125
126char *Progname;
127int Open_flags;
128
129int Flags[] = {
130 O_RDWR | O_CREAT | O_TRUNC,
131 O_RDWR | O_CREAT | O_TRUNC
132};
133
134int Num_flags;
135
136/***********************************************************************
137 * MAIN
138 ***********************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000139int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000140{
141
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200142 int i;
143 int ret_val;
subrata_modak56207ce2009-03-23 13:35:39 +0000144 int eok; /* everything is ok flag */
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200145 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200146 const char *msg;
subrata_modak56207ce2009-03-23 13:35:39 +0000147 int flag_cnt;
plars865695b2001-08-27 22:15:12 +0000148
subrata_modak56207ce2009-03-23 13:35:39 +0000149 Num_flags = sizeof(Flags) / sizeof(int);
150 TST_TOTAL = 3 * Num_flags;
plars865695b2001-08-27 22:15:12 +0000151
Garrett Coopera9e49f12010-12-16 10:54:03 -0800152 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
subrata_modak56207ce2009-03-23 13:35:39 +0000153 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000154
subrata_modak56207ce2009-03-23 13:35:39 +0000155 setup();
plars865695b2001-08-27 22:15:12 +0000156
subrata_modak56207ce2009-03-23 13:35:39 +0000157 for (lc = 0; TEST_LOOPING(lc); lc++) {
plars865695b2001-08-27 22:15:12 +0000158
Caspar Zhangd59a6592013-03-07 14:59:12 +0800159 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000160
subrata_modak56207ce2009-03-23 13:35:39 +0000161 for (flag_cnt = 0; flag_cnt < Num_flags; flag_cnt++) {
plars865695b2001-08-27 22:15:12 +0000162
subrata_modak56207ce2009-03-23 13:35:39 +0000163 /*
164 * call testrun writing (BUFSIZ + 1) byte chunks
165 */
plars865695b2001-08-27 22:15:12 +0000166
subrata_modak56207ce2009-03-23 13:35:39 +0000167 filename = FNAME1;
Cyril Hrubise38b9612014-06-02 17:20:57 +0200168 if (testrun(Flags[flag_cnt], BUFSIZ + 1, 1) != OK) {
subrata_modak56207ce2009-03-23 13:35:39 +0000169 tst_resm(TFAIL, ERR_MSG1);
Cyril Hrubise38b9612014-06-02 17:20:57 +0200170 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000171 tst_resm(TPASS,
172 "More than BUFSIZE bytes multiple synchronous writes to a file check out ok");
173 }
174
175 /*
176 * call testrun writing BUFSIZ byte chunks
177 */
178
179 filename = FNAME2;
180 if (testrun(Flags[flag_cnt], BUFSIZ, 2) != OK) {
181 tst_resm(TFAIL, ERR_MSG1);
Cyril Hrubise38b9612014-06-02 17:20:57 +0200182 } else {
subrata_modak56207ce2009-03-23 13:35:39 +0000183 tst_resm(TPASS,
184 "BUFSIZE bytes multiple synchronous writes to a file checks out ok");
185 }
186
187 /*
188 * while the byte chunks are greater than 0
189 * call testrun() with decreasing chunk sizes
190 */
191
192 filename = FNAME3;
193 eok = 1;
194 for (i = BUFSIZ - 1; i >= 0; i -= DECR) {
195 if ((ret_val =
196 testrun(Flags[flag_cnt], i, 3)) != OK) {
vapier6659f412009-08-28 11:17:39 +0000197 tst_resm(TFAIL, ERR_MSG2, ret_val,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800198 i * WRITES);
subrata_modak56207ce2009-03-23 13:35:39 +0000199 }
200 }
201
Cyril Hrubise38b9612014-06-02 17:20:57 +0200202 if (eok) {
subrata_modak56207ce2009-03-23 13:35:39 +0000203 tst_resm(TPASS,
204 "Less than BUFSIZE bytes multiple synchronous writes to a file checks out ok");
Cyril Hrubise38b9612014-06-02 17:20:57 +0200205 }
subrata_modak56207ce2009-03-23 13:35:39 +0000206 }
plars865695b2001-08-27 22:15:12 +0000207 }
subrata_modak56207ce2009-03-23 13:35:39 +0000208 cleanup();
Garrett Cooper43088e12010-12-13 23:30:59 -0800209 tst_exit();
subrata_modak56207ce2009-03-23 13:35:39 +0000210} /* end main() */
plars865695b2001-08-27 22:15:12 +0000211
subrata_modak56207ce2009-03-23 13:35:39 +0000212int testrun(int flag, int bytes, int ti)
plars865695b2001-08-27 22:15:12 +0000213{
214
Garrett Cooper43088e12010-12-13 23:30:59 -0800215 int fildes, i, ret;
plars865695b2001-08-27 22:15:12 +0000216
subrata_modak56207ce2009-03-23 13:35:39 +0000217 struct stat buffer; /* buffer of memory required for stat command */
plars865695b2001-08-27 22:15:12 +0000218
219 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000220 * Attempt to open a temporary file.
221 */
plars865695b2001-08-27 22:15:12 +0000222
subrata_modak56207ce2009-03-23 13:35:39 +0000223 if ((fildes = open(filename, flag, MODE)) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800224 tst_brkm(TBROK | TERRNO, cleanup, "open(%s) failed", filename);
plars865695b2001-08-27 22:15:12 +0000225 }
226
227 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000228 * Write the memory to the file.
229 */
plars865695b2001-08-27 22:15:12 +0000230
subrata_modak56207ce2009-03-23 13:35:39 +0000231 for (i = 0; i < WRITES; i++) {
232 TEST(write(fildes, dp, (unsigned)bytes));
233
234 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800235 tst_brkm(TBROK | TTERRNO, cleanup, "write() failed");
plars865695b2001-08-27 22:15:12 +0000236 }
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800237 } /* end for () */
plars865695b2001-08-27 22:15:12 +0000238
239 /*
subrata_modak56207ce2009-03-23 13:35:39 +0000240 * Attempt to close the file which also flushes the buffers.
241 */
plars865695b2001-08-27 22:15:12 +0000242
subrata_modak56207ce2009-03-23 13:35:39 +0000243 if (close(fildes) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800244 tst_brkm(TBROK | TERRNO, cleanup, "close() failed");
plars865695b2001-08-27 22:15:12 +0000245 }
246
subrata_modak56207ce2009-03-23 13:35:39 +0000247 ret = OK;
plars865695b2001-08-27 22:15:12 +0000248
Cyril Hrubise38b9612014-06-02 17:20:57 +0200249 /*
250 * Now check to see if the number of bytes written is the
251 * same as the number of bytes in the file.
252 */
plars865695b2001-08-27 22:15:12 +0000253
Cyril Hrubise38b9612014-06-02 17:20:57 +0200254 if (stat(filename, &buffer) == -1) {
255 tst_brkm(TBROK | TERRNO, cleanup, "stat() failed");
256 }
plars865695b2001-08-27 22:15:12 +0000257
Cyril Hrubise38b9612014-06-02 17:20:57 +0200258 if (buffer.st_size != (off_t) (bytes * WRITES)) {
259 ret = (int)buffer.st_size;
plars865695b2001-08-27 22:15:12 +0000260 }
261
subrata_modak56207ce2009-03-23 13:35:39 +0000262 if (unlink(filename) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800263 tst_brkm(TBROK | TERRNO, cleanup, "unlink(%s) failed",
264 filename);
subrata_modak56207ce2009-03-23 13:35:39 +0000265 }
plars865695b2001-08-27 22:15:12 +0000266
267 return ret;
268
subrata_modak56207ce2009-03-23 13:35:39 +0000269} /* end testrun() */
plars865695b2001-08-27 22:15:12 +0000270
271/***************************************************************
272 * setup() - performs all ONE TIME setup for this test.
273 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400274void setup(void)
plars865695b2001-08-27 22:15:12 +0000275{
Garrett Cooper2c282152010-12-16 00:55:50 -0800276
subrata_modak56207ce2009-03-23 13:35:39 +0000277 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000278
subrata_modak56207ce2009-03-23 13:35:39 +0000279 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000280
subrata_modak56207ce2009-03-23 13:35:39 +0000281 /* create a temporary directory and go to it */
282 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000283
subrata_modak56207ce2009-03-23 13:35:39 +0000284 /*
285 * Attempt to get some memory to work with.
286 */
plars865695b2001-08-27 22:15:12 +0000287
Cyril Hrubisd218f342014-09-23 13:14:56 +0200288 if ((dp = malloc((unsigned)BUFSIZ + 1)) == NULL) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800289 tst_brkm(TBROK | TERRNO, cleanup, "malloc() failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000290 }
plars865695b2001-08-27 22:15:12 +0000291
Garrett Cooper2c282152010-12-16 00:55:50 -0800292}
plars865695b2001-08-27 22:15:12 +0000293
294/***************************************************************
295 * cleanup() - performs all ONE TIME cleanup for this test at
296 * completion or premature exit.
297 ***************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400298void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000299{
plars865695b2001-08-27 22:15:12 +0000300
subrata_modak56207ce2009-03-23 13:35:39 +0000301 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700302}