2002-04-13 13:48:37

by Anthony Chee

[permalink] [raw]
Subject: read proc entry

I written the following code in a module

static struct proc_dir_entry *test_proc;
test_proc = create_proc_read_entry(test_proc, 0444, NULL, read_test_proc,
NULL);

void show_kernel_message() {
printk("\nkernel test\n");
}

int read_test_info(char* page, char** start, off_t off, int count, int* eof,
void* data) {
show_kernel_message();
}

After I use "cat /proc/test_proc", it is found that there are three "kernel
test" messages
appear. Why it happened like this? I expected the message should be shown
once.


2002-04-13 13:59:02

by Peter Wächtler

[permalink] [raw]
Subject: Re: read proc entry

Anthony Chee wrote:
>
> I written the following code in a module
>
> static struct proc_dir_entry *test_proc;
> test_proc = create_proc_read_entry(test_proc, 0444, NULL, read_test_proc,
> NULL);
>
> void show_kernel_message() {
> printk("\nkernel test\n");
> }
>
> int read_test_info(char* page, char** start, off_t off, int count, int* eof,
> void* data) {
> show_kernel_message();

I think you have to signal EOF

*eof=1;

> }
>
> After I use "cat /proc/test_proc", it is found that there are three "kernel
> test" messages
> appear. Why it happened like this? I expected the message should be shown
> once.
>

2002-04-13 14:07:53

by Anthony Chee

[permalink] [raw]
Subject: Re: read proc entry

After I added *eof =1, that message appears two times. How can I reduce it
only one?

----- Original Message -----
From: "Peter W?chtler" <[email protected]>
To: "Anthony Chee" <[email protected]>
Cc: <[email protected]>
Sent: Saturday, April 13, 2002 9:58 PM
Subject: Re: read proc entry


> Anthony Chee wrote:
> >
> > I written the following code in a module
> >
> > static struct proc_dir_entry *test_proc;
> > test_proc = create_proc_read_entry(test_proc, 0444, NULL,
read_test_proc,
> > NULL);
> >
> > void show_kernel_message() {
> > printk("\nkernel test\n");
> > }
> >
> > int read_test_info(char* page, char** start, off_t off, int count, int*
eof,
> > void* data) {
> > show_kernel_message();
>
> I think you have to signal EOF
>
> *eof=1;
>
> > }
> >
> > After I use "cat /proc/test_proc", it is found that there are three
"kernel
> > test" messages
> > appear. Why it happened like this? I expected the message should be
shown
> > once.
> >
>

2002-04-13 14:22:23

by James Washer

[permalink] [raw]
Subject: Re: read proc entry


First, I think you miss typed the mail you sent the group... in your call
to create_proc_read_entry, you passed "read_test_proc" as the name of the
function, yet you've declared here a function named "read_test_info"...

However, in answer to your question.

Since, in your routine, you've done NOTHING to tell the kernel that your
routine is completed transfering data to *page, the kernel will call it
again looking for more data to be transferred.


The sad thing is, the way things work, nearly all proc read functions will
be called twice, even when they signal they are done using *eof=1;
Why? Because your userland processes, in this case 'cat', will make
additional calls to read(2) UNTIL it gets zero bytes signalling
end-of-file.

A well written proc read function CAN recognize this subsequent read, and
return early, but nearly every proc read shipping with the kernel today
does NOT. They simply place the same information in the buffer at *page a
2nd time and return the same number of bytes written. However, since the
function proc_file_read(), which calls your proc read function, checks for
data BEYOND the old data, proc_file_read will return 0 upstream to the user
on this 2nd attempt.

It's a bit confusing at first.. but take a good long look at proc_file_read
().

- jim

"Anthony Chee" <[email protected]>@vger.kernel.org on 04/13/2002
06:48:31 AM

Sent by: [email protected]


To: <[email protected]>
cc:
Subject: read proc entry



I written the following code in a module

static struct proc_dir_entry *test_proc;
test_proc = create_proc_read_entry(test_proc, 0444, NULL, read_test_proc,
NULL);

void show_kernel_message() {
printk("\nkernel test\n");
}

int read_test_info(char* page, char** start, off_t off, int count, int*
eof,
void* data) {
show_kernel_message();
}

After I use "cat /proc/test_proc", it is found that there are three "kernel
test" messages
appear. Why it happened like this? I expected the message should be shown
once.

2002-04-18 18:52:47

by Randy.Dunlap

[permalink] [raw]
Subject: Re: read proc entry

Hi,

Another thing that you could do is use the seq_file
interface for proc-fs. That should do away with
the EOF/double-read problem.

seq_file is available in kernel 2.4.15 and later.

~Randy

On Sat, 13 Apr 2002, Anthony Chee wrote:

| After I added *eof =1, that message appears two times. How can I reduce it
| only one?
|
| ----- Original Message -----
| From: "Peter W?chtler" <[email protected]>
| To: "Anthony Chee" <[email protected]>
| Cc: <[email protected]>
| Sent: Saturday, April 13, 2002 9:58 PM
| Subject: Re: read proc entry
|
|
| > Anthony Chee wrote:
| > >
| > > I written the following code in a module
| > >
| > > static struct proc_dir_entry *test_proc;
| > > test_proc = create_proc_read_entry(test_proc, 0444, NULL,
| read_test_proc,
| > > NULL);
| > >
| > > void show_kernel_message() {
| > > printk("\nkernel test\n");
| > > }
| > >
| > > int read_test_info(char* page, char** start, off_t off, int count, int*
| eof,
| > > void* data) {
| > > show_kernel_message();
| >
| > I think you have to signal EOF
| >
| > *eof=1;
| >
| > > }
| > >
| > > After I use "cat /proc/test_proc", it is found that there are three
| "kernel
| > > test" messages
| > > appear. Why it happened like this? I expected the message should be
| shown
| > > once.
| > >
| >
| -