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 6E0905A0082 for ; Tue, 14 Feb 2023 11:08:19 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1676369298; 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=zyM/8Rzj3KmUoaSBwTG4VMlqTWHc7rK/9sipuNrpErA=; b=ZEwqaohmK3lDVWsOK1h0C+0O4Oma4Kbbtbk6bDadmCiZiLnxlss5ShyW/2JqbyuKY+Gwg0 kF0Blru8P955GYN8vSqyATOGtvxcRqDDwANrXxLPNF20H1e4QLlOEVMBEVcHBTBoNpQWFF 5Ll8Q1++mYUxLdhX/x8hRpKO8frqhHU= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-157-iE2RYIeKP0O32xJGmaigsA-1; Tue, 14 Feb 2023 05:08:17 -0500 X-MC-Unique: iE2RYIeKP0O32xJGmaigsA-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 CADFE1C041AA for ; Tue, 14 Feb 2023 10:08:16 +0000 (UTC) Received: from maya.cloud.tilaa.com (unknown [10.33.32.3]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6C90140CE2A7; Tue, 14 Feb 2023 10:08:16 +0000 (UTC) Date: Tue, 14 Feb 2023 11:08:13 +0100 From: Stefano Brivio To: Michal =?UTF-8?B?UHLDrXZvem7DrWs=?= Subject: Re: [libvirt PATCH] qemu: allow passt to self-daemonize Message-ID: <20230214110813.5a6a568c@elisabeth> In-Reply-To: References: <20230208231310.1728051-1-laine@redhat.com> 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=UTF-8 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: 5H6DNPSO7KRH7NII3SR3VRGHISD5N7V7 X-Message-ID-Hash: 5H6DNPSO7KRH7NII3SR3VRGHISD5N7V7 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: Laine Stump , libvir-list@redhat.com, =?UTF-8?B?SsOh?= =?UTF-8?B?bg==?= Tomko , 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: On Tue, 14 Feb 2023 09:01:39 +0100 Michal Pr=C3=ADvozn=C3=ADk wrote: > On 2/9/23 00:13, Laine Stump wrote: > > I initially had the passt process being started in an identical > > fashion to the slirp-helper - libvirt was daemonizing the new process > > and recording its pid in a pidfile. The problem with this is that, > > since it is daemonized immediately, any startup error in passt happens > > after the daemonization, and thus isn't seen by libvirt - libvirt > > believes that the process has started successfully and continues on > > its merry way. The result was that sometimes a guest would be started, > > but there would be no passt process for qemu to use for network > > traffic. > >=20 > > Instead, we should be starting passt in the same manner we start > > dnsmasq - we just exec it as normal (along with a request that passt > > create the pidfile, which is just another option on the passt > > commandline) and wait for the child process to exit; passt then has a > > chance to parse its commandline and complete all the setup prior to > > daemonizing itself; if it encounters an error and exits with a non-0 > > code, libvirt will see the code and know about the failure. We can > > then grab the output from stderr, log that so the "user" has some idea > > of what went wrong, and then fail the guest startup. > >=20 > > Signed-off-by: Laine Stump > > --- > > src/qemu/qemu_passt.c | 9 ++++----- > > 1 file changed, 4 insertions(+), 5 deletions(-) =20 >=20 >=20 > OOOPS, somehow I've accidentally merged this. Let me post follow up patch= es. >=20 > >=20 > > diff --git a/src/qemu/qemu_passt.c b/src/qemu/qemu_passt.c > > index 0f09bf3db8..f640a69c00 100644 > > --- a/src/qemu/qemu_passt.c > > +++ b/src/qemu/qemu_passt.c > > @@ -141,24 +141,23 @@ qemuPasstStart(virDomainObj *vm, > > g_autofree char *passtSocketName =3D qemuPasstCreateSocketPath(vm,= net); > > g_autoptr(virCommand) cmd =3D NULL; > > g_autofree char *pidfile =3D qemuPasstCreatePidFilename(vm, net); > > + g_autofree char *errbuf =3D NULL; > > char macaddr[VIR_MAC_STRING_BUFLEN]; > > size_t i; > > pid_t pid =3D (pid_t) -1; > > int exitstatus =3D 0; > > int cmdret =3D 0; > > - VIR_AUTOCLOSE errfd =3D -1; > > =20 > > cmd =3D virCommandNew(PASST); > > =20 > > virCommandClearCaps(cmd); > > - virCommandSetPidFile(cmd, pidfile); > > - virCommandSetErrorFD(cmd, &errfd); > > - virCommandDaemonize(cmd); > > + virCommandSetErrorBuffer(cmd, &errbuf); > > =20 > > virCommandAddArgList(cmd, > > "--one-off", =20 >=20 > BTW: we definitely need something better than this. IF, something goes > wrong after we've executed passt but before we execute QEMU, then passt > just hangs there. This is because passt clone()-s itself (i.e. creates a > child process), but QEMU that would connect to the socket never comes > around. Thus, the child process never sees the EOF on the socket and > just hangs in there thinking there will be somebody connecting, soon. Okay, I see the point now -- I thought libvirtd would start passt only once it knows for sure that the guest will connect to it. > I thought this could be solved by just killing the whole process group, > but the child process calls setsid(), which creates its own process > group. I've managed to work around this by passing --foreground, but I'm > unclear about the consequences. Though, it looks like it's still > dropping caps, creating its own namespaces, etc. So this may actually be > the way to go. I wouldn't recommend that: --foreground is really intended for interactive usage and we won't be able, for example, to spawn a child in a new PID namespace, which is a nice security feature, I think. I already suggested this to Laine offline: can libvirt just connect() to the socket and close() it, in case QEMU doesn't start? Then passt will terminate. It should be a few (~5) lines of code, instead of all the complexity potentially involved in tracking PIDs and avoiding related races, and design-wise it looks clean to me (libvirtd plays for a moment the QEMU role, because QEMU is not around). --=20 Stefano