brettwooldridge/NuProcess

Destroy all child process

MasseGuillaume opened this issue · 4 comments

it would be useful to kill all child process. Similar to what pkill -9 -P <ppid> does.

On posix systems this would require to call setsid system call at the beginning of the process and to call kill with negative value during destroy. @brettwooldridge would you accept such a change if I tried to contribute?

https://man7.org/linux/man-pages/man2/setsid.2.html
https://man7.org/linux/man-pages/man2/kill.2.html

Of course both syscalls would be called optionally, probably controlled by some flag

I looked into this, but unfortunately it seems to be near impossible to implement this in NuProcess.

setsid needs to be called between fork and exec, but that part is buried in Java_java_lang_UNIXProcess_forkAndExec, which is some C code in the JDK.

Therefore the best way to achieve that is to call the process by prefixing /usr/bin/setsid -w to the original command and kill it using LibC.kill(-process.getPid(), LibC.SIGTERM).

Unfortunately /usr/bin/setsid does not support -w on platforms like RHEL6 or earlier. For those it is probably easiest to create your own implementation in C or Rust.

You can also write your own setsid.pl as

#!/usr/bin/perl
use POSIX;
POSIX::setsid();
exec @ARGV;

and run your command as /path/to/setsid.pl originalcommand originalargs. Then you don't need C or Rust, because Perl should be available on any POSIXy system.