开发者

Access /proc fs variable from other parts of Kernel code

开发者 https://www.devze.com 2023-02-12 03:55 出处:网络
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

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 loa开发者_开发百科ded 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.


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 the symbol from the module using EXPORT_SYMBOL and then use it in other part of kernel.

0

精彩评论

暂无评论...
验证码 换一张
取 消