|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
/*
struct dirent
{
long d_ino;
off_t d_off;
unsigned short d_reclen;
unsigned char d_type;
char d_name [NAME_MAX+1];
}
*/
char rild_pid[NAME_MAX+1];
int find_rild_flag=0;
#define READ_BUF_SIZE 2048
void find_pid_by_name( char* pidName)
{
DIR *dir;
struct dirent *next;
long* pidList=NULL;
int i=0;
///proc中包括当前的进程信息,读取该目录
dir = opendir("/proc");
if (!dir)
printf("Cannot open /proc");
//遍历
while ((next = readdir(dir)) != NULL) {
FILE *status;
char filename[READ_BUF_SIZE];
char buffer[READ_BUF_SIZE];
char name[READ_BUF_SIZE];
/* Must skip ".." since that is outside /proc */
if (strcmp(next->d_name, "..") == 0)
continue;
/* If it isn't a number, we don't want it */
if (!isdigit(*next->d_name))
continue;
//设置进程
sprintf(filename, "/proc/%s/status", next->d_name);
if (! (status = fopen(filename, "r")) ) {
continue;
}
if (fgets(buffer, READ_BUF_SIZE-1, status) == NULL) {
fclose(status);
continue;
}
fclose(status);
//得到进程id
/* Buffer should contain a string like "Name: binary_name" */
sscanf(buffer, "%*s %s", name);
if (strcmp(name, pidName) == 0) {
#if 0
pidList=realloc( pidList, sizeof(long) * (i+2));
pidList[i++]=strtol(next->d_name, NULL, 0);
#else
memcpy(rild_pid,next->d_name,strlen(next->d_name));
rild_pid[strlen(next->d_name)]='\0';
find_rild_flag=1;
printf("find the pid number for zytoge process next->d_name=%s\n",next->d_name);
printf("find the pid number for zygote process zygote_pid=%s\n",rild_pid);
#endif
}
}
#if 0
if (pidList) {
pidList=0;
}
#endif
}
int main()
{
int pid;
int err;
find_pid_by_name( "zygote");
if( find_rild_flag)
{
printf("the original ZYGOTE process PID is = %s \n",rild_pid);
pid = atoi(rild_pid);
printf("pid =%d\n",pid);
err = kill(pid, SIGKILL);
if (err < 0)
{
printf("KILL ZYGOTE PROCESS FAILED BECAUSE could not kill pid: %d\n", pid);
return 0;
}
}
else
{
printf("KILL ZYGOTE PROCESS FAILED BECAUSE NO RILD PROCESS FIND\n");
return 0;
}
printf("****************KILL ZYGOTE PROCESS SUCCESS *************\n");
return 0;
}
代码中 以 android 系统 下的 "zygote” 进程 为例 , 若要杀掉其他进程 直接将 “zygote" 字符串改成要杀掉的进程 名即可 (执行时要有相应的权限)
嵌入式下编译:
arm-linux-gcc filename.c -static -o filename
运行: chmod 777 filename
./filename |
|