diff options
author | Brad King <brad.king@kitware.com> | 2006-03-08 16:38:51 (GMT) |
---|---|---|
committer | Brad King <brad.king@kitware.com> | 2006-03-08 16:38:51 (GMT) |
commit | 3c39accee9593cc02d4fef2d612d9aef2681d3dc (patch) | |
tree | 249b6180842063b4a47f52b1d5d82d113d1919f1 | |
parent | 892a439fad949bb535523856a4c2bc18162e414e (diff) | |
download | CMake-3c39accee9593cc02d4fef2d612d9aef2681d3dc.zip CMake-3c39accee9593cc02d4fef2d612d9aef2681d3dc.tar.gz CMake-3c39accee9593cc02d4fef2d612d9aef2681d3dc.tar.bz2 |
ENH: Added implementation of process tree killing that runs "ps" to traverse the tree.
-rw-r--r-- | Source/kwsys/ProcessUNIX.c | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/Source/kwsys/ProcessUNIX.c b/Source/kwsys/ProcessUNIX.c index ff65474..7700ccf 100644 --- a/Source/kwsys/ProcessUNIX.c +++ b/Source/kwsys/ProcessUNIX.c @@ -1899,6 +1899,22 @@ static pid_t kwsysProcessFork(kwsysProcess* cp, } /*--------------------------------------------------------------------------*/ +/* For systems without the /proc filesystem we try to obtain process + information by invoking the ps command. Here we define the command + to call on each platform and the corresponding parsing format + string. The parsing format should have two integers to store: the + pid and then the ppid. */ +#if defined(__linux__) || defined(__APPLE__) +# define KWSYSPE_PS_COMMAND "ps axo pid,ppid" +# define KWSYSPE_PS_HEADER "%*s %*s\n" +# define KWSYSPE_PS_FORMAT "%d %d\n" +#elif defined(__hpux) && 0 /* Disable until tested. */ +# define KWSYSPE_PS_COMMAND "ps -ef" +# define KWSYSPE_PS_HEADER "%*s %*s %*s %*s %*s %*s %*s %*s\n" +# define KWSYSPE_PS_FORMAT "%*s %d %d %*s %*s %*s %*s %*s\n" +#endif + +/*--------------------------------------------------------------------------*/ static void kwsysProcessKill(pid_t process_id) { DIR* procdir; @@ -1906,8 +1922,8 @@ static void kwsysProcessKill(pid_t process_id) /* Suspend the process to be sure it will not create more children. */ kill(process_id, SIGSTOP); - /* Kill all children if we can find them. Currently this works only - on systems that support the proc filesystem. */ + /* Kill all children if we can find them. First try using the /proc + filesystem. */ if((procdir = opendir("/proc")) != NULL) { #if defined(MAXPATHLEN) @@ -1962,6 +1978,31 @@ static void kwsysProcessKill(pid_t process_id) } closedir(procdir); } +#if defined(KWSYSPE_PS_COMMAND) + else + { + /* Try running "ps" to get the process information. */ + FILE* ps = popen(KWSYSPE_PS_COMMAND, "r"); + + /* Make sure the process started and provided a valid header. */ + if(ps && fscanf(ps, KWSYSPE_PS_HEADER) != EOF) + { + /* Look for processes whose parent is the process being killed. */ + int pid, ppid; + while(fscanf(ps, KWSYSPE_PS_FORMAT, &pid, &ppid) == 2) + { + if(ppid == process_id) + { + /* Recursively kill this child aned its children. */ + kwsysProcessKill(pid); + } + } + + /* We are done with the ps process. */ + pclose(ps); + } + } +#endif /* Kill the process. */ kill(process_id, SIGKILL); |