The Master said, “I was not born a man of knowledge. Analects of Confucius: A review
A hundred blog series. This is:
V71. Xx HongMeng kernel source code analysis (Shell editor) | two tasks, three stages
Process management:
- V02. Xx HongMeng kernel source code analysis (process management) | who in the management of core resources
- V24. Xx HongMeng kernel source code analysis concept (process) | process in which resources management
- V45. Xx HongMeng kernel source code analysis (Fork) | one call, return twice
- V46. Xx HongMeng kernel source code analysis (special) | mouse son can make hole
- V47. Xx HongMeng kernel source code analysis (process recycling) | how to entrust an orphan to ancestor dying
- V48. Xx HongMeng kernel source code analysis (signal) | the year lead half hundred, still energetic
- V49. Xx HongMeng kernel source code analysis (signal) | who let CPU run for four times in the stack
- V71. Xx HongMeng kernel source code analysis (Shell editor) | two tasks, three stages
- V72. Xx HongMeng kernel source code analysis (Shell) | watching the kernel of the application window
This series summarizes the underlying implementation of shell from the perspective of kernel in one sentence: two tasks, three stages. It is by nature an independent process, and therefore a process management module. Each time a shell process is created, two more tasks are created.
- Client task (ShellEntry) :Responsible for accepting character by character typed from the terminal (console), character press
VT
Specifications are assembled into sentences of commands. - ShellTask: Parses and executes commands and outputs the results to the console.
The command life cycle can be divided into three phases.
- Editor:In this section, Hongmeng implements a simple editor function that handles every character input from the console, including control characters such as
<ESC>
.\t
.\b
.\n
.\r
Four arrow keys0x41
~0x44
Processing. - Parsing: Parses the edited string, parses command items and parameter items, and finds the corresponding command items to execute the function.
- Execution: Commands can be registered with the kernel statically or dynamically. After the command is parsed, the corresponding function callback can be found in the registry. Output the results to the console.
The editing part is done by the client task, the last two parts are done by the server task, and command global registration is done by the kernel.
- This article focuses on client tasks and the editing process
- The server tasks and the parsing/execution process are explained in Shell parsing.
What is a Shell
From the perspective of users, shell is a window for users to peep and operate the kernel. The kernel is not monolithic, but opens two Windows on the application layer, one is system call and the other is shell. The kernel provides implementation functions and the user provides parameters to execute. The difference is that shell is completed by an independent task, which can be written into an independent and simple shell program by serializing shell commands. Therefore, shell is also a scripting language, and system call is attached to the task of the application program to complete, which can be limited. Through the shell window, you can see the running status of CPU, memory consumption, network link status and so on.
Where is the Hongmeng Shell code
The concept corresponding to shell is kernel. In hongmeng kernel, these two parts of code are separated. The shell code is viewed in the shell code, and the directory structure is as follows.
├ ─ the include │ dmesg. H │ dmesg_pri. H │ SHCMD. H │ shcmdparse. H │ shell. H │ shell_lk. H │ shell_pri. H │ SHMSG. H │ show. H │ └ ─ SRC ├ ─ base │ SHCMD. C │ shcmdparse. C │ shell_lk. C │ SHMSG. C │ show. C │ └ ─ CMDS date_shellcmd. C dmesg. C hwi_shellcmd. C shell_shellcmd.c watch_shellcmd.cCopy the code
Shell control block
Like process and task, behind each concept needs a main structure to support. The main structure of shell is ShellCB. If you master ShellCB, you can handle shell too tightly, but you can’t understand this structure and the kernel implementation of shell. So you can’t spend too much time on it.
typedef struct { UINT32 consoleID; // Console ID UINT32 shellTaskHandle; // Shell server task ID UINT32 shellEntryHandle; //shell client task ID VOID *cmdKeyLink; // List of shell commands to be processed VOID *cmdHistoryKeyLink; VOID *cmdMaskKeyLink; VOID *cmdMaskKeyLink; // Historically traversed by arrow keys. UINT32 shellBufOffset; // BUF offset UINT32 shellKeyType; // Button type EVENT_CB_S shellEvent; // The event type triggers pthread_mutex_t keyMutex; Pthread_mutex_t historyMutex; // Historical mutex CHAR shellBuf[SHOW_MAX_LEN]; // The shell command buf accepts input from the keyboard and needs to parse the input characters. CHAR shellWorkingDirectory[PATH_MAX]; } ShellCB; Typedef struct {UINT32 count; // The number of characters LOS_DL_LIST list; CHAR cmdString[0]; } CmdKeyLink;} CmdKeyLink; Enum {STAT_NOMAL_KEY, // Common key STAT_ESC_KEY, //<ESC> start key STAT_MULTI_KEY // combination key};Copy the code
Interpretation of the
- Hongmeng supports two ways of input in the console
Shell
Command, about the console please see the console chapter.- Enter the value in the serial port tool
Shell
The commandCONSOLE_SERIAL
. - in
telnet
Tool inputShell
The commandCONSOLE_TELNET
.
- Enter the value in the serial port tool
shellTaskHandle
andshellEntryHandle
Edit/processshell
Command’s two task ids, the latter of which is the focus of this article.cmdKeyLink
.cmdHistoryKeyLink
.cmdMaskKeyLink
There are three types ofCmdKeyLink
The structure is essentially a bidirectional linked list, corresponding to editingshell
Three functions in a command procedure.cmdKeyLink
List of commands to be executedcmdHistoryKeyLink
The command history is stored, that is:history
Command displaycmdMaskKeyLink
Record up and down arrow keys output content, this is a bit difficult to understand, in their ownshell
Press the up and down arrow keys to experience
shellBufOffset
andshellBuf
Is a pair, which is stored in the user typed after the character.keyMutex
andhistoryMutex
The kernel uses most of the mutex locks needed to manipulate linked lists.shellEvent
For communication between tasks, for example.SHELL_CMD_PARSE_EVENT
: Edit completed notification parsing task startedCONSOLE_SHELL_KEY_EVENT
: Received from the consoleCTRL + C
Signal generation events.
shellKeyType
There are three types of keys: normal keys and combination keysshellWorkingDirectory
Workspace does not need to say, from which directory to entershell
the
Create a Shell
Int main(int argc, char **argv) {//... g_shellCB = shellCB; Return OsShellCreateTask(shellCB); } // Creating a shell task STATIC UINT32 OsShellCreateTask(ShellCB * ShellCB) {UINT32 RET = ShellTaskInit(ShellCB); // Execute shell command tasks to initialize if (ret! = LOS_OK) { return ret; } return ShellEntryInit(shellCB); LITE_OS_SEC_TEXT_MINOR UINT32 Initializes a task that receives shell commands through the console. This task is responsible for editing commands and processing command generation processes, such as the direction keys, backspace keys, and Enter keys ShellEntryInit(ShellCB *shellCB) { UINT32 ret; CHAR *name = NULL; TSK_INIT_PARAM_S initParam = {0}; if (shellCB->consoleID == CONSOLE_SERIAL) { name = SERIAL_ENTRY_TASK_NAME; } else if (shellCB->consoleID == CONSOLE_TELNET) { name = TELNET_ENTRY_TASK_NAME; } else { return LOS_NOK; } initParam.pfnTaskEntry = (TSK_ENTRY_FUNC)ShellEntry; // Task entry function initparam. usTaskPrio = 9; /* 9:shell task priority */ initParam.auwArgs[0] = (UINTPTR)shellCB; initParam.uwStackSize = 0x1000; initParam.pcName = name; initParam.uwResved = LOS_TASK_STATUS_DETACHED; ret = LOS_TaskCreate(&shellCB->shellEntryHandle, &initParam); #ifdef LOSCFG_PLATFORM_CONSOLE (VOID)ConsoleTaskReg(INT32)shellCB->consoleID, shellCB->shellEntryHandle); #endif return ret; }Copy the code
Interpretation of the
main
forshell
The main task of a process. Each process creates a default thread (task), whose entry function is well knownmain
Function, not clear self-review task management has detailed instructions.- by
main
Tasks create two more tasks, one of which is the focus of this articleShellEntry
, the task priority is9
“Is considered a higher priority. - Specifies that the kernel stack size is
0x1000 = 4K
Since the task is only responsible for editing the characters entered by the console, the execution of the command is in other tasks, so 4K kernel space is enough to use. ShellEntry
Is the entry function, the implementation of which is the focus of this article
ShellEntry | editing process
LITE_OS_SEC_TEXT_MINOR UINT32 ShellEntry(UINTPTR param) { CHAR ch; INT32 n = 0; ShellCB *shellCB = (ShellCB *)param; CONSOLE_CB *consoleCB = OsGetConsoleByID((INT32)shellCB->consoleID); If (consoleCB == NULL) {PRINT_ERR("Shell task init error! \n"); return 1; } (VOID)memset_s(shellCB->shellBuf, SHOW_MAX_LEN, 0, SHOW_MAX_LEN); // reset shell command buf while (1) {#ifdef LOSCFG_PLATFORM_CONSOLE if (! IsConsoleOccupied(consoleCB) {// Is console ready for shell? */ n = read(consoleCB->fd, &ch, 1); ShellCmdLineParse(ch, (pf_OUTPUT)dprintf, shellCB); if (n == 1) {ShellCmdLineParse(ch, (pf_OUTPUT)dprintf, shellCB); } if (is_nonblock(consoleCB)) {// Pause in non-blocking mode for 50ms LOS_Msleep(50); / * 50: LITE_OS_SEC_TEXT_MINOR VOID for sleep */ #ifdef LOSCFG_PLATFORM_CONSOLE} #endif} ShellCmdLineParse(CHAR c, pf_OUTPUT outputFunc, ShellCB *shellCB) { const CHAR ch = c; INT32 ret; If ((shellCB->shellBufOffset == 0) && (ch! = '\n') && (ch ! = '\0')) { (VOID)memset_s(shellCB->shellBuf, SHOW_MAX_LEN, 0, SHOW_MAX_LEN); / / reset buf} / / meet carriage return or line feed the if ((ch = = '\ r') | | (ch = = '\ n')) {if (shellCB - > shellBufOffset < (SHOW_MAX_LEN - 1)) { shellCB->shellBuf[shellCB->shellBufOffset] = '\0'; } shellCB->shellBufOffset = 0; (VOID)pthread_mutex_lock(&shellCB->keyMutex); OsShellCmdPush(shellCB->shellBuf, shellCB->cmdKeyLink); VOID pthread_mutex_unlock(&shellcb ->keyMutex); ShellNotify(shellCB); // Notify the task to parse shell command return; } else if ((ch = = '\ b') | | (ch = = 0 x7f)) {/ * backspace or delete (0 x7f) * / / / in the delete button if ((shellCB - > shellBufOffset > 0) && (shellCB->shellBufOffset < (SHOW_MAX_LEN - 1))) { shellCB->shellBuf[shellCB->shellBufOffset - 1] = '\0'; // fill '\0' shellCB->shellBufOffset--; //buf reduces outputFunc("\b \b"); } return; } else if (ch == 0x09) { /* 0x09: If (shellCB->shellBufOffset > 0) && (shellCB->shellBufOffset < (show_max_len-1))) {ret = OsTabCompletion(shellCB->shellBuf, &shellCB->shellBufOffset); If (ret > 1) {outputFunc("OHOS # %s", shellCB->shellBuf); }} return; } /* parse the up/down/right/left key */ ret = ShellCmdLineCheckUDRL(ch, shellCB); If (ret == LOS_OK) {return; } if ((ch ! = '\n') && (ch ! If (shellCB->shellBufOffset < (show_max_len-1)) {//buf range shellCB->shellBuf[shellCB->shellBufOffset] = ch; } else {shellCB->shellBuf[show_max_len-1] = '\0'; } shellCB->shellBufOffset++; // Offset increases outputFunc("%c", ch); ShellCB ->shellKeyType = STAT_NOMAL_KEY;} shellCB->shellKeyType = STAT_NOMAL_KEY; // Common characters}Copy the code
Interpretation of the
ShellEntry
Inside is an infinite loop that reads every character typed by the console, note the character processing.- Four directions, line feed, carriage return,
tab
.backspace
.delete
.esc
And other control keys, equivalent to a new understanding of the nextAscii
Watch. You can do itshell
A terminal is understood as a simple editor.- Press Enter to complete the previous input and enter the parsing execution phase.
- Press the arrow key to display the up/next command, and press continuously to display the up/down command.
- According to the
tab
Key is to complete the content of the command, currently Hongmeng supports the following commands:arp cat cd chgrp chmod chown cp cpup date dhclient dmesg dns format free help hwi ifconfig ipdebug kill log ls lsfd memcheck mkdir mount netstat oom partinfo partition ping ping6 pwd reset rm rmdir sem statfs su swtmr sync systeminfo task telnet test tftp touch umount uname watch writeproc Copy the code
For example: when pressed in the console
ch
andtab
The following three outputs are generated after the keychgrp chmod chown Copy the code
Content, these functions seem very common to users, but all need to be implemented by the kernel.
shellBuf
Stores the results of the edit and, when the Enter key is pressed, saves the results and delivers them to the next stage for use.
Intensive reading of the kernel source code
Four code stores synchronous annotation kernel source code, >> view the Gitee repository
Analysis of 100 blogs. Dig deep into the core
Add comments to hongmeng kernel source code process, sort out the following article. Content based on the source code, often in life scene analogy as much as possible into the kernel knowledge of a scene, with a pictorial sense, easy to understand memory. It’s important to speak in a way that others can understand! The 100 blogs are by no means a bunch of ridiculously difficult concepts being put forward by Baidu. That’s not interesting. More hope to make the kernel become lifelike, feel more intimate. It’s hard, it’s hard, but there’s no turning back. 😛 and code bugs need to be constantly debug, there will be many mistakes and omissions in the article and annotation content, please forgive, but will be repeatedly amended, continuous update. Xx represents the number of modifications, refined, concise and comprehensive, and strive to create high-quality content.
Compile build | The fundamental tools | Loading operation | Process management |
---|---|---|---|
Compile environment The build process Environment script Build tools Designed.the gn application Ninja ninja |
Two-way linked list Bitmap management In the stack way The timer Atomic operation Time management |
The ELF format The ELF parsing Static link relocation Process image |
Process management Process concept Fork Special process Process recycling Signal production Signal consumption Shell editor Shell parsing |
Process of communication | Memory management | Ins and outs | Task management |
spinlocks The mutex Process of communication A semaphore Incident control The message queue |
Memory allocation Memory management Memory assembly The memory mapping Rules of memory Physical memory |
Total directory Scheduling the story Main memory slave The source code comments Source structure Static site |
The clock task Task scheduling Task management The scheduling queue Scheduling mechanism Thread concept Concurrent parallel The system calls Task switching |
The file system | Hardware architecture | ||
File concept The file system The index node Mount the directory Root file system Character device VFS File handle Pipeline file |
Compilation basis Assembly and the cords Working mode register Anomaly over Assembly summary Interrupt switch Interrupt concept Interrupt management |
HongMeng station | into a little bit every day, the original is not easy, welcome to reprint, please indicate the source.