• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  * fs/vfs/fs_getfilep.c
3  *
4  * Copyright (c) 2023 Huawei Device Co., Ltd. All rights reserved.
5  * Based on NuttX originally written by Gregory Nutt
6  *
7  *   Copyright (C) 2014, 2016-2017 Gregory Nutt. All rights reserved.
8  *   Author: Gregory Nutt <gnutt@nuttx.org>
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  * 3. Neither the name NuttX nor the names of its contributors may be
21  *    used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
31  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  ****************************************************************************/
38 
39 /****************************************************************************
40  * Included Files
41  ****************************************************************************/
42 
43 #include "errno.h"
44 #include "unistd.h"
45 #include "console.h"
46 #include "sched.h"
47 #include "sys/types.h"
48 #include "vnode.h"
49 #include "vfs_config.h"
50 
51 /****************************************************************************
52  * Public Functions
53  ****************************************************************************/
54 
55 /****************************************************************************
56  * Name: fs_getfilep
57  *
58  * Description:
59  *   Given a file descriptor, return the corresponding instance of struct
60  *   file.  NOTE that this function will currently fail if it is provided
61  *   with a socket descriptor.
62  *
63  * Input Parameters:
64  *   fd    - The file descriptor
65  *   filep - The location to return the struct file instance
66  *
67  * Returned Value:
68  *   Zero (OK) is returned on success; a negated errno value is returned on
69  *   any failure.
70  *
71  ****************************************************************************/
72 
fs_getfilep_normal(int fd,struct file ** filep)73 static int fs_getfilep_normal(int fd, struct file **filep)
74 {
75   struct filelist *list;
76 
77   *filep = (struct file *)NULL;
78 
79   if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
80     {
81         fd = ConsoleUpdateFd();
82     }
83 
84   if ((unsigned int)fd >= CONFIG_NFILE_DESCRIPTORS)
85     {
86       return -EBADF;
87     }
88 
89   /* The descriptor is in a valid range to file descriptor... Get the
90    * thread-specific file list.
91    */
92 
93   list = sched_getfiles();
94 
95   /* The file list can be NULL under two cases:  (1) One is an obscure
96    * cornercase:  When memory management debug output is enabled.  Then
97    * there may be attempts to write to stdout from malloc before the group
98    * data has been allocated.  The other other is (2) if this is a kernel
99    * thread.  Kernel threads have no allocated file descriptors.
100    */
101 
102   if (list == NULL)
103     {
104       return -EAGAIN;
105     }
106 
107   /* And return the file pointer from the list */
108 
109   *filep = &list->fl_files[fd];
110   return OK;
111 }
112 
fs_getfilep(int fd,struct file ** filep)113 int fs_getfilep(int fd, struct file **filep)
114 {
115   int ret = fs_getfilep_normal(fd, filep);
116   if (ret < 0)
117   {
118     set_errno(-ret);
119     return VFS_ERROR;
120   }
121   return OK;
122 }
123