LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2010, 09:09 PM   #31
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Original Poster
Rep: Reputation: 39

I am not too sure as what is the problem as in the link to the book I gave the author does not even says to use the -I flag and the kernel series is for newbies only.I have checked the program twice
Code:
#include <linux/kernel.h>
#include <linux/module.h>
#include <sys/syscall.h>
extern void *sys_table[];
asmlinkage int(*main_sys_exit)(int);
asmlinkage int alt_exit_function(int err_code)
{
        printk("Sys_exit called with err_code=%d\n",err_code);
        return main_sys_exit(err_code);
}

int init_module()
{
        main_sys_exit=sys_table[__NR_exit];
        sys_table[__NR_exit]=alt_exit_function;
}
void cleanup_module()
{
        sys_table[__NR_exit]=main_sys_exit;
}
The way in the original book it says to compile is

Code:
gcc  -Wall -DMODULE -D__KERNEL__ -DLINUX -c sample2.c
The above is the way book says to compile it.
Just try once if you can get it working.
Check this link
http://www.spinics.net/lists/newbies/msg39939.html
 
Old 08-23-2010, 09:24 PM   #32
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by tkmsr View Post
...
The above is the way book says to compile it.
...
And why should anybody care about the book ?

You should be able to live by the standard and by the reality. In your reality the code shouldn't compile exactly because of the standard.
 
Old 08-24-2010, 12:23 AM   #33
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Original Poster
Rep: Reputation: 39
You are right to an extent.Given the fact that I am learning about Kernel Development what ever that book had said so I want to follow what ever is mentioned in that book.
Hence I have not yet left the program as the book says.
I did a find for the program as given in book


Code:
/usr/src/linux-headers-2.6.28-11-generic/include/linux/module.h
/usr/src/linux-headers-2.6.28-11-generic/include/linux/kernel.h
/usr/include/sys/syscall.h
Above are the files I feel the program is referring to.

So I executed
Code:
gcc -I /usr/src/linux-headers-`uname -r`/include  -I /usr/include -Wall -DMODULE -D__KERNEL__ -DLINUX -c sample2.c
but there were errors
you can see them here.
http://pastebin.com/hrYdjmZ4

I am giving up finally.
Thanks for staying here.

Last edited by tkmsr; 08-24-2010 at 12:25 AM.
 
Old 08-24-2010, 07:32 AM   #34
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by tkmsr View Post
...
but there were errors
you can see them here.
http://pastebin.com/hrYdjmZ4

I am giving up finally.
Thanks for staying here.
The

Code:
/usr/src/linux-headers-2.6.28-11-generic/include/linux/linkage.h:5:25: error: asm/linkage.h: No such file or directory
error can be resolved the same way as previous errors. So, I do not understand why you are giving up.
 
Old 08-24-2010, 11:07 AM   #35
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Original Poster
Rep: Reputation: 39
Ok here is what made it work.
This is the program
Code:
#include <linux/kernel.h>
#include <asm/syscall.h>
#include <linux/module.h>
#include <linux/unistd.h>
extern void *sys_table[];
asmlinkage int(*main_sys_exit)(int);
asmlinkage int alt_exit_function(int err_code)
{
       printk("Sys_exit called with err_code=%d\n",err_code);
       return main_sys_exit(err_code);
}

int init_module()
{
       main_sys_exit=sys_table[__NR_exit];
       sys_table[__NR_exit]=alt_exit_function;
}
void cleanup_module()
{
       sys_table[__NR_exit]=main_sys_exit;
}
and this is the Makefile
Code:
obj-m += sample21.o
all: 
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean: 
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
I got it working with a warning
Code:
make -C /lib/modules/2.6.28-11-generic/build M=/home/tkmsr/exer modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.28-11-generic'
  CC [M]  /home/tkmsr/exer/sample21.o
/home/tkmsr/exer/sample21.c: In function ‘init_module’:
/home/tkmsr/exer/sample21.c:17: warning: control reaches end of non-void function
  Building modules, stage 2.
  MODPOST 1 modules
WARNING: "sys_table" [/home/tkmsr/exer/sample21.ko] undefined!
  CC      /home/tkmsr/exer/sample21.mod.o
  LD [M]  /home/tkmsr/exer/sample21.ko
Where as the program given in book is
Code:
#include <linux/kernel.h>
#include <linux/module.h>
#include <sys/syscall.h>
extern void *sys_table[];
asmlinkage int(*main_sys_exit)(int);
asmlinkage int alt_exit_function(int err_code)
{
        printk("Sys_exit called with err_code=%d\n",err_code);
        return main_sys_exit(err_code);
}

int init_module()
{
        main_sys_exit=sys_table[__NR_exit];
        sys_table[__NR_exit]=alt_exit_function;
}
void cleanup_module()
{
        sys_table[__NR_exit]=main_sys_exit;
}
and the Makefile (book says to go by gcc not Makefile)
Code:
obj-m += sample2.o
all: 
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean: 
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
again failed.With following errors
Code:
make -C /lib/modules/2.6.28-11-generic/build M=/home/tkmsr/exer modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.28-11-generic'
  CC [M]  /home/tkmsr/exer/sample2.o
/home/tkmsr/exer/sample2.c:3:25: error: sys/syscall.h: No such file or directory
/home/tkmsr/exer/sample2.c: In function ‘init_module’:
/home/tkmsr/exer/sample2.c:14: error: ‘__NR_exit’ undeclared (first use in this function)
/home/tkmsr/exer/sample2.c:14: error: (Each undeclared identifier is reported only once
/home/tkmsr/exer/sample2.c:14: error: for each function it appears in.)
/home/tkmsr/exer/sample2.c: In function ‘cleanup_module’:
/home/tkmsr/exer/sample2.c:19: error: ‘__NR_exit’ undeclared (first use in this function)
make[2]: *** [/home/tkmsr/exer/sample2.o] Error 1
make[1]: *** [_module_/home/tkmsr/exer] Error 2
make[1]: Leaving directory `/usr/src/linux-headers-2.6.28-11-generic'
make: *** [all] Error 2
 
Old 08-24-2010, 11:38 AM   #36
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
Quote:
error: sys/syscall.h: No such file or directory
sudo apt-get install libc6-dev

.. will provide : /usr/include/sys/syscall.h
..
 
Old 08-24-2010, 01:16 PM   #37
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Original Poster
Rep: Reputation: 39
Code:
sudo apt-get install libc6-dev
[sudo] password for tkmsr: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
libc6-dev is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 115 not upgraded.
 
Old 08-24-2010, 02:10 PM   #38
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by tkmsr View Post
Code:
sudo apt-get install libc6-dev
[sudo] password for tkmsr: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
libc6-dev is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 115 not upgraded.
There is nothing new - find where the file actually is, provide 'gcc' with the needed search place.
 
Old 08-24-2010, 09:29 PM   #39
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Original Poster
Rep: Reputation: 39
Why do I have to do it like this?
In any other program I did not had to do it like this.
 
Old 08-25-2010, 06:21 AM   #40
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by tkmsr View Post
Why do I have to do it like this?
In any other program I did not had to do it like this.
The answers are:
  1. C99 standard
  2. the program's author
.

You can't change the standard, but you can pick another program to play with.
 
  


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
(EE) Failed to load module "nvidia" (module does not exist, 0) sanitynotvanity Slackware 6 12-11-2011 12:04 PM
Failed to load module "ati" (module does not exist, after upgrad from Redhat 3 to 5. perrym8 Red Hat 2 07-24-2007 10:14 AM
Arch: error is failed to load vga, fbdev and nv module :doesnt exist flipwhy Linux - Newbie 24 04-08-2007 08:53 PM
X says nvidia module does not exist but it does broken-cog Linux - Software 3 02-14-2005 12:22 PM
ATI 9600: fglrx module is loaded but doesn't exist lopette Linux - Hardware 0 11-29-2004 01:26 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:42 PM.

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