halm
halm

Reputation: 377

Read /proc from within a module

Perhaps I'm just going about this wrong.

I have a Linux module (Ubuntu 12.04) that needs to gather information about current processes such as the number of files each process has open, and so on. I'd thought that the best way to do this would be for the module to read /proc and for each process represented there look in the /proc subdirectory for the information it wants.

Clearly my module can't call opendir/readdir. I'd thought that there was a proc_readdir() that I could use from kernel space but I can't seem to find any information on it.

Am I missing something? Is there a better way for the module to gather process information? If it is proc_readdir() then where can I find some example of that?

Upvotes: 3

Views: 1882

Answers (1)

jleahy
jleahy

Reputation: 16855

If you're just moving to kernel-space programming it may take you quite a while to get used to doing things the right way. /proc is just an interface to the kernel-space data structures, and an inconvenient one at that (what will all the ASCII). As you're in the kernel you can access the data you want directly.

You should be looking at processes task_struct entries (see http://lxr.linux.no/linux+v3.5.3/include/linux/sched.h). If you want to iterate over each process try something like the following:

struct task_struct *task;
for_each_process(task) {
    printk(KERN_INFO "Process %i is named %s\n", task->pid, task->comm); 
}

You can also find a specific task by pid using find_task_by_pid_ns, but you'll have to worry about pid namespaces. There's also get_current, which will find you the currently executing task.

Upvotes: 4

Related Questions