• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
43 
44 /*
45  * Template: Pid    Fd  [SysFd ] Name
46  */
FillFdInfo(struct SeqBuf * seqBuf,struct filelist * fileList,unsigned int pid,bool hasPrivilege)47 static void FillFdInfo(struct SeqBuf *seqBuf, struct filelist *fileList, unsigned int pid, bool hasPrivilege)
48 {
49     int fd;
50     int sysFd;
51     char *name = NULL;
52     struct file *filp = NULL;
53 
54     struct fd_table_s *fdt = LOS_GetFdTable(pid);
55     if ((fdt == NULL) || (fdt->proc_fds == NULL)) {
56         return;
57     }
58 
59     (void)sem_wait(&fdt->ft_sem);
60 
61     for (fd = MIN_START_FD; fd < fdt->max_fds; fd++) {
62         if (FD_ISSET(fd, fdt->proc_fds)) {
63             sysFd = fdt->ft_fds[fd].sysFd;
64             if (sysFd < CONFIG_NFILE_DESCRIPTORS) {
65                 filp = &fileList->fl_files[sysFd];
66                 name = filp->f_path;
67             } else if (sysFd < (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS)) {
68                 name = "(socks)";
69             } else if (sysFd < (FD_SETSIZE + CONFIG_NTIME_DESCRIPTORS)) {
70                 name = "(timer)";
71             } else if (sysFd < (MQUEUE_FD_OFFSET + CONFIG_NQUEUE_DESCRIPTORS)) {
72                 name = "(mqueue)";
73             } else if (sysFd < (EPOLL_FD_OFFSET + CONFIG_EPOLL_DESCRIPTORS)) {
74                 name = "(epoll)";
75             } else {
76                 name = "(unknown)";
77             }
78 
79             if (hasPrivilege) {
80                 (void)LosBufPrintf(seqBuf, "%u\t%d\t%6d <%d>\t%s\n", pid, fd, sysFd, filp ? filp->f_refcount : 1, name);
81             } else {
82                 (void)LosBufPrintf(seqBuf, "%u\t%d\t%s\n", pid, fd, name);
83             }
84         }
85     }
86 
87     (void)sem_post(&fdt->ft_sem);
88 }
89 
FdProcFill(struct SeqBuf * seqBuf,void * v)90 static int FdProcFill(struct SeqBuf *seqBuf, void *v)
91 {
92     int pidNum;
93     bool hasPrivilege;
94     unsigned int pidMaxNum;
95     unsigned int *pidList = NULL;
96 
97     /* privilege user */
98     if (IsCapPermit(CAP_DAC_READ_SEARCH)) {
99         pidMaxNum = LOS_GetSystemProcessMaximum();
100         pidList = (unsigned int *)malloc(pidMaxNum * sizeof(unsigned int));
101         if (pidList == NULL) {
102             return -ENOMEM;
103         }
104         pidNum = LOS_GetUsedPIDList(pidList, pidMaxNum);
105         hasPrivilege = true;
106         (void)LosBufPrintf(seqBuf, "%s\t%s\t%6s %s\t%s\n", "Pid", "Fd", "SysFd", "<ref>", "Name");
107     } else {
108         pidNum = 1;
109         pidList = (unsigned int *)malloc(pidNum * sizeof(unsigned int));
110         if (pidList == NULL) {
111             return -ENOMEM;
112         }
113         pidList[0] = LOS_GetCurrProcessID();
114         hasPrivilege = false;
115         (void)LosBufPrintf(seqBuf, "Pid\tFd\tName\n");
116     }
117 
118     struct filelist *fileList = &tg_filelist;
119     (void)sem_wait(&fileList->fl_sem);
120 
121     for (int i = 0; i < pidNum; i++) {
122         FillFdInfo(seqBuf, fileList, pidList[i], hasPrivilege);
123     }
124 
125     free(pidList);
126     (void)sem_post(&fileList->fl_sem);
127     return 0;
128 }
129 
130 static const struct ProcFileOperations FD_PROC_FOPS = {
131     .read       = FdProcFill,
132 };
133 
ProcFdInit(void)134 void ProcFdInit(void)
135 {
136     struct ProcDirEntry *pde = CreateProcEntry("fd", 0, NULL);
137     if (pde == NULL) {
138         PRINT_ERR("create /proc/fd error.\n");
139         return;
140     }
141 
142     pde->procFileOps = &FD_PROC_FOPS;
143 }
144 
145