Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Kernel Liteos A
提交
b9a445ca
K
Kernel Liteos A
项目概览
OpenHarmony
/
Kernel Liteos A
8 个月 前同步成功
通知
450
Star
414
Fork
55
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
4
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
Kernel Liteos A
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
4
Issue
4
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
b9a445ca
编写于
4月 07, 2022
作者:
O
openharmony_ci
提交者:
Gitee
4月 07, 2022
浏览文件
操作
浏览文件
下载
差异文件
!866 fix: 修复前后台进程支持
Merge pull request !866 from zhushengle/shell_
上级
18a2fc8e
338044cd
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
54 addition
and
69 deletion
+54
-69
apps/shell/include/shell_pri.h
apps/shell/include/shell_pri.h
+3
-1
apps/shell/include/shmsg.h
apps/shell/include/shmsg.h
+2
-2
apps/shell/src/main.c
apps/shell/src/main.c
+25
-11
apps/shell/src/shmsg.c
apps/shell/src/shmsg.c
+24
-55
未找到文件。
apps/shell/include/shell_pri.h
浏览文件 @
b9a445ca
...
...
@@ -32,13 +32,15 @@
#ifndef _SHELL_PRI_H
#define _SHELL_PRI_H
#include "shell.h"
#ifdef __cplusplus
#if __cplusplus
extern
"C"
{
#endif
/* __cplusplus */
#endif
/* __cplusplus */
extern
void
*
ShellEntry
(
void
*
argv
);
extern
void
ShellEntry
(
ShellCB
*
shellCB
);
extern
void
*
ShellTask
(
void
*
argv
);
#ifdef __cplusplus
...
...
apps/shell/include/shmsg.h
浏览文件 @
b9a445ca
...
...
@@ -32,6 +32,7 @@
#ifndef _SHMSG_H
#define _SHMSG_H
#include "shell_list.h"
#include "shell.h"
#ifdef __cplusplus
...
...
@@ -61,8 +62,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
ChildExec
(
const
char
*
cmdName
,
char
*
const
paramArray
[],
bool
foreground
);
extern
void
ShellCmdLineParse
(
char
c
,
OutputFunc
outputFunc
,
ShellCB
*
shellCB
);
extern
int
ShellNotify
(
ShellCB
*
shellCB
);
...
...
apps/shell/src/main.c
浏览文件 @
b9a445ca
...
...
@@ -31,13 +31,16 @@
#define _GNU_SOURCE
#include <signal.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/syscall.h>
#include "show.h"
#include "shmsg.h"
#include "shcmd.h"
#include "shell_pri.h"
#include "semaphore.h"
#include "securec.h"
#include "unistd.h"
#include <sys/syscall.h>
ShellCB
*
g_shellCB
=
NULL
;
...
...
@@ -77,13 +80,8 @@ static int OsShellCreateTask(ShellCB *shellCB)
goto
OUT
;
}
ret
=
ShellEntryInit
(
shellCB
);
if
(
ret
!=
SH_OK
)
{
goto
OUT
;
}
(
void
)
pthread_join
(
shellCB
->
shellTaskHandle
,
NULL
);
(
void
)
pthread_join
(
shellCB
->
shellEntryHandle
,
NULL
);
shellCB
->
shellEntryHandle
=
pthread_self
();
return
0
;
OUT:
ShellDeinit
(
shellCB
);
...
...
@@ -98,7 +96,7 @@ static int DoShellExec(char **argv)
char
*
cmdLine
=
NULL
;
if
(
strncmp
(
argv
[
0
],
SHELL_EXEC_COMMAND
,
SHELL_EXEC_COMMAND_BYTES
)
==
0
)
{
ChildExec
(
argv
[
1
],
argv
+
1
);
ChildExec
(
argv
[
1
],
argv
+
1
,
FALSE
);
}
for
(
i
=
0
;
argv
[
i
];
i
++
)
{
len
+=
strlen
(
argv
[
i
]);
...
...
@@ -125,11 +123,22 @@ static int DoShellExec(char **argv)
return
ret
;
}
static
void
ShellSigChildHook
(
int
sig
)
{
(
void
)
sig
;
while
(
waitpid
(
-
1
,
NULL
,
WNOHANG
)
>
0
)
{
continue
;
}
}
int
main
(
int
argc
,
char
**
argv
)
{
int
ret
=
SH_NOK
;
ShellCB
*
shellCB
=
NULL
;
(
void
)
signal
(
SIGCHLD
,
ShellSigChildHook
);
if
(
argc
>
1
)
{
ret
=
DoShellExec
(
argv
+
1
);
return
ret
;
...
...
@@ -165,7 +174,12 @@ int main(int argc, char **argv)
sem_init
(
&
shellCB
->
shellSem
,
0
,
0
);
g_shellCB
=
shellCB
;
return
OsShellCreateTask
(
shellCB
);
ret
=
OsShellCreateTask
(
shellCB
);
if
(
ret
!=
SH_OK
)
{
goto
ERR_OUT3
;
}
ShellEntry
(
shellCB
);
ERR_OUT3:
(
void
)
pthread_mutex_destroy
(
&
shellCB
->
historyMutex
);
...
...
apps/shell/src/shmsg.c
浏览文件 @
b9a445ca
...
...
@@ -351,7 +351,7 @@ char *GetCmdName(const char *cmdline, unsigned int len)
return
cmdName
;
}
void
ChildExec
(
const
char
*
cmdName
,
char
*
const
paramArray
[])
void
ChildExec
(
const
char
*
cmdName
,
char
*
const
paramArray
[]
,
bool
foreground
)
{
int
ret
;
pid_t
gid
;
...
...
@@ -367,10 +367,12 @@ void ChildExec(const char *cmdName, char *const paramArray[])
exit
(
1
);
}
ret
=
tcsetpgrp
(
STDIN_FILENO
,
gid
);
if
(
ret
!=
0
)
{
printf
(
"tcsetpgrp failed, errno %d
\n
"
,
errno
);
exit
(
1
);
if
(
!
foreground
)
{
ret
=
tcsetpgrp
(
STDIN_FILENO
,
gid
);
if
(
ret
!=
0
)
{
printf
(
"tcsetpgrp failed, errno %d
\n
"
,
errno
);
exit
(
1
);
}
}
ret
=
execve
(
cmdName
,
paramArray
,
NULL
);
...
...
@@ -404,20 +406,30 @@ int CheckExit(const char *cmdName, const CmdParsed *cmdParsed)
exit
(
ret
);
}
static
void
DoCmdExec
(
const
char
*
cmdName
,
const
char
*
cmdline
,
unsigned
int
len
,
const
CmdParsed
*
cmdParsed
)
static
void
DoCmdExec
(
const
char
*
cmdName
,
const
char
*
cmdline
,
unsigned
int
len
,
CmdParsed
*
cmdParsed
)
{
bool
foreground
=
FALSE
;
int
ret
;
pid_t
forkPid
;
if
(
strncmp
(
cmdline
,
CMD_EXEC_COMMAND
,
CMD_EXEC_COMMAND_BYTES
)
==
0
)
{
if
((
cmdParsed
->
paramCnt
>
1
)
&&
(
strcmp
(
cmdParsed
->
paramArray
[
cmdParsed
->
paramCnt
-
1
],
"&"
)
==
0
))
{
free
(
cmdParsed
->
paramArray
[
cmdParsed
->
paramCnt
-
1
]);
cmdParsed
->
paramArray
[
cmdParsed
->
paramCnt
-
1
]
=
NULL
;
cmdParsed
->
paramCnt
--
;
foreground
=
TRUE
;
}
forkPid
=
fork
();
if
(
forkPid
<
0
)
{
printf
(
"Faild to fork from shell
\n
"
);
return
;
}
else
if
(
forkPid
==
0
)
{
ChildExec
(
cmdParsed
->
paramArray
[
0
],
cmdParsed
->
paramArray
);
ChildExec
(
cmdParsed
->
paramArray
[
0
],
cmdParsed
->
paramArray
,
foreground
);
}
else
{
waitpid
(
forkPid
,
0
,
0
);
if
(
!
foreground
)
{
(
void
)
waitpid
(
forkPid
,
0
,
0
);
}
ret
=
tcsetpgrp
(
STDIN_FILENO
,
getpid
());
if
(
ret
!=
0
)
{
printf
(
"tcsetpgrp failed, errno %d
\n
"
,
errno
);
...
...
@@ -567,20 +579,10 @@ static void ExecCmdline(const char *cmdline)
free
(
output
);
}
void
RecycleZombieChild
(
void
)
{
while
(
waitpid
(
-
1
,
NULL
,
WNOHANG
)
>
0
)
{
continue
;
}
}
static
void
ShellCmdProcess
(
ShellCB
*
shellCB
)
{
char
*
buf
=
NULL
;
while
(
1
)
{
/* recycle zombine child process */
RecycleZombieChild
();
buf
=
GetCmdline
(
shellCB
);
char
*
buf
=
GetCmdline
(
shellCB
);
if
(
buf
==
NULL
)
{
break
;
}
...
...
@@ -654,25 +656,19 @@ static int ShellKernelReg(unsigned int shellHandle)
return
ioctl
(
STDIN_FILENO
,
CONSOLE_CONTROL_REG_USERTASK
,
shellHandle
);
}
void
*
ShellEntry
(
void
*
argv
)
void
ShellEntry
(
ShellCB
*
shellCB
)
{
char
ch
;
int
ret
;
int
n
;
pid_t
tid
=
syscall
(
__NR_gettid
);
ShellCB
*
shellCB
=
(
ShellCB
*
)
argv
;
if
(
shellCB
==
NULL
)
{
return
NULL
;
return
;
}
(
void
)
memset_s
(
shellCB
->
shellBuf
,
SHOW_MAX_LEN
,
0
,
SHOW_MAX_LEN
);
ret
=
prctl
(
PR_SET_NAME
,
"ShellEntry"
);
if
(
ret
!=
SH_OK
)
{
return
NULL
;
}
ret
=
ShellKernelReg
((
int
)
tid
);
if
(
ret
!=
0
)
{
printf
(
"another shell is already running!
\n
"
);
...
...
@@ -685,32 +681,5 @@ void *ShellEntry(void *argv)
ShellCmdLineParse
(
ch
,
(
OutputFunc
)
printf
,
shellCB
);
}
}
return
NULL
;
}
int
ShellEntryInit
(
ShellCB
*
shellCB
)
{
int
ret
;
size_t
stackSize
=
SHELL_ENTRY_STACKSIZE
;
void
*
arg
=
NULL
;
pthread_attr_t
attr
;
if
(
shellCB
==
NULL
)
{
return
SH_NOK
;
}
ret
=
pthread_attr_init
(
&
attr
);
if
(
ret
!=
SH_OK
)
{
return
SH_NOK
;
}
pthread_attr_setstacksize
(
&
attr
,
stackSize
);
arg
=
(
void
*
)
shellCB
;
ret
=
pthread_create
(
&
shellCB
->
shellEntryHandle
,
&
attr
,
&
ShellEntry
,
arg
);
if
(
ret
!=
SH_OK
)
{
return
SH_NOK
;
}
return
ret
;
return
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录