[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to read file in kernel module?
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 . I didnt exactly mean only two , In my previous mail u can
see i reffered the other way to read and write is by using internal kernal
functions .
----- 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/