Skip to contentSkip to conversation

C API: .needs_cmds and SAFE_CMD()

PATCH New Petr Vorel 2 participants 14 messages Mar 30, 2020 – Mar 31, 2020 Library code

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

v2 cover C API: .needs_cmds and SAFE_CMD()

Hi,

just in case due massive rename I'm posting the changes.

Kind regards,
Petr

Petr Vorel (5):
lib: Rename tst_run_cmd*() to tst_cmd*() lib/tst_cmd_*(): Turn int pass_exit_val into enum lib/tst_cmd_*(): Search for program in $PATH
lib: Implement SAFE_CMD() macro (new API only) Use SAFE_CMD()

 doc/test-writing-guidelines.txt               | 20 +++---
 include/tst_cmd.h                             | 61 +++++++++++--------
 include/tst_safe_macros.h                     | 17 ++++++
 lib/{tst_run_cmd.c => tst_cmd.c}              | 32 ++++++----
 lib/tst_kernel.c                              |  3 +-
 lib/tst_mkfs.c                                |  2 +-
 lib/tst_module.c                              |  5 +-
 lib/tst_virt.c                                |  2 +-
 testcases/cve/stack_clash.c                   |  2 +-
 testcases/kernel/input/input_helper.c         |  4 +-
 testcases/kernel/syscalls/acct/acct02.c       |  2 +-
 testcases/kernel/syscalls/add_key/add_key05.c | 16 ++---
 .../copy_file_range/copy_file_range02.c       |  2 +-
 .../kernel/syscalls/quotactl/quotactl01.c     | 11 +---
 .../kernel/syscalls/quotactl/quotactl06.c     |  9 +--
 .../syscalls/setpriority/setpriority01.c      |  4 +-
 testcases/kernel/syscalls/swapon/libswapon.c  |  2 +-
 testcases/network/netstress/netstress.c       |  2 +-
 .../network/nfs/nfs_stress/nfs05_make_tree.c  |  6 +-
 tools/apicmds/ltpapicmd.c                     |  2 +-
 20 files changed, 111 insertions(+), 93 deletions(-)
 rename lib/{tst_run_cmd.c => tst_cmd.c} (86%)
Signature · 1 line
2.26.0
author
#1 lore raw

PATCH v2 1/5 lib: Rename tst_run_cmd*() to tst_cmd*()

in function names and also C source file.
This is a preparation for next commit.

---
 doc/test-writing-guidelines.txt               | 10 ++++-----
 include/tst_cmd.h                             | 22 +++++++++----------
 lib/{tst_run_cmd.c => tst_cmd.c}              |  6 ++---
 lib/tst_kernel.c                              |  2 +-
 lib/tst_mkfs.c                                |  2 +-
 lib/tst_module.c                              |  4 ++--
 lib/tst_virt.c                                |  2 +-
 testcases/cve/stack_clash.c                   |  2 +-
 testcases/kernel/input/input_helper.c         |  4 ++--
 testcases/kernel/syscalls/acct/acct02.c       |  2 +-
 testcases/kernel/syscalls/add_key/add_key05.c |  4 ++--
 .../copy_file_range/copy_file_range02.c       |  2 +-
 .../kernel/syscalls/quotactl/quotactl01.c     |  2 +-
 .../kernel/syscalls/quotactl/quotactl06.c     |  2 +-
 .../syscalls/setpriority/setpriority01.c      |  4 ++--
 testcases/kernel/syscalls/swapon/libswapon.c  |  2 +-
 testcases/network/netstress/netstress.c       |  2 +-
 .../network/nfs/nfs_stress/nfs05_make_tree.c  |  6 ++---
 tools/apicmds/ltpapicmd.c                     |  2 +-
 19 files changed, 41 insertions(+), 41 deletions(-)
 rename lib/{tst_run_cmd.c => tst_cmd.c} (96%)
doc/test-writing-guidelines.txt +5 -5
index cff42f515..3956e2c4c 100644
@@ -1260,20 +1260,20 @@ different once the call returns and should be used only for rough estimates.
 -------------------------------------------------------------------------------
 #include "tst_test.h"
 
-int tst_run_cmd(const char *const argv[],
+int tst_cmd(const char *const argv[],
 	        const char *stdout_path,
 	        const char *stderr_path,
 	        int pass_exit_val);
 -------------------------------------------------------------------------------
 
-'tst_run_cmd' is a wrapper for 'vfork() + execvp()' which provides a way
+'tst_cmd' is a wrapper for 'vfork() + execvp()' which provides a way
 to execute an external program.
 
 'argv[]' is a 'NULL' terminated array of strings starting with the program name
 which is followed by optional arguments.
 
-A non-zero 'pass_exit_val' makes 'tst_run_cmd' return the program exit code to
-the caller. A zero for 'pass_exit_val' makes 'tst_run_cmd' exit the tests
+A non-zero 'pass_exit_val' makes 'tst_cmd' return the program exit code to
+the caller. A zero for 'pass_exit_val' makes 'tst_cmd' exit the tests
 on failure.
 
 In case that 'execvp()' has failed and the 'pass_exit_val' flag was set, the
@@ -1291,7 +1291,7 @@ const char *const cmd[] = { "ls", "-l", NULL };
 
 ...
 	/* Store output of 'ls -l' into log.txt */
-	tst_run_cmd(cmd, "log.txt", NULL, 0);
+	tst_cmd(cmd, "log.txt", NULL, 0);
 ...
 -------------------------------------------------------------------------------
 
include/tst_cmd.h +11 -11
index d0a3573f4..b8b86eb28 100644
@@ -18,13 +18,13 @@
  * exit code, otherwise it will call cleanup_fn() if the program
  * exit code is not zero.
  */
-int tst_run_cmd_fds_(void (cleanup_fn)(void),
+int tst_cmd_fds_(void (cleanup_fn)(void),
 			const char *const argv[],
 			int stdout_fd,
 			int stderr_fd,
 			int pass_exit_val);
 
-/* Executes tst_run_cmd_fds() and redirects its output to a file
+/* Executes tst_cmd_fds() and redirects its output to a file
  * @stdout_path: path where to redirect stdout. Set NULL if redirection is
  * not needed.
  * @stderr_path: path where to redirect stderr. Set NULL if redirection is
@@ -33,48 +33,48 @@ int tst_run_cmd_fds_(void (cleanup_fn)(void),
  * exit code, otherwise it will call cleanup_fn() if the program
  * exit code is not zero.
  */
-int tst_run_cmd_(void (cleanup_fn)(void),
+int tst_cmd_(void (cleanup_fn)(void),
 		const char *const argv[],
 		const char *stdout_path,
 		const char *stderr_path,
 		int pass_exit_val);
 
 #ifdef TST_TEST_H__
-static inline int tst_run_cmd_fds(const char *const argv[],
+static inline int tst_cmd_fds(const char *const argv[],
 				  int stdout_fd,
 				  int stderr_fd,
 				  int pass_exit_val)
 {
-	return tst_run_cmd_fds_(NULL, argv,
+	return tst_cmd_fds_(NULL, argv,
 	                        stdout_fd, stderr_fd, pass_exit_val);
 }
 
-static inline int tst_run_cmd(const char *const argv[],
+static inline int tst_cmd(const char *const argv[],
 			      const char *stdout_path,
 			      const char *stderr_path,
 			      int pass_exit_val)
 {
-	return tst_run_cmd_(NULL, argv,
+	return tst_cmd_(NULL, argv,
 	                    stdout_path, stderr_path, pass_exit_val);
 }
 #else
-static inline int tst_run_cmd_fds(void (cleanup_fn)(void),
+static inline int tst_cmd_fds(void (cleanup_fn)(void),
 				  const char *const argv[],
 				  int stdout_fd,
 				  int stderr_fd,
 				  int pass_exit_val)
 {
-	return tst_run_cmd_fds_(cleanup_fn, argv,
+	return tst_cmd_fds_(cleanup_fn, argv,
 	                        stdout_fd, stderr_fd, pass_exit_val);
 }
 
-static inline int tst_run_cmd(void (cleanup_fn)(void),
+static inline int tst_cmd(void (cleanup_fn)(void),
 			      const char *const argv[],
 			      const char *stdout_path,
 			      const char *stderr_path,
 			      int pass_exit_val)
 {
-	return tst_run_cmd_(cleanup_fn, argv,
+	return tst_cmd_(cleanup_fn, argv,
 	                    stdout_path, stderr_path, pass_exit_val);
 }
 #endif
lib/tst_cmd.c renamed from lib/tst_run_cmd.c +3 -3
similarity index 96%
rename from lib/tst_run_cmd.c
rename to lib/tst_cmd.c
index 8e4bf6ba3..1964ff784 100644
@@ -31,7 +31,7 @@
 #define OPEN_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
 #define OPEN_FLAGS	(O_WRONLY | O_APPEND | O_CREAT)
 
-int tst_run_cmd_fds_(void (cleanup_fn)(void),
+int tst_cmd_fds_(void (cleanup_fn)(void),
 		const char *const argv[],
 		int stdout_fd,
 		int stderr_fd,
@@ -107,7 +107,7 @@ int tst_run_cmd_fds_(void (cleanup_fn)(void),
 	return rc;
 }
 
-int tst_run_cmd_(void (cleanup_fn)(void),
+int tst_cmd_(void (cleanup_fn)(void),
 		const char *const argv[],
 		const char *stdout_path,
 		const char *stderr_path,
@@ -137,7 +137,7 @@ int tst_run_cmd_(void (cleanup_fn)(void),
 				stderr_path, __FILE__, __LINE__);
 	}
 
-	rc = tst_run_cmd_fds(cleanup_fn, argv, stdout_fd, stderr_fd,
+	rc = tst_cmd_fds(cleanup_fn, argv, stdout_fd, stderr_fd,
 			     pass_exit_val);
 
 	if ((stdout_fd != -1) && (close(stdout_fd) == -1))
lib/tst_kernel.c +1 -1
index 73ab9f1b1..210675b15 100644
@@ -85,7 +85,7 @@ int tst_check_driver(const char *name)
 {
 #ifndef __ANDROID__
 	const char * const argv[] = { "modprobe", "-n", name, NULL };
-	int res = tst_run_cmd_(NULL, argv, "/dev/null", "/dev/null", 1);
+	int res = tst_cmd_(NULL, argv, "/dev/null", "/dev/null", 1);
 
 	/* 255 - it looks like modprobe not available */
 	return (res == 255) ? 0 : res;
lib/tst_mkfs.c +1 -1
index 5d208eceb..9cdd35472 100644
@@ -88,7 +88,7 @@ void tst_mkfs_(const char *file, const int lineno, void (cleanup_fn)(void),
 
 	tst_resm(TINFO, "Formatting %s with %s opts='%s' extra opts='%s'",
 	         dev, fs_type, fs_opts_str, extra_opts_str);
-	ret = tst_run_cmd(cleanup_fn, argv, "/dev/null", NULL, 1);
+	ret = tst_cmd(cleanup_fn, argv, "/dev/null", NULL, 1);
 
 	switch (ret) {
 	case 0:
lib/tst_module.c +2 -2
index ed39952ee..a9ce90e8a 100644
@@ -97,7 +97,7 @@ void tst_module_load(void (cleanup_fn)(void),
 	for (i = offset; i < size; ++i)
 		mod_argv[i] = argv[i - offset];
 
-	tst_run_cmd(cleanup_fn, mod_argv, NULL, NULL, 0);
+	tst_cmd(cleanup_fn, mod_argv, NULL, NULL, 0);
 	free(mod_path);
 }
 
@@ -109,7 +109,7 @@ void tst_module_unload(void (cleanup_fn)(void), const char *mod_name)
 
 	rc = 1;
 	for (i = 0; i < 50; i++) {
-		rc = tst_run_cmd(NULL, argv, "/dev/null", "/dev/null", 1);
+		rc = tst_cmd(NULL, argv, "/dev/null", "/dev/null", 1);
 		if (!rc)
 			break;
 
lib/tst_virt.c +1 -1
index e95cf5e70..090e6334c 100644
@@ -70,7 +70,7 @@ static int try_systemd_detect_virt(void)
 	char virt_type[64];
 	int ret;
 
-	/* See tst_run_cmd.c */
+	/* See tst_cmd.c */
 	void *old_handler = signal(SIGCHLD, SIG_DFL);
 
 	f = popen("systemd-detect-virt", "r");
testcases/cve/stack_clash.c +1 -1
index ecbf5f6da..cd7f148c2 100644
@@ -119,7 +119,7 @@ void dump_proc_self_maps(void)
 	static char buf[64];
 	static const char *cmd[] = {"cat", buf, NULL};
 	sprintf(buf, "/proc/%d/maps", getpid());
-	tst_run_cmd(cmd, NULL, NULL, 0);
+	tst_cmd(cmd, NULL, NULL, 0);
 }
 
 void __attribute__((noinline)) preallocate_stack(unsigned long required)
testcases/kernel/input/input_helper.c +2 -2
index f6ae9c9b6..bd9c715ed 100644
@@ -92,7 +92,7 @@ static int try_load_uinput(void)
 
 	tst_resm(TINFO, "Trying to load uinput kernel module");
 
-	ret = tst_run_cmd(NULL, argv, NULL, NULL, 1);
+	ret = tst_cmd(NULL, argv, NULL, NULL, 1);
 	if (ret) {
 		tst_resm(TINFO, "Failed to load the uinput module");
 		return 0;
@@ -108,7 +108,7 @@ static void unload_uinput(void)
 
 	tst_resm(TINFO, "Unloading uinput kernel module");
 
-	ret = tst_run_cmd(NULL, argv, NULL, NULL, 1);
+	ret = tst_cmd(NULL, argv, NULL, NULL, 1);
 	if (ret)
 		tst_resm(TWARN, "Failed to unload uinput module");
 }
testcases/kernel/syscalls/acct/acct02.c +1 -1
index 2f1290fa2..22303dc3f 100644
@@ -67,7 +67,7 @@ static void run_command(void)
 {
 	const char *const cmd[] = {COMMAND, NULL};
 
-	rc = tst_run_cmd(cmd, NULL, NULL, 1) << 8;
+	rc = tst_cmd(cmd, NULL, NULL, 1) << 8;
 }
 
 static int verify_acct(void *acc, int elap_time)
testcases/kernel/syscalls/add_key/add_key05.c +2 -2
index 7443a4970..191889a58 100644
@@ -38,7 +38,7 @@ static void add_user(void)
 	const char *const cmd_useradd[] = {"useradd", username, NULL};
 	int rc;
 
-	switch ((rc = tst_run_cmd(cmd_useradd, NULL, NULL, 1))) {
+	switch ((rc = tst_cmd(cmd_useradd, NULL, NULL, 1))) {
 	case 0:
 		user_added = 1;
 		ltpuser = SAFE_GETPWNAM(username);
@@ -56,7 +56,7 @@ static void clean_user(void)
 
 	const char *const cmd_userdel[] = {"userdel", "-r", username, NULL};
 
-	if (tst_run_cmd(cmd_userdel, NULL, NULL, 1))
+	if (tst_cmd(cmd_userdel, NULL, NULL, 1))
 		tst_res(TWARN | TERRNO, "'userdel -r %s' failed", username);
 	else
 		user_added = 0;
testcases/kernel/syscalls/copy_file_range/copy_file_range02.c +1 -1
index c09766fe6..52a703b2b 100644
@@ -87,7 +87,7 @@ static int run_command(char *command, char *option, char *file)
 	const char *const cmd[] = {command, option, file, NULL};
 	int ret;
 
-	ret = tst_run_cmd(cmd, NULL, NULL, 1);
+	ret = tst_cmd(cmd, NULL, NULL, 1);
 	switch (ret) {
 	case 0:
 	return 0;
testcases/kernel/syscalls/quotactl/quotactl01.c +1 -1
index 6cc1deeb8..d1c4657b3 100644
@@ -164,7 +164,7 @@ static void setup(void)
 	const char *const cmd[] = {"quotacheck", "-ugF", "vfsv0", MNTPOINT, NULL};
 	int ret;
 
-	ret = tst_run_cmd(cmd, NULL, NULL, 1);
+	ret = tst_cmd(cmd, NULL, NULL, 1);
 	switch (ret) {
 	case 0:
 		break;
testcases/kernel/syscalls/quotactl/quotactl06.c +1 -1
index 758bd84cd..b5d59b706 100644
@@ -149,7 +149,7 @@ static void setup(void)
 	int ret;
 	unsigned int i;
 
-	ret = tst_run_cmd(cmd, NULL, NULL, 1);
+	ret = tst_cmd(cmd, NULL, NULL, 1);
 	switch (ret) {
 	case 0:
 		break;
testcases/kernel/syscalls/setpriority/setpriority01.c +2 -2
index ffd7499c4..70c015f18 100644
@@ -112,7 +112,7 @@ static void setup(void)
 	struct passwd *ltpuser;
 	int rc;
 
-	switch ((rc = tst_run_cmd(cmd_useradd, NULL, NULL, 1))) {
+	switch ((rc = tst_cmd(cmd_useradd, NULL, NULL, 1))) {
 	case 0:
 		user_added = 1;
 		ltpuser = SAFE_GETPWNAM(username);
@@ -133,7 +133,7 @@ static void cleanup(void)
 
 	const char *const cmd_userdel[] = {"userdel", "-r", username, NULL};
 
-	if (tst_run_cmd(cmd_userdel, NULL, NULL, 1))
+	if (tst_cmd(cmd_userdel, NULL, NULL, 1))
 		tst_res(TWARN | TERRNO, "'userdel -r %s' failed", username);
 }
 
testcases/kernel/syscalls/swapon/libswapon.c +1 -1
index 0a4501bdd..d5bbfa567 100644
@@ -47,7 +47,7 @@ int make_swapfile(void (cleanup)(void), const char *swapfile, int safe)
 	argv[1] = swapfile;
 	argv[2] = NULL;
 
-	return tst_run_cmd(cleanup, argv, "/dev/null", "/dev/null", safe);
+	return tst_cmd(cleanup, argv, "/dev/null", "/dev/null", safe);
 }
 
 /*
testcases/network/netstress/netstress.c +1 -1
index 76d2fdb04..fca476198 100644
@@ -967,7 +967,7 @@ static void setup(void)
 		/* dccp* modules can be blacklisted, load them manually */
 		const char * const argv[] = {"modprobe", "dccp_ipv6", NULL};
 
-		if (tst_run_cmd(argv, NULL, NULL, 1))
+		if (tst_cmd(argv, NULL, NULL, 1))
 			tst_brk(TCONF, "Failed to load dccp_ipv6 module");
 
 		tst_res(TINFO, "DCCP %s", (client_mode) ? "client" : "server");
testcases/network/nfs/nfs_stress/nfs05_make_tree.c +3 -3
index 25809e1a3..12c86799d 100644
@@ -82,7 +82,7 @@ static void run_targets(const char *dirname, char *cfile, pid_t tid)
 			snprintf(cfile, PATH_MAX, "%s%s/%d.%d.%d",
 				 dirname, subdir, tid, i, k);
 
-			tst_run_cmd(cmd_run, output_file, NULL, 0);
+			tst_cmd(cmd_run, output_file, NULL, 0);
 
 			fd = SAFE_OPEN(output_file, O_RDONLY);
 			SAFE_READ(1, fd, buf, 11);
@@ -179,11 +179,11 @@ static void *thread_fn(LTP_ATTRIBUTE_UNUSED void *args)
 	const char *const cmd_make_clean[] = {
 		"make", "-C", dirname, "-s", "clean", NULL};
 
-	tst_run_cmd(cmd_make, NULL, NULL, 0);
+	tst_cmd(cmd_make, NULL, NULL, 0);
 
 	run_targets(dirname, cfile, tid);
 
-	tst_run_cmd(cmd_make_clean, NULL, NULL, 0);
+	tst_cmd(cmd_make_clean, NULL, NULL, 0);
 
 	free(dirname);
 
tools/apicmds/ltpapicmd.c +1 -1
index c4fab3218..ac58c90cc 100644
@@ -108,7 +108,7 @@ void tst_cat_file(const char *filename)
 {
 	const char *cmd[] = {"cat", filename, NULL};
 
-	tst_run_cmd(NULL, cmd, NULL, NULL, 0);
+	tst_cmd(NULL, cmd, NULL, NULL, 0);
 }
 
 void apicmd_brk(int argc, char *argv[])
Signature · 1 line
2.26.0
author
#2 lore raw

PATCH v2 2/5 lib/tst_cmd_*(): Turn int pass_exit_val into enum

In reply to Petr Vorel

This is a preparation for next commit.
Now uses the only flag TST_CMD_PASS_RETVAL.

---
 doc/test-writing-guidelines.txt               | 12 +++----
 include/tst_cmd.h                             | 36 ++++++++++---------
 lib/tst_cmd.c                                 | 12 +++----
 lib/tst_kernel.c                              |  3 +-
 lib/tst_mkfs.c                                |  2 +-
 lib/tst_module.c                              |  3 +-
 testcases/kernel/input/input_helper.c         |  4 +--
 testcases/kernel/syscalls/acct/acct02.c       |  2 +-
 testcases/kernel/syscalls/add_key/add_key05.c |  4 +--
 .../copy_file_range/copy_file_range02.c       |  2 +-
 .../kernel/syscalls/quotactl/quotactl01.c     |  2 +-
 .../kernel/syscalls/quotactl/quotactl06.c     |  2 +-
 .../syscalls/setpriority/setpriority01.c      |  4 +--
 testcases/network/netstress/netstress.c       |  2 +-
 14 files changed, 48 insertions(+), 42 deletions(-)
doc/test-writing-guidelines.txt +6 -6
index 3956e2c4c..e48168dfb 100644
@@ -1263,20 +1263,20 @@ different once the call returns and should be used only for rough estimates.
 int tst_cmd(const char *const argv[],
 	        const char *stdout_path,
 	        const char *stderr_path,
-	        int pass_exit_val);
+			enum tst_cmd_flags flags);
 -------------------------------------------------------------------------------
 
-'tst_cmd' is a wrapper for 'vfork() + execvp()' which provides a way
+'tst_cmd()' is a wrapper for 'vfork() + execvp()' which provides a way
 to execute an external program.
 
 'argv[]' is a 'NULL' terminated array of strings starting with the program name
 which is followed by optional arguments.
 
-A non-zero 'pass_exit_val' makes 'tst_cmd' return the program exit code to
-the caller. A zero for 'pass_exit_val' makes 'tst_cmd' exit the tests
-on failure.
+'TST_CMD_PASS_RETVAL' enum 'tst_cmd_flags' makes 'tst_cmd()'
+return the program exit code to the caller, otherwise 'tst_cmd()' exit the
+tests on failure.
 
-In case that 'execvp()' has failed and the 'pass_exit_val' flag was set, the
+In case that 'execvp()' has failed and the enum 'TST_CMD_PASS_RETVAL' flag was set, the
 return value is '255' if 'execvp()' failed with 'ENOENT' and '254' otherwise.
 
 'stdout_path' and 'stderr_path' determine where to redirect the program
include/tst_cmd.h +20 -16
index b8b86eb28..bba530e50 100644
@@ -5,6 +5,14 @@
 #ifndef TST_CMD_H__
 #define TST_CMD_H__
 
+enum tst_cmd_flags {
+	/*
+	 * return the program exit code, otherwise it will call cleanup_fn() if the
+	 * program exit code is not zero.
+	 */
+	TST_CMD_PASS_RETVAL = 1,
+};
+
 /*
  * vfork() + execvp() specified program.
  * @argv: a list of two (at least program name + NULL) or more pointers that
@@ -14,68 +22,64 @@
  * redirection is not needed.
  * @stderr_fd: file descriptor where to redirect stderr. Set -1 if
  * redirection is not needed.
- * @pass_exit_val: if it's non-zero, this function will return the program
- * exit code, otherwise it will call cleanup_fn() if the program
- * exit code is not zero.
+ * @flags: enum tst_cmd_flags
  */
 int tst_cmd_fds_(void (cleanup_fn)(void),
 			const char *const argv[],
 			int stdout_fd,
 			int stderr_fd,
-			int pass_exit_val);
+			enum tst_cmd_flags flags);
 
 /* Executes tst_cmd_fds() and redirects its output to a file
  * @stdout_path: path where to redirect stdout. Set NULL if redirection is
  * not needed.
  * @stderr_path: path where to redirect stderr. Set NULL if redirection is
  * not needed.
- * @pass_exit_val: if it's non-zero, this function will return the program
- * exit code, otherwise it will call cleanup_fn() if the program
- * exit code is not zero.
+ * @flags: enum tst_cmd_flags
  */
 int tst_cmd_(void (cleanup_fn)(void),
 		const char *const argv[],
 		const char *stdout_path,
 		const char *stderr_path,
-		int pass_exit_val);
+		enum tst_cmd_flags flags);
 
 #ifdef TST_TEST_H__
 static inline int tst_cmd_fds(const char *const argv[],
 				  int stdout_fd,
 				  int stderr_fd,
-				  int pass_exit_val)
+				  enum tst_cmd_flags flags)
 {
 	return tst_cmd_fds_(NULL, argv,
-	                        stdout_fd, stderr_fd, pass_exit_val);
+	                        stdout_fd, stderr_fd, flags);
 }
 
 static inline int tst_cmd(const char *const argv[],
 			      const char *stdout_path,
 			      const char *stderr_path,
-			      int pass_exit_val)
+			      enum tst_cmd_flags flags)
 {
 	return tst_cmd_(NULL, argv,
-	                    stdout_path, stderr_path, pass_exit_val);
+	                    stdout_path, stderr_path, flags);
 }
 #else
 static inline int tst_cmd_fds(void (cleanup_fn)(void),
 				  const char *const argv[],
 				  int stdout_fd,
 				  int stderr_fd,
-				  int pass_exit_val)
+				  enum tst_cmd_flags flags)
 {
 	return tst_cmd_fds_(cleanup_fn, argv,
-	                        stdout_fd, stderr_fd, pass_exit_val);
+	                        stdout_fd, stderr_fd, flags);
 }
 
 static inline int tst_cmd(void (cleanup_fn)(void),
 			      const char *const argv[],
 			      const char *stdout_path,
 			      const char *stderr_path,
-			      int pass_exit_val)
+			      enum tst_cmd_flags flags)
 {
 	return tst_cmd_(cleanup_fn, argv,
-	                    stdout_path, stderr_path, pass_exit_val);
+	                    stdout_path, stderr_path, flags);
 }
 #endif
 
lib/tst_cmd.c +6 -6
index 1964ff784..012c2a32b 100644
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
+ * Copyright (c) 2020 Petr Vorel <[email protected]>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -16,7 +17,6 @@
  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  *
  * Author: Alexey Kodanev <[email protected]>
- *
  */
 
 #include <errno.h>
@@ -27,6 +27,7 @@
 #include <unistd.h>
 #include <signal.h>
 #include "test.h"
+#include "tst_cmd.h"
 
 #define OPEN_MODE	(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
 #define OPEN_FLAGS	(O_WRONLY | O_APPEND | O_CREAT)
@@ -35,7 +36,7 @@ int tst_cmd_fds_(void (cleanup_fn)(void),
 		const char *const argv[],
 		int stdout_fd,
 		int stderr_fd,
-		int pass_exit_val)
+		enum tst_cmd_flags flags)
 {
 	int rc;
 
@@ -97,7 +98,7 @@ int tst_cmd_fds_(void (cleanup_fn)(void),
 
 	rc = WEXITSTATUS(ret);
 
-	if ((!pass_exit_val) && rc) {
+	if (!(flags & TST_CMD_PASS_RETVAL) && rc) {
 		tst_brkm(TBROK, cleanup_fn,
 			 "'%s' exited with a non-zero code %d at %s:%d",
 			 argv[0], rc, __FILE__, __LINE__);
@@ -111,7 +112,7 @@ int tst_cmd_(void (cleanup_fn)(void),
 		const char *const argv[],
 		const char *stdout_path,
 		const char *stderr_path,
-		int pass_exit_val)
+		enum tst_cmd_flags flags)
 {
 	int stdout_fd = -1;
 	int stderr_fd = -1;
@@ -137,8 +138,7 @@ int tst_cmd_(void (cleanup_fn)(void),
 				stderr_path, __FILE__, __LINE__);
 	}
 
-	rc = tst_cmd_fds(cleanup_fn, argv, stdout_fd, stderr_fd,
-			     pass_exit_val);
+	rc = tst_cmd_fds(cleanup_fn, argv, stdout_fd, stderr_fd, flags);
 
 	if ((stdout_fd != -1) && (close(stdout_fd) == -1))
 		tst_resm(TWARN | TERRNO,
lib/tst_kernel.c +2 -1
index 210675b15..57fa4b2be 100644
@@ -85,7 +85,8 @@ int tst_check_driver(const char *name)
 {
 #ifndef __ANDROID__
 	const char * const argv[] = { "modprobe", "-n", name, NULL };
-	int res = tst_cmd_(NULL, argv, "/dev/null", "/dev/null", 1);
+	int res = tst_cmd_(NULL, argv, "/dev/null", "/dev/null",
+			       TST_CMD_PASS_RETVAL);
 
 	/* 255 - it looks like modprobe not available */
 	return (res == 255) ? 0 : res;
lib/tst_mkfs.c +1 -1
index 9cdd35472..b71ce4155 100644
@@ -88,7 +88,7 @@ void tst_mkfs_(const char *file, const int lineno, void (cleanup_fn)(void),
 
 	tst_resm(TINFO, "Formatting %s with %s opts='%s' extra opts='%s'",
 	         dev, fs_type, fs_opts_str, extra_opts_str);
-	ret = tst_cmd(cleanup_fn, argv, "/dev/null", NULL, 1);
+	ret = tst_cmd(cleanup_fn, argv, "/dev/null", NULL, TST_CMD_PASS_RETVAL);
 
 	switch (ret) {
 	case 0:
lib/tst_module.c +2 -1
index a9ce90e8a..eda61872f 100644
@@ -109,7 +109,8 @@ void tst_module_unload(void (cleanup_fn)(void), const char *mod_name)
 
 	rc = 1;
 	for (i = 0; i < 50; i++) {
-		rc = tst_cmd(NULL, argv, "/dev/null", "/dev/null", 1);
+		rc = tst_cmd(NULL, argv, "/dev/null", "/dev/null",
+				 TST_CMD_PASS_RETVAL);
 		if (!rc)
 			break;
 
testcases/kernel/input/input_helper.c +2 -2
index bd9c715ed..a014861da 100644
@@ -92,7 +92,7 @@ static int try_load_uinput(void)
 
 	tst_resm(TINFO, "Trying to load uinput kernel module");
 
-	ret = tst_cmd(NULL, argv, NULL, NULL, 1);
+	ret = tst_cmd(NULL, argv, NULL, NULL, TST_CMD_PASS_RETVAL);
 	if (ret) {
 		tst_resm(TINFO, "Failed to load the uinput module");
 		return 0;
@@ -108,7 +108,7 @@ static void unload_uinput(void)
 
 	tst_resm(TINFO, "Unloading uinput kernel module");
 
-	ret = tst_cmd(NULL, argv, NULL, NULL, 1);
+	ret = tst_cmd(NULL, argv, NULL, NULL, TST_CMD_PASS_RETVAL);
 	if (ret)
 		tst_resm(TWARN, "Failed to unload uinput module");
 }
testcases/kernel/syscalls/acct/acct02.c +1 -1
index 22303dc3f..d6b16b8c1 100644
@@ -67,7 +67,7 @@ static void run_command(void)
 {
 	const char *const cmd[] = {COMMAND, NULL};
 
-	rc = tst_cmd(cmd, NULL, NULL, 1) << 8;
+	rc = tst_cmd(cmd, NULL, NULL, TST_CMD_PASS_RETVAL) << 8;
 }
 
 static int verify_acct(void *acc, int elap_time)
testcases/kernel/syscalls/add_key/add_key05.c +2 -2
index 191889a58..aa2b9cb2f 100644
@@ -38,7 +38,7 @@ static void add_user(void)
 	const char *const cmd_useradd[] = {"useradd", username, NULL};
 	int rc;
 
-	switch ((rc = tst_cmd(cmd_useradd, NULL, NULL, 1))) {
+	switch ((rc = tst_cmd(cmd_useradd, NULL, NULL, TST_CMD_PASS_RETVAL))) {
 	case 0:
 		user_added = 1;
 		ltpuser = SAFE_GETPWNAM(username);
@@ -56,7 +56,7 @@ static void clean_user(void)
 
 	const char *const cmd_userdel[] = {"userdel", "-r", username, NULL};
 
-	if (tst_cmd(cmd_userdel, NULL, NULL, 1))
+	if (tst_cmd(cmd_userdel, NULL, NULL, TST_CMD_PASS_RETVAL))
 		tst_res(TWARN | TERRNO, "'userdel -r %s' failed", username);
 	else
 		user_added = 0;
testcases/kernel/syscalls/copy_file_range/copy_file_range02.c +1 -1
index 52a703b2b..fa679c4d3 100644
@@ -87,7 +87,7 @@ static int run_command(char *command, char *option, char *file)
 	const char *const cmd[] = {command, option, file, NULL};
 	int ret;
 
-	ret = tst_cmd(cmd, NULL, NULL, 1);
+	ret = tst_cmd(cmd, NULL, NULL, TST_CMD_PASS_RETVAL);
 	switch (ret) {
 	case 0:
 	return 0;
testcases/kernel/syscalls/quotactl/quotactl01.c +1 -1
index d1c4657b3..6f0b60ffc 100644
@@ -164,7 +164,7 @@ static void setup(void)
 	const char *const cmd[] = {"quotacheck", "-ugF", "vfsv0", MNTPOINT, NULL};
 	int ret;
 
-	ret = tst_cmd(cmd, NULL, NULL, 1);
+	ret = tst_cmd(cmd, NULL, NULL, TST_CMD_PASS_RETVAL);
 	switch (ret) {
 	case 0:
 		break;
testcases/kernel/syscalls/quotactl/quotactl06.c +1 -1
index b5d59b706..773114dfd 100644
@@ -149,7 +149,7 @@ static void setup(void)
 	int ret;
 	unsigned int i;
 
-	ret = tst_cmd(cmd, NULL, NULL, 1);
+	ret = tst_cmd(cmd, NULL, NULL, TST_CMD_PASS_RETVAL);
 	switch (ret) {
 	case 0:
 		break;
testcases/kernel/syscalls/setpriority/setpriority01.c +2 -2
index 70c015f18..22fbe1ae5 100644
@@ -112,7 +112,7 @@ static void setup(void)
 	struct passwd *ltpuser;
 	int rc;
 
-	switch ((rc = tst_cmd(cmd_useradd, NULL, NULL, 1))) {
+	switch ((rc = tst_cmd(cmd_useradd, NULL, NULL, TST_CMD_PASS_RETVAL))) {
 	case 0:
 		user_added = 1;
 		ltpuser = SAFE_GETPWNAM(username);
@@ -133,7 +133,7 @@ static void cleanup(void)
 
 	const char *const cmd_userdel[] = {"userdel", "-r", username, NULL};
 
-	if (tst_cmd(cmd_userdel, NULL, NULL, 1))
+	if (tst_cmd(cmd_userdel, NULL, NULL, TST_CMD_PASS_RETVAL))
 		tst_res(TWARN | TERRNO, "'userdel -r %s' failed", username);
 }
 
testcases/network/netstress/netstress.c +1 -1
index fca476198..6797be018 100644
@@ -967,7 +967,7 @@ static void setup(void)
 		/* dccp* modules can be blacklisted, load them manually */
 		const char * const argv[] = {"modprobe", "dccp_ipv6", NULL};
 
-		if (tst_cmd(argv, NULL, NULL, 1))
+		if (tst_cmd(argv, NULL, NULL, TST_CMD_PASS_RETVAL))
 			tst_brk(TCONF, "Failed to load dccp_ipv6 module");
 
 		tst_res(TINFO, "DCCP %s", (client_mode) ? "client" : "server");
Signature · 1 line
2.26.0
author
#3 lore raw

PATCH v2 3/5 lib/tst_cmd_*(): Search for program in $PATH

In reply to Petr Vorel

before calling execvp(). This is slightly safer than checking errno ENOENT. TST_CMD_TCONF_ON_MISSING flag cause TCONF when program not found.

---
 doc/test-writing-guidelines.txt |  3 ++-
 include/tst_cmd.h               |  3 +++
 lib/tst_cmd.c                   | 16 ++++++++++++----
 3 files changed, 17 insertions(+), 5 deletions(-)
doc/test-writing-guidelines.txt +2 -1
index e48168dfb..7069d3c8f 100644
@@ -1274,7 +1274,8 @@ which is followed by optional arguments.
 
 'TST_CMD_PASS_RETVAL' enum 'tst_cmd_flags' makes 'tst_cmd()'
 return the program exit code to the caller, otherwise 'tst_cmd()' exit the
-tests on failure.
+tests on failure. 'TST_CMD_TCONF_ON_MISSING' check for program in '$PATH' and exit
+with 'TCONF' if not found.
 
 In case that 'execvp()' has failed and the enum 'TST_CMD_PASS_RETVAL' flag was set, the
 return value is '255' if 'execvp()' failed with 'ENOENT' and '254' otherwise.
include/tst_cmd.h +3 -0
index bba530e50..1f39f690f 100644
@@ -11,6 +11,9 @@ enum tst_cmd_flags {
 	 * program exit code is not zero.
 	 */
 	TST_CMD_PASS_RETVAL = 1,
+
+	/* exit with TCONF if program is not in path */
+	TST_CMD_TCONF_ON_MISSING = 2,
 };
 
 /*
lib/tst_cmd.c +12 -4
index 012c2a32b..ba79806ce 100644
@@ -56,6 +56,17 @@ int tst_cmd_fds_(void (cleanup_fn)(void),
 	 */
 	void *old_handler = signal(SIGCHLD, SIG_DFL);
 
+	const char *cmd;
+	char path[PATH_MAX];
+
+	if (tst_get_path(argv[0], path, sizeof(path))) {
+		if (flags & TST_CMD_TCONF_ON_MISSING)
+			tst_brkm(TCONF, "Couldn't find '%s' in $PATH at %s:%d", argv[0],
+				 __FILE__, __LINE__);
+		else
+			_exit(255);
+	}
+
 	pid_t pid = vfork();
 	if (pid == -1) {
 		tst_brkm(TBROK | TERRNO, cleanup_fn, "vfork failed at %s:%d",
@@ -74,10 +85,7 @@ int tst_cmd_fds_(void (cleanup_fn)(void),
 			dup2(stderr_fd, STDERR_FILENO);
 		}
 
-		if (execvp(argv[0], (char *const *)argv)) {
-			if (errno == ENOENT)
-				_exit(255);
-		}
+		execvp(argv[0], (char *const *)argv);
 		_exit(254);
 	}
 
Signature · 1 line
2.26.0
author
#4 lore raw

PATCH v2 4/5 lib: Implement SAFE_CMD() macro (new API only)

In reply to Petr Vorel
---
 doc/test-writing-guidelines.txt |  3 +++
 include/tst_safe_macros.h       | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+)
doc/test-writing-guidelines.txt +3 -0
index 7069d3c8f..6f1eee490 100644
@@ -1283,6 +1283,9 @@ return value is '255' if 'execvp()' failed with 'ENOENT' and '254' otherwise.
 'stdout_path' and 'stderr_path' determine where to redirect the program
 stdout and stderr I/O streams.
 
+The 'SAFE_CMD()' macro can be used automatic handling non zero exits (exits
+with 'TBROK') or 'ENOENT' (exits with 'TCONF').
+
 .Example
 [source,c]
 -------------------------------------------------------------------------------
include/tst_safe_macros.h +18 -0
index d95d26219..38eb67d1d 100644
@@ -21,6 +21,7 @@
 #include <grp.h>
 
 #include "safe_macros_fn.h"
+#include "tst_cmd.h"
 
 #define SAFE_BASENAME(path) \
 	safe_basename(__FILE__, __LINE__, NULL, (path))
@@ -534,4 +535,21 @@ int safe_personality(const char *filename, unsigned int lineno,
 void safe_unshare(const char *file, const int lineno, int flags);
 #define SAFE_UNSHARE(flags) safe_unshare(__FILE__, __LINE__, (flags))
 
+void safe_cmd(const char *file, const int lineno, const char *const argv[],
+				  const char *stdout_path, const char *stderr_path)
+{
+	int rval;
+
+	switch ((rval = tst_run_cmd(argv, stdout_path, stderr_path,
+				    TST_RUN_CMD_PASS_EXIT_VAL |
+				    TST_RUN_CMD_CHECK_CMD))) {
+	case 0:
+		break;
+	default:
+		tst_brk(TBROK, "%s:%d: %s failed (%d)", file, lineno, argv[0], rval);
+	}
+}
+#define SAFE_CMD(argv, stdout_path, stderr_path) \
+	safe_cmd(__FILE__, __LINE__, (argv), (stdout_path), (stderr_path))
+
 #endif /* SAFE_MACROS_H__ */
Signature · 1 line
2.26.0
#5 lore raw

PATCH v2 4/5 lib: Implement SAFE_CMD() macro (new API only)

Quoted 49 lines · Petr Vorel
---
 doc/test-writing-guidelines.txt |  3 +++
 include/tst_safe_macros.h       | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+)
doc/test-writing-guidelines.txt +0 -0
index 7069d3c8f..6f1eee490 100644
@@ -1283,6 +1283,9 @@ return value is '255' if 'execvp()' failed with

'ENOENT' and '254' otherwise.
'stdout_path' and 'stderr_path' determine where to redirect the program stdout and stderr I/O streams.

+The 'SAFE_CMD()' macro can be used automatic handling non zero exits (exits

doc/test-writing-guidelines.txt +2 -0
+with 'TBROK') or 'ENOENT' (exits with 'TCONF').
+
 .Example
 [source,c]
 
 -------------------------------------------------------------------------------
include/tst_safe_macros.h +2 -0
index d95d26219..38eb67d1d 100644
@@ -21,6 +21,7 @@
 #include <grp.h>
 
 #include "safe_macros_fn.h"
+#include "tst_cmd.h"
 
 #define SAFE_BASENAME(path) \
        safe_basename(__FILE__, __LINE__, NULL, (path))
@@ -534,4 +535,21 @@ int safe_personality(const char *filename, unsigned
 int lineno,
 void safe_unshare(const char *file, const int lineno, int flags);
 #define SAFE_UNSHARE(flags) safe_unshare(__FILE__, __LINE__, (flags))
 
+void safe_cmd(const char *file, const int lineno, const char *const

argv[],
+ const char *stdout_path, const char
*stderr_path)

include/tst_safe_macros.h int safe_personality(const char *filename, unsigned int lineno, · line 545

Quoted 6 lines
include/tst_safe_macros.h +6 -0
+{
+       int rval;
+
+       switch ((rval = tst_run_cmd(argv, stdout_path, stderr_path,
+                                   TST_RUN_CMD_PASS_EXIT_VAL |
+                                   TST_RUN_CMD_CHECK_CMD))) {

This should be TST_CMD_TCONF_ON_MISSING ^.

I'd help to build&run the whole patches tomorrow, anything wrong if I can find will reply to you then.

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200330/d9c5a717/attachment.htm>
#6 lore raw

PATCH v2 4/5 lib: Implement SAFE_CMD() macro (new API only)

Quoted 48 lines · Li Wang
Quoted 44 lines · Petr Vorel
---
 doc/test-writing-guidelines.txt |  3 +++
 include/tst_safe_macros.h       | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+)
doc/test-writing-guidelines.txt +0 -0
index 7069d3c8f..6f1eee490 100644
@@ -1283,6 +1283,9 @@ return value is '255' if 'execvp()' failed with

'ENOENT' and '254' otherwise.
'stdout_path' and 'stderr_path' determine where to redirect the program stdout and stderr I/O streams.

+The 'SAFE_CMD()' macro can be used automatic handling non zero exits (exits

doc/test-writing-guidelines.txt +2 -0
+with 'TBROK') or 'ENOENT' (exits with 'TCONF').
+
 .Example
 [source,c]
 
 -------------------------------------------------------------------------------
include/tst_safe_macros.h +1 -0
index d95d26219..38eb67d1d 100644
@@ -21,6 +21,7 @@
 #include <grp.h>
 
 #include "safe_macros_fn.h"
+#include "tst_cmd.h"
 
 #define SAFE_BASENAME(path) \
        safe_basename(__FILE__, __LINE__, NULL, (path))
@@ -534,4 +535,21 @@ int safe_personality(const char *filename, unsigned
 int lineno,
 void safe_unshare(const char *file, const int lineno, int flags);
 #define SAFE_UNSHARE(flags) safe_unshare(__FILE__, __LINE__, (flags))
 
Quoted 10 lines

include/tst_safe_macros.h int safe_personality(const char *filename, unsigned int lineno, · line 543

Quoted 9 lines
include/tst_safe_macros.h +1 -0
+void safe_cmd(const char *file, const int lineno, const char *const

argv[],
+ const char *stdout_path, const char
*stderr_path)

include/tst_safe_macros.h +4 -0
+{
+       int rval;
+
+       switch ((rval = tst_run_cmd(argv, stdout_path, stderr_path,

tst_cmd ^

Quoted 13 lines

+ TST_RUN_CMD_PASS_EXIT_VAL |

Quoted 2 lines

+ TST_RUN_CMD_CHECK_CMD))) {

This should be TST_CMD_TCONF_ON_MISSING ^.

I'd help to build&run the whole patches tomorrow, anything wrong if I can find will reply to you then.

--
Regards,
Li Wang

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200330/b7d3f24f/attachment-0001.htm>
author
#7 lore raw

PATCH v2 4/5 lib: Implement SAFE_CMD() macro (new API only)

In reply to Li Wang

Hi Li,

Quoted 9 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+       switch ((rval = tst_run_cmd(argv, stdout_path, stderr_path,
+                                   TST_RUN_CMD_PASS_EXIT_VAL |
+                                   TST_RUN_CMD_CHECK_CMD))) {

This should be TST_CMD_TCONF_ON_MISSING ^.

I'd help to build&run the whole patches tomorrow, anything wrong if I can find will reply to you then.

Thanks for fixes. Here is following diff from v2 (addressing issues you found, fixed tst_run_cmd() being replaced in later commit, fix doc formatting), code available also on:
https://github.com/pevik/ltp/tree/needs_cmds.v2.fixes

I'm going to test each commit if it builds and wait for your ack tomorrow.

Kind regards,
Petr

test-writing-guidelines.txt +1 -1
index 75c2e517a..895f6c99d 100644
@@ -1263,7 +1263,7 @@ different once the call returns and should be used only for rough estimates.
 int tst_cmd(const char *const argv[],
 	        const char *stdout_path,
 	        const char *stderr_path,
-			enum tst_cmd_flags flags);
+	        enum tst_cmd_flags flags);
 -------------------------------------------------------------------------------
 
 'tst_cmd()' is a wrapper for 'vfork() + execvp()' which provides a way
tst_safe_macros.h +2 -2
index d050c5990..295800c1b 100644
@@ -543,8 +543,8 @@ void safe_cmd(const char *file, const int lineno, const char *const argv[],
 {
 	int rval;
 
-	switch ((rval = tst_cmd(argv, stdout_path, stderr_path, TST_CMD_PASS_RETVAL
-							| TST_CMD_CHECK_CMD))) {
+	switch ((rval = tst_cmd(argv, stdout_path, stderr_path,
+				TST_CMD_PASS_EXIT_VAL | TST_CMD_TCONF_ON_MISSING))) {
 	case 0:
 		break;
 	default:
author
#8 lore raw

PATCH v2 5/5 Use SAFE_CMD()

In reply to Petr Vorel
---
 doc/test-writing-guidelines.txt                 |  4 ++--
 include/tst_safe_macros.h                       |  5 ++---
 testcases/kernel/syscalls/add_key/add_key05.c   | 14 ++++----------
 testcases/kernel/syscalls/quotactl/quotactl01.c | 11 ++---------
 testcases/kernel/syscalls/quotactl/quotactl06.c |  9 +--------
 5 files changed, 11 insertions(+), 32 deletions(-)
doc/test-writing-guidelines.txt +2 -2
index 6f1eee490..75c2e517a 100644
@@ -1283,8 +1283,8 @@ return value is '255' if 'execvp()' failed with 'ENOENT' and '254' otherwise.
 'stdout_path' and 'stderr_path' determine where to redirect the program
 stdout and stderr I/O streams.
 
-The 'SAFE_CMD()' macro can be used automatic handling non zero exits (exits
-with 'TBROK') or 'ENOENT' (exits with 'TCONF').
+The 'SAFE_CMD()' macro can be used automatic handling non-zero exits (exits
+with 'TBROK') and 'ENOENT' (exits with 'TCONF').
 
 .Example
 [source,c]
include/tst_safe_macros.h +2 -3
index 38eb67d1d..bcd4b9c9e 100644
@@ -540,9 +540,8 @@ void safe_cmd(const char *file, const int lineno, const char *const argv[],
 {
 	int rval;
 
-	switch ((rval = tst_run_cmd(argv, stdout_path, stderr_path,
-				    TST_RUN_CMD_PASS_EXIT_VAL |
-				    TST_RUN_CMD_CHECK_CMD))) {
+	switch ((rval = tst_cmd(argv, stdout_path, stderr_path, TST_CMD_PASS_RETVAL
+							| TST_CMD_CHECK_CMD))) {
 	case 0:
 		break;
 	default:
testcases/kernel/syscalls/add_key/add_key05.c +4 -10
index aa2b9cb2f..f64c359bb 100644
@@ -36,16 +36,10 @@ static void add_user(void)
 		return;
 
 	const char *const cmd_useradd[] = {"useradd", username, NULL};
-	int rc;
-
-	switch ((rc = tst_cmd(cmd_useradd, NULL, NULL, TST_CMD_PASS_RETVAL))) {
-	case 0:
-		user_added = 1;
-		ltpuser = SAFE_GETPWNAM(username);
-		break;
-	default:
-		tst_brk(TBROK, "useradd failed (%d)", rc);
-	}
+
+	SAFE_CMD(cmd_useradd, NULL, NULL);
+	user_added = 1;
+	ltpuser = SAFE_GETPWNAM(username);
 	sprintf(fmt, "%5u: %%*5d %%*d/%%*d %%d/%%d %%d/%%d", ltpuser->pw_uid);
 }
 
testcases/kernel/syscalls/quotactl/quotactl01.c +2 -9
index 6f0b60ffc..e1ec22529 100644
@@ -162,15 +162,8 @@ static struct tcase {
 static void setup(void)
 {
 	const char *const cmd[] = {"quotacheck", "-ugF", "vfsv0", MNTPOINT, NULL};
-	int ret;
-
-	ret = tst_cmd(cmd, NULL, NULL, TST_CMD_PASS_RETVAL);
-	switch (ret) {
-	case 0:
-		break;
-	default:
-		tst_brk(TBROK, "quotacheck exited with %i", ret);
-	}
+
+	SAFE_CMD(cmd, NULL, NULL);
 
 	test_id = geteuid();
 	if (access(USRPATH, F_OK) == -1)
testcases/kernel/syscalls/quotactl/quotactl06.c +1 -8
index 773114dfd..0a36c97a4 100644
@@ -146,16 +146,9 @@ static void verify_quotactl(unsigned int n)
 static void setup(void)
 {
 	const char *const cmd[] = {"quotacheck", "-uF", "vfsv0", MNTPOINT, NULL};
-	int ret;
 	unsigned int i;
 
-	ret = tst_cmd(cmd, NULL, NULL, TST_CMD_PASS_RETVAL);
-	switch (ret) {
-	case 0:
-		break;
-	default:
-		tst_brk(TBROK, "quotacheck exited with %i", ret);
-	}
+	SAFE_CMD(cmd, NULL, NULL);
 
 	if (access(USRPATH, F_OK) == -1)
 		tst_brk(TFAIL | TERRNO, "user quotafile didn't exist");
Signature · 1 line
2.26.0
#9 lore raw

PATCH v2 5/5 Use SAFE_CMD()

Quoted 44 lines · Petr Vorel
---
 doc/test-writing-guidelines.txt                 |  4 ++--
 include/tst_safe_macros.h                       |  5 ++---
 testcases/kernel/syscalls/add_key/add_key05.c   | 14 ++++----------
 testcases/kernel/syscalls/quotactl/quotactl01.c | 11 ++---------
 testcases/kernel/syscalls/quotactl/quotactl06.c |  9 +--------
 5 files changed, 11 insertions(+), 32 deletions(-)
doc/test-writing-guidelines.txt +0 -0
index 6f1eee490..75c2e517a 100644
@@ -1283,8 +1283,8 @@ return value is '255' if 'execvp()' failed with

'ENOENT' and '254' otherwise.
'stdout_path' and 'stderr_path' determine where to redirect the program stdout and stderr I/O streams.

-The 'SAFE_CMD()' macro can be used automatic handling non zero exits (exits

doc/test-writing-guidelines.txt +1 -1
-with 'TBROK') or 'ENOENT' (exits with 'TCONF').
+The 'SAFE_CMD()' macro can be used automatic handling non-zero exits

(exits

doc/test-writing-guidelines.txt +1 -0
+with 'TBROK') and 'ENOENT' (exits with 'TCONF').
 
 .Example
 [source,c]
include/tst_safe_macros.h +1 -3
index 38eb67d1d..bcd4b9c9e 100644
@@ -540,9 +540,8 @@ void safe_cmd(const char *file, const int lineno,
 const char *const argv[],
 {
        int rval;
 
-       switch ((rval = tst_run_cmd(argv, stdout_path, stderr_path,
-                                   TST_RUN_CMD_PASS_EXIT_VAL |
-                                   TST_RUN_CMD_CHECK_CMD))) {
+       switch ((rval = tst_cmd(argv, stdout_path, stderr_path,
Quoted 4 lines

TST_CMD_PASS_RETVAL
+ |
TST_CMD_CHECK_CMD))) {

Oh, you did the renaming in patch 5/5, but pity that the TST_CMD_TCONF_ON_MISSING still not being used:). I guess this modification should belong to patch4/5.

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200330/6433524c/attachment.htm>
author
#10 lore raw

PATCH v2 5/5 Use SAFE_CMD()

Hi Li,

Quoted 3 lines

Oh, you did the renaming in patch 5/5, but pity that the TST_CMD_TCONF_ON_MISSING still not being used:). I guess this modification should belong to patch4/5.

Thanks for careful review. Yes, I found that as well + the other problem you reported - tst_run_cmd() during one more check.

+ I needed to put back static inline for safe_cmd() (it was in previous version, it's really needed). So there is latest version available on
https://github.com/pevik/ltp/tree/needs_cmds.v2.fixes

which passed some travis jobs (I'll check that tomorrow)
https://travis-ci.org/github/pevik/ltp/builds/668925599

Diff from v2 below.

IMHO now it should be ok, but I'm waiting for your or anybody else ack/review.

Kind regards,
Petr

test-writing-guidelines.txt +1 -1
index 75c2e517a..895f6c99d 100644
@@ -1263,7 +1263,7 @@ different once the call returns and should be used only for rough estimates.
 int tst_cmd(const char *const argv[],
 	        const char *stdout_path,
 	        const char *stderr_path,
-			enum tst_cmd_flags flags);
+	        enum tst_cmd_flags flags);
 -------------------------------------------------------------------------------
 
 'tst_cmd()' is a wrapper for 'vfork() + execvp()' which provides a way
tst_safe_macros.h +3 -3
index d050c5990..8126179f4 100644
@@ -538,13 +538,13 @@ void safe_unshare(const char *file, const int lineno, int flags);
 void safe_setns(const char *file, const int lineno, int fd, int nstype);
 #define SAFE_SETNS(fd, nstype) safe_setns(__FILE__, __LINE__, (fd), (nstype));
 
-void safe_cmd(const char *file, const int lineno, const char *const argv[],
+static inline void safe_cmd(const char *file, const int lineno, const char *const argv[],
 				  const char *stdout_path, const char *stderr_path)
 {
 	int rval;
 
-	switch ((rval = tst_cmd(argv, stdout_path, stderr_path, TST_CMD_PASS_RETVAL
-							| TST_CMD_CHECK_CMD))) {
+	switch ((rval = tst_cmd(argv, stdout_path, stderr_path,
+				TST_CMD_PASS_RETVAL | TST_CMD_TCONF_ON_MISSING))) {
 	case 0:
 		break;
 	default:
author
#11 lore raw

PATCH v2 5/5 Use SAFE_CMD()

Hi Li,

...

Quoted 6 lines

+ I needed to put back static inline for safe_cmd() (it was in previous version, it's really needed). So there is latest version available on
https://github.com/pevik/ltp/tree/needs_cmds.v2.fixes

which passed some travis jobs (I'll check that tomorrow)
https://travis-ci.org/github/pevik/ltp/builds/668925599

Working. Just opensuse/tumbleweed fails, but that's due bug in the distro (it also broke CI on master, I'll report it tomorrow).

Kind regards,
Petr

#12 lore raw

PATCH v2 5/5 Use SAFE_CMD()

In reply to Petr Vorel

Hi Petr,

On Tue, Mar 31, 2020 at 4:03 AM Petr Vorel <[email protected]> wrote:

+ I needed to put back static inline for safe_cmd() (it was in previous

Quoted 6 lines

version,
it's really needed). So there is latest version available on
https://github.com/pevik/ltp/tree/needs_cmds.v2.fixes

Diff from v2 below.

Awesome work! ACK for this(needs_cmds.v2.fixes + Diff).

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200331/53c49c96/attachment.htm>
author
#13 lore raw

PATCH v2 5/5 Use SAFE_CMD()

Hi Li,

Quoted 1 line

Awesome work! ACK for this(needs_cmds.v2.fixes + Diff).

Thanks a lot for your extensive review!
Merged.

Kind regards,
Petr