From nobody Sun Nov 24 16:35:14 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1722350206; cv=none; d=zohomail.com; s=zohoarc; b=CRKHvOCSfa2HKptu0UCJ29SgvuR9PsREk9EuZyOoO9dlnBn6iWWC2NqjVX0e+zmndBFMrJOClEBjhQ5ipF86nPp36GI8Enx5hUpFiHWgWrL5U4NjyQuLdvo9hrNKumjtmQ2EveHfi/MD5rijI+/tRDPBYGSWtcPmsRdWf/DgsYI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722350206; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=amdT7GOZcFRjKcSgOsJ0Xtj6iZAzzmW4Z/k0DIOKYcY=; b=Ud12V3QrEXaYz/1b8O4sDBoVZEvQ4nZr8pm7BqmxhFGaOUNJn/TmRg65BcMS/DGkzW6uE6Yc0iMa7Qj4u8s7yb+13Dm399K9PsmTosRYY0WpFWszFuK8GDWeMSLxVRaOugixdz4VjlKnuopDsCPKWQ4lHSQcKqqo72v7JeUxxtU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1722350206828717.0396825303028; Tue, 30 Jul 2024 07:36:46 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sYm4C-0003DI-RW; Tue, 30 Jul 2024 08:35:04 -0400 Received: from eggs.gnu.org ([209.51.188.92]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sYm3t-0003Aw-Tj for qemu-devel@nongnu.org; Tue, 30 Jul 2024 08:34:45 -0400 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sYm3A-00064g-JA for qemu-devel@nongnu.org; Tue, 30 Jul 2024 08:34:26 -0400 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-42111cf2706so3809085e9.0 for ; Tue, 30 Jul 2024 05:32:50 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b36862549sm14577757f8f.106.2024.07.30.05.24.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jul 2024 05:24:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1722342754; x=1722947554; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=amdT7GOZcFRjKcSgOsJ0Xtj6iZAzzmW4Z/k0DIOKYcY=; b=zMakEwmiKJhTszO3eHZ36CJRieWV+oRFbQkGuCMxK4hp2y4JwwSX1MQ/2DwxO1nZcU 4bPF8TnDDCEGOSlMgOdgyG2ATFjHLjRQ790yjOgUwdazbx3x83v0vAec2MgFrJEGswav G7WCSLoos4B1N2yVwOYqWifhw6LSUslJ6wgAS164nC8tqR8hlBqNRjg+aymqdzri6mvG rz596q7o/02ORzCykp9qjTNO0npoQ+ktRKRdHUuyXQUO1Py5gD6fKSasEv29d59Ezb+l 3yJ1WN41hf8Fvr4n1yTDm5S+V6obD2uSTk5CZKH1KXPZX/gThly4TDxWTdKpfuIRBj6c Np8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722342754; x=1722947554; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=amdT7GOZcFRjKcSgOsJ0Xtj6iZAzzmW4Z/k0DIOKYcY=; b=YHezpQ2G52SzO3vwXDMcP/Ot9OAGfgeJLwvjZX9sPjP6sKi5N7tTcWrxpC0H0Z1TLU BV/p1uQufZpff03n0JPM1z4jOPAAJBGNGS6XdEn0uOFs0j1pQXAMQeCPUYaSvd9U95AY +ud1gL9dKRZRa58PZvQpvqA/ZcroCxTLOU0wMou8xsE6ws4ZJ7iSde6S2a5ZlKOkBjK0 z9XdrO2cW/7JVmfhyFu+s9Qxxl7Fc/g9M8vzLSZtdKpFKC/pHs/rC91PDdAoGVKfUl5L 3erjrHK1AVSV0aYaHS+3J9DJ/wH7MagrTa3Om18I4zVdxSnEZzSuTlqiVo0aXSAa08JX s3Eg== X-Gm-Message-State: AOJu0YwoiCua62HVMKgaAqArD8iufn4s6u0cmcELdPazTYjTzACLClgw +twoNCMKSFCXKxKjYERrYxHnOJQ2t6PNceN5M3dlOCdvBZF73keDvneTmZJUQ14pOHIXclg3IHR zh9w= X-Google-Smtp-Source: AGHT+IFgZ57BfhHq/TcQvyfrAkOd7Tb/Imc1aMga0VFlSD5Ch20cr4WVOCdCLeGDAMM5dOUm1OieBA== X-Received: by 2002:a5d:64a3:0:b0:367:4d9d:56a6 with SMTP id ffacd0b85a97d-36b34be17d5mr6845131f8f.1.1722342283446; Tue, 30 Jul 2024 05:24:43 -0700 (PDT) From: =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Peter Maydell , Jason Wang , Richard Henderson , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PATCH v6 2/5] qemu/osdep: Split qemu_close_all_open_fd() and add fallback Date: Tue, 30 Jul 2024 14:24:25 +0200 Message-ID: <20240730122437.1749603-3-cleger@rivosinc.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240730122437.1749603-1-cleger@rivosinc.com> References: <20240730122437.1749603-1-cleger@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::334; envelope-from=cleger@rivosinc.com; helo=mail-wm1-x334.google.com X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, T_SPF_HELO_TEMPERROR=0.01, T_SPF_TEMPERROR=0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @rivosinc-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1722350209038116600 In order to make it cleaner, split qemu_close_all_open_fd() logic into multiple subfunctions (close with close_range(), with /proc/self/fd and fallback). Signed-off-by: Cl=C3=A9ment L=C3=A9ger --- util/oslib-posix.c | 51 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 1e867efa47..a6749d9f9b 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -808,27 +808,16 @@ int qemu_msync(void *addr, size_t length, int fd) return msync(addr, length, MS_SYNC); } =20 -/* - * Close all open file descriptors. - */ -void qemu_close_all_open_fd(void) +static bool qemu_close_all_open_fd_proc(void) { struct dirent *de; int fd, dfd; DIR *dir; =20 -#ifdef CONFIG_CLOSE_RANGE - int r =3D close_range(0, ~0U, 0); - if (!r) { - /* Success, no need to try other ways. */ - return; - } -#endif - dir =3D opendir("/proc/self/fd"); if (!dir) { /* If /proc is not mounted, there is nothing that can be done. */ - return; + return false; } /* Avoid closing the directory. */ dfd =3D dirfd(dir); @@ -840,4 +829,40 @@ void qemu_close_all_open_fd(void) } } closedir(dir); + + return true; +} + +static bool qemu_close_all_open_fd_close_range(void) +{ +#ifdef CONFIG_CLOSE_RANGE + int r =3D close_range(0, ~0U, 0); + if (!r) { + /* Success, no need to try other ways. */ + return true; + } +#endif + return false; +} + +/* + * Close all open file descriptors. + */ +void qemu_close_all_open_fd(void) +{ + int open_max =3D sysconf(_SC_OPEN_MAX); + int i; + + if (qemu_close_all_open_fd_close_range()) { + return; + } + + if (qemu_close_all_open_fd_proc()) { + return; + } + + /* Fallback */ + for (i =3D 0; i < open_max; i++) { + close(i); + } } --=20 2.45.2