/** * Sends signal to every child process of a tree root process * * @param process tree root process */ public static boolean sendSignalToProcessTree(Process process, int signal) { checkCLib(); final int our_pid = C_LIB.getpid(); final int process_pid = getProcessPid(process); final Ref<Integer> foundPid = new Ref<Integer>(); final ProcessInfo processInfo = new ProcessInfo(); final List<Integer> childrenPids = new ArrayList<Integer>(); processPSOutput( getPSCmd(false), new Processor<String>() { @Override public boolean process(String s) { StringTokenizer st = new StringTokenizer(s, " "); int parent_pid = Integer.parseInt(st.nextToken()); int pid = Integer.parseInt(st.nextToken()); processInfo.register(pid, parent_pid); if (parent_pid == process_pid) { childrenPids.add(pid); } if (pid == process_pid) { if (parent_pid == our_pid) { foundPid.set(pid); } else { throw new IllegalStateException("process is not our child"); } } return false; } }); boolean result; if (!foundPid.isNull()) { processInfo.killProcTree(foundPid.get(), signal, UNIX_KILLER); result = true; } else { for (Integer pid : childrenPids) { processInfo.killProcTree(pid, signal, UNIX_KILLER); } result = false; } return result; }
public static int getProcessPid() { checkCLib(); return C_LIB.getpid(); }