2002-12-07 20:48:15

by Peter Benie

[permalink] [raw]
Subject: [PATCH] [2.2] pipe_write can block even with non-blocking fd

Please apply the following patch. This makes non-blocking writes to
pipe actually non-blocking.

A program that demonstrates the bug can be found at:
http://www.chiark.greenend.org.uk/~peterb/linux/nonblock/pipe-testnonblock.c

--- 2.2.19/fs/pipe.c.orig Thu Dec 6 21:20:38 2001
+++ 2.2.19/fs/pipe.c Thu Dec 6 21:22:47 2001
@@ -105,9 +105,17 @@
else
free = 1; /* can't do it atomically, wait for any free space */
up(&inode->i_sem);
- if (down_interruptible(&inode->i_atomic_write)) {
- down(&inode->i_sem);
- return -ERESTARTSYS;
+ if (filp->f_flags & O_NONBLOCK) {
+ if (down_trylock(&inode->i_atomic_write)) {
+ down(&inode->i_sem);
+ return -ERESTARTSYS;
+ }
+ }
+ else {
+ if (down_interruptible(&inode->i_atomic_write)) {
+ down(&inode->i_sem);
+ return -ERESTARTSYS;
+ }
}
while (count>0) {
while ((PIPE_FREE(*inode) < free) || PIPE_LOCK(*inode)) {