Skip to contentSkip to conversation

Stringify flags to improve error msg of unshare()

PATCH New Clemens Famulla-Conrad 3 participants 3 messages Feb 26, 2020 – Feb 27, 2020 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Stringify flags to improve error msg of unshare()

If your test has multiple calls of unshare() it is hard to read which unshare() call really failed. With this we improve the error message to something like this:

sendmsg03.c:43: CONF: unshare(CLONE_NEWUSER) unsupported: EINVAL (22)

Instead of having a hard to understand number like:

sendmsg03.c:43: CONF: unshare(268435456) unsupported: EINVAL (22)

---
 include/tst_safe_macros.h | 5 +++--
 lib/tst_safe_macros.c     | 7 ++++---
 2 files changed, 7 insertions(+), 5 deletions(-)
include/tst_safe_macros.h +3 -2
index 80c4d9cb9..f2f8bd10f 100644
@@ -544,7 +544,8 @@ int safe_personality(const char *filename, unsigned int lineno,
 	}							\
 	} while (0)
 
-void safe_unshare(const char *file, const int lineno, int flags);
-#define SAFE_UNSHARE(flags) safe_unshare(__FILE__, __LINE__, (flags))
+void safe_unshare(const char *file, const int lineno, const char *flags_str,
+	int flags);
+#define SAFE_UNSHARE(flags) safe_unshare(__FILE__, __LINE__, #flags, (flags))
 
 #endif /* SAFE_MACROS_H__ */
lib/tst_safe_macros.c +4 -3
index f5413a18e..01270de2b 100644
@@ -187,7 +187,8 @@ int safe_chroot(const char *file, const int lineno, const char *path)
 	return rval;
 }
 
-void safe_unshare(const char *file, const int lineno, int flags)
+void safe_unshare(const char *file, const int lineno, const char *flags_str,
+		  int flags)
 {
 	int res;
 
@@ -195,10 +196,10 @@ void safe_unshare(const char *file, const int lineno, int flags)
 	if (res == -1) {
 		if (errno == EINVAL) {
 			tst_brk_(file, lineno, TCONF | TERRNO,
-				 "unshare(%d) unsupported", flags);
+				 "unshare(%s) unsupported", flags_str);
 		} else {
 			tst_brk_(file, lineno, TBROK | TERRNO,
-				 "unshare(%d) failed", flags);
+				 "unshare(%s) failed", flags_str);
 		}
 	}
 }
Signature · 1 line
2.16.4
#1 lore raw

PATCH 1/1 Stringify flags to improve error msg of unshare()

Hi Clemens,

Quoted 11 lines

If your test has multiple calls of unshare() it is hard to read which unshare() call really failed. With this we improve the error message to something like this:

sendmsg03.c:43: CONF: unshare(CLONE_NEWUSER) unsupported: EINVAL (22)

Instead of having a hard to understand number like:

sendmsg03.c:43: CONF: unshare(268435456) unsupported: EINVAL (22)

LGTM. BTW I guess sendmsg03.c is part of patch you're working on atm, right? It's a bit confusing to refer to file which does not exist in git yet. Actually, no test is using yet since it's adding in db2f0ed9e.

Kind regards,
Petr

#2 lore raw

PATCH 1/1 Stringify flags to improve error msg of unshare()

In reply to Clemens Famulla-Conrad

Hi!

Quoted 21 lines

If your test has multiple calls of unshare() it is hard to read which unshare() call really failed. With this we improve the error message to something like this:

sendmsg03.c:43: CONF: unshare(CLONE_NEWUSER) unsupported: EINVAL (22)

Instead of having a hard to understand number like:

sendmsg03.c:43: CONF: unshare(268435456) unsupported: EINVAL (22)

---
 include/tst_safe_macros.h | 5 +++--
 lib/tst_safe_macros.c     | 7 ++++---
 2 files changed, 7 insertions(+), 5 deletions(-)
include/tst_safe_macros.h +0 -0
index 80c4d9cb9..f2f8bd10f 100644

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

Quoted 8 lines
include/tst_safe_macros.h +3 -2
@@ -544,7 +544,8 @@ int safe_personality(const char *filename, unsigned int lineno,
 	}							\
 	} while (0)
 
-void safe_unshare(const char *file, const int lineno, int flags);
-#define SAFE_UNSHARE(flags) safe_unshare(__FILE__, __LINE__, (flags))
+void safe_unshare(const char *file, const int lineno, const char *flags_str,
+	int flags);
+#define SAFE_UNSHARE(flags) safe_unshare(__FILE__, __LINE__, #flags, (flags))

I would actually prefer to have a lookup table instead, because this breaks when pass flags by a variable, e.g.

	int ns_flags = CLONE_NEWNS | CLONE_NEWPID;

	SAFE_UNSHARE(ns_flags);

Looking that the flags, these are bigflags, which makes it a bit tricky, so we will have to write it as:

void get_ns_flags(int flags, char *flags, size_t flags_size)

{
	int first = 1;

	if (flags & CLONE_VM)
		append_flags("CLONE_VM", &first, &flags, &flags_size);

	if (flags & CLONE_FS)
		append_flags("CLONE_FS", &first, &flags, &flags_size);

	...
}
Signature · 2 lines
Cyril Hrubis
[email protected]