提交 726c2794 编写于 作者: C cheng_jinsong

使用独立的job启动服务

Signed-off-by: Ncheng_jinsong <chengjinsong2@huawei.com>
上级 2f868912
......@@ -253,7 +253,7 @@ if (defined(ohos_lite)) {
external_deps = [
"c_utils:utils",
"hilog_native:libhilog_base",
"ipc:ipc_core",
"ipc:ipc_single",
"samgr:samgr_proxy",
]
public_configs = [ ":exported_header_files" ]
......
......@@ -24,9 +24,11 @@ const.build. = root:root:0775
const.SystemCapability. = root:root:0775
const.product. = root:root:0775
persist.init. = root:root:0775
appspawn. = root:root:0750
startup.uevent. = root:root:0775
ohos.dev. = ueventd:ueventd:0775
startup.appspawn. = root:root:0750
startup.uevent. = ueventd:ueventd:0775
persist.init.debug. = root:root:0775:int
persist.appspawn. = root:root:0775
#udid and sn, only read
ohos.boot.sn = root:deviceprivate:0750
......
......@@ -139,7 +139,7 @@ void DoJobExecNow(const char *triggerName);
*/
int AddCompleteJob(const char *name, const char *condition, const char *cmdContent);
void RegisterBootStateChange(void (*bootStateChange)(const char *));
void RegisterBootStateChange(void (*bootStateChange)(int start, const char *));
/**
* 对外接口
......
......@@ -58,7 +58,6 @@ void StopAllServices(int flags, const char **exclude, int size,
int (*filter)(const Service *service, const char **exclude, int size));
void ParseAllServices(const cJSON *fileRoot);
void ReleaseService(Service *service);
void StartAllServices(int startMode);
void LoadAccessTokenId(void);
Service *AddService(const char *name);
void DumpServiceHookExecute(const char *name, const char *info);
......
......@@ -997,7 +997,7 @@ void StopAllServices(int flags, const char **exclude, int size,
int (*filter)(const Service *service, const char **exclude, int size))
{
Service *service = GetServiceByName("appspawn");
if (service != NULL && service->pid != -1) { // notify appspawn stop
if (service != NULL && service->pid > 0) { // notify appspawn stop
#ifndef STARTUP_INIT_TEST
kill(service->pid, SIGTERM);
waitpid(service->pid, 0, 0);
......@@ -1049,31 +1049,6 @@ Service *GetServiceByName(const char *servName)
return NULL;
}
void StartAllServices(int startMode)
{
INIT_LOGI("StartAllServices %d", startMode);
InitGroupNode *node = GetNextGroupNode(NODE_TYPE_SERVICES, NULL);
while (node != NULL) {
Service *service = node->data.service;
if (service == NULL || service->startMode != startMode) {
node = GetNextGroupNode(NODE_TYPE_SERVICES, node);
continue;
}
if (IsOnDemandService(service)) {
if (CreateServiceSocket(service) != 0) {
INIT_LOGE("service %s exit! create socket failed!", service->name);
}
node = GetNextGroupNode(NODE_TYPE_SERVICES, node);
continue;
}
if (ServiceStart(service) != SERVICE_SUCCESS) {
INIT_LOGE("Service %s start failed!", service->name);
}
node = GetNextGroupNode(NODE_TYPE_SERVICES, node);
}
INIT_LOGI("StartAllServices %d finish", startMode);
}
void LoadAccessTokenId(void)
{
GetAccessToken();
......
......@@ -16,6 +16,7 @@
#include <errno.h>
#include <poll.h>
#include <stdarg.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
......@@ -47,6 +48,10 @@
#include "bootstage.h"
static bool g_enableSandbox;
typedef struct HOOK_TIMING_STAT {
struct timespec startTime;
struct timespec endTime;
} HOOK_TIMING_STAT;
static int FdHolderSockInit(void)
{
......@@ -246,16 +251,27 @@ HOOK_MGR *GetBootStageHookMgr()
return bootStageHookMgr;
}
static void BootStateChange(const char *content)
HOOK_TIMING_STAT g_bootJob = {0};
static long long InitDiffTime(HOOK_TIMING_STAT *stat)
{
INIT_LOGI("boot start %s finish.", content);
if (strcmp("init", content) == 0) {
StartAllServices(START_MODE_BOOT);
return;
long long diff = (long long)((stat->endTime.tv_sec - stat->startTime.tv_sec) * 1000000); // 1000000 1000ms
if (stat->endTime.tv_nsec > stat->startTime.tv_nsec) {
diff += (stat->endTime.tv_nsec - stat->startTime.tv_nsec) / 1000; // 1000 ms
} else {
diff -= (stat->endTime.tv_nsec - stat->startTime.tv_nsec) / 1000; // 1000 ms
}
if (strcmp("post-init", content) == 0) {
StartAllServices(START_MODE_NORMAL);
return;
return diff;
}
static void BootStateChange(int start, const char *content)
{
if (start == 0) {
clock_gettime(CLOCK_MONOTONIC, &(g_bootJob.startTime));
INIT_LOGI("boot job %s start.", content);
} else {
clock_gettime(CLOCK_MONOTONIC, &(g_bootJob.endTime));
long long diff = InitDiffTime(&g_bootJob);
INIT_LOGI("boot job %s finish diff %lld us.", content, diff);
}
}
......@@ -296,11 +312,6 @@ static void InitLoadParamFiles(void)
FreeCfgFiles(files);
}
typedef struct HOOK_TIMING_STAT {
struct timespec startTime;
struct timespec endTime;
} HOOK_TIMING_STAT;
static void InitPreHook(const HOOK_INFO *hookInfo, void *executionContext)
{
HOOK_TIMING_STAT *stat = (HOOK_TIMING_STAT *)executionContext;
......@@ -309,20 +320,51 @@ static void InitPreHook(const HOOK_INFO *hookInfo, void *executionContext)
static void InitPostHook(const HOOK_INFO *hookInfo, void *executionContext, int executionRetVal)
{
long long diff;
const long long baseTime = 1000;
HOOK_TIMING_STAT *stat = (HOOK_TIMING_STAT *)executionContext;
clock_gettime(CLOCK_MONOTONIC, &(stat->endTime));
long long diff = InitDiffTime(stat);
INIT_LOGI("Executing hook [%d:%d:%p] cost [%lld]us, return %d.",
hookInfo->stage, hookInfo->prio, hookInfo->hook, diff, executionRetVal);
}
diff = (long long)((stat->endTime.tv_sec - stat->startTime.tv_sec) / baseTime);
if (stat->endTime.tv_nsec > stat->startTime.tv_nsec) {
diff += (stat->endTime.tv_nsec - stat->startTime.tv_nsec) * baseTime;
} else {
diff -= (stat->endTime.tv_nsec - stat->startTime.tv_nsec) * baseTime;
static void TriggerServices(int startMode)
{
int index = 0;
int jobNum = 0;
char jobName[64] = {0}; // 64 job name
char cmd[64] = {0}; // 64 job name
const int maxServiceInJob = 4; // 4 service in job
InitGroupNode *node = GetNextGroupNode(NODE_TYPE_SERVICES, NULL);
while (node != NULL) {
Service *service = node->data.service;
if (service == NULL || service->startMode != startMode) {
node = GetNextGroupNode(NODE_TYPE_SERVICES, node);
continue;
}
if (IsOnDemandService(service)) {
if (CreateServiceSocket(service) != 0) {
INIT_LOGE("service %s exit! create socket failed!", service->name);
}
node = GetNextGroupNode(NODE_TYPE_SERVICES, node);
continue;
}
if (index == 0) {
sprintf_s(jobName, sizeof(jobName), "boot-service:service-%d-%03d", startMode, jobNum);
jobNum++;
}
index++;
sprintf_s(cmd, sizeof(cmd), "start %s", service->name);
AddCompleteJob(jobName, NULL, cmd);
INIT_LOGV("Add %s to job %s", service->name, jobName);
if (index == maxServiceInJob) {
PostTrigger(EVENT_TRIGGER_BOOT, jobName, strlen(jobName));
index = 0;
}
node = GetNextGroupNode(NODE_TYPE_SERVICES, node);
}
if (index > 0) {
PostTrigger(EVENT_TRIGGER_BOOT, jobName, strlen(jobName));
}
INIT_LOGV("Executing hook [%d:%d:%p] cost [%lld]ms, return %d.",
hookInfo->stage, hookInfo->prio, hookInfo->hook, diff, executionRetVal);
}
void SystemConfig(void)
......@@ -342,6 +384,7 @@ void SystemConfig(void)
InitParseGroupCfg();
RegisterBootStateChange(BootStateChange);
INIT_LOGI("boot init finish.");
// load SELinux context and policy
// Do not move position!
PluginExecCmdByName("loadSelinuxPolicy", "");
......@@ -354,14 +397,17 @@ void SystemConfig(void)
// read config
HookMgrExecute(GetBootStageHookMgr(), INIT_PRE_CFG_LOAD, (void *)&timingStat, (void *)&options);
ReadConfig();
INIT_LOGI("Parse init config file done.");
INIT_LOGI("boot parse config file done.");
HookMgrExecute(GetBootStageHookMgr(), INIT_POST_CFG_LOAD, (void *)&timingStat, (void *)&options);
IsEnableSandbox();
// execute init
PostTrigger(EVENT_TRIGGER_BOOT, "pre-init", strlen("pre-init"));
PostTrigger(EVENT_TRIGGER_BOOT, "init", strlen("init"));
TriggerServices(START_MODE_BOOT);
PostTrigger(EVENT_TRIGGER_BOOT, "post-init", strlen("post-init"));
TriggerServices(START_MODE_NORMAL);
clock_gettime(CLOCK_MONOTONIC, &(g_bootJob.startTime));
}
void SystemRun(void)
......
......@@ -13,7 +13,7 @@
* limitations under the License.
*/
#include "le_task.h"
#include <time.h>
#include <sys/eventfd.h>
#include "le_loop.h"
......@@ -21,19 +21,15 @@
static void DoAsyncEvent_(const LoopHandle loopHandle, AsyncEventTask *asyncTask)
{
LE_CHECK(loopHandle != NULL && asyncTask != NULL, return, "Invalid parameters");
ListNode tmpHdr;
OH_ListInit(&tmpHdr);
#ifdef LOOP_DEBUG
struct timespec startTime;
struct timespec endTime;
long long diff;
clock_gettime(CLOCK_MONOTONIC, &(startTime));
#endif
StreamTask *task = &asyncTask->stream;
LoopMutexLock(&task->mutex);
tmpHdr.next = task->buffHead.next;
tmpHdr.prev = task->buffHead.prev;
task->buffHead.next->prev = &tmpHdr;
task->buffHead.prev->next = &tmpHdr;
OH_ListInit(&task->buffHead);
LoopMutexUnlock(&task->mutex);
ListNode *node = tmpHdr.next;
while (node != &tmpHdr) {
ListNode *node = task->buffHead.next;
while (node != &task->buffHead) {
LE_Buffer *buffer = ListEntry(node, LE_Buffer, node);
uint64_t eventId = *(uint64_t*)(buffer->data);
if (asyncTask->processAsyncEvent) {
......@@ -42,13 +38,27 @@ static void DoAsyncEvent_(const LoopHandle loopHandle, AsyncEventTask *asyncTask
}
OH_ListRemove(&buffer->node);
free(buffer);
node = tmpHdr.next;
#ifdef LOOP_DEBUG
clock_gettime(CLOCK_MONOTONIC, &(endTime));
diff = (long long)((endTime.tv_sec - startTime.tv_sec) * 1000000);
if (endTime.tv_nsec > startTime.tv_nsec) {
diff += (endTime.tv_nsec - startTime.tv_nsec) / 1000; // 1000 ms
} else {
diff -= (endTime.tv_nsec - startTime.tv_nsec) / 1000; // 1000 ms
}
LE_LOGI("DoAsyncEvent_ diff %ld", diff);
#endif
break;
}
}
#ifdef STARTUP_INIT_TEST
void LE_DoAsyncEvent(const LoopHandle loopHandle, const TaskHandle taskHandle)
{
DoAsyncEvent_(loopHandle, (AsyncEventTask *)taskHandle);
AsyncEventTask *asyncTask = (AsyncEventTask *)taskHandle;
while (!IsBufferEmpty(&asyncTask->stream)) {
DoAsyncEvent_(loopHandle, (AsyncEventTask *)taskHandle);
}
}
#endif
static LE_STATUS HandleAsyncEvent_(const LoopHandle loopHandle, const TaskHandle taskHandle, uint32_t oper)
......@@ -61,8 +71,8 @@ static LE_STATUS HandleAsyncEvent_(const LoopHandle loopHandle, const TaskHandle
int ret = read(GetSocketFd(taskHandle), &eventId, sizeof(eventId));
LE_LOGV("HandleAsyncEvent_ read fd:%d ret: %d eventId %llu", GetSocketFd(taskHandle), ret, eventId);
DoAsyncEvent_(loopHandle, asyncTask);
if (IsBufferEmpty(&asyncTask->stream)) {
loop->modEvent(loop, (const BaseTask *)taskHandle, Event_Read);
if (!IsBufferEmpty(&asyncTask->stream)) {
loop->modEvent(loop, (const BaseTask *)taskHandle, Event_Write);
return LE_SUCCESS;
}
} else {
......
......@@ -160,7 +160,7 @@ typedef struct TriggerWorkSpace_ {
TriggerHeader triggerHead[TRIGGER_MAX];
HashMapHandle hashMap;
ParamTaskPtr eventHandle;
void (*bootStateChange)(const char *);
void (*bootStateChange)(int start, const char *);
char cache[PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX];
} TriggerWorkSpace;
......
......@@ -330,7 +330,7 @@ INIT_LOCAL_API int CheckParameterSet(const char *name,
}
#if !(defined __LITEOS_A__ || defined __LITEOS_M__)
// do hook cmd
PARAM_LOGI("CheckParameterSet realKey %s cmd: '%s' value: %s",
PARAM_LOGV("CheckParameterSet realKey %s cmd: '%s' value: %s",
serviceInfo->realKey, serviceInfo->cmdName, (char *)serviceInfo->realKey + serviceInfo->valueOffset);
DoCmdByName(serviceInfo->cmdName, (char *)serviceInfo->realKey + serviceInfo->valueOffset);
#endif
......
......@@ -123,7 +123,9 @@ static TriggerNode *AddJobTrigger_(const TriggerWorkSpace *workSpace,
return NULL, "Failed to add hash node");
if (extInfo->type == TRIGGER_BOOT) {
TRIGGER_SET_FLAG(node, TRIGGER_FLAGS_ONCE);
TRIGGER_SET_FLAG(node, TRIGGER_FLAGS_SUBTRIGGER);
if (strncmp("boot-service:", extInfo->info.name, strlen("boot-service:")) != 0) {
TRIGGER_SET_FLAG(node, TRIGGER_FLAGS_SUBTRIGGER);
}
}
return node;
}
......
......@@ -44,7 +44,7 @@ static int DoTriggerExecute_(const TriggerNode *trigger, const char *content, ui
return 0;
}
static int DoTiggerCheckResult(TriggerNode *trigger, const char *content, uint32_t size)
static int DoTriggerCheckResult(TriggerNode *trigger, const char *content, uint32_t size)
{
UNUSED(content);
UNUSED(size);
......@@ -58,10 +58,10 @@ static int DoTiggerCheckResult(TriggerNode *trigger, const char *content, uint32
return 0;
}
static int ExecuteTiggerImmediately(TriggerNode *trigger, const char *content, uint32_t size)
static int ExecuteTriggerImmediately(TriggerNode *trigger, const char *content, uint32_t size)
{
PARAM_CHECK(trigger != NULL, return -1, "Invalid trigger");
PARAM_LOGV("ExecuteTiggerImmediately trigger %s", GetTriggerName(trigger));
PARAM_LOGV("ExecuteTriggerImmediately trigger %s", GetTriggerName(trigger));
TriggerHeader *triggerHead = GetTriggerHeader(&g_triggerWorkSpace, trigger->type);
if (triggerHead != NULL) {
triggerHead->executeTrigger(trigger, content, size);
......@@ -74,17 +74,17 @@ static int ExecuteTiggerImmediately(TriggerNode *trigger, const char *content, u
return 0;
}
static void StartTiggerExecute_(TriggerNode *trigger, const char *content, uint32_t size)
static void StartTriggerExecute_(TriggerNode *trigger, const char *content, uint32_t size)
{
TriggerHeader *triggerHead = GetTriggerHeader(&g_triggerWorkSpace, trigger->type);
if (triggerHead != NULL) {
PARAM_LOGV("StartTiggerExecute_ trigger %s flags:0x%04x",
PARAM_LOGV("StartTriggerExecute_ trigger %s flags:0x%04x",
GetTriggerName(trigger), trigger->flags);
triggerHead->executeTrigger(trigger, content, size);
TRIGGER_CLEAR_FLAG(trigger, TRIGGER_FLAGS_QUEUE);
if (TRIGGER_TEST_FLAG(trigger, TRIGGER_FLAGS_SUBTRIGGER)) { // boot && xxx=xxx trigger
const char *condition = triggerHead->getCondition(trigger);
CheckTrigger(&g_triggerWorkSpace, TRIGGER_UNKNOW, condition, strlen(condition), ExecuteTiggerImmediately);
CheckTrigger(&g_triggerWorkSpace, TRIGGER_UNKNOW, condition, strlen(condition), ExecuteTriggerImmediately);
}
if (TRIGGER_TEST_FLAG(trigger, TRIGGER_FLAGS_ONCE)) {
FreeTrigger(&g_triggerWorkSpace, trigger);
......@@ -97,7 +97,7 @@ static void ExecuteQueueWork(uint32_t maxCount)
uint32_t executeCount = 0;
TriggerNode *trigger = ExecuteQueuePop(&g_triggerWorkSpace);
while (trigger != NULL) {
StartTiggerExecute_(trigger, NULL, 0);
StartTriggerExecute_(trigger, NULL, 0);
executeCount++;
if (executeCount > maxCount) {
break;
......@@ -113,27 +113,30 @@ PARAM_STATIC void ProcessBeforeEvent(const ParamTaskPtr stream,
switch (eventId) {
case EVENT_TRIGGER_PARAM: {
CheckTrigger(&g_triggerWorkSpace, TRIGGER_PARAM,
(const char *)content, size, DoTiggerCheckResult);
(const char *)content, size, DoTriggerCheckResult);
ExecuteQueueWork(MAX_TRIGGER_COUNT_RUN_ONCE);
break;
}
case EVENT_TRIGGER_BOOT: {
if (g_triggerWorkSpace.bootStateChange != NULL) {
g_triggerWorkSpace.bootStateChange(0, (const char *)content);
}
CheckTrigger(&g_triggerWorkSpace, TRIGGER_BOOT,
(const char *)content, size, DoTiggerCheckResult);
(const char *)content, size, DoTriggerCheckResult);
ExecuteQueueWork(MAX_TRIGGER_COUNT_RUN_ONCE);
if (g_triggerWorkSpace.bootStateChange != NULL) {
g_triggerWorkSpace.bootStateChange((const char *)content);
g_triggerWorkSpace.bootStateChange(1, (const char *)content);
}
break;
}
case EVENT_TRIGGER_PARAM_WAIT: {
CheckTrigger(&g_triggerWorkSpace, TRIGGER_PARAM_WAIT,
(const char *)content, size, ExecuteTiggerImmediately);
(const char *)content, size, ExecuteTriggerImmediately);
break;
}
case EVENT_TRIGGER_PARAM_WATCH: {
CheckTrigger(&g_triggerWorkSpace, TRIGGER_PARAM_WATCH,
(const char *)content, size, ExecuteTiggerImmediately);
(const char *)content, size, ExecuteTriggerImmediately);
break;
}
default:
......@@ -174,6 +177,9 @@ static int GetTriggerType(const char *type)
if (strncmp("param:", type, strlen("param:")) == 0) {
return TRIGGER_PARAM;
}
if (strncmp("boot-service:", type, strlen("boot-service:")) == 0) {
return TRIGGER_BOOT;
}
const char *triggerTypeStr[] = {
"pre-init", "boot", "early-init", "init", "early-init", "late-init", "post-init",
"fs", "early-fs", "post-fs", "late-fs", "early-boot", "post-fs-data", "reboot", "suspend"
......@@ -375,14 +381,13 @@ void DoJobExecNow(const char *triggerName)
PARAM_CHECK(triggerName != NULL, return, "Invalid param");
JobNode *trigger = GetTriggerByName(&g_triggerWorkSpace, triggerName);
if (trigger != NULL) {
StartTiggerExecute_((TriggerNode *)trigger, NULL, 0);
StartTriggerExecute_((TriggerNode *)trigger, NULL, 0);
}
}
int AddCompleteJob(const char *name, const char *condition, const char *cmdContent)
{
PARAM_CHECK(name != NULL, return -1, "Invalid name");
PARAM_CHECK(condition != NULL, return -1, "Invalid condition");
PARAM_CHECK(cmdContent != NULL, return -1, "Invalid cmdContent");
int type = GetTriggerType(name);
PARAM_CHECK(type <= TRIGGER_UNKNOW, return -1, "Failed to get trigger index");
......@@ -402,7 +407,7 @@ int AddCompleteJob(const char *name, const char *condition, const char *cmdConte
return 0;
}
void RegisterBootStateChange(void (*bootStateChange)(const char *))
void RegisterBootStateChange(void (*bootStateChange)(int, const char *))
{
if (bootStateChange != NULL) {
g_triggerWorkSpace.bootStateChange = bootStateChange;
......
......@@ -169,7 +169,6 @@ HWTEST_F(InitGroupManagerUnitTest, TestInitGroupMgrInit, TestSize.Level1)
EXPECT_EQ(1, 0);
} // test read cfgfile
int ret = InitParseGroupCfg();
StartAllServices(GROUP_CHARGE);
EXPECT_EQ(ret, 0);
}
......
......@@ -34,7 +34,7 @@ static const int triggerBuffer = 512;
static uint32_t g_execCmdId = 0;
static int g_matchTrigger = 0;
static char g_matchTriggerName[triggerBuffer] = { 0 };
static void BootStateChange(const char *content)
static void BootStateChange(int start, const char *content)
{
UNUSED(content);
return;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册