From nobody Mon Feb 9 01:47:49 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1563267291; cv=none; d=zoho.com; s=zohoarc; b=HayTVgqWJZ0A8esLKqVMmmFsX3M2h1RTHjHPcupHgeVask3VVOwC4hv6PzwGtFsL2geQNHnjeCGTm5WmEX7dsvXUBkBMvxU5gHy6YhxTx3WPaWE/vbwdweqwmkvaINwAJWTgQTthaG3/vhTu6MYJNp8vyqfSNSvbGT2M6elqOF0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1563267291; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=QTsN1VSU2Xd6f49pNEdxH39Q1pvJtpGu38MnGewPlts=; b=jPp+R1wLX4KuwHaYT6Jv7xXCVoN966gVeqCCFRp/C59wKJMRD2G1G4bhf71GlMCVpO4S33s/CSkTpDhzHTLX2+Y96Z0fnOA2DHaFMdQi1hQ82vWpkZL7NKlwj2gE0zZW26uz6vQMcsaOhKqueAYLwCPRUvV0h0k4kV4d2JeK+nE= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1563267291849164.74449257002902; Tue, 16 Jul 2019 01:54:51 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 60087308FEC0; Tue, 16 Jul 2019 08:54:49 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 222CA5D71B; Tue, 16 Jul 2019 08:54:49 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 482784E58F; Tue, 16 Jul 2019 08:54:48 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x6G8sj7Q004082 for ; Tue, 16 Jul 2019 04:54:45 -0400 Received: by smtp.corp.redhat.com (Postfix) id 8C3F010A33D1; Tue, 16 Jul 2019 08:54:45 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id 12C4E108418D for ; Tue, 16 Jul 2019 08:54:44 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Tue, 16 Jul 2019 10:54:35 +0200 Message-Id: <7880f10a1805f0d98feb24ac6b4c7fb77ae9edf2.1563267063.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 3/4] vircommand: Separate mass FD closing into a function X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Tue, 16 Jul 2019 08:54:50 +0000 (UTC) I will optimize this code a bit in the next commit. But for that it is better if the code lives in a separate function. Signed-off-by: Michal Privoznik Reviewed-by: J=C3=A1n Tomko --- src/util/vircommand.c | 52 ++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/src/util/vircommand.c b/src/util/vircommand.c index c81ddfc0d0..6cd7cbe065 100644 --- a/src/util/vircommand.c +++ b/src/util/vircommand.c @@ -418,6 +418,37 @@ virExecCommon(virCommandPtr cmd, gid_t *groups, int ng= roups) return ret; } =20 +static int +virCommandMassClose(virCommandPtr cmd, + int childin, + int childout, + int childerr) +{ + int openmax =3D sysconf(_SC_OPEN_MAX); + int fd; + int tmpfd; + + if (openmax < 0) { + virReportSystemError(errno, "%s", + _("sysconf(_SC_OPEN_MAX) failed")); + return -1; + } + + for (fd =3D 3; fd < openmax; fd++) { + if (fd =3D=3D childin || fd =3D=3D childout || fd =3D=3D childerr) + continue; + if (!virCommandFDIsSet(cmd, fd)) { + tmpfd =3D fd; + VIR_MASS_CLOSE(tmpfd); + } else if (virSetInherit(fd, true) < 0) { + virReportSystemError(errno, _("failed to preserve fd %d"), fd); + return -1; + } + } + + return 0; +} + /* * virExec: * @cmd virCommandPtr containing all information about the program to @@ -427,13 +458,12 @@ static int virExec(virCommandPtr cmd) { pid_t pid; - int null =3D -1, fd, openmax; + int null =3D -1; int pipeout[2] =3D {-1, -1}; int pipeerr[2] =3D {-1, -1}; int childin =3D cmd->infd; int childout =3D -1; int childerr =3D -1; - int tmpfd; VIR_AUTOFREE(char *) binarystr =3D NULL; const char *binary =3D NULL; int ret; @@ -539,23 +569,9 @@ virExec(virCommandPtr cmd) if (cmd->mask) umask(cmd->mask); ret =3D EXIT_CANCELED; - openmax =3D sysconf(_SC_OPEN_MAX); - if (openmax < 0) { - virReportSystemError(errno, "%s", - _("sysconf(_SC_OPEN_MAX) failed")); + + if (virCommandMassClose(cmd, childin, childout, childerr) < 0) goto fork_error; - } - for (fd =3D 3; fd < openmax; fd++) { - if (fd =3D=3D childin || fd =3D=3D childout || fd =3D=3D childerr) - continue; - if (!virCommandFDIsSet(cmd, fd)) { - tmpfd =3D fd; - VIR_MASS_CLOSE(tmpfd); - } else if (virSetInherit(fd, true) < 0) { - virReportSystemError(errno, _("failed to preserve fd %d"), fd); - goto fork_error; - } - } =20 if (prepareStdFd(childin, STDIN_FILENO) < 0) { virReportSystemError(errno, --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list