brettwooldridge / NuProcess

Low-overhead, non-blocking I/O, external Process implementation for Java
Apache License 2.0
712 stars 84 forks source link

Destroy all child process #78

Open MasseGuillaume opened 6 years ago

MasseGuillaume commented 6 years ago

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

tpasternak commented 4 years ago

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

tpasternak commented 4 years ago

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

thraidh commented 3 years ago

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.

thraidh commented 3 years ago

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.