who can explain what does the following code means?
if __KERNEL__
is not defined, define following macros.
when and where define __KERNEL__
?
/* only for userspace compatibility */
#ifndef __KERNEL__
/* IP6 Hooks */
/* After promisc drops, checksum checks. */
#define NF_IP6_PRE_ROUTING 0
/* If the packet is destined for this box. */
#define NF_IP6_LOCAL_IN 1
/* If the packet is destined for another interface. */
#define NF_IP6_FORWARD 2
/* Packets coming from a local process. */
#define NF_IP6_LOCAL_OUT 3
/* Packets about to hit the wire. */
#define NF_IP6_POST_ROUTING 4
#define NF_IP6_NUMHOOKS 5
#endif /* ! __KERNEL__ */
The __user macro is defined with some other macros like __force / __kernel etc in the compiler. h header file. They are actually not of any use to traditional compilers, including GCC/ICC etc. But it's useful for kernel static analysis tools like sparse (more information here: Sparse - Linux Kernel Newbies).
The __init keyword tells the linker to place the code in a dedicated section into the kernel object file. This section is known in advance to the kernel, and freed when the module is loaded and the init function finished. This applies only to built-in drivers, not to loadable modules.
Kernel modules are pieces of code that can be loaded and unloaded into the kernel upon demand. They extend the functionality of the kernel without the need to reboot the system. Custom codes can be added to Linux kernels via two methods.
include/linux/init.h /* These macros are used to mark some functions or * initialized data (doesn't apply to uninitialized data) * as `initialization' functions.
When you compile your kernel, __KERNEL__
is defined on the command line.
User-space programs need access to the kernel headers, but some of the info in kernel headers is intended only for the kernel. Wrapping some statements in an #ifdef __KERNEL__/#endif
block ensures that user-space programs don't see those statements.
I used Google to search for __KERNEL__
and found this.
The
__KERNEL__
macro is defined because there is programs (like libraries) than include kernel code and there is many things that you don't want them to include. So most modules will want the__KERNEL__
macro to be enabled.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With