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.
13 #include <linux/slab.h>
15 #include <linux/sched.h>
16 #include <linux/pipe_fs_i.h>
18 static void wait_for_partner(struct inode* inode, unsigned int *cnt)
23 pipe_wait(inode->i_pipe);
24 if (signal_pending(current))
29 static void wake_up_partner(struct inode* inode)
31 wake_up_interruptible(&inode->i_pipe->wait);
34 static int fifo_open(struct inode *inode, struct file *filp)
36 struct pipe_inode_info *pipe;
39 mutex_lock(&inode->i_mutex);
43 pipe = alloc_pipe_info(inode);
50 /* We can only do regular read/write on fifos */
51 filp->f_mode &= (FMODE_READ | FMODE_WRITE);
53 switch (filp->f_mode) {
57 * POSIX.1 says that O_NONBLOCK means return with the FIFO
58 * opened, even when there is no process writing the FIFO.
60 filp->f_op = &read_fifo_fops;
62 if (pipe->readers++ == 0)
63 wake_up_partner(inode);
66 if ((filp->f_flags & O_NONBLOCK)) {
67 /* suppress POLLHUP until we have
69 filp->f_version = pipe->w_counter;
72 wait_for_partner(inode, &pipe->w_counter);
73 if(signal_pending(current))
82 * POSIX.1 says that O_NONBLOCK means return -1 with
83 * errno=ENXIO when there is no process reading the FIFO.
86 if ((filp->f_flags & O_NONBLOCK) && !pipe->readers)
89 filp->f_op = &write_fifo_fops;
92 wake_up_partner(inode);
95 wait_for_partner(inode, &pipe->r_counter);
96 if (signal_pending(current))
104 * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
105 * This implementation will NEVER block on a O_RDWR open, since
106 * the process can at least talk to itself.
108 filp->f_op = &rdwr_fifo_fops;
114 if (pipe->readers == 1 || pipe->writers == 1)
115 wake_up_partner(inode);
124 mutex_unlock(&inode->i_mutex);
128 if (!--pipe->readers)
129 wake_up_interruptible(&pipe->wait);
134 if (!--pipe->writers)
135 wake_up_interruptible(&pipe->wait);
140 if (!pipe->readers && !pipe->writers)
141 free_pipe_info(inode);
144 mutex_unlock(&inode->i_mutex);
149 * Dummy default file-operations: the only thing this does
150 * is contain the open that then fills in the correct operations
151 * depending on the access mode of the file...
153 const struct file_operations def_fifo_fops = {
154 .open = fifo_open, /* will set read or write pipe_fops */