comparison src/os/unix/ngx_shared.c @ 358:0a03c921c81d

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