存储阵列结构在内核空间,Linux的 [英] Storing struct array in kernel space, Linux

查看:89
本文介绍了存储阵列结构在内核空间,Linux的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我可能过度思考这个问题有点...我已经得到了位于我的文件系统,我在开机分析和结果存入结构数组的文本文件。我需要复制从用户空间到内核空间(调用copy_from_user)此数组,并且必须有这数据通过随时内核访问。在内核空间中的数据将需要由Sockets.c文件进行访问。是否有存储内核空间中的数组一个特殊的地方,或者我可以简单地添加一个参考Sockets.c数组?我的C是有点生疏...

I believe I may be over-thinking this problem a bit... I've got a text file located on my filesystem which I am parsing at boot and storing the results into an array of structs. I need to copy this array from user space into kernel space (copy_from_user), and must have this data accessible by the kernel at any time. The data in kernel space will need to be accessed by the Sockets.c file. Is there a special place to store an array within kernel space, or can I simply add a reference to the array in Sockets.c? My C is a bit rusty...

感谢您的任何建议。

推荐答案

我相信,在你的问题两个主要部分:

I believe there are two main parts in your problem:


  • 从用户空间传递的数据内核空间

  • Passing the data from userspace to kernelspace

在内核空间存储数据

对于第一个问题,我建议使用一个网络链路插座,而不是更传统的系统调用(读/写/ IOCTL)接口。网络链路插座允许配置数据传递到内核使用套接字一样的界面,这是显著简单和更安全的使用。

For the first issue, I would suggest using a Netlink socket, rather than the more traditional system call (read/write/ioctl) interface. Netlink sockets allow configuration data to be passed to the kernel using a socket-like interface, which is significantly simpler and safer to use.

您的程序应该执行所有的输入解析和验证,然后将数据传递给内核,preferably更结构化形式大于大规模数据块中(例如入门通过入境)。

Your program should perform all the input parsing and validation and then pass the data to the kernel, preferably in a more structured form (e.g. entry-by-entry) than a massive data blob.

除非你有兴趣的高吞吐量(每秒兆字节的数据),该网络连接接口是罚款。以下链接提供了一个解释,以及一个例子:

Unless you are interested in high throughput (megabytes of data per second), the netlink interface is fine. The following links provide an explanation, as well as an example:

http://en.wikipedia.org/wiki/Netlink

http://www.linuxjournal.com/article/7356

http://linux-net.osdl.org/index.php/Generic_Netlink_HOWTO

http://www.kernel.org/doc/Documentation/connector/

至于阵列存储云,如果你打算比数据的存储128KB越多,你将不得不使用的vmalloc()分配空间,否则的kmalloc()是preferred。你应该阅读的Linux设备驱动程序本书的相关章节:

As far as the array storage goes, if you plan on storing more than 128KB of data you will have to use vmalloc() to allocate the space, otherwise kmalloc() is preferred. You should read the related chapter of the Linux Device Drivers book:

http://lwn.net/images/pdf/LDD3/ch08.pdf

请注意,与vmalloc的()分配的缓冲区是不适合的DMA向/从设备,由于存储器页是不连续的。您可能还需要考虑像列表的更复杂的数据结构,如果你不知道你有多少项事先有。

Please note that buffers allocated with vmalloc() are not suitable for DMA to/from devices, since the memory pages are not contiguous. You might also want to consider a more complex data structure like a list if you do not know how many entries you will have beforehand.

作为全球访问存储,你可以做到这一点与任何C程序:

As for accessing the storage globally, you can do it as with any C program:

在由您需要访问数据的所有.c文件包含一个头文件把这样的:

In a header file included by all .c files that you need to access the data put something like:

extern struct my_struct *unique_name_that_will_not_conflict_with_other_symbols;

的extern 关键字表示该声明是在另一个源文件中实现的变量。这将使得这个指针入店到所有的C文件,其中包括这个头。
然后在C文件,preferrably一个与你的code的剩余部分 - 如果存在的话:

The extern keyword indicates that this declares a variable that is implemented at another source file. This will make this pointer accesible to all C files that include this header. Then in a C file, preferrably the one with the rest of your code - if one exists:

struct my_struct *unique_name_that_will_not_conflict_with_other_symbols = NULL;

这是实际执行中的头文件中声明的变量。

Which is the actual implementation of the variable declared in the header file.

PS:如果你要与Linux内核工作,你真的需要刷上你的C.否则,你会在一些非常令人沮丧的时刻,你会最终遗憾和痛

PS: If you are going to work with the Linux kernel, you really need to brush up on your C. Otherwise you will be in for some very frustrating moments and you WILL end up sorry and sore.

PS2:您也将节省大量的时间,如果你至少在整个Linux设备驱动程序的书脱脂。尽管它的名字和它相对的时代,有很多写任何code Linux内核时是当前和重要信息。

PS2: You will also save a lot of time if you at least skim through the whole Linux Device Drivers book. Despite its name and its relative age, it has a lot of information that is both current and important when writing any code for the Linux Kernel.

这篇关于存储阵列结构在内核空间,Linux的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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