From nobody Wed Feb 11 05:13:35 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1686980309482474.68022521904527; Fri, 16 Jun 2023 22:38:29 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qAOdj-0006Jf-CY; Sat, 17 Jun 2023 01:38:23 -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 1qAOdW-0005pL-Mw for qemu-devel@nongnu.org; Sat, 17 Jun 2023 01:38:14 -0400 Received: from bg4.exmail.qq.com ([43.155.67.158]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qAOdT-0006h3-B1 for qemu-devel@nongnu.org; Sat, 17 Jun 2023 01:38:09 -0400 Received: from ubuntu.. ( [111.196.130.174]) by bizesmtp.qq.com (ESMTP) with id ; Sat, 17 Jun 2023 13:36:39 +0800 (CST) X-QQ-mid: bizesmtp73t1686980200tmc8uug1 X-QQ-SSF: 01200000000000E0G000000A0000000 X-QQ-FEAT: YHTLUubWl25IZCNnJ0AgRvrjw5CKKED9w+RGLCmZdhx6ai2AkIQMFysmZtPm9 xJF7FwaLwGjU6HRiD7D/qTsq4VF2WePudXD/4SGe47dDLT7QOdPuI9JEJJbNoLlpTvw7YxZ oSB37Rvnm2RkUkdSyZyB7417fLZ+jnr1yNPgogkEN+kYJZ5J91HYS85ar6bA9+Dzi2uy8yV 2BHIIomEtgXxxTTv9J2G/qogP/xPkODdjfPAOU5GPTOc+rIPyCGY2xEhjG3Dy0hge2PnSbW 20hQCjRkABtBcXnzTrtUC4xNLt2+8bdrYZctm5ay8/0jwPCdHtQSYC5nzbV9IphttQ0X9w+ VvNMTEZ2VqXIKyiQD9P11jz1rPs9Aghb/VpIiutIW8EZBytJTU= X-QQ-GoodBg: 0 X-BIZMAIL-ID: 16536509347809008363 From: Bin Meng To: qemu-devel@nongnu.org Cc: Zhangjin Wu , Claudio Imbrenda , Juan Quintela , "Michael S. Tsirkin" , Paolo Bonzini Subject: [PATCH v3 5/6] util/async-teardown: Use qemu_close_range() to close fds Date: Sat, 17 Jun 2023 13:36:20 +0800 Message-Id: <20230617053621.50359-6-bmeng@tinylab.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230617053621.50359-1-bmeng@tinylab.org> References: <20230617053621.50359-1-bmeng@tinylab.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:tinylab.org:qybglogicsvrgz:qybglogicsvrgz7a-0 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=43.155.67.158; envelope-from=bmeng@tinylab.org; helo=bg4.exmail.qq.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, RCVD_IN_SBL=0.141, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1686980311177100003 Content-Type: text/plain; charset="utf-8" From: Zhangjin Wu Based on the old close_all_open_fd() of util/async-teardown.c, a new generic qemu_close_range() has been added in osdep.c. Now, let's switch over to use the generic qemu_close_range(). Signed-off-by: Zhangjin Wu Signed-off-by: Bin Meng --- Changes in v3: - limit the last_fd of qemu_close_range() to sysconf(_SC_OPEN_MAX) Changes in v2: - new patch: "util/async-teardown: Use qemu_close_range() to close fds" util/async-teardown.c | 42 ++---------------------------------------- 1 file changed, 2 insertions(+), 40 deletions(-) diff --git a/util/async-teardown.c b/util/async-teardown.c index 7e0177a8da..e102912f3f 100644 --- a/util/async-teardown.c +++ b/util/async-teardown.c @@ -29,44 +29,6 @@ =20 static pid_t the_ppid; =20 -/* - * Close all open file descriptors. - */ -static void close_all_open_fd(void) -{ - struct dirent *de; - int fd, dfd; - DIR *dir; - -#ifdef CONFIG_CLOSE_RANGE - int r =3D close_range(0, ~0U, 0); - if (!r) { - /* Success, no need to try other ways. */ - return; - } -#endif - - dir =3D opendir("/proc/self/fd"); - if (!dir) { - /* If /proc is not mounted, close fds one by one. */ - int open_max =3D sysconf(_SC_OPEN_MAX), i; - for (i =3D 0; i < open_max; i++) { - close(i); - } - return; - } - /* Avoid closing the directory. */ - dfd =3D dirfd(dir); - - for (de =3D readdir(dir); de; de =3D readdir(dir)) { - fd =3D atoi(de->d_name); - if (fd !=3D dfd) { - close(fd); - } - } - closedir(dir); -} - static void hup_handler(int signal) { /* Check every second if this process has been reparented. */ @@ -84,6 +46,7 @@ static int async_teardown_fn(void *arg) struct sigaction sa =3D { .sa_handler =3D hup_handler }; sigset_t hup_signal; char name[16]; + int open_max =3D sysconf(_SC_OPEN_MAX); =20 /* Set a meaningful name for this process. */ snprintf(name, 16, "cleanup/%d", the_ppid); @@ -92,9 +55,8 @@ static int async_teardown_fn(void *arg) /* * Close all file descriptors that might have been inherited from the * main qemu process when doing clone, needed to make libvirt happy. - * Not using close_range for increased compatibility with older kernel= s. */ - close_all_open_fd(); + qemu_close_range(0, open_max - 1); =20 /* Set up a handler for SIGHUP and unblock SIGHUP. */ sigaction(SIGHUP, &sa, NULL); --=20 2.34.1