f5d9670c38535f82a3a879be869fff1f1160f26a
[cascardo/kernel/samples/hello2/.git] / hello.c
1 /*
2  *  Copyright (C) 2010  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/init.h>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>");
28 MODULE_DESCRIPTION("Hello, world");
29
30 struct hello {
31         int times;
32         char greeting[0];
33 };
34
35 static struct hello *hello;
36
37 static char default_greeting[] = "Hello, world!\n";
38
39 static int hello_init(void)
40 {
41         hello = kmalloc(sizeof(*hello) + sizeof(default_greeting), GFP_KERNEL);
42         if (!hello)
43                 return -ENOMEM;
44         memcpy(hello->greeting, default_greeting, sizeof(default_greeting));
45         printk("%s\n", hello->greeting);
46         printk("%p %p %p\n", hello, hello->greeting,
47                 container_of(&hello->greeting, struct hello, greeting));
48         printk("size %d\n", ARRAY_SIZE(default_greeting));
49         return 0;
50 }
51
52 static __exit void hello_exit(void)
53 {
54         kfree(hello);
55 }
56
57 module_init(hello_init);
58 module_exit(hello_exit);