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 |
| 22 | * write04.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to check that write() sets errno to EAGAIN |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * Create a named pipe (fifo), open it in O_NONBLOCK mode, and |
| 29 | * attempt to write to it when it is full, write(2) should fail |
| 30 | * with EAGAIN. |
| 31 | * |
| 32 | * USAGE: <for command-line> |
| 33 | * write04 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 34 | * where, -c n : Run n copies concurrently. |
| 35 | * -e : Turn on errno logging. |
| 36 | * -i n : Execute test n times. |
| 37 | * -I x : Execute test for x seconds. |
| 38 | * -P x : Pause for x seconds between iterations. |
| 39 | * -t : Turn on syscall timing. |
| 40 | * |
robbiew | 4644c7e | 2002-04-26 14:33:32 +0000 | [diff] [blame] | 41 | * HISTORY |
| 42 | * ??/???? someone made this testcase but didn't add HISTORY |
| 43 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | * RESTRICTIONS |
| 45 | * NONE |
| 46 | */ |
| 47 | |
| 48 | #include <sys/stat.h> |
| 49 | #include <fcntl.h> |
| 50 | #include <signal.h> |
| 51 | #include <setjmp.h> |
| 52 | #include <errno.h> |
robbiew | 4644c7e | 2002-04-26 14:33:32 +0000 | [diff] [blame] | 53 | #include <string.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
robbiew | 4590c0d | 2005-10-04 16:16:23 +0000 | [diff] [blame] | 56 | #define PIPE_SIZE_TEST getpagesize() |
| 57 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | void alarm_handler(); |
| 59 | void setup(); |
| 60 | void cleanup(); |
| 61 | |
| 62 | char *TCID = "write04"; |
| 63 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | char fifo[100] = "fifo"; |
| 66 | static sigjmp_buf jmp; |
| 67 | int rfd, wfd; |
| 68 | |
robbiew | 6ee509b | 2003-04-01 20:28:41 +0000 | [diff] [blame] | 69 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | { |
| 71 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 72 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
| 74 | struct stat buf; |
| 75 | int fail; |
robbiew | 6ee509b | 2003-04-01 20:28:41 +0000 | [diff] [blame] | 76 | int cnt; |
robbiew | 4590c0d | 2005-10-04 16:16:23 +0000 | [diff] [blame] | 77 | char wbuf[17 * PIPE_SIZE_TEST]; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 78 | struct sigaction sigptr; /* set up signal handler */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 80 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 81 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 82 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
| 84 | /* global setup */ |
| 85 | setup(); |
| 86 | |
| 87 | /* |
| 88 | * The following loop checks looping state if -i option given |
| 89 | */ |
| 90 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 91 | /* reset tst_count in case we are looping */ |
| 92 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | |
| 94 | if (mknod(fifo, S_IFIFO | 0777, 0) < 0) { |
| 95 | tst_resm(TBROK, "mknod() failed, errno: %d", errno); |
| 96 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 97 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 98 | if (stat(fifo, &buf) != 0) { |
| 99 | tst_resm(TBROK, "stat() failed, errno: %d", errno); |
| 100 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 101 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | if ((buf.st_mode & S_IFIFO) == 0) { |
| 103 | tst_resm(TBROK, "Mode does not indicate fifo file"); |
| 104 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 105 | } |
iyermanoj | e0104b3 | 2002-11-08 17:21:47 +0000 | [diff] [blame] | 106 | #if 0 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | sigset(SIGALRM, alarm_handler); |
iyermanoj | e0104b3 | 2002-11-08 17:21:47 +0000 | [diff] [blame] | 108 | #endif |
| 109 | sigptr.sa_handler = (void (*)(int signal))alarm_handler; |
| 110 | sigfillset(&sigptr.sa_mask); |
| 111 | sigptr.sa_flags = 0; |
| 112 | sigaddset(&sigptr.sa_mask, SIGALRM); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 113 | if (sigaction(SIGALRM, &sigptr, NULL) == -1) { |
vapier | cff4af0 | 2006-02-11 04:46:30 +0000 | [diff] [blame] | 114 | tst_resm(TBROK, "sigaction(): Failed"); |
iyermanoj | e0104b3 | 2002-11-08 17:21:47 +0000 | [diff] [blame] | 115 | cleanup(); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | } |
robbiew | 6ee509b | 2003-04-01 20:28:41 +0000 | [diff] [blame] | 117 | //block1: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | tst_resm(TINFO, "Enter block 1: test for EAGAIN in write()"); |
| 119 | fail = 0; |
| 120 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 121 | (void)memset((void *)wbuf, 'A', 17 * PIPE_SIZE_TEST); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * open the read end of the pipe |
| 125 | */ |
| 126 | if (sigsetjmp(jmp, 1)) { |
| 127 | tst_resm(TBROK, "Error reading fifo, read blocked"); |
| 128 | fail = 1; |
| 129 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 130 | (void)alarm(10); /* set alarm for 10 seconds */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | rfd = open(fifo, O_RDONLY | O_NONBLOCK); |
| 132 | (void)alarm(0); |
| 133 | if (rfd < 0) { |
| 134 | tst_resm(TBROK, "open() for reading the pipe failed"); |
| 135 | fail = 1; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * open the write end of the pipe |
| 140 | */ |
| 141 | if (sigsetjmp(jmp, 1)) { |
| 142 | tst_resm(TBROK, "setjmp() failed"); |
| 143 | cleanup(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 144 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 145 | (void)alarm(10); /* set alarm for 10 seconds */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | wfd = open(fifo, O_WRONLY | O_NONBLOCK); |
| 147 | (void)alarm(0); |
| 148 | if (wfd < 0) { |
| 149 | tst_resm(TBROK, "open() for writing the pipe failed"); |
| 150 | fail = 1; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * attempt to fill the pipe with some data |
| 155 | */ |
| 156 | if (sigsetjmp(jmp, 1)) { |
| 157 | tst_resm(TBROK, "sigsetjmp() failed"); |
| 158 | fail = 1; |
| 159 | } |
| 160 | (void)alarm(10); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 161 | cnt = write(wfd, wbuf, 17 * PIPE_SIZE_TEST); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | (void)alarm(0); |
robbiew | 4590c0d | 2005-10-04 16:16:23 +0000 | [diff] [blame] | 163 | if (cnt == 17 * PIPE_SIZE_TEST) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | tst_resm(TBROK, "Error reading fifo, nozero read"); |
| 165 | fail = 1; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Now that the fifo is full try and send some more |
| 170 | */ |
| 171 | if (sigsetjmp(jmp, 1)) { |
| 172 | tst_resm(TBROK, "sigsetjmp() failed"); |
| 173 | fail = 1; |
| 174 | } |
| 175 | (void)alarm(10); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 176 | cnt = write(wfd, wbuf, 8 * PIPE_SIZE_TEST); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | (void)alarm(0); |
| 178 | if (cnt != -1) { |
| 179 | tst_resm(TBROK, "write() failed to fail when pipe " |
| 180 | "is full"); |
| 181 | fail = 1; |
| 182 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | if (errno != EAGAIN) { |
| 184 | tst_resm(TBROK, "write set bad errno, expected " |
| 185 | "EAGAIN, got %d", errno); |
| 186 | fail = 1; |
| 187 | } |
| 188 | tst_resm(TINFO, "read() succeded in setting errno to " |
| 189 | "EAGAIN"); |
| 190 | } |
| 191 | if (fail) { |
| 192 | tst_resm(TFAIL, "Block 1 FAILED"); |
| 193 | } else { |
| 194 | tst_resm(TPASS, "Block 1 PASSED"); |
| 195 | } |
| 196 | tst_resm(TINFO, "Exit block 1"); |
| 197 | |
| 198 | /* unlink fifo in case we are looping. */ |
| 199 | unlink(fifo); |
| 200 | } |
| 201 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 202 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 205 | void alarm_handler(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | { |
| 207 | siglongjmp(jmp, 1); |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * setup() |
| 212 | * performs all ONE TIME setup for this test |
| 213 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 214 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 215 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 216 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 217 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 218 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 219 | /* Pause if that option was specified |
| 220 | * TEST_PAUSE contains the code to fork the test with the -i option. |
| 221 | * You want to make sure you do this before you create your temporary |
| 222 | * directory. |
| 223 | */ |
| 224 | TEST_PAUSE; |
| 225 | |
| 226 | /* Create a unique temporary directory and chdir() to it. */ |
| 227 | tst_tmpdir(); |
| 228 | |
| 229 | /* create a temporary filename */ |
| 230 | sprintf(fifo, "%s.%d", fifo, getpid()); |
| 231 | |
| 232 | } |
| 233 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 234 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 235 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 236 | |
| 237 | close(rfd); |
| 238 | close(wfd); |
| 239 | unlink(fifo); |
| 240 | tst_rmdir(); |
| 241 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 242 | } |