• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
45 ShellCB *g_shellCB = NULL;
46 
OsGetShellCb(void)47 ShellCB *OsGetShellCb(void)
48 {
49     return g_shellCB;
50 }
51 
ShellDeinit(ShellCB * shellCB)52 static void ShellDeinit(ShellCB *shellCB)
53 {
54     (void)pthread_mutex_destroy(&shellCB->historyMutex);
55     (void)pthread_mutex_destroy(&shellCB->keyMutex);
56     OsShellKeyDeInit((CmdKeyLink *)shellCB->cmdKeyLink);
57     OsShellKeyDeInit((CmdKeyLink *)shellCB->cmdHistoryKeyLink);
58     (void)free(shellCB);
59 }
60 
OsShellCreateTask(ShellCB * shellCB)61 static 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         return ret;
69     }
70 
71     param.sched_priority = SHELL_PROCESS_PRIORITY_INIT;
72 
73     ret = sched_setparam(getpid(), &param);
74     if (ret != SH_OK) {
75         return ret;
76     }
77 
78     ret = ShellTaskInit(shellCB);
79     if (ret != SH_OK) {
80         return ret;
81     }
82 
83     shellCB->shellEntryHandle = pthread_self();
84     return 0;
85 }
86 
DoShellExec(char ** argv)87 static int DoShellExec(char **argv)
88 {
89     int i, j;
90     int len = 0;
91     int ret = SH_NOK;
92     char *cmdLine = NULL;
93 
94     if (strncmp(argv[0], SHELL_EXEC_COMMAND, SHELL_EXEC_COMMAND_BYTES) == 0) {
95         ChildExec(argv[1], argv + 1, FALSE);
96     }
97     for (i = 0; argv[i]; i++) {
98         len += strlen(argv[i]);
99     }
100     len += i + 1;
101     cmdLine = (char *)malloc(len);
102     if (!cmdLine) {
103         return ret;
104     }
105     errno_t ret1 = memset_s(cmdLine, len, 0, len);
106     if (ret1 != EOK) {
107         free(cmdLine);
108         return ret1;
109     }
110 
111     for (j = 0; j < i; j++) {
112         (void)strcat_s(cmdLine, len,  argv[j]);
113         (void)strcat_s(cmdLine, len, " ");
114     }
115 
116     cmdLine[len - 2] = '\0'; /* 2, (len - 2) is the end of cmdline buf */
117     ret = syscall(__NR_shellexec, argv[0], cmdLine);
118     free(cmdLine);
119     return ret;
120 }
121 
ShellSigChildHook(int sig)122 static void ShellSigChildHook(int sig)
123 {
124     (void)sig;
125 
126     while (waitpid(-1, NULL, WNOHANG) > 0) {
127         continue;
128     }
129 }
130 
main(int argc,char ** argv)131 int main(int argc, char **argv)
132 {
133     int ret = SH_NOK;
134     ShellCB *shellCB = NULL;
135 
136     (void)signal(SIGCHLD, ShellSigChildHook);
137 
138     if (argc > 1) {
139         ret = DoShellExec(argv + 1);
140         return ret;
141     }
142 
143     setbuf(stdout, NULL);
144 
145     shellCB = (ShellCB *)malloc(sizeof(ShellCB));
146     if (shellCB == NULL) {
147         return SH_NOK;
148     }
149     ret = memset_s(shellCB, sizeof(ShellCB), 0, sizeof(ShellCB));
150     if (ret != SH_OK) {
151         goto ERR_OUT1;
152     }
153 
154     ret = pthread_mutex_init(&shellCB->keyMutex, NULL);
155     if (ret != SH_OK) {
156         goto ERR_OUT1;
157     }
158 
159     ret = pthread_mutex_init(&shellCB->historyMutex, NULL);
160     if (ret != SH_OK) {
161         goto ERR_OUT2;
162     }
163 
164     ret = (int)OsShellKeyInit(shellCB);
165     if (ret != SH_OK) {
166         goto ERR_OUT3;
167     }
168     (void)strncpy_s(shellCB->shellWorkingDirectory, PATH_MAX, "/", 2); /* 2:space for "/" */
169 
170     sem_init(&shellCB->shellSem, 0, 0);
171 
172     g_shellCB = shellCB;
173     ret = OsShellCreateTask(shellCB);
174     if (ret != SH_OK) {
175         ShellDeinit(shellCB);
176         g_shellCB = NULL;
177         return ret;
178     }
179 
180     ShellEntry(shellCB);
181 
182 ERR_OUT3:
183     (void)pthread_mutex_destroy(&shellCB->historyMutex);
184 ERR_OUT2:
185     (void)pthread_mutex_destroy(&shellCB->keyMutex);
186 ERR_OUT1:
187     (void)free(shellCB);
188     return ret;
189 }
190 
191