annotate src/os/unix/ngx_shmem.c @ 660:d0f7a625f27c NGINX_1_1_14

nginx 1.1.14 *) Feature: multiple "limit_req" limits may be used simultaneously. *) Bugfix: in error handling while connecting to a backend. Thanks to Piotr Sikora. *) Bugfix: in AIO error handling on FreeBSD. *) Bugfix: in the OpenSSL library initialization. *) Bugfix: the "proxy_redirect" directives might not be correctly inherited. *) Bugfix: memory leak during reconfiguration if the "pcre_jit" directive was used.
author Igor Sysoev <http://sysoev.ru>
date Mon, 30 Jan 2012 00:00:00 +0400
parents af37b7cb6698
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
154
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
1
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
2 /*
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
3 * Copyright (C) Igor Sysoev
660
d0f7a625f27c nginx 1.1.14
Igor Sysoev <http://sysoev.ru>
parents: 188
diff changeset
4 * Copyright (C) Nginx, Inc.
154
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
5 */
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
6
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
7
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
8 #include <ngx_config.h>
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
9 #include <ngx_core.h>
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
10
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
11
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
12 #if (NGX_HAVE_MAP_ANON)
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
13
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
14 ngx_int_t
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
15 ngx_shm_alloc(ngx_shm_t *shm)
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
16 {
188
af37b7cb6698 nginx 0.3.41
Igor Sysoev <http://sysoev.ru>
parents: 154
diff changeset
17 shm->addr = (u_char *) mmap(NULL, shm->size,
af37b7cb6698 nginx 0.3.41
Igor Sysoev <http://sysoev.ru>
parents: 154
diff changeset
18 PROT_READ|PROT_WRITE,
af37b7cb6698 nginx 0.3.41
Igor Sysoev <http://sysoev.ru>
parents: 154
diff changeset
19 MAP_ANON|MAP_SHARED, -1, 0);
154
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
20
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
21 if (shm->addr == MAP_FAILED) {
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
22 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
23 "mmap(MAP_ANON|MAP_SHARED, %uz) failed", shm->size);
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
24 return NGX_ERROR;
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
25 }
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
26
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
27 return NGX_OK;
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
28 }
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
29
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
30
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
31 void
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
32 ngx_shm_free(ngx_shm_t *shm)
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
33 {
188
af37b7cb6698 nginx 0.3.41
Igor Sysoev <http://sysoev.ru>
parents: 154
diff changeset
34 if (munmap((void *) shm->addr, shm->size) == -1) {
154
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
35 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
36 "munmap(%p, %uz) failed", shm->addr, shm->size);
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
37 }
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
38 }
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
39
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
40 #elif (NGX_HAVE_MAP_DEVZERO)
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
41
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
42 ngx_int_t
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
43 ngx_shm_alloc(ngx_shm_t *shm)
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
44 {
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
45 ngx_fd_t fd;
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
46
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
47 fd = open("/dev/zero", O_RDWR);
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
48
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
49 if (fd == -1) {
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
50 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
51 "open(\"/dev/zero\") failed");
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
52 return NGX_ERROR;
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
53 }
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
54
188
af37b7cb6698 nginx 0.3.41
Igor Sysoev <http://sysoev.ru>
parents: 154
diff changeset
55 shm->addr = (u_char *) mmap(NULL, shm->size, PROT_READ|PROT_WRITE,
af37b7cb6698 nginx 0.3.41
Igor Sysoev <http://sysoev.ru>
parents: 154
diff changeset
56 MAP_SHARED, fd, 0);
154
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
57
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
58 if (shm->addr == MAP_FAILED) {
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
59 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
60 "mmap(/dev/zero, MAP_SHARED, %uz) failed", shm->size);
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
61 }
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
62
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
63 if (close(fd) == -1) {
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
64 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
65 "close(\"/dev/zero\") failed");
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
66 }
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
67
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
68 return (shm->addr == MAP_FAILED) ? NGX_ERROR : NGX_OK;
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
69 }
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
70
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
71
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
72 void
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
73 ngx_shm_free(ngx_shm_t *shm)
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
74 {
188
af37b7cb6698 nginx 0.3.41
Igor Sysoev <http://sysoev.ru>
parents: 154
diff changeset
75 if (munmap((void *) shm->addr, shm->size) == -1) {
154
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
76 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
77 "munmap(%p, %uz) failed", shm->addr, shm->size);
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
78 }
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
79 }
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
80
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
81 #elif (NGX_HAVE_SYSVSHM)
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
82
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
83 #include <sys/ipc.h>
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
84 #include <sys/shm.h>
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
85
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
86
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
87 ngx_int_t
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
88 ngx_shm_alloc(ngx_shm_t *shm)
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
89 {
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
90 int id;
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
91
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
92 id = shmget(IPC_PRIVATE, shm->size, (SHM_R|SHM_W|IPC_CREAT));
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
93
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
94 if (id == -1) {
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
95 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
96 "shmget(%uz) failed", shm->size);
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
97 return NGX_ERROR;
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
98 }
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
99
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
100 ngx_log_debug1(NGX_LOG_DEBUG_CORE, shm->log, 0, "shmget id: %d", id);
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
101
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
102 shm->addr = shmat(id, NULL, 0);
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
103
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
104 if (shm->addr == (void *) -1) {
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
105 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno, "shmat() failed");
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
106 }
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
107
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
108 if (shmctl(id, IPC_RMID, NULL) == -1) {
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
109 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
110 "shmctl(IPC_RMID) failed");
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
111 }
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
112
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
113 return (shm->addr == (void *) -1) ? NGX_ERROR : NGX_OK;
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
114 }
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
115
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
116
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
117 void
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
118 ngx_shm_free(ngx_shm_t *shm)
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
119 {
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
120 if (shmdt(shm->addr) == -1) {
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
121 ngx_log_error(NGX_LOG_ALERT, shm->log, ngx_errno,
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
122 "shmdt(%p) failed", shm->addr);
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
123 }
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
124 }
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
125
bb61aa162c6b nginx 0.3.24
Igor Sysoev <http://sysoev.ru>
parents:
diff changeset
126 #endif