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