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