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 B81965A026F for ; Thu, 6 Apr 2023 04:50:14 +0200 (CEST) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4PsQvz6LT4z4xFg; Thu, 6 Apr 2023 12:50:07 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1680749407; bh=1xLjusNeOTxfCQtiWQIxt0S9yR1VGXuW5gdF/2acPmc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Bi1dx3etOmmPicJb+f2SJqdwMrKhA2H7Uphu9na/JQPMSSvRprQm5DGu7HNvKg+3+ a1bSjOKPSRvlm1a1Ukczv9l47y/HIQcBGVC1ek6kJgDL42ihdPQcZDoRyKYtUJ92HR sMZxTJ4Y5ZZUTiDooaodj3tYWducgVDjAU9lCS04= Date: Thu, 6 Apr 2023 10:59:37 +1000 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 09/14] nstool: Add nstool exec command to execute commands in an nstool namespace Message-ID: References: <20230404014638.3225556-1-david@gibson.dropbear.id.au> <20230404014638.3225556-10-david@gibson.dropbear.id.au> <20230405135854.15e988a7@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="n+eC+yOfMS581Wd+" Content-Disposition: inline In-Reply-To: <20230405135854.15e988a7@elisabeth> Message-ID-Hash: EVCANUOLCXKBNHUKOW2JFSZ7PJ2GTDFK X-Message-ID-Hash: EVCANUOLCXKBNHUKOW2JFSZ7PJ2GTDFK 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: 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: --n+eC+yOfMS581Wd+ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Apr 05, 2023 at 01:58:54PM +0200, Stefano Brivio wrote: > On Tue, 4 Apr 2023 11:46:33 +1000 > David Gibson wrote: >=20 > > This combines nstool info -pw with nsenter with various options = for > > a more convenient and less verbose of entering existing nstool managed > > namespaces. > >=20 > > Signed-off-by: David Gibson > > --- > > test/nstool.c | 139 +++++++++++++++++++++++++++++++++++++++++++++++++- > > 1 file changed, 137 insertions(+), 2 deletions(-) > >=20 > > 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 hol= d\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 wai= t, > > struct holder_info *info, > > struct ucred *peercred) > > { > > - int fd =3D socket(AF_UNIX, SOCK_STREAM, PF_UNIX); > > + int fd =3D socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, PF_UNIX); > > struct sockaddr_un addr =3D { > > .sun_family =3D AF_UNIX, > > }; > > @@ -132,7 +137,7 @@ static int connect_ctl(const char *sockpath, bool w= ait, > > =20 > > static void cmd_hold(int argc, char *argv[]) > > { > > - int fd =3D socket(AF_UNIX, SOCK_STREAM, PF_UNIX); > > + int fd =3D socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, PF_UNIX); > > struct sockaddr_un addr =3D { > > .sun_family =3D AF_UNIX, > > }; > > @@ -301,6 +306,134 @@ static void cmd_info(int argc, char *argv[]) > > } > > } > > =20 > > +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) >=3D PATH_MAX) > > + die("Truncated path \"%s\"\n", nspath); > > + va_end(ap); > > + > > + fd =3D 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 =3D waitpid(pid, &status, WUNTRACED); > > + if (rc < 0) > > + die("waitpid() on %d: %s\n", pid, strerror(errno)); > > + if (rc !=3D 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)); >=20 > An alternative could be what pasta_child_handler() does on > WIFSIGNALED(status) -- I don't actually have a preference between the > two. Right. The idiom above I copied from unshare and nsenter in util-linux. Seems marginally more thorough than the 128+ trick. > > + > > + die("Unexpected status for child %d\n", pid); >=20 > Weird indentation in this function. Ugh, yes. I think I did a copy paste at some point that introduced a bunch of spaces where there should be tabs. Fixing. --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --n+eC+yOfMS581Wd+ Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEO+dNsU4E3yXUXRK2zQJF27ox2GcFAmQuGXMACgkQzQJF27ox 2Gen2Q//SApxF+t49I86jLnicVGO7IIv29CcdVDDnbA3UElchQfG4YsqQ7HIbuDk mCqzA1Dc+TR4nGOaSXfX65O/Fd3zWvcNKO37ByqnvdHwKfvJ5vXn/kxasDW8jjIp 1sNKLwWVwf2To287dCDddCZny0xejNgE7TqNK0G7MCc/yL3alA3AHoN2bsI6Cmeo SHGr3w6DPb1GRYLNi63iofrlAZ2Kb+7sMB2wUCXl0Duir0B0ViJZJS073Uansy5D fUBEW6hCtYdes3HpSKutCe/1JCyWyeOVYKHrjMj3qT6o71Qgm/MO7ggbVKrgXPbH XsO+b2Bw66lNJrV4tRXAFJk0Z87iqxbthApTQyRiS3r+sGpykwYHo98tBt2NovQH R2yFIK50rPF71m13gz8xdEuirjvt4rn1ZNrvRzZ2pDdJmwm2k18qnXD6EG2tNuZw ppMu+uUK3LoKUalXTsOdqXi7aq2UKfycN4veW1t/+TxHQNPTLpQ3Z15usCfpy+Xf zyMIa5iFnDkLz06l98gIhrMHmbPjxhOLjDGv+5iKJdshpdSm75eMr2YFhpw4AXH/ oXaihL90mtpop2vN2UjzEFgHnlrKIJM0mOvd5t83giXOPnPuScyKULJ9lMeQQs0/ lvos2lwK0+xhU20qrkVU6FpF2IH1IuW8EsfYMKAGPIah2hy24qY= =Xzz8 -----END PGP SIGNATURE----- --n+eC+yOfMS581Wd+--