Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Startup Init Lite
提交
4be5c06f
S
Startup Init Lite
项目概览
OpenHarmony
/
Startup Init Lite
大约 1 年 前同步成功
通知
3
Star
37
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Startup Init Lite
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4be5c06f
编写于
12月 24, 2021
作者:
X
xionglei6
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add: socket service start ondemand
Signed-off-by:
N
xionglei6
<
xionglei6@huawei.com
>
上级
b6ee22cc
变更
7
显示空白变更内容
内联
并排
Showing
7 changed file
with
180 addition
and
4 deletion
+180
-4
services/BUILD.gn
services/BUILD.gn
+2
-0
services/init/include/init_service.h
services/init/include/init_service.h
+1
-0
services/init/include/init_service_manager.h
services/init/include/init_service_manager.h
+6
-0
services/init/init_common_service.c
services/init/init_common_service.c
+28
-3
services/init/init_service_manager.c
services/init/init_service_manager.c
+24
-1
services/init/lite/init_socket_manager.c
services/init/lite/init_socket_manager.c
+25
-0
services/init/standard/init_socket_manager.c
services/init/standard/init_socket_manager.c
+94
-0
未找到文件。
services/BUILD.gn
浏览文件 @
4be5c06f
...
@@ -41,6 +41,7 @@ if (defined(ohos_lite)) {
...
@@ -41,6 +41,7 @@ if (defined(ohos_lite)) {
"init/lite/init_reboot.c",
"init/lite/init_reboot.c",
"init/lite/init_service.c",
"init/lite/init_service.c",
"init/lite/init_signal_handler.c",
"init/lite/init_signal_handler.c",
"init/lite/init_socket_manager.c",
]
]
sources += init_common_sources
sources += init_common_sources
...
@@ -106,6 +107,7 @@ if (defined(ohos_lite)) {
...
@@ -106,6 +107,7 @@ if (defined(ohos_lite)) {
"init/standard/init_reboot.c",
"init/standard/init_reboot.c",
"init/standard/init_service.c",
"init/standard/init_service.c",
"init/standard/init_signal_handler.c",
"init/standard/init_signal_handler.c",
"init/standard/init_socket_manager.c",
"init/standard/switch_root.c",
"init/standard/switch_root.c",
]
]
...
...
services/init/include/init_service.h
浏览文件 @
4be5c06f
...
@@ -44,6 +44,7 @@ extern "C" {
...
@@ -44,6 +44,7 @@ extern "C" {
#define SERVICE_ATTR_DISABLED 0x040 // disabled
#define SERVICE_ATTR_DISABLED 0x040 // disabled
#define SERVICE_ATTR_CONSOLE 0x080 // console
#define SERVICE_ATTR_CONSOLE 0x080 // console
#define SERVICE_ATTR_DYNAMIC 0x100 // dynamic service
#define SERVICE_ATTR_DYNAMIC 0x100 // dynamic service
#define SERVICE_ATTR_ONDEMAND 0x200 // ondemand, manage socket by init
#define MAX_SERVICE_NAME 32
#define MAX_SERVICE_NAME 32
#define MAX_WRITEPID_FILES 100
#define MAX_WRITEPID_FILES 100
...
...
services/init/include/init_service_manager.h
浏览文件 @
4be5c06f
...
@@ -46,10 +46,16 @@ typedef struct {
...
@@ -46,10 +46,16 @@ typedef struct {
int
serviceCount
;
int
serviceCount
;
}
ServiceSpace
;
}
ServiceSpace
;
inline
bool
IsOnDemandService
(
Service
*
service
)
{
return
!!
(
service
->
attribute
&
SERVICE_ATTR_ONDEMAND
);
}
Service
*
GetServiceByPid
(
pid_t
pid
);
Service
*
GetServiceByPid
(
pid_t
pid
);
Service
*
GetServiceByName
(
const
char
*
servName
);
Service
*
GetServiceByName
(
const
char
*
servName
);
cJSON
*
GetArrayItem
(
const
cJSON
*
fileRoot
,
int
*
arrSize
,
const
char
*
arrName
);
cJSON
*
GetArrayItem
(
const
cJSON
*
fileRoot
,
int
*
arrSize
,
const
char
*
arrName
);
int
ParseOneService
(
const
cJSON
*
curItem
,
Service
*
service
);
int
ParseOneService
(
const
cJSON
*
curItem
,
Service
*
service
);
void
SocketPollInit
(
int
sockFd
,
const
char
*
serviceName
);
int
CreateAndPollSocket
(
Service
*
service
);
void
StartServiceByName
(
const
char
*
serviceName
,
bool
checkDynamic
);
void
StartServiceByName
(
const
char
*
serviceName
,
bool
checkDynamic
);
void
StopServiceByName
(
const
char
*
serviceName
);
void
StopServiceByName
(
const
char
*
serviceName
);
void
StopAllServices
(
int
flags
);
void
StopAllServices
(
int
flags
);
...
...
services/init/init_common_service.c
浏览文件 @
4be5c06f
...
@@ -32,6 +32,7 @@
...
@@ -32,6 +32,7 @@
#include "init_cmds.h"
#include "init_cmds.h"
#include "init_log.h"
#include "init_log.h"
#include "init_service.h"
#include "init_service.h"
#include "init_service_manager.h"
#include "init_service_socket.h"
#include "init_service_socket.h"
#include "init_utils.h"
#include "init_utils.h"
#include "securec.h"
#include "securec.h"
...
@@ -203,8 +204,11 @@ int ServiceStart(Service *service)
...
@@ -203,8 +204,11 @@ int ServiceStart(Service *service)
}
}
int
pid
=
fork
();
int
pid
=
fork
();
if
(
pid
==
0
)
{
if
(
pid
==
0
)
{
if
(
!
IsOnDemandService
(
service
))
{
int
ret
=
CreateServiceSocket
(
service
->
socketCfg
);
int
ret
=
CreateServiceSocket
(
service
->
socketCfg
);
INIT_ERROR_CHECK
(
ret
>=
0
,
_exit
(
PROCESS_EXIT_CODE
),
"service %s exit! create socket failed!"
,
service
->
name
);
INIT_ERROR_CHECK
(
ret
>=
0
,
_exit
(
PROCESS_EXIT_CODE
),
"service %s exit! create socket failed!"
,
service
->
name
);
}
CreateServiceFile
(
service
->
fileCfg
);
CreateServiceFile
(
service
->
fileCfg
);
if
(
service
->
attribute
&
SERVICE_ATTR_CONSOLE
)
{
if
(
service
->
attribute
&
SERVICE_ATTR_CONSOLE
)
{
OpenConsole
();
OpenConsole
();
...
@@ -278,6 +282,20 @@ static int ExecRestartCmd(const Service *service)
...
@@ -278,6 +282,20 @@ static int ExecRestartCmd(const Service *service)
return
SERVICE_SUCCESS
;
return
SERVICE_SUCCESS
;
}
}
static
void
PollSocketAfresh
(
service
)
{
if
(
service
->
socketCfg
==
NULL
)
{
INIT_LOGE
(
"service %s socket config is NULL!"
,
service
->
name
);
return
;
}
ServiceSocket
*
tmpSock
=
service
->
socketCfg
;
while
(
tmpSock
!=
NULL
)
{
SocketPollInit
(
tmpSock
->
sockFd
,
service
->
name
);
tmpSock
=
tmpSock
->
next
;
}
return
;
}
void
ServiceReap
(
Service
*
service
)
void
ServiceReap
(
Service
*
service
)
{
{
INIT_CHECK
(
service
!=
NULL
,
return
);
INIT_CHECK
(
service
!=
NULL
,
return
);
...
@@ -290,7 +308,9 @@ void ServiceReap(Service *service)
...
@@ -290,7 +308,9 @@ void ServiceReap(Service *service)
return
;
return
;
}
}
if
(
!
IsOnDemandService
(
service
))
{
CloseServiceSocket
(
service
->
socketCfg
);
CloseServiceSocket
(
service
->
socketCfg
);
}
CloseServiceFile
(
service
->
fileCfg
);
CloseServiceFile
(
service
->
fileCfg
);
// stopped by system-init itself, no need to restart even if it is not one-shot service
// stopped by system-init itself, no need to restart even if it is not one-shot service
if
(
service
->
attribute
&
SERVICE_ATTR_NEED_STOP
)
{
if
(
service
->
attribute
&
SERVICE_ATTR_NEED_STOP
)
{
...
@@ -321,6 +341,11 @@ void ServiceReap(Service *service)
...
@@ -321,6 +341,11 @@ void ServiceReap(Service *service)
return
;
return
;
}
}
}
}
// service no need to restart which socket managed by init until socket message detected
if
(
IsOnDemandService
(
service
))
{
PollSocketAfresh
(
service
);
return
;
}
int
ret
;
int
ret
;
if
(
service
->
restartArg
!=
NULL
)
{
if
(
service
->
restartArg
!=
NULL
)
{
...
...
services/init/init_service_manager.c
浏览文件 @
4be5c06f
...
@@ -24,6 +24,7 @@
...
@@ -24,6 +24,7 @@
#include "init.h"
#include "init.h"
#include "init_jobs_internal.h"
#include "init_jobs_internal.h"
#include "init_log.h"
#include "init_log.h"
#include "init_param.h"
#include "init_service_file.h"
#include "init_service_file.h"
#include "init_service_socket.h"
#include "init_service_socket.h"
#include "init_utils.h"
#include "init_utils.h"
...
@@ -359,6 +360,9 @@ static int ParseServiceSocket(const cJSON *curArrItem, Service *curServ)
...
@@ -359,6 +360,9 @@ static int ParseServiceSocket(const cJSON *curArrItem, Service *curServ)
break
;
break
;
}
}
}
}
if
(
IsOnDemandService
(
curServ
))
{
ret
=
CreateAndPollSocket
(
curServ
);
}
return
ret
;
return
ret
;
}
}
...
@@ -466,11 +470,28 @@ static int GetDynamicService(const cJSON *curArrItem, Service *curServ)
...
@@ -466,11 +470,28 @@ static int GetDynamicService(const cJSON *curArrItem, Service *curServ)
return
SERVICE_SUCCESS
;
return
SERVICE_SUCCESS
;
}
}
static
int
GetServiceOnDemand
(
const
cJSON
*
curArrItem
,
Service
*
curServ
)
{
cJSON
*
item
=
cJSON_GetObjectItem
(
curArrItem
,
"ondemand"
);
if
(
item
==
NULL
)
{
return
SERVICE_SUCCESS
;
}
INIT_ERROR_CHECK
(
cJSON_IsBool
(
item
),
return
SERVICE_FAILURE
,
"Service : %s ondemand value only support bool."
,
curServ
->
name
);
bool
isOnDemand
=
(
bool
)
cJSON_GetNumberValue
(
item
);
INIT_INFO_CHECK
(
isOnDemand
,
return
SERVICE_SUCCESS
,
"Service : %s ondemand value is false, it will be manage socket by itself"
,
curServ
->
name
);
curServ
->
attribute
|=
SERVICE_ATTR_ONDEMAND
;
return
SERVICE_SUCCESS
;
}
static
int
CheckServiceKeyName
(
const
cJSON
*
curService
)
static
int
CheckServiceKeyName
(
const
cJSON
*
curService
)
{
{
char
*
cfgServiceKeyList
[]
=
{
char
*
cfgServiceKeyList
[]
=
{
"name"
,
"path"
,
"uid"
,
"gid"
,
"once"
,
"importance"
,
"caps"
,
"disabled"
,
"name"
,
"path"
,
"uid"
,
"gid"
,
"once"
,
"importance"
,
"caps"
,
"disabled"
,
"writepid"
,
"critical"
,
"socket"
,
"console"
,
"dynamic"
,
"file"
,
"writepid"
,
"critical"
,
"socket"
,
"console"
,
"dynamic"
,
"file"
,
"ondemand"
,
#ifdef WITH_SELINUX
#ifdef WITH_SELINUX
SECON_STR_IN_CFG
,
SECON_STR_IN_CFG
,
#endif // WITH_SELINUX
#endif // WITH_SELINUX
...
@@ -533,6 +554,8 @@ int ParseOneService(const cJSON *curItem, Service *service)
...
@@ -533,6 +554,8 @@ int ParseOneService(const cJSON *curItem, Service *service)
INIT_ERROR_CHECK
(
ret
==
0
,
return
SERVICE_FAILURE
,
"Failed to get caps for service %s"
,
service
->
name
);
INIT_ERROR_CHECK
(
ret
==
0
,
return
SERVICE_FAILURE
,
"Failed to get caps for service %s"
,
service
->
name
);
ret
=
GetDynamicService
(
curItem
,
service
);
ret
=
GetDynamicService
(
curItem
,
service
);
INIT_ERROR_CHECK
(
ret
==
0
,
return
SERVICE_FAILURE
,
"Failed to get dynamic flag for service %s"
,
service
->
name
);
INIT_ERROR_CHECK
(
ret
==
0
,
return
SERVICE_FAILURE
,
"Failed to get dynamic flag for service %s"
,
service
->
name
);
ret
=
GetServiceOnDemand
(
curItem
,
service
);
INIT_ERROR_CHECK
(
ret
==
0
,
return
SERVICE_FAILURE
,
"Failed to get ondemand flag for service %s"
,
service
->
name
);
return
ret
;
return
ret
;
}
}
...
...
services/init/lite/init_socket_manager.c
0 → 100644
浏览文件 @
4be5c06f
/*
* Copyright (c) 2020-2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "init_service_manager.h"
void
SocketPollInit
(
int
sockFd
,
const
char
*
serviceName
)
{
return
;
}
int
CreateAndPollSocket
(
Service
*
service
)
{
return
0
;
}
services/init/standard/init_socket_manager.c
0 → 100644
浏览文件 @
4be5c06f
/*
* Copyright (c) 2020-2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <sys/epoll.h>
#include "init_log.h"
#include "init_param.h"
#include "init_service_manager.h"
#include "securec.h"
#include "uv.h"
#define POLL_HANDLER_COUNT 1024
uv_poll_t
g_socketPollHandler
[
POLL_HANDLER_COUNT
];
static
int
handlerCounter
=
0
;
static
void
UVSocketPollHandler
(
uv_poll_t
*
handle
,
int
status
,
int
events
)
{
if
(
handle
==
NULL
)
{
INIT_LOGE
(
"handle is NULL!"
);
return
;
}
char
paramName
[
PARAM_NAME_LEN_MAX
]
=
{
0
};
char
paramValue
[
PARAM_VALUE_LEN_MAX
]
=
{
0
};
unsigned
int
len
=
PARAM_VALUE_LEN_MAX
;
INIT_CHECK_ONLY_ELOG
(
snprintf_s
(
paramName
,
PARAM_NAME_LEN_MAX
,
PARAM_NAME_LEN_MAX
-
1
,
"init.socket.%d"
,
handle
->
io_watcher
.
fd
)
>=
0
,
"snprintf_s paramName error %d "
,
errno
);
int
ret
=
SystemReadParam
(
paramName
,
paramValue
,
&
len
);
if
(
ret
!=
0
)
{
INIT_LOGE
(
"Failed to read param, param name = %s"
,
paramName
);
}
INIT_LOGI
(
"Socket information detected, sockFd:%d service name:%s"
,
handle
->
io_watcher
.
fd
,
paramValue
);
uv_poll_stop
(
handle
);
StartServiceByName
(
paramValue
,
false
);
return
;
}
static
void
SetSocketParam
(
Service
*
service
)
{
char
paramName
[
PARAM_NAME_LEN_MAX
]
=
{
0
};
ServiceSocket
*
tmpSock
=
service
->
socketCfg
;
while
(
tmpSock
!=
NULL
)
{
INIT_CHECK_ONLY_ELOG
(
snprintf_s
(
paramName
,
PARAM_NAME_LEN_MAX
,
PARAM_NAME_LEN_MAX
-
1
,
"init.socket.%d"
,
tmpSock
->
sockFd
)
>=
0
,
"snprintf_s paramName error %d "
,
errno
);
SystemWriteParam
(
paramName
,
service
->
name
);
(
void
)
memset_s
(
paramName
,
PARAM_NAME_LEN_MAX
,
0
,
PARAM_NAME_LEN_MAX
);
tmpSock
=
tmpSock
->
next
;
}
}
void
SocketPollInit
(
int
sockFd
,
const
char
*
serviceName
)
{
if
(
handlerCounter
>=
POLL_HANDLER_COUNT
)
{
INIT_LOGE
(
"Socket poll handler is not enough!"
);
return
;
}
int
ret
=
uv_poll_init_user_defined
(
uv_default_loop
(),
&
g_socketPollHandler
[
handlerCounter
],
sockFd
);
if
(
ret
!=
0
)
{
INIT_LOGE
(
"Failed to init socket poll!"
);
return
;
}
ret
=
uv_poll_start
(
&
g_socketPollHandler
[
handlerCounter
],
UV_READABLE
,
UVSocketPollHandler
);
if
(
ret
!=
0
)
{
INIT_LOGE
(
"Failed to start socket poll!"
);
return
;
}
handlerCounter
+=
1
;
INIT_LOGI
(
"Start to poll socket, sockFd:%d service name:%s"
,
sockFd
,
serviceName
);
}
int
CreateAndPollSocket
(
Service
*
service
)
{
int
ret
=
CreateServiceSocket
(
service
->
socketCfg
);
if
(
ret
!=
0
)
{
INIT_LOGE
(
"Create socket failed!"
);
return
SERVICE_FAILURE
;
}
SetSocketParam
(
service
);
ServiceSocket
*
tmpSock
=
service
->
socketCfg
;
while
(
tmpSock
!=
NULL
)
{
SocketPollInit
(
tmpSock
->
sockFd
,
service
->
name
);
tmpSock
=
tmpSock
->
next
;
}
return
0
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录