Writing First Module Hello World!!

Time to get started with programming and respecting the tradition, let's start learning kernel module programming by writing the Hello Globe kernel module. Once you mastered writing, building and running the hello world kernel module, you have crossed the first big hurdle.

Majority of the kernel code is written in 'C' while a very little part of kernel code is in assembly. Likewise most of the times you will write component code in 'C', though some occasions you may have to use set up language too. One point worth mentioning is that the kernel space 'C' library is different than the user space 'C' collection. So, as a rule, while writing code in kernel space , you'll avoid using any user space library call in module signal. Therefore you will not use any user space API in your code. Let's create a file with name Hello.c that contains the origin code of our first module. You can create this source document anywhere. Just create a directorywith name Modules under your home listing, like /home/Modules. We decide all of our module code to reside below this directory. Below is the code given for our Hello world module. Open a new file Hello.c and type this particular code in this file. Number vi /home/Modules/Hello.c // Your first kernel module HelloWorld.c // hello_init the init function, called when the module is loaded. // Returns zero if effectively loaded, nonzero otherwise. static int hello_init(emptiness)
 * 1) include <linux/init.h
 * 2) include <linux/module.h

printk((KERN_ALERT "Hello World !!\n");   return 0;

// hello_exit the leave function, called when the module is removed. static void hello_exit(emptiness)

printk((KERN_ALERT "Good Bye World !!\n");

module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("LEARNING LINUX KERNEL"); MODULE_DESCROPTION( HELLO WORLD MODULE); Fig. 4.One Hello World Module Code Right now let's understand the module code collection by line.

Headers:

After macro meanings, next comes the header file section. Every module includes various header files. Some header files rely on to which particularsubsystem we are going to add the module, e.g. memory, networking, block device driver etc. There are some header files that are independent to whichsubsystem we are including code. No matter for whatever purpose we are writing the actual module, those header files have to be included always. Those header files are, component.h, version.h, kernel.h and init.h. So, always after defining macros, we have to create below 4 include statements. Fig. 4.2 Mandatory header documents for a module. Let's see the reason for these header files one by one. module.h : Module Subsystem is responsible for launching and unloading of kernel modules. module.h header file is the interface of the modulesubsystem. It contains the actual signature of all the functions of module subsystem. init .h: In kernel module code, we write 1 init routine and one cleanup routine. They act like a resource percentage routine and resource cleanup routine. While writing a kernel module code, it is obligatory to write both these routines. The signature of these two capabilities is given in this header file. And you should take care that the personal of init and exit programs in your kernel module exactly matches with them.
 * 1) include    <linux/module.h
 * 1) consist of   <linux/init.h

Entry and Leave Routines:

Next comes hello_init and hello_exit functions. These functions are initialization as well as cleanup routines of a component. Conventionally they are referred as entry and exit routines. At runtime, when component gets loaded , the first function executed is entry program. So, for a linux kernel module, entry routine is similar to main function in user space 'C' program. Therefore it is mandatory to provide the entry routine to every kernel module. The second linux kernel module is inserted, entry routine starts executing.

Similarly, when the module gets unloaded, exit routine gets executed. All the acquired resources through module are released by the leave routine. We are free to select any names for admittance and exit routines however the signature of these two routines are fixed. So, what ever name we give to admittance and exit routines, the signature of those routines should match with the given signatures. fixed int hello_init(void)

printk((KERN_ALERT "Hello World !!\n");    return 0;

fixed void hello_exit(void)

printk((KERN_ALERT "Good Bye World !!\n"); Fig. Four.3 entry and exit functions. Now, we have defined the actual hello_init and hello_exit functions. For working them with the linux kernel, kernel also should have information about the admittance and exit routine of the module. These are special routines of a module and we notify kernel about these routine through registering them with the linux kernel. Registering those functions to the kernel is easy and achieved through macros, module_init and module_exit . printk:

Finally few term about printk. printk prints a kernel message. Its syntax is same as of printf. It is available to whole kernel space. But, unlike printf, by default printk does not print on console. Infact printk is more a kernel signing mechanism that logs the messages into different record files. We can view the output of printk in /var/log/messages or also we are able to use 'dmesg' command to see the output of printk. For now, only that much detail for printk .We have much more to say on printk in later on chapters.

Comments and License:

The last 3 lines in our hello world kernel module comprises 'comments and license section.

MODULE_DESCRIPTION ("HELLO Globe MODULE"); MODULE_AUTHOR ("LEARNING LINUX KERNEL"); MODULE_DESCRIPTION ("HELLO WORLD MODULE"); Fig. 4.4 Module Comments.

These are module comments and add additional information to the module. Unlike use space code comments these types of module comments go with the module code and become the part of compiled kernel module. MODULE_AUTHOR comment adds important information to the kernel module like module author name and the contact information. In case module isn't functioning well, author can be contacted and intimated about the problem with the help of this information.

Linux system kernel code is open source and released under GPL license. In case if you add a module towards the linux kernel, you must decide the licensing of your module separately. Your module can be certified under GPL or it can stay propriety too. But if it is propriety your module will not be able to use linux kernel functions and data structures because entire linux kernel code drops under GPL. As per GPL terms, if you wish to use any GPL licensed code, you have to license your signal also under GPL. Because of this cause usually all kernel modules falls under GPL licensing scheme. Additionally, if your module is not GPL you might not receive support from the open source community too. You can have a look into module.h h2 tags file to know different available licensing schemes.

"GPL" "GPLvl" "GPL and extra rights" "Dual BSD/GPL" "Dual MPL/GPL" "Proprietary" Fig. 4.5 Module Licensing
 * 1) cd linux 2.6.33/include/linux
 * 2) vi module.h

Compile a Kernel linux kernel development