From patchwork Mon Aug 5 00:31:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 816732 Delivered-To: patch@linaro.org Received: by 2002:adf:e00d:0:b0:367:895a:4699 with SMTP id s13csp1133749wrh; Sun, 4 Aug 2024 17:33:18 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCW1DU4+u8XersOW9meIG44JbG3yCMLIcFE+BacZnmyTpfebWTrqy7N4VD5AulGtb1wuP3iUgAgo/+bkIUeYbc7s X-Google-Smtp-Source: AGHT+IHOIpnq1iE0CjsxP/CEI3NhN7/OjwjZIwOW8cpvO2GSR5FKf426lLGWTiXhJbrJOkNCUu0H X-Received: by 2002:a05:620a:4149:b0:79f:1105:be3 with SMTP id af79cd13be357-7a34ef54184mr1359474885a.40.1722817998021; Sun, 04 Aug 2024 17:33:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1722817998; cv=none; d=google.com; s=arc-20160816; b=qjjLiMbVv0xtpZ14MOilcPK4570a0aNTDs4uFR9zm+HvhyyUEL7TC/tv3SxW8ocLsy 3b5P4/7t0bHbQExYq5Yq4FJijPrHC8AJesi6GkxHSZMFfgJUK7Dmyb3YBjsLwiz0BmCK ISosuI1+QUrp6kS/V+bC6fyBS/IJYHeH3/l0rv1Zr8SWt8MHHv76PmLdzZe4w1IImMrC dfHrj3i50me9ogLaZ2k02SrnQ6uXf8AkzzIYDkUzqDdQULEZqguLTEtibmG/zstYZobp t/OQ4DzHB5VFIqpLIzUWOEIqOo8oPIbkfZ18HJxXlYwH1nkwCnpxQPEe6xOqvvMZuLGr OGrw== 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=V6ZlS+VMsFpi/JD9xfA3NQaSnzna6YC/nJ4q2H/FlJk=; fh=j2Ck7JJDkuIhuIM6fXlcQpeLsiCqZ/awsmByeQRTUsY=; b=gxAE5HXhig64LODoixD/9CddDzopIpmw+i/ucI33UJ8eTigeCGhlkIgOdJiojdvL3u EMzixshjDYEphRJJgYze2hPAczLhLx18cEcFkr8FEMOmD317kIXZZcE2gJTNFCZlqx7M aqd8nhGHFbTQV9/a2pBMmwj87RfFoankMOcj+CgFRjL7nb6AT6y4FVPGxsFxO5M25oDz BoZT2rOkNObL62ibKc6oPGQYw/lpYvTP0g8jLjCEoC0DwijTGfR/qkDUsINfXYwX80t4 KqcB4Y71Twg7PlPydE3AqGbvCOOSKIYU5I6Z31CSDrSVE0VURwixffY4z/lhPd7+bXyz iOIA==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="FL00/yi0"; 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-7a34f77588esi874653285a.478.2024.08.04.17.33.17 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 04 Aug 2024 17:33:18 -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="FL00/yi0"; 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 1salde-00080A-W8; Sun, 04 Aug 2024 20:31:51 -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 1saldd-0007vp-SI for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:49 -0400 Received: from mail-pf1-x42c.google.com ([2607:f8b0: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 1saldc-0002JW-97 for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:49 -0400 Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-70d1c655141so7301749b3a.1 for ; Sun, 04 Aug 2024 17:31:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722817907; x=1723422707; 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=V6ZlS+VMsFpi/JD9xfA3NQaSnzna6YC/nJ4q2H/FlJk=; b=FL00/yi0BB+1eNCp4Jl2CuQYIMPbINmyP0hYKnOoLPQ4hkKU9bZv7ko3kxEbpSm2iS YUI/WQub3LhHKooFSF1c7M6S1q1Fb+R1Ub5y4o4JY2he+Pe6YcN5iwTpk9H2M7vjGFv2 F1DRtu9hKhEs4cHvfBLQzX2YlZoWLg/KNv7UnmVod3Q/xrfToWyWccpMrSFdbnmtXIKf vNs8ZQzqOXyDQtmwHqZHdUtGUL160mMoXsK8+9PvuVgpeIDm4TpFxTDcWRRq9Hfav126 icbLcS5KEmQiCQGvMzbFMBGfJqY3+GjL/g7VOYKItWFx6fw85LLstrVw6FxbGmzp1jCy 9Bcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722817907; x=1723422707; 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=V6ZlS+VMsFpi/JD9xfA3NQaSnzna6YC/nJ4q2H/FlJk=; b=f5UDe403Kj7E6uXdpL1iX5PhOjrt18ks7cHXfYkNSq2XbbDTgnxMS0uKTGsE0wq4vq mBNjMj1hnm/nB3cORemNn4wetD23+rgZ6I8dNrPLK3G0TXvRHLJDLHy3k+XovZPwA2+x Z+/nbxvaqJ4gm2qvrVFsHE0IxpuFoEJc43ZJtixqGtuUgsP2nIT/od28gRGCKf8/WTg9 YdyhlxXS0IW+Gm91yRUBUhak0gI2Y6xohI0miSt3rSrkiJ6hzQK6PgStQTC6B0wux36/ rEqW38imEp91nMBPSP5N1pFQOSXzqB4Di8pjLeRKjyVv9iIvUkgQT0qXJUdu61Dxh4gX ctTA== X-Gm-Message-State: AOJu0YyEjF815SCWC4XbdPNOD1MhRkju5O4FT+nddgU1fD8HzbjDFA3i dLndhVfzoQZWpVPGGU1L9VI4aTjsNCThOg+GCMVTpvunAMtCTbhJxwoO2nFuwCnZZseb62HYKhF GFUuZew== X-Received: by 2002:a05:6a00:194a:b0:70d:3a5d:5d4a with SMTP id d2e1a72fcca58-7106cfafe51mr9332762b3a.12.1722817906752; Sun, 04 Aug 2024 17:31:46 -0700 (PDT) Received: from stoup.. ([203.56.128.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7106ed16cb3sm4576038b3a.179.2024.08.04.17.31.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Aug 2024 17:31:46 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= Subject: [PULL 3/6] qemu/osdep: Split qemu_close_all_open_fd() and add fallback Date: Mon, 5 Aug 2024 10:31:26 +1000 Message-ID: <20240805003130.1421051-5-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::42c; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42c.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 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 Message-ID: <20240802145423.3232974-3-cleger@rivosinc.com> Signed-off-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(); + } }