Skip to content

Commit 108e40d

Browse files
committed
Subject: Again module with parameters
1 parent dc7331e commit 108e40d

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

module_param2/Makefile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Makefile for module_param.c
2+
# Makefile adopted from free-electrons slides on Kernel development
3+
ifneq ($(KERNELRELEASE),)
4+
obj-m := module-param.o
5+
else
6+
KDIR=/usr/src/linux-headers-$(shell uname -r)
7+
8+
all: modules
9+
10+
modules:
11+
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
12+
modules_install:
13+
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules_install
14+
kernel_clean:
15+
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean # from lkmpg
16+
clean: kernel_clean
17+
rm -rvf *~
18+
19+
endif
20+
21+

module_param2/module-param.c

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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

Comments
 (0)