LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-06-2007, 11:30 PM   #1
jayeshleo
LQ Newbie
 
Registered: Nov 2007
Posts: 3

Rep: Reputation: 0
Insert a user written .c program in kernel 2.4.20-8


Hello!

I have written a hello.c program
#define MODULE
#define LINUX
#define __KERNEL__

#include<linux/init.h>
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/config.h>



int init_module(void)
{
printk("<1> Hello World 1.\n");
return 0;
}

void cleanup_module(void)
{
printk("<1> Bye World");
}

I have also written a Makefile for it...

TARGET := hello
WARN := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /lib/modules/'uname -r'/build/include
CFLAGS := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
CC := gcc

${TARGET}.o: ${TARGET}.c

.PHONY: clean

clean:
rm -rf ${TARGET}.o

Can anybody please tell me the steps to insert the module in the kernel. so that if i do dmesg on terminal it should say "Hello World"..Tell me corrections if any in the above two programs...

Thanks

Last edited by jayeshleo; 12-06-2007 at 11:31 PM.
 
Old 12-07-2007, 12:34 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Well, if you are able to successfully compile your module, then inserting it is a breeze. You need to do it using this command:

Code:
# insmod /path/leading/to/your-module.ko
You can verify if the module loaded by running:
Code:
# lsmod | grep your-module
You can then remove the module using this command:

Code:
# rmmod your-module
 
Old 12-07-2007, 12:42 AM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Btw, I should add that I'm no expert when it comes to developing/compiling kernel modules, however I must state that your Makefile look overly complex compared to one that I use for a kernel module. My Makefile looks like:
Code:
obj-m += my-module.o
I build the module running this command:

Code:
make -C /lib/modules/`uname -r`/build M=`pwd`
For this to work, I must be in the directory where the source code of my kernel module and the Makefile (shown above) exists.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiling a program written for 2.4 kernel, with 2.6 kernel rvenkatesh25 Programming 0 08-28-2007 09:31 AM
how to insert a module in kernel through a user defined service sharad Linux - General 5 03-25-2007 11:46 PM
How Do I Run/Compile a Program Written in C? debsan Fedora 2 05-25-2006 02:58 PM
have you ever written a program for linux? today53 General 33 12-05-2003 06:54 PM
another dumb program written in bash. micxz Programming 0 10-13-2003 07:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration