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

时间:2021-03-20 16:49:39

I am trying to get notification when USB is connected and disconnected. So I am trying to implement signals. I created a file "file1" in debugfs. Then I provided a simple write file operation.

当USB连接和断开连接时,我正在尝试获取通知。我在尝试实现信号。我在debugfs中创建了一个文件“file1”。然后我提供了一个简单的写文件操作。

In user space there is a user space application, which will write its PID in the "file1" of debugfs.

在用户空间中有一个用户空间应用程序,它将在debugfs的“file1”中写入它的PID。

In kernel space I can get the PID passed using the write method mentioned above. But I want to use this PID in a different kernel module. So I tried using EXPORT_SYMBOL();, but if I don't include the common header file, I get a compilation error. If I include the header file, when I flash the image, I see that PID is '0'.

在内核空间中,我可以使用上面提到的写方法获得PID。但是我想在另一个内核模块中使用这个PID。因此我尝试使用EXPORT_SYMBOL();,但是如果不包含公共头文件,就会出现编译错误。如果我包含头文件,当我flash图像时,我看到PID是'0'。

Can anybody tell me, if this the right way? Or tell me where am I going wrong. Or can I get notification in different kernel module when PID is written to the file. If so how?

有人能告诉我,如果这是正确的方法吗?或者告诉我哪里出错了。或者我可以在将PID写入文件时在不同的内核模块中获得通知吗?如果是如何?

1 个解决方案

#1


3  

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:

EXPORT_SYMBOL()是正确的方法。我不太理解你所说的“如果我不包括公共头文件”是什么意思。这听起来像是您将EXPORT_SYMBOL()包含在共享头文件中,而这不是您想要做的。你想做如下的事情:

module1.c (compiles into module1.ko)

module1。c(编译成module1.ko)

int my_exported_variable;

EXPORT_SYMBOL(my_exported_variable);

// The rest of module1.c

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

然后在module2。c(编译成module2。必须在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

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.

在您输入第一个模块之后,您可以通过执行grep my_exported_variable /proc/kallsyms来检查符号是否被导出,假设您的系统上有/proc/kallsyms。如果你没有看到你的变量,那么module2的渗透。ko会对一个未解决的符号失败。

#1


3  

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:

EXPORT_SYMBOL()是正确的方法。我不太理解你所说的“如果我不包括公共头文件”是什么意思。这听起来像是您将EXPORT_SYMBOL()包含在共享头文件中,而这不是您想要做的。你想做如下的事情:

module1.c (compiles into module1.ko)

module1。c(编译成module1.ko)

int my_exported_variable;

EXPORT_SYMBOL(my_exported_variable);

// The rest of module1.c

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

然后在module2。c(编译成module2。必须在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

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.

在您输入第一个模块之后,您可以通过执行grep my_exported_variable /proc/kallsyms来检查符号是否被导出,假设您的系统上有/proc/kallsyms。如果你没有看到你的变量,那么module2的渗透。ko会对一个未解决的符号失败。