aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorAnders Kaseorg <andersk@MIT.EDU>2012-07-15 17:14:25 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-07-16 11:33:14 -0400
commit05d290d66be6ef77a0b962ebecf01911bd984a78 (patch)
treef220481b656c39cabd96c20dab8d58b49cd28a17 /fs
parent1694a0a08e85baade75752a6a902a2fa30d608e8 (diff)
fifo: Do not restart open() if it already found a partner
If a parent and child process open the two ends of a fifo, and the child immediately exits, the parent may receive a SIGCHLD before its open() returns. In that case, we need to make sure that open() will return successfully after the SIGCHLD handler returns, instead of throwing EINTR or being restarted. Otherwise, the restarted open() would incorrectly wait for a second partner on the other end. The following test demonstrates the EINTR that was wrongly thrown from the parent’s open(). Change .sa_flags = 0 to .sa_flags = SA_RESTART to see a deadlock instead, in which the restarted open() waits for a second reader that will never come. (On my systems, this happens pretty reliably within about 5 to 500 iterations. Others report that it manages to loop ~forever sometimes; YMMV.) #include <sys/stat.h> #include <sys/types.h> #include <sys/wait.h> #include <fcntl.h> #include <signal.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #define CHECK(x) do if ((x) == -1) {perror(#x); abort();} while(0) void handler(int signum) {} int main() { struct sigaction act = {.sa_handler = handler, .sa_flags = 0}; CHECK(sigaction(SIGCHLD, &act, NULL)); CHECK(mknod("fifo", S_IFIFO | S_IRWXU, 0)); for (;;) { int fd; pid_t pid; putc('.', stderr); CHECK(pid = fork()); if (pid == 0) { CHECK(fd = open("fifo", O_RDONLY)); _exit(0); } CHECK(fd = open("fifo", O_WRONLY)); CHECK(close(fd)); CHECK(waitpid(pid, NULL, 0)); } } This is what I suspect was causing the Git test suite to fail in t9010-svn-fe.sh: http://bugs.debian.org/678852 Signed-off-by: Anders Kaseorg <andersk@mit.edu> Reviewed-by: Jonathan Nieder <jrnieder@gmail.com> Cc: stable@kernel.org Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'fs')
-rw-r--r--fs/fifo.c9
1 files changed, 4 insertions, 5 deletions
diff --git a/fs/fifo.c b/fs/fifo.c
index b1a524d798e7..cf6f4345ceb0 100644
--- a/fs/fifo.c
+++ b/fs/fifo.c
@@ -14,7 +14,7 @@
14#include <linux/sched.h> 14#include <linux/sched.h>
15#include <linux/pipe_fs_i.h> 15#include <linux/pipe_fs_i.h>
16 16
17static void wait_for_partner(struct inode* inode, unsigned int *cnt) 17static int wait_for_partner(struct inode* inode, unsigned int *cnt)
18{ 18{
19 int cur = *cnt; 19 int cur = *cnt;
20 20
@@ -23,6 +23,7 @@ static void wait_for_partner(struct inode* inode, unsigned int *cnt)
23 if (signal_pending(current)) 23 if (signal_pending(current))
24 break; 24 break;
25 } 25 }
26 return cur == *cnt ? -ERESTARTSYS : 0;
26} 27}
27 28
28static void wake_up_partner(struct inode* inode) 29static void wake_up_partner(struct inode* inode)
@@ -67,8 +68,7 @@ static int fifo_open(struct inode *inode, struct file *filp)
67 * seen a writer */ 68 * seen a writer */
68 filp->f_version = pipe->w_counter; 69 filp->f_version = pipe->w_counter;
69 } else { 70 } else {
70 wait_for_partner(inode, &pipe->w_counter); 71 if (wait_for_partner(inode, &pipe->w_counter))
71 if(signal_pending(current))
72 goto err_rd; 72 goto err_rd;
73 } 73 }
74 } 74 }
@@ -90,8 +90,7 @@ static int fifo_open(struct inode *inode, struct file *filp)
90 wake_up_partner(inode); 90 wake_up_partner(inode);
91 91
92 if (!pipe->readers) { 92 if (!pipe->readers) {
93 wait_for_partner(inode, &pipe->r_counter); 93 if (wait_for_partner(inode, &pipe->r_counter))
94 if (signal_pending(current))
95 goto err_wr; 94 goto err_wr;
96 } 95 }
97 break; 96 break;