lsof

作者: Jesson3264 | 来源:发表于2021-05-08 16:43 被阅读0次
    #include <fcntl.h>
    #include <iostream>  
    #include <ctime>   
    #include <sys/types.h>    
    #include <sys/stat.h>
    #include <errno.h>
    #include <unistd.h>  
    #include <stdio.h>
    #include <time.h>
    #include <stdio.h>
    #include <sys/types.h>   
    #include <dirent.h>
    #include <stdio.h>
    #include <string.h>
    #include <errno.h> 
    
    int Lsof(char *strFileSubPath)
    {
        DIR *dir, *fdir;
        struct dirent *dirent, *fdirent;
        char strTmpPath[256] = "\0";
        char strSubPath[256] = "\0";
        char strHold[256] = "\0";
        if(NULL == strFileSubPath)
        {
            printf("Lsof函数参数k为空");
            return -1;
        }
    
        dir = opendir("/proc");
        if(NULL == dir)
        {
            printf(0, "打开文件路径[%s]出错,错误信息[%s]", "/proc", strerror(errno));
            return -1;
        }
    
        while(NULL != (dirent = readdir(dir)))
        {
            if(0 == strcmp(".", dirent->d_name) || 0 == strcmp("..", dirent->d_name))
                continue;
            sprintf(strTmpPath, "/proc/%s/fd/", dirent->d_name);
    
            fdir = opendir(strTmpPath);
            if(NULL == fdir)
            {
                continue;
            }
            while(NULL != (fdirent = readdir(fdir)))
            {
                if(0 == strcmp(".", fdirent->d_name) || 0 == strcmp("..", fdirent->d_name))
                    continue;
    
                sprintf(strSubPath, "%s/%s", strTmpPath, fdirent->d_name);
    
                memset(strHold, 0, 256);
                readlink(strSubPath, strHold, 256);
                printf("subpath:%s strhold:%s \n", strSubPath, strHold);
    
                if(0 == strcmp(strFileSubPath, strHold))
                {
                    return 1;
                }
            }
            closedir(fdir);
        }
        closedir(dir);
    
        return 0;
    }
    
    int main()
    {
        pid_t pid = fork();
        char filebuffer[100] = {"/home/jesson/cpp/test.txt"};
        if (pid == 0)
        {
    #if 1
            int fd = open(filebuffer, O_RDWR);  
            if (fd < 0)
            {
                printf("open filed errno:%d\n", errno);
                return 0;
            }
    #endif
    
            printf("open successed.\n");
            while (1)
            {
                usleep(100);
            }
    
        }
        else if (pid > 0)
        {
            usleep(100000);
    
            struct stat st;
            stat(filebuffer, &st);
            printf("st.nlink:%d\n", st.st_nlink);
    
            printf("start del\n");
            if (Lsof(filebuffer))
            {
                printf("has some body in use.\n");
            }
    
            int ret = unlink(filebuffer);   
            printf("ret:%d\n", ret);
            while (1)
                usleep(100000);
        }
        else 
            printf("errorll");
    
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:lsof

          本文链接:https://www.haomeiwen.com/subject/hrnudltx.html