public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v2 05/14] nstool: Replace "pid" subcommand with "info" subcommand
Date: Thu,  6 Apr 2023 13:28:10 +1000	[thread overview]
Message-ID: <20230406032819.707441-6-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20230406032819.707441-1-david@gibson.dropbear.id.au>

The new subcommand gives more information about the holder process and its
namespace, and may be further extended in future.  Add some options which
give the old behaviour for existing scripts.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 test/lib/setup | 12 +++++-----
 test/nstool.c  | 60 +++++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 55 insertions(+), 17 deletions(-)

diff --git a/test/lib/setup b/test/lib/setup
index e6180b1..6d7644a 100755
--- a/test/lib/setup
+++ b/test/lib/setup
@@ -78,7 +78,7 @@ setup_pasta() {
 	layout_pasta
 
 	context_run_bg unshare "unshare -rUnpf ${NSTOOL} hold ${STATESETUP}/ns.hold"
-	__target_pid=$(${NSTOOL} pid ${STATESETUP}/ns.hold)
+	__target_pid=$(${NSTOOL} info -pw ${STATESETUP}/ns.hold)
 
 	context_setup_nsenter ns -U -n -p --preserve-credentials -t ${__target_pid}
 
@@ -128,7 +128,7 @@ setup_passt_in_ns() {
 
 	context_run_bg pasta "./pasta ${__opts} -t 10001,10002,10011,10012 -T 10003,10013 -u 10001,10002,10011,10012 -U 10003,10013 -P ${STATESETUP}/pasta.pid --config-net ${NSTOOL} hold ${STATESETUP}/ns.hold"
 	wait_for [ -f "${STATESETUP}/pasta.pid" ]
-	__ns_pid=$(${NSTOOL} pid ${STATESETUP}/ns.hold)
+	__ns_pid=$(${NSTOOL} info -pw ${STATESETUP}/ns.hold)
 
 	context_setup_nsenter qemu "-t ${__ns_pid} -U -n -p --preserve-credentials"
 	context_setup_nsenter ns "-t ${__ns_pid} -U -n -p --preserve-credentials"
@@ -190,7 +190,7 @@ setup_two_guests() {
 	[ ${DEBUG} -eq 1 ] && __opts="${__opts} -d"
 	[ ${TRACE} -eq 1 ] && __opts="${__opts} --trace"
 	context_run_bg pasta_1 "./pasta ${__opts} --trace -l /tmp/pasta1.log -P ${STATESETUP}/pasta_1.pid -t 10001,10002 -T 10003,10004 -u 10001,10002 -U 10003,10004 --config-net ${NSTOOL} hold ${STATESETUP}/ns1.hold"
-	__ns1_pid=$(${NSTOOL} pid ${STATESETUP}/ns1.hold)
+	__ns1_pid=$(${NSTOOL} info -pw ${STATESETUP}/ns1.hold)
 	context_setup_nsenter passt_1 -U -n -p --preserve-credentials -t ${__ns1_pid}
 
 	__opts=
@@ -198,7 +198,7 @@ setup_two_guests() {
 	[ ${DEBUG} -eq 1 ] && __opts="${__opts} -d"
 	[ ${TRACE} -eq 1 ] && __opts="${__opts} --trace"
 	context_run_bg pasta_2 "./pasta ${__opts} --trace -l /tmp/pasta2.log -P ${STATESETUP}/pasta_2.pid -t 10004,10005 -T 10003,10001 -u 10004,10005 -U 10003,10001 --config-net ${NSTOOL} hold ${STATESETUP}/ns2.hold"
-	__ns2_pid=$(${NSTOOL} pid ${STATESETUP}/ns2.hold)
+	__ns2_pid=$(${NSTOOL} info -pw ${STATESETUP}/ns2.hold)
 	context_setup_nsenter passt_2 -U -n -p --preserve-credentials -t ${__ns2_pid}
 
 	context_setup_nsenter qemu_1 -U -n -p --preserve-credentials -t ${__ns1_pid}
@@ -310,8 +310,8 @@ teardown_passt_in_ns() {
 
 # teardown_two_guests() - Exit namespaces, kill qemu processes, passt and pasta
 teardown_two_guests() {
-	__ns1_pid=$(${NSTOOL} pid "${STATESETUP}/ns1.hold")
-	__ns2_pid=$(${NSTOOL} pid "${STATESETUP}/ns2.hold")
+	__ns1_pid=$(${NSTOOL} info -pw "${STATESETUP}/ns1.hold")
+	__ns2_pid=$(${NSTOOL} info -pw "${STATESETUP}/ns2.hold")
 	nsenter -U -p --preserve-credentials -t ${__ns1_pid} kill $(cat "${STATESETUP}/qemu_1.pid")
 	nsenter -U -p --preserve-credentials -t ${__ns2_pid} kill $(cat "${STATESETUP}/qemu_2.pid")
 	context_wait qemu_1
diff --git a/test/nstool.c b/test/nstool.c
index c75d458..5f54439 100644
--- a/test/nstool.c
+++ b/test/nstool.c
@@ -14,6 +14,7 @@
 #include <stdbool.h>
 #include <errno.h>
 #include <unistd.h>
+#include <getopt.h>
 #include <sys/socket.h>
 #include <linux/un.h>
 
@@ -30,9 +31,11 @@ static void usage(void)
 	    "    Run within a set of namespaces, open a Unix domain socket\n"
 	    "    (the \"control socket\") at SOCK and wait for requests from\n"
 	    "    other nstool subcommands.\n"
-	    "  nstool pid SOCK\n"
-	    "    Print the pid of the nstool hold process with control socket\n"
-	    "    at SOCK, as seen in the caller's namespace.\n"
+	    "  nstool info [-pw] pid SOCK\n"
+	    "    Print information about the nstool hold process with control\n"
+	    "    socket at SOCK\n"
+	    "      -p    Print just the holder's PID as seen by the caller\n"
+	    "      -w    Retry connecting to SOCK until it is ready\n"
 	    "  nstool stop SOCK\n"
 	    "    Instruct the nstool hold with control socket at SOCK to\n"
 	    "    terminate.\n");
@@ -103,17 +106,45 @@ static void cmd_hold(int argc, char *argv[])
 	unlink(sockpath);
 }
 
-static void cmd_pid(int argc, char *argv[])
+static void cmd_info(int argc, char *argv[])
 {
-	const char *sockpath = argv[1];
+	const struct option options[] = {
+		{"pid",		no_argument, 	NULL,	'p' },
+		{"wait",	no_argument,	NULL,	'w' },
+		{ 0 },
+	};
+	bool pidonly = false, waitforsock = false;
 	struct ucred peercred;
 	socklen_t optlen = sizeof(peercred);
-	int fd, rc;
+	const char *optstring = "pw";
+	const char *sockpath;
+	int fd, rc, opt;
 
-	if (argc != 2)
+	do {
+		opt = getopt_long(argc, argv, optstring, options, NULL);
+
+		switch (opt) {
+		case 'p':
+			pidonly = true;
+			break;
+		case 'w':
+			waitforsock = true;
+			break;
+		case -1:
+			break;
+		default:
+			usage();
+		}
+	} while (opt != -1);
+
+	if (optind != argc - 1) {
+		fprintf(stderr, "B\n");
 		usage();
+	}
+
+	sockpath = argv[optind];
 
-	fd = connect_ctl(sockpath, true);
+	fd = connect_ctl(sockpath, waitforsock);
 
 	rc = getsockopt(fd, SOL_SOCKET, SO_PEERCRED,
 			&peercred, &optlen);
@@ -123,7 +154,14 @@ static void cmd_pid(int argc, char *argv[])
 
 	close(fd);
 
-	printf("%d\n", peercred.pid);
+	if (pidonly) {
+		printf("%d\n", peercred.pid);
+	} else {
+		printf("As seen from calling context:\n");
+		printf("\tPID:\t%d\n", peercred.pid);
+		printf("\tUID:\t%u\n", peercred.uid);
+		printf("\tGID:\t%u\n", peercred.gid);
+	}
 }
 
 static void cmd_stop(int argc, char *argv[])
@@ -158,8 +196,8 @@ int main(int argc, char *argv[])
 
 	if (strcmp(subcmd, "hold") == 0)
 		cmd_hold(argc - 1, argv + 1);
-	else if (strcmp(subcmd, "pid") == 0)
-		cmd_pid(argc - 1, argv + 1);
+	else if (strcmp(subcmd, "info") == 0)
+		cmd_info(argc - 1, argv + 1);
 	else if (strcmp(subcmd, "stop") == 0)
 		cmd_stop(argc - 1, argv + 1);
 	else
-- 
@@ -14,6 +14,7 @@
 #include <stdbool.h>
 #include <errno.h>
 #include <unistd.h>
+#include <getopt.h>
 #include <sys/socket.h>
 #include <linux/un.h>
 
@@ -30,9 +31,11 @@ static void usage(void)
 	    "    Run within a set of namespaces, open a Unix domain socket\n"
 	    "    (the \"control socket\") at SOCK and wait for requests from\n"
 	    "    other nstool subcommands.\n"
-	    "  nstool pid SOCK\n"
-	    "    Print the pid of the nstool hold process with control socket\n"
-	    "    at SOCK, as seen in the caller's namespace.\n"
+	    "  nstool info [-pw] pid SOCK\n"
+	    "    Print information about the nstool hold process with control\n"
+	    "    socket at SOCK\n"
+	    "      -p    Print just the holder's PID as seen by the caller\n"
+	    "      -w    Retry connecting to SOCK until it is ready\n"
 	    "  nstool stop SOCK\n"
 	    "    Instruct the nstool hold with control socket at SOCK to\n"
 	    "    terminate.\n");
@@ -103,17 +106,45 @@ static void cmd_hold(int argc, char *argv[])
 	unlink(sockpath);
 }
 
-static void cmd_pid(int argc, char *argv[])
+static void cmd_info(int argc, char *argv[])
 {
-	const char *sockpath = argv[1];
+	const struct option options[] = {
+		{"pid",		no_argument, 	NULL,	'p' },
+		{"wait",	no_argument,	NULL,	'w' },
+		{ 0 },
+	};
+	bool pidonly = false, waitforsock = false;
 	struct ucred peercred;
 	socklen_t optlen = sizeof(peercred);
-	int fd, rc;
+	const char *optstring = "pw";
+	const char *sockpath;
+	int fd, rc, opt;
 
-	if (argc != 2)
+	do {
+		opt = getopt_long(argc, argv, optstring, options, NULL);
+
+		switch (opt) {
+		case 'p':
+			pidonly = true;
+			break;
+		case 'w':
+			waitforsock = true;
+			break;
+		case -1:
+			break;
+		default:
+			usage();
+		}
+	} while (opt != -1);
+
+	if (optind != argc - 1) {
+		fprintf(stderr, "B\n");
 		usage();
+	}
+
+	sockpath = argv[optind];
 
-	fd = connect_ctl(sockpath, true);
+	fd = connect_ctl(sockpath, waitforsock);
 
 	rc = getsockopt(fd, SOL_SOCKET, SO_PEERCRED,
 			&peercred, &optlen);
@@ -123,7 +154,14 @@ static void cmd_pid(int argc, char *argv[])
 
 	close(fd);
 
-	printf("%d\n", peercred.pid);
+	if (pidonly) {
+		printf("%d\n", peercred.pid);
+	} else {
+		printf("As seen from calling context:\n");
+		printf("\tPID:\t%d\n", peercred.pid);
+		printf("\tUID:\t%u\n", peercred.uid);
+		printf("\tGID:\t%u\n", peercred.gid);
+	}
 }
 
 static void cmd_stop(int argc, char *argv[])
@@ -158,8 +196,8 @@ int main(int argc, char *argv[])
 
 	if (strcmp(subcmd, "hold") == 0)
 		cmd_hold(argc - 1, argv + 1);
-	else if (strcmp(subcmd, "pid") == 0)
-		cmd_pid(argc - 1, argv + 1);
+	else if (strcmp(subcmd, "info") == 0)
+		cmd_info(argc - 1, argv + 1);
 	else if (strcmp(subcmd, "stop") == 0)
 		cmd_stop(argc - 1, argv + 1);
 	else
-- 
2.39.2


  parent reply	other threads:[~2023-04-06  3:28 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-06  3:28 [PATCH v2 00/14] Improved tool for testing across multiple namespaces David Gibson
2023-04-06  3:28 ` [PATCH v2 01/14] nstool: Rename nsholder to nstool David Gibson
2023-04-06  3:28 ` [PATCH v2 02/14] nstool: Reverse parameters " David Gibson
2023-04-06  3:28 ` [PATCH v2 03/14] nstool: Move description of its operation modes from comment to usage David Gibson
2023-04-06  3:28 ` [PATCH v2 04/14] nstool: Split some command line parsing and socket setup to subcommands David Gibson
2023-04-06  3:28 ` David Gibson [this message]
2023-04-06  3:28 ` [PATCH v2 06/14] nstool: Detect what namespaces target is in David Gibson
2023-04-06  3:28 ` [PATCH v2 07/14] nstool: Add magic number to advertized information David Gibson
2023-04-06  3:28 ` [PATCH v2 08/14] nstool: Helpers to iterate through namespace types David Gibson
2023-04-06  3:28 ` [PATCH v2 09/14] nstool: Add nstool exec command to execute commands in an nstool namespace David Gibson
2023-04-06  3:28 ` [PATCH v2 10/14] nstool: Add --keep-caps option to nstool exec David Gibson
2023-04-06  3:28 ` [PATCH v2 11/14] test: Initialise ${TRACE} properly David Gibson
2023-04-06  3:28 ` [PATCH v2 12/14] test: Use "nstool exec" to slightly simplify tests David Gibson
2023-04-06  3:28 ` [PATCH v2 13/14] nstool: Advertise the holder's cwd (in its mountns) across the socket David Gibson
2023-04-06  3:28 ` [PATCH v2 14/14] nstool: Enter holder's cwd when changing mount ns with nstool exec David Gibson
2023-04-07 23:12 ` [PATCH v2 00/14] Improved tool for testing across multiple namespaces Stefano Brivio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230406032819.707441-6-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).