@@ -101,6 +101,7 @@ static apr_table_t *collection_retrieve_ex(apr_sdbm_t *existing_dbm, modsec_rec
101
101
int expired = 0 ;
102
102
int i ;
103
103
104
+
104
105
if (msr -> txcfg -> data_dir == NULL ) {
105
106
msr_log (msr , 1 , "collection_retrieve_ex: Unable to retrieve collection (name \"%s\", key \"%s\"). Use "
106
107
"SecDataDir to define data directory first." , log_escape (msr -> mp , col_name ),
@@ -119,10 +120,18 @@ static apr_table_t *collection_retrieve_ex(apr_sdbm_t *existing_dbm, modsec_rec
119
120
key .dsize = col_key_len + 1 ;
120
121
121
122
if (existing_dbm == NULL ) {
123
+ rc = apr_global_mutex_lock (msr -> modsecurity -> dbm_lock );
124
+ if (rc != APR_SUCCESS ) {
125
+ msr_log (msr , 1 , "collection_retrieve_ex: Failed to lock proc mutex: %s" ,
126
+ get_apr_error (msr -> mp , rc ));
127
+ goto cleanup ;
128
+ }
129
+
122
130
rc = apr_sdbm_open (& dbm , dbm_filename , APR_READ | APR_SHARELOCK ,
123
131
CREATEMODE , msr -> mp );
124
132
if (rc != APR_SUCCESS ) {
125
133
dbm = NULL ;
134
+ apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
126
135
goto cleanup ;
127
136
}
128
137
}
@@ -156,6 +165,7 @@ static apr_table_t *collection_retrieve_ex(apr_sdbm_t *existing_dbm, modsec_rec
156
165
/* Close after "value" used from fetch or memory may be overwritten. */
157
166
if (existing_dbm == NULL ) {
158
167
apr_sdbm_close (dbm );
168
+ apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
159
169
dbm = NULL ;
160
170
}
161
171
@@ -202,12 +212,19 @@ static apr_table_t *collection_retrieve_ex(apr_sdbm_t *existing_dbm, modsec_rec
202
212
*/
203
213
if (apr_table_get (col , "KEY" ) == NULL ) {
204
214
if (existing_dbm == NULL ) {
215
+ rc = apr_global_mutex_lock (msr -> modsecurity -> dbm_lock );
216
+ if (rc != APR_SUCCESS ) {
217
+ msr_log (msr , 1 , "collection_retrieve_ex: Failed to lock proc mutex: %s" ,
218
+ get_apr_error (msr -> mp , rc ));
219
+ goto cleanup ;
220
+ }
205
221
rc = apr_sdbm_open (& dbm , dbm_filename , APR_CREATE | APR_WRITE | APR_SHARELOCK ,
206
222
CREATEMODE , msr -> mp );
207
223
if (rc != APR_SUCCESS ) {
208
224
msr_log (msr , 1 , "collection_retrieve_ex: Failed to access DBM file \"%s\": %s" ,
209
225
log_escape (msr -> mp , dbm_filename ), get_apr_error (msr -> mp , rc ));
210
226
dbm = NULL ;
227
+ apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
211
228
goto cleanup ;
212
229
}
213
230
}
@@ -230,6 +247,7 @@ static apr_table_t *collection_retrieve_ex(apr_sdbm_t *existing_dbm, modsec_rec
230
247
231
248
if (existing_dbm == NULL ) {
232
249
apr_sdbm_close (dbm );
250
+ apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
233
251
dbm = NULL ;
234
252
}
235
253
@@ -292,14 +310,16 @@ static apr_table_t *collection_retrieve_ex(apr_sdbm_t *existing_dbm, modsec_rec
292
310
log_escape (msr -> mp , col_name ), log_escape_ex (msr -> mp , col_key , col_key_len ));
293
311
294
312
apr_sdbm_close (dbm );
313
+ apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
295
314
}
296
-
315
+
297
316
return col ;
298
317
299
318
cleanup :
300
319
301
320
if ((existing_dbm == NULL ) && dbm ) {
302
321
apr_sdbm_close (dbm );
322
+ apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
303
323
}
304
324
305
325
return NULL ;
@@ -412,6 +432,14 @@ int collection_store(modsec_rec *msr, apr_table_t *col) {
412
432
var -> value_len = strlen (var -> value );
413
433
}
414
434
435
+ /* Need to lock to pull in the stored data again and apply deltas. */
436
+ rc = apr_global_mutex_lock (msr -> modsecurity -> dbm_lock );
437
+ if (rc != APR_SUCCESS ) {
438
+ msr_log (msr , 1 , "collection_store: Failed to lock proc mutex: %s" ,
439
+ get_apr_error (msr -> mp , rc ));
440
+ goto error ;
441
+ }
442
+
415
443
/* ENH Make the expiration timestamp accessible in blob form so that
416
444
* it is easier/faster to determine expiration without having to
417
445
* convert back to table form
@@ -420,19 +448,13 @@ int collection_store(modsec_rec *msr, apr_table_t *col) {
420
448
rc = apr_sdbm_open (& dbm , dbm_filename , APR_CREATE | APR_WRITE | APR_SHARELOCK ,
421
449
CREATEMODE , msr -> mp );
422
450
if (rc != APR_SUCCESS ) {
451
+ apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
423
452
msr_log (msr , 1 , "collection_store: Failed to access DBM file \"%s\": %s" , log_escape (msr -> mp , dbm_filename ),
424
453
get_apr_error (msr -> mp , rc ));
425
454
dbm = NULL ;
426
455
goto error ;
427
456
}
428
-
429
- /* Need to lock to pull in the stored data again and apply deltas. */
430
- rc = apr_sdbm_lock (dbm , APR_FLOCK_EXCLUSIVE );
431
- if (rc != APR_SUCCESS ) {
432
- msr_log (msr , 1 , "collection_store: Failed to exclusivly lock DBM file \"%s\": %s" , log_escape (msr -> mp , dbm_filename ),
433
- get_apr_error (msr -> mp , rc ));
434
- goto error ;
435
- }
457
+
436
458
437
459
/* If there is an original value, then create a delta and
438
460
* apply the delta to the current value */
@@ -497,8 +519,8 @@ int collection_store(modsec_rec *msr, apr_table_t *col) {
497
519
blob = apr_pcalloc (msr -> mp , blob_size );
498
520
if (blob == NULL ) {
499
521
if (dbm != NULL ) {
500
- apr_sdbm_unlock (dbm );
501
522
apr_sdbm_close (dbm );
523
+ apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
502
524
}
503
525
504
526
return -1 ;
@@ -555,16 +577,16 @@ int collection_store(modsec_rec *msr, apr_table_t *col) {
555
577
msr_log (msr , 1 , "collection_store: Failed to write to DBM file \"%s\": %s" , dbm_filename ,
556
578
get_apr_error (msr -> mp , rc ));
557
579
if (dbm != NULL ) {
558
- apr_sdbm_unlock (dbm );
559
580
apr_sdbm_close (dbm );
581
+ apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
560
582
}
561
583
562
584
return -1 ;
563
585
}
564
586
565
- apr_sdbm_unlock (dbm );
566
587
apr_sdbm_close (dbm );
567
-
588
+ apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
589
+
568
590
if (msr -> txcfg -> debuglog_level >= 4 ) {
569
591
msr_log (msr , 4 , "collection_store: Persisted collection (name \"%s\", key \"%s\")." ,
570
592
log_escape_ex (msr -> mp , var_name -> value , var_name -> value_len ),
@@ -608,9 +630,17 @@ int collections_remove_stale(modsec_rec *msr, const char *col_name) {
608
630
log_escape (msr -> mp , dbm_filename ));
609
631
}
610
632
633
+ rc = apr_global_mutex_lock (msr -> modsecurity -> dbm_lock );
634
+ if (rc != APR_SUCCESS ) {
635
+ msr_log (msr , 1 , "collections_remove_stale: Failed to lock proc mutex: %s" ,
636
+ get_apr_error (msr -> mp , rc ));
637
+ goto error ;
638
+ }
639
+
611
640
rc = apr_sdbm_open (& dbm , dbm_filename , APR_CREATE | APR_WRITE | APR_SHARELOCK ,
612
641
CREATEMODE , msr -> mp );
613
642
if (rc != APR_SUCCESS ) {
643
+ apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
614
644
msr_log (msr , 1 , "collections_remove_stale: Failed to access DBM file \"%s\": %s" , log_escape (msr -> mp , dbm_filename ),
615
645
get_apr_error (msr -> mp , rc ));
616
646
dbm = NULL ;
@@ -619,12 +649,6 @@ int collections_remove_stale(modsec_rec *msr, const char *col_name) {
619
649
620
650
/* First get a list of all keys. */
621
651
keys_arr = apr_array_make (msr -> mp , 256 , sizeof (char * ));
622
- rc = apr_sdbm_lock (dbm , APR_FLOCK_SHARED );
623
- if (rc != APR_SUCCESS ) {
624
- msr_log (msr , 1 , "collections_remove_stale: Failed to lock DBM file \"%s\": %s" , log_escape (msr -> mp , dbm_filename ),
625
- get_apr_error (msr -> mp , rc ));
626
- goto error ;
627
- }
628
652
629
653
/* No one can write to the file while doing this so
630
654
* do it as fast as possible.
@@ -637,7 +661,6 @@ int collections_remove_stale(modsec_rec *msr, const char *col_name) {
637
661
}
638
662
rc = apr_sdbm_nextkey (dbm , & key );
639
663
}
640
- apr_sdbm_unlock (dbm );
641
664
642
665
if (msr -> txcfg -> debuglog_level >= 9 ) {
643
666
msr_log (msr , 9 , "collections_remove_stale: Found %d record(s) in file \"%s\"." , keys_arr -> nelts ,
@@ -706,13 +729,14 @@ int collections_remove_stale(modsec_rec *msr, const char *col_name) {
706
729
}
707
730
708
731
apr_sdbm_close (dbm );
709
-
732
+ apr_global_mutex_unlock ( msr -> modsecurity -> dbm_lock );
710
733
return 1 ;
711
734
712
735
error :
713
736
714
737
if (dbm ) {
715
738
apr_sdbm_close (dbm );
739
+ apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
716
740
}
717
741
718
742
return -1 ;
0 commit comments