From patchwork Mon Aug 5 00:31:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 816726 Delivered-To: patch@linaro.org Received: by 2002:adf:e00d:0:b0:367:895a:4699 with SMTP id s13csp1133601wrh; Sun, 4 Aug 2024 17:32:31 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCVOd5chavZmCy18/urEptgqB69hk6WZXh1V05GoMwy5dkMFX2QFypnJFI6TGxvFVeOt3Y+wkZh0tA57U4qsZPsR X-Google-Smtp-Source: AGHT+IF3zLaqK7aLSVacD1C9CR7LjCGr/eCoqRedrXPk9vmO+BGME27Zy689MK2pfmedqZZyq6TF X-Received: by 2002:a05:6122:3b0f:b0:4f5:22cc:71b9 with SMTP id 71dfb90a1353d-4f89ff6c45emr12046174e0c.5.1722817951685; Sun, 04 Aug 2024 17:32:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1722817951; cv=none; d=google.com; s=arc-20160816; b=sUag/ye0HT82yAuTDXBsEU043ZFsee4P1Wrx80JbXTaqeXEmym8Z0JXVoEZHJ8K4QD b7s2xhLXDTKQqye9Hu3M4RrMlO+42OW7XGbVGZHtpkDQI1TWuUVOeCsB/fVckcYr8rkl /5PmJD9gzwcLV/V2J+ZoyG40jBJ7u85yxMn1qnSGWjRSAqQwehOc4kJznn1hsFlQdgdp N6nWrUNAXXqhC+bQ2mcGPHsCvYitjGYkLo6v2tzWnsVlCLXtuL+opIGryAAae2gBaMiv YvSgCiub4FwKKEinTv12TJKPduutNEtil+Ve31XDxwkZEluVRYQ+jdg/i8B4uUlSjPBL MrJA== 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=8WJQORCpqGoeFxGSwBWz+2gItcHfSAbHK8FGjbSdfpM=; fh=4RqdLMIrR7qTEUmnmFFI+83EHhKCrT2Fs4rC7TcdDhw=; b=qPCqqshyRcIcBLNn56fLVCXIo2X3WhZrd0Gd75p0Q6elN+Je1EqrV2FgZbWHeUSJcw IwjgBBW2dzbRtWOoiP8GtF0EGuz7Jgcj00eEk8MS00eWcT9KFjra8sVKShaVIo6NZwOu nfHuzNhASybyUYsHTvDdKvnpjMZ3/7xLYBQXAMNRzsyYDbNTQLXOIYWIxhvG6AEW42dA 9ySTpQA3y2aWlxobQsEmIbdUNBakR06dJJBlfks8+0U8BEQziHdspOcq4gKRTpwiRXe0 20+jl2TSTJxt4uzCsD8iSeHOOjB+0WbsFowSPvea4ep16ZzWDrN0kF3PnIRys0u0brVr SVZw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=g53sSO+R; 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-7a34f6ce7c3si868290685a.145.2024.08.04.17.32.31 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 04 Aug 2024 17:32:31 -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=g53sSO+R; 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 1saldY-0007Z6-8Y; Sun, 04 Aug 2024 20:31:44 -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 1saldW-0007Rl-Fj for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:42 -0400 Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1saldU-0002IY-TS for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:42 -0400 Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-70d2b921c48so7633708b3a.1 for ; Sun, 04 Aug 2024 17:31:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722817899; x=1723422699; 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=8WJQORCpqGoeFxGSwBWz+2gItcHfSAbHK8FGjbSdfpM=; b=g53sSO+RsOfxs0qHyP53M+L/fOnR+O3Ag8kiudkpuAHWWoLv+Ig+e3mwbYt7k+o8TO TRsoRbzOZghwfMztIjKkVM5vm6B/HEcU8sgJ8coRZlAn4/E7eCwu5/LpmjWK0b7C7uZc KSSrgDR7VjKf1dEroFhP5iLQNxP6pEbS35f+LNCuTY2rQAPrpvYDM2q9+Kt2FoODsj2P htcYfBiH4wURX6R+UQ5P6cgvVmnNZm9b7KJKjo58QKnoXV9GsQ4PgO7zEVQeHflDPWmd 1Dgsq+Ku9ziq+e0SFiIB6b7cLHp7k1vw31ZMY+tbqZ0ucw1q4KtLodSBZlU0ms/nX+49 0E0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722817899; x=1723422699; 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=8WJQORCpqGoeFxGSwBWz+2gItcHfSAbHK8FGjbSdfpM=; b=D9UDyadZCHeBYZs6YXW8Dpe7G+qYGVbn7wd/OPVv1TjqPVxEvJQVTbNgvf7fau084C KkdC7bwqM9OPUw504SbqUeENUuQJAgWiXdgvlMulNi3/mqEezMqk70p0E9o6/LYOG83A gQZVXskX2tzqYyiTJ/ixttcAtYhw+396j+xU4mAsVzyFiiV864QSjBlrtoc9BBlqMlTN ulJLbEll5EtBABTQ53K8yxgjyVC5QYrv5CCX1u2R3QdAbOynsqML148cz+fY/JdmCvCm +BkYtd1iaDv3wkG1eskmH4QjK7kkDKtG/6W9yVYvH2amZLUnOsnpW+NjizK5oZ/ui2cw KGOA== X-Gm-Message-State: AOJu0YzOJxAVXKhb2Kepo+47SgQlBKBRJgH/63Avq/fvk4Jjv3eRPoIa p0X9zKGOO6BRGvKM5jij+VLlcWxfsn8Lkure1pt0RI4021yRsGgRxcbEEHT5HliZikHy1/CKq6W Znn0hBg== X-Received: by 2002:a05:6a20:7f84:b0:1c4:8da5:5825 with SMTP id adf61e73a8af0-1c699429e34mr10679109637.0.1722817899175; Sun, 04 Aug 2024 17:31:39 -0700 (PDT) Received: from stoup.. ([203.56.128.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7106ed16cb3sm4576038b3a.179.2024.08.04.17.31.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Aug 2024 17:31:38 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: Ilya Leoshkevich , qemu-stable@nongnu.org Subject: [PULL 1/6] linux-user/elfload: Fix pr_pid values in core files Date: Mon, 5 Aug 2024 10:31:23 +1000 Message-ID: <20240805003130.1421051-2-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::42f; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42f.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: Ilya Leoshkevich Analyzing qemu-produced core dumps of multi-threaded apps runs into: (gdb) info threads [...] 21 Thread 0x3ff83cc0740 (LWP 9295) warning: Couldn't find general-purpose registers in core file. in ?? () The reason is that all pr_pid values are the same, because the same TaskState is used for all CPUs when generating NT_PRSTATUS notes. Fix by using TaskStates associated with individual CPUs. Cc: qemu-stable@nongnu.org Fixes: 243c47066253 ("linux-user/elfload: Write corefile elf header in one block") Signed-off-by: Ilya Leoshkevich Reviewed-by: Richard Henderson Message-ID: <20240801202340.21845-1-iii@linux.ibm.com> Signed-off-by: Richard Henderson --- linux-user/elfload.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 0d4dc1f6d1..b27dd01734 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -4102,8 +4102,7 @@ static void fill_elf_note_phdr(struct elf_phdr *phdr, size_t sz, off_t offset) bswap_phdr(phdr, 1); } -static void fill_prstatus_note(void *data, const TaskState *ts, - CPUState *cpu, int signr) +static void fill_prstatus_note(void *data, CPUState *cpu, int signr) { /* * Because note memory is only aligned to 4, and target_elf_prstatus @@ -4113,7 +4112,7 @@ static void fill_prstatus_note(void *data, const TaskState *ts, struct target_elf_prstatus prstatus = { .pr_info.si_signo = signr, .pr_cursig = signr, - .pr_pid = ts->ts_tid, + .pr_pid = get_task_state(cpu)->ts_tid, .pr_ppid = getppid(), .pr_pgrp = getpgrp(), .pr_sid = getsid(0), @@ -4428,8 +4427,7 @@ static int elf_core_dump(int signr, const CPUArchState *env) CPU_FOREACH(cpu_iter) { dptr = fill_note(&hptr, NT_PRSTATUS, "CORE", sizeof(struct target_elf_prstatus)); - fill_prstatus_note(dptr, ts, cpu_iter, - cpu_iter == cpu ? signr : 0); + fill_prstatus_note(dptr, cpu_iter, cpu_iter == cpu ? signr : 0); } if (dump_write(fd, header, data_offset) < 0) { 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); +} 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(); + } } From patchwork Mon Aug 5 00:31:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 816730 Delivered-To: patch@linaro.org Received: by 2002:adf:e00d:0:b0:367:895a:4699 with SMTP id s13csp1133672wrh; Sun, 4 Aug 2024 17:32:55 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCV0wsLtM3PfVs4oDmWuVZnpXjC3uU1nBMJqHSmojfUcsK7Td4io85tS8dI9pO/vQj4Q9curBSJyVyddyaz6Ritd X-Google-Smtp-Source: AGHT+IFNs6YdVeMjykNK1Us8X27sC5qGwpNEcOC/3J8SLXA8xptA41safAMPoroiRoaYCYKJiKdD X-Received: by 2002:a05:6214:5781:b0:6b5:d90d:ea3c with SMTP id 6a1803df08f44-6bb98378f87mr129513246d6.3.1722817974765; Sun, 04 Aug 2024 17:32:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1722817974; cv=none; d=google.com; s=arc-20160816; b=jgNpILExnINKSuLzaM4adwVrVehl/aZ39Rl7/8iKXzv7VoiCa06iVv5b1yzqwb/JIy yNO1Mkmkcm8J0do1tKFRaJeKJibsKYzt9AKZ8fxYS+p4L8LXO1rf4N+tEC1BS6xrPztZ eKVajPZeM7/8ZL/9/dddXOBvl5hHryCoUhmDhiSb9ejhG+y9tu6TWpxn2nuC6v7PsaGR y9F5NJsSC+ZYnyeGlEeKv3YSOGcTS15zhBEaldkvYVBjP5Nq4qtMdRTNPpO5ohhLwQyn bBiY2iQF6unk86F6+HSf/uMF62OU8cQzLLm5YONttgQeK4D8aH1Y3Bkc5icLPgDeyAlg FcAw== 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=W8wloA238Rk9C1XCBXsRbgtFn/P+Z4RpbJw5FHD4LSg=; fh=KmyENobZuF5nniwS/6+mL/9NgXtPwEgnseCuEPUTGL0=; b=BqakQEVTy+hB6jmwI+4DM+eiHFhK5aIvf3btArZQKLaKVwhcpKDb5iHun/mfCS7Uq6 FtNe2b78f1JLpo5viwSkI5wYOSrlEVlxjJF7cP4R2XTYEfey/ttKUsCehR39nq01r0KV QHXFY59nb5cg3itO3ismF9ojqcDLT40FBuWT/zFSoPR3DT/d7KX6BM5jyTGKfKfImdjk 75S8L1Vydm6b+tpX/SY8bEGgxmEj5sHAYWWOL3fW1sCqnj0rki4j/ml50VER6qGz/7V8 4zI9GPuYukCsMJ914XLq5EikHTbV6DsOn86JpJFu7FrU20sI0sdNd29V5KjuMlRSi58c 1Kvw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=no9g+cF7; 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 6a1803df08f44-6bb9c840f17si80108326d6.300.2024.08.04.17.32.54 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 04 Aug 2024 17:32:54 -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=no9g+cF7; 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 1saldi-0008Ef-IT; Sun, 04 Aug 2024 20:31:54 -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 1saldg-00086B-FQ for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:52 -0400 Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1salde-0002Jp-SH for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:52 -0400 Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-70d2b921cd1so8973281b3a.1 for ; Sun, 04 Aug 2024 17:31:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722817909; x=1723422709; 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=W8wloA238Rk9C1XCBXsRbgtFn/P+Z4RpbJw5FHD4LSg=; b=no9g+cF7i2SPDvqzbjzzpwImCOBFaFlePq1zHQ9CMzNCoa9p7SpRW4SPLOtFeCrPqP 40hI1xHC4cageNpmONS7xeIh4woZ39eVALO1BYfMOGdNGvaKOxM0bKjiEGJ5mZh8FSzT xiZYQuyTXvKwX9aeWGe1ND42zwPeBRJPnnkLg2hMMFcntpODWdOSPhpdG+OSKuSIXW34 xiK4HvUE46oUrP+qCnP1z7x6bpZ1mDqLROSJQ2jOJztbg3JPO5qp4Y8r4aBFdamJjqqp 0YC9fUPkK3DpX5EKf5TGC4QEYeSlfs9ahssUzmd4WwIhQFzjc2F6omEQCFv4Zl4sIEQ+ NfVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722817909; x=1723422709; 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=W8wloA238Rk9C1XCBXsRbgtFn/P+Z4RpbJw5FHD4LSg=; b=Fdin+vjr8L/DyEoZ/pjscXJ7OiFKgR85zFbOgB8djfmor8H6TDDIwnCjHTG2oCEnfB YTOx7FH8SnKfiAkAIrUZ2g1xm/SDo7L38y5oJptSQSN+UPhxhUh9gHb4pVntJYsLVbr6 e9SaT3sZTVdFQ+lQThDccdtSBtSTzxM+Rb3l6Rabxc7Dfw10ctpO6P5z+YAdc7fvK3F9 aipLinyLqO2dRtzQeojjJhG2CRqKTsPD+vmDaKpACnidUrvF7OxiZ+W6NOom9gXULTDe g7oaIW6p12xmvW29CTQN7WLLOw6cevURV2aSJpejedAHPz9Ly/DI3FWenQTHB8wvnJKH 3f0g== X-Gm-Message-State: AOJu0Yz/WPsMIkHlAHf0rlU4BIn+NpfK8qzCG7YLAgzWuaB2E8G9vda5 af61hgFcxbBy4kH4/TGT/V35+94oATTKPYeIL6dAGaw+Mkbu2edVrOMmsxK6qH/I3BVJBlZY/yX fAhY0nQ== X-Received: by 2002:a05:6a20:729c:b0:1be:c5d9:5a19 with SMTP id adf61e73a8af0-1c6995c9d4fmr16817280637.24.1722817909352; Sun, 04 Aug 2024 17:31:49 -0700 (PDT) Received: from stoup.. ([203.56.128.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7106ed16cb3sm4576038b3a.179.2024.08.04.17.31.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Aug 2024 17:31:48 -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 4/6] net/tap: Factorize fd closing after forking Date: Mon, 5 Aug 2024 10:31:27 +1000 Message-ID: <20240805003130.1421051-6-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::42f; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42f.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 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 Message-ID: <20240802145423.3232974-4-cleger@rivosinc.com> Signed-off-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 Mon Aug 5 00:31:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 816731 Delivered-To: patch@linaro.org Received: by 2002:adf:e00d:0:b0:367:895a:4699 with SMTP id s13csp1133701wrh; Sun, 4 Aug 2024 17:33:03 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCU4nAPqVfeXb6M9P6tdvX5P4aHkscLnXvyxUjGNBkJm4iPw+IvNfQPcQ0f0g+VXz3js6xffQMBHlOOE/Rj8p3lH X-Google-Smtp-Source: AGHT+IFm2b5ysZU94c7w+kB5tlMhlSLM6MyTLt/NFydyfQZNL9SWhBHgabEy08l0od7sJKTYSglM X-Received: by 2002:a05:620a:c55:b0:79e:f74a:82a6 with SMTP id af79cd13be357-7a34ef13b21mr1139638885a.26.1722817983408; Sun, 04 Aug 2024 17:33:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1722817983; cv=none; d=google.com; s=arc-20160816; b=hxcp9pfq9Imsu3L40wgCbftN38eQyHxTTSmDdDtwXjI1wYn+FMBnBFmaqdmsbx7TBz oIOOKLI6Otc8HbJ7+DVPN1PgVIFj3S66wUP/bzPQIQrxxEAr3UNLECwabFvpwPwsafkA A+4aKKf44tMFWAbTsmHEDF2/00q9rZf2kwWXRun38TD2StFMCSwsGfOBI9dW3wqRb/SV bzAuL/yGgJlW/PJE09VVE9TdUwSINXWC1HKjydRVI6KlmUDNPuKZmmpq1jITo2VedlRG mZoBKzvIzeVFiaeWwLxzwRvnrcKYELiiKkAWi8onsN+Fx8VdjmJZBNKbH2qLQ5saRbc0 cQ1A== 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=9loKzQW0fz51/k/WMOHKeqW5+iqLnIhl+VnJpSdO5ZI=; fh=j2Ck7JJDkuIhuIM6fXlcQpeLsiCqZ/awsmByeQRTUsY=; b=tVpOZAK9BbrZofoVAEDRCQs9VZz4t1mN0xDMLE6eBhOjus5G1Jl4qBiAC62EJOcVnF eLIfDCMoNHr75dpeRHKjlChF5ngNQax8RcBXwVOt2IX0MzjY5UkPgzmyeZoGpzSQgxGb YfSTRFxfeqh72ZZ8GjXqTDqOLGjw97o7hRHoCbsxDRYeAdODPvOccrOjLGDmDzvaCo6E 8iS8Tss9AzUqrHmi1oW7IOqGDtWCSkB8AaEG9CrnIp+8wITkZ62gMNG5JCNpVeAMFFJw a3yYlhXvsyXRmVYRdzfizIRnbDxXVhWHHrGIf1tkpJvQsmdxPzCEn5pFwja+eLubazJt qVDg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=z8JohOe5; 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-7a34f6d1893si876470985a.1.2024.08.04.17.33.03 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 04 Aug 2024 17:33:03 -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=z8JohOe5; 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 1saldk-0008Kp-44; Sun, 04 Aug 2024 20:31:56 -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 1saldi-0008GK-Vs for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:55 -0400 Received: from mail-pf1-x431.google.com ([2607:f8b0:4864:20::431]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1saldh-0002K0-74 for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:54 -0400 Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-70eb73a9f14so7496153b3a.2 for ; Sun, 04 Aug 2024 17:31:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722817912; x=1723422712; 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=9loKzQW0fz51/k/WMOHKeqW5+iqLnIhl+VnJpSdO5ZI=; b=z8JohOe5JP8EdqkOEMIzg7roJLJ9w0DnhGtYH8RzmrDAWxQvQ4k1XqMLV4P200wwa+ S6DyzEpIMYkiJHO4q9+EAUbdyo7bHnQZx2dlYZHArmmGI20cvOy5TJOdeAz7A0j71XAy JRsxlT6YiXG4iyw6wC8dUKoBbecSUdR2LplJ43k6Ov96TGhi2dBSvA7liIt0DECxi6HC aVLGAJG2HS9HCz8q59h/76IO6DPGFqiqtTNAyX+Fp+iQhZCetuDygh7x1NaKpn3TDmt/ dBFLDyd2cwpYnkf59vP4f0/5M+lYHL614CaagUD0z3SJukXW6X99nnjvYs+cJbk06NnQ WCFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722817912; x=1723422712; 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=9loKzQW0fz51/k/WMOHKeqW5+iqLnIhl+VnJpSdO5ZI=; b=nXUNWaoQNbu1N8j3dOBOoZ+uOBXvSQemn1M3bgNXWD6ovOqlSVyw1mDHxfL3tOcag9 c9/0SpcgtgQQvCBab7TUMRLxU3PIq27gjOyfeSgRcQN1sppipaFZjjKNvyaajdBlXC83 0XURflMuq/6b4iIHz7inxub4m2qrQMpkWmoA75jOMTKNGmpcbLDf9aC00fpiulCcDOyh PYu1Yi2VJPpLjSf0kr+uQf3fv4R2x6uggyB7TZhPge7GPE5Vw5grlhlD3LDycM/HKCc3 8RhxLrozBwnhTbNAdaEFS99rroH4aqxkt/RO1nNTtaWLVY17sc9Q5rzfBjqiiuBbY+Pu Ad3w== X-Gm-Message-State: AOJu0Yyehg4RrOKq5QcReITwmAuj/ymPgY2LvAKu5PW0RDQ9qh+BX8x/ l2coECoFi3G9xWp0nNgrbpQHnhvnlN+rUbRVACy+wSSCEiG6tED7cJBnQP80ls9HT7V7G6jask6 gnIRLSA== X-Received: by 2002:a05:6a00:22d6:b0:710:591e:b52f with SMTP id d2e1a72fcca58-7106cf933f9mr13521120b3a.5.1722817911689; Sun, 04 Aug 2024 17:31:51 -0700 (PDT) Received: from stoup.. ([203.56.128.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7106ed16cb3sm4576038b3a.179.2024.08.04.17.31.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Aug 2024 17:31:51 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= Subject: [PULL 5/6] qemu/osdep: Add excluded fd parameter to qemu_close_all_open_fd() Date: Mon, 5 Aug 2024 10:31:28 +1000 Message-ID: <20240805003130.1421051-7-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::431; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x431.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 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 Message-ID: <20240802145423.3232974-5-cleger@rivosinc.com> [rth: Use max_fd in qemu_close_all_open_fd_close_range] Signed-off-by: Richard Henderson --- include/qemu/osdep.h | 8 +++- system/async-teardown.c | 2 +- util/oslib-posix.c | 98 ++++++++++++++++++++++++++++++++++------- 3 files changed, 89 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..11b35e48fb 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,60 @@ 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 max_fd = open_max - 1; + int first = 0, last; + 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 */ + last = max_fd; + if (cur_skip < nskip) { + last = skip[cur_skip] - 1; + last = MIN(last, max_fd); + } + + /* With the adjustments to the range, we might be done. */ + if (first > last) { + break; + } + + ret = close_range(first, last, 0); + if (ret < 0) { + return false; + } + + first = last + 1; + } while (last < max_fd); + + 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 +920,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 Mon Aug 5 00:31:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 816729 Delivered-To: patch@linaro.org Received: by 2002:adf:e00d:0:b0:367:895a:4699 with SMTP id s13csp1133627wrh; Sun, 4 Aug 2024 17:32:40 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWc8VAiDNQZ3tgsCVpS4i7J7e/OVm7QUSTFWHQMQ5k4vSkqhUgEXxvcreWkL4MPMP19SSZ+8UZdqMHEXPf/Tthi X-Google-Smtp-Source: AGHT+IEzlWO7DXwCqNv+LAoqkPihihh/5Cj5njSOzECp+DMqdCyURqQuWOEDaP11Z2E0y6K/lxuW X-Received: by 2002:a05:6902:13cc:b0:e03:a3ac:fb75 with SMTP id 3f1490d57ef6-e0bde2622dcmr12084189276.23.1722817960602; Sun, 04 Aug 2024 17:32:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1722817960; cv=none; d=google.com; s=arc-20160816; b=TI6Sr4sIgibz18qGHc6D+82EyA9m/wHnBDRo8lixffeHxxxv4XC0FwzUQNNY6OaW7H Mx9FraoqYIYfeTWIFWwf9uE3/BhPwrZPiNYoejF5hT1vE/WUFGel+kn4PINyRotQasrH mU+1Mb+cTi27588g9Zj+lILJ4HrQ3D8zIvh5E84iEeuFAIWhIDR/WBJ+jqqEV4vq6rE/ CGAMeRl0LrEZLGIsBWJceNnNV3ov5C2DuWDcaxYQb+t84Z3+wKj9SrEGj9tjtW0HfSzF xu7TzJsl1gHsiHdUZTd5B1OuNxHrVegtDFG8c44Pm4ajeOnq/TsFZxdZypxDsu3UxE6K t28w== 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=zCe4GkwH/iAnpmcpXgqjGYu6VSZ8QZlYv/hHC8b9IYI=; fh=j2Ck7JJDkuIhuIM6fXlcQpeLsiCqZ/awsmByeQRTUsY=; b=Of8WaoyLst4jqY4GVCYNtQDifaqLll7Clrq/RBenBFxHQjg+ZIr3Nh5mhKufmEjYFX yLtqeB2enKd7s5wJjWf/vDeaBW+xhHFdTe8BWbsNwAujfhJaBRtmcRZ8Uf01XZCJvAyC nIDpcC5XicEydxi4y1PXG5m8A+Xof0kuQ1MhgfB36DyBkHBT3fbkj7dcd5zzIqODAML8 pMR8hyh1be9qp3qcX5XuswmTY6R5NQZERMpPi5QRTBD9x38D6I3Xy6uv/ZJfrNccda3n VaMbYRMIzn1sEeGGT60YU48kPg4/UYaDUlSFHTnMwkP+GiQEuSt/j+zrIXvVaDXoZNl9 mM0g==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=rdkeGhj1; 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 d75a77b69052e-4518a6bd0b1si81321461cf.24.2024.08.04.17.32.40 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 04 Aug 2024 17:32:40 -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=rdkeGhj1; 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 1saldn-00006d-MW; Sun, 04 Aug 2024 20:31:59 -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 1saldl-0008PV-8c for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:57 -0400 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1saldj-0002KC-LK for qemu-devel@nongnu.org; Sun, 04 Aug 2024 20:31:57 -0400 Received: by mail-pf1-x42d.google.com with SMTP id d2e1a72fcca58-70eaf5874ddso7494603b3a.3 for ; Sun, 04 Aug 2024 17:31:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722817914; x=1723422714; 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=zCe4GkwH/iAnpmcpXgqjGYu6VSZ8QZlYv/hHC8b9IYI=; b=rdkeGhj16oa8Y5B8j6NgH7jUWTJkb1fHjsRMQFoVhbrYuMCJcGGe+4+/FTsk/ipgWb 5PZ8kCs/7w6tBt7phOj8g9buewNhOE9+/J89nSEFteJBqcpl7uwI55ANK+yTcjvODb/8 WjvOvg91F5a9Y/nvMYd21I3IvVeO6R3NSN7SZ2X7deG/D71GdDUsgnqn6+uR3dCBhwMi mLsQWtHpb7dsbiKTLFNoIEu3dCSD8U9wAZ4Wh5s57lHPW+DovOZBE8QmVWHGOpTjOOkk 32NtHvbFkKv8QPSh7wIIICFYtMmQs+7rJb9poU3pgXk1Q8pGmHnQ1mvoEYAiH8AOx5Mz 5kMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722817914; x=1723422714; 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=zCe4GkwH/iAnpmcpXgqjGYu6VSZ8QZlYv/hHC8b9IYI=; b=r+gSc7Qdm3BkhDQ/A0awXJPRGXtuXlLe8EyYzeUam22kOfp2gYsPisUzl1ZtFoL6AC maYPXoyCmxKDSCez2pR5yytyLG3O09kEqy/I3XJGtOAmORpT9fb1Gjajm6jLkzBVbuNG +bJWrGnMAi/SSbcmn54wOwDa+0hqpqcf+Cur8Snr+6Z992HmR72knKYoYboMlvnQMyDk jJKIXSMcxAk1bxgYYPRDHWEuZyko/tPd/cfXR5mQidLYArr2KqnikRL4Ae1eLBYifCC7 jQr+34c+HJyfTIwVLaQPta/bs9Lqj0isr0XZOd/YbXsHrlevxnzP1MVQWUWsTIGX1EXH IseQ== X-Gm-Message-State: AOJu0YziB3XwGQibUombyQ6LMUIuI1F3F02gaZmZakdce6JqTYCCtc3m nnAIQxce50jWwOMzUgAd1m0arN3C9rhU7sekHtwc7/nBmmsqGJGC18E1A0YDGICFIBdGW0FmO0U UYHaLAg== X-Received: by 2002:a05:6a20:8416:b0:1c4:b927:1ffb with SMTP id adf61e73a8af0-1c69953db33mr9534159637.17.1722817914049; Sun, 04 Aug 2024 17:31:54 -0700 (PDT) Received: from stoup.. ([203.56.128.103]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7106ed16cb3sm4576038b3a.179.2024.08.04.17.31.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Aug 2024 17:31:53 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?utf-8?b?Q2zDqW1lbnQgTMOpZ2Vy?= Subject: [PULL 6/6] net/tap: Use qemu_close_all_open_fd() Date: Mon, 5 Aug 2024 10:31:29 +1000 Message-ID: <20240805003130.1421051-8-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::42d; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42d.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 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 Message-ID: <20240802145423.3232974-6-cleger@rivosinc.com> Signed-off-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,