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
|
154
|
12 static void ngx_destroy_cycle_pools(ngx_conf_t *conf);
|
28
|
13 static ngx_int_t ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2);
|
489
|
14 static ngx_int_t ngx_init_zone_pool(ngx_cycle_t *cycle,
|
|
15 ngx_shm_zone_t *shm_zone);
|
|
16 static ngx_int_t ngx_test_lockfile(u_char *file, ngx_log_t *log);
|
0
|
17 static void ngx_clean_old_cycles(ngx_event_t *ev);
|
|
18
|
|
19
|
|
20 volatile ngx_cycle_t *ngx_cycle;
|
|
21 ngx_array_t ngx_old_cycles;
|
|
22
|
|
23 static ngx_pool_t *ngx_temp_pool;
|
|
24 static ngx_event_t ngx_cleaner_event;
|
|
25
|
|
26 ngx_uint_t ngx_test_config;
|
|
27
|
|
28 #if (NGX_THREADS)
|
|
29 ngx_tls_key_t ngx_core_tls_key;
|
|
30 #endif
|
|
31
|
|
32
|
|
33 /* STUB NAME */
|
|
34 static ngx_connection_t dumb;
|
|
35 /* STUB */
|
|
36
|
|
37 #ifdef NGX_ERROR_LOG_PATH
|
|
38 static ngx_str_t error_log = ngx_string(NGX_ERROR_LOG_PATH);
|
|
39 #else
|
|
40 static ngx_str_t error_log = ngx_null_string;
|
|
41 #endif
|
|
42
|
|
43
|
112
|
44 ngx_cycle_t *
|
|
45 ngx_init_cycle(ngx_cycle_t *old_cycle)
|
0
|
46 {
|
356
|
47 void *rv;
|
|
48 char **senv, **env;
|
|
49 ngx_uint_t i, n;
|
|
50 ngx_log_t *log;
|
410
|
51 ngx_time_t *tp;
|
356
|
52 ngx_conf_t conf;
|
|
53 ngx_pool_t *pool;
|
|
54 ngx_cycle_t *cycle, **old;
|
|
55 ngx_shm_zone_t *shm_zone, *oshm_zone;
|
|
56 ngx_list_part_t *part, *opart;
|
|
57 ngx_open_file_t *file;
|
|
58 ngx_listening_t *ls, *nls;
|
|
59 ngx_core_conf_t *ccf, *old_ccf;
|
|
60 ngx_core_module_t *module;
|
376
|
61 char hostname[NGX_MAXHOSTNAMELEN];
|
0
|
62
|
410
|
63 ngx_timezone_update();
|
|
64
|
|
65 /* force localtime update with a new timezone */
|
|
66
|
|
67 tp = ngx_timeofday();
|
|
68 tp->sec = 0;
|
|
69
|
|
70 ngx_time_update(0, 0);
|
|
71
|
|
72
|
0
|
73 log = old_cycle->log;
|
|
74
|
56
|
75 pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);
|
50
|
76 if (pool == NULL) {
|
0
|
77 return NULL;
|
|
78 }
|
|
79 pool->log = log;
|
|
80
|
50
|
81 cycle = ngx_pcalloc(pool, sizeof(ngx_cycle_t));
|
|
82 if (cycle == NULL) {
|
0
|
83 ngx_destroy_pool(pool);
|
|
84 return NULL;
|
|
85 }
|
50
|
86
|
0
|
87 cycle->pool = pool;
|
|
88 cycle->log = log;
|
|
89 cycle->old_cycle = old_cycle;
|
|
90 cycle->root.len = sizeof(NGX_PREFIX) - 1;
|
|
91 cycle->root.data = (u_char *) NGX_PREFIX;
|
|
92
|
|
93
|
154
|
94 cycle->conf_file.len = old_cycle->conf_file.len;
|
382
|
95 cycle->conf_file.data = ngx_pnalloc(pool, old_cycle->conf_file.len + 1);
|
154
|
96 if (cycle->conf_file.data == NULL) {
|
|
97 ngx_destroy_pool(pool);
|
|
98 return NULL;
|
|
99 }
|
|
100 ngx_cpystrn(cycle->conf_file.data, old_cycle->conf_file.data,
|
|
101 old_cycle->conf_file.len + 1);
|
|
102
|
|
103
|
384
|
104 cycle->conf_param.len = old_cycle->conf_param.len;
|
|
105 cycle->conf_param.data = ngx_pnalloc(pool, old_cycle->conf_param.len);
|
|
106 if (cycle->conf_param.data == NULL) {
|
|
107 ngx_destroy_pool(pool);
|
|
108 return NULL;
|
|
109 }
|
|
110 ngx_memcpy(cycle->conf_param.data, old_cycle->conf_param.data,
|
|
111 old_cycle->conf_param.len);
|
|
112
|
|
113
|
0
|
114 n = old_cycle->pathes.nelts ? old_cycle->pathes.nelts : 10;
|
50
|
115
|
|
116 cycle->pathes.elts = ngx_pcalloc(pool, n * sizeof(ngx_path_t *));
|
|
117 if (cycle->pathes.elts == NULL) {
|
0
|
118 ngx_destroy_pool(pool);
|
|
119 return NULL;
|
|
120 }
|
50
|
121
|
0
|
122 cycle->pathes.nelts = 0;
|
|
123 cycle->pathes.size = sizeof(ngx_path_t *);
|
|
124 cycle->pathes.nalloc = n;
|
|
125 cycle->pathes.pool = pool;
|
|
126
|
|
127
|
|
128 if (old_cycle->open_files.part.nelts) {
|
|
129 n = old_cycle->open_files.part.nelts;
|
|
130 for (part = old_cycle->open_files.part.next; part; part = part->next) {
|
|
131 n += part->nelts;
|
|
132 }
|
|
133
|
|
134 } else {
|
|
135 n = 20;
|
|
136 }
|
|
137
|
|
138 if (ngx_list_init(&cycle->open_files, pool, n, sizeof(ngx_open_file_t))
|
463
|
139 != NGX_OK)
|
0
|
140 {
|
|
141 ngx_destroy_pool(pool);
|
|
142 return NULL;
|
|
143 }
|
|
144
|
|
145
|
258
|
146 if (old_cycle->shared_memory.part.nelts) {
|
|
147 n = old_cycle->shared_memory.part.nelts;
|
|
148 for (part = old_cycle->shared_memory.part.next; part; part = part->next)
|
|
149 {
|
|
150 n += part->nelts;
|
|
151 }
|
|
152
|
|
153 } else {
|
|
154 n = 1;
|
|
155 }
|
|
156
|
|
157 if (ngx_list_init(&cycle->shared_memory, pool, n, sizeof(ngx_shm_zone_t))
|
463
|
158 != NGX_OK)
|
258
|
159 {
|
|
160 ngx_destroy_pool(pool);
|
|
161 return NULL;
|
|
162 }
|
|
163
|
|
164
|
50
|
165 cycle->new_log = ngx_log_create_errlog(cycle, NULL);
|
|
166 if (cycle->new_log == NULL) {
|
0
|
167 ngx_destroy_pool(pool);
|
|
168 return NULL;
|
|
169 }
|
|
170
|
|
171 cycle->new_log->file->name = error_log;
|
|
172
|
|
173
|
|
174 n = old_cycle->listening.nelts ? old_cycle->listening.nelts : 10;
|
50
|
175
|
0
|
176 cycle->listening.elts = ngx_pcalloc(pool, n * sizeof(ngx_listening_t));
|
|
177 if (cycle->listening.elts == NULL) {
|
|
178 ngx_destroy_pool(pool);
|
|
179 return NULL;
|
|
180 }
|
50
|
181
|
0
|
182 cycle->listening.nelts = 0;
|
|
183 cycle->listening.size = sizeof(ngx_listening_t);
|
|
184 cycle->listening.nalloc = n;
|
|
185 cycle->listening.pool = pool;
|
|
186
|
|
187
|
|
188 cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *));
|
|
189 if (cycle->conf_ctx == NULL) {
|
|
190 ngx_destroy_pool(pool);
|
|
191 return NULL;
|
|
192 }
|
|
193
|
|
194
|
376
|
195 if (gethostname(hostname, NGX_MAXHOSTNAMELEN) == -1) {
|
|
196 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "gethostname() failed");
|
|
197 ngx_destroy_pool(pool);
|
|
198 return NULL;
|
|
199 }
|
|
200
|
|
201 /* on Linux gethostname() silently truncates name that does not fit */
|
|
202
|
|
203 hostname[NGX_MAXHOSTNAMELEN - 1] = '\0';
|
|
204 cycle->hostname.len = ngx_strlen(hostname);
|
|
205
|
382
|
206 cycle->hostname.data = ngx_pnalloc(pool, cycle->hostname.len);
|
376
|
207 if (cycle->hostname.data == NULL) {
|
|
208 ngx_destroy_pool(pool);
|
|
209 return NULL;
|
|
210 }
|
|
211
|
|
212 ngx_memcpy(cycle->hostname.data, hostname, cycle->hostname.len);
|
|
213
|
|
214
|
0
|
215 for (i = 0; ngx_modules[i]; i++) {
|
|
216 if (ngx_modules[i]->type != NGX_CORE_MODULE) {
|
|
217 continue;
|
|
218 }
|
|
219
|
|
220 module = ngx_modules[i]->ctx;
|
|
221
|
|
222 if (module->create_conf) {
|
|
223 rv = module->create_conf(cycle);
|
|
224 if (rv == NGX_CONF_ERROR) {
|
|
225 ngx_destroy_pool(pool);
|
|
226 return NULL;
|
|
227 }
|
|
228 cycle->conf_ctx[ngx_modules[i]->index] = rv;
|
|
229 }
|
|
230 }
|
|
231
|
|
232
|
356
|
233 senv = environ;
|
|
234
|
|
235
|
0
|
236 ngx_memzero(&conf, sizeof(ngx_conf_t));
|
|
237 /* STUB: init array ? */
|
50
|
238 conf.args = ngx_array_create(pool, 10, sizeof(ngx_str_t));
|
0
|
239 if (conf.args == NULL) {
|
|
240 ngx_destroy_pool(pool);
|
|
241 return NULL;
|
|
242 }
|
|
243
|
142
|
244 conf.temp_pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);
|
|
245 if (conf.temp_pool == NULL) {
|
|
246 ngx_destroy_pool(pool);
|
|
247 return NULL;
|
|
248 }
|
|
249
|
154
|
250
|
0
|
251 conf.ctx = cycle->conf_ctx;
|
|
252 conf.cycle = cycle;
|
|
253 conf.pool = pool;
|
|
254 conf.log = log;
|
|
255 conf.module_type = NGX_CORE_MODULE;
|
|
256 conf.cmd_type = NGX_MAIN_CONF;
|
|
257
|
4
|
258 #if 0
|
|
259 log->log_level = NGX_LOG_DEBUG_ALL;
|
|
260 #endif
|
0
|
261
|
384
|
262 if (ngx_conf_param(&conf) != NGX_CONF_OK) {
|
|
263 ngx_destroy_cycle_pools(&conf);
|
|
264 return NULL;
|
|
265 }
|
|
266
|
0
|
267 if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
|
154
|
268 ngx_destroy_cycle_pools(&conf);
|
0
|
269 return NULL;
|
|
270 }
|
|
271
|
|
272 if (ngx_test_config) {
|
489
|
273 ngx_log_stderr("the configuration file %s syntax is ok",
|
|
274 cycle->conf_file.data);
|
0
|
275 }
|
|
276
|
|
277
|
|
278 for (i = 0; ngx_modules[i]; i++) {
|
|
279 if (ngx_modules[i]->type != NGX_CORE_MODULE) {
|
|
280 continue;
|
|
281 }
|
|
282
|
|
283 module = ngx_modules[i]->ctx;
|
|
284
|
|
285 if (module->init_conf) {
|
|
286 if (module->init_conf(cycle, cycle->conf_ctx[ngx_modules[i]->index])
|
142
|
287 == NGX_CONF_ERROR)
|
0
|
288 {
|
154
|
289 ngx_destroy_cycle_pools(&conf);
|
0
|
290 return NULL;
|
|
291 }
|
|
292 }
|
|
293 }
|
|
294
|
|
295
|
160
|
296 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
|
0
|
297
|
160
|
298 if (ngx_test_config) {
|
|
299
|
|
300 if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {
|
|
301 goto failed;
|
|
302 }
|
|
303
|
|
304 } else if (!ngx_is_init_cycle(old_cycle)) {
|
|
305
|
|
306 /*
|
|
307 * we do not create the pid file in the first ngx_init_cycle() call
|
|
308 * because we need to write the demonized process pid
|
|
309 */
|
|
310
|
|
311 old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,
|
|
312 ngx_core_module);
|
|
313 if (ccf->pid.len != old_ccf->pid.len
|
|
314 || ngx_strcmp(ccf->pid.data, old_ccf->pid.data) != 0)
|
|
315 {
|
|
316 /* new pid file name */
|
|
317
|
|
318 if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {
|
|
319 goto failed;
|
|
320 }
|
|
321
|
|
322 ngx_delete_pidfile(old_cycle);
|
|
323 }
|
0
|
324 }
|
160
|
325
|
0
|
326
|
258
|
327 if (ngx_test_lockfile(cycle->lock_file.data, log) != NGX_OK) {
|
160
|
328 goto failed;
|
|
329 }
|
16
|
330
|
160
|
331
|
|
332 if (ngx_create_pathes(cycle, ccf->user) != NGX_OK) {
|
|
333 goto failed;
|
16
|
334 }
|
|
335
|
|
336
|
160
|
337 /* open the new files */
|
0
|
338
|
160
|
339 part = &cycle->open_files.part;
|
|
340 file = part->elts;
|
0
|
341
|
160
|
342 for (i = 0; /* void */ ; i++) {
|
0
|
343
|
160
|
344 if (i >= part->nelts) {
|
|
345 if (part->next == NULL) {
|
0
|
346 break;
|
|
347 }
|
160
|
348 part = part->next;
|
|
349 file = part->elts;
|
|
350 i = 0;
|
|
351 }
|
|
352
|
|
353 if (file[i].name.data == NULL) {
|
|
354 continue;
|
|
355 }
|
|
356
|
479
|
357 file[i].fd = ngx_open_file(file[i].name.data,
|
|
358 NGX_FILE_APPEND,
|
|
359 NGX_FILE_CREATE_OR_OPEN,
|
276
|
360 NGX_FILE_DEFAULT_ACCESS);
|
160
|
361
|
|
362 ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
|
|
363 "log: %p %d \"%s\"",
|
|
364 &file[i], file[i].fd, file[i].name.data);
|
|
365
|
|
366 if (file[i].fd == NGX_INVALID_FILE) {
|
|
367 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
|
|
368 ngx_open_file_n " \"%s\" failed",
|
|
369 file[i].name.data);
|
|
370 goto failed;
|
|
371 }
|
0
|
372
|
487
|
373 #if !(NGX_WIN32)
|
160
|
374 if (fcntl(file[i].fd, F_SETFD, FD_CLOEXEC) == -1) {
|
|
375 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
|
|
376 "fcntl(FD_CLOEXEC) \"%s\" failed",
|
|
377 file[i].name.data);
|
|
378 goto failed;
|
|
379 }
|
0
|
380 #endif
|
|
381 }
|
|
382
|
|
383 cycle->log = cycle->new_log;
|
|
384 pool->log = cycle->new_log;
|
|
385
|
449
|
386 if (cycle->log->log_level == 0) {
|
|
387 cycle->log->log_level = NGX_LOG_ERR;
|
|
388 }
|
|
389
|
10
|
390
|
258
|
391 /* create shared memory */
|
|
392
|
|
393 part = &cycle->shared_memory.part;
|
272
|
394 shm_zone = part->elts;
|
258
|
395
|
|
396 for (i = 0; /* void */ ; i++) {
|
|
397
|
|
398 if (i >= part->nelts) {
|
|
399 if (part->next == NULL) {
|
|
400 break;
|
|
401 }
|
|
402 part = part->next;
|
272
|
403 shm_zone = part->elts;
|
258
|
404 i = 0;
|
|
405 }
|
|
406
|
272
|
407 if (shm_zone[i].shm.size == 0) {
|
|
408 ngx_log_error(NGX_LOG_EMERG, log, 0,
|
|
409 "zero size shared memory zone \"%V\"",
|
489
|
410 &shm_zone[i].shm.name);
|
272
|
411 goto failed;
|
|
412 }
|
|
413
|
274
|
414 if (shm_zone[i].init == NULL) {
|
|
415 /* unused shared zone */
|
|
416 continue;
|
|
417 }
|
|
418
|
272
|
419 shm_zone[i].shm.log = cycle->log;
|
258
|
420
|
|
421 opart = &old_cycle->shared_memory.part;
|
272
|
422 oshm_zone = opart->elts;
|
258
|
423
|
|
424 for (n = 0; /* void */ ; n++) {
|
|
425
|
|
426 if (n >= opart->nelts) {
|
|
427 if (opart->next == NULL) {
|
|
428 break;
|
|
429 }
|
|
430 opart = opart->next;
|
272
|
431 oshm_zone = opart->elts;
|
258
|
432 n = 0;
|
|
433 }
|
|
434
|
489
|
435 if (shm_zone[i].shm.name.len != oshm_zone[n].shm.name.len) {
|
272
|
436 continue;
|
|
437 }
|
|
438
|
489
|
439 if (ngx_strncmp(shm_zone[i].shm.name.data,
|
|
440 oshm_zone[n].shm.name.data,
|
|
441 shm_zone[i].shm.name.len)
|
272
|
442 != 0)
|
|
443 {
|
258
|
444 continue;
|
|
445 }
|
|
446
|
272
|
447 if (shm_zone[i].shm.size == oshm_zone[n].shm.size) {
|
|
448 shm_zone[i].shm.addr = oshm_zone[n].shm.addr;
|
|
449
|
|
450 if (shm_zone[i].init(&shm_zone[i], oshm_zone[n].data)
|
|
451 != NGX_OK)
|
|
452 {
|
|
453 goto failed;
|
|
454 }
|
|
455
|
|
456 goto shm_zone_found;
|
258
|
457 }
|
|
458
|
272
|
459 ngx_shm_free(&oshm_zone[n].shm);
|
258
|
460
|
|
461 break;
|
|
462 }
|
|
463
|
272
|
464 if (ngx_shm_alloc(&shm_zone[i].shm) != NGX_OK) {
|
258
|
465 goto failed;
|
|
466 }
|
|
467
|
489
|
468 if (!shm_zone[i].shm.exists) {
|
258
|
469
|
489
|
470 if (ngx_init_zone_pool(cycle, &shm_zone[i]) != NGX_OK) {
|
|
471 goto failed;
|
|
472 }
|
258
|
473 }
|
|
474
|
272
|
475 if (shm_zone[i].init(&shm_zone[i], NULL) != NGX_OK) {
|
|
476 goto failed;
|
|
477 }
|
|
478
|
|
479 shm_zone_found:
|
258
|
480
|
|
481 continue;
|
|
482 }
|
|
483
|
|
484
|
160
|
485 /* handle the listening sockets */
|
10
|
486
|
160
|
487 if (old_cycle->listening.nelts) {
|
|
488 ls = old_cycle->listening.elts;
|
|
489 for (i = 0; i < old_cycle->listening.nelts; i++) {
|
|
490 ls[i].remain = 0;
|
|
491 }
|
0
|
492
|
160
|
493 nls = cycle->listening.elts;
|
|
494 for (n = 0; n < cycle->listening.nelts; n++) {
|
10
|
495
|
160
|
496 for (i = 0; i < old_cycle->listening.nelts; i++) {
|
|
497 if (ls[i].ignore) {
|
|
498 continue;
|
|
499 }
|
0
|
500
|
160
|
501 if (ngx_cmp_sockaddr(nls[n].sockaddr, ls[i].sockaddr) == NGX_OK)
|
|
502 {
|
|
503 nls[n].fd = ls[i].fd;
|
|
504 nls[n].previous = &ls[i];
|
|
505 ls[i].remain = 1;
|
72
|
506
|
160
|
507 if (ls[n].backlog != nls[i].backlog) {
|
|
508 nls[n].listen = 1;
|
|
509 }
|
86
|
510
|
72
|
511 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
|
|
512
|
160
|
513 /*
|
|
514 * FreeBSD, except the most recent versions,
|
|
515 * could not remove accept filter
|
|
516 */
|
|
517 nls[n].deferred_accept = ls[i].deferred_accept;
|
72
|
518
|
160
|
519 if (ls[i].accept_filter && nls[n].accept_filter) {
|
|
520 if (ngx_strcmp(ls[i].accept_filter,
|
|
521 nls[n].accept_filter)
|
|
522 != 0)
|
|
523 {
|
72
|
524 nls[n].delete_deferred = 1;
|
|
525 nls[n].add_deferred = 1;
|
|
526 }
|
160
|
527
|
|
528 } else if (ls[i].accept_filter) {
|
|
529 nls[n].delete_deferred = 1;
|
|
530
|
|
531 } else if (nls[n].accept_filter) {
|
|
532 nls[n].add_deferred = 1;
|
|
533 }
|
72
|
534 #endif
|
|
535
|
|
536 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
|
|
537
|
160
|
538 if (ls[n].deferred_accept && !nls[n].deferred_accept) {
|
|
539 nls[n].delete_deferred = 1;
|
72
|
540
|
160
|
541 } else if (ls[i].deferred_accept != nls[n].deferred_accept)
|
|
542 {
|
|
543 nls[n].add_deferred = 1;
|
|
544 }
|
72
|
545 #endif
|
160
|
546 break;
|
0
|
547 }
|
|
548 }
|
|
549
|
160
|
550 if (nls[n].fd == -1) {
|
|
551 nls[n].open = 1;
|
0
|
552 }
|
|
553 }
|
|
554
|
160
|
555 } else {
|
|
556 ls = cycle->listening.elts;
|
|
557 for (i = 0; i < cycle->listening.nelts; i++) {
|
|
558 ls[i].open = 1;
|
|
559 #if (NGX_HAVE_DEFERRED_ACCEPT && defined SO_ACCEPTFILTER)
|
|
560 if (ls[i].accept_filter) {
|
|
561 ls[i].add_deferred = 1;
|
0
|
562 }
|
160
|
563 #endif
|
|
564 #if (NGX_HAVE_DEFERRED_ACCEPT && defined TCP_DEFER_ACCEPT)
|
|
565 if (ls[i].deferred_accept) {
|
|
566 ls[i].add_deferred = 1;
|
72
|
567 }
|
160
|
568 #endif
|
0
|
569 }
|
|
570 }
|
|
571
|
160
|
572 if (ngx_open_listening_sockets(cycle) != NGX_OK) {
|
|
573 goto failed;
|
|
574 }
|
0
|
575
|
160
|
576 if (!ngx_test_config) {
|
|
577 ngx_configure_listening_socket(cycle);
|
0
|
578 }
|
|
579
|
|
580
|
|
581 /* commit the new cycle configuration */
|
|
582
|
10
|
583 #if !(NGX_WIN32)
|
0
|
584
|
|
585 if (!ngx_test_config && cycle->log->file->fd != STDERR_FILENO) {
|
|
586
|
|
587 ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
|
10
|
588 "dup2: %p %d \"%s\"",
|
0
|
589 cycle->log->file,
|
|
590 cycle->log->file->fd, cycle->log->file->name.data);
|
|
591
|
160
|
592 if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) {
|
0
|
593 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
|
|
594 "dup2(STDERR) failed");
|
|
595 /* fatal */
|
|
596 exit(1);
|
|
597 }
|
|
598 }
|
|
599
|
|
600 #endif
|
|
601
|
|
602 pool->log = cycle->log;
|
|
603
|
|
604 for (i = 0; ngx_modules[i]; i++) {
|
|
605 if (ngx_modules[i]->init_module) {
|
160
|
606 if (ngx_modules[i]->init_module(cycle) != NGX_OK) {
|
0
|
607 /* fatal */
|
|
608 exit(1);
|
|
609 }
|
|
610 }
|
|
611 }
|
|
612
|
16
|
613
|
0
|
614 /* close and delete stuff that lefts from an old cycle */
|
|
615
|
272
|
616 /* free the unnecessary shared memory */
|
|
617
|
|
618 opart = &old_cycle->shared_memory.part;
|
|
619 oshm_zone = opart->elts;
|
|
620
|
|
621 for (i = 0; /* void */ ; i++) {
|
|
622
|
|
623 if (i >= opart->nelts) {
|
|
624 if (opart->next == NULL) {
|
|
625 goto old_shm_zone_done;
|
|
626 }
|
|
627 opart = opart->next;
|
|
628 oshm_zone = opart->elts;
|
|
629 i = 0;
|
|
630 }
|
|
631
|
|
632 part = &cycle->shared_memory.part;
|
|
633 shm_zone = part->elts;
|
|
634
|
|
635 for (n = 0; /* void */ ; n++) {
|
|
636
|
|
637 if (n >= part->nelts) {
|
|
638 if (part->next == NULL) {
|
|
639 break;
|
|
640 }
|
|
641 part = part->next;
|
|
642 shm_zone = part->elts;
|
|
643 n = 0;
|
|
644 }
|
|
645
|
489
|
646 if (oshm_zone[i].shm.name.len == shm_zone[n].shm.name.len
|
|
647 && ngx_strncmp(oshm_zone[i].shm.name.data,
|
|
648 shm_zone[n].shm.name.data,
|
|
649 oshm_zone[i].shm.name.len)
|
272
|
650 == 0)
|
|
651 {
|
|
652 goto live_shm_zone;
|
|
653 }
|
|
654 }
|
|
655
|
|
656 ngx_shm_free(&oshm_zone[i].shm);
|
|
657
|
|
658 live_shm_zone:
|
|
659
|
|
660 continue;
|
|
661 }
|
|
662
|
|
663 old_shm_zone_done:
|
|
664
|
|
665
|
|
666 /* close the unnecessary listening sockets */
|
0
|
667
|
|
668 ls = old_cycle->listening.elts;
|
|
669 for (i = 0; i < old_cycle->listening.nelts; i++) {
|
|
670 if (ls[i].remain) {
|
|
671 continue;
|
|
672 }
|
|
673
|
|
674 if (ngx_close_socket(ls[i].fd) == -1) {
|
|
675 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
92
|
676 ngx_close_socket_n " listening socket on %V failed",
|
10
|
677 &ls[i].addr_text);
|
0
|
678 }
|
|
679 }
|
|
680
|
|
681
|
272
|
682 /* close the unnecessary open files */
|
0
|
683
|
|
684 part = &old_cycle->open_files.part;
|
|
685 file = part->elts;
|
|
686
|
|
687 for (i = 0; /* void */ ; i++) {
|
|
688
|
|
689 if (i >= part->nelts) {
|
|
690 if (part->next == NULL) {
|
|
691 break;
|
|
692 }
|
|
693 part = part->next;
|
|
694 file = part->elts;
|
|
695 i = 0;
|
|
696 }
|
|
697
|
|
698 if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr_fileno) {
|
|
699 continue;
|
|
700 }
|
|
701
|
|
702 if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
|
|
703 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
|
|
704 ngx_close_file_n " \"%s\" failed",
|
|
705 file[i].name.data);
|
|
706 }
|
|
707 }
|
|
708
|
142
|
709 ngx_destroy_pool(conf.temp_pool);
|
|
710
|
154
|
711 if (ngx_process == NGX_PROCESS_MASTER || ngx_is_init_cycle(old_cycle)) {
|
|
712
|
356
|
713 /*
|
|
714 * perl_destruct() frees environ if it is not the same as it was at
|
|
715 * perl_construct() time. So we have saved an previous cycle
|
|
716 * environment before ngx_conf_parse() where it will be changed.
|
|
717 */
|
|
718
|
|
719 env = environ;
|
|
720 environ = senv;
|
|
721
|
0
|
722 ngx_destroy_pool(old_cycle->pool);
|
160
|
723 cycle->old_cycle = NULL;
|
|
724
|
356
|
725 environ = env;
|
|
726
|
0
|
727 return cycle;
|
|
728 }
|
|
729
|
|
730
|
|
731 if (ngx_temp_pool == NULL) {
|
|
732 ngx_temp_pool = ngx_create_pool(128, cycle->log);
|
|
733 if (ngx_temp_pool == NULL) {
|
|
734 ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
|
735 "can not create ngx_temp_pool");
|
|
736 exit(1);
|
|
737 }
|
|
738
|
|
739 n = 10;
|
|
740 ngx_old_cycles.elts = ngx_pcalloc(ngx_temp_pool,
|
|
741 n * sizeof(ngx_cycle_t *));
|
|
742 if (ngx_old_cycles.elts == NULL) {
|
|
743 exit(1);
|
|
744 }
|
|
745 ngx_old_cycles.nelts = 0;
|
|
746 ngx_old_cycles.size = sizeof(ngx_cycle_t *);
|
|
747 ngx_old_cycles.nalloc = n;
|
|
748 ngx_old_cycles.pool = ngx_temp_pool;
|
|
749
|
58
|
750 ngx_cleaner_event.handler = ngx_clean_old_cycles;
|
0
|
751 ngx_cleaner_event.log = cycle->log;
|
|
752 ngx_cleaner_event.data = &dumb;
|
|
753 dumb.fd = (ngx_socket_t) -1;
|
|
754 }
|
|
755
|
|
756 ngx_temp_pool->log = cycle->log;
|
|
757
|
50
|
758 old = ngx_array_push(&ngx_old_cycles);
|
0
|
759 if (old == NULL) {
|
|
760 exit(1);
|
|
761 }
|
|
762 *old = old_cycle;
|
|
763
|
|
764 if (!ngx_cleaner_event.timer_set) {
|
|
765 ngx_add_timer(&ngx_cleaner_event, 30000);
|
|
766 ngx_cleaner_event.timer_set = 1;
|
|
767 }
|
|
768
|
|
769 return cycle;
|
160
|
770
|
|
771
|
|
772 failed:
|
|
773
|
296
|
774 if (!ngx_is_init_cycle(old_cycle)) {
|
|
775 old_ccf = (ngx_core_conf_t *) ngx_get_conf(old_cycle->conf_ctx,
|
|
776 ngx_core_module);
|
|
777 if (old_ccf->environment) {
|
|
778 environ = old_ccf->environment;
|
|
779 }
|
|
780 }
|
|
781
|
160
|
782 /* rollback the new cycle configuration */
|
|
783
|
|
784 part = &cycle->open_files.part;
|
|
785 file = part->elts;
|
|
786
|
|
787 for (i = 0; /* void */ ; i++) {
|
|
788
|
|
789 if (i >= part->nelts) {
|
|
790 if (part->next == NULL) {
|
|
791 break;
|
|
792 }
|
|
793 part = part->next;
|
|
794 file = part->elts;
|
|
795 i = 0;
|
|
796 }
|
|
797
|
|
798 if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr_fileno) {
|
|
799 continue;
|
|
800 }
|
|
801
|
|
802 if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
|
|
803 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
|
|
804 ngx_close_file_n " \"%s\" failed",
|
|
805 file[i].name.data);
|
|
806 }
|
|
807 }
|
|
808
|
|
809 if (ngx_test_config) {
|
|
810 ngx_destroy_cycle_pools(&conf);
|
|
811 return NULL;
|
|
812 }
|
|
813
|
|
814 ls = cycle->listening.elts;
|
|
815 for (i = 0; i < cycle->listening.nelts; i++) {
|
|
816 if (ls[i].fd == -1 || !ls[i].open) {
|
|
817 continue;
|
|
818 }
|
|
819
|
|
820 if (ngx_close_socket(ls[i].fd) == -1) {
|
|
821 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
|
|
822 ngx_close_socket_n " %V failed",
|
|
823 &ls[i].addr_text);
|
|
824 }
|
|
825 }
|
|
826
|
|
827 ngx_destroy_cycle_pools(&conf);
|
|
828
|
|
829 return NULL;
|
0
|
830 }
|
|
831
|
|
832
|
154
|
833 static void
|
|
834 ngx_destroy_cycle_pools(ngx_conf_t *conf)
|
|
835 {
|
|
836 ngx_destroy_pool(conf->temp_pool);
|
|
837 ngx_destroy_pool(conf->pool);
|
|
838 }
|
|
839
|
|
840
|
112
|
841 static ngx_int_t
|
|
842 ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2)
|
10
|
843 {
|
457
|
844 struct sockaddr_in *sin1, *sin2;
|
|
845 #if (NGX_HAVE_INET6)
|
|
846 struct sockaddr_in6 *sin61, *sin62;
|
|
847 #endif
|
10
|
848
|
457
|
849 if (sa1->sa_family != sa2->sa_family) {
|
10
|
850 return NGX_DECLINED;
|
|
851 }
|
|
852
|
457
|
853 switch (sa1->sa_family) {
|
|
854
|
|
855 #if (NGX_HAVE_INET6)
|
|
856 case AF_INET6:
|
|
857 sin61 = (struct sockaddr_in6 *) sa1;
|
|
858 sin62 = (struct sockaddr_in6 *) sa2;
|
|
859
|
|
860 if (sin61->sin6_port != sin61->sin6_port) {
|
|
861 return NGX_DECLINED;
|
|
862 }
|
|
863
|
|
864 if (ngx_memcmp(&sin61->sin6_addr, &sin62->sin6_addr, 16) != 0) {
|
|
865 return NGX_DECLINED;
|
|
866 }
|
10
|
867
|
457
|
868 break;
|
|
869 #endif
|
|
870
|
|
871 default: /* AF_INET */
|
|
872
|
|
873 sin1 = (struct sockaddr_in *) sa1;
|
|
874 sin2 = (struct sockaddr_in *) sa2;
|
10
|
875
|
457
|
876 if (sin1->sin_port != sin2->sin_port) {
|
|
877 return NGX_DECLINED;
|
|
878 }
|
|
879
|
|
880 if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) {
|
|
881 return NGX_DECLINED;
|
|
882 }
|
|
883
|
|
884 break;
|
10
|
885 }
|
|
886
|
|
887 return NGX_OK;
|
|
888 }
|
|
889
|
|
890
|
489
|
891 static ngx_int_t
|
|
892 ngx_init_zone_pool(ngx_cycle_t *cycle, ngx_shm_zone_t *zn)
|
|
893 {
|
|
894 u_char *file;
|
|
895 ngx_slab_pool_t *sp;
|
|
896
|
|
897 sp = (ngx_slab_pool_t *) zn->shm.addr;
|
|
898
|
|
899 sp->end = zn->shm.addr + zn->shm.size;
|
|
900 sp->min_shift = 3;
|
|
901
|
|
902 #if (NGX_HAVE_ATOMIC_OPS)
|
|
903
|
|
904 file = NULL;
|
|
905
|
|
906 #else
|
|
907
|
|
908 file = ngx_pnalloc(cycle->pool, cycle->lock_file.len + zn->shm.name.len);
|
|
909 if (file == NULL) {
|
|
910 return NGX_ERROR;
|
|
911 }
|
|
912
|
|
913 (void) ngx_sprintf(file, "%V%V%Z", &cycle->lock_file, &zn->shm.name);
|
|
914
|
|
915 #endif
|
|
916
|
|
917 if (ngx_shmtx_create(&sp->mutex, (void *) &sp->lock, file) != NGX_OK) {
|
|
918 return NGX_ERROR;
|
|
919 }
|
|
920
|
|
921 ngx_slab_init(sp);
|
|
922
|
|
923 return NGX_OK;
|
|
924 }
|
|
925
|
0
|
926
|
112
|
927 ngx_int_t
|
160
|
928 ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log)
|
0
|
929 {
|
487
|
930 size_t len;
|
|
931 ngx_uint_t create;
|
|
932 ngx_file_t file;
|
|
933 u_char pid[NGX_INT64_LEN + 2];
|
0
|
934
|
489
|
935 if (ngx_process > NGX_PROCESS_MASTER) {
|
|
936 return NGX_OK;
|
|
937 }
|
|
938
|
0
|
939 ngx_memzero(&file, sizeof(ngx_file_t));
|
92
|
940
|
160
|
941 file.name = *name;
|
|
942 file.log = log;
|
0
|
943
|
487
|
944 create = ngx_test_config ? NGX_FILE_CREATE_OR_OPEN : NGX_FILE_TRUNCATE;
|
0
|
945
|
|
946 file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR,
|
487
|
947 create, NGX_FILE_DEFAULT_ACCESS);
|
0
|
948
|
|
949 if (file.fd == NGX_INVALID_FILE) {
|
160
|
950 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
|
0
|
951 ngx_open_file_n " \"%s\" failed", file.name.data);
|
|
952 return NGX_ERROR;
|
|
953 }
|
|
954
|
|
955 if (!ngx_test_config) {
|
270
|
956 len = ngx_snprintf(pid, NGX_INT64_LEN + 2, "%P%N", ngx_pid) - pid;
|
10
|
957
|
0
|
958 if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) {
|
|
959 return NGX_ERROR;
|
|
960 }
|
|
961 }
|
|
962
|
|
963 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
|
160
|
964 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
0
|
965 ngx_close_file_n " \"%s\" failed", file.name.data);
|
|
966 }
|
|
967
|
|
968 return NGX_OK;
|
|
969 }
|
|
970
|
|
971
|
112
|
972 void
|
|
973 ngx_delete_pidfile(ngx_cycle_t *cycle)
|
126
|
974 {
|
0
|
975 u_char *name;
|
|
976 ngx_core_conf_t *ccf;
|
|
977
|
|
978 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
|
|
979
|
92
|
980 name = ngx_new_binary ? ccf->oldpid.data : ccf->pid.data;
|
0
|
981
|
|
982 if (ngx_delete_file(name) == NGX_FILE_ERROR) {
|
|
983 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
984 ngx_delete_file_n " \"%s\" failed", name);
|
|
985 }
|
|
986 }
|
|
987
|
|
988
|
160
|
989 static ngx_int_t
|
|
990 ngx_test_lockfile(u_char *file, ngx_log_t *log)
|
|
991 {
|
|
992 #if !(NGX_HAVE_ATOMIC_OPS)
|
|
993 ngx_fd_t fd;
|
|
994
|
284
|
995 fd = ngx_open_file(file, NGX_FILE_RDWR, NGX_FILE_CREATE_OR_OPEN,
|
|
996 NGX_FILE_DEFAULT_ACCESS);
|
160
|
997
|
|
998 if (fd == NGX_INVALID_FILE) {
|
|
999 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
|
|
1000 ngx_open_file_n " \"%s\" failed", file);
|
|
1001 return NGX_ERROR;
|
|
1002 }
|
|
1003
|
|
1004 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
1005 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
1006 ngx_close_file_n " \"%s\" failed", file);
|
|
1007 }
|
|
1008
|
|
1009 if (ngx_delete_file(file) == NGX_FILE_ERROR) {
|
|
1010 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
1011 ngx_delete_file_n " \"%s\" failed", file);
|
|
1012 }
|
|
1013
|
|
1014 #endif
|
|
1015
|
|
1016 return NGX_OK;
|
|
1017 }
|
|
1018
|
|
1019
|
112
|
1020 void
|
|
1021 ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user)
|
0
|
1022 {
|
427
|
1023 ssize_t n, len;
|
0
|
1024 ngx_fd_t fd;
|
|
1025 ngx_uint_t i;
|
|
1026 ngx_list_part_t *part;
|
|
1027 ngx_open_file_t *file;
|
|
1028
|
|
1029 part = &cycle->open_files.part;
|
|
1030 file = part->elts;
|
|
1031
|
|
1032 for (i = 0; /* void */ ; i++) {
|
|
1033
|
|
1034 if (i >= part->nelts) {
|
|
1035 if (part->next == NULL) {
|
|
1036 break;
|
|
1037 }
|
|
1038 part = part->next;
|
72
|
1039 file = part->elts;
|
0
|
1040 i = 0;
|
|
1041 }
|
|
1042
|
|
1043 if (file[i].name.data == NULL) {
|
|
1044 continue;
|
|
1045 }
|
|
1046
|
427
|
1047 len = file[i].pos - file[i].buffer;
|
|
1048
|
|
1049 if (file[i].buffer && len != 0) {
|
|
1050
|
|
1051 n = ngx_write_fd(file[i].fd, file[i].buffer, len);
|
|
1052
|
|
1053 if (n == NGX_FILE_ERROR) {
|
|
1054 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
1055 ngx_write_fd_n " to \"%s\" failed",
|
|
1056 file[i].name.data);
|
|
1057
|
|
1058 } else if (n != len) {
|
|
1059 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
|
1060 ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
|
|
1061 file[i].name.data, n, len);
|
|
1062 }
|
|
1063
|
120
|
1064 file[i].pos = file[i].buffer;
|
|
1065 }
|
|
1066
|
479
|
1067 fd = ngx_open_file(file[i].name.data, NGX_FILE_APPEND,
|
|
1068 NGX_FILE_CREATE_OR_OPEN, NGX_FILE_DEFAULT_ACCESS);
|
0
|
1069
|
|
1070 ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
1071 "reopen file \"%s\", old:%d new:%d",
|
|
1072 file[i].name.data, file[i].fd, fd);
|
|
1073
|
|
1074 if (fd == NGX_INVALID_FILE) {
|
|
1075 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
1076 ngx_open_file_n " \"%s\" failed", file[i].name.data);
|
|
1077 continue;
|
|
1078 }
|
|
1079
|
487
|
1080 #if !(NGX_WIN32)
|
66
|
1081 if (user != (ngx_uid_t) NGX_CONF_UNSET_UINT) {
|
356
|
1082 ngx_file_info_t fi;
|
2
|
1083
|
|
1084 if (ngx_file_info((const char *) file[i].name.data, &fi) == -1) {
|
|
1085 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
1086 ngx_file_info_n " \"%s\" failed",
|
|
1087 file[i].name.data);
|
|
1088
|
|
1089 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
1090 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
1091 ngx_close_file_n " \"%s\" failed",
|
|
1092 file[i].name.data);
|
|
1093 }
|
|
1094 }
|
|
1095
|
16
|
1096 if (fi.st_uid != user) {
|
|
1097 if (chown((const char *) file[i].name.data, user, -1) == -1) {
|
|
1098 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
1099 "chown(\"%s\", %d) failed",
|
|
1100 file[i].name.data, user);
|
|
1101
|
|
1102 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
1103 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
1104 ngx_close_file_n " \"%s\" failed",
|
|
1105 file[i].name.data);
|
|
1106 }
|
|
1107 }
|
|
1108 }
|
|
1109
|
2
|
1110 if ((fi.st_mode & (S_IRUSR|S_IWUSR)) != (S_IRUSR|S_IWUSR)) {
|
|
1111
|
|
1112 fi.st_mode |= (S_IRUSR|S_IWUSR);
|
|
1113
|
|
1114 if (chmod((const char *) file[i].name.data, fi.st_mode) == -1) {
|
|
1115 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
16
|
1116 "chmod() \"%s\" failed", file[i].name.data);
|
2
|
1117
|
|
1118 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
1119 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
1120 ngx_close_file_n " \"%s\" failed",
|
|
1121 file[i].name.data);
|
|
1122 }
|
|
1123 }
|
|
1124 }
|
0
|
1125 }
|
|
1126
|
|
1127 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) {
|
|
1128 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
1129 "fcntl(FD_CLOEXEC) \"%s\" failed",
|
|
1130 file[i].name.data);
|
|
1131
|
|
1132 if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
1133 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
1134 ngx_close_file_n " \"%s\" failed",
|
|
1135 file[i].name.data);
|
|
1136 }
|
|
1137
|
|
1138 continue;
|
|
1139 }
|
|
1140 #endif
|
|
1141
|
|
1142 if (ngx_close_file(file[i].fd) == NGX_FILE_ERROR) {
|
|
1143 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
1144 ngx_close_file_n " \"%s\" failed",
|
|
1145 file[i].name.data);
|
|
1146 }
|
|
1147
|
|
1148 file[i].fd = fd;
|
|
1149 }
|
|
1150
|
10
|
1151 #if !(NGX_WIN32)
|
0
|
1152
|
|
1153 if (cycle->log->file->fd != STDERR_FILENO) {
|
|
1154 if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) {
|
|
1155 ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
1156 "dup2(STDERR) failed");
|
|
1157 }
|
|
1158 }
|
|
1159
|
|
1160 #endif
|
|
1161 }
|
|
1162
|
|
1163
|
272
|
1164 ngx_shm_zone_t *
|
|
1165 ngx_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name, size_t size, void *tag)
|
|
1166 {
|
|
1167 ngx_uint_t i;
|
|
1168 ngx_shm_zone_t *shm_zone;
|
|
1169 ngx_list_part_t *part;
|
|
1170
|
|
1171 part = &cf->cycle->shared_memory.part;
|
|
1172 shm_zone = part->elts;
|
|
1173
|
|
1174 for (i = 0; /* void */ ; i++) {
|
|
1175
|
|
1176 if (i >= part->nelts) {
|
|
1177 if (part->next == NULL) {
|
|
1178 break;
|
|
1179 }
|
|
1180 part = part->next;
|
|
1181 shm_zone = part->elts;
|
|
1182 i = 0;
|
|
1183 }
|
|
1184
|
489
|
1185 if (name->len != shm_zone[i].shm.name.len) {
|
272
|
1186 continue;
|
|
1187 }
|
|
1188
|
489
|
1189 if (ngx_strncmp(name->data, shm_zone[i].shm.name.data, name->len)
|
|
1190 != 0)
|
|
1191 {
|
272
|
1192 continue;
|
|
1193 }
|
|
1194
|
|
1195 if (size && size != shm_zone[i].shm.size) {
|
|
1196 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
489
|
1197 "the size %uz of shared memory zone \"%V\" "
|
|
1198 "conflicts with already declared size %uz",
|
|
1199 size, &shm_zone[i].shm.name, shm_zone[i].shm.size);
|
272
|
1200 return NULL;
|
|
1201 }
|
|
1202
|
|
1203 if (tag != shm_zone[i].tag) {
|
|
1204 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
489
|
1205 "the shared memory zone \"%V\" is "
|
|
1206 "already declared for a different use",
|
|
1207 &shm_zone[i].shm.name);
|
272
|
1208 return NULL;
|
|
1209 }
|
|
1210
|
|
1211 return &shm_zone[i];
|
|
1212 }
|
|
1213
|
|
1214 shm_zone = ngx_list_push(&cf->cycle->shared_memory);
|
|
1215
|
|
1216 if (shm_zone == NULL) {
|
|
1217 return NULL;
|
|
1218 }
|
|
1219
|
|
1220 shm_zone->data = NULL;
|
|
1221 shm_zone->shm.log = cf->cycle->log;
|
|
1222 shm_zone->shm.size = size;
|
489
|
1223 shm_zone->shm.name = *name;
|
|
1224 shm_zone->shm.exists = 0;
|
272
|
1225 shm_zone->init = NULL;
|
|
1226 shm_zone->tag = tag;
|
|
1227
|
|
1228 return shm_zone;
|
|
1229 }
|
|
1230
|
|
1231
|
112
|
1232 static void
|
|
1233 ngx_clean_old_cycles(ngx_event_t *ev)
|
0
|
1234 {
|
|
1235 ngx_uint_t i, n, found, live;
|
|
1236 ngx_log_t *log;
|
|
1237 ngx_cycle_t **cycle;
|
|
1238
|
|
1239 log = ngx_cycle->log;
|
|
1240 ngx_temp_pool->log = log;
|
|
1241
|
|
1242 ngx_log_debug0(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycles");
|
|
1243
|
|
1244 live = 0;
|
|
1245
|
|
1246 cycle = ngx_old_cycles.elts;
|
|
1247 for (i = 0; i < ngx_old_cycles.nelts; i++) {
|
|
1248
|
|
1249 if (cycle[i] == NULL) {
|
|
1250 continue;
|
|
1251 }
|
|
1252
|
|
1253 found = 0;
|
|
1254
|
|
1255 for (n = 0; n < cycle[i]->connection_n; n++) {
|
110
|
1256 if (cycle[i]->connections[n].fd != (ngx_socket_t) -1) {
|
0
|
1257 found = 1;
|
|
1258
|
|
1259 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "live fd:%d", n);
|
|
1260
|
|
1261 break;
|
|
1262 }
|
|
1263 }
|
|
1264
|
|
1265 if (found) {
|
|
1266 live = 1;
|
|
1267 continue;
|
|
1268 }
|
|
1269
|
|
1270 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "clean old cycle: %d", i);
|
|
1271
|
|
1272 ngx_destroy_pool(cycle[i]->pool);
|
|
1273 cycle[i] = NULL;
|
|
1274 }
|
|
1275
|
|
1276 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "old cycles status: %d", live);
|
|
1277
|
|
1278 if (live) {
|
|
1279 ngx_add_timer(ev, 30000);
|
|
1280
|
|
1281 } else {
|
|
1282 ngx_destroy_pool(ngx_temp_pool);
|
|
1283 ngx_temp_pool = NULL;
|
|
1284 ngx_old_cycles.nelts = 0;
|
|
1285 }
|
|
1286 }
|