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 | /* |
| 21 | * NAME |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 22 | * write02.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 30 | * Create a file and write some bytes out to it. |
| 31 | * Check the return count against the number returned. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 32 | * |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 47 | * None |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | */ |
robbiew | 6ee509b | 2003-04-01 20:28:41 +0000 | [diff] [blame] | 49 | #include <sys/types.h> |
| 50 | #include <sys/stat.h> |
| 51 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | #include <errno.h> |
| 53 | #include <stdio.h> |
| 54 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
plars | 48049f3 | 2003-07-05 03:12:42 +0000 | [diff] [blame] | 56 | char *TCID = "write02"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
| 59 | void cleanup(void); |
| 60 | void setup(void); |
| 61 | |
| 62 | char pfiln[40] = ""; |
| 63 | |
robbiew | 6ee509b | 2003-04-01 20:28:41 +0000 | [diff] [blame] | 64 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 66 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 67 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
| 69 | int cwrite; |
| 70 | int fild; |
| 71 | int iws; |
| 72 | int badcount = 0; |
| 73 | char pwbuf[BUFSIZ + 1]; |
| 74 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 75 | if ((msg = parse_opts(argc, argv, NULL, NULL))) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 76 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 77 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | setup(); /* global setup for test */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
| 81 | /* The following loop checks looping state if -i option given */ |
| 82 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 83 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 84 | /* reset tst_count in case we are looping */ |
| 85 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | |
robbiew | 6ee509b | 2003-04-01 20:28:41 +0000 | [diff] [blame] | 87 | //block1: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | 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 Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 98 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | for (iws = BUFSIZ; iws > 0; iws--) { |
| 100 | if ((cwrite = write(fild, pwbuf, iws)) != iws) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | 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 Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 114 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* |
| 118 | * setup() - performs all ONE TIME setup for this test |
| 119 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 120 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 122 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 131 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | TEST_PAUSE; |
| 133 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | tst_tmpdir(); |
| 135 | |
robbiew | 7056be6 | 2004-12-08 17:03:22 +0000 | [diff] [blame] | 136 | sprintf(pfiln, "write1.%d", getpid()); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* |
| 140 | * cleanup() - performs all ONE TIME cleanup for this test at completion or |
| 141 | * premature exit. |
| 142 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | |
| 146 | unlink(pfiln); |
| 147 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 149 | } |