2
votes

I am new to kernel module progrmming. In my program I need to display the process name and its parent's pid.

The following is my simple_init() function

int simple_init(void){

printk(KERN_INFO "--------------Starting module--------------\n");

struct task_struct *intask = &init_task;

struct list_head fd = intask->children;

struct list_head *list; 
struct task_struct *task;

int k =0;
list_for_each(list, &fd){

    task = list_entry(list, struct task_struct, sibling);
    struct task_struct *parent = task->parent;
    pid_t parent_pid = parent -> pid;           

    printk(KERN_INFO "Name: %s ---- %d ----Parent: %d\n",task->comm, task->pid, parent_pid);

    if (k==2) break;

    k++;
}

return 0;
}

Problem is that after I added the line:

struct task_struct *parent = task->parent;

Now, when I run the insmod command it says Segementation fault, and I have to restart the machine (virtual machine) to try again.

Can anyone of you show me what's wrong with this?

1
If you'd leave out the question irrelevant chit-chat about you being new to progrmming from your post, you would also have one typo less.Anthon
Make a list of all the assumptions made in your code. Test those assumptions. For example, intask->children is what? list is what? task is what? task->parent is what?Zan Lynx

1 Answers

0
votes

Kernel code, in particular module initialization code, is not always running on behave of some process. Both interrrupt & scheduler related code is running without any particular process.

So I guess that your task could be NULL