ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
#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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.