Re: File I/O help(newby)

Richard B. Johnson (root@chaos.analogic.com)
Wed, 14 Jul 1999 08:42:52 -0400 (EDT)


On Tue, 13 Jul 1999, MICHAEL HARRISON wrote:

>
> I haven't joined the list yet (chaning email accounts) and am
> somewhat of a newby when it comes to kernel programming. I have a fair
> amount of unix programming experiance, though.
>

> I am working with a research group that is studying security issues. I
> have searched the archives and can't seem to find an answer to my
> question. Its pretty simple and straight forward I think. How do I open
> a file, write to it, and then close it. The following is linked into the
> kernel and called several times. The file gets opened erratically and
> with many errors. Again I am not on the list yet so CC me if you want to
> hepl.

[SNIPPED]

The problem is that the kernel is not a 'process'. For a file descriptor
to mean anything, it has to be associated with a process. Basically, in
UNIX, when a user wants the kernel to do something, i.e., write to
a file, the kernel does this in the context of the current process.

Some procedure, linked into the kernel, that needs to use a file-
descriptor, would end up creating the file in the context of whatever
process currently was running, then when the write actually occurs either
to buffers or the physical device, the task may 'sleep' while this is
done. Sleep, in this meaning, means to give the CPU to some other task
while hardware or even the memory manager (paging, swapping, etc.) is
busy.

After control is returned, the state of the kernel will have changed.
The result is undefined.

You can use a 'kernel thread' to establish process context in the kernel.
However, it is easier to write an installable driver that saves all
the kernel information that you intend to acquire in a buffer it
allocates/deallocates in the kernel.

A user-mode program (daemon) periodically queries the driver for its
data. You do all your file I/O in the context of a user.

Cheers,
Dick Johnson
FILE SYSTEM MODIFIED
Penguin : Linux version 2.2.6 on an i686 machine (400.59 BogoMips).
Warning : It's hard to remain at the trailing edge of technology.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/