Linux module

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.

Linux module

Postby soni » Mon Oct 18, 2004 3:37 am

Hello everybody, I'm working on a linux module, it needs to read from and write to the file and for all this reading and writing first the file has to be opened but when module tries to open the file it gets a negative file descriptor. Here is the code of the module ....



Code: Select all


/*
file, main.c
*/

/*
please change this macro so that it takes the path of the file which you like
to open.
*/
#define    YOUR_FILE_PATH  "/etc/my.file"

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

extern unsigned short int openOrCloseFile(void *, void *);

int init_module(){

   int fd = 0;
   openOrCloseFile(&fd, YOUR_FILE_PATH);

   if( fd > 0 )
      openOrCloseFile(&fd, "");
   else
      printk("openOrCloseFile ERROR with .... fd = %d\n",fd); 
   
   return 0;
}

void cleanup_module(){
}





Code: Select all


/*
file, module_c.c
*/

extern unsigned short int openOrCloseFile(void *, void *);

extern unsigned short int  openOrCloseFile(void * fileDescriptor,
                                           void * fileName){
 
   if( *((int *)fileDescriptor) == 0  ){         

/* try to open file in readonly mode */
      __asm__("movl $0x05, %%eax\n /* NR_OPEN */
               movl %1, %%ebx\n    /* pointer to file name */   
               xorl %%ecx, %%ecx\n /* mode, read only */
               int  $0x80\n         
               movl %%eax, %0" /* get the status of the operation */
                                  /* ouputs */
                               : "=r"(*(int *)fileDescriptor)
                                   /* inputs */
                               : "m"((char *)fileName)   /* "r"(fileName) */
                                   /* clobbered registers */
                               : "%eax", "%ebx", "%ecx"          );
   
/* has the file been opened successfully */
      if( *(int *)fileDescriptor < 0 )
         return(-1);
     
         
   }
   
   
   else{ 

/* close the file */
      __asm__("movl %1, %%ebx\n    /* give file descriptor */
               movl $0x06, %%eax\n /* NR_CLOSE */
               int  $0x80\n
               movl %%eax, %0"
                                  /* outputs  */
                               : "=r"(*(int *)fileDescriptor)
                                  /* inputs */
                               : "r"(*(int *)fileDescriptor)
                                    /* clobbered registers */
                               : "%eax", "%ebx"                  ); 
         
/* has the file been closed successfully */           
      if( *(int *)fileDescriptor != 0 )
         return(-1);
         
             
   }
   
   return(0);
 
}



Code: Select all


#Makefile

all      : main module_c myModule

main      : main.c
      gcc -O6 -Wall -D__KERNEL__ -DMODULE -o part.1 -c main.c

module_c   : module_c.c
      gcc -o part.2 -c module_c.c

myModule   : part.1 part.2
      ld -m elf_i386 -i -o myModule.o part.1 part.2

clean      :
      rm -f part.1
      rm -f part.2
      rm -f myModule.o   




The function "openOrCloseFile" is working fine when used in the application which runs in the user space but when used in the module, it is not working. I'll appreciate If some body tells me what I'm doing wrong, why the modue is not working in the way it suppose to.

Regards.
Soni.
Please do not open M$Windoze.
soni
Naik
 
Posts: 70
Joined: Sat Oct 04, 2003 1:44 pm
Website: http://www.cyberian.pk
Location: Karachi

Postby hameedkhan » Mon Oct 18, 2004 6:05 am

Hi,
soni, looks like you are involved in some cool project. But there is some thing missing. I am not a c programmer yet. But if I were you i had asked this question in lkml. Good Luck!
Thanks,
Hameed U. Khan
Linux User #: 354374
hameedkhan
Havaldaar
 
Posts: 141
Joined: Wed Apr 07, 2004 12:58 am
Location: Karachi, Pakistan

Postby Executive » Mon Oct 18, 2004 7:29 pm

Dear,

There is a big difference between user space file access and kernel space file access. I think that you cannot access the regular files that much easily from linux kernel. For reading and writing to the files from kernel there is a concept of Devices. Some are character and some are block. The regular files are accesing by block devices implementation. I mean the similar way linux kernel reads and write in /proc files.

Therefore, I would strongly recommend you to go through the meterial present on internet to make a clear understanding.

I achieved the same goal by implementing Character devices. I will be able to help you if you explain me what would you like to do.

For your help, I recommend you to visit following weblinks.
http://www.linuxforum.com/linux-kernel-programming/index.html
http://www.beesknees.freeserve.co.uk/articles/linux.html
http://www.cs.utexas.edu/users/ygz/378-03S/

BEST OF LUCK.

Raza
Syed Aoun Raza. M.Sc.(IT)
--------------------------------------
Experience is a hard teacher she gives the test first and lesson afterwords.
Executive
Lance Naik
 
Posts: 43
Joined: Mon Aug 09, 2004 2:45 pm
WLM: os_raza@hotmail.com
Yahoo Messenger: os_raza@yahoo.com

Postby soni » Tue Oct 19, 2004 2:23 am

Sallams....

The code which I posted in my query was a part of the character device.
The reason I posted this query to the forums like this was to find out if it was
possible or not.

The idea was to keep the data, which is static(in nature) like
ip-numbers, port numbers, mac-numbers etc on the disk instead of some array
or arrays of fixed sizes, the solutions which are advised like the using /proc
fs and the C/B devices, keep or place the data in the memory and not on the
disk.

I'm always asked to go for the simpler solution like either hard-coding the
data or using ioctl to give the data to the device, thus incresing the
devices memory foot-print and which is not only bad and painful to see but
also a pure waste of resources.... I think.

Anyways Thankyou Executive and hameedkhan for your time and replies.

Regards,
Soni.
Please do not open M$Windoze.
soni
Naik
 
Posts: 70
Joined: Sat Oct 04, 2003 1:44 pm
Website: http://www.cyberian.pk
Location: Karachi

Postby zeenix » Fri Nov 26, 2004 4:38 pm

Hello,
Sorry for the late reply. You'll always be faced with (time) efficiency vs. space (memory) concideration when you are coding. AFAIK, the idea of having all IPs (and stuff) in memory is a lot better idea than refering to the hard-disk for each packet for example. What you can do is (which is what i do) break your code into two: user-space app and kernel module. The kernel module is told to add/remove stuff from the app when needed using ioctls. Do you think linux kernel developers are not intelligent enough to realize these problems as all such info (IP address and routing tables) are always kept in memory and not HD?
"I think therefore I am" --- Rene Descartes
zeenix
Naik
 
Posts: 67
Joined: Wed Apr 23, 2003 4:01 am
WLM: appibrother@hotmail.com
Yahoo Messenger: zeenix@gmail.com
Location: Finland


Return to “%s” Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron