From nobody Sun Nov 24 16:25:32 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=1722415788; cv=none; d=zohomail.com; s=zohoarc; b=W/wJMXBrRZ5HYXG8NWhICP6ASnF53ydux2c+QnmrgsaiR7UMurlr2eW3jrEsDrJGYWvOsJJRClm9nKxidjb5BAH8U1hSRLyi2Ilw9zlRQF+ogcTsFnx5wBlcUpGYsGENgZOEen39RRjz8ZrnH3l/DRdtdZJ4kVRKUkBcRrHL750= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722415788; 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=q1X8hnMqcOSbqxXp33EZy6XsP0jjL/S5AUJi2hLZhJM=; b=C0+7cfbDZms/dWOHbjjSSq7Ma0WSbrV1ODVpgkIl+4vIEvnf9svklOunkdWsf1bWmzv+UAIJKiriubEo1OD1XRB06G0cxlsgELSVvaam027qIaUTDOOOaczL35prMsbj0kv/fG5bW7yCF4P9JzBdAUM1jinqrBYJBI1hI4oe7Us= 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 1722415788028182.62664039422202; Wed, 31 Jul 2024 01:49:48 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sZ50l-0002NJ-PD; Wed, 31 Jul 2024 04:48:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sZ50k-0002HB-4G for qemu-devel@nongnu.org; Wed, 31 Jul 2024 04:48:42 -0400 Received: from mail-lj1-x232.google.com ([2a00:1450:4864:20::232]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sZ50i-0004bc-GT for qemu-devel@nongnu.org; Wed, 31 Jul 2024 04:48:41 -0400 Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-2ef247e8479so4374941fa.1 for ; Wed, 31 Jul 2024 01:48:40 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b367c0344sm16576425f8f.2.2024.07.31.01.48.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jul 2024 01:48:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1722415718; x=1723020518; 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=q1X8hnMqcOSbqxXp33EZy6XsP0jjL/S5AUJi2hLZhJM=; b=0Mkp99U7+rQlECcE5tWZzBFSYz4fC/p/1nDuT/3Udn9n5inzkcWMag/naUtcvL1tLF Xn7M1+NlMXz5P0baKEIsI9WoOSSDOeogRt4jXdVe7cEiDWlPfo8JoXZ655aGzLzbYjyQ NC9NI8Oo36mvT6QHTZwmnMsmanQrWTM93XVEpUVJz+6L43axrxcAIEQPr+5Fuiak2ryy TwRqdwT/awa3H5CzJPsYvY+CURAjsmv7wUq23BXW9Y83kw22d2CtjphiZBbZu3T04Xuj PwJ4c1ezYZZd7qEyzVfC/yWbO/XOvIABG9qG1PU1I+RzLioDGypN6NIE3hHBSiRyZ/02 ybWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722415718; x=1723020518; 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=q1X8hnMqcOSbqxXp33EZy6XsP0jjL/S5AUJi2hLZhJM=; b=Rn6SMRPfCC0YbaGH8hQmweutZckiZn+QEa85QArJ4M/Ge0EbKo00jQgeDJapNKCg/r JHnMnHs99v8EWmpYqN0Yj4jFbcwAjrbNdqYd7h2zOh7PjMLxOvXK79xQrAYxMnRYNtyd 5a3avsb0Vhr9+ROLuPJRyFmdUbdLP/BSPXgcryL1+O+yIHGfhuoAPcFAQagcDZWN++Bk MzkLKh7d0Ehuo/8E9E4p8xN7UQdlj026jxvjWP8A6HNyO8aMDHb4eAMK3qk/9J9EbUCr pvp0bsh6PRugJDNY6/d5dNwb/q9E+3nVEozlffWgA67H+LaPtQeYstYw+hBAE5BKZnUQ wOSg== X-Gm-Message-State: AOJu0YwUanMPvPzeVYRfbF2jfQtJSGAfld3QmpTuqIgc/B7tQQf4IANa KbB+jhlNEEDUJYY+wsCd4LJnZIJuXkYBg4hGMOOsrasMWlyWY/IdAv1SI/kseFMMQLZvyg/GYv1 0HQY= X-Google-Smtp-Source: AGHT+IG5nyMLmjCWaiZJ8S93k+dKlfDj8Q9PSr8k5q5kE1juGXV4nNCSvtwDojdxoPbHs+rBqW7YtQ== X-Received: by 2002:a05:651c:155:b0:2ef:2346:9135 with SMTP id 38308e7fff4ca-2f03c8000e0mr67104591fa.9.1722415717439; Wed, 31 Jul 2024 01:48:37 -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 v7 2/5] qemu/osdep: Split qemu_close_all_open_fd() and add fallback Date: Wed, 31 Jul 2024 10:48:26 +0200 Message-ID: <20240731084832.1829291-3-cleger@rivosinc.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240731084832.1829291-1-cleger@rivosinc.com> References: <20240731084832.1829291-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::232; envelope-from=cleger@rivosinc.com; helo=mail-lj1-x232.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham 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: 1722415789855116600 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 Reviewed-by: Richard Henderson --- util/oslib-posix.c | 50 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 1e867efa47..9b79fc7cff 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,39 @@ 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; +} + +static void qemu_close_all_open_fd_fallback(void) +{ + int open_max =3D sysconf(_SC_OPEN_MAX), i; + + /* Fallback */ + for (i =3D 0; i < open_max; i++) { + close(i); + } +} + +/* + * Close all open file descriptors. + */ +void qemu_close_all_open_fd(void) +{ + if (!qemu_close_all_open_fd_close_range() && + !qemu_close_all_open_fd_proc()) { + qemu_close_all_open_fd_fallback(); + } } --=20 2.45.2