views:

698

answers:

2

I am trying to traverse a task_struct's children in the linux kernel and get information from the children. I'm having problems with all the information, so let's just keep it at the getting the pid for simplicity.

This is the relavant 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 I can find seem to be calling it the same way.

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

can anyone help me out here? compiling takes about 30 minutes, so trying a log of different things isn't really an option.

A: 

The assignment to tsk is in the wrong direction. current contains the, well, current task; to initialize tsk, you need to write

tsk = current;

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

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

thus assigning to task pointer, rather than copying the entire task struct.

Martin v. Löwis
oh whoops - ignore the reversed current/tsk assignment. In my real code that is corrict.
devin
so, that means that your problem is still unresolved?
DigitalRoss
yes, I haven't figured it out yet.
devin
+1  A: 

You should be using

list_entry(p, struct task_struct, sibling);

Not

list_entry(p, struct task_struct, children);

Ho and also, you should lock the tasklist_lock when you go through the childrens.

Nicolas Viennot