From patchwork Fri Aug 2 14:54:17 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: 13751603 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 A3F85C52D70 for ; Fri, 2 Aug 2024 14:55:43 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sZtg8-00036Z-0M; Fri, 02 Aug 2024 10:54:48 -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 1sZtg6-0002sn-2I for qemu-devel@nongnu.org; Fri, 02 Aug 2024 10:54:46 -0400 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sZtg4-0005EH-8u for qemu-devel@nongnu.org; Fri, 02 Aug 2024 10:54:45 -0400 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1fc5bc8d23cso4680145ad.1 for ; Fri, 02 Aug 2024 07:54:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1722610482; x=1723215282; 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=RbiqW0FBf0s4d09Z/i1Ew4rMVuSfOI51ePlXd/j06TE=; b=16ls7nVEFT3GqF3zQC/POivHH3K4kYwITUfq4r5LjswEWYDUPsaRT4aWp7iAwTJTVD FhVON1jkQLQTKP7lVzHth8q5L9uYbZApckvuHXZP+EpR+hUrnf6ybm338ZWQXvAcopQt kBFKnIuAIuQCbNNjed2deMx8x1g52ZWl4rERy5uNCS0OGZUtZG8xegnkkhcXCHYs4SAk zLQ5UK6owqSyWFG2pMFe+41yerULBxx6I2dHQzdKWSGgcZ+JoLqCJfHdFH8HSOnsaJc/ SF0mvJtdEEd1Q2Ss1TyOU4yB/e7v7FV2EEkAY9JK6xojHUO7UI3kVV4nEH3kbF0BPMga G4zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722610482; x=1723215282; 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=RbiqW0FBf0s4d09Z/i1Ew4rMVuSfOI51ePlXd/j06TE=; b=HbgJ5an7XulZ+WqSDPyBPDgjvyDRXN+tecFHNds4CrKVUvHEay71LjfqJXzvXRZ/gP G0w851RaxGPZNGzKmp7kKl9XgYoxyNiKAmXR9ft9t8m1YKEdmXawCIM86YMXWm8QVpqq 9+rU7FRSSsBuEz6nvr/feewBMSNeXSAHNWwGUlUcUiRf7evGH6up71rTERXN63Pyh9rB BEROmMF67RtGgtUJjdKt0lBnLceSCz8T+CXUDqfcn4n2U/OeuWvgBxCJ0VwD+9ydJm6v RfAM/wlRe/H6nPVbAvrc24KyIs7EuIG437JSL2cFt3Sif6dmBSbMNj4NOiz4twUQm20B cOfQ== X-Gm-Message-State: AOJu0YwfN2mu7cWAZi2LXc2xoiJ59+VxPP/iM+PDW5mUjbOUOZZyw1LT rSYqOax0v+sfeHEoFKjh0xPzVdasOR9HDpL7OqmUV89XR9vKG9p9nuqFfgWH8Is2ILj/nJWlC16 scgA= X-Google-Smtp-Source: AGHT+IG3AXqGPRklcFjv4Zj4ZHxg2d7d4ZvupJgR/ISntF5u5kG/89iUoRmum4VrkD6MBWAnbc6NDg== X-Received: by 2002:a17:902:f54b:b0:1fb:a38b:c5b6 with SMTP id d9443c01a7336-1ff57250140mr28789535ad.1.1722610481653; Fri, 02 Aug 2024 07:54:41 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff592aee19sm17920635ad.282.2024.08.02.07.54.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Aug 2024 07:54:41 -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 v8 1/5] qemu/osdep: Move close_all_open_fds() to oslib-posix Date: Fri, 2 Aug 2024 16:54:17 +0200 Message-ID: <20240802145423.3232974-2-cleger@rivosinc.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240802145423.3232974-1-cleger@rivosinc.com> References: <20240802145423.3232974-1-cleger@rivosinc.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62c; envelope-from=cleger@rivosinc.com; helo=mail-pl1-x62c.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 720ed21a7e..de77c5c254 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 Fri Aug 2 14:54:18 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: 13751602 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 5D659C3DA4A for ; Fri, 2 Aug 2024 14:55:41 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sZtgC-0003Nd-8Q; Fri, 02 Aug 2024 10:54:52 -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 1sZtgA-0003Hi-NL for qemu-devel@nongnu.org; Fri, 02 Aug 2024 10:54:50 -0400 Received: from mail-pg1-x529.google.com ([2607:f8b0:4864:20::529]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sZtg9-0005FE-2d for qemu-devel@nongnu.org; Fri, 02 Aug 2024 10:54:50 -0400 Received: by mail-pg1-x529.google.com with SMTP id 41be03b00d2f7-78556f71c10so540502a12.0 for ; Fri, 02 Aug 2024 07:54:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1722610487; x=1723215287; 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=X+7HflQUF1blNin5hk/K36A77NZBIc7HR6vMaeAuKWw=; b=1AWUjK8UBd6Hg220NN25PYkvQzTxwymTIA7LBzMy91mqQ7TpvTb4QqQPODvWQ1RM6m 6TxrdFvWUbytXYtYp97PvFAohEPsE0LINyGeGA0MQy6uHpAhncbQzOZcsl4Yx8stritB NFFeZ3kWBGFBiPIQAjHDykRRnhEeZkdtpHPktqinZHudHKh8AKDBo6V3DTz+npDJ/uhy tcPdpeOu1luZkc/vme84cm9OvHnx5af8JPwmPPY6CQYi/Ec/scQ+8gJ/aZ3lphpqUXA0 tlzWbBSZgD0XVRLhVNDF7zDsJ37fEsXlYhlDkXS732lpPXnD0VusfHssiGj+r/0wXRae IYVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722610487; x=1723215287; 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=X+7HflQUF1blNin5hk/K36A77NZBIc7HR6vMaeAuKWw=; b=b56/veYmJWCiJ68Qiuik2UGRJMvV7KsAjSXZA5otuknR7RAzZMyf5tT+YvWieVMjDJ C+Vhptp9ZBec3qK3cxTj9SCqz6F6BsboP5eT5ujhO+ObwMeoosdeT5JmsWEt3BCUFggY 0MVaPoT6OvxFsxdBY9zeRpAcwB0xyk7BuiEZ0U5nVssOcKEXZu8VO1kSkRVbre++XRyh aqym7LSr60o3+GmX4gZGbW2yk3gd4daj/QEx6Y7uKuwX7ompPD2r6UJlCB/ijAgngm8y Gq3WPqVB6K5YBK1HTsQlrkixlLijdymDS7uAYE8HrdqEoG1+JC3XWaHoF0MWlhLTXNGA IPZQ== X-Gm-Message-State: AOJu0YwR5qoEe9CIXInZAoep7TfDYryC6MCYAIc+xHHIS69KRjq0Lj/0 6hxfik0EdNZJ6BWX/XAo4pnZlC7e37i41fLG2qlv4B97ibdj0YQODuuQ3bvN8it8nGFH6a7WdD1 XGWI= X-Google-Smtp-Source: AGHT+IFUPQP1Y79Xu6jMeqfnMY3TX/XR+yQt6PoUqx01RM7rx2kqNKFBomG0P4iK6rHWabm+9FiLfg== X-Received: by 2002:a17:902:ecc6:b0:1fc:52f4:d7c8 with SMTP id d9443c01a7336-1ff574e2c4dmr25525685ad.10.1722610487105; Fri, 02 Aug 2024 07:54:47 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff592aee19sm17920635ad.282.2024.08.02.07.54.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Aug 2024 07:54:46 -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 v8 2/5] qemu/osdep: Split qemu_close_all_open_fd() and add fallback Date: Fri, 2 Aug 2024 16:54:18 +0200 Message-ID: <20240802145423.3232974-3-cleger@rivosinc.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240802145423.3232974-1-cleger@rivosinc.com> References: <20240802145423.3232974-1-cleger@rivosinc.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::529; envelope-from=cleger@rivosinc.com; helo=mail-pg1-x529.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 Fri Aug 2 14:54:19 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: 13751605 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 1A75AC52D70 for ; Fri, 2 Aug 2024 14:56:03 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sZtgI-0003mj-Qp; Fri, 02 Aug 2024 10:54:58 -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 1sZtgG-0003e6-B5 for qemu-devel@nongnu.org; Fri, 02 Aug 2024 10:54:56 -0400 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sZtgE-0005FX-Ok for qemu-devel@nongnu.org; Fri, 02 Aug 2024 10:54:56 -0400 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1fc53f91ed3so4589675ad.0 for ; Fri, 02 Aug 2024 07:54:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1722610493; x=1723215293; 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=pu0PDmphpWvAx2V7bKZPZi/uIpDD5LBQgrAfpnvPySfkCtt3wuPF0wMpLRtYWRlhu4 gTOz1BO3pXuuXfySeOAG7G7935Tz0MQi8Xz1yo4Km0bI3CC5oIvIl5VJ9WOYDVLC/YZD IScS/x8HwGZqXo9EodlsdsWR1Werg4xoZkSGXTSqBeD86LFL7OP/aDUTSw9iTlqZ0PRl z+plNoRFfX4itVmi2w32ZE3K8tGm44PHvc4/Bmz880XEEiOsYPAjWMeY/BL09I0NOcFP ESDa/Fu4+0NkMfQewTdpn/am3A6CcVTqEKZNb3iqkonciKo+F+8MeqmIaVRrdJQSeY9r dGGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722610493; x=1723215293; 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=m7VwL1Evgcg4txXwXGwoUq/0bwzWIaEILqidzMXLwYAlBjjTjaOM/nWWY0dnTMJW+u oXfHVQUuRKkYxpysVvEsSKT1xoGjbFDbDamwTOU7G/jTrgc/2hSTTjQWH9P+55NGb3WI XxPfAyxD5OClRFae429Ip84Uxgi28nSlzrCRxsHhW1PyRwU86XJC9p6Jr02qGf9xTglV D3vUhxW00eJ8o/x8VIYbV/TD2mFgmXZB0G3W8Bsk4oVJZF+2krhkhO0M7ArH53q47QBr CNre93Re6IChkEAXHMRrUQexFdp3w8Je6x3myA4fQhNsIyRlTrrkfANUiTRaqq8g8CdC PuJA== X-Gm-Message-State: AOJu0Yxr/FpvAPPaCqIC0pu6Y3Rstb/vuwVoOBaBd1rGLD7JGBkMEUrw eC2YT4MazTlrPGjF/Sqvmys1zdmUxu+hwuy14f+4lHflcQ0zQg+VrsLC8aGhcWYpDgMkCny86jl XsDQ= X-Google-Smtp-Source: AGHT+IEU6nGKnBmqq1WFJHeOlgdzhC0aE5oD0LAj7ZziQ8yOn1c8448Jrw6KxX8QKwu90VS16vX0Ig== X-Received: by 2002:a17:902:f9c6:b0:1f7:2576:7f88 with SMTP id d9443c01a7336-1ff57422d34mr27930775ad.6.1722610492647; Fri, 02 Aug 2024 07:54:52 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff592aee19sm17920635ad.282.2024.08.02.07.54.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Aug 2024 07:54:52 -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 v8 3/5] net/tap: Factorize fd closing after forking Date: Fri, 2 Aug 2024 16:54:19 +0200 Message-ID: <20240802145423.3232974-4-cleger@rivosinc.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240802145423.3232974-1-cleger@rivosinc.com> References: <20240802145423.3232974-1-cleger@rivosinc.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62d; envelope-from=cleger@rivosinc.com; helo=mail-pl1-x62d.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 Fri Aug 2 14:54:20 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: 13751601 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 C61A8C3DA4A for ; Fri, 2 Aug 2024 14:55:23 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sZtgN-00045g-TY; Fri, 02 Aug 2024 10:55:03 -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 1sZtgL-0003yY-Uh for qemu-devel@nongnu.org; Fri, 02 Aug 2024 10:55:01 -0400 Received: from mail-pl1-x62e.google.com ([2607:f8b0:4864:20::62e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sZtgJ-0005Fm-Sr for qemu-devel@nongnu.org; Fri, 02 Aug 2024 10:55:01 -0400 Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1fd8f92d805so3690075ad.0 for ; Fri, 02 Aug 2024 07:54:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1722610498; x=1723215298; 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=e/VuHi8HCpVaGQUBeXQ5vj7rlUpYLSoENN/sFJxhSHw=; b=LYAFDe0UZNEIkjJ6ix1ZZSE7i1zEXfbzbKuuWTJG6/n4IxbIGZmfEIaNE9MA9dxYuH m6oWvlzBfpw01gGRrjtm4HMvqlt9inofDF2zN5MKylZh5LNN9Hm32mq4xN8uIxfqaGLN 44s2rIAsbcqDVJMtPRriLBpBKeFO/BFOCGZjnY9j/zfdTZwq0xs0L+swe/S8V+UzO7CC CGux39NLB/50VNIyDI/OOlwXoz6YaY3wQuV60ZixuymziT9TYkXTEgGAtw/OQYfe6y38 6vsvXccQhCdB90Vc31mMjaTR1DVzQPukuuWqNoVMtGKx0F5w7OTZ/Twbf73qVqgbvTUS 9vHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722610498; x=1723215298; 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=e/VuHi8HCpVaGQUBeXQ5vj7rlUpYLSoENN/sFJxhSHw=; b=q3ZpMUGld8SJQ0KLLiydz4UvH9YtfChBYaCmjiJtSO1Sf8lgEbPy300bLa2gSwuVZH VzoIhyZPPkoqP4FQNrnJGuAixxeG1wQey+2PtkysN2j3NmoBvpNWG43836jrd/Fsmw/O Ed329I72qMcsoRDQAXhzFoOP4lGBcoVKKVPdrRjUasuSgs1iGf5rTioJkhBKfXCAZa1U eGTUycnhfgnvBzzIQoE/3wSTFt6wKI/qSsDI5R4Qt1Hjr0cYDrc9YXSvIRqGQxdylsKC 0XPv14yj0sKV+IPYcP1SaBaYwIhYF5SNHAZx+yYBT3RIGLqga41IfdA1ws4WQnj/WB5j J0Sg== X-Gm-Message-State: AOJu0YxsIHdoHd4hJy24MGVxqgVbkh+ItBjcLjlFMtTvLu1u6XJmZKwe L90JzXqq1DHT3MCiRaMPwTod6g/mmhcJVTy08BT4r/cA6P62/iBWxoEMiT141NNCGXoKZF4Jdda T6Y4= X-Google-Smtp-Source: AGHT+IFoBSPZtjftTvV1cgW54l3IXFDJC1iScLd4l9KhIUonX4gR0zCQFbpyGZ6+WZ1mpK29CFDB5w== X-Received: by 2002:a17:902:e811:b0:1fc:7180:f4af with SMTP id d9443c01a7336-1ff572930a2mr28830415ad.1.1722610498083; Fri, 02 Aug 2024 07:54:58 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff592aee19sm17920635ad.282.2024.08.02.07.54.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Aug 2024 07:54:57 -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 v8 4/5] qemu/osdep: Add excluded fd parameter to qemu_close_all_open_fd() Date: Fri, 2 Aug 2024 16:54:20 +0200 Message-ID: <20240802145423.3232974-5-cleger@rivosinc.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240802145423.3232974-1-cleger@rivosinc.com> References: <20240802145423.3232974-1-cleger@rivosinc.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62e; envelope-from=cleger@rivosinc.com; helo=mail-pl1-x62e.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 | 106 ++++++++++++++++++++++++++++++++++------ 3 files changed, 97 insertions(+), 19 deletions(-) diff --git a/include/qemu/osdep.h b/include/qemu/osdep.h index de77c5c254..4cc4c32b14 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..2636dcd1e0 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,33 @@ 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) { + 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 +859,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 - 1; + 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 +928,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); + + 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 Fri Aug 2 14:54:21 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: 13751604 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 9AD39C52D6F for ; Fri, 2 Aug 2024 14:56:02 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sZtga-0004co-13; Fri, 02 Aug 2024 10:55:18 -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 1sZtgS-0004OO-R4 for qemu-devel@nongnu.org; Fri, 02 Aug 2024 10:55:08 -0400 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sZtgQ-0005HE-Ox for qemu-devel@nongnu.org; Fri, 02 Aug 2024 10:55:08 -0400 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1ff499841f4so2624145ad.1 for ; Fri, 02 Aug 2024 07:55:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1722610504; x=1723215304; 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=DBb8FimTboUF6FURQ3ZthP9PB7XPgibNTkAPe914KOA4o44E7xnQQO4bFzdq/Sz4cK umPK0FLGuna9B9acEumWEo42gT1WfpE6DJyWUIbK9d5ppURwXAQkKj9dvBQbjUi6Xjqg 5fHzgMs014TN1RtggDjjn8GNIFDkFel4N7g8UZYPzk+kH/nQJL2gKei6xru+ylenhaLh S+yKeqIMyyeqxyF0ewnHw56nn/WArwj/BJYJ3rNnx/b2gAsopSQ/r+mjKVD6H4NlJmvT 9VRXFypO8sCnpZU9GaOqiOnhSQc4lEhIuxbRFVZ5aGv15JU4ypYEnRvBcHyltSyNO3rh 2glA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722610504; x=1723215304; 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=NUaMhihWAf8B/+s4+JCHM+TtIHuFOMtO9HqxN53XZe2BREwuMaW6PMFLzrG1xyIN3p OVxtYATeeqlt/hWhCRozdKnKsQ+bXiy9pTrsDjJYg1E/zwcoFsJU/3ljuP/8TmSZAkxo R2Y+2x96zRMpUTkgl7Ix1z+8NR59hTcagr8BsYCuvKGLI3tEMQP5sTYsmWXyULYMTMy0 mo4c66CNgMYszRASXSz1iBQGW7KHpAp4XV42Ova6VpqyM1Qrd/V6JEknX7aRjMlSdIXl 8ToM7WAA4v/8O+Yv85rM0OQy49W19npd52/3shCRJEcgDP0tTCie8LVc0FnpIOpHnWXT ixSw== X-Gm-Message-State: AOJu0YyZ+A1QdA+SQz7rm6rNAGQvFlhuUJOpNuQviKJdFj0NvV/kjocd e8/sGeIWyX9igv2vQbU4uILkS2Vi6gAskDJ3aiw5Wn8t8m0yHyHw0/Tu0FI77RqmC68KKr8EpcJ fFj8= X-Google-Smtp-Source: AGHT+IGdnsLMOuDXwtzrZ5yS0wi/cF6PrYqO+0dllNNGYuPkSLXDvKpTphPkxfcutc8qpHRVUbw5/g== X-Received: by 2002:a17:903:245:b0:1fd:aac9:a722 with SMTP id d9443c01a7336-1ff5728b2d0mr26232885ad.4.1722610503790; Fri, 02 Aug 2024 07:55:03 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1ff592aee19sm17920635ad.282.2024.08.02.07.54.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Aug 2024 07:55:03 -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 v8 5/5] net/tap: Use qemu_close_all_open_fd() Date: Fri, 2 Aug 2024 16:54:21 +0200 Message-ID: <20240802145423.3232974-6-cleger@rivosinc.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240802145423.3232974-1-cleger@rivosinc.com> References: <20240802145423.3232974-1-cleger@rivosinc.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62b; envelope-from=cleger@rivosinc.com; helo=mail-pl1-x62b.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,