From d7a6488da81e6c48c435b8bedc8a38dbf87eb796 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Tue, 5 Sep 2023 10:07:59 +0200 Subject: [PATCH] lib:param: Set a memory context for the globals if not initialized yet Typically once the smb.conf starts to be loaded, loadparm_s3_init_globals() will be called and a memory context for strings on the static Globals will be created. But we might call lpcfg_set_cmdline() before we load the smb.conf file, so we (via a helper pointer) call loadparm_s3_init_globals() to get that initialisation done earlier, ensuring that all allocations on Globals is done on a memory context that we can later TALLOC_FREE() before exit(). Signed-off-by: Andreas Schneider Reviewed-by: Andrew Bartlett --- lib/param/loadparm.c | 14 +++++++++++++- lib/param/s3_param.h | 1 + source3/param/loadparm_ctx.c | 3 ++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/param/loadparm.c b/lib/param/loadparm.c index 673b913e6e5..6ef29ed9656 100644 --- a/lib/param/loadparm.c +++ b/lib/param/loadparm.c @@ -3201,7 +3201,16 @@ struct loadparm_context *loadparm_init_global(bool load_default) } /** - * Initialise the global parameter structure. + * @brief Initialise the global parameter structure. + * + * This function initialized the globals if needed. Make sure that + * gfree_loadparm() is called before the application exits. + * + * @param mem_ctx The talloc memory context to allocate lp_ctx on. + * + * @param s3_fns The loadparm helper functions to use + * + * @return An initialized lp_ctx pointer or NULL on error. */ struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx, const struct loadparm_s3_helpers *s3_fns) @@ -3214,6 +3223,9 @@ struct loadparm_context *loadparm_init_s3(TALLOC_CTX *mem_ctx, loadparm_context->globals = s3_fns->globals; loadparm_context->flags = s3_fns->flags; + /* Make sure globals are correctly initialized */ + loadparm_context->s3_fns->init_globals(loadparm_context, false); + return loadparm_context; } diff --git a/lib/param/s3_param.h b/lib/param/s3_param.h index f9ed7c3e2d7..60a2911095a 100644 --- a/lib/param/s3_param.h +++ b/lib/param/s3_param.h @@ -15,6 +15,7 @@ struct loadparm_s3_helpers const char *, char **); void (*init_ldap_debugging)(void); bool (*do_section)(const char *pszSectionName, void *userdata); + void (*init_globals)(struct loadparm_context *lp_ctx, bool reinit_globals); struct loadparm_global *globals; unsigned int *flags; }; diff --git a/source3/param/loadparm_ctx.c b/source3/param/loadparm_ctx.c index 43e431cec1c..2cc6dfd610c 100644 --- a/source3/param/loadparm_ctx.c +++ b/source3/param/loadparm_ctx.c @@ -1,4 +1,4 @@ -/* +/* Unix SMB/CIFS implementation. Parameter loading functions Copyright (C) Andrew Bartlett 2011 @@ -69,6 +69,7 @@ static struct loadparm_s3_helpers s3_fns = .lp_include = lp_include, .init_ldap_debugging = init_ldap_debugging, .do_section = lp_do_section, + .init_globals = loadparm_s3_init_globals, }; const struct loadparm_s3_helpers *loadparm_s3_helpers(void) -- 2.11.4.GIT