lib:param: Set a memory context for the globals if not initialized yet
authorAndreas Schneider <asn@samba.org>
Tue, 5 Sep 2023 08:07:59 +0000 (10:07 +0200)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 25 Oct 2023 22:23:37 +0000 (22:23 +0000)
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 <asn@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
lib/param/loadparm.c
lib/param/s3_param.h
source3/param/loadparm_ctx.c

index 673b913e6e5a383a471181d90415eedeba62bd0e..6ef29ed9656c44c1c6cc1ebbe1840c033381c303 100644 (file)
@@ -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;
 }
 
index f9ed7c3e2d7486e036c27c3c865fc68ef81d94f2..60a2911095a26f39b85ccb6a1894642141226972 100644 (file)
@@ -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;
 };
index 43e431cec1cf0fb03766380d801b0c1f4ade8e13..2cc6dfd610ce9a456e3f9100b383e04897bace03 100644 (file)
@@ -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)