comparison src/core/ngx_cycle.c @ 2720:b3b8c66bd520

support attaching to an existent Win32 shared memory
author Igor Sysoev <igor@sysoev.ru>
date Sat, 18 Apr 2009 19:27:28 +0000
parents 73fe3fe858b9
children ffa8e2451457
comparison
equal deleted inserted replaced
2719:9237cf8b400b 2720:b3b8c66bd520
7 #include <ngx_config.h> 7 #include <ngx_config.h>
8 #include <ngx_core.h> 8 #include <ngx_core.h>
9 #include <ngx_event.h> 9 #include <ngx_event.h>
10 10
11 11
12 static ngx_int_t ngx_test_lockfile(u_char *file, ngx_log_t *log);
13 static void ngx_destroy_cycle_pools(ngx_conf_t *conf); 12 static void ngx_destroy_cycle_pools(ngx_conf_t *conf);
14 static ngx_int_t ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2); 13 static ngx_int_t ngx_cmp_sockaddr(struct sockaddr *sa1, struct sockaddr *sa2);
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);
15 static void ngx_clean_old_cycles(ngx_event_t *ev); 17 static void ngx_clean_old_cycles(ngx_event_t *ev);
16 18
17 19
18 volatile ngx_cycle_t *ngx_cycle; 20 volatile ngx_cycle_t *ngx_cycle;
19 ngx_array_t ngx_old_cycles; 21 ngx_array_t ngx_old_cycles;
42 ngx_cycle_t * 44 ngx_cycle_t *
43 ngx_init_cycle(ngx_cycle_t *old_cycle) 45 ngx_init_cycle(ngx_cycle_t *old_cycle)
44 { 46 {
45 void *rv; 47 void *rv;
46 char **senv, **env; 48 char **senv, **env;
47 u_char *lock_file;
48 ngx_uint_t i, n; 49 ngx_uint_t i, n;
49 ngx_log_t *log; 50 ngx_log_t *log;
50 ngx_time_t *tp; 51 ngx_time_t *tp;
51 ngx_conf_t conf; 52 ngx_conf_t conf;
52 ngx_pool_t *pool; 53 ngx_pool_t *pool;
53 ngx_cycle_t *cycle, **old; 54 ngx_cycle_t *cycle, **old;
54 ngx_shm_zone_t *shm_zone, *oshm_zone; 55 ngx_shm_zone_t *shm_zone, *oshm_zone;
55 ngx_slab_pool_t *shpool;
56 ngx_list_part_t *part, *opart; 56 ngx_list_part_t *part, *opart;
57 ngx_open_file_t *file; 57 ngx_open_file_t *file;
58 ngx_listening_t *ls, *nls; 58 ngx_listening_t *ls, *nls;
59 ngx_core_conf_t *ccf, *old_ccf; 59 ngx_core_conf_t *ccf, *old_ccf;
60 ngx_core_module_t *module; 60 ngx_core_module_t *module;
468 468
469 if (ngx_shm_alloc(&shm_zone[i].shm) != NGX_OK) { 469 if (ngx_shm_alloc(&shm_zone[i].shm) != NGX_OK) {
470 goto failed; 470 goto failed;
471 } 471 }
472 472
473 shpool = (ngx_slab_pool_t *) shm_zone[i].shm.addr; 473 if (!shm_zone[i].shm.exists) {
474 474
475 shpool->end = shm_zone[i].shm.addr + shm_zone[i].shm.size; 475 if (ngx_init_zone_pool(cycle, &shm_zone[i]) != NGX_OK) {
476 shpool->min_shift = 3; 476 goto failed;
477 477 }
478 #if (NGX_HAVE_ATOMIC_OPS) 478 }
479
480 lock_file = NULL;
481
482 #else
483
484 lock_file = ngx_pnalloc(cycle->pool,
485 cycle->lock_file.len + shm_zone[i].shm.name.len);
486
487 if (lock_file == NULL) {
488 goto failed;
489 }
490
491 (void) ngx_cpystrn(ngx_cpymem(lock_file, cycle->lock_file.data,
492 cycle->lock_file.len),
493 shm_zone[i].shm.name.data,
494 shm_zone[i].shm.name.len + 1);
495
496 #endif
497
498 if (ngx_shmtx_create(&shpool->mutex, (void *) &shpool->lock, lock_file)
499 != NGX_OK)
500 {
501 goto failed;
502 }
503
504 ngx_slab_init(shpool);
505 479
506 if (shm_zone[i].init(&shm_zone[i], NULL) != NGX_OK) { 480 if (shm_zone[i].init(&shm_zone[i], NULL) != NGX_OK) {
507 goto failed; 481 goto failed;
508 } 482 }
509 483
917 891
918 return NGX_OK; 892 return NGX_OK;
919 } 893 }
920 894
921 895
896 static ngx_int_t
897 ngx_init_zone_pool(ngx_cycle_t *cycle, ngx_shm_zone_t *zn)
898 {
899 u_char *file;
900 ngx_slab_pool_t *sp;
901
902 sp = (ngx_slab_pool_t *) zn->shm.addr;
903
904 sp->end = zn->shm.addr + zn->shm.size;
905 sp->min_shift = 3;
906
907 #if (NGX_HAVE_ATOMIC_OPS)
908
909 file = NULL;
910
911 #else
912
913 file = ngx_pnalloc(cycle->pool, cycle->lock_file.len + zn->shm.name.len);
914 if (file == NULL) {
915 return NGX_ERROR;
916 }
917
918 (void) ngx_sprintf(file, "%V%V%Z", &cycle->lock_file, &zn->shm.name);
919
920 #endif
921
922 if (ngx_shmtx_create(&sp->mutex, (void *) &sp->lock, file) != NGX_OK) {
923 return NGX_ERROR;
924 }
925
926 ngx_slab_init(sp);
927
928 return NGX_OK;
929 }
930
931
922 #if !(NGX_WIN32) 932 #if !(NGX_WIN32)
923 933
924 ngx_int_t 934 ngx_int_t
925 ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log) 935 ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log)
926 { 936 {
1214 1224
1215 shm_zone->data = NULL; 1225 shm_zone->data = NULL;
1216 shm_zone->shm.log = cf->cycle->log; 1226 shm_zone->shm.log = cf->cycle->log;
1217 shm_zone->shm.size = size; 1227 shm_zone->shm.size = size;
1218 shm_zone->shm.name = *name; 1228 shm_zone->shm.name = *name;
1229 shm_zone->shm.exists = 0;
1219 shm_zone->init = NULL; 1230 shm_zone->init = NULL;
1220 shm_zone->tag = tag; 1231 shm_zone->tag = tag;
1221 1232
1222 return shm_zone; 1233 return shm_zone;
1223 } 1234 }