From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202512 header.b=vcD6VozB; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id D19F45A0627 for ; Thu, 11 Dec 2025 04:54:41 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202512; t=1765425278; bh=8fqaR3ptMXQvFsCXGzrCPSalZOUkAEJ5uflwslFCc70=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vcD6VozBAjT7fb/sLZLOIzEBk2YzyvnS5OEZ5lNthnzk7uRyWzJE2sVdHTH5GwC3q RDDcfbrXpvhbDKRYlFr8BJu0jZNkNhagOLfY+ERFTKdhznqeNnmCgqv3U1YsqQDIMA 2bvS9OrpGSk2FNFRhVz2H+2Vxzm+99nS2Lqf3oTTUQTc4v5RHBGNsZyqbW7JLiBaq3 euGpWtdC+L3HI54xAFQIUwU9An8483qBfVuPItWgYfO/lzvEeHG8NbdwuwO3Se9Brm YpeG2VcFQ1F8vOuKoa3jF/nlsa7Ex2vRDeccJgmyaHLt/h9EeVX1NNVmIkG4ho1hRa aa7W/tLvBRhRA== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4dRdy615Yhz4wB8; Thu, 11 Dec 2025 14:54:38 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v2 3/3] pasta: Clean up waiting pasta child on failures Date: Thu, 11 Dec 2025 14:54:36 +1100 Message-ID: <20251211035436.2844623-4-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251211035436.2844623-1-david@gibson.dropbear.id.au> References: <20251211035436.2844623-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: UDIFSTUAUNUGPKROXFL6QOWCO3L7ANVT X-Message-ID-Hash: UDIFSTUAUNUGPKROXFL6QOWCO3L7ANVT X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: When pasta is invoked with a command rather than an existing namespace to attach to, it spawns a child process to run a shell or other command. We create that process during conf(), since we need the namespace to exist for much of our setup. However, we don't want the specified command to run until the pasta network interface is ready for use. Therefore, pasta_spawn_cmd() executing in the child waits before exec()ing. main() signals the child to continue with SIGUSR1 shortly before entering the main forwarding loop. This has the downside that if we exit due to any kind of failure between conf() and the SIGUSR1, the child process will be around waiting indefinitely. The user must manually clean this up. Make this cleaner, by having the child use PR_SET_PDEATHSIG to have itself killed if the parent dies during this window. Technically speaking this is racy: if the parent dies before the child can call the prctl() it will be left zombie-like as before. However, as long as the parent completes pasta_wait_for_ns() before dying, I wasn't able to trigger the race. Since the consequences of this going wrong are merely a bit ugly, I think that's good enough. Signed-off-by: David Gibson --- pasta.c | 11 +++++++++++ util.c | 1 + 2 files changed, 12 insertions(+) diff --git a/pasta.c b/pasta.c index 5c693de1..c307b8a8 100644 --- a/pasta.c +++ b/pasta.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include @@ -189,6 +190,10 @@ static int pasta_spawn_cmd(void *arg) size_t conf_hostname_len; sigset_t set; + /* If the parent dies with an error, so should we */ + if (prctl(PR_SET_PDEATHSIG, SIGKILL)) + die_perror("Couldn't set PR_SET_PDEATHSIG"); + /* We run in a detached PID and mount namespace: mount /proc over */ if (mount("", "/proc", "proc", 0, NULL)) warn_perror("Couldn't mount /proc"); @@ -215,6 +220,12 @@ static int pasta_spawn_cmd(void *arg) sigaddset(&set, SIGUSR1); sigwaitinfo(&set, NULL); + /* Once exec()ed this process is more valuable, and easier to see and + * clean up. Let us outlive our parent now. + */ + if (prctl(PR_SET_PDEATHSIG, 0)) + die_perror("Couldn't clear PR_SET_PDEATHSIG"); + execvp(a->exe, a->argv); die_perror("Failed to start command or shell"); diff --git a/util.c b/util.c index da12c962..27303950 100644 --- a/util.c +++ b/util.c @@ -35,6 +35,7 @@ #include "log.h" #include "pcap.h" #include "epoll_ctl.h" +#include "pasta.h" #ifdef HAS_GETRANDOM #include #endif -- 2.52.0