fs_filedetach.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /****************************************************************************
  2. * fs/inode/fs_filedetach.c
  3. *
  4. * Copyright (C) 2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <semaphore.h>
  40. #include <assert.h>
  41. #include <errno.h>
  42. #include <nuttx/sched.h>
  43. #include <nuttx/fs/fs.h>
  44. #include "inode/inode.h"
  45. /****************************************************************************
  46. * Private Functions
  47. ****************************************************************************/
  48. /****************************************************************************
  49. * Name: _files_semtake
  50. ****************************************************************************/
  51. static inline void _files_semtake(FAR struct filelist *list)
  52. {
  53. /* Take the semaphore (perhaps waiting) */
  54. while (sem_wait(&list->fl_sem) != 0)
  55. {
  56. /* The only case that an error should occur here is if
  57. * the wait was awakened by a signal.
  58. */
  59. DEBUGASSERT(get_errno() == EINTR);
  60. }
  61. }
  62. /****************************************************************************
  63. * Name: _files_semgive
  64. ****************************************************************************/
  65. #define _files_semgive(list) sem_post(&list->fl_sem)
  66. /****************************************************************************
  67. * Public Functions
  68. ****************************************************************************/
  69. /****************************************************************************
  70. * Name: file_detach
  71. *
  72. * Description:
  73. * This function is used in device drivers to create a task-independent
  74. * handle to an entity in the file system. file_detach() duplicates the
  75. * 'struct file' that underlies the file descriptor, then closes the file
  76. * descriptor.
  77. *
  78. * This function will fail if fd is not a valid file descriptor. In
  79. * particular, it will fail if fd is a socket descriptor.
  80. *
  81. * Input Parameters:
  82. * fd - The file descriptor to be detached. This descriptor will be
  83. * closed and invalid if the file was successfully detached.
  84. * filep - A pointer to a user provided memory location in which to
  85. * received the duplicated, detached file structure.
  86. *
  87. * Returned Value:
  88. * Zero (OK) is returned on success; A negated errno value is returned on
  89. * any failure to indicate the nature of the failure.
  90. *
  91. ****************************************************************************/
  92. #if CONFIG_NFILE_DESCRIPTORS > 0
  93. int file_detach(int fd, FAR struct file *filep)
  94. {
  95. FAR struct filelist *list;
  96. FAR struct file *parent;
  97. /* Verify the file descriptor range */
  98. if (fd < 0 || fd >= CONFIG_NFILE_DESCRIPTORS)
  99. {
  100. /* Not a file descriptor (might be a socket descriptor) */
  101. return -EBADF;
  102. }
  103. /* Get the thread-specific file list. It should never be NULL in this
  104. * context.
  105. */
  106. list = sched_getfiles();
  107. DEBUGASSERT(list != NULL);
  108. /* If the file was properly opened, there should be an inode assigned */
  109. _files_semtake(list);
  110. parent = &list->fl_files[fd];
  111. if (parent->f_inode == NULL)
  112. {
  113. /* File is not open */
  114. _files_semgive(list);
  115. return -EBADF;
  116. }
  117. /* Duplicate the 'struct file' content into the user-provided file
  118. * structure.
  119. */
  120. filep->f_oflags = parent->f_oflags;
  121. filep->f_pos = parent->f_pos;
  122. filep->f_inode = parent->f_inode;
  123. filep->f_priv = parent->f_priv;
  124. /* Release the file descriptore *without* calling the drive close method
  125. * and without decrementing the inode reference count. That will be done
  126. * in file_close_detached().
  127. */
  128. parent->f_oflags = 0;
  129. parent->f_pos = 0;
  130. parent->f_inode = NULL;
  131. parent->f_priv = NULL;
  132. _files_semgive(list);
  133. return OK;
  134. }
  135. #endif
  136. /****************************************************************************
  137. * Name: file_close_detached
  138. *
  139. * Description:
  140. * Close a file that was previously detached with file_detach().
  141. *
  142. * REVISIT: This is essentially the same as _files_close()
  143. *
  144. * Input Parameters:
  145. * filep - A pointer to a user provided memory location containing the
  146. * open file data returned by file_detach().
  147. *
  148. * Returned Value:
  149. * Zero (OK) is returned on success; A negated errno value is returned on
  150. * any failure to indicate the nature of the failure.
  151. *
  152. ****************************************************************************/
  153. int file_close_detached(FAR struct file *filep)
  154. {
  155. struct inode *inode;
  156. int ret = OK;
  157. DEBUGASSERT(filep != NULL);
  158. inode = filep->f_inode;
  159. /* Check if the struct file is open (i.e., assigned an inode) */
  160. if (inode)
  161. {
  162. /* Close the file, driver, or mountpoint. */
  163. if (inode->u.i_ops && inode->u.i_ops->close)
  164. {
  165. /* Perform the close operation */
  166. ret = inode->u.i_ops->close(filep);
  167. }
  168. /* And release the inode */
  169. inode_release(inode);
  170. /* Reset the user file struct instance so that it cannot be reused. */
  171. filep->f_oflags = 0;
  172. filep->f_pos = 0;
  173. filep->f_inode = NULL;
  174. filep->f_priv = NULL;
  175. }
  176. return ret;
  177. }