27
27
#include < stdlib.h>
28
28
#include < string.h>
29
29
30
-
30
+ # include < utility >
31
31
#include < fstream>
32
32
#include < string>
33
33
34
34
namespace modsecurity {
35
35
namespace utils {
36
36
37
37
38
- msc_file_handler_t * SharedFiles::find_handler (
38
+ std::pair<msc_file_handler *, FILE *> SharedFiles::find_handler (
39
39
const std::string &fileName) {
40
- for (const auto &i: m_handlers) {
40
+ for (const auto &i : m_handlers) {
41
41
if (i.first == fileName) {
42
42
return i.second ;
43
43
}
44
44
}
45
- return NULL ;
45
+ return std::pair<modsecurity::utils::msc_file_handler*,
46
+ _IO_FILE*>(NULL , NULL );
46
47
}
47
48
48
49
49
- msc_file_handler_t * SharedFiles::add_new_handler (
50
+ std::pair<msc_file_handler *, FILE *> SharedFiles::add_new_handler (
50
51
const std::string &fileName, std::string *error) {
51
52
int shm_id;
53
+ int ret;
52
54
key_t mem_key_structure;
53
55
msc_file_handler_t *new_debug_log;
56
+ struct shmid_ds shared_mem_info;
54
57
FILE *fp;
55
58
bool toBeCreated = true ;
56
59
@@ -67,8 +70,8 @@ msc_file_handler_t *SharedFiles::add_new_handler(
67
70
goto err_mem_key;
68
71
}
69
72
70
- shm_id = shmget (mem_key_structure, sizeof (msc_file_handler_t ) + fileName. size () + 1 ,
71
- IPC_CREAT | IPC_EXCL | 0666 );
73
+ shm_id = shmget (mem_key_structure, sizeof (msc_file_handler_t ) \
74
+ + fileName. size () + 1 , IPC_CREAT | IPC_EXCL | 0666 );
72
75
if (shm_id < 0 ) {
73
76
shm_id = shmget (mem_key_structure, sizeof (msc_file_handler_t )
74
77
+ fileName.size () + 1 , IPC_CREAT | 0666 );
@@ -80,6 +83,13 @@ msc_file_handler_t *SharedFiles::add_new_handler(
80
83
}
81
84
}
82
85
86
+ ret = shmctl (shm_id, IPC_STAT, &shared_mem_info);
87
+ if (ret < 0 ) {
88
+ error->assign (" Failed to get information on shared memory (1): " );
89
+ error->append (strerror (errno));
90
+ goto err_shmctl1;
91
+ }
92
+
83
93
new_debug_log = reinterpret_cast <msc_file_handler_t *>(
84
94
shmat (shm_id, NULL , 0 ));
85
95
if ((reinterpret_cast <char *>(new_debug_log)[0 ]) == -1 ) {
@@ -88,103 +98,143 @@ msc_file_handler_t *SharedFiles::add_new_handler(
88
98
goto err_shmat1;
89
99
}
90
100
101
+ if (toBeCreated == false && shared_mem_info.shm_nattch == 0 ) {
102
+ toBeCreated = true ;
103
+ }
104
+
91
105
if (toBeCreated) {
92
106
memset (new_debug_log, ' \0 ' , sizeof (msc_file_handler_t ));
93
107
pthread_mutex_init (&new_debug_log->lock , NULL );
94
- new_debug_log->fp = fp;
95
- new_debug_log->file_handler = fileno (new_debug_log->fp );
96
108
new_debug_log->shm_id_structure = shm_id;
97
109
memcpy (new_debug_log->file_name , fileName.c_str (), fileName.size ());
98
110
new_debug_log->file_name [fileName.size ()] = ' \0 ' ;
99
111
}
100
- m_handlers.push_back (std::make_pair (fileName, new_debug_log));
112
+ m_handlers.push_back (std::make_pair (fileName,
113
+ std::make_pair (new_debug_log, fp)));
101
114
102
- return new_debug_log;
115
+ return std::make_pair ( new_debug_log, fp) ;
103
116
err_shmget1:
117
+ err_shmctl1:
104
118
err_shmat1:
105
119
shmdt (new_debug_log);
106
120
err_mem_key:
107
121
fclose (fp);
108
122
err_fh:
109
- return NULL ;
123
+ return std::pair<modsecurity::utils::msc_file_handler*,
124
+ _IO_FILE*>(NULL , NULL );
110
125
}
111
126
112
127
113
128
bool SharedFiles::open (const std::string& fileName, std::string *error) {
114
- msc_file_handler_t *a = find_handler (fileName);
115
- if (a == NULL ) {
129
+ std::pair<msc_file_handler *, FILE *> a;
130
+
131
+ #if MODSEC_USE_GENERAL_LOCK
132
+ pthread_mutex_lock (m_generalLock);
133
+ #endif
134
+
135
+ a = find_handler (fileName);
136
+ if (a.first == NULL ) {
116
137
a = add_new_handler (fileName, error);
117
138
if (error->size () > 0 ) {
118
- return false ;
139
+ goto out ;
119
140
}
120
141
}
121
- if (a == NULL ) {
142
+ if (a. first == NULL ) {
122
143
error->assign (" Not able to open: " + fileName);
123
- return false ;
144
+ goto out ;
124
145
}
125
146
126
- a->using_it ++;
147
+ out:
148
+ #if MODSEC_USE_GENERAL_LOCK
149
+ pthread_mutex_unlock (m_generalLock);
150
+ #endif
127
151
128
152
return true ;
129
153
}
130
154
131
155
132
156
void SharedFiles::close (const std::string& fileName) {
133
- msc_file_handler_t *a;
134
- int j = 0 ;
157
+ std::pair<msc_file_handler *, FILE *> a;
158
+ /* int ret; */
159
+ /* int shm_id; */
160
+ /* struct shmid_ds shared_mem_info; */
161
+ /* int j = 0; */
162
+
163
+ #if MODSEC_USE_GENERAL_LOCK
164
+ pthread_mutex_lock (m_generalLock);
165
+ #endif
135
166
136
167
if (fileName.empty ()) {
137
- return ;
168
+ goto out ;
138
169
}
139
170
140
171
a = find_handler (fileName);
141
- if (a == NULL ) {
142
- return ;
172
+ if (a. first == NULL || a. second == NULL ) {
173
+ goto out ;
143
174
}
144
175
145
- a->using_it --;
146
-
147
- if (a->using_it == 0 ) {
148
- int shm_id1 = a->shm_id_structure ;
149
- msc_file_handler_t *p , *n;
150
- pthread_mutex_lock (&a->lock );
151
- fclose (a->fp );
152
- pthread_mutex_unlock (&a->lock );
153
- pthread_mutex_destroy (&a->lock );
154
- shmdt (a);
155
- shmctl (shm_id1, IPC_RMID, NULL );
156
- }
176
+ /* fclose(a.second); */
177
+ a.second = 0 ;
157
178
158
- for (const auto &i: m_handlers) {
179
+ /*
180
+ * Delete the file structure will be welcomed, but we cannot delay
181
+ * while the process is being killed.
182
+ *
183
+ for (std::pair<std::string,
184
+ std::pair<msc_file_handler *, FILE *>> i : m_handlers) {
159
185
if (i.first == fileName) {
160
186
j++;
161
187
}
162
188
}
163
189
164
- m_handlers.erase (m_handlers.begin () + j, m_handlers.begin () + j + 1 );
190
+ m_handlers.erase(m_handlers.begin()+j);
191
+ */
192
+
193
+ /* hmdt(a.second); */
194
+ shmctl (a.first ->shm_id_structure , IPC_RMID, NULL );
195
+
196
+ /*
197
+ *
198
+ * We could check to see how many process attached to the shared memory
199
+ * we have, prior to the deletion of the shared memory.
200
+ *
201
+ ret = shmctl(a.first->shm_id_structure, IPC_STAT, &shared_mem_info);
202
+ if (ret < 0) {
203
+ goto out;
204
+ }
205
+ ret = shared_mem_info.shm_nattch;
206
+ shm_id = a.first->shm_id_structure;
207
+ */
208
+
209
+ out:
210
+ #if MODSEC_USE_GENERAL_LOCK
211
+ pthread_mutex_unlock (m_generalLock);
212
+ #endif
213
+ return ;
165
214
}
166
215
167
216
168
217
bool SharedFiles::write (const std::string& fileName,
169
218
const std::string &msg, std::string *error) {
219
+ std::pair<msc_file_handler *, FILE *> a;
170
220
std::string lmsg = msg;
171
221
size_t wrote;
172
222
bool ret = true ;
173
223
174
- msc_file_handler_t * a = find_handler (fileName);
175
- if (a == NULL ) {
224
+ a = find_handler (fileName);
225
+ if (a. first == NULL ) {
176
226
error->assign (" file is not open: " + fileName);
177
227
return false ;
178
228
}
179
229
180
- pthread_mutex_lock (&a->lock );
230
+ pthread_mutex_lock (&a. first ->lock );
181
231
wrote = fwrite (reinterpret_cast <const char *>(lmsg.c_str ()), 1 ,
182
- lmsg.size (), a-> fp );
232
+ lmsg.size (), a. second );
183
233
if (wrote < msg.size ()) {
184
234
error->assign (" failed to write: " + fileName);
185
235
ret = false ;
186
236
}
187
- pthread_mutex_unlock (&a->lock );
237
+ pthread_mutex_unlock (&a. first ->lock );
188
238
189
239
return ret;
190
240
}
0 commit comments