From nobody Sun Nov 24 14:10:26 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=1722415811; cv=none; d=zohomail.com; s=zohoarc; b=XiJ2Ckg87w2sHdKP3m+7NNi5mnakDgE5Lxa9YJOMGUPIb9KijX9GhBXdLcXbl6kfJ5tmSKVhqE5LGjKuEFgCyTbgaBtSlViVemk4kXSyQAiBObhIcOkZ09q0ptJCQnf4+JocnviSnkZmWIgVEFeYKHiydCH4T4MCUY0PAtbf/3E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722415811; 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=Yvb2fdITY/6760paQNKw8FXGBghCfFjytCSj+DZtnsM=; b=WB1mSl0Weuwm3BjaQTo81x/8zjy5Y+pM6gjk4MaTPknZDS4ucwQCKRh4CFX8cHOisIfxqPC+fyVMeW+V/PDJIIVEBvxTwR+ebpe89BIlVbRe+YWqhA50a13fHx4RDZ4PO86LK1uvkGLRYS/4Af8diCNWjthZ3d5qsDLU+hGMTT8= 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 1722415811354153.53463463947855; Wed, 31 Jul 2024 01:50:11 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sZ50k-0002IA-Es; Wed, 31 Jul 2024 04:48:42 -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 1sZ50i-0002G0-Or for qemu-devel@nongnu.org; Wed, 31 Jul 2024 04:48:40 -0400 Received: from mail-wr1-x42f.google.com ([2a00:1450:4864:20::42f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sZ50g-0004bP-Bc for qemu-devel@nongnu.org; Wed, 31 Jul 2024 04:48:40 -0400 Received: by mail-wr1-x42f.google.com with SMTP id ffacd0b85a97d-3684e2d0d8bso394727f8f.2 for ; Wed, 31 Jul 2024 01:48:37 -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.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jul 2024 01:48:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1722415716; x=1723020516; 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=Yvb2fdITY/6760paQNKw8FXGBghCfFjytCSj+DZtnsM=; b=iE7aVlQ84NehZb7rHKOmoiDkjP/0cOQVXLkxXjoDoCK+OL08yqdNjyAkngLmL5HZyv xHsCvWDn5bLH/6WE9c2R11suNLpueJG2X6vruNAjclum3gYt+2IwGqoCgtnQYnYVDKHy NaWliq6PbesAWiU/iUQL+CZ/fmk3W0cHBRjXc5fg/KWR5CtOhhLDee0G/F3E1k6zNFvz 1Bpnc2bSQRFw/Cwil84TB2/htfnz4P6ByCSyK0OdbNxahtiITfI+aPb9WDp/WHFz7fHU jxdIE/lKAnVw1eeVBb96w3c3uxnJDl8cj8aESHbJI0+NnfrMxYlKQz5XKwRwtzpy9Ply ni5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722415716; x=1723020516; 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=Yvb2fdITY/6760paQNKw8FXGBghCfFjytCSj+DZtnsM=; b=XXhcqZ83xv+Vz/aRhTgyOogCTiPtDMR4169IFR7h2qD7OWteelf+lyiL0iH8WzNOdy N364M/Pq6ZbjtmlF4leU1uqcarSXllz9kxEUaW8iTS38RMASS9dht8hVnSj2FVgx7Ojl 905v74QtiJ4OJWFX060iFkzL1Onhg576s0HBPrp+SkjxJKGqXjfHTFUKlY7m14kWAly2 AtlrHGxUP9c6GoSvcI+hDTnEiPSHiYnonH23yM9ewBA/gBKkzMMSIC+NcewSllJf12z4 3IA9e7fvtUcyuc5cXIC+ggfn3dnpLw8WDKvUztk+043U8qLS96eRnQyTeEWKUzjkWVSO +VVA== X-Gm-Message-State: AOJu0Ywd01ZA/C5TeQ5NHb/Y7uj9r5CCdU9jGeKqT5kLXuPWZhf/2IFi Y5oHea/Elu9e8QzWrbYPomdb37vBMGWFVy8jwAjnFNjQQ0GhgohuWx2rwFjaAaIJ0hNkchJSvGY 2Shw= X-Google-Smtp-Source: AGHT+IFwudU6SBz46pqVnn7AwdgBaZTsttrMAOHpfnJeyP/NPz5OqpmrO99kqxXzQVCPgDCdg4vjwA== X-Received: by 2002:a05:600c:138c:b0:424:8b08:26aa with SMTP id 5b1f17b1804b1-428054f24c7mr84073275e9.3.1722415716060; Wed, 31 Jul 2024 01:48:36 -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 1/5] qemu/osdep: Move close_all_open_fds() to oslib-posix Date: Wed, 31 Jul 2024 10:48:25 +0200 Message-ID: <20240731084832.1829291-2-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::42f; envelope-from=cleger@rivosinc.com; helo=mail-wr1-x42f.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: 1722415811860116600 Move close_all_open_fds() in oslib-posix, rename it qemu_close_all_open_fds() and export it. Signed-off-by: Cl=C3=A9ment L=C3=A9ger Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Richard Henderson --- include/qemu/osdep.h | 7 +++++++ system/async-teardown.c | 37 +------------------------------------ util/oslib-posix.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 191916f38e..5cd8517380 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -757,6 +757,13 @@ static inline void qemu_reset_optind(void) =20 int qemu_fdatasync(int fd); =20 +/** + * qemu_close_all_open_fd: + * + * Close all open file descriptors + */ +void qemu_close_all_open_fd(void); + /** * Sync changes made to the memory mapped file back to the backing * storage. For POSIX compliant systems this will fallback diff --git a/system/async-teardown.c b/system/async-teardown.c index 396963c091..edf49e1007 100644 --- a/system/async-teardown.c +++ b/system/async-teardown.c @@ -26,40 +26,6 @@ =20 static pid_t the_ppid; =20 -/* - * Close all open file descriptors. - */ -static void close_all_open_fd(void) -{ - struct dirent *de; - int fd, dfd; - DIR *dir; - -#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; - } - /* Avoid closing the directory. */ - dfd =3D dirfd(dir); - - for (de =3D readdir(dir); de; de =3D readdir(dir)) { - fd =3D atoi(de->d_name); - if (fd !=3D dfd) { - close(fd); - } - } - closedir(dir); -} - static void hup_handler(int signal) { /* Check every second if this process has been reparented. */ @@ -85,9 +51,8 @@ static int async_teardown_fn(void *arg) /* * Close all file descriptors that might have been inherited from the * main qemu process when doing clone, needed to make libvirt happy. - * Not using close_range for increased compatibility with older kernel= s. */ - close_all_open_fd(); + qemu_close_all_open_fd(); =20 /* Set up a handler for SIGHUP and unblock SIGHUP. */ sigaction(SIGHUP, &sa, NULL); diff --git a/util/oslib-posix.c b/util/oslib-posix.c index b090fe0eed..1e867efa47 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -807,3 +807,37 @@ int qemu_msync(void *addr, size_t length, int fd) =20 return msync(addr, length, MS_SYNC); } + +/* + * Close all open file descriptors. + */ +void qemu_close_all_open_fd(void) +{ + struct dirent *de; + int fd, dfd; + DIR *dir; + +#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; + } + /* Avoid closing the directory. */ + dfd =3D dirfd(dir); + + for (de =3D readdir(dir); de; de =3D readdir(dir)) { + fd =3D atoi(de->d_name); + if (fd !=3D dfd) { + close(fd); + } + } + closedir(dir); +} --=20 2.45.2 From nobody Sun Nov 24 14:10:26 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 From nobody Sun Nov 24 14:10:26 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=1722415760; cv=none; d=zohomail.com; s=zohoarc; b=ADbgdKQmoG9InpvH+vHElGYy44o8js59NXXtSaao01e2ud/w2t3ka3kGvegqEbS6jalqH9m8olN23xu1XgLSY897kCs4es9OAKW39N/OliSOOEXnMcXo9WZX6fw+ayAdZ1XgT4UMGCOce7Kyw/E8T7cR/EoiYazDOF/6ws0d3+E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722415760; 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=MacBk+b81Y9XSBf3+Dejb8DivJ0DDo61Nr8SOpG9EWU=; b=Qji9RymkFcXh2K0EktxnghRVF6qLBJam+U91JyidfsEo6yDEQtXR0u8bvUg43zyJixbz/Frtsc1lQCJS4mtdbMujUCty5hH5lCi46MZDRDzff37W0TJimUdxh5M8xE93SZpL1irYEZ9ivQBAPwsW5gVj9WLHLJiFZ2y/q0HqtBU= 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 1722415760453379.273842472738; Wed, 31 Jul 2024 01:49:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sZ50m-0002Qf-Ge; Wed, 31 Jul 2024 04:48:44 -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-0002I1-Ar for qemu-devel@nongnu.org; Wed, 31 Jul 2024 04:48:42 -0400 Received: from mail-wr1-x431.google.com ([2a00:1450:4864:20::431]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sZ50i-0004bf-CR for qemu-devel@nongnu.org; Wed, 31 Jul 2024 04:48:42 -0400 Received: by mail-wr1-x431.google.com with SMTP id ffacd0b85a97d-36874d7f70bso605324f8f.1 for ; Wed, 31 Jul 2024 01:48:39 -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.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jul 2024 01:48:37 -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=MacBk+b81Y9XSBf3+Dejb8DivJ0DDo61Nr8SOpG9EWU=; b=2tDVLmMuCAeHdzn0sC78tz2O0WH5aqkqAPMt1D1nce0fwN4b9Iz/sKS+38OW1BrlOY Al98ulw5ft72EyWUx3uzVvyvfF1Ygx0RU+BDQIJTkjmVd/NNtwS+3tPhjoZnLVlOHrcy P2I8r+JEyitRuZf6KIstz/ZdH70ID/+xkeb6H8wEaD47nxdDXQbvFot1fTdxUrijj2J7 0WV6UzVR4zJtTN2X9CZ6c5ATbOfkbCKmXDOwy6lNNgxpNgtq5pylwEQzXojBC7z4uem2 nRkikAZcuoafPopqjyy6NZy1JJKebjpzQaNmE1I/oIbyrFLwpuzfqUavQtQ8rBmfl4j4 YdOg== 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=MacBk+b81Y9XSBf3+Dejb8DivJ0DDo61Nr8SOpG9EWU=; b=kaAHXX3RF0JaY0kmqc3RFyLqNxXibIiAsHB1AbpYb1tEEjF6R/MgRJvt3SAcjdDSV+ uMqUFlvVxv5Ih62DOM/m5cMtofk9Wi+H8y1gI+i3QCN0eMOCHO+NiNjU+YK+wGk5AwX5 lDZQxRsalKmoibrGvBARzqO+XNAk0aaffOtm2vUCPmt6FoFCj3jHi1hznVnr6/ckvSTm YVh65M9XjMSDwqLrVNSIgYvjOUrpizHSVOMm1FZNGoNvDvrch1lO351gvssYqbf9Qcqg Hhvi8RXGVdQ51dm7u2xPLx4TsbhwA+3KJeCOgbLyoADZRWXbjh03HVmw6kGQDS3QcKzm X09g== X-Gm-Message-State: AOJu0Yx0tjHNZw7JCpx2y3jAdwdg3YTxb9rINHCMXSllFJgh61xUR575 SsIVgvV2szXTImhao7meQZ5wLJ6vQm6f80hWxlmV0FUajHsd4wRstm5hum2O0a6l9+apIFh+ewv j6uI= X-Google-Smtp-Source: AGHT+IFD82QolmIrJcH+O9fEBiISrlJAJHBkvhgIcd+oMOnSShpF9H56hE3GWETiAQxXO5I4qEPmyg== X-Received: by 2002:a05:600c:3ca8:b0:426:6cd1:d104 with SMTP id 5b1f17b1804b1-4280578251bmr90184735e9.4.1722415718319; Wed, 31 Jul 2024 01:48:38 -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 3/5] net/tap: Factorize fd closing after forking Date: Wed, 31 Jul 2024 10:48:27 +0200 Message-ID: <20240731084832.1829291-4-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::431; envelope-from=cleger@rivosinc.com; helo=mail-wr1-x431.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: 1722415761822116600 The same code is used twice to actually close all open file descriptors after forking. Factorize it in a single place. Signed-off-by: Cl=C3=A9ment L=C3=A9ger Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Richard Henderson --- net/tap.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/net/tap.c b/net/tap.c index 51f7aec39d..7b2d5d5703 100644 --- a/net/tap.c +++ b/net/tap.c @@ -385,6 +385,17 @@ static TAPState *net_tap_fd_init(NetClientState *peer, return s; } =20 +static void close_all_fds_after_fork(int excluded_fd) +{ + int open_max =3D sysconf(_SC_OPEN_MAX), i; + + for (i =3D 3; i < open_max; i++) { + if (i !=3D excluded_fd) { + close(i); + } + } +} + static void launch_script(const char *setup_script, const char *ifname, int fd, Error **errp) { @@ -400,13 +411,7 @@ static void launch_script(const char *setup_script, co= nst char *ifname, return; } if (pid =3D=3D 0) { - int open_max =3D sysconf(_SC_OPEN_MAX), i; - - for (i =3D 3; i < open_max; i++) { - if (i !=3D fd) { - close(i); - } - } + close_all_fds_after_fork(fd); parg =3D args; *parg++ =3D (char *)setup_script; *parg++ =3D (char *)ifname; @@ -490,17 +495,11 @@ static int net_bridge_run_helper(const char *helper, = const char *bridge, return -1; } if (pid =3D=3D 0) { - int open_max =3D sysconf(_SC_OPEN_MAX), i; char *fd_buf =3D NULL; char *br_buf =3D NULL; char *helper_cmd =3D NULL; =20 - for (i =3D 3; i < open_max; i++) { - if (i !=3D sv[1]) { - close(i); - } - } - + close_all_fds_after_fork(sv[1]); fd_buf =3D g_strdup_printf("%s%d", "--fd=3D", sv[1]); =20 if (strrchr(helper, ' ') || strrchr(helper, '\t')) { --=20 2.45.2 From nobody Sun Nov 24 14:10:26 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=1722415804; cv=none; d=zohomail.com; s=zohoarc; b=IRh83szxch0cjinmx6oWH7FyzgSepAAxxEw9JV7wGEX1D7rk087u+nXa5eZdTwVEjpUA0OMWxHMLZkk1HZoYZ3Ix58pry9JGFKCXvEGT+GlsatEQ3l8UzfdGtguMdeOPhAfqEw8vebWZb8ODSi9C+lDRj/HUvHEJJNk/OiylOPQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722415804; 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=UZswZ/5MIFNXWQkpnF7EXAEm1eZvqChAOiAdHEAGgjA=; b=GqeX0pWhg8OkvWrw/c/a4C2QFTWwkK+eurNwm3ynGGe8GvrTOPXD1V0dHEkbJf3Zx8W148hLWk1hIOBqzho/BLDCu76DJUY0L4YIF7A59M8mif6TwNgy1VSxNVu4h5mjDbsv7J41gMrAV+2CHCKalHFWfLxzUjoEy3Tl1W9V9bI= 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 172241580487624.17685274786413; Wed, 31 Jul 2024 01:50:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sZ50n-0002Vw-Mp; Wed, 31 Jul 2024 04:48:45 -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 1sZ50l-0002Ng-O5 for qemu-devel@nongnu.org; Wed, 31 Jul 2024 04:48:43 -0400 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sZ50j-0004c1-Oo for qemu-devel@nongnu.org; Wed, 31 Jul 2024 04:48:43 -0400 Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-4281e715904so2823505e9.0 for ; Wed, 31 Jul 2024 01:48:41 -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.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jul 2024 01:48:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1722415720; x=1723020520; 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=UZswZ/5MIFNXWQkpnF7EXAEm1eZvqChAOiAdHEAGgjA=; b=QVQT6SAmGMakOxVmj9JW3Kg69zF/kqRbgjgSDdIB9pZR8qSUbUjcvBdgmSWPAlqsxu NKSeOo+/ok9CRJFjLc8BlaCd2DjMNuwy5qbwF5YQXy400MitOnVMt4u0ASUc50ZO++rq RN0M2F6yre4gjRZCi9ZRWjkCftjNhJRxDoYMEzJ+oxYchICqr8EHWsBYJOQXl/wFYEUB J5dMOWatoD2WKzDWDX3g6IZH0zJYtSYXonjx+EtehaXHjxwMIizrrpVYhHUTAJp1z2qf wVqhKxIz8ghj3OaV+XcEx74SG2Xh3vBfBbGNOIk4XjO2q4jG5IDQ1a9+34xQ9uzGY423 vDqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722415720; x=1723020520; 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=UZswZ/5MIFNXWQkpnF7EXAEm1eZvqChAOiAdHEAGgjA=; b=WyU+v+CNIZacmBuzicxkaPoc6FVg5dhDrDejsk1grJkV/LqGuY5lUVN31xsnqMjcnc ZMv0MjsKgqZdguw2H8NzhtbSP3tYIQ4b0u5Of8klTOMdoDaTvjQ+5wHH147S1U91dTHJ o4kKVL3eMlf6FzXqKHR+w2N5R3oS7Ga8SwY6ZS9xfZDYq5v/zOWgeEY96K+7aYZ7kDg2 UTzc4chKGsoHPG0ElVcCkVj4Bb1PdBssvpZOi7hfCIj1M1Q+GvxoTCvjgrwgjw1GYAZy ZXQRIZ6RuA8F0iFXNbL1cKUx2rmlL3WfHTD+Dyv1sQmXZdmelnLbxdzcOMSqSSfvXZZD CqwA== X-Gm-Message-State: AOJu0Yx0y34zrbmoNfI57DiyBRuu9Mmbz1JMeuUjU4TONkXmgGUiIk5X eJjYSBoWnTqXKUlQ/BQZrgADfi1AbFgR37pwOCN6YyiKqt2LVkNp12rmo2g7cpd/6Qv3C1jzGaq AytA= X-Google-Smtp-Source: AGHT+IFrNa+Qcp+j0nYQbca6HofxQChaxNE4u33lZFNIqMyMdPu75bwou5Y1FquqlKshGujff9d/fw== X-Received: by 2002:a05:6000:188e:b0:362:4aac:8697 with SMTP id ffacd0b85a97d-36b3497af4emr8144064f8f.0.1722415719751; Wed, 31 Jul 2024 01:48:39 -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 4/5] qemu/osdep: Add excluded fd parameter to qemu_close_all_open_fd() Date: Wed, 31 Jul 2024 10:48:28 +0200 Message-ID: <20240731084832.1829291-5-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::333; envelope-from=cleger@rivosinc.com; helo=mail-wm1-x333.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: 1722415805824116600 In order for this function to be usable by tap.c code, add a list of file descriptors that should not be closed. Signed-off-by: Cl=C3=A9ment L=C3=A9ger --- include/qemu/osdep.h | 8 ++- system/async-teardown.c | 2 +- util/oslib-posix.c | 107 ++++++++++++++++++++++++++++++++++------ 3 files changed, 98 insertions(+), 19 deletions(-) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index 5cd8517380..0bf6f0a356 100644 --- a/include/qemu/osdep.h +++ b/include/qemu/osdep.h @@ -760,9 +760,13 @@ int qemu_fdatasync(int fd); /** * qemu_close_all_open_fd: * - * Close all open file descriptors + * Close all open file descriptors except the ones supplied in the @skip a= rray + * + * @skip: ordered array of distinct file descriptors that should not be cl= osed + * if any, or NULL. + * @nskip: number of entries in the @skip array or 0 if @skip is NULL. */ -void qemu_close_all_open_fd(void); +void qemu_close_all_open_fd(const int *skip, unsigned int nskip); =20 /** * Sync changes made to the memory mapped file back to the backing diff --git a/system/async-teardown.c b/system/async-teardown.c index edf49e1007..9148ee8d04 100644 --- a/system/async-teardown.c +++ b/system/async-teardown.c @@ -52,7 +52,7 @@ static int async_teardown_fn(void *arg) * Close all file descriptors that might have been inherited from the * main qemu process when doing clone, needed to make libvirt happy. */ - qemu_close_all_open_fd(); + qemu_close_all_open_fd(NULL, 0); =20 /* Set up a handler for SIGHUP and unblock SIGHUP. */ sigaction(SIGHUP, &sa, NULL); diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 9b79fc7cff..7583192192 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -808,11 +808,12 @@ int qemu_msync(void *addr, size_t length, int fd) return msync(addr, length, MS_SYNC); } =20 -static bool qemu_close_all_open_fd_proc(void) +static bool qemu_close_all_open_fd_proc(const int *skip, unsigned int nski= p) { struct dirent *de; int fd, dfd; DIR *dir; + unsigned int skip_start =3D 0, skip_end =3D nskip; =20 dir =3D opendir("/proc/self/fd"); if (!dir) { @@ -823,8 +824,34 @@ static bool qemu_close_all_open_fd_proc(void) dfd =3D dirfd(dir); =20 for (de =3D readdir(dir); de; de =3D readdir(dir)) { + bool close_fd =3D true; + + if (de->d_name[0] =3D=3D '.') { + continue; + } fd =3D atoi(de->d_name); - if (fd !=3D dfd) { + if (fd =3D=3D dfd) { + close_fd =3D false; + continue; + } + + for (unsigned int i =3D skip_start; i < skip_end; i++) { + if (fd < skip[i]) { + /* We are below the next skipped fd, break */ + break; + } else if (fd =3D=3D skip[i]) { + close_fd =3D false; + /* Restrict the range as we found fds matching start/end */ + if (i =3D=3D skip_start) { + skip_start++; + } else if (i =3D=3D skip_end) { + skip_end--; + } + break; + } + } + + if (close_fd) { close(fd); } } @@ -833,24 +860,68 @@ static bool qemu_close_all_open_fd_proc(void) return true; } =20 -static bool qemu_close_all_open_fd_close_range(void) +static bool qemu_close_all_open_fd_close_range(const int *skip, + unsigned int nskip, + int open_max) { #ifdef CONFIG_CLOSE_RANGE - int r =3D close_range(0, ~0U, 0); - if (!r) { - /* Success, no need to try other ways. */ - return true; - } -#endif + int first =3D 0, last =3D open_max; + unsigned int cur_skip =3D 0; + int ret; + + do { + /* Find the start boundary of the range to close */ + while (cur_skip < nskip && first =3D=3D skip[cur_skip]) { + cur_skip++; + first++; + } + + /* Find the upper boundary of the range to close */ + if (cur_skip < nskip) { + last =3D skip[cur_skip] - 1; + } + /* + * Adjust the maximum fd to close if it's above what the system + * supports + */ + if (last > open_max) { + last =3D open_max; + /* + * We can directly skip the remaining skip fds since the curre= nt + * one is already above the maximum supported one. + */ + cur_skip =3D nskip; + } + /* If last was adjusted, we might be > first, bail out */ + if (first > last) { + break; + } + + ret =3D close_range(first, last, 0); + if (ret < 0) { + return false; + } + first =3D last + 1; + last =3D open_max; + } while (cur_skip < nskip); + + return true; +#else return false; +#endif } =20 -static void qemu_close_all_open_fd_fallback(void) +static void qemu_close_all_open_fd_fallback(const int *skip, unsigned int = nskip, + int open_max) { - int open_max =3D sysconf(_SC_OPEN_MAX), i; + unsigned int cur_skip =3D 0; =20 /* Fallback */ - for (i =3D 0; i < open_max; i++) { + for (int i =3D 0; i < open_max; i++) { + if (cur_skip < nskip && i =3D=3D skip[cur_skip]) { + cur_skip++; + continue; + } close(i); } } @@ -858,10 +929,14 @@ static void qemu_close_all_open_fd_fallback(void) /* * Close all open file descriptors. */ -void qemu_close_all_open_fd(void) +void qemu_close_all_open_fd(const int *skip, unsigned int nskip) { - if (!qemu_close_all_open_fd_close_range() && - !qemu_close_all_open_fd_proc()) { - qemu_close_all_open_fd_fallback(); + int open_max =3D sysconf(_SC_OPEN_MAX) - 1; + + assert(skip !=3D NULL || nskip =3D=3D 0); + + if (!qemu_close_all_open_fd_close_range(skip, nskip, open_max) && + !qemu_close_all_open_fd_proc(skip, nskip)) { + qemu_close_all_open_fd_fallback(skip, nskip, open_max); } } --=20 2.45.2 From nobody Sun Nov 24 14:10:26 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=1722415765; cv=none; d=zohomail.com; s=zohoarc; b=dFiK4tP+7ET1kXYjZcEvbAkN6bRUTHCSbgckKTOU4xsBuXw0HjzwQ64ygu6l2AREOFYuhr7pnzyLqOhMvIbOQVGLktyBrKn9bMOwFOwAx4UD+LqmhApKjcQxqarBpe6VWdaUWy+qrM5b2ulxFP4NbnvpV9YQe7AKSl0YWF0+7Zo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722415765; 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=XxTT+tyZr9At4fgl+GpnY8w1n+uFaZlL69cg05NKgv4=; b=QAobzmAPY7iavwQoaqxqc7+JJNCTvW2wjPY7TMeMoFMWHhuaiIRUOZARB0UwPaqPOM21RU3+TMyO4554/yhSc3cfEpnYg9uGVNovqNGrufCvJBWd9F0/Rkqmhmh7VuMf5x2AfS/k77nMHmugJ4gdESYrhNb19QlBP7u3lfdkrw8= 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 1722415765065745.8667498350628; Wed, 31 Jul 2024 01:49:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sZ50o-0002YS-AY; Wed, 31 Jul 2024 04:48:46 -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 1sZ50m-0002Pq-8O for qemu-devel@nongnu.org; Wed, 31 Jul 2024 04:48:44 -0400 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sZ50k-0004cF-Kh for qemu-devel@nongnu.org; Wed, 31 Jul 2024 04:48:43 -0400 Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-42111cf2706so5061475e9.0 for ; Wed, 31 Jul 2024 01:48:42 -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.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jul 2024 01:48:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1722415721; x=1723020521; 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=XxTT+tyZr9At4fgl+GpnY8w1n+uFaZlL69cg05NKgv4=; b=v2MtBze/VlpYSPY5h02ftnn/PEkANQ1MHdFLCOIQ212j9bporkKZXuLM+XhJqAC8+a /zakYxWE8bZo8YK83mLii374p1/rkElui3IPeRTpAWeTDWrb4XlgF6/8xj2m8fSq2dbj 3dt0r6bu4TbpUvfIcMxsWaJl5WsEwl0e1TN5sWJBD9OPp/r+9TOr6/+DLrY04v966udN a+z1QRDKZ5C+7G8rssUUYgxz7U9nEnptcn/2gBxuBdaSEENF0Cucu0XKZZziJYNtCtAE Qk0/qyy6LjXAKDB54BP8hIry7iU6DVZ6xVLvCZyNGyUx9jVJJl8Z10V6hEeG/JumTcgU snRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722415721; x=1723020521; 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=XxTT+tyZr9At4fgl+GpnY8w1n+uFaZlL69cg05NKgv4=; b=c3pVwoZalZkdPouZZ2PrMz0l/F/zh5iNUdv0zPg/+cRW06cIsAorrp0guFbBe+7+5q BDWCm+XOuo8xJhP5JkfUUtyKCj+SQjCCIauZl0jNtJeBBA+OhqVSlQ9Lok3PZ3cbqnKL nnDoqk4nODHQD8rHoxG89k2uhFQGEj/FcF1NWtnTrTMGa2DoM1EHJcZ/5fBhJEOsvH0y GvaEukBq4rSHh0bphyfnUAkkytVrJJyUo+dWLxAMTMdvrdmQBPqp5FF3HrMwWToVJxsZ omaeyi1FCk9NVj592IYcwqxs/fBuXaL9FYZgJngXjDlFH3HMsZddo4OU9Jykp36h9fJo SA3A== X-Gm-Message-State: AOJu0YyIPx+MW0bA6sXu1jE4mp+dD8pCtDX6oazZNo1u/gC5KpxkEyoj PbBiynx3Kx0hbGGHo2XiCbJK/dRoEuHJU+SZiHxrNSHgYPs3mQ9M94hR2ZLAII7OKB442e8upwY FhRg= X-Google-Smtp-Source: AGHT+IHMNgq4HoQ6IJGLsznC23wWCYpClU66IAnUMh9rfl8p0vwZEJfRwvN6n98Xnl+wRjSQO8Kjng== X-Received: by 2002:a05:6000:18ab:b0:367:90a8:4d3b with SMTP id ffacd0b85a97d-36b34bf2d44mr8401583f8f.3.1722415720939; Wed, 31 Jul 2024 01:48:40 -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 5/5] net/tap: Use qemu_close_all_open_fd() Date: Wed, 31 Jul 2024 10:48:29 +0200 Message-ID: <20240731084832.1829291-6-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::333; envelope-from=cleger@rivosinc.com; helo=mail-wm1-x333.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: 1722415765779116600 Instead of using a slow implementation to close all open fd after forking, use qemu_close_all_open_fd(). Signed-off-by: Cl=C3=A9ment L=C3=A9ger Reviewed-by: Richard Henderson --- net/tap.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/net/tap.c b/net/tap.c index 7b2d5d5703..3f90022c0b 100644 --- a/net/tap.c +++ b/net/tap.c @@ -387,13 +387,20 @@ static TAPState *net_tap_fd_init(NetClientState *peer, =20 static void close_all_fds_after_fork(int excluded_fd) { - int open_max =3D sysconf(_SC_OPEN_MAX), i; + const int skip_fd[] =3D {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, + excluded_fd}; + unsigned int nskip =3D ARRAY_SIZE(skip_fd); =20 - for (i =3D 3; i < open_max; i++) { - if (i !=3D excluded_fd) { - close(i); - } + /* + * skip_fd must be an ordered array of distinct fds, exclude + * excluded_fd if already included in the [STDIN_FILENO - STDERR_FILEN= O] + * range + */ + if (excluded_fd <=3D STDERR_FILENO) { + nskip--; } + + qemu_close_all_open_fd(skip_fd, nskip); } =20 static void launch_script(const char *setup_script, const char *ifname, --=20 2.45.2