write04.c revision 7d0a4a57fbcd47f72b67c08df532e8ef47f6fdae
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 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 * 41 * HISTORY 42 * ??/???? someone made this testcase but didn't add HISTORY 43 * 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> 53#include <string.h> 54#include "test.h" 55#include "usctest.h" 56 57#define PIPE_SIZE_TEST getpagesize() 58 59void alarm_handler(); 60void setup(); 61void cleanup(); 62 63char *TCID = "write04"; 64int TST_TOTAL = 1; 65extern int Tst_count; 66 67/* 0 terminated list of expected errnos */ 68int exp_enos[] = { 11, 0 }; 69 70char fifo[100] = "fifo"; 71static sigjmp_buf jmp; 72int rfd, wfd; 73 74int main(int argc, char **argv) 75{ 76 int lc; 77 char *msg; 78 79 struct stat buf; 80 int fail; 81 int cnt; 82 char wbuf[17 * PIPE_SIZE_TEST]; 83 struct sigaction sigptr; /* set up signal handler */ 84 85 /* parse standard options */ 86 if ((msg = parse_opts(argc, argv, NULL, NULL)) != 87 NULL) { 88 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); 89 } 90 91 /* global setup */ 92 setup(); 93 94 /* 95 * The following loop checks looping state if -i option given 96 */ 97 for (lc = 0; TEST_LOOPING(lc); lc++) { 98 /* reset Tst_count in case we are looping */ 99 Tst_count = 0; 100 101 if (mknod(fifo, S_IFIFO | 0777, 0) < 0) { 102 tst_resm(TBROK, "mknod() failed, errno: %d", errno); 103 cleanup(); 104 } 105 if (stat(fifo, &buf) != 0) { 106 tst_resm(TBROK, "stat() failed, errno: %d", errno); 107 cleanup(); 108 } 109 if ((buf.st_mode & S_IFIFO) == 0) { 110 tst_resm(TBROK, "Mode does not indicate fifo file"); 111 cleanup(); 112 } 113#if 0 114 sigset(SIGALRM, alarm_handler); 115#endif 116 sigptr.sa_handler = (void (*)(int signal))alarm_handler; 117 sigfillset(&sigptr.sa_mask); 118 sigptr.sa_flags = 0; 119 sigaddset(&sigptr.sa_mask, SIGALRM); 120 if (sigaction(SIGALRM, &sigptr, NULL) == -1) { 121 tst_resm(TBROK, "sigaction(): Failed"); 122 cleanup(); 123 } 124//block1: 125 tst_resm(TINFO, "Enter block 1: test for EAGAIN in write()"); 126 fail = 0; 127 128 (void)memset((void *)wbuf, 'A', 17 * PIPE_SIZE_TEST); 129 130 /* 131 * open the read end of the pipe 132 */ 133 if (sigsetjmp(jmp, 1)) { 134 tst_resm(TBROK, "Error reading fifo, read blocked"); 135 fail = 1; 136 } 137 (void)alarm(10); /* set alarm for 10 seconds */ 138 rfd = open(fifo, O_RDONLY | O_NONBLOCK); 139 (void)alarm(0); 140 if (rfd < 0) { 141 tst_resm(TBROK, "open() for reading the pipe failed"); 142 fail = 1; 143 } 144 145 /* 146 * open the write end of the pipe 147 */ 148 if (sigsetjmp(jmp, 1)) { 149 tst_resm(TBROK, "setjmp() failed"); 150 cleanup(); 151 } 152 (void)alarm(10); /* set alarm for 10 seconds */ 153 wfd = open(fifo, O_WRONLY | O_NONBLOCK); 154 (void)alarm(0); 155 if (wfd < 0) { 156 tst_resm(TBROK, "open() for writing the pipe failed"); 157 fail = 1; 158 } 159 160 /* 161 * attempt to fill the pipe with some data 162 */ 163 if (sigsetjmp(jmp, 1)) { 164 tst_resm(TBROK, "sigsetjmp() failed"); 165 fail = 1; 166 } 167 (void)alarm(10); 168 cnt = write(wfd, wbuf, 17 * PIPE_SIZE_TEST); 169 (void)alarm(0); 170 if (cnt == 17 * PIPE_SIZE_TEST) { 171 tst_resm(TBROK, "Error reading fifo, nozero read"); 172 fail = 1; 173 } 174 175 /* 176 * Now that the fifo is full try and send some more 177 */ 178 if (sigsetjmp(jmp, 1)) { 179 tst_resm(TBROK, "sigsetjmp() failed"); 180 fail = 1; 181 } 182 (void)alarm(10); 183 cnt = write(wfd, wbuf, 8 * PIPE_SIZE_TEST); 184 (void)alarm(0); 185 if (cnt != -1) { 186 tst_resm(TBROK, "write() failed to fail when pipe " 187 "is full"); 188 fail = 1; 189 } else { 190 TEST_ERROR_LOG(errno); 191 if (errno != EAGAIN) { 192 tst_resm(TBROK, "write set bad errno, expected " 193 "EAGAIN, got %d", errno); 194 fail = 1; 195 } 196 tst_resm(TINFO, "read() succeded in setting errno to " 197 "EAGAIN"); 198 } 199 if (fail) { 200 tst_resm(TFAIL, "Block 1 FAILED"); 201 } else { 202 tst_resm(TPASS, "Block 1 PASSED"); 203 } 204 tst_resm(TINFO, "Exit block 1"); 205 206 /* unlink fifo in case we are looping. */ 207 unlink(fifo); 208 } 209 cleanup(); 210 tst_exit(); 211} 212 213void alarm_handler() 214{ 215 siglongjmp(jmp, 1); 216} 217 218/* 219 * setup() 220 * performs all ONE TIME setup for this test 221 */ 222void setup(void) 223{ 224 225 tst_sig(FORK, DEF_HANDLER, cleanup); 226 227 /* Set up the expected error numbers for -e option */ 228 TEST_EXP_ENOS(exp_enos); 229 230 /* Pause if that option was specified 231 * TEST_PAUSE contains the code to fork the test with the -i option. 232 * You want to make sure you do this before you create your temporary 233 * directory. 234 */ 235 TEST_PAUSE; 236 237 /* Create a unique temporary directory and chdir() to it. */ 238 tst_tmpdir(); 239 240 /* create a temporary filename */ 241 sprintf(fifo, "%s.%d", fifo, getpid()); 242 243} 244 245void cleanup() 246{ 247 /* 248 * print errno log if that option was specified. 249 */ 250 TEST_CLEANUP; 251 252 close(rfd); 253 close(wfd); 254 unlink(fifo); 255 tst_rmdir(); 256 257 }