Skip to contentSkip to conversation

lib/safe_file_ops: print file info when file_lines_scanf parsed fail

PATCH New Yang Xu 2 participants 12 messages Apr 21, 2020 – May 6, 2020 Library code

lore
Share & tools
lore

Yang Xu

author
#0 lore raw

PATCH 1/3 lib/safe_file_ops: print file info when file_lines_scanf parsed fail

---
 lib/safe_file_ops.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/safe_file_ops.c +1 -1
index b576cb97e..e8ac2fdb8 100644
@@ -217,7 +217,7 @@ int file_lines_scanf(const char *file, const int lineno,
 
 	if (strict && ret != arg_count) {
 		tst_brkm(TBROK, cleanup_fn, "Expected %i conversions got %i"
-			" at %s:%d", arg_count, ret, file, lineno);
+			" in %s at %s:%d", arg_count, ret, path, file, lineno);
 		return 1;
 	}
 
Signature · 1 line
2.23.0
author
#1 lore raw

PATCH 2/3 lib: Add TST_ASSERT_FILE_INT and TST_ASSERT_FILE_STR

These functions are similar to TST_ASSERT_INT/STR, but they are designed to get field value of proc or sys file. ie NoNewPrivs field in the /proc/[pid]/status.

---
 include/tst_assert.h | 28 ++++++++++++++++++++++++++--
 lib/tst_assert.c     | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 2 deletions(-)
include/tst_assert.h +26 -2
index 04e80777c..913fff1b5 100644
@@ -16,7 +16,20 @@
  * values in sysfs, procfs, etc.
  */
 void tst_assert_int(const char *file, const int lineno,
-                    const char *path, int val);
+		    const char *path, int val);
+
+#define TST_ASSERT_FILE_INT(path, buf, val) \
+	tst_assert_file_int(__FILE__, __LINE__, path, buf, val)
+
+/*
+ * Asserts that integer value stored in the buf field of file pointed by path
+ * equals to the value passed to this function. This is mostly useful for
+ * asserting correct field values in sysfs, procfs, etc.
+ */
+
+void tst_assert_file_int(const char *file, const int lineno,
+			 const char *path, const char *buf, int val);
+
 
 #define TST_ASSERT_STR(path, val) \
 	tst_assert_str(__FILE__, __LINE__, path, val)
@@ -27,6 +40,17 @@ void tst_assert_int(const char *file, const int lineno,
  * values in sysfs, procfs, etc.
  */
 void tst_assert_str(const char *file, const int lineno,
-                    const char *path, const char *val);
+		    const char *path, const char *val);
+
+#define TST_ASSERT_FILE_STR(path, buf, val) \
+	tst_assert_file_str(__FILE__, __LINE__, path, buf, val)
+
+/*
+ * Asserts that a string value stored in the buf field of file pointed by path
+ * equals to the value passed to this function. This is mostly useful for
+ * asserting correct field values in sysfs, procfs, etc.
+ */
+void tst_assert_file_str(const char *file, const int lineno,
+			 const char *path, const char *buf, const char *val);
 
 #endif /* TST_ASSERT_H__ */
lib/tst_assert.c +33 -0
index 8ef3cd72e..65ee76473 100644
@@ -4,6 +4,7 @@
  * Author: Yang Xu <[email protected]>
  * Copyright (c) 2020 Cyril Hrubis <[email protected]>
  */
+#include <stdio.h>
 #define TST_NO_DEFAULT_MAIN
 #include "tst_assert.h"
 #include "tst_test.h"
@@ -22,6 +23,22 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
 	tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
 }
 
+void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *buf, int val)
+{
+	int sys_val;
+	char fmt[1024];
+
+	sprintf(fmt, "%s: %%d", buf);
+	SAFE_FILE_LINES_SCANF(path, fmt, &sys_val);
+
+	if (val == sys_val) {
+		tst_res_(file, lineno, TPASS, "%s %s = %d", path, buf, sys_val);
+		return;
+	}
+
+	tst_res_(file, lineno, TFAIL, "%s %s != %d got %d", path, buf, val, sys_val);
+}
+
 void tst_assert_str(const char *file, const int lineno, const char *path, const char *val)
 {
 	char sys_val[1024];
@@ -34,3 +51,19 @@ void tst_assert_str(const char *file, const int lineno, const char *path, const
 
 	tst_res_(file, lineno, TFAIL, "%s != '%s' got '%s'", path, val, sys_val);
 }
+
+void tst_assert_file_str(const char *file, const int lineno, const char *path, const char *buf, const char *val)
+{
+	char sys_val[1024];
+	char fmt[2048];
+
+	sprintf(fmt, "%s: %%1024s", buf);
+	SAFE_FILE_LINES_SCANF(path, fmt, sys_val);
+
+	if (!strcmp(val, sys_val)) {
+		tst_res_(file, lineno, TPASS, "%s %s = '%s'", path, buf, sys_val);
+		return;
+	}
+
+	tst_res_(file, lineno, TFAIL, "%s %s != '%s' got '%s'", path, buf, val, sys_val);
+}
Signature · 1 line
2.23.0
#2 lore raw

PATCH 2/3 lib: Add TST_ASSERT_FILE_INT and TST_ASSERT_FILE_STR

Hi!

Quoted 14 lines

#endif /* TST_ASSERT_H__ */

lib/tst_assert.c +1 -0
index 8ef3cd72e..65ee76473 100644
@@ -4,6 +4,7 @@
  * Author: Yang Xu <[email protected]>
  * Copyright (c) 2020 Cyril Hrubis <[email protected]>
  */
+#include <stdio.h>
 #define TST_NO_DEFAULT_MAIN
 #include "tst_assert.h"
 #include "tst_test.h"

lib/tst_assert.c void tst_assert_int(const char *file, const int lineno, const char *path, int va · line 31

Quoted 9 lines
lib/tst_assert.c +6 -0
@@ -22,6 +23,22 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
 	tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
 }
 
+void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *buf, int val)
+{
+	int sys_val;
+	char fmt[1024];
+
+	sprintf(fmt, "%s: %%d", buf);

If we want to keep the function as generic as possible we shouldn't add the colon and space after the %s here.

There is no standard on proc files, for instance it wouldn't be possible to parse /proc/vmstat if we hardcode the format string like that.

So I would just change this to "%s%%d" instead and pass "Foo: " instead just "Foo" in the testcases.

Also I guess that we should call it prefix rather than buf, but that's minor.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#3 lore raw

PATCH 2/3 lib: Add TST_ASSERT_FILE_INT and TST_ASSERT_FILE_STR

Hi Cyril

Quoted 28 lines

Hi!

Quoted 14 lines
  #endif /* TST_ASSERT_H__ */
lib/tst_assert.c +1 -0
index 8ef3cd72e..65ee76473 100644
@@ -4,6 +4,7 @@
   * Author: Yang Xu <[email protected]>
   * Copyright (c) 2020 Cyril Hrubis <[email protected]>
   */
+#include <stdio.h>
  #define TST_NO_DEFAULT_MAIN
  #include "tst_assert.h"
  #include "tst_test.h"

lib/tst_assert.c void tst_assert_int(const char *file, const int lineno, const char *path, int va · line 31

Quoted 9 lines
lib/tst_assert.c +6 -0
@@ -22,6 +23,22 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
  	tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
  }
 
+void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *buf, int val)
+{
+	int sys_val;
+	char fmt[1024];
+
+	sprintf(fmt, "%s: %%d", buf);

If we want to keep the function as generic as possible we shouldn't add the colon and space after the %s here.

Quoted 5 lines

There is no standard on proc files, for instance it wouldn't be possible to parse /proc/vmstat if we hardcode the format string like that.

So I would just change this to "%s%%d" instead and pass "Foo: " instead just "Foo" in the testcases.

Yes, good suggestion!!

Quoted 3 lines

Also I guess that we should call it prefix rather than buf, but that's minor.

Agree. I will send a v2 patch.

Best Regards
Yang Xu

Quoted 1 line
author
#4 lore raw

PATCH v2 1/3 lib/tst_assert: print correct file and lineno when parsing failed

In reply to Cyril Hrubis

Use safe_file_scanf instead of SAFE_FILE_SCANF, so we can pass correct file and lineno. ie prctl05.c

Before this patch, fail as below:
safe_file_ops.c:142: BROK: Failed to open FILE '/proc/self/comm1' for reading at tst_assert.c:29: ENOENT (2)

After this patch, fail as below:
safe_file_ops.c:142: BROK: Failed to open FILE '/proc/self/comm1' for reading at prctl05.c:63: ENOENT (2)

---
 lib/tst_assert.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/tst_assert.c +2 -2
index 8ef3cd72e..f05aea222 100644
@@ -12,7 +12,7 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
 {
 	int sys_val;
 
-	SAFE_FILE_SCANF(path, "%d", &sys_val);
+	safe_file_scanf(file, lineno, NULL, path, "%d", &sys_val);
 
 	if (val == sys_val) {
 		tst_res_(file, lineno, TPASS, "%s = %d", path, val);
@@ -26,7 +26,7 @@ void tst_assert_str(const char *file, const int lineno, const char *path, const
 {
 	char sys_val[1024];
 
-	SAFE_FILE_SCANF(path, "%1024s", sys_val);
+	safe_file_scanf(file, lineno, NULL, path, "%1024s", sys_val);
 	if (!strcmp(val, sys_val)) {
 		tst_res_(file, lineno, TPASS, "%s = '%s'", path, val);
 		return;
Signature · 1 line
2.23.0
author
#5 lore raw

PATCH v2 2/3 lib: Add TST_ASSERT_FILE_INT and TST_ASSERT_FILE_STR

These functions are similar to TST_ASSERT_INT/STR, but they are designed to get field value of proc or sys file. ie NoNewPrivs field in the /proc/[pid]/status.

---
 doc/test-writing-guidelines.txt |  8 ++++++++
 include/tst_assert.h            | 28 ++++++++++++++++++++++++++--
 lib/newlib_tests/.gitignore     |  1 +
 lib/newlib_tests/test_assert.c  | 19 +++++++++++++++++++
 lib/tst_assert.c                | 33 +++++++++++++++++++++++++++++++++
 5 files changed, 87 insertions(+), 2 deletions(-)
 create mode 100644 lib/newlib_tests/test_assert.c
doc/test-writing-guidelines.txt +8 -0
index 3e33fd4c1..bda9bcfd5 100644
@@ -2045,6 +2045,14 @@ terminated array of strings such as:
 },
 -------------------------------------------------------------------------------
 
+2.2.36 Assert sys or proc file value
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+Using TST_ASSERT_INT/STR(path, val) to assert that integer value or string stored in
+the prefix field of file pointed by path equals to the value passed to this function.
+
+Also having a similar api pair TST_ASSERT_FILE_INT/STR(path, prefix, val) to assert
+the field value of file.
+
 2.3 Writing a testcase in shell
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
include/tst_assert.h +26 -2
index 04e80777c..9969a8169 100644
@@ -16,7 +16,20 @@
  * values in sysfs, procfs, etc.
  */
 void tst_assert_int(const char *file, const int lineno,
-                    const char *path, int val);
+		    const char *path, int val);
+
+#define TST_ASSERT_FILE_INT(path, prefix, val) \
+	tst_assert_file_int(__FILE__, __LINE__, path, prefix, val)
+
+/*
+ * Asserts that integer value stored in the prefix field of file pointed by path
+ * equals to the value passed to this function. This is mostly useful for
+ * asserting correct field values in sysfs, procfs, etc.
+ */
+
+void tst_assert_file_int(const char *file, const int lineno,
+			 const char *path, const char *prefix, int val);
+
 
 #define TST_ASSERT_STR(path, val) \
 	tst_assert_str(__FILE__, __LINE__, path, val)
@@ -27,6 +40,17 @@ void tst_assert_int(const char *file, const int lineno,
  * values in sysfs, procfs, etc.
  */
 void tst_assert_str(const char *file, const int lineno,
-                    const char *path, const char *val);
+		    const char *path, const char *val);
+
+#define TST_ASSERT_FILE_STR(path, prefix, val) \
+	tst_assert_file_str(__FILE__, __LINE__, path, prefix, val)
+
+/*
+ * Asserts that a string value stored in the prefix field of file pointed by path
+ * equals to the value passed to this function. This is mostly useful for
+ * asserting correct field values in sysfs, procfs, etc.
+ */
+void tst_assert_file_str(const char *file, const int lineno,
+			 const char *path, const char *prefix, const char *val);
 
 #endif /* TST_ASSERT_H__ */
lib/newlib_tests/.gitignore +1 -0
index dd9899927..fee8795b6 100644
@@ -25,6 +25,7 @@ test18
 test19
 test20
 tst_expiration_timer
+test_assert
 test_timer
 test_exec
 test_exec_child
lib/newlib_tests/test_assert.c new file +19 -0
new file mode 100644
index 000000000..092303893
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 Yang Xu <[email protected]>
+ */
+
+#include <stdio.h>
+#include "tst_test.h"
+
+static void do_test(void)
+{
+	TST_ASSERT_INT("/proc/self/oom_score", 0);
+	TST_ASSERT_STR("/proc/self/comm", "test_assert");
+	TST_ASSERT_FILE_INT("/proc/self/io", "read_bytes:", 0);
+	TST_ASSERT_FILE_STR("/proc/self/status1", "State", "unexpected");
+}
+
+static struct tst_test test = {
+	.test_all = do_test,
+};
lib/tst_assert.c +33 -0
index f05aea222..8418fb72d 100644
@@ -4,6 +4,7 @@
  * Author: Yang Xu <[email protected]>
  * Copyright (c) 2020 Cyril Hrubis <[email protected]>
  */
+#include <stdio.h>
 #define TST_NO_DEFAULT_MAIN
 #include "tst_assert.h"
 #include "tst_test.h"
@@ -22,6 +23,22 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
 	tst_res_(file, lineno, TFAIL, "%s != %d got %d", path, val, sys_val);
 }
 
+void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *prefix, int val)
+{
+	int sys_val;
+	char fmt[1024];
+
+	sprintf(fmt, "%s%%d", prefix);
+	file_lines_scanf(file, lineno, NULL, 1, path, fmt, &sys_val);
+
+	if (val == sys_val) {
+		tst_res_(file, lineno, TPASS, "%s %s = %d", path, prefix, sys_val);
+		return;
+	}
+
+	tst_res_(file, lineno, TFAIL, "%s %s != %d got %d", path, prefix, val, sys_val);
+}
+
 void tst_assert_str(const char *file, const int lineno, const char *path, const char *val)
 {
 	char sys_val[1024];
@@ -34,3 +51,19 @@ void tst_assert_str(const char *file, const int lineno, const char *path, const
 
 	tst_res_(file, lineno, TFAIL, "%s != '%s' got '%s'", path, val, sys_val);
 }
+
+void tst_assert_file_str(const char *file, const int lineno, const char *path, const char *prefix, const char *val)
+{
+	char sys_val[1024];
+	char fmt[2048];
+
+	sprintf(fmt, "%s: %%1024s", prefix);
+	file_lines_scanf(file, lineno, NULL, 1, path, fmt, sys_val);
+
+	if (!strcmp(val, sys_val)) {
+		tst_res_(file, lineno, TPASS, "%s %s = '%s'", path, prefix, sys_val);
+		return;
+	}
+
+	tst_res_(file, lineno, TFAIL, "%s %s != '%s' got '%s'", path, prefix, val, sys_val);
+}
Signature · 1 line
2.23.0
#6 lore raw

PATCH v2 2/3 lib: Add TST_ASSERT_FILE_INT and TST_ASSERT_FILE_STR

Hi!

lib/tst_assert.c void tst_assert_int(const char *file, const int lineno, const char *path, int va · line 31

Quoted 6 lines
lib/tst_assert.c +6 -0
+void tst_assert_file_int(const char *file, const int lineno, const char *path, const char *prefix, int val)
+{
+	int sys_val;
+	char fmt[1024];
+
+	sprintf(fmt, "%s%%d", prefix);

I've changed this sprintf() to snprintf() and the same for the file_str function and pushed, thanks.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#7 lore raw

PATCH v2 3/3 syscalls/prctl: Use TST_ASSERT_FILE_INT/STR

In reply to Yang Xu
---
 testcases/kernel/syscalls/prctl/prctl06.c     | 20 ++++++--
 testcases/kernel/syscalls/prctl/prctl06.h     | 29 ++----------
 .../kernel/syscalls/prctl/prctl06_execve.c    | 13 ++++--
 testcases/kernel/syscalls/prctl/prctl07.c     | 46 +++++--------------
 4 files changed, 39 insertions(+), 69 deletions(-)
testcases/kernel/syscalls/prctl/prctl06.c +15 -5
index 0e1274a2a..b64c94e40 100644
@@ -21,16 +21,18 @@
 
 static uid_t nobody_uid;
 static gid_t nobody_gid;
+static int proc_flag = 1;
+static char proc_sup[20] = "Yes";
 
 static void do_prctl(void)
 {
 	char ipc_env_var[1024];
-	char *const argv[] = {BIN_PATH, "After execve, parent process", NULL};
-	char *const childargv[] = {BIN_PATH, "After execve, child process", NULL};
+	char *const argv[] = {BIN_PATH, "After execve, parent process", proc_sup, NULL};
+	char *const childargv[] = {BIN_PATH, "After execve, child process", proc_sup, NULL};
 	char *const envp[] = {ipc_env_var, NULL };
 	int childpid;
 
-	check_no_new_privs(0, "parent");
+	check_no_new_privs(0, "parent", proc_flag);
 
 	TEST(prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0));
 	if (TST_RET == -1) {
@@ -46,14 +48,14 @@ static void do_prctl(void)
 
 	childpid = SAFE_FORK();
 	if (childpid == 0) {
-		check_no_new_privs(1, "After fork, child process");
+		check_no_new_privs(1, "After fork, child process", proc_flag);
 		execve(BIN_PATH, childargv, envp);
 		tst_brk(TFAIL | TTERRNO,
 			"child process failed to execute prctl_execve");
 
 	} else {
 		tst_reap_children();
-		check_no_new_privs(1, "parent process");
+		check_no_new_privs(1, "parent process", proc_flag);
 		execve(BIN_PATH, argv, envp);
 		tst_brk(TFAIL | TTERRNO,
 			"parent process failed to execute prctl_execve");
@@ -74,6 +76,7 @@ static void verify_prctl(void)
 static void setup(void)
 {
 	struct passwd *pw;
+	int field;
 
 	pw = SAFE_GETPWNAM("nobody");
 	nobody_uid = pw->pw_uid;
@@ -96,6 +99,13 @@ static void setup(void)
 
 	tst_brk(TBROK | TTERRNO,
 		"current environment doesn't permit PR_GET/SET_NO_NEW_PRIVS");
+
+	TEST(FILE_LINES_SCANF(PROC_STATUS, "NoNewPrivs:%d", &field));
+	if (TST_RET == 1) {
+		tst_res(TCONF, "%s doesn't support NoNewPrivs field", PROC_STATUS);
+		proc_flag = 0;
+		strcpy(proc_sup, "No");
+	}
 }
 
 static const char *const resfile[] = {
testcases/kernel/syscalls/prctl/prctl06.h +3 -26
index 7f5be20bb..227ce3006 100644
@@ -24,30 +24,7 @@
 #define BIN_PATH           MNTPOINT"/"TESTBIN
 #define SUID_MODE          (S_ISUID|S_ISGID|S_IXUSR|S_IXGRP|S_IXOTH)
 
-void check_proc_field(int val, char *name)
-{
-	static int flag = 1;
-	int field = 0;
-
-	if (!flag)
-		return;
-
-	TEST(FILE_LINES_SCANF(PROC_STATUS, "NoNewPrivs:%d", &field));
-	if (TST_RET == 1) {
-		tst_res(TCONF,
-			"%s doesn't support NoNewPrivs field", PROC_STATUS);
-		flag = 0;
-		return;
-	}
-	if (val == field)
-		tst_res(TPASS, "%s %s NoNewPrivs field expected %d got %d",
-			name, PROC_STATUS, val, field);
-	else
-		tst_res(TFAIL, "%s %s NoNewPrivs field expected %d got %d",
-			name, PROC_STATUS, val, field);
-}
-
-void check_no_new_privs(int val, char *name)
+void check_no_new_privs(int val, char *name, int flag)
 {
 	TEST(prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0));
 	if (TST_RET == val)
@@ -58,8 +35,8 @@ void check_no_new_privs(int val, char *name)
 		tst_res(TFAIL,
 			"%s prctl(PR_GET_NO_NEW_PRIVS) expected %d got %ld",
 			name, val, TST_RET);
-
-	check_proc_field(val, name);
+	if (flag)
+		TST_ASSERT_FILE_INT(PROC_STATUS, "NoNewPrivs:", val);
 }
 
 #endif
testcases/kernel/syscalls/prctl/prctl06_execve.c +9 -4
index d1e60e6c2..cca261ac4 100644
@@ -12,14 +12,19 @@
 int main(int argc, char **argv)
 {
 	struct passwd *pw;
+	int proc_flag;
 
 	pw = SAFE_GETPWNAM("nobody");
 
 	tst_reinit();
-	if (argc != 2)
-		tst_brk(TFAIL, "argc is %d, expected 2", argc);
-
-	check_no_new_privs(1, argv[1]);
+	if (argc != 3)
+		tst_brk(TFAIL, "argc is %d, expected 3", argc);
+
+	if (!strcmp(argv[2], "Yes"))
+		proc_flag = 1;
+	else
+		proc_flag = 0;
+	check_no_new_privs(1, argv[1], proc_flag);
 
 	TEST(getegid());
 	if (TST_RET == 0)
testcases/kernel/syscalls/prctl/prctl07.c +12 -34

76 lines not on this page · raw message

Signature · 1 line
2.23.0
#8 lore raw

PATCH v2 3/3 syscalls/prctl: Use TST_ASSERT_FILE_INT/STR

Hi!

Quoted 1 line

+static char proc_sup[20] = "Yes";

I've changed the proc_sup to a pointer, there is no point in having this as an array when we can just change the pointer instead of strcpy().

Pushed, thanks.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#10 lore raw

PATCH 3/3 syscalls/prctl: Use TST_ASSERT_FILE_INT/STR

In reply to Yang Xu
---
 testcases/kernel/syscalls/prctl/prctl06.c     | 20 ++++++--
 testcases/kernel/syscalls/prctl/prctl06.h     | 29 ++----------
 .../kernel/syscalls/prctl/prctl06_execve.c    | 13 ++++--
 testcases/kernel/syscalls/prctl/prctl07.c     | 46 +++++--------------
 4 files changed, 39 insertions(+), 69 deletions(-)
testcases/kernel/syscalls/prctl/prctl06.c +15 -5
index 0e1274a2a..b64c94e40 100644
@@ -21,16 +21,18 @@
 
 static uid_t nobody_uid;
 static gid_t nobody_gid;
+static int proc_flag = 1;
+static char proc_sup[20] = "Yes";
 
 static void do_prctl(void)
 {
 	char ipc_env_var[1024];
-	char *const argv[] = {BIN_PATH, "After execve, parent process", NULL};
-	char *const childargv[] = {BIN_PATH, "After execve, child process", NULL};
+	char *const argv[] = {BIN_PATH, "After execve, parent process", proc_sup, NULL};
+	char *const childargv[] = {BIN_PATH, "After execve, child process", proc_sup, NULL};
 	char *const envp[] = {ipc_env_var, NULL };
 	int childpid;
 
-	check_no_new_privs(0, "parent");
+	check_no_new_privs(0, "parent", proc_flag);
 
 	TEST(prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0));
 	if (TST_RET == -1) {
@@ -46,14 +48,14 @@ static void do_prctl(void)
 
 	childpid = SAFE_FORK();
 	if (childpid == 0) {
-		check_no_new_privs(1, "After fork, child process");
+		check_no_new_privs(1, "After fork, child process", proc_flag);
 		execve(BIN_PATH, childargv, envp);
 		tst_brk(TFAIL | TTERRNO,
 			"child process failed to execute prctl_execve");
 
 	} else {
 		tst_reap_children();
-		check_no_new_privs(1, "parent process");
+		check_no_new_privs(1, "parent process", proc_flag);
 		execve(BIN_PATH, argv, envp);
 		tst_brk(TFAIL | TTERRNO,
 			"parent process failed to execute prctl_execve");
@@ -74,6 +76,7 @@ static void verify_prctl(void)
 static void setup(void)
 {
 	struct passwd *pw;
+	int field;
 
 	pw = SAFE_GETPWNAM("nobody");
 	nobody_uid = pw->pw_uid;
@@ -96,6 +99,13 @@ static void setup(void)
 
 	tst_brk(TBROK | TTERRNO,
 		"current environment doesn't permit PR_GET/SET_NO_NEW_PRIVS");
+
+	TEST(FILE_LINES_SCANF(PROC_STATUS, "NoNewPrivs:%d", &field));
+	if (TST_RET == 1) {
+		tst_res(TCONF, "%s doesn't support NoNewPrivs field", PROC_STATUS);
+		proc_flag = 0;
+		strcpy(proc_sup, "No");
+	}
 }
 
 static const char *const resfile[] = {
testcases/kernel/syscalls/prctl/prctl06.h +3 -26
index 7f5be20bb..a0c8ea05a 100644
@@ -24,30 +24,7 @@
 #define BIN_PATH           MNTPOINT"/"TESTBIN
 #define SUID_MODE          (S_ISUID|S_ISGID|S_IXUSR|S_IXGRP|S_IXOTH)
 
-void check_proc_field(int val, char *name)
-{
-	static int flag = 1;
-	int field = 0;
-
-	if (!flag)
-		return;
-
-	TEST(FILE_LINES_SCANF(PROC_STATUS, "NoNewPrivs:%d", &field));
-	if (TST_RET == 1) {
-		tst_res(TCONF,
-			"%s doesn't support NoNewPrivs field", PROC_STATUS);
-		flag = 0;
-		return;
-	}
-	if (val == field)
-		tst_res(TPASS, "%s %s NoNewPrivs field expected %d got %d",
-			name, PROC_STATUS, val, field);
-	else
-		tst_res(TFAIL, "%s %s NoNewPrivs field expected %d got %d",
-			name, PROC_STATUS, val, field);
-}
-
-void check_no_new_privs(int val, char *name)
+void check_no_new_privs(int val, char *name, int flag)
 {
 	TEST(prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0));
 	if (TST_RET == val)
@@ -58,8 +35,8 @@ void check_no_new_privs(int val, char *name)
 		tst_res(TFAIL,
 			"%s prctl(PR_GET_NO_NEW_PRIVS) expected %d got %ld",
 			name, val, TST_RET);
-
-	check_proc_field(val, name);
+	if (flag)
+		TST_ASSERT_FILE_INT(PROC_STATUS, "NoNewPrivs", val);
 }
 
 #endif
testcases/kernel/syscalls/prctl/prctl06_execve.c +9 -4
index d1e60e6c2..cca261ac4 100644
@@ -12,14 +12,19 @@
 int main(int argc, char **argv)
 {
 	struct passwd *pw;
+	int proc_flag;
 
 	pw = SAFE_GETPWNAM("nobody");
 
 	tst_reinit();
-	if (argc != 2)
-		tst_brk(TFAIL, "argc is %d, expected 2", argc);
-
-	check_no_new_privs(1, argv[1]);
+	if (argc != 3)
+		tst_brk(TFAIL, "argc is %d, expected 3", argc);
+
+	if (!strcmp(argv[2], "Yes"))
+		proc_flag = 1;
+	else
+		proc_flag = 0;
+	check_no_new_privs(1, argv[1], proc_flag);
 
 	TEST(getegid());
 	if (TST_RET == 0)
testcases/kernel/syscalls/prctl/prctl07.c +12 -34

76 lines not on this page · raw message

Signature · 1 line
2.23.0
#11 lore raw

PATCH 1/3 lib/safe_file_ops: print file info when file_lines_scanf parsed fail

In reply to Yang Xu

Hi!

lib/safe_file_ops.c int file_lines_scanf(const char *file, const int lineno, · line 220

Quoted 4 lines
lib/safe_file_ops.c +1 -1
 	if (strict && ret != arg_count) {
 		tst_brkm(TBROK, cleanup_fn, "Expected %i conversions got %i"
-			" at %s:%d", arg_count, ret, file, lineno);
+			" in %s at %s:%d", arg_count, ret, path, file, lineno);

I've changed the " in %s" to " FILKE '%s'" to match the style in the rest of the file and pushed, good catch, thanks.

Signature · 2 lines
Cyril Hrubis
[email protected]