9
10
#include <ngx_event.h>
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);
76
85
cycle->pool = pool;
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;
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);
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);
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);
89
110
ngx_cpystrn(cycle->conf_file.data, old_cycle->conf_file.data,
90
111
old_cycle->conf_file.len + 1);
93
n = old_cycle->pathes.nelts ? old_cycle->pathes.nelts : 10;
95
cycle->pathes.elts = ngx_pcalloc(pool, n * sizeof(ngx_path_t *));
96
if (cycle->pathes.elts == NULL) {
97
ngx_destroy_pool(pool);
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);
121
n = old_cycle->paths.nelts ? old_cycle->paths.nelts : 10;
123
cycle->paths.elts = ngx_pcalloc(pool, n * sizeof(ngx_path_t *));
124
if (cycle->paths.elts == NULL) {
125
ngx_destroy_pool(pool);
129
cycle->paths.nelts = 0;
130
cycle->paths.size = sizeof(ngx_path_t *);
131
cycle->paths.nalloc = n;
132
cycle->paths.pool = pool;
107
135
if (old_cycle->open_files.part.nelts) {
182
203
hostname[NGX_MAXHOSTNAMELEN - 1] = '\0';
183
204
cycle->hostname.len = ngx_strlen(hostname);
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);
191
ngx_memcpy(cycle->hostname.data, hostname, cycle->hostname.len);
212
ngx_strlow(cycle->hostname.data, (u_char *) hostname, cycle->hostname.len);
194
215
for (i = 0; ngx_modules[i]; i++) {
238
259
log->log_level = NGX_LOG_DEBUG_ALL;
262
if (ngx_conf_param(&conf) != NGX_CONF_OK) {
264
ngx_destroy_cycle_pools(&conf);
241
268
if (ngx_conf_parse(&conf, &cycle->conf_file) != NGX_CONF_OK) {
242
270
ngx_destroy_cycle_pools(&conf);
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);
253
279
for (i = 0; ngx_modules[i]; i++) {
254
280
if (ngx_modules[i]->type != NGX_CORE_MODULE) {
332
if (file[i].name.data == NULL) {
365
if (file[i].name.len == 0) {
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,
371
NGX_FILE_CREATE_OR_OPEN,
338
372
NGX_FILE_DEFAULT_ACCESS);
340
374
ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
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",
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",
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) {
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)
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)
432
453
shm_zone[i].shm.addr = oshm_zone[n].shm.addr;
434
455
if (shm_zone[i].init(&shm_zone[i], oshm_zone[n].data)
452
shpool = (ngx_slab_pool_t *) shm_zone[i].shm.addr;
454
shpool->end = shm_zone[i].shm.addr + shm_zone[i].shm.size;
455
shpool->min_shift = 3;
457
#if (NGX_HAVE_ATOMIC_OPS)
463
lock_file = ngx_palloc(cycle->pool,
464
cycle->lock_file.len + shm_zone[i].name.len);
466
if (lock_file == NULL) {
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);
476
if (ngx_shmtx_create(&shpool->mutex, (void *) &shpool->lock, lock_file)
482
ngx_slab_init(shpool);
473
if (ngx_init_zone_pool(cycle, &shm_zone[i]) != NGX_OK) {
484
477
if (shm_zone[i].init(&shm_zone[i], NULL) != NGX_OK) {
585
578
if (!ngx_test_config) {
586
ngx_configure_listening_socket(cycle);
579
ngx_configure_listening_sockets(cycle);
590
583
/* commit the new cycle configuration */
594
if (!ngx_test_config && cycle->log->file->fd != STDERR_FILENO) {
596
ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0,
597
"dup2: %p %d \"%s\"",
599
cycle->log->file->fd, cycle->log->file->name.data);
601
if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) {
602
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
603
"dup2(STDERR) failed");
585
if (!ngx_use_stderr && cycle->log->file->fd != ngx_stderr) {
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");
611
593
pool->log = cycle->log;
613
595
for (i = 0; ngx_modules[i]; i++) {
685
668
ngx_close_socket_n " listening socket on %V failed",
686
669
&ls[i].addr_text);
672
#if (NGX_HAVE_UNIX_DOMAIN)
674
if (ls[i].sockaddr->sa_family == AF_UNIX) {
677
name = ls[i].addr_text.data + sizeof("unix:") - 1;
679
ngx_log_error(NGX_LOG_WARN, cycle->log, 0,
680
"deleting socket %s", name);
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);
851
852
ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2)
853
struct sockaddr_in *sin1, *sin2;
857
if (sa1->sa_family != AF_INET || sa2->sa_family != AF_INET) {
861
sin1 = (struct sockaddr_in *) sa1;
862
sin2 = (struct sockaddr_in *) sa2;
864
if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) {
868
if (sin1->sin_port != sin2->sin_port) {
854
struct sockaddr_in *sin1, *sin2;
856
struct sockaddr_in6 *sin61, *sin62;
858
#if (NGX_HAVE_UNIX_DOMAIN)
859
struct sockaddr_un *saun1, *saun2;
862
if (sa1->sa_family != sa2->sa_family) {
866
switch (sa1->sa_family) {
870
sin61 = (struct sockaddr_in6 *) sa1;
871
sin62 = (struct sockaddr_in6 *) sa2;
873
if (sin61->sin6_port != sin62->sin6_port) {
877
if (ngx_memcmp(&sin61->sin6_addr, &sin62->sin6_addr, 16) != 0) {
884
#if (NGX_HAVE_UNIX_DOMAIN)
886
saun1 = (struct sockaddr_un *) sa1;
887
saun2 = (struct sockaddr_un *) sa2;
889
if (ngx_memcmp(&saun1->sun_path, &saun2->sun_path,
890
sizeof(saun1->sun_path))
899
default: /* AF_INET */
901
sin1 = (struct sockaddr_in *) sa1;
902
sin2 = (struct sockaddr_in *) sa2;
904
if (sin1->sin_port != sin2->sin_port) {
908
if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr) {
920
ngx_init_zone_pool(ngx_cycle_t *cycle, ngx_shm_zone_t *zn)
925
sp = (ngx_slab_pool_t *) zn->shm.addr;
927
if (zn->shm.exists) {
929
if (sp == sp->addr) {
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);
939
sp->end = zn->shm.addr + zn->shm.size;
941
sp->addr = zn->shm.addr;
943
#if (NGX_HAVE_ATOMIC_OPS)
949
file = ngx_pnalloc(cycle->pool, cycle->lock_file.len + zn->shm.name.len);
954
(void) ngx_sprintf(file, "%V%V%Z", &cycle->lock_file, &zn->shm.name);
958
if (ngx_shmtx_create(&sp->mutex, &sp->lock, file) != NGX_OK) {
879
969
ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log)
884
u_char pid[NGX_INT64_LEN + 2];
974
u_char pid[NGX_INT64_LEN + 2];
976
if (ngx_process > NGX_PROCESS_MASTER) {
886
980
ngx_memzero(&file, sizeof(ngx_file_t));
888
982
file.name = *name;
891
trunc = ngx_test_config ? 0 : NGX_FILE_TRUNCATE;
985
create = ngx_test_config ? NGX_FILE_CREATE_OR_OPEN : NGX_FILE_TRUNCATE;
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);
897
990
if (file.fd == NGX_INVALID_FILE) {
898
991
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
1031
ngx_signal_process(ngx_cycle_t *cycle, char *sig)
1036
ngx_core_conf_t *ccf;
1037
u_char buf[NGX_INT64_LEN + 2];
1039
ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "signal process started");
1041
ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
1043
ngx_memzero(&file, sizeof(ngx_file_t));
1045
file.name = ccf->pid;
1046
file.log = cycle->log;
1048
file.fd = ngx_open_file(file.name.data, NGX_FILE_RDONLY,
1049
NGX_FILE_OPEN, NGX_FILE_DEFAULT_ACCESS);
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);
1057
n = ngx_read_file(&file, buf, NGX_INT64_LEN + 2, 0);
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);
1064
if (n == NGX_ERROR) {
1068
while (n-- && (buf[n] == CR || buf[n] == LF)) { /* void */ }
1070
pid = ngx_atoi(buf, ++n);
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);
1079
return ngx_os_signal_process(cycle, sig, pid);
939
1084
static ngx_int_t
993
if (file[i].name.data == NULL) {
1137
if (file[i].name.len == 0) {
997
len = file[i].pos - file[i].buffer;
999
if (file[i].buffer && len != 0) {
1001
n = ngx_write_fd(file[i].fd, file[i].buffer, len);
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",
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);
1014
file[i].pos = file[i].buffer;
1141
if (file[i].flush) {
1142
file[i].flush(&file[i], cycle->log);
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);
1021
1148
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
1022
1149
"reopen file \"%s\", old:%d new:%d",
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",
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",
1046
1159
if (user != (ngx_uid_t) NGX_CONF_UNSET_UINT) {
1047
1160
ngx_file_info_t fi;
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)
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);
1150
if (name->len != shm_zone[i].name.len) {
1154
if (ngx_strncmp(name->data, shm_zone[i].name.data, name->len) != 0) {
1265
if (name->len != shm_zone[i].shm.name.len) {
1269
if (ngx_strncmp(name->data, shm_zone[i].shm.name.data, name->len)
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);
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);
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",
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);