Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kernel module: Accessing member of structure defined in another module header

I'm trying to write a kernel module but I'm stuck with accessing member of structure defined in another module header. I'll try to explain:

Let's say the other module is:

<kernel-src>/drivers/xxx/xxx.c|.h

in xxx.c there are some exported functions which I'm using in my driver. However I want to access the member m1 from struct s_xxx defined in xxx.h:

struct s_xxx {
    ...
    int m1;
};

Next I have this in /usr/include/linux/yyy.h:

struct s_xxx;
struct s_yyy {
    ...
    struct s_xxx *p_xxx;
};

I my driver I've:

#include <linux/yyy.h>

and I'm successfully using the exported symbols from the xxx driver. But of course if I try to access member from s_xxx the compiler complains:

struct s_yyy *p_yyy;
...
m = p_yyy->p_xxx->m1; /* error */

xxx.h can't be found in /usr/include/linux/. So far I've found 2 workarounds:

1) to download the kernel sources and include the full path to xxx.h in my module

2) copy/paste the s_xxx definition from xxx.h to my module

What is the correct way to do this?

(sorry for the long and crappy explanation :@ )

like image 598
Meh Avatar asked Nov 27 '12 18:11

Meh


People also ask

What is Export_symbol in Linux kernel?

EXPORT_SYMBOL() is a macro the Linux kernel headers define. It has not much in common with extern. It tells the kbuild mechanism that the symbol referred to should be part of the global list of kernel symbols. That, in turn allows kernel modules to access them.

How are kernel modules linked?

A user can link a module into the running kernel by executing the insmod external program. This program performs the following operations: Reads from the command line the name of the module to be linked. Locates the file containing the module's object code in the system directory tree.

Why we Cannot use C standard library function in kernel module?

Because the GNU C Library which you are familiar with is implemented for user mode, not kernel mode. The kernel cannot access a userspace API (which might invoke a syscall to the Linux kernel).

What is kernel module explain the process for inserting a module in the kernel?

Kernel modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system. Custom codes can be added to Linux kernels via two methods.


1 Answers

struct s_xxx;
struct s_yyy {
    ...
    struct s_xxx *p_xxx;
};

In absence of xxx.h this means you have a forward declaration of struct s_xxx and you can declare pointers to it, but you can't use its members yet or instantiate one, because its size or members aren't known to the compiler. You need to include the full structure definition by including xxx.h in the compilation unit.

If xxx.h is not part of xxx's public interface but you still insist on accessing the internals of the module you face the risk of a catastrophic compatibility break in the future if the internal structure of xxx is changed.

It is not advisable to copy the structure definition to your own code because binary compatibility between the structures cannot be guaranteed, unless you have built everything yourself with the same compiler and options.

like image 88
otto Avatar answered Sep 18 '22 23:09

otto