Tuesday 16 July 2013

[Kernel Programming #5] Passing command line arguments to module

How to pass command line arguments to module.
We know argc/argv in C programming to pass command line arguments.

In kernel programming also, we have set of macros to do this task. They are,
module_param(name, type, permissions)
module_param_array(name, type, number_pointer, permissions)
and, we have other macros also, will discuss it later.
These macros are defined in <linux/moduleparam.h>.

Example: insmod module_2.ko variable_name=value, variable_name2=value

The argument name is the name of the variable declared in the module. The variables which we can define through command line option should be declared as global variable only. The name which we use in command line argument should match with the defined variable name in the module. Instead, we have another macro to set the name of the command line argument different from the variable name in the module.

module_param_named(command_line_var_name, actual_var_name, type, permissions)

The argument type is the type of the variable. It might be int, long, short, byte, uint, ulong, ushort, charp and bool.
charp: is used in case of string variable. The kernel copy and store the string passed by user in memory and points the actual variable to this memory.
Example:
static char *student_name;
module_param(student_name, charp, perm);

Instead, if we want to copy/store the string into the memory which is pointed by the actual variable then we can use another macro.

module_param_string(command_line_var_name, actual_var_name, len, permissions)
Example:
static char student_name[len];
module_param_string(studentname, student_name, len, perm);

The argument permissions is used to set read/write permissions for the variable.
Typical value would be 0644 (owner can read, write, group can read, everyone can read).

Kernel can also accepts the array by using the macro module_param_array.
The extra argument number_pointer is a pointer to an array_datatype in which the kernel stores the number of entries stored into the array.
Example:
static int students_marks[count];
static int marks;
module_param_array(students_marks, int, &marks, perm);

Program: module_param.c


  1. #include<linux/module.h>
  2. #include<linux/init.h>
  3. #include<linux/moduleparam.h>

  4. static int param_int = 999;
  5. static char *param_string = "KERNEL";
  6. static int param_array[3] = {-1,-1,-1};
  7. static int count = 0;
  8. static char param_string_len[10];

  9. module_param(param_int, int, 0);
  10. module_param(param_string, charp, 0);
  11. module_param_string(param_string_len, param_string_len, 10, 0);
  12. module_param_array(param_array, int, &count, 0);

  13. static int __init module_param_init(void) {
  14.         int i;

  15.         printk(KERN_INFO "param_int: %d\n", param_int);
  16.         printk(KERN_INFO "param_string: %s\n", param_string);
  17.         printk(KERN_INFO "param_string_len: %s\n", param_string_len);
  18.         for (i = 0; i < (sizeof(param_array)/sizeof(int)); i++)
  19.                 printk(KERN_INFO "param_array[%d]: %d\n", i, param_array[i]);

  20. return 0;
  21. }

  22. static void __exit module_param_exit(void) {
  23.         printk(KERN_INFO "module_param_exit:\n");
  24. }

  25. module_init(module_param_init);
  26. module_exit(module_param_exit);


Build: Please refer to previous posts to build the module, need to setup makefile.

Makefile for this program:
obj-m += module_param.o

KDIR = /usr/src/linux-headers-2.6.32-28-generic

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

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

Experiment_1: insmod module_param.ko

Output: dmesg
[17046328.638809] param_int: 999
[17046328.638812] param_string: KERNEL
[17046328.638814] param_string_len:
[17046328.638816] param_array[0]: -1
[17046328.638818] param_array[1]: -1

[17046328.638819] param_array[2]: -1

Experiment_2: insmod module_param.ko param_int=111 param_string="PRACTICE" param_string_len="PEOPLE" param_array=11,22,33

Output: dmesg
[17047110.761552] param_int: 111
[17047110.761555] param_string: PRACTICE
[17047110.761557] param_string_len: PEOPLE
[17047110.761559] param_array[0]: 11
[17047110.761561] param_array[1]: 22
[17047110.761562] param_array[2]: 33

How to build the module, how to write makefile and how to insert module using insmod, and how to check the log ? Please refer the below posts for detailed explanation:

No comments:

Post a Comment

You might also like

Related Posts Plugin for WordPress, Blogger...