fs_open.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /****************************************************************************
  2. * fs/vfs/fs_open.c
  3. *
  4. * Copyright (C) 2007-2009, 2011-2012, 2016-2018 Gregory Nutt. All rights
  5. * reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <sys/types.h>
  41. #include <stdbool.h>
  42. #include <fcntl.h>
  43. #include <sched.h>
  44. #include <errno.h>
  45. #include <assert.h>
  46. #include <stdarg.h>
  47. #include <nuttx/cancelpt.h>
  48. #include <nuttx/fs/fs.h>
  49. #include "inode/inode.h"
  50. #include "driver/driver.h"
  51. /****************************************************************************
  52. * Public Functions
  53. ****************************************************************************/
  54. /****************************************************************************
  55. * Name: inode_checkflags
  56. *
  57. * Description:
  58. * Check if the access described by 'oflags' is supported on 'inode'
  59. *
  60. ****************************************************************************/
  61. int inode_checkflags(FAR struct inode *inode, int oflags)
  62. {
  63. if (((oflags & O_RDOK) != 0 && !inode->u.i_ops->read) ||
  64. ((oflags & O_WROK) != 0 && !inode->u.i_ops->write))
  65. {
  66. return -EACCES;
  67. }
  68. else
  69. {
  70. return OK;
  71. }
  72. }
  73. /****************************************************************************
  74. * Name: nx_vopen
  75. *
  76. * Description:
  77. * nx_vopen() is identical to 'nx_open' except that it accepts a va_list
  78. * as an argument versus taking a variable length list of arguments.
  79. *
  80. * nx_vopen() is an internal NuttX interface and should not be called from
  81. * applications.
  82. *
  83. * Returned Value:
  84. * Zero (OK) is returned on success; a negated errno value is returned on
  85. * any failure.
  86. *
  87. ****************************************************************************/
  88. int nx_vopen(FAR const char *path, int oflags, va_list ap)
  89. {
  90. struct inode_search_s desc;
  91. FAR struct file *filep;
  92. FAR struct inode *inode;
  93. #if defined(CONFIG_FILE_MODE) || !defined(CONFIG_DISABLE_MOUNTPOINT)
  94. mode_t mode = 0666;
  95. #endif
  96. int ret;
  97. int fd;
  98. if (path == NULL)
  99. {
  100. return -EINVAL;
  101. }
  102. #ifdef CONFIG_FILE_MODE
  103. # ifdef CONFIG_CPP_HAVE_WARNING
  104. # warning "File creation not implemented"
  105. # endif
  106. /* If the file is opened for creation, then get the mode bits */
  107. if ((oflags & (O_WRONLY | O_CREAT)) != 0)
  108. {
  109. va_start(ap, oflags);
  110. mode = va_arg(ap, mode_t);
  111. va_end(ap);
  112. }
  113. #endif
  114. /* Get an inode for this file */
  115. SETUP_SEARCH(&desc, path, false);
  116. ret = inode_find(&desc);
  117. if (ret < 0)
  118. {
  119. /* "O_CREAT is not set and the named file does not exist. Or, a
  120. * directory component in pathname does not exist or is a dangling
  121. * symbolic link."
  122. */
  123. goto errout_with_search;
  124. }
  125. /* Get the search results */
  126. inode = desc.node;
  127. DEBUGASSERT(inode != NULL);
  128. #if !defined(CONFIG_DISABLE_MOUNTPOINT) && \
  129. !defined(CONFIG_DISABLE_PSEUDOFS_OPERATIONS)
  130. /* If the inode is block driver, then we may return a character driver
  131. * proxy for the block driver. block_proxy() will instantiate a BCH
  132. * character driver wrapper around the block driver, open(), then
  133. * unlink() the character driver. On success, block_proxy() will
  134. * return the file descriptor of the opened character driver.
  135. *
  136. * NOTE: This will recurse to open the character driver proxy.
  137. */
  138. if (INODE_IS_BLOCK(inode) || INODE_IS_MTD(inode))
  139. {
  140. /* Release the inode reference */
  141. inode_release(inode);
  142. /* Get the file descriptor of the opened character driver proxy */
  143. fd = block_proxy(path, oflags);
  144. if (fd < 0)
  145. {
  146. ret = fd;
  147. goto errout_with_search;
  148. }
  149. /* Return the file descriptor */
  150. RELEASE_SEARCH(&desc);
  151. return fd;
  152. }
  153. else
  154. #endif
  155. /* Verify that the inode is either a "normal" character driver or a
  156. * mountpoint. We specifically "special" inodes (semaphores, message
  157. * queues, shared memory).
  158. */
  159. #ifndef CONFIG_DISABLE_MOUNTPOINT
  160. if ((!INODE_IS_DRIVER(inode) && !INODE_IS_MOUNTPT(inode)) ||
  161. !inode->u.i_ops)
  162. #else
  163. if (!INODE_IS_DRIVER(inode) || !inode->u.i_ops)
  164. #endif
  165. {
  166. ret = -ENXIO;
  167. goto errout_with_inode;
  168. }
  169. /* Make sure that the inode supports the requested access */
  170. ret = inode_checkflags(inode, oflags);
  171. if (ret < 0)
  172. {
  173. goto errout_with_inode;
  174. }
  175. /* Associate the inode with a file structure */
  176. fd = files_allocate(inode, oflags, 0, 0);
  177. if (fd < 0)
  178. {
  179. ret = -EMFILE;
  180. goto errout_with_inode;
  181. }
  182. /* Get the file structure corresponding to the file descriptor. */
  183. ret = fs_getfilep(fd, &filep);
  184. if (ret < 0)
  185. {
  186. goto errout_with_inode;
  187. }
  188. /* Perform the driver open operation. NOTE that the open method may be
  189. * called many times. The driver/mountpoint logic should handled this
  190. * because it may also be closed that many times.
  191. */
  192. ret = OK;
  193. if (inode->u.i_ops->open)
  194. {
  195. #ifndef CONFIG_DISABLE_MOUNTPOINT
  196. if (INODE_IS_MOUNTPT(inode))
  197. {
  198. ret = inode->u.i_mops->open(filep, desc.relpath, oflags, mode);
  199. }
  200. else
  201. #endif
  202. {
  203. ret = inode->u.i_ops->open(filep);
  204. }
  205. }
  206. if (ret < 0)
  207. {
  208. goto errout_with_fd;
  209. }
  210. #ifdef CONFIG_PSEUDOTERM_SUSV1
  211. /* If the return value from the open method is > 0, then it may actually
  212. * be an encoded file descriptor. This kind of logic is currently only
  213. * needed for /dev/ptmx: When dev ptmx is opened, it does not return a
  214. * file descriptor associated with the /dev/ptmx inode, but rather with
  215. * the inode of master device created by the /dev/ptmx open method.
  216. *
  217. * The encoding supports (a) returning file descriptor 0 (which really
  218. * should not happen), and (b) avoiding confusion if some other open
  219. * method returns a positive, non-zero value which is not a file
  220. * descriptor.
  221. */
  222. if (OPEN_ISFD(ret))
  223. {
  224. /* Release file descriptor and inode that we allocated. We don't
  225. * need those.
  226. */
  227. files_release(fd);
  228. inode_release(inode);
  229. /* Instead, decode and return the descriptor associated with the
  230. * master side device.
  231. */
  232. fd = (int)OPEN_GETFD(ret);
  233. DEBUGASSERT((unsigned)fd < (CONFIG_NFILE_DESCRIPTORS +
  234. CONFIG_NSOCKET_DESCRIPTORS));
  235. }
  236. #endif
  237. RELEASE_SEARCH(&desc);
  238. return fd;
  239. errout_with_fd:
  240. files_release(fd);
  241. errout_with_inode:
  242. inode_release(inode);
  243. errout_with_search:
  244. RELEASE_SEARCH(&desc);
  245. return ret;
  246. }
  247. /****************************************************************************
  248. * Name: nx_open
  249. *
  250. * Description:
  251. * nx_open() is similar to the standard 'open' interface except that is is
  252. * not a cancellation point and it does not modify the errno variable.
  253. *
  254. * nx_open() is an internal NuttX interface and should not be called from
  255. * applications.
  256. *
  257. * Returned Value:
  258. * Zero (OK) is returned on success; a negated errno value is returned on
  259. * any failure.
  260. *
  261. ****************************************************************************/
  262. int nx_open(FAR const char *path, int oflags, ...)
  263. {
  264. va_list ap;
  265. int fd;
  266. /* Let nx_vopen() do all of the work */
  267. va_start(ap, oflags);
  268. fd = nx_vopen(path, oflags, ap);
  269. va_end(ap);
  270. return fd;
  271. }
  272. /****************************************************************************
  273. * Name: open
  274. *
  275. * Description:
  276. * Standard 'open' interface
  277. *
  278. * Returned Value:
  279. * Zero (OK) is returned on success; -1 (ERROR) is returned on any failure
  280. * the the errno value set appropriately.
  281. *
  282. ****************************************************************************/
  283. int open(FAR const char *path, int oflags, ...)
  284. {
  285. va_list ap;
  286. int fd;
  287. /* open() is a cancellation point */
  288. (void)enter_cancellation_point();
  289. /* Let nx_vopen() do most of the work */
  290. va_start(ap, oflags);
  291. fd = nx_vopen(path, oflags, ap);
  292. va_end(ap);
  293. /* Set the errno value if any errors were reported by nx_open() */
  294. if (fd < 0)
  295. {
  296. set_errno(-fd);
  297. fd = ERROR;
  298. }
  299. leave_cancellation_point();
  300. return fd;
  301. }