问题描述
我正在编写一个需要打开和读取文件的Linux内核模块。什么是最好的方式来完成?我能问你为什么要打开一个文件吗? b
$ b
我喜欢遵循Linux开发(出于好奇,我不是内核开发者,我是Java),之前我已经看到过这个问题的讨论。我能够找到关于这个的,基本上提到这通常是一个坏主意。我几乎肯定LWN在去年覆盖了它,但是我很难找到这篇文章。
如果这是一个私有模块(就像一些自定义硬件和模块将不会被分配),那么你可以做到这一点,但我的印象是,如果你要将你的代码提交到主线,那么它可能不被接受。
Evan Teran提到了sysfs,这对我来说似乎是个好主意。如果你真的需要做更难的定制的东西,你总是可以做出新的ioctrls。
编辑:
我找到了我正在寻找的文章,来自。它解释了为什么做这种东西通常是一个坏主意,然后继续告诉你如何做到这一点。
I'm writing a Linux kernel module that needs to open and read files. What's the best way to accomplish that?
Can I ask why are you trying to open a file?
I like to follow Linux development (out of curiosity, I'm not a kernel developer, I do Java), and I've seen discussion of this question before. I was able to find a LKML message about this, basically mentioning it's usually a bad idea. I'm almost positive that LWN covered it in the last year, but I'm having trouble finding the article.
If this is a private module (like for some custom hardware and the module won't be distributed) then you can do this, but I'm under the impression that if you are going to submit your code to the mainline then it may not be accepted.
Evan Teran mentioned sysfs, which seems like a good idea to me. If you really need to do harder custom stuff you could always make new ioctrls.
EDIT:
OK, I found the article I was looking for, it's from Linux Journal. It explains why doing this kind of stuff is generally a bad idea, then goes on to tell you exactly how to do it anyway.
这篇关于Linux内核模块中的文件I / O的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!