Re: Bug or feature? No EOF on /proc/xxx/fd/x

Alain Knaff (Alain.Knaff@imag.fr)
Thu, 15 Feb 1996 23:53:23 +0100


The following patch fixes the date | cat /proc/self/fd/0 problem.
What happened was that when cat opened /proc/self/fd/0 as read-only,
the kernel nevertheless tallied it as a writer to the pipe. Thus it
thought that there was still a writer to the pipe, even after date
closed its end of the pipe.

There's also a more fundamental problem lurking around (not fixed):
/proc/*/fd file descriptors only keep the properties of the inode
(including the fops), not those of the file pointer... Thus, it is
still possible to do 'date | cat >/proc/self/fd/0'. This should
really fail, as in this case /proc/self/fd/0 is the read end of the
pipe.

Alain

--- linux/fs/pipe.c.ori Thu Feb 15 23:33:21 1996
+++ linux/fs/pipe.c Thu Feb 15 23:34:42 1996
@@ -271,10 +271,27 @@
return 0;
}

+extern struct file_operations read_pipe_fops;
+extern struct file_operations write_pipe_fops;
+extern struct file_operations rdwe_pipe_fops;
+
static int pipe_rdwr_open(struct inode * inode, struct file * filp)
{
- PIPE_READERS(*inode)++;
- PIPE_WRITERS(*inode)++;
+ switch(filp->f_mode & 3) {
+ case 1: /* read-only open */
+ PIPE_READERS(*inode)++;
+ filp->f_op = &read_pipe_fops;
+ break;
+ case 2: /* write-only open */
+ PIPE_WRITERS(*inode)++;
+ filp->f_op = &write_pipe_fops;
+ break;
+ default:
+ PIPE_READERS(*inode)++;
+ PIPE_WRITERS(*inode)++;
+ filp->f_op = &rdwr_pipe_fops;
+ break;
+ }
return 0;
}