From 2622ccaafc7e1d1dd6d350c2f623c2ecbd7da610 Mon Sep 17 00:00:00 2001 From: Iain Date: Thu, 22 Jul 2021 21:48:16 +0800 Subject: [PATCH] can run monitor process 1 --- CrossCompile.cmake | 5 +++++ main.c | 2 +- src/ProcessInfo.c | 28 +++++++++++++++++++++++++--- src/ProcessInfo.h | 1 + 4 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 CrossCompile.cmake diff --git a/CrossCompile.cmake b/CrossCompile.cmake new file mode 100644 index 0000000..65ccd52 --- /dev/null +++ b/CrossCompile.cmake @@ -0,0 +1,5 @@ +set(CMAKE_SYSTEM_NAME Linux) +set(CMAKE_SYSTEM_PROCESSOR arm) + +#cmake -DCMAKE_TOOLCHAIN_FILE=../CrossCompile.cmake .. +set(CMAKE_C_COMPILER,"/usr/bin/arm-linux-gnuedbihf-gcc") \ No newline at end of file diff --git a/main.c b/main.c index 07e9edd..fc51c52 100644 --- a/main.c +++ b/main.c @@ -24,7 +24,7 @@ int main() { while (1) { i++; printf("\nloop:%d\n", i); - usleep(60000000 / UPDATE_FREQUENCY * 0.98); + usleep(600000 / UPDATE_FREQUENCY * 0.98); //monitor Global_Info and decicde to kill some process updateSystemInfo(); //monitor Proc_List diff --git a/src/ProcessInfo.c b/src/ProcessInfo.c index 97d66d9..a888b30 100644 --- a/src/ProcessInfo.c +++ b/src/ProcessInfo.c @@ -42,6 +42,8 @@ void freeProcInfoList() { return; } +//not found,return -1 +//found,return id int foundProcNode(unsigned int pid) { int i; for (i = 0; i < LIMIT_PROCESS_NUMBER; i++) { @@ -77,11 +79,12 @@ int setFreeProcNode(unsigned int pid) { printf("/proc/%d/status: %s", pid, buf); #endif sscanf(buf, "Name: %s", PID_name); + fclose(fp); //compare PID name with whitelist //check PID in ProcList - if (foundProcNode(pid) == -1) { + if (foundProcNode(pid) != -1) { return -1; } @@ -91,6 +94,25 @@ int setFreeProcNode(unsigned int pid) { //set Proc Info Proc_List[i].pid = pid; strcpy(Proc_List[i].name, PID_name); + return 0; +} + + +int monitorProcDirChange() { + struct dirent **namelist; + int n; + + n = scandir("/proc", &namelist, 0, alphasort); + if (n < 0) + perror("scandir /proc"); + else { + while (n--) { + printf("%s\n", namelist[n]->d_name); + free(namelist[n]); + } + free(namelist); + } + return 0; } //unsigned int *getProcess() { @@ -209,14 +231,14 @@ int updateProcInfo(Proc_Info *proc) { void updateProcList() { #if defined(LOG) printf("/****** Proc Info ******/\n"); + monitorProcDirChange(); int i; for (i = 0; i < LIMIT_PROCESS_NUMBER; i++) { printf("id=%d,", Proc_List[i].pid); } printf("\n"); -#endif setFreeProcNode(1); - setFreeProcNode(101); +#endif char file_path[64] = {0}; int currentProcNode; for (currentProcNode = 0; currentProcNode < LIMIT_PROCESS_NUMBER; currentProcNode++) { diff --git a/src/ProcessInfo.h b/src/ProcessInfo.h index 4cb5cdd..cf5bbbc 100644 --- a/src/ProcessInfo.h +++ b/src/ProcessInfo.h @@ -30,6 +30,7 @@ typedef struct Proc_Info_ { } Proc_Info; void freeProcInfo(Proc_Info *proc); + void freeProcInfoList(); //kill this process