Android killall 命令

来源:互联网 发布:国外网络代理服务器 编辑:程序博客网 时间:2024/06/02 20:36

Android命令自带的没有实现killall命令,当我们需要实现通过程序名kil掉一个进程是,这个时候就需要它了

下面参考了一会打个的getpid代码,通过进程名获取其piD

#include <stdio.h>#include <sys/types.h>#include <dirent.h>#include <stdlib.h>#include <string.h>#define PATH_MAX 128#include <cutils/log.h>int find_pid_by_name( char* ProcName, int* foundpid){        DIR             *dir;        struct dirent   *d;        int             pid, i;        char            *s;        int pnlen;        i = 0;        foundpid[0] = 0;        pnlen = strlen(ProcName);        /* Open the /proc directory. */        dir = opendir("/proc");        if (!dir)        {                printf("cannot open /proc");                return -1;        }        /* Walk through the directory. */        while ((d = readdir(dir)) != NULL) {                char exe [PATH_MAX+1];                char path[PATH_MAX+1];                int len;                int namelen;                /* See if this is a process */                if ((pid = atoi(d->d_name)) == 0)       continue;                snprintf(exe, sizeof(exe), "/proc/%s/exe", d->d_name);              LOGI("find_pid_by_name  exe = %s.\n", exe);                if ((len = readlink(exe, path, PATH_MAX)) < 0)                        continue;                path[len] = '\0';                /* Find ProcName */                s = strrchr(path, '/');                if(s == NULL) continue;                s++;                /* we don't need small name len */                namelen = strlen(s);                LOGI("s = %s.\n", s);                if(namelen < pnlen)     continue;                if(!strncmp(ProcName, s, pnlen)) {                        /* to avoid subname like search proc tao but proc taolinke matched */                        if(s[pnlen] == ' ' || s[pnlen] == '\0') {                                foundpid[i] = pid;                                i++;                        }                }        }        foundpid[i] = 0;        closedir(dir);        return  0;}int main(int argc, char *argv[]){        int i, rv, pid_t[128];        int status = 0;                if ( argc != 2 )        {               LOGI("Usage %s procname\n",argv[0]);                return 0;        }        rv = find_pid_by_name( argv[1], pid_t);        LOGI("rv = %d.\n", rv);        for(i=0; pid_t[i] != 0; i++)        {        char process_pid[20];int pid    = 0;memset(&process_pid, 0x0, 10);sprintf(process_pid, " %d", pid_t[i]);LOGI("process_pid = %s.\n", process_pid);pid = vfork();if (pid < 0){LOGI( "(out)vfork error");continue;}if (0 == pid){execl("/system/bin/kill", "/system/bin/kill"," -9", process_pid, (char*)0);}else{while(waitpid(pid, &status, 0) < 0){printf(  "waitpid error pid:%d \n", pid);break;}}}        return 0; }

通过上面的程序就可以通过程序名KIll掉一个进程了。

原创粉丝点击