更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
proc_shellcmd.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <unistd.h>
36#include <fcntl.h>
37#include <sys/statfs.h>
38#include <errno.h>
39
40#include "los_config.h"
41
42#if defined(LOSCFG_SHELL_CMD_DEBUG) && defined(LOSCFG_FS_PROC)
43#include "los_typedef.h"
44#include "shell.h"
45#include "shcmd.h"
46#include "proc_file.h"
47#include "dirent.h"
48#include "proc_fs.h"
49
50#define WRITEPROC_ARGC 3
51
52/**
53 * @file fb_table.h
54 * @verbatim
55 鸿蒙内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构、改变内核设置的机制。
56 proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间。它以文件系统的方式为
57 访问系统内核数据的操作提供接口。
58 用户和应用程序可以通过proc得到系统的信息,并可以改变内核的某些参数。由于系统的信息,
59 如进程,是动态改变的,所以用户或应用程序读取proc文件时,proc文件系统是动态从系统内核读出
60 所需信息并提交的。
61
62 proc fs支持传入字符串参数,需要每个文件实现自己的写方法。
63 命令格式
64 writeproc <data> >> /proc/<filename>
65
66 参数说明
67 参数 参数说明
68 data 要输入的字符串,以空格为结束符,如需输入空格,请用""包裹。
69 filename data要传入的proc文件。
70
71 proc文件实现自身的write函数,调用writeproc命令后会将入参传入write函数。
72 注意:procfs暂不支持多线程访问
73
74 OHOS # writeproc test >> /proc/uptime
75
76 [INFO]write buf is: test
77 test >> /proc/uptime
78 说明: uptime proc文件临时实现write函数,INFO日志为实现的测试函数打印的日志。
79 * @endverbatim
80 * @brief
81 */
82int OsShellCmdWriteProc(int argc, char **argv)
83{
84 int i;
85 int ret;
86 const char *path = NULL;
87 const char *value = NULL;
88 unsigned int len;
89 struct ProcDirEntry *handle = NULL;
90 char realPath[PATH_MAX] = {'\0'};
91 const char *rootProcDir = "/proc/";
92
93 if (argc == WRITEPROC_ARGC) {//argv[0] = ">>"
94 value = argv[0];//test
95 path = argv[2];///proc/uptime
96 len = strlen(value) + 1; /* +1:add the \0 */
97 if (strncmp(argv[1], ">>", strlen(">>")) == 0) { // 第二个参数必须得是 >>
98 if ((realpath(path, realPath) == NULL) || (strncmp(realPath, rootProcDir, strlen(rootProcDir)) != 0)) {
99 PRINT_ERR("No such file or directory\n");
100 return PROC_ERROR;
101 }
102
103 handle = OpenProcFile(realPath, O_TRUNC);//打开 proc 文件
104 if (handle == NULL) {
105 PRINT_ERR("No such file or directory\n");
106 return PROC_ERROR;
107 }
108
109 ret = WriteProcFile(handle, value, len);
110 if (ret < 0) {
111 (void)CloseProcFile(handle);
112 PRINT_ERR("write error\n");
113 return PROC_ERROR;
114 }
115 for (i = 0; i < argc; i++) {
116 PRINTK("%s%s", i > 0 ? " " : "", argv[i]);
117 }
118 PRINTK("\n");
119 (void)CloseProcFile(handle);
120 return LOS_OK;
121 }
122 }
123 PRINT_ERR("writeproc [data] [>>] [path]\n");
124 return PROC_ERROR;
125}
126
127SHELLCMD_ENTRY(writeproc_shellcmd, CMD_TYPE_EX, "writeproc", XARGS, (CmdCallBackFunc)OsShellCmdWriteProc);
128#endif
@ CMD_TYPE_EX
不支持标准命令参数输入,会把用户填写的命令关键字屏蔽掉,例如:输入ls /ramfs,传入给注册函数的参数只有/ramfs,而ls命令关键字并不会被传入。
Definition: shell.h:91
int CloseProcFile(struct ProcDirEntry *pde)
close a proc node
Definition: proc_file.c:663
struct ProcDirEntry * OpenProcFile(const char *fileName, int flags,...)
open a proc node
Definition: proc_file.c:553
int WriteProcFile(struct ProcDirEntry *pde, const void *buf, size_t len)
write a proc node
Definition: proc_file.c:601
int OsShellCmdWriteProc(int argc, char **argv)
Definition: proc_shellcmd.c:82
SHELLCMD_ENTRY(writeproc_shellcmd, CMD_TYPE_EX, "writeproc", XARGS,(CmdCallBackFunc) OsShellCmdWriteProc)
proc 目录/文件项, @notethinking 直接叫 ProcEntry不香吗 ? 操作 /proc的 真正结构体
Definition: proc_fs.h:101
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
u32_t(* CmdCallBackFunc)(u32_t argc, const char **argv)
Definition: types_adapt.h:86
char * realpath(const char *path, char *resolved_path)
Definition: vfs_other.c:651