From nobody Sun Nov 24 13:50:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1722817976; cv=none; d=zohomail.com; s=zohoarc; b=NIRGPZv2QAcpDRduR0XZ1Fwqt/dMRji09dA4bMC8F1DiRBID+OlHQc+8z/4SCE1bMHJ6nV8WLDTDZw2+OtETtvoBBdj4DN8N+lXAtL0dtv4BOgU3UcZ2AzIak0wOewpNPvj93XAr2iyuSg96Z7PZ3TaakhVg0c0b6DutWt5WRqA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722817976; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=W8wloA238Rk9C1XCBXsRbgtFn/P+Z4RpbJw5FHD4LSg=; b=El8z9sgd/syR+5lzJ/lPoPk+349yszCiMIYSvPQS/5+yzSENKoPvETirhgLUSg9fUpuIEGiYQ0tA5gkV7/yBYqw6Jm/kYAw1xLiYR84WhPg2nxUJN6x+q5Tr7Ij6aFlFQ3cQm1xNyDKeoOYfti/gP69XfilfYjnKBhTw0gK6BsI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1722817976782900.6635564706579; Sun, 4 Aug 2024 17:32:56 -0700 (PDT) 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) 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) 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-Google-Smtp-Source: AGHT+IHeBj42U4EyspU8ERcDuYOk1rg9SnhvJpOreqRiSepaXuyIpLjXYNVnWE6Tj+dOQnwSkArVeg== 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) From: Richard Henderson To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= , =?UTF-8?q?Philippe=20Mathieu-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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1722817978471116600 From: Cl=C3=A9ment L=C3=A9ger 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=C3=A9ment L=C3=A9ger Reviewed-by: Philippe Mathieu-Daud=C3=A9 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; } =20 +static void close_all_fds_after_fork(int excluded_fd) +{ + int open_max =3D sysconf(_SC_OPEN_MAX), i; + + for (i =3D 3; i < open_max; i++) { + if (i !=3D 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, co= nst char *ifname, return; } if (pid =3D=3D 0) { - int open_max =3D sysconf(_SC_OPEN_MAX), i; - - for (i =3D 3; i < open_max; i++) { - if (i !=3D fd) { - close(i); - } - } + close_all_fds_after_fork(fd); parg =3D args; *parg++ =3D (char *)setup_script; *parg++ =3D (char *)ifname; @@ -490,17 +495,11 @@ static int net_bridge_run_helper(const char *helper, = const char *bridge, return -1; } if (pid =3D=3D 0) { - int open_max =3D sysconf(_SC_OPEN_MAX), i; char *fd_buf =3D NULL; char *br_buf =3D NULL; char *helper_cmd =3D NULL; =20 - for (i =3D 3; i < open_max; i++) { - if (i !=3D sv[1]) { - close(i); - } - } - + close_all_fds_after_fork(sv[1]); fd_buf =3D g_strdup_printf("%s%d", "--fd=3D", sv[1]); =20 if (strrchr(helper, ' ') || strrchr(helper, '\t')) { --=20 2.43.0