Skip to content

Commit 117915a

Browse files
committed
refactor: add acquire mutex function
Signed-off-by: Felipe Zipitria <[email protected]>
1 parent 19a7ae4 commit 117915a

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

apache2/modsecurity.c

+21-22
Original file line numberDiff line numberDiff line change
@@ -122,19 +122,30 @@ 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+
// use temp path template for lock files
129+
char *path = apr_pstrcat(p, temp_dir, GLOBAL_LOCK_TEMPLATE, NULL);
130+
131+
rc = apr_file_mktemp(&lock_name, path, 0, p)
132+
if (rc != APR_SUCCESS) {
133+
return -1
134+
}
135+
136+
rc = apr_global_mutex_create(&lock_name, lock_name->fname, APR_LOCK_DEFAULT, mp);
137+
if (rc != APR_SUCCESS) {
138+
return -1;
139+
}
140+
return APR_SUCCESS;
141+
}
125142
/**
126143
* Initialise the modsecurity engine. This function must be invoked
127144
* after configuration processing is complete as Apache needs to know the
128145
* username it is running as.
129146
*/
130147
int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
131148
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);
138149

139150
msce->auditlog_lock = msce->geo_lock = NULL;
140151
#ifdef GLOBAL_COLLECTION_LOCK
@@ -151,12 +162,8 @@ int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
151162
#ifdef WITH_CURL
152163
curl_global_init(CURL_GLOBAL_ALL);
153164
#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);
165+
/* Serial audit log mutex */
166+
rc = acquire_global_lock(&msce->auditlog_lock, mp);
160167
if (rc != APR_SUCCESS) {
161168
return -1;
162169
}
@@ -175,11 +182,7 @@ int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
175182
}
176183
#endif /* SET_MUTEX_PERMS */
177184

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);
185+
rc = acquire_global_lock(&msce->geo_lock, mp);
183186
if (rc != APR_SUCCESS) {
184187
return -1;
185188
}
@@ -196,11 +199,7 @@ int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
196199
#endif /* SET_MUTEX_PERMS */
197200

198201
#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);
202+
rc = acquire_global_lock(&msce->dbm_lock, mp);
204203
if (rc != APR_SUCCESS) {
205204
return -1;
206205
}

apache2/modsecurity.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,7 @@ typedef struct msc_parm msc_parm;
135135

136136
#define FATAL_ERROR "ModSecurity: Fatal error (memory allocation or unexpected internal error)!"
137137

138-
static char auditlog_lock_name[L_tmpnam];
139-
static char geo_lock_name[L_tmpnam];
140-
#ifdef GLOBAL_COLLECTION_LOCK
141-
static char dbm_lock_name[L_tmpnam];
142-
#endif
138+
#define GLOBAL_LOCK_TEMPLATE "/modsec-lock-tmp.XXXXXX"
143139

144140
extern DSOLOCAL char *new_server_signature;
145141
extern DSOLOCAL char *real_server_signature;

0 commit comments

Comments
 (0)