Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Functions operating on SOCK_STREAM and buffers. |
| 3 | * |
Willy Tarreau | e09e0ce | 2007-03-18 16:31:29 +0100 | [diff] [blame] | 4 | * Copyright 2000-2007 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <errno.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/types.h> |
| 21 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #include <common/compat.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 23 | #include <common/config.h> |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 24 | #include <common/standard.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 25 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | #include <types/buffers.h> |
| 28 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | #include <types/polling.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | |
| 31 | #include <proto/client.h> |
| 32 | #include <proto/fd.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | #include <proto/stream_sock.h> |
| 34 | #include <proto/task.h> |
| 35 | |
| 36 | |
| 37 | /* |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 38 | * this function is called on a read event from a stream socket. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 39 | * It returns 0 if we have a high confidence that we will not be |
| 40 | * able to read more data without polling first. Returns non-zero |
| 41 | * otherwise. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 43 | int stream_sock_read(int fd) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 44 | __label__ out_eternity, out_wakeup, out_error; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 45 | struct buffer *b = fdtab[fd].cb[DIR_RD].b; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 46 | int ret, max, retval; |
Willy Tarreau | b8949f1 | 2007-03-23 22:39:59 +0100 | [diff] [blame] | 47 | int read_poll = MAX_READ_POLL_LOOPS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | |
| 49 | #ifdef DEBUG_FULL |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 50 | fprintf(stderr,"stream_sock_read : fd=%d, owner=%p\n", fd, fdtab[fd].owner); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | #endif |
| 52 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 53 | retval = 1; |
| 54 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 55 | if (unlikely(fdtab[fd].ev & FD_POLL_HUP)) { |
| 56 | /* connection closed */ |
| 57 | b->flags |= BF_READ_NULL; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 58 | goto out_eternity; |
| 59 | } |
| 60 | else if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) { |
| 61 | goto out_error; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 62 | } |
| 63 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 64 | while (1) { |
| 65 | /* |
| 66 | * 1. compute the maximum block size we can read at once. |
| 67 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 68 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 69 | b->r = b->w = b->lr = b->data; |
| 70 | max = b->rlim - b->data; |
| 71 | } |
| 72 | else if (b->r > b->w) { |
| 73 | max = b->rlim - b->r; |
| 74 | } |
| 75 | else { |
| 76 | max = b->w - b->r; |
| 77 | /* FIXME: theorically, if w>0, we shouldn't have rlim < data+size anymore |
| 78 | * since it means that the rewrite protection has been removed. This |
| 79 | * implies that the if statement can be removed. |
| 80 | */ |
| 81 | if (max > b->rlim - b->data) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 82 | max = b->rlim - b->data; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 83 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 84 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 85 | if (unlikely(max == 0)) { |
| 86 | /* Not anymore room to store data. This should theorically |
| 87 | * never happen, but better safe than sorry ! |
| 88 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 89 | EV_FD_CLR(fd, DIR_RD); |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 90 | goto out_eternity; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 91 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 92 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 93 | /* |
| 94 | * 2. read the largest possible block |
| 95 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 96 | #ifndef MSG_NOSIGNAL |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 97 | { |
| 98 | int skerr; |
| 99 | socklen_t lskerr = sizeof(skerr); |
| 100 | |
| 101 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 102 | if (ret == -1 || skerr) |
| 103 | ret = -1; |
| 104 | else |
| 105 | ret = recv(fd, b->r, max, 0); |
| 106 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 107 | #else |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 108 | ret = recv(fd, b->r, max, MSG_NOSIGNAL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 109 | #endif |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 110 | if (ret > 0) { |
| 111 | b->r += ret; |
| 112 | b->l += ret; |
| 113 | b->flags |= BF_PARTIAL_READ; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 114 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 115 | if (b->r == b->data + BUFSIZE) { |
| 116 | b->r = b->data; /* wrap around the buffer */ |
| 117 | } |
Willy Tarreau | 9641e8f | 2007-03-23 23:02:09 +0100 | [diff] [blame] | 118 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 119 | b->total += ret; |
Willy Tarreau | 9641e8f | 2007-03-23 23:02:09 +0100 | [diff] [blame] | 120 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 121 | if (b->l == b->rlim - b->data) { |
| 122 | /* The buffer is now full, there's no point in going through |
| 123 | * the loop again. |
| 124 | */ |
| 125 | EV_FD_CLR(fd, DIR_RD); |
| 126 | goto out_eternity; |
| 127 | } |
| 128 | |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 129 | /* if too many bytes were missing from last read, it means that |
| 130 | * it's pointless trying to read again because the system does |
| 131 | * not have them in buffers. |
| 132 | */ |
| 133 | if (ret < max) |
| 134 | break; |
| 135 | |
| 136 | /* generally if we read something smaller than 1 or 2 MSS, |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 137 | * it means that it's not worth trying to read again. It may |
| 138 | * also happen on headers, but the application then can stop |
| 139 | * reading before we start polling. |
| 140 | */ |
| 141 | if (ret < MIN_RET_FOR_READ_LOOP) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 142 | break; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 143 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 144 | if (--read_poll <= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 145 | break; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 146 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 147 | } |
| 148 | else if (ret == 0) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 149 | /* connection closed */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 150 | b->flags |= BF_READ_NULL; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 151 | goto out_eternity; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 152 | } |
Willy Tarreau | 9f19529 | 2007-04-15 21:26:58 +0200 | [diff] [blame] | 153 | else if (errno == EAGAIN) { |
| 154 | /* Ignore EAGAIN but inform the poller that there is |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 155 | * nothing to read left. But we may have done some work |
| 156 | * justifying to notify the task. |
Willy Tarreau | 9f19529 | 2007-04-15 21:26:58 +0200 | [diff] [blame] | 157 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 158 | retval = 0; |
| 159 | break; |
| 160 | } |
| 161 | else { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 162 | goto out_error; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 163 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 164 | } /* while (1) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 165 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 166 | /* |
| 167 | * The only way to get out of this loop is to have stopped reading |
| 168 | * without any error nor close, either by limiting the number of |
| 169 | * loops, or because of an EAGAIN. We only rearm the timer if we |
| 170 | * have at least read something. |
| 171 | */ |
| 172 | |
| 173 | if (b->flags & BF_PARTIAL_READ) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 174 | if (tv_add_ifset(&b->rex, &now, &b->rto)) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 175 | goto out_wakeup; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 176 | out_eternity: |
| 177 | tv_eternity(&b->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 180 | out_wakeup: |
| 181 | if (b->flags & BF_READ_STATUS) |
| 182 | task_wakeup(fdtab[fd].owner); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 183 | fdtab[fd].ev &= ~FD_POLL_RD; |
| 184 | return retval; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 185 | |
| 186 | out_error: |
| 187 | /* There was an error. we must wakeup the task. No need to clear |
| 188 | * the events, the task will do it. |
| 189 | */ |
| 190 | fdtab[fd].state = FD_STERROR; |
| 191 | b->flags |= BF_READ_ERROR; |
| 192 | goto out_eternity; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | |
| 196 | /* |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 197 | * this function is called on a write event from a stream socket. |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 198 | * It returns 0 if we have a high confidence that we will not be |
| 199 | * able to write more data without polling first. Returns non-zero |
| 200 | * otherwise. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 201 | */ |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 202 | int stream_sock_write(int fd) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 203 | __label__ out_eternity, out_wakeup, out_error; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 204 | struct buffer *b = fdtab[fd].cb[DIR_WR].b; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 205 | int ret, max, retval; |
| 206 | int write_poll = MAX_WRITE_POLL_LOOPS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 207 | |
| 208 | #ifdef DEBUG_FULL |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 209 | fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 210 | #endif |
| 211 | |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 212 | retval = 1; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 213 | if (unlikely(fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))) |
| 214 | goto out_error; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 215 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 216 | while (1) { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 217 | if (b->l == 0) { /* let's realign the buffer to optimize I/O */ |
| 218 | b->r = b->w = b->lr = b->data; |
| 219 | max = 0; |
| 220 | } |
| 221 | else if (b->r > b->w) { |
| 222 | max = b->r - b->w; |
| 223 | } |
| 224 | else { |
| 225 | max = b->data + BUFSIZE - b->w; |
| 226 | } |
| 227 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 228 | if (max == 0) { |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 229 | /* may be we have received a connection acknowledgement in TCP mode without data */ |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 230 | if (likely(fdtab[fd].state == FD_STCONN)) { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 231 | /* We have no data to send to check the connection, and |
| 232 | * getsockopt() will not inform us whether the connection |
| 233 | * is still pending. So we'll reuse connect() to check the |
| 234 | * state of the socket. This has the advantage of givig us |
| 235 | * the following info : |
| 236 | * - error |
| 237 | * - connecting (EALREADY, EINPROGRESS) |
| 238 | * - connected (EISCONN, 0) |
| 239 | */ |
Willy Tarreau | e94ebd0 | 2007-10-09 17:14:37 +0200 | [diff] [blame^] | 240 | if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0)) |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 241 | errno = 0; |
| 242 | |
| 243 | if (errno == EALREADY || errno == EINPROGRESS) { |
| 244 | retval = 0; |
| 245 | goto out_wakeup; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 246 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 247 | |
| 248 | if (errno && errno != EISCONN) |
| 249 | goto out_error; |
| 250 | |
| 251 | /* OK we just need to indicate that we got a connection |
| 252 | * and that we wrote nothing. |
| 253 | */ |
| 254 | b->flags |= BF_WRITE_NULL; |
| 255 | fdtab[fd].state = FD_STREADY; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 258 | /* Funny, we were called to write something but there wasn't |
| 259 | * anything. Theorically we cannot get there, but just in case, |
| 260 | * let's disable the write event and pretend we never came there. |
| 261 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 262 | EV_FD_CLR(fd, DIR_WR); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 263 | goto out_eternity; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | #ifndef MSG_NOSIGNAL |
| 267 | { |
| 268 | int skerr; |
| 269 | socklen_t lskerr = sizeof(skerr); |
| 270 | |
Willy Tarreau | c642348 | 2006-10-15 14:59:03 +0200 | [diff] [blame] | 271 | ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr); |
| 272 | if (ret == -1 || skerr) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 273 | ret = -1; |
| 274 | else |
| 275 | ret = send(fd, b->w, max, MSG_DONTWAIT); |
| 276 | } |
| 277 | #else |
| 278 | ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL); |
| 279 | #endif |
| 280 | |
| 281 | if (ret > 0) { |
| 282 | b->l -= ret; |
| 283 | b->w += ret; |
| 284 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 285 | b->flags |= BF_PARTIAL_WRITE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 286 | |
| 287 | if (b->w == b->data + BUFSIZE) { |
| 288 | b->w = b->data; /* wrap around the buffer */ |
| 289 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 290 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 291 | if (!b->l) { |
| 292 | EV_FD_CLR(fd, DIR_WR); |
| 293 | goto out_eternity; |
| 294 | } |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 295 | |
Willy Tarreau | ab3e1d3 | 2007-06-03 14:10:36 +0200 | [diff] [blame] | 296 | /* if the system buffer is full, don't insist */ |
| 297 | if (ret < max) |
| 298 | break; |
| 299 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 300 | if (--write_poll <= 0) |
| 301 | break; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 302 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 303 | else if (ret == 0 || errno == EAGAIN) { |
| 304 | /* nothing written, just pretend we were never called |
| 305 | * and wait for the socket to be ready. But we may have |
| 306 | * done some work justifying to notify the task. |
| 307 | */ |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 308 | retval = 0; |
| 309 | break; |
| 310 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 311 | else { |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 312 | goto out_error; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 313 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 314 | } /* while (1) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 315 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 316 | /* |
| 317 | * The only way to get out of this loop is to have stopped writing |
| 318 | * without any error, either by limiting the number of loops, or |
| 319 | * because of an EAGAIN. We only rearm the timer if we have at least |
| 320 | * written something. |
| 321 | */ |
| 322 | |
| 323 | if (b->flags & BF_PARTIAL_WRITE) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 324 | if (tv_add_ifset(&b->wex, &now, &b->wto)) { |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 325 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 326 | * we refresh it. A solution would be to merge read+write timeouts into a |
| 327 | * unique one, although that needs some study particularly on full-duplex |
| 328 | * TCP connections. */ |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 329 | if (!(b->flags & BF_SHUTR_STATUS)) |
| 330 | b->rex = b->wex; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 331 | goto out_wakeup; |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 332 | } |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 333 | out_eternity: |
| 334 | tv_eternity(&b->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 335 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 336 | |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 337 | out_wakeup: |
| 338 | if (b->flags & BF_WRITE_STATUS) |
| 339 | task_wakeup(fdtab[fd].owner); |
Willy Tarreau | 8374918 | 2007-04-15 20:56:27 +0200 | [diff] [blame] | 340 | fdtab[fd].ev &= ~FD_POLL_WR; |
| 341 | return retval; |
Willy Tarreau | 6996e15 | 2007-04-30 14:37:43 +0200 | [diff] [blame] | 342 | |
| 343 | out_error: |
| 344 | /* There was an error. we must wakeup the task. No need to clear |
| 345 | * the events, the task will do it. |
| 346 | */ |
| 347 | fdtab[fd].state = FD_STERROR; |
| 348 | b->flags |= BF_WRITE_ERROR; |
| 349 | goto out_eternity; |
| 350 | |
| 351 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 354 | |
| 355 | |
| 356 | /* |
| 357 | * Local variables: |
| 358 | * c-indent-level: 8 |
| 359 | * c-basic-offset: 8 |
| 360 | * End: |
| 361 | */ |