Results 1 to 4 of 4
I have just had a first attempt at this, using the simple 'Hello World' module :
Code:
#define MODULE
#include <linux/module.h>
int init_module(void)
{
printk("<1>Hello, world\n");
return 0;
}
void ...
- 04-14-2006 #1Just Joined!
- Join Date
- Apr 2006
- Posts
- 2
Kernel Module programming...
I have just had a first attempt at this, using the simple 'Hello World' module :
Now this is the 'standard' example used in just about all ttutorials and books (some have a few extra bits like #include <linux/hernel.h> and such - but the much the same).Code:#define MODULE #include <linux/module.h> int init_module(void) { printk("<1>Hello, world\n"); return 0; } void cleanup_module(void) { printk ("<1>Goodbye cruel world\n"); }
When i try to compile with the command
gcc -c hello.c as suggested in one book - I get a huge list of errors (mostly about things not being declared, or defined.. etc.)
Any pointers?
- 04-18-2006 #2
Try this
You tried the old way to compile modules, now in 2.6 series this is do in this way:
obj-m += hello-1.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
Best regards!
- 04-18-2006 #3Just Joined!
- Join Date
- Apr 2006
- Posts
- 2
Thanks for your reply.
I also came across this link which proved useful.. (says the same thing that you said..)
http://www.captain.at/programming/kernel-2.6/
- 08-28-2007 #4Just Joined!
- Join Date
- Aug 2007
- Posts
- 1
try this
Adding a module to Linux Kernel
module name:hello.c
This is a simple module that will be inserted into the kernel.
when it is inserted into Kernel it display a message Hello world
When it is removed from the Kernel it display a message Goodbye, world
-----------------------------------------------------------------
#ifndef __MODULE__
#define __MODULE__
#endif
#ifndef __KERNEL__
#define __KERNEL__
#endif
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
static int __init hello_2_init(void)
{
printk(KERN_EMERG "Hello, world 2\n");
return 0;
}
static void __exit hello_2_exit(void)
{
printk(KERN_EMERG "Goodbye, world 2\n");
}
module_init(hello_2_init);
module_exit(hello_2_exit);
-------------------------------------------------------------------
Compilation steps:
1.Create a Makefile ( it will be used bu make command)
contents in make file
-------------------------------------------------------------------
KDIR:=/lib/modules/$(shell uname -r)/build
obj-m:=hello.o
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
$(RM) *.ko *.mod.c *.mod.o *.o
--------------------------------------------------------------------
Once the makefile is ready, use the following command to compile
1.make
2.lsmod|less
3.dmesg
4.rmmod
5.dmesg
Message on the console after each command is executed
1.make
--------------------------------------------------------------------------
[root@localhost new]# make
make -C /lib/modules/2.6.18-1.2798.fc6/build SUBDIRS=/root/Desktop/new modules
make[1]: Entering directory `/usr/src/kernels/2.6.18-1.2798.fc6-i586'
CC [M] /root/Desktop/new/hello.o
Building modules, stage 2.
MODPOST
CC /root/Desktop/new/hello.mod.o
LD [M] /root/Desktop/new/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.18-1.2798.fc6-i586'
[root@localhost new]#
---------------------------------------------------------------------------
2.insmod hello.ko
---------------------------------------------------------------
[root@localhost new]# insmod hello.ko
[root@localhost new]#
Message from syslogd@localhost at Tue Aug 28 12:38:12 2007 ...
localhost kernel: Hello, world 2
---------------------------------------------------------------
-----------------------------------------------------------------------------
4.lsmod | less
---------------------------------------------------------------------
Module Size Used by
hello 5632 0
autofs4 25413 2
hidp 24129 2
rfcomm 45912 0
l2cap 31681 10 hidp,rfcomm
bluetooth 58917 5 hidp,rfcomm,l2cap
sunrpc 158333 1
ip_conntrack_ftp 12081 0
ip_conntrack_netbios_ns 7105 0
ipt_REJECT 9665 1
xt_state 6337 6
ip_conntrack 56993 3 ip_conntrack_ftp,ip_conntrack_netbios_ns,xt_stat
e
nfnetlink 11353 1 ip_conntrack
iptable_filter 7233 1
ip_tables 17669 1 iptable_filter
ip6t_REJECT 9537 1
xt_tcpudp 7361 21
ip6table_filter 7105 1
ip6_tables 18821 1 ip6table_filter
x_tables 18501 6 ipt_REJECT,xt_state,ip_tables,ip6t_REJECT,xt_tcp
udp,ip6_tables
-----------------------------------------------------------------------
5.rmmod
------------------------------------------------------------
[root@localhost new]# rmmod hello
[root@localhost new]#
Message from syslogd@localhost at Tue Aug 28 12:43:51 2007 ...
localhost kernel: Goodbye, world 2
--------------------------------------------------------------
6.make clean
----------------------------------------------------------
[root@localhost new]# make clean
rm -f *.ko *.mod.c *.mod.o *.o
-----------------------------------------------------------



