Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is __KERNEL__ used for in linux kernel?

Tags:

linux-kernel

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__ */
like image 215
DaVid Avatar asked Jan 20 '11 08:01

DaVid


People also ask

What is __ user in Linux?

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).

What is __ init in C?

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.

What is the use of kernel modules in Linux?

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.

What does init h do?

include/linux/init.h /* These macros are used to mark some functions or * initialized data (doesn't apply to uninitialized data) * as `initialization' functions.


2 Answers

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.

like image 130
Karmastan Avatar answered Sep 20 '22 10:09

Karmastan


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.

like image 40
ta.speot.is Avatar answered Sep 21 '22 10:09

ta.speot.is