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 /****************************************************************************
37 * Included Files
38 ****************************************************************************/
39
40 #include "vfs_config.h"
41
42 #include "errno.h"
43 #include "unistd.h"
44 #include "sched.h"
45
46 #if defined(LOSCFG_NET_LWIP_SACK)
47 #include "lwip/sockets.h"
48 #endif
49
50 #include "mqueue.h"
51 #include "fs/file.h"
52
53 /****************************************************************************
54 * Public Functions
55 ****************************************************************************/
56
57 /****************************************************************************
58 * Name: close
59 *
60 * Description:
61 * close() closes a file descriptor, so that it no longer refers to any
62 * file and may be reused. Any record locks (see fcntl(2)) held on the file
63 * it was associated with, and owned by the process, are removed (regardless
64 * of the file descriptor that was used to obtain the lock).
65 *
66 * If fd is the last copy of a particular file descriptor the resources
67 * associated with it are freed; if the descriptor was the last reference
68 * to a file which has been removed using unlink(2) the file is deleted.
69 *
70 * Input Parameters:
71 * fd file descriptor to close
72 *
73 * Returned Value:
74 * 0 on success; -1 on error with errno set appropriately.
75 *
76 * Assumptions:
77 *
78 ****************************************************************************/
79
close(int fd)80 int close(int fd)
81 {
82 int err;
83 #if CONFIG_NFILE_DESCRIPTORS > 0
84 int ret;
85
86 /* Did we get a valid file descriptor? */
87
88 if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS)
89 #endif
90 {
91 /* Close a socket descriptor */
92
93 #if defined(LOSCFG_NET_LWIP_SACK)
94 if ((unsigned int)fd < (unsigned int)(CONFIG_NFILE_DESCRIPTORS+CONFIG_NSOCKET_DESCRIPTORS))
95 {
96 return socks_close(fd);
97 }
98 else
99 #endif
100 #if defined(LOSCFG_COMPAT_POSIX)
101 if ((unsigned int)fd >= MQUEUE_FD_OFFSET && \
102 (unsigned int)fd < (unsigned int)(MQUEUE_FD_OFFSET + CONFIG_NQUEUE_DESCRIPTORS))
103 {
104 return mq_close((mqd_t)fd);
105 }
106 #endif
107 else
108 {
109 err = EBADF;
110 goto errout;
111 }
112 }
113
114 #if CONFIG_NFILE_DESCRIPTORS > 0
115 /* Close the driver or mountpoint. NOTES: (1) there is no
116 * exclusion mechanism here, the driver or mountpoint must be
117 * able to handle concurrent operations internally, (2) The driver
118 * may have been opened numerous times (for different file
119 * descriptors) and must also handle being closed numerous times.
120 * (3) for the case of the mountpoint, we depend on the close
121 * methods bing identical in signature and position in the operations
122 * vtable.
123 */
124
125 ret = files_close(fd);
126 if (ret < 0)
127 {
128 /* An error occurred while closing the driver */
129
130 err = -ret;
131 goto errout;
132 }
133 return OK;
134
135 #endif
136
137 errout:
138 set_errno(err);
139 return VFS_ERROR;
140 }
141