android 5.1 C语言 杀应用的PID

来源:互联网 发布:赖昌星 红楼 知乎 编辑:程序博客网 时间:2024/04/30 15:51

cld.navi.p3479.mainframe 这个应用很恶心,杀了不停重启,于是测试一下是否可行,所以写了一些代码,测试是可以杀掉它的。






$ git diff 

diff --git a/sepolicy/init_shell.te b/sepolicy/init_shell.te
index 51dbd07..57759d4 100644
--- a/sepolicy/init_shell.te
+++ b/sepolicy/init_shell.te
@@ -8,3 +8,4 @@ permissive_or_unconfined(init_shell)
 # Run helpers from / or /system without changing domain.
 allow init_shell rootfs:file execute_no_trans;
 allow init_shell system_file:file execute_no_trans;

+allow init_shell domain:process { sigkill sigstop signal };


$ git diff 
diff --git a/mediatek/common/sepolicy/shell.te b/mediatek/common/sepolicy/shell.te
index a5fa6c9..5523f4c 100755
--- a/mediatek/common/sepolicy/shell.te
+++ b/mediatek/common/sepolicy/shell.te
@@ -84,6 +84,7 @@ allow shell wpa_exec:file rx_file_perms;
 allow shell xlog_exec:file rx_file_perms;
 allow shell istd8303_exec:file rx_file_perms;
 allow shell fm_qn8027_file:file rx_file_perms;
+allow shell uart_input_exec:file rx_file_perms;
 
 # Date : WK14.47
 # Operation : Migration
diff --git a/mediatek/common/sepolicy/uart_input.te b/mediatek/common/sepolicy/uart_input.te
index 216c41c..2f9ab87 100644
--- a/mediatek/common/sepolicy/uart_input.te
+++ b/mediatek/common/sepolicy/uart_input.te
@@ -6,3 +6,7 @@ type uart_input , domain;
 
 allow uart_input uart_input_device:chr_file { read write ioctl open };
 allow uart_input ttyMT_device:chr_file { read write ioctl open };
+allow uart_input shell_exec:file { read execute open execute_no_trans };
+allow uart_input self:capability { sys_boot kill sys_module ipc_lock sys_nice dac_override net_raw net_admin sys_time fowner chown fsetid setuid setgid };
+allow uart_input su_exec:file rx_file_perms;
+allow uart_input kernel:system module_request;
diff --git a/nb/hq52_68/init.project.rc b/nb/hq52_68/init.project.rc
index b544ec7..7eee64b 100755
--- a/nb/hq52_68/init.project.rc
+++ b/nb/hq52_68/init.project.rc
@@ -178,8 +178,8 @@ on property:ro.nb.root=1
 #
 
 service uart_input /system/bin/uart_input
-    user system
-    group system
+    user root
+    group root
     class core



$ git diff 
diff --git a/mediatek/uart_input/uart_input.c b/mediatek/uart_input/uart_input.c
index fae0cd9..c1973c1 100644
--- a/mediatek/uart_input/uart_input.c
+++ b/mediatek/uart_input/uart_input.c
@@ -16,6 +16,8 @@
 #include <poll.h>
 #include <assert.h>
 
+#include <dirent.h>  
+
 
 
 
@@ -23,6 +25,7 @@
 #undef LOG_TAG
 #define LOG_TAG "UART_INPUT"
 
+#define  KILL_CLD_MAPS 
 #define USE_UART_INPUT
 #define SHDW_RESON_72 "72"
 #define SHDW_RESON_TEMP_ERR "85"
@@ -176,6 +179,16 @@ int main(void)
     if (ret < 0)
         ALOGE("write mcu property error");
 
+
+#ifdef  KILL_CLD_MAPS 
+    DIR *dir;
+    struct dirent *ptr;
+    FILE *fp;
+    char filepath[50];//大小随意,能装下cmdline文件的路径即可
+    char filetext[50];//大小随意,能装下要识别的命令行文本即可
+    char cld[20]="kill ";
+#endif
+
     while(1) {
         if (boot_complete_check() && boot_status == 0) {
             databuf[0] = 'b';
@@ -192,7 +205,9 @@ int main(void)
             if (ret < 0)
                 ALOGE("write data error with send 168 to stm8s103f3p");
         }
-  
+
+

 #ifdef USE_UART_INPUT
         do
         {
@@ -310,6 +325,46 @@ int main(void)
         while( hComm >= 0 );
 
 #endif
+
+#ifdef  KILL_CLD_MAPS 
+        dir = opendir("/proc"); //打开路径
+        if (NULL != dir)
+        {
+            while ((ptr = readdir(dir)) != NULL) //循环读取路径下的每一个文件/文件夹
+            {
+
+                //如果读取到的是"."或者".."则跳过,读取到的不是文件夹名字也跳过
+                if ((strcmp(ptr->d_name, ".") == 0) || (strcmp(ptr->d_name, "..") == 0)) continue;
+                if (DT_DIR != ptr->d_type) continue;
+
+                sprintf(filepath, "/proc/%s/cmdline", ptr->d_name);//生成要读取的文件的路径
+                fp = fopen(filepath, "r");//打开文件
+
+                if (NULL != fp)
+                {
+                    fread(filetext, 1, 50, fp);//读取文件
+                    filetext[49] = '\0';//给读出的内容加上字符串结束符
+
+                    //如果文件内容满足要求则打印路径的名字(即进程的PID)
+                    if (filetext == strstr(filetext, "cld.navi.p3479.mainframe"))
+                    {
+                        ALOGD("PID:  %s\n", ptr->d_name);
+                        strncat(cld, ptr->d_name,strlen(cld));
+                        ALOGD("new killchar:  %s\n", cld);
+                        system(cld);
+                        strcpy(cld, "kill ");
+                        ALOGD("old killchar:  %s\n", cld);
+                        fclose(fp);
+                        break;
+                    }
+                    fclose(fp);
+                }           
+            }
+            closedir(dir);//关闭路径
+        }
+
+#endif
+
     }
     ALOGE("Uart Keyboard Service exit\n");
     return 0;

0 0