public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Paul Holzinger <pholzing@redhat.com>
To: Stefano Brivio <sbrivio@redhat.com>
Cc: passt-dev@passt.top
Subject: Re: [PATCH] pasta: wait for netns setup before calling exec
Date: Mon, 6 Feb 2023 20:53:08 +0100	[thread overview]
Message-ID: <ac4d61a4-d8ba-cacc-5f62-f958a2829c02@redhat.com> (raw)
In-Reply-To: <20230203195536.667a09f6@elisabeth>


On 03/02/2023 19:55, Stefano Brivio wrote:
> On Fri, 3 Feb 2023 17:37:03 +0100
> Stefano Brivio <sbrivio@redhat.com> wrote:
>
>> On Fri, 3 Feb 2023 15:44:40 +0100
>> Paul Holzinger <pholzing@redhat.com> wrote:
>>
>>> On 02/02/2023 11:25, Stefano Brivio wrote:
>>>> On Wed,  1 Feb 2023 19:01:16 +0100
>>>> Paul Holzinger <pholzing@redhat.com> wrote:
>>>>     
>>>>> When a user spawns a command with pasta they expect the network to be
>>>>> ready. Currently this does not work because pasta will fork/exec
>>>>> before it will setup the network config.
>>>>>
>>>>> This patch fixes it by using a pipe to sync parent and child. The child
>>>>> will now block reading from this pipe before the exec call. The parent
>>>>> will then unblock the child only after the netns was configured.
>>>> Thanks for the patch! I'm reviewing this in a bit.
>>>>
>>>> A few considerations meanwhile:
>>>>
>>>> - there's actually a bigger issue (you're fixing here) than the
>>>>     namespace configuration (via netlink) itself: the tap device isn't
>>>>     ready (tap_sock_init() hasn't been called yet) when we spawn the
>>>>     command in the new namespace. Oops.
>>>>
>>>>     If you're wondering: we can't just reorder things, because to complete
>>>>     the configuration phase (conf()) we need the namespace to be set up,
>>>>     and we can't initialise the tap device before it's set up
>>>>
>>>> - pipes are more commonly used to transfer data around (hence the whole
>>>>     code you need to open a communication channel, check it, close it).
>>>>     Did you try with a signal? Or is there a reason why it wouldn't work?
>>>>
>>>>     You could simply SIGSTOP the child, from the child itself:
>>>>
>>>> 	kill(getpid(), SIGSTOP);
>>>>
>>>>     and send a SIGCONT to it (we already store the PID of the child in
>>>>     pasta_child_pid) once we're ready.
>>>>
>>>>     SIGCONT is special in that it doesn't need CAP_KILL or the processes
>>>>     to run under the same UID -- just in the same session, so it wouldn't
>>>>     risk interfering with the isolation_*() calls.
>>>>
>>>>     I haven't tested this but I think it should lead to simpler code.
>>> Thinking about this more STOP/CONT will not work reliably, it could stop
>>> the child forever when the parent sends SIGCONT before the child
>>> SIGSTOPs itself. While this is unlikely we have no control over how both
>>> processes are scheduled.
>>>
>>> With this pipe version there is no problem when the parent closes the fd
>>> before the child calls read, read will simply return EOF and the child can
>>> continue, thus it will work correctly in all cases.
>> Ah, right, nice catch. Still, you could probably use pause() or
>> sigsuspend() instead of the SIGSTOP. Let me try a quick stand-alone
>> experiment and I'll get back to you (probably early next week), unless
>> you manage to get it working before.
> Sorry, forget about it -- it doesn't solve the problem of waiting, in
> the parent, that the child is stopped, which is exactly the point you
> raised. A waitpid() with WUNTRACED does:
>
> #include <stdio.h>
> #include <unistd.h>
> #include <signal.h>
> #include <sys/wait.h>
>
> #define DELAY_PARENT	0
> #define DELAY_CHILD	0
>
> int main()
> {
> 	pid_t pid;
> 	int i;
>
> 	if ((pid = fork())) {
> #if DELAY_PARENT
> 		for (i = 0; i < 10000000; i++);
> #endif
> 		waitpid(pid, NULL, WUNTRACED);
> 		kill(pid, SIGCONT);
> 		sleep(1);
> 		return 0;
> 	}
>
> #if DELAY_CHILD
> 	for (i = 0; i < 10000000; i++);
> #endif
> 	raise(SIGSTOP);
> 	fprintf(stderr, "received SIGCONT\n");
> 	return 0;
> }
>
> I left in some busyloops you can use to check. It's three lines, with
> error checks probably 9, still less than the pipe thing (~16) and it
> looks simpler (to me).
>

I don't know what it is but this doesn't work when I implement it in 
pasta. Somehow
the child doesn't seem to be stopped. A short lived processes such as ip 
addr causes
pasta to exit even before the parent got to the point where it would 
send SIGCONT.
If I get the nanoseconds before and after raise(SIGSTOP) there is almost 
no delay.
It is clear that the child still runs after raise(SIGSTOP) even though 
the parent never send
SIGCONT at this point, in fact I can completely remove the kill(pid, 
SIGCONT) call and
the program works without hanging.

And of course if I run this through strace it works just fine, so I am 
bit lost right now.
Likely because strace makes things much slower?

The waitpid call also always fails with ECHILD, I don't understand why. 
I think it must
have something to do with the SIGCHILD signal handler that already reaps 
the signal info?

So either I made a a stupid mistake somewhere or it simply does not work.


  reply	other threads:[~2023-02-06 19:53 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-02-01 18:01 [PATCH] pasta: wait for netns setup before calling exec Paul Holzinger
2023-02-02 10:25 ` Stefano Brivio
2023-02-02 15:23   ` Paul Holzinger
2023-02-03 14:44   ` Paul Holzinger
2023-02-03 16:37     ` Stefano Brivio
2023-02-03 18:55       ` Stefano Brivio
2023-02-06 19:53         ` Paul Holzinger [this message]
2023-02-07 10:55           ` Stefano Brivio
2023-02-07 19:09             ` Paul Holzinger
2023-02-08 13:01               ` Stefano Brivio
2023-02-08 15:06                 ` 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=ac4d61a4-d8ba-cacc-5f62-f958a2829c02@redhat.com \
    --to=pholzing@redhat.com \
    --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).