Skip to contentSkip to conversation

new cmd support option for needs_cmds

PATCH Superseded Wei Gao via ltp 3 participants 100 messages Sep 26, 2025 – Jan 21 Library code

lore
Share & tools
lore
#0 lore raw

cover new cmd support option for needs_cmds

NOTE:This patchset is draft for review not final one. Since not include update .needs_cmds for all related testcases.

Wei Gao (2):
lib: Add support option for .needs_cmds ioctl_loop01.c: Update to new .needs_cmds struct

 include/tst_cmd.h                             | 15 +++
 include/tst_private.h                         |  2 +
 include/tst_test.h                            |  2 +-
 lib/tst_cmd.c                                 | 98 +++++++++++++++++++
 lib/tst_test.c                                |  9 +-
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 23 ++---
 6 files changed, 130 insertions(+), 19 deletions(-)
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH 1/2 lib: Add support option for .needs_cmds

NOTE:This patchset is draft for review not final one. Since not include update .needs_cmds for all related testcases.

---
 include/tst_cmd.h     | 15 +++++++
 include/tst_private.h |  2 +
 include/tst_test.h    |  2 +-
 lib/tst_cmd.c         | 98 +++++++++++++++++++++++++++++++++++++++++++
 lib/tst_test.c        |  9 ++--
 5 files changed, 121 insertions(+), 5 deletions(-)
include/tst_cmd.h +15 -0
index 939825646..c7e7e56c1 100644
@@ -5,6 +5,8 @@
 #ifndef TST_CMD_H__
 #define TST_CMD_H__
 
+#include <stdbool.h>
+
 enum tst_cmd_flags {
 	/*
 	 * return the program exit code, otherwise it will call cleanup_fn() if the
@@ -16,6 +18,19 @@ enum tst_cmd_flags {
 	TST_CMD_TCONF_ON_MISSING = 2,
 };
 
+struct tst_cmd {
+	const char *cmd;
+	unsigned int required:1;
+	unsigned int support:1;
+};
+
+
+/*
+ * tst_cmd_present would loop over the tst_cmd array and return the supported flag
+ * value.
+ */
+bool tst_cmd_present(struct tst_cmd *pcmd, const char *cmd);
+
 /*
  * vfork() + execvp() specified program.
  *
include/tst_private.h +2 -0
index 4c6479f4b..d549cf968 100644
@@ -47,4 +47,6 @@ char tst_kconfig_get(const char *confname);
  */
 int tst_check_cmd(const char *cmd, const int brk_nosupp);
 
+int tst_check_needs_cmds(struct tst_cmd *cmd, const int brk_nosupp);
+
 #endif
include/tst_test.h +1 -1
index 9c21c1728..8fb7cd86c 100644
@@ -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;
 
lib/tst_cmd.c +98 -0

101 lines not on this page · raw message

lib/tst_test.c +5 -4
index b8894f782..41519d4e1 100644
@@ -1422,11 +1422,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 *cmd = tst_test->needs_cmds;
 
-		for (i = 0; (cmd = tst_test->needs_cmds[i]); ++i)
-			tst_check_cmd(cmd, 1);
+		while (cmd->cmd) {
+			tst_check_needs_cmds(cmd,cmd->required);
+			cmd++;
+		}
 	}
 
 	if (tst_test->needs_drivers) {
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] [PATCH v1 1/2] lib: Add support option for .needs_cmds

Hi!

Quoted 24 lines
---
 include/tst_cmd.h     | 15 +++++++
 include/tst_private.h |  2 +
 include/tst_test.h    |  2 +-
 lib/tst_cmd.c         | 98 +++++++++++++++++++++++++++++++++++++++++++
 lib/tst_test.c        |  9 ++--
 5 files changed, 121 insertions(+), 5 deletions(-)
include/tst_cmd.h +2 -0
index 939825646..c7e7e56c1 100644
@@ -5,6 +5,8 @@
 #ifndef TST_CMD_H__
 #define TST_CMD_H__
 
+#include <stdbool.h>
+
 enum tst_cmd_flags {
 	/*
 	 * return the program exit code, otherwise it will call cleanup_fn() if the

include/tst_cmd.h enum tst_cmd_flags { · line 23

Quoted 6 lines
include/tst_cmd.h +3 -0
@@ -16,6 +18,19 @@ enum tst_cmd_flags {
 	TST_CMD_TCONF_ON_MISSING = 2,
 };
 
+struct tst_cmd {
+	const char *cmd;
+	unsigned int required:1;

Maybe it would make more sense to flip the logic and add an 'optional' flag here instead. So that for most of the cases we just need to initialize the cmd member.

include/tst_cmd.h enum tst_cmd_flags { · line 24

Quoted 1 line
include/tst_cmd.h +1 -0
+	unsigned int support:1;

^
This should be named 'present' to match the function name.

Quoted 121 lines
include/tst_cmd.h +9 -0
+};
+
+
+/*
+ * tst_cmd_present would loop over the tst_cmd array and return the supported flag
+ * value.
+ */
+bool tst_cmd_present(struct tst_cmd *pcmd, const char *cmd);
+
 /*
  * vfork() + execvp() specified program.
  *
include/tst_private.h +2 -0
index 4c6479f4b..d549cf968 100644
@@ -47,4 +47,6 @@ char tst_kconfig_get(const char *confname);
  */
 int tst_check_cmd(const char *cmd, const int brk_nosupp);
 
+int tst_check_needs_cmds(struct tst_cmd *cmd, const int brk_nosupp);
+
 #endif
include/tst_test.h +1 -1
index 9c21c1728..8fb7cd86c 100644
@@ -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;
 
lib/tst_cmd.c +77 -0
index 82d60497a..7457d17c4 100644
@@ -330,3 +330,101 @@ error:
 
 	return 1;
 }
+
+int tst_check_needs_cmds(struct tst_cmd *cmd, const int brk_nosupp)
+{
+	struct version_parser *p;
+	char *cmd_token, *op_token, *version_token, *next, *str;
+	char path[PATH_MAX];
+	char parser_cmd[100];
+	int ver_parser, ver_get;
+
+	strcpy(parser_cmd, cmd->cmd);
+
+	cmd_token = strtok_r(parser_cmd, " ", &next);
+	op_token = strtok_r(NULL, " ", &next);
+	version_token = strtok_r(NULL, " ", &next);
+	str = strtok_r(NULL, " ", &next);
+
+	if (tst_get_path(cmd_token, path, sizeof(path)))
+		if (brk_nosupp)
+			tst_brkm(TCONF, NULL, "Couldn't find '%s' in $PATH", cmd_token);
+		else
+			goto error;
+
+	if (!op_token)
+		goto pass;
+
+	if (!version_token || str) {
+		tst_brkm(TCONF, NULL,
+			"Illegal format(%s), should use format like mkfs.ext4 >= 1.43.0",
+			cmd->cmd);
+	}
+
+	for (p = &version_parsers[0]; p->cmd; p++) {
+		if (!strcmp(p->cmd, cmd_token)) {
+			tst_resm(TINFO, "Parsing %s version", p->cmd);
+			break;
+		}
+	}
+
+	if (!p->cmd) {
+		tst_brkm(TBROK, NULL, "No version parser for %s implemented!",
+			cmd_token);
+	}
+
+	ver_parser = p->parser();
+	if (ver_parser < 0)
+		tst_brkm(TBROK, NULL, "Failed to parse %s version", p->cmd);
+
+	ver_get = p->table_get(version_token);
+	if (ver_get < 0)
+		tst_brkm(TBROK, NULL, "Failed to get %s version", p->cmd);
+
+	if (!strcmp(op_token, ">=")) {
+		if (ver_parser < ver_get)
+			goto error;
+	} else if (!strcmp(op_token, ">")) {
+		if (ver_parser <= ver_get)
+			goto error;
+	} else if (!strcmp(op_token, "<=")) {
+		if (ver_parser > ver_get)
+			goto error;
+	} else if (!strcmp(op_token, "<")) {
+		if (ver_parser >= ver_get)
+			goto error;
+	} else if (!strcmp(op_token, "==")) {
+		if (ver_parser != ver_get)
+			goto error;
+	} else if (!strcmp(op_token, "!=")) {
+		if (ver_parser == ver_get)
+			goto error;
+	} else {
+		tst_brkm(TCONF, NULL, "Invalid op(%s)", op_token);
+	}
+pass:
+	cmd->support = 1;
+	return 0;
+error:
+	cmd->support = 0;

lib/tst_cmd.c error: · line 419

Quoted 10 lines
lib/tst_cmd.c +10 -0
+	if (brk_nosupp) {
+		tst_brkm(TCONF, NULL, "%s requires %s %d, but got %d",
+			cmd, op_token, ver_get, ver_parser);
+	} else {
+		tst_resm(TCONF, "%s requires %s %d, but got %d",
+			cmd, op_token, ver_get, ver_parser);
+	}
+
+	return 1;
+}

There is no reason add this function at all, we can just call with the right arguments and get the return value from tst_check_cmd() in the tst_test.c instead.

lib/tst_cmd.c error: · line 421

Quoted 1 line
lib/tst_cmd.c +1 -0
+bool tst_cmd_present(struct tst_cmd *pcmd, const char *cmd)

No need to pass the struct tst_cmd here, we can access it through the global tst_test pointer instead. Also this function should go to the tst_test.c.

lib/tst_cmd.c error: · line 429

Quoted 8 lines
lib/tst_cmd.c +8 -0
+{
+	while (pcmd->cmd) {
+		if (!strcmp(pcmd->cmd, cmd))
+			return pcmd->support;
+
+		pcmd++;
+	}
+	return false;

If we got here we asked for something that wasn't defined in tst_test structure so here we should do:

tst_res(TBROK, "Invalid cmd request '%s'", cmd);

Quoted 27 lines
lib/tst_cmd.c +1 -0
+}
lib/tst_test.c +5 -4
index b8894f782..41519d4e1 100644
@@ -1422,11 +1422,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 *cmd = tst_test->needs_cmds;
 
-		for (i = 0; (cmd = tst_test->needs_cmds[i]); ++i)
-			tst_check_cmd(cmd, 1);
+		while (cmd->cmd) {
+			tst_check_needs_cmds(cmd,cmd->required);
+			cmd++;
+		}
 	}
 
 	if (tst_test->needs_drivers) {
Signature · 1 line
2.51.0
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

PATCH 2/2 ioctl_loop01.c: Update to new .needs_cmds struct

In reply to Wei Gao via ltp

NOTE:This patchset is draft for review not final one. Since not include update .needs_cmds for all related testcases.

---
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 23 ++++++++-----------
 1 file changed, 9 insertions(+), 14 deletions(-)
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +9 -14
index c9137bf1e..a3f0857e3 100644
@@ -97,9 +97,10 @@ static void verify_ioctl_loop(void)
 
 static void setup(void)
 {
-	int ret;
+	parted_sup = tst_cmd_present(test.needs_cmds, "parted");
+
 	const char *const cmd_parted[] = {"parted", "-s", "test.img", "mklabel", "msdos", "mkpart",
-	                                  "primary", "ext4", "1M", "10M", NULL};
+					"primary", "ext4", "1M", "10M", NULL};
 
 	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
 	if (dev_num < 0)
@@ -107,18 +108,8 @@ static void setup(void)
 
 	tst_fill_file("test.img", 0, 1024 * 1024, 10);
 
-	ret = tst_cmd(cmd_parted, NULL, NULL, TST_CMD_PASS_RETVAL);
-	switch (ret) {
-	case 0:
-		parted_sup = 1;
-	break;
-	case 255:
-		tst_res(TCONF, "parted binary not installed or failed");
-	break;
-	default:
-		tst_res(TCONF, "parted exited with %i", ret);
-	break;
-	}
+	if (parted_sup)
+		SAFE_CMD(cmd_parted, NULL, NULL);
 
 	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
 	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);
@@ -152,5 +143,9 @@ static struct tst_test test = {
 		{"linux-git", "6ac92fb5cdff"},
 		{}
 	},
+	.needs_cmds = (struct tst_cmd[]) {
+		{"parted", 0, 0},
+		{}
+	},
 	.needs_tmpdir = 1,
 };
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Re: [LTP] [PATCH v1 2/2] ioctl_loop01.c: Update to new .needs_cmds struct

Quoted 48 lines · Wei Gao

NOTE:This patchset is draft for review not final one. Since not include update .needs_cmds for all related testcases.

---
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 23 ++++++++-----------
 1 file changed, 9 insertions(+), 14 deletions(-)
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +5 -14
index c9137bf1e..a3f0857e3 100644
@@ -97,9 +97,10 @@ static void verify_ioctl_loop(void)
 
 static void setup(void)
 {
-	int ret;
+	parted_sup = tst_cmd_present(test.needs_cmds, "parted");
+
 	const char *const cmd_parted[] = {"parted", "-s", "test.img", "mklabel", "msdos", "mkpart",
-	                                  "primary", "ext4", "1M", "10M", NULL};
+					"primary", "ext4", "1M", "10M", NULL};
 
 	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
 	if (dev_num < 0)
@@ -107,18 +108,8 @@ static void setup(void)
 
 	tst_fill_file("test.img", 0, 1024 * 1024, 10);
 
-	ret = tst_cmd(cmd_parted, NULL, NULL, TST_CMD_PASS_RETVAL);
-	switch (ret) {
-	case 0:
-		parted_sup = 1;
-	break;
-	case 255:
-		tst_res(TCONF, "parted binary not installed or failed");
-	break;
-	default:
-		tst_res(TCONF, "parted exited with %i", ret);
-	break;
-	}
+	if (parted_sup)
+		SAFE_CMD(cmd_parted, NULL, NULL);
 
 	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
 	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);

testcases/kernel/syscalls/ioctl/ioctl_loop01.c static struct tst_test test = { · line 147

Quoted 5 lines
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +2 -0
@@ -152,5 +143,9 @@ static struct tst_test test = {
 		{"linux-git", "6ac92fb5cdff"},
 		{}
 	},
+	.needs_cmds = (struct tst_cmd[]) {
+		{"parted", 0, 0},

No need to pass the zeros if we just initialize the cmd part. Also I would use the named initializers as:

		{.cmd = "parted", .optional = 1},
Quoted 7 lines
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +2 -0
+		{}
+	},
 	.needs_tmpdir = 1,
 };
Signature · 1 line
2.51.0
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

cover new cmd support option for needs_cmds

In reply to Wei Gao via ltp

Changes in v2:
- update base Cyril's comments

Wei Gao (2):
lib: Add support option for .needs_cmds ioctl_loop01.c: Update to new .needs_cmds struct

 include/tst_cmd.h                             |  6 +++++
 include/tst_test.h                            |  8 +++++-
 lib/tst_cmd.c                                 |  7 +++++-
 lib/tst_test.c                                | 25 ++++++++++++++++---
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 23 +++++++----------
 5 files changed, 49 insertions(+), 20 deletions(-)
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

PATCH v2 1/2 lib: Add support option for .needs_cmds

---
 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(-)
include/tst_cmd.h +6 -0
index 939825646..148afc49c 100644
@@ -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.
  *
include/tst_test.h +7 -1
index 9c21c1728..bef836525 100644
@@ -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
  */
lib/tst_cmd.c +6 -1
index 82d60497a..cfd38c31a 100644
@@ -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;
lib/tst_test.c +21 -4
index b8894f782..ed9459e2f 100644
@@ -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) {
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

Re: [LTP] [PATCH v2 1/2] lib: Add support option for .needs_cmds

Hi Wei,

...

include/tst_cmd.h enum tst_cmd_flags { · line 22

Quoted 9 lines

+++ b/include/tst_cmd.h

include/tst_cmd.h +4 -0
@@ -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;

Maybe s/present/available/ ?

include/tst_cmd.h enum tst_cmd_flags { · line 23

Quoted 1 line
include/tst_cmd.h +1 -0
+};

Could you please add doc? Whole include/tst_cmd.h is documented, it's obvious that new functionality should be documented.

And best would be if you use kerneldoc syntax.
https://docs.kernel.org/doc-guide/kernel-doc.html
https://www.sphinx-doc.org/en/master/usage/domains/c.html

Quoted 9 lines
include/tst_cmd.h +1 -0
+
 /*
  * vfork() + execvp() specified program.
  *
include/tst_test.h +0 -0
index 9c21c1728..bef836525 100644

include/tst_test.h struct tst_fs { · line 620

Quoted 5 lines
include/tst_test.h +1 -1
@@ -617,7 +617,7 @@ struct tst_fs {
 
 	const struct tst_tag *tags;
 
-	const char *const *needs_cmds;
+	struct tst_cmd *needs_cmds;

This effectively breaks build, because you haven't update any of these users (24 tests + 2 doc files.

include/tst_test.h int tst_creat_unlinked(const char *path, int flags, mode_t mode); · line 725

Quoted 8 lines
include/tst_test.h +2 -0
 	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

I'm not a native speaker, but I think:

s/would loop/loops/

include/tst_test.h int tst_creat_unlinked(const char *path, int flags, mode_t mode); · line 727

Quoted 2 lines
include/tst_test.h +2 -0
+ * value.
+ */

Please use kernel doc syntax (see links above).

Quoted 10 lines
include/tst_test.h +2 -0
+bool tst_cmd_present(const char *cmd);
+
 /*
  * Validates exit status of child processes
  */
lib/tst_cmd.c +0 -0
index 82d60497a..cfd38c31a 100644

lib/tst_cmd.c int tst_check_cmd(const char *cmd, const int brk_nosupp) · line 273

Quoted 10 lines
lib/tst_cmd.c +6 -1
@@ -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;
+		}

+1

...

Quoted 26 lines
lib/tst_cmd.c +14 -1
+++ 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;

lib/tst_cmd.c static void do_setup(int argc, char *argv[]) · line 1444

Quoted 10 lines
lib/tst_cmd.c +6 -3
-		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;

very nit: use ternary operator?

				pcmd->present = tst_check_cmd(pcmd->cmd, !pcmd->optional) ? 0 : 1;

Kind regards,
Petr

Quoted 2 lines
lib/tst_cmd.c +2 -0
+			pcmd++;
+		}
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw

Re: [LTP] [PATCH v2 1/2] lib: Add support option for .needs_cmds

Quoted 8 lines · Petr Vorel

Hi Wei,

...

Quoted 4 lines

+++ b/include/tst_cmd.h

include/tst_cmd.h +0 -0
@@ -16,6 +16,12 @@ enum tst_cmd_flags {
 	TST_CMD_TCONF_ON_MISSING = 2,
 };
Quoted 6 lines

include/tst_cmd.h enum tst_cmd_flags { · line 22

Quoted 4 lines
include/tst_cmd.h +4 -0
+struct tst_cmd {
+	const char *cmd;
+	unsigned int optional:1;
+	unsigned int present:1;

Maybe s/present/available/ ?

This is suggested by Cyril Hrubis during v1 review.

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#9 lore raw

v3 cover new cmd support option for needs_cmds

In reply to Wei Gao via ltp

Changes in v2:
- update base Cyril's comments
Changes in v3:
- update all new needs_cmds related test cases
- fix Ci issue for tst_run_shell.c

Wei Gao (4):
lib: Add support option for .needs_cmds ioctl_loop01.c: Update to new .needs_cmds struct Update test cases use new needs_cmds tst_run_shell.c: Add new function handle new needs_cmds

 include/tst_cmd.h                             | 13 ++++
 include/tst_test.h                            | 15 ++++-
 lib/newlib_tests/tst_needs_cmds01.c           | 16 ++---
 lib/newlib_tests/tst_needs_cmds02.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds03.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds04.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds05.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds06.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds07.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds08.c           |  8 +--
 lib/tst_cmd.c                                 |  7 ++-
 lib/tst_test.c                                | 22 +++++--
 testcases/kernel/device-drivers/zram/zram03.c |  8 +--
 testcases/kernel/fs/squashfs/squashfs01.c     |  6 +-
 testcases/kernel/security/aslr/aslr01.c       |  6 +-
 testcases/kernel/syscalls/add_key/add_key05.c | 10 +--
 .../kernel/syscalls/fanotify/fanotify22.c     |  6 +-
 testcases/kernel/syscalls/ioctl/ioctl09.c     |  6 +-
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 28 ++++-----
 testcases/kernel/syscalls/madvise/madvise11.c |  8 +--
 .../kernel/syscalls/quotactl/quotactl01.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl04.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl06.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl08.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl09.c     |  6 +-
 testcases/kernel/syscalls/statx/statx05.c     |  8 +--
 testcases/kernel/syscalls/statx/statx07.c     |  6 +-
 testcases/kernel/syscalls/statx/statx09.c     |  6 +-
 testcases/lib/tst_run_shell.c                 | 61 ++++++++++++++++++-
 29 files changed, 204 insertions(+), 102 deletions(-)
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#10 lore raw

PATCH v3 1/4 lib: Add support option for .needs_cmds

---
 include/tst_cmd.h  | 13 +++++++++++++
 include/tst_test.h | 15 +++++++++++++--
 lib/tst_cmd.c      |  7 ++++++-
 lib/tst_test.c     | 22 ++++++++++++++++++----
 4 files changed, 50 insertions(+), 7 deletions(-)
include/tst_cmd.h +13 -0
index 939825646..703589019 100644
@@ -16,6 +16,19 @@ enum tst_cmd_flags {
 	TST_CMD_TCONF_ON_MISSING = 2,
 };
 
+/**
+ * struct tst_cmd - This structure provides details about a command 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.
  *
include/tst_test.h +13 -2
index 9c21c1728..2730d22c1 100644
@@ -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,17 @@ 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 '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
  */
lib/tst_cmd.c +6 -1
index 82d60497a..cfd38c31a 100644
@@ -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;
lib/tst_test.c +18 -4
index 53b53af1a..6bd0ea44a 100644
@@ -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) {
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#11 lore raw

Re: [LTP] [PATCH v3 1/4] lib: Add support option for .needs_cmds

Hi Wei,

Quoted 6 lines
---
 include/tst_cmd.h  | 13 +++++++++++++
 include/tst_test.h | 15 +++++++++++++--
 lib/tst_cmd.c      |  7 ++++++-
 lib/tst_test.c     | 22 ++++++++++++++++++----
 4 files changed, 50 insertions(+), 7 deletions(-)

tst_needs_cmds01.c:15:23: error: initialization of ‘struct tst_cmd *’ from incompatible pointer type ‘const char **’ [-Wincompatible-pointer-types]
15 | .needs_cmds = (const char *[]) {
| ^
tst_needs_cmds01.c:15:23: note: (near initialization for ‘test.needs_cmds’)
make[1]: *** [../../include/mk/rules.mk:48: tst_needs_cmds01] Error 1

I asked on v2 not to break build [1], but you didn't reflected that. The reason for this requirement is that all commits need to built due ability to bisect (quite obvious requirement). This mean you need in this commit also modify all the tests which use .needs_cmds, i.e. 3rd commit "Update test cases use new needs_cmds" needs to be
squashed into this 1st commit.

[1] https://lore.kernel.org/ltp/20250930133623.GB238065@pevik/

include/tst_cmd.h enum tst_cmd_flags { · line 20

Quoted 10 lines
include/tst_cmd.h +2 -0
index 939825646..703589019 100644
@@ -16,6 +16,19 @@ enum tst_cmd_flags {
 	TST_CMD_TCONF_ON_MISSING = 2,
 };
 
+/**
+ * struct tst_cmd - This structure provides details about a command needed by LTP test

I would personally avoid "This structure" / "This function". Missing dot at the end.

include/tst_cmd.h enum tst_cmd_flags { · line 23

Quoted 3 lines
include/tst_cmd.h +3 -0
+ * @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

Maybe use 2 sentences:
* @present: A flag indicating if the command was found at runtime. This is an output

include/tst_cmd.h enum tst_cmd_flags { · line 29

Quoted 6 lines
include/tst_cmd.h +6 -0
+ * parameter, set by the LTP library during the test setup.
+ */
+struct tst_cmd {
+	const char *cmd;
+	unsigned int optional:1;
+	unsigned int present:1;

Hopefully nobody misuses this to set it in the test.

Quoted 28 lines
include/tst_cmd.h +2 -0
+};
+
 /*
  * vfork() + execvp() specified program.
  *
include/tst_test.h +2 -2
index 9c21c1728..2730d22c1 100644
@@ -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;
 

include/tst_test.h int tst_creat_unlinked(const char *path, int flags, mode_t mode); · line 728

Quoted 8 lines
include/tst_test.h +5 -0
@@ -721,6 +721,17 @@ 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 'needs_cmds' array. It compares the given command name

Please use &tst_test->needs_cmds:

* 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.

That helps struct to be linkable.

include/tst_test.h int tst_creat_unlinked(const char *path, int flags, mode_t mode); · line 731

Quoted 3 lines
include/tst_test.h +3 -0
+ * with each entry in the array and returns the 'present' flag for the matching command.
+ *
+ * Return: `true` if the command is present, `false` otherwise.

This needs to be Returns: (missing s)
https://docs.kernel.org/doc-guide/kernel-doc.html

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#12 lore raw

Re: [LTP] [PATCH v3 1/4] lib: Add support option for .needs_cmds

Quoted 7 lines

Hi Wei,

Quoted 3 lines

+ * with each entry in the array and returns the 'present' flag for the matching command.
+ *
+ * Return: true if the command is present, false otherwise.

This needs to be Returns: (missing s)
https://docs.kernel.org/doc-guide/kernel-doc.html

I'm sorry, I was wrong, it should be 'Return:', kerneldoc is wrong.

Kind regards,
Petr

Quoted 2 lines

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#13 lore raw

PATCH v3 2/4 ioctl_loop01.c: Update to new .needs_cmds struct

In reply to Wei Gao via ltp
---
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 28 +++++++++----------
 1 file changed, 14 insertions(+), 14 deletions(-)
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +14 -14
index cb1b506d2..fe62e7e5a 100644
@@ -78,21 +78,7 @@ static void check_loop_value(int set_flag, int get_flag, int autoclear_field)
 
 static void verify_ioctl_loop(void)
 {
-	int ret;
-	const char *const cmd_parted[] = {"parted", "-s", dev_path, "mklabel", "msdos", "mkpart",
-					"primary", "ext4", "1M", "10M", NULL};
-
-	tst_fill_file("test.img", 0, 1024 * 1024, 10);
 	tst_attach_device(dev_path, "test.img");
-
-	ret = tst_cmd(cmd_parted, NULL, NULL, TST_CMD_PASS_RETVAL);
-	if (!ret)
-		parted_sup = 1;
-	else if (ret == 255)
-		tst_res(TCONF, "parted binary not installed or failed");
-	else
-		tst_res(TCONF, "parted exited with %i", ret);
-
 	attach_flag = 1;
 
 	TST_ASSERT_INT(partscan_path, 0);
@@ -112,10 +98,20 @@ static void verify_ioctl_loop(void)
 
 static void setup(void)
 {
+	parted_sup = tst_cmd_present("parted");
+
+	const char *const cmd_parted[] = {"parted", "-s", "test.img", "mklabel", "msdos", "mkpart",
+	                                  "primary", "ext4", "1M", "10M", NULL};
+
 	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
 	if (dev_num < 0)
 		tst_brk(TBROK, "Failed to find free loop device");
 
+	tst_fill_file("test.img", 0, 1024 * 1024, 10);
+
+	if (parted_sup)
+		SAFE_CMD(cmd_parted, NULL, NULL);
+
 	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
 	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);
 	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
@@ -148,5 +144,9 @@ static struct tst_test test = {
 		{"linux-git", "6ac92fb5cdff"},
 		{}
 	},
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "parted", .optional = 1},
+		{}
+	},
 	.needs_tmpdir = 1,
 };
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#14 lore raw

PATCH v3 3/4 Update test cases use new needs_cmds

In reply to Wei Gao via ltp
---
 lib/newlib_tests/tst_needs_cmds01.c             | 16 ++++++++--------
 lib/newlib_tests/tst_needs_cmds02.c             |  6 +++---
 lib/newlib_tests/tst_needs_cmds03.c             |  6 +++---
 lib/newlib_tests/tst_needs_cmds04.c             |  6 +++---
 lib/newlib_tests/tst_needs_cmds05.c             |  6 +++---
 lib/newlib_tests/tst_needs_cmds06.c             |  6 +++---
 lib/newlib_tests/tst_needs_cmds07.c             |  6 +++---
 lib/newlib_tests/tst_needs_cmds08.c             |  8 ++++----
 testcases/kernel/device-drivers/zram/zram03.c   |  8 ++++----
 testcases/kernel/fs/squashfs/squashfs01.c       |  6 +++---
 testcases/kernel/security/aslr/aslr01.c         |  6 +++---
 testcases/kernel/syscalls/add_key/add_key05.c   | 10 +++++-----
 testcases/kernel/syscalls/fanotify/fanotify22.c |  6 +++---
 testcases/kernel/syscalls/ioctl/ioctl09.c       |  6 +++---
 testcases/kernel/syscalls/madvise/madvise11.c   |  8 ++++----
 testcases/kernel/syscalls/quotactl/quotactl01.c |  6 +++---
 testcases/kernel/syscalls/quotactl/quotactl04.c |  6 +++---
 testcases/kernel/syscalls/quotactl/quotactl06.c |  6 +++---
 testcases/kernel/syscalls/quotactl/quotactl08.c |  6 +++---
 testcases/kernel/syscalls/quotactl/quotactl09.c |  6 +++---
 testcases/kernel/syscalls/statx/statx05.c       |  8 ++++----
 testcases/kernel/syscalls/statx/statx07.c       |  6 +++---
 testcases/kernel/syscalls/statx/statx09.c       |  6 +++---
 23 files changed, 80 insertions(+), 80 deletions(-)
lib/newlib_tests/tst_needs_cmds01.c +8 -8
index 777c69505..592456da6 100644
@@ -12,13 +12,13 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4",
-		"mkfs.ext4 >= 1.0.0",
-		"mkfs.ext4 <= 2.0.0",
-		"mkfs.ext4 != 2.0.0",
-		"mkfs.ext4 > 1.0.0",
-		"mkfs.ext4 < 2.0.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4"},
+		{.cmd = "mkfs.ext4 >= 1.0.0"},
+		{.cmd = "mkfs.ext4 <= 2.0.0"},
+		{.cmd = "mkfs.ext4 != 2.0.0"},
+		{.cmd = "mkfs.ext4 > 1.0.0"},
+		{.cmd = "mkfs.ext4 < 2.0.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds02.c +3 -3
index 455a275ea..f1d6105c2 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext45 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext45 >= 1.43.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds03.c +3 -3
index bdc1cdf6a..ac5f368e2 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 ! 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 ! 1.43.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds04.c +3 -3
index de10b8f3e..2aea51772 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds05.c +3 -3
index c3b2b3b9a..969d4e2f5 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43.0-1",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43.0-1"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds06.c +3 -3
index 40b1cf09c..91470ccf9 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43.0 2",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43.0 2"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds07.c +3 -3
index d0b4ce2ff..371bbdc1b 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext45",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext45"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds08.c +4 -4
index 38df2ef6d..412f9f293 100644
@@ -18,9 +18,9 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.xfs",
-		"mkfs.xfs >= 4.20.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.xfs"},
+		{.cmd = "mkfs.xfs >= 4.20.0"},
+		{}
 	}
 };
testcases/kernel/device-drivers/zram/zram03.c +4 -4
index 8cf26de4c..4dcf23b4e 100644
@@ -240,9 +240,9 @@ static struct tst_test test = {
 		"zram",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"modprobe",
-		"rmmod",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "modprobe"},
+		{.cmd = "rmmod"},
+		{}
 	}
 };
testcases/kernel/fs/squashfs/squashfs01.c +3 -3
index fbcb76582..7a548d62c 100644
@@ -102,9 +102,9 @@ static struct tst_test test = {
 	.needs_root = 1,
 	.needs_device = 1,
 	.dev_min_size = 1,
-	.needs_cmds = (const char *const []) {
-		"mksquashfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mksquashfs"},
+		{}
 	},
 	.needs_drivers = (const char *const []) {
 		"squashfs",
testcases/kernel/security/aslr/aslr01.c +3 -3
index 6a396e29d..e6feb2d3c 100644
@@ -277,8 +277,8 @@ static struct tst_test test = {
 		"CONFIG_HAVE_ARCH_MMAP_RND_BITS=y",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"ldd",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "ldd"},
+		{}
 	},
 };
testcases/kernel/syscalls/add_key/add_key05.c +5 -5
index c9a2f840e..b51a84b97 100644
@@ -231,11 +231,11 @@ static struct tst_test test = {
 		{&user_buf, .size = 64},
 		{}
 	},
-	.needs_cmds = (const char *const []) {
-		"useradd",
-		"userdel",
-		"groupdel",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "useradd"},
+		{.cmd = "userdel"},
+		{.cmd = "groupdel"},
+		{}
 	},
 	.tags = (const struct tst_tag[]) {
 		{"linux-git", "a08bf91ce28"},
testcases/kernel/syscalls/fanotify/fanotify22.c +3 -3
index 357e74dbc..6578474a7 100644
@@ -327,9 +327,9 @@ static struct tst_test test = {
 		{"linux-git", "76486b104168"},
 		{}
 	},
-	.needs_cmds = (const char *[]) {
-		"debugfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "debugfs"},
+		{}
 	}
 };
 
testcases/kernel/syscalls/ioctl/ioctl09.c +3 -3
index f86355f2c..1bd60fc84 100644
@@ -108,9 +108,9 @@ static struct tst_test test = {
 		"loop",
 		NULL
 	},
-	.needs_cmds = (const char *const []) {
-		"parted",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "parted"},
+		{}
 	},
 	.needs_tmpdir = 1,
 };
testcases/kernel/syscalls/madvise/madvise11.c +4 -4
index 422589f5b..73d2d4294 100644
@@ -421,10 +421,10 @@ static struct tst_test test = {
 		HW_MODULE,
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"modprobe",
-		"rmmod",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "modprobe"},
+		{.cmd = "rmmod"},
+		{}
 	},
 	.needs_kconfigs = (const char *[]) {
 		"CONFIG_MEMORY_FAILURE=y",
testcases/kernel/syscalls/quotactl/quotactl01.c +3 -3
index 9dcf74ceb..3091ac5bf 100644
@@ -223,9 +223,9 @@ static struct tst_test test = {
 		{}
 	},
 	.mntpoint = MNTPOINT,
-	.needs_cmds = (const char *const []) {
-		"quotacheck",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "quotacheck"},
+		{}
 	},
 	.setup = setup,
 	.cleanup = cleanup,
testcases/kernel/syscalls/quotactl/quotactl04.c +3 -3
index d2d7b3f3e..a70903330 100644
@@ -165,8 +165,8 @@ static struct tst_test test = {
 	},
 	.mntpoint = MNTPOINT,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/quotactl/quotactl06.c +3 -3
index 110a3aa9b..333f5720d 100644
@@ -226,9 +226,9 @@ static struct tst_test test = {
 	},
 	.mntpoint = MNTPOINT,
 	.mount_device = 1,
-	.needs_cmds = (const char *const []) {
-		"quotacheck",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "quotacheck"},
+		{}
 	},
 	.needs_root = 1,
 	.test_variants = QUOTACTL_FMT_VARIANTS,
testcases/kernel/syscalls/quotactl/quotactl08.c +3 -3
index 63087345f..ce7a8b008 100644
@@ -220,8 +220,8 @@ static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/quotactl/quotactl09.c +3 -3
index 673666347..fd6629c0c 100644
@@ -185,8 +185,8 @@ static struct tst_test test = {
 	.mount_device = 1,
 	.needs_root = 1,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx05.c +4 -4
index 2a4603226..07b5e7b94 100644
@@ -123,9 +123,9 @@ static struct tst_test test = {
 		{.type = "ext4"},
 		{}
 	},
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		"e4crypt",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{.cmd = "e4crypt"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx07.c +3 -3
index bab64591f..755644cf9 100644
@@ -171,8 +171,8 @@ static struct tst_test test = {
 		{}
 	},
 	.needs_root = 1,
-	.needs_cmds = (const char *[]) {
-		"exportfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "exportfs"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx09.c +3 -3
index ee4be4250..8838d5450 100644
@@ -162,8 +162,8 @@ static struct tst_test test = {
 		"CONFIG_FS_VERITY",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.45.2",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.45.2"},
+		{}
 	}
 };
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#15 lore raw

PATCH v3 4/4 tst_run_shell.c: Add new function handle new needs_cmds

In reply to Wei Gao via ltp
---
 testcases/lib/tst_run_shell.c | 61 ++++++++++++++++++++++++++++++++++-
 1 file changed, 60 insertions(+), 1 deletion(-)
testcases/lib/tst_run_shell.c +60 -1
index 7a446e004..a6e0d1d29 100644
@@ -175,6 +175,23 @@ static ujson_obj fs_obj = {
 	.attr_cnt = UJSON_ARRAY_SIZE(fs_attrs),
 };
 
+enum cmd_ids {
+	CMD,
+	OPTIONAL,
+	PRESENT,
+};
+
+static ujson_obj_attr cmd_attrs[] = {
+	UJSON_OBJ_ATTR_IDX(CMD, "cmd", UJSON_STR),
+	UJSON_OBJ_ATTR_IDX(OPTIONAL, "optional", UJSON_INT),
+	UJSON_OBJ_ATTR_IDX(PRESENT, "present", UJSON_INT),
+};
+
+static ujson_obj cmd_obj = {
+	.attrs = cmd_attrs,
+	.attr_cnt = UJSON_ARRAY_SIZE(cmd_attrs),
+};
+
 static int parse_mnt_flags(ujson_reader *reader, ujson_val *val)
 {
 	int ret = 0;
@@ -252,6 +269,48 @@ static struct tst_fs *parse_filesystems(ujson_reader *reader, ujson_val *val)
 	return ret;
 }
 
+static struct tst_cmd *parse_cmds(ujson_reader *reader, ujson_val *val)
+{
+	unsigned int i = 0, cnt = 0;
+	struct tst_cmd *ret;
+
+	ujson_reader_state state = ujson_reader_state_save(reader);
+
+	UJSON_ARR_FOREACH(reader, val) {
+		if (val->type != UJSON_ARR) {
+			ujson_err(reader, "Expected array!");
+			return NULL;
+		}
+		ujson_arr_skip(reader);
+		cnt++;
+	}
+
+	ujson_reader_state_load(reader, state);
+
+	ret = SAFE_MALLOC(sizeof(struct tst_cmd) * (cnt + 1));
+	memset(&ret[cnt], 0, sizeof(ret[cnt]));
+
+	UJSON_ARR_FOREACH(reader, val) {
+		UJSON_OBJ_FOREACH_FILTER(reader, val, &cmd_obj, ujson_empty_obj) {
+			switch ((enum cmd_ids)val->idx) {
+			case CMD:
+				ret[i].cmd = strdup(val->val_str);
+			break;
+			case OPTIONAL:
+				ret[i].optional = val->val_int;
+			break;
+			case MKFS_VER:
+				ret[i].present = val->val_int;
+			break;
+			}
+		}
+
+		i++;
+	}
+
+	return ret;
+}
+
 static struct tst_tag *parse_tags(ujson_reader *reader, ujson_val *val)
 {
 	unsigned int i = 0, cnt = 0;
@@ -440,7 +499,7 @@ static void parse_metadata(void)
 				ujson_err(&reader, "ABI bits must be 32 or 64");
 		break;
 		case NEEDS_CMDS:
-			test.needs_cmds = parse_strarr(&reader, &val);
+			test.needs_cmds = parse_cmds(&reader, &val);
 		break;
 		case NEEDS_DEVFS:
 			test.needs_devfs = val.val_bool;
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#16 lore raw

v4 cover new cmd support option for needs_cmds

In reply to Wei Gao via ltp

Wei Gao (3):
lib: Add support option for .needs_cmds
Update test cases use new needs_cmds tst_run_shell.c: Add new function handle new needs_cmds

 include/tst_cmd.h                             | 13 ++++
 include/tst_test.h                            | 16 ++++-
 lib/newlib_tests/tst_needs_cmds01.c           | 16 ++---
 lib/newlib_tests/tst_needs_cmds02.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds03.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds04.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds05.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds06.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds07.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds08.c           |  8 +--
 lib/tst_cmd.c                                 |  7 ++-
 lib/tst_test.c                                | 22 +++++--
 testcases/kernel/device-drivers/zram/zram03.c |  8 +--
 testcases/kernel/fs/squashfs/squashfs01.c     |  6 +-
 testcases/kernel/security/aslr/aslr01.c       |  6 +-
 testcases/kernel/syscalls/add_key/add_key05.c | 10 +--
 .../kernel/syscalls/fanotify/fanotify22.c     |  6 +-
 testcases/kernel/syscalls/ioctl/ioctl09.c     |  6 +-
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 28 ++++-----
 testcases/kernel/syscalls/madvise/madvise11.c |  8 +--
 .../kernel/syscalls/quotactl/quotactl01.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl04.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl06.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl08.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl09.c     |  6 +-
 testcases/kernel/syscalls/statx/statx05.c     |  8 +--
 testcases/kernel/syscalls/statx/statx07.c     |  6 +-
 testcases/kernel/syscalls/statx/statx09.c     |  6 +-
 testcases/lib/tst_run_shell.c                 | 61 ++++++++++++++++++-
 29 files changed, 205 insertions(+), 102 deletions(-)
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#17 lore raw

PATCH v4 1/3 lib: Add support option for .needs_cmds

---
 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(-)
include/tst_cmd.h +13 -0
index 939825646..3c3668eff 100644
@@ -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.
  *
include/tst_test.h +14 -2
index 9c21c1728..9305cf39d 100644
@@ -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
  */
lib/tst_cmd.c +6 -1
index 82d60497a..cfd38c31a 100644
@@ -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;
lib/tst_test.c +18 -4
index 53b53af1a..6bd0ea44a 100644
@@ -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) {
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#18 lore raw

Re: [LTP] [PATCH v4 1/3] lib: Add support option for .needs_cmds

Hi Wei,

Quoted 11 lines

+++ b/include/tst_test.h

include/tst_test.h +1 -1
@@ -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.

include/tst_test.h struct tst_fs { · line 620

Quoted 5 lines
include/tst_test.h +1 -1
@@ -617,7 +617,7 @@ struct tst_fs {
 
 	const struct tst_tag *tags;
 
-	const char *const *needs_cmds;
+	struct tst_cmd *needs_cmds;

As I wrote in all previous versions, changing struct used in struct tst_test alone without changing will break this particular commit.

See when I apply just this commit.:
https://github.com/pevik/ltp/actions/runs/18595891586
https://github.com/pevik/ltp/commits/refs/heads/wei/needs_cmds.v4.first-commit-broken/

CC lib/newlib_tests/tst_expiration_timer
tst_needs_cmds01.c:15:23: error: initialization of ‘struct tst_cmd *’ from incompatible pointer type ‘const char **’ [-Wincompatible-pointer-types]
15 | .needs_cmds = (const char *[]) {
| ^
tst_needs_cmds01.c:15:23: note: (near initialization for ‘test.needs_cmds’)

...

quotactl01.c:226:23: error: initialization of ‘struct tst_cmd *’ from incompatible pointer type ‘const char * const*’ [-Wincompatible-pointer-types]
226 | .needs_cmds = (const char *const []) {
| ^

Please you need to have this commit together with "Update test cases use new needs_cmds" commit into single commit (squash these two into a single commit).

Or am I missing something?

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#19 lore raw

Re: [LTP] [PATCH v4 1/3] lib: Add support option for .needs_cmds

Quoted 34 lines · Petr Vorel

Hi Wei,

Quoted 11 lines

+++ b/include/tst_test.h

include/tst_test.h +1 -1
@@ -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.

include/tst_test.h struct tst_fs { · line 620

Quoted 5 lines
include/tst_test.h +1 -1
@@ -617,7 +617,7 @@ struct tst_fs {
 
 	const struct tst_tag *tags;
 
-	const char *const *needs_cmds;
+	struct tst_cmd *needs_cmds;

As I wrote in all previous versions, changing struct used in struct tst_test alone without changing will break this particular commit.

See when I apply just this commit.:
https://github.com/pevik/ltp/actions/runs/18595891586
https://github.com/pevik/ltp/commits/refs/heads/wei/needs_cmds.v4.first-commit-broken/

CC lib/newlib_tests/tst_expiration_timer
tst_needs_cmds01.c:15:23: error: initialization of ‘struct tst_cmd *’ from incompatible pointer type ‘const char **’ [-Wincompatible-pointer-types]
15 | .needs_cmds = (const char *[]) {
| ^
tst_needs_cmds01.c:15:23: note: (near initialization for ‘test.needs_cmds’)

...

Quoted 8 lines

quotactl01.c:226:23: error: initialization of ‘struct tst_cmd *’ from incompatible pointer type ‘const char * const*’ [-Wincompatible-pointer-types]
226 | .needs_cmds = (const char *const []) {
| ^

Please you need to have this commit together with "Update test cases use new needs_cmds" commit into single commit (squash these two into a single commit).

Or am I missing something?

Thanks for your time do verify test can review.
The reason i split this patch to small commits is easy for review, if you need every commits can pass compile phase then i have to combine all commits into a single big one, is that your request?

Quoted 3 lines

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#20 lore raw

Re: [LTP] [PATCH v4 1/3] lib: Add support option for .needs_cmds

Hi Wei,

Quoted 22 lines
Quoted 21 lines

include/tst_test.h struct tst_fs { · line 620

Quoted 2 lines
include/tst_test.h +1 -1
-	const char *const *needs_cmds;
+	struct tst_cmd *needs_cmds;

As I wrote in all previous versions, changing struct used in struct tst_test alone without changing will break this particular commit.

See when I apply just this commit.:
https://github.com/pevik/ltp/actions/runs/18595891586
https://github.com/pevik/ltp/commits/refs/heads/wei/needs_cmds.v4.first-commit-broken/

CC lib/newlib_tests/tst_expiration_timer
tst_needs_cmds01.c:15:23: error: initialization of ‘struct tst_cmd *’ from incompatible pointer type ‘const char **’ [-Wincompatible-pointer-types]
15 | .needs_cmds = (const char *[]) {
| ^
tst_needs_cmds01.c:15:23: note: (near initialization for ‘test.needs_cmds’)

...

quotactl01.c:226:23: error: initialization of ‘struct tst_cmd *’ from incompatible pointer type ‘const char * const*’ [-Wincompatible-pointer-types]
226 | .needs_cmds = (const char *const []) {
| ^

Quoted 5 lines
Quoted 4 lines

Please you need to have this commit together with "Update test cases use new needs_cmds" commit into single commit (squash these two into a single commit).

Or am I missing something?

Thanks for your time do verify test can review.

yw :).

Quoted 1 line

The reason i split this patch to small commits is easy for review, if you

Yes, it's desired that changes are split into smaller logical parts if *possible*. Yes, this really helps readability. But if possible means at lest 1) not breaking compilation (the worst case) 2) not breaking test functionality.

Therefore it's much easier to split into more commits some new library functionality (i.e. add some library functionality and enable it in separate commits, enable tests in a separate commit)

But modifying the functionality like this (when it breaks compilation) is worse than a bit more complex commit. This is the rule in many open source projects.

Quoted 2 lines

need every commits can pass compile phase then i have to combine all commits into a single big one, is that your request?

No, that's other extreme :). There is something in between, right? You did not get me correct, therefore in v4 you not only kept broken functionality, but you also joined the part which could be separated. At least "ioctl_loop01.c: Update to new .needs_cmds struct" from v3 could have been added as a separate commit (after the main change, not before). Or am I missing something?

Unfortunately "lib: Add support option for .needs_cmds" and "Update test cases use new needs_cmds" and "tst_run_shell.c: Add new function handle new needs_cmds" needs to be in a single commit, but maybe you could add functions which implement it in a separate commits (e.g. preparation for a new change) and do the change (when it's actually used) in the last commit). I'm not sure if it's worth of a separation, maybe not (hopefully we get a feedback from others). If yes:

1) commit (lib preparation) would have: struct tst_cmd, bool tst_cmd_present(const char *cmd)

2) commit (shell loader preparation) would have: enum cmd_ids, static ujson_obj_attr cmd_attrs[], static ujson_obj cmd_obj, static struct tst_cmd *parse_cmds(ujson_reader *reader, ujson_val *val).

3) commit (use new functionality) would have: from "lib: Add support option for .needs_cmds":

- const char *const *needs_cmds;
+ struct tst_cmd *needs_cmds;

and use of tst_check_cmd()

from "tst_run_shell.c: Add new function handle new needs_cmds"
- test.needs_cmds = parse_strarr(&reader, &val);
+ test.needs_cmds = parse_cmds(&reader, &val);

all code changes in "Update test cases use new needs_cmds"

4) "ioctl_loop01.c: Update to new .needs_cmds struct" from v3 can be separate, it just have to be after library function changed.

5) commit: modify some test to actually use some of new functionality.

If we are ok to do too many separate commits, then:

1) commit: everything from this v4 in a single commit, but separate at least "ioctl_loop01.c: Update to new .needs_cmds struct" from v3.

2) "ioctl_loop01.c: Update to new .needs_cmds struct" from v3.

3) commit: modify some test to actually use some of new functionality.

Kind regards,
Petr

Quoted 2 lines
Quoted 2 lines

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#21 lore raw

Re: [LTP] [PATCH v4 1/3] lib: Add support option for .needs_cmds

Quoted 88 lines · Petr Vorel

Hi Wei,

Quoted 22 lines
Quoted 21 lines

include/tst_test.h struct tst_fs { · line 620

Quoted 2 lines
include/tst_test.h +1 -1
-	const char *const *needs_cmds;
+	struct tst_cmd *needs_cmds;

As I wrote in all previous versions, changing struct used in struct tst_test alone without changing will break this particular commit.

See when I apply just this commit.:
https://github.com/pevik/ltp/actions/runs/18595891586
https://github.com/pevik/ltp/commits/refs/heads/wei/needs_cmds.v4.first-commit-broken/

CC lib/newlib_tests/tst_expiration_timer
tst_needs_cmds01.c:15:23: error: initialization of ‘struct tst_cmd *’ from incompatible pointer type ‘const char **’ [-Wincompatible-pointer-types]
15 | .needs_cmds = (const char *[]) {
| ^
tst_needs_cmds01.c:15:23: note: (near initialization for ‘test.needs_cmds’)

...

quotactl01.c:226:23: error: initialization of ‘struct tst_cmd *’ from incompatible pointer type ‘const char * const*’ [-Wincompatible-pointer-types]
226 | .needs_cmds = (const char *const []) {
| ^

Quoted 5 lines
Quoted 4 lines

Please you need to have this commit together with "Update test cases use new needs_cmds" commit into single commit (squash these two into a single commit).

Or am I missing something?

Thanks for your time do verify test can review.

yw :).

Quoted 1 line

The reason i split this patch to small commits is easy for review, if you

Yes, it's desired that changes are split into smaller logical parts if *possible*. Yes, this really helps readability. But if possible means at lest 1) not breaking compilation (the worst case) 2) not breaking test functionality.

Therefore it's much easier to split into more commits some new library functionality (i.e. add some library functionality and enable it in separate commits, enable tests in a separate commit)

But modifying the functionality like this (when it breaks compilation) is worse than a bit more complex commit. This is the rule in many open source projects.

Quoted 2 lines

need every commits can pass compile phase then i have to combine all commits into a single big one, is that your request?

No, that's other extreme :). There is something in between, right? You did not get me correct, therefore in v4 you not only kept broken functionality, but you also joined the part which could be separated. At least "ioctl_loop01.c: Update to new .needs_cmds struct" from v3 could have been added as a separate commit (after the main change, not before). Or am I missing something?

Unfortunately "lib: Add support option for .needs_cmds" and "Update test cases use new needs_cmds" and "tst_run_shell.c: Add new function handle new needs_cmds" needs to be in a single commit, but maybe you could add functions which implement it in a separate commits (e.g. preparation for a new change) and do the change (when it's actually used) in the last commit). I'm not sure if it's worth of a separation, maybe not (hopefully we get a feedback from others). If yes:

1) commit (lib preparation) would have: struct tst_cmd, bool tst_cmd_present(const char *cmd)

2) commit (shell loader preparation) would have: enum cmd_ids, static ujson_obj_attr cmd_attrs[], static ujson_obj cmd_obj, static struct tst_cmd *parse_cmds(ujson_reader *reader, ujson_val *val).

3) commit (use new functionality) would have: from "lib: Add support option for .needs_cmds":

include/tst_test.h +1 -1
-	const char *const *needs_cmds;
+	struct tst_cmd *needs_cmds;

and use of tst_check_cmd()

from "tst_run_shell.c: Add new function handle new needs_cmds"

(quoted diff) +1 -1
-			test.needs_cmds = parse_strarr(&reader, &val);
+			test.needs_cmds = parse_cmds(&reader, &val);

all code changes in "Update test cases use new needs_cmds"

4) "ioctl_loop01.c: Update to new .needs_cmds struct" from v3 can be separate, it just have to be after library function changed.

5) commit: modify some test to actually use some of new functionality.

Quoted 8 lines

If we are ok to do too many separate commits, then:

1) commit: everything from this v4 in a single commit, but separate at least "ioctl_loop01.c: Update to new .needs_cmds struct" from v3.

2) "ioctl_loop01.c: Update to new .needs_cmds struct" from v3.

3) commit: modify some test to actually use some of new functionality.

Thanks for such detail explaination :)
Let's wait one more day for feedback from others, i prefer second solution squash core changes into one commit without break from function level.

BTW: Absent timely feedback, I will automatically begin creating the next patch with the second solution.

Quoted 6 lines

Kind regards,
Petr

Quoted 2 lines
Quoted 2 lines

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#22 lore raw

Re: [LTP] [PATCH v4 1/3] lib: Add support option for .needs_cmds

In reply to Petr Vorel

Hi Wei,

Quoted 13 lines · Petr Vorel
Quoted 3 lines

need every commits can pass compile phase then i have to combine all commits into a single big one, is that your request?

No, that's other extreme :). There is something in between, right? You did not get me correct, therefore in v4 you not only kept broken functionality, but you also joined the part which could be separated. At least
"ioctl_loop01.c: Update to new .needs_cmds struct" from v3 could have been added
as a separate commit (after the main change, not before). Or am I missing something?

Quoted 13 lines

Unfortunately "lib: Add support option for .needs_cmds" and "Update test cases use
new needs_cmds" and "tst_run_shell.c: Add new function handle new needs_cmds" needs to be in a single commit, but maybe you could add functions
which implement it in a separate commits (e.g. preparation for a new change) and
do the change (when it's actually used) in the last commit). I'm not sure if
it's worth of a separation, maybe not (hopefully we get a feedback from others).
If yes:

Yes.

Quoted 20 lines

1) commit (lib preparation) would have: struct tst_cmd, bool tst_cmd_present(const char *cmd)

2) commit (shell loader preparation) would have: enum cmd_ids, static ujson_obj_attr cmd_attrs[], static ujson_obj cmd_obj, static struct tst_cmd *parse_cmds(ujson_reader *reader, ujson_val *val).

3) commit (use new functionality) would have: from "lib: Add support option for
.needs_cmds":

include/tst_test.h +1 -1
-       const char *const *needs_cmds;
+       struct tst_cmd *needs_cmds;

and use of tst_check_cmd()

from "tst_run_shell.c: Add new function handle new needs_cmds"

(quoted diff) +1 -1
-                       test.needs_cmds = parse_strarr(&reader, &val);
+                       test.needs_cmds = parse_cmds(&reader, &val);
Quoted 8 lines

all code changes in "Update test cases use new needs_cmds"

4) "ioctl_loop01.c: Update to new .needs_cmds struct" from v3 can be separate,
it just have to be after library function changed.

5) commit: modify some test to actually use some of new functionality.

Obviously, this is a good suggestion.

Wei, if you take a look at the git-history of LTP feature change, most patchset organizations follow this principle.

We must keep a clean code and compile pass at the same time, I have roughly gone through your patches, and they are seems not so hard to rebase.

Quoted 11 lines

If we are ok to do too many separate commits, then:

1) commit: everything from this v4 in a single commit, but separate at least
"ioctl_loop01.c: Update to new .needs_cmds struct" from v3.

2) "ioctl_loop01.c: Update to new .needs_cmds struct" from v3.

3) commit: modify some test to actually use some of new functionality.

Yes, this is also acceptable to me.

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#23 lore raw

Re: [LTP] [PATCH v4 1/3] lib: Add support option for .needs_cmds

Quoted 82 lines · Li Wang

Hi Wei,

Quoted 13 lines · Petr Vorel
Quoted 3 lines

need every commits can pass compile phase then i have to combine all commits into a single big one, is that your request?

No, that's other extreme :). There is something in between, right? You did not get me correct, therefore in v4 you not only kept broken functionality, but you also joined the part which could be separated. At least
"ioctl_loop01.c: Update to new .needs_cmds struct" from v3 could have been added
as a separate commit (after the main change, not before). Or am I missing something?

Quoted 13 lines

Unfortunately "lib: Add support option for .needs_cmds" and "Update test cases use
new needs_cmds" and "tst_run_shell.c: Add new function handle new needs_cmds" needs to be in a single commit, but maybe you could add functions
which implement it in a separate commits (e.g. preparation for a new change) and
do the change (when it's actually used) in the last commit). I'm not sure if
it's worth of a separation, maybe not (hopefully we get a feedback from others).
If yes:

Yes.

Quoted 20 lines

1) commit (lib preparation) would have: struct tst_cmd, bool tst_cmd_present(const char *cmd)

2) commit (shell loader preparation) would have: enum cmd_ids, static ujson_obj_attr cmd_attrs[], static ujson_obj cmd_obj, static struct tst_cmd *parse_cmds(ujson_reader *reader, ujson_val *val).

3) commit (use new functionality) would have: from "lib: Add support option for
.needs_cmds":

include/tst_test.h +1 -1
-       const char *const *needs_cmds;
+       struct tst_cmd *needs_cmds;

and use of tst_check_cmd()

from "tst_run_shell.c: Add new function handle new needs_cmds"

(quoted diff) +1 -1
-                       test.needs_cmds = parse_strarr(&reader, &val);
+                       test.needs_cmds = parse_cmds(&reader, &val);
Quoted 8 lines

all code changes in "Update test cases use new needs_cmds"

4) "ioctl_loop01.c: Update to new .needs_cmds struct" from v3 can be separate,
it just have to be after library function changed.

5) commit: modify some test to actually use some of new functionality.

Obviously, this is a good suggestion.

Wei, if you take a look at the git-history of LTP feature change, most patchset organizations follow this principle.

We must keep a clean code and compile pass at the same time, I have roughly gone through your patches, and they are seems not so hard to rebase.

Quoted 7 lines

If we are ok to do too many separate commits, then:

1) commit: everything from this v4 in a single commit, but separate at least
"ioctl_loop01.c: Update to new .needs_cmds struct" from v3.

Quoted 6 lines
Quoted 4 lines

2) "ioctl_loop01.c: Update to new .needs_cmds struct" from v3.

3) commit: modify some test to actually use some of new functionality.

Yes, this is also acceptable to me.

Li, Thanks for your feedback, will plan next patch.

Quoted 4 lines
Signature · 3 lines
-- 
Regards,
Li Wang
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#24 lore raw

Re: [LTP] [PATCH v4 1/3] lib: Add support option for .needs_cmds

In reply to Wei Gao via ltp

Wei,

..

include/tst_cmd.h enum tst_cmd_flags { · line 28

Quoted 10 lines
include/tst_cmd.h +10 -0
+/**
+ * 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;

Any change you would modify one of lib/newlib_tests/tst_needs_cmds*.c to use tst_cmd->optional?

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#25 lore raw

Re: [LTP] [PATCH v4 1/3] lib: Add support option for .needs_cmds

Quoted 15 lines · Petr Vorel

Wei,

..

include/tst_cmd.h enum tst_cmd_flags { · line 28

Quoted 10 lines
include/tst_cmd.h +10 -0
+/**
+ * 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;

Any change you would modify one of lib/newlib_tests/tst_needs_cmds*.c to use tst_cmd->optional?

Good idea, will add test for this new parameter.

Quoted 3 lines

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#26 lore raw

Re: [LTP] [PATCH v4 1/3] lib: Add support option for .needs_cmds

In reply to Wei Gao via ltp

Hi Wei,

...

lib/tst_cmd.c int tst_check_cmd(const char *cmd, const int brk_nosupp) · line 273

Quoted 12 lines

+++ b/lib/tst_cmd.c

lib/tst_cmd.c +6 -1
@@ -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;
+		}

You ask me how you could split changes to be compilable and yet not in a single commit. This is a completely unrelated change, it should be in a separate commit, with:

Fixes 257394e4e3 ("Filter mkfs version in tst_fs")

Kind regards,
Petr

Quoted 2 lines
lib/tst_cmd.c +0 -0
 	if (!op_token)
 		return 0;
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#27 lore raw

Re: [LTP] [PATCH v4 1/3] lib: Add support option for .needs_cmds

Quoted 20 lines · Petr Vorel

Hi Wei,

...

lib/tst_cmd.c int tst_check_cmd(const char *cmd, const int brk_nosupp) · line 273

Quoted 12 lines

+++ b/lib/tst_cmd.c

lib/tst_cmd.c +6 -1
@@ -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;
+		}

You ask me how you could split changes to be compilable and yet not in a single commit. This is a completely unrelated change, it should be in a separate commit, with:

Quoted 1 line
Fixes 257394e4e3 ("Filter mkfs version in tst_fs")

Thanks, will use separte commit next patch.

Quoted 6 lines

Kind regards,
Petr

Quoted 2 lines
lib/tst_cmd.c +0 -0
 	if (!op_token)
 		return 0;
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#28 lore raw

PATCH v4 2/3 Update test cases use new needs_cmds

In reply to Wei Gao via ltp
---
 lib/newlib_tests/tst_needs_cmds01.c           | 16 +++++------
 lib/newlib_tests/tst_needs_cmds02.c           |  6 ++--
 lib/newlib_tests/tst_needs_cmds03.c           |  6 ++--
 lib/newlib_tests/tst_needs_cmds04.c           |  6 ++--
 lib/newlib_tests/tst_needs_cmds05.c           |  6 ++--
 lib/newlib_tests/tst_needs_cmds06.c           |  6 ++--
 lib/newlib_tests/tst_needs_cmds07.c           |  6 ++--
 lib/newlib_tests/tst_needs_cmds08.c           |  8 +++---
 testcases/kernel/device-drivers/zram/zram03.c |  8 +++---
 testcases/kernel/fs/squashfs/squashfs01.c     |  6 ++--
 testcases/kernel/security/aslr/aslr01.c       |  6 ++--
 testcases/kernel/syscalls/add_key/add_key05.c | 10 +++----
 .../kernel/syscalls/fanotify/fanotify22.c     |  6 ++--
 testcases/kernel/syscalls/ioctl/ioctl09.c     |  6 ++--
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 28 +++++++++----------
 testcases/kernel/syscalls/madvise/madvise11.c |  8 +++---
 .../kernel/syscalls/quotactl/quotactl01.c     |  6 ++--
 .../kernel/syscalls/quotactl/quotactl04.c     |  6 ++--
 .../kernel/syscalls/quotactl/quotactl06.c     |  6 ++--
 .../kernel/syscalls/quotactl/quotactl08.c     |  6 ++--
 .../kernel/syscalls/quotactl/quotactl09.c     |  6 ++--
 testcases/kernel/syscalls/statx/statx05.c     |  8 +++---
 testcases/kernel/syscalls/statx/statx07.c     |  6 ++--
 testcases/kernel/syscalls/statx/statx09.c     |  6 ++--
 24 files changed, 94 insertions(+), 94 deletions(-)
lib/newlib_tests/tst_needs_cmds01.c +8 -8
index 777c69505..592456da6 100644
@@ -12,13 +12,13 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4",
-		"mkfs.ext4 >= 1.0.0",
-		"mkfs.ext4 <= 2.0.0",
-		"mkfs.ext4 != 2.0.0",
-		"mkfs.ext4 > 1.0.0",
-		"mkfs.ext4 < 2.0.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4"},
+		{.cmd = "mkfs.ext4 >= 1.0.0"},
+		{.cmd = "mkfs.ext4 <= 2.0.0"},
+		{.cmd = "mkfs.ext4 != 2.0.0"},
+		{.cmd = "mkfs.ext4 > 1.0.0"},
+		{.cmd = "mkfs.ext4 < 2.0.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds02.c +3 -3
index 455a275ea..f1d6105c2 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext45 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext45 >= 1.43.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds03.c +3 -3
index bdc1cdf6a..ac5f368e2 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 ! 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 ! 1.43.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds04.c +3 -3
index de10b8f3e..2aea51772 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds05.c +3 -3
index c3b2b3b9a..969d4e2f5 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43.0-1",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43.0-1"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds06.c +3 -3
index 40b1cf09c..91470ccf9 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43.0 2",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43.0 2"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds07.c +3 -3
index d0b4ce2ff..371bbdc1b 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext45",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext45"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds08.c +4 -4
index 38df2ef6d..412f9f293 100644
@@ -18,9 +18,9 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.xfs",
-		"mkfs.xfs >= 4.20.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.xfs"},
+		{.cmd = "mkfs.xfs >= 4.20.0"},
+		{}
 	}
 };
testcases/kernel/device-drivers/zram/zram03.c +4 -4
index 8cf26de4c..4dcf23b4e 100644
@@ -240,9 +240,9 @@ static struct tst_test test = {
 		"zram",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"modprobe",
-		"rmmod",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "modprobe"},
+		{.cmd = "rmmod"},
+		{}
 	}
 };
testcases/kernel/fs/squashfs/squashfs01.c +3 -3
index fbcb76582..7a548d62c 100644
@@ -102,9 +102,9 @@ static struct tst_test test = {
 	.needs_root = 1,
 	.needs_device = 1,
 	.dev_min_size = 1,
-	.needs_cmds = (const char *const []) {
-		"mksquashfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mksquashfs"},
+		{}
 	},
 	.needs_drivers = (const char *const []) {
 		"squashfs",
testcases/kernel/security/aslr/aslr01.c +3 -3
index 6a396e29d..e6feb2d3c 100644
@@ -277,8 +277,8 @@ static struct tst_test test = {
 		"CONFIG_HAVE_ARCH_MMAP_RND_BITS=y",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"ldd",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "ldd"},
+		{}
 	},
 };
testcases/kernel/syscalls/add_key/add_key05.c +5 -5
index c9a2f840e..b51a84b97 100644
@@ -231,11 +231,11 @@ static struct tst_test test = {
 		{&user_buf, .size = 64},
 		{}
 	},
-	.needs_cmds = (const char *const []) {
-		"useradd",
-		"userdel",
-		"groupdel",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "useradd"},
+		{.cmd = "userdel"},
+		{.cmd = "groupdel"},
+		{}
 	},
 	.tags = (const struct tst_tag[]) {
 		{"linux-git", "a08bf91ce28"},
testcases/kernel/syscalls/fanotify/fanotify22.c +3 -3
index 357e74dbc..6578474a7 100644
@@ -327,9 +327,9 @@ static struct tst_test test = {
 		{"linux-git", "76486b104168"},
 		{}
 	},
-	.needs_cmds = (const char *[]) {
-		"debugfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "debugfs"},
+		{}
 	}
 };
 
testcases/kernel/syscalls/ioctl/ioctl09.c +3 -3
index f86355f2c..1bd60fc84 100644
@@ -108,9 +108,9 @@ static struct tst_test test = {
 		"loop",
 		NULL
 	},
-	.needs_cmds = (const char *const []) {
-		"parted",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "parted"},
+		{}
 	},
 	.needs_tmpdir = 1,
 };
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +14 -14
index cb1b506d2..fe62e7e5a 100644
@@ -78,21 +78,7 @@ static void check_loop_value(int set_flag, int get_flag, int autoclear_field)
 
 static void verify_ioctl_loop(void)
 {
-	int ret;
-	const char *const cmd_parted[] = {"parted", "-s", dev_path, "mklabel", "msdos", "mkpart",
-					"primary", "ext4", "1M", "10M", NULL};
-
-	tst_fill_file("test.img", 0, 1024 * 1024, 10);
 	tst_attach_device(dev_path, "test.img");
-
-	ret = tst_cmd(cmd_parted, NULL, NULL, TST_CMD_PASS_RETVAL);
-	if (!ret)
-		parted_sup = 1;
-	else if (ret == 255)
-		tst_res(TCONF, "parted binary not installed or failed");
-	else
-		tst_res(TCONF, "parted exited with %i", ret);
-
 	attach_flag = 1;
 
 	TST_ASSERT_INT(partscan_path, 0);
@@ -112,10 +98,20 @@ static void verify_ioctl_loop(void)
 
 static void setup(void)
 {
+	parted_sup = tst_cmd_present("parted");
+
+	const char *const cmd_parted[] = {"parted", "-s", "test.img", "mklabel", "msdos", "mkpart",
+	                                  "primary", "ext4", "1M", "10M", NULL};
+
 	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
 	if (dev_num < 0)
 		tst_brk(TBROK, "Failed to find free loop device");
 
+	tst_fill_file("test.img", 0, 1024 * 1024, 10);
+
+	if (parted_sup)
+		SAFE_CMD(cmd_parted, NULL, NULL);
+
 	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
 	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);
 	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
@@ -148,5 +144,9 @@ static struct tst_test test = {
 		{"linux-git", "6ac92fb5cdff"},
 		{}
 	},
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "parted", .optional = 1},
+		{}
+	},
 	.needs_tmpdir = 1,
 };
testcases/kernel/syscalls/madvise/madvise11.c +4 -4
index 422589f5b..73d2d4294 100644
@@ -421,10 +421,10 @@ static struct tst_test test = {
 		HW_MODULE,
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"modprobe",
-		"rmmod",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "modprobe"},
+		{.cmd = "rmmod"},
+		{}
 	},
 	.needs_kconfigs = (const char *[]) {
 		"CONFIG_MEMORY_FAILURE=y",
testcases/kernel/syscalls/quotactl/quotactl01.c +3 -3
index 9dcf74ceb..3091ac5bf 100644
@@ -223,9 +223,9 @@ static struct tst_test test = {
 		{}
 	},
 	.mntpoint = MNTPOINT,
-	.needs_cmds = (const char *const []) {
-		"quotacheck",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "quotacheck"},
+		{}
 	},
 	.setup = setup,
 	.cleanup = cleanup,
testcases/kernel/syscalls/quotactl/quotactl04.c +3 -3
index d2d7b3f3e..a70903330 100644
@@ -165,8 +165,8 @@ static struct tst_test test = {
 	},
 	.mntpoint = MNTPOINT,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/quotactl/quotactl06.c +3 -3
index 110a3aa9b..333f5720d 100644
@@ -226,9 +226,9 @@ static struct tst_test test = {
 	},
 	.mntpoint = MNTPOINT,
 	.mount_device = 1,
-	.needs_cmds = (const char *const []) {
-		"quotacheck",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "quotacheck"},
+		{}
 	},
 	.needs_root = 1,
 	.test_variants = QUOTACTL_FMT_VARIANTS,
testcases/kernel/syscalls/quotactl/quotactl08.c +3 -3
index 63087345f..ce7a8b008 100644
@@ -220,8 +220,8 @@ static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/quotactl/quotactl09.c +3 -3
index 673666347..fd6629c0c 100644
@@ -185,8 +185,8 @@ static struct tst_test test = {
 	.mount_device = 1,
 	.needs_root = 1,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx05.c +4 -4
index 2a4603226..07b5e7b94 100644
@@ -123,9 +123,9 @@ static struct tst_test test = {
 		{.type = "ext4"},
 		{}
 	},
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		"e4crypt",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{.cmd = "e4crypt"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx07.c +3 -3
index bab64591f..755644cf9 100644
@@ -171,8 +171,8 @@ static struct tst_test test = {
 		{}
 	},
 	.needs_root = 1,
-	.needs_cmds = (const char *[]) {
-		"exportfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "exportfs"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx09.c +3 -3
index ee4be4250..8838d5450 100644
@@ -162,8 +162,8 @@ static struct tst_test test = {
 		"CONFIG_FS_VERITY",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.45.2",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.45.2"},
+		{}
 	}
 };
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#29 lore raw

PATCH v4 3/3 tst_run_shell.c: Add new function handle new needs_cmds

In reply to Wei Gao via ltp
---
 testcases/lib/tst_run_shell.c | 61 ++++++++++++++++++++++++++++++++++-
 1 file changed, 60 insertions(+), 1 deletion(-)
testcases/lib/tst_run_shell.c +60 -1
index 7a446e004..a6e0d1d29 100644
@@ -175,6 +175,23 @@ static ujson_obj fs_obj = {
 	.attr_cnt = UJSON_ARRAY_SIZE(fs_attrs),
 };
 
+enum cmd_ids {
+	CMD,
+	OPTIONAL,
+	PRESENT,
+};
+
+static ujson_obj_attr cmd_attrs[] = {
+	UJSON_OBJ_ATTR_IDX(CMD, "cmd", UJSON_STR),
+	UJSON_OBJ_ATTR_IDX(OPTIONAL, "optional", UJSON_INT),
+	UJSON_OBJ_ATTR_IDX(PRESENT, "present", UJSON_INT),
+};
+
+static ujson_obj cmd_obj = {
+	.attrs = cmd_attrs,
+	.attr_cnt = UJSON_ARRAY_SIZE(cmd_attrs),
+};
+
 static int parse_mnt_flags(ujson_reader *reader, ujson_val *val)
 {
 	int ret = 0;
@@ -252,6 +269,48 @@ static struct tst_fs *parse_filesystems(ujson_reader *reader, ujson_val *val)
 	return ret;
 }
 
+static struct tst_cmd *parse_cmds(ujson_reader *reader, ujson_val *val)
+{
+	unsigned int i = 0, cnt = 0;
+	struct tst_cmd *ret;
+
+	ujson_reader_state state = ujson_reader_state_save(reader);
+
+	UJSON_ARR_FOREACH(reader, val) {
+		if (val->type != UJSON_ARR) {
+			ujson_err(reader, "Expected array!");
+			return NULL;
+		}
+		ujson_arr_skip(reader);
+		cnt++;
+	}
+
+	ujson_reader_state_load(reader, state);
+
+	ret = SAFE_MALLOC(sizeof(struct tst_cmd) * (cnt + 1));
+	memset(&ret[cnt], 0, sizeof(ret[cnt]));
+
+	UJSON_ARR_FOREACH(reader, val) {
+		UJSON_OBJ_FOREACH_FILTER(reader, val, &cmd_obj, ujson_empty_obj) {
+			switch ((enum cmd_ids)val->idx) {
+			case CMD:
+				ret[i].cmd = strdup(val->val_str);
+			break;
+			case OPTIONAL:
+				ret[i].optional = val->val_int;
+			break;
+			case MKFS_VER:
+				ret[i].present = val->val_int;
+			break;
+			}
+		}
+
+		i++;
+	}
+
+	return ret;
+}
+
 static struct tst_tag *parse_tags(ujson_reader *reader, ujson_val *val)
 {
 	unsigned int i = 0, cnt = 0;
@@ -440,7 +499,7 @@ static void parse_metadata(void)
 				ujson_err(&reader, "ABI bits must be 32 or 64");
 		break;
 		case NEEDS_CMDS:
-			test.needs_cmds = parse_strarr(&reader, &val);
+			test.needs_cmds = parse_cmds(&reader, &val);
 		break;
 		case NEEDS_DEVFS:
 			test.needs_devfs = val.val_bool;
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#30 lore raw

Re: [LTP] [PATCH v4 3/3] tst_run_shell.c: Add new function handle new needs_cmds

Hi Wei,

testcases/lib/tst_run_shell.c static struct tst_fs *parse_filesystems(ujson_reader *reader, ujson_val *val) · line 303

Quoted 11 lines
testcases/lib/tst_run_shell.c +11 -0
+	UJSON_ARR_FOREACH(reader, val) {
+		UJSON_OBJ_FOREACH_FILTER(reader, val, &cmd_obj, ujson_empty_obj) {
+			switch ((enum cmd_ids)val->idx) {
+			case CMD:
+				ret[i].cmd = strdup(val->val_str);
+			break;
+			case OPTIONAL:
+				ret[i].optional = val->val_int;
+			break;
+			case MKFS_VER:
+				ret[i].present = val->val_int;

My compiler shows 2 warnings:

tst_run_shell.c:302:25: warning: case value ‘3’ not in enumerated type ‘enum cmd_ids’ [-Wswitch]

  302 |                         case MKFS_VER:
      |                         ^~~~

=> Indeed, MKFS_VER is part of enum fs_ids, it should not be here.

Another warning:

tst_run_shell.c: In function ‘parse_cmds’:
tst_run_shell.c:295:25: warning: enumeration value ‘PRESENT’ not handled in switch [-Wswitch]

  295 |                         switch ((enum cmd_ids)val->idx) {
      |                         ^~~~~~

=> Either it MKFS_VER should have PRESENT, or, because struct tst_cmd->present should not be set in the test, but by C API, we might want to call ujson_err()?

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#31 lore raw

Re: [LTP] [PATCH v4 3/3] tst_run_shell.c: Add new function handle new needs_cmds

In reply to Wei Gao via ltp

Hi Wei,

Quoted 2 lines
 testcases/lib/tst_run_shell.c | 61 ++++++++++++++++++++++++++++++++++-
 1 file changed, 60 insertions(+), 1 deletion(-)

Also here, any change you would add a test for a new feature (as a separate commit in e.g. testcases/lib/tests/shell_loader_cmd.sh).

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#32 lore raw

Re: [LTP] [PATCH v4 3/3] tst_run_shell.c: Add new function handle new needs_cmds

Quoted 7 lines · Petr Vorel

Hi Wei,

Quoted 2 lines
 testcases/lib/tst_run_shell.c | 61 ++++++++++++++++++++++++++++++++++-
 1 file changed, 60 insertions(+), 1 deletion(-)

Also here, any change you would add a test for a new feature (as a separate commit in e.g. testcases/lib/tests/shell_loader_cmd.sh).

Will added in separate commit in next patch.

Quoted 3 lines

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#33 lore raw

v4 cover new cmd support option for needs_cmds

In reply to Wei Gao via ltp

v3->v4:
- Reorganize the patches so that each one compiles successfully
- Add new test case for needs_cmds

Wei Gao (4):
tst_cmd.c: Check brk_nosupp when tst_get_path failed
lib: Add support option for .needs_cmds ioctl_loop01.c: Add new support .needs_cmds shell_loader_cmd.sh: New test case check needs_cmds

 include/tst_cmd.h                             | 13 ++++
 include/tst_test.h                            | 16 ++++-
 lib/newlib_tests/tst_needs_cmds01.c           | 16 ++---
 lib/newlib_tests/tst_needs_cmds02.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds03.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds04.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds05.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds06.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds07.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds08.c           |  8 +--
 lib/tst_cmd.c                                 | 10 ++-
 lib/tst_test.c                                | 22 +++++--
 testcases/kernel/device-drivers/zram/zram03.c |  8 +--
 testcases/kernel/fs/squashfs/squashfs01.c     |  6 +-
 testcases/kernel/security/aslr/aslr01.c       |  6 +-
 testcases/kernel/syscalls/add_key/add_key05.c | 10 +--
 .../kernel/syscalls/fanotify/fanotify22.c     |  6 +-
 testcases/kernel/syscalls/ioctl/ioctl09.c     |  6 +-
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 28 ++++-----
 testcases/kernel/syscalls/madvise/madvise11.c |  8 +--
 .../kernel/syscalls/quotactl/quotactl01.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl04.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl06.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl08.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl09.c     |  6 +-
 testcases/kernel/syscalls/statx/statx05.c     |  8 +--
 testcases/kernel/syscalls/statx/statx07.c     |  6 +-
 testcases/kernel/syscalls/statx/statx09.c     |  6 +-
 testcases/lib/run_tests.sh                    |  1 +
 testcases/lib/tests/shell_loader_cmd.sh       | 24 ++++++++
 testcases/lib/tst_run_shell.c                 | 61 ++++++++++++++++++-
 31 files changed, 232 insertions(+), 103 deletions(-)
 create mode 100755 testcases/lib/tests/shell_loader_cmd.sh
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#34 lore raw

PATCH v4 1/4 tst_cmd.c: Check brk_nosupp when tst_get_path failed

Fixes 257394e4e3 ("Filter mkfs version in tst_fs")
---
 lib/tst_cmd.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
lib/tst_cmd.c +8 -2
index 2faf7c743..716029c9b 100644
@@ -265,8 +265,14 @@ bool tst_check_cmd(const char *cmd, const int brk_nosupp)
 	version_token = strtok_r(NULL, " ", &next);
 	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 (tst_get_path(cmd_token, path, sizeof(path))) {
+		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 true;
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#35 lore raw

Re: [LTP] [PATCH v4 1/4] tst_cmd.c: Check brk_nosupp when tst_get_path failed

Hi Wei,

Quoted 12 lines
Fixes 257394e4e3 ("Filter mkfs version in tst_fs")
---
 lib/tst_cmd.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
lib/tst_cmd.c +0 -0
index 2faf7c743..716029c9b 100644
@@ -265,8 +265,14 @@ bool tst_check_cmd(const char *cmd, const int brk_nosupp)
 	version_token = strtok_r(NULL, " ", &next);

lib/tst_cmd.c bool tst_check_cmd(const char *cmd, const int brk_nosupp) · line 273

Quoted 10 lines
lib/tst_cmd.c +6 -2
 	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 (tst_get_path(cmd_token, path, sizeof(path))) {
+		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;

Function now returns 'bool', therefore 'true' or 'false' should be returned. IMHO this should be false, therefore I resent it:
https://lore.kernel.org/ltp/[email protected]/

and set this patch "changes requested".

Also, I found a fix in the change I introduced, therefore send a patch which fixes it:
https://lore.kernel.org/ltp/[email protected]/

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#36 lore raw

PATCH v4 2/4 lib: Add support option for .needs_cmds

In reply to Wei Gao via ltp
---
 include/tst_cmd.h                             | 13 ++++
 include/tst_test.h                            | 16 ++++-
 lib/newlib_tests/tst_needs_cmds01.c           | 16 ++---
 lib/newlib_tests/tst_needs_cmds02.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds03.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds04.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds05.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds06.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds07.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds08.c           |  8 +--
 lib/tst_test.c                                | 22 +++++--
 testcases/kernel/device-drivers/zram/zram03.c |  8 +--
 testcases/kernel/fs/squashfs/squashfs01.c     |  6 +-
 testcases/kernel/security/aslr/aslr01.c       |  6 +-
 testcases/kernel/syscalls/add_key/add_key05.c | 10 +--
 .../kernel/syscalls/fanotify/fanotify22.c     |  6 +-
 testcases/kernel/syscalls/ioctl/ioctl09.c     |  6 +-
 testcases/kernel/syscalls/madvise/madvise11.c |  8 +--
 .../kernel/syscalls/quotactl/quotactl01.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl04.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl06.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl08.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl09.c     |  6 +-
 testcases/kernel/syscalls/statx/statx05.c     |  8 +--
 testcases/kernel/syscalls/statx/statx07.c     |  6 +-
 testcases/kernel/syscalls/statx/statx09.c     |  6 +-
 testcases/lib/tst_run_shell.c                 | 61 ++++++++++++++++++-
 27 files changed, 185 insertions(+), 87 deletions(-)
include/tst_cmd.h +13 -0
index cf6382955..2203241c5 100644
@@ -19,6 +19,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.
  *
include/tst_test.h +14 -2
index 9c21c1728..9305cf39d 100644
@@ -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
  */
lib/newlib_tests/tst_needs_cmds01.c +8 -8
index 777c69505..6ab1145d1 100644
@@ -12,13 +12,13 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4",
-		"mkfs.ext4 >= 1.0.0",
-		"mkfs.ext4 <= 2.0.0",
-		"mkfs.ext4 != 2.0.0",
-		"mkfs.ext4 > 1.0.0",
-		"mkfs.ext4 < 2.0.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4", .optional = 1},
+		{.cmd = "mkfs.ext4 >= 1.0.0", .optional = 0},
+		{.cmd = "mkfs.ext4 <= 2.0.0"},
+		{.cmd = "mkfs.ext4 != 2.0.0"},
+		{.cmd = "mkfs.ext4 > 1.0.0"},
+		{.cmd = "mkfs.ext4 < 2.0.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds02.c +3 -3
index 455a275ea..f1d6105c2 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext45 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext45 >= 1.43.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds03.c +3 -3
index bdc1cdf6a..ac5f368e2 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 ! 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 ! 1.43.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds04.c +3 -3
index de10b8f3e..2aea51772 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds05.c +3 -3
index c3b2b3b9a..969d4e2f5 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43.0-1",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43.0-1"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds06.c +3 -3
index 40b1cf09c..91470ccf9 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43.0 2",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43.0 2"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds07.c +3 -3
index d0b4ce2ff..371bbdc1b 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext45",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext45"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds08.c +4 -4
index 38df2ef6d..412f9f293 100644
@@ -18,9 +18,9 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.xfs",
-		"mkfs.xfs >= 4.20.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.xfs"},
+		{.cmd = "mkfs.xfs >= 4.20.0"},
+		{}
 	}
 };
lib/tst_test.c +18 -4
index da5314c50..67553f65d 100644
@@ -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) ? 1 : 0;
+			pcmd++;
+		}
 	}
 
 	if (tst_test->needs_drivers) {
testcases/kernel/device-drivers/zram/zram03.c +4 -4
index 8cf26de4c..4dcf23b4e 100644
@@ -240,9 +240,9 @@ static struct tst_test test = {
 		"zram",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"modprobe",
-		"rmmod",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "modprobe"},
+		{.cmd = "rmmod"},
+		{}
 	}
 };
testcases/kernel/fs/squashfs/squashfs01.c +3 -3
index fbcb76582..7a548d62c 100644
@@ -102,9 +102,9 @@ static struct tst_test test = {
 	.needs_root = 1,
 	.needs_device = 1,
 	.dev_min_size = 1,
-	.needs_cmds = (const char *const []) {
-		"mksquashfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mksquashfs"},
+		{}
 	},
 	.needs_drivers = (const char *const []) {
 		"squashfs",
testcases/kernel/security/aslr/aslr01.c +3 -3
index 6a396e29d..e6feb2d3c 100644
@@ -277,8 +277,8 @@ static struct tst_test test = {
 		"CONFIG_HAVE_ARCH_MMAP_RND_BITS=y",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"ldd",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "ldd"},
+		{}
 	},
 };
testcases/kernel/syscalls/add_key/add_key05.c +5 -5
index c9a2f840e..b51a84b97 100644
@@ -231,11 +231,11 @@ static struct tst_test test = {
 		{&user_buf, .size = 64},
 		{}
 	},
-	.needs_cmds = (const char *const []) {
-		"useradd",
-		"userdel",
-		"groupdel",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "useradd"},
+		{.cmd = "userdel"},
+		{.cmd = "groupdel"},
+		{}
 	},
 	.tags = (const struct tst_tag[]) {
 		{"linux-git", "a08bf91ce28"},
testcases/kernel/syscalls/fanotify/fanotify22.c +3 -3
index 357e74dbc..6578474a7 100644
@@ -327,9 +327,9 @@ static struct tst_test test = {
 		{"linux-git", "76486b104168"},
 		{}
 	},
-	.needs_cmds = (const char *[]) {
-		"debugfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "debugfs"},
+		{}
 	}
 };
 
testcases/kernel/syscalls/ioctl/ioctl09.c +3 -3
index 262d6fcab..737aee680 100644
@@ -108,9 +108,9 @@ static struct tst_test test = {
 		"loop",
 		NULL
 	},
-	.needs_cmds = (const char *const []) {
-		"parted",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "parted"},
+		{}
 	},
 	.needs_tmpdir = 1,
 };
testcases/kernel/syscalls/madvise/madvise11.c +4 -4
index 422589f5b..73d2d4294 100644
@@ -421,10 +421,10 @@ static struct tst_test test = {
 		HW_MODULE,
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"modprobe",
-		"rmmod",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "modprobe"},
+		{.cmd = "rmmod"},
+		{}
 	},
 	.needs_kconfigs = (const char *[]) {
 		"CONFIG_MEMORY_FAILURE=y",
testcases/kernel/syscalls/quotactl/quotactl01.c +3 -3
index 9dcf74ceb..3091ac5bf 100644
@@ -223,9 +223,9 @@ static struct tst_test test = {
 		{}
 	},
 	.mntpoint = MNTPOINT,
-	.needs_cmds = (const char *const []) {
-		"quotacheck",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "quotacheck"},
+		{}
 	},
 	.setup = setup,
 	.cleanup = cleanup,
testcases/kernel/syscalls/quotactl/quotactl04.c +3 -3
index d2d7b3f3e..a70903330 100644
@@ -165,8 +165,8 @@ static struct tst_test test = {
 	},
 	.mntpoint = MNTPOINT,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/quotactl/quotactl06.c +3 -3
index 110a3aa9b..333f5720d 100644
@@ -226,9 +226,9 @@ static struct tst_test test = {
 	},
 	.mntpoint = MNTPOINT,
 	.mount_device = 1,
-	.needs_cmds = (const char *const []) {
-		"quotacheck",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "quotacheck"},
+		{}
 	},
 	.needs_root = 1,
 	.test_variants = QUOTACTL_FMT_VARIANTS,
testcases/kernel/syscalls/quotactl/quotactl08.c +3 -3
index 63087345f..ce7a8b008 100644
@@ -220,8 +220,8 @@ static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/quotactl/quotactl09.c +3 -3
index 673666347..fd6629c0c 100644
@@ -185,8 +185,8 @@ static struct tst_test test = {
 	.mount_device = 1,
 	.needs_root = 1,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx05.c +4 -4
index 2a4603226..07b5e7b94 100644
@@ -123,9 +123,9 @@ static struct tst_test test = {
 		{.type = "ext4"},
 		{}
 	},
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		"e4crypt",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{.cmd = "e4crypt"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx07.c +3 -3
index bab64591f..755644cf9 100644
@@ -171,8 +171,8 @@ static struct tst_test test = {
 		{}
 	},
 	.needs_root = 1,
-	.needs_cmds = (const char *[]) {
-		"exportfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "exportfs"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx09.c +3 -3
index ee4be4250..8838d5450 100644
@@ -162,8 +162,8 @@ static struct tst_test test = {
 		"CONFIG_FS_VERITY",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.45.2",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.45.2"},
+		{}
 	}
 };
testcases/lib/tst_run_shell.c +60 -1

79 lines not on this page · raw message

Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#37 lore raw

PATCH v4 3/4 ioctl_loop01.c: Add new support .needs_cmds

In reply to Wei Gao via ltp
---
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 28 +++++++++----------
 1 file changed, 14 insertions(+), 14 deletions(-)
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +14 -14
index 4ecc93b1e..dd5097f5f 100644
@@ -78,21 +78,7 @@ static void check_loop_value(int set_flag, int get_flag, int autoclear_field)
 
 static void verify_ioctl_loop(void)
 {
-	int ret;
-	const char *const cmd_parted[] = {"parted", "-s", dev_path, "mklabel", "msdos", "mkpart",
-					"primary", "ext4", "1M", "10M", NULL};
-
-	tst_fill_file("test.img", 0, 1024 * 1024, 10);
 	tst_attach_device(dev_path, "test.img");
-
-	ret = tst_cmd(cmd_parted, NULL, NULL, TST_CMD_PASS_RETVAL);
-	if (!ret)
-		parted_sup = 1;
-	else if (ret == 255)
-		tst_res(TCONF, "parted binary not installed or failed");
-	else
-		tst_res(TCONF, "parted exited with %i", ret);
-
 	attach_flag = 1;
 
 	TST_ASSERT_INT(partscan_path, 0);
@@ -113,10 +99,20 @@ static void verify_ioctl_loop(void)
 
 static void setup(void)
 {
+	parted_sup = tst_cmd_present("parted");
+
+	const char *const cmd_parted[] = {"parted", "-s", "test.img", "mklabel", "msdos", "mkpart",
+	                                  "primary", "ext4", "1M", "10M", NULL};
+
 	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
 	if (dev_num < 0)
 		tst_brk(TBROK, "Failed to find free loop device");
 
+	tst_fill_file("test.img", 0, 1024 * 1024, 10);
+
+	if (parted_sup)
+		SAFE_CMD(cmd_parted, NULL, NULL);
+
 	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
 	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);
 	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
@@ -148,5 +144,9 @@ static struct tst_test test = {
 		{"linux-git", "6ac92fb5cdff"},
 		{}
 	},
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "parted", .optional = 1},
+		{}
+	},
 	.needs_tmpdir = 1,
 };
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#38 lore raw

Re: [LTP] [PATCH v4 3/4] ioctl_loop01.c: Add new support .needs_cmds

Hi Wei,

...

Quoted 9 lines

+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c

testcases/kernel/syscalls/ioctl/ioctl_loop01.c +0 -4
@@ -78,21 +78,7 @@ static void check_loop_value(int set_flag, int get_flag, int autoclear_field)
 
 static void verify_ioctl_loop(void)
 {
-	int ret;
-	const char *const cmd_parted[] = {"parted", "-s", dev_path, "mklabel", "msdos", "mkpart",
-					"primary", "ext4", "1M", "10M", NULL};
-

...

testcases/kernel/syscalls/ioctl/ioctl_loop01.c static void verify_ioctl_loop(void) · line 106

Quoted 7 lines

static void setup(void)
{

testcases/kernel/syscalls/ioctl/ioctl_loop01.c +5 -0
+	parted_sup = tst_cmd_present("parted");
+
+	const char *const cmd_parted[] = {"parted", "-s", "test.img", "mklabel", "msdos", "mkpart",
+	                                  "primary", "ext4", "1M", "10M", NULL};
+

dev_path => "test.img". With this you're effectively reverting your own fix from ba605cc955 ("ioctl_loop01.c: Use proper device for partitioning"). Why?

'parted' with dev_path immediately fails (haven't found why). But with your patch applied I'm able to trigger again the problem "TBROK: mkfs.vfat failed with exit code 1".

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#39 lore raw

Re: [LTP] [PATCH v4 3/4] ioctl_loop01.c: Add new support .needs_cmds

Quoted 13 lines · Petr Vorel

Hi Wei,

...

Quoted 9 lines

+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c

testcases/kernel/syscalls/ioctl/ioctl_loop01.c +0 -4
@@ -78,21 +78,7 @@ static void check_loop_value(int set_flag, int get_flag, int autoclear_field)
 
 static void verify_ioctl_loop(void)
 {
-	int ret;
-	const char *const cmd_parted[] = {"parted", "-s", dev_path, "mklabel", "msdos", "mkpart",
-					"primary", "ext4", "1M", "10M", NULL};
-

...

Quoted 11 lines

testcases/kernel/syscalls/ioctl/ioctl_loop01.c static void verify_ioctl_loop(void) · line 106

Quoted 7 lines

static void setup(void)
{

testcases/kernel/syscalls/ioctl/ioctl_loop01.c +5 -0
+	parted_sup = tst_cmd_present("parted");
+
+	const char *const cmd_parted[] = {"parted", "-s", "test.img", "mklabel", "msdos", "mkpart",
+	                                  "primary", "ext4", "1M", "10M", NULL};
+

dev_path => "test.img". With this you're effectively reverting your own fix from ba605cc955 ("ioctl_loop01.c: Use proper device for partitioning"). Why?

Sorry, my mistake.
The root cause is that patchv2 was created before the date associated with commit ba605cc955, so change base patchv2 without rebase trigger this update.
patchv2: https://patchwork.ozlabs.org/project/ltp/patch/[email protected]/

Quoted 3 lines

'parted' with dev_path immediately fails (haven't found why). But with your patch applied I'm able to trigger again the problem "TBROK: mkfs.vfat failed with exit code 1".

Because missing tst_attach_device for /loop/devX before execute "parted", i will fix that in next version. tst_attach_device(dev_path, "test.img");

Quoted 3 lines

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#40 lore raw

PATCH v4 4/4 shell_loader_cmd.sh: New test case check needs_cmds

In reply to Wei Gao via ltp
---
 testcases/lib/run_tests.sh              |  1 +
 testcases/lib/tests/shell_loader_cmd.sh | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+)
 create mode 100755 testcases/lib/tests/shell_loader_cmd.sh
testcases/lib/run_tests.sh +1 -0
index 5c309bbeb..24ac81a44 100755
@@ -6,6 +6,7 @@ shell_loader.sh
 shell_loader_all_filesystems.sh
 shell_loader_c_child.sh
 shell_loader_filesystems.sh
+shell_loader_cmd.sh
 shell_loader_kconfigs.sh
 shell_loader_supported_archs.sh
 shell_loader_tcnt.sh
testcases/lib/tests/shell_loader_cmd.sh new file +24 -0
new file mode 100755
index 000000000..a05562089
@@ -0,0 +1,24 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2025 Wei Gao <[email protected]>
+#
+# ---
+# env
+# {
+#  "needs_cmds": [
+#   {
+#    "cmd": "ls",
+#    "optional": 1
+#   }
+#  ]
+# }
+# ---
+
+. tst_loader.sh
+
+tst_test()
+{
+	tst_res TPASS "We are running with needs_cmds"
+}
+
+. tst_run.sh
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v4 4/4
#41 lore raw

Re: [LTP] [PATCH v4 4/4] shell_loader_cmd.sh: New test case check needs_cmds

Hi Wei,

Quoted 5 lines
testcases/lib/tests/shell_loader_cmd.sh new file +2 -0
@@ -0,0 +1,24 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later

testcases/lib/tests/shell_loader_cmd.sh line 12

Quoted 10 lines
testcases/lib/tests/shell_loader_cmd.sh new file +10 -0
+# Copyright (c) 2025 Wei Gao <[email protected]>
+#
+# ---
+# env
+# {
+#  "needs_cmds": [
+#   {
+#    "cmd": "ls",
+#    "optional": 1
+#   }

Ideally we would also some test with an expression with version on "cmd", e.g. "mkfs.ext4 >= 1.0.0" to make sure it's correctly handled. I know it's just passing to the C library, but just for sure.

Kind regards,
Petr

Quoted 10 lines
testcases/lib/tests/shell_loader_cmd.sh new file +10 -0
+#  ]
+# }
+# ---
+
+. tst_loader.sh
+
+tst_test()
+{
+	tst_res TPASS "We are running with needs_cmds"
+}
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#42 lore raw

v5 cover new cmd support option for needs_cmds

In reply to Wei Gao via ltp

v4->v5:
1.Remove https://patchwork.ozlabs.org/project/ltp/patch/[email protected]/
since already rewritten by Petr's another patch.
2.Fix ioctl_loop01.c rebase issue.
3.Add expression check such as "mkfs.ext4 >= 1.0.0" into test case.

Wei Gao (3):
lib: Add support option for .needs_cmds ioctl_loop01.c: Add new support .needs_cmds shell_loader_cmd.sh: New test case check needs_cmds

 include/tst_cmd.h                             | 13 ++++
 include/tst_test.h                            | 16 ++++-
 lib/newlib_tests/tst_needs_cmds01.c           | 16 ++---
 lib/newlib_tests/tst_needs_cmds02.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds03.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds04.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds05.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds06.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds07.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds08.c           |  8 +--
 lib/tst_test.c                                | 22 +++++--
 testcases/kernel/device-drivers/zram/zram03.c |  8 +--
 testcases/kernel/fs/squashfs/squashfs01.c     |  6 +-
 testcases/kernel/security/aslr/aslr01.c       |  6 +-
 testcases/kernel/syscalls/add_key/add_key05.c | 10 +--
 .../kernel/syscalls/fanotify/fanotify22.c     |  6 +-
 testcases/kernel/syscalls/ioctl/ioctl09.c     |  6 +-
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 34 +++++------
 testcases/kernel/syscalls/madvise/madvise11.c |  8 +--
 .../kernel/syscalls/quotactl/quotactl01.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl04.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl06.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl08.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl09.c     |  6 +-
 testcases/kernel/syscalls/statx/statx05.c     |  8 +--
 testcases/kernel/syscalls/statx/statx07.c     |  6 +-
 testcases/kernel/syscalls/statx/statx09.c     |  6 +-
 testcases/lib/run_tests.sh                    |  1 +
 testcases/lib/tests/shell_loader_cmd.sh       | 28 +++++++++
 testcases/lib/tst_run_shell.c                 | 61 ++++++++++++++++++-
 30 files changed, 231 insertions(+), 104 deletions(-)
 create mode 100755 testcases/lib/tests/shell_loader_cmd.sh
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#43 lore raw

PATCH v5 1/3 lib: Add support option for .needs_cmds

---
 include/tst_cmd.h                             | 13 ++++
 include/tst_test.h                            | 16 ++++-
 lib/newlib_tests/tst_needs_cmds01.c           | 16 ++---
 lib/newlib_tests/tst_needs_cmds02.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds03.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds04.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds05.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds06.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds07.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds08.c           |  8 +--
 lib/tst_test.c                                | 22 +++++--
 testcases/kernel/device-drivers/zram/zram03.c |  8 +--
 testcases/kernel/fs/squashfs/squashfs01.c     |  6 +-
 testcases/kernel/security/aslr/aslr01.c       |  6 +-
 testcases/kernel/syscalls/add_key/add_key05.c | 10 +--
 .../kernel/syscalls/fanotify/fanotify22.c     |  6 +-
 testcases/kernel/syscalls/ioctl/ioctl09.c     |  6 +-
 testcases/kernel/syscalls/madvise/madvise11.c |  8 +--
 .../kernel/syscalls/quotactl/quotactl01.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl04.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl06.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl08.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl09.c     |  6 +-
 testcases/kernel/syscalls/statx/statx05.c     |  8 +--
 testcases/kernel/syscalls/statx/statx07.c     |  6 +-
 testcases/kernel/syscalls/statx/statx09.c     |  6 +-
 testcases/lib/tst_run_shell.c                 | 61 ++++++++++++++++++-
 27 files changed, 185 insertions(+), 87 deletions(-)
include/tst_cmd.h +13 -0
index cf6382955..2203241c5 100644
@@ -19,6 +19,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.
  *
include/tst_test.h +14 -2
index 9c21c1728..9305cf39d 100644
@@ -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
  */
lib/newlib_tests/tst_needs_cmds01.c +8 -8
index 777c69505..6ab1145d1 100644
@@ -12,13 +12,13 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4",
-		"mkfs.ext4 >= 1.0.0",
-		"mkfs.ext4 <= 2.0.0",
-		"mkfs.ext4 != 2.0.0",
-		"mkfs.ext4 > 1.0.0",
-		"mkfs.ext4 < 2.0.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4", .optional = 1},
+		{.cmd = "mkfs.ext4 >= 1.0.0", .optional = 0},
+		{.cmd = "mkfs.ext4 <= 2.0.0"},
+		{.cmd = "mkfs.ext4 != 2.0.0"},
+		{.cmd = "mkfs.ext4 > 1.0.0"},
+		{.cmd = "mkfs.ext4 < 2.0.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds02.c +3 -3
index 455a275ea..f1d6105c2 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext45 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext45 >= 1.43.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds03.c +3 -3
index bdc1cdf6a..ac5f368e2 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 ! 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 ! 1.43.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds04.c +3 -3
index de10b8f3e..2aea51772 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds05.c +3 -3
index c3b2b3b9a..969d4e2f5 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43.0-1",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43.0-1"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds06.c +3 -3
index 40b1cf09c..91470ccf9 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43.0 2",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43.0 2"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds07.c +3 -3
index d0b4ce2ff..371bbdc1b 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext45",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext45"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds08.c +4 -4
index 38df2ef6d..412f9f293 100644
@@ -18,9 +18,9 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.xfs",
-		"mkfs.xfs >= 4.20.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.xfs"},
+		{.cmd = "mkfs.xfs >= 4.20.0"},
+		{}
 	}
 };
lib/tst_test.c +18 -4
index da5314c50..67553f65d 100644
@@ -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) ? 1 : 0;
+			pcmd++;
+		}
 	}
 
 	if (tst_test->needs_drivers) {
testcases/kernel/device-drivers/zram/zram03.c +4 -4
index 8cf26de4c..4dcf23b4e 100644
@@ -240,9 +240,9 @@ static struct tst_test test = {
 		"zram",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"modprobe",
-		"rmmod",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "modprobe"},
+		{.cmd = "rmmod"},
+		{}
 	}
 };
testcases/kernel/fs/squashfs/squashfs01.c +3 -3
index fbcb76582..7a548d62c 100644
@@ -102,9 +102,9 @@ static struct tst_test test = {
 	.needs_root = 1,
 	.needs_device = 1,
 	.dev_min_size = 1,
-	.needs_cmds = (const char *const []) {
-		"mksquashfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mksquashfs"},
+		{}
 	},
 	.needs_drivers = (const char *const []) {
 		"squashfs",
testcases/kernel/security/aslr/aslr01.c +3 -3
index 6a396e29d..e6feb2d3c 100644
@@ -277,8 +277,8 @@ static struct tst_test test = {
 		"CONFIG_HAVE_ARCH_MMAP_RND_BITS=y",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"ldd",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "ldd"},
+		{}
 	},
 };
testcases/kernel/syscalls/add_key/add_key05.c +5 -5
index c9a2f840e..b51a84b97 100644
@@ -231,11 +231,11 @@ static struct tst_test test = {
 		{&user_buf, .size = 64},
 		{}
 	},
-	.needs_cmds = (const char *const []) {
-		"useradd",
-		"userdel",
-		"groupdel",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "useradd"},
+		{.cmd = "userdel"},
+		{.cmd = "groupdel"},
+		{}
 	},
 	.tags = (const struct tst_tag[]) {
 		{"linux-git", "a08bf91ce28"},
testcases/kernel/syscalls/fanotify/fanotify22.c +3 -3
index 357e74dbc..6578474a7 100644
@@ -327,9 +327,9 @@ static struct tst_test test = {
 		{"linux-git", "76486b104168"},
 		{}
 	},
-	.needs_cmds = (const char *[]) {
-		"debugfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "debugfs"},
+		{}
 	}
 };
 
testcases/kernel/syscalls/ioctl/ioctl09.c +3 -3
index 262d6fcab..737aee680 100644
@@ -108,9 +108,9 @@ static struct tst_test test = {
 		"loop",
 		NULL
 	},
-	.needs_cmds = (const char *const []) {
-		"parted",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "parted"},
+		{}
 	},
 	.needs_tmpdir = 1,
 };
testcases/kernel/syscalls/madvise/madvise11.c +4 -4
index 422589f5b..73d2d4294 100644
@@ -421,10 +421,10 @@ static struct tst_test test = {
 		HW_MODULE,
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"modprobe",
-		"rmmod",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "modprobe"},
+		{.cmd = "rmmod"},
+		{}
 	},
 	.needs_kconfigs = (const char *[]) {
 		"CONFIG_MEMORY_FAILURE=y",
testcases/kernel/syscalls/quotactl/quotactl01.c +3 -3
index 9dcf74ceb..3091ac5bf 100644
@@ -223,9 +223,9 @@ static struct tst_test test = {
 		{}
 	},
 	.mntpoint = MNTPOINT,
-	.needs_cmds = (const char *const []) {
-		"quotacheck",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "quotacheck"},
+		{}
 	},
 	.setup = setup,
 	.cleanup = cleanup,
testcases/kernel/syscalls/quotactl/quotactl04.c +3 -3
index d2d7b3f3e..a70903330 100644
@@ -165,8 +165,8 @@ static struct tst_test test = {
 	},
 	.mntpoint = MNTPOINT,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/quotactl/quotactl06.c +3 -3
index 110a3aa9b..333f5720d 100644
@@ -226,9 +226,9 @@ static struct tst_test test = {
 	},
 	.mntpoint = MNTPOINT,
 	.mount_device = 1,
-	.needs_cmds = (const char *const []) {
-		"quotacheck",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "quotacheck"},
+		{}
 	},
 	.needs_root = 1,
 	.test_variants = QUOTACTL_FMT_VARIANTS,
testcases/kernel/syscalls/quotactl/quotactl08.c +3 -3
index 63087345f..ce7a8b008 100644
@@ -220,8 +220,8 @@ static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/quotactl/quotactl09.c +3 -3
index 673666347..fd6629c0c 100644
@@ -185,8 +185,8 @@ static struct tst_test test = {
 	.mount_device = 1,
 	.needs_root = 1,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx05.c +4 -4
index 2a4603226..07b5e7b94 100644
@@ -123,9 +123,9 @@ static struct tst_test test = {
 		{.type = "ext4"},
 		{}
 	},
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		"e4crypt",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{.cmd = "e4crypt"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx07.c +3 -3
index bab64591f..755644cf9 100644
@@ -171,8 +171,8 @@ static struct tst_test test = {
 		{}
 	},
 	.needs_root = 1,
-	.needs_cmds = (const char *[]) {
-		"exportfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "exportfs"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx09.c +3 -3
index ee4be4250..8838d5450 100644
@@ -162,8 +162,8 @@ static struct tst_test test = {
 		"CONFIG_FS_VERITY",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.45.2",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.45.2"},
+		{}
 	}
 };
testcases/lib/tst_run_shell.c +60 -1

79 lines not on this page · raw message

Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v5 1/3
#44 lore raw

Re: [LTP] [PATCH v5 1/3] lib: Add support option for .needs_cmds

Hi Wei,

...

include/tst_test.h struct tst_fs { · line 527

Quoted 13 lines

/*
* vfork() + execvp() specified program.
*

include/tst_test.h +1 -1
index 9c21c1728..9305cf39d 100644
@@ -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.

Please use :ref:... formatting to make struct clickable.

* @needs_cmds: A NULL terminated array of :ref:struct tst_cmd required for
* the test to run.

Quoted 13 lines
include/tst_test.h +1 -1
  *
  * @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;
 

include/tst_test.h int tst_creat_unlinked(const char *path, int flags, mode_t mode); · line 729

Quoted 9 lines
include/tst_test.h +6 -0
@@ -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'

And here please again make it clickable tst_cmd->present. It really helps people to click and see what you mean by 'present'.

* the given command name with each entry in the array and returns the
* &tst_cmd->present flag for the matching command.

If this is the only part to change, it can be done before merge.

include/tst_test.h int tst_creat_unlinked(const char *path, int flags, mode_t mode); · line 734

Quoted 5 lines
include/tst_test.h +5 -0
+ * flag for the matching command.
+ *
+ * Return: `true` if the command is present, `false` otherwise.
+ */
+bool tst_cmd_present(const char *cmd);

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#45 lore raw

Re: [LTP] [PATCH v5 1/3] lib: Add support option for .needs_cmds

In reply to Wei Gao via ltp

Hi!

Quoted 9 lines
lib/tst_test.c +1 -0
index da5314c50..67553f65d 100644
@@ -1365,6 +1365,19 @@ static const char *default_fs_type(void)
 	return tst_dev_fs_type();
 }
 
+bool tst_cmd_present(const char *cmd)

lib/tst_test.c static const char *default_fs_type(void) · line 1378

Quoted 10 lines
lib/tst_test.c +10 -0
+{
+	struct tst_cmd *pcmd = tst_test->needs_cmds;
+
+	while (pcmd->cmd) {
+		if (!strcmp(pcmd->cmd, cmd))
+			return pcmd->present;
+
+		pcmd++;
+	}
+	return false;

Once again, we should tst_brk(TBROK, "Unexpected command '%s'", cmd) here since we asked for something that we haven't checked for!

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#46 lore raw

Re: [LTP] [PATCH v5 1/3] lib: Add support option for .needs_cmds

Quoted 9 lines

Hi!

Quoted 7 lines
lib/tst_test.c +0 -0
index da5314c50..67553f65d 100644
@@ -1365,6 +1365,19 @@ static const char *default_fs_type(void)
 	return tst_dev_fs_type();
 }
Quoted 14 lines

lib/tst_test.c static const char *default_fs_type(void) · line 1378

Quoted 11 lines
lib/tst_test.c +11 -0
+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;

Once again, we should tst_brk(TBROK, "Unexpected command '%s'", cmd) here since we asked for something that we haven't checked for!

Good point. I'm sorry to overlook this in my review.

Also, strictly speaking tst_cmd_present() is only defined, not used. The commit is about changing .needs_cmds from char array (string) to struct tst_cmd. Therefore it could be in a separate commit. We don't have to be too strict, but given how many tests needed to be adapted adding tst_cmd_present() is somehow buried with other changes.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#47 lore raw

Re: [LTP] [PATCH v5 1/3] lib: Add support option for .needs_cmds

Quoted 24 lines · Petr Vorel
Quoted 9 lines

Hi!

Quoted 7 lines
lib/tst_test.c +0 -0
index da5314c50..67553f65d 100644
@@ -1365,6 +1365,19 @@ static const char *default_fs_type(void)
 	return tst_dev_fs_type();
 }
Quoted 14 lines

lib/tst_test.c static const char *default_fs_type(void) · line 1378

Quoted 11 lines
lib/tst_test.c +11 -0
+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;

Once again, we should tst_brk(TBROK, "Unexpected command '%s'", cmd) here since we asked for something that we haven't checked for!

Quoted 7 lines

Good point. I'm sorry to overlook this in my review.

Also, strictly speaking tst_cmd_present() is only defined, not used. The commit is about changing .needs_cmds from char array (string) to struct tst_cmd. Therefore it could be in a separate commit. We don't have to be too strict, but given how many tests needed to be adapted adding tst_cmd_present() is somehow buried with other changes.

Thanks all for detail feedback. Let's me give some explaination why skip tst_brk in above function:
tst_cmd_present will be used in latest ioctl_loop01.c and it should not use tst_brk otherwise we will brk out of test in setup() too early. The new support needs_cmds.optional in current patch is used for support tst_cmd_present scenario in ioctl_loop01.c.
Correct me if any mistake.

ioctl_loop01.c code:
static void setup(void)

{
        parted_sup = tst_cmd_present("parted");  <=====

.....

        if (parted_sup)
                SAFE_CMD(cmd_parted, NULL, NULL);  <===

        sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
}
Quoted 3 lines

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#48 lore raw

Re: [LTP] [PATCH v5 1/3] lib: Add support option for .needs_cmds

Hi Wei,

...

Quoted 6 lines
Quoted 5 lines

Also, strictly speaking tst_cmd_present() is only defined, not used. The commit is about changing .needs_cmds from char array (string) to struct tst_cmd. Therefore it could be in a separate commit. We don't have to be too strict, but given how many tests needed to be adapted adding tst_cmd_present() is somehow buried with other changes.

Quoted 7 lines

Thanks all for detail feedback. Let's me give some explaination why skip tst_brk in above function:
tst_cmd_present will be used in latest ioctl_loop01.c and it should not use tst_brk otherwise we will brk out of test in setup() too early. The new support needs_cmds.optional in current patch is used for support tst_cmd_present scenario in ioctl_loop01.c.
Correct me if any mistake.

Sure, I noticed tst_cmd_present() usage in a later commit :).

My point was (while this patchset is also about ideal feature change split into commits) that if you touch many files with struct tst_cmd change, adding unrelated change (tst_cmd_present()) will hide this change. I would personally move adding tst_cmd_present() to a separate commit. It's not that important, just to make review easier.

The rules (which I've tried to explain on this patchset) are
* each commit must compile (even temporarily break build is not acceptable)
* split changes into small logical parts

People talk about atomic changes: "As small as possible, but complete" so that they don't break CI.
https://dev.to/samuelfaure/how-atomic-git-commits-dramatically-increased-my-productivity-and-will-increase-yours-too-4a84

Kind regards,
Petr

Quoted 14 lines

ioctl_loop01.c code:
static void setup(void)

{
        parted_sup = tst_cmd_present("parted");  <=====

.....

        if (parted_sup)
                SAFE_CMD(cmd_parted, NULL, NULL);  <===

        sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
}
Quoted 2 lines

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#49 lore raw

Re: [LTP] [PATCH v5 1/3] lib: Add support option for .needs_cmds

Quoted 17 lines · Petr Vorel

Hi Wei,

...

Quoted 6 lines
Quoted 5 lines

Also, strictly speaking tst_cmd_present() is only defined, not used. The commit is about changing .needs_cmds from char array (string) to struct tst_cmd. Therefore it could be in a separate commit. We don't have to be too strict, but given how many tests needed to be adapted adding tst_cmd_present() is somehow buried with other changes.

Quoted 7 lines

Thanks all for detail feedback. Let's me give some explaination why skip tst_brk in above function:
tst_cmd_present will be used in latest ioctl_loop01.c and it should not use tst_brk otherwise we will brk out of test in setup() too early. The new support needs_cmds.optional in current patch is used for support tst_cmd_present scenario in ioctl_loop01.c.
Correct me if any mistake.

Quoted 7 lines

Sure, I noticed tst_cmd_present() usage in a later commit :).

My point was (while this patchset is also about ideal feature change split into commits) that if you touch many files with struct tst_cmd change, adding unrelated change (tst_cmd_present()) will hide this change. I would personally move adding tst_cmd_present() to a separate commit. It's not that important, just to make review easier.

Thanks, i get your point now.
@Petr @Cyril
If we have are agree with following changes then i can sent next patch:
1) Just move the declaration and definition of tst_cmd_present to a separate commit without any functional content changes.
2) Modify function comments such as add ref:...

Quoted 8 lines

The rules (which I've tried to explain on this patchset) are
* each commit must compile (even temporarily break build is not acceptable)
* split changes into small logical parts

People talk about atomic changes: "As small as possible, but complete" so that they don't break CI.
https://dev.to/samuelfaure/how-atomic-git-commits-dramatically-increased-my-productivity-and-will-increase-yours-too-4a84

Nice link:)

Quoted 18 lines

Kind regards,
Petr

Quoted 14 lines

ioctl_loop01.c code:
static void setup(void)

{
        parted_sup = tst_cmd_present("parted");  <=====

.....

        if (parted_sup)
                SAFE_CMD(cmd_parted, NULL, NULL);  <===

        sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
}
Quoted 2 lines

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#50 lore raw

Re: [LTP] [PATCH v5 1/3] lib: Add support option for .needs_cmds

Quoted 24 lines
Quoted 17 lines

Hi Wei,

...

Quoted 6 lines
Quoted 5 lines

Also, strictly speaking tst_cmd_present() is only defined, not used. The commit is about changing .needs_cmds from char array (string) to struct tst_cmd. Therefore it could be in a separate commit. We don't have to be too strict, but given how many tests needed to be adapted adding tst_cmd_present() is somehow buried with other changes.

Quoted 7 lines

Thanks all for detail feedback. Let's me give some explaination why skip tst_brk in above function:
tst_cmd_present will be used in latest ioctl_loop01.c and it should not use tst_brk otherwise we will brk out of test in setup() too early. The new support needs_cmds.optional in current patch is used for support tst_cmd_present scenario in ioctl_loop01.c.
Correct me if any mistake.

Quoted 7 lines

Sure, I noticed tst_cmd_present() usage in a later commit :).

My point was (while this patchset is also about ideal feature change split into commits) that if you touch many files with struct tst_cmd change, adding unrelated change (tst_cmd_present()) will hide this change. I would personally move adding tst_cmd_present() to a separate commit. It's not that important, just to make review easier.

Quoted 5 lines

Thanks, i get your point now.
@Petr @Cyril
If we have are agree with following changes then i can sent next patch:
1) Just move the declaration and definition of tst_cmd_present to a separate commit without any functional content changes.
2) Modify function comments such as add ref:...

+1, thank you!

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#51 lore raw

Re: [LTP] [PATCH v5 1/3] lib: Add support option for .needs_cmds

In reply to Wei Gao via ltp
Quoted 46 lines · Petr Vorel
Quoted 24 lines
Quoted 9 lines

Hi!

Quoted 7 lines
lib/tst_test.c +0 -0
index da5314c50..67553f65d 100644
@@ -1365,6 +1365,19 @@ static const char *default_fs_type(void)
 	return tst_dev_fs_type();
 }
Quoted 14 lines

lib/tst_test.c static const char *default_fs_type(void) · line 1378

Quoted 11 lines
lib/tst_test.c +11 -0
+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;

Once again, we should tst_brk(TBROK, "Unexpected command '%s'", cmd) here since we asked for something that we haven't checked for!

Quoted 7 lines

Good point. I'm sorry to overlook this in my review.

Also, strictly speaking tst_cmd_present() is only defined, not used. The commit is about changing .needs_cmds from char array (string) to struct tst_cmd. Therefore it could be in a separate commit. We don't have to be too strict, but given how many tests needed to be adapted adding tst_cmd_present() is somehow buried with other changes.

Thanks all for detail feedback. Let's me give some explaination why skip tst_brk in above function:
tst_cmd_present will be used in latest ioctl_loop01.c and it should not use tst_brk otherwise we will brk out of test in setup() too early. The new support needs_cmds.optional in current patch is used for support tst_cmd_present scenario in ioctl_loop01.c.
Correct me if any mistake.

ioctl_loop01.c code:
static void setup(void)

{
        parted_sup = tst_cmd_present("parted");  <=====
Quoted 6 lines

.....

        if (parted_sup)
                SAFE_CMD(cmd_parted, NULL, NULL);  <===

        sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
}

And also:

static struct tst_test test = {
	...
	.needs_cmds = (struct tst_cmd[]) {
		{.cmd = "parted", .optional = 1},
		{}

Therefore IMHO you are wrong.

+ while (pcmd->cmd) {
+ if (!strcmp(pcmd->cmd, cmd))
We always match this for programs which we checked via .needs_cmds (regardless true or false return).
+ return pcmd->present;
+
+ pcmd++;
+ }

If we get here that means that we ask in tst_cmd_present("...") for program we did not add .needs_cmds. That's why Cyril asked for tst_brk().
+ return false;

Kind regards,
Petr

Quoted 2 lines
Quoted 2 lines

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#52 lore raw

PATCH v5 2/3 ioctl_loop01.c: Add new support .needs_cmds

In reply to Wei Gao via ltp
---
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 34 +++++++++----------
 1 file changed, 17 insertions(+), 17 deletions(-)
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +17 -17
index 4ecc93b1e..a8a9305c8 100644
@@ -78,23 +78,6 @@ static void check_loop_value(int set_flag, int get_flag, int autoclear_field)
 
 static void verify_ioctl_loop(void)
 {
-	int ret;
-	const char *const cmd_parted[] = {"parted", "-s", dev_path, "mklabel", "msdos", "mkpart",
-					"primary", "ext4", "1M", "10M", NULL};
-
-	tst_fill_file("test.img", 0, 1024 * 1024, 10);
-	tst_attach_device(dev_path, "test.img");
-
-	ret = tst_cmd(cmd_parted, NULL, NULL, TST_CMD_PASS_RETVAL);
-	if (!ret)
-		parted_sup = 1;
-	else if (ret == 255)
-		tst_res(TCONF, "parted binary not installed or failed");
-	else
-		tst_res(TCONF, "parted exited with %i", ret);
-
-	attach_flag = 1;
-
 	TST_ASSERT_INT(partscan_path, 0);
 	TST_ASSERT_INT(autoclear_path, 0);
 	TST_ASSERT_STR(backing_path, backing_file_path);
@@ -113,10 +96,23 @@ static void verify_ioctl_loop(void)
 
 static void setup(void)
 {
+	parted_sup = tst_cmd_present("parted");
+
+	const char *const cmd_parted[] = {"parted", "-s", dev_path, "mklabel", "msdos", "mkpart",
+	                                  "primary", "ext4", "1M", "10M", NULL};
+
 	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
 	if (dev_num < 0)
 		tst_brk(TBROK, "Failed to find free loop device");
 
+	tst_fill_file("test.img", 0, 1024 * 1024, 10);
+
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+
+	if (parted_sup)
+		SAFE_CMD(cmd_parted, NULL, NULL);
+
 	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
 	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);
 	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
@@ -148,5 +144,9 @@ static struct tst_test test = {
 		{"linux-git", "6ac92fb5cdff"},
 		{}
 	},
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "parted", .optional = 1},
+		{}
+	},
 	.needs_tmpdir = 1,
 };
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v5 2/3
#53 lore raw

Re: [LTP] [PATCH v5 2/3] ioctl_loop01.c: Add new support .needs_cmds

Hi Wei,

Quoted 26 lines

+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c

testcases/kernel/syscalls/ioctl/ioctl_loop01.c +0 -17
@@ -78,23 +78,6 @@ static void check_loop_value(int set_flag, int get_flag, int autoclear_field)
 
 static void verify_ioctl_loop(void)
 {
-	int ret;
-	const char *const cmd_parted[] = {"parted", "-s", dev_path, "mklabel", "msdos", "mkpart",
-					"primary", "ext4", "1M", "10M", NULL};
-
-	tst_fill_file("test.img", 0, 1024 * 1024, 10);
-	tst_attach_device(dev_path, "test.img");
-
-	ret = tst_cmd(cmd_parted, NULL, NULL, TST_CMD_PASS_RETVAL);
-	if (!ret)
-		parted_sup = 1;
-	else if (ret == 255)
-		tst_res(TCONF, "parted binary not installed or failed");
-	else
-		tst_res(TCONF, "parted exited with %i", ret);
-
-	attach_flag = 1;
-
 	TST_ASSERT_INT(partscan_path, 0);
 	TST_ASSERT_INT(autoclear_path, 0);
 	TST_ASSERT_STR(backing_path, backing_file_path);

testcases/kernel/syscalls/ioctl/ioctl_loop01.c static void verify_ioctl_loop(void) · line 102

Quoted 7 lines
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +4 -0
@@ -113,10 +96,23 @@ static void verify_ioctl_loop(void)
 
 static void setup(void)
 {
+	parted_sup = tst_cmd_present("parted");
+
+	const char *const cmd_parted[] = {"parted", "-s", dev_path, "mklabel", "msdos", "mkpart",
+	                                  "primary", "ext4", "1M", "10M", NULL};

FYI unfortunately I'm still able to trigger loop failure on SLES 5.14 based kernel when looping ioctl_loop01 and ioctl_ficlone02 for a while on both current master and with this patch (which does not handle the problem).

dmesg:

[ 1101.501003] ioctl_loop01
[ 1101.519346] loop0: detected capacity change from 0 to 20480
[ 1101.664879] loop_reread_partitions: partition scan of loop0 () failed (rc=-16)
[ 1101.705931] blk_update_request: I/O error, dev loop0, sector 20352 op 0x0:(READ) flags 0x80700 phys_seg 1 prio class 0
[ 1101.710398] blk_update_request: I/O error, dev loop0, sector 20352 op 0x0:(READ) flags 0x0 phys_seg 1 prio class 0
[ 1101.710423] __loop_clr_fd: partition scan of loop0 failed (rc=-16)
[ 1101.713612] Buffer I/O error on dev loop0p1, logical block 2288, async page read
[ 1101.774843] blk_update_request: I/O error, dev loop0, sector 20352 op 0x0:(READ) flags 0x80700 phys_seg 1 prio class 0
[ 1101.783014] blk_update_request: I/O error, dev loop0, sector 20352 op 0x0:(READ) flags 0x0 phys_seg 1 prio class 0
[ 1101.787585] Buffer I/O error on dev loop0p1, logical block 2288, async page read
[ 1101.824974] ioctl_ficlone02
[ 1101.827315] loop0: detected capacity change from 0 to 614400
[ 1101.861851] /dev/zero: Can't open blockdev
[ 1101.968916] EXT4-fs (loop0): mounting ext2 file system using the ext4 subsystem
[ 1101.975954] EXT4-fs (loop0): mounted filesystem without journal. Opts: (null). Quota mode: none.
[ 1102.156957] EXT4-fs (loop0): mounting ext3 file system using the ext4 subsystem
[ 1102.163452] EXT4-fs (loop0): mounted filesystem with ordered data mode. Opts: (null). Quota mode: none.
[ 1102.268502] EXT4-fs (loop0): mounted filesystem with ordered data mode. Opts: (null). Quota mode: none.

Fortunately Martin is working on a patch for LTP library that'll check for leftover partitions after loop device release and clear them, with appropriate TWARN message.

Kind regards,
Petr

Quoted 24 lines
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +13 -0
+
 	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
 	if (dev_num < 0)
 		tst_brk(TBROK, "Failed to find free loop device");
 
+	tst_fill_file("test.img", 0, 1024 * 1024, 10);
+
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+
+	if (parted_sup)
+		SAFE_CMD(cmd_parted, NULL, NULL);
+
 	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
 	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);
 	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
@@ -148,5 +144,9 @@ static struct tst_test test = {
 		{"linux-git", "6ac92fb5cdff"},
 		{}
 	},
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "parted", .optional = 1},
+		{}
+	},
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#54 lore raw

PATCH v5 3/3 shell_loader_cmd.sh: New test case check needs_cmds

In reply to Wei Gao via ltp
---
 testcases/lib/run_tests.sh              |  1 +
 testcases/lib/tests/shell_loader_cmd.sh | 28 +++++++++++++++++++++++++
 2 files changed, 29 insertions(+)
 create mode 100755 testcases/lib/tests/shell_loader_cmd.sh
testcases/lib/run_tests.sh +1 -0
index 5c309bbeb..24ac81a44 100755
@@ -6,6 +6,7 @@ shell_loader.sh
 shell_loader_all_filesystems.sh
 shell_loader_c_child.sh
 shell_loader_filesystems.sh
+shell_loader_cmd.sh
 shell_loader_kconfigs.sh
 shell_loader_supported_archs.sh
 shell_loader_tcnt.sh
testcases/lib/tests/shell_loader_cmd.sh new file +28 -0
new file mode 100755
index 000000000..4e8e61eb6
@@ -0,0 +1,28 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2025 Wei Gao <[email protected]>
+#
+# ---
+# env
+# {
+#  "needs_cmds": [
+#   {
+#    "cmd": "ls",
+#    "optional": 1
+#   },
+#   {
+#    "cmd": "mkfs.ext4 >= 1.0.0",
+#    "optional": 1
+#   }
+#  ]
+# }
+# ---
+
+. tst_loader.sh
+
+tst_test()
+{
+	tst_res TPASS "We are running with needs_cmds"
+}
+
+. tst_run.sh
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#55 lore raw

Re: [LTP] [PATCH v5 3/3] shell_loader_cmd.sh: New test case check needs_cmds

Sorry, It seems some strange CI failure triggered by "cmd": "mkfs.ext4 >= 1.0.0", i need further check and will update it.

Quoted 59 lines · Wei Gao
---
 testcases/lib/run_tests.sh              |  1 +
 testcases/lib/tests/shell_loader_cmd.sh | 28 +++++++++++++++++++++++++
 2 files changed, 29 insertions(+)
 create mode 100755 testcases/lib/tests/shell_loader_cmd.sh
testcases/lib/run_tests.sh +1 -0
index 5c309bbeb..24ac81a44 100755
@@ -6,6 +6,7 @@ shell_loader.sh
 shell_loader_all_filesystems.sh
 shell_loader_c_child.sh
 shell_loader_filesystems.sh
+shell_loader_cmd.sh
 shell_loader_kconfigs.sh
 shell_loader_supported_archs.sh
 shell_loader_tcnt.sh
testcases/lib/tests/shell_loader_cmd.sh new file +28 -1
new file mode 100755
index 000000000..4e8e61eb6
@@ -0,0 +1,28 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2025 Wei Gao <[email protected]>
+#
+# ---
+# env
+# {
+#  "needs_cmds": [
+#   {
+#    "cmd": "ls",
+#    "optional": 1
+#   },
+#   {
+#    "cmd": "mkfs.ext4 >= 1.0.0",
+#    "optional": 1
+#   }
+#  ]
+# }
+# ---
+
+. tst_loader.sh
+
+tst_test()
+{
+       tst_res TPASS "We are running with needs_cmds"
+}
+
+. tst_run.sh
--

2.51.0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#57 lore raw

v6 cover new cmd support option for needs_cmds

In reply to Wei Gao via ltp

v5-v6:
1) Move the declaration and definition of tst_cmd_present to a separate commit
2) Modify function comments such as add ref:...

Wei Gao (4):
lib: Add support option for .needs_cmds
tst_test.c: Add tst_cmd_present check if a command is present ioctl_loop01.c: Add new support .needs_cmds shell_loader_cmd.sh: New test case check needs_cmds

 include/tst_cmd.h                             | 13 ++++
 include/tst_test.h                            | 16 ++++-
 lib/newlib_tests/tst_needs_cmds01.c           | 16 ++---
 lib/newlib_tests/tst_needs_cmds02.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds03.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds04.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds05.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds06.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds07.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds08.c           |  8 +--
 lib/tst_test.c                                | 22 +++++--
 testcases/kernel/device-drivers/zram/zram03.c |  8 +--
 testcases/kernel/fs/squashfs/squashfs01.c     |  6 +-
 testcases/kernel/security/aslr/aslr01.c       |  6 +-
 testcases/kernel/syscalls/add_key/add_key05.c | 10 +--
 .../kernel/syscalls/fanotify/fanotify22.c     |  6 +-
 testcases/kernel/syscalls/ioctl/ioctl09.c     |  6 +-
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 34 +++++------
 testcases/kernel/syscalls/madvise/madvise11.c |  8 +--
 .../kernel/syscalls/quotactl/quotactl01.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl04.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl06.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl08.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl09.c     |  6 +-
 testcases/kernel/syscalls/statx/statx05.c     |  8 +--
 testcases/kernel/syscalls/statx/statx07.c     |  6 +-
 testcases/kernel/syscalls/statx/statx09.c     |  6 +-
 testcases/lib/run_tests.sh                    |  1 +
 testcases/lib/tests/shell_loader_cmd.sh       | 28 +++++++++
 testcases/lib/tst_run_shell.c                 | 61 ++++++++++++++++++-
 30 files changed, 231 insertions(+), 104 deletions(-)
 create mode 100755 testcases/lib/tests/shell_loader_cmd.sh
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#58 lore raw

PATCH v6 1/4 lib: Add support option for .needs_cmds

---
 include/tst_cmd.h                             | 13 ++++
 include/tst_test.h                            |  4 +-
 lib/newlib_tests/tst_needs_cmds01.c           | 16 ++---
 lib/newlib_tests/tst_needs_cmds02.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds03.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds04.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds05.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds06.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds07.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds08.c           |  8 +--
 lib/tst_test.c                                |  9 +--
 testcases/kernel/device-drivers/zram/zram03.c |  8 +--
 testcases/kernel/fs/squashfs/squashfs01.c     |  6 +-
 testcases/kernel/security/aslr/aslr01.c       |  6 +-
 testcases/kernel/syscalls/add_key/add_key05.c | 10 +--
 .../kernel/syscalls/fanotify/fanotify22.c     |  6 +-
 testcases/kernel/syscalls/ioctl/ioctl09.c     |  6 +-
 testcases/kernel/syscalls/madvise/madvise11.c |  8 +--
 .../kernel/syscalls/quotactl/quotactl01.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl04.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl06.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl08.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl09.c     |  6 +-
 testcases/kernel/syscalls/statx/statx05.c     |  8 +--
 testcases/kernel/syscalls/statx/statx07.c     |  6 +-
 testcases/kernel/syscalls/statx/statx09.c     |  6 +-
 testcases/lib/tst_run_shell.c                 | 61 ++++++++++++++++++-
 27 files changed, 160 insertions(+), 87 deletions(-)
include/tst_cmd.h +13 -0
index cf6382955..2203241c5 100644
@@ -19,6 +19,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.
  *
include/tst_test.h +2 -2
index 9c21c1728..38a9d0d54 100644
@@ -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 :ref:`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;
 
lib/newlib_tests/tst_needs_cmds01.c +8 -8
index 777c69505..6ab1145d1 100644
@@ -12,13 +12,13 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4",
-		"mkfs.ext4 >= 1.0.0",
-		"mkfs.ext4 <= 2.0.0",
-		"mkfs.ext4 != 2.0.0",
-		"mkfs.ext4 > 1.0.0",
-		"mkfs.ext4 < 2.0.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4", .optional = 1},
+		{.cmd = "mkfs.ext4 >= 1.0.0", .optional = 0},
+		{.cmd = "mkfs.ext4 <= 2.0.0"},
+		{.cmd = "mkfs.ext4 != 2.0.0"},
+		{.cmd = "mkfs.ext4 > 1.0.0"},
+		{.cmd = "mkfs.ext4 < 2.0.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds02.c +3 -3
index 455a275ea..f1d6105c2 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext45 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext45 >= 1.43.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds03.c +3 -3
index bdc1cdf6a..ac5f368e2 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 ! 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 ! 1.43.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds04.c +3 -3
index de10b8f3e..2aea51772 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds05.c +3 -3
index c3b2b3b9a..969d4e2f5 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43.0-1",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43.0-1"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds06.c +3 -3
index 40b1cf09c..91470ccf9 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43.0 2",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43.0 2"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds07.c +3 -3
index d0b4ce2ff..371bbdc1b 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext45",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext45"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds08.c +4 -4
index 38df2ef6d..412f9f293 100644
@@ -18,9 +18,9 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.xfs",
-		"mkfs.xfs >= 4.20.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.xfs"},
+		{.cmd = "mkfs.xfs >= 4.20.0"},
+		{}
 	}
 };
lib/tst_test.c +5 -4
index da5314c50..42a54621e 100644
@@ -1439,11 +1439,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) ? 1 : 0;
+			pcmd++;
+		}
 	}
 
 	if (tst_test->needs_drivers) {
testcases/kernel/device-drivers/zram/zram03.c +4 -4
index 8cf26de4c..4dcf23b4e 100644
@@ -240,9 +240,9 @@ static struct tst_test test = {
 		"zram",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"modprobe",
-		"rmmod",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "modprobe"},
+		{.cmd = "rmmod"},
+		{}
 	}
 };
testcases/kernel/fs/squashfs/squashfs01.c +3 -3
index fbcb76582..7a548d62c 100644
@@ -102,9 +102,9 @@ static struct tst_test test = {
 	.needs_root = 1,
 	.needs_device = 1,
 	.dev_min_size = 1,
-	.needs_cmds = (const char *const []) {
-		"mksquashfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mksquashfs"},
+		{}
 	},
 	.needs_drivers = (const char *const []) {
 		"squashfs",
testcases/kernel/security/aslr/aslr01.c +3 -3
index 6a396e29d..e6feb2d3c 100644
@@ -277,8 +277,8 @@ static struct tst_test test = {
 		"CONFIG_HAVE_ARCH_MMAP_RND_BITS=y",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"ldd",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "ldd"},
+		{}
 	},
 };
testcases/kernel/syscalls/add_key/add_key05.c +5 -5
index c9a2f840e..b51a84b97 100644
@@ -231,11 +231,11 @@ static struct tst_test test = {
 		{&user_buf, .size = 64},
 		{}
 	},
-	.needs_cmds = (const char *const []) {
-		"useradd",
-		"userdel",
-		"groupdel",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "useradd"},
+		{.cmd = "userdel"},
+		{.cmd = "groupdel"},
+		{}
 	},
 	.tags = (const struct tst_tag[]) {
 		{"linux-git", "a08bf91ce28"},
testcases/kernel/syscalls/fanotify/fanotify22.c +3 -3
index 357e74dbc..6578474a7 100644
@@ -327,9 +327,9 @@ static struct tst_test test = {
 		{"linux-git", "76486b104168"},
 		{}
 	},
-	.needs_cmds = (const char *[]) {
-		"debugfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "debugfs"},
+		{}
 	}
 };
 
testcases/kernel/syscalls/ioctl/ioctl09.c +3 -3
index c046d8959..21e16efb6 100644
@@ -108,9 +108,9 @@ static struct tst_test test = {
 		"loop",
 		NULL
 	},
-	.needs_cmds = (const char *const []) {
-		"parted",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "parted"},
+		{}
 	},
 	.needs_tmpdir = 1,
 };
testcases/kernel/syscalls/madvise/madvise11.c +4 -4
index 422589f5b..73d2d4294 100644
@@ -421,10 +421,10 @@ static struct tst_test test = {
 		HW_MODULE,
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"modprobe",
-		"rmmod",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "modprobe"},
+		{.cmd = "rmmod"},
+		{}
 	},
 	.needs_kconfigs = (const char *[]) {
 		"CONFIG_MEMORY_FAILURE=y",
testcases/kernel/syscalls/quotactl/quotactl01.c +3 -3
index 9dcf74ceb..3091ac5bf 100644
@@ -223,9 +223,9 @@ static struct tst_test test = {
 		{}
 	},
 	.mntpoint = MNTPOINT,
-	.needs_cmds = (const char *const []) {
-		"quotacheck",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "quotacheck"},
+		{}
 	},
 	.setup = setup,
 	.cleanup = cleanup,
testcases/kernel/syscalls/quotactl/quotactl04.c +3 -3
index d2d7b3f3e..a70903330 100644
@@ -165,8 +165,8 @@ static struct tst_test test = {
 	},
 	.mntpoint = MNTPOINT,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/quotactl/quotactl06.c +3 -3
index 110a3aa9b..333f5720d 100644
@@ -226,9 +226,9 @@ static struct tst_test test = {
 	},
 	.mntpoint = MNTPOINT,
 	.mount_device = 1,
-	.needs_cmds = (const char *const []) {
-		"quotacheck",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "quotacheck"},
+		{}
 	},
 	.needs_root = 1,
 	.test_variants = QUOTACTL_FMT_VARIANTS,
testcases/kernel/syscalls/quotactl/quotactl08.c +3 -3
index 63087345f..ce7a8b008 100644
@@ -220,8 +220,8 @@ static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/quotactl/quotactl09.c +3 -3
index 673666347..fd6629c0c 100644
@@ -185,8 +185,8 @@ static struct tst_test test = {
 	.mount_device = 1,
 	.needs_root = 1,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx05.c +4 -4
index 2a4603226..07b5e7b94 100644
@@ -123,9 +123,9 @@ static struct tst_test test = {
 		{.type = "ext4"},
 		{}
 	},
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		"e4crypt",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{.cmd = "e4crypt"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx07.c +3 -3
index bab64591f..755644cf9 100644
@@ -171,8 +171,8 @@ static struct tst_test test = {
 		{}
 	},
 	.needs_root = 1,
-	.needs_cmds = (const char *[]) {
-		"exportfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "exportfs"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx09.c +3 -3
index ee4be4250..8838d5450 100644
@@ -162,8 +162,8 @@ static struct tst_test test = {
 		"CONFIG_FS_VERITY",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.45.2",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.45.2"},
+		{}
 	}
 };
testcases/lib/tst_run_shell.c +60 -1

79 lines not on this page · raw message

Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v6 1/4
#59 lore raw

Re: [LTP] [PATCH v6 1/4] lib: Add support option for .needs_cmds

Hi Wei,

LGTM now, thanks!

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v6 1/4
#60 lore raw

Re: [LTP] [PATCH v6 1/4] lib: Add support option for .needs_cmds

In reply to Wei Gao via ltp

Hi!

testcases/lib/tst_run_shell.c static ujson_obj fs_obj = { · line 191

Quoted 10 lines
testcases/lib/tst_run_shell.c +10 -0
+enum cmd_ids {
+	CMD,
+	OPTIONAL,
+	PRESENT,
+};
+
+static ujson_obj_attr cmd_attrs[] = {
+	UJSON_OBJ_ATTR_IDX(CMD, "cmd", UJSON_STR),
+	UJSON_OBJ_ATTR_IDX(OPTIONAL, "optional", UJSON_INT),
+	UJSON_OBJ_ATTR_IDX(PRESENT, "present", UJSON_INT),

I do not think that we should add the present attribute to the shell parser, that attribute should never be set by the test and is set by the shell library.

The rest looks good.

With the "present" attribute removed from the json parser:

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#61 lore raw

PATCH v6 2/4 tst_test.c: Add tst_cmd_present check if a command is present

In reply to Wei Gao via ltp
---
 include/tst_test.h | 12 ++++++++++++
 lib/tst_test.c     | 13 +++++++++++++
 2 files changed, 25 insertions(+)
include/tst_test.h +12 -0
index 38a9d0d54..89dd31582 100644
@@ -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
+ * &tst_cmd->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
  */
lib/tst_test.c +13 -0
index 42a54621e..67553f65d 100644
@@ -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");
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v6 2/4
#62 lore raw

Re: [LTP] [PATCH v6 2/4] tst_test.c: Add tst_cmd_present check if a command is present

Hi Wei,

Thanks!

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#63 lore raw

Re: [LTP] [PATCH v6 2/4] tst_test.c: Add tst_cmd_present check if a command is present

In reply to Wei Gao via ltp

Hi!

Quoted 12 lines

/*
* Validates exit status of child processes
*/

lib/tst_test.c +1 -0
index 42a54621e..67553f65d 100644
@@ -1365,6 +1365,19 @@ static const char *default_fs_type(void)
 	return tst_dev_fs_type();
 }
 
+bool tst_cmd_present(const char *cmd)

lib/tst_test.c static const char *default_fs_type(void) · line 1378

Quoted 10 lines
lib/tst_test.c +10 -0
+{
+	struct tst_cmd *pcmd = tst_test->needs_cmds;
+
+	while (pcmd->cmd) {
+		if (!strcmp(pcmd->cmd, cmd))
+			return pcmd->present;
+
+		pcmd++;
+	}
+	return false;

For a third time, when we get here we asked for something that haven't been tested for so we should tst_brk() here!

Quoted 8 lines
lib/tst_test.c +2 -0
+}
+
 static void do_setup(int argc, char *argv[])
 {
 	char *tdebug_env = getenv("LTP_ENABLE_DEBUG");
Signature · 1 line
2.52.0
Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#64 lore raw

Re: [LTP] [PATCH v6 2/4] tst_test.c: Add tst_cmd_present check if a command is present

Quoted 12 lines · Cyril Hrubis

Hi!

Quoted 11 lines

/*
* Validates exit status of child processes
*/

lib/tst_test.c +0 -0
index 42a54621e..67553f65d 100644
@@ -1365,6 +1365,19 @@ static const char *default_fs_type(void)
 	return tst_dev_fs_type();
 }
 
Quoted 15 lines

lib/tst_test.c static const char *default_fs_type(void) · line 1378

Quoted 11 lines
lib/tst_test.c +11 -0
+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;

For a third time, when we get here we asked for something that haven't been tested for so we should tst_brk() here!

I think you may have missed my earlier explanation of why we should avoid using tst_brk() here.
https://lore.kernel.org/ltp/[email protected]/

Quoted 12 lines
Quoted 8 lines
lib/tst_test.c +2 -0
+}
+
 static void do_setup(int argc, char *argv[])
 {
 	char *tdebug_env = getenv("LTP_ENABLE_DEBUG");
Signature · 1 line
2.52.0
Signature · 2 lines
Cyril Hrubis
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#65 lore raw

Re: [LTP] [PATCH v6 2/4] tst_test.c: Add tst_cmd_present check if a command is present

Hi all,

...

Quoted 5 lines
Quoted 2 lines

For a third time, when we get here we asked for something that haven't been tested for so we should tst_brk() here!

I think you may have missed my earlier explanation of why we should avoid using tst_brk() here.
https://lore.kernel.org/ltp/[email protected]/

Wei, I got your point but IMHO you're wrong. I replied to the thread to keep discussion on the same place.
https://lore.kernel.org/ltp/20260107080545.GC727950@pevik/

Kind regards,
Petr

Quoted 2 lines
Quoted 2 lines
Signature · 1 line
Cyril Hrubis
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#66 lore raw

Re: [LTP] [PATCH v6 2/4] tst_test.c: Add tst_cmd_present check if a command is present

Hi all,

Quoted 3 lines

Wei, I got your point but IMHO you're wrong. I replied to the thread to keep discussion on the same place.
https://lore.kernel.org/ltp/20260107080545.GC727950@pevik/

And besides the discussion why tst_brk() is needed, we need other sanity checks.

To summarize sanity checks

1) tst_brk for not checked command at the end.
2) tst_brk for NULL otherwise we get segfault. Maybe check also for empty command before attempting to do anything else.

tst_test.c +4 -0
index bf92e6cc92..cf67c2a9f0 100644
@@ -1379,12 +1379,16 @@ bool tst_cmd_present(const char *cmd)
 {
 	struct tst_cmd *pcmd = tst_test->needs_cmds;
 
+	if (!cmd || cmd[0] == '\0')
+		tst_brk(TBROK, "Invalid cmd");
+
 	while (pcmd->cmd) {
 		if (!strcmp(pcmd->cmd, cmd))
 			return pcmd->present;
 
 		pcmd++;
 	}
+	tst_brk(TBROK, "'%s' not checked", cmd);
 	return false;
 }
 

3) Shouldn't we use tst_brk_() to point to the code in the test instead of tst_brk()? If yes, we probably want to have tst_cmd_present_() as library internal (static, with const char *file, const int lineno parameters) which calls tst_brk_() and tst_cmd_present() as public (the only with signature in header).

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#67 lore raw

Re: [LTP] [PATCH v6 2/4] tst_test.c: Add tst_cmd_present check if a command is present

Hi!

Quoted 3 lines

1) tst_brk for not checked command at the end.
2) tst_brk for NULL otherwise we get segfault. Maybe check also for empty command before attempting to do anything else.

Technically the empty command will end up in the tst_brk() at the end, so I would add only the NULL check.

Quoted 21 lines

index bf92e6cc92..cf67c2a9f0 100644

tst_test.c +4 -0
@@ -1379,12 +1379,16 @@ bool tst_cmd_present(const char *cmd)
 {
 	struct tst_cmd *pcmd = tst_test->needs_cmds;
 
+	if (!cmd || cmd[0] == '\0')
+		tst_brk(TBROK, "Invalid cmd");
+
 	while (pcmd->cmd) {
 		if (!strcmp(pcmd->cmd, cmd))
 			return pcmd->present;
 
 		pcmd++;
 	}
+	tst_brk(TBROK, "'%s' not checked", cmd);
 	return false;
 }
 
Quoted 5 lines

3) Shouldn't we use tst_brk_() to point to the code in the test instead of tst_brk()? If yes, we probably want to have tst_cmd_present_() as library internal (static, with const char *file, const int lineno parameters) which calls tst_brk_() and tst_cmd_present() as public (the only with signature in header).

I do not think that this is that important to have, the tst_cmd_present() calls are going to be quite rare and hence easy to locate in the sources.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#68 lore raw

Re: [LTP] [PATCH v6 2/4] tst_test.c: Add tst_cmd_present check if a command is present

Quoted 29 lines · Cyril Hrubis

Hi!

Quoted 3 lines

1) tst_brk for not checked command at the end.
2) tst_brk for NULL otherwise we get segfault. Maybe check also for empty command before attempting to do anything else.

Technically the empty command will end up in the tst_brk() at the end, so I would add only the NULL check.

Quoted 21 lines

index bf92e6cc92..cf67c2a9f0 100644

tst_test.c +4 -0
@@ -1379,12 +1379,16 @@ bool tst_cmd_present(const char *cmd)
 {
 	struct tst_cmd *pcmd = tst_test->needs_cmds;
 
+	if (!cmd || cmd[0] == '\0')
+		tst_brk(TBROK, "Invalid cmd");
+
 	while (pcmd->cmd) {
 		if (!strcmp(pcmd->cmd, cmd))
 			return pcmd->present;
 
 		pcmd++;
 	}
+	tst_brk(TBROK, "'%s' not checked", cmd);
 	return false;
 }
 
Quoted 9 lines
Quoted 5 lines

3) Shouldn't we use tst_brk_() to point to the code in the test instead of tst_brk()? If yes, we probably want to have tst_cmd_present_() as library internal (static, with const char *file, const int lineno parameters) which calls tst_brk_() and tst_cmd_present() as public (the only with signature in header).

I do not think that this is that important to have, the tst_cmd_present() calls are going to be quite rare and hence easy to locate in the sources.

Thanks @Petr @Cyril, will send next patchset.

Quoted 4 lines
Signature · 3 lines
-- 
Cyril Hrubis
[email protected]
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#69 lore raw

Re: [LTP] [PATCH v6 2/4] tst_test.c: Add tst_cmd_present check if a command is present

In reply to Cyril Hrubis
Quoted 7 lines

Hi!

Quoted 3 lines

1) tst_brk for not checked command at the end.
2) tst_brk for NULL otherwise we get segfault. Maybe check also for empty command before attempting to do anything else.

Technically the empty command will end up in the tst_brk() at the end, so I would add only the NULL check.

Sure, just the message will be a bit confusing:
'' not checked.

That's why I checked also for empty command.
Anyway, v7 is out with cmd[0] == '\0' as well. But it can be removed during merge.
https://lore.kernel.org/ltp/[email protected]/

Quoted 27 lines
Quoted 21 lines

index bf92e6cc92..cf67c2a9f0 100644

tst_test.c +4 -0
@@ -1379,12 +1379,16 @@ bool tst_cmd_present(const char *cmd)
 {
 	struct tst_cmd *pcmd = tst_test->needs_cmds;
 
+	if (!cmd || cmd[0] == '\0')
+		tst_brk(TBROK, "Invalid cmd");
+
 	while (pcmd->cmd) {
 		if (!strcmp(pcmd->cmd, cmd))
 			return pcmd->present;
 
 		pcmd++;
 	}
+	tst_brk(TBROK, "'%s' not checked", cmd);
 	return false;
 }
 
Quoted 5 lines

3) Shouldn't we use tst_brk_() to point to the code in the test instead of tst_brk()? If yes, we probably want to have tst_cmd_present_() as library internal (static, with const char *file, const int lineno parameters) which calls tst_brk_() and tst_cmd_present() as public (the only with signature in header).

Quoted 3 lines

I do not think that this is that important to have, the tst_cmd_present() calls are going to be quite rare and hence easy to locate in the sources.

OK, that simplifies things.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#70 lore raw

Re: [LTP] [PATCH v6 2/4] tst_test.c: Add tst_cmd_present check if a command is present

In reply to Wei Gao via ltp

Hi!

Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
lib/tst_test.c +6 -0
+bool tst_cmd_present(const char *cmd)
+{
+	struct tst_cmd *pcmd = tst_test->needs_cmds;
+
+	while (pcmd->cmd) {
+		if (!strcmp(pcmd->cmd, cmd))
Quoted 9 lines

lib/tst_test.c static const char *default_fs_type(void) · line 1378

Quoted 5 lines
lib/tst_test.c +5 -0
+			return pcmd->present;
+
+		pcmd++;
+	}
+	return false;

For a third time, when we get here we asked for something that haven't been tested for so we should tst_brk() here!

Quoted 2 lines

I think you may have missed my earlier explanation of why we should avoid using tst_brk() here.
https://lore.kernel.org/ltp/[email protected]/

Indeed I missed that one, but that is incorrect, we will get to this point only if we asked for a command that wasn't added to the tst_test.needs_cmds array, which is a programming mistake and test should report TBROK in that case.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#71 lore raw

PATCH v6 3/4 ioctl_loop01.c: Add new support .needs_cmds

In reply to Wei Gao via ltp
---
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 34 +++++++++----------
 1 file changed, 17 insertions(+), 17 deletions(-)
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +17 -17
index 9fbdbb1f2..b8df2d633 100644
@@ -79,23 +79,6 @@ static void check_loop_value(int set_flag, int get_flag, int autoclear_field)
 
 static void verify_ioctl_loop(void)
 {
-	int ret;
-	const char *const cmd_parted[] = {"parted", "-s", dev_path, "mklabel", "msdos", "mkpart",
-					"primary", "ext4", "1M", "10M", NULL};
-
-	tst_fill_file("test.img", 0, 1024 * 1024, 10);
-	tst_attach_device(dev_path, "test.img");
-
-	ret = tst_cmd(cmd_parted, NULL, NULL, TST_CMD_PASS_RETVAL);
-	if (!ret)
-		parted_sup = 1;
-	else if (ret == 255)
-		tst_res(TCONF, "parted binary not installed or failed");
-	else
-		tst_res(TCONF, "parted exited with %i", ret);
-
-	attach_flag = 1;
-
 	TST_ASSERT_INT(partscan_path, 0);
 	TST_ASSERT_INT(autoclear_path, 0);
 	TST_ASSERT_STR(backing_path, backing_file_path);
@@ -114,10 +97,23 @@ static void verify_ioctl_loop(void)
 
 static void setup(void)
 {
+	parted_sup = tst_cmd_present("parted");
+
+	const char *const cmd_parted[] = {"parted", "-s", dev_path, "mklabel", "msdos", "mkpart",
+	                                  "primary", "ext4", "1M", "10M", NULL};
+
 	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
 	if (dev_num < 0)
 		tst_brk(TBROK, "Failed to find free loop device");
 
+	tst_fill_file("test.img", 0, 1024 * 1024, 10);
+
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+
+	if (parted_sup)
+		SAFE_CMD(cmd_parted, NULL, NULL);
+
 	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
 	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);
 	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
@@ -149,5 +145,9 @@ static struct tst_test test = {
 		{"linux-git", "6ac92fb5cdff"},
 		{}
 	},
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "parted", .optional = 1},
+		{}
+	},
 	.needs_tmpdir = 1,
 };
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v6 3/4
#72 lore raw

Re: [LTP] [PATCH v6 3/4] ioctl_loop01.c: Add new support .needs_cmds

Hi Wei,

(The same as in v5, where I added my RBT. FYI you can carry RBT on the commits without changes).

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#73 lore raw

PATCH v6 4/4 shell_loader_cmd.sh: New test case check needs_cmds

In reply to Wei Gao via ltp
---
 testcases/lib/run_tests.sh              |  1 +
 testcases/lib/tests/shell_loader_cmd.sh | 28 +++++++++++++++++++++++++
 2 files changed, 29 insertions(+)
 create mode 100755 testcases/lib/tests/shell_loader_cmd.sh
testcases/lib/run_tests.sh +1 -0
index 5c309bbeb..24ac81a44 100755
@@ -6,6 +6,7 @@ shell_loader.sh
 shell_loader_all_filesystems.sh
 shell_loader_c_child.sh
 shell_loader_filesystems.sh
+shell_loader_cmd.sh
 shell_loader_kconfigs.sh
 shell_loader_supported_archs.sh
 shell_loader_tcnt.sh
testcases/lib/tests/shell_loader_cmd.sh new file +28 -0
new file mode 100755
index 000000000..4e8e61eb6
@@ -0,0 +1,28 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2025 Wei Gao <[email protected]>
+#
+# ---
+# env
+# {
+#  "needs_cmds": [
+#   {
+#    "cmd": "ls",
+#    "optional": 1
+#   },
+#   {
+#    "cmd": "mkfs.ext4 >= 1.0.0",
+#    "optional": 1
+#   }
+#  ]
+# }
+# ---
+
+. tst_loader.sh
+
+tst_test()
+{
+	tst_res TPASS "We are running with needs_cmds"
+}
+
+. tst_run.sh
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v6 4/4
#74 lore raw

Re: [LTP] [PATCH v6 4/4] shell_loader_cmd.sh: New test case check needs_cmds

Hi Wei,

(The same as in v5, where I added my RBT. FYI you can carry RBT on the commits without changes).

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#75 lore raw

v7 cover new cmd support option for needs_cmds

In reply to Wei Gao via ltp

v6->v7:
1) Add tst_brk in tst_cmd_present
2) Remove "present" attribute from the json parser

Wei Gao (4):
lib: Add support option for .needs_cmds
tst_test.c: Add tst_cmd_present check if a command is present ioctl_loop01.c: Add new support .needs_cmds shell_loader_cmd.sh: New test case check needs_cmds

 include/tst_cmd.h                             | 13 +++++
 include/tst_test.h                            | 16 +++++-
 lib/newlib_tests/tst_needs_cmds01.c           | 16 +++---
 lib/newlib_tests/tst_needs_cmds02.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds03.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds04.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds05.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds06.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds07.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds08.c           |  8 +--
 lib/tst_test.c                                | 27 +++++++--
 testcases/kernel/device-drivers/zram/zram03.c |  8 +--
 testcases/kernel/fs/squashfs/squashfs01.c     |  6 +-
 testcases/kernel/security/aslr/aslr01.c       |  6 +-
 testcases/kernel/syscalls/add_key/add_key05.c | 10 ++--
 .../kernel/syscalls/fanotify/fanotify22.c     |  6 +-
 testcases/kernel/syscalls/ioctl/ioctl09.c     |  6 +-
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 34 +++++------
 testcases/kernel/syscalls/madvise/madvise11.c |  8 +--
 .../kernel/syscalls/quotactl/quotactl01.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl04.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl06.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl08.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl09.c     |  6 +-
 testcases/kernel/syscalls/statx/statx05.c     |  8 +--
 testcases/kernel/syscalls/statx/statx07.c     |  6 +-
 testcases/kernel/syscalls/statx/statx09.c     |  6 +-
 testcases/lib/run_tests.sh                    |  1 +
 testcases/lib/tests/shell_loader_cmd.sh       | 28 ++++++++++
 testcases/lib/tst_run_shell.c                 | 56 ++++++++++++++++++-
 30 files changed, 231 insertions(+), 104 deletions(-)
 create mode 100755 testcases/lib/tests/shell_loader_cmd.sh
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#76 lore raw

PATCH v7 1/4 lib: Add support option for .needs_cmds

---
 include/tst_cmd.h                             | 13 +++++
 include/tst_test.h                            |  4 +-
 lib/newlib_tests/tst_needs_cmds01.c           | 16 +++---
 lib/newlib_tests/tst_needs_cmds02.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds03.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds04.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds05.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds06.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds07.c           |  6 +-
 lib/newlib_tests/tst_needs_cmds08.c           |  8 +--
 lib/tst_test.c                                |  9 +--
 testcases/kernel/device-drivers/zram/zram03.c |  8 +--
 testcases/kernel/fs/squashfs/squashfs01.c     |  6 +-
 testcases/kernel/security/aslr/aslr01.c       |  6 +-
 testcases/kernel/syscalls/add_key/add_key05.c | 10 ++--
 .../kernel/syscalls/fanotify/fanotify22.c     |  6 +-
 testcases/kernel/syscalls/ioctl/ioctl09.c     |  6 +-
 testcases/kernel/syscalls/madvise/madvise11.c |  8 +--
 .../kernel/syscalls/quotactl/quotactl01.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl04.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl06.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl08.c     |  6 +-
 .../kernel/syscalls/quotactl/quotactl09.c     |  6 +-
 testcases/kernel/syscalls/statx/statx05.c     |  8 +--
 testcases/kernel/syscalls/statx/statx07.c     |  6 +-
 testcases/kernel/syscalls/statx/statx09.c     |  6 +-
 testcases/lib/tst_run_shell.c                 | 56 ++++++++++++++++++-
 27 files changed, 155 insertions(+), 87 deletions(-)
include/tst_cmd.h +13 -0
index cf6382955..2203241c5 100644
@@ -19,6 +19,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.
  *
include/tst_test.h +2 -2
index 75bc57ab6..dc2e9a397 100644
@@ -526,7 +526,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 :ref:`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.
@@ -619,7 +619,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;
 
lib/newlib_tests/tst_needs_cmds01.c +8 -8
index 777c69505..6ab1145d1 100644
@@ -12,13 +12,13 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4",
-		"mkfs.ext4 >= 1.0.0",
-		"mkfs.ext4 <= 2.0.0",
-		"mkfs.ext4 != 2.0.0",
-		"mkfs.ext4 > 1.0.0",
-		"mkfs.ext4 < 2.0.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4", .optional = 1},
+		{.cmd = "mkfs.ext4 >= 1.0.0", .optional = 0},
+		{.cmd = "mkfs.ext4 <= 2.0.0"},
+		{.cmd = "mkfs.ext4 != 2.0.0"},
+		{.cmd = "mkfs.ext4 > 1.0.0"},
+		{.cmd = "mkfs.ext4 < 2.0.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds02.c +3 -3
index 455a275ea..f1d6105c2 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext45 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext45 >= 1.43.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds03.c +3 -3
index bdc1cdf6a..ac5f368e2 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 ! 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 ! 1.43.0"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds04.c +3 -3
index de10b8f3e..2aea51772 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds05.c +3 -3
index c3b2b3b9a..969d4e2f5 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43.0-1",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43.0-1"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds06.c +3 -3
index 40b1cf09c..91470ccf9 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 > 1.43.0 2",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 > 1.43.0 2"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds07.c +3 -3
index d0b4ce2ff..371bbdc1b 100644
@@ -16,8 +16,8 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext45",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext45"},
+		{}
 	}
 };
lib/newlib_tests/tst_needs_cmds08.c +4 -4
index 38df2ef6d..412f9f293 100644
@@ -18,9 +18,9 @@ static void do_test(void)
 
 static struct tst_test test = {
 	.test_all = do_test,
-	.needs_cmds = (const char *[]) {
-		"mkfs.xfs",
-		"mkfs.xfs >= 4.20.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.xfs"},
+		{.cmd = "mkfs.xfs >= 4.20.0"},
+		{}
 	}
 };
lib/tst_test.c +5 -4
index e115ce689..09aa7fce2 100644
@@ -1449,11 +1449,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) ? 1 : 0;
+			pcmd++;
+		}
 	}
 
 	if (tst_test->needs_drivers) {
testcases/kernel/device-drivers/zram/zram03.c +4 -4
index 8cf26de4c..4dcf23b4e 100644
@@ -240,9 +240,9 @@ static struct tst_test test = {
 		"zram",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"modprobe",
-		"rmmod",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "modprobe"},
+		{.cmd = "rmmod"},
+		{}
 	}
 };
testcases/kernel/fs/squashfs/squashfs01.c +3 -3
index fbcb76582..7a548d62c 100644
@@ -102,9 +102,9 @@ static struct tst_test test = {
 	.needs_root = 1,
 	.needs_device = 1,
 	.dev_min_size = 1,
-	.needs_cmds = (const char *const []) {
-		"mksquashfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mksquashfs"},
+		{}
 	},
 	.needs_drivers = (const char *const []) {
 		"squashfs",
testcases/kernel/security/aslr/aslr01.c +3 -3
index 6a396e29d..e6feb2d3c 100644
@@ -277,8 +277,8 @@ static struct tst_test test = {
 		"CONFIG_HAVE_ARCH_MMAP_RND_BITS=y",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"ldd",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "ldd"},
+		{}
 	},
 };
testcases/kernel/syscalls/add_key/add_key05.c +5 -5
index c9a2f840e..b51a84b97 100644
@@ -231,11 +231,11 @@ static struct tst_test test = {
 		{&user_buf, .size = 64},
 		{}
 	},
-	.needs_cmds = (const char *const []) {
-		"useradd",
-		"userdel",
-		"groupdel",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "useradd"},
+		{.cmd = "userdel"},
+		{.cmd = "groupdel"},
+		{}
 	},
 	.tags = (const struct tst_tag[]) {
 		{"linux-git", "a08bf91ce28"},
testcases/kernel/syscalls/fanotify/fanotify22.c +3 -3
index 357e74dbc..6578474a7 100644
@@ -327,9 +327,9 @@ static struct tst_test test = {
 		{"linux-git", "76486b104168"},
 		{}
 	},
-	.needs_cmds = (const char *[]) {
-		"debugfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "debugfs"},
+		{}
 	}
 };
 
testcases/kernel/syscalls/ioctl/ioctl09.c +3 -3
index c046d8959..21e16efb6 100644
@@ -108,9 +108,9 @@ static struct tst_test test = {
 		"loop",
 		NULL
 	},
-	.needs_cmds = (const char *const []) {
-		"parted",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "parted"},
+		{}
 	},
 	.needs_tmpdir = 1,
 };
testcases/kernel/syscalls/madvise/madvise11.c +4 -4
index 422589f5b..73d2d4294 100644
@@ -421,10 +421,10 @@ static struct tst_test test = {
 		HW_MODULE,
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"modprobe",
-		"rmmod",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "modprobe"},
+		{.cmd = "rmmod"},
+		{}
 	},
 	.needs_kconfigs = (const char *[]) {
 		"CONFIG_MEMORY_FAILURE=y",
testcases/kernel/syscalls/quotactl/quotactl01.c +3 -3
index 9dcf74ceb..3091ac5bf 100644
@@ -223,9 +223,9 @@ static struct tst_test test = {
 		{}
 	},
 	.mntpoint = MNTPOINT,
-	.needs_cmds = (const char *const []) {
-		"quotacheck",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "quotacheck"},
+		{}
 	},
 	.setup = setup,
 	.cleanup = cleanup,
testcases/kernel/syscalls/quotactl/quotactl04.c +3 -3
index d2d7b3f3e..a70903330 100644
@@ -165,8 +165,8 @@ static struct tst_test test = {
 	},
 	.mntpoint = MNTPOINT,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/quotactl/quotactl06.c +3 -3
index 110a3aa9b..333f5720d 100644
@@ -226,9 +226,9 @@ static struct tst_test test = {
 	},
 	.mntpoint = MNTPOINT,
 	.mount_device = 1,
-	.needs_cmds = (const char *const []) {
-		"quotacheck",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "quotacheck"},
+		{}
 	},
 	.needs_root = 1,
 	.test_variants = QUOTACTL_FMT_VARIANTS,
testcases/kernel/syscalls/quotactl/quotactl08.c +3 -3
index 63087345f..ce7a8b008 100644
@@ -220,8 +220,8 @@ static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/quotactl/quotactl09.c +3 -3
index 673666347..fd6629c0c 100644
@@ -185,8 +185,8 @@ static struct tst_test test = {
 	.mount_device = 1,
 	.needs_root = 1,
 	.test_variants = QUOTACTL_SYSCALL_VARIANTS,
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx05.c +4 -4
index 2a4603226..07b5e7b94 100644
@@ -123,9 +123,9 @@ static struct tst_test test = {
 		{.type = "ext4"},
 		{}
 	},
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.43.0",
-		"e4crypt",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.43.0"},
+		{.cmd = "e4crypt"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx07.c +3 -3
index bab64591f..755644cf9 100644
@@ -171,8 +171,8 @@ static struct tst_test test = {
 		{}
 	},
 	.needs_root = 1,
-	.needs_cmds = (const char *[]) {
-		"exportfs",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "exportfs"},
+		{}
 	}
 };
testcases/kernel/syscalls/statx/statx09.c +3 -3
index ee4be4250..8838d5450 100644
@@ -162,8 +162,8 @@ static struct tst_test test = {
 		"CONFIG_FS_VERITY",
 		NULL
 	},
-	.needs_cmds = (const char *[]) {
-		"mkfs.ext4 >= 1.45.2",
-		NULL
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "mkfs.ext4 >= 1.45.2"},
+		{}
 	}
 };
testcases/lib/tst_run_shell.c +55 -1

74 lines not on this page · raw message

Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v7 1/4
#77 lore raw

Re: [LTP] [PATCH v7 1/4] lib: Add support option for .needs_cmds

Hi Wei,

I'm sorry, I merged old patch which unfortunately touched the files you modified. Therefore CI fails. OTOH the basic rule is always rebase to the current master before sending a patch. Please remember this for next time.

For now, my fork has your branch rebased: wei/needs_cmds.v7.rebased
https://github.com/pevik/ltp/tree/wei/needs_cmds.v7.rebased

Thanks for removing present attribute.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#80 lore raw

Re: [LTP] [PATCH v7 1/4] lib: Add support option for .needs_cmds

In reply to Wei Gao via ltp

Hi!

testcases/lib/tst_run_shell.c static ujson_obj fs_obj = { · line 189

Quoted 8 lines
testcases/lib/tst_run_shell.c +8 -0
+enum cmd_ids {
+	CMD,
+	OPTIONAL,
+};
+
+static ujson_obj_attr cmd_attrs[] = {
+	UJSON_OBJ_ATTR_IDX(CMD, "cmd", UJSON_STR),
+	UJSON_OBJ_ATTR_IDX(OPTIONAL, "optional", UJSON_INT),

I guess that it would be cleaner to map this to BOOL, and adjust metadata parser to match the value. I guess that I can send a separate patch to adjust these two once this is pushed though.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#81 lore raw

Re: [LTP] [PATCH v7 1/4] lib: Add support option for .needs_cmds

Quoted 13 lines

Hi!

testcases/lib/tst_run_shell.c static ujson_obj fs_obj = { · line 189

Quoted 8 lines
testcases/lib/tst_run_shell.c +8 -0
+enum cmd_ids {
+	CMD,
+	OPTIONAL,
+};
+
+static ujson_obj_attr cmd_attrs[] = {
+	UJSON_OBJ_ATTR_IDX(CMD, "cmd", UJSON_STR),
+	UJSON_OBJ_ATTR_IDX(OPTIONAL, "optional", UJSON_INT),

I guess that it would be cleaner to map this to BOOL, and adjust metadata parser to match the value. I guess that I can send a separate patch to adjust these two once this is pushed though.

Good point. I also agree to merge this and fix in a follow up. If you add your RBT, I'll merge whole patchset.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#82 lore raw

PATCH v7 2/4 tst_test.c: Add tst_cmd_present check if a command is present

In reply to Wei Gao via ltp
---
 include/tst_test.h | 12 ++++++++++++
 lib/tst_test.c     | 18 ++++++++++++++++++
 2 files changed, 30 insertions(+)
include/tst_test.h +12 -0
index dc2e9a397..d15bf87fd 100644
@@ -726,6 +726,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
+ * &tst_cmd->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
  */
lib/tst_test.c +18 -0
index 09aa7fce2..0c2bbbbd6 100644
@@ -1375,6 +1375,24 @@ 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;
+
+	if (!cmd || cmd[0] == '\0')
+		tst_brk(TBROK, "Invalid cmd");
+
+	while (pcmd->cmd) {
+		if (!strcmp(pcmd->cmd, cmd))
+			return pcmd->present;
+
+		pcmd++;
+	}
+
+	tst_brk(TBROK, "'%s' not checked", cmd);
+	return false;
+}
+
 static void do_setup(int argc, char *argv[])
 {
 	char *tdebug_env = getenv("LTP_ENABLE_DEBUG");
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#83 lore raw

Re: [LTP] [PATCH v7 2/4] tst_test.c: Add tst_cmd_present check if a command is present

Hi Wei,

...

lib/tst_test.c static const char *default_fs_type(void) · line 1383

Quoted 11 lines

+++ b/lib/tst_test.c

lib/tst_test.c +6 -0
@@ -1375,6 +1375,24 @@ 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;
+
+	if (!cmd || cmd[0] == '\0')
+		tst_brk(TBROK, "Invalid cmd");

I'd still prefer tst_brk_() as I described at v6, but that can be done as a separate change. I keep my RBT :).

Kind regards,
Petr

Quoted 15 lines
lib/tst_test.c +12 -0
+
+	while (pcmd->cmd) {
+		if (!strcmp(pcmd->cmd, cmd))
+			return pcmd->present;
+
+		pcmd++;
+	}
+
+	tst_brk(TBROK, "'%s' not checked", cmd);
+	return false;
+}
+
 static void do_setup(int argc, char *argv[])
 {
 	char *tdebug_env = getenv("LTP_ENABLE_DEBUG");
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#84 lore raw

Re: [LTP] [PATCH v7 2/4] tst_test.c: Add tst_cmd_present check if a command is present

Quoted 12 lines

...

Quoted 10 lines

+++ b/lib/tst_test.c

lib/tst_test.c +5 -0
@@ -1375,6 +1375,24 @@ 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;
+
+	if (!cmd || cmd[0] == '\0')
Quoted 3 lines

lib/tst_test.c static const char *default_fs_type(void) · line 1383

Quoted 1 line
lib/tst_test.c +1 -0
+		tst_brk(TBROK, "Invalid cmd");

I'd still prefer tst_brk_() as I described at v6, but that can be done as a separate change. I keep my RBT :).

I see Cyril was OK with keeping tst_brk() in v6, I'm sorry to overlook it.
https://lore.kernel.org/ltp/[email protected]/

Quoted 2 lines

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#85 lore raw

Re: [LTP] [PATCH v7 2/4] tst_test.c: Add tst_cmd_present check if a command is present

In reply to Wei Gao via ltp

Hi Wei,

I don't get the point why we needs seperately define the tst_cmd_present() function, from what I understand, as long as we fill "commands" in the new struct field, that would be completed the check and store the state in .present, right?

Shouldn't we use that .present value in testcase directly?

Quoted 67 lines · Wei Gao via ltp
---
 include/tst_test.h | 12 ++++++++++++
 lib/tst_test.c     | 18 ++++++++++++++++++
 2 files changed, 30 insertions(+)
include/tst_test.h +12 -0
index dc2e9a397..d15bf87fd 100644
@@ -726,6 +726,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
+ * &tst_cmd->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
  */
lib/tst_test.c +18 -1
index 09aa7fce2..0c2bbbbd6 100644
@@ -1375,6 +1375,24 @@ 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;
+
+       if (!cmd || cmd[0] == '\0')
+               tst_brk(TBROK, "Invalid cmd");
+
+       while (pcmd->cmd) {
+               if (!strcmp(pcmd->cmd, cmd))
+                       return pcmd->present;
+
+               pcmd++;
+       }
+
+       tst_brk(TBROK, "'%s' not checked", cmd);
+       return false;
+}
+
 static void do_setup(int argc, char *argv[])
 {
        char *tdebug_env = getenv("LTP_ENABLE_DEBUG");
--

2.52.0

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

Signature · 6 lines
Regards,
Li Wang


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#87 lore raw

PATCH v7 3/4 ioctl_loop01.c: Add new support .needs_cmds

In reply to Wei Gao via ltp
---
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 34 +++++++++----------
 1 file changed, 17 insertions(+), 17 deletions(-)
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +17 -17
index 9fbdbb1f2..b8df2d633 100644
@@ -79,23 +79,6 @@ static void check_loop_value(int set_flag, int get_flag, int autoclear_field)
 
 static void verify_ioctl_loop(void)
 {
-	int ret;
-	const char *const cmd_parted[] = {"parted", "-s", dev_path, "mklabel", "msdos", "mkpart",
-					"primary", "ext4", "1M", "10M", NULL};
-
-	tst_fill_file("test.img", 0, 1024 * 1024, 10);
-	tst_attach_device(dev_path, "test.img");
-
-	ret = tst_cmd(cmd_parted, NULL, NULL, TST_CMD_PASS_RETVAL);
-	if (!ret)
-		parted_sup = 1;
-	else if (ret == 255)
-		tst_res(TCONF, "parted binary not installed or failed");
-	else
-		tst_res(TCONF, "parted exited with %i", ret);
-
-	attach_flag = 1;
-
 	TST_ASSERT_INT(partscan_path, 0);
 	TST_ASSERT_INT(autoclear_path, 0);
 	TST_ASSERT_STR(backing_path, backing_file_path);
@@ -114,10 +97,23 @@ static void verify_ioctl_loop(void)
 
 static void setup(void)
 {
+	parted_sup = tst_cmd_present("parted");
+
+	const char *const cmd_parted[] = {"parted", "-s", dev_path, "mklabel", "msdos", "mkpart",
+	                                  "primary", "ext4", "1M", "10M", NULL};
+
 	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
 	if (dev_num < 0)
 		tst_brk(TBROK, "Failed to find free loop device");
 
+	tst_fill_file("test.img", 0, 1024 * 1024, 10);
+
+	tst_attach_device(dev_path, "test.img");
+	attach_flag = 1;
+
+	if (parted_sup)
+		SAFE_CMD(cmd_parted, NULL, NULL);
+
 	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
 	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);
 	sprintf(backing_path, "/sys/block/loop%d/loop/backing_file", dev_num);
@@ -149,5 +145,9 @@ static struct tst_test test = {
 		{"linux-git", "6ac92fb5cdff"},
 		{}
 	},
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "parted", .optional = 1},
+		{}
+	},
 	.needs_tmpdir = 1,
 };
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#88 lore raw

Re: [LTP] [PATCH v7 3/4] ioctl_loop01.c: Add new support .needs_cmds

testcases/kernel/syscalls/ioctl/ioctl_loop01.c static void verify_ioctl_loop(void) · line 100

Quoted 8 lines
        TST_ASSERT_INT(partscan_path, 0);
        TST_ASSERT_INT(autoclear_path, 0);
        TST_ASSERT_STR(backing_path, backing_file_path);
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +1 -0
@@ -114,10 +97,23 @@ static void verify_ioctl_loop(void)
 
 static void setup(void)
 {
+       parted_sup = tst_cmd_present("parted");

In the logic of tst_test.c, there already did check and store the value in tst_test->needs_cmds.present, so why here do the check seperately again?

+ struct tst_cmd *pcmd = tst_test->needs_cmds;
+ while (pcmd->cmd) {
+ pcmd->present = tst_check_cmd(pcmd->cmd,
!pcmd->optional) ? 1 : 0;
+ pcmd++;
+ }

testcases/kernel/syscalls/ioctl/ioctl_loop01.c static void verify_ioctl_loop(void) · line 114

Quoted 13 lines
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +9 -0
+       const char *const cmd_parted[] = {"parted", "-s", dev_path, "mklabel", "msdos", "mkpart",
+                                         "primary", "ext4", "1M", "10M", NULL};
+
        dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
        if (dev_num < 0)
                tst_brk(TBROK, "Failed to find free loop device");
 
+       tst_fill_file("test.img", 0, 1024 * 1024, 10);
+
+       tst_attach_device(dev_path, "test.img");
+       attach_flag = 1;
+
+       if (parted_sup)

Can we just reuse the 'tst_test->needs_cmds.present' result?

Quoted 1 line
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +1 -0
+               SAFE_CMD(cmd_parted, NULL, NULL);
Signature · 6 lines
Regards,
Li Wang


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#89 lore raw

Re: [LTP] [PATCH v7 3/4] ioctl_loop01.c: Add new support .needs_cmds

Quoted 19 lines · Li Wang

testcases/kernel/syscalls/ioctl/ioctl_loop01.c static void verify_ioctl_loop(void) · line 100

Quoted 8 lines
        TST_ASSERT_INT(partscan_path, 0);
        TST_ASSERT_INT(autoclear_path, 0);
        TST_ASSERT_STR(backing_path, backing_file_path);
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +1 -0
@@ -114,10 +97,23 @@ static void verify_ioctl_loop(void)
 
 static void setup(void)
 {
+       parted_sup = tst_cmd_present("parted");

In the logic of tst_test.c, there already did check and store the value in tst_test->needs_cmds.present, so why here do the check seperately again?

(quoted diff) +3 -0
+               struct tst_cmd *pcmd = tst_test->needs_cmds;
+               while (pcmd->cmd) {
+                       pcmd->present = tst_check_cmd(pcmd->cmd,

!pcmd->optional) ? 1 : 0;

Review

Quoted 2 lines
(quoted diff) +2 -0
+                       pcmd++;
+               }

Let me clarify, and please correct me if I’m mistaken :) tst_cmd_present not do tst_check_cmd again but just go through list find specific cmd such as "parted" present status.

Quoted 8 lines
Quoted 8 lines
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +3 -0
+       const char *const cmd_parted[] = {"parted", "-s", dev_path, "mklabel", "msdos", "mkpart",
+                                         "primary", "ext4", "1M", "10M", NULL};
+
        dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
        if (dev_num < 0)
                tst_brk(TBROK, "Failed to find free loop device");
 
Quoted 9 lines

testcases/kernel/syscalls/ioctl/ioctl_loop01.c static void verify_ioctl_loop(void) · line 114

Quoted 6 lines
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +6 -0
+       tst_fill_file("test.img", 0, 1024 * 1024, 10);
+
+       tst_attach_device(dev_path, "test.img");
+       attach_flag = 1;
+
+       if (parted_sup)

Can we just reuse the 'tst_test->needs_cmds.present' result?

You cannot access tst_test->needs_cmds.present directly as if it were a single variable, because needs_cmds is an array (a list) of structures. To get the status of a specific command like "parted", you must iterate through that list to find the entry matching that name.

Quoted 7 lines
Quoted 1 line
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +1 -0
+               SAFE_CMD(cmd_parted, NULL, NULL);
Signature · 2 lines
Regards,
Li Wang
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#90 lore raw

Re: [LTP] [PATCH v7 3/4] ioctl_loop01.c: Add new support .needs_cmds

Quoted 10 lines · Wei Gao
Quoted 3 lines

Can we just reuse the 'tst_test->needs_cmds.present' result?

You cannot access tst_test->needs_cmds.present directly as if it were a single variable,
because needs_cmds is an array (a list) of structures. To get the status of a specific command
like "parted", you must iterate through that list to find the entry matching that name.

So how about using: tst_test->needs_cmds[0].present?

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#91 lore raw

Re: [LTP] [PATCH v7 3/4] ioctl_loop01.c: Add new support .needs_cmds

Quoted 14 lines · Li Wang
Quoted 10 lines · Wei Gao
Quoted 3 lines

Can we just reuse the 'tst_test->needs_cmds.present' result?

You cannot access tst_test->needs_cmds.present directly as if it were a single variable,
because needs_cmds is an array (a list) of structures. To get the status of a specific command
like "parted", you must iterate through that list to find the entry matching that name.

Quoted 1 line

So how about using: tst_test->needs_cmds[0].present?

Yes, i think direct indexing is more efficient, but helper function is more readable and extensible(If someone add more entry into .needs_cmds it possible break the test case).
I would like to get more feedback on this topic before next patch. @Petr @Cyril ?

Quoted 5 lines
Signature · 3 lines
-- 
Regards,
Li Wang
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#92 lore raw

Re: [LTP] [PATCH v7 3/4] ioctl_loop01.c: Add new support .needs_cmds

Quoted 10 lines · Wei Gao
Quoted 10 lines
Quoted 9 lines
Quoted 2 lines

Can we just reuse the 'tst_test->needs_cmds.present' result?

You cannot access tst_test->needs_cmds.present directly as if it were a single variable,
because needs_cmds is an array (a list) of structures. To get the status of a specific command
like "parted", you must iterate through that list to find the entry matching that name.

Quoted 5 lines
Quoted 1 line

So how about using: tst_test->needs_cmds[0].present?

Yes, i think direct indexing is more efficient, but helper function is more readable and extensible(If someone add more entry into .needs_cmds it possible break the test case).

Yes, but not entirely, once we have the function tst_cmd_present(), we need to manually write the "commands" again, and easy to typo.

Even if we plan to add this feature, perhaps it's better not to place it in tst_test.h or tst_test.c, as that may not be the best fit.

We should respect the principle of high cohesion and low coupling.

Signature · 6 lines
Regards,
Li Wang


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#95 lore raw

PATCH v7 4/4 shell_loader_cmd.sh: New test case check needs_cmds

In reply to Wei Gao via ltp
---
 testcases/lib/run_tests.sh              |  1 +
 testcases/lib/tests/shell_loader_cmd.sh | 28 +++++++++++++++++++++++++
 2 files changed, 29 insertions(+)
 create mode 100755 testcases/lib/tests/shell_loader_cmd.sh
testcases/lib/run_tests.sh +1 -0
index 5c309bbeb..24ac81a44 100755
@@ -6,6 +6,7 @@ shell_loader.sh
 shell_loader_all_filesystems.sh
 shell_loader_c_child.sh
 shell_loader_filesystems.sh
+shell_loader_cmd.sh
 shell_loader_kconfigs.sh
 shell_loader_supported_archs.sh
 shell_loader_tcnt.sh
testcases/lib/tests/shell_loader_cmd.sh new file +28 -0
new file mode 100755
index 000000000..4e8e61eb6
@@ -0,0 +1,28 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2025 Wei Gao <[email protected]>
+#
+# ---
+# env
+# {
+#  "needs_cmds": [
+#   {
+#    "cmd": "ls",
+#    "optional": 1
+#   },
+#   {
+#    "cmd": "mkfs.ext4 >= 1.0.0",
+#    "optional": 1
+#   }
+#  ]
+# }
+# ---
+
+. tst_loader.sh
+
+tst_test()
+{
+	tst_res TPASS "We are running with needs_cmds"
+}
+
+. tst_run.sh
Signature · 5 lines
2.52.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#97 lore raw

Re: [LTP] [PATCH v7 4/4] shell_loader_cmd.sh: New test case check needs_cmds

Hi Cyril, Wei,

thanks, merged!

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#98 lore raw

PATCH v2 2/2 ioctl_loop01.c: Update to new .needs_cmds struct

In reply to Wei Gao via ltp
---
 .../kernel/syscalls/ioctl/ioctl_loop01.c      | 23 ++++++++-----------
 1 file changed, 9 insertions(+), 14 deletions(-)
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +9 -14
index c9137bf1e..70036d6ef 100644
@@ -97,9 +97,10 @@ static void verify_ioctl_loop(void)
 
 static void setup(void)
 {
-	int ret;
+	parted_sup = tst_cmd_present("parted");
+
 	const char *const cmd_parted[] = {"parted", "-s", "test.img", "mklabel", "msdos", "mkpart",
-	                                  "primary", "ext4", "1M", "10M", NULL};
+					"primary", "ext4", "1M", "10M", NULL};
 
 	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
 	if (dev_num < 0)
@@ -107,18 +108,8 @@ static void setup(void)
 
 	tst_fill_file("test.img", 0, 1024 * 1024, 10);
 
-	ret = tst_cmd(cmd_parted, NULL, NULL, TST_CMD_PASS_RETVAL);
-	switch (ret) {
-	case 0:
-		parted_sup = 1;
-	break;
-	case 255:
-		tst_res(TCONF, "parted binary not installed or failed");
-	break;
-	default:
-		tst_res(TCONF, "parted exited with %i", ret);
-	break;
-	}
+	if (parted_sup)
+		SAFE_CMD(cmd_parted, NULL, NULL);
 
 	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
 	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);
@@ -152,5 +143,9 @@ static struct tst_test test = {
 		{"linux-git", "6ac92fb5cdff"},
 		{}
 	},
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "parted", .optional = 1},
+		{}
+	},
 	.needs_tmpdir = 1,
 };
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v2 2/2
#99 lore raw

Re: [LTP] [PATCH v2 2/2] ioctl_loop01.c: Update to new .needs_cmds struct

Hi Wei,

...

Review static void verify_ioctl_loop(void)

Quoted 2 lines

+++ b/testcases/kernel/syscalls/ioctl/ioctl_loop01.c

(quoted diff) +0 -0
@@ -97,9 +97,10 @@ static void verify_ioctl_loop(void)

Patch does not apply, thus CI fail. Please always rebase before sending.

testcases/kernel/syscalls/ioctl/ioctl_loop01.c static void verify_ioctl_loop(void) · line 103

Quoted 8 lines
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +3 -2
 static void setup(void)
 {
-	int ret;
+	parted_sup = tst_cmd_present("parted");
+
 	const char *const cmd_parted[] = {"parted", "-s", "test.img", "mklabel", "msdos", "mkpart",
-	                                  "primary", "ext4", "1M", "10M", NULL};
+					"primary", "ext4", "1M", "10M", NULL};

This change just changes whitespace. I would avoid it.

Quoted 24 lines
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +2 -12
 	dev_num = tst_find_free_loopdev(dev_path, sizeof(dev_path));
 	if (dev_num < 0)
@@ -107,18 +108,8 @@ static void setup(void)
 
 	tst_fill_file("test.img", 0, 1024 * 1024, 10);
 
-	ret = tst_cmd(cmd_parted, NULL, NULL, TST_CMD_PASS_RETVAL);
-	switch (ret) {
-	case 0:
-		parted_sup = 1;
-	break;
-	case 255:
-		tst_res(TCONF, "parted binary not installed or failed");
-	break;
-	default:
-		tst_res(TCONF, "parted exited with %i", ret);
-	break;
-	}
+	if (parted_sup)
+		SAFE_CMD(cmd_parted, NULL, NULL);
 
 	sprintf(partscan_path, "/sys/block/loop%d/loop/partscan", dev_num);
 	sprintf(autoclear_path, "/sys/block/loop%d/loop/autoclear", dev_num);

testcases/kernel/syscalls/ioctl/ioctl_loop01.c static struct tst_test test = { · line 149

Quoted 7 lines
testcases/kernel/syscalls/ioctl/ioctl_loop01.c +4 -0
@@ -152,5 +143,9 @@ static struct tst_test test = {
 		{"linux-git", "6ac92fb5cdff"},
 		{}
 	},
+	.needs_cmds = (struct tst_cmd[]) {
+		{.cmd = "parted", .optional = 1},
+		{}
+	},

Otherwise LGTM.

With above fixed:

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp