从内核代码的其他部分访问 /proc fs 变量
我正在尝试让用户级程序通过 /proc 与内核进行通信。
我按照 tldp 上的说明进行操作,并成功创建了自定义proc 文件,使用 insmod 动态加载它,并从用户态读取 (cat) 和写入 (echo) 到 proc 文件。
现在我的问题是如何从内核的另一部分(例如系统调用基础结构)访问 /proc 变量(它是一个字节缓冲区)?由于自定义 proc 文件是动态加载和链接的,如何从静态编译的内核代码中引用它?
系统规格:Ubuntu 10.10 在 MacBook Pro 13" (2009) 上的 VMWare Fusion 中运行。
编辑:相关代码(根据请求)-
procfile.c
//This function is called when the module is loaded
int init_module()
{
/* create the /proc file */
EXPORT_SYMBOL(procfs_buffer);
EXPORT_SYMBOL(procfs_buffer_size);
...
...
}
get_procvariable.c(在内核的另一部分)
//The buffer used to store character for this module
extern char * procfs_buffer;
//The size of the buffer
extern unsigned long procfs_buffer_size;
int get_procvariable(void)
{
.. do something
return procfs_buffer; // LD Error: Undefined reference
}
请在评论中告诉我,如果您需要更多详细信息,请提前致谢。
I'm trying to get a user-level program to communicate with the kernel via /proc.
I followed the instructions on tldp, and was successfully able to create a custom proc file, load it dynamically with insmod and read (cat) and write (echo) to the proc file from userland.
Now my question is how do I access the /proc variable (it's a byte buffer) from within another part of the kernel, say the system call infrastructure? Since the custom proc file is dynamically loaded and linked, how can I reference it from statically compiled kernel code?
System specs: Ubuntu 10.10 running in VMWare Fusion on a MacBook Pro 13" (2009).
Edit: Pertinent code (by request) -
procfile.c
//This function is called when the module is loaded
int init_module()
{
/* create the /proc file */
EXPORT_SYMBOL(procfs_buffer);
EXPORT_SYMBOL(procfs_buffer_size);
...
...
}
get_procvariable.c (In another part of the kernel)
//The buffer used to store character for this module
extern char * procfs_buffer;
//The size of the buffer
extern unsigned long procfs_buffer_size;
int get_procvariable(void)
{
.. do something
return procfs_buffer; // LD Error: Undefined reference
}
Do let me know in the comments, if you need further details. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
回答了我自己的问题,从上面的答案中得到了一些提示:
我缺少的关键是我需要在内核本身内声明一个变量(比如 int kernel_var = 0;)(而不是在 procfs 开销模块内,因为我之前做错了)。此后使用 EXPORT_SYMBOL 导出它,将其添加到全局模块符号表中,最后将其作为外部变量包含在开销 procfs 模块中。
因此本质上开销变量已经存在于内核中,我只是使用 procfs 模块将其作为外部变量引用并修改其值。
我编写了这个假设,它发挥了神奇的作用。
Answered my own question, taking a few hints from the answers above:
The key thing I was missing was that I needed to declare a variable (say int kernel_var = 0;) within the kernel itself (and not within the procfs overhead module, as I had incorrectly done before). Thereafter export this with EXPORT_SYMBOL, which adds it to the global module symbol table and finally include it in the overhead procfs module as an extern variable.
So essentially the overhead variable already exists within the kernel and I'm simply using the procfs module to reference it as an extern variable and modify its value.
I coded out this hypothesis and it worked like a charm.
您不应该从静态加载的代码(即系统调用)引用内核中动态加载的代码(即模块)。如果您将静态加载的代码放入内核中,则它具有的任何配置都应该由其他静态加载的代码公开。然而,在不知道您正在实现什么类型的功能的情况下,很难给出更具体的建议。
You should not reference dynamically loaded code in the kernel (ie, modules) from statically loaded code (ie, syscalls). If you're putting statically loaded code in the kernel, any configuration it has should be exposed by other statically loaded code. It's hard to give any more specific advice without knowing what kind of functionality you're implementing, however.
使用 EXPORT_SYMBOL 从模块导出符号,然后在内核的其他部分使用它。
Export the symbol from the module using EXPORT_SYMBOL and then use it in other part of kernel.