更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
proc_file.c 文件参考

浏览源代码.

函数

 DEFINE_SPINLOCK (procfsLock)
 
int ProcMatch (unsigned int len, const char *name, struct ProcDirEntry *pn)
 
static struct ProcDirEntryProcFindNode (struct ProcDirEntry *parent, const char *name)
 
struct ProcDirEntryProcFindEntry (const char *path)
 
static int CheckProcName (const char *name, struct ProcDirEntry **parent, const char **lastName)
 检查 proc名称有效性 更多...
 
static struct ProcDirEntryProcAllocNode (struct ProcDirEntry **parent, const char *name, mode_t mode)
 分配 proc 节点 更多...
 
static int ProcAddNode (struct ProcDirEntry *parent, struct ProcDirEntry *pn)
 
static void ProcDetachNode (struct ProcDirEntry *pn)
 
static struct ProcDirEntryProcCreateDir (struct ProcDirEntry *parent, const char *name, const struct ProcFileOperations *procFileOps, mode_t mode)
 /proc文件系统创建目录的方式 更多...
 
static struct ProcDirEntryProcCreateFile (struct ProcDirEntry *parent, const char *name, const struct ProcFileOperations *procFileOps, mode_t mode)
 创建文件项 更多...
 
struct ProcDirEntryCreateProcEntry (const char *name, mode_t mode, struct ProcDirEntry *parent)
 创建 pro (目录/文件)项 更多...
 
static void FreeProcEntry (struct ProcDirEntry *entry)
 释放proc 更多...
 
void ProcFreeEntry (struct ProcDirEntry *pn)
 释放 更多...
 
static void RemoveProcEntryTravalsal (struct ProcDirEntry *pn)
 
void RemoveProcEntry (const char *name, struct ProcDirEntry *parent)
 remove a proc node 更多...
 
struct ProcDirEntryProcMkdirMode (const char *name, mode_t mode, struct ProcDirEntry *parent)
 
struct ProcDirEntryProcMkdir (const char *name, struct ProcDirEntry *parent)
 create a proc directory node 更多...
 
struct ProcDirEntryProcCreateData (const char *name, mode_t mode, struct ProcDirEntry *parent, const struct ProcFileOperations *procFileOps, void *data)
 创建数据 更多...
 
struct ProcDirEntryProcCreate (const char *name, mode_t mode, struct ProcDirEntry *parent, const struct ProcFileOperations *procFileOps)
 create a proc node 更多...
 
int ProcStat (const char *file, struct ProcStat *buf)
 
static int GetNextDir (struct ProcDirEntry *pn, void *buf, size_t len)
 
int ProcOpen (struct ProcFile *procFile)
 打开 pro 更多...
 
static int ProcRead (struct ProcDirEntry *pde, char *buf, size_t len)
 
struct ProcDirEntryOpenProcFile (const char *fileName, int flags,...)
 open a proc node 更多...
 
int ReadProcFile (struct ProcDirEntry *pde, void *buf, size_t len)
 read a proc node 更多...
 
int WriteProcFile (struct ProcDirEntry *pde, const void *buf, size_t len)
 写 proc 文件 更多...
 
loff_t LseekProcFile (struct ProcDirEntry *pde, loff_t offset, int whence)
 seek proc 文件 更多...
 
int LseekDirProcFile (struct ProcDirEntry *pde, off_t *pos, int whence)
 directory migration 更多...
 
int CloseProcFile (struct ProcDirEntry *pde)
 close a proc node 更多...
 
struct ProcDirEntryGetProcRootEntry (void)
 

变量

bool procfsInit = false
 
static struct ProcFile g_procPf
 
static struct ProcDirEntry g_procRootDirEntry
 

函数说明

◆ CheckProcName()

static int CheckProcName ( const char *  name,
struct ProcDirEntry **  parent,
const char **  lastName 
)
static

检查 proc名称有效性

在文件 proc_file.c166 行定义.

167{
168 struct ProcDirEntry *pn = *parent;
169 const char *segment = name;
170 const char *restName = NULL;
171 int length;
172
173 if (pn == NULL) {
174 pn = &g_procRootDirEntry;
175 }
176
177 spin_lock(&procfsLock);
178
179 restName = strchr(segment, '/');
180 for (; restName != NULL; restName = strchr(segment, '/')) {
181 length = restName - segment;
182 for (pn = pn->subdir; pn != NULL; pn = pn->next) {
183 if (ProcMatch(length, segment, pn)) {
184 break;
185 }
186 }
187 if (pn == NULL) {
188 PRINT_ERR(" Error!No such name '%s'\n", name);
189 spin_unlock(&procfsLock);
190 return -ENOENT;
191 }
192 segment = restName;
193 segment++;
194 }
195 *lastName = segment;
196 *parent = pn;
197 spin_unlock(&procfsLock);
198
199 return 0;
200}
spinlock_t procfsLock
int ProcMatch(unsigned int len, const char *name, struct ProcDirEntry *pn)
Definition: proc_file.c:63
static struct ProcDirEntry g_procRootDirEntry
Definition: proc_file.c:50
proc 目录/文件项, @notethinking 直接叫 ProcEntry不香吗 ? 操作 /proc的 真正结构体
Definition: proc_fs.h:101
struct ProcDirEntry * next
Definition: proc_fs.h:108
struct ProcDirEntry * subdir
当前目录项的关系项
Definition: proc_fs.h:108
struct ProcDirEntry * parent
Definition: proc_fs.h:108
char name[NAME_MAX]
Definition: proc_fs.h:114
函数调用图:
这是这个函数的调用关系图:

◆ CloseProcFile()

int CloseProcFile ( struct ProcDirEntry pde)

close a proc node

Description:
This API is used to close the node by pde
注意
  • None.
参数
pde[IN] Type #struct ProcDirEntry * pointer of the node structure to be closed
返回值
#-1close failed
#0close successfully
Dependency:
  • proc_fs.h: the header file that contains the API declaration.
参见
proc_open

在文件 proc_file.c663 行定义.

664{
665 int result = 0;
666
667 if (pde == NULL) {
668 return -EPERM;
669 }
670 pde->pf->fPos = 0;
671 atomic_set(&pde->count, 1);
672 if (S_ISDIR(pde->mode)) {
673 pde->pdirCurrent = pde->subdir;
674 }
675
676 if ((pde->procFileOps != NULL) && (pde->procFileOps->release != NULL)) {
677 result = pde->procFileOps->release((struct Vnode *)pde, pde->pf);
678 }
679 LosBufRelease(pde->pf->sbuf);
680 pde->pf->sbuf = NULL;
681
682 if (pde->parent == NULL) {
683 FreeProcEntry(pde);
684 }
685 return result;
686}
int LosBufRelease(struct SeqBuf *seqBuf)
释放 seq buf
Definition: los_seq_buf.c:145
static void FreeProcEntry(struct ProcDirEntry *entry)
释放proc
Definition: proc_file.c:376
struct ProcFile * pf
proc文件指针
Definition: proc_fs.h:107
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
mode_t mode
模式(读|写...)
Definition: proc_fs.h:104
loff_t fPos
文件操作偏移位
Definition: proc_fs.h:128
struct SeqBuf * sbuf
序列号BUF
Definition: proc_fs.h:125
int(* release)(struct Vnode *vnode, struct ProcFile *pf)
Definition: proc_fs.h:93
vnode并不包含文件名,因为 vnode和文件名是 1:N 的关系
Definition: vnode.h:164
函数调用图:
这是这个函数的调用关系图:

◆ CreateProcEntry()

struct ProcDirEntry * CreateProcEntry ( const char *  name,
mode_t  mode,
struct ProcDirEntry parent 
)

创建 pro (目录/文件)项

create a proc node

在文件 proc_file.c364 行定义.

365{
366 struct ProcDirEntry *pde = NULL;
367
368 if (S_ISDIR(mode)) {//目录模式 0
369 pde = ProcCreateDir(parent, name, NULL, mode);//无驱动程序
370 } else {
371 pde = ProcCreateFile(parent, name, NULL, mode);//无驱动程序
372 }
373 return pde;
374}
static struct ProcDirEntry * ProcCreateDir(struct ProcDirEntry *parent, const char *name, const struct ProcFileOperations *procFileOps, mode_t mode)
/proc文件系统创建目录的方式
Definition: proc_file.c:319
static struct ProcDirEntry * ProcCreateFile(struct ProcDirEntry *parent, const char *name, const struct ProcFileOperations *procFileOps, mode_t mode)
创建文件项
Definition: proc_file.c:341
函数调用图:
这是这个函数的调用关系图:

◆ DEFINE_SPINLOCK()

DEFINE_SPINLOCK ( procfsLock  )

◆ FreeProcEntry()

static void FreeProcEntry ( struct ProcDirEntry entry)
static

释放proc

在文件 proc_file.c376 行定义.

377{
378 if (entry == NULL) {
379 return;
380 }
381 if (entry->pf != NULL) {
382 free(entry->pf);
383 entry->pf = NULL;
384 }
385 free(entry);
386}
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
函数调用图:
这是这个函数的调用关系图:

◆ GetNextDir()

static int GetNextDir ( struct ProcDirEntry pn,
void buf,
size_t  len 
)
static

在文件 proc_file.c484 行定义.

485{
486 char *buff = (char *)buf;
487
488 if (pn->pdirCurrent == NULL) {
489 *buff = '\0';
490 return -ENOENT;
491 }
492 int namelen = pn->pdirCurrent->nameLen;
493 int ret = memcpy_s(buff, len, pn->pdirCurrent->name, namelen);
494 if (ret != EOK) {
495 return -ENAMETOOLONG;
496 }
497
498 pn->pdirCurrent = pn->pdirCurrent->next;
499 pn->pf->fPos++;
500 return ENOERR;
501}
int nameLen
Definition: proc_fs.h:112
这是这个函数的调用关系图:

◆ GetProcRootEntry()

struct ProcDirEntry * GetProcRootEntry ( void  )

在文件 proc_file.c688 行定义.

689{
690 return &g_procRootDirEntry;
691}
这是这个函数的调用关系图:

◆ LseekDirProcFile()

int LseekDirProcFile ( struct ProcDirEntry pde,
off_t *  pos,
int  whence 
)

directory migration

Description:
This API is used to set the proc directory migration
注意
  • Only allow SEEK_SET to zero.
参数
pde[IN] Type #struct ProcDirEntry * pointer of the node structure to be deviated
pos[IN] Type #off_t * the number of deviation
whence[IN] Type int the begin of deviation
返回值
#EINVALdeviation failed
#ENOERRdeviation successfully
Dependency:
  • proc_fs.h: the header file that contains the API declaration.
参见
proc_open

在文件 proc_file.c652 行定义.

653{
654 /* Only allow SEEK_SET to zero */
655 if ((whence != SEEK_SET) || (*pos != 0)) {
656 return EINVAL;
657 }
658 pde->pdirCurrent = pde->subdir;
659 pde->pf->fPos = 0;
660 return ENOERR;
661}

◆ LseekProcFile()

loff_t LseekProcFile ( struct ProcDirEntry pde,
loff_t  offset,
int  whence 
)

seek proc 文件

File migration

在文件 proc_file.c622 行定义.

623{
624 if (pde == NULL || pde->pf == NULL) {
625 return PROC_ERROR;
626 }
627
628 struct ProcFile *procFile = pde->pf;
629
630 loff_t result = -EINVAL;
631
632 switch (whence) {
633 case SEEK_CUR:
634 result = procFile->fPos + offset;
635 break;
636
637 case SEEK_SET:
638 result = offset;
639 break;
640
641 default:
642 break;
643 }
644
645 if (result >= 0) {
646 procFile->fPos = result;
647 }
648
649 return result;
650}
Proc文件结构体,对标 FILE 结构体
Definition: proc_fs.h:121

◆ OpenProcFile()

struct ProcDirEntry * OpenProcFile ( const char *  fileName,
int  flags,
  ... 
)

open a proc node

Description:
This API is used to open the node by 'fileName' and flags,
注意
  • None.
参数
fileName[IN] Type #const char * the fileName of the node to be opened
flags[IN] Type int the flags of open's node
返回值
#NULLopen failed
#NOTNULL open successfully
Dependency:
  • proc_fs.h: the header file that contains the API declaration.
参见
proc_close

在文件 proc_file.c553 行定义.

554{
555 unsigned int intSave;
556 struct ProcDirEntry *pn = ProcFindEntry(fileName);
557 if (pn == NULL) {
558 return NULL;
559 }
560
561 SCHEDULER_LOCK(intSave);
562 if (S_ISREG(pn->mode) && (pn->count != 1)) {
563 SCHEDULER_UNLOCK(intSave);
564 return NULL;
565 }
566
567 pn->flags = (unsigned int)(pn->flags) | (unsigned int)flags;
568 atomic_set(&pn->count, PROC_INUSE);
569 SCHEDULER_UNLOCK(intSave);
570 if (ProcOpen(pn->pf) != OK) {
571 return NULL;
572 }
573 if (S_ISREG(pn->mode) && (pn->procFileOps != NULL) && (pn->procFileOps->open != NULL)) {
574 (void)pn->procFileOps->open((struct Vnode *)pn, pn->pf);
575 }
576 if (S_ISDIR(pn->mode)) {
577 pn->pdirCurrent = pn->subdir;
578 pn->pf->fPos = 0;
579 }
580
581 return pn;
582}
int ProcOpen(struct ProcFile *procFile)
打开 pro
Definition: proc_file.c:503
struct ProcDirEntry * ProcFindEntry(const char *path)
Definition: proc_file.c:95
int flags
标签
Definition: proc_fs.h:105
int(* open)(struct Vnode *vnode, struct ProcFile *pf)
Definition: proc_fs.h:92
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 void
函数调用图:
这是这个函数的调用关系图:

◆ ProcAddNode()

static int ProcAddNode ( struct ProcDirEntry parent,
struct ProcDirEntry pn 
)
static

在文件 proc_file.c261 行定义.

262{
263 struct ProcDirEntry *temp = NULL;
264
265 if (parent == NULL) {
266 PRINT_ERR("%s(): parent is NULL", __FUNCTION__);
267 return -EINVAL;
268 }
269
270 if (pn->parent != NULL) {
271 PRINT_ERR("%s(): node already has a parent", __FUNCTION__);
272 return -EINVAL;
273 }
274
275 if (S_ISDIR(parent->mode) == 0) {
276 PRINT_ERR("%s(): parent is not a directory", __FUNCTION__);
277 return -EINVAL;
278 }
279
280 spin_lock(&procfsLock);
281
282 temp = ProcFindNode(parent, pn->name);
283 if (temp != NULL) {
284 PRINT_ERR("Error!ProcDirEntry '%s/%s' already registered\n", parent->name, pn->name);
285 spin_unlock(&procfsLock);
286 return -EEXIST;
287 }
288
289 pn->parent = parent;
290 pn->next = parent->subdir;
291 parent->subdir = pn;
292
293 spin_unlock(&procfsLock);
294
295 return 0;
296}
static struct ProcDirEntry * ProcFindNode(struct ProcDirEntry *parent, const char *name)
Definition: proc_file.c:70
函数调用图:
这是这个函数的调用关系图:

◆ ProcAllocNode()

static struct ProcDirEntry * ProcAllocNode ( struct ProcDirEntry **  parent,
const char *  name,
mode_t  mode 
)
static

分配 proc 节点

在文件 proc_file.c202 行定义.

203{
204 struct ProcDirEntry *pn = NULL;
205 const char *lastName = NULL;
206 int ret;
207
208 if ((name == NULL) || (strlen(name) == 0) || (procfsInit == false)) {
209 return pn;
210 }
211
212 if (CheckProcName(name, parent, &lastName) != 0) {
213 return pn;
214 }
215
216 if (strlen(lastName) > NAME_MAX) {
217 return pn;
218 }
219
220 if ((S_ISDIR((*parent)->mode) == 0) || (strchr(lastName, '/'))) {
221 return pn;
222 }
223 //开始各种造初始数据
224 pn = (struct ProcDirEntry *)malloc(sizeof(struct ProcDirEntry));//从内核分配内存
225 if (pn == NULL) {
226 return NULL;
227 }
228
229 if ((mode & S_IALLUGO) == 0) {
230 mode |= S_IRUSR | S_IRGRP | S_IROTH;
231 }
232
233 (void)memset_s(pn, sizeof(struct ProcDirEntry), 0, sizeof(struct ProcDirEntry));//初始化内存
234 pn->nameLen = strlen(lastName);
235 pn->mode = mode;
236 ret = memcpy_s(pn->name, sizeof(pn->name), lastName, strlen(lastName) + 1);
237 if (ret != EOK) {
238 free(pn);
239 return NULL;
240 }
241
242 pn->pf = (struct ProcFile *)malloc(sizeof(struct ProcFile));
243 if (pn->pf == NULL) {
244 free(pn);
245 return NULL;
246 }
247 (void)memset_s(pn->pf, sizeof(struct ProcFile), 0, sizeof(struct ProcFile));
248 pn->pf->pPDE = pn;
249 ret = memcpy_s(pn->pf->name, sizeof(pn->pf->name), pn->name, pn->nameLen + 1);
250 if (ret != EOK) {
251 free(pn->pf);
252 free(pn);
253 return NULL;
254 }
255
256 atomic_set(&pn->count, 1);//默认有一个引用
257 spin_lock_init(&pn->pdeUnloadLock);
258 return pn;
259}
void * malloc(size_t size)
动态分配内存块大小
Definition: malloc.c:81
static int CheckProcName(const char *name, struct ProcDirEntry **parent, const char **lastName)
检查 proc名称有效性
Definition: proc_file.c:166
bool procfsInit
Definition: proc_file.c:44
spinlock_t pdeUnloadLock
Definition: proc_fs.h:111
char name[NAME_MAX]
文件名
Definition: proc_fs.h:129
struct ProcDirEntry * pPDE
目录项
Definition: proc_fs.h:126
函数调用图:
这是这个函数的调用关系图:

◆ ProcCreate()

struct ProcDirEntry * ProcCreate ( const char *  name,
mode_t  mode,
struct ProcDirEntry parent,
const struct ProcFileOperations procFops 
)

create a proc node

Description:
This API is used to create the node by 'name' and parent vnode, And assignment operation function
注意
  • This interface should be called after system initialization.
  • The parameter name should be a valid string.
参数
name[IN] Type #const char * The name of the node to be created.
mode[IN] Type mode_t the mode of create's node.
parent[IN] Type #struct ProcDirEntry * the parent node of the node to be created.
procFops[IN] Type #const struct ProcFileOperations * operation function of the node.
返回值
#NULLCreate failed.
#ProcDirEntry*Create successfully.
Dependency:
  • proc_fs.h: the header file that contains the API declaration.
参见

在文件 proc_file.c457 行定义.

459{
460 return ProcCreateData(name, mode, parent, procFileOps, NULL);
461}
struct ProcDirEntry * ProcCreateData(const char *name, mode_t mode, struct ProcDirEntry *parent, const struct ProcFileOperations *procFileOps, void *data)
创建数据
Definition: proc_file.c:444
函数调用图:
这是这个函数的调用关系图:

◆ ProcCreateData()

struct ProcDirEntry * ProcCreateData ( const char *  name,
mode_t  mode,
struct ProcDirEntry parent,
const struct ProcFileOperations procFileOps,
void data 
)

创建数据

在文件 proc_file.c444 行定义.

446{
448 if (pde != NULL) {
449 if (procFileOps != NULL) {
451 }
452 pde->data = data;
453 }
454 return pde;
455}
struct ProcDirEntry * CreateProcEntry(const char *name, mode_t mode, struct ProcDirEntry *parent)
创建 pro (目录/文件)项
Definition: proc_file.c:364
void * data
Definition: proc_fs.h:109
函数调用图:
这是这个函数的调用关系图:

◆ ProcCreateDir()

static struct ProcDirEntry * ProcCreateDir ( struct ProcDirEntry parent,
const char *  name,
const struct ProcFileOperations procFileOps,
mode_t  mode 
)
static

/proc文件系统创建目录的方式

在文件 proc_file.c319 行定义.

321{
322 struct ProcDirEntry *pn = NULL;
323 int ret;
324
325 pn = ProcAllocNode(&parent, name, S_IFDIR | mode);//1.先创造一个数据项 ProcDirEntry
326 if (pn == NULL) {
327 return pn;
328 }
329 pn->procFileOps = procFileOps;//绑定数据项的文件系统
330 pn->type = VNODE_TYPE_DIR;
331 ret = ProcAddNode(parent, pn);
332 if (ret != 0) {
333 free(pn->pf);
334 free(pn);
335 return NULL;
336 }
337
338 return pn;
339}
static int ProcAddNode(struct ProcDirEntry *parent, struct ProcDirEntry *pn)
Definition: proc_file.c:261
static struct ProcDirEntry * ProcAllocNode(struct ProcDirEntry **parent, const char *name, mode_t mode)
分配 proc 节点
Definition: proc_file.c:202
enum VnodeType type
节点类型
Definition: proc_fs.h:115
@ VNODE_TYPE_DIR
Definition: vnode.h:137
函数调用图:
这是这个函数的调用关系图:

◆ ProcCreateFile()

static struct ProcDirEntry * ProcCreateFile ( struct ProcDirEntry parent,
const char *  name,
const struct ProcFileOperations procFileOps,
mode_t  mode 
)
static

创建文件项

在文件 proc_file.c341 行定义.

343{
344 struct ProcDirEntry *pn = NULL;
345 int ret;
346
347 pn = ProcAllocNode(&parent, name, S_IFREG | mode);//分配一个节点,S_IFREG代表是文件类型
348 if (pn == NULL) {
349 return pn;
350 }
351
352 pn->procFileOps = procFileOps;//驱动程序
353 pn->type = VNODE_TYPE_REG; //文件类型
354 ret = ProcAddNode(parent, pn);
355 if (ret != 0) {
356 free(pn->pf);
357 free(pn);
358 return NULL;
359 }
360
361 return pn;
362}
@ VNODE_TYPE_REG
Definition: vnode.h:136
函数调用图:
这是这个函数的调用关系图:

◆ ProcDetachNode()

static void ProcDetachNode ( struct ProcDirEntry pn)
static

在文件 proc_file.c298 行定义.

299{
300 struct ProcDirEntry *parent = pn->parent;
301 struct ProcDirEntry **iter = NULL;
302
303 if (parent == NULL) {
304 PRINT_ERR("%s(): node has no parent", __FUNCTION__);
305 return;
306 }
307
308 iter = &parent->subdir;
309 while (*iter != NULL) {
310 if (*iter == pn) {
311 *iter = pn->next;
312 break;
313 }
314 iter = &(*iter)->next;
315 }
316 pn->parent = NULL;
317}
这是这个函数的调用关系图:

◆ ProcFindEntry()

struct ProcDirEntry * ProcFindEntry ( const char *  path)

在文件 proc_file.c95 行定义.

96{
97 struct ProcDirEntry *pn = NULL;
98 int isfoundsub;
99 const char *next = NULL;
100 unsigned int len;
101 int leveltotal = 0;
102 int levelcount = 0;
103 const char *p = NULL;
104 const char *name = path;
105
106 while ((p = strchr(name, '/')) != NULL) {
107 leveltotal++;
108 name = p;
109 name++;
110 }
111 if (leveltotal < 1) {
112 return pn;
113 }
114
115 spin_lock(&procfsLock);
116
117 pn = &g_procRootDirEntry;
118
119 while ((pn != NULL) && (levelcount < leveltotal)) {
120 levelcount++;
121 isfoundsub = 0;
122 while (pn != NULL) {
123 next = strchr(path, '/');
124 if (next == NULL) {
125 while (pn != NULL) {
126 if (strcmp(path, pn->name) == 0) {
127 spin_unlock(&procfsLock);
128 return pn;
129 }
130 pn = pn->next;
131 }
132 pn = NULL;
133 spin_unlock(&procfsLock);
134 return pn;
135 }
136
137 len = next - path;
138 if (pn == &g_procRootDirEntry) {
139 if (levelcount == leveltotal) {
140 spin_unlock(&procfsLock);
141 return pn;
142 }
144 }
145 if (ProcMatch(len, path, pn)) {
146 isfoundsub = 1;
147 path += len + 1;
148 break;
149 }
150
151 pn = pn->next;
152 }
153
154 if ((isfoundsub == 1) && (pn != NULL)) {
155 pn = pn->subdir;
156 } else {
157 pn = NULL;
158 spin_unlock(&procfsLock);
159 return pn;
160 }
161 }
162 spin_unlock(&procfsLock);
163 return NULL;
164}
函数调用图:
这是这个函数的调用关系图:

◆ ProcFindNode()

static struct ProcDirEntry * ProcFindNode ( struct ProcDirEntry parent,
const char *  name 
)
static

在文件 proc_file.c70 行定义.

71{
72 struct ProcDirEntry *pn = NULL;
73 int length;
74
75 if ((parent == NULL) || (name == NULL)) {
76 return pn;
77 }
78 length = strlen(name);
79
80 for (pn = parent->subdir; pn != NULL; pn = pn->next) {
81 if ((length == pn->nameLen) && strcmp(pn->name, name) == 0) {
82 break;
83 }
84 }
85
86 return pn;
87}
这是这个函数的调用关系图:

◆ ProcFreeEntry()

void ProcFreeEntry ( struct ProcDirEntry pn)

释放

在文件 proc_file.c388 行定义.

389{
390 if (atomic_dec_and_test(&pn->count))
391 FreeProcEntry(pn);
392}
函数调用图:
这是这个函数的调用关系图:

◆ ProcMatch()

int ProcMatch ( unsigned int  len,
const char *  name,
struct ProcDirEntry pn 
)

在文件 proc_file.c63 行定义.

64{
65 if (len != pn->nameLen)
66 return 0;
67 return !strncmp(name, pn->name, len);
68}
这是这个函数的调用关系图:

◆ ProcMkdir()

struct ProcDirEntry * ProcMkdir ( const char *  name,
struct ProcDirEntry parent 
)

create a proc directory node

Description:
This API is used to create the directory node by 'name' and parent vnode
注意
  • This interface should be called after system initialization.
  • The parameter name should be a valid string.
参数
name[IN] Type #const char * The name of the node directory to be created.
parent[IN] Type #struct ProcDirEntry * the parent node of the directory node to be created, if pass NULL, default parent node is "/proc".
返回值
#NULLCreate failed.
#ProcDirEntry*Create successfully.
Dependency:
  • proc_fs.h: the header file that contains the API declaration.
参见

在文件 proc_file.c439 行定义.

440{
441 return ProcCreateDir(parent, name, NULL, 0);
442}
函数调用图:

◆ ProcMkdirMode()

struct ProcDirEntry * ProcMkdirMode ( const char *  name,
mode_t  mode,
struct ProcDirEntry parent 
)

在文件 proc_file.c434 行定义.

435{
436 return ProcCreateDir(parent, name, NULL, mode);
437}
函数调用图:

◆ ProcOpen()

int ProcOpen ( struct ProcFile procFile)

打开 pro

在文件 proc_file.c503 行定义.

504{
505 if (procFile == NULL) {
506 return PROC_ERROR;
507 }
508 if (procFile->sbuf != NULL) {
509 return OK;
510 }
511
512 struct SeqBuf *buf = LosBufCreat();//创建一个 seq buf
513 if (buf == NULL) {
514 return PROC_ERROR;
515 }
516 procFile->sbuf = buf;
517 return OK;
518}
struct SeqBuf * LosBufCreat(void)
创建seq buf
Definition: los_seq_buf.c:76
char * buf
Definition: los_seq_buf.h:49
函数调用图:
这是这个函数的调用关系图:

◆ ProcRead()

static int ProcRead ( struct ProcDirEntry pde,
char *  buf,
size_t  len 
)
static

在文件 proc_file.c520 行定义.

521{
522 if (pde == NULL || pde->pf == NULL) {
523 return PROC_ERROR;
524 }
525 struct ProcFile *procFile = pde->pf;
526 struct SeqBuf *sb = procFile->sbuf;
527
528 if (sb->buf == NULL) {
529 // only read once to build the storage buffer
530 if (pde->procFileOps->read(sb, NULL) != 0) {
531 return PROC_ERROR;
532 }
533 }
534
535 size_t realLen;
536 loff_t pos = procFile->fPos;
537
538 if ((pos >= sb->count) || (len == 0)) {
539 /* there's no data or at the file tail. */
540 realLen = 0;
541 } else {
542 realLen = MIN((sb->count - pos), MIN(len, INT_MAX));
543 if (LOS_CopyFromKernel(buf, len, sb->buf + pos, realLen) != 0) {
544 return PROC_ERROR;
545 }
546
547 procFile->fPos = pos + realLen;
548 }
549
550 return (ssize_t)realLen;
551}
INT64 ssize_t
Definition: los_typedef.h:79
int(* read)(struct SeqBuf *m, void *v)
Definition: proc_fs.h:94
size_t count
Definition: los_seq_buf.h:51
INT32 LOS_CopyFromKernel(VOID *dest, UINT32 max, const VOID *src, UINT32 count)
将内核数据拷贝到用户空间
Definition: user_copy.c:88
函数调用图:
这是这个函数的调用关系图:

◆ ProcStat()

int ProcStat ( const char *  file,
struct ProcStat buf 
)

在文件 proc_file.c463 行定义.

464{
465 struct ProcDirEntry *pn = NULL;
466 int len = sizeof(buf->name);
467 int ret;
468
469 pn = ProcFindEntry(file);
470 if (pn == NULL) {
471 return ENOENT;
472 }
473 ret = strncpy_s(buf->name, len, pn->name, len - 1);
474 if (ret != EOK) {
475 return ENAMETOOLONG;
476 }
477 buf->name[len - 1] = '\0';
478 buf->stMode = pn->mode;
479 buf->pPDE = pn;
480
481 return 0;
482}
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
函数调用图:

◆ ReadProcFile()

int ReadProcFile ( struct ProcDirEntry pde,
void buf,
size_t  len 
)

read a proc node

Description:
This API is used to read the node by pde
注意
  • None.
参数
pde[IN] Type #struct ProcDirEntry * pointer of the node structure to be read
buf[IN] Type void * user-provided to save the data
len[IN] Type size_t the length of want to read
返回值
#-1read failed
#>0Number of bytes read success
Dependency:
  • proc_fs.h: the header file that contains the API declaration.
参见
proc_open

在文件 proc_file.c584 行定义.

585{
586 int result = -EPERM;
587
588 if (pde == NULL) {
589 return result;
590 }
591 if (S_ISREG(pde->mode)) {
592 if ((pde->procFileOps != NULL) && (pde->procFileOps->read != NULL)) {
593 result = ProcRead(pde, (char *)buf, len);
594 }
595 } else if (S_ISDIR(pde->mode)) {
596 result = GetNextDir(pde, buf, len);
597 }
598 return result;
599}
static int GetNextDir(struct ProcDirEntry *pn, void *buf, size_t len)
Definition: proc_file.c:484
static int ProcRead(struct ProcDirEntry *pde, char *buf, size_t len)
Definition: proc_file.c:520
函数调用图:
这是这个函数的调用关系图:

◆ RemoveProcEntry()

void RemoveProcEntry ( const char *  name,
struct ProcDirEntry parent 
)

remove a proc node

Description:
This API is used to remove the node by 'name' and parent vnode
注意
  • This interface should be called after system initialization.
  • The parameter name should be a valid string.
参数
name[IN] Type #const char * The name of the node to be removed.
parent[IN] Type #struct ProcDirEntry * the parent node of the node to be remove.
Dependency:
  • proc_fs.h: the header file that contains the API declaration.
参见

在文件 proc_file.c405 行定义.

406{
407 struct ProcDirEntry *pn = NULL;
408 const char *lastName = name;
409
410 if ((name == NULL) || (strlen(name) == 0) || (procfsInit == false)) {
411 return;
412 }
413
414 if (CheckProcName(name, &parent, &lastName) != 0) {
415 return;
416 }
417
418 spin_lock(&procfsLock);
419
420 pn = ProcFindNode(parent, lastName);
421 if (pn == NULL) {
422 PRINT_ERR("Error:name '%s' not found!\n", name);
423 spin_unlock(&procfsLock);
424 return;
425 }
426 ProcDetachNode(pn);
427
428 spin_unlock(&procfsLock);
429
431 ProcFreeEntry(pn);
432}
static void ProcDetachNode(struct ProcDirEntry *pn)
Definition: proc_file.c:298
static void RemoveProcEntryTravalsal(struct ProcDirEntry *pn)
Definition: proc_file.c:394
void ProcFreeEntry(struct ProcDirEntry *pn)
释放
Definition: proc_file.c:388
函数调用图:

◆ RemoveProcEntryTravalsal()

static void RemoveProcEntryTravalsal ( struct ProcDirEntry pn)
static

在文件 proc_file.c394 行定义.

395{
396 if (pn == NULL) {
397 return;
398 }
401
402 ProcFreeEntry(pn);
403}
函数调用图:
这是这个函数的调用关系图:

◆ WriteProcFile()

int WriteProcFile ( struct ProcDirEntry pde,
const void buf,
size_t  len 
)

写 proc 文件

write a proc node

在文件 proc_file.c601 行定义.

602{
603 int result = -EPERM;
604
605 if (pde == NULL) {
606 return result;
607 }
608
609 if (S_ISDIR(pde->mode)) {
610 return -EISDIR;
611 }
612
613 spin_lock(&procfsLock);
614 if ((pde->procFileOps != NULL) && (pde->procFileOps->write != NULL)) {
615 result = pde->procFileOps->write(pde->pf, (const char *)buf, len, &(pde->pf->fPos));
616 }
617 spin_unlock(&procfsLock);
618
619 return result;
620}
ssize_t(* write)(struct ProcFile *pf, const char *buf, size_t count, loff_t *ppos)
Definition: proc_fs.h:91
这是这个函数的调用关系图:

变量说明

◆ g_procPf

struct ProcFile g_procPf
static
初始值:
= {
.fPos = 0,
}

在文件 proc_file.c46 行定义.

◆ g_procRootDirEntry

struct ProcDirEntry g_procRootDirEntry
static
初始值:
= {
.nameLen = 5,
.mode = S_IFDIR | PROCFS_DEFAULT_MODE,
.count = ATOMIC_INIT(1),
.procFileOps = NULL,
.parent = &g_procRootDirEntry,
.name = "/proc",
.subdir = NULL,
.next = NULL,
.pf = &g_procPf,
.type = VNODE_TYPE_DIR,
}
static struct ProcFile g_procPf
Definition: proc_file.c:46

在文件 proc_file.c50 行定义.

◆ procfsInit

bool procfsInit = false

在文件 proc_file.c44 行定义.