regeagainstthecage.c源代码
来源:互联网 发布:全国c语言二级考试题库 编辑:程序博客网 时间:2024/05/16 17:46
//头文件包含
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
//输出错误消息后退出程序
void die(const char *msg)
{
perror(msg);
exit(errno);
}
//搜索ADB进程,返回其PID,没有找到时返回0
pid_t find_adb()
{
char buf[256];
int i = 0, fd = 0;
pid_t found = 0; //初始化为0,如果没有找到adb将一直保持0值
//遍历进程ID的有效范围
for (i = 0; i < 32000; ++i)
{
//拼接字符串"/proc/<PID>/cmdline"
sprintf(buf, "/proc/%d/cmdline", i);
//打开失败,进程不存在或无权访问
if ((fd = open(buf, O_RDONLY)) < 0)
{
//跳过这个PID,继续下一轮循环
continue;
} //end if
//buf填0
memset(buf, 0, sizeof(buf));
//读取进程的命令行
read(fd, buf, sizeof(buf) - 1);
//关闭进程
close(fd);
//在命令行中查找"/sbin/adb"
if (strstr(buf, "/sbin/adb"))
{
#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <stdlib.h>
//输出错误消息后退出程序
void die(const char *msg)
{
}
//搜索ADB进程,返回其PID,没有找到时返回0
pid_t find_adb()
{