|
| 1 | +/* hello_param.c*/ |
| 2 | +/* Usage: |
| 3 | + sudo insmod module_param.ko |
| 4 | +
|
| 5 | + OR |
| 6 | +
|
| 7 | + sudo rmmod module_param.ko |
| 8 | + */ |
| 9 | + |
| 10 | +#include<linux/init.h> /* to initiate a module */ |
| 11 | +#include<linux/module.h> /* to recognise this module as a mmodule */ |
| 12 | + |
| 13 | +static char *whom = "Hello"; |
| 14 | +module_param(whom, charp, 0); |
| 15 | +MODULE_PARM_DESC(whom, "Whom to greet?"); |
| 16 | + |
| 17 | +static int howmany = 3; |
| 18 | +module_param(howmany, int, 0); |
| 19 | +MODULE_PARM_DESC(howmany, "How many subjects?"); |
| 20 | + |
| 21 | +static int marks[3] = {18, 67, 90}; |
| 22 | +module_param_array(marks, int, &howmany, 0); |
| 23 | +MODULE_PARM_DESC(marks, "Marks obtained"); |
| 24 | + |
| 25 | +static int __init hello_param_init(void) { |
| 26 | + /* entry point of the module */ |
| 27 | + /* to register functionalities and to allocate resources */ |
| 28 | + int i; |
| 29 | + printk(KERN_INFO "%s world", whom); |
| 30 | + for (i = 0; i < howmany; i++) { |
| 31 | + pr_alert("Subject-%d: %d\n", i, marks[i]); |
| 32 | + } |
| 33 | + |
| 34 | + return 0; |
| 35 | +} |
| 36 | + |
| 37 | +static void __exit hello_param_exit(void) { |
| 38 | + /* exit point of the module */ |
| 39 | + /* to unregister functionalities and to deallocate resources */ |
| 40 | + pr_alert(KERN_ALERT "GoodBye, cruel %s\n", whom); |
| 41 | +} |
| 42 | + |
| 43 | +module_init(hello_param_init); /* whenever the module loads, just to |
| 44 | + this line and start hello_init |
| 45 | + function first */ |
| 46 | +module_exit(hello_param_exit); /* when the module is removed, |
| 47 | + initiate the function |
| 48 | + hello_exit. And deallocate any |
| 49 | + resources this module uses */ |
| 50 | + |
| 51 | +/* additional module info. Can be displayed when calling 'modinfo hello3.ko' */ |
| 52 | +MODULE_LICENSE("GPL"); /* license */ |
| 53 | +MODULE_AUTHOR("Tas Devil"); /* who wrote it */ |
| 54 | +MODULE_DESCRIPTION("hello3: Kernel module demo"); /* purpose of module */ |
| 55 | +MODULE_VERSION("0.9"); |
0 commit comments