Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.2 kB
1
Indexable
Never
#include<linux/init.h>
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/sched.h>
#include<linux/init_task.h>

void displayTree(struct task_struct *root, int level){
    struct list_head *oneEntry;
    struct task_struct *p;
    int i=0;
    char tab[100]="";
    
    while(i<level){
        strcat(tab, "    |");
        i++;
    }

    
    printk("%sparent: %d \n", tab, root->pid);
    printk("%schildren:-\n", tab);
    list_for_each(oneEntry, &root->children){
        p=list_entry(oneEntry, struct task_struct, sibling);
        printk("%spid: %d: %s :state- %ld", tab, p->pid, p->comm, p->state);
        if(!list_empty(&p->children)){
            displayTree(p,level+1);
        }
    } 
}

int __init simple_init(void){
    printk(KERN_INFO "loading module\n");
    displayTree(&init_task, 0);
    printk(KERN_INFO "module loaded\n");
    return 0;
}

void __exit simple_exit(void){
    printk(KERN_INFO "removing module");
}

module_init(simple_init);
module_exit(simple_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Module to list all running processes in tree format");
MODULE_AUTHOR("");