Re: Opening files in kernel mode

From: Tigran Aivazian (tigran@veritas.com)
Date: Fri Jun 02 2000 - 05:04:51 EST


On Fri, 2 Jun 2000, S.Venkat Raman wrote:

> Hi,
> Does anyone know if there is a function to
> open ( and read )a file when working in kernel
> mode. Like for example there is a kernel
> equivalent of printf ( printk ) and malloc (kmalloc).
> Open is not there in <linux/fcntl.h> i am
> also looking for kernel version of read too
> which is also not there in <linux/unistd.h>.
>
> if i try and use <fcntl.h> and <unistd.h> there
> are some conflicts with the other files that
> i include as a result it doesnt compile.
>
> i am need the functions as part of a device driver
> in which i need to open a file and copy the
> contents into the device.
>
> Please CC a reply to me incase you have some
> solution to this problem.
>
> thanks in advance,
> venkat

look at how fs/exec.c:sys_uselib() opens/closes files in kernel mode. Note
that the 'library' parameter there is a userspace address so if you pass
kernel addresses you need to do the standard trick:

mm_segment_t old_fs;

old_fs = get_fs();
set_fs(get_ds());
... do the stuff ....
set_fs(old_fs);

Also, the way sys_uselib() resorts to using sys_open/sys_close is not
perfect because it uses a file descriptor where it doesn't really have
to. It could all be done by just using filp_open()/filp_close() if
manipulating 'struct file' is enough for your purposes.

Regards,
Tigran

-
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/



This archive was generated by hypermail 2b29 : Wed Jun 07 2000 - 21:00:14 EST