如何访问Linux内核数据结构



我想打印每个进程的信息以及该进程在运行时正在做什么。即哪个文件被该进程连续地读写。

为此,我正在编写一个内核模块。

有人知道如何在内核模块中访问此信息或如何访问我的内核模块中的进程表数据结构吗?

任务的伪代码将是这样的:

1. get each process from /proc.
2. Access the data structure of that process i.e. process table and all
3. print what that process is doing i.e. which file it is accessing (i.e. reading or writing) at rutime.

请看下面的例子。

它特别展示了如何创建一个内核模块来打印进程打开的文件(并依赖于我在评论中提到的从当前宏获得的task_struct结构)。可以通过task_struct struct进程访问更复杂的东西。

/include/linux/sched.h中声明了一个名为for_each_process的宏

http://lxr.free-electrons.com/source/include/linux/sched.h L2621

通过使用这个宏,可以遍历所有进程的task_struct
http://lxr.free-electrons.com/source/include/linux/sched.h L1343

最新更新