From nobody Mon Feb 9 01:22:38 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=1562138497; cv=none; d=zoho.com; s=zohoarc; b=Gm0R8j5uduoU+uyEn3nGyGC66FmI+SgiW1mpEiw/2PdxihlwQjOGcTBAhbWMuEdyszDIOHD0M06E4UbKgvcwLqMEV5rTnSySJ1dmRXcvkfIvtTEdQMu6y/68xZ/7/EDfdb8HyOXAHCjcebW9fz8bVHw9Y/5tqsXvBLqtos7KhzQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562138497; 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=Dfh3/hsmw6vevqpYkCLPVpsRk7VG99Fr+/slrYv/JII=; b=beUj+lI9hQrX6HSEo3PNwmjMrRSK5YqdBXgtBdMkCa1VkMoVGzihx6ZIPH+b3Abd7LL8Gch2AMRSoZ4RKHEhsJnl/A0NK3pFBOP0qiUv8gQBvQii5WaPPPk2DgNcfoDzPV/sEesLdAnYJEwdrnAq/o+M7Ly/TrIRKMSxRj1G/TQ= 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 1562138497511151.7144879915562; Wed, 3 Jul 2019 00:21:37 -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 D5790859FE; Wed, 3 Jul 2019 07:21:35 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A7B8298AC; Wed, 3 Jul 2019 07:21:35 +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 4B02D18184A5; Wed, 3 Jul 2019 07:21:35 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x637JZMC019963 for ; Wed, 3 Jul 2019 03:19:35 -0400 Received: by smtp.corp.redhat.com (Postfix) id A85F47840D; Wed, 3 Jul 2019 07:19:35 +0000 (UTC) Received: from localhost.localdomain (ovpn-204-217.brq.redhat.com [10.40.204.217]) by smtp.corp.redhat.com (Postfix) with ESMTP id AEF2718EC7 for ; Wed, 3 Jul 2019 07:19:34 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Wed, 3 Jul 2019 09:19:19 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 2/3] 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-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.26]); Wed, 03 Jul 2019 07:21:36 +0000 (UTC) Content-Type: text/plain; charset="utf-8" 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 8695c98d1b..faee36c449 100644 --- a/src/util/vircommand.c +++ b/src/util/vircommand.c @@ -491,6 +491,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 @@ -500,13 +531,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; @@ -612,23 +642,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