MCPcopy Create free account
hub / github.com/Kitware/CMake / kwsysProcessKill

Function kwsysProcessKill

Source/kwsys/ProcessUNIX.c:2528–2630  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2526}
2527
2528static void kwsysProcessKill(pid_t process_id)
2529{
2530#if defined(__linux__) || defined(__CYGWIN__)
2531 DIR* procdir;
2532#endif
2533
2534 /* Suspend the process to be sure it will not create more children. */
2535 kill(process_id, SIGSTOP);
2536
2537#if defined(__CYGWIN__)
2538 /* Some Cygwin versions seem to need help here. Give up our time slice
2539 so that the child can process SIGSTOP before we send SIGKILL. */
2540 usleep(1);
2541#endif
2542
2543/* Kill all children if we can find them. */
2544#if defined(__linux__) || defined(__CYGWIN__)
2545 /* First try using the /proc filesystem. */
2546 if ((procdir = opendir("/proc"))) {
2547# if defined(MAXPATHLEN)
2548 char fname[MAXPATHLEN];
2549# elif defined(PATH_MAX)
2550 char fname[PATH_MAX];
2551# else
2552 char fname[4096];
2553# endif
2554 char buffer[KWSYSPE_PIPE_BUFFER_SIZE + 1];
2555 struct dirent* d;
2556
2557 /* Each process has a directory in /proc whose name is the pid.
2558 Within this directory is a file called stat that has the
2559 following format:
2560
2561 pid (command line) status ppid ...
2562
2563 We want to get the ppid for all processes. Those that have
2564 process_id as their parent should be recursively killed. */
2565 for (d = readdir(procdir); d; d = readdir(procdir)) {
2566 int pid;
2567 if (sscanf(d->d_name, "%d", &pid) == 1 && pid != 0) {
2568 struct stat finfo;
2569 snprintf(fname, sizeof(fname), "/proc/%d/stat", pid);
2570 if (stat(fname, &finfo) == 0) {
2571 FILE* f = fopen(fname, "r");
2572 if (f) {
2573 size_t nread = fread(buffer, 1, KWSYSPE_PIPE_BUFFER_SIZE, f);
2574 fclose(f);
2575 buffer[nread] = '\0';
2576 if (nread > 0) {
2577 char const* rparen = strrchr(buffer, ')');
2578 int ppid;
2579 if (rparen && (sscanf(rparen + 1, "%*s %d", &ppid) == 1)) {
2580 if (ppid == process_id) {
2581 /* Recursively kill this child and its children. */
2582 kwsysProcessKill(pid);
2583 }
2584 }
2585 }

Callers 3

kwsysProcess_KillFunction · 0.70
kwsysProcessCleanupFunction · 0.70
kwsysProcess_KillPIDFunction · 0.70

Calls 3

usleepFunction · 0.85
statClass · 0.70
snprintfFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…