Linux/UNIX编程:获取指定用户所有正在运行的进程ID和进程名

先用系统函数 `getpwnam` 获得指定用户名的 UID,然后遍历 /proc/ 中所有 PID 目录,如果 /proc/PID/status 中的 UID 是输入用户名对应的 UID 则输出该 status 文件中的进程名,进程ID就是目录名。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/fcntl.h>
#include <pwd.h>

int main(int argc, char const *argv[])
{
    //判断参数是否合法
    if(argc != 2) {
        perror("Arguments error: lack of username");
        exit(EXIT_FAILURE);
    }

    //获得指定用户名的ID
    errno = 0;
    struct passwd *pwd = getpwnam(argv[1]);
    if(NULL == pwd){
        if(errno == 0){
            char err_meg[50];
            sprintf(err_meg, "Not found the user: %s", argv[1]);
        } else {
            perror(strerror(errno));
        }
        exit(EXIT_FAILURE);
    }
    uid_t uid = pwd->pw_uid;
    //printf("%d\n", uid);

    //打开目录 /proc
    DIR *dir = opendir("/proc");
    if(NULL == dir){
        perror(strerror(errno));
        exit(EXIT_FAILURE);
    }

    errno = 0;
    struct dirent *pid_dir;
    while(pid_dir = readdir(dir)){
        // puts(pid_dir->d_name);

        //是pid目录
        if((pid_dir->d_name)[0] <= '9' && (pid_dir->d_name)[0] >= '1'){
            //构造 /proc/PID/status 文件名
            char status[50] = "/proc/";
            strcat(status, pid_dir->d_name);
            strcat(status, "/status");
            //打开
            int fd = open(status, O_RDONLY);
            if(fd == -1){
                perror(strerror(errno));
                exit(EXIT_FAILURE);
            }
            //读取
            char buffer[512];
            read(fd, buffer, 512);
            char* uid_ptr = strstr(buffer, "Uid") + 5; //UID在字符'Uid'位置向后偏移5个字符
            if((uid_t)(strtol(uid_ptr, NULL, 10)) == uid){ //strtol:将字符形式的UID转换为long,然后再转换成uit_t
                int i = 6; //进程名的在status文件开头位置向后偏移6个字符
                printf("%s\t",pid_dir->d_name);
                while(*(buffer + i) != '\n'){ //输出进程名字
                    printf("%c", *(buffer + i));
                    i++;
                }
                puts("");
            }
            //关闭
            close(fd);
        }
    }

    if(0 != errno){
        perror(strerror(errno));
        exit(EXIT_FAILURE);
    }
    closedir(dir);
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/yuanyb/p/11073519.html