Mercurial > hg > nginx-quic
annotate src/stream/ngx_stream_upstream_zone_module.c @ 6432:cf3e75cfa951
Added variables support to server_tokens.
It can now be set to "off" conditionally, e.g. using the map
directive.
An empty value will disable the emission of the Server: header
and the signature in error messages generated by nginx.
Any other value is treated as "on", meaning that full nginx
version is emitted in the Server: header and error messages
generated by nginx.
author | Ruslan Ermilov <ru@nginx.com> |
---|---|
date | Tue, 15 Mar 2016 13:36:19 +0300 |
parents | 341e4303d25b |
children | 2f41d383c9c7 |
rev | line source |
---|---|
6115 | 1 |
2 /* | |
3 * Copyright (C) Ruslan Ermilov | |
4 * Copyright (C) Nginx, Inc. | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_stream.h> | |
11 | |
12 | |
13 static char *ngx_stream_upstream_zone(ngx_conf_t *cf, ngx_command_t *cmd, | |
14 void *conf); | |
15 static ngx_int_t ngx_stream_upstream_init_zone(ngx_shm_zone_t *shm_zone, | |
16 void *data); | |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
17 static ngx_stream_upstream_rr_peers_t *ngx_stream_upstream_zone_copy_peers( |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
18 ngx_slab_pool_t *shpool, ngx_stream_upstream_srv_conf_t *uscf); |
6115 | 19 |
20 | |
21 static ngx_command_t ngx_stream_upstream_zone_commands[] = { | |
22 | |
23 { ngx_string("zone"), | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
24 NGX_STREAM_UPS_CONF|NGX_CONF_TAKE12, |
6115 | 25 ngx_stream_upstream_zone, |
26 0, | |
27 0, | |
28 NULL }, | |
29 | |
30 ngx_null_command | |
31 }; | |
32 | |
33 | |
34 static ngx_stream_module_t ngx_stream_upstream_zone_module_ctx = { | |
6174
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6123
diff
changeset
|
35 NULL, /* postconfiguration */ |
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6123
diff
changeset
|
36 |
6115 | 37 NULL, /* create main configuration */ |
38 NULL, /* init main configuration */ | |
39 | |
40 NULL, /* create server configuration */ | |
41 NULL, /* merge server configuration */ | |
42 }; | |
43 | |
44 | |
45 ngx_module_t ngx_stream_upstream_zone_module = { | |
46 NGX_MODULE_V1, | |
47 &ngx_stream_upstream_zone_module_ctx, /* module context */ | |
48 ngx_stream_upstream_zone_commands, /* module directives */ | |
49 NGX_STREAM_MODULE, /* module type */ | |
50 NULL, /* init master */ | |
51 NULL, /* init module */ | |
52 NULL, /* init process */ | |
53 NULL, /* init thread */ | |
54 NULL, /* exit thread */ | |
55 NULL, /* exit process */ | |
56 NULL, /* exit master */ | |
57 NGX_MODULE_V1_PADDING | |
58 }; | |
59 | |
60 | |
61 static char * | |
62 ngx_stream_upstream_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
63 { | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
64 ssize_t size; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
65 ngx_str_t *value; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
66 ngx_stream_upstream_srv_conf_t *uscf; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
67 ngx_stream_upstream_main_conf_t *umcf; |
6115 | 68 |
69 uscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_upstream_module); | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
70 umcf = ngx_stream_conf_get_module_main_conf(cf, ngx_stream_upstream_module); |
6115 | 71 |
72 value = cf->args->elts; | |
73 | |
74 if (!value[1].len) { | |
75 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
76 "invalid zone name \"%V\"", &value[1]); | |
77 return NGX_CONF_ERROR; | |
78 } | |
79 | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
80 if (cf->args->nelts == 3) { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
81 size = ngx_parse_size(&value[2]); |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
82 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
83 if (size == NGX_ERROR) { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
84 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
85 "invalid zone size \"%V\"", &value[2]); |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
86 return NGX_CONF_ERROR; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
87 } |
6115 | 88 |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
89 if (size < (ssize_t) (8 * ngx_pagesize)) { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
90 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
91 "zone \"%V\" is too small", &value[1]); |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
92 return NGX_CONF_ERROR; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
93 } |
6115 | 94 |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
95 } else { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
96 size = 0; |
6115 | 97 } |
98 | |
99 uscf->shm_zone = ngx_shared_memory_add(cf, &value[1], size, | |
100 &ngx_stream_upstream_module); | |
101 if (uscf->shm_zone == NULL) { | |
102 return NGX_CONF_ERROR; | |
103 } | |
104 | |
105 uscf->shm_zone->init = ngx_stream_upstream_init_zone; | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
106 uscf->shm_zone->data = umcf; |
6115 | 107 |
108 uscf->shm_zone->noreuse = 1; | |
109 | |
110 return NGX_CONF_OK; | |
111 } | |
112 | |
113 | |
114 static ngx_int_t | |
115 ngx_stream_upstream_init_zone(ngx_shm_zone_t *shm_zone, void *data) | |
116 { | |
117 size_t len; | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
118 ngx_uint_t i; |
6115 | 119 ngx_slab_pool_t *shpool; |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
120 ngx_stream_upstream_rr_peers_t *peers, **peersp; |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
121 ngx_stream_upstream_srv_conf_t *uscf, **uscfp; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
122 ngx_stream_upstream_main_conf_t *umcf; |
6115 | 123 |
124 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; | |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
125 umcf = shm_zone->data; |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
126 uscfp = umcf->upstreams.elts; |
6115 | 127 |
128 if (shm_zone->shm.exists) { | |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
129 peers = shpool->data; |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
130 |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
131 for (i = 0; i < umcf->upstreams.nelts; i++) { |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
132 uscf = uscfp[i]; |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
133 |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
134 if (uscf->shm_zone != shm_zone) { |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
135 continue; |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
136 } |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
137 |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
138 uscf->peer.data = peers; |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
139 peers = peers->zone_next; |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
140 } |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
141 |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
142 return NGX_OK; |
6115 | 143 } |
144 | |
145 len = sizeof(" in upstream zone \"\"") + shm_zone->shm.name.len; | |
146 | |
147 shpool->log_ctx = ngx_slab_alloc(shpool, len); | |
148 if (shpool->log_ctx == NULL) { | |
149 return NGX_ERROR; | |
150 } | |
151 | |
152 ngx_sprintf(shpool->log_ctx, " in upstream zone \"%V\"%Z", | |
153 &shm_zone->shm.name); | |
154 | |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
155 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
156 /* copy peers to shared memory */ |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
157 |
6214
341e4303d25b
Fixed strict aliasing warnings with old GCC versions.
Ruslan Ermilov <ru@nginx.com>
parents:
6198
diff
changeset
|
158 peersp = (ngx_stream_upstream_rr_peers_t **) (void *) &shpool->data; |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
159 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
160 for (i = 0; i < umcf->upstreams.nelts; i++) { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
161 uscf = uscfp[i]; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
162 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
163 if (uscf->shm_zone != shm_zone) { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
164 continue; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
165 } |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
166 |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
167 peers = ngx_stream_upstream_zone_copy_peers(shpool, uscf); |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
168 if (peers == NULL) { |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
169 return NGX_ERROR; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
170 } |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
171 |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
172 *peersp = peers; |
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
173 peersp = &peers->zone_next; |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
174 } |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
175 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
176 return NGX_OK; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
177 } |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
178 |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
179 |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
180 static ngx_stream_upstream_rr_peers_t * |
6123
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
181 ngx_stream_upstream_zone_copy_peers(ngx_slab_pool_t *shpool, |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
182 ngx_stream_upstream_srv_conf_t *uscf) |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
183 { |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
184 ngx_stream_upstream_rr_peer_t *peer, **peerp; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
185 ngx_stream_upstream_rr_peers_t *peers, *backup; |
caa103acf180
Upstream: allow multiple upstreams to use the same shared zone.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
186 |
6115 | 187 peers = ngx_slab_alloc(shpool, sizeof(ngx_stream_upstream_rr_peers_t)); |
188 if (peers == NULL) { | |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
189 return NULL; |
6115 | 190 } |
191 | |
192 ngx_memcpy(peers, uscf->peer.data, sizeof(ngx_stream_upstream_rr_peers_t)); | |
193 | |
194 peers->shpool = shpool; | |
195 | |
196 for (peerp = &peers->peer; *peerp; peerp = &peer->next) { | |
197 /* pool is unlocked */ | |
198 peer = ngx_slab_calloc_locked(shpool, | |
199 sizeof(ngx_stream_upstream_rr_peer_t)); | |
200 if (peer == NULL) { | |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
201 return NULL; |
6115 | 202 } |
203 | |
204 ngx_memcpy(peer, *peerp, sizeof(ngx_stream_upstream_rr_peer_t)); | |
205 | |
206 *peerp = peer; | |
207 } | |
208 | |
209 if (peers->next == NULL) { | |
210 goto done; | |
211 } | |
212 | |
213 backup = ngx_slab_alloc(shpool, sizeof(ngx_stream_upstream_rr_peers_t)); | |
214 if (backup == NULL) { | |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
215 return NULL; |
6115 | 216 } |
217 | |
218 ngx_memcpy(backup, peers->next, sizeof(ngx_stream_upstream_rr_peers_t)); | |
219 | |
220 backup->shpool = shpool; | |
221 | |
222 for (peerp = &backup->peer; *peerp; peerp = &peer->next) { | |
223 /* pool is unlocked */ | |
224 peer = ngx_slab_calloc_locked(shpool, | |
225 sizeof(ngx_stream_upstream_rr_peer_t)); | |
226 if (peer == NULL) { | |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
227 return NULL; |
6115 | 228 } |
229 | |
230 ngx_memcpy(peer, *peerp, sizeof(ngx_stream_upstream_rr_peer_t)); | |
231 | |
232 *peerp = peer; | |
233 } | |
234 | |
235 peers->next = backup; | |
236 | |
237 done: | |
238 | |
239 uscf->peer.data = peers; | |
240 | |
6198
311d232ad803
Upstream: fixed shared upstreams on win32.
Ruslan Ermilov <ru@nginx.com>
parents:
6174
diff
changeset
|
241 return peers; |
6115 | 242 } |