[LTP] [PATCH v2 1/2] lib: Add support option for .needs_cmds
Wei Gao
wegao@suse.com
Mon Sep 29 01:26:57 CEST 2025
Signed-off-by: Wei Gao <wegao@suse.com>
Suggested-by: Cyril Hrubis <chrubis@suse.cz>
---
include/tst_cmd.h | 6 ++++++
include/tst_test.h | 8 +++++++-
lib/tst_cmd.c | 7 ++++++-
lib/tst_test.c | 25 +++++++++++++++++++++----
4 files changed, 40 insertions(+), 6 deletions(-)
diff --git a/include/tst_cmd.h b/include/tst_cmd.h
index 939825646..148afc49c 100644
--- a/include/tst_cmd.h
+++ b/include/tst_cmd.h
@@ -16,6 +16,12 @@ enum tst_cmd_flags {
TST_CMD_TCONF_ON_MISSING = 2,
};
+struct tst_cmd {
+ const char *cmd;
+ unsigned int optional:1;
+ unsigned int present:1;
+};
+
/*
* vfork() + execvp() specified program.
*
diff --git a/include/tst_test.h b/include/tst_test.h
index 9c21c1728..bef836525 100644
--- a/include/tst_test.h
+++ b/include/tst_test.h
@@ -617,7 +617,7 @@ struct tst_fs {
const struct tst_tag *tags;
- const char *const *needs_cmds;
+ struct tst_cmd *needs_cmds;
const enum tst_cg_ver needs_cgroup_ver;
@@ -721,6 +721,12 @@ int tst_creat_unlinked(const char *path, int flags, mode_t mode);
*/
const char *tst_get_tmpdir_root(void);
+/*
+ * tst_cmd_present would loop over the tst_cmd array and return the supported flag
+ * value.
+ */
+bool tst_cmd_present(const char *cmd);
+
/*
* Validates exit status of child processes
*/
diff --git a/lib/tst_cmd.c b/lib/tst_cmd.c
index 82d60497a..cfd38c31a 100644
--- a/lib/tst_cmd.c
+++ b/lib/tst_cmd.c
@@ -265,7 +265,12 @@ int tst_check_cmd(const char *cmd, const int brk_nosupp)
str = strtok_r(NULL, " ", &next);
if (tst_get_path(cmd_token, path, sizeof(path)))
- tst_brkm(TCONF, NULL, "Couldn't find '%s' in $PATH", cmd_token);
+ if (brk_nosupp) {
+ tst_brkm(TCONF, NULL, "Couldn't find '%s' in $PATH", cmd_token);
+ } else {
+ tst_resm(TCONF, "Couldn't find '%s' in $PATH", cmd_token);
+ return 1;
+ }
if (!op_token)
return 0;
diff --git a/lib/tst_test.c b/lib/tst_test.c
index b8894f782..ed9459e2f 100644
--- a/lib/tst_test.c
+++ b/lib/tst_test.c
@@ -1353,6 +1353,19 @@ static const char *default_fs_type(void)
return tst_dev_fs_type();
}
+bool tst_cmd_present(const char *cmd)
+{
+ struct tst_cmd *pcmd = tst_test->needs_cmds;
+
+ while (pcmd->cmd) {
+ if (!strcmp(pcmd->cmd, cmd))
+ return pcmd->present;
+
+ pcmd++;
+ }
+ return false;
+}
+
static void do_setup(int argc, char *argv[])
{
char *tdebug_env = getenv("LTP_ENABLE_DEBUG");
@@ -1422,11 +1435,15 @@ static void do_setup(int argc, char *argv[])
tst_brk(TCONF, "%dbit ABI is not supported", tst_test->needs_abi_bits);
if (tst_test->needs_cmds) {
- const char *cmd;
- int i;
+ struct tst_cmd *pcmd = tst_test->needs_cmds;
- for (i = 0; (cmd = tst_test->needs_cmds[i]); ++i)
- tst_check_cmd(cmd, 1);
+ while (pcmd->cmd) {
+ if (tst_check_cmd(pcmd->cmd, !pcmd->optional))
+ pcmd->present = 0;
+ else
+ pcmd->present = 1;
+ pcmd++;
+ }
}
if (tst_test->needs_drivers) {
--
2.51.0
More information about the ltp
mailing list