@@ -120,18 +120,21 @@ static apr_table_t *collection_retrieve_ex(apr_sdbm_t *existing_dbm, modsec_rec
120
120
key .dsize = col_key_len + 1 ;
121
121
122
122
if (existing_dbm == NULL ) {
123
+ #ifdef GLOBAL_COLLECTION_LOCK
123
124
rc = apr_global_mutex_lock (msr -> modsecurity -> dbm_lock );
124
125
if (rc != APR_SUCCESS ) {
125
126
msr_log (msr , 1 , "collection_retrieve_ex: Failed to lock proc mutex: %s" ,
126
127
get_apr_error (msr -> mp , rc ));
127
128
goto cleanup ;
128
129
}
129
-
130
+ #endif
130
131
rc = apr_sdbm_open (& dbm , dbm_filename , APR_READ | APR_SHARELOCK ,
131
132
CREATEMODE , msr -> mp );
132
133
if (rc != APR_SUCCESS ) {
133
134
dbm = NULL ;
135
+ #ifdef GLOBAL_COLLECTION_LOCK
134
136
apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
137
+ #endif
135
138
goto cleanup ;
136
139
}
137
140
}
@@ -165,7 +168,9 @@ static apr_table_t *collection_retrieve_ex(apr_sdbm_t *existing_dbm, modsec_rec
165
168
/* Close after "value" used from fetch or memory may be overwritten. */
166
169
if (existing_dbm == NULL ) {
167
170
apr_sdbm_close (dbm );
171
+ #ifdef GLOBAL_COLLECTION_LOCK
168
172
apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
173
+ #endif
169
174
dbm = NULL ;
170
175
}
171
176
@@ -212,19 +217,23 @@ static apr_table_t *collection_retrieve_ex(apr_sdbm_t *existing_dbm, modsec_rec
212
217
*/
213
218
if (apr_table_get (col , "KEY" ) == NULL ) {
214
219
if (existing_dbm == NULL ) {
220
+ #ifdef GLOBAL_COLLECTION_LOCK
215
221
rc = apr_global_mutex_lock (msr -> modsecurity -> dbm_lock );
216
222
if (rc != APR_SUCCESS ) {
217
223
msr_log (msr , 1 , "collection_retrieve_ex: Failed to lock proc mutex: %s" ,
218
224
get_apr_error (msr -> mp , rc ));
219
225
goto cleanup ;
220
- }
226
+ }
227
+ #endif
221
228
rc = apr_sdbm_open (& dbm , dbm_filename , APR_CREATE | APR_WRITE | APR_SHARELOCK ,
222
229
CREATEMODE , msr -> mp );
223
230
if (rc != APR_SUCCESS ) {
224
231
msr_log (msr , 1 , "collection_retrieve_ex: Failed to access DBM file \"%s\": %s" ,
225
232
log_escape (msr -> mp , dbm_filename ), get_apr_error (msr -> mp , rc ));
226
233
dbm = NULL ;
234
+ #ifdef GLOBAL_COLLECTION_LOCK
227
235
apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
236
+ #endif
228
237
goto cleanup ;
229
238
}
230
239
}
@@ -247,7 +256,9 @@ static apr_table_t *collection_retrieve_ex(apr_sdbm_t *existing_dbm, modsec_rec
247
256
248
257
if (existing_dbm == NULL ) {
249
258
apr_sdbm_close (dbm );
259
+ #ifdef GLOBAL_COLLECTION_LOCK
250
260
apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
261
+ #endif
251
262
dbm = NULL ;
252
263
}
253
264
@@ -310,16 +321,20 @@ static apr_table_t *collection_retrieve_ex(apr_sdbm_t *existing_dbm, modsec_rec
310
321
log_escape (msr -> mp , col_name ), log_escape_ex (msr -> mp , col_key , col_key_len ));
311
322
312
323
apr_sdbm_close (dbm );
313
- apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
324
+ #ifdef GLOBAL_COLLECTION_LOCK
325
+ apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
326
+ #endif
314
327
}
315
-
328
+
316
329
return col ;
317
330
318
331
cleanup :
319
332
320
333
if ((existing_dbm == NULL ) && dbm ) {
321
334
apr_sdbm_close (dbm );
322
- apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
335
+ #ifdef GLOBAL_COLLECTION_LOCK
336
+ apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
337
+ #endif
323
338
}
324
339
325
340
return NULL ;
@@ -384,13 +399,15 @@ int collection_store(modsec_rec *msr, apr_table_t *col) {
384
399
log_escape (msr -> mp , dbm_filename ));
385
400
}
386
401
402
+ #ifdef GLOBAL_COLLECTION_LOCK
387
403
/* Need to lock to pull in the stored data again and apply deltas. */
388
404
rc = apr_global_mutex_lock (msr -> modsecurity -> dbm_lock );
389
405
if (rc != APR_SUCCESS ) {
390
406
msr_log (msr , 1 , "collection_store: Failed to lock proc mutex: %s" ,
391
407
get_apr_error (msr -> mp , rc ));
392
408
goto error ;
393
409
}
410
+ #endif
394
411
395
412
/* Delete IS_NEW on store. */
396
413
apr_table_unset (col , "IS_NEW" );
@@ -439,7 +456,7 @@ int collection_store(modsec_rec *msr, apr_table_t *col) {
439
456
var -> value = apr_psprintf (msr -> mp , "%d" , counter + 1 );
440
457
var -> value_len = strlen (var -> value );
441
458
}
442
-
459
+
443
460
/* ENH Make the expiration timestamp accessible in blob form so that
444
461
* it is easier/faster to determine expiration without having to
445
462
* convert back to table form
@@ -448,13 +465,24 @@ int collection_store(modsec_rec *msr, apr_table_t *col) {
448
465
rc = apr_sdbm_open (& dbm , dbm_filename , APR_CREATE | APR_WRITE | APR_SHARELOCK ,
449
466
CREATEMODE , msr -> mp );
450
467
if (rc != APR_SUCCESS ) {
468
+ #ifdef GLOBAL_COLLECTION_LOCK
451
469
apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
470
+ #endif
452
471
msr_log (msr , 1 , "collection_store: Failed to access DBM file \"%s\": %s" , log_escape (msr -> mp , dbm_filename ),
453
472
get_apr_error (msr -> mp , rc ));
454
473
dbm = NULL ;
455
474
goto error ;
456
475
}
457
-
476
+
477
+ #ifndef GLOBAL_COLLECTION_LOCK
478
+ /* Need to lock to pull in the stored data again and apply deltas. */
479
+ rc = apr_sdbm_lock (dbm , APR_FLOCK_EXCLUSIVE );
480
+ if (rc != APR_SUCCESS ) {
481
+ msr_log (msr , 1 , "collection_store: Failed to exclusivly lock DBM file \"%s\": %s" , log_escape (msr -> mp , dbm_filename ),
482
+ get_apr_error (msr -> mp , rc ));
483
+ goto error ;
484
+ }
485
+ #endif
458
486
459
487
/* If there is an original value, then create a delta and
460
488
* apply the delta to the current value */
@@ -519,8 +547,13 @@ int collection_store(modsec_rec *msr, apr_table_t *col) {
519
547
blob = apr_pcalloc (msr -> mp , blob_size );
520
548
if (blob == NULL ) {
521
549
if (dbm != NULL ) {
550
+ #ifdef GLOBAL_COLLECTION_LOCK
522
551
apr_sdbm_close (dbm );
523
552
apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
553
+ #else
554
+ apr_sdbm_unlock (dbm );
555
+ apr_sdbm_close (dbm );
556
+ #endif
524
557
}
525
558
526
559
return -1 ;
@@ -577,16 +610,26 @@ int collection_store(modsec_rec *msr, apr_table_t *col) {
577
610
msr_log (msr , 1 , "collection_store: Failed to write to DBM file \"%s\": %s" , dbm_filename ,
578
611
get_apr_error (msr -> mp , rc ));
579
612
if (dbm != NULL ) {
613
+ #ifdef GLOBAL_COLLECTION_LOCK
614
+ apr_sdbm_close (dbm );
615
+ apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
616
+ #else
617
+ apr_sdbm_unlock (dbm );
580
618
apr_sdbm_close (dbm );
581
- apr_global_mutex_unlock ( msr -> modsecurity -> dbm_lock );
619
+ #endif
582
620
}
583
621
584
622
return -1 ;
585
623
}
586
624
625
+ #ifdef GLOBAL_COLLECTION_LOCK
587
626
apr_sdbm_close (dbm );
588
627
apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
589
-
628
+ #else
629
+ apr_sdbm_unlock (dbm );
630
+ apr_sdbm_close (dbm );
631
+ #endif
632
+
590
633
if (msr -> txcfg -> debuglog_level >= 4 ) {
591
634
msr_log (msr , 4 , "collection_store: Persisted collection (name \"%s\", key \"%s\")." ,
592
635
log_escape_ex (msr -> mp , var_name -> value , var_name -> value_len ),
@@ -630,17 +673,21 @@ int collections_remove_stale(modsec_rec *msr, const char *col_name) {
630
673
log_escape (msr -> mp , dbm_filename ));
631
674
}
632
675
676
+ #ifdef GLOBAL_COLLECTION_LOCK
633
677
rc = apr_global_mutex_lock (msr -> modsecurity -> dbm_lock );
634
678
if (rc != APR_SUCCESS ) {
635
679
msr_log (msr , 1 , "collections_remove_stale: Failed to lock proc mutex: %s" ,
636
680
get_apr_error (msr -> mp , rc ));
637
681
goto error ;
638
682
}
639
-
683
+ #endif
684
+
640
685
rc = apr_sdbm_open (& dbm , dbm_filename , APR_CREATE | APR_WRITE | APR_SHARELOCK ,
641
686
CREATEMODE , msr -> mp );
642
687
if (rc != APR_SUCCESS ) {
688
+ #ifdef GLOBAL_COLLECTION_LOCK
643
689
apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
690
+ #endif
644
691
msr_log (msr , 1 , "collections_remove_stale: Failed to access DBM file \"%s\": %s" , log_escape (msr -> mp , dbm_filename ),
645
692
get_apr_error (msr -> mp , rc ));
646
693
dbm = NULL ;
@@ -650,6 +697,15 @@ int collections_remove_stale(modsec_rec *msr, const char *col_name) {
650
697
/* First get a list of all keys. */
651
698
keys_arr = apr_array_make (msr -> mp , 256 , sizeof (char * ));
652
699
700
+ #ifndef GLOBAL_COLLECTION_LOCK
701
+ rc = apr_sdbm_lock (dbm , APR_FLOCK_SHARED );
702
+ if (rc != APR_SUCCESS ) {
703
+ msr_log (msr , 1 , "collections_remove_stale: Failed to lock DBM file \"%s\": %s" , log_escape (msr -> mp , dbm_filename ),
704
+ get_apr_error (msr -> mp , rc ));
705
+ goto error ;
706
+ }
707
+ #endif
708
+
653
709
/* No one can write to the file while doing this so
654
710
* do it as fast as possible.
655
711
*/
@@ -661,6 +717,9 @@ int collections_remove_stale(modsec_rec *msr, const char *col_name) {
661
717
}
662
718
rc = apr_sdbm_nextkey (dbm , & key );
663
719
}
720
+ #ifndef GLOBAL_COLLECTION_LOCK
721
+ apr_sdbm_unlock (dbm );
722
+ #endif
664
723
665
724
if (msr -> txcfg -> debuglog_level >= 9 ) {
666
725
msr_log (msr , 9 , "collections_remove_stale: Found %d record(s) in file \"%s\"." , keys_arr -> nelts ,
@@ -729,14 +788,18 @@ int collections_remove_stale(modsec_rec *msr, const char *col_name) {
729
788
}
730
789
731
790
apr_sdbm_close (dbm );
791
+ #ifdef GLOBAL_COLLECTION_LOCK
732
792
apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
793
+ #endif
733
794
return 1 ;
734
795
735
796
error :
736
797
737
798
if (dbm ) {
738
799
apr_sdbm_close (dbm );
800
+ #ifdef GLOBAL_COLLECTION_LOCK
739
801
apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
802
+ #endif
740
803
}
741
804
742
805
return -1 ;
0 commit comments