phusion / baseimage-docker

A minimal Ubuntu base image modified for Docker-friendliness
http://phusion.github.io/baseimage-docker/
MIT License
8.96k stars 1.09k forks source link

Question: learning about my_init #250

Closed samrocketman closed 9 years ago

samrocketman commented 9 years ago

I'm currently trying to understand what my_init does for learning and rewriting for fun (in go). I don't understand what the following section is doing.

https://github.com/phusion/baseimage-docker/blob/14ec533a164cdb495e1c6ab10b82ebe96695a971/image/bin/my_init#L114-L144

What is the while loop trying to accomplish?

cc @mynameismevin

samrocketman commented 9 years ago

cc @FooBarWidget pinging you since you're the only author listed on my_init file. Do you mind sharing your thoughts?

tinco commented 9 years ago

Hi @samrocketman I feel the code is pretty well documented. It tries to reap all terminating child processes. You can look at the man page of waitpid here: http://linux.die.net/man/2/waitpid to see that waitpid amongst other things does this: "In the case of a terminated child, performing a wait allows the system to release the resources associated with the child" so it prevents processes from becoming zombies.

samrocketman commented 9 years ago

Thanks, @tinco I was not familiar with waitpid at all so this documentation helps. I'm coming from a perspective who has little understanding of init but wants to understand more about it and the my_init process. So it's not quite so well documented for someone who is reading with zero background on this.

FooBarWidget commented 9 years ago

It's pretty important that you refer to the POSIX API documentation. my_init performs a bunch of POSIX operations, which are exposed in Python through the os module.

A normal waitpid() call waits until a specific child process terminates. Unfortunately, while waiting for a specific PID, we won't be able to reap any other child process that terminates even though the system expects child processes to be reaped as quickly as possible. So with the while loop I perform a non-blocking waitpid().

samrocketman commented 9 years ago

Thanks for your insight @FooBarWidget. That clearly explains the WNOHANG while loop which was a specific point of confusion and lack of understanding. I feel this question has been fully answered. Thanks all!