Is there anybody that can help me understand what`s wrong with this code?
Mostly i`m not sure about the correct use of the functions filp_open,
generic_file_read and filp_close.
When i compile as a module on the kernel and after, when i do the INSMOD ,
at first said SEGMENTATION FAULT, right now it just blocks the whole
machine.
Any help or hint is very welcome!!!
thanks
boba
Here is the code:
/* FILE NAME : pops.c */
#include <linux/kernel.h>
#include <linux/module.h>
#define BUFSIZ 8192
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include <linux/modversions.h>
#endif
#include <linux/fs.h>
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,2,0)
#include <asm/uaccess.h>
#endif
int init_err_frame(int []);
/* prints the vector err_frame */
int init_module()
{
int err_frame[BUFSIZ];
int i, k;
k = init_err_frame(err_frame);
for (i = 0; i < k; i++){
printk("%d \n", err_frame[i]);
}
return 0;
}
/* inizializza il vettore err_frame */
int init_err_frame(int err_frame[]) {
int i, k = 0, j = 0;
char buffer[BUFSIZ];
int temp[2];
int retval;
struct file * f =
filp_open("/usr/src/kernel-source-2.2.19/pinux/misc/err_file", 0, 0); /* i
want it only readable */
if (f == '\0'){
printk ("errore nell' APERTURA del file d'errore");
return -1;
}
printk("%d ", f->f_count);
if ((generic_file_read(f, buffer, BUFSIZ, &f->f_pos)) < 0 ){
printk ("errore nella LETTURA del file");
if ((retval = filp_close(f, NULL)) < 0){
printk ("errore nella CHIUSURA del file");
return -3;
}
return -2;
}
for (i=0; i < '\0'; i++){
temp[j] = buffer[i];
if (j == 1){
err_frame[k] = (int) temp[0] + (int) temp[1];
j = 0;
k++;
} else
j = 1;
}
filp_close(f,NULL);
return k;
}
void cleanup_module()
{
printk(".........");
}
and i compile it as
cc -D__KERNEL__ -I/usr/src/kernel-source-2.2.19/include -Wall
-Wstrict-prototypes -O2 -fomit-frame-pointer -fno-strict-aliasing -pipe
-fno-strength-reduce -mpreferred-stack-boundary=2 -m486 -DCPU=486 -DMODULE
-DEXPORT_SYMTAB -c pops.c
_________________________________________________________________
MSN Foto ? il modo pi? semplice per condividere e stampare le tue foto:
http://photos.msn.com/support/worldwide.aspx
On Apr 24, 2002 18:06 +0200, il boba wrote:
> Is there anybody that can help me understand what`s wrong with this code?
Yes, easily spotted a major problem without even reading the whole
thing.
> #define BUFSIZ 8192
>
> int init_module()
> {
> int err_frame[BUFSIZ];
The entire kernel stack is only 8kB in size. You have already killed
a bunch of random memory by allocating this much memory on the stack.
You allocated 4*8192 = 32kB on the stack here.
> #if CONFIG_MODVERSIONS==1
> #define MODVERSIONS
> #include <linux/modversions.h>
> #endif
You shouldn't do things like this. Rather have a makefile which sets
the correct defines.
> int init_err_frame(int err_frame[]) {
> int i, k = 0, j = 0;
> char buffer[BUFSIZ];
Another 8kB on the stack here - further random corruption.
> struct file * f =
> filp_open("/usr/src/kernel-source-2.2.19/pinux/misc/err_file", 0, 0); /* i
> want it only readable */
More clear to add "O_RDONLY" to filp_open, even though it is still 0.
> if (f == '\0'){
> printk ("errore nell' APERTURA del file d'errore");
> return -1;
> }
How about "if (f == NULL)"?
> if ((generic_file_read(f, buffer, BUFSIZ, &f->f_pos)) < 0 ){
Calling generic_file_read() may work in some cases, but not others. It
depends on the filesystem. Use "f->f_op->read" instead.
Cheers, Andreas
--
Andreas Dilger
http://www-mddsp.enel.ucalgary.ca/People/adilger/
http://sourceforge.net/projects/ext2resize/
On Wed, Apr 24, 2002 at 10:40:56AM -0600, Andreas Dilger wrote:
> On Apr 24, 2002 18:06 +0200, il boba wrote:
> > Is there anybody that can help me understand what`s wrong with this code?
......
Even with all this he's going to have trouble since his buffer is in
kernel space and the read is going to expect it to be in user-context...
Reading and writing files from within the kernel is almost never a good
idea and has been made difficult by design. He should look at what
he is trying to do and perhaps look for another interface, such as
use of ioctls, proc, seq_file, etc.. He probably doesn't need to
do this I/O from within the kernel and should rethink it.
Jerry Cooperstein <[email protected]>
Senior Consultant
Axian, Inc. <[email protected]>
4800 SW Griffith Dr., Ste. 202, Beaverton, OR 97005 USA
http://www.axian.com/ Software Consulting and Training
>On Apr 24, 2002 18:06 +0200, il boba wrote:
>> Is there anybody that can help me understand what`s wrong with this code?
>Yes, easily spotted a major problem without even reading the whole
>thing.
>> #define BUFSIZ 8192
>>
>> int init_module()
>> {
>> int err_frame[BUFSIZ];
>
>The entire kernel stack is only 8kB in size. You have already killed
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>a bunch of random memory by allocating this much memory on the stack.
>You allocated 4*8192 = 32kB on the stack here.
Sure, the kernel stack is 8192 Bytes, but "err_frame[]" is a local variable. Does the kernel allocate memory for "err_frame[]" from the stack??
>> int init_err_frame(int err_frame[]) {
>> int i, k = 0, j = 0;
>> char buffer[BUFSIZ];
>
>Another 8kB on the stack here - further random corruption.
Here, I think, err_frame[] as a function parameter will take 8K in the kernel stack.
Am I correct?
Thank you.
On Thu, Apr 25, 2002 at 05:12:21PM +0800, Huo Zhigang wrote:
...
> >The entire kernel stack is only 8kB in size. You have already killed
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >a bunch of random memory by allocating this much memory on the stack.
> >You allocated 4*8192 = 32kB on the stack here.
>
> Sure, the kernel stack is 8192 Bytes, but "err_frame[]" is a local
> variable. Does the kernel allocate memory for "err_frame[]" from the
> stack??
It is not about how KERNEL does it, but how C (programming language)
does it. If you don't know C's memory management things regarding
various classes of variables, I suggest you pick some good reference
book and study it asap.
> Here, I think, err_frame[] as a function parameter will take 8K in
> the kernel stack. Am I correct?
Sorry, this isn't "programming in C"-education forum..
> Thank you.
/Matti Aarnio
I am very sorry to ask such a stupid question. I think I have to work harder 8-)
Thank you all, especially Matti Aarnio, Mike Galbraith, Joe Thornber.
Now, I began to treat the kernel as an ordinary, but much more complex, C program. :)
>On Thu, Apr 25, 2002 at 05:12:21PM +0800, Huo Zhigang wrote:
>....
>> >The entire kernel stack is only 8kB in size. You have already killed
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> >a bunch of random memory by allocating this much memory on the stack.
>> >You allocated 4*8192 = 32kB on the stack here.
>>
>> Sure, the kernel stack is 8192 Bytes, but "err_frame[]" is a local
>> variable. Does the kernel allocate memory for "err_frame[]" from the
>> stack??
>
> It is not about how KERNEL does it, but how C (programming language)
> does it. If you don't know C's memory management things regarding
> various classes of variables, I suggest you pick some good reference
> book and study it asap.
>
>> Here, I think, err_frame[] as a function parameter will take 8K in
>> the kernel stack. Am I correct?
>