From nobody Sat Nov 23 18:02:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1724847443189279.27277197611727; Wed, 28 Aug 2024 05:17:23 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 010AD11E3; Wed, 28 Aug 2024 08:17:21 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id DFE9013B5; Wed, 28 Aug 2024 08:16:30 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id A1556A6C; Wed, 28 Aug 2024 08:16:25 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id 3283EA6A for ; Wed, 28 Aug 2024 08:16:25 -0400 (EDT) Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-206-QXTju2IDPJyAb0YHX2ArJw-1; Wed, 28 Aug 2024 08:16:23 -0400 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D32911955BFA for ; Wed, 28 Aug 2024 12:16:22 +0000 (UTC) Received: from maggie.brq.redhat.com (unknown [10.43.3.102]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 2C9E519560A3 for ; Wed, 28 Aug 2024 12:16:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724847384; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AZGnITsFUIgbhggQe103twdCQhjObhcqwVeQwH0WipQ=; b=CCY0tm2l+7Y3I5i9SSgS0Y2qGZ9/9jhbJ2+RjxLssnGRci4ro3F5RAXOTf4u+SSY6wmIQC SMB7JtqAjjfnkaTYnNVxtBi0+7EyhDfv7pnAldjRo3EOOaONz7LaJmiOrwpUtOy5eM6bU9 33SxL4sFtHdaGmMsRT3QUMZDUxE3tdE= X-MC-Unique: QXTju2IDPJyAb0YHX2ArJw-1 From: Michal Privoznik To: devel@lists.libvirt.org Subject: [PATCH 2/4] vircommand: Isolate FD dir parsing into a separate function Date: Wed, 28 Aug 2024 14:16:15 +0200 Message-ID: <39dcf435634062deedef8984e6934f775319ffe8.1724847331.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: VXKUGSC67NG5VX5AUSCHQLRYC7MCFNVN X-Message-ID-Hash: VXKUGSC67NG5VX5AUSCHQLRYC7MCFNVN X-MailFrom: mprivozn@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1724847445093116600 Content-Type: text/plain; charset="utf-8"; x-default="true" So far, virCommandMassCloseGetFDsLinux() opens "/proc/self/fd", iterates over it marking opened FDs in @fds bitmap. Well, we can do the same on other systems (with altered path), like MacOS or FreeBSD. Therefore, isolate dir iteration into a separate function that accepts dir path as an argument. Unfortunately, this function might be unused on some systems (e.g. mingw), therefore mark it as such. Signed-off-by: Michal Privoznik Reviewed-by: Martin Kletzander --- src/util/vircommand.c | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/util/vircommand.c b/src/util/vircommand.c index 8bd5e5e771..c0aab85c53 100644 --- a/src/util/vircommand.c +++ b/src/util/vircommand.c @@ -472,17 +472,12 @@ virExecCommon(virCommand *cmd, gid_t *groups, int ngr= oups) return 0; } =20 -# ifdef __linux__ -/* On Linux, we can utilize procfs and read the table of opened - * FDs and selectively close only those FDs we don't want to pass - * onto child process (well, the one we will exec soon since this - * is called from the child). */ -static int -virCommandMassCloseGetFDsLinux(virBitmap *fds) +static int G_GNUC_UNUSED +virCommandMassCloseGetFDsDir(virBitmap *fds, + const char *dirName) { g_autoptr(DIR) dp =3D NULL; struct dirent *entry; - const char *dirName =3D "/proc/self/fd"; int rc; =20 if (virDirOpen(&dp, dirName) < 0) @@ -507,15 +502,20 @@ virCommandMassCloseGetFDsLinux(virBitmap *fds) return 0; } =20 -# else /* !__linux__ */ - static int -virCommandMassCloseGetFDsGeneric(virBitmap *fds) +virCommandMassCloseGetFDs(virBitmap *fds) { +# ifdef __linux__ + /* On Linux, we can utilize procfs and read the table of opened + * FDs and selectively close only those FDs we don't want to pass + * onto child process (well, the one we will exec soon since this + * is called from the child). */ + return virCommandMassCloseGetFDsDir(fds, "/proc/self/fd"); +# else virBitmapSetAll(fds); return 0; +# endif } -# endif /* !__linux__ */ =20 static int virCommandMassCloseFrom(virCommand *cmd, @@ -544,13 +544,8 @@ virCommandMassCloseFrom(virCommand *cmd, =20 fds =3D virBitmapNew(openmax); =20 -# ifdef __linux__ - if (virCommandMassCloseGetFDsLinux(fds) < 0) + if (virCommandMassCloseGetFDs(fds) < 0) return -1; -# else - if (virCommandMassCloseGetFDsGeneric(fds) < 0) - return -1; -# endif =20 lastfd =3D MAX(lastfd, childin); lastfd =3D MAX(lastfd, childout); --=20 2.44.2