内核模块遍历进程和任务队列保存到proc文件中

来源:互联网 发布:无尽之剑3宝石数据修改 编辑:程序博客网 时间:2024/05/01 15:25

实现一个模块用它遍历当前进程的父进程和任务队列,并将遍历的结果输出到一个proc 文件中(遍历可以从 current 当前进程开始,父进程遍历到初始化进程,遍历任务队列可以利用 for_each_process 宏)。

下面是我的内核模块的实现部分:

/*************************************************************   使用内核模块从当前进程开始先前遍历,知道找到第一个进程为止  **   并将遍历的结果保存到proc文件中                           ************************************************************/#include <linux/module.h>#include <linux/init.h>#include <linux/kernel.h>#include <linux/proc_fs.h>#include <linux/jiffies.h>#include <asm/uaccess.h>#include <linux/moduleparam.h>#include <linux/list.h>#include <linux/sched.h>#include <asm/current.h>#define MODULE_NAME "MyProcess"#define MYDATA_LEN 10000//放用户空间的数据struct my_proc_data{    char value[MYDATA_LEN];};struct my_proc_data mydata,fathers_data;//proc结构变量struct proc_dir_entry *example_dir;//存放任务队列struct proc_dir_entry *date_file;//存放父进程struct proc_dir_entry *father_file;static int param;module_param(param,int,0644);//读文件驱动函数static int proc_read(char *page,char **start,off_t off,int count,int *eof,void *data){    int len;    struct my_proc_data *mydatap = (struct my_poroc_data *)data;    len += sprintf(page,"%s",mydatap->value);    return len;}//写文件驱动函数static int proc_write(struct file *file,const char *buffer,unsigned long count,void *data){    int len;    struct my_proc_data *mydatap = (struct my_proc_data *)data;    if(count > MYDATA_LEN)        len = MYDATA_LEN;    else        len = count;    if(copy_from_user(mydatap->value,buffer,len)){        return -EFAULT;    }    mydatap->value[len-1] = '\0';    return len;}//加载模块int init_module(void){    //创建dir文件夹    example_dir = (struct proc_dir_entry *)proc_mkdir("mydir",0);    if(example_dir == 0){        printk("mkdir fail!!\n");        return -1;    }    //创建文件    date_file = (struct proc_dir_entry *)create_proc_entry("myfile",0666,example_dir);    if(date_file == 0){        printk("create file fails!!\n");        return -ENOMEM;    }    //创建文件    father_file = (struct proc_dir_entry *)create_proc_entry("fathers",0666,example_dir);    if(father_file == 0){        printk("create file fails!!\n");        return -ENOMEM;    }    struct  task_struct *pos = get_current();    for_each_process(pos){        strcat(mydata.value,pos->comm);        strcat(mydata.value,"\n");    }    date_file->data = &mydata;    date_file->read_proc = &proc_read;    date_file->write_proc = &proc_write;    date_file->owner = THIS_MODULE;    pos = get_current();    while(pos != &init_task){        strcat(fathers_data.value,pos->parent->comm);        strcat(fathers_data.value,"=>");        strcat(fathers_data.value,pos->comm);        strcat(fathers_data.value,"\n");        pos = pos->parent;    }    father_file->data = &fathers_data;    father_file->read_proc = &proc_read;    father_file->write_proc = &proc_write;    father_file->owner = THIS_MODULE;    return 0;}//卸载模块void cleanup_module(void){    remove_proc_entry("myfile",example_dir);    remove_proc_entry("fathers",example_dir);    remove_proc_entry("mydir",NULL);    printk("GoodBye!!\n");}MODULE_LICENSE("GPL");MODULE_DESCRIPTION("This is the description");MODULE_AUTHOR("bobo");

其中的Makefile文件为:

obj-m := process.oall:    make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modulesclean:    make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean 

现在我们使用make命令编译,编译完成时候,我们动态加载内核模块

sudo insmod process.ko

然后我们查看proc文档下我们创建的文件:

cat /proc/mydir/myfile

运行后的效果为:

下面我们查看一下所有的父进程的文件:

cat /proc/mydir/fathers

运行效果为:

1 0
原创粉丝点击