It is a piece of cake, don’t have idea on LKM.

Module Creation

$ vim hello.c

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

//step 1
#include <linux/moduleparam.h>

//step 2
int param_variable = 0

//step 3 register (macro)
// module_param(name_var,type,permissions);
/*
S_IRUSR
S_IWUSR
S_IXUSR

S_IRGRP
S_IWGRP

S_IRUSR | S_IWUSR
*/

module_param(param_variable,int,S_IRUSR | S_IWUSR);

void display()
{
printk(KERN_ALERT “Now the value is : %d”, param_variable);
}

static int hello_init(void)
{
printk(KERN_ALERT “Jack and jill went upto hill\n”);
display();
return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT “to fetch a pail of water\n”);
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_AUTHOR(“S Mohammed Audhil”);
MODULE_DESCRIPTION(“Demonstration of LKM”);
MODULE_LICENSE(“GPL”);

Be aware of 3 steps what I mentioned above.

Creation of Makefile

obj-m += hello.o

KDIR = /usr/src/linux-headers-$(shell uname -r)

all:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules

clean:
rm -rf *.o *.ko *.mod.* *.symvers *.order

Module compilation

$ make

Pass parameter at insmod (inserting module inside kernel)

$ sudo insmod param_variable=1234

dmesg

$ dmesg

<output truncated>

[18093.156608] chromium-browse[24441]: segfault at 4076f004 ip b23b63d8 sp bfcc38c4 error 4 in libflashplayer.so[b1dac000+bc4000]
[19466.015651] Jack and jill went upto hill
[19466.015656] Now the value is : 1346

To pass argument to an array replace,

hello.c as

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

#include <linux/moduleparam.h>

int param_variable[3] = {0,0,0};

module_param(param_variable,int,NULL,S_IRUSR | S_IWUSR); // In place of NULL represents number of arguments to pass

void display()

{

printk(KERN_ALERT “Now the value is : %d”, param_variable[0]);

printk(KERN_ALERT “Now the value is : %d”, param_variable[1]);

printk(KERN_ALERT “Now the value is : %d”, param_variable[2]);

}

static int hello_init(void)
{
printk(KERN_ALERT “Jack and jill went upto hill\n”);
display();
return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT “to fetch a pail of water\n”);
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_AUTHOR(“S Mohammed Audhil”);
MODULE_DESCRIPTION(“Demonstration of LKM”);
MODULE_LICENSE(“GPL”);

and insmod becomes,

$ sudo insmod hello.ko param_variable=1,2,3

and dmesg will display,

$ dmesg

<output truncated>

[22508.247127] Jack and jill went upto hill
[22508.247131] Now the value is : 1
[22508.247132] Now the value is : 2
[22508.247134] Now the value is : 3

Hope, this helps…

Enjoy…!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s