4 * written by Paul H. Hargrove
7 * 10-06-1999, AV: fixed OOM handling in fifo_open(), moved
8 * initialization there, switched to external
9 * allocation of pipe_inode_info.
14 #include <linux/sched.h>
15 #include <linux/pipe_fs_i.h>
17 static void wait_for_partner(struct inode* inode, unsigned int *cnt)
22 pipe_wait(inode->i_pipe);
23 if (signal_pending(current))
28 static void wake_up_partner(struct inode* inode)
30 wake_up_interruptible(&inode->i_pipe->wait);
33 static int fifo_open(struct inode *inode, struct file *filp)
35 struct pipe_inode_info *pipe;
38 mutex_lock(&inode->i_mutex);
42 pipe = alloc_pipe_info(inode);
49 /* We can only do regular read/write on fifos */
50 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
52 switch (filp->f_mode) {
56 * POSIX.1 says that O_NONBLOCK means return with the FIFO
57 * opened, even when there is no process writing the FIFO.
59 filp->f_op = &read_pipefifo_fops;
61 if (pipe->readers++ == 0)
62 wake_up_partner(inode);
65 if ((filp->f_flags & O_NONBLOCK)) {
66 /* suppress POLLHUP until we have
68 filp->f_version = pipe->w_counter;
70 wait_for_partner(inode, &pipe->w_counter);
71 if(signal_pending(current))
80 * POSIX.1 says that O_NONBLOCK means return -1 with
81 * errno=ENXIO when there is no process reading the FIFO.
84 if ((filp->f_flags & O_NONBLOCK) && !pipe->readers)
87 filp->f_op = &write_pipefifo_fops;
90 wake_up_partner(inode);
93 wait_for_partner(inode, &pipe->r_counter);
94 if (signal_pending(current))
99 case FMODE_READ | FMODE_WRITE:
102 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
103 * This implementation will NEVER block on a O_RDWR open, since
104 * the process can at least talk to itself.
106 filp->f_op = &rdwr_pipefifo_fops;
112 if (pipe->readers == 1 || pipe->writers == 1)
113 wake_up_partner(inode);
122 mutex_unlock(&inode->i_mutex);
126 if (!--pipe->readers)
127 wake_up_interruptible(&pipe->wait);
132 if (!--pipe->writers)
133 wake_up_interruptible(&pipe->wait);
138 if (!pipe->readers && !pipe->writers)
139 free_pipe_info(inode);
142 mutex_unlock(&inode->i_mutex);
147 * Dummy default file-operations: the only thing this does
148 * is contain the open that then fills in the correct operations
149 * depending on the access mode of the file...
151 const struct file_operations def_fifo_fops = {
152 .open = fifo_open, /* will set read_ or write_pipefifo_fops */
153 .llseek = noop_llseek,