!693 fix: 修复Ctrl+C会终止所有进程的问题

Merge pull request !693 from huangshan/fix-pgrp
This commit is contained in:
openharmony_ci 2021-11-05 05:58:22 +00:00 committed by Gitee
commit 5077b1793f
7 changed files with 53 additions and 35 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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'

View File

@ -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;

View File

@ -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