• 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 {
74                 name = "(unknown)";
75             }
76 
77             if (hasPrivilege) {
78                 (void)LosBufPrintf(seqBuf, "%u\t%d\t%6d <%lu>\t%s\n", pid, fd, sysFd, filp ? filp->f_refcount : 1, name);
79             } else {
80                 (void)LosBufPrintf(seqBuf, "%u\t%d\t%s\n", pid, fd, name);
81             }
82         }
83     }
84 
85     (void)sem_post(&fdt->ft_sem);
86 }
87 
FdProcFill(struct SeqBuf * seqBuf,void * v)88 static int FdProcFill(struct SeqBuf *seqBuf, void *v)
89 {
90     int pidNum;
91     bool hasPrivilege;
92     unsigned int pidMaxNum;
93     unsigned int *pidList = NULL;
94 
95     /* privilege user */
96     if (IsCapPermit(CAP_DAC_READ_SEARCH)) {
97         pidMaxNum = LOS_GetSystemProcessMaximum();
98         pidList = (unsigned int *)malloc(pidMaxNum * sizeof(unsigned int));
99         if (pidList == NULL) {
100             return -ENOMEM;
101         }
102         pidNum = LOS_GetUsedPIDList(pidList, pidMaxNum);
103         hasPrivilege = true;
104         (void)LosBufPrintf(seqBuf, "%s\t%s\t%6s %s\t%s\n", "Pid", "Fd", "SysFd", "<ref>", "Name");
105     } else {
106         pidNum = 1;
107         pidList = (unsigned int *)malloc(pidNum * sizeof(unsigned int));
108         if (pidList == NULL) {
109             return -ENOMEM;
110         }
111         pidList[0] = LOS_GetCurrProcessID();
112         hasPrivilege = false;
113         (void)LosBufPrintf(seqBuf, "Pid\tFd\tName\n");
114     }
115 
116     struct filelist *fileList = &tg_filelist;
117     (void)sem_wait(&fileList->fl_sem);
118 
119     for (int i = 0; i < pidNum; i++) {
120         FillFdInfo(seqBuf, fileList, pidList[i], hasPrivilege);
121     }
122 
123     free(pidList);
124     (void)sem_post(&fileList->fl_sem);
125     return 0;
126 }
127 
128 static const struct ProcFileOperations FD_PROC_FOPS = {
129     .read       = FdProcFill,
130 };
131 
ProcFdInit(void)132 void ProcFdInit(void)
133 {
134     struct ProcDirEntry *pde = CreateProcEntry("fd", 0, NULL);
135     if (pde == NULL) {
136         PRINT_ERR("creat /proc/fd error.\n");
137         return;
138     }
139 
140     pde->procFileOps = &FD_PROC_FOPS;
141 }
142 
143