如何使用C从PID获取进程名称 [英] How to get process name from PID using C

查看:649
本文介绍了如何使用C从PID获取进程名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从task_struct获取进程名称,但是 我收到一个将指针指向不完整类型(任务-> comm)的错误. 我必须使用pid_task函数. 我不知道为什么它不起作用.

I want to get the process name from task_struct, but I get an error dereferencing pointer to incomplete type (task->comm). I have to use pid_task function. I have no idea why it does not work.

ssize_t simple_read(struct file *filp, char __user *user_buf, size_t count, loff_t *f_pos) {

    int len=0;
    pid_struct = find_get_pid(pid);
    task = pid_task(pid_struct,PIDTYPE_PID);

    len = sprintf(user_buf,"\nname %s\n ",task->comm);
    return len;
}

推荐答案

要查找进程的task_struct,我们可以使用kernel/pid.c中定义的pid_task函数.

To find the task_struct of a process we can make use of the function pid_task defined in kernel/pid.c .

struct task_struct *pid_task(struct pid *pid, enum pid_type type)

参数:

pid : Pointer to the struct pid of the process. 
pid_type:  PIDTYPE_PID,
       PIDTYPE_PGID,
          PIDTYPE_SID,
          PIDTYPE_MAX

要查找具有进程的pid的pid结构,可以使用在kernel/pid.c中定义的functionfind_get_pid

To find the pid structure if we have the pid of a process we can use the functionfind_get_pid which is also defined in kernel/pid.c

struct pid *find_get_pid(pid_t nr)

在下面的模块中,我们创建一个名为task_by_pid的读/写proc条目.我们想要找到使用它的pid的task_struct的哪个进程,我们都可以将其写入proc条目中.

In the below module we create a read/write proc entry named task_by_pid. Which ever process we want to find the task_struct of using its pid we can write to number into the proc entry.

当我们阅读proc条目时,它将显示与我们写入其中的pid相对应的进程的名称.

When we read the proc entry, it will display the name of the process corresponding to the pid we wrote into it.

proc_task_pid:

#include <linux/module.h>
#include <linux/kernel.h>
#include <asm/uaccess.h>
#include <linux/fs.h> 
#include <linux/cdev.h>
#include <linux/proc_fs.h>
#include <linux/pid.h>

#include <linux/pid_namespace.h>
int p_id;
struct pid *pid_struct;
struct task_struct *task;
static struct proc_dir_entry *proc_write_entry;
char *proc_name="task_by_pid";

int read_proc(char *buf,char **start,off_t offset,int count,int *eof,void *data )
{
int len=0;
pid_struct = find_get_pid(p_id);
task = pid_task(pid_struct,PIDTYPE_PID);

 len = sprintf(buf,"\nname %s\n ",task->comm);

return len;
}

int write_proc(struct file *file,const char *buf,int count,void *data )
{
int ret;
char *id;
id = (char *)kmalloc(1000*sizeof(char),GFP_KERNEL);
printk(KERN_INFO "buf passed %s",buf);
if(copy_from_user(id,buf,count))
    return -EFAULT;
printk(KERN_INFO "id passed %s",id);
p_id = simple_strtoul(id,NULL,0);
printk(KERN_INFO "pid %d ret %d",p_id,ret);
return sizeof(buf);
}

void create_new_proc_entry()
{
proc_write_entry = create_proc_entry(proc_name,0666,NULL);
if(!proc_write_entry)
      {
    printk(KERN_INFO "Error creating proc entry");
    return -ENOMEM;
    }
proc_write_entry->read_proc = read_proc ;
proc_write_entry->write_proc = write_proc;
printk(KERN_INFO "proc initialized");

}



int proc_init (void) {
    create_new_proc_entry();
    return 0;
}

void proc_cleanup(void) {
    printk(KERN_INFO " Inside cleanup_module\n");
    remove_proc_entry(proc_name,NULL);
}
MODULE_LICENSE("GPL");   
module_init(proc_init);
module_exit(proc_cleanup);

使用以下make文件对其进行编译:

ifneq ($(KERNELRELEASE),) 
   obj-m := proc_task_pid.o
else 

KERNELDIR ?= /lib/modules/$(shell uname -r)/build 

PWD := $(shell pwd)

default: 
 $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
endif 
clean:
 $(MAKE) -C $(KERNELDIR) M=$(PWD) clean

Compile it using 

$make 

将其插入内核:

$ insmod proc_task_pid.ko 

现在,让我们尝试使用始终为init的pid"1"查找进程的名称.

Now let us try to find the name of the process with pid "1", which is always init.

$ printf "1" > /proc/task_by_pid 
$ cat /proc/task_by_pid
name init

按预期,输出为"init".因此,我们可以使用其pid查找任何进程的task_struct.

As expected the output is "init". Thus we can find the task_struct of any process using its pid.

此处.

这篇关于如何使用C从PID获取进程名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆