简体   繁体   English

我可以在可加载内核模块(外部库除外)中使用哪些头文件和函数?

[英]Which headers and functions can I use in loadable kernel modules (except external libraries)?

Section 2 of the Linux man pages contains system calls . Linux手册页的第2节包含system calls

http://linux.die.net/man/2/ http://linux.die.net/man/2/

After finding this link, I say "OK! I use this reference for developing every modules, It is complete". 找到此链接后,我说:“好!我使用此参考来开发每个模块,它是完整的”。

But it seems I can not use some functions of this section (like bind(2) ). 但是似乎我不能使用本节的某些功能(例如bind(2) )。 After including required headers (like <sys/socket.h> ) the module compilation failed. 包含必需的头文件(如<sys/socket.h> )后,模块编译失败。

Some functions/macros like printk does not listed in the section 2. Many people used Linux Apis that I could not find any official reference for it (just like printk ). 某些功能/宏(例如printk未在第2节中列出。很多人使用Linux Apis,但我找不到任何官方参考(就像printk一样)。 I don't know how they found these functions? 我不知道他们是如何找到这些功能的? (May be by reading whole source code of kernel instead of reading any reference) (可能是通过阅读内核的完整源代码,而不是阅读任何参考资料)

Some functions like memset describes in section 3 (user space libraries) but kernel module developer can use it without any error! 诸如memset类的某些功能在第3节(用户空间库)中进行了介绍,但是内核模块开发人员可以毫无错误地使用它!

Where I can found a COMPLETE reference of available headers and functions when developing a loadable kernel modules? 开发可加载内核模块时,在哪里可以找到可用标头函数完整引用? (Something like MSDN and WINDDK references) (类似于MSDN和WINDDK参考)

#include <linux/init.h>
#include <linux/module.h>

/* A complete list of availabe headers and functions is missing! */

static int my_init(void)
{
    return  0;
}

static void my_exit(void)
{
    return;
}

module_init(my_init);
module_exit(my_exit);

As you know, the Kernel is quite a big, big and independent beast. 如您所知,内核是一个很大,很大且独立的野兽。 This means that you cannot include anything which is not found under your kernel source tree. 这意味着您不能包含在内核源代码树下找不到的任何内容。

When you're implementing your module, that means the only things you can link to is what you got in : 当您实现模块时,这意味着您可以链接到的唯一东西是:

  • The kernel source tree itself 内核源代码树本身
  • Another module you've made (I would recommend to avoid this scenario though whenever possible ... ) 您已经制作了另一个模块(我建议尽管可能就避免这种情况……)

Note that you will be able to use the functions only if they are exported. 请注意,只有在导出功能后才能使用它们。 You can check this using the nm utility on the compiled .ko file of a module. 您可以使用模块的已编译.ko文件上的nm实用工具进行检查。 If you want to export some symbols of your module, you have to use the macro EXPORT_SYMBOL . 如果要导出模块的某些符号,则必须使用EXPORT_SYMBOL

Be careful not to confuse user-space includes, usually found under /usr/include with the kernel module headers (which you are likely wanting to link against). 注意不要将通常位于/ usr / include下的用户空间包含与内核模块标头(您可能希望链接到该标头)混淆。

In the example you gave, the sys/socket.h is the location of the header after a make headers_install . 在您给出的示例中, sys / socket.hmake headers_install之后的标题位置。 At this location, it is meant to be used by userspace applications. 在此位置,它应由用户空间应用程序使用。 This is NOT what you want when you are programming a module. 在编写模块时,这不是您想要的。

I think you have to look deeper in the use of sockets in the kernel. 我认为您必须更深入地研究内核中套接字的使用。 By googling up a little, I found -> this <- on LWN and checked the include/linux/net.h header of my 3.8 kernel source tree. 通过仔细搜索,我在LWN上找到-> this <- ,并检查了3.8内核源代码树的include / linux / net.h标头。 The patch described in the link is integrated in the kernel and is likely to be what you are looking for. 链接中描述的补丁已集成在内核中,很可能正是您所需要的。

The memset example is also an illustration of what I explained this far : if you want to use it in a common application, you'll have to include the string.h header, which is simply located at /usr/include/string.h . memset示例也说明了我到目前为止的内容:如果要在通用应用程序中使用它,则必须包含string.h标头,该标头位于/usr/include/string.h中 Its kernel equivalent is found under ... 其内核等效项位于...

/your-kernel-source-tree-dir/include/linux/string.h /您的内核源树目录/include/linux/string.h

Et voila ! 等等! You can use memset and friends inside any piece of kernel code, as long as you include the right header ! 只要包含正确的标头,就可以在任何内核代码中使用memset和friends!

Thus, when you feel some frustration, thinking Man, I was able to use this by simply including this header in my apps ... Take a deep look at the kernel source tree (or have grep do it for you ;) ). 因此,当您感到有些沮丧时,想想Man,我可以通过在我的应用程序中简单地包含此标头来使用它……深入了解内核源代码树(或让grep为您完成;)。 You are very likely to find what you are looking for ! 您很可能会找到想要的东西!

As per my knowledge there is no such complete reference for kernel module programming. 据我所知,内核模块编程还没有这样完整的参考。 I suggest you study the LDD3 basic sections and http://www.tldp.org/LDP/lkmpg/2.6/html/lkmpg.html#AEN978 for a beginner. 建议初学者学习LDD3基本部分和http://www.tldp.org/LDP/lkmpg/2.6/html/lkmpg.html#AEN978

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM