更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
fd_proc.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "proc_fs.h"
32
33#include <stdbool.h>
34#include "fs/file.h"
35#include "vfs_config.h"
36#include "internal.h"
37
38#include "fs/fd_table.h"
39#include "los_process.h"
40#include "capability_api.h"
41#include "capability_type.h"
42// /proc 下的文件描述符适配层
43
44
45
46/*
47 * Template: Pid Fd [SysFd ] Name
48 */ //打印系统文件描述符信息
49static void FillFdInfo(struct SeqBuf *seqBuf, struct filelist *fileList, unsigned int pid, bool hasPrivilege)
50{
51 int fd;
52 int sysFd;
53 char *name = NULL;
54 struct file *filp = NULL;
55
56 struct fd_table_s *fdt = LOS_GetFdTable(pid);
57 if ((fdt == NULL) || (fdt->proc_fds == NULL)) {
58 return;
59 }
60
61 (void)sem_wait(&fdt->ft_sem);
62
63 for (fd = MIN_START_FD; fd < fdt->max_fds; fd++) {
64 if (FD_ISSET(fd, fdt->proc_fds)) {
65 sysFd = fdt->ft_fds[fd].sysFd;
66 if (sysFd < CONFIG_NFILE_DESCRIPTORS) {
67 filp = &fileList->fl_files[sysFd];
68 name = filp->f_path;
69 } else if (sysFd < (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS)) {
70 name = "(socks)";
71 } else if (sysFd < (FD_SETSIZE + CONFIG_NTIME_DESCRIPTORS)) {
72 name = "(timer)";
73 } else if (sysFd < (MQUEUE_FD_OFFSET + CONFIG_NQUEUE_DESCRIPTORS)) {
74 name = "(mqueue)";
75 } else if (sysFd < (EPOLL_FD_OFFSET + CONFIG_EPOLL_DESCRIPTORS)) {
76 name = "(epoll)";
77 } else {
78 name = "(unknown)";
79 }
80
81 if (hasPrivilege) {
82 (void)LosBufPrintf(seqBuf, "%u\t%d\t%6d <%d>\t%s\n", pid, fd, sysFd, filp ? filp->f_refcount : 1, name);
83 } else {
84 (void)LosBufPrintf(seqBuf, "%u\t%d\t%s\n", pid, fd, name);
85 }
86 }
87 }
88
89 (void)sem_post(&fdt->ft_sem);
90}
91
92static int FdProcFill(struct SeqBuf *seqBuf, void *v)
93{
94 int pidNum;
95 bool hasPrivilege;
96 unsigned int pidMaxNum;
97 unsigned int *pidList = NULL;
98
99 /* privilege user */
100 if (IsCapPermit(CAP_DAC_READ_SEARCH)) {
101 pidMaxNum = LOS_GetSystemProcessMaximum();
102 pidList = (unsigned int *)malloc(pidMaxNum * sizeof(unsigned int));
103 if (pidList == NULL) {
104 return -ENOMEM;
105 }
106 pidNum = LOS_GetUsedPIDList(pidList, pidMaxNum);
107 hasPrivilege = true;
108 (void)LosBufPrintf(seqBuf, "%s\t%s\t%6s %s\t%s\n", "Pid", "Fd", "SysFd", "<ref>", "Name");
109 } else {
110 pidNum = 1;
111 pidList = (unsigned int *)malloc(pidNum * sizeof(unsigned int));
112 if (pidList == NULL) {
113 return -ENOMEM;
114 }
115 pidList[0] = LOS_GetCurrProcessID();
116 hasPrivilege = false;
117 (void)LosBufPrintf(seqBuf, "Pid\tFd\tName\n");
118 }
119
120 struct filelist *fileList = &tg_filelist;
121 (void)sem_wait(&fileList->fl_sem);
122
123 for (int i = 0; i < pidNum; i++) {
124 FillFdInfo(seqBuf, fileList, pidList[i], hasPrivilege);
125 }
126
127 free(pidList);
128 (void)sem_post(&fileList->fl_sem);
129 return 0;
130}
131
132static const struct ProcFileOperations FD_PROC_FOPS = {
133 .read = FdProcFill,
134};
135// 初始化 /proc/fd
136void ProcFdInit(void)
137{
138 struct ProcDirEntry *pde = CreateProcEntry("fd", 0, NULL);
139 if (pde == NULL) {
140 PRINT_ERR("creat /proc/fd error.\n");
141 return;
142 }
143
145}
146
BOOL IsCapPermit(UINT32 capIndex)
Definition: capability.c:43
void ProcFdInit(void)
Definition: fd_proc.c:136
static const struct ProcFileOperations FD_PROC_FOPS
Definition: fd_proc.c:132
static void FillFdInfo(struct SeqBuf *seqBuf, struct filelist *fileList, unsigned int pid, bool hasPrivilege)
Definition: fd_proc.c:49
static int FdProcFill(struct SeqBuf *seqBuf, void *v)
Definition: fd_proc.c:92
LITE_OS_SEC_TEXT INT32 LOS_GetUsedPIDList(UINT32 *pidList, INT32 pidMaxNum)
LOS_GetUsedPIDList 获取使用中的进程列表
Definition: los_process.c:2115
LITE_OS_SEC_TEXT struct fd_table_s * LOS_GetFdTable(UINT32 pid)
Definition: los_process.c:2143
LITE_OS_SEC_TEXT UINT32 LOS_GetSystemProcessMaximum(VOID)
获取系统支持的最大进程数目
Definition: los_process.c:2239
LITE_OS_SEC_TEXT UINT32 LOS_GetCurrProcessID(VOID)
获取当前进程的进程ID
Definition: los_process.c:2161
int LosBufPrintf(struct SeqBuf *seqBuf, const char *fmt,...)
支持可变参数 写 buf
Definition: los_seq_buf.c:133
void * malloc(size_t size)
动态分配内存块大小
Definition: malloc.c:81
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
struct ProcDirEntry * CreateProcEntry(const char *name, mode_t mode, struct ProcDirEntry *parent)
create a proc node
Definition: proc_file.c:364
int sem_post(sem_t *sem)
增加信号量计数
Definition: semaphore.c:139
int sem_wait(sem_t *sem)
获取信号量
Definition: semaphore.c:77
proc 目录/文件项, @notethinking 直接叫 ProcEntry不香吗 ? 操作 /proc的 真正结构体
Definition: proc_fs.h:101
const struct ProcFileOperations * procFileOps
驱动程序,每个 /proc 下目录的驱动程序都不一样
Definition: proc_fs.h:106
真正最后能操作pro file的接口,proc本质是个内存文件系统, vfs - > ProcFileOperations
Definition: proc_fs.h:89
int(* read)(struct SeqBuf *m, void *v)
Definition: proc_fs.h:94
进程fd表结构体
Definition: fd_table.h:84
struct file_table_s * ft_fds
Definition: fd_table.h:86
unsigned int max_fds
进程的文件描述符最多有256个
Definition: fd_table.h:85
fd_set * proc_fds
进程fd管理位,用bitmap管理FD使用情况,默认打开了 0,1,2 (stdin,stdout,stderr)
Definition: fd_table.h:87
sem_t ft_sem
Definition: fd_table.h:89
intptr_t sysFd
Definition: fd_table.h:81
char * f_path
unsigned long f_refcount
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