内核模块编程输出进程树
[code=C/C++][/code]
//帮忙看看程序哪里有问题,加载模块后不停止的输出
#include <linux/sched.h> //task_struct
#include <linux/unistd.h> //unix std
#include <linux/list.h> //list_entry ,list_head
#include <linux/init.h> //needed by macros
#include <linux/module.h> //needed by all modules
#include <linux/kernel.h> //needed by all kernel programming
MODULE_LICENSE("GPL");
static int num=-1; //decide means of print tree
module_param(num,int,S_IRUGO); //input num via "insmod"
struct proc_info
{
struct list_head *task; //point to struct task_struct->tasks
char flag; //visited or not
int rec; //number of fathers
};
void pstree(struct proc_info *proc, int total, int ppid, int rec) //print ptree
{
int i,k;
for (i=0; i<total; i++)
{
if(proc[i].flag == 0 && list_entry(proc[i].task,struct task_struct,tasks)->parent->pid==ppid)
{
proc[i].rec=rec+1;
proc[i].flag=1;
for(k=0; k<rec; k++)
{
printk( KERN_ALERT " ");
}
if(list_entry(proc[i].task,struct task_struct,tasks)->pid>0)
{
printk(KERN_ALERT "├──%s(%d)\n",list_entry(proc[i].task,struct task_struct,tasks)->comm,list_entry(proc[i].task,struct task_struct,tasks)->pid);
}
pstree(proc,total,list_entry(proc[i].task,struct task_struct,tasks)->pid,proc[i].rec);
}
}
}
static int printree_init(void) //initialize the module
{
struct task_struct* p;
struct proc_info proc[512];
int total=0;
int rec=0;
proc[total++].task=&p->tasks;
for ( p=&init_task; (p=list_entry((p)->tasks.next,struct task_struct,tasks))!=&init_task; total++)
{
proc[total].task=&p->tasks;
}
memset(&proc->flag,0,total);//将proc.flag的所有字节初始化为0
memset(&proc->rec,0,total);
if (num<0)
{
pstree(proc,total,0,rec);
}
else
{
}
return 0;
}
static void printree_exit(void) //exit from the module
{
printk( KERN_ALERT "Hello, kernel!\nGoodbye user\n");
}
module_init(printree_init);
module_exit(printree_exit);
------解决方案--------------------
struct proc_info proc[512]; 内核栈一共8K,这一下就用了至少4K甚至很可能是6K,够狠
proc[total++].task=&p->tasks; 这时候p还没初始化吧?
memset(&proc->flag,0,total); proc是个数组,proc->flag是什么意思?能编译过?