• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _PROC_FS_H
33 #define _PROC_FS_H
34 
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 
38 #include "los_config.h"
39 
40 #ifdef LOSCFG_FS_PROC
41 #include "linux/spinlock.h"
42 #include "asm/atomic.h"
43 #include "vnode.h"
44 #include "fs/file.h"
45 #include "los_seq_buf.h"
46 
47 #ifdef __cplusplus
48 #if __cplusplus
49 extern "C" {
50 #endif /* __cplusplus */
51 #endif /* __cplusplus */
52 
53 typedef unsigned short fmode_t;
54 #define PROC_ERROR (-1)
55 
56 /* Default file mode for procfs */
57 #define PROCFS_DEFAULT_MODE 0550
58 
59 /* 64bit hashes as llseek() offset (for directories) */
60 #define FMODE_64BITHASH   ((fmode_t)0x400)
61 /* 32bit hashes as llseek() offset (for directories) */
62 #define FMODE_32BITHASH   ((fmode_t)0x200)
63 /* File is opened using open(.., 3, ..) and is writable only for ioctls
64  *     (specialy hack for floppy.c)
65  */
66 #define FMODE_WRITE_IOCTL ((fmode_t)0x100)
67 /* File is opened with O_EXCL (only set for block devices) */
68 #define FMODE_EXCL        ((fmode_t)0x80)
69 /* File is opened with O_NDELAY (only set for block devices) */
70 #define FMODE_NDELAY      ((fmode_t)0x40)
71 /* File is opened for execution with sys_execve / sys_uselib */
72 #define FMODE_EXEC        ((fmode_t)0x20)
73 /* file can be accessed using pwrite */
74 #define FMODE_PWRITE      ((fmode_t)0x10)
75 /* file can be accessed using pread */
76 #define FMODE_PREAD       ((fmode_t)0x8)
77 /* file is seekable */
78 #define FMODE_LSEEK       ((fmode_t)0x4)
79 /* file is open for writing */
80 #define FMODE_WRITE       ((fmode_t)0x2)
81 /* file is open for reading */
82 #define FMODE_READ        ((fmode_t)0x1)
83 
84 struct ProcFile;
85 
86 struct ProcFileOperations {
87     char *name;
88     ssize_t (*write)(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos);
89     int (*open)(struct Vnode *vnode, struct ProcFile *pf);
90     int (*release)(struct Vnode *vnode, struct ProcFile *pf);
91     int (*read)(struct SeqBuf *m, void *v);
92 };
93 
94 struct ProcDirEntry {
95     uint uid;
96     uint gid;
97     mode_t mode;
98     int flags;
99     const struct ProcFileOperations *procFileOps;
100     struct ProcFile *pf;
101     struct ProcDirEntry *next, *parent, *subdir;
102     void *data;
103     atomic_t count; /* open file count */
104     spinlock_t pdeUnloadLock;
105 
106     int nameLen;
107     struct ProcDirEntry *pdirCurrent;
108     char name[NAME_MAX];
109     enum VnodeType type;
110 };
111 
112 struct ProcFile {
113     fmode_t fMode;
114     spinlock_t fLock;
115     atomic_t fCount;
116     struct SeqBuf *sbuf;
117     struct ProcDirEntry *pPDE;
118     unsigned long long fVersion;
119     loff_t fPos;
120     char name[NAME_MAX];
121 };
122 
123 struct ProcStat {
124     mode_t stMode;
125     struct ProcDirEntry *pPDE;
126     char name[NAME_MAX];
127 };
128 
129 struct ProcData {
130     ssize_t size;
131     loff_t fPos;
132     char buf[1];
133 };
134 
135 #define PROCDATA(n) (sizeof(struct ProcData) + (n))
136 
137 #define S_IALLUGO (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
138 
139 /**
140  * Interface for modules using proc below internal proc module;
141  */
142 /**
143  * @ingroup  procfs
144  * @brief create a proc node
145  *
146  * @par Description:
147  * This API is used to create the node by 'name' and parent vnode
148  *
149  * @attention
150  * <ul>
151  * <li>This interface should be called after system initialization.</li>
152  * <li>The parameter name should be a valid string.</li>
153  * </ul>
154  *
155  * @param  name    [IN] Type #const char * The name of the node to be created.
156  * @param  mode    [IN] Type #mode_t the mode of create's node.
157  * @param  parent  [IN] Type #struct ProcDirEntry * the parent node of the node to be created,
158  * if pass NULL, default parent node is "/proc".
159  *
160  * @retval #NULL                Create failed.
161  * @retval #ProcDirEntry*     Create successfully.
162  * @par Dependency:
163  * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul>
164  * @see
165  *
166  */
167 extern struct ProcDirEntry *CreateProcEntry(const char *name, mode_t mode, struct ProcDirEntry *parent);
168 
169 /**
170  * @ingroup  procfs
171  * @brief remove a proc node
172  *
173  * @par Description:
174  * This API is used to remove the node by 'name' and parent vnode
175  *
176  * @attention
177  * <ul>
178  * <li>This interface should be called after system initialization.</li>
179  * <li>The parameter name should be a valid string.</li>
180  * </ul>
181  *
182  * @param  name   [IN] Type #const char * The name of the node to be removed.
183  * @param  parent [IN] Type #struct ProcDirEntry * the parent node of the node to be remove.
184  *
185  * @par Dependency:
186  * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul>
187  * @see
188  *
189  */
190 extern void RemoveProcEntry(const char *name, struct ProcDirEntry *parent);
191 
192 /**
193  * @ingroup  procfs
194  * @brief create a proc directory node
195  *
196  * @par Description:
197  * This API is used to create the directory node by 'name' and parent vnode
198  *
199  * @attention
200  * <ul>
201  * <li>This interface should be called after system initialization.</li>
202  * <li>The parameter name should be a valid string.</li>
203  * </ul>
204  *
205  * @param  name   [IN] Type #const char * The name of the node directory to be created.
206  * @param  parent [IN] Type #struct ProcDirEntry * the parent node of the directory node to be created,
207  * if pass NULL, default parent node is "/proc".
208  *
209  * @retval #NULL               Create failed.
210  * @retval #ProcDirEntry*    Create successfully.
211  * @par Dependency:
212  * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul>
213  * @see
214  *
215  */
216 extern struct ProcDirEntry *ProcMkdir(const char *name, struct ProcDirEntry *parent);
217 
218 /**
219  * @ingroup  procfs
220  * @brief create a proc  node
221  *
222  * @par Description:
223  * This API is used to create the node by 'name' and parent vnode,
224  * And assignment operation function
225  *
226  * @attention
227  * <ul>
228  * <li>This interface should be called after system initialization.</li>
229  * <li>The parameter name should be a valid string.</li>
230  * </ul>
231  *
232  * @param  name      [IN] Type #const char * The name of the node to be created.
233  * @param  mode      [IN] Type #mode_t the mode of create's node.
234  * @param  parent    [IN] Type #struct ProcDirEntry * the parent node of the node to be created.
235  * @param  procFops [IN] Type #const struct ProcFileOperations * operation function of the node.
236  *
237  * @retval #NULL               Create failed.
238  * @retval #ProcDirEntry*    Create successfully.
239  * @par Dependency:
240  * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul>
241  * @see
242  *
243  */
244 extern struct ProcDirEntry *ProcCreate(const char *name, mode_t mode,
245     struct ProcDirEntry *parent, const struct ProcFileOperations *procFops);
246 
247 /**
248  * @ingroup  procfs
249  * @brief init proc fs
250  *
251  * @par Description:
252  * This API is used to init proc fs.
253  *
254  * @attention
255  * <ul>
256  * <li>None.</li>
257  * </ul>
258  *
259  * @param  NONE
260  *
261  * @retval NONE
262  * @par Dependency:
263  * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul>
264  * @see ProcFsInit
265  *
266  */
267 extern void ProcFsInit(void);
268 
269 #ifdef __cplusplus
270 #if __cplusplus
271 }
272 #endif /* __cplusplus */
273 #endif /* __cplusplus */
274 
275 #endif /* LOSCFG_FS_PROC */
276 #endif /* _PROC_FS_H */
277