From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTP id 88E055A026D for ; Wed, 5 Apr 2023 13:58:59 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680695938; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3uFH+4Mnhs2PbywI8ACmWkAFhHEPcPCPl5mEtrMsQBw=; b=Niqm7OvMXzHzUsVsQ+QpfUk8lyg6JDVOup4vXvuvtZol08ZyZQGL0xI4uT5eVGy19FwSZ8 bLriXkRca9nh2L1MOMxVyEr+aCfPVHAR7wfezVvRx5QWeV9QQgucG9MkaKQ751yJ7MjUfs PGgz/lb1psvdrDdqNii1l05tp149J0Q= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-397-6AgDAkfeM_iOF9PsW5ntdA-1; Wed, 05 Apr 2023 07:58:57 -0400 X-MC-Unique: 6AgDAkfeM_iOF9PsW5ntdA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 03A7285A5A3; Wed, 5 Apr 2023 11:58:57 +0000 (UTC) Received: from elisabeth (unknown [10.39.208.33]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 5310040C83A9; Wed, 5 Apr 2023 11:58:56 +0000 (UTC) Date: Wed, 5 Apr 2023 13:58:54 +0200 From: Stefano Brivio To: David Gibson Subject: Re: [PATCH 09/14] nstool: Add nstool exec command to execute commands in an nstool namespace Message-ID: <20230405135854.15e988a7@elisabeth> In-Reply-To: <20230404014638.3225556-10-david@gibson.dropbear.id.au> References: <20230404014638.3225556-1-david@gibson.dropbear.id.au> <20230404014638.3225556-10-david@gibson.dropbear.id.au> Organization: Red Hat MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: 37LSKL6FM55LF74IYO4UIQQUMU5GDJIL X-Message-ID-Hash: 37LSKL6FM55LF74IYO4UIQQUMU5GDJIL X-MailFrom: sbrivio@redhat.com 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: passt-dev@passt.top 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: On Tue, 4 Apr 2023 11:46:33 +1000 David Gibson wrote: > This combines nstool info -pw with nsenter with various options for > a more convenient and less verbose of entering existing nstool managed > namespaces. > > Signed-off-by: David Gibson > --- > test/nstool.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++- > 1 file changed, 137 insertions(+), 2 deletions(-) > > diff --git a/test/nstool.c b/test/nstool.c > index 5681ce8..25079aa 100644 > --- a/test/nstool.c > +++ b/test/nstool.c > @@ -17,7 +17,9 @@ > #include > #include > #include > +#include > #include > +#include > #include > #include > #include > @@ -75,6 +77,9 @@ static void usage(void) > " 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 exec SOCK [COMMAND [ARGS...]]\n" > + " Execute command or shell in the namespaces of the nstool hold\n" > + " with control socket at SOCK\n" > " nstool stop SOCK\n" > " Instruct the nstool hold with control socket at SOCK to\n" > " terminate.\n"); > @@ -84,7 +89,7 @@ static int connect_ctl(const char *sockpath, bool wait, > struct holder_info *info, > struct ucred *peercred) > { > - int fd = socket(AF_UNIX, SOCK_STREAM, PF_UNIX); > + int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, PF_UNIX); > struct sockaddr_un addr = { > .sun_family = AF_UNIX, > }; > @@ -132,7 +137,7 @@ static int connect_ctl(const char *sockpath, bool wait, > > static void cmd_hold(int argc, char *argv[]) > { > - int fd = socket(AF_UNIX, SOCK_STREAM, PF_UNIX); > + int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, PF_UNIX); > struct sockaddr_un addr = { > .sun_family = AF_UNIX, > }; > @@ -301,6 +306,134 @@ static void cmd_info(int argc, char *argv[]) > } > } > > +static int openns(const char *fmt, ...) > +{ > + char nspath[PATH_MAX]; > + va_list ap; > + int fd; > + > + va_start(ap, fmt); > + if (vsnprintf(nspath, sizeof(nspath), fmt, ap) >= PATH_MAX) > + die("Truncated path \"%s\"\n", nspath); > + va_end(ap); > + > + fd = open(nspath, O_RDONLY | O_CLOEXEC); > + if (fd < 0) > + die("open() %s: %s\n", nspath, strerror(errno)); > + > + return fd; > +} > + > +static void wait_for_child(pid_t pid) > +{ > + int status; > + > + /* Match the child's exit status, if possible */ > + for (;;) { > + pid_t rc; > + > + rc = waitpid(pid, &status, WUNTRACED); > + if (rc < 0) > + die("waitpid() on %d: %s\n", pid, strerror(errno)); > + if (rc != pid) > + die("waitpid() on %d returned %d", pid, rc); > + if (WIFSTOPPED(status)) { > + /* Stop the parent to patch */ > + kill(getpid(), SIGSTOP); > + /* We must have resumed, resume the child */ > + kill(pid, SIGCONT); > + continue; > + } > + > + break; > + } > + > + if (WIFEXITED(status)) > + exit(WEXITSTATUS(status)); > + else if (WIFSIGNALED(status)) > + kill(getpid(), WTERMSIG(status)); An alternative could be what pasta_child_handler() does on WIFSIGNALED(status) -- I don't actually have a preference between the two. > + > + die("Unexpected status for child %d\n", pid); Weird indentation in this function. -- Stefano