且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在两个 Linux 内核模块之间共享全局变量?

更新时间:2023-11-14 08:43:04

EXPORT_SYMBOL() 是正确的方法.我不太明白您所说的如果我不包含通用头文件"是什么意思.听起来您将 EXPORT_SYMBOL() 包含在共享头文件中,这不是您想要做的.您想要执行以下操作:

EXPORT_SYMBOL() is the correct approach. I do not quite understand what you mean by "if I don't include the common header file". It sounds like you are including the EXPORT_SYMBOL() in a shared header file which is not what you want to do. You want to do something like the following:

module1.c(编译成module1.ko)

module1.c (compiles into module1.ko)

int my_exported_variable;

EXPORT_SYMBOL(my_exported_variable);

// The rest of module1.c

然后在module2.c中(编译成module2.ko,必须在module1.ko之后进行insmod-ed)

And then in module2.c (compiles into module2.ko which must be insmod-ed after module1.ko)

extern int my_exported_variable; // Note the extern, it is declaring but not defining it, the definition is in module1

// The rest of module2.c

在插入第一个模块后,您可以通过执行 grep my_exported_variable/proc/kallsyms 来检查符号是否已导出,假设您有 /proc/kallsyms系统.如果您在那里没有看到您的变量,那么 module2.ko 的 insmod 将无法处理未解析的符号.

After you insmod the first module you can check that the symbol is exported by doing a grep my_exported_variable /proc/kallsyms, assuming you have /proc/kallsyms on your system. If you don't see your variable there then the insmod of module2.ko will fail do to an unresolved symbol.