@@ -120,18 +120,21 @@ static apr_table_t *collection_retrieve_ex(apr_sdbm_t *existing_dbm, modsec_rec
120120 key .dsize = col_key_len + 1 ;
121121
122122 if (existing_dbm == NULL ) {
123+ #ifdef GLOBAL_COLLECTION_LOCK
123124 rc = apr_global_mutex_lock (msr -> modsecurity -> dbm_lock );
124125 if (rc != APR_SUCCESS ) {
125126 msr_log (msr , 1 , "collection_retrieve_ex: Failed to lock proc mutex: %s" ,
126127 get_apr_error (msr -> mp , rc ));
127128 goto cleanup ;
128129 }
129-
130+ #endif
130131 rc = apr_sdbm_open (& dbm , dbm_filename , APR_READ | APR_SHARELOCK ,
131132 CREATEMODE , msr -> mp );
132133 if (rc != APR_SUCCESS ) {
133134 dbm = NULL ;
135+ #ifdef GLOBAL_COLLECTION_LOCK
134136 apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
137+ #endif
135138 goto cleanup ;
136139 }
137140 }
@@ -165,7 +168,9 @@ static apr_table_t *collection_retrieve_ex(apr_sdbm_t *existing_dbm, modsec_rec
165168 /* Close after "value" used from fetch or memory may be overwritten. */
166169 if (existing_dbm == NULL ) {
167170 apr_sdbm_close (dbm );
171+ #ifdef GLOBAL_COLLECTION_LOCK
168172 apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
173+ #endif
169174 dbm = NULL ;
170175 }
171176
@@ -212,19 +217,23 @@ static apr_table_t *collection_retrieve_ex(apr_sdbm_t *existing_dbm, modsec_rec
212217 */
213218 if (apr_table_get (col , "KEY" ) == NULL ) {
214219 if (existing_dbm == NULL ) {
220+ #ifdef GLOBAL_COLLECTION_LOCK
215221 rc = apr_global_mutex_lock (msr -> modsecurity -> dbm_lock );
216222 if (rc != APR_SUCCESS ) {
217223 msr_log (msr , 1 , "collection_retrieve_ex: Failed to lock proc mutex: %s" ,
218224 get_apr_error (msr -> mp , rc ));
219225 goto cleanup ;
220- }
226+ }
227+ #endif
221228 rc = apr_sdbm_open (& dbm , dbm_filename , APR_CREATE | APR_WRITE | APR_SHARELOCK ,
222229 CREATEMODE , msr -> mp );
223230 if (rc != APR_SUCCESS ) {
224231 msr_log (msr , 1 , "collection_retrieve_ex: Failed to access DBM file \"%s\": %s" ,
225232 log_escape (msr -> mp , dbm_filename ), get_apr_error (msr -> mp , rc ));
226233 dbm = NULL ;
234+ #ifdef GLOBAL_COLLECTION_LOCK
227235 apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
236+ #endif
228237 goto cleanup ;
229238 }
230239 }
@@ -247,7 +256,9 @@ static apr_table_t *collection_retrieve_ex(apr_sdbm_t *existing_dbm, modsec_rec
247256
248257 if (existing_dbm == NULL ) {
249258 apr_sdbm_close (dbm );
259+ #ifdef GLOBAL_COLLECTION_LOCK
250260 apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
261+ #endif
251262 dbm = NULL ;
252263 }
253264
@@ -310,16 +321,20 @@ static apr_table_t *collection_retrieve_ex(apr_sdbm_t *existing_dbm, modsec_rec
310321 log_escape (msr -> mp , col_name ), log_escape_ex (msr -> mp , col_key , col_key_len ));
311322
312323 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
314327 }
315-
328+
316329 return col ;
317330
318331cleanup :
319332
320333 if ((existing_dbm == NULL ) && dbm ) {
321334 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
323338 }
324339
325340 return NULL ;
@@ -384,13 +399,15 @@ int collection_store(modsec_rec *msr, apr_table_t *col) {
384399 log_escape (msr -> mp , dbm_filename ));
385400 }
386401
402+ #ifdef GLOBAL_COLLECTION_LOCK
387403 /* Need to lock to pull in the stored data again and apply deltas. */
388404 rc = apr_global_mutex_lock (msr -> modsecurity -> dbm_lock );
389405 if (rc != APR_SUCCESS ) {
390406 msr_log (msr , 1 , "collection_store: Failed to lock proc mutex: %s" ,
391407 get_apr_error (msr -> mp , rc ));
392408 goto error ;
393409 }
410+ #endif
394411
395412 /* Delete IS_NEW on store. */
396413 apr_table_unset (col , "IS_NEW" );
@@ -439,7 +456,7 @@ int collection_store(modsec_rec *msr, apr_table_t *col) {
439456 var -> value = apr_psprintf (msr -> mp , "%d" , counter + 1 );
440457 var -> value_len = strlen (var -> value );
441458 }
442-
459+
443460 /* ENH Make the expiration timestamp accessible in blob form so that
444461 * it is easier/faster to determine expiration without having to
445462 * convert back to table form
@@ -448,13 +465,24 @@ int collection_store(modsec_rec *msr, apr_table_t *col) {
448465 rc = apr_sdbm_open (& dbm , dbm_filename , APR_CREATE | APR_WRITE | APR_SHARELOCK ,
449466 CREATEMODE , msr -> mp );
450467 if (rc != APR_SUCCESS ) {
468+ #ifdef GLOBAL_COLLECTION_LOCK
451469 apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
470+ #endif
452471 msr_log (msr , 1 , "collection_store: Failed to access DBM file \"%s\": %s" , log_escape (msr -> mp , dbm_filename ),
453472 get_apr_error (msr -> mp , rc ));
454473 dbm = NULL ;
455474 goto error ;
456475 }
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
458486
459487 /* If there is an original value, then create a delta and
460488 * apply the delta to the current value */
@@ -519,8 +547,13 @@ int collection_store(modsec_rec *msr, apr_table_t *col) {
519547 blob = apr_pcalloc (msr -> mp , blob_size );
520548 if (blob == NULL ) {
521549 if (dbm != NULL ) {
550+ #ifdef GLOBAL_COLLECTION_LOCK
522551 apr_sdbm_close (dbm );
523552 apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
553+ #else
554+ apr_sdbm_unlock (dbm );
555+ apr_sdbm_close (dbm );
556+ #endif
524557 }
525558
526559 return -1 ;
@@ -577,16 +610,26 @@ int collection_store(modsec_rec *msr, apr_table_t *col) {
577610 msr_log (msr , 1 , "collection_store: Failed to write to DBM file \"%s\": %s" , dbm_filename ,
578611 get_apr_error (msr -> mp , rc ));
579612 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 );
580618 apr_sdbm_close (dbm );
581- apr_global_mutex_unlock ( msr -> modsecurity -> dbm_lock );
619+ #endif
582620 }
583621
584622 return -1 ;
585623 }
586624
625+ #ifdef GLOBAL_COLLECTION_LOCK
587626 apr_sdbm_close (dbm );
588627 apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
589-
628+ #else
629+ apr_sdbm_unlock (dbm );
630+ apr_sdbm_close (dbm );
631+ #endif
632+
590633 if (msr -> txcfg -> debuglog_level >= 4 ) {
591634 msr_log (msr , 4 , "collection_store: Persisted collection (name \"%s\", key \"%s\")." ,
592635 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) {
630673 log_escape (msr -> mp , dbm_filename ));
631674 }
632675
676+ #ifdef GLOBAL_COLLECTION_LOCK
633677 rc = apr_global_mutex_lock (msr -> modsecurity -> dbm_lock );
634678 if (rc != APR_SUCCESS ) {
635679 msr_log (msr , 1 , "collections_remove_stale: Failed to lock proc mutex: %s" ,
636680 get_apr_error (msr -> mp , rc ));
637681 goto error ;
638682 }
639-
683+ #endif
684+
640685 rc = apr_sdbm_open (& dbm , dbm_filename , APR_CREATE | APR_WRITE | APR_SHARELOCK ,
641686 CREATEMODE , msr -> mp );
642687 if (rc != APR_SUCCESS ) {
688+ #ifdef GLOBAL_COLLECTION_LOCK
643689 apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
690+ #endif
644691 msr_log (msr , 1 , "collections_remove_stale: Failed to access DBM file \"%s\": %s" , log_escape (msr -> mp , dbm_filename ),
645692 get_apr_error (msr -> mp , rc ));
646693 dbm = NULL ;
@@ -650,6 +697,15 @@ int collections_remove_stale(modsec_rec *msr, const char *col_name) {
650697 /* First get a list of all keys. */
651698 keys_arr = apr_array_make (msr -> mp , 256 , sizeof (char * ));
652699
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+
653709 /* No one can write to the file while doing this so
654710 * do it as fast as possible.
655711 */
@@ -661,6 +717,9 @@ int collections_remove_stale(modsec_rec *msr, const char *col_name) {
661717 }
662718 rc = apr_sdbm_nextkey (dbm , & key );
663719 }
720+ #ifndef GLOBAL_COLLECTION_LOCK
721+ apr_sdbm_unlock (dbm );
722+ #endif
664723
665724 if (msr -> txcfg -> debuglog_level >= 9 ) {
666725 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) {
729788 }
730789
731790 apr_sdbm_close (dbm );
791+ #ifdef GLOBAL_COLLECTION_LOCK
732792 apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
793+ #endif
733794 return 1 ;
734795
735796error :
736797
737798 if (dbm ) {
738799 apr_sdbm_close (dbm );
800+ #ifdef GLOBAL_COLLECTION_LOCK
739801 apr_global_mutex_unlock (msr -> modsecurity -> dbm_lock );
802+ #endif
740803 }
741804
742805 return -1 ;
0 commit comments