made parent kill childeren
This commit is contained in:
+32
-7
@@ -7,7 +7,6 @@
|
|||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <sys/prctl.h>
|
|
||||||
|
|
||||||
|
|
||||||
#define DEFAULT_NUM_LEN 512
|
#define DEFAULT_NUM_LEN 512
|
||||||
@@ -202,6 +201,36 @@ static long read_self_ticks(void) {
|
|||||||
return utime + stime;
|
return utime + stime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void kill_children_of(pid_t ppid) {
|
||||||
|
DIR *dir = opendir("/proc");
|
||||||
|
if (!dir) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
struct dirent *ent;
|
||||||
|
while ((ent = readdir(dir)) != NULL) {
|
||||||
|
if (ent->d_type != DT_DIR || !is_numeric_dir(ent->d_name)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
char path[256];
|
||||||
|
snprintf(path, sizeof(path), "/proc/%s/stat", ent->d_name);
|
||||||
|
FILE *f = fopen(path, "r");
|
||||||
|
if (!f) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
long pid = 0;
|
||||||
|
long parent = 0;
|
||||||
|
char comm[256];
|
||||||
|
char state = 0;
|
||||||
|
if (fscanf(f, "%ld (%255[^)]) %c %ld", &pid, comm, &state, &parent) == 4) {
|
||||||
|
if (parent == (long)ppid) {
|
||||||
|
kill((pid_t)pid, SIGKILL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
closedir(dir);
|
||||||
|
}
|
||||||
|
|
||||||
static void read_cpu_total(long *total, long *idle) {
|
static void read_cpu_total(long *total, long *idle) {
|
||||||
FILE *f = fopen("/proc/stat", "r");
|
FILE *f = fopen("/proc/stat", "r");
|
||||||
if (!f) {
|
if (!f) {
|
||||||
@@ -289,12 +318,6 @@ int main(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
if (prctl(PR_SET_PDEATHSIG, SIGKILL) != 0) {
|
|
||||||
_exit(1);
|
|
||||||
}
|
|
||||||
if (getppid() == 1) {
|
|
||||||
_exit(0);
|
|
||||||
}
|
|
||||||
pause();
|
pause();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -359,6 +382,8 @@ int main(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
kill_children_of(getpid());
|
||||||
|
|
||||||
fclose(csv);
|
fclose(csv);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user