mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Richard B. Johnson" <root@chaos.analogic.com>
To: Libor Vanek <libor@conet.cz>
Cc: linux-kernel@vger.kernel.org
Subject: Re: Read from file fails
Date: Mon, 3 May 2004 09:11:14 -0400 (EDT)	[thread overview]
Message-ID: <Pine.LNX.4.53.0405030852220.10896@chaos> (raw)
In-Reply-To: <20040503000004.GA26707@Loki>

On Mon, 3 May 2004, Libor Vanek wrote:

> Hi,
> can anybody help me with reading from file? I've got this code in my module for 2.6.5-mm6:
>
> char buffer[4096];
> ssize_t read;
> file *f;
> f = filp_open("/some/file",O_RDONLY | O_LARGEFILE,0);
> f->f_pos = 0;
> read = vfs_read(f,(char __user *) buffer,4096,&f->f_pos);
>
>
> but here read value is "-14" :-((( any hints?
>

Sure it's -EFAULT. In user-space, you would get a core-dump.


> It seems that file is opened OK (I've tested:
> if (f->f_op->read) {
> 	read = f->f_op->read(file, buf, count, pos);
> }
> and result was the same - so I assume that file is opened OK and
> structure "file f" is filled correctly.

This has become a FAQ and NO If you know what you are doing, you
NEVER need to read files from within the kernel. PERIOD. There are
Hacks that __sometimes__ allow code to get away with it, but
they are only going to work --- sometimes ---.

>
> I need to copy files (yes - I know that kernel shouldn't do this but I REALLY need) and  there is nothing like "sys_copy" and "sys_sendfile" is not exported (which seems strange to me but there is nothing like EXPORT_SYMBOL(sys_sendfile) in fs/read_write.c
>

I will explain for the Nth time. The kernel is a bunch of code
that performs functions on behalf of a process. It does that in
the context of that process. A file descriptor is a number that
belongs to a process. It is worthless without a process with which
to associate. If the kernel were to open a file, who owns the
file descriptor? The kernel isn't a process, it's a big LIBRARY!
So, if you are lucky, you can steal the context from another
unknowing process. This works unless there was a context-switch or
if the process was asleep so it didn't access any files of
its own. It leaves the kernel in a bad state because there
is a wild file-descriptor that nobody owns that will never be
closed because when your cludge closes that file-descriptor, you
probably end up closing STDOUT_FILENO of some important task
that you haven't discovered yet.

The cludges that work involve creating a "kernel thread" which
has a process context. That thread does the file I/O. However,
it's certainly easier to create a user-mode task (program) that
opens your module and, using an ioctl(), sends it anything from
anywhere in the known universe. You can get data from a file or
from anywhere.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.26 on an i686 machine (5557.45 BogoMips).
            Note 96.31% of all statistics are fiction.



  reply	other threads:[~2004-05-03 13:09 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-03  0:00 Libor Vanek
2004-05-03 13:11 ` Richard B. Johnson [this message]
2004-05-03 15:06   ` Libor Vanek
2004-05-04  0:47     ` Richard B. Johnson
2004-05-04  1:19       ` Libor Vanek
2004-05-04 13:49         ` Richard B. Johnson
2004-05-05 10:34           ` Libor Vanek
2004-05-04 14:31         ` Bart Samwel
2004-05-05  9:54           ` Libor Vanek
2004-05-05 10:04             ` Bart Samwel
2004-05-05 10:19               ` Libor Vanek
2004-05-05 10:45                 ` Bart Samwel
2004-05-05 11:22                   ` Libor Vanek
2004-05-05 11:50                     ` Bart Samwel
2004-05-05 10:54                 ` Denis Vlasenko
2004-05-05 11:58                   ` Michael Clark
2004-05-04 18:45         ` Paulo Marques
2004-05-05  9:47           ` Libor Vanek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.53.0405030852220.10896@chaos \
    --to=root@chaos.analogic.com \
    --cc=libor@conet.cz \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®