更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
main.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#define _GNU_SOURCE
33
34#include <signal.h>
35#include <sys/wait.h>
36#include <unistd.h>
37#include <sys/syscall.h>
38#include "show.h"
39#include "shmsg.h"
40#include "shcmd.h"
41#include "shell_pri.h"
42#include "semaphore.h"
43#include "securec.h"
44
45ShellCB *g_shellCB = NULL; ///< 全部shell控制块
46/// 获取shell控制块
48{
49 return g_shellCB;
50}
51
52void ShellDeinit(ShellCB *shellCB)
53{
58 (void)free(shellCB);
59}
60
61static int OsShellCreateTask(ShellCB *shellCB)
62{
63 struct sched_param param = { 0 };
64 int ret;
65
66 ret = sched_getparam(getpid(), &param);
67 if (ret != SH_OK) {
68 goto OUT;
69 }
70
71 param.sched_priority = SHELL_PROCESS_PRIORITY_INIT;
72
73 ret = sched_setparam(getpid(), &param);
74 if (ret != SH_OK) {
75 goto OUT;
76 }
77
78 ret = ShellTaskInit(shellCB);
79 if (ret != SH_OK) {
80 goto OUT;
81 }
82
83 shellCB->shellEntryHandle = pthread_self();
84 return 0;
85
86OUT:
87 ShellDeinit(shellCB);
88 return ret;
89}
90
91static int DoShellExec(char **argv)
92{
93 int i, j;
94 int len = 0;
95 int ret = SH_NOK;
96 char *cmdLine = NULL;
97
98 if (strncmp(argv[0], SHELL_EXEC_COMMAND, SHELL_EXEC_COMMAND_BYTES) == 0) {
99 ChildExec(argv[1], argv + 1, FALSE);
100 }
101 for (i = 0; argv[i]; i++) {
102 len += strlen(argv[i]);
103 }
104 len += i + 1;
105 cmdLine = (char *)malloc(len);
106 if (!cmdLine) {
107 return ret;
108 }
109 errno_t ret1 = memset_s(cmdLine, len, 0, len);
110 if (ret1 != EOK) {
111 free(cmdLine);
112 return ret1;
113 }
114
115 for (j = 0; j < i; j++) {
116 (void)strcat_s(cmdLine, len, argv[j]);
117 (void)strcat_s(cmdLine, len, " ");
118 }
119
120 cmdLine[len - 2] = '\0';
121 ret = syscall(__NR_shellexec, argv[0], cmdLine);//执行 系统调用 __NR_shellexec 对应为 SysShellExec
122 free(cmdLine);
123 return ret;
124}
125static void ShellSigChildHook(int sig)
126{
127 (void)sig;
128
129 while (waitpid(-1, NULL, WNOHANG) > 0) {
130 continue;
131 }
132}
133
134/// shell进程的入口函数
135int main(int argc, char **argv)
136{
137 int ret = SH_NOK;
138 ShellCB *shellCB = NULL;
139
140 (void)signal(SIGCHLD, ShellSigChildHook);
141 if (argc > 1) {
142 ret = DoShellExec(argv + 1);
143 return ret;
144 }
145
146 setbuf(stdout, NULL);
147
148 shellCB = (ShellCB *)malloc(sizeof(ShellCB));//申请shell控制块
149 if (shellCB == NULL) {
150 goto ERR_OUT1;
151 }
152 ret = memset_s(shellCB, sizeof(ShellCB), 0, sizeof(ShellCB));//初始化控制块
153 if (ret != SH_OK) {
154 goto ERR_OUT1;
155 }
156
157 ret = pthread_mutex_init(&shellCB->keyMutex, NULL);//初始化待处理命令链表互斥量
158 if (ret != SH_OK) {
159 goto ERR_OUT1;
160 }
161
162 ret = pthread_mutex_init(&shellCB->historyMutex, NULL);//初始化命令历史记录链表互斥量
163 if (ret != SH_OK) {
164 goto ERR_OUT2;
165 }
166
167 ret = (int)OsShellKeyInit(shellCB);//初始化待处理命令
168 if (ret != SH_OK) {
169 goto ERR_OUT3;
170 }//设置工作目录
171 (void)strncpy_s(shellCB->shellWorkingDirectory, PATH_MAX, "/", 2); /* 2:space for "/" */
172
173 sem_init(&shellCB->shellSem, 0, 0);//信号量初始化
174
175 g_shellCB = shellCB;//全局变量,说明鸿蒙同时只支持一个shell进程
176 ret = OsShellCreateTask(shellCB);
177 if (ret != SH_OK) {
178 goto ERR_OUT3;
179 }
180
181 ShellEntry(shellCB);
182
183ERR_OUT3:
185ERR_OUT2:
187ERR_OUT1:
188 (void)free(shellCB);
189 return ret;
190}
191
int main(int argc, char **argv)
Definition: main.c:44
unsigned int OsShellKeyInit(ShellCB *shellCB)
shell 命令初始化
Definition: shcmd.c:465
void OsShellKeyDeInit(CmdKeyLink *cmdKeyLink)
shell的析构函数
Definition: shcmd.c:497
void ShellEntry(ShellCB *shellCB)
Definition: shmsg.c:659
void ChildExec(const char *cmdName, char *const paramArray[], bool foreground)
Definition: shmsg.c:354
int ShellTaskInit(ShellCB *shellCB)
给控制台注册一个shell客户端任务
Definition: shmsg.c:628
static void ShellSigChildHook(int sig)
Definition: main.c:125
static int OsShellCreateTask(ShellCB *shellCB)
Definition: main.c:61
ShellCB * OsGetShellCb()
获取shell控制块
Definition: main.c:47
ShellCB * g_shellCB
Definition: main.c:45
void ShellDeinit(ShellCB *shellCB)
Definition: main.c:52
static int DoShellExec(char **argv)
Definition: main.c:91
void * malloc(size_t size)
动态分配内存块大小
Definition: malloc.c:81
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
pid_t getpid(void)
获取当前任务ID
Definition: misc.c:150
pthread_t pthread_self(void)
Definition: pthread.c:764
int pthread_mutex_destroy(pthread_mutex_t *mutex)
销毁互斥锁
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexAttr)
初始化互斥锁。 如果 mutexAttr 为 NULL,则使用默认属性。
int sem_init(sem_t *sem, int shared, unsigned int value)
Definition: semaphore.c:39
Definition: shell.h:71
pthread_t shellEntryHandle
shell客户端任务
Definition: shell.h:74
sem_t shellSem
shell信号量
Definition: shell.h:80
void * cmdKeyLink
命令链表,所有敲过的命令链表
Definition: shell.h:75
void * cmdHistoryKeyLink
命令的历史记录链表,去重,10个
Definition: shell.h:76
pthread_mutex_t historyMutex
操作cmdHistoryKeyLink的互斥量
Definition: shell.h:82
char shellWorkingDirectory[PATH_MAX]
shell工作目录
Definition: shell.h:84
pthread_mutex_t keyMutex
操作cmdKeyLink的互斥量
Definition: shell.h:81
ARG_NUM_3 int
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