From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id C94EA5A0267 for ; Thu, 13 Oct 2022 11:37:25 +0200 (CEST) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Mp4Dg128Yz4xGx; Thu, 13 Oct 2022 20:37:23 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1665653843; bh=9S2Rp2vZjiRfe+cpC8//mC5ZrbnnkwoMwCPK422LSKA=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Yz7Il7c0Pnb7j0vzM5oA4osfhreo/2SO87PnaWlh5pApLNrnFvJKssvO54M7IRbrU 03MTio9we2x+HUCmflX7P3/Pk5TXV08R5+GcNAUtrqqDj24dIjOHvYBar4EvAVF2xP Ttt1eJxwmWByG/llMU14qQ+jXkgzvRJwonZYmzKQ= Date: Thu, 13 Oct 2022 19:22:27 +1100 From: David Gibson To: Stefano Brivio Subject: Re: [PATCH 02/10] pasta: More general way of starting spawned shell as a login shell Message-ID: References: <20221011054018.1449506-1-david@gibson.dropbear.id.au> <20221011054018.1449506-3-david@gibson.dropbear.id.au> <20221013041659.02ab4ec7@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="HUcF/0UqdRVTKTcF" Content-Disposition: inline In-Reply-To: <20221013041659.02ab4ec7@elisabeth> Message-ID-Hash: UONTSZY2LDS3AA6CDYIZKMXZ7C7WJBRB X-Message-ID-Hash: UONTSZY2LDS3AA6CDYIZKMXZ7C7WJBRB 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.3 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: --HUcF/0UqdRVTKTcF Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Oct 13, 2022 at 04:16:59AM +0200, Stefano Brivio wrote: > Just nits here: >=20 > On Tue, 11 Oct 2022 16:40:10 +1100 > David Gibson wrote: >=20 > > When invoked so as to spawn a shell, pasta checks explicitly for the > > shell being bash and if so, adds a "-l" option to make it a login shell. > > This is not ideal, since this is a bash specific option and requires pa= sta > > to know about specific shell variants. > >=20 > > There's a general convention for starting a login shell, which is to > > prepend a "-" to argv[0]. Use this approach instead, so we don't need = bash > > specific logic. >=20 > Hah, I didn't know that was the meaning. >=20 > > Signed-off-by: David Gibson > > --- > > pasta.c | 32 ++++++++++++++++++++------------ > > 1 file changed, 20 insertions(+), 12 deletions(-) > >=20 > > diff --git a/pasta.c b/pasta.c > > index 1dd8267..7c3acef 100644 > > --- a/pasta.c > > +++ b/pasta.c > > @@ -148,10 +148,12 @@ void pasta_open_ns(struct ctx *c, const char *net= ns) > > =20 > > /** > > * struct pasta_setup_ns_arg - Argument for pasta_setup_ns() > > + * @exe: Executable to run > > * @argv: Command and arguments to run > > */ > > struct pasta_setup_ns_arg { > > - char **argv; > > + const char *exe; > > + char *const *argv; > > }; > > =20 > > /** > > @@ -162,12 +164,13 @@ struct pasta_setup_ns_arg { > > */ > > static int pasta_setup_ns(void *arg) > > { > > - struct pasta_setup_ns_arg *a =3D (struct pasta_setup_ns_arg *)arg; > > + const struct pasta_setup_ns_arg *a > > + =3D (const struct pasta_setup_ns_arg *)arg; >=20 > At this point the assignment could be split onto another line. Uh.. I'm not sure what you mean by that. > > =20 > > FWRITE("/proc/sys/net/ipv4/ping_group_range", "0 0", > > "Cannot set ping_group_range, ICMP requests might fail"); > > =20 > > - execvp(a->argv[0], a->argv); > > + execvp(a->exe, a->argv); > > =20 > > perror("execvp"); > > exit(EXIT_FAILURE); > > @@ -182,26 +185,31 @@ static int pasta_setup_ns(void *arg) > > void pasta_start_ns(struct ctx *c, int argc, char *argv[]) > > { > > struct pasta_setup_ns_arg arg =3D { > > + .exe =3D argv[0], > > .argv =3D argv, > > }; > > - char *shell =3D getenv("SHELL"); > > - char *sh_argv[] =3D { shell, NULL }; > > - char *bash_argv[] =3D { shell, "-l", NULL }; > > + char *sh_argv[] =3D { NULL, NULL }; > > char ns_fn_stack[NS_FN_STACK_SIZE]; >=20 > If you respin, it would be nice to have the usual ordering here > (sh_argv[] after ns_fn_stack). Done. > > + char sh_arg0[PATH_MAX + 1]; > > =20 > > c->foreground =3D 1; > > if (!c->debug) > > c->quiet =3D 1; > > =20 > > - if (!shell) > > - shell =3D "/bin/sh"; > > =20 > > if (argc =3D=3D 0) { > > - if (strstr(shell, "/bash")) { > > - arg.argv =3D bash_argv; > > - } else { > > - arg.argv =3D sh_argv; > > + arg.exe =3D getenv("SHELL"); > > + if (!arg.exe) > > + arg.exe =3D "/bin/sh"; > > + > > + if ((size_t)snprintf(sh_arg0, sizeof(sh_arg0), > > + "-%s", arg.exe) >=3D sizeof(sh_arg0)) { >=20 > This is completely specified and looks safe, but it also looks more > complicated than it actually is, at a glance. >=20 > Maybe a separate length check before snprintf() would make it look > more natural. Not a strong preference though. Uh.. not sure what you mean by that either. > > + err("$SHELL is too long (%u bytes)", > > + strlen(arg.exe)); > > + exit(EXIT_FAILURE); > > } > > + sh_argv[0] =3D sh_arg0; > > + arg.argv =3D sh_argv; > > } > > =20 > > pasta_child_pid =3D clone(pasta_setup_ns, >=20 --=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 --HUcF/0UqdRVTKTcF Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEoULxWu4/Ws0dB+XtgypY4gEwYSIFAmNHypgACgkQgypY4gEw YSIU4w//dGTnWO84OcnKT2aNey7C/JM/E1kKKyzbkiYT0ZGYoB8Kx7TXHTu/p5X4 9bNEizDFflcOFByXw/V+nHkk73lMTwWluvRYCry+A72H9g6BFZ1Hqc7Flm/YvS+M qalxwTcOCnBk80uVBNGVmDHIQdRPDt6hMSy+61//30pmESlUcmMCcUm4AXMKq6e5 oN/kGFwi1RB3F4cYfQQv0la2FGhI41bMwtyEYAG90YiVu8mKC+KrNRqird1BQCx/ p5S0rZQdkNhLR7VgMX+PzsEVykgmiJQMB3+8P+HyZTSKTei9MO4cSxJwIsCd9IUW 5XOvwvOqAyHA8Mb3PGlEUUCcSkPZBN0t/dKDBwnfLjL/aZylJ5qMPCjdZ4rjAkVu 7OxHmh36lsTvctAz2Tj9i8/9PkCxJhCyN92XB5oCrWbKFXbwFLJBKsbH5Tr1lKT8 8rkjjR+3oVTacKg7T02YCY/rOWt+C8GfE8RdaxPI4lqX6Q1mtd50/StfZU7up8k+ nZaZBlFxOPXjnZjYcjaClPL8K4m0MwX2e+89+Ux+ytNZpR2h7KnoZESj5tvLh5ml 0ct0A/A6Yh5Inumo5vXKrnh5Q6Dg5X1WNH38LtChlqyeAPIpfY8orA2R7IipVynw djtu296k9ZMQ35X9/bJ7V9U7bimlhAPlJspmYxJTqd1xu+r37PY= =xIG0 -----END PGP SIGNATURE----- --HUcF/0UqdRVTKTcF--