2003-01-04 16:06:28

by anil vijarnia

[permalink] [raw]
Subject: writing from kernel

can any one tell me how to write into files from kernel space.i
tried
sys_open(),sys_write() from my module but they don't work.

anil




2003-01-04 22:16:20

by Kasper Dupont

[permalink] [raw]
Subject: Re: writing from kernel

anil vijarnia wrote:
>
> can anyone tell me how to write into i file from kernel space.
> i tried sys_open,sys_write functions bbbbbut they don't work
> from my module.

That is rarely a good idea. But if you insist on doing it anyway,
you could use filp_open and the methods in the returned struct.
I have an old code example here:

http://www.daimi.au.dk/~kasperd/linux_kernel/kcp.c

--
Kasper Dupont -- der bruger for meget tid p? usenet.
For sending spam use mailto:[email protected]
for(_=52;_;(_%5)||(_/=5),(_%5)&&(_-=2))putchar(_);

2003-01-05 00:18:01

by Olaf Dietsche

[permalink] [raw]
Subject: Re: writing from kernel

"anil vijarnia" <[email protected]> writes:

> can anyone tell me how to write into i file from kernel space.
> i tried sys_open,sys_write functions bbbbbut they don't work
> from
> my module.

As others already pointed out, it's not always a good idea to do this
from kernel space. However, if you still want to do it, see snippet
below. If you want to write, you have to copy kernel_read() from
fs/exec.c and modify it for writing.

Regards, Olaf.

--cut here-->8--
#include <linux/fs.h>

struct file *filp;
unsigned long offset = 0;
char buf[1024];
int n;

filp = filp_open("/path/to/some/file", O_RDONLY, 0);
if (!filp || IS_ERR(filp)) {
/* do some error handling */
}

n = kernel_read(filp, offset, buf, sizeof(buf));
if (n != sizeof(buf)) {
/* do some checking */
}

filp_close(filp, 0);
--8<--cut here--