From 3bcbca5db8547142df54debbe3ca2aba9e807b9c Mon Sep 17 00:00:00 2001 From: David Gibson Date: Thu, 6 Apr 2023 13:28:13 +1000 Subject: nstool: Helpers to iterate through namespace types Will make things a bit less verbose in future. Signed-off-by: David Gibson Signed-off-by: Stefano Brivio --- test/nstool.c | 22 ++++++++++++---------- 1 file 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; -- cgit v1.2.3