1 /****************************************************************************
2 * fs/vfs/fs_getfilep.c
3 *
4 * Copyright (C) 2014, 2016-2017 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 "errno.h"
41 #include "unistd.h"
42 #include "console.h"
43 #include "sched.h"
44 #include "sys/types.h"
45 #include "vnode.h"
46 #include "vfs_config.h"
47
48 /****************************************************************************
49 * Public Functions
50 ****************************************************************************/
51
52 /****************************************************************************
53 * Name: fs_getfilep
54 *
55 * Description:
56 * Given a file descriptor, return the corresponding instance of struct
57 * file. NOTE that this function will currently fail if it is provided
58 * with a socket descriptor.
59 *
60 * Input Parameters:
61 * fd - The file descriptor
62 * filep - The location to return the struct file instance
63 *
64 * Returned Value:
65 * Zero (OK) is returned on success; a negated errno value is returned on
66 * any failure.
67 *
68 ****************************************************************************/
69
fs_getfilep_normal(int fd,struct file ** filep)70 static int fs_getfilep_normal(int fd, struct file **filep)
71 {
72 struct filelist *list;
73
74 *filep = (struct file *)NULL;
75
76 if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
77 {
78 fd = ConsoleUpdateFd();
79 }
80
81 if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS)
82 {
83 return -EBADF;
84 }
85
86 /* The descriptor is in a valid range to file descriptor... Get the
87 * thread-specific file list.
88 */
89
90 list = sched_getfiles();
91
92 /* The file list can be NULL under two cases: (1) One is an obscure
93 * cornercase: When memory management debug output is enabled. Then
94 * there may be attempts to write to stdout from malloc before the group
95 * data has been allocated. The other other is (2) if this is a kernel
96 * thread. Kernel threads have no allocated file descriptors.
97 */
98
99 if (list == NULL)
100 {
101 return -EAGAIN;
102 }
103
104 /* And return the file pointer from the list */
105
106 *filep = &list->fl_files[fd];
107 return OK;
108 }
109
fs_getfilep(int fd,struct file ** filep)110 int fs_getfilep(int fd, struct file **filep)
111 {
112 int ret = fs_getfilep_normal(fd, filep);
113 if (ret < 0)
114 {
115 set_errno(-ret);
116 return VFS_ERROR;
117 }
118 return OK;
119 }
120