Tuesday, August 12, 2008

Training for Ankit (2)

Ok, we built the kernel module. What do we do with it now?

1. Insert the kernel module to kernel
insmod tzap.ko

2. See the messages appearing in /var/log/messages

At this point, Ankit asked me why tail /var/log/messages? Why can't we see that directly?
Ans: Insmod calls the hello_init function. The message comes from

printk(KERN_ALERT "Before: Hello, world %d \n",gpltest);

Now, KERN_ALERT is a priority for kernel messages. ALERT is not a high priority so that the message comes on our Terminal. We changed the line to

printk(KERN_EMERG "Before: Hello, world %d \n",gpltest);

This time, the message appeared on the Terminal. I then showed him /etc/syslog.conf and told him that, it is syslog which reads these messages and decides what to do with it. I showed him my /etc/syslog.conf and explained how various messages are destined to go to various locations.

3. Unloading the module

This is done with the command rmmod tzap. hello_exit is called when this happens.

4. Module license line.

MODULE_LICENSE("GPL"); or something like that tells the module is licensed under GPL license. This has an important for our customer. They will have to GPL the code if they plan to access the symbols that are exported using EXPORT_SYMBOL_GPL. Anyway, they are lucky and zaptel does not have GPL exports.

5. Init & Exit

module_init and module_exit defines the entry points for insmod and rmmod.

6. One more step __init and __exit

These macros tell the kernel that the functions thus defined are specific to module loading and unloading. I told him to remember the Linux boot process and the line "Freeing unused kernel memory". This is possible because if I mark a function as __init, the kernel knows it has no need of the function after initialization. This allows the kernel to free some memory after boot-up

No comments: