Re: How to read file in kernel module?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



--- Shyamjith <shyamjithe@xxxxxxxxxxxxxxx> wrote:

> I know that this will work with 2.4.X kernel (not
> sure with 2.6). But
> usually its very dangerous to use  this . because
> there is no proper locking
> mechanism while wrting to a file (i suppose ) . This
> may lead to
> inconsistancy . 


do u mean that writing to a file in kernel, using
sys_write function can lead to inconsistency, i dont
think so. Its the top most function in system call
write and being the top most function it take care of
all things for file operation in kernel, so i think we
need not to worry about locking and unlocking of a
file.

When any application makes a system call write, this
function (sys_write) is dispacthed forom
sys_call_table to do all the write functionality.

I think its the best way to do file operations in
kernel by using top most function in kernel related to
file operations (like sys_open, sys_close, sys_read
.... etc)

All the top most functions in kernel which implements
the system calls start with "sys_" string in front. I
tried using sys_kill in kernel mode to send signal to
some process and it works fine.

Yes definitely this puts some overhead, as we call the
top most function.

--gd



> 
> 
> ----- Original Message -----
> From: "Amith" <amith@xxxxxxxxxxxxxxx>
> To: "shyamjith" <shyamjithe@xxxxxxxxxxxxxxx>
> Cc: <kernelnewbies@xxxxxxxxxxxx>; "Jan Hudec"
> <bulb@xxxxxx>
> Sent: Wednesday, February 02, 2005 2:58 PM
> Subject: Re: How to read file in kernel module?
> 
> 
> > shyamjith wrote:
> >
> > > When u register a device with the kernel for
> example, a character device
> > > U attach the device structure into the device
> table . which is filled
> > > with
> > > pointers to our read and write functions .while
> registering a proc
> > > entry  also we can fill such functional pointers
> .
> > >
> > > up  up and away
> > >
> > > shyam
> >
> > ok , so u mean the file_operat ions  table.
> >
> >
> > >
> > >See i think we are talking about kernel
> programming . What i mean to say
> is
> > >that there is only two ways to  communiacte to
> user space . One through a
> > >registerd read or write functions & other one
> through proc file system  .
> > >
> >
> > Well, what about the code below ? i know it's not
> an accepted way of doing
> it , but it sure beats your
> > *only* 2 ways of doing it :-) !
> >
> > < kfs.c >
> > int
> > kopen(const char *filename,int flags)
> > {
> >   oldmm = get_fs();
> >   set_fs(get_ds());
> >   kfd = sys_open(filename,flags |
> O_CREAT,S_IRWXU);
> >   if(kfd > 0) {
> >   printk("<1>kfd = %d \n",kfd);
> >   set_fs(oldmm);
> >   return kfd;
> >   }
> >   return kfd;
> > }
> >
> >
> > int
> > kwrite(int fd,char *buffer,int size)
> > {
> >   int red;
> >   oldmm = get_fs();
> >   set_fs(get_ds());
> >   red = sys_write(fd,write_buffer,size);
> >   if(red > 0) {
> >   printk("<1>write buffer  = %s \n",write_buffer);
> >   set_fs(oldmm);
> >   return red;
> >   }
> > }
> >
> >
> > i had written this code sometime ago and it did
> work !
> >
> > cheers,
> > Amith
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> 
> 
> --
> Kernelnewbies: Help each other learn about the Linux
> kernel.
> Archive:      
> http://mail.nl.linux.org/kernelnewbies/
> FAQ:           http://kernelnewbies.org/faq/
> 
> 


=====
-------------------------------------------
Subscribe LKG_INDIA Group :
Its Linux Kernel oriented group, started by bunch of guys from India.
This group discusses the technical details of Linux Kernel, helping the members to share and enhance their knowledge on Linux Kernel and general OS concepts.
Be a member of it if you are Linux Kernel Geek.
For joining this group, mail to gauravd_chd@xxxxxxxxx
Visit the LKG_INDIA home page: http://groups.yahoo.com/group/lkg_india/
-------------------------------------------


	
		
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - You care about security. So do we. 
http://promotions.yahoo.com/new_mail

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux