dumb-init icon indicating copy to clipboard operation
dumb-init copied to clipboard

survive bereaving

Open fkluknav opened this issue 7 years ago • 7 comments

Hi,

sometimes it is useful to keep init alive when its direct child dies. For example when running a double-forking daemon or SysV init scripts.

This pull request adds a new option '--survive-bereaving' or '-b' that keeps dumb-init running. It checks /proc/ after SIGCHLD and quits only when there are no processes left.

Documentation is not updated, I do not feel confident enough in English.

Any comments are welcome.

Have a nice day,

Fero

fkluknav avatar May 25 '17 12:05 fkluknav

Thanks for sending in this PR! This request has come up a couple of times, notably in #80, so I think there are definitely legitimate use-cases for it.

Sort of continuing my comment in that thread, it'd be cool if we could find a way to implement this that works even when dumb-init isn't PID 1, since it makes testing a lot easier (and also we use dumb-init in some non-PID-1 places). In that case, we'd need a way to list all child processes of dumb-init (rather than all processes on the system), and I don't know of a portable way to do that.

On Linux, we could read /proc/<pid>/tasks/<pid>/children, but to my knowledge this interface isn't available outside of Linux and the only other way is to iterate all processes on the system and read their parent PID (https://lwn.net/Articles/475688/ -- this also seems to be what pstree does).

I guess maybe we could also support this feature on Linux only? Do you know if there's a straightforward way to modify this change so that it only considers child PIDs when counting?

chriskuehl avatar May 26 '17 04:05 chriskuehl

I am not completely sure what is the desired behavior as non-pid1. Wait for all children, grandchildren... to exit and then exit dumb-init? My use-case is the opposite, I want init to keep running as long as there is any process in the namespace, including processes that are not descendants of init but entered the namespace from elsewhere. To achieve that, init must wake at regular intervals (I do not know of a better way) - init will not receive sigchld for those processes.

What kind of portability are you trying to achieve? POSIX? In that case, the answer is 'ps' utility.

fkluknav avatar May 26 '17 11:05 fkluknav

Can I help push this pull request forward somehow? I'd love to use this functionality (on Linux).

Mathiasdm avatar Sep 18 '17 13:09 Mathiasdm

@Mathiasdm thanks for the bump! I think this is a solid PR, I've got a couple minor comments I'll leave but overall I think we can merge this pretty soon.

chriskuehl avatar Sep 22 '17 18:09 chriskuehl

Does PR_SET_CHILD_SUBREAPER work for the non PID-1 case?

Alternatively, if you're trying to monitor all processes in the namespace, you'll have to monitor /proc at regular intervals (not just when SIGCHLD is received) or write some hack with inotify...

kpengboy avatar Oct 11 '17 00:10 kpengboy

I just ran into an issue that this would have solved nicely. Is there anything needed before this can be merged or?

nvx avatar Jun 01 '20 05:06 nvx

current MR failed to compile with :

dumb-init.c: In function 'handle_signal':
dumb-init.c:170:21: error: 'exit_status' may be used uninitialized in this function [-Werror=maybe-uninitialized]
                 exit(exit_status);
                     ^
cc1: all warnings being treated as errors

odoucet avatar Mar 26 '21 16:03 odoucet