fs_close.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /****************************************************************************
  2. * fs/vfs/fs_close.c
  3. *
  4. * Copyright (C) 2007-2009, 2012, 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 <unistd.h>
  40. #include <sched.h>
  41. #include <errno.h>
  42. #include <nuttx/cancelpt.h>
  43. #include <nuttx/fs/fs.h>
  44. #ifdef CONFIG_NET
  45. # include <nuttx/net/net.h>
  46. #endif
  47. #include "inode/inode.h"
  48. /****************************************************************************
  49. * Public Functions
  50. ****************************************************************************/
  51. /****************************************************************************
  52. * Name: nx_close
  53. *
  54. * Description:
  55. * nx_close() is similar to the standard 'close' interface except that is
  56. * not a cancellation point and it does not modify the errno variable.
  57. *
  58. * nx_close() is an internal NuttX interface and should not be called from
  59. * applications.
  60. *
  61. * Returned Value:
  62. * The new file descriptor is returned on success; a negated errno value is
  63. * returned on any failure.
  64. *
  65. ****************************************************************************/
  66. int nx_close(int fd)
  67. {
  68. /* Did we get a valid file descriptor? */
  69. if (fd >= CONFIG_NFILE_DESCRIPTORS)
  70. {
  71. /* Close a socket descriptor */
  72. #ifdef CONFIG_NET
  73. if (fd < (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS))
  74. {
  75. return net_close(fd);
  76. }
  77. else
  78. #endif
  79. {
  80. return -EBADF;
  81. }
  82. }
  83. /* Close the driver or mountpoint. NOTES: (1) there is no
  84. * exclusion mechanism here, the driver or mountpoint must be
  85. * able to handle concurrent operations internally, (2) The driver
  86. * may have been opened numerous times (for different file
  87. * descriptors) and must also handle being closed numerous times.
  88. * (3) for the case of the mountpoint, we depend on the close
  89. * methods bing identical in signature and position in the operations
  90. * vtable.
  91. */
  92. return files_close(fd);
  93. }
  94. /****************************************************************************
  95. * Name: close
  96. *
  97. * Description:
  98. * close() closes a file descriptor, so that it no longer refers to any
  99. * file and may be reused. Any record locks (see fcntl(2)) held on the file
  100. * it was associated with, and owned by the process, are removed
  101. * (regardless of the file descriptor that was used to obtain the lock).
  102. *
  103. * If fd is the last copy of a particular file descriptor the resources
  104. * associated with it are freed; if the descriptor was the last reference
  105. * to a file which has been removed using unlink(2) the file is deleted.
  106. *
  107. * Input Parameters:
  108. * fd file descriptor to close
  109. *
  110. * Returned Value:
  111. * 0 on success; -1 on error with errno set appropriately.
  112. *
  113. * Assumptions:
  114. *
  115. ****************************************************************************/
  116. int close(int fd)
  117. {
  118. int ret;
  119. /* close() is a cancellation point */
  120. enter_cancellation_point();
  121. ret = nx_close(fd);
  122. if (ret < 0)
  123. {
  124. set_errno(-ret);
  125. ret = ERROR;
  126. }
  127. leave_cancellation_point();
  128. return ret;
  129. }