Reputation: 13
I noticed that the Linux kernel has two structs with the same name (module
). One is found in include/linux/module.h
and the other one is found in scripts/mod/modpost.h
. Now, I know that THIS_MODULE
is assigned to the struct in module.h
, but how can the kernel know which struct it should use?
THIS_MODULE
is defined as a macro inside export.h
#ifdef MODULE
extern struct module __this_module;
#define THIS_MODULE (&__this_module)
#else
#define THIS_MODULE ((struct module *)0)
#endif
I would like someone to explain the code above
Another question I have is how can I access the module struct which is in modpost.h
?
Upvotes: 0
Views: 213
Reputation: 66143
Everything under scripts/
is for user-space utilities used for building the kernel and its modules. Headers from this directory are never included into the kernel module code, so these definitions are never accessible to the modules.
When you search for some type/variable/function for use in the Linux kernel code, you could safely ignore everything which is found under scripts/
directory.
Upvotes: 2
Reputation:
It literally means if the macro MODULE is defined then you get the first definition for THIS_MODULE, otherwise you get the other (null pointer). This happens at compile time. It is the same for which struct to use, most likely by which header file is included.
You include modpost.h for the declaration, and to access variable you need your code to run in a process where this variable is alive. From context, I would guess it is when you load your module with modprobe.
Upvotes: 0