更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
proc_fs.h
浏览该文件的文档.
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
49extern "C" {
50#endif /* __cplusplus */
51#endif /* __cplusplus */
52
53typedef 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 writeable 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
84struct ProcFile;
85
86/**
87 * @brief 真正最后能操作pro file的接口,proc本质是个内存文件系统, vfs - > ProcFileOperations
88 */
90 char *name;
91 ssize_t (*write)(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos);
92 int (*open)(struct Vnode *vnode, struct ProcFile *pf);
93 int (*release)(struct Vnode *vnode, struct ProcFile *pf);
94 int (*read)(struct SeqBuf *m, void *v);
95};
96
97/**
98 * @brief proc 目录/文件项, @notethinking 直接叫 ProcEntry不香吗 ?
99 * \n 操作 /proc的 真正结构体
100 */
102 uint uid;
103 uint gid;
104 mode_t mode; ///<模式(读|写...)
105 int flags; ///<标签
106 const struct ProcFileOperations *procFileOps; ///< 驱动程序,每个 /proc 下目录的驱动程序都不一样
107 struct ProcFile *pf; ///<proc文件指针
108 struct ProcDirEntry *next, *parent, *subdir; ///<当前目录项的关系项
109 void *data;
110 atomic_t count; /* open file count | 打开文件的数量*/
111 spinlock_t pdeUnloadLock;
113 struct ProcDirEntry *pdirCurrent; ///<当前目录
114 char name[NAME_MAX];
115 enum VnodeType type; ///<节点类型
116};
117
118/**
119 * @brief Proc文件结构体,对标 FILE 结构体
120 */
121struct ProcFile {
122 fmode_t fMode; ///< 操作文件的模式
123 spinlock_t fLock; ///< 自旋锁
124 atomic_t fCount; ///< 原子操作
125 struct SeqBuf *sbuf; ///< 序列号BUF
126 struct ProcDirEntry *pPDE; ///< 目录项
127 unsigned long long fVersion; ///< 版本号
128 loff_t fPos; ///<文件操作偏移位
129 char name[NAME_MAX]; ///<文件名
130};
131
132struct ProcStat {
135 char name[NAME_MAX];
136};
137
138struct ProcData {
140 loff_t fPos;
141 char buf[1];
142};
143
144#define PROCDATA(n) (sizeof(struct ProcData) + (n))
145
146#define S_IALLUGO (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
147
148/**
149 * Interface for modules using proc below internal proc moudule;
150 */
151/**
152 * @ingroup procfs
153 * @brief create a proc node
154 *
155 * @par Description:
156 * This API is used to create the node by 'name' and parent vnode
157 *
158 * @attention
159 * <ul>
160 * <li>This interface should be called after system initialization.</li>
161 * <li>The parameter name should be a valid string.</li>
162 * </ul>
163 *
164 * @param name [IN] Type #const char * The name of the node to be created.
165 * @param mode [IN] Type #mode_t the mode of create's node.
166 * @param parent [IN] Type #struct ProcDirEntry * the parent node of the node to be created,
167 * if pass NULL, default parent node is "/proc".
168 *
169 * @retval #NULL Create failed.
170 * @retval #ProcDirEntry* Create successfully.
171 * @par Dependency:
172 * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul>
173 * @see
174 *
175 */
176extern struct ProcDirEntry *CreateProcEntry(const char *name, mode_t mode, struct ProcDirEntry *parent);
177
178/**
179 * @ingroup procfs
180 * @brief remove a proc node
181 *
182 * @par Description:
183 * This API is used to remove the node by 'name' and parent vnode
184 *
185 * @attention
186 * <ul>
187 * <li>This interface should be called after system initialization.</li>
188 * <li>The parameter name should be a valid string.</li>
189 * </ul>
190 *
191 * @param name [IN] Type #const char * The name of the node to be removed.
192 * @param parent [IN] Type #struct ProcDirEntry * the parent node of the node to be remove.
193 *
194 * @par Dependency:
195 * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul>
196 * @see
197 *
198 */
199extern void RemoveProcEntry(const char *name, struct ProcDirEntry *parent);
200
201/**
202 * @ingroup procfs
203 * @brief create a proc directory node
204 *
205 * @par Description:
206 * This API is used to create the directory node by 'name' and parent vnode
207 *
208 * @attention
209 * <ul>
210 * <li>This interface should be called after system initialization.</li>
211 * <li>The parameter name should be a valid string.</li>
212 * </ul>
213 *
214 * @param name [IN] Type #const char * The name of the node directory to be created.
215 * @param parent [IN] Type #struct ProcDirEntry * the parent node of the directory node to be created,
216 * if pass NULL, default parent node is "/proc".
217 *
218 * @retval #NULL Create failed.
219 * @retval #ProcDirEntry* Create successfully.
220 * @par Dependency:
221 * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul>
222 * @see
223 *
224 */
225extern struct ProcDirEntry *ProcMkdir(const char *name, struct ProcDirEntry *parent);
226
227/**
228 * @ingroup procfs
229 * @brief create a proc node
230 *
231 * @par Description:
232 * This API is used to create the node by 'name' and parent vnode,
233 * And assignment operation function
234 *
235 * @attention
236 * <ul>
237 * <li>This interface should be called after system initialization.</li>
238 * <li>The parameter name should be a valid string.</li>
239 * </ul>
240 *
241 * @param name [IN] Type #const char * The name of the node to be created.
242 * @param mode [IN] Type #mode_t the mode of create's node.
243 * @param parent [IN] Type #struct ProcDirEntry * the parent node of the node to be created.
244 * @param procFops [IN] Type #const struct ProcFileOperations * operation function of the node.
245 *
246 * @retval #NULL Create failed.
247 * @retval #ProcDirEntry* Create successfully.
248 * @par Dependency:
249 * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul>
250 * @see
251 *
252 */
253extern struct ProcDirEntry *ProcCreate(const char *name, mode_t mode,
254 struct ProcDirEntry *parent, const struct ProcFileOperations *procFops);
255
256/**
257 * @ingroup procfs
258 * @brief init proc fs
259 *
260 * @par Description:
261 * This API is used to init proc fs.
262 *
263 * @attention
264 * <ul>
265 * <li>None.</li>
266 * </ul>
267 *
268 * @param NONE
269 *
270 * @retval NONE
271 * @par Dependency:
272 * <ul><li>proc_fs.h: the header file that contains the API declaration.</li></ul>
273 * @see ProcFsInit
274 *
275 */
276extern void ProcFsInit(void);
277
278#ifdef __cplusplus
279#if __cplusplus
280}
281#endif /* __cplusplus */
282#endif /* __cplusplus */
283
284#endif /* LOSCFG_FS_PROC */
285#endif /* _PROC_FS_H */
INT64 ssize_t
Definition: los_typedef.h:79
struct ProcDirEntry * ProcCreate(const char *name, mode_t mode, struct ProcDirEntry *parent, const struct ProcFileOperations *procFops)
create a proc node
Definition: proc_file.c:457
unsigned short fmode_t
Definition: proc_fs.h:53
struct ProcDirEntry * CreateProcEntry(const char *name, mode_t mode, struct ProcDirEntry *parent)
create a proc node
Definition: proc_file.c:364
struct ProcDirEntry * ProcMkdir(const char *name, struct ProcDirEntry *parent)
create a proc directory node
Definition: proc_file.c:439
void ProcFsInit(void)
init proc fs
Definition: proc_init.c:43
void RemoveProcEntry(const char *name, struct ProcDirEntry *parent)
remove a proc node
Definition: proc_file.c:405
char buf[1]
Definition: proc_fs.h:141
ssize_t size
Definition: proc_fs.h:139
loff_t fPos
Definition: proc_fs.h:140
proc 目录/文件项, @notethinking 直接叫 ProcEntry不香吗 ? 操作 /proc的 真正结构体
Definition: proc_fs.h:101
uint uid
Definition: proc_fs.h:102
int nameLen
Definition: proc_fs.h:112
struct ProcFile * pf
proc文件指针
Definition: proc_fs.h:107
spinlock_t pdeUnloadLock
Definition: proc_fs.h:111
struct ProcDirEntry * next
Definition: proc_fs.h:108
int flags
标签
Definition: proc_fs.h:105
struct ProcDirEntry * subdir
当前目录项的关系项
Definition: proc_fs.h:108
enum VnodeType type
节点类型
Definition: proc_fs.h:115
void * data
Definition: proc_fs.h:109
struct ProcDirEntry * pdirCurrent
当前目录
Definition: proc_fs.h:113
atomic_t count
Definition: proc_fs.h:110
const struct ProcFileOperations * procFileOps
驱动程序,每个 /proc 下目录的驱动程序都不一样
Definition: proc_fs.h:106
struct ProcDirEntry * parent
Definition: proc_fs.h:108
mode_t mode
模式(读|写...)
Definition: proc_fs.h:104
char name[NAME_MAX]
Definition: proc_fs.h:114
uint gid
Definition: proc_fs.h:103
Proc文件结构体,对标 FILE 结构体
Definition: proc_fs.h:121
loff_t fPos
文件操作偏移位
Definition: proc_fs.h:128
char name[NAME_MAX]
文件名
Definition: proc_fs.h:129
spinlock_t fLock
自旋锁
Definition: proc_fs.h:123
atomic_t fCount
原子操作
Definition: proc_fs.h:124
struct SeqBuf * sbuf
序列号BUF
Definition: proc_fs.h:125
unsigned long long fVersion
版本号
Definition: proc_fs.h:127
fmode_t fMode
操作文件的模式
Definition: proc_fs.h:122
struct ProcDirEntry * pPDE
目录项
Definition: proc_fs.h:126
真正最后能操作pro file的接口,proc本质是个内存文件系统, vfs - > ProcFileOperations
Definition: proc_fs.h:89
int(* read)(struct SeqBuf *m, void *v)
Definition: proc_fs.h:94
ssize_t(* write)(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
Definition: proc_fs.h:91
int(* release)(struct Vnode *vnode, struct ProcFile *pf)
Definition: proc_fs.h:93
int(* open)(struct Vnode *vnode, struct ProcFile *pf)
Definition: proc_fs.h:92
char name[NAME_MAX]
Definition: proc_fs.h:135
struct ProcDirEntry * pPDE
Definition: proc_fs.h:134
mode_t stMode
Definition: proc_fs.h:133
vnode并不包含文件名,因为 vnode和文件名是 1:N 的关系
Definition: vnode.h:164
ARG_NUM_3 int
ARG_NUM_3 ARG_NUM_1 ARG_NUM_2 ARG_NUM_2 ARG_NUM_3 ARG_NUM_1 ARG_NUM_4 ARG_NUM_2 ARG_NUM_2 ARG_NUM_5 ARG_NUM_2 ARG_NUM_0 ARG_NUM_2 ARG_NUM_1 ARG_NUM_2 ARG_NUM_3 ARG_NUM_7 ARG_NUM_2 ARG_NUM_3 ARG_NUM_2 ARG_NUM_4 ARG_NUM_5 ARG_NUM_6 ARG_NUM_3 ARG_NUM_5 ARG_NUM_7 ARG_NUM_1 ARG_NUM_4 ARG_NUM_5 ARG_NUM_4 ARG_NUM_7 ARG_NUM_2 ARG_NUM_3 ARG_NUM_7 ARG_NUM_7 ARG_NUM_3 ARG_NUM_3 ARG_NUM_3 ARG_NUM_7 ARG_NUM_3 ARG_NUM_2 char ARG_NUM_2 ARG_NUM_1 ARG_NUM_0 ARG_NUM_0 ARG_NUM_3 void ARG_NUM_1 ARG_NUM_0 unsigned ARG_NUM_0 ARG_NUM_2 ARG_NUM_3 ARG_NUM_2 ARG_NUM_5 ARG_NUM_3 ARG_NUM_3 ARG_NUM_4 ARG_NUM_1 ARG_NUM_1 ARG_NUM_3 ARG_NUM_2 mode_t
VnodeType
Definition: vnode.h:134