fifo.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * linux/fs/fifo.c
  3. *
  4. * written by Paul H. Hargrove
  5. *
  6. * Fixes:
  7. * 10-06-1999, AV: fixed OOM handling in fifo_open(), moved
  8. * initialization there, switched to external
  9. * allocation of pipe_inode_info.
  10. */
  11. #include <linux/mm.h>
  12. #include <linux/fs.h>
  13. #include <linux/sched.h>
  14. #include <linux/pipe_fs_i.h>
  15. static int wait_for_partner(struct inode* inode, unsigned int *cnt)
  16. {
  17. int cur = *cnt;
  18. while (cur == *cnt) {
  19. pipe_wait(inode->i_pipe);
  20. if (signal_pending(current))
  21. break;
  22. }
  23. return cur == *cnt ? -ERESTARTSYS : 0;
  24. }
  25. static void wake_up_partner(struct inode* inode)
  26. {
  27. wake_up_interruptible(&inode->i_pipe->wait);
  28. }
  29. static int fifo_open(struct inode *inode, struct file *filp)
  30. {
  31. struct pipe_inode_info *pipe;
  32. int ret;
  33. mutex_lock(&inode->i_mutex);
  34. pipe = inode->i_pipe;
  35. if (!pipe) {
  36. ret = -ENOMEM;
  37. pipe = alloc_pipe_info(inode);
  38. if (!pipe)
  39. goto err_nocleanup;
  40. inode->i_pipe = pipe;
  41. }
  42. filp->f_version = 0;
  43. /* We can only do regular read/write on fifos */
  44. filp->f_mode &= (FMODE_READ | FMODE_WRITE);
  45. switch (filp->f_mode) {
  46. case FMODE_READ:
  47. /*
  48. * O_RDONLY
  49. * POSIX.1 says that O_NONBLOCK means return with the FIFO
  50. * opened, even when there is no process writing the FIFO.
  51. */
  52. filp->f_op = &read_pipefifo_fops;
  53. pipe->r_counter++;
  54. if (pipe->readers++ == 0)
  55. wake_up_partner(inode);
  56. if (!pipe->writers) {
  57. if ((filp->f_flags & O_NONBLOCK)) {
  58. /* suppress POLLHUP until we have
  59. * seen a writer */
  60. filp->f_version = pipe->w_counter;
  61. } else {
  62. if (wait_for_partner(inode, &pipe->w_counter))
  63. goto err_rd;
  64. }
  65. }
  66. break;
  67. case FMODE_WRITE:
  68. /*
  69. * O_WRONLY
  70. * POSIX.1 says that O_NONBLOCK means return -1 with
  71. * errno=ENXIO when there is no process reading the FIFO.
  72. */
  73. ret = -ENXIO;
  74. if ((filp->f_flags & O_NONBLOCK) && !pipe->readers)
  75. goto err;
  76. filp->f_op = &write_pipefifo_fops;
  77. pipe->w_counter++;
  78. if (!pipe->writers++)
  79. wake_up_partner(inode);
  80. if (!pipe->readers) {
  81. if (wait_for_partner(inode, &pipe->r_counter))
  82. goto err_wr;
  83. }
  84. break;
  85. case FMODE_READ | FMODE_WRITE:
  86. /*
  87. * O_RDWR
  88. * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
  89. * This implementation will NEVER block on a O_RDWR open, since
  90. * the process can at least talk to itself.
  91. */
  92. filp->f_op = &rdwr_pipefifo_fops;
  93. pipe->readers++;
  94. pipe->writers++;
  95. pipe->r_counter++;
  96. pipe->w_counter++;
  97. if (pipe->readers == 1 || pipe->writers == 1)
  98. wake_up_partner(inode);
  99. break;
  100. default:
  101. ret = -EINVAL;
  102. goto err;
  103. }
  104. /* Ok! */
  105. mutex_unlock(&inode->i_mutex);
  106. return 0;
  107. err_rd:
  108. if (!--pipe->readers)
  109. wake_up_interruptible(&pipe->wait);
  110. ret = -ERESTARTSYS;
  111. goto err;
  112. err_wr:
  113. if (!--pipe->writers)
  114. wake_up_interruptible(&pipe->wait);
  115. ret = -ERESTARTSYS;
  116. goto err;
  117. err:
  118. if (!pipe->readers && !pipe->writers)
  119. free_pipe_info(inode);
  120. err_nocleanup:
  121. mutex_unlock(&inode->i_mutex);
  122. return ret;
  123. }
  124. /*
  125. * Dummy default file-operations: the only thing this does
  126. * is contain the open that then fills in the correct operations
  127. * depending on the access mode of the file...
  128. */
  129. const struct file_operations def_fifo_fops = {
  130. .open = fifo_open, /* will set read_ or write_pipefifo_fops */
  131. .llseek = noop_llseek,
  132. };