From nobody Sun Feb 8 23:41:53 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.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.133.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=1687357105; cv=none; d=zohomail.com; s=zohoarc; b=DkCILFxKfiSVC2lR94P9OKbf2m9kP+NF0aZILab30yLUxekEaiak+XAKDy2zIuvd08s90zO2jJVzhq1dw24+6yt/n5Lh0UX/zWZOkjoJVpNVF1ya0PkDbqUaGLPK2y8EAiWEMn3qmlFBzMGzlr088wr7VE2ZI5pYVcx4LlIqdTo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1687357105; 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=fBMlRFdIzsKp5nqfsorvdF9fqrXHLMxsaMQtnmSZeYg=; b=P4Xf5ZjapHXg/3EhjlynTjbuq+Kq+6vzdBYDnkyStnYqhDiAwI9FVjpOwIiyETnPVmAr2RmXAIZ0LJQPWgpeAtFOb5trgnmmrDZe/tbhO0uVdUl6hbR0Q0f4seD4G7kHg+9dVH8hiNnUUhH1ugyf64HZQX75TnbtY8pB2aMszy8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.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.133.124]) by mx.zohomail.com with SMTPS id 168735710531318.131101950755692; Wed, 21 Jun 2023 07:18:25 -0700 (PDT) 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-472-wOvKj91nPHaRNuw5dq3Bww-1; Wed, 21 Jun 2023 10:18:08 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3B66A386B861; Wed, 21 Jun 2023 14:09:17 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 72B88112132E; Wed, 21 Jun 2023 14:09:16 +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 529B11946587; Wed, 21 Jun 2023 14:09:16 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id BD6141946586 for ; Wed, 21 Jun 2023 14:09:14 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id AF621C1ED97; Wed, 21 Jun 2023 14:09:14 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.224.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2A5D8C1ED96 for ; Wed, 21 Jun 2023 14:09:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687357104; 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=fBMlRFdIzsKp5nqfsorvdF9fqrXHLMxsaMQtnmSZeYg=; b=MJjyLPhwebpmYIKt8x3zTOEY2w3YMujgdS/nRq9iwxjl09a/VbcNn4KDsIh+O3j3WUr3hG 2eeROMGwRupxs+RQBXm/jmcSQASyQMHmBMoLUkwPJIvs+iFlB6iYEiPBvcKezTzNiU3/gJ DRY7im8k4GXXbQIGkVGlciu5XVqUps8= X-MC-Unique: wOvKj91nPHaRNuw5dq3Bww-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 1/3] vircommand: Use closefrom() more often Date: Wed, 21 Jun 2023 16:09:09 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 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.3 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: 1687357106712100001 Content-Type: text/plain; charset="utf-8"; x-default="true" As of commit v5.9-rc1~160^2~3 the Linux kernel has close_range() syscall, which closes not just one FD but whole range. Then, in its commit glibc-2.34~115 glibc introduced closefrom() which is just a wrapper over close_range(), but it allows us to use FreeBSD-only implementation on Linux too, as both OS-es now have the same function. Signed-off-by: Michal Privoznik --- meson.build | 1 + src/util/vircommand.c | 124 ++++++++++++++++++++++-------------------- 2 files changed, 66 insertions(+), 59 deletions(-) diff --git a/meson.build b/meson.build index aa391e7178..a4b52b6156 100644 --- a/meson.build +++ b/meson.build @@ -573,6 +573,7 @@ libvirt_export_dynamic =3D cc.first_supported_link_argu= ment([ # check availability of various common functions (non-fatal if missing) =20 functions =3D [ + 'closefrom', 'elf_aux_info', 'explicit_bzero', 'fallocate', diff --git a/src/util/vircommand.c b/src/util/vircommand.c index 49abb53c28..b8b8d48f92 100644 --- a/src/util/vircommand.c +++ b/src/util/vircommand.c @@ -479,7 +479,68 @@ virExecCommon(virCommand *cmd, gid_t *groups, int ngro= ups) return 0; } =20 -# ifdef __linux__ +# ifdef WITH_CLOSEFROM +# define USE_CLOSEFROM +# else +# define USE_GENERIC +# endif + + +# ifdef USE_CLOSEFROM +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; +} +# endif /* ! WITH_CLOSEFROM */ + + +# ifdef USE_GENERIC +# 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 @@ -515,7 +576,7 @@ virCommandMassCloseGetFDsLinux(virCommand *cmd G_GNUC_U= NUSED, return 0; } =20 -# else /* !__linux__ */ +# else /* !__linux__ */ =20 static int virCommandMassCloseGetFDsGeneric(virCommand *cmd G_GNUC_UNUSED, @@ -524,61 +585,7 @@ virCommandMassCloseGetFDsGeneric(virCommand *cmd G_GNU= C_UNUSED, virBitmapSetAll(fds); return 0; } -# endif /* !__linux__ */ - -# 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__ */ +# endif /* !__linux__ */ =20 static int virCommandMassClose(virCommand *cmd, @@ -628,8 +635,7 @@ virCommandMassClose(virCommand *cmd, =20 return 0; } - -# endif /* ! __FreeBSD__ */ +# endif /* ! USE_GENERIC */ =20 =20 /* --=20 2.39.3