Thursday, November 8, 2012

Linux Kernel Modules: Example #2

Here is another example of a module for the linux kernel

File:hello-5.c

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/stat.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Giridhar Bhujanga");

static short int myshort = 1;
static int myint = 420;
static long int mylong = 9999;
static char *mystring = "blah";
static int myintArray[2] = { -1, 1 };
static int arr_argc = 0;

/*
 * module_param(foo, int, 0000)
 * the first param is the parameters name
 * the second parameter is it's data type
 * the final argument is the permission bits
 * for exposing parameters in sysfs (if non-zero) at a later stage
 */

module_param(myshort, short, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP );
MODULE_PARM_DESC(myshort, "A short integer");

module_param(myint, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
MODULE_PARM_DESC(myint, "An integer");

module_param(mylong, long, S_IRUSR);
MODULE_PARM_DESC(mylong, "A long integer");

module_param(mystring , charp, 0000);
MODULE_PARM_DESC(mystring, "A character string");

/* module_param_array(name, type, num, perm);
 * the first param is the parameter's (in this case array's) name
 * the second param is the data type of the elements in the array
 * the third argument is the pointer to the variable that will store the number
 * of elements of the array initialized by the user at the module loading time
 * the fourth argument is the permission bits
 */

static int __init hello_5_init(void)
{
   int i;
   printk(KERN_INFO "Hello world 5\n=========================\n");
   printk(KERN_INFO "myshort is a short integer: %hd\n", myshort);
   printk(KERN_INFO "myint is an integer: %d\n", myint);
   printk(KERN_INFO "mylong is a long integer: %ld\n", mylong);
   printk(KERN_INFO "mystring is a string: %s\n", mystring);

   for(i=0; i< (sizeof myintArray / sizeof (int)); i++)
   {
      printk(KERN_INFO "myintArray[%d] = %d\n", i, myintArray[i]);
   }

   printk(KERN_INFO "\ngot %d arguments for myintArray\n",arr_argc);
   return 0;
}

static void __exit hello_5_exit(void)
{
   printk(KERN_INFO "\n>>> Good Bye !! <<<\n");
}

module_init(hello_5_init);
module_exit(hello_5_exit);

File:Makefile

obj-m := hello-5.o
KVERSION := $(shell uname -r)
KDIR := /lib/modules/$(shell uname -r)/build/
PWD := $(shell pwd)
INC := -I/usr/src/kernels/2.6.18-194.el5-x86_64/include/ -I/usr/include -I/usr/lib/x86_64-redhat-linux4E/include/

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

clean:
 $(MAKE) $(INC) -C $(KDIR) M=$(PWD) clean

Output of "make"

[root@RHEL-55 tutorial-2]# make
make -I/usr/src/kernels/2.6.18-194.el5-x86_64/include/ -I/usr/include -I/usr/lib/x86_64-redhat-linux4E/include/ -C /lib/modules/2.6.18-194.el5/build/ M=/root/kernel-programs/tutorial-2 modules
make[1]: Entering directory `/usr/src/kernels/2.6.18-194.el5-x86_64'
  CC [M]  /root/kernel-programs/tutorial-2/hello-5.o
  Building modules, stage 2.
  MODPOST
  CC      /root/kernel-programs/tutorial-2/hello-5.mod.o
  LD [M]  /root/kernel-programs/tutorial-2/hello-5.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.18-194.el5-x86_64'
[root@RHEL-55 tutorial-2]# 

Output ("tail -f /var/log/messages")

Nov  8 22:19:33 RHEL-55 kernel: Hello world 5
Nov  8 22:19:33 RHEL-55 kernel: =========================
Nov  8 22:19:33 RHEL-55 kernel: myshort is a short integer: 1
Nov  8 22:19:33 RHEL-55 kernel: myint is an integer: 420
Nov  8 22:19:33 RHEL-55 kernel: mylong is a long integer: 9999
Nov  8 22:19:33 RHEL-55 kernel: mystring is a string: blah
Nov  8 22:19:33 RHEL-55 kernel: myintArray[0] = -1
Nov  8 22:19:33 RHEL-55 kernel: myintArray[1] = 1
Nov  8 22:19:33 RHEL-55 kernel: 
Nov  8 22:19:33 RHEL-55 kernel: got 0 arguments for myintArray
Nov  8 22:19:49 RHEL-55 kernel: 
Nov  8 22:19:49 RHEL-55 kernel: >>> Good Bye !! <<<

No comments :

Post a Comment