From patchwork Tue Jul 30 12:24:24 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: 1966557 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=PMNeYh53; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WYH5l1sr4z1ybX for ; Wed, 31 Jul 2024 00:04:11 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sYm4e-0003Pq-L2; Tue, 30 Jul 2024 08:35:35 -0400 Received: from [2001:470:142:3::10] (helo=eggs.gnu.org) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sYm4J-0003EL-Hx for qemu-devel@nongnu.org; Tue, 30 Jul 2024 08:35:08 -0400 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sYm3c-00069m-Iy for qemu-devel@nongnu.org; Tue, 30 Jul 2024 08:34:54 -0400 Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-52f00427236so836847e87.0 for ; Tue, 30 Jul 2024 05:33:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1722342783; x=1722947583; 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=zBkVCmIiTiPE9vfd/7IXrJK8wgsdsb9XrAkbKfnUBLU=; b=PMNeYh53VtwnIsw6OAZqd52pwe1JNszCkhDPmaE+ATYUoJhzYQj/roa2UNaB1ycszs iTBMNA+cOUtOJ1QaQok2qss8gceKdfIIT5TPVZ2x/mdYQd//tstguP13X0XqdnXGSRnI Hy2lAqKHAQXL09E7ddP3nUwP7jGUh0aXABQ0Fd2L3SxidlTbabJvfCg0zwtjxiKq8QmE AHOF2Mb0XZ72Zgiys2MG2rA0lOMOeHDeWLKran9FNJDQDaR2JM6TStuY6yeeqC93jYbQ kHhYtV8fSGHu9+IVbERJUsQQBobPNLDIr4nRkJX5dcLZKLgCNM7G7m42HQ6AXEAN12uH 7kAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722342783; x=1722947583; 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=zBkVCmIiTiPE9vfd/7IXrJK8wgsdsb9XrAkbKfnUBLU=; b=ZQtxAMxgjh77uwWLc7uo24nJ29atSpYv8+MyoV7w5b1EgCv1KG8VIyC4LeE+ZzqhfJ HNpvDWYX4q7DVvN8OEum+1V8su+Am8k2SHRlFaXY/ur2NN7g9Zkar+KPiebxNT/D9RhR TFCUbQLsx9tbjKWbMDMwQGCWzMbIHJdXZhCXcJtSB1n9/I3BItUr7rxL8H1j/qd08rtB zW3S6w0d6cv1TxaiYw1nNXd1cZ0sBEk3TKlfKXmb906p6ukvMKluu723tENYNXwU3B05 GiQUKUlJP7YbdOX4PBgDK2N/8D0j5T0PDzU0X5ppWwMcImLkC9F7afCDSJ+i3gya/9q9 aAvw== X-Gm-Message-State: AOJu0Yzrg6kUf39swlCWCePey0RLPDSJxMNuEKcGXDUOeiWCilL98RE+ UP7TVEQ6RGX0l3OHhqJpvY0HXdG/+E/W2HYb9ZPdg9kJ1gx1h66QXvZpsLFnRU93gHbAYY463PI WmQQ= X-Google-Smtp-Source: AGHT+IEniLmR/90yo57Z+so4cK1Xdbg2SW1Oh703uTXr1CvLJDhpsOaUUE/EmU3y1EXMH3giVIquyQ== X-Received: by 2002:a05:6000:400f:b0:365:da7f:6c17 with SMTP id ffacd0b85a97d-36b34e592d6mr7731008f8f.7.1722342282500; Tue, 30 Jul 2024 05:24:42 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b36862549sm14577757f8f.106.2024.07.30.05.24.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jul 2024 05:24: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 v6 1/5] qemu/osdep: Move close_all_open_fds() to oslib-posix Date: Tue, 30 Jul 2024 14:24:24 +0200 Message-ID: <20240730122437.1749603-2-cleger@rivosinc.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240730122437.1749603-1-cleger@rivosinc.com> References: <20240730122437.1749603-1-cleger@rivosinc.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::132; envelope-from=cleger@rivosinc.com; helo=mail-lf1-x132.google.com X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.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 Tue Jul 30 12:24: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: 1966568 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=zMakEwmi; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WYHqM56zFz1ybV for ; Wed, 31 Jul 2024 00:36:47 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sYm4C-0003DI-RW; Tue, 30 Jul 2024 08:35:04 -0400 Received: from eggs.gnu.org ([209.51.188.92]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sYm3t-0003Aw-Tj for qemu-devel@nongnu.org; Tue, 30 Jul 2024 08:34:45 -0400 Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sYm3A-00064g-JA for qemu-devel@nongnu.org; Tue, 30 Jul 2024 08:34:26 -0400 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-42111cf2706so3809085e9.0 for ; Tue, 30 Jul 2024 05:32:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1722342754; x=1722947554; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=amdT7GOZcFRjKcSgOsJ0Xtj6iZAzzmW4Z/k0DIOKYcY=; b=zMakEwmiKJhTszO3eHZ36CJRieWV+oRFbQkGuCMxK4hp2y4JwwSX1MQ/2DwxO1nZcU 4bPF8TnDDCEGOSlMgOdgyG2ATFjHLjRQ790yjOgUwdazbx3x83v0vAec2MgFrJEGswav G7WCSLoos4B1N2yVwOYqWifhw6LSUslJ6wgAS164nC8tqR8hlBqNRjg+aymqdzri6mvG rz596q7o/02ORzCykp9qjTNO0npoQ+ktRKRdHUuyXQUO1Py5gD6fKSasEv29d59Ezb+l 3yJ1WN41hf8Fvr4n1yTDm5S+V6obD2uSTk5CZKH1KXPZX/gThly4TDxWTdKpfuIRBj6c Np8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722342754; x=1722947554; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=amdT7GOZcFRjKcSgOsJ0Xtj6iZAzzmW4Z/k0DIOKYcY=; b=YHezpQ2G52SzO3vwXDMcP/Ot9OAGfgeJLwvjZX9sPjP6sKi5N7tTcWrxpC0H0Z1TLU BV/p1uQufZpff03n0JPM1z4jOPAAJBGNGS6XdEn0uOFs0j1pQXAMQeCPUYaSvd9U95AY +ud1gL9dKRZRa58PZvQpvqA/ZcroCxTLOU0wMou8xsE6ws4ZJ7iSde6S2a5ZlKOkBjK0 z9XdrO2cW/7JVmfhyFu+s9Qxxl7Fc/g9M8vzLSZtdKpFKC/pHs/rC91PDdAoGVKfUl5L 3erjrHK1AVSV0aYaHS+3J9DJ/wH7MagrTa3Om18I4zVdxSnEZzSuTlqiVo0aXSAa08JX s3Eg== X-Gm-Message-State: AOJu0YwoiCua62HVMKgaAqArD8iufn4s6u0cmcELdPazTYjTzACLClgw +twoNCMKSFCXKxKjYERrYxHnOJQ2t6PNceN5M3dlOCdvBZF73keDvneTmZJUQ14pOHIXclg3IHR zh9w= X-Google-Smtp-Source: AGHT+IFgZ57BfhHq/TcQvyfrAkOd7Tb/Imc1aMga0VFlSD5Ch20cr4WVOCdCLeGDAMM5dOUm1OieBA== X-Received: by 2002:a5d:64a3:0:b0:367:4d9d:56a6 with SMTP id ffacd0b85a97d-36b34be17d5mr6845131f8f.1.1722342283446; Tue, 30 Jul 2024 05:24:43 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b36862549sm14577757f8f.106.2024.07.30.05.24.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jul 2024 05:24:42 -0700 (PDT) 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 v6 2/5] qemu/osdep: Split qemu_close_all_open_fd() and add fallback Date: Tue, 30 Jul 2024 14:24:25 +0200 Message-ID: <20240730122437.1749603-3-cleger@rivosinc.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240730122437.1749603-1-cleger@rivosinc.com> References: <20240730122437.1749603-1-cleger@rivosinc.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::334; envelope-from=cleger@rivosinc.com; helo=mail-wm1-x334.google.com X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, T_SPF_HELO_TEMPERROR=0.01, T_SPF_TEMPERROR=0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.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 --- util/oslib-posix.c | 51 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 1e867efa47..a6749d9f9b 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -808,27 +808,16 @@ int qemu_msync(void *addr, size_t length, int fd) return msync(addr, length, MS_SYNC); } -/* - * 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,40 @@ void qemu_close_all_open_fd(void) } } closedir(dir); + + return true; +} + +static bool qemu_close_all_open_fd_close_range(void) +{ +#ifdef CONFIG_CLOSE_RANGE + int r = close_range(0, ~0U, 0); + if (!r) { + /* Success, no need to try other ways. */ + return true; + } +#endif + return false; +} + +/* + * Close all open file descriptors. + */ +void qemu_close_all_open_fd(void) +{ + int open_max = sysconf(_SC_OPEN_MAX); + int i; + + if (qemu_close_all_open_fd_close_range()) { + return; + } + + if (qemu_close_all_open_fd_proc()) { + return; + } + + /* Fallback */ + for (i = 0; i < open_max; i++) { + close(i); + } } From patchwork Tue Jul 30 12:24: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: 1966569 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=2EucOFso; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WYHqw2z0Xz1ybV for ; Wed, 31 Jul 2024 00:37:16 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sYlwG-0007or-6Y; Tue, 30 Jul 2024 08:27:00 -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 1sYlw1-0007np-A6 for qemu-devel@nongnu.org; Tue, 30 Jul 2024 08:26:33 -0400 Received: from mail-wm1-x32d.google.com ([2a00:1450:4864:20::32d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sYlvL-0004k5-0L for qemu-devel@nongnu.org; Tue, 30 Jul 2024 08:26:25 -0400 Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-4280772333eso1821925e9.0 for ; Tue, 30 Jul 2024 05:25:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1722342284; x=1722947084; 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=RnrHjU6Qii1wGn9F38c++27n0hhZOVgu1GhR/v/xyMo=; b=2EucOFsoztdsLMsgd7frNcbv8NsL6AOx0ce8oph8dNT/B9gbeeOHsLnUso0sc7HmW5 ZQYtz6Vpkbvzej9PdcFK39RsQtY55cOBQErO+wdpIE+Gx/dh5D1GMR4EeUkP085frogv rR13sKD8c2t1pXAVdfchNShf7RulPqUb4b1RJ/ZKXxv0Zti4uPdyqG5QAxXEQfjTdyUS 2xxTgsiNVavUjqHfaHXv2cxZTR42NqFkFNYruqQYINGv1MBouhTJ0SjKqT6o7Rys7QAi CtNBpkaQapnm+eWMSWCP7fISgZ5jsq82BNiEKvOPMMrDlDRI9cpMRdTCVTuYcpSPE+b6 oSIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722342284; x=1722947084; 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=RnrHjU6Qii1wGn9F38c++27n0hhZOVgu1GhR/v/xyMo=; b=Hw1II4KuQsr0kW8807pdEl12nZ4GOakXno6VE5Ab9rgqC/OZNXWWE0sqy1XQH9o5JY syX3BZKLwx1UDUMkj+JAnp2FaOv6Y5WPHS80AM/nD1MGfxwyDQ1qhBbek0Tcju/cGLbU Hk6UuV69Tb076cmItmPW95YWBJOHQJFvWfo5pXFQC+DrKrhLJANTIgbaS5pDOBanHLzC LF80RwE2czxSLPDqYz7URPwv/6J4q6//usDJmJEb2RCXeu9soytCOK2X4JlgqH26wqIk Tm0aY38PzCLf8kjWRdk0PfTQdHeBjS47m2gi6sI3YdPlATlTtlMKeWFV0Y2psW8J4Yf9 gOPg== X-Gm-Message-State: AOJu0YyTsNIHorsLeYHBYN39o84+ja7N3TVOnafv3FaRur5uPgu+5n2N rcI+BNire912E0aYVokuEQERwu21s9LGxlu4tGyMhgas1ePK6WYPXqOpblv0+93OVA2pOvZ5dXO xefw= X-Google-Smtp-Source: AGHT+IH5ayAWqqzxkiR2qNjnvOBYMAJ4N2mAzX6sT+f2+sKAqEqeaDDS5iESvYW6iq9Hk4pL8J+jBQ== X-Received: by 2002:a5d:5f8c:0:b0:367:9cf7:6df8 with SMTP id ffacd0b85a97d-36b34cec914mr7329214f8f.2.1722342284118; Tue, 30 Jul 2024 05:24:44 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b36862549sm14577757f8f.106.2024.07.30.05.24.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jul 2024 05:24:43 -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 v6 3/5] net/tap: Factorize fd closing after forking Date: Tue, 30 Jul 2024 14:24:26 +0200 Message-ID: <20240730122437.1749603-4-cleger@rivosinc.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240730122437.1749603-1-cleger@rivosinc.com> References: <20240730122437.1749603-1-cleger@rivosinc.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32d; envelope-from=cleger@rivosinc.com; helo=mail-wm1-x32d.google.com X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_INVALID=0.1, DKIM_SIGNED=0.1, T_SPF_HELO_TEMPERROR=0.01, T_SPF_TEMPERROR=0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.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 Tue Jul 30 12:24: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: 1966604 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=YbQO36Cl; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WYJTt5lvNz1ybV for ; Wed, 31 Jul 2024 01:06:40 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sYoQ7-00010L-4y; Tue, 30 Jul 2024 11:05:47 -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 1sYoQ5-0000yx-2e for qemu-devel@nongnu.org; Tue, 30 Jul 2024 11:05:45 -0400 Received: from mail-lj1-x234.google.com ([2a00:1450:4864:20::234]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sYoQ2-00041J-Ek for qemu-devel@nongnu.org; Tue, 30 Jul 2024 11:05:44 -0400 Received: by mail-lj1-x234.google.com with SMTP id 38308e7fff4ca-2ef298ff716so8604761fa.1 for ; Tue, 30 Jul 2024 08:05:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1722351939; x=1722956739; 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=MD3SZjQaELpHhaRRsED0fgSL5OizI87LWpY75t1A8Zw=; b=YbQO36ClSzLyvQLjUcQpdnXnnPoq6A/FD0NDRyT+9Wwy+9zoImhAXqkbOIoIB/YqWH O6IpmOi25bRGzrG8kJYfdDeARXzTilZdSs0AG7BWIzr1toQDfGoaq9qFyQWJLd11hQP3 n1wsx+kwvzzDj/52/+j/bY0eRwlbEHZ47IHN0PC08QTYAxTQruubghHCqriWrg97v9vX APWYt683uuItJpWAL1Ex9rXT/c0tl63J42sxqa8irYsOtjsBGPPK5KasMFIF2bHLUqnS BCUIwjjWu9xKLQVeqS8b+F5U5J7/KTSSG8DF8BDR0eQuTz5KyR2zvgtu8xF2/C35QtE9 xjyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722351939; x=1722956739; 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=MD3SZjQaELpHhaRRsED0fgSL5OizI87LWpY75t1A8Zw=; b=Ci0IrINsOQHqa9wZppv8vFS7MtdIa92wxWDRnjrY490h6hxOcI7M5MUKxYv0GMZ6XY 3flXkuOoDUC4v4ezy6N7U+R5RP/tE00ZHjVuvbNPtmfP64eJbC4GAkjX0l+X2s66x1ou ilTKlBEcoPQpCK8QNXDctZVUd8cWC+oNU7XTr2AE8G22Ycp8jhC0qCDuUEPTFaRkSuEt ujwpAbcp28wpWRQ62tBsLEfTav0/bdmDEkTyqtoojXf5DlQ9IjKYP0WWJbyxiKCOSFef hvFx/yyRrarvhYOc46I7jm7ZUDHzFlBTYOPd7S4AEbPkPAQpm4WgSqeW1OCW22BSUpms Uznw== X-Gm-Message-State: AOJu0YzPDqKdfoq5nwHek2IrojGZVko6Gth6+4eYJaLWqF1wJLQlIKJt ajT363YkhLqLQTY9Y1NjHS/jViuHHhbEPePKVH49ToniGQKRryZM/Ad+9Wwx5CHeMFbHu2qhThn Mqpk= X-Google-Smtp-Source: AGHT+IHriaT1wvvutTP7Qzjy0U75fJZvDUaiSh3bdM3D+wHZsXRb9cA2f7bzk4c+4JC/Js4jE2GHRg== X-Received: by 2002:a5d:5f49:0:b0:360:8490:74d with SMTP id ffacd0b85a97d-36b34d240a7mr7415237f8f.5.1722342285039; Tue, 30 Jul 2024 05:24:45 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b36862549sm14577757f8f.106.2024.07.30.05.24.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jul 2024 05:24:44 -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 v6 4/5] qemu/osdep: Add excluded fd parameter to qemu_close_all_open_fd() Date: Tue, 30 Jul 2024 14:24:27 +0200 Message-ID: <20240730122437.1749603-5-cleger@rivosinc.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240730122437.1749603-1-cleger@rivosinc.com> References: <20240730122437.1749603-1-cleger@rivosinc.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::234; envelope-from=cleger@rivosinc.com; helo=mail-lj1-x234.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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.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 | 99 ++++++++++++++++++++++++++++++++++------- 3 files changed, 91 insertions(+), 18 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 a6749d9f9b..e7bffaea16 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -808,11 +808,14 @@ 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; + bool close_fd; DIR *dir; + unsigned int i, skip_start = 0, skip_end = nskip; dir = opendir("/proc/self/fd"); if (!dir) { @@ -823,8 +826,31 @@ static bool qemu_close_all_open_fd_proc(void) dfd = dirfd(dir); for (de = readdir(dir); de; de = readdir(dir)) { + if (de->d_name[0] == '.') { + continue; + } fd = atoi(de->d_name); - if (fd != dfd) { + close_fd = true; + if (fd == dfd) { + close_fd = false; + } else { + for (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,36 +859,79 @@ 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) { #ifdef CONFIG_CLOSE_RANGE - int r = close_range(0, ~0U, 0); - if (!r) { - /* Success, no need to try other ways. */ - return true; - } -#endif + int max_fd = sysconf(_SC_OPEN_MAX) - 1; + int first = 0, last = max_fd; + 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 > max_fd) { + last = max_fd; + /* + * 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 = max_fd; + } while (cur_skip < nskip); + + return true; +#else return false; +#endif } -/* - * Close all open file descriptors. - */ -void qemu_close_all_open_fd(void) +void qemu_close_all_open_fd(const int *skip, unsigned int nskip) { int open_max = sysconf(_SC_OPEN_MAX); + unsigned int cur_skip = 0; int i; - if (qemu_close_all_open_fd_close_range()) { + assert(skip != NULL || nskip == 0); + + if (qemu_close_all_open_fd_close_range(skip, nskip)) { return; } - if (qemu_close_all_open_fd_proc()) { + if (qemu_close_all_open_fd_proc(skip, nskip)) { return; } /* Fallback */ for (i = 0; i < open_max; i++) { + if (cur_skip < nskip && i == skip[cur_skip]) { + cur_skip++; + continue; + } close(i); } } From patchwork Tue Jul 30 12:24: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: 1966536 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.a=rsa-sha256 header.s=20230601 header.b=PTgFDjdC; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=patchwork.ozlabs.org) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WYFFy4stcz1yYq for ; Tue, 30 Jul 2024 22:41:08 +1000 (AEST) Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sYlwb-0007ur-Ap; Tue, 30 Jul 2024 08:27:13 -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 1sYlwE-0007tM-7k for qemu-devel@nongnu.org; Tue, 30 Jul 2024 08:26:51 -0400 Received: from mail-wr1-x42c.google.com ([2a00:1450:4864:20::42c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sYlvd-0004kB-O4 for qemu-devel@nongnu.org; Tue, 30 Jul 2024 08:26:39 -0400 Received: by mail-wr1-x42c.google.com with SMTP id ffacd0b85a97d-3684c05f7afso512264f8f.2 for ; Tue, 30 Jul 2024 05:25:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1722342286; x=1722947086; 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=SZadAk3fRT0RL0HiFb35pVw+CCBNLGILEP7kV4cE7OA=; b=PTgFDjdCXvm9+SAgyfgSV6fJI+4ZSNLDyK8/Wgsnu9FWYcPBf54IieGku/47nm1txW /ythVRJq0dSkh0dt04/PgzpSaiCeJD+XPCzIsydmOgArQH6etg5YGmi6W6HQYrtp4nLe DimlM+h98WQHLNQN/7uyI0oDFRygTSGxlN5QzwcWfKX0JOieXLNk6b/M6YfsxJGTqSoL YEpEj460nywJpkChcKoy+KDSZELoz+kuI9bIZmmFEbAsiPUgLxBHc207C8wyPHVNFtVv cZLm8R445h/ZN0pzGH3t4PTD4Xc+uICG89KH0mXXXDFlQGvCeozEay1OzgaB5+GZH6I9 G55A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722342286; x=1722947086; 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=SZadAk3fRT0RL0HiFb35pVw+CCBNLGILEP7kV4cE7OA=; b=nAYYn9xH7tgv2xfid/x5gIruUJksJsxvTeQ6WnXPzZLgUTwraJCclyd00Rdss/WRg5 eg0J+6VAEUAnA64Z00SPg8XGvAIoYnobP+aeCE7B5KjQh6yl1+5bi//cVV1xGmU5IvOq X2xhCFrtgORIITfC6w0Ec02mZiZy7Se+eknCbKyXx2YdpXJjmZgs6nAf57yLZ+qq1/1L MS9uWUw42fEEG0/chkqKd/dmp//tCLEZJ6oRN4wWiM7cT7UAtTvTzk9uO/KzSQQ2hKUu S5u6K+cp7bc5FsFD+UNDnMo7K29O+c8BoobxKuSfiYSvxC5GQ9s5vaQS6qpVOSa22xiH lkJg== X-Gm-Message-State: AOJu0YxWwL4yDFKcLO8O8nTNa/2CSIBovOW7k9nvQfravK1jVQ9fvEKV F3yUJ45eJ318bE3m0Rf2SGvCktNpKQBu+iZPra/XW1e6TOr+Z2W6Z/r9L5FvA/2zdMew9Aa/by7 DRL0= X-Google-Smtp-Source: AGHT+IFkn9lZxu6El/M+4N1dNXSCVOgkw+7QGXdLiqhLVF8fsQoSVj1CBAL9j5bEoE7vA0JjE0bDog== X-Received: by 2002:a05:600c:3b87:b0:426:668f:5ed7 with SMTP id 5b1f17b1804b1-42805700160mr75947875e9.2.1722342286030; Tue, 30 Jul 2024 05:24:46 -0700 (PDT) Received: from carbon-x1.. ([2a01:e0a:e17:9700:16d2:7456:6634:9626]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b36862549sm14577757f8f.106.2024.07.30.05.24.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jul 2024 05:24:45 -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 v6 5/5] net/tap: Use qemu_close_all_open_fd() Date: Tue, 30 Jul 2024 14:24:28 +0200 Message-ID: <20240730122437.1749603-6-cleger@rivosinc.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240730122437.1749603-1-cleger@rivosinc.com> References: <20240730122437.1749603-1-cleger@rivosinc.com> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::42c; envelope-from=cleger@rivosinc.com; helo=mail-wr1-x42c.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, SPF_HELO_NONE=0.001, T_SPF_TEMPERROR=0.01 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.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,