From nobody Sun Feb 8 23:06:15 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 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=1692711319; cv=none; d=zohomail.com; s=zohoarc; b=a1/qTPlCSlU5Vu8eE1uUDrfGLPBzs6dl/tKcJc5ZlpHiuPmYbAO2wbkkBI8CREqkIzLCGbWLPDYhJDNjvR1LLGpZHVAoglUMHli//na4Av9eXAOujX7YHf0RHndMLqhCVDAyaOWlalgWoSybwGTQ7n2lRxHxcj7zxHbJtw/iF+Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692711319; 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; bh=xKKhNCNgsqE2VNL2RaHFpNwq+5+89ppEByLZOB/JuZU=; b=dDNP9CALVEfWIwTEwSIu9o2UbRNU5OR5FiqUVyCeFhmcXQ6/jx4ZrnJY2diTEFnWyp30PFeMS+TX3myS54cMTuGMDtTnIZ+HjxaEA+Y8GUY5bu/aibnXYt4ZClv6p8LUjY0GiIZFC7SJfLBv3cxaZNI2/Ikbn38Yf+edcNX5xAU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1692711319902985.8253944980926; Tue, 22 Aug 2023 06:35:19 -0700 (PDT) Received: from mimecast-mx02.redhat.com (66.187.233.73 [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-41-y2-7YA8fOlyCg4slY9hhgw-1; Tue, 22 Aug 2023 09:35:11 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 675A63815F8D; Tue, 22 Aug 2023 13:34:52 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 533B9492C13; Tue, 22 Aug 2023 13:34:52 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 7371319465A4; Tue, 22 Aug 2023 13:34:46 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id A1ECB1946586 for ; Tue, 22 Aug 2023 13:34:45 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 9D1A42026D33; Tue, 22 Aug 2023 13:34:45 +0000 (UTC) Received: from localhost.localdomain (unknown [10.43.2.56]) by smtp.corp.redhat.com (Postfix) with ESMTP id 44C3E2026D2B for ; Tue, 22 Aug 2023 13:34:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692711318; h=from:from:sender:sender: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:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=xKKhNCNgsqE2VNL2RaHFpNwq+5+89ppEByLZOB/JuZU=; b=gfxQRFrz2SLp07IRtM83kKLe+TiYtk3RLH+ooLlV2sViCO88T47cX6m+u/urmJwypQa5Fs +OYQvGMSmUVTJpqNnO5XG+i58eB8z1zhRJT1rEzcxxPoHuJESidenbeEEi2aJV94M/s8Tc hMZ4k14lzt8GQymUBXcylPip9zpkTTE= X-MC-Unique: y2-7YA8fOlyCg4slY9hhgw-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 3/5] vircommand: Unify mass FD closing Date: Tue, 22 Aug 2023 15:34:39 +0200 Message-ID: <897e79b0557b62d3b609311e6163fa5487f2899c.1692710969.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1692711321885100003 Content-Type: text/plain; charset="utf-8"; x-default="true" We have two version of mass FD closing: one for FreeBSD (because it has closefrom()) and the other for everything else. But now that we have closefrom() wrapper even for Linux, we can unify these two. Signed-off-by: Michal Privoznik --- src/util/vircommand.c | 85 +++++++++++++------------------------------ 1 file changed, 25 insertions(+), 60 deletions(-) diff --git a/src/util/vircommand.c b/src/util/vircommand.c index 49abb53c28..867f45b57b 100644 --- a/src/util/vircommand.c +++ b/src/util/vircommand.c @@ -526,60 +526,6 @@ virCommandMassCloseGetFDsGeneric(virCommand *cmd G_GNU= C_UNUSED, } # endif /* !__linux__ */ =20 -# ifdef __FreeBSD__ - -static int -virCommandMassClose(virCommand *cmd, - int childin, - int childout, - int childerr) -{ - int lastfd =3D -1; - int fd =3D -1; - size_t i; - - /* - * Two phases of closing. - * - * The first (inefficient) phase iterates over FDs, - * preserving certain FDs we need to pass down, and - * closing others. The number of iterations is bounded - * to the number of the biggest FD we need to preserve. - * - * The second (speedy) phase uses closefrom() to cull - * all remaining FDs in the process. - * - * Usually the first phase will be fairly quick only - * processing a handful of low FD numbers, and thus using - * closefrom() is a massive win for high ulimit() NFILES - * values. - */ - lastfd =3D MAX(lastfd, childin); - lastfd =3D MAX(lastfd, childout); - lastfd =3D MAX(lastfd, childerr); - - for (i =3D 0; i < cmd->npassfd; i++) - lastfd =3D MAX(lastfd, cmd->passfd[i].fd); - - for (fd =3D 0; fd <=3D lastfd; fd++) { - if (fd =3D=3D childin || fd =3D=3D childout || fd =3D=3D childerr) - continue; - if (!virCommandFDIsSet(cmd, fd)) { - int tmpfd =3D fd; - VIR_MASS_CLOSE(tmpfd); - } else if (virSetInherit(fd, true) < 0) { - virReportSystemError(errno, _("failed to preserve fd %1$d"), f= d); - return -1; - } - } - - closefrom(lastfd + 1); - - return 0; -} - -# else /* ! __FreeBSD__ */ - static int virCommandMassClose(virCommand *cmd, int childin, @@ -588,7 +534,9 @@ virCommandMassClose(virCommand *cmd, { g_autoptr(virBitmap) fds =3D NULL; int openmax =3D sysconf(_SC_OPEN_MAX); + int lastfd =3D -1; int fd =3D -1; + size_t i; =20 /* In general, it is not safe to call malloc() between fork() and exec= () * because the child might have forked at the worst possible time, i.e. @@ -605,16 +553,23 @@ virCommandMassClose(virCommand *cmd, =20 fds =3D virBitmapNew(openmax); =20 -# ifdef __linux__ +# ifdef __linux__ if (virCommandMassCloseGetFDsLinux(cmd, fds) < 0) return -1; -# else +# else if (virCommandMassCloseGetFDsGeneric(cmd, fds) < 0) return -1; -# endif +# endif + + lastfd =3D MAX(lastfd, childin); + lastfd =3D MAX(lastfd, childout); + lastfd =3D MAX(lastfd, childerr); + + for (i =3D 0; i < cmd->npassfd; i++) + lastfd =3D MAX(lastfd, cmd->passfd[i].fd); =20 fd =3D virBitmapNextSetBit(fds, 2); - for (; fd >=3D 0; fd =3D virBitmapNextSetBit(fds, fd)) { + for (; fd >=3D 0 && fd <=3D lastfd; fd =3D virBitmapNextSetBit(fds, fd= )) { if (fd =3D=3D childin || fd =3D=3D childout || fd =3D=3D childerr) continue; if (!virCommandFDIsSet(cmd, fd)) { @@ -626,11 +581,21 @@ virCommandMassClose(virCommand *cmd, } } =20 + if (virCloseFrom(lastfd + 1) < 0) { + if (errno !=3D ENOSYS) + return -1; + + if (fd > 0) { + for (; fd >=3D 0; fd =3D virBitmapNextSetBit(fds, fd)) { + int tmpfd =3D fd; + VIR_MASS_CLOSE(tmpfd); + } + } + } + return 0; } =20 -# endif /* ! __FreeBSD__ */ - =20 /* * virExec: --=20 2.41.0