fs_filedetach.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /****************************************************************************
  2. * fs/inode/fs_filedetach.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <assert.h>
  25. #include <errno.h>
  26. #include <nuttx/sched.h>
  27. #include <nuttx/fs/fs.h>
  28. #include <nuttx/semaphore.h>
  29. #include "inode/inode.h"
  30. /****************************************************************************
  31. * Private Functions
  32. ****************************************************************************/
  33. /****************************************************************************
  34. * Name: _files_semtake
  35. ****************************************************************************/
  36. static inline int _files_semtake(FAR struct filelist *list)
  37. {
  38. return nxsem_wait_uninterruptible(&list->fl_sem);
  39. }
  40. /****************************************************************************
  41. * Name: _files_semgive
  42. ****************************************************************************/
  43. #define _files_semgive(list) nxsem_post(&list->fl_sem)
  44. /****************************************************************************
  45. * Public Functions
  46. ****************************************************************************/
  47. /****************************************************************************
  48. * Name: file_detach
  49. *
  50. * Description:
  51. * This function is used in device drivers to create a task-independent
  52. * handle to an entity in the file system. file_detach() duplicates the
  53. * 'struct file' that underlies the file descriptor, then closes the file
  54. * descriptor.
  55. *
  56. * This function will fail if fd is not a valid file descriptor. In
  57. * particular, it will fail if fd is a socket descriptor.
  58. *
  59. * Input Parameters:
  60. * fd - The file descriptor to be detached. This descriptor will be
  61. * closed and invalid if the file was successfully detached.
  62. * filep - A pointer to a user provided memory location in which to
  63. * received the duplicated, detached file structure.
  64. *
  65. * Returned Value:
  66. * Zero (OK) is returned on success; A negated errno value is returned on
  67. * any failure to indicate the nature of the failure.
  68. *
  69. ****************************************************************************/
  70. int file_detach(int fd, FAR struct file *filep)
  71. {
  72. FAR struct filelist *list;
  73. FAR struct file *parent;
  74. int ret;
  75. DEBUGASSERT(filep != NULL);
  76. /* Verify the file descriptor range */
  77. if (fd < 0 || fd >= CONFIG_NFILE_DESCRIPTORS)
  78. {
  79. /* Not a file descriptor (might be a socket descriptor) */
  80. return -EBADF;
  81. }
  82. /* Get the thread-specific file list. It should never be NULL in this
  83. * context.
  84. */
  85. list = nxsched_get_files();
  86. DEBUGASSERT(list != NULL);
  87. /* If the file was properly opened, there should be an inode assigned */
  88. ret = _files_semtake(list);
  89. if (ret < 0)
  90. {
  91. /* Probably canceled */
  92. return ret;
  93. }
  94. parent = &list->fl_files[fd];
  95. if (parent->f_inode == NULL)
  96. {
  97. /* File is not open */
  98. _files_semgive(list);
  99. return -EBADF;
  100. }
  101. /* Duplicate the 'struct file' content into the user-provided file
  102. * structure.
  103. */
  104. filep->f_oflags = parent->f_oflags;
  105. filep->f_pos = parent->f_pos;
  106. filep->f_inode = parent->f_inode;
  107. filep->f_priv = parent->f_priv;
  108. /* Release the file descriptor *without* calling the driver close method
  109. * and without decrementing the inode reference count. That will be done
  110. * in file_close().
  111. */
  112. parent->f_oflags = 0;
  113. parent->f_pos = 0;
  114. parent->f_inode = NULL;
  115. parent->f_priv = NULL;
  116. _files_semgive(list);
  117. return OK;
  118. }