]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib/torture: extend torture_context_init()
authorStefan Metzmacher <metze@samba.org>
Fri, 9 May 2025 08:41:52 +0000 (10:41 +0200)
committerAndreas Schneider <asn@cryptomilk.org>
Mon, 19 May 2025 09:11:29 +0000 (09:11 +0000)
Give it a dedicated talloc parent and avoid
talloc_references, the caller needs to take care
that the lifetime of the arguments is longer.

This makes sure any talloc destructors of torture_context
children run before torture->ev is destroyed.

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
lib/torture/torture.c
lib/torture/torture.h
source4/torture/smbtorture.c

index 41226ef75047ebc0de3a4cc5cedfafa318ca155a..14adf7c570da5f861234ff51d3e6e0237bb07261 100644 (file)
@@ -42,23 +42,39 @@ struct torture_results *torture_results_init(TALLOC_CTX *mem_ctx, const struct t
 /**
  * Initialize a torture context
  */
-struct torture_context *torture_context_init(struct tevent_context *event_ctx, 
-                                                                                        struct torture_results *results)
+struct torture_context *torture_context_init(TALLOC_CTX *mem_ctx,
+                                            struct tevent_context *event_ctx,
+                                            struct loadparm_context *lp_ctx,
+                                            struct torture_results *results,
+                                            char *outputdir_template)
 {
-       struct torture_context *torture = talloc_zero(event_ctx, 
-                                                     struct torture_context);
+       struct torture_context *torture = NULL;
 
-       if (torture == NULL)
+       SMB_ASSERT(event_ctx);
+       SMB_ASSERT(lp_ctx);
+       SMB_ASSERT(results);
+       SMB_ASSERT(outputdir_template);
+
+       torture = talloc_zero(mem_ctx, struct torture_context);
+       if (torture == NULL) {
                return NULL;
+       }
 
        torture->ev = event_ctx;
-       torture->results = talloc_reference(torture, results);
+       torture->lp_ctx = lp_ctx;
+       torture->results = results;
 
        /*
         * We start with an empty subunit prefix
         */
        torture_subunit_prefix_reset(torture, NULL);
 
+       torture->outputdir = mkdtemp(outputdir_template);
+       if (torture->outputdir == NULL) {
+               TALLOC_FREE(torture);
+               return NULL;
+       }
+
        return torture;
 }
 
index 1bea7bd2c34b8e43303fd012393b5138dbb102ae..63124e8392904bcbd85bc368cbcaa447afd7ccb7 100644 (file)
@@ -908,7 +908,11 @@ bool torture_suite_init_tcase(struct torture_suite *suite,
                              const char *name);
 int torture_suite_children_count(const struct torture_suite *suite);
 
-struct torture_context *torture_context_init(struct tevent_context *event_ctx, struct torture_results *results);
+struct torture_context *torture_context_init(TALLOC_CTX *mem_ctx,
+                                            struct tevent_context *event_ctx,
+                                            struct loadparm_context *lp_ctx,
+                                            struct torture_results *results,
+                                            char *outputdir_template);
 
 struct torture_results *torture_results_init(TALLOC_CTX *mem_ctx, const struct torture_ui_ops *ui_ops);
 
index e8cd368824ef409f7d9309803f0e652a217d811f..0a9d433567d23fbcb745622e8f87a3363140043d 100644 (file)
@@ -423,6 +423,7 @@ int main(int argc, const char *argv[])
              OPT_DANGEROUS,OPT_SMB_PORTS,OPT_ASYNC,OPT_NUMPROGS,
              OPT_EXTRA_USER,};
        TALLOC_CTX *mem_ctx = NULL;
+       struct tevent_context *ev = NULL;
        struct loadparm_context *lp_ctx = NULL;
        bool ok;
 
@@ -691,9 +692,21 @@ int main(int argc, const char *argv[])
        }
 
        results = torture_results_init(mem_ctx, ui_ops);
+       if (results == NULL) {
+               perror("torture_results_init() failed");
+               poptFreeContext(pc);
+               talloc_free(mem_ctx);
+               return 1;
+       }
+
+       ev = s4_event_context_init(mem_ctx);
+       if (ev == NULL) {
+               perror("s4_event_context_init() failed");
+               poptFreeContext(pc);
+               talloc_free(mem_ctx);
+               return 1;
+       }
 
-       torture = torture_context_init(s4_event_context_init(mem_ctx),
-                                      results);
        if (basedir != NULL) {
                if (basedir[0] != '/') {
                        fprintf(stderr, "Please specify an absolute path to --basedir\n");
@@ -701,16 +714,16 @@ int main(int argc, const char *argv[])
                        talloc_free(mem_ctx);
                        return 1;
                }
-               outputdir = talloc_asprintf(torture, "%s/smbtortureXXXXXX", basedir);
+               outputdir = talloc_asprintf(mem_ctx, "%s/smbtortureXXXXXX", basedir);
        } else {
-               char *pwd = talloc_size(torture, PATH_MAX);
+               char *pwd = talloc_size(mem_ctx, PATH_MAX);
                if (!getcwd(pwd, PATH_MAX)) {
                        fprintf(stderr, "Unable to determine current working directory\n");
                        poptFreeContext(pc);
                        talloc_free(mem_ctx);
                        return 1;
                }
-               outputdir = talloc_asprintf(torture, "%s/smbtortureXXXXXX", pwd);
+               outputdir = talloc_asprintf(mem_ctx, "%s/smbtortureXXXXXX", pwd);
        }
        if (!outputdir) {
                fprintf(stderr, "Could not allocate per-run output dir\n");
@@ -718,16 +731,15 @@ int main(int argc, const char *argv[])
                talloc_free(mem_ctx);
                return 1;
        }
-       torture->outputdir = mkdtemp(outputdir);
-       if (!torture->outputdir) {
-               perror("Failed to make temp output dir");
+
+       torture = torture_context_init(mem_ctx, ev, lp_ctx, results, outputdir);
+       if (torture == NULL) {
+               perror("torture_context_init() failed");
                poptFreeContext(pc);
                talloc_free(mem_ctx);
                return 1;
        }
 
-       torture->lp_ctx = lp_ctx;
-
        gensec_init();
 
        if (shell) {
@@ -764,10 +776,12 @@ int main(int argc, const char *argv[])
 
        if (torture->results->returncode && correct) {
                poptFreeContext(pc);
+               talloc_free(torture);
                talloc_free(mem_ctx);
                return(0);
        } else {
                poptFreeContext(pc);
+               talloc_free(torture);
                talloc_free(mem_ctx);
                return(1);
        }