@@ -122,19 +122,41 @@ msc_engine *modsecurity_create(apr_pool_t *mp, int processing_mode) {
122122 return msce ;
123123}
124124
125+ int acquire_global_lock (apr_global_mutex_t * lock , apr_pool_t * mp ) {
126+ apr_status_t rc ;
127+ apr_file_t * lock_name ;
128+ const char * temp_dir ;
129+ const char * filename ;
130+
131+ // get platform temp dir
132+ rc = apr_temp_dir_get (& temp_dir , mp );
133+ if (rc != APR_SUCCESS ) {
134+ return -1 ;
135+ }
136+
137+ // use temp path template for lock files
138+ char * path = apr_pstrcat (mp , temp_dir , GLOBAL_LOCK_TEMPLATE , NULL );
139+
140+ rc = apr_file_mktemp (& lock_name , path , 0 , mp );
141+ if (rc != APR_SUCCESS ) {
142+ return -1 ;
143+ }
144+ // below func always return APR_SUCCESS
145+ apr_file_name_get (& filename , lock_name );
146+
147+ rc = apr_global_mutex_create (& lock , filename , APR_LOCK_DEFAULT , mp );
148+ if (rc != APR_SUCCESS ) {
149+ return -1 ;
150+ }
151+ return APR_SUCCESS ;
152+ }
125153/**
126154 * Initialise the modsecurity engine. This function must be invoked
127155 * after configuration processing is complete as Apache needs to know the
128156 * username it is running as.
129157 */
130158int modsecurity_init (msc_engine * msce , apr_pool_t * mp ) {
131159 apr_status_t rc ;
132- apr_file_t * auditlog_lock_name ;
133- apr_file_t * geo_lock_name ;
134- apr_file_t * dbm_lock_name ;
135-
136- // use temp path template for lock files
137- char * path = apr_pstrcat (p , temp_dir , "/modsec-lock-tmp.XXXXXX" , NULL );
138160
139161 msce -> auditlog_lock = msce -> geo_lock = NULL ;
140162#ifdef GLOBAL_COLLECTION_LOCK
@@ -151,12 +173,8 @@ int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
151173#ifdef WITH_CURL
152174 curl_global_init (CURL_GLOBAL_ALL );
153175#endif
154- /* Serial audit log mutext */
155- rc = apr_file_mktemp (& auditlog_lock_name , path , 0 , p )
156- if (rc != APR_SUCCESS ) {
157- return -1
158- }
159- rc = apr_global_mutex_create (& msce -> auditlog_lock , auditlog_lock_name , APR_LOCK_DEFAULT , mp );
176+ /* Serial audit log mutex */
177+ rc = acquire_global_lock (msce -> auditlog_lock , mp );
160178 if (rc != APR_SUCCESS ) {
161179 return -1 ;
162180 }
@@ -175,11 +193,7 @@ int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
175193 }
176194#endif /* SET_MUTEX_PERMS */
177195
178- rc = apr_file_mktemp (& geo_lock_name , path , 0 , p )
179- if (rc != APR_SUCCESS ) {
180- return -1
181- }
182- rc = apr_global_mutex_create (& msce -> geo_lock , geo_lock_name , APR_LOCK_DEFAULT , mp );
196+ rc = acquire_global_lock (msce -> geo_lock , mp );
183197 if (rc != APR_SUCCESS ) {
184198 return -1 ;
185199 }
@@ -196,11 +210,7 @@ int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
196210#endif /* SET_MUTEX_PERMS */
197211
198212#ifdef GLOBAL_COLLECTION_LOCK
199- rc = apr_file_mktemp (& dbm_lock_name , path , 0 , p )
200- if (rc != APR_SUCCESS ) {
201- return -1
202- }
203- rc = apr_global_mutex_create (& msce -> dbm_lock , dbm_lock_name , APR_LOCK_DEFAULT , mp );
213+ rc = acquire_global_lock (& msce -> dbm_lock , mp );
204214 if (rc != APR_SUCCESS ) {
205215 return -1 ;
206216 }
0 commit comments