144 lines
4.4 KiB
C
144 lines
4.4 KiB
C
#include "thread_mon.h"
|
|
|
|
|
|
int is_number(const char *str) {
|
|
while (*str) {
|
|
if (!isdigit(*str)) return 0;
|
|
str++;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
pprocess_list GetProcessListByPID(int pid) {
|
|
pprocess_list head = malloc(sizeof(process_list));
|
|
if (!head) return NULL;
|
|
head->pid = -1;
|
|
InitializeListHead(&head->entry);
|
|
PLIST_ENTRY list_head = &head->entry;
|
|
DIR *dir = opendir("/proc");
|
|
if (!dir) return head;
|
|
struct dirent *entry;
|
|
while ((entry = readdir(dir)) != NULL) {
|
|
if (!is_number(entry->d_name)) continue;
|
|
|
|
int cpid = atoi(entry->d_name);
|
|
if (cpid <= 0) continue;
|
|
|
|
char stat_path[256];
|
|
snprintf(stat_path, sizeof(stat_path), "/proc/%d/stat", cpid);
|
|
FILE *fp = fopen(stat_path, "r");
|
|
if (!fp) continue;
|
|
|
|
int ppid;
|
|
char name[256];
|
|
if (fscanf(fp, "%*d (%[^)]) %*c %d", name, &ppid) != 2) {
|
|
fclose(fp);
|
|
continue;
|
|
}
|
|
fclose(fp);
|
|
|
|
if (ppid == pid) {
|
|
pprocess_list node = malloc(sizeof(process_list));
|
|
if (!node) continue;
|
|
node->pid = cpid;
|
|
InitializeListHead(&node->entry);
|
|
InsertTailList(list_head, &node->entry);
|
|
pprocess_list child_list = GetProcessListByPID(cpid);
|
|
if (child_list) {
|
|
PLIST_ENTRY iter = child_list->entry.flink;
|
|
while (iter != &child_list->entry) {
|
|
pprocess_list child_node = CONTAINING_RECORD(iter, process_list, entry);
|
|
iter = iter->flink;
|
|
InsertTailList(list_head, &child_node->entry);
|
|
}
|
|
free(child_list);child_list=NULL;
|
|
}
|
|
}
|
|
}
|
|
|
|
closedir(dir);
|
|
return head;
|
|
}
|
|
|
|
pprocess_list GetThreadListByPID(int pid){
|
|
char path[256];
|
|
snprintf(path, sizeof(path), "/proc/%d/task", pid);
|
|
DIR *dir = opendir(path);
|
|
if (!dir) return NULL;
|
|
|
|
pprocess_list fa = malloc(sizeof(process_list));
|
|
if (!fa) { return NULL; }
|
|
InitializeListHead(&fa->entry);
|
|
fa->pid = -1;
|
|
|
|
struct dirent *entry;
|
|
while ((entry = readdir(dir)) != NULL) {
|
|
char *name = entry->d_name;
|
|
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
|
|
continue;
|
|
if (!is_number(name))
|
|
continue;
|
|
int tid = atoi(name);
|
|
if (tid <= 0) continue;
|
|
pprocess_list node = malloc(sizeof(process_list));
|
|
if (!node) continue;
|
|
node->pid = tid;
|
|
InsertTailList(&fa->entry, &node->entry);
|
|
}
|
|
closedir(dir);
|
|
return fa;
|
|
}
|
|
|
|
pprocess_list GetAllProcessAndThreadByPID(int pid) {
|
|
|
|
pprocess_list head = malloc(sizeof(process_list));
|
|
if (!head) return NULL;
|
|
head->pid = -1;
|
|
InitializeListHead(&head->entry);
|
|
PLIST_ENTRY list_head = &head->entry;
|
|
|
|
pprocess_list thread_list = GetThreadListByPID(pid);
|
|
if (thread_list) {
|
|
PLIST_ENTRY iter = thread_list->entry.flink;
|
|
while (iter != &thread_list->entry) {
|
|
pprocess_list node = CONTAINING_RECORD(iter, process_list, entry);
|
|
iter = iter->flink;
|
|
InsertTailList(list_head, &node->entry);
|
|
}
|
|
free(thread_list);thread_list=NULL;
|
|
}
|
|
|
|
pprocess_list child_list = GetProcessListByPID(pid);
|
|
if (child_list) {
|
|
PLIST_ENTRY iter = child_list->entry.flink;
|
|
while (iter != &child_list->entry) {
|
|
pprocess_list node = CONTAINING_RECORD(iter, process_list, entry);
|
|
iter = iter->flink;
|
|
//InsertTailList(list_head, &node->entry);
|
|
|
|
pprocess_list child_thread = GetThreadListByPID(node->pid);
|
|
if (child_thread) {
|
|
PLIST_ENTRY titer = child_thread->entry.flink;
|
|
while (titer != &child_thread->entry) {
|
|
pprocess_list tnode = CONTAINING_RECORD(titer, process_list, entry);
|
|
titer = titer->flink;
|
|
InsertTailList(list_head, &tnode->entry);
|
|
}
|
|
free(child_thread);child_thread=NULL;
|
|
}
|
|
}
|
|
FreeProcessOrThreadList(child_list);
|
|
}
|
|
|
|
return head;
|
|
}
|
|
|
|
void FreeProcessOrThreadList(pprocess_list list){
|
|
if (list == NULL) return ;
|
|
while (!IsListEmpty(&list->entry)) {
|
|
PLIST_ENTRY removed = RemoveHeadList(&list->entry);
|
|
free(CONTAINING_RECORD(removed, process_list, entry));
|
|
}
|
|
free(list);
|
|
list = NULL;
|
|
} |