comparison src/os/win32/ngx_shmem.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 d5896f6608e8
children d52cf82d0d77
comparison
equal deleted inserted replaced
2719:9237cf8b400b 2720:b3b8c66bd520
9 9
10 10
11 ngx_int_t 11 ngx_int_t
12 ngx_shm_alloc(ngx_shm_t *shm) 12 ngx_shm_alloc(ngx_shm_t *shm)
13 { 13 {
14 u_char *name;
15
16 name = ngx_alloc(shm->name.len + 2 + sizeof(NGX_INT32_LEN), shm->log);
17 if (name == NULL) {
18 return NGX_ERROR;
19 }
20
21 ngx_sprintf(name, "%V_%s%Z", &shm->name, ngx_unique);
22
23 ngx_set_errno(0);
24
14 shm->handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 25 shm->handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
15 0, shm->size, (char *) shm->name.data); 26 0, shm->size, (char *) name);
16 27
17 if (shm->handle == NULL) { 28 if (shm->handle == NULL) {
18 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, 29 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
19 "CreateFileMapping(%uz, %s) failed", 30 "CreateFileMapping(%uz, %s) failed",
20 shm->size, shm->name.data); 31 shm->size, shm->name.data);
21 return NGX_ERROR; 32 goto failed;
33 }
34
35 if (ngx_errno == ERROR_ALREADY_EXISTS) {
36 shm->exists = 1;
22 } 37 }
23 38
24 shm->addr = MapViewOfFile(shm->handle, FILE_MAP_WRITE, 0, 0, 0); 39 shm->addr = MapViewOfFile(shm->handle, FILE_MAP_WRITE, 0, 0, 0);
25 40
26 if (shm->addr == NULL) { 41 if (shm->addr != NULL) {
27 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, 42 return NGX_OK;
28 "MapViewOfFile(%uz) failed", shm->size);
29
30 if (CloseHandle(shm->handle) == 0) {
31 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
32 "CloseHandle() failed");
33 }
34
35 return NGX_ERROR;
36 } 43 }
37 44
38 return NGX_OK; 45 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
46 "MapViewOfFile(%uz) failed", shm->size);
47
48 if (CloseHandle(shm->handle) == 0) {
49 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
50 "CloseHandle() failed");
51 }
52
53 failed:
54
55 ngx_free(name);
56
57 return NGX_ERROR;
39 } 58 }
40 59
41 60
42 void 61 void
43 ngx_shm_free(ngx_shm_t *shm) 62 ngx_shm_free(ngx_shm_t *shm)