简体   繁体   English

内核模块的配置文件

[英]configuration file for kernel module

I learn how to write linux kernel drivers and now I would like to give user from userspace possibility to change behaviour of my modules (or any other subsystem / module). 我学习了如何编写Linux内核驱动程序,现在我想让用户空间的用户可以更改模块(或任何其他子系统/模块)的行为。

I would like to store any value (string list) in any type of configuration file / system and in form of the value inside file / system - change the behavior of modules. 我想将任何值(字符串列表)存储在任何类型的配置文件/系统中,并以文件/系统内部的值的形式存储-更改模块的行为。

I thought about procfs , I can make module which creates /proc/file and react to read/write operations from userspace. 我考虑过procfs ,我可以使模块创建/proc/file并对来自用户空间的读/写操作做出反应。

The thing is: how to read that configuration from another, name it B, module in kernel space? 关键是:如何从内核空间的另一个配置中读取该配置(名称为B)?

Maybe another type of config is possible (I thought about sysctl but I see there is no strings stored and I have to store some kind of simple "list"). 也许另一种类型的配置是可能的(我考虑过sysctl,但是我看到没有存储任何字符串,我必须存储某种简单的“列表”)。

1) Usually, if it is configuration and some of parameter list, ioctl is more popular and recommend to use it. 1)通常,如果它是配置文件和一些参数列表,则ioctl更受欢迎,建议使用它。

2) You are asking, There are 2 kernel modules - A and B and you want to read A's configuration on B. Right? 2)您问,有2个内核模块-A和B,并且您想阅读A在B上的配置。 In this case, no matter what you have proc or ioctl (remember, proc is not real file-system. You are printing or reading and returning some value from some kernel variables... you can read contents from "fs" by using VFS in kernel and return the string but that is very very stupid since you have user space control.), eventually, you need to store your kernel module config to some variables. 在这种情况下,无论您拥有proc还是ioctl(记住,proc不是真正的文件系统。您都在打印或读取某些内核变量并从中返回一些值...您可以使用VFS从“ fs”中读取内容在内核中并返回该字符串,但这非常愚蠢,因为您具有用户空间控制权。)最终,您需要将内核模块配置存储到某些变量中。 If you want to read them from another module, your variable should be exposed by using EXPORT_SYMBOL() keyword, but usually, we don't do. 如果要从另一个模块读取它们,则应使用EXPORT_SYMBOL()关键字公开您的变量,但通常不这样做。

Create some API on your symbol which is returning the config value and expose those APIs and call it from another module. 在返回配置值的符号上创建一些API,并公开这些API并从另一个模块调用它。 Because of module dependency, you may need to be careful. 由于模块依赖性,您可能需要小心。

The easiest way to do it is, to create some callback ptr and define it under kernel source code. 最简单的方法是创建一些回调ptr并在内核源代码下定义它。 And then, when module A is initialized, configure callback ptr, and call it from module B. In this case, you will get rid of module dependency. 然后,在初始化模块A时,配置回调ptr,然后从模块B调用它。在这种情况下,您将摆脱模块依赖性。

or, create callback pointer on module B and make module B as built-in module and expose callback ptr. 或者,在模块B上创建回调指针,并使模块B成为内置模块,并公开回调ptr。

Then, from module A, you can initialize those and whenever you call it from B, you can check "null ptr" on that callback ptr. 然后,可以从模块A初始化它们,并且每当从B调用它时,都可以在该回调ptr上检查“ null ptr”。

Hope that it will help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM