Skip to contentSkip to conversation

lib: Implement SAFE_RUN() macro (new API only)

PATCH New Petr Vorel 2 participants 3 messages Mar 23, 2020 – Mar 25, 2020

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

PATCH v2 1/2 lib: Implement SAFE_RUN() macro (new API only)

---
Changes v1->v2:
Fixes errors reported by Xu:
* Fixed params

 doc/test-writing-guidelines.txt |  3 +++
 include/tst_safe_macros.h       | 20 ++++++++++++++++++++
 2 files changed, 23 insertions(+)
doc/test-writing-guidelines.txt +3 -0
index b56f1a0f2..b8330801b 100644
@@ -1282,6 +1282,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_RUNCMD()' 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 +20 -0
index 80c4d9cb9..dea101a8e 100644
@@ -547,4 +547,24 @@ 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))
 
+static inline void safe_run_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, 1))) {
+	case 0:
+		break;
+	case 255:
+		tst_brk(TCONF, "%s:%d: %s not found in $PATH", file, lineno, argv[0]);
+		break;
+	default:
+		tst_brk(TBROK, "%s:%d: %s failed (%d)", file, lineno, argv[0], rval);
+	}
+}
+#define SAFE_RUNCMD(argv, stdout_path, stderr_path) \
+	safe_run_cmd(__FILE__, __LINE__, (argv), (stdout_path), (stderr_path))
+
 #endif /* SAFE_MACROS_H__ */
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v2 2/2 Use SAFE_RUNCMD()

---
Changes v1->v2:
Fixes errors reported by Xu:
* Keep user addition

 testcases/kernel/syscalls/add_key/add_key05.c   | 17 ++++-------------
 testcases/kernel/syscalls/quotactl/quotactl01.c | 14 ++------------
 testcases/kernel/syscalls/quotactl/quotactl06.c | 12 +-----------
 3 files changed, 7 insertions(+), 36 deletions(-)
testcases/kernel/syscalls/add_key/add_key05.c +4 -13
index a39bfa0b7..560bbe478 100644
@@ -36,19 +36,10 @@ static void add_user(void)
 		return;
 
 	const char *const cmd_useradd[] = {"useradd", username, NULL};
-	int rc;
-
-	switch ((rc = tst_run_cmd(cmd_useradd, NULL, NULL, 1))) {
-	case 0:
-		user_added = 1;
-		ltpuser = SAFE_GETPWNAM(username);
-		break;
-	case 255:
-		tst_brk(TCONF, "useradd not found");
-		break;
-	default:
-		tst_brk(TBROK, "useradd failed (%d)", rc);
-	}
+
+	SAFE_RUNCMD(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 -12
index ede61d7e4..30151814e 100644
@@ -162,18 +162,8 @@ static struct tcase {
 static void setup(void)
 {
 	const char *const cmd[] = {"quotacheck", "-ugF", "vfsv0", MNTPOINT, NULL};
-	int ret;
-
-	ret = tst_run_cmd(cmd, NULL, NULL, 1);
-	switch (ret) {
-	case 0:
-		break;
-	case 255:
-		tst_brk(TCONF, "quotacheck binary not installed");
-		break;
-	default:
-		tst_brk(TBROK, "quotacheck exited with %i", ret);
-	}
+
+	SAFE_RUNCMD(cmd, NULL, NULL);
 
 	test_id = geteuid();
 	if (access(USRPATH, F_OK) == -1)
testcases/kernel/syscalls/quotactl/quotactl06.c +1 -11
index a3b4517e0..0b62e6240 100644
@@ -146,19 +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_run_cmd(cmd, NULL, NULL, 1);
-	switch (ret) {
-	case 0:
-		break;
-	case 255:
-		tst_brk(TCONF, "quotacheck binary not installed");
-		break;
-	default:
-		tst_brk(TBROK, "quotacheck exited with %i", ret);
-	}
+	SAFE_RUNCMD(cmd, NULL, NULL);
 
 	if (access(USRPATH, F_OK) == -1)
 		tst_brk(TFAIL | TERRNO, "user quotafile didn't exist");
Signature · 1 line
2.25.1
#2 lore raw

PATCH v2 1/2 lib: Implement SAFE_RUN() macro (new API only)

In reply to Petr Vorel

Hi Petr,

Quoted 3 lines · Petr Vorel
Quoted 8 lines
include/tst_safe_macros.h +5 -0
+       switch ((rval = tst_run_cmd(argv, stdout_path, stderr_path, 1))) {
+       case 0:
+               break;
+       case 255:
+               tst_brk(TCONF, "%s:%d: %s not found in $PATH", file,

lineno, argv[0]);
+ break;

Yes, we can remove this TCONF after adding the command checking in tst_run_cmd_fds_(). So now I think it's OK to apply this patchset and do the remaining work separately. Thanks Petr!

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20200325/139db264/attachment.htm>