Kernel space programming: Implement and add a loadable kernel module to Linux kernel, demonstrate using insmod, lsmod and rmmod commands. A sample kernel space program should print the "Hello World" while loading the kernel module and "Goodbye World" while unloading the kernel module.

//Save these files in one folder

//Program: hello.c

#include
#include
#include

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Huzaif Avez Sayyed");
MODULE_DESCRIPTION("This is a simple kernel program ");
static int myinit(void)
{
    printk(KERN_INFO,"HELLO WORKD\n");
    return 0;
}

static void mycleanup(void)
{
    printk(KERN_INFO,"GOOD BYE");
}

module_init(myinit);
module_exit(mycleanup);

//Makefile.txt

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

//OUTPUT

[email protected]:~/Desktop/os/7$ make
make -C /lib/modules/4.4.0-72-generic/build M=/home/huzaif/Desktop/os/7 modules
make[1]: Entering directory '/usr/src/linux-headers-4.4.0-72-generic'
  CC [M]  /home/huzaif/Desktop/os/7/hello.o
In file included from include/linux/printk.h:6:0,
                 from include/linux/kernel.h:13,
                 from /home/huzaif/Desktop/os/7/hello.c:1:
/home/huzaif/Desktop/os/7/hello.c: In function ‘myinit’:
include/linux/kern_levels.h:4:18: warning: too many arguments for format [-Wformat-extra-args]
 #define KERN_SOH "\001"  /* ASCII Start Of Header */
                  ^
include/linux/kern_levels.h:13:19: note: in expansion of macro ‘KERN_SOH’
 #define KERN_INFO KERN_SOH "6" /* informational */
                   ^
/home/huzaif/Desktop/os/7/hello.c:11:9: note: in expansion of macro ‘KERN_INFO’
  printk(KERN_INFO,"HELLO WORKD\n");
         ^
/home/huzaif/Desktop/os/7/hello.c: In function ‘mycleanup’:
include/linux/kern_levels.h:4:18: warning: too many arguments for format [-Wformat-extra-args]
 #define KERN_SOH "\001"  /* ASCII Start Of Header */
                  ^
include/linux/kern_levels.h:13:19: note: in expansion of macro ‘KERN_SOH’
 #define KERN_INFO KERN_SOH "6" /* informational */
                   ^
/home/huzaif/Desktop/os/7/hello.c:17:9: note: in expansion of macro ‘KERN_INFO’
  printk(KERN_INFO,"GOOD BYE");
         ^
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/huzaif/Desktop/os/7/hello.mod.o
  LD [M]  /home/huzaif/Desktop/os/7/hello.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.4.0-72-generic'

[email protected]:~/Desktop/os/7$sudo insmod hello.ko

[email protected]:~/Desktop/os/7$sudo dmesg | tail -1

[email protected]:~/Desktop/os/7$sudo lsmod

[email protected]:~/Desktop/os/7$sudo rmmod hello

[email protected]:~/Desktop/os/7$sudo dmesg | tail -1
Kernel space programming: Implement and add a loadable kernel module to Linux kernel, demonstrate using insmod, lsmod and rmmod commands. A sample kernel space program should print the "Hello World" while loading the kernel module and "Goodbye World" while unloading the kernel module. Kernel space programming: Implement and add a loadable kernel module to Linux kernel, demonstrate using insmod, lsmod and rmmod commands. A sample kernel space program should print the "Hello World" while loading the kernel module and "Goodbye World" while unloading the kernel module. Reviewed by Huzaif Sayyed on April 17, 2017 Rating: 5

No comments:

Powered by Blogger.