提交 4f2b3466 编写于 作者: M Mupceet

改造服务状态为int

Signed-off-by: NMupceet <laiguizhong@huawei.com>
上级 bae80ccc
...@@ -29,10 +29,10 @@ extern "C" { ...@@ -29,10 +29,10 @@ extern "C" {
typedef enum { typedef enum {
SERVICE_IDLE = 0, // service add SERVICE_IDLE = 0, // service add
SERVICE_STARTING, // service start SERVICE_STARTING, // service start
SERVICE_STARTED, // service ok SERVICE_STARTED, // 2 service ok running
SERVICE_READY, // service ok SERVICE_READY, // 3 service ok running
SERVICE_STOPPING, SERVICE_STOPPING,
SERVICE_STOPPED, SERVICE_STOPPED, // 5
SERVICE_ERROR, SERVICE_ERROR,
SERVICE_SUSPENDED, SERVICE_SUSPENDED,
SERVICE_FREEZED, SERVICE_FREEZED,
......
...@@ -154,6 +154,9 @@ int GetParameterName(uint32_t handle, char *key, uint32_t len); ...@@ -154,6 +154,9 @@ int GetParameterName(uint32_t handle, char *key, uint32_t len);
int GetParameterValue(uint32_t handle, char *value, uint32_t len); int GetParameterValue(uint32_t handle, char *value, uint32_t len);
long long GetSystemCommitId(void); long long GetSystemCommitId(void);
int32_t GetIntParameter(const char *key, int32_t def);
uint32_t GetUintParameter(const char *key, uint32_t def);
#ifdef __cplusplus #ifdef __cplusplus
#if __cplusplus #if __cplusplus
} }
......
...@@ -29,9 +29,10 @@ ...@@ -29,9 +29,10 @@
static void ServiceStateChange(const char *key, const char *value, void *context) static void ServiceStateChange(const char *key, const char *value, void *context)
{ {
ServiceStatusChangePtr callback = (ServiceStatusChangePtr)context; ServiceStatusChangePtr callback = (ServiceStatusChangePtr)context;
int size = 0; uint32_t v = 0;
const InitArgInfo *statusMap = GetServieStatusMap(&size); int ret = StringToUint(value, &v);
ServiceStatus status = (ServiceStatus)GetMapValue(value, statusMap, size, SERVICE_IDLE); BEGET_ERROR_CHECK(ret == 0, return, "Failed to get value from %s", value);
ServiceStatus status = (ServiceStatus)v;
if (strlen(key) > strlen(STARTUP_SERVICE_CTL)) { if (strlen(key) > strlen(STARTUP_SERVICE_CTL)) {
callback(key + strlen(STARTUP_SERVICE_CTL) + 1, status); callback(key + strlen(STARTUP_SERVICE_CTL) + 1, status);
} else { } else {
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "param_comm.h" #include "param_comm.h"
#include "init_param.h" #include "init_param.h"
#include "init_utils.h"
#include "sysparam_errno.h" #include "sysparam_errno.h"
#include "securec.h" #include "securec.h"
#include "beget_ext.h" #include "beget_ext.h"
...@@ -77,21 +78,6 @@ int GetParameter(const char *key, const char *def, char *value, uint32_t len) ...@@ -77,21 +78,6 @@ int GetParameter(const char *key, const char *def, char *value, uint32_t len)
return (ret != 0) ? ret : strlen(value); 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) int SetParameter(const char *key, const char *value)
{ {
if ((key == NULL) || (value == NULL)) { if ((key == NULL) || (value == NULL)) {
...@@ -314,4 +300,38 @@ const char *GetBuildRootHash(void) ...@@ -314,4 +300,38 @@ const char *GetBuildRootHash(void)
{ {
static const char *buildRootHash = NULL; static const char *buildRootHash = NULL;
return GetProperty("const.ohos.buildroothash", &buildRootHash); 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 @@ ...@@ -14,8 +14,8 @@
"setparam sys.usb.state ${sys.usb.config}" "setparam sys.usb.state ${sys.usb.config}"
] ]
}, { }, {
"name" : "param:startup.service.ctl.hdcd=stopped", "name" : "param:startup.service.ctl.hdcd=5",
"condition" : "startup.service.ctl.hdcd=stopped", "condition" : "startup.service.ctl.hdcd=5",
"cmds" : [ "cmds" : [
"setparam sys.usb.ffs.ready 0" "setparam sys.usb.ffs.ready 0"
] ]
......
...@@ -33,3 +33,4 @@ debug.bytrace. = root:system:0775 ...@@ -33,3 +33,4 @@ debug.bytrace. = root:system:0775
persist.distributed_hardware.device_manager. = system:system:0775 persist.distributed_hardware.device_manager. = system:system:0775
bootevent. = samgr:samgr:0777 bootevent. = samgr:samgr:0777
hw_sc. = root:root:0777 hw_sc. = root:root:0777
startup.service.ctl. = root:root:0775:int
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h> #include <unistd.h>
#include "beget_ext.h"
#ifdef __cplusplus #ifdef __cplusplus
#if __cplusplus #if __cplusplus
extern "C" { extern "C" {
...@@ -32,6 +34,8 @@ typedef struct { ...@@ -32,6 +34,8 @@ typedef struct {
int value; int value;
} InitArgInfo; } InitArgInfo;
#define MAX_INT_LEN 20
#define HEX_BASE 16
#define BINARY_BASE 2 #define BINARY_BASE 2
#define OCTAL_BASE 8 #define OCTAL_BASE 8
#define DECIMAL_BASE 10 #define DECIMAL_BASE 10
...@@ -72,8 +76,6 @@ void FreeStringVector(char **vector, int count); ...@@ -72,8 +76,6 @@ void FreeStringVector(char **vector, int count);
int InUpdaterMode(void); int InUpdaterMode(void);
int StringReplaceChr(char *strl, char oldChr, char newChr); 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); uint32_t GetRandom(void);
void OpenConsole(void); void OpenConsole(void);
#ifdef __cplusplus #ifdef __cplusplus
......
...@@ -88,7 +88,7 @@ extern "C" { ...@@ -88,7 +88,7 @@ extern "C" {
typedef enum { typedef enum {
START_MODE_CONDITION, START_MODE_CONDITION,
START_MODE_BOOT, START_MODE_BOOT,
START_MODE_NARMAL, START_MODE_NORMAL,
} ServiceStartMode; } ServiceStartMode;
typedef enum { typedef enum {
......
...@@ -96,10 +96,14 @@ void DumpOneService(const Service *service) ...@@ -96,10 +96,14 @@ void DumpOneService(const Service *service)
const InitArgInfo startModeMap[] = { const InitArgInfo startModeMap[] = {
{"condition", START_MODE_CONDITION}, {"condition", START_MODE_CONDITION},
{"boot", START_MODE_BOOT}, {"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); printf("\tservice name: [%s] \n", service->name);
#ifdef WITH_SELINUX #ifdef WITH_SELINUX
if (service->secon != NULL) { if (service->secon != NULL) {
...@@ -110,8 +114,8 @@ void DumpOneService(const Service *service) ...@@ -110,8 +114,8 @@ void DumpOneService(const Service *service)
printf("\tservice crashCnt: [%d] \n", service->crashCnt); printf("\tservice crashCnt: [%d] \n", service->crashCnt);
printf("\tservice attribute: [%u] \n", service->attribute); printf("\tservice attribute: [%u] \n", service->attribute);
printf("\tservice importance: [%d] \n", service->importance); printf("\tservice importance: [%d] \n", service->importance);
printf("\tservice startMode: [%s] \n", startModeMap[service->status].name); printf("\tservice startMode: [%s] \n", startModeMap[service->startMode].name);
printf("\tservice status: [%s] \n", statusMap[service->status].name); printf("\tservice status: [%s] \n", serviceStatusMap[service->status]);
printf("\tservice perms uID [%u] \n", service->servPerm.uID); printf("\tservice perms uID [%u] \n", service->servPerm.uID);
DumpServiceArgs("path arg", &service->pathArgs); DumpServiceArgs("path arg", &service->pathArgs);
DumpServiceArgs("writepid file", &service->writePidArgs); DumpServiceArgs("writepid file", &service->writePidArgs);
...@@ -646,12 +650,25 @@ static int GetServiceOnDemand(const cJSON *curArrItem, Service *curServ) ...@@ -646,12 +650,25 @@ static int GetServiceOnDemand(const cJSON *curArrItem, Service *curServ)
return SERVICE_SUCCESS; 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) static int GetServiceMode(Service *service, const cJSON *json)
{ {
const InitArgInfo startModeMap[] = { const InitArgInfo startModeMap[] = {
{"condition", START_MODE_CONDITION}, {"condition", START_MODE_CONDITION},
{"boot", START_MODE_BOOT}, {"boot", START_MODE_BOOT},
{"normal", START_MODE_NARMAL} {"normal", START_MODE_NORMAL}
}; };
const InitArgInfo endModeMap[] = { const InitArgInfo endModeMap[] = {
{"pre-fork", END_PRE_FORK}, {"pre-fork", END_PRE_FORK},
...@@ -659,12 +676,12 @@ static int GetServiceMode(Service *service, const cJSON *json) ...@@ -659,12 +676,12 @@ static int GetServiceMode(Service *service, const cJSON *json)
{"after-exec", END_AFTER_EXEC}, {"after-exec", END_AFTER_EXEC},
{"ready", END_RECV_READY} {"ready", END_RECV_READY}
}; };
service->startMode = START_MODE_NARMAL; service->startMode = START_MODE_NORMAL;
service->endMode = END_AFTER_EXEC; service->endMode = END_AFTER_EXEC;
char *value = cJSON_GetStringValue(cJSON_GetObjectItem(json, "start-mode")); char *value = cJSON_GetStringValue(cJSON_GetObjectItem(json, "start-mode"));
if (value != NULL) { if (value != NULL) {
service->startMode = GetMapValue(value, 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")); value = cJSON_GetStringValue(cJSON_GetObjectItem(json, "end-mode"));
if (value != NULL) { if (value != NULL) {
......
...@@ -273,7 +273,7 @@ static void BootStateChange(const char *content) ...@@ -273,7 +273,7 @@ static void BootStateChange(const char *content)
return; return;
} }
if (strcmp("post-init", content) == 0) { if (strcmp("post-init", content) == 0) {
StartAllServices(START_MODE_NARMAL); StartAllServices(START_MODE_NORMAL);
// Destroy all hooks // Destroy all hooks
HookMgrDestroy(bootStageHookMgr); HookMgrDestroy(bootStageHookMgr);
bootStageHookMgr = NULL; bootStageHookMgr = NULL;
......
...@@ -36,19 +36,16 @@ ...@@ -36,19 +36,16 @@
void NotifyServiceChange(Service *service, int status) void NotifyServiceChange(Service *service, int status)
{ {
int size = 0; INIT_LOGI("NotifyServiceChange %s %d to %d", service->name, service->status, status);
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);
service->status = status; service->status = status;
INIT_CHECK(status != SERVICE_IDLE, return); INIT_CHECK(status != SERVICE_IDLE, return);
char paramName[PARAM_NAME_LEN_MAX] = { 0 }; char paramName[PARAM_NAME_LEN_MAX] = { 0 };
int ret = snprintf_s(paramName, sizeof(paramName), sizeof(paramName) - 1, int ret = snprintf_s(paramName, sizeof(paramName), sizeof(paramName) - 1,
"%s.%s", STARTUP_SERVICE_CTL, service->name); "%s.%s", STARTUP_SERVICE_CTL, service->name);
if (ret >= 0) { char statusStr[MAX_INT_LEN] = {0};
SystemWriteParam(paramName, statusMap[status].name); int ret1 = snprintf_s(statusStr, sizeof(statusStr), sizeof(statusStr) - 1, "%d", status);
if (ret >= 0 && ret1 > 0) {
SystemWriteParam(paramName, statusStr);
} }
} }
......
...@@ -522,40 +522,6 @@ int StringReplaceChr(char *strl, char oldChr, char newChr) ...@@ -522,40 +522,6 @@ int StringReplaceChr(char *strl, char oldChr, char newChr)
return 0; 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},
{"critial", SERVICE_CRITIAL}
};
const InitArgInfo *GetServieStatusMap(int *size)
{
if (size != 0) {
*size = ARRAY_LENGTH(g_servieStatusMap);
}
return g_servieStatusMap;
}
uint32_t GetRandom() uint32_t GetRandom()
{ {
uint32_t ulSeed = 0; uint32_t ulSeed = 0;
......
...@@ -7,15 +7,15 @@ ...@@ -7,15 +7,15 @@
] ]
}, },
{ {
"name" : "param:startup.service.ctl.group-test-stage1=running", "name" : "param:startup.service.ctl.group-test-stage1=2",
"condition" : "startup.service.ctl.group-test-stage1=running", "condition" : "startup.service.ctl.group-test-stage1=2",
"cmds" : [ "cmds" : [
"start group-test-stage2" "start group-test-stage2"
] ]
}, },
{ {
"name" : "param:startup.service.ctl.group-test-stage2=running", "name" : "param:startup.service.ctl.group-test-stage2=2",
"condition" : "startup.service.ctl.group-test-stage2=running", "condition" : "startup.service.ctl.group-test-stage2=2",
"cmds" : [ "cmds" : [
"start group-test-stage3" "start group-test-stage3"
] ]
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "init_param.h" #include "init_param.h"
#include "init_utils.h"
#include "parameter.h" #include "parameter.h"
#include "sysparam_errno.h" #include "sysparam_errno.h"
#include "param_comm.h" #include "param_comm.h"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册