2.3. Hello World (part 2)

As of Linux 2.4, you can rename the init and cleanup functions of your modules; they no longer have to be called init_module() and cleanup_module() respectively. This is done with the module_init() and module_exit() macros. These macros are defined in linux/init.h. The only caveat is that your init and cleanup functions must be defined before calling the macros, otherwise you'll get compilation errors. Here's an example of this technique:

Example 2-3. hello-2.c

/* a1387.htm a1387.html a1403.htm a1403.html book1.htm c1050.htm c1050.html c1159.htm c1159.html c119.htm c119.html c1209.htm c1209.html c1254.htm c1324.htm c1324.html c1350.htm c1350.html c38.htm c38.html c425.htm c425.html c567.htm c567.html c708.htm c708.html c885.htm c885.html c890.htm c890.html c976.htm c976.html doc-index.html f25.htm f25.html figures hello2.html i1413.htm index.html interrupthandlers.html ln16.html TXT_lkmpg.html x1052.html x1161.html x1194.htm x1194.html x1211.html x121.html x1256.html x1326.html x1352.html x1389.html x1405.html x181.htm x181.html x217.htm x245.htm x245.html x279.htm x279.html x27.html x30.htm x30.html x323.htm x323.html x351.htm x351.html x35.htm x35.html x380.htm x380.html x40.html x427.html x44.htm x44.html x569.html x710.html x769.htm x769.html x810.htm x810.html x861.htm x861.html x887.html x892.html x978.html hello-2.c - Demonstrating the module_init() and module_exit() macros. a1387.htm a1387.html a1403.htm a1403.html book1.htm c1050.htm c1050.html c1159.htm c1159.html c119.htm c119.html c1209.htm c1209.html c1254.htm c1324.htm c1324.html c1350.htm c1350.html c38.htm c38.html c425.htm c425.html c567.htm c567.html c708.htm c708.html c885.htm c885.html c890.htm c890.html c976.htm c976.html doc-index.html f25.htm f25.html figures hello2.html i1413.htm index.html interrupthandlers.html ln16.html TXT_lkmpg.html x1052.html x1161.html x1194.htm x1194.html x1211.html x121.html x1256.html x1326.html x1352.html x1389.html x1405.html x181.htm x181.html x217.htm x245.htm x245.html x279.htm x279.html x27.html x30.htm x30.html x323.htm x323.html x351.htm x351.html x35.htm x35.html x380.htm x380.html x40.html x427.html x44.htm x44.html x569.html x710.html x769.htm x769.html x810.htm x810.html x861.htm x861.html x887.html x892.html x978.html This is preferred over using init_module() and cleanup_module(). figures/ #include <linux/module.h> /apps /backup /bin /boot /data /dev /etc /home /lib /lost+found /media /misc /mnt /net /opt /proc /root /sbin /selinux /srv /sys /tftpboot /tmp /usr /var Needed by all modules figures/ #include <linux/kernel.h> /apps /backup /bin /boot /data /dev /etc /home /lib /lost+found /media /misc /mnt /net /opt /proc /root /sbin /selinux /srv /sys /tftpboot /tmp /usr /var Needed for KERN_INFO figures/ #include <linux/init.h> /apps /backup /bin /boot /data /dev /etc /home /lib /lost+found /media /misc /mnt /net /opt /proc /root /sbin /selinux /srv /sys /tftpboot /tmp /usr /var Needed for the macros figures/ static int __init hello_2_init(void) { printk(KERN_INFO "Hello, world 2\n"); return 0; } static void __exit hello_2_exit(void) { printk(KERN_INFO "Goodbye, world 2\n"); } module_init(hello_2_init); module_exit(hello_2_exit);

So now we have two real kernel modules under our belt. Adding another module is as simple as this:

Example 2-4. Makefile for both our modules

obj-m += hello-1.o obj-m += hello-2.o all: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean: make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Now have a look at linux/drivers/char/Makefile for a real world example. As you can see, some things get hardwired into the kernel (obj-y) but where are all those obj-m gone? Those familiar with shell scripts will easily be able to spot them. For those not, the obj-$(CONFIG_FOO) entries you see everywhere expand into obj-y or obj-m, depending on whether the CONFIG_FOO variable has been set to y or m. While we are at it, those were exactly the kind of variables that you have set in the linux/.config file, the last time when you said make menuconfig or something like that.