Skip to content

Commit

Permalink
refactor: add acquire mutex function
Browse files Browse the repository at this point in the history
Signed-off-by: Felipe Zipitria <[email protected]>
  • Loading branch information
fzipi committed May 29, 2024
1 parent 19a7ae4 commit 117915a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
43 changes: 21 additions & 22 deletions apache2/modsecurity.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,19 +122,30 @@ msc_engine *modsecurity_create(apr_pool_t *mp, int processing_mode) {
return msce;
}

int acquire_global_lock(apr_global_mutex_t *lock, apr_pool_t *mp) {
apr_status_t rc;
apr_file_t *lock_name;
// use temp path template for lock files
char *path = apr_pstrcat(p, temp_dir, GLOBAL_LOCK_TEMPLATE, NULL);

rc = apr_file_mktemp(&lock_name, path, 0, p)
if (rc != APR_SUCCESS) {
return -1
}

rc = apr_global_mutex_create(&lock_name, lock_name->fname, APR_LOCK_DEFAULT, mp);
if (rc != APR_SUCCESS) {
return -1;
}
return APR_SUCCESS;
}
/**
* Initialise the modsecurity engine. This function must be invoked
* after configuration processing is complete as Apache needs to know the
* username it is running as.
*/
int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
apr_status_t rc;
apr_file_t *auditlog_lock_name;
apr_file_t *geo_lock_name;
apr_file_t *dbm_lock_name;

// use temp path template for lock files
char *path = apr_pstrcat(p, temp_dir, "/modsec-lock-tmp.XXXXXX", NULL);

msce->auditlog_lock = msce->geo_lock = NULL;
#ifdef GLOBAL_COLLECTION_LOCK
Expand All @@ -151,12 +162,8 @@ int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
#ifdef WITH_CURL
curl_global_init(CURL_GLOBAL_ALL);
#endif
/* Serial audit log mutext */
rc = apr_file_mktemp(&auditlog_lock_name, path, 0, p)
if (rc != APR_SUCCESS) {
return -1
}
rc = apr_global_mutex_create(&msce->auditlog_lock, auditlog_lock_name, APR_LOCK_DEFAULT, mp);
/* Serial audit log mutex */
rc = acquire_global_lock(&msce->auditlog_lock, mp);
if (rc != APR_SUCCESS) {
return -1;
}
Expand All @@ -175,11 +182,7 @@ int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
}
#endif /* SET_MUTEX_PERMS */

rc = apr_file_mktemp(&geo_lock_name, path, 0, p)
if (rc != APR_SUCCESS) {
return -1
}
rc = apr_global_mutex_create(&msce->geo_lock, geo_lock_name, APR_LOCK_DEFAULT, mp);
rc = acquire_global_lock(&msce->geo_lock, mp);
if (rc != APR_SUCCESS) {
return -1;
}
Expand All @@ -196,11 +199,7 @@ int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
#endif /* SET_MUTEX_PERMS */

#ifdef GLOBAL_COLLECTION_LOCK
rc = apr_file_mktemp(&dbm_lock_name, path, 0, p)
if (rc != APR_SUCCESS) {
return -1
}
rc = apr_global_mutex_create(&msce->dbm_lock, dbm_lock_name, APR_LOCK_DEFAULT, mp);
rc = acquire_global_lock(&msce->dbm_lock, mp);
if (rc != APR_SUCCESS) {
return -1;
}
Expand Down
6 changes: 1 addition & 5 deletions apache2/modsecurity.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,7 @@ typedef struct msc_parm msc_parm;

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

static char auditlog_lock_name[L_tmpnam];
static char geo_lock_name[L_tmpnam];
#ifdef GLOBAL_COLLECTION_LOCK
static char dbm_lock_name[L_tmpnam];
#endif
#define GLOBAL_LOCK_TEMPLATE "/modsec-lock-tmp.XXXXXX"

extern DSOLOCAL char *new_server_signature;
extern DSOLOCAL char *real_server_signature;
Expand Down

0 comments on commit 117915a

Please sign in to comment.