From patchwork Wed Jul 31 08:48:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= X-Patchwork-Id: 13748274 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 390FBC3DA7F for ; Wed, 31 Jul 2024 08:50:11 +0000 (UTC) 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) 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) 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) From: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= To: qemu-devel@nongnu.org Cc: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= , =?utf-8?q?Daniel?= =?utf-8?q?_P=2E_Berrang=C3=A9?= , Peter Maydell , Jason Wang , Richard Henderson , =?utf-8?q?Philippe_Mathie?= =?utf-8?q?u-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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Move close_all_open_fds() in oslib-posix, rename it qemu_close_all_open_fds() and export it. Signed-off-by: Clément Léger Reviewed-by: Philippe Mathieu-Daudé 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) int qemu_fdatasync(int fd); +/** + * 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 @@ static pid_t the_ppid; -/* - * 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 = close_range(0, ~0U, 0); - if (!r) { - /* Success, no need to try other ways. */ - return; - } -#endif - - dir = opendir("/proc/self/fd"); - if (!dir) { - /* If /proc is not mounted, there is nothing that can be done. */ - return; - } - /* Avoid closing the directory. */ - dfd = dirfd(dir); - - for (de = readdir(dir); de; de = readdir(dir)) { - fd = atoi(de->d_name); - if (fd != 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 kernels. */ - close_all_open_fd(); + qemu_close_all_open_fd(); /* 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) 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 = close_range(0, ~0U, 0); + if (!r) { + /* Success, no need to try other ways. */ + return; + } +#endif + + dir = opendir("/proc/self/fd"); + if (!dir) { + /* If /proc is not mounted, there is nothing that can be done. */ + return; + } + /* Avoid closing the directory. */ + dfd = dirfd(dir); + + for (de = readdir(dir); de; de = readdir(dir)) { + fd = atoi(de->d_name); + if (fd != dfd) { + close(fd); + } + } + closedir(dir); +} From patchwork Wed Jul 31 08:48:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= X-Patchwork-Id: 13748272 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AF49EC3DA64 for ; Wed, 31 Jul 2024 08:49:47 +0000 (UTC) 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) 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) 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) From: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= To: qemu-devel@nongnu.org Cc: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= , =?utf-8?q?Daniel?= =?utf-8?q?_P=2E_Berrang=C3=A9?= , Peter Maydell , Jason Wang , Richard Henderson , =?utf-8?q?Philippe_Mathie?= =?utf-8?q?u-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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 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ément Léger 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); } -/* - * 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; -#ifdef CONFIG_CLOSE_RANGE - int r = close_range(0, ~0U, 0); - if (!r) { - /* Success, no need to try other ways. */ - return; - } -#endif - dir = 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 = 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 = 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 = sysconf(_SC_OPEN_MAX), i; + + /* Fallback */ + for (i = 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(); + } } From patchwork Wed Jul 31 08:48:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= X-Patchwork-Id: 13748270 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 399ADC3DA7F for ; Wed, 31 Jul 2024 08:49:20 +0000 (UTC) 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) 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) 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) From: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= To: qemu-devel@nongnu.org Cc: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= , =?utf-8?q?Daniel?= =?utf-8?q?_P=2E_Berrang=C3=A9?= , Peter Maydell , Jason Wang , Richard Henderson , =?utf-8?q?Philippe_Mathie?= =?utf-8?q?u-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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 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ément Léger Reviewed-by: Philippe Mathieu-Daudé 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; } +static void close_all_fds_after_fork(int excluded_fd) +{ + int open_max = sysconf(_SC_OPEN_MAX), i; + + for (i = 3; i < open_max; i++) { + if (i != 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, const char *ifname, return; } if (pid == 0) { - int open_max = sysconf(_SC_OPEN_MAX), i; - - for (i = 3; i < open_max; i++) { - if (i != fd) { - close(i); - } - } + close_all_fds_after_fork(fd); parg = args; *parg++ = (char *)setup_script; *parg++ = (char *)ifname; @@ -490,17 +495,11 @@ static int net_bridge_run_helper(const char *helper, const char *bridge, return -1; } if (pid == 0) { - int open_max = sysconf(_SC_OPEN_MAX), i; char *fd_buf = NULL; char *br_buf = NULL; char *helper_cmd = NULL; - for (i = 3; i < open_max; i++) { - if (i != sv[1]) { - close(i); - } - } - + close_all_fds_after_fork(sv[1]); fd_buf = g_strdup_printf("%s%d", "--fd=", sv[1]); if (strrchr(helper, ' ') || strrchr(helper, '\t')) { From patchwork Wed Jul 31 08:48:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= X-Patchwork-Id: 13748273 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 78594C3DA64 for ; Wed, 31 Jul 2024 08:50:04 +0000 (UTC) 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) 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) 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) From: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= To: qemu-devel@nongnu.org Cc: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= , =?utf-8?q?Daniel?= =?utf-8?q?_P=2E_Berrang=C3=A9?= , Peter Maydell , Jason Wang , Richard Henderson , =?utf-8?q?Philippe_Mathie?= =?utf-8?q?u-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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org 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ément Léger --- 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 array + * + * @skip: ordered array of distinct file descriptors that should not be closed + * 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); /** * 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); /* 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); } -static bool qemu_close_all_open_fd_proc(void) +static bool qemu_close_all_open_fd_proc(const int *skip, unsigned int nskip) { struct dirent *de; int fd, dfd; DIR *dir; + unsigned int skip_start = 0, skip_end = nskip; dir = opendir("/proc/self/fd"); if (!dir) { @@ -823,8 +824,34 @@ static bool qemu_close_all_open_fd_proc(void) dfd = dirfd(dir); for (de = readdir(dir); de; de = readdir(dir)) { + bool close_fd = true; + + if (de->d_name[0] == '.') { + continue; + } fd = atoi(de->d_name); - if (fd != dfd) { + if (fd == dfd) { + close_fd = false; + continue; + } + + for (unsigned int i = skip_start; i < skip_end; i++) { + if (fd < skip[i]) { + /* We are below the next skipped fd, break */ + break; + } else if (fd == skip[i]) { + close_fd = false; + /* Restrict the range as we found fds matching start/end */ + if (i == skip_start) { + skip_start++; + } else if (i == 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; } -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 = close_range(0, ~0U, 0); - if (!r) { - /* Success, no need to try other ways. */ - return true; - } -#endif + int first = 0, last = open_max; + unsigned int cur_skip = 0; + int ret; + + do { + /* Find the start boundary of the range to close */ + while (cur_skip < nskip && first == skip[cur_skip]) { + cur_skip++; + first++; + } + + /* Find the upper boundary of the range to close */ + if (cur_skip < nskip) { + last = skip[cur_skip] - 1; + } + /* + * Adjust the maximum fd to close if it's above what the system + * supports + */ + if (last > open_max) { + last = open_max; + /* + * We can directly skip the remaining skip fds since the current + * one is already above the maximum supported one. + */ + cur_skip = nskip; + } + /* If last was adjusted, we might be > first, bail out */ + if (first > last) { + break; + } + + ret = close_range(first, last, 0); + if (ret < 0) { + return false; + } + first = last + 1; + last = open_max; + } while (cur_skip < nskip); + + return true; +#else return false; +#endif } -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 = sysconf(_SC_OPEN_MAX), i; + unsigned int cur_skip = 0; /* Fallback */ - for (i = 0; i < open_max; i++) { + for (int i = 0; i < open_max; i++) { + if (cur_skip < nskip && i == 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 = sysconf(_SC_OPEN_MAX) - 1; + + assert(skip != NULL || nskip == 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); } } From patchwork Wed Jul 31 08:48:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= X-Patchwork-Id: 13748271 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9E053C3DA64 for ; Wed, 31 Jul 2024 08:49:24 +0000 (UTC) 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) 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) 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) From: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= To: qemu-devel@nongnu.org Cc: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= , =?utf-8?q?Daniel?= =?utf-8?q?_P=2E_Berrang=C3=A9?= , Peter Maydell , Jason Wang , Richard Henderson , =?utf-8?q?Philippe_Mathie?= =?utf-8?q?u-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 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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Instead of using a slow implementation to close all open fd after forking, use qemu_close_all_open_fd(). Signed-off-by: Clément Léger 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, static void close_all_fds_after_fork(int excluded_fd) { - int open_max = sysconf(_SC_OPEN_MAX), i; + const int skip_fd[] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, + excluded_fd}; + unsigned int nskip = ARRAY_SIZE(skip_fd); - for (i = 3; i < open_max; i++) { - if (i != 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_FILENO] + * range + */ + if (excluded_fd <= STDERR_FILENO) { + nskip--; } + + qemu_close_all_open_fd(skip_fd, nskip); } static void launch_script(const char *setup_script, const char *ifname,