public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Wei Gao via ltp <ltp@lists.linux.it>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v4 1/3] lib: Add support option for .needs_cmds
Date: Fri, 17 Oct 2025 10:09:56 +0000	[thread overview]
Message-ID: <20251017101011.3811-2-wegao@suse.com> (raw)
In-Reply-To: <20251017101011.3811-1-wegao@suse.com>

Signed-off-by: Wei Gao <wegao@suse.com>
Suggested-by: Cyril Hrubis <chrubis@suse.cz>
---
 include/tst_cmd.h  | 13 +++++++++++++
 include/tst_test.h | 16 ++++++++++++++--
 lib/tst_cmd.c      |  7 ++++++-
 lib/tst_test.c     | 22 ++++++++++++++++++----
 4 files changed, 51 insertions(+), 7 deletions(-)

diff --git a/include/tst_cmd.h b/include/tst_cmd.h
index 939825646..3c3668eff 100644
--- a/include/tst_cmd.h
+++ b/include/tst_cmd.h
@@ -16,6 +16,19 @@ enum tst_cmd_flags {
 	TST_CMD_TCONF_ON_MISSING = 2,
 };
 
+/**
+ * struct tst_cmd - Provides details about a command struct needed by LTP test.
+ * @cmd: The name of the command.
+ * @optional: A flag indicating if the command is optional.
+ * @present: A flag indicating if the command was found at runtime. This is an output
+ * parameter, set by the LTP library during the test setup.
+ */
+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..9305cf39d 100644
--- a/include/tst_test.h
+++ b/include/tst_test.h
@@ -524,7 +524,7 @@ struct tst_fs {
  *
  * @tags: A {} terminated array of test tags. See struct tst_tag for details.
  *
- * @needs_cmds: A NULL terminated array of commands required for the test to run.
+ * @needs_cmds: A NULL terminated array of struct tst_cmd required for the test to run.
  *
  * @needs_cgroup_ver: If set the test will run only if the specified cgroup
  *                    version is present on the system.
@@ -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,18 @@ int tst_creat_unlinked(const char *path, int flags, mode_t mode);
  */
 const char *tst_get_tmpdir_root(void);
 
+/**
+ * tst_cmd_present() - Check if a command is present
+ * @cmd: The name of the command to check for.
+ *
+ * This function iterates through the &tst_test->needs_cmds array. It compares
+ * the given command name with each entry in the array and returns the 'present'
+ * flag for the matching command.
+ *
+ * Return: `true` if the command is present, `false` otherwise.
+ */
+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 53b53af1a..6bd0ea44a 100644
--- a/lib/tst_test.c
+++ b/lib/tst_test.c
@@ -1365,6 +1365,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");
@@ -1439,11 +1452,12 @@ 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) {
+			pcmd->present = tst_check_cmd(pcmd->cmd, !pcmd->optional) ? 0 : 1;
+			pcmd++;
+		}
 	}
 
 	if (tst_test->needs_drivers) {
-- 
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  reply	other threads:[~2025-10-17 10:11 UTC|newest]

Thread overview: 100+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-26  8:50 [LTP] [PATCH v1 0/2] new cmd support option for needs_cmds Wei Gao via ltp
2025-09-26  8:50 ` [LTP] [PATCH v1 1/2] lib: Add support option for .needs_cmds Wei Gao via ltp
2025-09-26  9:31   ` Cyril Hrubis
2025-09-26  8:50 ` [LTP] [PATCH v1 2/2] ioctl_loop01.c: Update to new .needs_cmds struct Wei Gao via ltp
2025-09-26  9:32   ` Cyril Hrubis
2025-09-28 23:26 ` [LTP] [PATCH v1 0/2] new cmd support option for needs_cmds Wei Gao via ltp
2025-09-28 23:26   ` [LTP] [PATCH v2 1/2] lib: Add support option for .needs_cmds Wei Gao via ltp
2025-09-30 13:36     ` Petr Vorel
2025-10-10  6:32       ` Wei Gao via ltp
2025-10-10  6:45     ` [LTP] [PATCH v3 0/4] new cmd support option for needs_cmds Wei Gao via ltp
2025-10-10  6:45       ` [LTP] [PATCH v3 1/4] lib: Add support option for .needs_cmds Wei Gao via ltp
2025-10-10  9:13         ` Petr Vorel
2025-10-10  9:45           ` Petr Vorel
2025-10-10  6:45       ` [LTP] [PATCH v3 2/4] ioctl_loop01.c: Update to new .needs_cmds struct Wei Gao via ltp
2025-10-10  6:45       ` [LTP] [PATCH v3 3/4] Update test cases use new needs_cmds Wei Gao via ltp
2025-10-10  6:45       ` [LTP] [PATCH v3 4/4] tst_run_shell.c: Add new function handle " Wei Gao via ltp
2025-10-17 10:09       ` [LTP] [PATCH v4 0/3] new cmd support option for needs_cmds Wei Gao via ltp
2025-10-17 10:09         ` Wei Gao via ltp [this message]
2025-10-17 14:35           ` [LTP] [PATCH v4 1/3] lib: Add support option for .needs_cmds Petr Vorel
2025-10-20  1:22             ` Wei Gao via ltp
2025-10-20 13:21               ` Petr Vorel
2025-10-21  3:42                 ` Wei Gao via ltp
2025-10-22  9:23                 ` Li Wang via ltp
2025-10-22 14:19                   ` Wei Gao via ltp
2025-10-17 15:37           ` Petr Vorel
2025-10-20  1:24             ` Wei Gao via ltp
2025-10-20 13:33           ` Petr Vorel
2025-10-21  3:17             ` Wei Gao via ltp
2025-10-17 10:09         ` [LTP] [PATCH v4 2/3] Update test cases use new needs_cmds Wei Gao via ltp
2025-10-17 10:09         ` [LTP] [PATCH v4 3/3] tst_run_shell.c: Add new function handle " Wei Gao via ltp
2025-10-17 15:30           ` Petr Vorel
2025-10-17 15:41           ` Petr Vorel
2025-10-20  1:41             ` Wei Gao via ltp
2025-11-07  0:30       ` [LTP] [PATCH v4 0/4] new cmd support option for needs_cmds Wei Gao via ltp
2025-11-07  0:30         ` [LTP] [PATCH v4 1/4] tst_cmd.c: Check brk_nosupp when tst_get_path failed Wei Gao via ltp
2025-11-07 10:33           ` Petr Vorel
2025-11-07  0:30         ` [LTP] [PATCH v4 2/4] lib: Add support option for .needs_cmds Wei Gao via ltp
2025-11-07  0:30         ` [LTP] [PATCH v4 3/4] ioctl_loop01.c: Add new support .needs_cmds Wei Gao via ltp
2025-11-07 11:04           ` Petr Vorel
2025-11-08 12:58             ` Wei Gao via ltp
2025-11-07  0:30         ` [LTP] [PATCH v4 4/4] shell_loader_cmd.sh: New test case check needs_cmds Wei Gao via ltp
2025-11-07 11:41           ` Petr Vorel
2025-11-10  2:47         ` [LTP] [PATCH v5 0/3] new cmd support option for needs_cmds Wei Gao via ltp
2025-11-10  2:47           ` [LTP] [PATCH v5 1/3] lib: Add support option for .needs_cmds Wei Gao via ltp
2025-11-11 11:06             ` Petr Vorel
2025-12-12 10:30             ` Cyril Hrubis
2025-12-12 11:16               ` Petr Vorel
2025-12-15  7:33                 ` Wei Gao via ltp
2025-12-15  9:36                   ` Petr Vorel
2025-12-15 10:59                     ` Wei Gao via ltp
2025-12-17 13:18                       ` Petr Vorel
2026-01-07  8:05                   ` Petr Vorel
2025-11-10  2:47           ` [LTP] [PATCH v5 2/3] ioctl_loop01.c: Add new support .needs_cmds Wei Gao via ltp
2025-11-11 11:14             ` Petr Vorel
2025-11-10  2:47           ` [LTP] [PATCH v5 3/3] shell_loader_cmd.sh: New test case check needs_cmds Wei Gao via ltp
2025-11-11 10:41             ` Wei Gao via ltp
2025-11-11 11:15             ` Petr Vorel
2025-12-23  2:08           ` [LTP] [PATCH v6 0/4] new cmd support option for needs_cmds Wei Gao via ltp
2025-12-23  2:08             ` [LTP] [PATCH v6 1/4] lib: Add support option for .needs_cmds Wei Gao via ltp
2026-01-05 13:48               ` Petr Vorel
2026-01-06 10:01               ` Cyril Hrubis
2025-12-23  2:08             ` [LTP] [PATCH v6 2/4] tst_test.c: Add tst_cmd_present check if a command is present Wei Gao via ltp
2026-01-05 13:52               ` Petr Vorel
2026-01-06 10:02               ` Cyril Hrubis
2026-01-07  6:16                 ` Wei Gao via ltp
2026-01-07  8:09                   ` Petr Vorel
2026-01-07  8:27                     ` Petr Vorel
2026-01-07  9:59                       ` Cyril Hrubis
2026-01-09  6:11                         ` Wei Gao via ltp
2026-01-12 11:05                         ` Petr Vorel
2026-01-07  9:56                   ` Cyril Hrubis
2025-12-23  2:08             ` [LTP] [PATCH v6 3/4] ioctl_loop01.c: Add new support .needs_cmds Wei Gao via ltp
2026-01-05 13:56               ` Petr Vorel
2025-12-23  2:08             ` [LTP] [PATCH v6 4/4] shell_loader_cmd.sh: New test case check needs_cmds Wei Gao via ltp
2026-01-05 13:57               ` Petr Vorel
2026-01-09  6:16             ` [LTP] [PATCH v7 0/4] new cmd support option for needs_cmds Wei Gao via ltp
2026-01-09  6:16               ` [LTP] [PATCH v7 1/4] lib: Add support option for .needs_cmds Wei Gao via ltp
2026-01-09 19:15                 ` Petr Vorel
2026-01-09 19:21                   ` Petr Vorel
2026-01-16 14:03                 ` Li Wang via ltp
2026-01-19 14:51                 ` Cyril Hrubis
2026-01-20  6:42                   ` Petr Vorel
2026-01-09  6:16               ` [LTP] [PATCH v7 2/4] tst_test.c: Add tst_cmd_present check if a command is present Wei Gao via ltp
2026-01-09 19:17                 ` Petr Vorel
2026-01-12 11:08                   ` Petr Vorel
2026-01-16 13:58                 ` Li Wang via ltp
2026-01-19 13:17                 ` Cyril Hrubis
2026-01-09  6:16               ` [LTP] [PATCH v7 3/4] ioctl_loop01.c: Add new support .needs_cmds Wei Gao via ltp
2026-01-16 13:25                 ` Li Wang via ltp
2026-01-17 13:16                   ` Wei Gao via ltp
2026-01-19  3:00                     ` Li Wang via ltp
2026-01-19  5:34                       ` Wei Gao via ltp
2026-01-19  6:27                         ` Li Wang via ltp
2026-01-19 14:57                 ` Cyril Hrubis
2026-01-21 13:08                 ` Cyril Hrubis
2026-01-09  6:16               ` [LTP] [PATCH v7 4/4] shell_loader_cmd.sh: New test case check needs_cmds Wei Gao via ltp
2026-01-21 13:09                 ` Cyril Hrubis
2026-01-21 13:11                   ` Petr Vorel
2025-09-28 23:26   ` [LTP] [PATCH v2 2/2] ioctl_loop01.c: Update to new .needs_cmds struct Wei Gao via ltp
2025-09-30 13:12     ` Petr Vorel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251017101011.3811-2-wegao@suse.com \
    --to=ltp@lists.linux.it \
    --cc=wegao@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox