Skip to content

Commit f040dbc

Browse files
committed
code: refactor/rename cleanup handling
Signed-off-by: Hans Zandbelt <[email protected]>
1 parent b2bd435 commit f040dbc

File tree

4 files changed

+36
-41
lines changed

4 files changed

+36
-41
lines changed

src/cache/common.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,20 +178,25 @@ static apr_byte_t oidc_cache_mutex_global_create(apr_pool_t *pool, server_rec *s
178178
* initialize a server- or process-wide mutex
179179
*/
180180
apr_byte_t oidc_cache_mutex_post_config(apr_pool_t *pool, server_rec *s, oidc_cache_mutex_t *m, const char *type) {
181-
181+
apr_byte_t rc = TRUE;
182182
apr_status_t rv = APR_SUCCESS;
183183

184-
if (m->is_global)
185-
return oidc_cache_mutex_global_create(pool, s, m, type);
184+
if (m->is_global) {
185+
rc = oidc_cache_mutex_global_create(pool, s, m, type);
186+
goto end;
187+
}
186188

187189
// NB: see note above at apr_global_mutex_create on the use of s->process->pool
188190
rv = apr_thread_mutex_create(&m->tmutex, APR_THREAD_MUTEX_DEFAULT, s->process->pool);
189191
if (rv != APR_SUCCESS) {
190192
oidc_serror(s, "apr_thread_mutex_create failed: %s (%d)", oidc_cache_status2str(pool, rv), rv);
191-
return FALSE;
193+
rc = FALSE;
194+
goto end;
192195
}
193196

194-
return TRUE;
197+
end:
198+
199+
return rc;
195200
}
196201

197202
/*

src/cfg/cfg.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -693,14 +693,10 @@ typedef struct oidc_cfg_cleanup_ctx_t {
693693
/*
694694
* destroy a server config record and its members
695695
*/
696-
static apr_status_t oidc_cfg_server_destroy(void *data) {
697-
oidc_cfg_cleanup_ctx_t *ctx = (oidc_cfg_cleanup_ctx_t *)data;
698-
oidc_cfg_t *cfg = ctx->cfg;
699-
if (cfg->cache.impl && cfg->cache.impl->destroy) {
700-
cfg->cache.impl->destroy(ctx->pool, ctx->svr);
701-
cfg->cache.impl->destroy = NULL;
702-
cfg->cache.impl = NULL;
703-
}
696+
apr_byte_t oidc_cfg_server_destroy(apr_pool_t *pool, server_rec *s, oidc_cfg_t *cfg) {
697+
if ((cfg->cache.impl) && (cfg->cache.impl->destroy))
698+
cfg->cache.impl->destroy(pool, s);
699+
cfg->cache.impl = NULL;
704700
oidc_cfg_provider_destroy(cfg->provider);
705701
cfg->provider = NULL;
706702
oidc_cfg_oauth_destroy(cfg->oauth);
@@ -709,18 +705,25 @@ static apr_status_t oidc_cfg_server_destroy(void *data) {
709705
cfg->public_keys = NULL;
710706
oidc_jwk_list_destroy(cfg->private_keys);
711707
cfg->private_keys = NULL;
712-
return APR_SUCCESS;
708+
return TRUE;
709+
}
710+
711+
static apr_status_t oidc_cfg_server_cleanup(void *data) {
712+
oidc_cfg_cleanup_ctx_t *ctx = (oidc_cfg_cleanup_ctx_t *)data;
713+
oidc_cfg_t *cfg = ctx->cfg;
714+
return oidc_cfg_server_destroy(ctx->pool, ctx->svr, cfg) ? APR_SUCCESS : APR_EGENERAL;
713715
}
714716

715-
static oidc_cfg_t *oidc_cfg_server_alloc(apr_pool_t *pool, server_rec *svr) {
717+
static oidc_cfg_t *oidc_cfg_server_alloc(apr_pool_t *pool, server_rec *s) {
716718
oidc_cfg_t *c = apr_pcalloc(pool, sizeof(oidc_cfg_t));
717719
oidc_cfg_cleanup_ctx_t *ctx = apr_pcalloc(pool, sizeof(oidc_cfg_cleanup_ctx_t));
718720
ctx->cfg = c;
719-
// pool used at descrution time
721+
// pconf pool used at destruction time
720722
ctx->pool = pool;
721-
ctx->svr = svr;
722-
// need to register a cleanup handler to the config pool to handle graceful restarts without memory leaks
723-
apr_pool_cleanup_register(pool, ctx, oidc_cfg_server_destroy, oidc_cfg_server_destroy);
723+
ctx->svr = s;
724+
// need to register a cleanup handler to the config pool to handle graceful restarts without memory increasing
725+
// memory consumption
726+
apr_pool_cleanup_register(pool, ctx, oidc_cfg_server_cleanup, apr_pool_cleanup_null);
724727
return c;
725728
}
726729

@@ -1040,7 +1043,7 @@ void oidc_cfg_child_init(apr_pool_t *pool, oidc_cfg_t *cfg, server_rec *s) {
10401043
}
10411044
}
10421045

1043-
void oidc_cfg_cleanup_child(oidc_cfg_t *cfg, server_rec *s) {
1046+
void oidc_cfg_process_cleanup(oidc_cfg_t *cfg, server_rec *s) {
10441047
if (_oidc_refresh_mutex != NULL) {
10451048
if (oidc_cache_mutex_destroy(s, _oidc_refresh_mutex) != TRUE) {
10461049
oidc_serror(s, "oidc_cache_mutex_destroy on refresh mutex failed");

src/cfg/cfg.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,12 @@ int oidc_cfg_merged_get(oidc_cfg_t *cfg);
188188

189189
void oidc_pre_config_init();
190190

191-
void *oidc_cfg_server_create(apr_pool_t *pool, server_rec *svr);
191+
void *oidc_cfg_server_create(apr_pool_t *pool, server_rec *s);
192192
void *oidc_cfg_server_merge(apr_pool_t *pool, void *BASE, void *ADD);
193+
apr_byte_t oidc_cfg_server_destroy(apr_pool_t *pool, server_rec *s, oidc_cfg_t *cfg);
193194
int oidc_cfg_post_config(apr_pool_t *pool, oidc_cfg_t *cfg, server_rec *s);
194195
void oidc_cfg_child_init(apr_pool_t *pool, oidc_cfg_t *cfg, server_rec *s);
195-
void oidc_cfg_cleanup_child(oidc_cfg_t *cfg, server_rec *s);
196+
void oidc_cfg_process_cleanup(oidc_cfg_t *cfg, server_rec *s);
196197
const char *oidc_cfg_string_list_add(apr_pool_t *pool, apr_array_header_t **list, const char *arg);
197198
const char *oidc_cfg_endpoint_auth_set(apr_pool_t *pool, oidc_cfg_t *cfg, const char *arg, char **auth, char **alg);
198199

src/mod_auth_openidc.c

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,26 +1648,17 @@ static void oidc_ssl_id_callback(CRYPTO_THREADID *id) {
16481648
#endif /* defined(OPENSSL_THREADS) && APR_HAS_THREADS */
16491649

16501650
/*
1651-
* cleanup resources allocated in a child process
1651+
* cleanup resources allocated in a process
16521652
*/
1653-
static apr_status_t oidc_cleanup_child(void *data) {
1653+
static apr_status_t oidc_process_cleanup(void *data) {
1654+
16541655
server_rec *sp = (server_rec *)data;
16551656
while (sp != NULL) {
16561657
oidc_cfg_t *cfg = (oidc_cfg_t *)ap_get_module_config(sp->module_config, &auth_openidc_module);
1657-
oidc_cfg_cleanup_child(cfg, sp);
1658+
oidc_cfg_process_cleanup(cfg, sp);
16581659
sp = sp->next;
16591660
}
16601661

1661-
return APR_SUCCESS;
1662-
}
1663-
1664-
/*
1665-
* cleanup resources allocated in a parent process
1666-
*/
1667-
static apr_status_t oidc_cleanup_parent(void *data) {
1668-
1669-
oidc_cleanup_child(data);
1670-
16711662
#if ((OPENSSL_VERSION_NUMBER < 0x10100000) && defined(OPENSSL_THREADS) && APR_HAS_THREADS)
16721663
if (CRYPTO_get_locking_callback() == oidc_ssl_locking_callback)
16731664
CRYPTO_set_locking_callback(NULL);
@@ -1754,7 +1745,7 @@ static int oidc_post_config(apr_pool_t *pool, apr_pool_t *p1, apr_pool_t *p2, se
17541745

17551746
#endif /* (OPENSSL_VERSION_NUMBER < 0x10100000) && defined (OPENSSL_THREADS) && APR_HAS_THREADS */
17561747

1757-
apr_pool_cleanup_register(pool, s, oidc_cleanup_parent, apr_pool_cleanup_null);
1748+
apr_pool_cleanup_register(pool, s, oidc_process_cleanup, apr_pool_cleanup_null);
17581749

17591750
server_rec *sp = s;
17601751
while (sp != NULL) {
@@ -1822,11 +1813,6 @@ static void oidc_child_init(apr_pool_t *p, server_rec *s) {
18221813
oidc_cfg_child_init(p, cfg, sp);
18231814
sp = sp->next;
18241815
}
1825-
/*
1826-
* NB: don't pass oidc_cleanup_child as the child cleanup routine parameter
1827-
* because that does not actually get called upon child cleanup...
1828-
*/
1829-
apr_pool_cleanup_register(p, s, oidc_cleanup_child, apr_pool_cleanup_null);
18301816
}
18311817

18321818
static const char oidcFilterName[] = "oidc_filter_in_filter";

0 commit comments

Comments
 (0)