From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v2 3/3] pasta: Clean up waiting pasta child on failures
Date: Thu, 11 Dec 2025 14:54:36 +1100 [thread overview]
Message-ID: <20251211035436.2844623-4-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20251211035436.2844623-1-david@gibson.dropbear.id.au>
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 <david@gibson.dropbear.id.au>
---
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 <arpa/inet.h>
#include <netinet/in.h>
#include <net/ethernet.h>
+#include <sys/prctl.h>
#include <sys/syscall.h>
#include <linux/magic.h>
@@ -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 <sys/random.h>
#endif
--
2.52.0
next prev parent reply other threads:[~2025-12-11 3:54 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-11 3:54 [PATCH v2 0/3] Improved exit()/_exit() handling David Gibson
2025-12-11 3:54 ` [PATCH v2 1/3] tcp: Suppress new instance of cppcheck bug 14191 David Gibson
2025-12-11 3:54 ` [PATCH v2 2/3] treewide: Introduce passt_exit() helper David Gibson
2025-12-11 3:54 ` David Gibson [this message]
2025-12-11 7:16 ` [PATCH v2 3/3] pasta: Clean up waiting pasta child on failures Stefano Brivio
2025-12-11 8:45 ` David Gibson
2025-12-11 13:52 ` Paul Holzinger
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251211035436.2844623-4-david@gibson.dropbear.id.au \
--to=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
--cc=sbrivio@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).