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 π
#Detaching from parent process
50 messages Β· Page 1 of 1 (latest)
Anyone at all? π€
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
I've tried a few things and so far I haven't succeeded :/
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?
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
simple little repro that behaves the same
not exactly clear where the EPERM is coming from though
Sorry missed the conditional. Ah, try running as root then and if it works you have a way forward
I am root π
If you run through strace it'll show you
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:~#
π
I don't know then, hopefully strace will give you a clue of some sort.
yeah
Thanks πββοΈ
Hmmm I think I found something
[pid 2727] <... setpgid resumed>) = -1 EPERM (Operation not permitted)
Hmmm https://stackoverflow.com/questions/41778732/unable-to-change-gid-of-a-process sort of explains what's going on here I think
Success! π₯³
Final commit / PR https://git.mills.io/prologic/box/pulls
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 π
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!
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
I didn't feel like it, but I'll take it. Glad you got it
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!