Reputation: 127
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/module.h>
int start(void){
printk("starting module\n");
printk("pid\ttask_name\tstate\n");
struct task_struct *task;
struct list_head *list;
list_for_each(list,&init_task->children){
task=list_entry(list,struct task_struct,sibling);
printk("%5d\t%s\t%li\n",task->pid,task->comm,task->state);
}
return 0;
}
void ending(void){
printk("closing module\n");
}
module_init(start);
module_exit(ending);
When I compile, an error this below shows up
error: invalid type argument of ‘->’ (have ‘struct task_struct’)
list_for_each(list,&init_task->children){
^
include/linux/list.h:408:14: note: in definition of macro ‘list_for_each’
for (pos = (head)->next; pos != (head); pos = pos->next)
I think the error msg is trying to say that "&init_task" should have "struct task_struct" but doesn't this suffice? As far as I know, variable 'init_task' is externally declared in 'linux/sched.h' which means that the error shouldn't be caused by not being able to locate or find 'init_task'.
Since a pointer should come in front of '->', using '&init_task' seems right.
Could someone point out what I'm missing here?
---update: solved ---
reconsidering operator precedences of '->' and '&', the proper use is either '(&init_task)->children' or 'init_task.children'. However, even with this changed, an error came up:
include/linux/list.h:408:31: error: invalid operands to binary != (have ‘struct list_head *’ and ‘struct list_head’)
for (pos = (head)->next; pos != (head); pos = pos->next)
This error msg pointed out that I should be providing the address of 'children' in 'struct task_struct'. Thus, I changed the problematic line as:
list_for_each(list,&(init_task.children)){
which solved the problem and the compilation went smoothly.
Upvotes: 2
Views: 1935
Reputation: 18361
init_task
is a structure, not pointer. So you should either convert it to the pointer prior dereferencing (&init_task)->children
, or access children
using the period notation init_task.children
. The &init_task->children
is denoting the address of the children
field of the structure pointed by init_task
, if it were pointer.
Upvotes: 5