• 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 "show.h"
35 #include "shmsg.h"
36 #include "shcmd.h"
37 #include "semaphore.h"
38 #include "securec.h"
39 #include "unistd.h"
40 #include <sys/syscall.h>
41 
42 ShellCB *g_shellCB = NULL;
43 
OsGetShellCb()44 ShellCB *OsGetShellCb()
45 {
46     return g_shellCB;
47 }
48 
ShellDeinit(ShellCB * shellCB)49 void ShellDeinit(ShellCB *shellCB)
50 {
51     (void)pthread_mutex_destroy(&shellCB->historyMutex);
52     (void)pthread_mutex_destroy(&shellCB->keyMutex);
53     OsShellKeyDeInit((CmdKeyLink *)shellCB->cmdKeyLink);
54     OsShellKeyDeInit((CmdKeyLink *)shellCB->cmdHistoryKeyLink);
55     (void)free(shellCB);
56 }
57 
OsShellCreateTask(ShellCB * shellCB)58 static int OsShellCreateTask(ShellCB *shellCB)
59 {
60     struct sched_param param = { 0 };
61     int ret;
62 
63     ret = sched_getparam(getpid(), &param);
64     if (ret != SH_OK) {
65         goto OUT;
66     }
67 
68     param.sched_priority = SHELL_PROCESS_PRIORITY_INIT;
69 
70     ret = sched_setparam(getpid(), &param);
71     if (ret != SH_OK) {
72         goto OUT;
73     }
74 
75     ret = ShellTaskInit(shellCB);
76     if (ret != SH_OK) {
77         goto OUT;
78     }
79 
80     ret = ShellEntryInit(shellCB);
81     if (ret != SH_OK) {
82         goto OUT;
83     }
84 
85     (void)pthread_join(shellCB->shellTaskHandle, NULL);
86     (void)pthread_join(shellCB->shellEntryHandle, NULL);
87 
88 OUT:
89     ShellDeinit(shellCB);
90     return ret;
91 }
92 
DoShellExec(char ** argv)93 static int DoShellExec(char **argv)
94 {
95     int i, j;
96     int len = 0;
97     int ret = SH_NOK;
98     char *cmdLine = NULL;
99 
100     if (strncmp(argv[0], SHELL_EXEC_COMMAND, SHELL_EXEC_COMMAND_BYTES) == 0) {
101         ChildExec(argv[1], argv + 1);
102     }
103     for (i = 0; argv[i]; i++) {
104         len += strlen(argv[i]);
105     }
106     len += i + 1;
107     cmdLine = (char *)malloc(len);
108     if (!cmdLine) {
109         return ret;
110     }
111     errno_t ret1 = memset_s(cmdLine, len, 0, len);
112     if (ret1 != EOK) {
113         free(cmdLine);
114         return ret1;
115     }
116 
117     for (j = 0; j < i; j++) {
118         (void)strcat_s(cmdLine, len,  argv[j]);
119         (void)strcat_s(cmdLine, len, " ");
120     }
121 
122     cmdLine[len - 2] = '\0'; /* 2, (len - 2) is the end of cmdline buf */
123     ret = syscall(__NR_shellexec, argv[0], cmdLine);
124     free(cmdLine);
125     return ret;
126 }
127 
main(int argc,char ** argv)128 int main(int argc, char **argv)
129 {
130     int ret = SH_NOK;
131     ShellCB *shellCB = NULL;
132 
133     if (argc > 1) {
134         ret = DoShellExec(argv + 1);
135         return ret;
136     }
137 
138     setbuf(stdout, NULL);
139 
140     shellCB = (ShellCB *)malloc(sizeof(ShellCB));
141     if (shellCB == NULL) {
142         goto ERR_OUT1;
143     }
144     ret = memset_s(shellCB, sizeof(ShellCB), 0, sizeof(ShellCB));
145     if (ret != SH_OK) {
146         goto ERR_OUT1;
147     }
148 
149     ret = pthread_mutex_init(&shellCB->keyMutex, NULL);
150     if (ret != SH_OK) {
151         goto ERR_OUT1;
152     }
153 
154     ret = pthread_mutex_init(&shellCB->historyMutex, NULL);
155     if (ret != SH_OK) {
156         goto ERR_OUT2;
157     }
158 
159     ret = (int)OsShellKeyInit(shellCB);
160     if (ret != SH_OK) {
161         goto ERR_OUT3;
162     }
163     (void)strncpy_s(shellCB->shellWorkingDirectory, PATH_MAX, "/", 2); /* 2:space for "/" */
164 
165     sem_init(&shellCB->shellSem, 0, 0);
166 
167     g_shellCB = shellCB;
168     return OsShellCreateTask(shellCB);
169 
170 ERR_OUT3:
171     (void)pthread_mutex_destroy(&shellCB->historyMutex);
172 ERR_OUT2:
173     (void)pthread_mutex_destroy(&shellCB->keyMutex);
174 ERR_OUT1:
175     (void)free(shellCB);
176     return ret;
177 }
178 
179