打印内核的进程树

时间:2017-04-25 09:30:23

标签: c linux linux-kernel kernel kernel-module

我是C和内核模块的新手。我正在尝试打印所有内核进程并打印其子代。但是,当我插入模块终端时,它只提供部分进程。我的算法首先进入进程树的根,然后逐步递归子进程。

我的代码如下:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <asm/current.h>
#include <linux/list.h>


void getProcInfo(struct task_struct* task);

static int mod_init(void)
{
    printk(KERN_ALERT "ProcInfo sucessfully loaded.\n");
    struct task_struct *task=current;


    while(task->pid != 1 ){
        task=task->parent;
    }
    //task = current->real_parent;
    getProcInfo(task);
    return 0; 
}

static void mod_exit(void)
{
        printk(KERN_ALERT "ProcInfo sucessfully unloaded.\n");
}

void getProcInfo(struct task_struct* task)
{
    struct list_head *children_tasks;
    children_tasks = &(task->children);

    list_for_each(children_tasks, &(task->children))
    {

        struct task_struct *child_task;
        child_task = list_entry(children_tasks, struct task_struct, sibling);

        printk(KERN_INFO "The parent process is \"%s\" (pid %i)\tThe process is \"%s\" (pid %i)\n",
        child_task->parent->comm, child_task->parent->pid,child_task->comm, child_task->pid);
        getProcInfo(child_task);
    }
}

module_init(mod_init);
module_exit(mod_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Nihad");

0 个答案:

没有答案