From cdc0877f163ce7508095dfbb6fa2ee8d1c920a46 Mon Sep 17 00:00:00 2001 From: huangshan Date: Wed, 3 Nov 2021 09:58:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DCtrl+C=E4=BC=9A=E7=BB=88?= =?UTF-8?q?=E6=AD=A2=E6=89=80=E6=9C=89=E8=BF=9B=E7=A8=8B=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: huangshan Change-Id: I5861569c63fd6c1adf8c702797e4d79eba76211f --- apps/shell/include/shmsg.h | 7 +++-- apps/shell/src/main.c | 5 +++- apps/shell/src/shmsg.c | 53 ++++++++++++++++++++++-------------- kernel/common/console.c | 15 +++++----- kernel/common/console.h | 2 +- kernel/common/los_magickey.c | 4 +-- kernel/common/los_magickey.h | 2 +- 7 files changed, 53 insertions(+), 35 deletions(-) diff --git a/apps/shell/include/shmsg.h b/apps/shell/include/shmsg.h index ee90ec6c..b182db34 100644 --- a/apps/shell/include/shmsg.h +++ b/apps/shell/include/shmsg.h @@ -43,8 +43,10 @@ extern "C" { #define SHELL_ENTRY_STACKSIZE 0x1000 #define SHELL_TASK_STACKSIZE 0x3000 -#define SHELL_EXEC_COMMAND "exec " -#define SHELL_EXEC_COMMAND_BYTES 5 +#define SHELL_EXEC_COMMAND "exec" +#define SHELL_EXEC_COMMAND_BYTES 4 +#define CMD_EXEC_COMMAND SHELL_EXEC_COMMAND" " +#define CMD_EXEC_COMMAND_BYTES (SHELL_EXEC_COMMAND_BYTES+1) #define CONSOLE_IOC_MAGIC 'c' #define CONSOLE_CONTROL_REG_USERTASK _IO(CONSOLE_IOC_MAGIC, 7) @@ -57,6 +59,7 @@ extern "C" { typedef void (*OutputFunc)(const char *fmt, ...); extern int ShellTaskInit(ShellCB *shellCB); extern int ShellEntryInit(ShellCB *shellCB); +extern void ChildExec(const char *cmdName, char *const paramArray[]); extern void ShellCmdLineParse(char c, OutputFunc outputFunc, ShellCB *shellCB); extern int ShellNotify(ShellCB *shellCB); diff --git a/apps/shell/src/main.c b/apps/shell/src/main.c index 0d08fa5e..1a691988 100644 --- a/apps/shell/src/main.c +++ b/apps/shell/src/main.c @@ -97,6 +97,9 @@ static int DoShellExec(char **argv) int ret = SH_NOK; char *cmdLine = NULL; + if (strncmp(argv[0], SHELL_EXEC_COMMAND, SHELL_EXEC_COMMAND_BYTES) == 0) { + ChildExec(argv[1], argv + 1); + } for (i = 0; argv[i]; i++) { len += strlen(argv[i]); } @@ -123,7 +126,7 @@ int main(int argc, char **argv) int ret = SH_NOK; ShellCB *shellCB = NULL; - if (!strcmp(argv[0], "shell") && argv[1]) { + if (argc > 1) { ret = DoShellExec(argv + 1); return ret; } diff --git a/apps/shell/src/shmsg.c b/apps/shell/src/shmsg.c index 795951d1..8329691b 100644 --- a/apps/shell/src/shmsg.c +++ b/apps/shell/src/shmsg.c @@ -331,38 +331,51 @@ char *GetCmdName(const char *cmdline, unsigned int len) return cmdName; } +void ChildExec(const char *cmdName, char *const paramArray[]) +{ + int ret; + pid_t gid; + + ret = setpgrp(); + if (ret == -1) { + exit(1); + } + + gid = getpgrp(); + if (gid < 0) { + printf("get group id failed, pgrpid %d, errno %d\n", gid, errno); + } + + ret = tcsetpgrp(STDIN_FILENO, gid); + if (ret != 0) { + printf("tcsetpgrp failed, errno %d\n", errno); + } + + ret = execve(cmdName, paramArray, NULL); + if (ret == -1) { + perror("execve"); + exit(-1); + } +} + static void DoCmdExec(const char *cmdName, const char *cmdline, unsigned int len, const CmdParsed *cmdParsed) { int ret; pid_t forkPid; - pid_t gid; - if (strncmp(cmdline, SHELL_EXEC_COMMAND, SHELL_EXEC_COMMAND_BYTES) == 0) { + if (strncmp(cmdline, CMD_EXEC_COMMAND, CMD_EXEC_COMMAND_BYTES) == 0) { forkPid = fork(); if (forkPid < 0) { printf("Faild to fork from shell\n"); return; } else if (forkPid == 0) { - ret = setpgrp(); - if (ret == -1) { - exit(1); - } - - gid = getpgrp(); - if (gid < 0) { - printf("get group id failed, pgrpid %d, errno %d\n", gid, errno); - } - - ret = tcsetpgrp(STDIN_FILENO, gid); + ChildExec(cmdParsed->paramArray[0], cmdParsed->paramArray); + } else { + waitpid(forkPid, 0, 0); + ret = tcsetpgrp(STDIN_FILENO, getpid()); if (ret != 0) { printf("tcsetpgrp failed, errno %d\n", errno); } - - ret = execve((const char *)cmdParsed->paramArray[0], (char * const *)cmdParsed->paramArray, NULL); - if (ret == -1) { - perror("execve"); - exit(-1); - } } } else { (void)syscall(__NR_shellexec, cmdName, cmdline); @@ -419,7 +432,7 @@ unsigned int PreHandleCmdline(const char *input, char **output, unsigned int *ou unsigned int removeLen = strlen("./"); /* "./" needs to be removed if it exists */ unsigned int ret; char *newCmd = NULL; - char *execCmd = SHELL_EXEC_COMMAND; + char *execCmd = CMD_EXEC_COMMAND; const char *cmdBuf = input; unsigned int cmdBufLen = strlen(cmdBuf); char *shiftStr = (char *)malloc(cmdBufLen + 1); diff --git a/kernel/common/console.c b/kernel/common/console.c index 992af94d..46cb1c8e 100644 --- a/kernel/common/console.c +++ b/kernel/common/console.c @@ -436,17 +436,16 @@ STATIC VOID StoreReadChar(CONSOLE_CB *consoleCB, char ch, INT32 readcount) } } -VOID KillPgrp() +VOID KillPgrp(UINT16 consoleId) { - INT32 consoleId; - LosProcessCB *process = OsCurrProcessGet(); - - if ((process->consoleID > CONSOLE_NUM - 1) || (process->consoleID < 0)) { + if ((consoleId > CONSOLE_NUM) || (consoleId <= 0)) { + return; + } + CONSOLE_CB *consoleCB = g_console[consoleId-1]; + /* the default of consoleCB->pgrpId is -1, may not be set yet, avoid killing all processes */ + if (consoleCB->pgrpId < 0) { return; } - - consoleId = process->consoleID; - CONSOLE_CB *consoleCB = g_console[consoleId]; (VOID)OsKillLock(consoleCB->pgrpId, SIGINT); } diff --git a/kernel/common/console.h b/kernel/common/console.h index 25122e83..b95ac48a 100644 --- a/kernel/common/console.h +++ b/kernel/common/console.h @@ -124,7 +124,7 @@ extern INT32 GetFilepOps(const struct file *filep, struct file **privFilep, cons extern VOID OsWaitConsoleSendTaskPend(UINT32 taskID); extern VOID OsWakeConsoleSendTask(VOID); #endif -extern VOID KillPgrp(VOID); +extern VOID KillPgrp(UINT16 consoleId); /* console ioctl */ #define CONSOLE_IOC_MAGIC 'c' diff --git a/kernel/common/los_magickey.c b/kernel/common/los_magickey.c index 1db5c88c..458faf24 100644 --- a/kernel/common/los_magickey.c +++ b/kernel/common/los_magickey.c @@ -118,14 +118,14 @@ STATIC VOID OsMagicMemCheck(VOID) } #endif -INT32 CheckMagicKey(CHAR key) +INT32 CheckMagicKey(CHAR key, UINT16 consoleId) { #ifdef LOSCFG_ENABLE_MAGICKEY INT32 i; STATIC UINT32 magicKeySwitch = 0; if (key == 0x03) { /* ctrl + c */ - KillPgrp(); + KillPgrp(consoleId); return 0; } else if (key == 0x12) { /* ctrl + r */ magicKeySwitch = ~magicKeySwitch; diff --git a/kernel/common/los_magickey.h b/kernel/common/los_magickey.h index 266d49ba..f04b7a7c 100644 --- a/kernel/common/los_magickey.h +++ b/kernel/common/los_magickey.h @@ -46,7 +46,7 @@ typedef struct { CHAR magicKey; } MagicKeyOp; -extern INT32 CheckMagicKey(CHAR key); +extern INT32 CheckMagicKey(CHAR key, UINT16 consoleId); #ifdef __cplusplus #if __cplusplus