[PATCH 4 OF 5]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

This patch implements the behavior of open() and the flag O_NONBLOCK
when opening named pipes.

Moved release of pipe buffers from iput() to pipe_release(). Now,
fs/inode.c knows even less about pipes.

When possible, declared variables and functions in fs/pipe.c as static
and removed their prototypes from fs.h

Pipes can be tested with the following command:

# ls -l /bin | more

Named pipes can be checked with:

# mknod fifo p
# cat /etc/passwd > fifo &
# cat fifo

When you hit enter in the last command, you will see the contents of
file /etc/passwd on screen and then the message of the first 'cat'
exiting. Finally, remove the fifo:

# rm fifo

Code size was increased by 144 bytes.
diff -Nur elks.orig/fs/inode.c elks/fs/inode.c
--- elks.orig/fs/inode.c	2015-03-12 12:24:04.000000000 -0600
+++ elks/fs/inode.c	2015-03-12 12:25:35.000000000 -0600
@@ -359,11 +359,6 @@
 	    }
 
 	    wake_up(&inode_wait);
-	    if (((inode->i_mode & S_IFMT) == S_IFIFO) && inode->u.pipe_i.base) {
-	    /* Free up any memory allocated to the pipe */
-		free_pipe_mem(inode->u.pipe_i.base);
-		inode->u.pipe_i.base = NULL;
-	    }
 
 	    if (inode->i_sb) {
 		struct super_operations *sop = inode->i_sb->s_op;
diff -Nur elks.orig/fs/pipe.c elks/fs/pipe.c
--- elks.orig/fs/pipe.c	2015-03-12 12:24:04.000000000 -0600
+++ elks/fs/pipe.c	2015-03-12 12:33:51.000000000 -0600
@@ -59,11 +59,11 @@
  *	V7, and they should be buffers
  */
 
-char pipe_base[MAX_PIPES][PIPE_BUF];
+static char pipe_base[MAX_PIPES][PIPE_BUF];
 
-int pipe_in_use[(MAX_PIPES + 15)/16];
+static int pipe_in_use[(MAX_PIPES + 15)/16];
 
-char *get_pipe_mem(void)
+static char *get_pipe_mem(void)
 {
     int i = 0;
 
@@ -77,7 +77,7 @@
     return NULL;
 }
 
-void free_pipe_mem(char *buf)
+static void free_pipe_mem(char *buf)
 {
     int i;
 
@@ -211,7 +211,15 @@
     if (filp->f_mode & FMODE_WRITE)
 	(inode->u.pipe_i.writers)--;
 
-    wake_up_interruptible(&(inode->u.pipe_i.wait));
+    if(!(inode->u.pipe_i.readers + inode->u.pipe_i.writers)) {
+	if(inode->u.pipe_i.base) {
+	/* Free up any memory allocated to the pipe */
+	    free_pipe_mem(inode->u.pipe_i.base);
+	    inode->u.pipe_i.base = NULL;
+	}
+    }
+    else
+	wake_up_interruptible(&(inode->u.pipe_i.wait));
 }
 
 #ifdef STRICT_PIPES
@@ -237,12 +245,6 @@
 {
     debug("PIPE: rdwr called.\n");
 
-    if (filp->f_mode & FMODE_READ)
-	(inode->u.pipe_i.readers)++;
-
-    if (filp->f_mode & FMODE_WRITE)
-	(inode->u.pipe_i.writers)++;
-
     if(!PIPE_BASE(*inode)) {
 	if(!(PIPE_BASE(*inode) = get_pipe_mem()))
 	    return -ENOMEM;
@@ -254,6 +256,32 @@
 	PIPE_LOCK(*inode) = 0;
 #endif
     }
+    if (filp->f_mode & FMODE_READ) {
+	(inode->u.pipe_i.readers)++;
+	if(inode->u.pipe_i.writers > 0) {
+	    if(inode->u.pipe_i.readers < 2)
+		wake_up_interruptible(&(inode->u.pipe_i.wait));
+	}
+	else {
+	    if(!(filp->f_flags & O_NONBLOCK) && (inode->i_sb))
+		while(!(inode->u.pipe_i.writers))
+		    interruptible_sleep_on(&(inode->u.pipe_i.wait));
+	}
+    }
+
+    if (filp->f_mode & FMODE_WRITE) {
+	(inode->u.pipe_i.writers)++;
+	if(inode->u.pipe_i.readers > 0) {
+	    if(inode->u.pipe_i.writers < 2)
+		wake_up_interruptible(&(inode->u.pipe_i.wait));
+	}
+	else {
+	    if(filp->f_flags & O_NONBLOCK)
+		return -ENXIO;
+	    while(!(inode->u.pipe_i.readers))
+		interruptible_sleep_on(&(inode->u.pipe_i.wait));
+	}
+    }
     return 0;
 }
 
@@ -320,7 +348,7 @@
 
 /*@+type@*/
 
-int do_pipe(int *fd)
+static int do_pipe(int *fd)
 {
     register struct inode *inode;
     struct file *f1;
diff -Nur elks.orig/include/linuxmt/fs.h elks/include/linuxmt/fs.h
--- elks.orig/include/linuxmt/fs.h	2015-03-12 12:24:04.000000000 -0600
+++ elks/include/linuxmt/fs.h	2015-03-12 12:34:09.000000000 -0600
@@ -453,7 +453,6 @@
 
 extern int open_namei(char *,int,int,struct inode **,struct inode *);
 extern int do_mknod(char *,int,dev_t);
-extern int do_pipe(int *);
 extern void iput(struct inode *);
 
 extern struct inode *get_empty_inode(void);
@@ -510,8 +509,6 @@
 extern struct buffer_head *bread(dev_t,block_t);
 
 extern int get_unused_fd(struct file *);
-extern char *get_pipe_mem(void);
-extern void free_pipe_mem(char *buf);
 
 extern void mark_buffer_uptodate(struct buffer_head *,int);
 

[Index of Archives]     [Kernel]     [Linux ia64]     [DCCP]     [Linux for ARM]     [Yosemite News]     [Linux SCSI]     [Linux Hams]

  Powered by Linux