• 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 #include "show.h"
33 #include "shmsg.h"
34 #include "shcmd.h"
35 #include "console.h"
36 
37 
38 STATIC BOOL g_shellSourceFlag = FALSE;
39 
OsShellCmdInit(VOID)40 STATIC UINT32 OsShellCmdInit(VOID)
41 {
42     UINT32 ret = OsCmdInit();
43     if (ret != LOS_OK) {
44         return ret;
45     }
46 
47     return OsShellSysCmdRegister();
48 }
49 
OsShellCreateTask(ShellCB * shellCB)50 STATIC UINT32 OsShellCreateTask(ShellCB *shellCB)
51 {
52     UINT32 ret = ShellTaskInit(shellCB);
53     if (ret != LOS_OK) {
54         return ret;
55     }
56 
57     return ShellEntryInit(shellCB);
58 }
59 
OsShellSourceInit(INT32 consoleId)60 STATIC UINT32 OsShellSourceInit(INT32 consoleId)
61 {
62     UINT32 ret = LOS_NOK;
63     CONSOLE_CB *consoleCB = OsGetConsoleByID(consoleId);
64     if ((consoleCB == NULL) || (consoleCB->shellHandle != NULL)) {
65         return LOS_NOK;
66     }
67     consoleCB->shellHandle = LOS_MemAlloc((VOID *)m_aucSysMem0, sizeof(ShellCB));
68     if (consoleCB->shellHandle == NULL) {
69         return LOS_NOK;
70     }
71     ShellCB *shellCB = (ShellCB *)consoleCB->shellHandle;
72     if (memset_s(shellCB, sizeof(ShellCB), 0, sizeof(ShellCB)) != EOK) {
73         goto ERR_OUT1;
74     }
75 
76     shellCB->consoleID = (UINT32)consoleId;
77     ret = (UINT32)pthread_mutex_init(&shellCB->keyMutex, NULL);
78     if (ret != LOS_OK) {
79         goto ERR_OUT1;
80     }
81     ret = (UINT32)pthread_mutex_init(&shellCB->historyMutex, NULL);
82     if (ret != LOS_OK) {
83         goto ERR_OUT2;
84     }
85 
86     ret = OsShellKeyInit(shellCB);
87     if (ret != LOS_OK) {
88         goto ERR_OUT3;
89     }
90     if (strncpy_s(shellCB->shellWorkingDirectory, PATH_MAX, "/", 2) != EOK) { /* 2:space for "/" */
91         ret = LOS_NOK;
92         goto ERR_OUT4;
93     }
94 #if !defined(LOSCFG_PLATFORM_ROOTFS)
95     /*
96      * In case of ROOTFS disabled but
97      * serial console enabled, it is required
98      * to create Shell task in kernel for it.
99      */
100     if (consoleId == CONSOLE_TELNET || consoleId == CONSOLE_SERIAL) {
101 #else
102     if (consoleId == CONSOLE_TELNET) {
103 #endif
104         ret = OsShellCreateTask(shellCB);
105         if (ret != LOS_OK) {
106             goto ERR_OUT4;
107         }
108     }
109 
110     return LOS_OK;
111 ERR_OUT4:
112     (VOID)LOS_MemFree((VOID *)m_aucSysMem0, shellCB->cmdKeyLink);
113     (VOID)LOS_MemFree((VOID *)m_aucSysMem0, shellCB->cmdHistoryKeyLink);
114 ERR_OUT3:
115     (VOID)pthread_mutex_destroy(&shellCB->historyMutex);
116 ERR_OUT2:
117     (VOID)pthread_mutex_destroy(&shellCB->keyMutex);
118 ERR_OUT1:
119     (VOID)LOS_MemFree((VOID *)m_aucSysMem0, shellCB);
120     consoleCB->shellHandle = NULL;
121     return ret;
122 }
123 
124 UINT32 OsShellInit(INT32 consoleId)
125 {
126     if (g_shellSourceFlag == FALSE) {
127         UINT32 ret = OsShellCmdInit();
128         if (ret == LOS_OK) {
129             g_shellSourceFlag = TRUE;
130         } else {
131             return ret;
132         }
133     }
134     return OsShellSourceInit(consoleId);
135 }
136 
137 INT32 OsShellDeinit(INT32 consoleId)
138 {
139     CONSOLE_CB *consoleCB = NULL;
140     ShellCB *shellCB = NULL;
141 
142     consoleCB = OsGetConsoleByID(consoleId);
143     if (consoleCB == NULL) {
144         PRINT_ERR("shell deinit error.\n");
145         return -1;
146     }
147 
148     shellCB = (ShellCB *)consoleCB->shellHandle;
149     consoleCB->shellHandle = NULL;
150     if (shellCB == NULL) {
151         PRINT_ERR("shell deinit error.\n");
152         return -1;
153     }
154 
155     (VOID)LOS_TaskDelete(shellCB->shellEntryHandle);
156     (VOID)LOS_EventWrite(&shellCB->shellEvent, CONSOLE_SHELL_KEY_EVENT);
157 
158     return 0;
159 }
160 
161 CHAR *OsShellGetWorkingDirectory(VOID)
162 {
163     CONSOLE_CB *consoleCB = OsGetConsoleByTaskID(OsCurrTaskGet()->taskID);
164     ShellCB *shellCB = NULL;
165 
166     if (consoleCB == NULL) {
167         return NULL;
168     }
169     shellCB = (ShellCB *)consoleCB->shellHandle;
170     if (shellCB == NULL) {
171         return NULL;
172     }
173     return shellCB->shellWorkingDirectory;
174 }
175 
176