From patchwork Mon Aug 5 00:31:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 816733 Delivered-To: patch@linaro.org Received: by 2002:adf:e00d:0:b0:367:895a:4699 with SMTP id s13csp1133773wrh; Sun, 4 Aug 2024 17:33:27 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCV8+XlfKeNinYjvE2MqQDle3Ka0L696pYkOmpJ8tMI+aLiZkYzLyxovvU2C40bX4rzzWETIXFdUFShVIhwZPJWg X-Google-Smtp-Source: AGHT+IESgMbs+ePIBOp9DisiLYRaYkuzdhEttTqjfEyluWSCijFn8QCyo7QSkeC8ibc9ysyssFPG X-Received: by 2002:a05:620a:29d4:b0:79e:f932:7ca7 with SMTP id af79cd13be357-7a34f0001a0mr1706830085a.29.1722818007438; Sun, 04 Aug 2024 17:33:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1722818007; cv=none; d=google.com; s=arc-20160816; b=m7QIQ6J07yh/XcoZV4f79oeWawpxbqPoU8WCxlu+Kt3TB2EUBlf39//GVV42z6TJtZ /TiA4EWkSMeLMcdE2+NCC//3E4yto4M2OD+rDm00wqU+IVpT/pfD+m3w2N5uZRz+yTJr WCGjFZoZgCi4qGZCnFMbhCFp/5GywLPbO8Shw+KMX4Nmf1jpcn43M4K31dn1LQiX2lVQ 5mVq+gy1nTSmAwhh3emxYZcc9zqoXtI2jcWbfR8Xe6QryTUBmrOoF8ATLZYZyLZDcsZ0 fcC3/7Ncuio6fz9WWbMmMGFjyVMN8RZHwepe3jeSo4QpH/skEhLw5XQazFHeeJ/GvmFq LpwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=hbxagQGZ9enwm2uWyobspLUjv+kLe0ys5wANtBKsnzE=; fh=KmyENobZuF5nniwS/6+mL/9NgXtPwEgnseCuEPUTGL0=; b=F1iysS/8aePCLohh96qff/dEJl22ZC/UzJErx9pKaoumB2bVP9qvkRRpUdkCWBGUQx 8lcy73Uf722tNa0dhJlhpbwTx+pB1OgPEBjOka9YtIJyw9ANU6wY7hrmdxrm5oTy/xN8 HWkiN2YOqZBg3R4kAR3NvOM5e+kTiD34171za6AXfqko4uR+pxaygGEqIUpC2MZYXNNK ZkYQ2oIE8oHAj5jtGbd3EKI3/UUrvdSblNuB2OgkOwrDOq2GgD8NBWrIgjGs9iv+2fa3 sQT3Rcm1HBSDjd3zUpUE8MyB6RZEAj6nqjk84RXHPtDxrBMhn9Q1oC9MTzqyAWnULglL /GVQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CZ1DcJwZ; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id af79cd13be357-7a34f7762e9si846541585a.518.2024.08.04.17.33.27 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 04 Aug 2024 17:33:27 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CZ1DcJwZ; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1saldd-0007ts-Co; Sun, 04 Aug 2024 20:31:49 -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 1saldb-0007m6-FN for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:47 -0400 Received: from mail-pf1-x430.google.com ([2607:f8b0:4864:20::430]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1saldZ-0002JM-Qj for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:47 -0400 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-70d150e8153so3301865b3a.0 for ; Sun, 04 Aug 2024 17:31:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722817904; x=1723422704; 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=hbxagQGZ9enwm2uWyobspLUjv+kLe0ys5wANtBKsnzE=; b=CZ1DcJwZuqy/nrM8pY5oc5XgukNTvh4OffcFLyd3oxKMZDsUYaGuZFQ3yicXXaaZyk bDFVhKOQ6tgnchLnIqOSP1xSpAot9W1RljpQlUWAcJLpJWivoRO/rHkjl1e/o6eDovCH PlmZecFGjRZZbyzCfGFR964hRQcShryN8dTxYsKVkG/cHUtBXhFXR0ZcqWQE0tEveW20 1FM+hU3DDmUXMEI9YAX88h36u4mXkZmBSoRXOysYkFPqbOfpyBfv6e/Hz1BmeXg06via UMjyDLAiKkYlrb+2GVKqJNVNhF2PxLyvy5VjksJYt1MmAWRWQbS0/KpYEENFnM8joqjI 6Euw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722817904; x=1723422704; 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=hbxagQGZ9enwm2uWyobspLUjv+kLe0ys5wANtBKsnzE=; b=ICOfgLHxGY705U8ryk2DvRz03OXMMZnO2jNu2cCfK8sAJvFGnbVRpGMpC+zmKLKGPu wA9SDkFN1xMvGm60hxMpOdijchlZUrESZ1lZkK0nTVkmgCTz/D5ZKWaiwgfLdLKcyAJm rS11y0wksvqM/VdHue7ea6EN+8UwVtHaI5kO9g2tvotyqwiAUi3AHfmcJOIjKkLy7xKC FHXCwkeVlY4eKyAs7GPKtEimMs3la7MzXOGFzPrI0tkWpKIhwZFphd5otpqhAGzaWzpl L4EeRVWLn0uORDe/1/UEls+ShrdwS4Qag4Tg90IgrA7XC4l1bdcJbZ0cSCoYuLaO4RE5 Homg== X-Gm-Message-State: AOJu0Yx7ZNyNX7D84xnrCEBpjjo7/PG4d3Uhdv/R5RptUi6/E/Kid9+F MuHPbrYi662E4ltNA9W38EaUBEQnQo+MMfx/xjIzyurKU1VCML+9WHCHu6kGPF4iGvBwN5xqhKG /yM+JuA== X-Received: by 2002:a05:6a00:2406:b0:70d:14d1:1bad with SMTP id d2e1a72fcca58-7106d9d9330mr18041602b3a.3.1722817904230; Sun, 04 Aug 2024 17:31:44 -0700 (PDT) Received: from stoup.. ([203.56.128.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7106ed16cb3sm4576038b3a.179.2024.08.04.17.31.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Aug 2024 17:31:43 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= , =?utf-8?q?Philip?= =?utf-8?q?pe_Mathieu-Daud=C3=A9?= Subject: [PULL 2/6] qemu/osdep: Move close_all_open_fds() to oslib-posix Date: Mon, 5 Aug 2024 10:31:25 +1000 Message-ID: <20240805003130.1421051-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240805003130.1421051-1-richard.henderson@linaro.org> References: <20240805003130.1421051-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::430; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x430.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Clément Léger 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 Message-ID: <20240802145423.3232974-2-cleger@rivosinc.com> Signed-off-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); +}