Mercurial > hg > nginx-quic
annotate src/os/unix/ngx_readv_chain.c @ 6081:12ab5cd445c0 stable-1.6
Core: fixed potential buffer overrun when initializing hash.
Initial size as calculated from the number of elements may be bigger
than max_size. If this happens, make sure to set size to max_size.
Reported by Chris West.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Tue, 24 Feb 2015 18:37:14 +0300 |
parents | ee78c7705a8e |
children | d1bde5c3c5d2 |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
375
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
375
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
4412 | 4 * Copyright (C) Nginx, Inc. |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
375
diff
changeset
|
5 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
375
diff
changeset
|
6 |
73
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 |
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_config.h> |
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
9 #include <ngx_core.h> |
103
6dfda4cf5200
nginx-0.0.1-2003-06-11-19:28:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
93
diff
changeset
|
10 #include <ngx_event.h> |
73
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
11 |
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
12 |
501 | 13 #define NGX_IOVS 16 |
14 | |
15 | |
469 | 16 #if (NGX_HAVE_KQUEUE) |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
17 |
501 | 18 ssize_t |
19 ngx_readv_chain(ngx_connection_t *c, ngx_chain_t *chain) | |
73
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
20 { |
290
87e73f067470
nginx-0.0.2-2004-03-16-10:10:12 import
Igor Sysoev <igor@sysoev.ru>
parents:
257
diff
changeset
|
21 u_char *prev; |
164
84036764e215
nginx-0.0.1-2003-10-29-11:30:44 import
Igor Sysoev <igor@sysoev.ru>
parents:
163
diff
changeset
|
22 ssize_t n, size; |
74
17ab1af8c3dd
nginx-0.0.1-2003-04-11-20:01:14 import
Igor Sysoev <igor@sysoev.ru>
parents:
73
diff
changeset
|
23 ngx_err_t err; |
501 | 24 ngx_array_t vec; |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
25 ngx_event_t *rev; |
501 | 26 struct iovec *iov, iovs[NGX_IOVS]; |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
27 |
577 | 28 rev = c->read; |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
29 |
455 | 30 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) { |
257
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
31 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0, |
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
32 "readv: eof:%d, avail:%d, err:%d", |
375
744ccb59062d
nginx-0.0.7-2004-07-02-19:54:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
343
diff
changeset
|
33 rev->pending_eof, rev->available, rev->kq_errno); |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
34 |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
35 if (rev->available == 0) { |
587 | 36 if (rev->pending_eof) { |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
37 rev->ready = 0; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
38 rev->eof = 1; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
39 |
587 | 40 ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno, |
41 "kevent() reported about an closed connection"); | |
42 | |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
43 if (rev->kq_errno) { |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
44 rev->error = 1; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
45 ngx_set_socket_errno(rev->kq_errno); |
587 | 46 return NGX_ERROR; |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
47 } |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
48 |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
49 return 0; |
587 | 50 |
51 } else { | |
52 return NGX_AGAIN; | |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
53 } |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
54 } |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
55 } |
73
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
56 |
163
fb61ba77beba
nginx-0.0.1-2003-10-28-18:45:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
155
diff
changeset
|
57 prev = NULL; |
89
29bf798b583f
nginx-0.0.1-2003-05-15-19:42:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
83
diff
changeset
|
58 iov = NULL; |
164
84036764e215
nginx-0.0.1-2003-10-29-11:30:44 import
Igor Sysoev <igor@sysoev.ru>
parents:
163
diff
changeset
|
59 size = 0; |
89
29bf798b583f
nginx-0.0.1-2003-05-15-19:42:53 import
Igor Sysoev <igor@sysoev.ru>
parents:
83
diff
changeset
|
60 |
501 | 61 vec.elts = iovs; |
62 vec.nelts = 0; | |
63 vec.size = sizeof(struct iovec); | |
64 vec.nalloc = NGX_IOVS; | |
65 vec.pool = c->pool; | |
73
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
66 |
343
6bdf858bff8c
nginx-0.0.3-2004-05-28-19:49:23 import; rename ngx_hunk_t to ngx_buf_t
Igor Sysoev <igor@sysoev.ru>
parents:
328
diff
changeset
|
67 /* coalesce the neighbouring bufs */ |
150
ad5f382c9e7d
nginx-0.0.1-2003-10-19-23:57:23 import
Igor Sysoev <igor@sysoev.ru>
parents:
103
diff
changeset
|
68 |
155
46eb23d9471d
nginx-0.0.1-2003-10-22-20:38:26 import
Igor Sysoev <igor@sysoev.ru>
parents:
151
diff
changeset
|
69 while (chain) { |
343
6bdf858bff8c
nginx-0.0.3-2004-05-28-19:49:23 import; rename ngx_hunk_t to ngx_buf_t
Igor Sysoev <igor@sysoev.ru>
parents:
328
diff
changeset
|
70 if (prev == chain->buf->last) { |
6bdf858bff8c
nginx-0.0.3-2004-05-28-19:49:23 import; rename ngx_hunk_t to ngx_buf_t
Igor Sysoev <igor@sysoev.ru>
parents:
328
diff
changeset
|
71 iov->iov_len += chain->buf->end - chain->buf->last; |
163
fb61ba77beba
nginx-0.0.1-2003-10-28-18:45:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
155
diff
changeset
|
72 |
fb61ba77beba
nginx-0.0.1-2003-10-28-18:45:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
155
diff
changeset
|
73 } else { |
4597
4be36d5e78ae
Fixed ngx_readv_chain() to honor IOV_MAX (ticket #14).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4412
diff
changeset
|
74 if (vec.nelts >= IOV_MAX) { |
4be36d5e78ae
Fixed ngx_readv_chain() to honor IOV_MAX (ticket #14).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4412
diff
changeset
|
75 break; |
4be36d5e78ae
Fixed ngx_readv_chain() to honor IOV_MAX (ticket #14).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4412
diff
changeset
|
76 } |
4be36d5e78ae
Fixed ngx_readv_chain() to honor IOV_MAX (ticket #14).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4412
diff
changeset
|
77 |
501 | 78 iov = ngx_array_push(&vec); |
79 if (iov == NULL) { | |
80 return NGX_ERROR; | |
81 } | |
82 | |
343
6bdf858bff8c
nginx-0.0.3-2004-05-28-19:49:23 import; rename ngx_hunk_t to ngx_buf_t
Igor Sysoev <igor@sysoev.ru>
parents:
328
diff
changeset
|
83 iov->iov_base = (void *) chain->buf->last; |
6bdf858bff8c
nginx-0.0.3-2004-05-28-19:49:23 import; rename ngx_hunk_t to ngx_buf_t
Igor Sysoev <igor@sysoev.ru>
parents:
328
diff
changeset
|
84 iov->iov_len = chain->buf->end - chain->buf->last; |
163
fb61ba77beba
nginx-0.0.1-2003-10-28-18:45:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
155
diff
changeset
|
85 } |
fb61ba77beba
nginx-0.0.1-2003-10-28-18:45:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
155
diff
changeset
|
86 |
343
6bdf858bff8c
nginx-0.0.3-2004-05-28-19:49:23 import; rename ngx_hunk_t to ngx_buf_t
Igor Sysoev <igor@sysoev.ru>
parents:
328
diff
changeset
|
87 size += chain->buf->end - chain->buf->last; |
6bdf858bff8c
nginx-0.0.3-2004-05-28-19:49:23 import; rename ngx_hunk_t to ngx_buf_t
Igor Sysoev <igor@sysoev.ru>
parents:
328
diff
changeset
|
88 prev = chain->buf->end; |
155
46eb23d9471d
nginx-0.0.1-2003-10-22-20:38:26 import
Igor Sysoev <igor@sysoev.ru>
parents:
151
diff
changeset
|
89 chain = chain->next; |
73
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
90 } |
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
91 |
257
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
92 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, |
501 | 93 "readv: %d, last:%d", vec.nelts, iov->iov_len); |
75
869b10be682f
nginx-0.0.1-2003-04-14-21:04:58 import
Igor Sysoev <igor@sysoev.ru>
parents:
74
diff
changeset
|
94 |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
95 rev = c->read; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
96 |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
97 do { |
501 | 98 n = readv(c->fd, (struct iovec *) vec.elts, vec.nelts); |
73
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
99 |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
100 if (n >= 0) { |
455 | 101 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) { |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
102 rev->available -= n; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
103 |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
104 /* |
587 | 105 * rev->available may be negative here because some additional |
106 * bytes may be received between kevent() and recv() | |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
107 */ |
163
fb61ba77beba
nginx-0.0.1-2003-10-28-18:45:41 import
Igor Sysoev <igor@sysoev.ru>
parents:
155
diff
changeset
|
108 |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
109 if (rev->available <= 0) { |
375
744ccb59062d
nginx-0.0.7-2004-07-02-19:54:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
343
diff
changeset
|
110 if (!rev->pending_eof) { |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
111 rev->ready = 0; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
112 } |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
113 |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
114 if (rev->available < 0) { |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
115 rev->available = 0; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
116 } |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
117 } |
73
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
118 |
605 | 119 if (n == 0) { |
120 | |
121 /* | |
122 * on FreeBSD recv() may return 0 on closed socket | |
123 * even if kqueue reported about available data | |
124 */ | |
125 | |
627 | 126 #if 0 |
605 | 127 ngx_log_error(NGX_LOG_ALERT, c->log, 0, |
609 | 128 "readv() returned 0 while kevent() reported " |
605 | 129 "%d available bytes", rev->available); |
627 | 130 #endif |
605 | 131 |
5370
ee78c7705a8e
Fixed handling of the ready flag with kqueue.
Valentin Bartenev <vbart@nginx.com>
parents:
5267
diff
changeset
|
132 rev->ready = 0; |
605 | 133 rev->eof = 1; |
134 rev->available = 0; | |
135 } | |
136 | |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
137 return n; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
138 } |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
139 |
5267
13c006f0c40e
Events: honor NGX_USE_GREEDY_EVENT when kqueue support is enabled.
Valentin Bartenev <vbart@nginx.com>
parents:
4597
diff
changeset
|
140 if (n < size && !(ngx_event_flags & NGX_USE_GREEDY_EVENT)) { |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
141 rev->ready = 0; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
142 } |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
143 |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
144 if (n == 0) { |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
145 rev->eof = 1; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
146 } |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
147 |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
148 return n; |
73
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
149 } |
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
150 |
257
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
151 err = ngx_socket_errno; |
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
152 |
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
153 if (err == NGX_EAGAIN || err == NGX_EINTR) { |
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
154 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err, |
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
155 "readv() not ready"); |
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
156 n = NGX_AGAIN; |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
157 |
257
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
158 } else { |
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
159 n = ngx_connection_error(c, err, "readv() failed"); |
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
160 break; |
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
161 } |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
162 |
257
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
163 } while (err == NGX_EINTR); |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
164 |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
165 rev->ready = 0; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
166 |
3642 | 167 if (n == NGX_ERROR) { |
164
84036764e215
nginx-0.0.1-2003-10-29-11:30:44 import
Igor Sysoev <igor@sysoev.ru>
parents:
163
diff
changeset
|
168 c->read->error = 1; |
73
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
169 } |
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
170 |
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
171 return n; |
4534060fde92
nginx-0.0.1-2003-04-10-19:08:54 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
172 } |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
173 |
469 | 174 #else /* ! NGX_HAVE_KQUEUE */ |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
175 |
501 | 176 ssize_t |
177 ngx_readv_chain(ngx_connection_t *c, ngx_chain_t *chain) | |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
178 { |
305
4b1a3a4acc60
nginx-0.0.3-2004-04-02-19:13:20 import
Igor Sysoev <igor@sysoev.ru>
parents:
303
diff
changeset
|
179 u_char *prev; |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
180 ssize_t n, size; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
181 ngx_err_t err; |
501 | 182 ngx_array_t vec; |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
183 ngx_event_t *rev; |
501 | 184 struct iovec *iov, iovs[NGX_IOVS]; |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
185 |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
186 prev = NULL; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
187 iov = NULL; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
188 size = 0; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
189 |
501 | 190 vec.elts = iovs; |
191 vec.nelts = 0; | |
192 vec.size = sizeof(struct iovec); | |
193 vec.nalloc = NGX_IOVS; | |
194 vec.pool = c->pool; | |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
195 |
343
6bdf858bff8c
nginx-0.0.3-2004-05-28-19:49:23 import; rename ngx_hunk_t to ngx_buf_t
Igor Sysoev <igor@sysoev.ru>
parents:
328
diff
changeset
|
196 /* coalesce the neighbouring bufs */ |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
197 |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
198 while (chain) { |
343
6bdf858bff8c
nginx-0.0.3-2004-05-28-19:49:23 import; rename ngx_hunk_t to ngx_buf_t
Igor Sysoev <igor@sysoev.ru>
parents:
328
diff
changeset
|
199 if (prev == chain->buf->last) { |
6bdf858bff8c
nginx-0.0.3-2004-05-28-19:49:23 import; rename ngx_hunk_t to ngx_buf_t
Igor Sysoev <igor@sysoev.ru>
parents:
328
diff
changeset
|
200 iov->iov_len += chain->buf->end - chain->buf->last; |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
201 |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
202 } else { |
4597
4be36d5e78ae
Fixed ngx_readv_chain() to honor IOV_MAX (ticket #14).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4412
diff
changeset
|
203 if (vec.nelts >= IOV_MAX) { |
4be36d5e78ae
Fixed ngx_readv_chain() to honor IOV_MAX (ticket #14).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4412
diff
changeset
|
204 break; |
4be36d5e78ae
Fixed ngx_readv_chain() to honor IOV_MAX (ticket #14).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4412
diff
changeset
|
205 } |
4be36d5e78ae
Fixed ngx_readv_chain() to honor IOV_MAX (ticket #14).
Maxim Dounin <mdounin@mdounin.ru>
parents:
4412
diff
changeset
|
206 |
501 | 207 iov = ngx_array_push(&vec); |
208 if (iov == NULL) { | |
209 return NGX_ERROR; | |
210 } | |
211 | |
561 | 212 iov->iov_base = (void *) chain->buf->last; |
343
6bdf858bff8c
nginx-0.0.3-2004-05-28-19:49:23 import; rename ngx_hunk_t to ngx_buf_t
Igor Sysoev <igor@sysoev.ru>
parents:
328
diff
changeset
|
213 iov->iov_len = chain->buf->end - chain->buf->last; |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
214 } |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
215 |
343
6bdf858bff8c
nginx-0.0.3-2004-05-28-19:49:23 import; rename ngx_hunk_t to ngx_buf_t
Igor Sysoev <igor@sysoev.ru>
parents:
328
diff
changeset
|
216 size += chain->buf->end - chain->buf->last; |
6bdf858bff8c
nginx-0.0.3-2004-05-28-19:49:23 import; rename ngx_hunk_t to ngx_buf_t
Igor Sysoev <igor@sysoev.ru>
parents:
328
diff
changeset
|
217 prev = chain->buf->end; |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
218 chain = chain->next; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
219 } |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
220 |
257
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
221 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, |
501 | 222 "readv: %d:%d", vec.nelts, iov->iov_len); |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
223 |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
224 rev = c->read; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
225 |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
226 do { |
501 | 227 n = readv(c->fd, (struct iovec *) vec.elts, vec.nelts); |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
228 |
328
fb4dfb2ac0fc
nginx-0.0.3-2004-04-28-10:14:50 import
Igor Sysoev <igor@sysoev.ru>
parents:
305
diff
changeset
|
229 if (n == 0) { |
fb4dfb2ac0fc
nginx-0.0.3-2004-04-28-10:14:50 import
Igor Sysoev <igor@sysoev.ru>
parents:
305
diff
changeset
|
230 rev->ready = 0; |
fb4dfb2ac0fc
nginx-0.0.3-2004-04-28-10:14:50 import
Igor Sysoev <igor@sysoev.ru>
parents:
305
diff
changeset
|
231 rev->eof = 1; |
fb4dfb2ac0fc
nginx-0.0.3-2004-04-28-10:14:50 import
Igor Sysoev <igor@sysoev.ru>
parents:
305
diff
changeset
|
232 |
fb4dfb2ac0fc
nginx-0.0.3-2004-04-28-10:14:50 import
Igor Sysoev <igor@sysoev.ru>
parents:
305
diff
changeset
|
233 return n; |
fb4dfb2ac0fc
nginx-0.0.3-2004-04-28-10:14:50 import
Igor Sysoev <igor@sysoev.ru>
parents:
305
diff
changeset
|
234 |
fb4dfb2ac0fc
nginx-0.0.3-2004-04-28-10:14:50 import
Igor Sysoev <igor@sysoev.ru>
parents:
305
diff
changeset
|
235 } else if (n > 0) { |
fb4dfb2ac0fc
nginx-0.0.3-2004-04-28-10:14:50 import
Igor Sysoev <igor@sysoev.ru>
parents:
305
diff
changeset
|
236 |
455 | 237 if (n < size && !(ngx_event_flags & NGX_USE_GREEDY_EVENT)) { |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
238 rev->ready = 0; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
239 } |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
240 |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
241 return n; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
242 } |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
243 |
257
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
244 err = ngx_socket_errno; |
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
245 |
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
246 if (err == NGX_EAGAIN || err == NGX_EINTR) { |
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
247 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err, |
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
248 "readv() not ready"); |
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
249 n = NGX_AGAIN; |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
250 |
257
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
251 } else { |
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
252 n = ngx_connection_error(c, err, "readv() failed"); |
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
253 break; |
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
254 } |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
255 |
257
70e1c7d2b83d
nginx-0.0.2-2004-02-11-20:08:49 import
Igor Sysoev <igor@sysoev.ru>
parents:
188
diff
changeset
|
256 } while (err == NGX_EINTR); |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
257 |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
258 rev->ready = 0; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
259 |
3642 | 260 if (n == NGX_ERROR) { |
188
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
261 c->read->error = 1; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
262 } |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
263 |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
264 return n; |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
265 } |
0061d1f0908d
nginx-0.0.1-2003-11-18-11:04:34 import
Igor Sysoev <igor@sysoev.ru>
parents:
164
diff
changeset
|
266 |
469 | 267 #endif /* NGX_HAVE_KQUEUE */ |