From nobody Sat Feb 7 08:42:43 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 From nobody Sat Feb 7 08:42:43 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=1687357107; cv=none; d=zohomail.com; s=zohoarc; b=SaDx9u0xQucGxg5FAO5pa6sDYez0Ci5z9PmZP37lfWYOPMpFnInH47bUEeNpYXSvpkndZhaz+tPeWplAWRhkxNA3utVlsuauPe13Wo6Kttt6O9qAgcaMDTqD22Ur04BcybmqfqMDiSq748OvlvW0xgKr+pK6gL/sHm0VaKQjTgc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1687357107; 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=EkX3ek2NS9nrb4k0srd8cc023sLPF7PLAxGUWQOwOO4=; b=RW3Ju7MG+GeG+HWrXnynGF12UavV29Uumu2GLqmE16R2a3XgDHfRq58zOXOMoFJ/9/MPOVRTCKefNElvcQSUmKjuBovzImhlQy6hWlCMs8FUD3mii8uD3Mtagr5PU4x/NJD5j6s2l2vAh+9RfAhekR3MpPKjMj5EP7Ohgp5zaDg= 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 1687357107288756.1392883852377; Wed, 21 Jun 2023 07:18:27 -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-542-GLrGnK96PM2vRCWfvdbAbg-1; Wed, 21 Jun 2023 10:18:04 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 85C88386B87B; Wed, 21 Jun 2023 14:09:18 +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 73858C1ED96; Wed, 21 Jun 2023 14:09:18 +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 92B8A19465B2; 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 8921A1946586 for ; Wed, 21 Jun 2023 14:09:15 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 69C40C478C8; Wed, 21 Jun 2023 14:09:15 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.224.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id E9543C1ED96 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=1687357106; 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=EkX3ek2NS9nrb4k0srd8cc023sLPF7PLAxGUWQOwOO4=; b=bEp0G0ePwCJRpx/mTxODQhb2HulTmKe/lrdpxkHdiPuOVbLwa9EDr6Tj1Eaig3kqyxCekr o3rbR90bmJxtpFj38DMDCh3WTNIs7OHOptDvhCs4NLWAwHva/FJfhdJTmvMplPy6jfRG6q Q64slJ45IHC1KTI0/11vUxNmCCi1M9g= X-MC-Unique: GLrGnK96PM2vRCWfvdbAbg-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 2/3] vircommand: Utilize close_range() Date: Wed, 21 Jun 2023 16:09:10 +0200 Message-Id: <1eea47b8d8082113cba420ea84d8c3ed6fd937a1.1687356000.git.mprivozn@redhat.com> 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.8 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: 1687357107940100003 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. In glibc this is exposed by automatically generated wrapper of the same name. In musl, this is not exposed, yet, but we can call the syscall() directly. In either case, we have to deal with a situation, when the kernel we're running under does not have the syscall as glibc deliberately does not implement fallback. Signed-off-by: Michal Privoznik --- meson.build | 1 + src/util/vircommand.c | 117 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 117 insertions(+), 1 deletion(-) diff --git a/meson.build b/meson.build index a4b52b6156..ecfc1b6bdf 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 [ + 'close_range', 'closefrom', 'elf_aux_info', 'explicit_bzero', diff --git a/src/util/vircommand.c b/src/util/vircommand.c index b8b8d48f92..e826f5f348 100644 --- a/src/util/vircommand.c +++ b/src/util/vircommand.c @@ -479,13 +479,128 @@ virExecCommon(virCommand *cmd, gid_t *groups, int ng= roups) return 0; } =20 -# ifdef WITH_CLOSEFROM +# if defined(WITH_CLOSE_RANGE) || \ + (defined(WITH_SYS_SYSCALL_H) && defined(SYS_close_range)) +# define USE_CLOSE_RANGE +# elif defined(WITH_CLOSEFROM) # define USE_CLOSEFROM # else # define USE_GENERIC # endif =20 =20 +# ifdef USE_CLOSE_RANGE +static int +virCloseRange(unsigned int first, + unsigned int last, + unsigned int flags) +{ + + static virTristateBool has_close_range =3D VIR_TRISTATE_BOOL_ABSENT; + int fd; + int ret =3D -1; + + VIR_DEBUG("first=3D%u, last=3D%u, flags=3D0x%x, has_close_range=3D%d", + first, last, flags, has_close_range); + + if (has_close_range !=3D VIR_TRISTATE_BOOL_NO) { +# if WITH_CLOSE_RANGE + ret =3D close_range(first, last, flags); +# else + ret =3D syscall(SYS_close_range, first, last, flags); +# endif + } + + if (ret =3D=3D 0) { + if (has_close_range =3D=3D VIR_TRISTATE_BOOL_ABSENT) + has_close_range =3D VIR_TRISTATE_BOOL_YES; + return 0; + } + + if (errno =3D=3D ENOSYS) { + if (has_close_range =3D=3D VIR_TRISTATE_BOOL_ABSENT) { + VIR_DEBUG("Kernel does not support close_range, falling back t= o naive implementation"); + has_close_range =3D VIR_TRISTATE_BOOL_NO; + } + } else { + return ret; + } + + /* glibc does not implement fallback, we have to implement it ourselve= s. */ + if (flags !=3D 0) { + errno =3D EINVAL; + return -1; + } + + for (fd =3D first; fd <=3D last; fd++) { + int tmpfd =3D fd; + VIR_MASS_CLOSE(tmpfd); + } + + return 0; +} + + +static int +virCommandMassClose(virCommand *cmd, + int childin, + int childout, + int childerr) +{ + g_autoptr(virBitmap) fds =3D virBitmapNew(3); + ssize_t first; + ssize_t last; + int openmax =3D sysconf(_SC_OPEN_MAX); + size_t i; + + virBitmapSetBitExpand(fds, childin); + virBitmapSetBitExpand(fds, childout); + virBitmapSetBitExpand(fds, childerr); + + for (i =3D 0; i < cmd->npassfd; i++) { + int fd =3D cmd->passfd[i].fd; + + virBitmapSetBitExpand(fds, fd); + + if (virSetInherit(fd, true) < 0) { + virReportSystemError(errno, _("failed to preserve fd %1$d"), f= d); + return -1; + } + } + + first =3D 2; + while ((last =3D virBitmapNextSetBit(fds, first)) >=3D 0) { + if (first + 1 =3D=3D last) { + first =3D last; + continue; + } + + /* Preserve @first and @last and close everything in between. */ + if (virCloseRange(first + 1, last - 1, 0) < 0) { + virReportSystemError(errno, + _("Unable to mass close FDs (first=3D%1$z= d, last=3D%2$zd)"), + first + 1, last - 1); + return -1; + } + + first =3D last; + } + + if (openmax < 0) + openmax =3D INT_MAX; + + if (virCloseRange(first + 1, openmax, 0) < 0) { + virReportSystemError(errno, + _("Unable to mass close FDs (first=3D%1$zd, l= ast=3D%2$d"), + first + 1, openmax); + return -1; + } + + return 0; +} +# endif /* USE_CLOSE_RANGE */ + + # ifdef USE_CLOSEFROM static int virCommandMassClose(virCommand *cmd, --=20 2.39.3 From nobody Sat Feb 7 08:42:43 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=1687357197; cv=none; d=zohomail.com; s=zohoarc; b=ZjypiWlpP1HQ5FPchxuFvtTK/ZinHDEEprc4n+Vw9LCTlRl4qO3IQ0HLYo99Nzair3JFUgUKrE8t+awqB2iMVPFFWwRqX6oSmBoDCPVmUpWRpaaAMT+w4qOAB58YCvSBzAz/EIuduXGr34NzbXiR/iRdjfEbDU5YCJPq3MO/OL8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1687357197; 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=KN8lRPBnHS0RorqxHTudghyF2wFWa1wEQ0Bvgwbyj0g=; b=DZwlzs8xJUtkk6ib2K9UxYCBLSYApDK1zdJFXLSyB+3ahtE01Ndndyyec5KC8jdrBKE7uDh+ND4crK3sKrSUnX+D94DR+YFK/BGWaMHivZI664OqG1idk2EchRnSGe3SzIOHxfqNd7dAUQbYxBMf2+fayF9XUB4lo1D+FbJCUvw= 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 1687357197002760.7075128189117; Wed, 21 Jun 2023 07:19:57 -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-104-M5ulZTNHNzql8rsLpzg7nw-1; Wed, 21 Jun 2023 10:19:32 -0400 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 308621C319E5; Wed, 21 Jun 2023 14:09:20 +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 1D897492C13; Wed, 21 Jun 2023 14:09:20 +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 6B3931946A42; Wed, 21 Jun 2023 14:09:19 +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 58B8B194658C for ; Wed, 21 Jun 2023 14:09:16 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 201D3C1ED96; Wed, 21 Jun 2023 14:09:16 +0000 (UTC) Received: from localhost.localdomain (unknown [10.45.224.77]) by smtp.corp.redhat.com (Postfix) with ESMTP id B7868C1ED97 for ; Wed, 21 Jun 2023 14:09:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687357196; 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=KN8lRPBnHS0RorqxHTudghyF2wFWa1wEQ0Bvgwbyj0g=; b=GgBBlx3HlJwDYKAVqzLun8wYIBIG4PJkrdrKyKuE7q0/To6/2ZBcG0QsM81Tnw2AQTwaMz xN0Sn7G5bJaSG6N8JZJ8niMeth4Sv32iKRTMA0Msm2y9CQW1PX9FggVnyU8yUF0RfneIC1 c9Ya6OCiBTh0QXseHusB4f+odeCM4Og= X-MC-Unique: M5ulZTNHNzql8rsLpzg7nw-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Michal Privoznik To: libvir-list@redhat.com Subject: [PATCH 3/3] virGlobalInit: Make glib init its own global state Date: Wed, 21 Jun 2023 16:09:11 +0200 Message-Id: <0a91987d4d027f74756c7b1d2dba4d1024f20160.1687356000.git.mprivozn@redhat.com> 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.10 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: 1687357199011100001 Content-Type: text/plain; charset="utf-8"; x-default="true" This should not be needed, but here's what's happening: virStrToLong_*() family of functions was switched from strtol*() to g_ascii_strtol*() in order to handle corner cases on Windows (most notably parsing hex numbers with base=3D0) - see v9.4.0-61-g2ed41d7cd9. But what we did not realize back then, is the fact that g_ascii_strtol*() family has their own global lock rendering virStrToLong_*() function unsafe between fork() + exec(). Worse, if one of the threads has to wait for the lock (or on its corresponding condition), then errno is mangled and g_ascii_strtol*() signals an error, even though there's no error. Read more here: https://gitlab.gnome.org/GNOME/glib/-/issues/3034 Nevertheless, if we make glib init the g_ascii_strtol*() global state (by calling one function from g_ascii_strtol*() family), then there shouldn't be any congestion on the lock and thus no errno mangling. Signed-off-by: Michal Privoznik Reviewed-by: Daniel P. Berrang=C3=A9 --- src/libvirt.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/libvirt.c b/src/libvirt.c index 2e470adf98..69d5b13bff 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -214,6 +214,14 @@ virGlobalInit(void) if (virErrorInitialize() < 0) goto error; =20 + /* Make glib initialize its own global state. See more: + * + * https://gitlab.gnome.org/GNOME/glib/-/issues/3034 + * + * TODO: Remove ASAP. + */ + g_ascii_strtoull("0", NULL, 0); + virFileActivateDirOverrideForLib(); =20 if (getuid() !=3D geteuid() || --=20 2.39.3