Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Startup Init Lite
提交
a4325cad
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看板
未验证
提交
a4325cad
编写于
4月 06, 2023
作者:
O
openharmony_ci
提交者:
Gitee
4月 06, 2023
浏览文件
操作
浏览文件
下载
差异文件
!1868 需求:提供service status pid 通知
Merge pull request !1868 from cheng_jinsong/servicestate
上级
5e60ce73
9a683b2d
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
153 addition
and
26 deletion
+153
-26
interfaces/innerkits/include/service_watcher.h
interfaces/innerkits/include/service_watcher.h
+9
-1
interfaces/innerkits/service_watcher/service_watcher.c
interfaces/innerkits/service_watcher/service_watcher.c
+22
-10
services/init/init_common_service.c
services/init/init_common_service.c
+1
-1
services/init/standard/init_service.c
services/init/standard/init_service.c
+13
-4
services/param/base/param_comm.c
services/param/base/param_comm.c
+2
-0
test/fuzztest/ServiceWatchForStatus_fuzzer/ServiceWatchForStatus_fuzzer.cpp
...iceWatchForStatus_fuzzer/ServiceWatchForStatus_fuzzer.cpp
+2
-2
test/moduletest/param_test_cmds.c
test/moduletest/param_test_cmds.c
+34
-4
test/moduletest/service_watcher_moduleTest.cpp
test/moduletest/service_watcher_moduleTest.cpp
+27
-2
test/moduletest/syspara_moduleTest.cpp
test/moduletest/syspara_moduleTest.cpp
+41
-0
test/unittest/param/watcher_agent_unittest.cpp
test/unittest/param/watcher_agent_unittest.cpp
+2
-2
未找到文件。
interfaces/innerkits/include/service_watcher.h
浏览文件 @
a4325cad
...
...
@@ -16,6 +16,7 @@
#ifndef SERVICE_WATCH_API_H
#define SERVICE_WATCH_API_H
#include <unistd.h>
#include "init_param.h"
#include "service_control.h"
...
...
@@ -25,7 +26,14 @@ extern "C" {
#endif
#endif
typedef
void
(
*
ServiceStatusChangePtr
)(
const
char
*
key
,
ServiceStatus
status
);
#define INVALID_PID 0
typedef
struct
serviceInfo
{
ServiceStatus
status
;
pid_t
pid
;
}
ServiceInfo
;
typedef
void
(
*
ServiceStatusChangePtr
)(
const
char
*
key
,
const
ServiceInfo
*
serviceInfo
);
int
ServiceWatchForStatus
(
const
char
*
serviceName
,
ServiceStatusChangePtr
changeCallback
);
#ifdef __cplusplus
...
...
interfaces/innerkits/service_watcher/service_watcher.c
浏览文件 @
a4325cad
...
...
@@ -31,9 +31,17 @@ static void ServiceStateChange(const char *key, const char *value, void *context
uint32_t
v
=
0
;
int
ret
=
StringToUint
(
value
,
&
v
);
BEGET_ERROR_CHECK
(
ret
==
0
,
return
,
"Failed to get value from %s"
,
value
);
ServiceStatus
status
=
(
ServiceStatus
)
v
;
// get pid
char
paramName
[
PARAM_NAME_LEN_MAX
]
=
{
0
};
ret
=
snprintf_s
(
paramName
,
sizeof
(
paramName
),
sizeof
(
paramName
)
-
1
,
"%s.pid"
,
key
);
BEGET_ERROR_CHECK
(
ret
!=
-
1
,
return
,
"Failed to get format pid ret %d for %s "
,
ret
,
key
);
ServiceInfo
info
=
{
0
};
info
.
status
=
(
ServiceStatus
)
v
;
info
.
pid
=
(
pid_t
)
GetUintParameter
(
paramName
,
INVALID_PID
);
if
(
strlen
(
key
)
>
strlen
(
STARTUP_SERVICE_CTL
))
{
callback
(
key
+
strlen
(
STARTUP_SERVICE_CTL
)
+
1
,
status
);
callback
(
key
+
strlen
(
STARTUP_SERVICE_CTL
)
+
1
,
&
info
);
}
else
{
BEGET_LOGE
(
"Invalid service name %s %s"
,
key
,
value
);
}
...
...
@@ -41,19 +49,23 @@ static void ServiceStateChange(const char *key, const char *value, void *context
int
ServiceWatchForStatus
(
const
char
*
serviceName
,
ServiceStatusChangePtr
changeCallback
)
{
if
(
serviceName
==
NULL
)
{
BEGET_LOGE
(
"Service wait failed, service is null."
);
return
-
1
;
}
BEGET_ERROR_CHECK
(
serviceName
!=
NULL
,
return
EC_INVALID
,
"Service watch failed, service is null."
);
BEGET_ERROR_CHECK
(
changeCallback
!=
NULL
,
return
EC_INVALID
,
"Service watch failed, callback is null."
);
char
paramName
[
PARAM_NAME_LEN_MAX
]
=
{
0
};
BEGET_LOGI
(
"Watcher service %s status"
,
serviceName
);
if
(
snprintf_s
(
paramName
,
PARAM_NAME_LEN_MAX
,
PARAM_NAME_LEN_MAX
-
1
,
"%s.%s"
,
STARTUP_SERVICE_CTL
,
serviceName
)
==
-
1
)
{
BEGET_LOGE
(
"Failed snprintf_s err=%d"
,
errno
);
return
-
1
;
return
EC_SYSTEM_ERR
;
}
if
(
SystemWatchParameter
(
paramName
,
ServiceStateChange
,
(
void
*
)
changeCallback
)
!=
0
)
{
BEGET_LOGE
(
"Wait param for %s failed."
,
paramName
);
return
-
1
;
int
ret
=
SystemWatchParameter
(
paramName
,
ServiceStateChange
,
(
void
*
)
changeCallback
);
if
(
ret
!=
0
)
{
BEGET_LOGE
(
"Failed to watcher service %s ret %d."
,
serviceName
,
ret
);
if
(
ret
==
DAC_RESULT_FORBIDED
)
{
return
SYSPARAM_PERMISSION_DENIED
;
}
return
EC_SYSTEM_ERR
;
}
return
0
;
}
...
...
services/init/init_common_service.c
浏览文件 @
a4325cad
...
...
@@ -414,6 +414,7 @@ int ServiceStart(Service *service)
int
ServiceStop
(
Service
*
service
)
{
INIT_ERROR_CHECK
(
service
!=
NULL
,
return
SERVICE_FAILURE
,
"stop service failed! null ptr."
);
NotifyServiceChange
(
service
,
SERVICE_STOPPING
);
if
(
service
->
serviceJobs
.
jobsName
[
JOB_ON_STOP
]
!=
NULL
)
{
DoJobNow
(
service
->
serviceJobs
.
jobsName
[
JOB_ON_STOP
]);
}
...
...
@@ -435,7 +436,6 @@ int ServiceStop(Service *service)
}
INIT_ERROR_CHECK
(
kill
(
service
->
pid
,
GetKillServiceSig
(
service
->
name
))
==
0
,
return
SERVICE_FAILURE
,
"stop service %s pid %d failed! err %d."
,
service
->
name
,
service
->
pid
,
errno
);
NotifyServiceChange
(
service
,
SERVICE_STOPPING
);
INIT_LOGI
(
"stop service %s, pid %d."
,
service
->
name
,
service
->
pid
);
service
->
pid
=
-
1
;
NotifyServiceChange
(
service
,
SERVICE_STOPPED
);
...
...
services/init/standard/init_service.c
浏览文件 @
a4325cad
...
...
@@ -46,11 +46,20 @@ void NotifyServiceChange(Service *service, int status)
char
paramName
[
PARAM_NAME_LEN_MAX
]
=
{
0
};
int
ret
=
snprintf_s
(
paramName
,
sizeof
(
paramName
),
sizeof
(
paramName
)
-
1
,
"%s.%s"
,
STARTUP_SERVICE_CTL
,
service
->
name
);
INIT_ERROR_CHECK
(
ret
>
0
,
return
,
"Failed to format service name %s."
,
service
->
name
);
char
statusStr
[
MAX_INT_LEN
]
=
{
0
};
int
ret1
=
snprintf_s
(
statusStr
,
sizeof
(
statusStr
),
sizeof
(
statusStr
)
-
1
,
"%d"
,
status
);
if
(
ret
>=
0
&&
ret1
>
0
)
{
SystemWriteParam
(
paramName
,
statusStr
);
}
ret
=
snprintf_s
(
statusStr
,
sizeof
(
statusStr
),
sizeof
(
statusStr
)
-
1
,
"%d"
,
status
);
INIT_ERROR_CHECK
(
ret
>
0
,
return
,
"Failed to format service status %s."
,
service
->
name
);
SystemWriteParam
(
paramName
,
statusStr
);
// write pid
ret
=
snprintf_s
(
paramName
,
sizeof
(
paramName
),
sizeof
(
paramName
)
-
1
,
"%s.%s.pid"
,
STARTUP_SERVICE_CTL
,
service
->
name
);
INIT_ERROR_CHECK
(
ret
>
0
,
return
,
"Failed to format service pid name %s."
,
service
->
name
);
ret
=
snprintf_s
(
statusStr
,
sizeof
(
statusStr
),
sizeof
(
statusStr
)
-
1
,
"%u"
,
(
service
->
pid
==
-
1
)
?
0
:
service
->
pid
);
INIT_ERROR_CHECK
(
ret
>
0
,
return
,
"Failed to format service pid %s."
,
service
->
name
);
SystemWriteParam
(
paramName
,
statusStr
);
}
int
IsForbidden
(
const
char
*
fieldStr
)
...
...
services/param/base/param_comm.c
浏览文件 @
a4325cad
...
...
@@ -35,6 +35,8 @@ INIT_LOCAL_API WorkSpace *GetWorkSpace(uint32_t labelIndex)
{
ParamWorkSpace
*
paramSpace
=
GetParamWorkSpace
();
PARAM_CHECK
(
paramSpace
!=
NULL
,
return
NULL
,
"Invalid paramSpace"
);
PARAM_CHECK
(
paramSpace
->
workSpace
!=
NULL
,
return
NULL
,
"Invalid paramSpace->workSpace"
);
PARAM_WORKSPACE_CHECK
(
paramSpace
,
return
NULL
,
"Invalid space"
);
#ifdef PARAM_SUPPORT_SELINUX
if
(
labelIndex
==
0
)
{
return
paramSpace
->
workSpace
[
0
];
...
...
test/fuzztest/ServiceWatchForStatus_fuzzer/ServiceWatchForStatus_fuzzer.cpp
浏览文件 @
a4325cad
...
...
@@ -18,9 +18,9 @@
#include "service_watcher.h"
#include "fuzz_utils.h"
void
callback
(
const
char
*
key
,
ServiceStatus
status
)
void
callback
(
const
char
*
key
,
const
ServiceInfo
*
status
)
{
printf
(
"key is: %s, ServiceStatus is: %d
\n
"
,
key
,
status
);
printf
(
"key is: %s, ServiceStatus is: %d
\n
"
,
key
,
status
->
status
);
}
namespace
OHOS
{
...
...
test/moduletest/param_test_cmds.c
浏览文件 @
a4325cad
...
...
@@ -246,10 +246,10 @@ static int32_t BShellParamCmdDisplay(BShellHandle shell, int32_t argc, char *arg
return
0
;
}
void
ServiceStatusChangeTest
(
const
char
*
key
,
ServiceStatus
status
)
void
ServiceStatusChangeTest
(
const
char
*
key
,
const
ServiceInfo
*
status
)
{
PLUGIN_LOGI
(
"group-test-stage3: wait service %s status: %d"
,
key
,
status
);
if
(
status
==
SERVICE_READY
||
status
==
SERVICE_STARTED
)
{
PLUGIN_LOGI
(
"group-test-stage3: wait service %s status: %d"
,
key
,
status
->
status
);
if
(
status
->
status
==
SERVICE_READY
||
status
->
status
==
SERVICE_STARTED
)
{
PLUGIN_LOGI
(
"Service %s start work"
,
key
);
}
}
...
...
@@ -340,6 +340,33 @@ static int32_t BShellParamCmdMemGet(BShellHandle shell, int32_t argc, char *argv
return
0
;
}
void
CmdServiceStatusChange
(
const
char
*
key
,
const
ServiceInfo
*
status
)
{
static
const
char
*
serviceStatusMap
[]
=
{
"idle"
,
"starting"
,
"running"
,
"ready"
,
"stopping"
,
"stopped"
,
"error"
,
"suspended"
,
"freezed"
,
"disabled"
,
"critical"
,
};
PLUGIN_CHECK
(
key
!=
NULL
&&
status
!=
NULL
,
return
,
"Invalid parameter"
);
printf
(
"Service %s status: %s pid %d
\n
"
,
key
,
((
status
->
status
<
ARRAY_LENGTH
(
serviceStatusMap
))
?
serviceStatusMap
[
status
->
status
]
:
"unknown"
),
status
->
pid
);
}
static
int32_t
BShellParamCmdWatchService
(
BShellHandle
shell
,
int32_t
argc
,
char
*
argv
[])
{
PLUGIN_CHECK
(
argc
>
1
,
return
-
1
,
"Invalid parameter"
);
return
ServiceWatchForStatus
(
argv
[
1
],
CmdServiceStatusChange
);
}
int32_t
BShellCmdRegister
(
BShellHandle
shell
,
int
execMode
)
{
if
(
execMode
==
0
)
{
...
...
@@ -354,12 +381,15 @@ int32_t BShellCmdRegister(BShellHandle shell, int execMode)
const
CmdInfo
infos
[]
=
{
{
"display"
,
BShellParamCmdDisplay
,
"display system service"
,
"display service"
,
"display service"
},
{
"read"
,
BShellParamCmdRead
,
"read system parameter"
,
"read [start | stop]"
,
""
},
{
"watcher"
,
BShellParamCmdWatch
,
"watcher system parameter"
,
"watcher [name]"
,
""
},
{
"watcher"
,
BShellParamCmdWatch
,
"watcher system parameter"
,
"watcher parameter [name]"
,
"watcher parameter"
},
{
"install"
,
BShellParamCmdInstall
,
"install plugin"
,
"install [name]"
,
""
},
{
"uninstall"
,
BShellParamCmdInstall
,
"uninstall plugin"
,
"uninstall [name]"
,
""
},
{
"group"
,
BShellParamCmdGroupTest
,
"group test"
,
"group test [stage]"
,
"group test"
},
{
"display"
,
BShellParamCmdUdidGet
,
"display udid"
,
"display udid"
,
"display udid"
},
{
"display"
,
BShellParamCmdMemGet
,
"display memory pid"
,
"display memory [pid]"
,
"display memory"
},
{
"watcher"
,
BShellParamCmdWatchService
,
"watcher service status"
,
"watcher service [name]"
,
"watcher service"
},
};
for
(
size_t
i
=
0
;
i
<
sizeof
(
infos
)
/
sizeof
(
infos
[
0
]);
i
++
)
{
BShellEnvRegisterCmd
(
GetShellHandle
(),
&
infos
[
i
]);
...
...
test/moduletest/service_watcher_moduleTest.cpp
浏览文件 @
a4325cad
...
...
@@ -16,6 +16,8 @@
#include <thread>
#include <string>
#include <gtest/gtest.h>
#include "parameter.h"
#include "service_control.h"
#include "service_watcher.h"
#include "test_utils.h"
...
...
@@ -31,9 +33,11 @@ public:
void
TearDown
(
void
)
{};
};
static
void
ServiceStatusChange
(
const
char
*
key
,
ServiceStatus
status
)
static
void
ServiceStatusChange
(
const
char
*
key
,
const
ServiceInfo
*
status
)
{
std
::
cout
<<
"service Name is: "
<<
key
<<
", ServiceStatus is: "
<<
status
<<
std
::
endl
;
std
::
cout
<<
"service Name is: "
<<
key
;
std
::
cout
<<
", ServiceStatus is: "
<<
status
->
status
;
std
::
cout
<<
", pid is: "
<<
status
->
pid
<<
std
::
endl
;
}
HWTEST_F
(
ServiceWatcherModuleTest
,
serviceWatcher_test_001
,
TestSize
.
Level0
)
...
...
@@ -65,4 +69,25 @@ HWTEST_F(ServiceWatcherModuleTest, serviceWatcher_test_002, TestSize.Level0)
EXPECT_TRUE
(
status
==
"stopped"
);
GTEST_LOG_
(
INFO
)
<<
"serviceWatcher_test_002 end"
;
}
HWTEST_F
(
ServiceWatcherModuleTest
,
serviceWatcher_test_003
,
TestSize
.
Level0
)
{
GTEST_LOG_
(
INFO
)
<<
"serviceWatcher_test_003 start"
;
const
std
::
string
serviceName
=
"deviceinfoservice"
;
// watcher service status
int
ret
=
ServiceWatchForStatus
(
serviceName
.
c_str
(),
ServiceStatusChange
);
EXPECT_EQ
(
ret
,
0
);
// No matter if service exist or not, ServiceWatchForStatus always success.
// start service
char
udid
[
65
]
=
{};
// 65 udid len
ret
=
AclGetDevUdid
(
udid
,
sizeof
(
udid
));
EXPECT_EQ
(
ret
,
0
);
auto
status1
=
GetServiceStatus
(
serviceName
);
EXPECT_TRUE
(
status1
==
"running"
);
// wait service exit
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
seconds
(
80
));
// wait sa died 80s
auto
status2
=
GetServiceStatus
(
serviceName
);
EXPECT_TRUE
(
status2
==
"stopped"
);
GTEST_LOG_
(
INFO
)
<<
"serviceWatcher_test_003 end"
;
}
}
test/moduletest/syspara_moduleTest.cpp
浏览文件 @
a4325cad
...
...
@@ -404,4 +404,45 @@ HWTEST_F(SysparaModuleTest, Syspara_GetParameter_test_011, TestSize.Level0)
GTEST_LOG_
(
INFO
)
<<
"Syspara_GetParameter_test_011 end"
;
}
HWTEST_F
(
SysparaModuleTest
,
Syspara_CacheParameter_test_001
,
TestSize
.
Level0
)
{
GTEST_LOG_
(
INFO
)
<<
"Syspara_CacheParameter_test_001 start"
;
const
char
*
name
=
"test.write.1111111.222222"
;
CachedHandle
cacheHandle
=
CachedParameterCreate
(
name
,
"true"
);
EXPECT_NE
(
cacheHandle
,
nullptr
);
const
char
*
value
=
CachedParameterGet
(
cacheHandle
);
EXPECT_EQ
(
strcmp
(
value
,
"true"
),
0
);
int
ret
=
SetParameter
(
name
,
"false"
);
EXPECT_EQ
(
ret
,
0
);
value
=
CachedParameterGet
(
cacheHandle
);
EXPECT_EQ
(
strcmp
(
value
,
"false"
),
0
);
CachedParameterDestroy
(
cacheHandle
);
GTEST_LOG_
(
INFO
)
<<
"Syspara_CacheParameter_test_001 end"
;
}
HWTEST_F
(
SysparaModuleTest
,
Syspara_CacheParameter_test_002
,
TestSize
.
Level0
)
{
GTEST_LOG_
(
INFO
)
<<
"Syspara_CacheParameter_test_002 start"
;
const
char
*
name
=
"test.write.1111111.333333"
;
int
ret
=
SetParameter
(
name
,
"3333"
);
EXPECT_EQ
(
ret
,
0
);
CachedHandle
cacheHandle3
=
CachedParameterCreate
(
name
,
"true"
);
EXPECT_NE
(
cacheHandle3
,
nullptr
);
const
char
*
value
=
CachedParameterGet
(
cacheHandle3
);
EXPECT_EQ
(
strcmp
(
value
,
"3333"
),
0
);
ret
=
SetParameter
(
name
,
"2222222"
);
EXPECT_EQ
(
ret
,
0
);
int
valueChange
=
0
;
value
=
CachedParameterGetChanged
(
cacheHandle3
,
&
valueChange
);
EXPECT_EQ
(
strcmp
(
value
,
"2222222"
),
0
);
EXPECT_EQ
(
valueChange
,
1
);
CachedParameterDestroy
(
cacheHandle3
);
GTEST_LOG_
(
INFO
)
<<
"Syspara_CacheParameter_test_002 end"
;
}
}
\ No newline at end of file
test/unittest/param/watcher_agent_unittest.cpp
浏览文件 @
a4325cad
...
...
@@ -42,9 +42,9 @@ static void TestParameterChange(const char *key, const char *value, void *contex
g_callbackCount
++
;
}
static
void
TestWatcherCallBack
(
const
char
*
key
,
ServiceStatus
status
)
static
void
TestWatcherCallBack
(
const
char
*
key
,
const
ServiceInfo
*
status
)
{
printf
(
"TestWatcherCallBack key:%s %d"
,
key
,
status
);
printf
(
"TestWatcherCallBack key:%s %d"
,
key
,
status
->
status
);
}
class
WatcherAgentUnitTest
:
public
::
testing
::
Test
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录