Traversing task_struct-> children in linux kernel

I am trying to cross task_struct children in the linux kernel and receive information from children. I am having problems with all the information, so let me just keep it while getting the pid for simplicity.

This is the supporting part of my code.

struct list_head * p; struct task_struct ts, *tsk; pid_t tmp_pid; INIT_LIST_HEAD(&ts.children); current = tsk; list_for_each(p, &(tsk->children)){ ts = *list_entry(p, struct task_struct, children); tmp_pid = ts.pid; printk("the pid is %d\n", tmp_pid); } 

I think the problem is with list_entry, but I don’t know how to fix it, all the examples that I can find seem to call it the same way.

This should print all the child PIDs, instead I always get the same number -17 .... it is on the order of 10 ^ 9 or 10 ^ 11.

Can someone help me here? compilation takes about 30 minutes, so trying to log different things is not really an option.

+4
source share
2 answers

You have to use

 list_entry(p, struct task_struct, sibling); 

Not

 list_entry(p, struct task_struct, children); 

Ho, and also you have to lock tasklist_lock when you go through child elements.

+7
source

Destination tsk is in the wrong direction. the current one contains the current task; to initialize tsk you need to write

 tsk = current; 

FWIW, you should avoid copying structures. Therefore in the do loop

 tsk = list_entry(p, struct task_struct, children); 

thus assigning a pointer to the task, and not copying the entire structure of the task.

0
source

All Articles