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 85B145A026F for ; Tue, 4 Apr 2023 03:46:49 +0200 (CEST) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Pr9bj485cz4xFh; Tue, 4 Apr 2023 11:46:41 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1680572801; bh=8O2Xd0fmJxf39EeXAw6Iq5/AaE+rJeB1Vu3ISHl7BSY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=hOxda6fTawbQDsWbulBp9JOwVcnMLKpAElAw3SpAhlK/qmpwqcitfzYk2L4H3lrUM WjQRMKSVAf+WWIhI7miwWeJM43cWDpLgcKZ1dg0hw+ntReY1yOWtt3fOBdjEfwgEix 6kg267y8TZWDpKQdxTkE76sPEYydw1WsZQirquj4= From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 08/14] nstool: Helpers to iterate through namespace types Date: Tue, 4 Apr 2023 11:46:32 +1000 Message-Id: <20230404014638.3225556-9-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230404014638.3225556-1-david@gibson.dropbear.id.au> References: <20230404014638.3225556-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: PHW6MLQDWGBHRZN3VXMMGQK2SZLO7D7Q X-Message-ID-Hash: PHW6MLQDWGBHRZN3VXMMGQK2SZLO7D7Q 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 e995f3e..5681ce8 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, 0x7fffffff) + #define NSTOOL_MAGIC 0x75601d75 struct holder_info { @@ -196,11 +204,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; @@ -218,15 +225,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