从用户级空间访问内核空间中的变量 [英] Accessing variable in kernel-space from user-level space

查看:134
本文介绍了从用户级空间访问内核空间中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

示例:

在内核模块中,我有一个全局结构. 结构{ 诠释 int b; }测试;

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

外部结构{ 诠释 int b; }测试;

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

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

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?

推荐答案

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

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).

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

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).

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

(NB: this is Linux / Unix specific)

这篇关于从用户级空间访问内核空间中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆