From nobody Sun Apr 6 02:02:59 2025 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 ARC-Seal: i=1; a=rsa-sha256; t=1736942636; cv=none; d=zohomail.com; s=zohoarc; b=ZOUWF6+r2rX3R/D6rkEHxRl6YzzaEdudFJLVe0UxsCcO+ntso1z6YRKdHfXY9fwVMN/KrDY2OA7iPt5Q5Beq6kEPR9wlO//IKQo1Gtgc08GVy1cV7F5wR4GSVYjKi2EUdKmDJgqeOPOcOyq1/oT6qqqGJaRSybMnBJ4SHKepluA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1736942636; 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=Pdcvm4r1FFh3mjhqnqaxRGqrOq+krKeGclEgq6h25tg=; b=nP/x7m40cI04vzu2sx2FVM/f1o1K0EVQ0zF/OOAzmtwMDVbqguDGDyBij9vUxfxQiQd6Wa+dzwPKI+QEYZYKr9D+RwG626b6JcnRKJ8c0GCvHGlP2uRiu5kggx4Qyu5qJJ76JdzEi8pIpRPrG+lNBNIgP5vnio9CyQFQRcjQw+M= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1736942636052426.2643927884827; Wed, 15 Jan 2025 04:03:56 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tY27J-0003ZY-7S; Wed, 15 Jan 2025 07:03:25 -0500 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 1tY27I-0003YE-18 for qemu-devel@nongnu.org; Wed, 15 Jan 2025 07:03:24 -0500 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tY27G-0008AA-CN for qemu-devel@nongnu.org; Wed, 15 Jan 2025 07:03:23 -0500 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-21644aca3a0so145412945ad.3 for ; Wed, 15 Jan 2025 04:03:21 -0800 (PST) Received: from localhost ([157.82.207.107]) by smtp.gmail.com with UTF8SMTPSA id 41be03b00d2f7-a31844ba221sm9704801a12.24.2025.01.15.04.03.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 15 Jan 2025 04:03:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1736942600; x=1737547400; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=Pdcvm4r1FFh3mjhqnqaxRGqrOq+krKeGclEgq6h25tg=; b=JqipSV2JR94WWepCwmGHXBKxCeO1npr9FSt7IQLQljfXmlolfry3zicPB5lkxObojf Igv4NED5V5HyCT0rfio+Ic8odkirgh2DLeinu9+69MvR4wNZxJpASz9nkS5xc+yXxFMR P+rItoHt6HZBMPNqf5sTWc6cd4h/+qc8Ck3D8Ua5vUjid8+3fRcl5JmxNDOqF9tOurRR jH5KtwCCejFQSY/+K+lWXXWn0Mz4TdcMqsaYL1mU1TnvF4/Naa423I/g6Sh3hVWvMS3E /6d95XmP1u5qHshqs6XOZfhbn+jmb2iU7DfDdtm3xkxMecYylnSqzi/KBnYfQMYrNwiK NrhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736942600; x=1737547400; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Pdcvm4r1FFh3mjhqnqaxRGqrOq+krKeGclEgq6h25tg=; b=XWbhZhpK6wImnDs7TrUfatBdUj8Cm9ryEMNwZ18iXars1/W7GWfgYwsEAs3sPWc3dF K8dCdR6HI9OdBr9pBAOiJOWC8ZmdHcGjPL0BHs1VdJCzhFs2RHKO9ZPDY8Hzn+zEaZzj ZtNY/XSTD+Uj+lE8taAEGa6yEn+UsnsT8aDKNG1qS6Rd6sT4y5u10qgiA45gQNw84VIA UlXcftchj8v00QlqJ3s06pQvohItl2cFI8mnmIaJxTOurkJ5KtHCwY3UBKmTM5hzrDr0 dtCKY3l5ZviWn7ZzW5zBnjhJFo8J3AR/BaVJMFMp68HXeOvu7NwQiN+/BLUopEU1U0Mw Lo8A== X-Gm-Message-State: AOJu0YwBeHE9ugEsxUXO/kk8MP1cYHy1+zNUR1UFrx9IHzjblYI3bdJO ZrXkb6biwYhoUC2Clstch/N5N1kXUQBhn9yXy5UMkDibHic5hytTQqkISCplXMc= X-Gm-Gg: ASbGnctoiLxYduupHF6eqiIj+DqAkXLpi5tfKgpW4bmw9IvF3nUYGXg29ZV5/zrehOk g9dMnI65cu53qImSmlBBTQ1lM+1nJmwEE2GLiIysdyIWjd6+wZIl2vPDQfX/b2CjL7xCaEdEP6R juHCuJUCxevvvjUVyvxixf66QBQ8xGjEs9wwq2y1b+YMnxzy1CUdBg0N7UtUY14fnMViJfo8LKs 7roJTF02va+rl+mOYotHoPaxP9qQ/VkB3KqpKgSXkMmucTSK7YAT8ik8khA X-Google-Smtp-Source: AGHT+IEV4UNSyiehZCiUvatqLFajTvW+mdS5YjCdjA2R0nkjuLZuofglBpVCwU8oFvpDPH2DwYGEmA== X-Received: by 2002:a05:6a20:841c:b0:1e1:a9dd:5a58 with SMTP id adf61e73a8af0-1e88d0b6d05mr51136157637.30.1736942600309; Wed, 15 Jan 2025 04:03:20 -0800 (PST) From: Akihiko Odaki Date: Wed, 15 Jan 2025 21:03:07 +0900 Subject: [PATCH v4 1/2] glib-compat: Define g_spawn_check_wait_status() MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250115-glib-v4-1-a827b2676259@daynix.com> References: <20250115-glib-v4-0-a827b2676259@daynix.com> In-Reply-To: <20250115-glib-v4-0-a827b2676259@daynix.com> To: Jason Wang , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= Cc: qemu-devel@nongnu.org, devel@daynix.com, Akihiko Odaki , Lei Yang X-Mailer: b4 0.14-dev-fd6e3 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::629; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x629.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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 @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1736942637615019000 g_spawn_check_exit_status() is renamed to g_spawn_check_wait_status() in 2.70. Signed-off-by: Akihiko Odaki Tested-by: Lei Yang Reviewed-by: Daniel P. Berrang=C3=A9 --- include/glib-compat.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/glib-compat.h b/include/glib-compat.h index 86be439ba0ef..a553ba13a36e 100644 --- a/include/glib-compat.h +++ b/include/glib-compat.h @@ -68,6 +68,17 @@ * without generating warnings. */ =20 +static inline gboolean g_spawn_check_wait_status_qemu(gint wait_status, + GError **error) +{ +#if GLIB_CHECK_VERSION(2, 70, 0) + return g_spawn_check_wait_status(wait_status, error); +#else + return g_spawn_check_exit_status(wait_status, error); +#endif +} +#define g_spawn_check_wait_status(w, e) g_spawn_check_wait_status_qemu(w, = e) + /* * g_memdup2_qemu: * @mem: (nullable): the memory to copy. --=20 2.47.1 From nobody Sun Apr 6 02:02:59 2025 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 ARC-Seal: i=1; a=rsa-sha256; t=1736942659; cv=none; d=zohomail.com; s=zohoarc; b=VLd1yn5FNCUpgyigEbtU0aSGulpKXPn0UXNDUibQfu6B9S7Qx7sH31Bi1280Mm5Od+ZYg6+s9/HC+qMVXELcunKXLcA2B9435402LV3DXOvuyV7O0drDGtdk6JATtqjfOwl41+TLty9hDNJu3KN/GzdBALywdwnwdfkIZGs+j6g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1736942659; 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=NPcUIuA+1v7HK1TMvnl73wkTi9fItVYjHIROGL3QJso=; b=IlcertBcNjFYbmMNexODymnik8+rt6aBxkQrM84aCikaGZcE8GWj8o2i/B2xDxAbGVIczKYDpP8/6iFRR9CINKNH1wd5BDgsyrQ3OpiMYFyR4BJPAahJRRApx2N0mvIdO2eLOVY5IR19Y49UAvq3TVBIh4IYIch0ufvwxPNUFDY= 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1736942659676626.6601625282985; Wed, 15 Jan 2025 04:04:19 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tY27W-0003j9-9s; Wed, 15 Jan 2025 07:03:38 -0500 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 1tY27P-0003dX-H7 for qemu-devel@nongnu.org; Wed, 15 Jan 2025 07:03:32 -0500 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tY27N-0008Aj-8T for qemu-devel@nongnu.org; Wed, 15 Jan 2025 07:03:31 -0500 Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-21649a7bcdcso113007745ad.1 for ; Wed, 15 Jan 2025 04:03:28 -0800 (PST) Received: from localhost ([157.82.207.107]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-21a9f219a94sm81685845ad.129.2025.01.15.04.03.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 15 Jan 2025 04:03:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1736942608; x=1737547408; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=NPcUIuA+1v7HK1TMvnl73wkTi9fItVYjHIROGL3QJso=; b=Pe/ENQT6dqRa4z2fpr7/1YS0VF0GwJ/tO0kPXOgig7q2opOw3Gk8O/SnoJpnCdh67F K2WwPvOnW+67Zj+3XSV7v6kw40sAWZPkt+RK7y/DicqurGd3iLlaHKlNEikJdJE+73jz drP3dkF1GhGeKx272GB1Zx9WAihqXJHXpXmC8MLv0MzXE+xwN/lNkqf8WoVs2BxA8BD2 rVVvCgjq8cpRIcWpmI7PoPjhl//QrPSTzPX3knrOFy2tTz3k9KiQTlvnbVkfnACYIZqw oLmmkeGHnN0Mib/M0P0KD9MZBg2TyO71MHoOWd4tzzOfGY6zrxq/KUX9v0/DwSJMXXXL wxww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736942608; x=1737547408; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NPcUIuA+1v7HK1TMvnl73wkTi9fItVYjHIROGL3QJso=; b=It9AOYqm5iRjmrTt801WVYzQBfYaTDOQoUneox1Cf6C1qN+TwqZxvKgugWtIXKsrWu mLCh3gU4IAzEeEYYOcfKnCkxFCy2rPYqYQ1ciUVs5KAGoAnnuAZv3HreP+ypE5i9Bqm6 9KzjxN8awpSyIMIJnXs5grhoDfBW7j/eflLF8DR6luFj7NNfQKU5cY9iwex7VpuYgCNI alLu8ABbxifAuUpVzhhUHI640oDFsFmWXNDkYDz+cKCnKCJIUiS2fF7fu0eJ/cC8Cmp7 DV+nKc+PASoyWVYDeLrnnynDAPPh7f7KIjEgV+TUqfBX+5oHPrTAcaIKTNM1Y072IAfG 676g== X-Gm-Message-State: AOJu0Yx/Q6wS6iZTBfTZqNZamle4o/qydPM1Ln44mGblQIDa9XDkDXvc TjavVim006m2LEaP+0yuyvyMLxi1hbySyhWhjOzqfFYF46qZzRXq0NUYUPS9jLk= X-Gm-Gg: ASbGncuhIAKD6Pb3LhEd7n5KAH2RPIAgGZpfLX7p5tZ5j1wqKiN045v3i3LIN1rlhmB Di3vTHBeiRinImGHSCHsG2KHRKVdFNJ9tibnOjd3u7bu4LCDzXSQStQP68ObAKfm3sIqudSc7o8 /x9wZ0f8XQQJ4ObLRiK7hwsn6AfrR33erCJd2VPhgP7xZqYAmuYlexVgf/HMchuwgkk846UWArH NdIzfXlZ43GhMrzCsLmrKsfnjiE1T/pBbGxTfcyDMTC5e8XHXszgqO9cnQd X-Google-Smtp-Source: AGHT+IHwF9rqf14Q99mM9bLnSiPXZh7CM3vfJwM8vQR9VPQmupTPiucL6mmbhn8VnDwOA4Ag212BeQ== X-Received: by 2002:a17:902:d2ca:b0:211:8404:a957 with SMTP id d9443c01a7336-21a83fc0619mr437535545ad.41.1736942607768; Wed, 15 Jan 2025 04:03:27 -0800 (PST) From: Akihiko Odaki Date: Wed, 15 Jan 2025 21:03:08 +0900 Subject: [PATCH v4 2/2] tap: Use g_spawn_sync() and g_spawn_check_wait_status() MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250115-glib-v4-2-a827b2676259@daynix.com> References: <20250115-glib-v4-0-a827b2676259@daynix.com> In-Reply-To: <20250115-glib-v4-0-a827b2676259@daynix.com> To: Jason Wang , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= Cc: qemu-devel@nongnu.org, devel@daynix.com, Akihiko Odaki , Lei Yang X-Mailer: b4 0.14-dev-fd6e3 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::635; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x635.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-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 @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1736942662195019000 g_spawn_sync() (for GLib < 2.68) or g_spawn_async_with_pipes_and_fds() (for the newer) gives an informative message if it fails to execute the script instead of reporting exiting status 1. g_spawn_check_wait_status() also gives an message easier to understand than the raw value returned by waitpid(). Signed-off-by: Akihiko Odaki Tested-by: Lei Yang Reviewed-by: Daniel P. Berrang=C3=A9 --- net/tap.c | 220 +++++++++++++++++++++++++++++++++-------------------------= ---- 1 file changed, 117 insertions(+), 103 deletions(-) diff --git a/net/tap.c b/net/tap.c index ae1c7e398321..f1e00de4181c 100644 --- a/net/tap.c +++ b/net/tap.c @@ -385,56 +385,59 @@ static TAPState *net_tap_fd_init(NetClientState *peer, return s; } =20 -static void close_all_fds_after_fork(int excluded_fd) +#if !GLIB_CHECK_VERSION(2, 68, 0) +static void unset_cloexec(gpointer data) { - const int skip_fd[] =3D {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, - excluded_fd}; - unsigned int nskip =3D ARRAY_SIZE(skip_fd); - - /* - * skip_fd must be an ordered array of distinct fds, exclude - * excluded_fd if already included in the [STDIN_FILENO - STDERR_FILEN= O] - * range - */ - if (excluded_fd <=3D STDERR_FILENO) { - nskip--; - } - - qemu_close_all_open_fd(skip_fd, nskip); + g_assert(!fcntl(GPOINTER_TO_INT(data), F_SETFD, 0)); } +#endif =20 static void launch_script(const char *setup_script, const char *ifname, int fd, Error **errp) { - int pid, status; - char *args[3]; - char **parg; + int status; + const gchar *args[] =3D { setup_script, ifname, NULL }; + g_autoptr(GError) error =3D NULL; + bool spawned; =20 /* try to launch network script */ - pid =3D fork(); - if (pid < 0) { - error_setg_errno(errp, errno, "could not launch network script %s", - setup_script); - return; - } - if (pid =3D=3D 0) { - close_all_fds_after_fork(fd); - parg =3D args; - *parg++ =3D (char *)setup_script; - *parg++ =3D (char *)ifname; - *parg =3D NULL; - execv(setup_script, args); - _exit(1); - } else { +#if GLIB_CHECK_VERSION(2, 68, 0) + pid_t pid; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" + spawned =3D g_spawn_async_with_pipes_and_fds(NULL, args, NULL, + G_SPAWN_DO_NOT_REAP_CHILD | + G_SPAWN_CHILD_INHERITS_STDI= N, + NULL, NULL, -1, -1, -1, + &fd, &fd, 1, &pid, + NULL, NULL, NULL, &error); +#pragma GCC diagnostic pop + if (spawned) { while (waitpid(pid, &status, 0) !=3D pid) { /* loop */ } + } +#else + gchar *mutable_args[sizeof(args)]; =20 - if (WIFEXITED(status) && WEXITSTATUS(status) =3D=3D 0) { - return; - } - error_setg(errp, "network script %s failed with status %d", - setup_script, status); + for (size_t i =3D 0; i < ARRAY_SIZE(args); i++) { + mutable_args[i] =3D (gchar *)args[i]; + } + + spawned =3D g_spawn_sync(NULL, mutable_args, NULL, + G_SPAWN_CHILD_INHERITS_STDIN, + unset_cloexec, GINT_TO_POINTER(fd), + NULL, NULL, &status, &error); +#endif + if (!spawned) { + error_setg(errp, "could not launch network script %s: %s", + setup_script, error->message); + return; + } + + if (!g_spawn_check_wait_status(status, &error)) { + error_setg(errp, "network script %s failed: %s", + setup_script, error->message); } } =20 @@ -477,10 +480,17 @@ static int net_bridge_run_helper(const char *helper, = const char *bridge, { sigset_t oldmask, mask; g_autofree char *default_helper =3D NULL; + g_autofree char *fd_buf =3D NULL; + g_autofree char *br_buf =3D NULL; + g_autofree char *helper_cmd =3D NULL; + g_autoptr(GError) error =3D NULL; + int fd; + int saved_errno; int pid, status; - char *args[5]; - char **parg; + const char *args[5]; + const char **parg; int sv[2]; + bool spawned; =20 sigemptyset(&mask); sigaddset(&mask, SIGCHLD); @@ -495,82 +505,86 @@ static int net_bridge_run_helper(const char *helper, = const char *bridge, return -1; } =20 - /* try to launch bridge helper */ - pid =3D fork(); - if (pid < 0) { - error_setg_errno(errp, errno, "Can't fork bridge helper"); - return -1; - } - if (pid =3D=3D 0) { - char *fd_buf =3D NULL; - char *br_buf =3D NULL; - char *helper_cmd =3D NULL; - - close_all_fds_after_fork(sv[1]); - fd_buf =3D g_strdup_printf("%s%d", "--fd=3D", sv[1]); + fd_buf =3D g_strdup_printf("%s%d", "--fd=3D", sv[1]); =20 - if (strrchr(helper, ' ') || strrchr(helper, '\t')) { - /* assume helper is a command */ + if (strrchr(helper, ' ') || strrchr(helper, '\t')) { + /* assume helper is a command */ =20 - if (strstr(helper, "--br=3D") =3D=3D NULL) { - br_buf =3D g_strdup_printf("%s%s", "--br=3D", bridge); - } - - helper_cmd =3D g_strdup_printf("%s %s %s %s", helper, - "--use-vnet", fd_buf, br_buf ? br_buf : ""); + if (strstr(helper, "--br=3D") =3D=3D NULL) { + br_buf =3D g_strdup_printf("%s%s", "--br=3D", bridge); + } =20 - parg =3D args; - *parg++ =3D (char *)"sh"; - *parg++ =3D (char *)"-c"; - *parg++ =3D helper_cmd; - *parg++ =3D NULL; + helper_cmd =3D g_strdup_printf("%s %s %s %s", helper, + "--use-vnet", fd_buf, br_buf ? br_buf : ""); =20 - execv("/bin/sh", args); - g_free(helper_cmd); - } else { - /* assume helper is just the executable path name */ + parg =3D args; + *parg++ =3D "sh"; + *parg++ =3D "-c"; + *parg++ =3D helper_cmd; + *parg++ =3D NULL; + } else { + /* assume helper is just the executable path name */ =20 - br_buf =3D g_strdup_printf("%s%s", "--br=3D", bridge); + br_buf =3D g_strdup_printf("%s%s", "--br=3D", bridge); =20 - parg =3D args; - *parg++ =3D (char *)helper; - *parg++ =3D (char *)"--use-vnet"; - *parg++ =3D fd_buf; - *parg++ =3D br_buf; - *parg++ =3D NULL; + parg =3D args; + *parg++ =3D helper; + *parg++ =3D "--use-vnet"; + *parg++ =3D fd_buf; + *parg++ =3D br_buf; + *parg++ =3D NULL; + } =20 - execv(helper, args); - } - g_free(fd_buf); - g_free(br_buf); - _exit(1); + /* try to launch bridge helper */ +#if GLIB_CHECK_VERSION(2, 68, 0) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" + spawned =3D g_spawn_async_with_pipes_and_fds(NULL, args, NULL, + G_SPAWN_DO_NOT_REAP_CHILD | + G_SPAWN_CHILD_INHERITS_STDI= N, + NULL, NULL, -1, -1, -1, + &fd, &fd, 1, &pid, + NULL, NULL, NULL, &error); +#pragma GCC diagnostic pop +#else + gchar *mutable_args[sizeof(args)]; + + for (size_t i =3D 0; i < ARRAY_SIZE(args); i++) { + mutable_args[i] =3D (gchar *)args[i]; + } =20 - } else { - int fd; - int saved_errno; + spawned =3D g_spawn_async(NULL, mutable_args, NULL, + G_SPAWN_DO_NOT_REAP_CHILD | + G_SPAWN_CHILD_INHERITS_STDIN, + unset_cloexec, GINT_TO_POINTER(sv[1]), + &pid, &error); +#endif + if (!spawned) { + error_setg(errp, "could not launch bridge helper: %s", error->mess= age); + return -1; + } =20 - close(sv[1]); + close(sv[1]); =20 - fd =3D RETRY_ON_EINTR(recv_fd(sv[0])); - saved_errno =3D errno; + fd =3D RETRY_ON_EINTR(recv_fd(sv[0])); + saved_errno =3D errno; =20 - close(sv[0]); + close(sv[0]); =20 - while (waitpid(pid, &status, 0) !=3D pid) { - /* loop */ - } - sigprocmask(SIG_SETMASK, &oldmask, NULL); - if (fd < 0) { - error_setg_errno(errp, saved_errno, - "failed to recv file descriptor"); - return -1; - } - if (!WIFEXITED(status) || WEXITSTATUS(status) !=3D 0) { - error_setg(errp, "bridge helper failed"); - return -1; - } - return fd; + while (waitpid(pid, &status, 0) !=3D pid) { + /* loop */ } + sigprocmask(SIG_SETMASK, &oldmask, NULL); + if (fd < 0) { + error_setg_errno(errp, saved_errno, + "failed to recv file descriptor"); + return -1; + } + if (!g_spawn_check_wait_status(status, &error)) { + error_setg(errp, "bridge helper failed: %s", error->message); + return -1; + } + return fd; } =20 int net_init_bridge(const Netdev *netdev, const char *name, --=20 2.47.1