aboutgitcodebugslistschat
path: root/test/nstool.c
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2023-04-06 13:28:13 +1000
committerStefano Brivio <sbrivio@redhat.com>2023-04-08 01:11:59 +0200
commit3bcbca5db8547142df54debbe3ca2aba9e807b9c (patch)
tree005c1b7f33b7abc7e536a79bf5c683139233484d /test/nstool.c
parentf6a9ea3af5384ac561b67494073d520145ae2281 (diff)
downloadpasst-3bcbca5db8547142df54debbe3ca2aba9e807b9c.tar
passt-3bcbca5db8547142df54debbe3ca2aba9e807b9c.tar.gz
passt-3bcbca5db8547142df54debbe3ca2aba9e807b9c.tar.bz2
passt-3bcbca5db8547142df54debbe3ca2aba9e807b9c.tar.lz
passt-3bcbca5db8547142df54debbe3ca2aba9e807b9c.tar.xz
passt-3bcbca5db8547142df54debbe3ca2aba9e807b9c.tar.zst
passt-3bcbca5db8547142df54debbe3ca2aba9e807b9c.zip
nstool: Helpers to iterate through namespace types
Will make things a bit less verbose in future. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Diffstat (limited to 'test/nstool.c')
-rw-r--r--test/nstool.c22
1 files changed, 12 insertions, 10 deletions
diff --git a/test/nstool.c b/test/nstool.c
index fccbfb9..d29187f 100644
--- a/test/nstool.c
+++ b/test/nstool.c
@@ -46,6 +46,14 @@ const struct ns_type nstypes[] = {
{ CLONE_NEWUTS, "uts" },
};
+#define for_each_nst(_nst, _flags) \
+ for ((_nst) = &nstypes[0]; \
+ ((_nst) - nstypes) < ARRAY_SIZE(nstypes); \
+ (_nst)++) \
+ if ((_flags) & (_nst)->flag)
+
+#define for_every_nst(_nst) for_each_nst(_nst, INT_MAX)
+
#define NSTOOL_MAGIC 0x7570017575601d75ULL
struct holder_info {
@@ -199,11 +207,10 @@ static ssize_t getlink(char *buf, size_t bufsiz, const char *fmt, ...)
static int detect_namespaces(pid_t pid)
{
- int i;
+ const struct ns_type *nst;
int flags = 0;
- for (i = 0; i < ARRAY_SIZE(nstypes); i++) {
- const struct ns_type *nst = &nstypes[i];
+ for_every_nst(nst) {
char selflink[PATH_MAX], pidlink[PATH_MAX];
ssize_t selflen, pidlen;
@@ -221,15 +228,10 @@ static int detect_namespaces(pid_t pid)
static void print_nstypes(int flags)
{
+ const struct ns_type *nst;
bool first = true;
- int i;
-
- for (i = 0; i < ARRAY_SIZE(nstypes); i++) {
- const struct ns_type *nst = &nstypes[i];
-
- if (!(flags & nst->flag))
- continue;
+ for_each_nst(nst, flags) {
printf("%s%s", first ? "" : ", " , nst->name);
first = false;
flags &= ~nst->flag;