From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id C59CD5A0270 for ; Thu, 6 Apr 2023 05:28:29 +0200 (CEST) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4PsRm56cMsz4xFl; Thu, 6 Apr 2023 13:28:21 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1680751701; bh=+bhKVakMuqJlrcOIBOBMc4EsnJjskRbrOb6Pu7yF/FI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EUr+LgQYYGuSOeOTzEPMRO6q0AP8BOwIxJxerC7k5GHideXvfcEiLMhdtNpaSDE2B vjj8h65Gxepe+J5r1rSKTqO7lJG0quRCoYDXx2RAUoVynCsDB3K5bJ+7GHwUwLXbmS l+tTwAb0PoXnN+qHcJlUKlKgTELU5DDiMwYxsYr4= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 08/14] nstool: Helpers to iterate through namespace types Date: Thu, 6 Apr 2023 13:28:13 +1000 Message-Id: <20230406032819.707441-9-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230406032819.707441-1-david@gibson.dropbear.id.au> References: <20230406032819.707441-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: IXMWG6ZISMY5KNDIABP2NSQJ4KPDESYT X-Message-ID-Hash: IXMWG6ZISMY5KNDIABP2NSQJ4KPDESYT X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Will make things a bit less verbose in future. Signed-off-by: David Gibson --- test/nstool.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/test/nstool.c b/test/nstool.c index 7b222dd..b70b053 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; -- 2.39.2