fifo.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 void 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. }
  24. static void wake_up_partner(struct inode* inode)
  25. {
  26. wake_up_interruptible(&inode->i_pipe->wait);
  27. }
  28. static int fifo_open(struct inode *inode, struct file *filp)
  29. {
  30. struct pipe_inode_info *pipe;
  31. int ret;
  32. mutex_lock(&inode->i_mutex);
  33. pipe = inode->i_pipe;
  34. if (!pipe) {
  35. ret = -ENOMEM;
  36. pipe = alloc_pipe_info(inode);
  37. if (!pipe)
  38. goto err_nocleanup;
  39. inode->i_pipe = pipe;
  40. }
  41. filp->f_version = 0;
  42. /* We can only do regular read/write on fifos */
  43. filp->f_mode &= (FMODE_READ | FMODE_WRITE);
  44. switch (filp->f_mode) {
  45. case FMODE_READ:
  46. /*
  47. * O_RDONLY
  48. * POSIX.1 says that O_NONBLOCK means return with the FIFO
  49. * opened, even when there is no process writing the FIFO.
  50. */
  51. filp->f_op = &read_pipefifo_fops;
  52. pipe->r_counter++;
  53. if (pipe->readers++ == 0)
  54. wake_up_partner(inode);
  55. if (!pipe->writers) {
  56. if ((filp->f_flags & O_NONBLOCK)) {
  57. /* suppress POLLHUP until we have
  58. * seen a writer */
  59. filp->f_version = pipe->w_counter;
  60. } else {
  61. wait_for_partner(inode, &pipe->w_counter);
  62. if(signal_pending(current))
  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. wait_for_partner(inode, &pipe->r_counter);
  82. if (signal_pending(current))
  83. goto err_wr;
  84. }
  85. break;
  86. case FMODE_READ | FMODE_WRITE:
  87. /*
  88. * O_RDWR
  89. * POSIX.1 leaves this case "undefined" when O_NONBLOCK is set.
  90. * This implementation will NEVER block on a O_RDWR open, since
  91. * the process can at least talk to itself.
  92. */
  93. filp->f_op = &rdwr_pipefifo_fops;
  94. pipe->readers++;
  95. pipe->writers++;
  96. pipe->r_counter++;
  97. pipe->w_counter++;
  98. if (pipe->readers == 1 || pipe->writers == 1)
  99. wake_up_partner(inode);
  100. break;
  101. default:
  102. ret = -EINVAL;
  103. goto err;
  104. }
  105. /* Ok! */
  106. mutex_unlock(&inode->i_mutex);
  107. return 0;
  108. err_rd:
  109. if (!--pipe->readers)
  110. wake_up_interruptible(&pipe->wait);
  111. ret = -ERESTARTSYS;
  112. goto err;
  113. err_wr:
  114. if (!--pipe->writers)
  115. wake_up_interruptible(&pipe->wait);
  116. ret = -ERESTARTSYS;
  117. goto err;
  118. err:
  119. if (!pipe->readers && !pipe->writers)
  120. free_pipe_info(inode);
  121. err_nocleanup:
  122. mutex_unlock(&inode->i_mutex);
  123. return ret;
  124. }
  125. /*
  126. * Dummy default file-operations: the only thing this does
  127. * is contain the open that then fills in the correct operations
  128. * depending on the access mode of the file...
  129. */
  130. const struct file_operations def_fifo_fops = {
  131. .open = fifo_open, /* will set read_ or write_pipefifo_fops */
  132. .llseek = noop_llseek,
  133. };