简体   繁体   English

如何在内核模块中创建 /proc 文件?

[英]how to create /proc file inside kernel module?

I want to save some information in a kernel module.我想在内核模块中保存一些信息。 I have seen similar question to mine here in stackoverflow, however mine is slightly different.我在stackoverflow中看到过类似的问题,但我的略有不同。 Assuming I am using this code to write in a /proc file.假设我使用此代码写入 /proc 文件。 How should I call it inside one of my kernel module?我应该如何在我的内核模块之一中调用它? I have a custom kernel module called mymodule which is not the main file (does not have init_module()) inside it the below function is called.我有一个名为mymodule的自定义内核模块,它不是主文件(没有 init_module()),下面的函数被调用。 In this case what should be the input value to the function like value of file ?在这种情况下,函数的输入值应该是什么,比如file值? Basically is it possible to create a /proc file inside a kernel module?基本上可以在内核模块中创建 /proc 文件吗?

int procfile_write(struct file *file, const char *buffer, unsigned long count, void *data)

It is definitely possible to add a proc entry in a kernel module but you might be misunderstanding how file handling works in the kernel.绝对可以在内核模块中添加 proc 条目,但您可能会误解文件处理在内核中的工作方式。

When you create a file in proc, you're not actually 'creating' a file like you would in userspace.当您在 proc 中创建文件时,您实际上并没有像在用户空间中那样“创建”文件。 You're registering a few callbacks that are called when userspace programs request your file.您正在注册一些在用户空间程序请求您的文件时调用的回调。

That means, when userspace programs request a read, your file read callback has to provide data.这意味着,当用户空间程序请求读取时,您的文件读取回调必须提供数据。 When a userspace program requests a write, your file write callback has to handle it.当用户空间程序请求写入时,您的文件写入回调必须处理它。

If you want to use it like a conventional file to store information, you have to allocate the required space and have your read callback copy data from there.如果您想像传统文件一样使用它来存储信息,则必须分配所需的空间并从那里读取回调复制数据。

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

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