1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <grp.h>
16 #include <pwd.h>
17 #include <sys/ioctl.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <sys/wait.h>
21 #include <termios.h>
22 #include <unistd.h>
23 #include <signal.h>
24 #undef _GNU_SOURCE
25 #define _GNU_SOURCE
26 #include <sched.h>
27
28 #include "begetctl.h"
29 #include "param_manager.h"
30 #include "param_security.h"
31 #include "shell_utils.h"
32 #include "init_param.h"
33 #include "beget_ext.h"
34 #ifdef PARAM_SUPPORT_SELINUX
35 #include <policycoreutils.h>
36 #include <selinux/selinux.h>
37 #include "selinux_parameter.h"
38 #endif // PARAM_SUPPORT_SELINUX
39
40 typedef struct {
41 uid_t uid;
42 gid_t gid;
43 int cloneFlg;
44 char *parameter;
45 } ParamShellExecArgs;
46
47 #define STACK_SIZE (1024 * 1024 * 8)
48 #define MASK_LENGTH_MAX 4
49 pid_t g_shellPid = 0;
50 static struct termios g_terminalState;
51 char g_isSetTerminal = 0;
52
demoExit(void)53 void demoExit(void)
54 {
55 BShellEnvDestory(GetShellHandle());
56 if (g_shellPid != 0) {
57 #ifndef STARTUP_INIT_TEST
58 kill(g_shellPid, SIGKILL);
59 #endif
60 }
61 BEGET_CHECK(g_isSetTerminal == 0, tcsetattr(0, TCSAFLUSH, &g_terminalState));
62 }
63
64 #define CMD_PATH "/system/bin/paramshell"
65 #define SHELL_NAME "paramshell"
66 #ifndef TIOCSCTTY
67 #define TIOCSCTTY 0x540E
68 #endif
GetLocalBuffer(uint32_t * buffSize)69 static char *GetLocalBuffer(uint32_t *buffSize)
70 {
71 static char buffer[PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX] = {0};
72 BEGET_CHECK(buffSize == NULL, *buffSize = sizeof(buffer));
73 return buffer;
74 }
75
GetRealParameter(BShellHandle shell,const char * name,char * buffer,uint32_t buffSize)76 static char *GetRealParameter(BShellHandle shell, const char *name, char *buffer, uint32_t buffSize)
77 {
78 BSH_CHECK(buffer != NULL && name != NULL, return NULL, "Invalid parameter");
79 const BShellParam *param = BShellEnvGetParam(shell, PARAM_REVERESD_NAME_CURR_PARAMETER);
80 const char *current = (param == NULL) ? "" : param->value.string;
81 int32_t realLen = 0;
82 int ret = 0;
83 if (name[0] == '.') { // relatively
84 if (strcmp(name, "..") == 0) {
85 char *tmp = strrchr(current, '.');
86 if (tmp != NULL) {
87 realLen = tmp - current;
88 ret = memcpy_s(buffer, buffSize, current, realLen);
89 } else {
90 ret = memcpy_s(buffer, buffSize, "#", 1);
91 realLen = 1;
92 }
93 BSH_CHECK(ret == 0, return NULL, "Failed to memcpy");
94 } else if (strcmp(name, ".") == 0) {
95 realLen = sprintf_s(buffer, buffSize, "%s", current);
96 } else {
97 realLen = sprintf_s(buffer, buffSize, "%s%s", current, name);
98 }
99 } else if (strlen(name) == 0) {
100 realLen = sprintf_s(buffer, buffSize, "%s", current);
101 } else {
102 realLen = sprintf_s(buffer, buffSize, "%s", name);
103 }
104 BSH_CHECK(realLen >= 0, return NULL, "Failed to format buffer");
105 buffer[realLen] = '\0';
106 BSH_LOGV("GetRealParameter current %s input %s real %s", current, name, buffer);
107 return buffer;
108 }
109
SetParamShellPrompt(BShellHandle shell,const char * param)110 int SetParamShellPrompt(BShellHandle shell, const char *param)
111 {
112 uint32_t buffSize = 0;
113 char *buffer = GetLocalBuffer(&buffSize);
114 char *realParameter = GetRealParameter(shell, param, buffer, buffSize);
115 BSH_CHECK(realParameter != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
116 if (strlen(realParameter) == 0) {
117 BShellEnvOutputPrompt(shell, PARAM_SHELL_DEFAULT_PROMPT);
118 return -1;
119 }
120 // check parameter
121 int ret = SystemCheckParamExist(realParameter);
122 if (ret == PARAM_CODE_NOT_FOUND) {
123 BShellEnvOutput(shell, "Error: parameter \'%s\' not found\r\n", realParameter);
124 return -1;
125 } else if (ret != 0 && ret != PARAM_CODE_NODE_EXIST) {
126 BShellEnvOutput(shell, "Error: Forbid to enter parameters \'%s\'\r\n", realParameter);
127 return -1;
128 }
129 if (strcmp(realParameter, "#") == 0) {
130 ret = BShellEnvSetParam(shell, PARAM_REVERESD_NAME_CURR_PARAMETER,
131 "", PARAM_STRING, (void *)"");
132 BSH_CHECK(ret == 0, return BSH_SYSTEM_ERR, "Failed to set param value");
133 BShellEnvOutputPrompt(shell, PARAM_SHELL_DEFAULT_PROMPT);
134 return 0;
135 }
136 ret = BShellEnvSetParam(shell, PARAM_REVERESD_NAME_CURR_PARAMETER,
137 "", PARAM_STRING, (void *)realParameter);
138 BSH_CHECK(ret == 0, return BSH_SYSTEM_ERR, "Failed to set param value");
139 if (strcat_s(realParameter, buffSize, "#") != 0) {
140 BSH_CHECK(ret != 0, return BSH_SYSTEM_ERR, "Failed to cat prompt %s", realParameter);
141 }
142 BShellEnvOutputPrompt(shell, realParameter);
143 return 0;
144 }
145
GetPermissionString(uint32_t mode,int shift,char * str,int size)146 static char *GetPermissionString(uint32_t mode, int shift, char *str, int size)
147 {
148 BEGET_CHECK(!(size < MASK_LENGTH_MAX), return str);
149 str[0] = '-';
150 str[1] = '-';
151 str[2] = '-'; // 2 watcher
152 str[3] = '\0'; // 3 end
153 if (mode & (DAC_READ >> shift)) {
154 str[0] = 'r';
155 }
156 if (mode & (DAC_WRITE >> shift)) {
157 str[1] = 'w';
158 }
159 if (mode & (DAC_WATCH >> shift)) {
160 str[2] = 'w'; // 2 watcher
161 }
162 return str;
163 }
164
ShowParam(BShellHandle shell,const char * name,const char * value)165 static void ShowParam(BShellHandle shell, const char *name, const char *value)
166 {
167 ParamAuditData auditData = {};
168 int ret = GetParamSecurityAuditData(name, 0, &auditData);
169 BSH_CHECK(ret == 0, return, "Failed to get param security for %s", name);
170 BShellEnvOutput(shell, "Parameter information:\r\n");
171 #ifdef PARAM_SUPPORT_SELINUX
172 BShellEnvOutput(shell, "selinux : %s \r\n", auditData.label);
173 #endif
174 char permissionStr[3][MASK_LENGTH_MAX] = {}; // 3 permission
175 struct passwd *user = getpwuid(auditData.dacData.uid);
176 struct group *group = getgrgid(auditData.dacData.gid);
177 if (user != NULL && group != NULL) {
178 BShellEnvOutput(shell, " dac : %s(%s) %s(%s) (%s) \r\n",
179 user->pw_name,
180 GetPermissionString(auditData.dacData.mode, 0, permissionStr[0], MASK_LENGTH_MAX),
181 group->gr_name,
182 GetPermissionString(auditData.dacData.mode,DAC_GROUP_START, permissionStr[1], MASK_LENGTH_MAX),
183 // 2 other
184 GetPermissionString(auditData.dacData.mode, DAC_OTHER_START, permissionStr[2], MASK_LENGTH_MAX));
185 }
186 if (strcmp("#", name) != 0) {
187 BShellEnvOutput(shell, " name : %s\r\n", name);
188 }
189 if (value != NULL) {
190 BShellEnvOutput(shell, " value: %s\r\n", value);
191 }
192 }
193
ShowParamForCmdLs(ParamHandle handle,void * cookie)194 static void ShowParamForCmdLs(ParamHandle handle, void *cookie)
195 {
196 uint32_t buffSize = 0;
197 char *buffer = GetLocalBuffer(&buffSize);
198 if (buffSize < (PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX)) {
199 return;
200 }
201 char *value = buffer + PARAM_NAME_LEN_MAX;
202 (void)SystemGetParameterName(handle, buffer, PARAM_NAME_LEN_MAX);
203 uint32_t valueLen = buffSize - PARAM_NAME_LEN_MAX;
204 (void)SystemGetParameterValue(handle, value, &valueLen);
205 ShowParam((BShellHandle)cookie, buffer, value);
206 }
207
BShellParamCmdLs(BShellHandle shell,int32_t argc,char * argv[])208 static int32_t BShellParamCmdLs(BShellHandle shell, int32_t argc, char *argv[])
209 {
210 BSH_CHECK(shell != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
211 int all = 0;
212 char *input = NULL;
213 for (int32_t i = 1; i < argc; i++) {
214 if (strcmp(argv[i], "-r") == 0) {
215 all = 1;
216 } else if (input == NULL) {
217 input = argv[i];
218 }
219 }
220 uint32_t buffSize = 0;
221 char *buffer = GetLocalBuffer(&buffSize);
222 char *realParameter = GetRealParameter(shell, (input == NULL) ? "" : input, buffer, buffSize);
223 BSH_CHECK(realParameter != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
224 char *prefix = strdup((strlen(realParameter) == 0) ? "#" : realParameter);
225 BSH_LOGV("BShellParamCmdLs prefix %s", prefix);
226 int ret = 0;
227 if (all != 0) {
228 ret = SystemTraversalParameter(prefix, ShowParamForCmdLs, (void *)shell);
229 if (ret != 0) {
230 BShellEnvOutput(shell, "Error: Forbid to list parameters\r\n");
231 }
232 } else {
233 ret = SystemCheckParamExist(prefix);
234 if (ret == 0) {
235 ParamHandle handle;
236 ret = SystemFindParameter(prefix, &handle);
237 if (ret != 0) {
238 BShellEnvOutput(shell, "Error: Forbid to list parameters\r\n");
239 } else {
240 ShowParamForCmdLs(handle, (void *)shell);
241 }
242 } else if (ret == PARAM_CODE_NODE_EXIST) {
243 ShowParam(shell, prefix, NULL);
244 } else if (ret != PARAM_CODE_NOT_FOUND) {
245 BShellEnvOutput(shell, "Error: Forbid to list parameters\r\n");
246 } else {
247 BShellEnvOutput(shell, "Parameter %s not found\r\n", prefix);
248 }
249 }
250 free(prefix);
251 return 0;
252 }
253
BShellParamCmdCat(BShellHandle shell,int32_t argc,char * argv[])254 static int32_t BShellParamCmdCat(BShellHandle shell, int32_t argc, char *argv[])
255 {
256 BSH_CHECK(shell != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
257 BSH_CHECK(argc >= 1, return BSH_CMD_PARAM_INVALID, "Invalid shell env");
258 uint32_t buffSize = 0;
259 char *buffer = GetLocalBuffer(&buffSize);
260 char *realParameter = GetRealParameter(shell, argv[1], buffer, buffSize);
261 BSH_CHECK(realParameter != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
262 int ret = SystemGetParameter(realParameter, buffer, &buffSize);
263 BSH_CHECK(ret != 0, BShellEnvOutput(shell, " %s\r\n", buffer));
264 return 0;
265 }
266
BShellParamCmdCd(BShellHandle shell,int32_t argc,char * argv[])267 static int32_t BShellParamCmdCd(BShellHandle shell, int32_t argc, char *argv[])
268 {
269 BSH_CHECK(shell != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
270 BSH_CHECK(argc >= 1, return BSH_CMD_PARAM_INVALID, "Invalid shell env");
271 SetParamShellPrompt(shell, argv[1]);
272 return 0;
273 }
274
ShowParamForCmdGet(ParamHandle handle,void * cookie)275 static void ShowParamForCmdGet(ParamHandle handle, void *cookie)
276 {
277 uint32_t buffSize = 0;
278 char *buffer = GetLocalBuffer(&buffSize);
279 BSH_CHECK(!(buffSize < (PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX)), return);
280 char *value = buffer + PARAM_NAME_LEN_MAX;
281 (void)SystemGetParameterName(handle, buffer, PARAM_NAME_LEN_MAX);
282 uint32_t valueLen = buffSize - PARAM_NAME_LEN_MAX;
283 (void)SystemGetParameterValue(handle, value, &valueLen);
284 BShellEnvOutput((BShellHandle)cookie, " %s = %s\r\n", buffer, value);
285 }
286
BShellParamCmdGet(BShellHandle shell,int32_t argc,char * argv[])287 static int32_t BShellParamCmdGet(BShellHandle shell, int32_t argc, char *argv[])
288 {
289 BSH_CHECK(shell != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
290 BSH_CHECK(argc >= 1, return BSH_CMD_PARAM_INVALID, "Invalid shell env");
291 int ret = 0;
292 uint32_t buffSize = 0;
293 char *buffer = GetLocalBuffer(&buffSize);
294 char *realParameter = GetRealParameter(shell, (argc == 1) ? "" : argv[1], buffer, buffSize);
295 if ((argc == 1) || (realParameter == NULL) ||
296 (strlen(realParameter) == 0) || (strcmp(realParameter, "#") == 0)) {
297 ret = SystemTraversalParameter(realParameter, ShowParamForCmdGet, (void *)shell);
298 if (ret != 0) {
299 BShellEnvOutput(shell, "Error: Forbid to get all parameters\r\n");
300 }
301 return 0;
302 }
303 char *key = strdup(realParameter);
304 ret = SystemGetParameter(key, buffer, &buffSize);
305 if (ret == 0) {
306 BShellEnvOutput(shell, "%s \n", buffer);
307 } else {
308 BShellEnvOutput(shell, "Get parameter \"%s\" fail\n", key);
309 }
310 free(key);
311 return 0;
312 }
313
BShellParamCmdSet(BShellHandle shell,int32_t argc,char * argv[])314 static int32_t BShellParamCmdSet(BShellHandle shell, int32_t argc, char *argv[])
315 {
316 BSH_CHECK(shell != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
317 if (argc < 3) { // 3 min param
318 char *helpArgs[] = {"param", NULL};
319 BShellCmdHelp(shell, 1, helpArgs);
320 return 0;
321 }
322 uint32_t buffSize = 0;
323 char *buffer = GetLocalBuffer(&buffSize);
324 char *realParameter = GetRealParameter(shell, argv[1], buffer, buffSize);
325 if ((realParameter == NULL) || (strlen(realParameter) == 0) || (strcmp(realParameter, "#") == 0)) {
326 BShellEnvOutput(shell, "Set parameter %s %s fail\n", argv[1], argv[2]); // 2 value param
327 return 0;
328 }
329 int ret = SystemSetParameter(realParameter, argv[2]); // 2 value param
330 if (ret == 0) {
331 BShellEnvOutput(shell, "Set parameter %s %s success\n", realParameter, argv[2]); // 2 value param
332 } else {
333 BShellEnvOutput(shell, "Set parameter %s %s fail\n", realParameter, argv[2]); // 2 value param
334 }
335 return 0;
336 }
337
BShellParamCmdWait(BShellHandle shell,int32_t argc,char * argv[])338 static int32_t BShellParamCmdWait(BShellHandle shell, int32_t argc, char *argv[])
339 {
340 BSH_CHECK(shell != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
341 if (argc < 2) { // 2 min param
342 char *helpArgs[] = {"param", NULL};
343 BShellCmdHelp(shell, 1, helpArgs);
344 return 0;
345 }
346 int32_t timeout = 30; // 30s
347 char *value = "*";
348 if (argc > 2) { // 2 value param
349 value = argv[2]; // 2 value param
350 }
351 if (argc > 3) { // 3 timeout param
352 timeout = atoi(argv[3]); // 3 timeout param
353 }
354 uint32_t buffSize = 0;
355 char *buffer = GetLocalBuffer(&buffSize);
356 char *realParameter = GetRealParameter(shell, argv[1], buffer, buffSize);
357 if ((realParameter == NULL) || (strlen(realParameter) == 0) || (strcmp(realParameter, "#") == 0)) {
358 BShellEnvOutput(shell, "Wait parameter %s fail\n", argv[1]);
359 return 0;
360 }
361
362 int ret = SystemWaitParameter(realParameter, value, timeout);
363 if (ret == 0) {
364 BShellEnvOutput(shell, "Wait parameter %s success\n", argv[1]);
365 } else {
366 BShellEnvOutput(shell, "Wait parameter %s fail\n", argv[1]);
367 }
368 return 0;
369 }
370
BShellParamCmdDump(BShellHandle shell,int32_t argc,char * argv[])371 static int32_t BShellParamCmdDump(BShellHandle shell, int32_t argc, char *argv[])
372 {
373 BSH_CHECK(shell != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
374 if (argc >= 2) { // 2 min parameter
375 if (strcmp(argv[1], "verbose") == 0) {
376 SystemDumpParameters(1, -1, printf);
377 return 0;
378 }
379 int index = StringToInt(argv[1], 0);
380 SystemDumpParameters(1, index, printf);
381 return 0;
382 }
383 SystemDumpParameters(0, -1, printf);
384 return 0;
385 }
386
BShellParamCmdPwd(BShellHandle shell,int32_t argc,char * argv[])387 static int32_t BShellParamCmdPwd(BShellHandle shell, int32_t argc, char *argv[])
388 {
389 uint32_t buffSize = 0;
390 char *buffer = GetLocalBuffer(&buffSize);
391 char *realParameter = GetRealParameter(shell, "", buffer, buffSize);
392 BShellEnvOutput(shell, "%s\r\n", realParameter);
393 return 0;
394 }
395
GetUserInfo(ParamShellExecArgs * execArg,int32_t argc,char * argv[])396 static void GetUserInfo(ParamShellExecArgs *execArg, int32_t argc, char *argv[])
397 {
398 int32_t i = 0;
399 execArg->parameter = NULL;
400 while (i < argc) {
401 if (strcmp(argv[i], "-p") == 0 && ((i + 1) < argc)) {
402 execArg->parameter = argv[i + 1];
403 ++i;
404 } else if (strcmp(argv[i], "-u") == 0 && ((i + 1) < argc)) {
405 execArg->uid = DecodeUid(argv[i + 1]);
406 execArg->uid = (execArg->uid == -1) ? 0 : execArg->uid;
407 ++i;
408 } else if (strcmp(argv[i], "-g") == 0 && ((i + 1) < argc)) {
409 execArg->gid = DecodeGid(argv[i + 1]);
410 execArg->gid = (execArg->gid == -1) ? 0 : execArg->gid;
411 ++i;
412 } else if (strcmp(argv[i], "-c") == 0) {
413 execArg->cloneFlg = 1;
414 }
415 ++i;
416 }
417 }
418
ExecFunc(void * arg)419 static int ExecFunc(void *arg)
420 {
421 ParamShellExecArgs *execArg = (ParamShellExecArgs *)arg;
422 int ret = 0;
423 setuid(execArg->uid);
424 setgid(execArg->gid);
425 BSH_LOGI("Exec shell %s \n", SHELL_NAME);
426 if (execArg->parameter != NULL) { // 2 min argc
427 char *args[] = {SHELL_NAME, execArg->parameter, NULL};
428 ret = execv(CMD_PATH, args);
429 } else {
430 char *args[] = {SHELL_NAME, NULL};
431 ret = execv(CMD_PATH, args);
432 }
433 if (ret != 0) {
434 printf("error on exec %d \n", errno);
435 exit(0);
436 }
437 return ret;
438 }
439
ForkChild(int (* childFunc)(void * arg),void * args)440 static pid_t ForkChild(int (*childFunc)(void *arg), void *args)
441 {
442 pid_t pid = fork();
443 if (pid == 0) {
444 childFunc(args);
445 exit(0);
446 }
447 return pid;
448 }
449
BShellParamCmdShell(BShellHandle shell,int32_t argc,char * argv[])450 static int32_t BShellParamCmdShell(BShellHandle shell, int32_t argc, char *argv[])
451 {
452 #ifndef STARTUP_INIT_TEST
453 BSH_CHECK(shell != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
454 int ret = 0;
455 if (tcgetattr(0, &g_terminalState)) {
456 return BSH_SYSTEM_ERR;
457 }
458 g_isSetTerminal = 1;
459 ParamShellExecArgs args = {0, 0, 0, NULL};
460 GetUserInfo(&args, argc, argv);
461 BSH_LOGV("BShellParamCmdShell %s %d %d argc %d", args.parameter, args.uid, args.gid, argc);
462 if (args.parameter != NULL) {
463 ret = SystemCheckParamExist(args.parameter);
464 if (ret != 0) {
465 BShellEnvOutput(shell, "Error: parameter \'%s\' not found\r\n", args.parameter);
466 return -1;
467 }
468 }
469 SetInitLogLevel(INIT_INFO);
470 pid_t pid = 0;
471 if (args.cloneFlg) {
472 char *childStack = (char *)malloc(STACK_SIZE);
473 BSH_CHECK(childStack != NULL, return -1, "malloc failed");
474 pid = clone(ExecFunc, childStack + STACK_SIZE, CLONE_NEWPID | CLONE_NEWNS | SIGCHLD, (void *)&args);
475 free(childStack);
476 } else {
477 pid = ForkChild(ExecFunc, (void *)&args);
478 }
479 if (pid > 0) {
480 g_shellPid = pid;
481 int status = 0;
482 wait(&status);
483 tcsetattr(0, TCSAFLUSH, &g_terminalState);
484 g_isSetTerminal = 0;
485 }
486 #endif
487 return 0;
488 }
489
BShellParamCmdRegForShell(BShellHandle shell)490 static int32_t BShellParamCmdRegForShell(BShellHandle shell)
491 {
492 const CmdInfo infos[] = {
493 {"ls", BShellParamCmdLs, "display system parameter", "ls [-r] [name]", NULL},
494 {"get", BShellParamCmdGet, "get system parameter", "get [name]", NULL},
495 {"set", BShellParamCmdSet, "set system parameter", "set name value", NULL},
496 {"wait", BShellParamCmdWait, "wait system parameter", "wait name [value] [timeout]", NULL},
497 {"dump", BShellParamCmdDump, "dump system parameter", "dump [verbose]", ""},
498 {"cd", BShellParamCmdCd, "change path of parameter", "cd name", NULL},
499 {"cat", BShellParamCmdCat, "display value of parameter", "cat name", NULL},
500 {"pwd", BShellParamCmdPwd, "display current parameter", "pwd", NULL},
501 };
502 for (size_t i = sizeof(infos) / sizeof(infos[0]); i > 0; i--) {
503 BShellEnvRegisterCmd(shell, &infos[i - 1]);
504 }
505 return 0;
506 }
507
BShellParamCmdRegForIndepent(BShellHandle shell)508 static int32_t BShellParamCmdRegForIndepent(BShellHandle shell)
509 {
510 const CmdInfo infos[] = {
511 {"param", BShellParamCmdLs, "display system parameter", "param ls [-r] [name]", "param ls"},
512 {"param", BShellParamCmdGet, "get system parameter", "param get [name]", "param get"},
513 {"param", BShellParamCmdSet, "set system parameter", "param set name value", "param set"},
514 {"param", BShellParamCmdWait, "wait system parameter", "param wait name [value] [timeout]", "param wait"},
515 {"param", BShellParamCmdDump, "dump system parameter", "param dump [verbose]", "param dump"},
516 {"param", BShellParamCmdShell, "shell system parameter",
517 "param shell [-p] [name] [-u] [username] [-g] [groupname]", "param shell"},
518 };
519 for (size_t i = sizeof(infos) / sizeof(infos[0]); i > 0; i--) {
520 BShellEnvRegisterCmd(shell, &infos[i - 1]);
521 }
522 return 0;
523 }
524
UpdateInitLogLevel(void)525 static void UpdateInitLogLevel(void)
526 {
527 char level[2] = {0}; // 2 max length
528 uint32_t length = sizeof(level);
529 int ret = SystemGetParameter(INIT_DEBUG_LEVEL, level, &length);
530 if (ret == 0) {
531 errno = 0;
532 InitLogLevel value = (InitLogLevel)strtoul(level, NULL, DECIMAL_BASE);
533 SetInitLogLevel((errno != 0) ? INIT_WARN : value);
534 }
535 }
536
BShellParamCmdRegister(BShellHandle shell,int execMode)537 int32_t BShellParamCmdRegister(BShellHandle shell, int execMode)
538 {
539 UpdateInitLogLevel();
540 if (execMode) {
541 BShellParamCmdRegForShell(shell);
542 } else {
543 BShellParamCmdRegForIndepent(shell);
544 }
545 return 0;
546 }
547