且构网

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

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

更新时间:2023-11-14 08:42:16

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之后进行安装)

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.