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