Files within the kernel <FAQ>

Richard B. Johnson (root@chaos.analogic.com)
Tue, 26 Oct 1999 13:36:17 -0400 (EDT)


Hello!

Every day or two somebody is trying to access files from within the
kernel. To access files, you need a process context. This is because a
file-descriptor means nothing to the kernel without some task to associate
it with.

For example, you have a process on one virtual terminal and another
on another terminal. Both processes have files 0, 1, and 2 open. How
does the kernel 'know' that identical file numbers do not refer to
the same thing? Easy, it knows the pid.

Now, in the kernel, kernel services are performed on behalf of
a calling process. That process is called 'current' If your
driver attempted to open a file, the returned file-descriptor
would refer to the current process. By the time you actually
read or wrote to the file, the current process probably changed.
Now you are accessing an invalid file-descriptor, probably someone
else's file.

That said, the only way you can sucessfully access files from within
the kernel is to start a 'kernel thread'. You can look at
../linux/init/main.c (line 1284 or so) to get some ideas.

Kernel threads are not easy and it's not going to be an easy way
to access files from within the kernel. This is why files are
usually accessed outside the kernel by a daemon when some kernel
event occurs (check how modprobe works).

Cheers,
Dick Johnson

Penguin : Linux version 2.3.13 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/