Access /proc fs variable from other parts of Kernel code

1.5k views Asked by At

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.

3

There are 3 answers

0
Warr1ck On BEST ANSWER

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.

2
Zimbabao On

Export the symbol from the module using EXPORT_SYMBOL and then use it in other part of kernel.

0
bdonlan On

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.