10d163575Sopenharmony_ci/* 20d163575Sopenharmony_ci * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. 30d163575Sopenharmony_ci * 40d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification, 50d163575Sopenharmony_ci * are permitted provided that the following conditions are met: 60d163575Sopenharmony_ci * 70d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of 80d163575Sopenharmony_ci * conditions and the following disclaimer. 90d163575Sopenharmony_ci * 100d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list 110d163575Sopenharmony_ci * of conditions and the following disclaimer in the documentation and/or other materials 120d163575Sopenharmony_ci * provided with the distribution. 130d163575Sopenharmony_ci * 140d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used 150d163575Sopenharmony_ci * to endorse or promote products derived from this software without specific prior written 160d163575Sopenharmony_ci * permission. 170d163575Sopenharmony_ci * 180d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 190d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 200d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 210d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 220d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 230d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 240d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 250d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 260d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 270d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 280d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 290d163575Sopenharmony_ci */ 300d163575Sopenharmony_ci 310d163575Sopenharmony_ci#include "proc_fs.h" 320d163575Sopenharmony_ci 330d163575Sopenharmony_ci#include <stdbool.h> 340d163575Sopenharmony_ci#include "fs/file.h" 350d163575Sopenharmony_ci#include "vfs_config.h" 360d163575Sopenharmony_ci#include "internal.h" 370d163575Sopenharmony_ci 380d163575Sopenharmony_ci#include "fs/fd_table.h" 390d163575Sopenharmony_ci#include "los_process.h" 400d163575Sopenharmony_ci#include "capability_api.h" 410d163575Sopenharmony_ci#include "capability_type.h" 420d163575Sopenharmony_ci 430d163575Sopenharmony_ci 440d163575Sopenharmony_ci/* 450d163575Sopenharmony_ci * Template: Pid Fd [SysFd ] Name 460d163575Sopenharmony_ci */ 470d163575Sopenharmony_cistatic void FillFdInfo(struct SeqBuf *seqBuf, struct filelist *fileList, unsigned int pid, bool hasPrivilege) 480d163575Sopenharmony_ci{ 490d163575Sopenharmony_ci int fd; 500d163575Sopenharmony_ci int sysFd; 510d163575Sopenharmony_ci char *name = NULL; 520d163575Sopenharmony_ci struct file *filp = NULL; 530d163575Sopenharmony_ci 540d163575Sopenharmony_ci struct fd_table_s *fdt = LOS_GetFdTable(pid); 550d163575Sopenharmony_ci if ((fdt == NULL) || (fdt->proc_fds == NULL)) { 560d163575Sopenharmony_ci return; 570d163575Sopenharmony_ci } 580d163575Sopenharmony_ci 590d163575Sopenharmony_ci (void)sem_wait(&fdt->ft_sem); 600d163575Sopenharmony_ci 610d163575Sopenharmony_ci for (fd = MIN_START_FD; fd < fdt->max_fds; fd++) { 620d163575Sopenharmony_ci if (FD_ISSET(fd, fdt->proc_fds)) { 630d163575Sopenharmony_ci sysFd = fdt->ft_fds[fd].sysFd; 640d163575Sopenharmony_ci if (sysFd < CONFIG_NFILE_DESCRIPTORS) { 650d163575Sopenharmony_ci filp = &fileList->fl_files[sysFd]; 660d163575Sopenharmony_ci name = filp->f_path; 670d163575Sopenharmony_ci } else if (sysFd < (CONFIG_NFILE_DESCRIPTORS + CONFIG_NSOCKET_DESCRIPTORS)) { 680d163575Sopenharmony_ci name = "(socks)"; 690d163575Sopenharmony_ci } else if (sysFd < (FD_SETSIZE + CONFIG_NTIME_DESCRIPTORS)) { 700d163575Sopenharmony_ci name = "(timer)"; 710d163575Sopenharmony_ci } else if (sysFd < (MQUEUE_FD_OFFSET + CONFIG_NQUEUE_DESCRIPTORS)) { 720d163575Sopenharmony_ci name = "(mqueue)"; 730d163575Sopenharmony_ci } else if (sysFd < (EPOLL_FD_OFFSET + CONFIG_EPOLL_DESCRIPTORS)) { 740d163575Sopenharmony_ci name = "(epoll)"; 750d163575Sopenharmony_ci } else { 760d163575Sopenharmony_ci name = "(unknown)"; 770d163575Sopenharmony_ci } 780d163575Sopenharmony_ci 790d163575Sopenharmony_ci if (hasPrivilege) { 800d163575Sopenharmony_ci (void)LosBufPrintf(seqBuf, "%u\t%d\t%6d <%d>\t%s\n", pid, fd, sysFd, filp ? filp->f_refcount : 1, name); 810d163575Sopenharmony_ci } else { 820d163575Sopenharmony_ci (void)LosBufPrintf(seqBuf, "%u\t%d\t%s\n", pid, fd, name); 830d163575Sopenharmony_ci } 840d163575Sopenharmony_ci } 850d163575Sopenharmony_ci } 860d163575Sopenharmony_ci 870d163575Sopenharmony_ci (void)sem_post(&fdt->ft_sem); 880d163575Sopenharmony_ci} 890d163575Sopenharmony_ci 900d163575Sopenharmony_cistatic int FdProcFill(struct SeqBuf *seqBuf, void *v) 910d163575Sopenharmony_ci{ 920d163575Sopenharmony_ci int pidNum; 930d163575Sopenharmony_ci bool hasPrivilege; 940d163575Sopenharmony_ci unsigned int pidMaxNum; 950d163575Sopenharmony_ci unsigned int *pidList = NULL; 960d163575Sopenharmony_ci 970d163575Sopenharmony_ci /* privilege user */ 980d163575Sopenharmony_ci if (IsCapPermit(CAP_DAC_READ_SEARCH)) { 990d163575Sopenharmony_ci pidMaxNum = LOS_GetSystemProcessMaximum(); 1000d163575Sopenharmony_ci pidList = (unsigned int *)malloc(pidMaxNum * sizeof(unsigned int)); 1010d163575Sopenharmony_ci if (pidList == NULL) { 1020d163575Sopenharmony_ci return -ENOMEM; 1030d163575Sopenharmony_ci } 1040d163575Sopenharmony_ci pidNum = LOS_GetUsedPIDList(pidList, pidMaxNum); 1050d163575Sopenharmony_ci hasPrivilege = true; 1060d163575Sopenharmony_ci (void)LosBufPrintf(seqBuf, "%s\t%s\t%6s %s\t%s\n", "Pid", "Fd", "SysFd", "<ref>", "Name"); 1070d163575Sopenharmony_ci } else { 1080d163575Sopenharmony_ci pidNum = 1; 1090d163575Sopenharmony_ci pidList = (unsigned int *)malloc(pidNum * sizeof(unsigned int)); 1100d163575Sopenharmony_ci if (pidList == NULL) { 1110d163575Sopenharmony_ci return -ENOMEM; 1120d163575Sopenharmony_ci } 1130d163575Sopenharmony_ci pidList[0] = LOS_GetCurrProcessID(); 1140d163575Sopenharmony_ci hasPrivilege = false; 1150d163575Sopenharmony_ci (void)LosBufPrintf(seqBuf, "Pid\tFd\tName\n"); 1160d163575Sopenharmony_ci } 1170d163575Sopenharmony_ci 1180d163575Sopenharmony_ci struct filelist *fileList = &tg_filelist; 1190d163575Sopenharmony_ci (void)sem_wait(&fileList->fl_sem); 1200d163575Sopenharmony_ci 1210d163575Sopenharmony_ci for (int i = 0; i < pidNum; i++) { 1220d163575Sopenharmony_ci FillFdInfo(seqBuf, fileList, pidList[i], hasPrivilege); 1230d163575Sopenharmony_ci } 1240d163575Sopenharmony_ci 1250d163575Sopenharmony_ci free(pidList); 1260d163575Sopenharmony_ci (void)sem_post(&fileList->fl_sem); 1270d163575Sopenharmony_ci return 0; 1280d163575Sopenharmony_ci} 1290d163575Sopenharmony_ci 1300d163575Sopenharmony_cistatic const struct ProcFileOperations FD_PROC_FOPS = { 1310d163575Sopenharmony_ci .read = FdProcFill, 1320d163575Sopenharmony_ci}; 1330d163575Sopenharmony_ci 1340d163575Sopenharmony_civoid ProcFdInit(void) 1350d163575Sopenharmony_ci{ 1360d163575Sopenharmony_ci struct ProcDirEntry *pde = CreateProcEntry("fd", 0, NULL); 1370d163575Sopenharmony_ci if (pde == NULL) { 1380d163575Sopenharmony_ci PRINT_ERR("create /proc/fd error.\n"); 1390d163575Sopenharmony_ci return; 1400d163575Sopenharmony_ci } 1410d163575Sopenharmony_ci 1420d163575Sopenharmony_ci pde->procFileOps = &FD_PROC_FOPS; 1430d163575Sopenharmony_ci} 1440d163575Sopenharmony_ci 145