0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9
|
|
10
|
|
11 #if (HAVE_MAP_ANON)
|
|
12
|
|
13 void *ngx_create_shared_memory(size_t size, ngx_log_t *log)
|
|
14 {
|
|
15 void *p;
|
|
16
|
|
17 p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
|
|
18
|
|
19 if (p == MAP_FAILED) {
|
|
20 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
21 "mmap(MAP_ANON|MAP_SHARED, " SIZE_T_FMT ") failed",
|
|
22 size);
|
|
23 return NULL;
|
|
24 }
|
|
25
|
|
26 return p;
|
|
27 }
|
|
28
|
|
29 #elif (HAVE_MAP_DEVZERO)
|
|
30
|
|
31 void *ngx_create_shared_memory(size_t size, ngx_log_t *log)
|
|
32 {
|
|
33 void *p;
|
|
34 ngx_fd_t fd;
|
|
35
|
|
36 fd = open("/dev/zero", O_RDWR);
|
|
37
|
|
38 if (fd == -1) {
|
|
39 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
40 "open(/dev/zero) failed");
|
|
41 return NULL;
|
|
42 }
|
|
43
|
|
44 p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
|
|
45
|
|
46 if (p == MAP_FAILED) {
|
|
47 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
48 "mmap(/dev/zero, MAP_SHARED, " SIZE_T_FMT ") failed",
|
|
49 size);
|
|
50 p = NULL;
|
|
51 }
|
|
52
|
|
53 if (close(fd) == -1) {
|
|
54 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "close() failed");
|
|
55 }
|
|
56
|
|
57 return p;
|
|
58 }
|
|
59
|
|
60 #elif (HAVE_SYSVSHM)
|
|
61
|
|
62 #include <sys/ipc.h>
|
|
63 #include <sys/shm.h>
|
|
64
|
|
65
|
|
66 void *ngx_create_shared_memory(size_t size, ngx_log_t *log)
|
|
67 {
|
|
68 int id;
|
|
69 void *p;
|
|
70
|
|
71 id = shmget(IPC_PRIVATE, size, (SHM_R|SHM_W|IPC_CREAT));
|
|
72
|
|
73 if (id == -1) {
|
|
74 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
75 "shmget(" SIZE_T_FMT ") failed", size);
|
|
76 return NULL;
|
|
77 }
|
|
78
|
|
79 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, "shmget id: %d", id);
|
|
80
|
|
81 p = shmat(id, NULL, 0);
|
|
82
|
|
83 if (p == (void *) -1) {
|
|
84 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "shmat() failed");
|
|
85 p = NULL;
|
|
86 }
|
|
87
|
|
88 if (shmctl(id, IPC_RMID, NULL) == -1) {
|
|
89 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "shmctl(IPC_RMID) failed");
|
|
90 p = NULL;
|
|
91 }
|
|
92
|
|
93 return p;
|
|
94 }
|
|
95
|
|
96 #endif
|