Skip to content

Commit 4e7e21b

Browse files
silviupopescudbaluta
authored andcommitted
misc-modules: bring 'Hello World' kernel modules to the modern era.
Update the modules described in Chapter 2 of LDD3 to use pr_fmt() and place initialization and cleanup routines in special code sections. Signed-off-by: Silviu-Mihai Popescu <[email protected]> Signed-off-by: Daniel Baluta <[email protected]>
1 parent 406eba0 commit 4e7e21b

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

misc-modules/hello.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
*/
44
#include <linux/init.h>
55
#include <linux/module.h>
6+
#include <linux/kernel.h>
7+
68
MODULE_LICENSE("Dual BSD/GPL");
9+
MODULE_AUTHOR("LDD3, Silviu-Mihai Popescu");
10+
MODULE_DESCRIPTION("'Hello World' module in LDD3, with modern practices.");
711

8-
static int hello_init(void)
12+
static int __init hello_init(void)
913
{
10-
printk(KERN_ALERT "Hello, world\n");
14+
pr_alert("Hello, world\n");
1115
return 0;
1216
}
1317

14-
static void hello_exit(void)
18+
static void __exit hello_exit(void)
1519
{
16-
printk(KERN_ALERT "Goodbye, cruel world\n");
20+
pr_alert("Goodbye, cruel world\n");
1721
}
1822

1923
module_init(hello_init);

misc-modules/hellop.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
#include <linux/init.h>
55
#include <linux/module.h>
66
#include <linux/moduleparam.h>
7+
#include <linux/kernel.h>
78

89
MODULE_LICENSE("Dual BSD/GPL");
10+
MODULE_AUTHOR("LDD3, Silviu-Mihai Popescu");
11+
MODULE_DESCRIPTION("'Hello World with params' module in LDD3.\
12+
with modern practices.");
913

1014
/*
1115
* These lines, although not shown in the book,
@@ -23,17 +27,17 @@ static int howmany = 1;
2327
module_param(howmany, int, S_IRUGO);
2428
module_param(whom, charp, S_IRUGO);
2529

26-
static int hello_init(void)
30+
static int __init hello_init(void)
2731
{
2832
int i;
2933
for (i = 0; i < howmany; i++)
30-
printk(KERN_ALERT "(%d) Hello, %s\n", i, whom);
34+
pr_alert("(%d) Hello, %s\n", i, whom);
3135
return 0;
3236
}
3337

34-
static void hello_exit(void)
38+
static void __exit hello_exit(void)
3539
{
36-
printk(KERN_ALERT "Goodbye, cruel world\n");
40+
pr_alert("Goodbye, cruel world\n");
3741
}
3842

3943
module_init(hello_init);

0 commit comments

Comments
 (0)