From 262f673fa6cdf82891cfaabdbd0d0d75fc4244a9 Mon Sep 17 00:00:00 2001 From: xionglei6 Date: Fri, 1 Apr 2022 22:13:34 +0800 Subject: [PATCH] fix: ondemand replace dynamic Signed-off-by: xionglei6 --- .../service_control/service_control.c | 6 +-- services/init/include/init_service.h | 9 ++--- services/init/include/init_service_manager.h | 2 +- services/init/init_common_cmds.c | 4 +- services/init/init_common_service.c | 5 +-- services/init/init_service_manager.c | 38 ++----------------- services/param/trigger/trigger_processor.c | 2 +- .../test_startup_ServiceControlStart_fuzzer.h | 4 +- .../test_startup_ServiceControlStop_fuzzer.h | 4 +- test/unittest/common/cmd_func_test.cpp | 2 +- test/unittest/init/group_unittest.cpp | 2 +- test/unittest/init/service_unittest.cpp | 4 +- 12 files changed, 24 insertions(+), 58 deletions(-) diff --git a/interfaces/innerkits/service_control/service_control.c b/interfaces/innerkits/service_control/service_control.c index 1a8396f6..6b232b7d 100644 --- a/interfaces/innerkits/service_control/service_control.c +++ b/interfaces/innerkits/service_control/service_control.c @@ -28,7 +28,7 @@ static int StartProcess(const char *name, const char *extArgv[], int extArgc) { if (name == NULL) { - BEGET_LOGE("Start dynamic service failed, service name is null."); + BEGET_LOGE("Start ondemand service failed, service name is null."); return -1; } int extraArg = 0; @@ -79,7 +79,7 @@ static int StartProcess(const char *name, const char *extArgv[], int extArgc) static int StopProcess(const char *serviceName) { if (serviceName == NULL) { - BEGET_LOGE("Stop dynamic service failed, service is null.\n"); + BEGET_LOGE("Stop ondemand service failed, service is null.\n"); return -1; } if (SystemSetParameter("ohos.ctl.stop", serviceName) != 0) { @@ -112,7 +112,7 @@ static int GetCurrentServiceStatus(const char *serviceName, ServiceStatus *statu static int RestartProcess(const char *serviceName, const char *extArgv[], int extArgc) { if (serviceName == NULL) { - BEGET_LOGE("Restart dynamic service failed, service is null.\n"); + BEGET_LOGE("Restart ondemand service failed, service is null.\n"); return -1; } ServiceStatus status = SERVICE_IDLE; diff --git a/services/init/include/init_service.h b/services/init/include/init_service.h index 6472c0e3..6d588506 100644 --- a/services/init/include/init_service.h +++ b/services/init/include/init_service.h @@ -48,11 +48,10 @@ extern "C" { #define SERVICE_ATTR_CRITICAL 0x020 // critical, will reboot if it crash 4 times in 4 minutes #define SERVICE_ATTR_DISABLED 0x040 // disabled #define SERVICE_ATTR_CONSOLE 0x080 // console -#define SERVICE_ATTR_DYNAMIC 0x100 // dynamic service -#define SERVICE_ATTR_ONDEMAND 0x200 // ondemand, manage socket by init -#define SERVICE_ATTR_TIMERSTART 0x400 // Mark a service will be started by timer -#define SERVICE_ATTR_NEEDWAIT 0x800 // Mark a service will be started by timer -#define SERVICE_ATTR_SANDBOX 0x1000 // make service will enter sandbox +#define SERVICE_ATTR_ONDEMAND 0x100 // ondemand, manage socket by init +#define SERVICE_ATTR_TIMERSTART 0x200 // Mark a service will be started by timer +#define SERVICE_ATTR_NEEDWAIT 0x400 // Mark a service will be started by timer +#define SERVICE_ATTR_SANDBOX 0x800 // make service will enter sandbox #define MAX_SERVICE_NAME 32 #define MAX_APL_NAME 32 diff --git a/services/init/include/init_service_manager.h b/services/init/include/init_service_manager.h index 19e868ae..35080b1d 100644 --- a/services/init/include/init_service_manager.h +++ b/services/init/include/init_service_manager.h @@ -54,7 +54,7 @@ Service *GetServiceByName(const char *servName); cJSON *GetArrayItem(const cJSON *fileRoot, int *arrSize, const char *arrName); int ParseOneService(const cJSON *curItem, Service *service); -void StartServiceByName(const char *serviceName, bool checkDynamic); +void StartServiceByName(const char *serviceName); void StopServiceByName(const char *serviceName); void StopAllServices(int flags, const char **exclude, int size, int (*filter)(const Service *service, const char **exclude, int size)); diff --git a/services/init/init_common_cmds.c b/services/init/init_common_cmds.c index 2c0ea155..bb69fe87 100644 --- a/services/init/init_common_cmds.c +++ b/services/init/init_common_cmds.c @@ -202,7 +202,7 @@ static void DoWait(const struct CmdArgs *ctx) static void DoStart(const struct CmdArgs *ctx) { INIT_LOGV("DoStart %s", ctx->argv[0]); - StartServiceByName(ctx->argv[0], true); + StartServiceByName(ctx->argv[0]); } static void DoStop(const struct CmdArgs *ctx) @@ -226,7 +226,7 @@ static void DoReset(const struct CmdArgs *ctx) return; } } else { - StartServiceByName(ctx->argv[0], false); + StartServiceByName(ctx->argv[0]); } return; } diff --git a/services/init/init_common_service.c b/services/init/init_common_service.c index b49b0f96..ae028b72 100644 --- a/services/init/init_common_service.c +++ b/services/init/init_common_service.c @@ -397,10 +397,9 @@ static int ExecRestartCmd(Service *service) return SERVICE_SUCCESS; } -static void PollSocketAfresh(Service *service) +static void CheckServiceSocket(Service *service) { if (service->socketCfg == NULL) { - INIT_LOGE("service %s socket config is NULL!", service->name); return; } ServiceSocket *tmpSock = service->socketCfg; @@ -468,7 +467,7 @@ void ServiceReap(Service *service) } // service no need to restart which socket managed by init until socket message detected if (IsOnDemandService(service)) { - PollSocketAfresh(service); + CheckServiceSocket(service); return; } diff --git a/services/init/init_service_manager.c b/services/init/init_service_manager.c index ae758e12..b0be5f31 100644 --- a/services/init/init_service_manager.c +++ b/services/init/init_service_manager.c @@ -598,25 +598,6 @@ static bool IsServiceInMainStrap(Service *curServ) return false; } -static int GetDynamicService(const cJSON *curArrItem, Service *curServ) -{ - cJSON *item = cJSON_GetObjectItem(curArrItem, "dynamic"); - if (item == NULL) { - return SERVICE_SUCCESS; - } - - INIT_ERROR_CHECK(cJSON_IsBool(item), return SERVICE_FAILURE, - "Service : %s dynamic value only support bool.", curServ->name); - INIT_INFO_CHECK(cJSON_IsTrue(item), return SERVICE_SUCCESS, - "Service : %s dynamic value is false, it will be started with init.", curServ->name); - INIT_CHECK_RETURN_VALUE(!IsServiceInMainStrap(curServ), SERVICE_SUCCESS); - INIT_LOGI("%s is dynamic service", curServ->name); - - curServ->attribute |= SERVICE_ATTR_DYNAMIC; - curServ->attribute |= SERVICE_ATTR_ONCE; - return SERVICE_SUCCESS; -} - static int GetServiceOnDemand(const cJSON *curArrItem, Service *curServ) { cJSON *item = cJSON_GetObjectItem(curArrItem, "ondemand"); @@ -627,7 +608,7 @@ static int GetServiceOnDemand(const cJSON *curArrItem, Service *curServ) INIT_ERROR_CHECK(cJSON_IsBool(item), return SERVICE_FAILURE, "Service : %s ondemand value only support bool.", curServ->name); INIT_INFO_CHECK(cJSON_IsTrue(item), return SERVICE_SUCCESS, - "Service : %s ondemand value is false, it will be manage socket by itself", curServ->name); + "Service : %s ondemand value is false, it should be pulled up by init", curServ->name); if (curServ->attribute & SERVICE_ATTR_CRITICAL) { INIT_LOGE("Service : %s is invalid which has both critical and ondemand attribute", curServ->name); return SERVICE_FAILURE; @@ -641,7 +622,7 @@ static int CheckServiceKeyName(const cJSON *curService) { char *cfgServiceKeyList[] = { "name", "path", "uid", "gid", "once", "importance", "caps", "disabled", - "writepid", "critical", "socket", "console", "dynamic", "file", "ondemand", + "writepid", "critical", "socket", "console", "file", "ondemand", "d-caps", "apl", "jobs", "start-mode", "end-mode", "cpucore", "secon", "sandbox" }; INIT_CHECK_RETURN_VALUE(curService != NULL, SERVICE_FAILURE); @@ -847,8 +828,6 @@ int ParseOneService(const cJSON *curItem, Service *service) INIT_ERROR_CHECK(ret == 0, return SERVICE_FAILURE, "Failed to get sandbox for service %s", service->name); ret = GetServiceCaps(curItem, service); INIT_ERROR_CHECK(ret == 0, return SERVICE_FAILURE, "Failed to get caps for service %s", service->name); - ret = GetDynamicService(curItem, service); - 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); ret = GetServiceMode(service, curItem); @@ -945,7 +924,7 @@ static Service *GetServiceByExtServName(const char *fullServName) return service; } -void StartServiceByName(const char *servName, bool checkDynamic) +void StartServiceByName(const char *servName) { INIT_LOGE("StartServiceByName Service %s", servName); Service *service = GetServiceByName(servName); @@ -954,11 +933,6 @@ void StartServiceByName(const char *servName, bool checkDynamic) } INIT_ERROR_CHECK(service != NULL, return, "Cannot find service %s.", servName); - if (checkDynamic && (service->attribute & SERVICE_ATTR_DYNAMIC)) { - INIT_LOGI("%s is dynamic service.", servName); - NotifyServiceChange(service, SERVICE_STOPPED); - return; - } if (ServiceStart(service) != SERVICE_SUCCESS) { INIT_LOGE("Service %s start failed!", servName); } @@ -1049,12 +1023,6 @@ void StartAllServices(int startMode) node = GetNextGroupNode(NODE_TYPE_SERVICES, node); continue; } - if (service->attribute & SERVICE_ATTR_DYNAMIC) { - INIT_LOGI("%s is dynamic service.", service->name); - NotifyServiceChange(service, SERVICE_STOPPED); - node = GetNextGroupNode(NODE_TYPE_SERVICES, node); - continue; - } if (ServiceStart(service) != SERVICE_SUCCESS) { INIT_LOGE("Service %s start failed!", service->name); } diff --git a/services/param/trigger/trigger_processor.c b/services/param/trigger/trigger_processor.c index 44d8cac5..7af1adae 100644 --- a/services/param/trigger/trigger_processor.c +++ b/services/param/trigger/trigger_processor.c @@ -189,7 +189,7 @@ static void SendTriggerEvent(int type, const char *content, uint32_t contentLen) } else if (strncmp(content, OHOS_SERVICE_CTRL_PREFIX, prefixSize) == 0) { DoServiceCtrlTrigger(content + prefixSize, contentLen - prefixSize, 1); } else if (strncmp(content, OHOS_CTRL_START, strlen(OHOS_CTRL_START)) == 0) { - StartServiceByName(content + strlen(OHOS_CTRL_START), false); + StartServiceByName(content + strlen(OHOS_CTRL_START)); } else if (strncmp(content, OHOS_CTRL_STOP, strlen(OHOS_CTRL_STOP)) == 0) { StopServiceByName(content + strlen(OHOS_CTRL_STOP)); } else { diff --git a/test/fuzztest/test_startup_ServiceControlStart_fuzzer/test_startup_ServiceControlStart_fuzzer.h b/test/fuzztest/test_startup_ServiceControlStart_fuzzer/test_startup_ServiceControlStart_fuzzer.h index 5e0c6c64..1194742a 100644 --- a/test/fuzztest/test_startup_ServiceControlStart_fuzzer/test_startup_ServiceControlStart_fuzzer.h +++ b/test/fuzztest/test_startup_ServiceControlStart_fuzzer/test_startup_ServiceControlStart_fuzzer.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef TEST_FUZZTEST_STARTDYNAMICPROCESS_FUZZER_H -#define TEST_FUZZTEST_STARTDYNAMICPROCESS_FUZZER_H +#ifndef TEST_FUZZTEST_STARTONDEMANDPROCESS_FUZZER_H +#define TEST_FUZZTEST_STARTONDEMANDPROCESS_FUZZER_H #include "fuzz_utils.h" #define FUZZ_PROJECT_NAME "ServiceControlStart_fuzzer" #endif diff --git a/test/fuzztest/test_startup_ServiceControlStop_fuzzer/test_startup_ServiceControlStop_fuzzer.h b/test/fuzztest/test_startup_ServiceControlStop_fuzzer/test_startup_ServiceControlStop_fuzzer.h index ee79e68b..2b666b20 100644 --- a/test/fuzztest/test_startup_ServiceControlStop_fuzzer/test_startup_ServiceControlStop_fuzzer.h +++ b/test/fuzztest/test_startup_ServiceControlStop_fuzzer/test_startup_ServiceControlStop_fuzzer.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef TEST_FUZZTEST_STOPDYNAMICPROCESS_FUZZER_H -#define TEST_FUZZTEST_STOPDYNAMICPROCESS_FUZZER_H +#ifndef TEST_FUZZTEST_STOPONDEMANDPROCESS_FUZZER_H +#define TEST_FUZZTEST_STOPONDEMANDPROCESS_FUZZER_H #include "fuzz_utils.h" #define FUZZ_PROJECT_NAME "ServiceControlStop_fuzzer" #endif diff --git a/test/unittest/common/cmd_func_test.cpp b/test/unittest/common/cmd_func_test.cpp index 75255e89..71148222 100644 --- a/test/unittest/common/cmd_func_test.cpp +++ b/test/unittest/common/cmd_func_test.cpp @@ -957,7 +957,7 @@ HWTEST_F(StartupInitUTest, cmdJobTest_001, TestSize.Level0) DoJob(nullptr); DoJob("job name does not exist"); ReleaseAllJobs(); - StartServiceByName("service name does not exist", false); + StartServiceByName("service name does not exist"); StopAllServices(0, nullptr, 0, nullptr); ServiceReap(nullptr); EXPECT_NE(0, ServiceStart(nullptr)); diff --git a/test/unittest/init/group_unittest.cpp b/test/unittest/init/group_unittest.cpp index f6089861..a6f0a1f7 100644 --- a/test/unittest/init/group_unittest.cpp +++ b/test/unittest/init/group_unittest.cpp @@ -335,7 +335,7 @@ HWTEST_F(InitGroupManagerUnitTest, TestAddService2, TestSize.Level1) HWTEST_F(InitGroupManagerUnitTest, TestParseServiceCpucore, TestSize.Level1) { const char *jsonStr = "{\"services\":{\"name\":\"test_service22\",\"path\":[\"/data/init_ut/test_service\"]," - "\"importance\":-20,\"uid\":\"root\",\"writepid\":[\"/dev/test_service\"],\"console\":1,\"dynamic\":true," + "\"importance\":-20,\"uid\":\"root\",\"writepid\":[\"/dev/test_service\"],\"console\":1," "\"gid\":[\"root\"], \"cpucore\":[5, 2, 4, 1, 2, 0, 1]}}"; cJSON* jobItem = cJSON_Parse(jsonStr); ASSERT_NE(nullptr, jobItem); diff --git a/test/unittest/init/service_unittest.cpp b/test/unittest/init/service_unittest.cpp index d6ee85f4..df6d09ea 100644 --- a/test/unittest/init/service_unittest.cpp +++ b/test/unittest/init/service_unittest.cpp @@ -48,7 +48,7 @@ public: HWTEST_F(ServiceUnitTest, case01, TestSize.Level1) { const char *jsonStr = "{\"services\":{\"name\":\"test_service\",\"path\":[\"/data/init_ut/test_service\"]," - "\"importance\":-20,\"uid\":\"system\",\"writepid\":[\"/dev/test_service\"],\"console\":1,\"dynamic\":true," + "\"importance\":-20,\"uid\":\"system\",\"writepid\":[\"/dev/test_service\"],\"console\":1," "\"gid\":[\"system\"]}}"; cJSON* jobItem = cJSON_Parse(jsonStr); ASSERT_NE(nullptr, jobItem); @@ -70,7 +70,7 @@ HWTEST_F(ServiceUnitTest, case01, TestSize.Level1) HWTEST_F(ServiceUnitTest, TestServiceStartAbnormal, TestSize.Level1) { const char *jsonStr = "{\"services\":{\"name\":\"test_service1\",\"path\":[\"/data/init_ut/test_service\"]," - "\"importance\":-20,\"uid\":\"system\",\"writepid\":[\"/dev/test_service\"],\"console\":1,\"dynamic\":true," + "\"importance\":-20,\"uid\":\"system\",\"writepid\":[\"/dev/test_service\"],\"console\":1," "\"gid\":[\"system\"]}}"; cJSON* jobItem = cJSON_Parse(jsonStr); ASSERT_NE(nullptr, jobItem); -- GitLab