tutorial - Write your own kernel module and insert it into running kernel

So, you want to write a kernel module. You know C, you've written a few normal programs to run as processes, and now you want to get to where the real action is, to where a single wild pointer can wipe out your file system and a core dump means a reboot.

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. For example, one type of module is the device driver, which allows the kernel to access hardware connected to the system.

1) Check if you have all the required tools and lib (Linux Kernel headers)for building the kernel modules for this you need - kernel-headers, you can check if it's install or not by using command: # rpm -qa | grep kernel-headers, if installed, Typing the following command ...

# ls -d /lib/modules/$(uname -r)/build 
Output (OpenSuse 11.1) : /lib/modules/2.6.27.7-9-pae/build 

else install the kernel-header from your installation CD/DVD

2) Now, start with the famous "hello World" program, create c file call - hello.c

/*************************************************************/
#include <linux/module.h>
#include <linux/kernel.h>
int init_module(void)
{
    printk(KERN_INFO "Hello world.\n");
    return 0;
}
void cleanup_module(void)
{
    printk(KERN_INFO "Goodbye world\n");
}

/*************************************************************/

3) Create the make file: vi Makefile

obj-m += hello.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

save and close

4) Now compile the module to create - hello.ko using command : # make and you should see something like ...

make -C /lib/modules/2.6.27.7-9-pae/build M=/root/kernel modules
make[1]: Entering directory `/usr/src/linux-2.6.27.7-9-obj/i386/pae'
make -C ../../../linux-2.6.27.7-9 O=/usr/src/linux-2.6.27.7-9-obj/i386/pae/. modules
  CC [M]  /root/kernel/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /root/kernel/hello.mod.o
  LD [M]  /root/kernel/hello.ko
make[1]: Leaving directory `/usr/src/linux-2.6.27.7-9-obj/i386/pae'

you should see lot of news files get created inside the directory, check it "ls" command

5) Load/insert our kernel module into the running kernel (hello.ko) using command: # insmod hello.ko

6) Now let check the information about our module (hello.ko) using command:
# modinfo hello.ko, you should see something like ...

filename:       hello.ko
srcversion:     A59CC2D814343F3CA40CADF
depends:        built-in
vermagic:       2.6.27.7-9-pae SMP mod_unload modversions 586

7) To list the module currently running inside the kernel : # lsmod

8) To remove the "hello.ko" module: # rmmod hello.ko

9) Check the output of our module by looking at the /var/log/message file, you should find the entries like ..
Jan 11 12:31:48 poison kernel: Hello world.
Jan 11 12:32:26 poison kernel: Goodbye world


Free, facebook, tips, Links, blogging, Downloads, Google, facebookTips, money, news, apps, Social, Media, Website, Tricks, games, Android, software, PIctures, Internet, Security, Web, codes, Review, bloggers, SAMSUNG, Worldwide, Contest, Exitic, Phones, facebookTricks, hacking, London, Olympics, SEO, Youtube, iOS, Adsense, gadgets, iPHONE, widgets, Doodle, twitter, video, Deals, technology, Aircel, Airtel, iPAD, Angry, Birds, BSNL, TechLife, GMAIL, Idea, Microsoft, SmartPhones, Stress, Buster, Windows, Yahoo, Infolinks, Nokia, Scam, Uninor, browsers, Amazon, Euro, CUP, Chat, IDM, JOBS, Modem, Music, Reliance, Results, SSC, Tata, Docomo, bing, freebie, mobile, placements, AIEEE, AlertPay, Chrome, College, Competetive, Exam, Dehradun, Extension, FireFox, GPRS, HTC, IMPACT, Info, MTS, Mark, Zukerberg, Paypal, Promotional, Post, Torrent, UTU, Unlocking, VodaFone, Wall, Paper, apple, books, engineering, iCAR, iTunes, pinterest, rovio, AVG, Admit, Card, Adobe, Affiliate, Marketing, Akhilesh, Amul, Girl, BlackBerry, ChromeBook, Clixsense, Coupon, Digitallife, Discovery, Emoticons, Festival, GATE, GIMP, Income, Tax, International, JSS, JailBreaking, Kindle, Linux, Local, MAX, PAYNE, Mac, Mango, Memory, Speed, Nexus, Online, Shopping, Raakhi, Report, Rising, Stars, Sample, Science, Sony, Syllabus, TabletBooK, Teamviewer, Templates, Dark, Knight, Rises, USA, UPMT, Virgin, Xperia, ZTE, challan, counselling, course, btech, funny, iMOVE, registration

source:http://linuxpoison.blogspot.com/2009/01/13578175712252.html