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 2C4425A005E for ; Fri, 14 Oct 2022 01:42:38 +0200 (CEST) Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4MpQzr0N5yz4xGG; Fri, 14 Oct 2022 10:42:32 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1665704552; bh=w4DOIEP+mjtQeuqLnQAHlvm7yFcJ2O+N3q8tQHPWHXw=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Pv2KVf/f4gpB8VnN4F8ibB17/OvWOkXSg1nw72gWDWiuvLLTrxf63lVlbbt52dQaw 8m1w9G/UgN7OPc3R/5hPFjvkK8f/8eEZ+wNKrj+jA4y/DNJk07SwYnG0bWkHroVpHq wjLzFrfE6m/GdVVznxytFqRWADQiDo2G6C19WjPc= Date: Fri, 14 Oct 2022 10:24:43 +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> <20221013114818.247a7cc2@elisabeth> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="aD16pHE5mBKDrPJe" Content-Disposition: inline In-Reply-To: <20221013114818.247a7cc2@elisabeth> Message-ID-Hash: 77DDHIIQEZYHKPVY5DSFSHOY67AWOF7E X-Message-ID-Hash: 77DDHIIQEZYHKPVY5DSFSHOY67AWOF7E 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: --aD16pHE5mBKDrPJe Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Thu, Oct 13, 2022 at 11:48:18AM +0200, Stefano Brivio wrote: > On Thu, 13 Oct 2022 19:22:27 +1100 > David Gibson wrote: >=20 > > 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 s= hell. > > > > This is not ideal, since this is a bash specific option and require= s pasta > > > > 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 n= eed bash > > > > specific logic. =20 > > >=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 = *netns) > > > > =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 > > >=20 > > > At this point the assignment could be split onto another line. =20 > >=20 > > Uh.. I'm not sure what you mean by that. >=20 > const struct pasta_setup_ns_arg *a; >=20 > a =3D (const struct pasta_setup_ns_arg *)arg; Ah, right, that makes sense. Adjusted. > > > > 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 > > >=20 > > > If you respin, it would be nice to have the usual ordering here > > > (sh_argv[] after ns_fn_stack). =20 > >=20 > > Done. > >=20 > > > > + 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 > > >=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. =20 > >=20 > > Uh.. not sure what you mean by that either. >=20 > Ah, sorry. >=20 > if ((len =3D strlen(arg.exe)) + 1 /* - */ + 1 /* \0 */ > sizeof(sh_arg0)) > err("$SHELL is too long (%u bytes)", strlen(arg.exe)); >=20 > (void)snprintf(sh_arg0, sizeof(sh_arg0), "-%s", arg.exe); >=20 > I don't know, it doesn't look so... cramped. Maybe it's just me. If it > doesn't make sense just disregard ;) I see what you mean about the crampedness. But the proposed alternative seems more complicated in other ways to me. I think I'll leave it as is for now. --=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 --aD16pHE5mBKDrPJe Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEoULxWu4/Ws0dB+XtgypY4gEwYSIFAmNInf0ACgkQgypY4gEw YSIXWA/+P8G4m7+wS5/kLccWgunm5oyMLwuDWckLm2PXdyTzMqaJdqFoR+xMrpWi 1BOGSf/vemFklNqHRmOv2cfjUTMCiLK17/kwmx0AwczOlK2WyTVW3uT6CMe0SG+z +ZP37sMWDXNKkgXn21uuhaLbfl6Kjrg559+1wi/PXXiUIF+QJKxjhbq2kVNHNm5w r4odLT4+bPhy+dERJC906q/20OuS7y1HOYO35pNDDBZ3r/AqUFAZWTxbIPycSvk6 racr5GiVjF1ZuO8wV0euIR+zVQxfrL1YzZeuBP5RdeulDJ/7sQcRy3Hx6F0kbol1 hRbb94SoTKNTYl7N1tXt2qfATOp37aqswkh975Bei0FBmqGk9KjPxSzTN8BejmEP 1HfY2pPAGz2vuPFBePJ9H3WKJLfUmWdJTmYNkrY450JK21GL3U6C2LR6vhyt+op+ Wy0J0f12QXp3co+1zSg9MRbI1dS9XoOhyKI0rKuggqcCPqgYqPCrvz/Cb9d6prwT 12PuuzzpnVoHlbXseZnetzpi0GjLy8OvXPQMIG3tGpk6kr5eKgha+8eABk42HPnh sb3HJ6NM6M1PCKQwfSG+ziTpmySv3mIvgBgUqK+WUr9BPhx4EqymEXSX+0V2NVo0 mqUU4NtHf5VqchP2XGIyTDZNRSpNBDt0JgKueST4oxgh9koFM/4= =YpYR -----END PGP SIGNATURE----- --aD16pHE5mBKDrPJe--