blob: a9c02af9384b04e0142e50e68a32422305f399c9 [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/*
21 * NAME
subrata_modak56207ce2009-03-23 13:35:39 +000022 * write02.c
plars865695b2001-08-27 22:15:12 +000023 *
24 * DESCRIPTION
25 * Basic functionality test: does the return from write match the count
26 * of the number of bytes written.
27 *
28 *
29 * ALGORITHM
subrata_modak56207ce2009-03-23 13:35:39 +000030 * Create a file and write some bytes out to it.
31 * Check the return count against the number returned.
plars865695b2001-08-27 22:15:12 +000032 *
33 * USAGE: <for command-line>
34 * write02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
35 * where, -c n : Run n copies concurrently.
36 * -e : Turn on errno logging.
37 * -i n : Execute test n times.
38 * -I x : Execute test for x seconds.
39 * -P x : Pause for x seconds between iterations.
40 * -t : Turn on syscall timing.
41 *
42 * History
43 * 07/2001 John George
44 * -Ported
45 *
46 * Restrictions
subrata_modak56207ce2009-03-23 13:35:39 +000047 * None
plars865695b2001-08-27 22:15:12 +000048 */
robbiew6ee509b2003-04-01 20:28:41 +000049#include <sys/types.h>
50#include <sys/stat.h>
51#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000052#include <errno.h>
53#include <stdio.h>
54#include "test.h"
plars865695b2001-08-27 22:15:12 +000055
plars48049f32003-07-05 03:12:42 +000056char *TCID = "write02";
plars865695b2001-08-27 22:15:12 +000057int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000058
59void cleanup(void);
60void setup(void);
61
62char pfiln[40] = "";
63
robbiew6ee509b2003-04-01 20:28:41 +000064int main(int argc, char **argv)
plars865695b2001-08-27 22:15:12 +000065{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020066 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020067 const char *msg;
plars865695b2001-08-27 22:15:12 +000068
69 int cwrite;
70 int fild;
71 int iws;
72 int badcount = 0;
73 char pwbuf[BUFSIZ + 1];
74
Garrett Cooper45e285d2010-11-22 12:19:25 -080075 if ((msg = parse_opts(argc, argv, NULL, NULL))) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080076 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Wanlong Gao354ebb42012-12-07 10:10:04 +080077 }
plars865695b2001-08-27 22:15:12 +000078
subrata_modak56207ce2009-03-23 13:35:39 +000079 setup(); /* global setup for test */
plars865695b2001-08-27 22:15:12 +000080
81 /* The following loop checks looping state if -i option given */
82 for (lc = 0; TEST_LOOPING(lc); lc++) {
83
Caspar Zhangd59a6592013-03-07 14:59:12 +080084 /* reset tst_count in case we are looping */
85 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000086
robbiew6ee509b2003-04-01 20:28:41 +000087//block1:
plars865695b2001-08-27 22:15:12 +000088 tst_resm(TINFO, "Block 1: test to see write() returns proper "
89 "write count");
90
91 for (iws = 0; iws < BUFSIZ; iws++) {
92 pwbuf[iws] = 'A' + (iws % 26);
93 }
94 pwbuf[BUFSIZ] = '\n';
95
96 if ((fild = creat(pfiln, 0777)) == -1) {
97 tst_brkm(TBROK, cleanup, "Can't creat Xwrit");
Wanlong Gao354ebb42012-12-07 10:10:04 +080098 }
plars865695b2001-08-27 22:15:12 +000099 for (iws = BUFSIZ; iws > 0; iws--) {
100 if ((cwrite = write(fild, pwbuf, iws)) != iws) {
plars865695b2001-08-27 22:15:12 +0000101 badcount++;
102 tst_resm(TINFO, "bad write count");
103 }
104 }
105 if (badcount != 0) {
106 tst_resm(TFAIL, "write() FAILED to return proper cnt");
107 } else {
108 tst_resm(TPASS, "write() PASSED");
109 }
110 tst_resm(TINFO, "block 1 passed");
111 close(fild);
112 }
113 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800114 tst_exit();
plars865695b2001-08-27 22:15:12 +0000115}
116
117/*
118 * setup() - performs all ONE TIME setup for this test
119 */
subrata_modak56207ce2009-03-23 13:35:39 +0000120void setup(void)
plars865695b2001-08-27 22:15:12 +0000121{
Garrett Cooper2c282152010-12-16 00:55:50 -0800122
plars865695b2001-08-27 22:15:12 +0000123 tst_sig(FORK, DEF_HANDLER, cleanup);
124
125 umask(0);
126
127 /* Pause if that option was specified
128 * TEST_PAUSE contains the code to fork the test with the -i option.
129 * You want to make sure you do this before you create your temporary
130 * directory.
subrata_modak56207ce2009-03-23 13:35:39 +0000131 */
plars865695b2001-08-27 22:15:12 +0000132 TEST_PAUSE;
133
plars865695b2001-08-27 22:15:12 +0000134 tst_tmpdir();
135
robbiew7056be62004-12-08 17:03:22 +0000136 sprintf(pfiln, "write1.%d", getpid());
plars865695b2001-08-27 22:15:12 +0000137}
138
139/*
140 * cleanup() - performs all ONE TIME cleanup for this test at completion or
141 * premature exit.
142 */
subrata_modak56207ce2009-03-23 13:35:39 +0000143void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000144{
plars865695b2001-08-27 22:15:12 +0000145
146 unlink(pfiln);
147
plars865695b2001-08-27 22:15:12 +0000148 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700149}