Saturday, December 26, 2015

KERNEL MODULE PROGRAMS

KERNEL MODULE PROGRAMS

/////////////////////////////////////////////////////////////////
Program No. : 1
-----------------------------------------------------------------------

// hello-1.c
-----------------------------------------------------------------------

// This header file is require for all kernel module.
#include<linux/module.h>

// This header file is require for KERN_INFO macro.
#include<linux/kernel.h>

// This function gets called automatically when module gets loaded by insmod command

// This function returns 0 if it successfully executed otherwise it returns non zero value

int init_module(void)
{
printk(KERN_INFO "Jay ShreeKrishna: module inserted 1\n");
return 0;
}

// This function gets called automatically when module gets removed from memory by rmmod.

void cleanup_module(void)
{
printk(KERN_INFO "Hare Ram: module removed 1\n");
}

-----------------------------------------------------------------------
// Makefile
-------------------------------------------------------------------------

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

-----------------------------------------------------------------------
// readme
----------------------------------------------------------------
module name : hello-1
build makefile
$ make

if there is any problem in our kernel then download requred linux headers as
apt-get install build-essential linux-headers-$(uname -r)

get information of our module using modinfo .ko

insert module using sudo insmod .ko

get list of all installed module as lsmod

check kernel log using /var/log/syslog file

remove kernel module using sudo rmmod  .ko

-------------------------------------------------------------------------

/////////////////////////////////////////////////////////////////
Program No. :- 2
-------------------------------------------------------------------------
// hello-2.c
-------------------------------------------------------------------------

// This header file is required by all kernel modules
#include <linux/module.h>

// This header file is required for KERN_INFO
#include <linux/kernel.h>

// This header file is required macroes
#include <linux/init.h>

// This function gets called automatically when module gets loaded by insmod

// This function returns 0 if it successfully executed otherwise it returns non zero value

static int __init hello_2_init(void)
{
printk(KERN_INFO "Jay ShreeKrishna: module inserted 2\n");
return 0;
}

// This function gets called automatically when module gets removed from memory by rmmod //command
static void __exit hello_2_exit(void)
{
printk(KERN_INFO "Hare Ram: module removed 2\n");
}

// Register our init function
module_init(hello_2_init);

// Register our cleanup function
module_exit(hello_2_exit);

-------------------------------------------------------------------------
// Makefile
-------------------------------------------------------------------------
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

-------------------------------------------------------------------------
// readme
-------------------------------------------------------------------------
module name : hello-2
build makefile
$ make

if there is any problem in our kernel then download requred linux headers as
apt-get install build-essential linux-headers-$(uname -r)

get information of our module using modinfo .ko

insert module using sudo insmod .ko

get list of all installed module as lsmod

check kernel log using /var/log/syslog file

remove kernel module using sudo rmmod  .ko

-------------------------------------------------------------------------

/////////////////////////////////////////////////////////////////
Program No. :- 3
-------------------------------------------------------------------------
// hello-3.c
-------------------------------------------------------------------------

// This header file is required by all kernel modules
#include <linux/module.h>

// This header file is required for KERN_INFO
#include <linux/kernel.h>

// This header file is required macroes
#include <linux/init.h>

// This is used to declare variable named as hello3_data
static int hello3_data __initdata = 0;

// This function gets called automatically when module gets loaded by insmod

// This function returns 0 if it successfully executed otherwise it returns non zero value

static int __init hello_3_init(void)
{
printk(KERN_INFO "Jay ShreeKrishna: module inserted with data %d\n", hello3_data);
return 0;
}

// This function gets called automatically when module gets removed from memory by rmmod
static void __exit hello_3_exit(void)
{
printk(KERN_INFO "Hare Ram: module removed\n");
}

// Register our init function
module_init(hello_3_init);

// Register our cleanup function
module_exit(hello_3_exit);

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

-------------------------------------------------------------------------
// readme
-------------------------------------------------------------------------
module name : hello-3
build makefile
$ make

if there is any problem in our kernel then download requred linux headers as
apt-get install build-essential linux-headers-$(uname -r)

get information of our module using modinfo .ko

insert module using sudo insmod .ko

get list of all installed module as lsmod

check kernel log using /var/log/syslog file

remove kernel module using sudo rmmod  .ko

-------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////
Program No. :- 4
-------------------------------------------------------------------------
// hello-4.c
-------------------------------------------------------------------------

// This header file is required by all kernel modules
#include <linux/module.h>

// This header file is required for KERN_INFO
#include <linux/kernel.h>

// This header file is required macroes
#include <linux/init.h>

// Macro for name of author
#define DRIVER_AUTHOR "Sunil Bhagwat"

// Macro for module description
#define DRIVER_DESC   "Demo module"

static int __init init_hello_4(void)
{
printk(KERN_INFO "Jay ShreeKrishna: module inserted 4\n");
return 0;
}

static void __exit cleanup_hello_4(void)
{
printk(KERN_INFO "Hare Ram: module removed 4\n");
}

module_init(init_hello_4);
module_exit(cleanup_hello_4);

// Register name of licance
MODULE_LICENSE("GPL");

// Register name of author
MODULE_AUTHOR(DRIVER_AUTHOR);

// Provide module description
MODULE_DESCRIPTION(DRIVER_DESC);
-------------------------------------------------------------------------
// Makefile
-------------------------------------------------------------------------
obj-m += hello-4.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

-------------------------------------------------------------------------
// readme
-------------------------------------------------------------------------
module name : hello-4
build makefile
$ make

if there is any problem in our kernel then download requred linux headers as
apt-get install build-essential linux-headers-$(uname -r)

get information of our module using modinfo .ko

insert module using sudo insmod .ko

get list of all installed module as lsmod

check kernel log using /var/log/syslog file

remove kernel module using sudo rmmod  .ko

-------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////Program No. :- 5
-----------------------------------------------------------------------
// hello-5.c
-----------------------------------------------------------------------
// This header file is required by all kernel modules
#include <linux/module.h>

// This header file is required for KERN_INFO
#include <linux/kernel.h>

// This header file is required macroes
#include <linux/init.h>

// This header file is used to get information about the parameters
#include <linux/moduleparam.h>

// Macro for name of author
#define DRIVER_AUTHOR "Sunil Bhagwat"

// Macro for module description
#define DRIVER_DESC   "Demo module with arguments"

int myint = 21;

/*
This macro is used to to register information about the input argument
First parameter is name of variable
Second parameter is its type
Third parameter is its permission
*/
module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(myint, "Integer variable");

static int __init hello_5_init(void)
{
printk(KERN_INFO "The Integer Value is:  %d\n", myint);
return 0;
}

static void __exit hello_5_exit(void)
{
printk(KERN_INFO "Hare Ram : Removing module\n");
}

module_init(hello_5_init);
module_exit(hello_5_exit);

// Register name of licance
MODULE_LICENSE("GPL");

// Register name of author
MODULE_AUTHOR(DRIVER_AUTHOR);

// Provide module description
MODULE_DESCRIPTION(DRIVER_DESC);

// we can insert our module as insmod myint=11
-------------------------------------------------------------------------
// Makefile
-------------------------------------------------------------------------
obj-m += hello-5.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

-------------------------------------------------------------------------
// readme
-------------------------------------------------------------------------
module name : hello-5
build makefile
$ make

if there is any problem in our kernel then download requred linux headers as
apt-get install build-essential linux-headers-$(uname -r)

get information of our module using modinfo .ko

insert module using sudo insmod .ko

get list of all installed module as lsmod

check kernel log using /var/log/syslog file

remove kernel module using sudo rmmod  .ko

-------------------------------------------------------------------------
/////////////////////////////////////////////////////////////////Program No. :- 6
-----------------------------------------------------------------------
// first.c
-------------------------------------------------------------------------
// This header file is required by all kernel modules
#include <linux/module.h>

// This header file is required for KERN_INFO
#include <linux/kernel.h>

// This header file is required macroes
#include <linux/init.h>

int init_module(void)
{
              printk(KERN_INFO "Jay ShreeKrishna : Inserting module\n");
              return 0;
}

-------------------------------------------------------------------------
// second.c
-------------------------------------------------------------------------

#include <linux/kernel.h>
#include <linux/module.h>

void cleanup_module()
{
            printk(KERN_INFO "Hare Ram : Removing the module\n");
}

-------------------------------------------------------------------------

// Makefile
-------------------------------------------------------------------------
obj-m += firstsecond.o
firstsecond-objs := first.o second.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
-------------------------------------------------------------------------
// readme
-------------------------------------------------------------------------
module name : hello-6
build makefile

if there is any problem in our kernel then download requred linux headers as
apt-get install build-essential linux-headers-$(uname -r)

get information of our module using modinfo .ko

insert module using sudo insmod .ko

get list of all installed module as lsmod

check kernel log using /var/log/syslog file

remove kernel module using sudo rmmod  .ko

-------------------------------------------------------------------------

No comments:

Post a Comment

To Connect Java program with the MYSQL Database

To connect java application with the MYSQL database, first of all to install MYSQL database in your machine. To install MYSQL in your mach...