从用户级空间访问内核空间中的变量

发布于 2024-08-20 15:32:15 字数 388 浏览 1 评论 0原文

因此,让我有一个想要从内核空间中定义的用户级空间读取的结构,但用户级空间有多个进程。

示例:

在内核模块中,我有一个全局结构。 结构体{ 整数a; 整数b; } 测试;

在用户级模块中,我“外部”了全局 struct

extern struct { 整数a; 整数b; } 测试;

编译器不会抱怨,链接编辑器也不会抱怨。但是,如果用户有多个进程,那么是否会为每个进程克隆该结构?如果我将共享内存与 extern 一起使用,那么我可以访问内核的结构,如果我有 n 个进程,那么自共享以来只有 1 个结构。我可以使用 1 个用户级进程访问内核级变量,但是如果我有更多进程,那么我会获得每个“外部”结构的克隆

我的问题是,多个用户级进程可以读取内核级变量吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风柔一江水 2024-08-27 15:32:15

用户空间在任何情况下都无法直接看到内核 ram - 并且 mmap'ing /dev/kmem 也不是一个好的解决方案(在我看来,它真的很丑陋,应该只用于内核调试)。

我认为最好的方法是通过 /proc 中的文件(这非常简单)或带有 IOCTL 的字符设备(这只是稍微复杂一点)来公开它。

(注意:这是 Linux / Unix 特定的)

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)

单挑你×的.吻 2024-08-27 15:32:15

在大多数操作系统上,您无法从用户空间访问内核空间变量。
您将需要通过操作系统提供的机制公开您的数据。这可以是自定义系统调用、通过 vfs 或任何其他形式的 IPC 公开的文件。

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.

金兰素衣 2024-08-27 15:32:15

在 Unix 上,这通常是通过 mmap-ing 一些特殊的设备文件(如 /dev/kmem)来完成的。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文