Accessing variable in kernel-space from user-level space

扶醉桌前 提交于 2019-12-08 04:32:45

问题


So let's I have a struct that I want to read from user-level space that is defined in the kernel-space, but the user-level space has multiple processes.

Example:

In a kernel module, I have a global struct. struct { int a; int b; } test;

In a user-level module, I have "externed" that global struct

extern struct { int a; int b; } test;

Compiler doesn't complain, and linkage editor doesn't complain. However, if the user has multiple processes, then is that struct cloned for each process? If I use shared memory along with extern, then I could access the kernel's struct, and if I have n processes, then there's only 1 struct since its shared. I can access a kernel-level variable with 1 user-level process, but if I have more processes, then I get clones for each struct that is "externed"

My question is, Can multiple user-level processes read a kernel-level variable?


回答1:


Userspace cannot see kernel ram directly in any case - and mmap'ing /dev/kmem isn't a good solution either (it is really ugly in my opinion and should only be used for kernel debugging).

I think the nicest way is to expose it either through a file in /proc (which is pretty easy) or a character-device with an IOCTL (which is only slightly more complicated).

(NB: this is Linux / Unix specific)




回答2:


On most operating systems you cannot access kernel space variables from user space.
You will need to expose your data via the mechanisms that your os provides. This could be a custom system call, a file exposed via the vfs or any other form of IPC.




回答3:


On Unix this is usually done by mmap-ing some special device file like /dev/kmem.



来源:https://stackoverflow.com/questions/2202160/accessing-variable-in-kernel-space-from-user-level-space

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!