+7
-32
@@ -7,6 +7,7 @@
|
||||
#include <ctype.h>
|
||||
#include <time.h>
|
||||
#include <signal.h>
|
||||
#include <sys/prctl.h>
|
||||
|
||||
|
||||
#define DEFAULT_NUM_LEN 512
|
||||
@@ -201,36 +202,6 @@ static long read_self_ticks(void) {
|
||||
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) {
|
||||
FILE *f = fopen("/proc/stat", "r");
|
||||
if (!f) {
|
||||
@@ -318,6 +289,12 @@ int main(void)
|
||||
}
|
||||
|
||||
if (pid == 0) {
|
||||
if (prctl(PR_SET_PDEATHSIG, SIGKILL) != 0) {
|
||||
_exit(1);
|
||||
}
|
||||
if (getppid() == 1) {
|
||||
_exit(0);
|
||||
}
|
||||
pause();
|
||||
return 0;
|
||||
}
|
||||
@@ -382,8 +359,6 @@ int main(void)
|
||||
}
|
||||
}
|
||||
|
||||
kill_children_of(getpid());
|
||||
|
||||
fclose(csv);
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user