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

浏览源代码.

函数

static void FillFdInfo (struct SeqBuf *seqBuf, struct filelist *fileList, unsigned int pid, bool hasPrivilege)
 
static int FdProcFill (struct SeqBuf *seqBuf, void *v)
 
void ProcFdInit (void)
 

变量

static const struct ProcFileOperations FD_PROC_FOPS
 

函数说明

◆ FdProcFill()

static int FdProcFill ( struct SeqBuf seqBuf,
void v 
)
static

在文件 fd_proc.c92 行定义.

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}
BOOL IsCapPermit(UINT32 capIndex)
Definition: capability.c:43
static void FillFdInfo(struct SeqBuf *seqBuf, struct filelist *fileList, unsigned int pid, bool hasPrivilege)
Definition: fd_proc.c:49
LITE_OS_SEC_TEXT INT32 LOS_GetUsedPIDList(UINT32 *pidList, INT32 pidMaxNum)
LOS_GetUsedPIDList 获取使用中的进程列表
Definition: los_process.c:2115
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
int sem_post(sem_t *sem)
增加信号量计数
Definition: semaphore.c:139
int sem_wait(sem_t *sem)
获取信号量
Definition: semaphore.c:77
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
函数调用图:

◆ FillFdInfo()

static void FillFdInfo ( struct SeqBuf seqBuf,
struct filelist *  fileList,
unsigned int  pid,
bool  hasPrivilege 
)
static

在文件 fd_proc.c49 行定义.

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}
LITE_OS_SEC_TEXT struct fd_table_s * LOS_GetFdTable(UINT32 pid)
Definition: los_process.c:2143
进程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
函数调用图:
这是这个函数的调用关系图:

◆ ProcFdInit()

void ProcFdInit ( void  )

在文件 fd_proc.c136 行定义.

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}
static const struct ProcFileOperations FD_PROC_FOPS
Definition: fd_proc.c:132
struct ProcDirEntry * CreateProcEntry(const char *name, mode_t mode, struct ProcDirEntry *parent)
create a proc node
Definition: proc_file.c:364
proc 目录/文件项, @notethinking 直接叫 ProcEntry不香吗 ? 操作 /proc的 真正结构体
Definition: proc_fs.h:101
const struct ProcFileOperations * procFileOps
驱动程序,每个 /proc 下目录的驱动程序都不一样
Definition: proc_fs.h:106
函数调用图:
这是这个函数的调用关系图:

变量说明

◆ FD_PROC_FOPS

const struct ProcFileOperations FD_PROC_FOPS
static
初始值:
= {
.read = FdProcFill,
}
static int FdProcFill(struct SeqBuf *seqBuf, void *v)
Definition: fd_proc.c:92

在文件 fd_proc.c132 行定义.