未验证 提交 1ac4e171 编写于 作者: O openharmony_ci 提交者: Gitee

!947 系统优化:修改服务状态支持int

Merge pull request !947 from Mupceet/initservice
......@@ -29,10 +29,10 @@ extern "C" {
typedef enum {
SERVICE_IDLE = 0, // service add
SERVICE_STARTING, // service start
SERVICE_STARTED, // service ok
SERVICE_READY, // service ok
SERVICE_STARTED, // 2 service ok running
SERVICE_READY, // 3 service ok running
SERVICE_STOPPING,
SERVICE_STOPPED,
SERVICE_STOPPED, // 5
SERVICE_ERROR,
SERVICE_SUSPENDED,
SERVICE_FREEZED,
......
......@@ -154,6 +154,9 @@ int GetParameterName(uint32_t handle, char *key, uint32_t len);
int GetParameterValue(uint32_t handle, char *value, uint32_t len);
long long GetSystemCommitId(void);
int32_t GetIntParameter(const char *key, int32_t def);
uint32_t GetUintParameter(const char *key, uint32_t def);
#ifdef __cplusplus
#if __cplusplus
}
......
......@@ -22,8 +22,9 @@
#include "beget_ext.h"
#include "init_utils.h"
#include "securec.h"
#include "init_param.h"
#include "parameter.h"
#include "securec.h"
static int StartProcess(const char *name, const char *extArgv[], int extArgc)
{
......@@ -100,15 +101,8 @@ static int GetCurrentServiceStatus(const char *serviceName, ServiceStatus *statu
BEGET_LOGE("Failed snprintf_s err=%d", errno);
return -1;
}
char paramValue[PARAM_VALUE_LEN_MAX] = {0};
unsigned int valueLen = PARAM_VALUE_LEN_MAX;
if (SystemGetParameter(paramName, paramValue, &valueLen) != 0) {
BEGET_LOGE("Failed get paramName.");
return -1;
}
int size = 0;
const InitArgInfo *statusMap = GetServieStatusMap(&size);
*status = GetMapValue(paramValue, statusMap, size, SERVICE_IDLE);
uint32_t value = GetUintParameter(paramName, SERVICE_IDLE);
*status = (ServiceStatus)value;
return 0;
}
......@@ -184,13 +178,7 @@ int ServiceControl(const char *serviceName, int action)
int ServiceWaitForStatus(const char *serviceName, ServiceStatus status, int waitTimeout)
{
char *state = NULL;
int size = 0;
const InitArgInfo *statusMap = GetServieStatusMap(&size);
if (((int)status < size) && (statusMap[status].value == (int)status)) {
state = statusMap[status].name;
}
if (serviceName == NULL || state == NULL || waitTimeout <= 0) {
if (serviceName == NULL || waitTimeout <= 0) {
BEGET_LOGE("Service wait failed, service name is null or status invalid %d", status);
return -1;
}
......@@ -200,7 +188,12 @@ int ServiceWaitForStatus(const char *serviceName, ServiceStatus status, int wait
BEGET_LOGE("Failed snprintf_s err=%d", errno);
return -1;
}
if (SystemWaitParameter(paramName, state, waitTimeout) != 0) {
char value[MAX_INT_LEN] = {0};
if (snprintf_s(value, sizeof(value), sizeof(value) - 1, "%d", (int)status) == -1) {
BEGET_LOGE("Failed snprintf_s err=%d", errno);
return -1;
}
if (SystemWaitParameter(paramName, value, waitTimeout) != 0) {
BEGET_LOGE("Wait param for %s failed.", paramName);
return -1;
}
......@@ -220,7 +213,12 @@ int ServiceSetReady(const char *serviceName)
BEGET_LOGE("Failed snprintf_s err=%d", errno);
return -1;
}
if (SystemSetParameter(paramName, "ready") != 0) {
char value[MAX_INT_LEN] = {0};
if (snprintf_s(value, sizeof(value), sizeof(value) - 1, "%d", (int)SERVICE_READY) == -1) {
BEGET_LOGE("Failed snprintf_s err=%d", errno);
return -1;
}
if (SystemSetParameter(paramName, value) != 0) {
BEGET_LOGE("Set param for %s failed.", paramName);
return -1;
}
......
......@@ -28,9 +28,10 @@
static void ServiceStateChange(const char *key, const char *value, void *context)
{
ServiceStatusChangePtr callback = (ServiceStatusChangePtr)context;
int size = 0;
const InitArgInfo *statusMap = GetServieStatusMap(&size);
ServiceStatus status = (ServiceStatus)GetMapValue(value, statusMap, size, SERVICE_IDLE);
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;
if (strlen(key) > strlen(STARTUP_SERVICE_CTL)) {
callback(key + strlen(STARTUP_SERVICE_CTL) + 1, status);
} else {
......
......@@ -78,21 +78,6 @@ int GetParameter(const char *key, const char *def, char *value, uint32_t len)
return (ret != 0) ? ret : strlen(value);
}
int GetIntParameter(const char *key, int def)
{
int out = 0;
char value[32] = {0}; // 32 max for int
int ret = GetParameter(key, "0", value, sizeof(value));
if (ret != 0) {
return out;
}
long long int result = 0;
if (StringToLL(value, &result) != 0) {
return def;
}
return (int32_t)result;
}
int SetParameter(const char *key, const char *value)
{
if ((key == NULL) || (value == NULL)) {
......@@ -325,4 +310,38 @@ const char *GetBuildRootHash(void)
{
static const char *buildRootHash = NULL;
return GetProperty("const.ohos.buildroothash", &buildRootHash);
}
int32_t GetIntParameter(const char *key, int32_t def)
{
char value[MAX_INT_LEN] = {0};
int ret = GetParameter(key, "0", value, sizeof(value));
if (ret != 0) {
return def;
}
long long int result = 0;
if (StringToLL(value, &result) != 0) {
return def;
}
if (result <= INT32_MIN && result >= INT32_MAX) {
return def;
}
return (int32_t)result;
}
uint32_t GetUintParameter(const char *key, uint32_t def)
{
char value[MAX_INT_LEN] = {0};
int ret = GetParameter(key, "0", value, sizeof(value));
if (ret != 0) {
return def;
}
unsigned long long int result = 0;
if (StringToULL(value, &result) != 0) {
return def;
}
if (result >= UINT32_MAX) {
return def;
}
return (uint32_t)result;
}
\ No newline at end of file
......@@ -14,8 +14,8 @@
"setparam sys.usb.state ${sys.usb.config}"
]
}, {
"name" : "param:startup.service.ctl.hdcd=stopped",
"condition" : "startup.service.ctl.hdcd=stopped",
"name" : "param:startup.service.ctl.hdcd=5",
"condition" : "startup.service.ctl.hdcd=5",
"cmds" : [
"setparam sys.usb.ffs.ready 0"
]
......
......@@ -33,3 +33,4 @@ debug.bytrace. = root:system:0775
persist.distributed_hardware.device_manager. = system:system:0775
bootevent. = samgr:samgr:0777
hw_sc. = root:root:0777
startup.service.ctl. = system:servicectrl:0775:int
......@@ -34,6 +34,7 @@ typedef struct {
int value;
} InitArgInfo;
#define MAX_INT_LEN 20
#define HEX_BASE 16
#define BINARY_BASE 2
#define OCTAL_BASE 8
......@@ -75,8 +76,6 @@ void FreeStringVector(char **vector, int count);
int InUpdaterMode(void);
int StringReplaceChr(char *strl, char oldChr, char newChr);
int GetMapValue(const char *name, const InitArgInfo *infos, int argNum, int defValue);
const InitArgInfo *GetServieStatusMap(int *size);
uint32_t GetRandom(void);
void OpenConsole(void);
void TrimTail(char *str, char c);
......
......@@ -88,7 +88,7 @@ extern "C" {
typedef enum {
START_MODE_CONDITION,
START_MODE_BOOT,
START_MODE_NARMAL,
START_MODE_NORMAL,
} ServiceStartMode;
typedef enum {
......
......@@ -96,10 +96,14 @@ void DumpOneService(const Service *service)
const InitArgInfo startModeMap[] = {
{"condition", START_MODE_CONDITION},
{"boot", START_MODE_BOOT},
{"normal", START_MODE_NARMAL}
{"normal", START_MODE_NORMAL}
};
int size = 0;
const InitArgInfo *statusMap = GetServieStatusMap(&size);
const static char *serviceStatusMap[] = {
"created", "starting", "running", "ready",
"stopping", "stopped", "suspended", "freezed", "disabled", "critical"
};
printf("\tservice name: [%s] \n", service->name);
#ifdef WITH_SELINUX
if (service->secon != NULL) {
......@@ -110,8 +114,8 @@ void DumpOneService(const Service *service)
printf("\tservice crashCnt: [%d] \n", service->crashCnt);
printf("\tservice attribute: [%u] \n", service->attribute);
printf("\tservice importance: [%d] \n", service->importance);
printf("\tservice startMode: [%s] \n", startModeMap[service->status].name);
printf("\tservice status: [%s] \n", statusMap[service->status].name);
printf("\tservice startMode: [%s] \n", startModeMap[service->startMode].name);
printf("\tservice status: [%s] \n", serviceStatusMap[service->status]);
printf("\tservice perms uID [%u] \n", service->servPerm.uID);
DumpServiceArgs("path arg", &service->pathArgs);
DumpServiceArgs("writepid file", &service->writePidArgs);
......@@ -646,12 +650,25 @@ static int GetServiceOnDemand(const cJSON *curArrItem, Service *curServ)
return SERVICE_SUCCESS;
}
static int GetMapValue(const char *name, const InitArgInfo *infos, int argNum, int defValue)
{
if ((argNum == 0) || (infos == NULL) || (name == NULL)) {
return defValue;
}
for (int i = 0; i < argNum; i++) {
if (strcmp(infos[i].name, name) == 0) {
return infos[i].value;
}
}
return defValue;
}
static int GetServiceMode(Service *service, const cJSON *json)
{
const InitArgInfo startModeMap[] = {
{"condition", START_MODE_CONDITION},
{"boot", START_MODE_BOOT},
{"normal", START_MODE_NARMAL}
{"normal", START_MODE_NORMAL}
};
const InitArgInfo endModeMap[] = {
{"pre-fork", END_PRE_FORK},
......@@ -659,12 +676,12 @@ static int GetServiceMode(Service *service, const cJSON *json)
{"after-exec", END_AFTER_EXEC},
{"ready", END_RECV_READY}
};
service->startMode = START_MODE_NARMAL;
service->startMode = START_MODE_NORMAL;
service->endMode = END_AFTER_EXEC;
char *value = cJSON_GetStringValue(cJSON_GetObjectItem(json, "start-mode"));
if (value != NULL) {
service->startMode = GetMapValue(value,
(InitArgInfo *)startModeMap, (int)ARRAY_LENGTH(startModeMap), START_MODE_NARMAL);
(InitArgInfo *)startModeMap, (int)ARRAY_LENGTH(startModeMap), START_MODE_NORMAL);
}
value = cJSON_GetStringValue(cJSON_GetObjectItem(json, "end-mode"));
if (value != NULL) {
......
......@@ -273,7 +273,7 @@ static void BootStateChange(const char *content)
return;
}
if (strcmp("post-init", content) == 0) {
StartAllServices(START_MODE_NARMAL);
StartAllServices(START_MODE_NORMAL);
// Destroy all hooks
HookMgrDestroy(bootStageHookMgr);
bootStageHookMgr = NULL;
......
......@@ -36,19 +36,16 @@
void NotifyServiceChange(Service *service, int status)
{
int size = 0;
const InitArgInfo *statusMap = GetServieStatusMap(&size);
INIT_ERROR_CHECK(statusMap != NULL && size > status, service->status = status;
return, "Service status error %d", status);
INIT_LOGI("NotifyServiceChange %s %s to %s", service->name,
statusMap[service->status].name, statusMap[status].name);
INIT_LOGI("NotifyServiceChange %s %d to %d", service->name, service->status, status);
service->status = status;
INIT_CHECK(status != SERVICE_IDLE, return);
char paramName[PARAM_NAME_LEN_MAX] = { 0 };
int ret = snprintf_s(paramName, sizeof(paramName), sizeof(paramName) - 1,
"%s.%s", STARTUP_SERVICE_CTL, service->name);
if (ret >= 0) {
SystemWriteParam(paramName, statusMap[status].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);
}
}
......
......@@ -520,40 +520,6 @@ int StringReplaceChr(char *strl, char oldChr, char newChr)
return 0;
}
int GetMapValue(const char *name, const InitArgInfo *infos, int argNum, int defValue)
{
if ((argNum == 0) || (infos == NULL) || (name == NULL)) {
return defValue;
}
for (int i = 0; i < argNum; i++) {
if (strcmp(infos[i].name, name) == 0) {
return infos[i].value;
}
}
return defValue;
}
const static InitArgInfo g_servieStatusMap[] = {
{"created", SERVICE_IDLE},
{"starting", SERVICE_STARTING},
{"running", SERVICE_STARTED},
{"ready", SERVICE_READY},
{"stopping", SERVICE_STOPPING},
{"stopped", SERVICE_STOPPED},
{"suspended", SERVICE_SUSPENDED},
{"freezed", SERVICE_FREEZED},
{"disabled", SERVICE_DISABLED},
{"critical", SERVICE_CRITICAL}
};
const InitArgInfo *GetServieStatusMap(int *size)
{
if (size != 0) {
*size = ARRAY_LENGTH(g_servieStatusMap);
}
return g_servieStatusMap;
}
uint32_t GetRandom()
{
uint32_t ulSeed = 0;
......
......@@ -7,15 +7,15 @@
]
},
{
"name" : "param:startup.service.ctl.group-test-stage1=running",
"condition" : "startup.service.ctl.group-test-stage1=running",
"name" : "param:startup.service.ctl.group-test-stage1=2",
"condition" : "startup.service.ctl.group-test-stage1=2",
"cmds" : [
"start group-test-stage2"
]
},
{
"name" : "param:startup.service.ctl.group-test-stage2=running",
"condition" : "startup.service.ctl.group-test-stage2=running",
"name" : "param:startup.service.ctl.group-test-stage2=2",
"condition" : "startup.service.ctl.group-test-stage2=2",
"cmds" : [
"start group-test-stage3"
]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册