|
@@ -4,7 +4,7 @@ static const char *semnames[] = PYFCGI_SEMNAMES;
|
4
|
4
|
static const pyfcgi_ipc_flag_t semflg[PYFCGI_NSEM] = {
|
5
|
5
|
IPC_WSTATE, IPC_WREQS, IPC_SEMST};
|
6
|
6
|
|
7
|
|
-void pyfcgi_name_sems(pid_t master_pid)
|
|
7
|
+void pyfcgi_name_IPC(pid_t master_pid)
|
8
|
8
|
{
|
9
|
9
|
short i;
|
10
|
10
|
for(i=0; i<PYFCGI_NSEM; i++)
|
|
@@ -16,6 +16,13 @@ void pyfcgi_name_sems(pid_t master_pid)
|
16
|
16
|
exit(PYFCGI_FATAL);
|
17
|
17
|
}
|
18
|
18
|
}
|
|
19
|
+ if(snprintf(PyFCGI_conf.shm.name, NAME_MAX, PYFCGI_IPCNAME_FMT,
|
|
20
|
+ master_pid, "SHMstats") < 0)
|
|
21
|
+ {
|
|
22
|
+ perror("Error setting shm name");
|
|
23
|
+ exit(PYFCGI_FATAL);
|
|
24
|
+ }
|
|
25
|
+ PyFCGI_conf.shm.len = sizeof(pyfcgi_stats_shm_t);
|
19
|
26
|
}
|
20
|
27
|
|
21
|
28
|
int pyfcgi_IPC_create(pyfcgi_ipc_flag_t flag)
|
|
@@ -29,13 +36,13 @@ int pyfcgi_IPC_create(pyfcgi_ipc_flag_t flag)
|
29
|
36
|
if(flag & semflg[i])
|
30
|
37
|
{
|
31
|
38
|
flag ^= semflg[i];
|
32
|
|
- PyFCGI_SEM(i).sem = sem_open(PyFCGI_SEM(i).name, O_CREAT | O_EXCL,
|
33
|
|
- 0770, 0);
|
|
39
|
+ PyFCGI_SEM(i).sem = sem_open(PyFCGI_SEM(i).name,
|
|
40
|
+ O_CREAT | O_EXCL, 0770, 0);
|
34
|
41
|
if(PyFCGI_SEM(i).sem == SEM_FAILED)
|
35
|
42
|
{
|
36
|
43
|
err = errno;
|
37
|
44
|
pyfcgi_log(LOG_ALERT,
|
38
|
|
- "Unable to open semaphore %s(named '%s') : %s",
|
|
45
|
+ "Unable to create semaphore %s(named '%s') : %s",
|
39
|
46
|
semnames[i], PyFCGI_SEM(i).name,
|
40
|
47
|
strerror(err));
|
41
|
48
|
PyFCGI_SEM(i).sem = NULL;
|
|
@@ -50,9 +57,39 @@ int pyfcgi_IPC_create(pyfcgi_ipc_flag_t flag)
|
50
|
57
|
if(flag & IPC_SHMST)
|
51
|
58
|
{
|
52
|
59
|
flag ^= IPC_SHMST;
|
53
|
|
-dprintf(2, "SHM not yet implemented...\n");
|
54
|
|
-res = -1;
|
55
|
|
- //PyFCGI_conf.context.ipc_flag |= IPC_SHMST;
|
|
60
|
+ PyFCGI_conf.shm.fd = shm_open(PyFCGI_conf.shm.name,
|
|
61
|
+ O_RDWR | O_CREAT | O_EXCL, 0770);
|
|
62
|
+ if(PyFCGI_conf.shm.fd == -1)
|
|
63
|
+ {
|
|
64
|
+ err = errno;
|
|
65
|
+ pyfcgi_log(LOG_ALERT,
|
|
66
|
+ "Unable to create SHM '%s' : %s",
|
|
67
|
+ PyFCGI_conf.shm.name, strerror(errno));
|
|
68
|
+ PyFCGI_conf.shm.fd = 0;
|
|
69
|
+ return -1;
|
|
70
|
+ }
|
|
71
|
+ PyFCGI_conf.context.ipc_flag |= IPC_SHMST;
|
|
72
|
+ if(ftruncate(PyFCGI_conf.shm.fd,
|
|
73
|
+ sizeof(pyfcgi_stats_shm_t)) < 0)
|
|
74
|
+ {
|
|
75
|
+ pyfcgi_log(LOG_ALERT,
|
|
76
|
+ "Unable to truncate SHM to wanted size %ld : %s",
|
|
77
|
+ sizeof(pyfcgi_stats_shm_t),
|
|
78
|
+ strerror(errno));
|
|
79
|
+ pyfcgi_IPC_destroy(IPC_SHMST);
|
|
80
|
+ return -1;
|
|
81
|
+ }
|
|
82
|
+pyfcgi_log(LOG_INFO, "MMAP LENGHt : %ld", PyFCGI_conf.shm.len);
|
|
83
|
+ PyFCGI_conf.shm.ptr = mmap(NULL, PyFCGI_conf.shm.len,
|
|
84
|
+ PROT_READ | PROT_WRITE, MAP_SHARED, PyFCGI_conf.shm.fd,
|
|
85
|
+ 0);
|
|
86
|
+ if(PyFCGI_conf.shm.ptr == (void*)-1)
|
|
87
|
+ {
|
|
88
|
+ pyfcgi_log(LOG_ALERT,
|
|
89
|
+ "Unable to mmap SHM : %s", strerror(errno));
|
|
90
|
+ pyfcgi_IPC_destroy(IPC_SHMST);
|
|
91
|
+ return -1;
|
|
92
|
+ }
|
56
|
93
|
}
|
57
|
94
|
return res;
|
58
|
95
|
}
|
|
@@ -88,9 +125,27 @@ int pyfcgi_IPC_init(pyfcgi_ipc_flag_t flag)
|
88
|
125
|
if(flag & IPC_SHMST)
|
89
|
126
|
{
|
90
|
127
|
flag ^= IPC_SHMST;
|
91
|
|
-dprintf(2, "SHM not yet implemented...\n");
|
92
|
|
-res = -1;
|
93
|
|
- //PyFCGI_conf.context.ipc_flag |= IPC_SHMST;
|
|
128
|
+ PyFCGI_conf.shm.fd = shm_open(PyFCGI_conf.shm.name, O_RDONLY, 0);
|
|
129
|
+ if(PyFCGI_conf.shm.fd == -1)
|
|
130
|
+ {
|
|
131
|
+ err = errno;
|
|
132
|
+ pyfcgi_log(LOG_ALERT,
|
|
133
|
+ "Unable to open SHM '%s' : %s",
|
|
134
|
+ PyFCGI_conf.shm.name, strerror(errno));
|
|
135
|
+ PyFCGI_conf.shm.fd = 0;
|
|
136
|
+ return -1;
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ PyFCGI_conf.context.ipc_flag |= IPC_SHMST;
|
|
140
|
+ PyFCGI_conf.shm.ptr = mmap(NULL, PyFCGI_conf.shm.len,
|
|
141
|
+ PROT_READ, MAP_SHARED, PyFCGI_conf.shm.fd, 0);
|
|
142
|
+ if(PyFCGI_conf.shm.ptr == (void*)-1)
|
|
143
|
+ {
|
|
144
|
+ pyfcgi_log(LOG_ALERT,
|
|
145
|
+ "Unable to mmap SHM : %s", strerror(errno));
|
|
146
|
+ pyfcgi_IPC_destroy(IPC_SHMST);
|
|
147
|
+ return -1;
|
|
148
|
+ }
|
94
|
149
|
}
|
95
|
150
|
return res;
|
96
|
151
|
}
|
|
@@ -121,9 +176,20 @@ int pyfcgi_IPC_close()
|
121
|
176
|
}
|
122
|
177
|
if(flag & IPC_SHMST)
|
123
|
178
|
{
|
124
|
|
-dprintf(2, "SHM not yet implemented...\n");
|
125
|
|
-res = -1;
|
126
|
|
- //PyFCGI_conf.context.ipc_flag ^= IPC_SHMST;
|
|
179
|
+ if(PyFCGI_conf.shm.ptr &&
|
|
180
|
+ munmap(PyFCGI_conf.shm.ptr, PyFCGI_conf.shm.len) < 0)
|
|
181
|
+ {
|
|
182
|
+ pyfcgi_log(LOG_WARNING, "Unable to unmap SHM : %s",
|
|
183
|
+ strerror(errno));
|
|
184
|
+ }
|
|
185
|
+ if(close(PyFCGI_conf.shm.fd) < 0)
|
|
186
|
+ {
|
|
187
|
+ pyfcgi_log(LOG_WARNING, "Unable to close SHM : %s",
|
|
188
|
+ strerror(errno));
|
|
189
|
+ }
|
|
190
|
+ PyFCGI_conf.shm.fd = 0;
|
|
191
|
+ PyFCGI_conf.shm.ptr = NULL;
|
|
192
|
+ PyFCGI_conf.context.ipc_flag ^= IPC_SHMST;
|
127
|
193
|
}
|
128
|
194
|
|
129
|
195
|
return res;
|
|
@@ -153,9 +219,26 @@ int pyfcgi_IPC_destroy(pyfcgi_ipc_flag_t flag)
|
153
|
219
|
}
|
154
|
220
|
if(flag & IPC_SHMST)
|
155
|
221
|
{
|
156
|
|
-dprintf(2, "SHM not yet implemented...\n");
|
157
|
|
-res = -1;
|
158
|
|
- //PyFCGI_conf.context.ipc_flag ^= IPC_SHMST;
|
|
222
|
+
|
|
223
|
+ if(PyFCGI_conf.shm.ptr &&
|
|
224
|
+ munmap(PyFCGI_conf.shm.ptr, PyFCGI_conf.shm.len) < 0)
|
|
225
|
+ {
|
|
226
|
+ pyfcgi_log(LOG_WARNING, "Unable to unmap SHM : %s",
|
|
227
|
+ strerror(errno));
|
|
228
|
+ }
|
|
229
|
+ if(close(PyFCGI_conf.shm.fd) < 0)
|
|
230
|
+ {
|
|
231
|
+ pyfcgi_log(LOG_WARNING, "Unable to close SHM : %s",
|
|
232
|
+ strerror(errno));
|
|
233
|
+ }
|
|
234
|
+ if(shm_unlink(PyFCGI_conf.shm.name) < 0)
|
|
235
|
+ {
|
|
236
|
+ pyfcgi_log(LOG_WARNING, "Unabe to unlink SHM : %s",
|
|
237
|
+ strerror(errno));
|
|
238
|
+ }
|
|
239
|
+ PyFCGI_conf.shm.fd = 0;
|
|
240
|
+ PyFCGI_conf.shm.ptr = NULL;
|
|
241
|
+ PyFCGI_conf.context.ipc_flag ^= IPC_SHMST;
|
159
|
242
|
}
|
160
|
243
|
return res;
|
161
|
244
|
}
|