~ubuntu-branches/ubuntu/trusty/nginx/trusty-proposed

« back to all changes in this revision

Viewing changes to src/core/ngx_cycle.c

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry
  • Date: 2013-04-25 12:51:45 UTC
  • mfrom: (1.3.28)
  • mto: (1.3.29) (15.1.2 experimental)
  • mto: This revision was merged to the branch mainline in revision 64.
  • Revision ID: package-import@ubuntu.com-20130425125145-ugl0wor6bq0u5eae
Tags: upstream-1.4.0
ImportĀ upstreamĀ versionĀ 1.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
/*
3
3
 * Copyright (C) Igor Sysoev
 
4
 * Copyright (C) Nginx, Inc.
4
5
 */
5
6
 
6
7
 
9
10
#include <ngx_event.h>
10
11
 
11
12
 
12
 
static ngx_int_t ngx_test_lockfile(u_char *file, ngx_log_t *log);
13
13
static void ngx_destroy_cycle_pools(ngx_conf_t *conf);
14
14
static ngx_int_t ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2);
 
15
static ngx_int_t ngx_init_zone_pool(ngx_cycle_t *cycle,
 
16
    ngx_shm_zone_t *shm_zone);
 
17
static ngx_int_t ngx_test_lockfile(u_char *file, ngx_log_t *log);
15
18
static void ngx_clean_old_cycles(ngx_event_t *ev);
16
19
 
17
20
 
22
25
static ngx_event_t     ngx_cleaner_event;
23
26
 
24
27
ngx_uint_t             ngx_test_config;
 
28
ngx_uint_t             ngx_quiet_mode;
25
29
 
26
30
#if (NGX_THREADS)
27
31
ngx_tls_key_t          ngx_core_tls_key;
32
36
static ngx_connection_t  dumb;
33
37
/* STUB */
34
38
 
35
 
#ifdef NGX_ERROR_LOG_PATH
36
39
static ngx_str_t  error_log = ngx_string(NGX_ERROR_LOG_PATH);
37
 
#else
38
 
static ngx_str_t  error_log = ngx_null_string;
39
 
#endif
40
40
 
41
41
 
42
42
ngx_cycle_t *
44
44
{
45
45
    void                *rv;
46
46
    char               **senv, **env;
47
 
    u_char              *lock_file;
48
47
    ngx_uint_t           i, n;
49
48
    ngx_log_t           *log;
 
49
    ngx_time_t          *tp;
50
50
    ngx_conf_t           conf;
51
51
    ngx_pool_t          *pool;
52
52
    ngx_cycle_t         *cycle, **old;
53
53
    ngx_shm_zone_t      *shm_zone, *oshm_zone;
54
 
    ngx_slab_pool_t     *shpool;
55
54
    ngx_list_part_t     *part, *opart;
56
55
    ngx_open_file_t     *file;
57
56
    ngx_listening_t     *ls, *nls;
59
58
    ngx_core_module_t   *module;
60
59
    char                 hostname[NGX_MAXHOSTNAMELEN];
61
60
 
 
61
    ngx_timezone_update();
 
62
 
 
63
    /* force localtime update with a new timezone */
 
64
 
 
65
    tp = ngx_timeofday();
 
66
    tp->sec = 0;
 
67
 
 
68
    ngx_time_update();
 
69
 
 
70
 
62
71
    log = old_cycle->log;
63
72
 
64
73
    pool = ngx_create_pool(NGX_CYCLE_POOL_SIZE, log);
75
84
 
76
85
    cycle->pool = pool;
77
86
    cycle->log = log;
 
87
    cycle->new_log.log_level = NGX_LOG_ERR;
78
88
    cycle->old_cycle = old_cycle;
79
 
    cycle->root.len = sizeof(NGX_PREFIX) - 1;
80
 
    cycle->root.data = (u_char *) NGX_PREFIX;
81
 
 
 
89
 
 
90
    cycle->conf_prefix.len = old_cycle->conf_prefix.len;
 
91
    cycle->conf_prefix.data = ngx_pstrdup(pool, &old_cycle->conf_prefix);
 
92
    if (cycle->conf_prefix.data == NULL) {
 
93
        ngx_destroy_pool(pool);
 
94
        return NULL;
 
95
    }
 
96
 
 
97
    cycle->prefix.len = old_cycle->prefix.len;
 
98
    cycle->prefix.data = ngx_pstrdup(pool, &old_cycle->prefix);
 
99
    if (cycle->prefix.data == NULL) {
 
100
        ngx_destroy_pool(pool);
 
101
        return NULL;
 
102
    }
82
103
 
83
104
    cycle->conf_file.len = old_cycle->conf_file.len;
84
 
    cycle->conf_file.data = ngx_palloc(pool, old_cycle->conf_file.len + 1);
 
105
    cycle->conf_file.data = ngx_pnalloc(pool, old_cycle->conf_file.len + 1);
85
106
    if (cycle->conf_file.data == NULL) {
86
107
        ngx_destroy_pool(pool);
87
108
        return NULL;
89
110
    ngx_cpystrn(cycle->conf_file.data, old_cycle->conf_file.data,
90
111
                old_cycle->conf_file.len + 1);
91
112
 
92
 
 
93
 
    n = old_cycle->pathes.nelts ? old_cycle->pathes.nelts : 10;
94
 
 
95
 
    cycle->pathes.elts = ngx_pcalloc(pool, n * sizeof(ngx_path_t *));
96
 
    if (cycle->pathes.elts == NULL) {
97
 
        ngx_destroy_pool(pool);
98
 
        return NULL;
99
 
    }
100
 
 
101
 
    cycle->pathes.nelts = 0;
102
 
    cycle->pathes.size = sizeof(ngx_path_t *);
103
 
    cycle->pathes.nalloc = n;
104
 
    cycle->pathes.pool = pool;
 
113
    cycle->conf_param.len = old_cycle->conf_param.len;
 
114
    cycle->conf_param.data = ngx_pstrdup(pool, &old_cycle->conf_param);
 
115
    if (cycle->conf_param.data == NULL) {
 
116
        ngx_destroy_pool(pool);
 
117
        return NULL;
 
118
    }
 
119
 
 
120
 
 
121
    n = old_cycle->paths.nelts ? old_cycle->paths.nelts : 10;
 
122
 
 
123
    cycle->paths.elts = ngx_pcalloc(pool, n * sizeof(ngx_path_t *));
 
124
    if (cycle->paths.elts == NULL) {
 
125
        ngx_destroy_pool(pool);
 
126
        return NULL;
 
127
    }
 
128
 
 
129
    cycle->paths.nelts = 0;
 
130
    cycle->paths.size = sizeof(ngx_path_t *);
 
131
    cycle->paths.nalloc = n;
 
132
    cycle->paths.pool = pool;
105
133
 
106
134
 
107
135
    if (old_cycle->open_files.part.nelts) {
115
143
    }
116
144
 
117
145
    if (ngx_list_init(&cycle->open_files, pool, n, sizeof(ngx_open_file_t))
118
 
        == NGX_ERROR)
 
146
        != NGX_OK)
119
147
    {
120
148
        ngx_destroy_pool(pool);
121
149
        return NULL;
134
162
    }
135
163
 
136
164
    if (ngx_list_init(&cycle->shared_memory, pool, n, sizeof(ngx_shm_zone_t))
137
 
        == NGX_ERROR)
 
165
        != NGX_OK)
138
166
    {
139
167
        ngx_destroy_pool(pool);
140
168
        return NULL;
141
169
    }
142
170
 
143
 
 
144
 
    cycle->new_log = ngx_log_create_errlog(cycle, NULL);
145
 
    if (cycle->new_log == NULL) {
146
 
        ngx_destroy_pool(pool);
147
 
        return NULL;
148
 
    }
149
 
 
150
 
    cycle->new_log->file->name = error_log;
151
 
 
152
 
 
153
171
    n = old_cycle->listening.nelts ? old_cycle->listening.nelts : 10;
154
172
 
155
173
    cycle->listening.elts = ngx_pcalloc(pool, n * sizeof(ngx_listening_t));
164
182
    cycle->listening.pool = pool;
165
183
 
166
184
 
 
185
    ngx_queue_init(&cycle->reusable_connections_queue);
 
186
 
 
187
 
167
188
    cycle->conf_ctx = ngx_pcalloc(pool, ngx_max_module * sizeof(void *));
168
189
    if (cycle->conf_ctx == NULL) {
169
190
        ngx_destroy_pool(pool);
182
203
    hostname[NGX_MAXHOSTNAMELEN - 1] = '\0';
183
204
    cycle->hostname.len = ngx_strlen(hostname);
184
205
 
185
 
    cycle->hostname.data = ngx_palloc(pool, cycle->hostname.len);
 
206
    cycle->hostname.data = ngx_pnalloc(pool, cycle->hostname.len);
186
207
    if (cycle->hostname.data == NULL) {
187
208
        ngx_destroy_pool(pool);
188
209
        return NULL;
189
210
    }
190
211
 
191
 
    ngx_memcpy(cycle->hostname.data, hostname, cycle->hostname.len);
 
212
    ngx_strlow(cycle->hostname.data, (u_char *) hostname, cycle->hostname.len);
192
213
 
193
214
 
194
215
    for (i = 0; ngx_modules[i]; i++) {
200
221
 
201
222
        if (module->create_conf) {
202
223
            rv = module->create_conf(cycle);
203
 
            if (rv == NGX_CONF_ERROR) {
 
224
            if (rv == NULL) {
204
225
                ngx_destroy_pool(pool);
205
226
                return NULL;
206
227
            }
238
259
    log->log_level = NGX_LOG_DEBUG_ALL;
239
260
#endif
240
261
 
 
262
    if (ngx_conf_param(&conf) != NGX_CONF_OK) {
 
263
        environ = senv;
 
264
        ngx_destroy_cycle_pools(&conf);
 
265
        return NULL;
 
266
    }
 
267
 
241
268
    if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
 
269
        environ = senv;
242
270
        ngx_destroy_cycle_pools(&conf);
243
271
        return NULL;
244
272
    }
245
273
 
246
 
    if (ngx_test_config) {
247
 
        ngx_log_error(NGX_LOG_INFO, log, 0,
248
 
                      "the configuration file %s syntax is ok",
249
 
                      cycle->conf_file.data);
 
274
    if (ngx_test_config && !ngx_quiet_mode) {
 
275
        ngx_log_stderr(0, "the configuration file %s syntax is ok",
 
276
                       cycle->conf_file.data);
250
277
    }
251
278
 
252
 
 
253
279
    for (i = 0; ngx_modules[i]; i++) {
254
280
        if (ngx_modules[i]->type != NGX_CORE_MODULE) {
255
281
            continue;
261
287
            if (module->init_conf(cycle, cycle->conf_ctx[ngx_modules[i]->index])
262
288
                == NGX_CONF_ERROR)
263
289
            {
 
290
                environ = senv;
264
291
                ngx_destroy_cycle_pools(&conf);
265
292
                return NULL;
266
293
            }
267
294
        }
268
295
    }
269
296
 
 
297
    if (ngx_process == NGX_PROCESS_SIGNALLER) {
 
298
        return cycle;
 
299
    }
270
300
 
271
301
    ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
272
302
 
273
 
#if !(NGX_WIN32)
274
 
 
275
303
    if (ngx_test_config) {
276
304
 
277
305
        if (ngx_create_pidfile(&ccf->pid, log) != NGX_OK) {
300
328
        }
301
329
    }
302
330
 
303
 
#endif
304
 
 
305
331
 
306
332
    if (ngx_test_lockfile(cycle->lock_file.data, log) != NGX_OK) {
307
333
        goto failed;
308
334
    }
309
335
 
310
336
 
311
 
    if (ngx_create_pathes(cycle, ccf->user) != NGX_OK) {
 
337
    if (ngx_create_paths(cycle, ccf->user) != NGX_OK) {
312
338
        goto failed;
313
339
    }
314
340
 
315
341
 
 
342
    if (cycle->new_log.file == NULL) {
 
343
        cycle->new_log.file = ngx_conf_open_file(cycle, &error_log);
 
344
        if (cycle->new_log.file == NULL) {
 
345
            goto failed;
 
346
        }
 
347
    }
 
348
 
316
349
    /* open the new files */
317
350
 
318
351
    part = &cycle->open_files.part;
329
362
            i = 0;
330
363
        }
331
364
 
332
 
        if (file[i].name.data == NULL) {
 
365
        if (file[i].name.len == 0) {
333
366
            continue;
334
367
        }
335
368
 
336
 
        file[i].fd = ngx_open_file(file[i].name.data, NGX_FILE_RDWR,
337
 
                                   NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND,
 
369
        file[i].fd = ngx_open_file(file[i].name.data,
 
370
                                   NGX_FILE_APPEND,
 
371
                                   NGX_FILE_CREATE_OR_OPEN,
338
372
                                   NGX_FILE_DEFAULT_ACCESS);
339
373
 
340
374
        ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
348
382
            goto failed;
349
383
        }
350
384
 
351
 
#if (NGX_WIN32)
352
 
        if (ngx_file_append_mode(file[i].fd) != NGX_OK) {
353
 
            ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
354
 
                          ngx_file_append_mode_n " \"%s\" failed",
355
 
                          file[i].name.data);
356
 
            goto failed;
357
 
        }
358
 
#else
 
385
#if !(NGX_WIN32)
359
386
        if (fcntl(file[i].fd, F_SETFD, FD_CLOEXEC) == -1) {
360
387
            ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
361
388
                          "fcntl(FD_CLOEXEC) \"%s\" failed",
365
392
#endif
366
393
    }
367
394
 
368
 
    cycle->log = cycle->new_log;
369
 
    pool->log = cycle->new_log;
370
 
 
371
 
    if (cycle->log->log_level == 0) {
372
 
        cycle->log->log_level = NGX_LOG_ERR;
373
 
    }
 
395
    cycle->log = &cycle->new_log;
 
396
    pool->log = &cycle->new_log;
374
397
 
375
398
 
376
399
    /* create shared memory */
392
415
        if (shm_zone[i].shm.size == 0) {
393
416
            ngx_log_error(NGX_LOG_EMERG, log, 0,
394
417
                          "zero size shared memory zone \"%V\"",
395
 
                          &shm_zone[i].name);
 
418
                          &shm_zone[i].shm.name);
396
419
            goto failed;
397
420
        }
398
421
 
399
 
        if (shm_zone[i].init == NULL) {
400
 
            /* unused shared zone */
401
 
            continue;
402
 
        }
403
 
 
404
422
        shm_zone[i].shm.log = cycle->log;
405
423
 
406
424
        opart = &old_cycle->shared_memory.part;
417
435
                n = 0;
418
436
            }
419
437
 
420
 
            if (shm_zone[i].name.len != oshm_zone[n].name.len) {
 
438
            if (shm_zone[i].shm.name.len != oshm_zone[n].shm.name.len) {
421
439
                continue;
422
440
            }
423
441
 
424
 
            if (ngx_strncmp(shm_zone[i].name.data, oshm_zone[n].name.data,
425
 
                            shm_zone[i].name.len)
 
442
            if (ngx_strncmp(shm_zone[i].shm.name.data,
 
443
                            oshm_zone[n].shm.name.data,
 
444
                            shm_zone[i].shm.name.len)
426
445
                != 0)
427
446
            {
428
447
                continue;
429
448
            }
430
449
 
431
 
            if (shm_zone[i].shm.size == oshm_zone[n].shm.size) {
 
450
            if (shm_zone[i].tag == oshm_zone[n].tag
 
451
                && shm_zone[i].shm.size == oshm_zone[n].shm.size)
 
452
            {
432
453
                shm_zone[i].shm.addr = oshm_zone[n].shm.addr;
433
454
 
434
455
                if (shm_zone[i].init(&shm_zone[i], oshm_zone[n].data)
449
470
            goto failed;
450
471
        }
451
472
 
452
 
        shpool = (ngx_slab_pool_t *) shm_zone[i].shm.addr;
453
 
 
454
 
        shpool->end = shm_zone[i].shm.addr + shm_zone[i].shm.size;
455
 
        shpool->min_shift = 3;
456
 
 
457
 
#if (NGX_HAVE_ATOMIC_OPS)
458
 
 
459
 
        lock_file = NULL;
460
 
 
461
 
#else
462
 
 
463
 
        lock_file = ngx_palloc(cycle->pool,
464
 
                               cycle->lock_file.len + shm_zone[i].name.len);
465
 
 
466
 
        if (lock_file == NULL) {
467
 
            goto failed;
468
 
        }
469
 
 
470
 
        (void) ngx_cpystrn(ngx_cpymem(lock_file, cycle->lock_file.data,
471
 
                                      cycle->lock_file.len),
472
 
                           shm_zone[i].name.data, shm_zone[i].name.len + 1);
473
 
 
474
 
#endif
475
 
 
476
 
        if (ngx_shmtx_create(&shpool->mutex, (void *) &shpool->lock, lock_file)
477
 
            != NGX_OK)
478
 
        {
479
 
            goto failed;
480
 
        }
481
 
 
482
 
        ngx_slab_init(shpool);
 
473
        if (ngx_init_zone_pool(cycle, &shm_zone[i]) != NGX_OK) {
 
474
            goto failed;
 
475
        }
483
476
 
484
477
        if (shm_zone[i].init(&shm_zone[i], NULL) != NGX_OK) {
485
478
            goto failed;
583
576
    }
584
577
 
585
578
    if (!ngx_test_config) {
586
 
        ngx_configure_listening_socket(cycle);
 
579
        ngx_configure_listening_sockets(cycle);
587
580
    }
588
581
 
589
582
 
590
583
    /* commit the new cycle configuration */
591
584
 
592
 
#if !(NGX_WIN32)
593
 
 
594
 
    if (!ngx_test_config && cycle->log->file->fd != STDERR_FILENO) {
595
 
 
596
 
        ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
597
 
                       "dup2: %p %d \"%s\"",
598
 
                       cycle->log->file,
599
 
                       cycle->log->file->fd, cycle->log->file->name.data);
600
 
 
601
 
        if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) {
602
 
            ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
603
 
                          "dup2(STDERR) failed");
604
 
            /* fatal */
605
 
            exit(1);
 
585
    if (!ngx_use_stderr && cycle->log->file->fd != ngx_stderr) {
 
586
 
 
587
        if (ngx_set_stderr(cycle->log->file->fd) == NGX_FILE_ERROR) {
 
588
            ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
 
589
                          ngx_set_stderr_n " failed");
606
590
        }
607
591
    }
608
592
 
609
 
#endif
610
 
 
611
593
    pool->log = cycle->log;
612
594
 
613
595
    for (i = 0; ngx_modules[i]; i++) {
652
634
                n = 0;
653
635
            }
654
636
 
655
 
            if (oshm_zone[i].name.len == shm_zone[n].name.len
656
 
                && ngx_strncmp(oshm_zone[i].name.data,
657
 
                               shm_zone[n].name.data,
658
 
                               oshm_zone[i].name.len)
 
637
            if (oshm_zone[i].shm.name.len == shm_zone[n].shm.name.len
 
638
                && ngx_strncmp(oshm_zone[i].shm.name.data,
 
639
                               shm_zone[n].shm.name.data,
 
640
                               oshm_zone[i].shm.name.len)
659
641
                == 0)
660
642
            {
661
643
                goto live_shm_zone;
676
658
 
677
659
    ls = old_cycle->listening.elts;
678
660
    for (i = 0; i < old_cycle->listening.nelts; i++) {
679
 
        if (ls[i].remain) {
 
661
 
 
662
        if (ls[i].remain || ls[i].fd == -1) {
680
663
            continue;
681
664
        }
682
665
 
685
668
                          ngx_close_socket_n " listening socket on %V failed",
686
669
                          &ls[i].addr_text);
687
670
        }
 
671
 
 
672
#if (NGX_HAVE_UNIX_DOMAIN)
 
673
 
 
674
        if (ls[i].sockaddr->sa_family == AF_UNIX) {
 
675
            u_char  *name;
 
676
 
 
677
            name = ls[i].addr_text.data + sizeof("unix:") - 1;
 
678
 
 
679
            ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
 
680
                          "deleting socket %s", name);
 
681
 
 
682
            if (ngx_delete_file(name) == NGX_FILE_ERROR) {
 
683
                ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno,
 
684
                              ngx_delete_file_n " %s failed", name);
 
685
            }
 
686
        }
 
687
 
 
688
#endif
688
689
    }
689
690
 
690
691
 
704
705
            i = 0;
705
706
        }
706
707
 
707
 
        if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr_fileno) {
 
708
        if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) {
708
709
            continue;
709
710
        }
710
711
 
720
721
    if (ngx_process == NGX_PROCESS_MASTER || ngx_is_init_cycle(old_cycle)) {
721
722
 
722
723
        /*
723
 
         * perl_destruct() frees environ if it is not the same as it was at
724
 
         * perl_construct() time.  So we have saved an previous cycle
 
724
         * perl_destruct() frees environ, if it is not the same as it was at
 
725
         * perl_construct() time, therefore we save the previous cycle
725
726
         * environment before ngx_conf_parse() where it will be changed.
726
727
         */
727
728
 
741
742
        ngx_temp_pool = ngx_create_pool(128, cycle->log);
742
743
        if (ngx_temp_pool == NULL) {
743
744
            ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
744
 
                          "can not create ngx_temp_pool");
 
745
                          "could not create ngx_temp_pool");
745
746
            exit(1);
746
747
        }
747
748
 
804
805
            i = 0;
805
806
        }
806
807
 
807
 
        if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr_fileno) {
 
808
        if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) {
808
809
            continue;
809
810
        }
810
811
 
850
851
static ngx_int_t
851
852
ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2)
852
853
{
853
 
    struct sockaddr_in  *sin1, *sin2;
854
 
 
855
 
    /* AF_INET only */
856
 
 
857
 
    if (sa1->sa_family != AF_INET || sa2->sa_family != AF_INET) {
858
 
        return NGX_DECLINED;
859
 
    }
860
 
 
861
 
    sin1 = (struct sockaddr_in *) sa1;
862
 
    sin2 = (struct sockaddr_in *) sa2;
863
 
 
864
 
    if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) {
865
 
        return NGX_DECLINED;
866
 
    }
867
 
 
868
 
    if (sin1->sin_port != sin2->sin_port) {
869
 
        return NGX_DECLINED;
870
 
    }
871
 
 
872
 
    return NGX_OK;
873
 
}
874
 
 
875
 
 
876
 
#if !(NGX_WIN32)
 
854
    struct sockaddr_in   *sin1, *sin2;
 
855
#if (NGX_HAVE_INET6)
 
856
    struct sockaddr_in6  *sin61, *sin62;
 
857
#endif
 
858
#if (NGX_HAVE_UNIX_DOMAIN)
 
859
    struct sockaddr_un   *saun1, *saun2;
 
860
#endif
 
861
 
 
862
    if (sa1->sa_family != sa2->sa_family) {
 
863
        return NGX_DECLINED;
 
864
    }
 
865
 
 
866
    switch (sa1->sa_family) {
 
867
 
 
868
#if (NGX_HAVE_INET6)
 
869
    case AF_INET6:
 
870
        sin61 = (struct sockaddr_in6 *) sa1;
 
871
        sin62 = (struct sockaddr_in6 *) sa2;
 
872
 
 
873
        if (sin61->sin6_port != sin62->sin6_port) {
 
874
            return NGX_DECLINED;
 
875
        }
 
876
 
 
877
        if (ngx_memcmp(&sin61->sin6_addr, &sin62->sin6_addr, 16) != 0) {
 
878
            return NGX_DECLINED;
 
879
        }
 
880
 
 
881
        break;
 
882
#endif
 
883
 
 
884
#if (NGX_HAVE_UNIX_DOMAIN)
 
885
    case AF_UNIX:
 
886
       saun1 = (struct sockaddr_un *) sa1;
 
887
       saun2 = (struct sockaddr_un *) sa2;
 
888
 
 
889
       if (ngx_memcmp(&saun1->sun_path, &saun2->sun_path,
 
890
                      sizeof(saun1->sun_path))
 
891
           != 0)
 
892
       {
 
893
           return NGX_DECLINED;
 
894
       }
 
895
 
 
896
       break;
 
897
#endif
 
898
 
 
899
    default: /* AF_INET */
 
900
 
 
901
        sin1 = (struct sockaddr_in *) sa1;
 
902
        sin2 = (struct sockaddr_in *) sa2;
 
903
 
 
904
        if (sin1->sin_port != sin2->sin_port) {
 
905
            return NGX_DECLINED;
 
906
        }
 
907
 
 
908
        if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) {
 
909
            return NGX_DECLINED;
 
910
        }
 
911
 
 
912
        break;
 
913
    }
 
914
 
 
915
    return NGX_OK;
 
916
}
 
917
 
 
918
 
 
919
static ngx_int_t
 
920
ngx_init_zone_pool(ngx_cycle_t *cycle, ngx_shm_zone_t *zn)
 
921
{
 
922
    u_char           *file;
 
923
    ngx_slab_pool_t  *sp;
 
924
 
 
925
    sp = (ngx_slab_pool_t *) zn->shm.addr;
 
926
 
 
927
    if (zn->shm.exists) {
 
928
 
 
929
        if (sp == sp->addr) {
 
930
            return NGX_OK;
 
931
        }
 
932
 
 
933
        ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
 
934
                      "shared zone \"%V\" has no equal addresses: %p vs %p",
 
935
                      &zn->shm.name, sp->addr, sp);
 
936
        return NGX_ERROR;
 
937
    }
 
938
 
 
939
    sp->end = zn->shm.addr + zn->shm.size;
 
940
    sp->min_shift = 3;
 
941
    sp->addr = zn->shm.addr;
 
942
 
 
943
#if (NGX_HAVE_ATOMIC_OPS)
 
944
 
 
945
    file = NULL;
 
946
 
 
947
#else
 
948
 
 
949
    file = ngx_pnalloc(cycle->pool, cycle->lock_file.len + zn->shm.name.len);
 
950
    if (file == NULL) {
 
951
        return NGX_ERROR;
 
952
    }
 
953
 
 
954
    (void) ngx_sprintf(file, "%V%V%Z", &cycle->lock_file, &zn->shm.name);
 
955
 
 
956
#endif
 
957
 
 
958
    if (ngx_shmtx_create(&sp->mutex, &sp->lock, file) != NGX_OK) {
 
959
        return NGX_ERROR;
 
960
    }
 
961
 
 
962
    ngx_slab_init(sp);
 
963
 
 
964
    return NGX_OK;
 
965
}
 
966
 
877
967
 
878
968
ngx_int_t
879
969
ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log)
880
970
{
881
 
    size_t            len;
882
 
    ngx_uint_t        trunc;
883
 
    ngx_file_t        file;
884
 
    u_char            pid[NGX_INT64_LEN + 2];
 
971
    size_t      len;
 
972
    ngx_uint_t  create;
 
973
    ngx_file_t  file;
 
974
    u_char      pid[NGX_INT64_LEN + 2];
 
975
 
 
976
    if (ngx_process > NGX_PROCESS_MASTER) {
 
977
        return NGX_OK;
 
978
    }
885
979
 
886
980
    ngx_memzero(&file, sizeof(ngx_file_t));
887
981
 
888
982
    file.name = *name;
889
983
    file.log = log;
890
984
 
891
 
    trunc = ngx_test_config ? 0 : NGX_FILE_TRUNCATE;
 
985
    create = ngx_test_config ? NGX_FILE_CREATE_OR_OPEN : NGX_FILE_TRUNCATE;
892
986
 
893
987
    file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR,
894
 
                            NGX_FILE_CREATE_OR_OPEN|trunc,
895
 
                            NGX_FILE_DEFAULT_ACCESS);
 
988
                            create, NGX_FILE_DEFAULT_ACCESS);
896
989
 
897
990
    if (file.fd == NGX_INVALID_FILE) {
898
991
        ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
933
1026
    }
934
1027
}
935
1028
 
936
 
#endif
 
1029
 
 
1030
ngx_int_t
 
1031
ngx_signal_process(ngx_cycle_t *cycle, char *sig)
 
1032
{
 
1033
    ssize_t           n;
 
1034
    ngx_int_t         pid;
 
1035
    ngx_file_t        file;
 
1036
    ngx_core_conf_t  *ccf;
 
1037
    u_char            buf[NGX_INT64_LEN + 2];
 
1038
 
 
1039
    ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "signal process started");
 
1040
 
 
1041
    ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
 
1042
 
 
1043
    ngx_memzero(&file, sizeof(ngx_file_t));
 
1044
 
 
1045
    file.name = ccf->pid;
 
1046
    file.log = cycle->log;
 
1047
 
 
1048
    file.fd = ngx_open_file(file.name.data, NGX_FILE_RDONLY,
 
1049
                            NGX_FILE_OPEN, NGX_FILE_DEFAULT_ACCESS);
 
1050
 
 
1051
    if (file.fd == NGX_INVALID_FILE) {
 
1052
        ngx_log_error(NGX_LOG_ERR, cycle->log, ngx_errno,
 
1053
                      ngx_open_file_n " \"%s\" failed", file.name.data);
 
1054
        return 1;
 
1055
    }
 
1056
 
 
1057
    n = ngx_read_file(&file, buf, NGX_INT64_LEN + 2, 0);
 
1058
 
 
1059
    if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
 
1060
        ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
 
1061
                      ngx_close_file_n " \"%s\" failed", file.name.data);
 
1062
    }
 
1063
 
 
1064
    if (n == NGX_ERROR) {
 
1065
        return 1;
 
1066
    }
 
1067
 
 
1068
    while (n-- && (buf[n] == CR || buf[n] == LF)) { /* void */ }
 
1069
 
 
1070
    pid = ngx_atoi(buf, ++n);
 
1071
 
 
1072
    if (pid == NGX_ERROR) {
 
1073
        ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
 
1074
                      "invalid PID number \"%*s\" in \"%s\"",
 
1075
                      n, buf, file.name.data);
 
1076
        return 1;
 
1077
    }
 
1078
 
 
1079
    return ngx_os_signal_process(cycle, sig, pid);
 
1080
 
 
1081
}
937
1082
 
938
1083
 
939
1084
static ngx_int_t
970
1115
void
971
1116
ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user)
972
1117
{
973
 
    ssize_t           n, len;
974
1118
    ngx_fd_t          fd;
975
1119
    ngx_uint_t        i;
976
1120
    ngx_list_part_t  *part;
990
1134
            i = 0;
991
1135
        }
992
1136
 
993
 
        if (file[i].name.data == NULL) {
 
1137
        if (file[i].name.len == 0) {
994
1138
            continue;
995
1139
        }
996
1140
 
997
 
        len = file[i].pos - file[i].buffer;
998
 
 
999
 
        if (file[i].buffer && len != 0) {
1000
 
 
1001
 
            n = ngx_write_fd(file[i].fd, file[i].buffer, len);
1002
 
 
1003
 
            if (n == NGX_FILE_ERROR) {
1004
 
                ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
1005
 
                              ngx_write_fd_n " to \"%s\" failed",
1006
 
                              file[i].name.data);
1007
 
 
1008
 
            } else if (n != len) {
1009
 
                ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
1010
 
                          ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
1011
 
                          file[i].name.data, n, len);
1012
 
            }
1013
 
 
1014
 
            file[i].pos = file[i].buffer;
 
1141
        if (file[i].flush) {
 
1142
            file[i].flush(&file[i], cycle->log);
1015
1143
        }
1016
1144
 
1017
 
        fd = ngx_open_file(file[i].name.data, NGX_FILE_RDWR,
1018
 
                           NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND,
1019
 
                           NGX_FILE_DEFAULT_ACCESS);
 
1145
        fd = ngx_open_file(file[i].name.data, NGX_FILE_APPEND,
 
1146
                           NGX_FILE_CREATE_OR_OPEN, NGX_FILE_DEFAULT_ACCESS);
1020
1147
 
1021
1148
        ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
1022
1149
                       "reopen file \"%s\", old:%d new:%d",
1028
1155
            continue;
1029
1156
        }
1030
1157
 
1031
 
#if (NGX_WIN32)
1032
 
        if (ngx_file_append_mode(fd) == NGX_ERROR) {
1033
 
            ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1034
 
                          ngx_file_append_mode_n " \"%s\" failed",
1035
 
                          file[i].name.data);
1036
 
 
1037
 
            if (ngx_close_file(fd) == NGX_FILE_ERROR) {
1038
 
                ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1039
 
                              ngx_close_file_n " \"%s\" failed",
1040
 
                              file[i].name.data);
1041
 
            }
1042
 
 
1043
 
            continue;
1044
 
        }
1045
 
#else
 
1158
#if !(NGX_WIN32)
1046
1159
        if (user != (ngx_uid_t) NGX_CONF_UNSET_UINT) {
1047
1160
            ngx_file_info_t  fi;
1048
1161
 
1049
 
            if (ngx_file_info((const char *) file[i].name.data, &fi) == -1) {
 
1162
            if (ngx_file_info((const char *) file[i].name.data, &fi)
 
1163
                == NGX_FILE_ERROR)
 
1164
            {
1050
1165
                ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
1051
1166
                              ngx_file_info_n " \"%s\" failed",
1052
1167
                              file[i].name.data);
1147
1262
            i = 0;
1148
1263
        }
1149
1264
 
1150
 
        if (name->len != shm_zone[i].name.len) {
1151
 
            continue;
1152
 
        }
1153
 
 
1154
 
        if (ngx_strncmp(name->data, shm_zone[i].name.data, name->len) != 0) {
1155
 
            continue;
 
1265
        if (name->len != shm_zone[i].shm.name.len) {
 
1266
            continue;
 
1267
        }
 
1268
 
 
1269
        if (ngx_strncmp(name->data, shm_zone[i].shm.name.data, name->len)
 
1270
            != 0)
 
1271
        {
 
1272
            continue;
 
1273
        }
 
1274
 
 
1275
        if (tag != shm_zone[i].tag) {
 
1276
            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
 
1277
                            "the shared memory zone \"%V\" is "
 
1278
                            "already declared for a different use",
 
1279
                            &shm_zone[i].shm.name);
 
1280
            return NULL;
1156
1281
        }
1157
1282
 
1158
1283
        if (size && size != shm_zone[i].shm.size) {
1159
1284
            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1160
 
                               "the size %uz of shared memory zone \"%V\" "
1161
 
                               "conflicts with already declared size %uz",
1162
 
                               size, &shm_zone[i].name, shm_zone[i].shm.size);
1163
 
            return NULL;
1164
 
        }
1165
 
 
1166
 
        if (tag != shm_zone[i].tag) {
1167
 
            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1168
 
                               "the shared memory zone \"%V\" is "
1169
 
                               "already declared for a different use",
1170
 
                               &shm_zone[i].name);
 
1285
                            "the size %uz of shared memory zone \"%V\" "
 
1286
                            "conflicts with already declared size %uz",
 
1287
                            size, &shm_zone[i].shm.name, shm_zone[i].shm.size);
1171
1288
            return NULL;
1172
1289
        }
1173
1290
 
1183
1300
    shm_zone->data = NULL;
1184
1301
    shm_zone->shm.log = cf->cycle->log;
1185
1302
    shm_zone->shm.size = size;
 
1303
    shm_zone->shm.name = *name;
 
1304
    shm_zone->shm.exists = 0;
1186
1305
    shm_zone->init = NULL;
1187
 
    shm_zone->name = *name;
1188
1306
    shm_zone->tag = tag;
1189
1307
 
1190
1308
    return shm_zone;