#Detaching from parent process

50 messages Β· Page 1 of 1 (latest)

deep holly
#

Hey guys. I don't have a specific question per se, wondering if someone can take a look at https://git.mills.io/prologic/box/issues/4#issuecomment-12682 and give me a hand? You need to be familiar with Linux/UNIX semantics and process internals. Goal: Get box run -d ... (-d/--deatch) to work. I can (as seen in the issue) get a minimal example of what I think should also work in box working, but I'm not yet sure where I'm going wrong πŸ˜„

deep holly
#

Anyone at all? πŸ€”

slow hinge
#

I think you're having an issue with the detach mode when the parent group goes away based on the ptree.

Try adding these options to the command before you call Start:
command.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: 0, Setsid: true}

#

Or maybe you pass them directly to Start

deep holly
#

@slow hinge Hey! Thanks for chiming in ! Do I add these in my run.go or fork.go?

#

Hmm

deep holly
#

I've tried a few things and so far I haven't succeeded :/

slow hinge
#

Hmm I'm not sure exactly which. I suspect it is just required in run. One thing I noticed in the code in run is the passing of the signal to the container process. I think you'll need to not do that when using the detached option or you might be killing the new process when the program that is spawning it is exiting?

deep holly
#

Hmm

#

Let me try again before going to bed πŸ˜„

#

Oh but wait

#

In run we have something like:

if !deatch {
  // handle/pass signals
}
#

I'm confused πŸ˜„

#

Ahhh! Hold on a sec

#
localhost:~# ./box run -d alpine sleep 300
2022/09/18 14:43:26 detach: #true
2022/09/18 14:43:26 Process ID: 2494
2022/09/18 14:43:26 Process Group ID: 2494
Error: failed run fork process: fork/exec /proc/self/exe: operation not permitted
localhost:~#
#

This is what happens when I set set Pgid, Setpgid and Setsid

deep holly
#

simple little repro that behaves the same

#

not exactly clear where the EPERM is coming from though

slow hinge
#

Sorry missed the conditional. Ah, try running as root then and if it works you have a way forward

deep holly
#

I am root πŸ˜„

slow hinge
#

If you run through strace it'll show you

deep holly
#
localhost:~# whoami
root
localhost:~# id
uid=0(root) gid=0(root) groups=0(root),0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),11(floppy),20(dialout),26(tape),27(video)
localhost:~#
#

πŸ˜„

slow hinge
#

I don't know then, hopefully strace will give you a clue of some sort.

deep holly
#

yeah

#

Thanks πŸ™‡β€β™‚οΈ

#

Hmmm I think I found something

#
[pid  2727] <... setpgid resumed>)      = -1 EPERM (Operation not permitted)
deep holly
#

Success! πŸ₯³

#

I was (in the end) missing improtant devices in the new container/namespace like /dev/null and friends (fixed that)

#

And I needed to actually wait on the process spanwed in the container in fork.go or the detached process in the container would just silently die

#

So now:

$ box run -d alpine sleep 300
$ box ps

works as expected πŸ‘Œ

slow hinge
#

I don't see that in your gist example strace? I do see eperm for it setting up the polling which night make sense now that the parent isn't in the same session of the child. So making sure command won't pipe them might help in the example.

One think to try is not to setup the pipesInstead of using command have you tried to use ForkExec instead with these permissions?

I honestly don't still see the detach condition in the GitHub internal/run.go file I'm looking at master I think

#

Oh, you got it!

deep holly
#

Yup πŸ˜„

#

Have a look at the PR/brnach and hopefully it'll all make sense

#

You were most helpful!

#

Can't Setsid: true though as we're already a session and process group leader at that point

slow hinge
#

I didn't feel like it, but I'll take it. Glad you got it

deep holly
#

too many fork/exec's going on her e:D

#

I learned a bunch of stuff from fixing this

#

But super glad it now works!

slow hinge
#

Great. Now just close the terminal after it starts and make sure it still works .. but if you're already your own leaders yeah should be fine.

#

I wonder if ya need to check for it though, I feel like on a Mac I had to do the Setsid

deep holly
#

Yeah I'll test that for sure!

#

Need to write some integration tests

#

Make sure things continue to work smoothly πŸ˜„