2005-03-23 05:20:50

by P.Manohar

[permalink] [raw]
Subject: segmentation fault while loading modules


hai,

I have a problem in loading a module ,it is giving segmentation fault,
when I do the /sbin/lsmod
signal4 944 (initializing)

here signal4 is my module name.I am using 2.4.20-8 kernel.
And I am unable to remove this module also.

following is the code of my module

#include <bits/signum.h> /* signal number macros SIGHUP etc. */
#include <linux/kernel.h> /* printk level */
#include <linux/module.h> /* kernel version etc. */
#include <linux/sched.h> /* task_struct */

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Signal to a user-space app from a kernel module");

int pid=0;
MODULE_PARM(pid,"i");

int
ksignal(int pid,int signum)
{
struct task_struct x;
struct task_struct *p;
/* run through the task list of linux until we find our pid */
//for (p = &init_task ; (p = next_task(p)) != &init_task ; ){
for (p = &x ; (p = next_task(p)) != &x ; ){
if(p->pid == pid){
printk("sending signal %d for pid %d\n",signum,(int)p->pid);
/* don't have a sig_info struct to send along - pass 0 */
return send_sig(signum,p,0);
}
}
/* did not find the requested pid */
return -1;
}

int
init_module(void)
{
/* send pid a SIGKILL */
ksignal(2345,SIGKILL);
return 0;
}

void
cleanup_module(void)
{
printk("out of here\n");
}
-----------------------
my makefile is:

TARGET := signal4
WARN := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /lib/modules/2.4.20-8feb9.1/build/include
CFLAGS := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
CC := gcc

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

.PHONY: clean


Any suggestions are welcome.





Thanks&Regards,
P.Manohar.


2005-03-23 06:49:16

by Jan Engelhardt

[permalink] [raw]
Subject: Re: segmentation fault while loading modules

> int
> ksignal(int pid,int signum)
> {
> struct task_struct x;
> struct task_struct *p;
> /* run through the task list of linux until we find our pid */
> //for (p = &init_task ; (p = next_task(p)) != &init_task ; ){
> for (p = &x ; (p = next_task(p)) != &x ; ){
...

next_task(p) is defined (not in the sense of a macro, though) as
p->tasks.next
and your x is not initiailzed, so what do you expect next_task(x)
to do, if p->tasks... does not contain a valid value?

You want this:
for(p = &init_task; (p = next_task(p)) != &init_task; ) {
...
}


Jan Engelhardt
--

2005-03-23 09:30:24

by Sam Ravnborg

[permalink] [raw]
Subject: Re: segmentation fault while loading modules

> my makefile is:
>
> TARGET := signal4
> WARN := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
> INCLUDE := -isystem /lib/modules/2.4.20-8feb9.1/build/include
> CFLAGS := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
> CC := gcc
>
> ${TARGET}.o: ${TARGET}.c
>
> .PHONY: clean
>
>
> Any suggestions are welcome.

It may be due to inconsistency in gcc flags.

Plase try with a simpler Makefile:
Makefile:
obj-m := signal4.o

And compile with:
make -C /lib/modules/2.4.20-8feb9.1/build SUBDIRS=`pwd` modules

This will make sure you use correct gcc flags.

Sam