0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10
|
|
11
|
50
|
12 #define NGX_IOVS 16
|
|
13
|
|
14
|
18
|
15 #if (NGX_HAVE_KQUEUE)
|
0
|
16
|
50
|
17 ssize_t
|
|
18 ngx_readv_chain(ngx_connection_t *c, ngx_chain_t *chain)
|
0
|
19 {
|
|
20 u_char *prev;
|
|
21 ssize_t n, size;
|
|
22 ngx_err_t err;
|
50
|
23 ngx_array_t vec;
|
0
|
24 ngx_event_t *rev;
|
50
|
25 struct iovec *iov, iovs[NGX_IOVS];
|
0
|
26
|
126
|
27 rev = c->read;
|
0
|
28
|
4
|
29 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
|
0
|
30 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
31 "readv: eof:%d, avail:%d, err:%d",
|
|
32 rev->pending_eof, rev->available, rev->kq_errno);
|
|
33
|
|
34 if (rev->available == 0) {
|
130
|
35
|
|
36 if (!rev->pending_eof) {
|
|
37 return NGX_AGAIN;
|
|
38 }
|
|
39
|
|
40 /* FreeBSD 5.x-6.x may erroneously report ETIMEDOUT */
|
|
41 if (rev->kq_errno != NGX_ETIMEDOUT) {
|
|
42
|
0
|
43 rev->ready = 0;
|
|
44 rev->eof = 1;
|
|
45
|
|
46 if (rev->kq_errno) {
|
|
47 rev->error = 1;
|
|
48 ngx_set_socket_errno(rev->kq_errno);
|
130
|
49
|
|
50 return ngx_connection_error(c, rev->kq_errno,
|
|
51 "kevent() reported about an closed connection");
|
0
|
52 }
|
|
53
|
|
54 return 0;
|
|
55 }
|
|
56 }
|
|
57 }
|
|
58
|
|
59 prev = NULL;
|
|
60 iov = NULL;
|
|
61 size = 0;
|
|
62
|
50
|
63 vec.elts = iovs;
|
|
64 vec.nelts = 0;
|
|
65 vec.size = sizeof(struct iovec);
|
|
66 vec.nalloc = NGX_IOVS;
|
|
67 vec.pool = c->pool;
|
0
|
68
|
|
69 /* coalesce the neighbouring bufs */
|
|
70
|
|
71 while (chain) {
|
|
72 if (prev == chain->buf->last) {
|
|
73 iov->iov_len += chain->buf->end - chain->buf->last;
|
|
74
|
|
75 } else {
|
50
|
76 iov = ngx_array_push(&vec);
|
|
77 if (iov == NULL) {
|
|
78 return NGX_ERROR;
|
|
79 }
|
|
80
|
0
|
81 iov->iov_base = (void *) chain->buf->last;
|
|
82 iov->iov_len = chain->buf->end - chain->buf->last;
|
|
83 }
|
|
84
|
|
85 size += chain->buf->end - chain->buf->last;
|
|
86 prev = chain->buf->end;
|
|
87 chain = chain->next;
|
|
88 }
|
|
89
|
|
90 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
50
|
91 "readv: %d, last:%d", vec.nelts, iov->iov_len);
|
0
|
92
|
|
93 rev = c->read;
|
|
94
|
|
95 do {
|
50
|
96 n = readv(c->fd, (struct iovec *) vec.elts, vec.nelts);
|
0
|
97
|
|
98 if (n >= 0) {
|
4
|
99 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
|
0
|
100 rev->available -= n;
|
|
101
|
|
102 /*
|
|
103 * rev->available can be negative here because some additional
|
|
104 * bytes can be received between kevent() and recv()
|
|
105 */
|
|
106
|
|
107 if (rev->available <= 0) {
|
|
108 if (!rev->pending_eof) {
|
|
109 rev->ready = 0;
|
|
110 }
|
|
111
|
|
112 if (rev->available < 0) {
|
|
113 rev->available = 0;
|
|
114 }
|
|
115 }
|
|
116
|
|
117 return n;
|
|
118 }
|
|
119
|
|
120 if (n < size) {
|
|
121 rev->ready = 0;
|
|
122 }
|
|
123
|
|
124 if (n == 0) {
|
|
125 rev->eof = 1;
|
|
126 }
|
|
127
|
|
128 return n;
|
|
129 }
|
|
130
|
|
131 err = ngx_socket_errno;
|
|
132
|
|
133 if (err == NGX_EAGAIN || err == NGX_EINTR) {
|
|
134 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
|
|
135 "readv() not ready");
|
|
136 n = NGX_AGAIN;
|
|
137
|
|
138 } else {
|
|
139 n = ngx_connection_error(c, err, "readv() failed");
|
|
140 break;
|
|
141 }
|
|
142
|
|
143 } while (err == NGX_EINTR);
|
|
144
|
|
145 rev->ready = 0;
|
|
146
|
|
147 if (n == NGX_ERROR){
|
|
148 c->read->error = 1;
|
|
149 }
|
|
150
|
|
151 return n;
|
|
152 }
|
|
153
|
18
|
154 #else /* ! NGX_HAVE_KQUEUE */
|
0
|
155
|
50
|
156 ssize_t
|
|
157 ngx_readv_chain(ngx_connection_t *c, ngx_chain_t *chain)
|
0
|
158 {
|
|
159 u_char *prev;
|
|
160 ssize_t n, size;
|
|
161 ngx_err_t err;
|
50
|
162 ngx_array_t vec;
|
0
|
163 ngx_event_t *rev;
|
50
|
164 struct iovec *iov, iovs[NGX_IOVS];
|
0
|
165
|
|
166 prev = NULL;
|
|
167 iov = NULL;
|
|
168 size = 0;
|
|
169
|
50
|
170 vec.elts = iovs;
|
|
171 vec.nelts = 0;
|
|
172 vec.size = sizeof(struct iovec);
|
|
173 vec.nalloc = NGX_IOVS;
|
|
174 vec.pool = c->pool;
|
0
|
175
|
|
176 /* coalesce the neighbouring bufs */
|
|
177
|
|
178 while (chain) {
|
|
179 if (prev == chain->buf->last) {
|
|
180 iov->iov_len += chain->buf->end - chain->buf->last;
|
|
181
|
|
182 } else {
|
50
|
183 iov = ngx_array_push(&vec);
|
|
184 if (iov == NULL) {
|
|
185 return NGX_ERROR;
|
|
186 }
|
|
187
|
110
|
188 iov->iov_base = (void *) chain->buf->last;
|
0
|
189 iov->iov_len = chain->buf->end - chain->buf->last;
|
|
190 }
|
|
191
|
|
192 size += chain->buf->end - chain->buf->last;
|
|
193 prev = chain->buf->end;
|
|
194 chain = chain->next;
|
|
195 }
|
|
196
|
|
197 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
50
|
198 "readv: %d:%d", vec.nelts, iov->iov_len);
|
0
|
199
|
|
200 rev = c->read;
|
|
201
|
|
202 do {
|
50
|
203 n = readv(c->fd, (struct iovec *) vec.elts, vec.nelts);
|
0
|
204
|
|
205 if (n == 0) {
|
|
206 rev->ready = 0;
|
|
207 rev->eof = 1;
|
|
208
|
|
209 return n;
|
|
210
|
|
211 } else if (n > 0) {
|
|
212
|
4
|
213 if (n < size && !(ngx_event_flags & NGX_USE_GREEDY_EVENT)) {
|
0
|
214 rev->ready = 0;
|
|
215 }
|
|
216
|
|
217 return n;
|
|
218 }
|
|
219
|
|
220 err = ngx_socket_errno;
|
|
221
|
|
222 if (err == NGX_EAGAIN || err == NGX_EINTR) {
|
|
223 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
|
|
224 "readv() not ready");
|
|
225 n = NGX_AGAIN;
|
|
226
|
|
227 } else {
|
|
228 n = ngx_connection_error(c, err, "readv() failed");
|
|
229 break;
|
|
230 }
|
|
231
|
|
232 } while (err == NGX_EINTR);
|
|
233
|
|
234 rev->ready = 0;
|
|
235
|
|
236 if (n == NGX_ERROR){
|
|
237 c->read->error = 1;
|
|
238 }
|
|
239
|
|
240 return n;
|
|
241 }
|
|
242
|
18
|
243 #endif /* NGX_HAVE_KQUEUE */
|