From nobody Tue Feb 10 12:40:14 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) client-ip=207.211.31.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1597832244; cv=none; d=zohomail.com; s=zohoarc; b=WeqW4P22nJ38zOXMWsHlKmG2q8REFY5sebOmXRp/z9022ZuLr1An3Qh1qEIjAI035t9w/L+VzTEh3yLNs7jmzQ0xdksJsFBYMYkb9cpOQpwuc7sTOysZIlpA6hOSUY7waI/fZavP989Wsc6e01c9EPDQDGsR3Rc2K9uUob8j40I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1597832244; h=Content-Type:Content-Transfer-Encoding:Cc: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=YO6eT10WjK82nJVkMuzMRfkt2KEyahN0FKHPHIkV8g4=; b=J6LETvgLBqoXhQrpnwGZZ3t8i6dxBdMcH2h0gyd3bEq+3ulboYS7jC1GqxofT86IcovYcraiBzLaw4LIcyEzCZgc5ja+DpVcZB+AYSMEMXvskMBdnDuzLU/ggUQFfea38wyh+OB0F+1G4jLUr8b2XqGhqPQb8aXFFaUJ15rq+Ak= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by mx.zohomail.com with SMTPS id 1597832244786744.4673667072849; Wed, 19 Aug 2020 03:17:24 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-19-t_bf49zUNjOnZG7y1H813w-1; Wed, 19 Aug 2020 06:17:21 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id F22EF57052; Wed, 19 Aug 2020 10:17:14 +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 D3DDF600DD; Wed, 19 Aug 2020 10:17:14 +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 98FA34EE1D; Wed, 19 Aug 2020 10:17:14 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 07JAARgI026770 for ; Wed, 19 Aug 2020 06:10:27 -0400 Received: by smtp.corp.redhat.com (Postfix) id 9E853A9E7B; Wed, 19 Aug 2020 10:10:27 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast01.extmail.prod.ext.rdu2.redhat.com [10.11.55.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9867AB5523 for ; Wed, 19 Aug 2020 10:10:24 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C114C984BAE for ; Wed, 19 Aug 2020 10:10:24 +0000 (UTC) Received: from mx1.tetrasec.net (mx1.tetrasec.net [66.245.176.36]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-495-mjpo9sITOJG6PX8Y2BlkfA-1; Wed, 19 Aug 2020 06:10:17 -0400 Received: from mx1.tetrasec.net (mail.local [127.0.0.1]) by mx1.tetrasec.net (Postfix) with ESMTP id 5D04FF2CDA; Wed, 19 Aug 2020 10:04:02 +0000 (UTC) Received: from ncopa-desktop.lan (67.63.200.37.customer.cdi.no [37.200.63.67]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: n@tanael.org) by mx1.tetrasec.net (Postfix) with ESMTPSA id 90829F2CD8; Wed, 19 Aug 2020 10:04:01 +0000 (UTC) X-MC-Unique: t_bf49zUNjOnZG7y1H813w-1 X-MC-Unique: mjpo9sITOJG6PX8Y2BlkfA-1 From: Natanael Copa To: libvir-list@redhat.com Subject: [PATCH 2/2] util: command: improve generic mass close of fds Date: Wed, 19 Aug 2020 12:03:41 +0200 Message-Id: <20200819100341.31374-3-ncopa@alpinelinux.org> In-Reply-To: <20200819100341.31374-1-ncopa@alpinelinux.org> References: <20200819100341.31374-1-ncopa@alpinelinux.org> MIME-Version: 1.0 X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false; X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-MIME-Autoconverted: from quoted-printable to 8bit by lists01.pubmisc.prod.ext.phx2.redhat.com id 07JAARgI026770 X-loop: libvir-list@redhat.com X-Mailman-Approved-At: Wed, 19 Aug 2020 06:17:13 -0400 Cc: Natanael Copa 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0.002 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a portable generic implementation of virMassClose as fallback on non-FreeBSD and non-glibc. This implementation uses poll(2) to look for open files to keep performance reasonable while not using any mallocs. This solves a deadlock with musl libc. Signed-off-by: Natanael Copa --- src/util/vircommand.c | 76 +++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/src/util/vircommand.c b/src/util/vircommand.c index 17e5bb00d3..06579cfb44 100644 --- a/src/util/vircommand.c +++ b/src/util/vircommand.c @@ -443,7 +443,7 @@ virExecCommon(virCommandPtr cmd, gid_t *groups, int ngr= oups) return 0; } =20 -# ifdef __linux__ +# if defined(__linux__) && defined(__GLIBC__) /* 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 @@ -482,17 +482,7 @@ virCommandMassCloseGetFDsLinux(virCommandPtr cmd G_GNU= C_UNUSED, VIR_DIR_CLOSE(dp); return ret; } - -# else /* !__linux__ */ - -static int -virCommandMassCloseGetFDsGeneric(virCommandPtr cmd G_GNUC_UNUSED, - virBitmapPtr fds) -{ - virBitmapSetAll(fds); - return 0; -} -# endif /* !__linux__ */ +# endif /* __linux__ && __GLIBC__ */ =20 # ifdef __FreeBSD__ =20 @@ -546,7 +536,7 @@ virCommandMassClose(virCommandPtr cmd, return 0; } =20 -# else /* ! __FreeBSD__ */ +# elif defined(__GLIBC_) /* ! __FreeBSD__ */ =20 static int virCommandMassClose(virCommandPtr cmd, @@ -574,13 +564,8 @@ virCommandMassClose(virCommandPtr cmd, if (!(fds =3D virBitmapNew(openmax))) return -1; =20 -# ifdef __linux__ if (virCommandMassCloseGetFDsLinux(cmd, fds) < 0) return -1; -# else - if (virCommandMassCloseGetFDsGeneric(cmd, fds) < 0) - return -1; -# endif =20 fd =3D virBitmapNextSetBit(fds, 2); for (; fd >=3D 0; fd =3D virBitmapNextSetBit(fds, fd)) { @@ -598,6 +583,61 @@ virCommandMassClose(virCommandPtr cmd, return 0; } =20 +#else /* ! __FreeBSD__ && ! __GLIBC__ */ +static int +virCommandMassClose(virCommandPtr cmd, + int childin, + int childout, + int childerr) +{ + static struct pollfd pfds[1024]; + int fd =3D 0; + int i, total; + int max_fd =3D sysconf(_SC_OPEN_MAX); + + if (max_fd < 0) { + virReportSystemError(errno, "%s", _("sysconf(_SC_OPEN_MAX) failed"= )); + return -1; + } + + total =3D max_fd - fd; + for (i =3D 0; i < (total < 1024 ? total : 1024); i++) + pfds[i].events =3D 0; + + while (fd < max_fd) { + int nfds, r =3D 0; + + total =3D max_fd - fd; + nfds =3D total < 1024 ? total : 1024; + + for (i =3D 0; i < nfds; i++) + pfds[i].fd =3D fd + i; + + do { + r =3D poll(pfds, nfds, 0); + } while (r =3D=3D -1 && errno =3D=3D EINTR); + + if (r < 0) { + virReportSystemError(errno, "%s", _("poll() failed")); + return -1; + } + + for (i =3D 0; i < nfds; i++) + if (pfds[i].revents !=3D POLLNVAL) { + if (pfds[i].fd =3D=3D childin || pfds[i].fd =3D=3D childou= t || pfds[i].fd =3D=3D childerr) + continue; + if (!virCommandFDIsSet(cmd, pfds[i].fd)) { + VIR_MASS_CLOSE(pfds[i].fd); + } else if (virSetInherit(pfds[i].fd, true) < 0) { + virReportSystemError(errno, _("failed to preserve fd %= d"), pfds[i].fd); + return -1; + } + } + fd +=3D nfds; + } + return 0; +} + # endif /* ! __FreeBSD__ */ =20 /* --=20 2.28.0