自动查找进程PID 并关闭

来源:互联网 发布:机房机柜网络整改 编辑:程序博客网 时间:2024/06/05 16:06
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h> // for opendir(), readdir(), closedir()
#include <sys/stat.h> // for stat()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>


#define PROC_DIRECTORY "/proc/"
#define CASE_SENSITIVE    1
#define CASE_INSENSITIVE  0
#define EXACT_MATCH       1
#define INEXACT_MATCH     0


//是不是数字
int IsNumeric(const char* ccharptr_CharacterList)
{
    for ( ; *ccharptr_CharacterList; ccharptr_CharacterList++)
        if (*ccharptr_CharacterList < '0' || *ccharptr_CharacterList > '9')
            return 0; // false
    return 1; // true
}


//intCaseSensitive=0大小写不敏感
int strcmp_Wrapper(const char *s1, const char *s2, int intCaseSensitive)
{
    if (intCaseSensitive)
        return !strcmp(s1, s2);
    else
        return !strcasecmp(s1, s2);
}


//intCaseSensitive=0大小写不敏感
int strstr_Wrapper(const char* haystack, const char* needle, int intCaseSensitive)
{
    if (intCaseSensitive)
        return (int) strstr(haystack, needle);
    else
        return (int) strcasestr(haystack, needle);
}


pid_t GetPIDbyName_implements(const char* cchrptr_ProcessName, int intCaseSensitiveness, int intExactMatch)
{
    char chrarry_CommandLinePath[100]  ;
    char chrarry_NameOfProcess[300]  ;
    char* chrptr_StringToCompare = NULL ;
    pid_t pid_ProcessIdentifier = (pid_t) -1 ;
    struct dirent* de_DirEntity = NULL ;
    DIR* dir_proc = NULL ;


    int (*CompareFunction) (const char*, const char*, int) ;


    if (intExactMatch)
        CompareFunction = &strcmp_Wrapper;
    else
        CompareFunction = &strstr_Wrapper;




    dir_proc = opendir(PROC_DIRECTORY) ;
    if (dir_proc == NULL)
    {
        perror("Couldn't open the " PROC_DIRECTORY " directory") ;
        return (pid_t) -2 ;
    }


    // Loop while not NULL
    while ( (de_DirEntity = readdir(dir_proc)) )
    {
        if (de_DirEntity->d_type == DT_DIR)
        {
            if (IsNumeric(de_DirEntity->d_name))
            {
                strcpy(chrarry_CommandLinePath, PROC_DIRECTORY) ;
                strcat(chrarry_CommandLinePath, de_DirEntity->d_name) ;
                strcat(chrarry_CommandLinePath, "/cmdline") ;
                FILE* fd_CmdLineFile = fopen (chrarry_CommandLinePath, "rt") ;  //open the file for reading text
                if (fd_CmdLineFile)
                {
                    fscanf(fd_CmdLineFile, "%s", chrarry_NameOfProcess) ; //read from /proc/<NR>/cmdline
                    fclose(fd_CmdLineFile);  //close the file prior to exiting the routine


                    if (strrchr(chrarry_NameOfProcess, '/'))
                        chrptr_StringToCompare = strrchr(chrarry_NameOfProcess, '/') +1 ;
                    else
                        chrptr_StringToCompare = chrarry_NameOfProcess ;


                    //printf("Process name: %s\n", chrarry_NameOfProcess);
                    //这个是全路径,比如/bin/ls
                    //printf("Pure Process name: %s\n", chrptr_StringToCompare );
                    //这个是纯进程名,比如ls


                    //这里可以比较全路径名,设置为chrarry_NameOfProcess即可
                    if ( CompareFunction(chrptr_StringToCompare, cchrptr_ProcessName, intCaseSensitiveness) )
                    {
                        pid_ProcessIdentifier = (pid_t) atoi(de_DirEntity->d_name) ;
                        closedir(dir_proc) ;
                        return pid_ProcessIdentifier ;
                    }
                }
            }
        }
    }
    closedir(dir_proc) ;
    return pid_ProcessIdentifier ;
}


    //简单实现
    pid_t GetPIDbyName_Wrapper(const char* cchrptr_ProcessName)
    {
            return GetPIDbyName_implements(cchrptr_ProcessName, 0,0);//大小写不敏感
    }


int main()
{
    pid_t pid = GetPIDbyName_Wrapper("dbuff") ; // If -1 = not found, if -2 = proc fs access error
    //printf("PID dbuff %d\n", pid);
    char tmp[20];
    sprintf(tmp,"kill %d",pid);
    system(tmp);

    return EXIT_SUCCESS ;
}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 吃了减肥药拉肚子怎么办 遇到他心通的人怎么办 被茅山术害了怎么办 鱼缸鱼身上烂了怎么办 鳄鱼龟皮肤烂了怎么办 墨水渗透进皮肤里了怎么办 中药渗透到皮肤里怎么办 甘露醇渗透到皮肤下怎么办 水银弄到眼睛里怎么办 水银粘到皮肤上怎么办 榴莲和虾同吃了怎么办 吃榴莲和虾中毒怎么办 榴莲和虾一起吃怎么办 狗被别人下毒了怎么办 大掌门2没存元宝怎么办 大掌门2转换阵容怎么办 率土之滨s2绝版怎么办 异界气息的装备怎么办 vivo电板没电了怎么办 门套拼接有缝隙怎么办 公司如果一直没有上税收入怎么办 赛车输了俩百万怎么办 交pk金员工不交怎么办 员工不想交pk金怎么办 心悦光环领错角色怎么办 心悦光环领错了怎么办 扑克牌1到13洗后怎么办 南通长牌没钱了怎么办 镇魔曲手游阵营人数已满怎么办 镇魔曲忘记在哪个区怎么办 登录镇魔曲卡在实名验证怎么办 电脑玩联盟花屏怎么办 优盘文件或目录损坏怎么办 苹果下吃鸡设备不兼容怎么办 龙之谷账号忘了怎么办 不花钱的排风除湿怎么办 苹果平板id密码忘了怎么办 苹果平板忘记id及密码怎么办 饥荒抓到的兔子怎么办 苹果手机下载不了王者荣耀怎么办 ipad登录显示验证失败怎么办