tools subcmd: Add a no exec function call option
authorIan Rogers <irogers@google.com>
Wed, 21 Feb 2024 03:41:51 +0000 (19:41 -0800)
committerNamhyung Kim <namhyung@kernel.org>
Thu, 22 Feb 2024 17:12:25 +0000 (09:12 -0800)
Tools like perf fork tests in case they crash, but they don't want to
exec a full binary. Add an option to call a function rather than do an
exec. The child process exits with the result of the function call and
is passed the struct of the run_command, things like container_of can
then allow the child process function to determine additional
arguments.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: James Clark <james.clark@arm.com>
Cc: Justin Stitt <justinstitt@google.com>
Cc: Bill Wendling <morbo@google.com>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Yang Jihong <yangjihong1@huawei.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Athira Jajeev <atrajeev@linux.vnet.ibm.com>
Cc: llvm@lists.linux.dev
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20240221034155.1500118-5-irogers@google.com
tools/lib/subcmd/run-command.c
tools/lib/subcmd/run-command.h

index 5cdac21625322bda990163e51e43236e9f9bb89b..d435eb42354bfe263e57cdce0fa8ecc4b9a7729b 100644 (file)
@@ -122,6 +122,8 @@ int start_command(struct child_process *cmd)
                }
                if (cmd->preexec_cb)
                        cmd->preexec_cb();
+               if (cmd->no_exec_cmd)
+                       exit(cmd->no_exec_cmd(cmd));
                if (cmd->exec_cmd) {
                        execv_cmd(cmd->argv);
                } else {
index 17d969c6add3bb7948bcea1ec4c346fa46298765..d794138a797f4aec5929a967c1526cded56fc665 100644 (file)
@@ -47,6 +47,8 @@ struct child_process {
        unsigned exec_cmd:1; /* if this is to be external sub-command */
        unsigned stdout_to_stderr:1;
        void (*preexec_cb)(void);
+        /* If set, call function in child rather than doing an exec. */
+       int (*no_exec_cmd)(struct child_process *process);
 };
 
 int start_command(struct child_process *);