从内核代码的其他部分访问/proc fs变量 [英] Access /proc fs variable from other parts of Kernel code
问题描述
我正在尝试获取一个用户级程序,以通过/proc与内核进行通信.
I'm trying to get a user-level program to communicate with the kernel via /proc.
我按照 tldp 上的说明进行操作,并成功地创建一个自定义proc文件,并使用 insmod 动态加载它,然后从用户区读取(cat)并写入(echo)该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.
现在我的问题是,如何从内核的另一部分(例如系统调用基础结构)访问/proc变量(它是字节缓冲区)?由于自定义proc文件是动态加载和链接的,因此如何从静态编译的内核代码中引用它?
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?
系统规格:在Mac Pro Fusion 13(2009)上的VMWare Fusion中运行的Ubuntu 10.10.
System specs: Ubuntu 10.10 running in VMWare Fusion on a MacBook Pro 13" (2009).
相关代码(根据要求)-
Pertinent code (by request) -
procfile.c
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(在内核的另一部分)
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.
推荐答案
回答了我自己的问题,并从上面的答案中得到了一些提示:
Answered my own question, taking a few hints from the answers above:
我缺少的关键是我需要在内核本身内声明一个变量(例如int kernel_var = 0;)(而不是在procfs开销模块内声明,因为我之前做错了).之后,使用EXPORT_SYMBOL将其导出,然后将其添加到全局模块符号表中,最后将其作为extern变量包含在开销procfs模块中.
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.
因此,本质上来说,开销变量已经存在于内核中,我只是使用procfs模块将其作为外部变量引用并修改其值.
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.
这篇关于从内核代码的其他部分访问/proc fs变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!